HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/asynctest/__pycache__/mock.cpython-37.pyc
B

�P�e ��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z
yejjZWne
k
rpdZYnXGdd�d�ZdZedZde
jjd	<d
d�Zee
jjd<ee�Zee�Ze
jjeO_d
d�Zdd�Zdd�ZGdd�de�Zdd�Zdd�Zdd�Zdd�ZGdd�de�Z Gdd �d e �Z!Gd!d"�d"�Z"Gd#d$�d$e
jj#e!d%�Z#Gd&d'�d'e"e
jj$e!d%�Z$Gd(d)�d)e
jj%e d%�Z%Gd*d+�d+e"e
jj&e d%�Z&Gd,d-�d-�Z'Gd.d/�d/e%�Z(dJd0d1�Z)dKd3d4�Z*e
jj+Z+e
jj,j-Z-d5d6�Z.e�/d7d8�Z0e0j1Z1e0j2Z2d9d:�Z3Gd;d<�d<ejj4�Z5Gd=d>�d>e
jj6�Z6e-ddddde2fd?d@�Z7e-ddddde2fdAdB�Z8ddddde2fdCdD�Z9GdEdF�dFe
jj:�Z:e
jj;Z;e8e7_<e:e7_=e9e7_>e
jj?e7_@dGe7_Ae
jj,Z,e
jjBZBe
jjCZCdLdHdI�ZDdS)Mz�
Module ``mock``
---------------

Wrapper to unittest.mock reducing the boilerplate when testing asyncio powered
code.

A mock can behave as a coroutine, as specified in the documentation of
:class:`~asynctest.mock.Mock`.
�NTc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�_AsyncIteratorz8
    Wraps an iterator in an asynchronous iterator.
    cCs
||_dS)N)�iterator)�selfr�r�?/opt/alt/python37/lib/python3.7/site-packages/asynctest/mock.py�__init__%sz_AsyncIterator.__init__cCs|S)Nr)rrrr�	__aiter__(sz_AsyncIterator.__aiter__c�s(y
t|j�Stk
rYnXt�dS)N)�nextr�
StopIteration�StopAsyncIteration)rrrr�	__anext__+s

z_AsyncIterator.__anext__N)�__name__�
__module__�__qualname__�__doc__rrrrrrrr!sr)�
__aenter__�	__aexit__r)rFrcs&�fdd�}t��j�r"t�|�S|S)aV
    Factory of ``__aiter__`` magic methods for a MagicMock.

    It creates a function which returns an asynchronous iterator based on the
    return value of ``mock.__aiter__``.

    Since __aiter__ used could be a coroutine in Python 3.5 and 3.6, we also
    support this case.

    See: https://www.python.org/dev/peps/pep-0525/#id23
    cs*�jj}|tkrtg�}nt|�}t|�S)N)rZ_mock_return_value�DEFAULT�iterr)�return_valuer)�mockrrrKs

z"_get_async_iter.<locals>.__aiter__)�asyncio�iscoroutinefunctionr�	coroutine)rrr)rr�_get_async_iter?s	
rrcCs|�dS)Nr)�	exceptionrrr�_raisedsrcs(t���r�St����fdd��}|S)z^
    Wrap a coroutine (or any function returning an awaitable) in a native
    coroutine.
    c�s�||�IdHS)Nr)�args�kwargs)rrr�wrapperqsz'_make_native_coroutine.<locals>.wrapper)�inspectr�	functools�wraps)rrr)rr�_make_native_coroutinehs
r#cCs t|t�r|jStj�|�SdS)N)�
isinstance�_patch_dict�_is_started�unittestr)�patchingrrrr&xs
r&cs8eZdZdZ�fdd�Zedd��Z�fdd�Z�ZS)�FakeInheritanceMetaz�
    A metaclass which recreates the original inheritance model from
    unittest.mock.

    - NonCallableMock > NonCallableMagicMock
    - NonCallable > Mock
    - Mock > MagicMock
    cs&t�|j|�|d<t��|||�dS)N�__new__)�types�
