HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib64/python3.10/site-packages/vine/__pycache__/promises.cpython-310.pyc
o

we�@sfdZddlZddlZddlmZddlmZmZddlm	Z	ddl
mZdgZe	j
Gd	d�d��ZdS)
zPromise implementation.�N)�deque)�
WeakMethod�ref�)�Thenable)�reraise�promisec@s�eZdZdZeed�s
dZ			ddd�Zedd	��Z	d
d�Z
dd
�Zdd�Zdd�Z
ddd�Zddd�Zddd�Zedd��ZdS)ra~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__NFcCs�||_||_|j||d�|_|pd|_|pi|_d|_d|_d|_d|_	d|_
d|_||_d|_
|dur9|�|�|jrE|jrCt|�sGJ�dSdS)N�r
r�F)rr�_get_fun_or_weakrefr
rrr
rrrrrrr�then�callable)�selfr
rr�callbackrrrrr�F/home/arjun/projects/env/lib/python3.10/site-packages/vine/promises.py�__init__Ts&


�zpromise.__init__cCs"|s|St�|�r
t|�St|�S)zbReturn the callable or a weak reference.

        Handles both bound and unbound methods.
        )�inspect�ismethodrrrrrr ros

zpromise._get_fun_or_weakrefcCs.|jrdnd�t|�j�dt|�d��|j�S)Nz<{0} --> {1!r}>z<{0}>z@0x�x)r
�format�type�__name__�id�rrrr �__repr__}s�zpromise.__repr__c	Cs�d|_z<|jdur|j��|jdur|jD]}|��qt|jt�r4|j��Wd|_|_|_dSWd|_|_|_dSd|_|_|_w)NT)rr�cancelr�
isinstancerr)r�pendingrrr r+�s




�*zpromise.cancelcOsBd}|jrdS|r|j|n|j}|rt|jfi|��n|j}|�|j�}|durYz"|jr9||i|��d}i}n||i|��}|fif|_\}}WntyX|�	�YSw||f|_\}}d|_
|j}	|	dur~z
|	|i|��Wd|_|Sd|_w|j}
z|
r�|
�
�}||i|��|
s�Wd|_|SWd|_|Sd|_w)NrT)rr�dictr�
_fun_is_aliver
rr�	Exception�throwr
rr�popleft)rrr�retval�
final_args�final_kwargsr
�ca�ck�	svpending�	lvpending�prrr �__call__�sN��	����zpromise.__call__cCs|jr|�S|jS�N)rr
)rr
rrr r/�szpromise._fun_is_alivecCs�t|t�st||d�}|jr|��|S|jr|�|j�n|jr-|j	\}}||i|��|j
durI|j}|durDdt|g�|_|_
n||_|S|j
�
|�|S)N)r)r,rrrr+rr1rr
rrrr�append)rrrrrr8rrr r�s$


zpromise.thencCsX|js(|dur	|nt��d}d||_|_|jr*|j|j|fi|j��dSdSdS)NrT)r�sys�exc_inforrrrr)r�excrrr �throw1�s�zpromise.throw1Tc
Cs|js�t��d}|dur|n|}zV|�|�|j}|dur-z
|�|�Wd|_nd|_w|j}z|r<|���|�|s3Wd|_nd|_wW|jdurd|rf|durZ|dusY||urZ�tt	|�||�dSdSdS|jdur�|r�|dur||dus{||ur|�tt	|�||�wwwdS)Nr)
rr>r?rArrr2rrr&)rr@�tb�	propagate�current_excr8r9rrr r1�s8
������z
promise.throwcCs|jr|jS|jgSr<)rrr)rrr �	listeners�szpromise.listeners)NNNNNFFr<)NNT)r'�
__module__�__qualname__�__doc__�hasattrr>�	__slots__r!�staticmethodrr*r+r;r/rrAr1�propertyrErrrr r
s&
<
�


&


)rHr"r>�collectionsr�weakrefrr�abstractr�utilsr�__all__�registerrrrrr �<module>s