o
    q>e#                     @   sT   d Z ddlZddlZddlZddlZedZedZd	ddZG dd dZ	dS )
z-Refactored 'safe reference from dispatcher.py    N__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s7t| ds7J d| 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.
    Nim_funcr   zsafe_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BoundMethodWeakref)r   r   im_self	reference r   Q/var/www/bmteknikk.ddns.net/venv/lib/python3.10/site-packages/blinker/_saferef.pysafe_ref.   s   
r   c                       sl   e Zd ZdZe Zd fdd	ZdddZe	dd Z
d	d
 ZeZdd 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                    sb   |  |}| j|}|dur|j| |S 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super__new____init__)clsr   r   	argumentsnamedkeycurrentbase	__class__r   r   r   q   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 ]8}z
t|r,|| W q! tyY   zt  W n t	yV   t
 d }td| d| d|  Y nw Y q!w dS )z=Set self.isDead to True when method or instance is destroyed.N   zException during saferef z cleanup function z: )r   r"   r   r   KeyErrorr	   	Exception	traceback	print_excr   sysexc_infoprint)weakselfmethodsfunctioner   r   r   remove   s8   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   r0   r   r   r   r   r   r      s   
zBoundMethodWeakref.__init__c                 C   s   t t|t t|fS )zCalculate 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   r1   )r   r   r   r   r   r      s   z BoundMethodWeakref.calculate_keyc                 C   s   d | jj| j| jS )z-Give a friendly representation of the object.z	{}({}.{}))formatr"   r6   r5   r7   r,   r   r   r   __str__   s
   zBoundMethodWeakref.__str__c                 C   s   t | j| jfS N)hashr5   r   r:   r   r   r   __hash__   s   zBoundMethodWeakref.__hash__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"   operatoreqtyper   )r,   otherr   r   r   __eq__   s   zBoundMethodWeakref.__eq__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)r2   r3   __get__)r,   r   r.   r   r   r   __call__   s   

zBoundMethodWeakref.__call__r<   )r6   
__module____qualname____doc__r
   WeakValueDictionaryr   r   r   classmethodr   r;   __repr__r>   r?   rE   rG   __classcell__r   r   r!   r   r   M   s    !
0
r   r<   )
rJ   rA   r(   r&   r
   
attrgetterr   r1   r   r   r   r   r   r   <module>   s   "


