o
    Ø ÔQ$  ã                   @   sš   d Z ddlZddlZddlZddlZze W n ey#   dd„ ZY nw ejdk r4e d¡Z	e d¡Z
n
e d¡Z	e d	¡Z
dd
d„ZG dd„ deƒZdS )z-Refactored 'safe reference from dispatcher.pyé    Nc                 C   s
   t | dƒS )NÚ__call__)Úhasattr)Úobject© r   ú2/usr/lib/python3/dist-packages/blinker/_saferef.pyÚcallable.   s   
r   )é   Úim_selfÚim_funcÚ__self__Ú__func__c                 C   s~   zt | ƒ}W n ty    t|ƒrt | |¡ Y S t | ¡ Y S w |dur=t| dƒs5t| dƒs5J d|  ƒ‚t| |d�}|S dS )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.
    Nr
   r   zRsafe_ref target %r has im_self, but no im_func, don't know how to create reference)ÚtargetÚ	on_delete)Úget_selfÚAttributeErrorr   ÚweakrefÚrefr   ÚBoundMethodWeakref)r   r   r	   Ú	referencer   r   r   Úsafe_ref:   s    üÿÿùr   c                       sh   e Zd ZdZe ¡ Zd‡ fdd„	Zddd„Zdd„ Z	e
e	ƒZ	d	d
„ ZeZdd„ Zdd„ Zdd„ Z‡  ZS )r   a‰  '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.
    Nc                    sf   |   |¡}| j |¡}|dur|j |¡ |S tt| ƒ | ¡}|| j|< |j||g|¢R i |¤Ž |S )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Úsuperr   Ú__new__Ú__init__)Úclsr   r   Ú	argumentsÚnamedÚkeyÚcurrentÚbase©Ú	__class__r   r   r   |   s   

zBoundMethodWeakref.__new__c                 C   sf   | fdd„}|g| _ |  |¡| _t|ƒ}t|ƒ}t ||¡| _t ||¡| _t	|ƒ| _
t	|jƒ| _dS )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.
        c                 S   s°   |j dd… }|j dd…= z|jj|j= W n	 ty   Y nw |D ]4}z
t|ƒr,||ƒ W q! tyU   zt ¡  W n t	yR   t
 ¡ d }td|||f ƒ Y nw Y q!w dS )z=Set self.isDead to True when method or instance is destroyed.Né   z3Exception during saferef %s cleanup function %s: %s)r   r%   r   r!   ÚKeyErrorr   Ú	ExceptionÚ	tracebackÚ	print_excr   ÚsysÚexc_infoÚprint)ÚweakÚselfÚmethodsÚfunctionÚer   r   r   Úremove¢   s0   ÿ€
ÿþ€ýüz+BoundMethodWeakref.__init__.<locals>.removeN)r   r   r!   r   Úget_funcr   r   Ú	weak_selfÚ	weak_funcÚstrÚ	self_nameÚ__name__Ú	func_name)r/   r   r   r3   r	   r
   r   r   r   r   ’   s   
zBoundMethodWeakref.__init__c                 C   s   t t|ƒƒt t|ƒƒfS )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.
        )Úidr   r4   )r   r   r   r   r   r   ¾   s   z BoundMethodWeakref.calculate_keyc                 C   s   d| j j| j| jf S )z-Give a friendly representation of the object.z	%s(%s.%s))r%   r9   r8   r:   ©r/   r   r   r   Ú__str__Ç   s
   ýzBoundMethodWeakref.__str__c                 C   s
   | ƒ duS )z'Whether we are still a valid reference.Nr   r<   r   r   r   Ú__nonzero__Ñ   s   
zBoundMethodWeakref.__nonzero__c                 C   s*   t || jƒst| jt|ƒƒS t| j|jƒS )zCompare with another reference.)Ú
isinstancer%   ÚcmpÚtyper!   )r/   Úotherr   r   r   Ú__cmp__Õ   s   zBoundMethodWeakref.__cmp__c                 C   s.   |   ¡ }|dur|  ¡ }|dur| |¡S dS )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)r5   r6   Ú__get__)r/   r   r1   r   r   r   r   Û   s   

zBoundMethodWeakref.__call__©N)r9   Ú
__module__Ú__qualname__Ú__doc__r   ÚWeakValueDictionaryr   r   r   r   Úclassmethodr=   Ú__repr__r>   rC   r   Ú__classcell__r   r   r$   r   r   X   s    !
,r   rE   )rH   Úoperatorr+   r)   r   r   Ú	NameErrorÚversion_infoÚ
attrgetterr   r4   r   r   r   r   r   r   r   Ú<module>   s"   "ÿ