MethodType�_FakeInheritanceMeta__new�superr)r�name�bases�attrs)�	__class__rrr�szFakeInheritanceMeta.__init__cOs(t|j|fd|ji�}tj|f|�|�S)Nr)�typer
r�objectr*)�clsrr�newrrrZ__new�szFakeInheritanceMeta.__newcs\t��|�rdSt|�}t|t�r4t|ttf�r4dSt|t�rXt|t�sXt|tf�rXdSdS)NTF)	r.�__instancecheck__r3�
issubclass�NonCallableMock�NonCallableMagicMock�Mock�
CoroutineMock�	MagicMock)r5�obj�_type)r2rrr7�s
z%FakeInheritanceMeta.__instancecheck__)	r
rrrr�staticmethodr-r7�
__classcell__rr)r2rr)sr)cCs
|jdS)N�_mock_is_coroutine)�__dict__)rrrr�_get_is_coroutine�srDcCst|�rtnd}||jd<dS)NFrB)�bool�
_is_coroutinerC)r�valuerrr�_set_is_coroutine�srHcOsVtjjj||f|�|�g}x*t|�D]}t�t||��r&|�|�q&W||j	d<dS)N�_spec_coroutines)
r'rr9�_mock_add_spec�dirrr�getattr�appendrC)r�specrrrI�attrrrrrJ�srJcOs�|�d�}||jdkr"t||�St|�}t|t�rB|tkrBt}nLt|t�rRt}n<t|tjj	�s�t|tjj
�rtt}q�t|t�r�t}n
|j
d}|||�S)N�	_new_namerI�)�getrCr<r3r8r=�async_magic_coroutinesr'rZ
CallableMixinr:r9r;�__mro__)rrrrPr?�klassrrr�_get_child_mock�s




rVcseZdZ�fdd�Z�ZS)�
MockMetaMixincsPt�fdd�|D��s>tjjtjd�}d|_|�tt	|d��t
���|||�S)Nc3s|]}t|��VqdS)N)r$)�.0�	baseclass)�metarr�	<genexpr>�sz(MockMetaMixin.__new__.<locals>.<genexpr>)�spec_setr)rJrV�__code__)�anyr'rr9r+�CodeType�co_flags�updaterJrVr.r*)rZr/�base�	namespaceZ	code_mock)r2)rZrr*�s
zMockMetaMixin.__new__)r
rrr*rArr)r2rrW�srWcseZdZ�fdd�Z�ZS)�IsCoroutineArgMetac	spt�fdd�|D��s^|�tttttdd�tt�d��|�d|dj���fdd	�}||d<t���|||�S)
Nc3s|]}t|��VqdS)N)r$)rXrY)rZrrr[�sz-IsCoroutineArgMeta.__new__.<locals>.<genexpr>z(True if the object mocked is a coroutine)�doc)Z_asynctest_get_is_coroutine�_asynctest_set_is_coroutine�is_coroutinerF�__setattr__rcs$|dkr|�|�n�|||�SdS)Nrg)rf)r�attrnamerG)�wrapped_setattrrrrh�sz/IsCoroutineArgMeta.__new__.<locals>.__setattr__)	r^rarDrH�propertyrRrhr.r*)rZr/rbrcrh)r2)rZrjrr*�szIsCoroutineArgMeta.__new__)r
rrr*rArr)r2rrd�srdc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�AsyncMagicMixina�
    Add support for async magic methods to :class:`MagicMock` and
    :class:`NonCallableMagicMock`.

    Actually, it's a shameless copy-paste of :class:`unittest.mock.MagicMixin`:
        when added to our classes, it will just do exactly what its
        :mod:`unittest` counterpart does, but for magic methods. It adds some
        behavior but should be compatible with future additions of
        :class:`MagicMock`.
    cOs*|��tj�t|�j||�|��dS)N)�_mock_set_async_magicsr'r�_safe_superrlr)rrrrrrrszAsyncMagicMixin.__init__cCs�t}t|dd�dk	rNt�|j�}t|}x$|D]}|t|�jkr.t||�q.W|tt|�j�}t|�}x"|D]}t||t	j
�||��qnWdS)N�
_mock_methods)�
_async_magicsrL�intersectionror3rC�delattr�set�setattrr'rZ
MagicProxy)rZthese_magicsZ
remove_magics�entryr?rrrrms

z&AsyncMagicMixin._mock_set_async_magicscOs"tjjj|f|�|�|��dS)N)r'rr=�
mock_add_specrm)rrrrrrrvszAsyncMagicMixin.mock_add_speccs�t�dd�}|dks||kr�|tkr�tj�|�s^tt��|tj�||��|���fdd�}n,tj��|d|�tt��||�|�j	|<t
��||�Stj�t
���||�dS)Nrocs��f|�|�S)Nr)rr)�originalrrrrG+sz*AsyncMagicMixin.__setattr__.<locals>.value)rLrpr'rZ_is_instance_mockrtr3Z_get_methodZ_check_and_set_parent�_mock_childrenr4rhrnrl)rr/rGror)rwrrrh"s

zAsyncMagicMixin.__setattr__N)r
rrrrrmrvrhrrrrrl�s

