ó
N`¾Tc           @   sÏ   d  Z  d d l Z d d l Z d d l Z d d l Z y e Wn e k
 rZ d „  Z n Xe j d k  r‹ e j d ƒ Z	 e j d ƒ Z
 n e j d ƒ Z	 e j d ƒ Z
 d d	 „ Z d
 e f d „  ƒ  YZ d S(   s-   Refactored 'safe reference from dispatcher.pyiÿÿÿÿNc         C   s   t  |  d ƒ S(   Nt   __call__(   t   hasattr(   t   object(    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   callable.   s    i   t   im_selft   im_funct   __self__t   __func__c         C   s¤   y t  |  ƒ } Wn: t k
 rL t | ƒ r< t j |  | ƒ St j |  ƒ SnT X| d k	 r  t |  d ƒ s‡ t |  d ƒ s‡ t d |  ƒ ‚ t d |  d | ƒ } | Sd S(   sÖ  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.
    R   R   sR   safe_ref target %r has im_self, but no im_func, don't know how to create referencet   targett	   on_deleteN(	   t   get_selft   AttributeErrorR   t   weakreft   reft   NoneR   t   AssertionErrort   BoundMethodWeakref(   R   R	   R   t	   reference(    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   safe_ref:   s    !
R   c           B   sq   e  Z d  Z e j ƒ  Z d d „ Z d d „ Z d „  Z	 e
 e	 ƒ Z	 d „  Z e Z d „  Z d „  Z 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.

    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.
    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 deletion_methods 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(    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyR   |   s    c         C   s‘   |  d „ } | g |  _  |  j | ƒ |  _ t | ƒ } t | ƒ } t j | | ƒ |  _ t j | | ƒ |  _ t	 | ƒ |  _
 t	 | j ƒ |  _ d S(   sƒ  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  } | j  2y | j j | j =Wn t k
 r8 n Xxƒ | D]{ } y t | ƒ rb | | ƒ n  Wq@ t k
 rº y t j ƒ  Wq» t	 k
 r¶ t
 j ƒ  d } d | | | f GHq» Xq@ Xq@ Wd S(   s=   Set self.isDead to True when method or instance is destroyed.i   s3   Exception during saferef %s cleanup function %s: %sN(   R   t	   __class__R   R   t   KeyErrorR   t	   Exceptiont	   tracebackt	   print_excR   t   syst   exc_info(   t   weakt   selft   methodst   functiont   e(    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   remove¢   s"    
N(   R   R   R   R
   t   get_funcR   R   t	   weak_selft	   weak_funct   strt	   self_namet   __name__t	   func_name(   R)   R   R	   R-   R   R   (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyR   ’   s    c         C   s"   t  t | ƒ ƒ t  t | ƒ ƒ 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   (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyR   ¾   s    c         C   s   d |  j  j |  j |  j f S(   s-   Give a friendly representation of the object.s	   %s(%s.%s)(   R!   R3   R2   R4   (   R)   (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   __str__Ç   s    	c         C   s   |  ƒ  d k	 S(   s'   Whether we are still a valid reference.N(   R   (   R)   (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   __nonzero__Ñ   s    c         C   s;   t  | |  j ƒ s( t |  j t | ƒ ƒ St |  j | j ƒ S(   s   Compare with another reference.(   t
   isinstanceR!   t   cmpt   typeR   (   R)   t   other(    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   __cmp__Õ   s    c         C   sD   |  j  ƒ  } | d k	 r@ |  j ƒ  } | d k	 r@ | j | ƒ Sn  d S(   s?  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(   R/   R   R0   t   __get__(   R)   R   R+   (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyR    Û   s    
N(   R3   t
   __module__t   __doc__R   t   WeakValueDictionaryR   R   R   R   R   t   classmethodR6   t   __repr__R7   R<   R    (    (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyR   X   s    ,				(   i   (   R?   t   operatorR&   R$   R   R   t	   NameErrort   version_infot
   attrgetterR
   R.   R   R   R   R   (    (    (    sV   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/_saferef.pyt   <module>#   s   