
    ؾh                         d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ dgZe	j                   G d	 d             Zy)
zPromise implementation.    N)deque)
WeakMethodref   )Thenable)reraisepromisec                       e Zd ZdZ eed      sdZ	 	 	 ddZed        Z	d Z
d Zd	 Zd
 ZddZddZddZed        Zy)r	   a~  Promise of future evaluation.

    This is a special implementation of promises in that it can
    be used both for "promise of a value" and lazy evaluation.
    The biggest upside for this is that everything in a promise can also be
    a promise, e.g. filters, callbacks and errbacks can all be promises.

    Usage examples:

    .. code-block:: python

        >>> p = promise()
        >>> p.then(promise(print, ('OK',)))  # noqa
        >>> p.on_error = promise(print, ('ERROR',))  # noqa
        >>> p(20)
        OK, 20
        >>> p.then(promise(print, ('hello',)))  # noqa
        hello, 20


        >>> p.throw(KeyError('foo'))
        ERROR, KeyError('foo')


        >>> p2 = promise()
        >>> p2.then(print)  # noqa
        >>> p2.cancel()
        >>> p(30)

    Example:
    .. code-block:: python

        from vine import promise, wrap

        class Protocol:

            def __init__(self):
                self.buffer = []

            def receive_message(self):
                return self.read_header().then(
                    self.read_body).then(
                        wrap(self.prepare_body))

            def read(self, size, callback=None):
                callback = callback or promise()
                tell_eventloop_to_read(size, callback)
                return callback

            def read_header(self, callback=None):
                return self.read(4, callback)

            def read_body(self, header, callback=None):
                body_size, = unpack('>L', header)
                return self.read(body_size, callback)

            def prepare_body(self, value):
                self.buffer.append(value)
    pypy_version_info)funargskwargsreadyfailedvalueignore_resultreason
_svpending
_lvpendingon_error	cancelledweak__weakref____dict__Nc                 ^   || _         || _        | j                  ||      | _        |xs d| _        |xs i | _        d| _        d| _        d | _        d | _	        d | _
        d | _        || _        d| _        || j                  |       | j                  r| j                  rt        |      sJ y y )Nr   r    F)r   r   _get_fun_or_weakrefr   r   r   r   r   r   r   r   r   r   r   thencallable)selfr   r   r   callbackr   r   r   s           I/var/www/Befach/backend/env/lib/python3.12/site-packages/vine/promises.py__init__zpromise.__init__T   s     	*++$+?JB	l


  IIh8888--     c                 `    |s| S t        j                  |       rt        |       S t        |       S )zbReturn the callable or a weak reference.

        Handles both bound and unbound methods.
        )inspectismethodr   r   r   s     r#   r   zpromise._get_fun_or_weakrefo   s-     JC c?"s8Or%   c                     | j                   rdndj                  t        |       j                   dt	        |       d| j                         S )Nz<{0} --> {1!r}>z<{0}>z@0xx)r   formattype__name__idr!   s    r#   __repr__zpromise.__repr__}   sC    %)XX!7BBDz""#3r$xl3TXX
 	
r%   c                    d| _         	 | j                  | j                  j                          | j                  !| j                  D ]  }|j                           t	        | j
                  t              r| j
                  j                          d x| _        x| _        | _        y # d x| _        x| _        | _        w xY w)NT)r   r   cancelr   
isinstancer   r   )r!   pendings     r#   r2   zpromise.cancel   s    		E*&&(*#GNN$  /$--2$$&@DDDODdoDDODdos   BB& &B=c                 ~   d }| j                   ry |r| j                  |z   n| j                  }|rt        | j                  fi |n| j                  }| j	                  | j
                        }|2	 | j                  r ||i | d}i }n ||i |}|fi fx| _        \  }}n||fx| _        \  }}d| _
        | j                  }	|		  |	|i | d | _        |S | j                  }
	 |
r|
j                         } ||i | |
rd | _        |S # t        $ r | j                         cY S w xY w# d | _        w xY w# d | _        w xY w)Nr   T)r   r   dictr   _fun_is_aliver   r   r   	Exceptionthrowr   r   r   popleft)r!   r   r   retval
final_argsfinal_kwargsr   cack	svpending	lvpendingps               r#   __call__zpromise.__call__   sc   >>)-TYY%499
6<tDKK262$++  *?	$%%4|4BB *==F-3IrM9DJ"b %/$<<DJ"b
OO	 '2$$"&  I'!))+ArLRL   #''  $zz|#$ #' #'s*   /0D D' "D3 D$#D$'	D03	D<c                 @    | j                   r |       S | j                  S N)r   r   )r!   r   s     r#   r7   zpromise._fun_is_alive   s    		su/txx/r%   c                    t        |t              st        ||      }| j                  r|j	                          |S | j
                  r|j                  | j                         n#| j                  r| j                  \  }} ||i | | j                  1| j                  }|d t        |g      c| _        | _
        n	|| _        |S | j                  j                  |       |S )N)r   )r3   r   r	   r   r2   r   r9   r   r   r   r   r   r   append)r!   r"   r   r   r   r@   s         r#   r   zpromise.then   s    (H-x(;H>>OOO;;NN4;;'ZZ::LD&d%f%??"I$37	{9K0"*x(r%   c                     | j                   sb||nt        j                         d   }d|c| _        | _        | j
                  r+ | j
                  | j                  |fz   i | j                   y y y )Nr   T)r   sysexc_infor   r   r   r   r   )r!   excs     r#   throw1zpromise.throw1   sb    ~~#cllnQ.?C'+S$DK}}tyyC61AT[[A  r%   c                 2   | j                   st        j                         d   }||n|}	 | j                  |       | j                  }|	 |j                  |       d | _        n8| j
                  }	 |r"|j                         j                  |       |r"d | _        | j                  #|r ||||u r t        t        |      ||       y y y y # d | _        w xY w# d | _        w xY w# | j                  #|r ||||u r t        t        |      ||       w w w xY w)Nr   )
r   rI   rJ   rL   r   r   r:   r   r   r,   )r!   rK   tb	propagatecurrent_excr@   rA   s          r#   r9   zpromise.throw   s   ~~,,.+K#kC0C  OO	(/!((-*. $I/'%--/66s; ( +/==(Yzs{c[6HDIsB/ .7('  +/ +/==(Yzs{c[6HDIsB/ .7(s;   C% C C% 1#C C% 	CC% 	C""C% %1Dc                 L    | j                   r| j                   S | j                  gS rE   )r   r   r/   s    r#   	listenerszpromise.listeners   s     ????"  r%   )NNNNNFFrE   )NNT)r-   
__module____qualname____doc__hasattrrI   	__slots__r$   staticmethodr   r0   r2   rC   r7   r   rL   r9   propertyrR   r   r%   r#   r	   r	      sz    :x 3+,
	 4849$.6  

E$L0*B02 ! !r%   )rU   r'   rI   collectionsr   weakrefr   r   abstractr   utilsr   __all__registerr	   r   r%   r#   <module>r`      sC      
  #  + 
c! c! c!r%   