rlcs"eZdZdZd�fdd�	Z�ZS)r9aH
    Enhance :class:`unittest.mock.NonCallableMock` with features allowing to
    mock a coroutine function.

    If ``is_coroutine`` is set to ``True``, the :class:`NonCallableMock`
    object will behave so :func:`asyncio.iscoroutinefunction` will return
    ``True`` with ``mock`` as parameter.

    If ``spec`` or ``spec_set`` is defined and an attribute is get,
    :class:`~asynctest.CoroutineMock` is returned instead of
    :class:`~asynctest.Mock` when the matching spec attribute is a coroutine
    function.

    The test author can also specify a wrapped object with ``wraps``. In this
    case, the :class:`~asynctest.Mock` object behavior is the same as with an
    :class:`unittest.mock.Mock` object: the wrapped object may have methods
    defined as coroutine functions.

    See :class:`unittest.mock.NonCallableMock`
    Ncs,t�jf|||||d�|��|�|�dS)N)rNr"r/r\�parent)r.rrf)rrNr"r/r\rgryr)r2rrrTszNonCallableMock.__init__)NNNNNN)r
rrrrrArr)r2rr9>sr9)�	metaclasscs"eZdZdZd�fdd�	Z�ZS)r:zI
    A version of :class:`~asynctest.MagicMock` that isn't callable.
    Ncs,t�jf|||||d�|��|�|�dS)N)rNr"r/r\ry)r.rrf)rrNr"r/r\rgryr)r2rrraszNonCallableMagicMock.__init__)NNNNNN)r
rrrrrArr)r2rr:\sr:c@seZdZdZdS)r;aL
    Enhance :class:`unittest.mock.Mock` so it returns
    a :class:`~asynctest.CoroutineMock` object instead of
    a :class:`~asynctest.Mock` object where a method on a ``spec`` or
    ``spec_set`` object is a coroutine.

    For instance:

    >>> class Foo:
    ...     @asyncio.coroutine
    ...     def foo(self):
    ...         pass
    ...
    ...     def bar(self):
    ...         pass

    >>> type(asynctest.mock.Mock(Foo()).foo)
    <class 'asynctest.mock.CoroutineMock'>

    >>> type(asynctest.mock.Mock(Foo()).bar)
    <class 'asynctest.mock.Mock'>

    The test author can also specify a wrapped object with ``wraps``. In this
    case, the :class:`~asynctest.Mock` object behavior is the same as with an
    :class:`unittest.mock.Mock` object: the wrapped object may have methods
    defined as coroutine functions.

    If you want to mock a coroutine function, use :class:`CoroutineMock`
    instead.

    See :class:`~asynctest.NonCallableMock` for details about :mod:`asynctest`
    features, and :mod:`unittest.mock` for the comprehensive documentation
    about mocking.
    N)r
rrrrrrrr;js"r;c@seZdZdZdS)r=a{
    Enhance :class:`unittest.mock.MagicMock` so it returns
    a :class:`~asynctest.CoroutineMock` object instead of
    a :class:`~asynctest.Mock` object where a method on a ``spec`` or
    ``spec_set`` object is a coroutine.

    If you want to mock a coroutine function, use :class:`CoroutineMock`
    instead.

    :class:`MagicMock` allows to mock ``__aenter__``, ``__aexit__``,
    ``__aiter__`` and ``__anext__``.

    When mocking an asynchronous iterator, you can set the
    ``return_value`` of ``__aiter__`` to an iterable to define the list of
    values to be returned during iteration.

    You can not mock ``__await__``. If you want to mock an object implementing
    __await__, :class:`CoroutineMock` will likely be sufficient.

    see :class:`~asynctest.Mock`.

    .. versionadded:: 0.11

        support of asynchronous iterators and asynchronous context managers.
    N)r
