File: //lib/python3/dist-packages/twisted/internet/test/__pycache__/test_inlinecb.cpython-38.pyc
U
W[O- � @ s� d Z ddlmZmZ ddlZddlmZmZ ddlm Z m
Z
mZmZ G dd� de�Z
ejdk rdd e
_G d
d� de�ZG dd
� d
e�ZG dd� de�ZG dd� de�ZG dd� de�ZG dd� de�ZdS )z�
Tests for L{twisted.internet.defer.inlineCallbacks}.
Some tests for inlineCallbacks are defined in L{twisted.test.test_defgen} as
well.
� )�division�absolute_importN)�TestCase�SynchronousTestCase)�Deferred�returnValue�inlineCallbacks�CancelledErrorc @ s e Zd ZdZdd� ZdS )�StopIterationReturnTestsz�
On Python 3.4 and newer generator functions may use the C{return} statement
with a value, which is attached to the L{StopIteration} exception that is
raised.
L{inlineCallbacks} will use this value when it fires the C{callback}.
c C sD dt i}td|� t� }|d |�}|�d� | �| �|�d� dS )z�
If the C{return} statement has a value it is propagated back to the
L{Deferred} that the C{inlineCallbacks} function returned.
r z>
@inlineCallbacks
def f(d):
yield d
return 14
�fN� )r �execr �callback�assertEqual�successResultOf)�self�environZd1Zd2� r �E/usr/lib/python3/dist-packages/twisted/internet/test/test_inlinecb.py�test_returnWithValue s �
z-StopIterationReturnTests.test_returnWithValueN)�__name__�
__module__�__qualname__�__doc__r r r r r r
s r
)� � z#Test requires Python 3.4 or greaterc @ s0 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
S )�NonLocalExitTestsa�
It's possible for L{returnValue} to be (accidentally) invoked at a stack
level below the L{inlineCallbacks}-decorated function which it is exiting.
If this happens, L{returnValue} should report useful errors.
If L{returnValue} is invoked from a function not decorated by
L{inlineCallbacks}, it will emit a warning if it causes an
L{inlineCallbacks} function further up the stack to exit.
c C s t d� dS )z�
This method mistakenly invokes L{returnValue}, despite the fact that it
is not decorated with L{inlineCallbacks}.
� N)r �r r r r �mistakenMethodA s z NonLocalExitTests.mistakenMethodc C sZ | � |dg� | j| jgd�}| � t|�d� | � |d d t� | � |d d d� dS )ac
Flush the current warnings and assert that we have been told that
C{mistakenMethod} was invoked, and that the result from the Deferred
that was fired (appended to the given list) is C{mistakenMethod}'s
result. The warning should indicate that an inlineCallbacks function
called 'inline' was made to exit.
r )ZoffendingFunctionsr �category�messagez�returnValue() in 'mistakenMethod' causing 'inline' to exit: returnValue should only be invoked by functions decorated with inlineCallbacksN)r Z
flushWarningsr �len�DeprecationWarning)r Z
resultList�warningsr r r �assertMistakenMethodWarningI s
�z-NonLocalExitTests.assertMistakenMethodWarningc s4 t � fdd��}|� }g }|�|j� � �|� dS )z�
L{returnValue} will emit a non-local exit warning in the simplest case,
where the offending function is invoked immediately.
c 3 s � � � td� dV d S )N� r �r r r r r r �inlinea s zANonLocalExitTests.test_returnValueNonLocalWarning.<locals>.inlineN)r �addCallback�appendr% )r r( �d�resultsr r r �test_returnValueNonLocalWarning\ s z1NonLocalExitTests.test_returnValueNonLocalWarningc sR t � � t� �fdd��}|� }g }|�|j� ��|g � � �d� ��|� dS )z�
L{returnValue} will emit a non-local warning in the case where the
L{inlineCallbacks}-decorated function has already yielded a Deferred
and therefore moved its generator function along.
c 3 s � V �� � td� d S )Nr&