ó
P'—^c           @@  s›   d  Z  d d l m Z d d l Z d d l Z d d l Z d g Z e j d d k Z d d „ Z
 d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d „  Z d S(   s«   
"Safe weakrefs", originally from pyDispatcher.

Provides a way to safely weakref any function, including bound methods (which
aren't handled by the core weakref module).
i    (   t   absolute_importNt   safe_refi   c         C@  s|   t  |  d d ƒ d k	 rO t |  d ƒ s< t d j |  ƒ ƒ ‚ t d |  d | ƒ St | ƒ rk t j |  | ƒ St j |  ƒ Sd S(   s  Return a *safe* weak reference to a callable target

    :param target: the object to be weakly referenced, if it's a
        bound method reference, will create a :class:`BoundMethodWeakref`,
        otherwise creates a simple :class:`weakref.ref`.

    :keyword 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
        :class:`weakref.ref` or a :class:`BoundMethodWeakref`) as argument.
    t   __self__t   __func__sc   safe_ref target {0!r} has __self__, but no __func__:             don't know how to create referencet   targett	   on_deleteN(	   t   getattrt   Nonet   hasattrt   AssertionErrort   formatt   get_bound_method_weakreft   callablet   weakreft   ref(   R   R   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR      s    t   BoundMethodWeakrefc           B@  s€   e  Z d  Z e j ƒ  Z d d „ Z d d „ Z d „  Z	 e
 e	 ƒ Z	 d „  Z e Z d „  Z e Z e su d „  Z n  d „  Z RS(	   sõ  '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.

    .. attribute:: key

        the identity key for the reference, calculated
        by the class's :meth:`calculate_key` method applied to the
        target instance method

    .. attribute:: 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 :func:`safe_ref` calls.

    .. attribute:: weak_self

        weak reference to the target object

    .. attribute:: weak_fun

        weak reference to the target function

    .. attribute:: _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.

    c         O@  s„   |  j  | ƒ } |  j j | ƒ } | d k	 rA | j j | ƒ | St t |  ƒ j |  ƒ } | |  j | <| j	 | | | | Ž | Sd S(   sý  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
        deletionMethods attribute updated.  Otherwise the
        new instance is created and registered in the table
        of already-referenced methods.

        N(
   t   calculate_keyt   _all_instancest   getR   t   deletion_methodst   appendt   superR   t   __new__t   __init__(   t   clsR   R   t	   argumentst   namedt   keyt   currentt   base(    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR   \   s    c         C@  s…   |  d „ } | g |  _  |  j | ƒ |  _ t j | j | ƒ |  _ t j | j | ƒ |  _ t	 | j ƒ |  _
 t	 | j j ƒ |  _ d S(   s’  Return a weak-reference-like instance for a bound method

        :param target: the instance-method target for the weak
            reference, must have `__self__` and `__func__` attributes
            and be reconstructable via::

                target.__func__.__get__(target.__self__)

            which is true of built-in instance methods.

        :keyword 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  } | j  2y | j j | j =Wn t k
 r8 n Xxw | D]o } y t | ƒ rb | | ƒ n  Wq@ t k
 r® } y t j ƒ  Wq¯ t	 k
 rª d j
 | | | ƒ GHq¯ Xq@ Xq@ Wd S(   s=   Set self.is_dead to true when method or instance is destroyeds6   Exception during saferef {0} cleanup function {1}: {2}N(   R   t	   __class__R   R   t   KeyErrorR   t	   Exceptiont	   tracebackt	   print_exct   AttributeErrorR
   (   t   weakt   selft   methodst   functiont   exc(    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyt   remove†   s     
N(   R   R   R   R   R   R   t	   weak_selfR   t   weak_funt   strt	   self_namet   __name__t   fun_name(   R%   R   R   R)   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR   t   s    c         C@  s   t  | j ƒ t  | j ƒ f S(   s±   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.
        (   t   idR   R   (   R   R   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR       s    c         C@  s"   d j  t |  ƒ j |  j |  j ƒ S(   s,   Give a friendly representation of the objects   {0}( {1}.{2} )(   R
   t   typeR.   R-   R/   (   R%   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyt   __str__©   s    c         C@  s   |  ƒ  d k	 S(   s&   Whether we are still a valid referenceN(   R   (   R%   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyt   __bool__³   s    c         C@  s;   t  | |  j ƒ s( t |  j t | ƒ ƒ St |  j | j ƒ S(   s   Compare with another reference(   t
   isinstanceR   t   cmpR1   R   (   R%   t   other(    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyt   __cmp__¹   s    c         C@  sD   |  j  ƒ  } | d k	 r@ |  j ƒ  } | d k	 r@ | j | ƒ Sn  d S(   sM  Return a strong reference to the bound method

        If the target cannot be retrieved, then will
        return None, otherwise return 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(   R*   R   R+   t   __get__(   R%   R   R'   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyt   __call__¿   s
    N(   R.   t
   __module__t   __doc__R   t   WeakValueDictionaryR   R   R   R   R   t   classmethodR2   t   __repr__R3   t   __nonzero__t   PY3R7   R9   (    (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR   -   s   +,			t   BoundNonDescriptorMethodWeakrefc           B@  s#   e  Z d  Z d d „ Z d „  Z RS(   sá  A specialized :class:`BoundMethodWeakref`, for platforms where
    instance methods are not descriptors.

    It assumes that the function name and the target attribute name are the
    same, instead of assuming that the function is a descriptor. This approach
    is equally fast, but not 100% reliable because functions can be stored on
    an attribute named differenty than the function's name such as in::

        >>> class A(object):
        ...     pass

        >>> def foo(self):
        ...     return 'foo'
        >>> A.bar = foo

    But this shouldn't be a common use case. So, on platforms where methods
    aren't descriptors (such as Jython) this implementation has the advantage
    of working in the most cases.

    c         C@  s>   t  | j | j ƒ | k s! t ‚ t t |  ƒ j | | ƒ d S(   s‘  Return a weak-reference-like instance for a bound method

        :param target: the instance-method target for the weak
            reference, must have `__self__` and `__func__` attributes
            and be reconstructable via::

                target.__func__.__get__(target.__self__)

            which is true of built-in instance methods.

        :keyword 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.

        N(   R   R   R.   R	   R   RA   R   (   R%   R   R   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR   æ   s    !c         C@  sG   |  j  ƒ  } | d k	 rC |  j ƒ  } | d k	 rC t | | j ƒ Sn  d S(   sN  Return a strong reference to the bound method

        If the target cannot be retrieved, then will
        return None, otherwise return 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(   R*   R   R+   R   R.   (   R%   R   R'   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR9   ü   s
    N(   R.   R:   R;   R   R   R9   (    (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyRA   Ñ   s   c         C@  s9   t  |  d ƒ r" t d |  d | ƒ St d |  d | ƒ Sd S(   s„   Instantiates the appropiate :class:`BoundMethodWeakRef`, depending
    on the details of the underlying class method implementation.R8   R   R   N(   R   R   RA   (   R   R   (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyR     s    (   R;   t
   __future__R    t   sysR!   R   t   __all__t   version_infoR@   R   R   t   objectR   RA   R   (    (    (    s?   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/saferef.pyt   <module>   s   	¤D