rrrrrrrr=�sr=c@s`eZdZdd�Zejddd��Zejddd��Zejdd	��Zejd
d��Z	dd
�Z
dd�ZdS)�_AwaitEventcCs||_d|_dS)N)�_mock�
_condition)rrrrrr�sz_AwaitEvent.__init__rc#s�fdd�}|�|�EdHS)z�
        Wait for await.

        :param skip: How many awaits will be skipped.
                     As a result, the mock should be awaited at least
                     ``skip + 1`` times.
        cs
|j�kS)N)�await_count)r)�skiprr�	predicate�sz#_AwaitEvent.wait.<locals>.predicateN)�wait_for)rrr�r)rr�wait�s	z_AwaitEvent.waitc#s&|jj���fdd�}|�|�EdHS)a�
        Wait for the next await.

        Unlike :meth:`wait` that counts any await, mock has to be awaited once
        more, disregarding to the current
        :attr:`asynctest.CoroutineMock.await_count`.

        :param skip: How many awaits will be skipped.
                     As a result, the mock should be awaited at least
                     ``skip + 1`` more times.
        cs|j��kS)N)r~)r)r~rrrr��sz(_AwaitEvent.wait_next.<locals>.predicateN)r|r~r�)rrr�r)r~rr�	wait_next�s
z_AwaitEvent.wait_nextc#sD���}z,|��EdH��fdd�}|�|�EdHS|��XdS)a

        Wait for a given predicate to become True.

        :param predicate: A callable that receives mock which result
                          will be interpreted as a boolean value.
                          The final predicate value is the return value.
        Ncs
��j�S)N)r|r)r�rrr�
_predicate�sz(_AwaitEvent.wait_for.<locals>._predicate)�_get_condition�acquirer��release)rr��	conditionr�r)r�rrr��s	z_AwaitEvent.wait_forccs2|��}z|��EdH|��Wd|��XdS)N)r�r��
notify_allr�)rr�rrr�_notify�s
z_AwaitEvent._notifycCs|jdkrt��|_|jS)ag
        Creation of condition is delayed, to minimize the change of using the
        wrong loop.

        A user may create a mock with _AwaitEvent before selecting the
        execution loop.  Requiring a user to delay creation is error-prone and
        inflexible. Instead, condition is created when user actually starts to
        use the mock.
        N)r}r�	Condition)rrrrr��s

z_AwaitEvent._get_conditioncCs|jjdkS)Nr)r|r~)rrrr�__bool__sz_AwaitEvent.__bool__N)r)r)r
rrrrrr�r�r�r�r�r�rrrrr{�s

r{cs�eZdZdZej�d�Zej�d�Zej�d�Z	ej�d�Z
�fdd�Z�fdd	�Zd
d�Z
dd
�Zdd�Zdd�Zdd�Zddd�Zdd�Z�fdd�Z�ZS)r<a8
    Enhance :class:`~asynctest.mock.Mock` with features allowing to mock
    a coroutine function.

    The :class:`~asynctest.CoroutineMock` object will behave so the object is
    recognized as coroutine function, and the result of a call as a coroutine:

    >>> mock = CoroutineMock()
    >>> asyncio.iscoroutinefunction(mock)
    True
    >>> asyncio.iscoroutine(mock())
    True


    The result of ``mock()`` is a coroutine which will have the outcome of
    ``side_effect`` or ``return_value``:

    - if ``side_effect`` is a function, the coroutine will return the result
      of that function,
    - if ``side_effect`` is an exception, the coroutine will raise the
      exception,
    - if ``side_effect`` is an iterable, the coroutine will return the next
      value of the iterable, however, if the sequence of result is exhausted,
      ``StopIteration`` is raised immediately,
    - if ``side_effect`` is not defined, the coroutine will return the value
      defined by ``return_value``, hence, by default, the coroutine returns
      a new :class:`~asynctest.CoroutineMock` object.

    If the outcome of ``side_effect`` or ``return_value`` is a coroutine, the
    mock coroutine obtained when the mock object is called will be this
    coroutine itself (and not a coroutine returning a coroutine).

    The test author can also specify a wrapped object with ``wraps``. In this
    case, the :class:`~asynctest.Mock` object behavior is the same as with an
    :class:`unittest.mock.Mock` object: the wrapped object may have methods
    defined as coroutine functions.
    �awaitedr~�
await_args�await_args_listcsNt�j||�t|jd<t|�|jd<d|jd<d|jd<tj��|jd<dS)NrFZ
_mock_awaitedrZ_mock_await_countZ_mock_await_argsZ_mock_await_args_list)r.rrFrCr{r'r�	_CallList)rrr)r2rrr;s


zCoroutineMock.__init__c
s�yt�j||��Wnvtk
rZ}z*�j}|dk	r<t|�s<�t�t�|��Wdd}~XYn0tk
r�}zt�t�|��Wdd}~XYnX�j	�tj���fdd��}|�S)Nc	3sTzt���r�EdHS�SWd�jd7_��_�j����j��EdHXdS)NrQ)r �isawaitabler~r�r�rMr�r�r)�_call�
_mock_self�resultrr�proxyVs

z'CoroutineMock._mock_call.<locals>.proxy)
r.�
_mock_callr
�side_effect�callablerrr�
BaseException�	call_args)r�rr�er�r�)r2)r�r�r�rr�Hs 
zCoroutineMock._mock_callcCs(|}|jdkr$d|jpd}t|��dS)za
        Assert that the mock was awaited at least once.

        .. versionadded:: 0.12
        rz#Expected '%s' to have been awaited.rN)r~�
_mock_name�AssertionError)r�r�msgrrr�assert_awaitedes

zCoroutineMock.assert_awaitedcOs.|}|jdks*d|jpd|jf}t|��dS)z`
        Assert that the mock was awaited exactly once.

        .. versionadded:: 0.12
        rQz:Expected '%s' to have been awaited once. Awaited %s times.rN)r~r�r�)r�rrrr�rrr�assert_awaited_onceqs

