zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
U
Þ=®dˆ#ã@sTdZddlZddlZddlZddlZe d¡Ze d¡Zd    dd„ZGdd„dƒZ    dS)
z-Refactored 'safe reference from dispatcher.pyéNÚ__self__Ú__func__cCsˆz t|ƒ}Wn:tk
rFt|ƒr4t ||¡YSt |¡YSYn>X|dk    r„t|dƒstt|dƒsttd|›dƒ‚t||d}|SdS)aÖReturn a *safe* weak reference to a callable target.
 
    - ``target``: The object to be weakly referenced, if it's a bound
      method reference, will create a BoundMethodWeakref, otherwise
      creates a simple weakref.
 
    - ``on_delete``: If provided, will have a hard reference stored to
      the callable to be called after the safe reference goes out of
      scope with the reference object, (either a weakref or a
      BoundMethodWeakref) as argument.
    NÚim_funcrzsafe_ref target z@ has im_self, but no im_func, don't know how to create reference)ÚtargetÚ    on_delete)Úget_selfÚAttributeErrorÚcallableÚweakrefÚrefÚhasattrÚAssertionErrorÚBoundMethodWeakref)rrÚim_selfÚ    reference©rúGd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\blinker/_saferef.pyÚsafe_ref.s  
ÿ rcsleZdZdZe ¡Zd‡fdd„    Zddd„Ze    dd„ƒZ
d    d
„Z e Z d d „Z d d„Zdd„Zdd„Z‡ZS)ra‰'Safe' and reusable weak references to instance methods.
 
    BoundMethodWeakref objects provide a mechanism for referencing a
    bound method without requiring that the method object itself
    (which is normally a transient object) is kept alive.  Instead,
    the BoundMethodWeakref object keeps weak references to both the
    object and the function which together define the instance method.
 
    Attributes:
 
    - ``key``: The identity key for the reference, calculated by the
      class's calculate_key method applied to the target instance method.
 
    - ``deletion_methods``: Sequence of callable objects taking single
      argument, a reference to this object which will be called when
      *either* the target object or target function is garbage
      collected (i.e. when this object becomes invalid).  These are
      specified as the on_delete parameters of safe_ref calls.
 
    - ``weak_self``: Weak reference to the target object.
 
    - ``weak_func``: Weak reference to the target function.
 
    Class Attributes:
 
    - ``_all_instances``: Class attribute pointing to all live
      BoundMethodWeakref objects indexed by the class's
      calculate_key(target) method applied to the target objects.
      This weak value dictionary is used to short-circuit creation so
      that multiple references to the same (object, function) pair
      produce the same BoundMethodWeakref instance.
    Ncs`| |¡}|j |¡}|dk    r.|j |¡|Stƒ |¡}||j|<|j||f|ž|Ž|SdS)aôCreate new instance or return current instance.
 
        Basically this method of construction allows us to
        short-circuit creation of references to already-referenced
        instance methods.  The key corresponding to the target is
        calculated, and if there is already an existing reference,
        that is returned, with its deletion_methods attribute updated.
        Otherwise the new instance is created and registered in the
        table of already-referenced methods.
        N)Ú calculate_keyÚ_all_instancesÚgetÚdeletion_methodsÚappendÚsuperÚ__new__Ú__init__)ÚclsrrÚ    argumentsÚnamedÚkeyÚcurrentÚbase©Ú    __class__rrrqs
 
zBoundMethodWeakref.__new__cCsf|fdd„}|g|_| |¡|_t|ƒ}t|ƒ}t ||¡|_t ||¡|_t    |ƒ|_
t    |j ƒ|_ dS)aƒReturn a weak-reference-like instance for a bound method.
 
        - ``target``: The instance-method target for the weak reference,
          must have im_self and im_func attributes and be
          reconstructable via the following, which is true of built-in
          instance methods::
 
            target.im_func.__get__( target.im_self )
 
        - ``on_delete``: Optional callback which will be called when
          this weak reference ceases to be valid (i.e. either the
          object or the function is garbage collected).  Should take a
          single argument, which will be passed a pointer to this
          object.
        cSsÀ|jdd…}|jdd…=z|jj|j=Wntk
r>YnX|D]v}zt|ƒrZ||ƒWqDtk
r¸z t ¡Wn:t    k
r²t
  ¡d}t d|›d|›d|›ƒYnXYqDXqDdS)z=Set self.isDead to True when method or instance is destroyed.NézException during saferef z cleanup function z: ) rr#rrÚKeyErrorr    Ú    ExceptionÚ    tracebackÚ    print_excrÚsysÚexc_infoÚprint)ZweakÚselfÚmethodsÚfunctionÚerrrÚremove˜s$    ÿz+BoundMethodWeakref.__init__.<locals>.removeN) rrrrÚget_funcr
r Ú    weak_selfÚ    weak_funcÚstrÚ    self_nameÚ__name__Ú    func_name)r,rrr0rrrrrr‡s  
zBoundMethodWeakref.__init__cCstt|ƒƒtt|ƒƒfS)z°Calculate the reference key for this reference.
 
        Currently this is a two-tuple of the id()'s of the target
        object and the target function respectively.
        )Úidrr1)rrrrrr·sz BoundMethodWeakref.calculate_keycCsd |jj|j|j¡S)z-Give a friendly representation of the object.z    {}({}.{}))Úformatr#r6r5r7©r,rrrÚ__str__Às
ýzBoundMethodWeakref.__str__cCst|j|jfƒS)N)Úhashr5rr:rrrÚ__hash__ÊszBoundMethodWeakref.__hash__cCs
|ƒdk    S)z'Whether we are still a valid reference.Nrr:rrrÚ __nonzero__ÍszBoundMethodWeakref.__nonzero__cCs.t||jƒst |jt|ƒ¡St |j|j¡S)zCompare with another reference.)Ú
isinstancer#ÚoperatorÚeqÚtyper)r,ÚotherrrrÚ__eq__Ñs zBoundMethodWeakref.__eq__cCs.| ¡}|dk    r*| ¡}|dk    r*| |¡SdS)a?Return a strong reference to the bound method.
 
        If the target cannot be retrieved, then will return None,
        otherwise returns a bound instance method for our object and
        function.
 
        Note: You may call this method any number of times, as it does
        not invalidate the reference.
        N)r2r3Ú__get__)r,rr.rrrÚ__call__×s
 
zBoundMethodWeakref.__call__)N)N)r6Ú
__module__Ú __qualname__Ú__doc__r
ÚWeakValueDictionaryrrrÚ classmethodrr;Ú__repr__r=r>rDrFÚ __classcell__rrr"rrMs!
0
r)N)
rIr@r)r'r
Ú
attrgetterrr1rrrrrrÚ<module>#s