z!CoroutineMock.assert_awaited_oncecs||��jdkr(�����}td|f�����fdd�}����f�}���j�}||krxt|t�rh|nd}t|��|�dS)zn
        Assert that the last await was with the specified arguments.

        .. versionadded:: 0.12
        NzExpected await: %s
Not awaitedcs�����}|S)N)Z_format_mock_failure_message)r�)rrrrr�_error_message�sz9CoroutineMock.assert_awaited_with.<locals>._error_message)r��_format_mock_call_signaturer��
_call_matcherr$�	Exception)r�rr�expectedr��actual�causer)rrrr�assert_awaited_with}s
z!CoroutineMock.assert_awaited_withcOs6|}|jdks*d|jpd|jf}t|��|j||�S)z�
        Assert that the mock was awaited exactly once and with the specified arguments.

        .. versionadded:: 0.12
        rQz3Expected '%s' to be awaited once. Awaited %s times.r)r~r�r�r�)r�rrrr�rrr�assert_awaited_once_with�s
z&CoroutineMock.assert_awaited_once_withcs^|���||f�}�fdd��jD�}||krZt|t�r<|nd}��||�}td|�|�dS)zu
        Assert the mock has ever been awaited with the specified arguments.

        .. versionadded:: 0.12
        csg|]}��|��qSr)r�)rX�c)rrr�
<listcomp>�sz2CoroutineMock.assert_any_await.<locals>.<listcomp>Nz%s await not found)r�r�r$r�r�r�)r�rrr�r�r�Zexpected_stringr)rr�assert_any_await�szCoroutineMock.assert_any_awaitFc	s�|��fdd�|D�}t|t�r$|nd}tj��fdd��jD��}|sp||krltdtj�|��jf�|�dSt|�}g}x:|D]2}y|�|�Wq�t	k
r�|�
|�Yq�Xq�W|r�tdt|�f�|�dS)a�
        Assert the mock has been awaited with the specified calls.
        The :attr:`await_args_list` list is checked for the awaits.

        If `any_order` is False (the default) then the awaits must be
        sequential. There can be extra calls before or after the
        specified awaits.

        If `any_order` is True then the awaits can be in any order, but
        they must all appear in :attr:`await_args_list`.

        .. versionadded:: 0.12
        csg|]}��|��qSr)r�)rXr�)rrrr��sz3CoroutineMock.assert_has_awaits.<locals>.<listcomp>Nc3s|]}��|�VqdS)N)r�)rXr�)rrrr[�sz2CoroutineMock.assert_has_awaits.<locals>.<genexpr>z)Awaits not found.
Expected: %r
Actual: %rz%r not all found in await list)r$r�r'rr�r�r��list�remove�
ValueErrorrM�tuple)r�ZcallsZ	any_orderr�r�Z
all_awaits�	not_foundZkallr)rr�assert_has_awaits�s,
zCoroutineMock.assert_has_awaitscCs.|}|jdkr*d|jpd|jf}t|��dS)zY
        Assert that the mock was never awaited.

        .. versionadded:: 0.12
        rz9Expected '%s' to not have been awaited. Awaited %s times.rN)r~r�r�)r�rr�rrr�assert_not_awaited�s

z CoroutineMock.assert_not_awaitedcs4t�j||�t|�|_d|_d|_tj��|_	dS)z=
        See :func:`unittest.mock.Mock.reset_mock()`
        rN)
r.�
reset_mockr{r�r~r�r'rr�r�)rrr)r2rrr��s

zCoroutineMock.reset_mock)F)r
rrrr'rZ_delegating_propertyr�r~r�r�rr�r�r�r�r�r�r�r�r�rArr)r2rr<	s%


'r<c	Ks�tj�|�rt|�}t|t�}t�|�}d|i}|r>d|i}n|dkrJi}|rZ|rZd|d<|�|�t}	t	�
|�rxi}n@|r�|r�td��t}	n*tj�
|�s�t}	n|r�|r�tj�|�s�t}	|�d|�}|}
|dkr�d}
|	f|||
|d	�|��}t|tjj��r`|}tj�||�}|�rrt|_t|�|_d
|_d|_tj��|_x4dD]}
t||
t||
���qBWntj�||||�|dk	�r�|�s�||j|<|�r�|�s�d|k�r�t||dd
|d�|_�x.t |�D�] }tj�!|��rڐq�yt||�}Wnt"k
�r�w�YnXd|i}|�rd|i}t|tjj��sJtj�#|||||�}||j|<n||}t|tjj��rd|j}tj�$|||�}||d<t�|��r�t}nt}|f||||d�|��}||j|<tjj|||d�t|tjj��r�t|||��q�W|S)a�
    Create a mock object using another object as a spec. Attributes on the mock
    will use the corresponding attribute on the spec object as their spec.

    ``spec`` can be a coroutine function, a class or object with coroutine
    functions as attributes.

    If ``spec`` is a coroutine function, and ``instance`` is not ``False``, a
    :exc:`RuntimeError` is raised.

    .. versionadded:: 0.12
    rNr\NTZ_spec_as_instancezMInstance can not be True when create_autospec is mocking a coroutine functionr/�)ry�_new_parentrPr/r)r�r�r�r�r�r�r�rz())�instance�_name�_parentZ	_eat_self)ryr/rPr�)�	skipfirst)%r'rZ_is_listr3r$rrrar=r �isdatadescriptor�RuntimeErrorr<Z	_callabler:Z_instance_callable�popZ
FunctionTypesZ_set_signaturerFr{r�r~r�r�r�rtrLZ_check_signaturerx�create_autospecrrKZ	_is_magic�AttributeErrorZ
_SpecStateZ
_must_skip)rNr\r�r�r�rZis_type�is_coroutine_func�_kwargsZKlassrPrZwrapped_mock�arurwr6ryr�Zchild_klassrrrr��s�











r�r�cCs"|dkrtdtd�}tj�||�S)aI
    A helper function to create a mock to replace the use of :func:`open()`. It
    works for :func:`open()` called directly or used as a context manager.

    :param mock: mock object to configure, by default
                 a :class:`~asynctest.MagicMock` object is
                 created with the API limited to methods or attributes
                 available on standard file handles.

    :param read_data: string for the :func:`read()` and :func:`readlines()` of
                      the file handle to return. This is an empty string by
                      default.
    N�open)r/rN)r=r�r'r�	mock_open)r�	read_datarrrr�ksr�cCsN|tkrJ|sJ|��d}t|ttf�r2|�dt�}t�|�rDt	|_
nt|_
|S)Nr)r�get_originalr$�classmethodr@�__get__r4rrr<�new_callabler=)�patcherr6r�rwrrr�_update_new_callable�s
r��
PatchScopezLIMITED GLOBALcs�t�d�r�j�|��St���}t���}yt���}Wntk
rTd}YnX|sb|sbdS|g���fdd��|r�t��}n(|r��fdd�}|}|r�t�	|�}n�}�|_t
���|�S)N�	patchingsFc
	s\g}g}g}t�}�z"y�x��D]�}|��}|jtkr>|�|�t|t�r�|jtkrxx.|D]}|j|jkrX|�	|�qXWq�|�|�q|j
dk	r�|�|�|jt
kr�||j
|_q|jt
kr||_|�|�qW|t|�7}�||�}	t|	�t����Stk
�r2||k�r$t|��r$|�|�t��}�YnXWdxt|�D]}|j|��qBWXdS)N)r��	__enter__�scope�LIMITEDrMr$r%�GLOBAL�in_dict�_keep_global_patch�attribute_namerar6r�
mock_to_reuse�_PatchedGeneratorrrr�r&�sys�exc_info�reversed�__exit__)
rr�
extra_argsZpatchers_to_exitZpatch_dict_with_limited_scoper�r(�argZlimited_patching�gen)�funcr�rr�patched_factory�sB











z5_decorate_coroutine_callable.<locals>.patched_factoryc?s�||�EdHS)Nr)rr)r�rr�patched_generator�sz7_decorate_coroutine_callable.<locals>.patched_generator)�hasattrr�rMr �isgeneratorfunctionrrr�r#rr!r")r�Znew_patchingZis_generator_funcr�Zis_native_coroutine_funcZpatchedr�r)r�r�r�r�_decorate_coroutine_callable�s.



.
r�cs^eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Z�fdd�Zddd�Z	dd�Z
dd�Z�ZS)r�cCsR||_||_t|dd�|_t|dd�|_||_dd�|D�|_dd�|D�|_dS)Nr
rcSsg|]}|jtkr|�qSr)r�r�)rX�prrrr��sz._PatchedGenerator.__init__.<locals>.<listcomp>cSsg|]}|jtkr|�qSr)r�r�)rXr�rrrr��s)r�rFrLr
rr��global_patchings�limited_patchings)rr�r�rgrrrr�sz_PatchedGenerator.__init__c	Cs6t���$}x|jD]}|�|�qW|��SQRXdS)N)�
contextlib�	ExitStackr��
enter_context�pop_all)r�stackr(rrr�_limited_patchings_stack�s
z*_PatchedGenerator._limited_patchings_stackcCs(x"t|j�D]}t|�r|��qWdS)N)r�r�r&�stop)rr(rrr�_stop_global_patchingssz(_PatchedGenerator._stop_global_patchingscCs
t|j�S)N)�repr�	generator)rrrr�__repr__sz_PatchedGenerator.__repr__c	CsDy |���|j�d�SQRXWntk
r>|���YnXdS)N)r�r��sendr�r�)rrrr�__next__s
z_PatchedGenerator.__next__c	s |���t��|�SQRXdS)N)r�r.r�)rrG)r2rrr�s
z_PatchedGenerator.sendNc	Cs$|���|j�|||�SQRXdS)N)r�r��throw)r�excrG�	tracebackrrrr�s
z_PatchedGenerator.throwc	Cs.z|���|j��SQRXWd|��XdS)N)r�r��closer�)rrrrr�s
z_PatchedGenerator.closecCs|��dS)N)r�)rrrr�__del__&sz_PatchedGenerator.__del__)NN)
r
rrrr�r�r�r�r�r�r�r�rArr)r2rr��s	
r�csJeZdZed��fdd�
Zdd�Zdd�Z�fdd	�Z�fd
d�Z�Z	S)�_patch)r�cst�j||�||_d|_dS)N)r.rr�r�)rr�rr)r2rrr-sz_patch.__init__cCsNt|j|j|j|j|j|j|j|j|j	|j
d�
}|j|_dd�|jD�|_|S)N)r�cSsg|]}|���qSr)�copy)rXr�rrrr�:sz_patch.copy.<locals>.<listcomp>)
r��getter�	attributer6rN�creater\�autospecr�rr�r��additional_patchers)rr�rrrr�2s
z_patch.copycCsj|jdk	r^|��|_|��\|_|_t|j|j|j�|jdk	rXx|j	D]}|�
�qHW|jS|��SdS)N)r�r��targetr��
temp_original�is_localrtr�r�r�r��_perform_patch)rr(rrrr�>s


z_patch.__enter__cs�|��\}}t���}|jdks&|js*|S|jdkr:|}n|j}t|ft|j�|jd�|j��}||_	||_
t|j|j|�|j
dk	r�|jtkr�|||j
<|S|S)NT)r\r�)r�r.r�r�r�rEr\r�rrrrtrr�r6r)rrw�localr�r�r6)r2rrrKs"




z_patch._perform_patchcs&t||�}|dkrt��|�S|SdS)N)r�r.�decorate_callable)rr��wrapped)r2rrrhs
z_patch.decorate_callable)
r
rrr�rr�r�rrrArr)r2rr�,s

r�cKs8tj�|�\}	}
t|	|
||||||||d�
}t|||�S)ao

    A context manager, function decorator or class decorator which patches the
    target with the value given by the ``new`` argument.

    ``new`` specifies which object will replace the ``target`` when the patch
    is applied. By default, the target will be patched with an instance of
    :class:`~asynctest.CoroutineMock` if it is a coroutine, or
    a :class:`~asynctest.MagicMock` object.

    It is a replacement to :func:`unittest.mock.patch`, but using
    :mod:`asynctest.mock` objects.

    When a generator or a coroutine is patched using the decorator, the patch
    is activated or deactivated according to the ``scope`` argument value:

      * :const:`asynctest.GLOBAL`: the default, enables the patch until the
        generator or the coroutine finishes (returns or raises an exception),

      * :const:`asynctest.LIMITED`: the patch will be activated when the
        generator or coroutine is being executed, and deactivated when it
        yields a value and pauses its execution (with ``yield``, ``yield from``
        or ``await``).

    The behavior differs from :func:`unittest.mock.patch` for generators.

    When used as a context manager, the patch is still active even if the
    generator or coroutine is paused, which may affect concurrent tasks::

        @asyncio.coroutine
        def coro():
            with asynctest.mock.patch("module.function"):
                yield from asyncio.get_event_loop().sleep(1)

        @asyncio.coroutine
        def independent_coro():
            assert not isinstance(module.function, asynctest.mock.Mock)

        asyncio.create_task(coro())
        asyncio.create_task(independent_coro())
        # this will raise an AssertionError(coro() is scheduled first)!
        loop.run_forever()

    :param scope: :const:`asynctest.GLOBAL` or :const:`asynctest.LIMITED`,
        controls when the patch is activated on generators and coroutines

    When used as a decorator with a generator based coroutine, the order of
    the decorators matters. The order of the ``@patch()`` decorators is in
    the reverse order of the parameters produced by these patches for the
    patched function. And the ``@asyncio.coroutine`` decorator should be
    the last since ``@patch()`` conceptually patches the coroutine, not
    the function::

        @patch("module.function2")
        @patch("module.function1")
        @asyncio.coroutine
        def test_coro(self, mock_function1, mock_function2):
            yield from asyncio.get_event_loop().sleep(1)

    see :func:`unittest.mock.patch()`.

    .. versionadded:: 0.6 patch into generators and coroutines with
                      a decorator.
    )r�)r'rZ_get_targetr�r�)rr6rNr�r\r�r�r�rr�r�r�rrr�patchpsArc	s0t�fdd�||||||||	|d�
}
t|
||�S)Ncs�S)Nrr)rrr�<lambda>��z_patch_object.<locals>.<lambda>)r�)r�r�)rr�r6rNr�r\r�r�r�rr�r)rr�
_patch_object�sr
c
s�t��tkr�fdd�}n�fdd�}|s2td��t|���}	|	d\}
}t||
|||||�i|d�
}|
|_xF|	dd�D]6\}
}t||
|||||�i|d�
}
|
|
_|j�|
�qzW�fdd	�}||�}tt	||j��|_|S)
Ncstj���S)N)r'r�	_importerr)rrrr��sz_patch_multiple.<locals>.gettercs�S)Nrr)rrrr��sz=Must supply at least one keyword argument with patch.multipler)r�rQcst||j��S)N)r�r6)r�)r�rr�_update�sz _patch_multiple.<locals>._update)
r3�strr�r��itemsr�r�r�rM�map)rrNr�r\r�r�r�rr�rr�r6r�Zthis_patcherrr)r�rr�_patch_multiple�s(rcsPeZdZddef�fdd�	Zdd�Zdd�Z�fd	d
�Zdd�Zd
d�Z	�Z
S)r%rFcs*t�j|||f|�||_d|_g|_dS)NF)r.rr�r&�_global_patchings)rr��values�clearr�r)r2rrr�sz_patch_dict.__init__cCs|j�|�dS)N)rrM)rZother_patchingrrrr��sz_patch_dict._keep_global_patchcCs\xVt|�D]J}t||�}|�tj�r
t|d�r
t|j|j|j	�}||�}t
|||�q
W|S)N�__call__)rKrL�
startswithr�TEST_PREFIXr�r%r�rrrt)rrUrOZ
attr_value�	decoratorZ	decoratedrrr�decorate_class�s

z_patch_dict.decorate_classcs:t|t�r|�|�St||�}|dkr2t��|�S|SdS)N)r$r3rr�r.r)rr�r)r2rrr�s


z_patch_dict.__call__cCs�d|_t|jt�r"tj�|j�|_y|j��|_Wn:t	k
rli|_x|jD]}|j||j|<qPWYnX|j
r~t|j�y|j�|j
�Wn4t	k
r�x|j
D]}|j
||j|<q�WYnXdS)NT)r&r$r�r
r'rrr��	_originalr�r�_clear_dictrar)r�keyrrrr%s 
z_patch_dict._patch_dictc	Cs�d|_|jtkrNx<|jD]2}||jks:|j||j|k	r|j||j|<qWt|j�|jg}x |jD]}|jrh|�|j�qhWxN|D]F}y|j�	|�Wq�t
k
r�x|D]}|||j|<q�WYq�Xq�WdS)NF)r&r�r�r�rrrrrMrar�)rrZ	originalsr(rwrrr�
_unpatch_dicts"




z_patch_dict._unpatch_dict)r
rrr�rr�rrr%rrArr)r2rr%�s

r%�testccs|Vx
|VqWdS)a�
    Helper to use with ``side_effect``, so a mock will return a given value
    only once, then return another value.

    When used as a ``side_effect`` value, if one of ``value`` or ``then`` is an
    :class:`Exception` type, an instance of this exception will be raised.

    >>> mock.recv = Mock(side_effect=return_once(b"data"))
    >>> mock.recv()
    b"data"
    >>> repr(mock.recv())
    'None'
    >>> repr(mock.recv())
    'None'

    >>> mock.recv = Mock(side_effect=return_once(b"data", then=BlockingIOError))
    >>> mock.recv()
    b"data"
    >>> mock.recv()
    Traceback BlockingIOError

    :param value: value to be returned once by the mock when called.

    :param then: value returned for any subsequent call.

    .. versionadded:: 0.4
    Nr)rGZthenrrr�return_onceHsr)FFNN)Nr�)N)ErrZasyncio.coroutinesr��enumr!r r�r+Z
unittest.mockr'�
coroutinesrFr�rrSrprZ_return_valuesrZ_side_effect_methodsrsZ_all_magicsrr#r&r3r)rDrHrJrVrWrdrlr9r:r;r=r{r<r�r��ANY�sentinelrr��Enumr�r�r�r��CoroWrapperr�r�rr
rr%rr4�dictZmultipleZ_patch_stopallZstopallr�callZPropertyMockrrrrr�<module>s�
$	E
%]e
}

X;DG"V