HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/trial/test/__pycache__/test_warning.cpython-38.pyc
U


W[�A�@s�dZddlmZmZddlZddlZddlmZddlm	Z
ddlmZddl
mZddlmZmZGd	d
�d
e�ZGdd�de�ZGd
d�de�ZGdd�de�ZdS)z?
Tests for Trial's interaction with the Python warning system.
�)�division�absolute_importN)�
TestResult)�NativeStringIO)�FilePath)�SynchronousTestCase)�_collectWarnings�_setWarningRegistryToNonec@s eZdZdZGdd�de�ZdS)�MaskzQ
    Hide a test case definition from trial's automatic discovery mechanism.
    c@s(eZdZdZdZeZdd�Zdd�ZdS)zMask.MockTestsz�
        A test case which is used by L{FlushWarningsTests} to verify behavior
        which cannot be verified by code inside a single test method.
        �some warning textcCst�|j|j�dS)z@
            Generate a warning and don't flush it.
            N)�warnings�warn�message�category��self�r�A/usr/lib/python3/dist-packages/twisted/trial/test/test_warning.py�test_unflushed szMask.MockTests.test_unflushedcCs(t�|j|j�|�t|���d�dS)z:
            Generate a warning and flush it.
            �N)rr
rr�assertEqual�len�
flushWarningsrrrr�test_flushed'szMask.MockTests.test_flushedN)	�__name__�
__module__�__qualname__�__doc__r�UserWarningrrrrrrr�	MockTestss
rN)rrrrrrrrrrr
sr
c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"S)#�FlushWarningsTestsze
    Tests for C{flushWarnings}, an API for examining the warnings
    emitted so far in a test.
    cCs&|��D]\}}|�|||�qdS)z�
        Assert that all the keys present in C{subset} are also present in
        C{set} and that the corresponding values are equal.
        N)�itemsr)r�setZsubset�k�vrrr�assertDictSubset6sz#FlushWarningsTests.assertDictSubsetcCs8|�t|�t|��t||�D]\}}|�||�qdS)z�
        For each pair of corresponding elements in C{sets} and C{subsets},
        assert that the element from C{subsets} is a subset of the element from
        C{sets}.
        N)rr�zipr%)rZsetsZsubsets�a�brrr�assertDictSubsets?sz$FlushWarningsTests.assertDictSubsetscCs|�|��g�dS)zg
        If no warnings are emitted by a test, C{flushWarnings} returns an empty
        list.
        N)rrrrrr�	test_noneJszFlushWarningsTests.test_nonecCsPd}t}tj||d�d}t}tj||d�|�|��||d�||d�g�dS)z|
        If several warnings are emitted by a test, C{flushWarnings} returns a
        list containing all of them.
        zfirst warning message�rrzsecond warning message�rrN)rrr
�RuntimeWarningr)r)r�firstMessage�
firstCategory�
secondMessage�secondCategoryrrr�test_severalRs��zFlushWarningsTests.test_severalcCsDd}t}td�D]}tj||d�q|�|��||d�gd�dS)z�
        The same warning triggered twice from the same place is included twice
        in the list returned by C{flushWarnings}.
        �the message�r+r,N)r-�rangerr
r)r)rrr�irrr�
test_repeatedes�z FlushWarningsTests.test_repeatedcCsBd}t}tj||d�|�|��||d�g�|�|��g�dS)z�
        After a particular warning event has been returned by C{flushWarnings},
        it is not returned by subsequent calls.
        r3r+r,N)r-rr
r)rr)rrrrrr�test_clearedts
�zFlushWarningsTests.test_clearedcCs�t�}t�d�}|�|�|�tjjg�}|�|ddd�|�|ddt�t	|�jj
}|j}|jd}|�|dd|�|�|dd|�|�t
|�d	�d
S)zx
        Any warnings emitted by a test which are not flushed are emitted to the
        Python warning system.
        rrrrr��filename�linenorN)rr
r�runrrr�assertIdenticalr�type�__code__�co_filename�co_firstlinenor)r�result�caseZ
warningsShown�wherer:r;rrrr�s


z!FlushWarningsTests.test_unflushedcCsJt�}t�d�}t�}|�td|�}|�|�|��|�|�	�d�dS)zx
        Any warnings emitted by a test which are flushed are not emitted to the
        Python warning system.
        r�stdout�N)
rr
r�StringIO�patch�sysr<Zrestorer�getvalue)rrBrC�outputZmonkeyrrrr�s

zFlushWarningsTests.test_flushedc
Cs�Gdd�dt�}t�}t�d�}||_tjdd�}zbt�d�|�|�|�	t
|j�d�|�|jdd|�|�
|jdd��d�d	��W5|tjdd�<XdS)
z�
        If a warnings filter has been installed which turns warnings into
        exceptions, tests have an error added to the reporter for them for each
        unflushed warning.
        c@seZdZdS)zIFlushWarningsTests.test_warningsConfiguredAsErrors.<locals>.CustomWarningN�rrrrrrr�
CustomWarning�srMrN�errorrr���z CustomWarning: some warning text)�Warningrr
rrr�filters�simplefilterr<rr�errorsr=Z
assertTrue�
splitlines�endswith�rrMrBrCZoriginalWarningsrrr�test_warningsConfiguredAsErrors�s 


��z2FlushWarningsTests.test_warningsConfiguredAsErrorsc
CspGdd�dt�}t�}t�d�}||_tjdd�}z&t�d�|�|�|�	|j
g�W5|tjdd�<XdS)z�
        If a warnings filter has been installed which turns warnings into
        exceptions, tests which emit those warnings but flush them do not have
        an error added to the reporter.
        c@seZdZdS)zPFlushWarningsTests.test_flushedWarningsConfiguredAsErrors.<locals>.CustomWarningNrLrrrrrM�srMrNrN)rPrr
rrrrQrRr<rrSrVrrr�&test_flushedWarningsConfiguredAsErrors�s


z9FlushWarningsTests.test_flushedWarningsConfiguredAsErrorscCs@t�d�|�t|���d�t�d�|�t|���d�dS)z�
        Any warnings emitted after a call to C{flushWarnings} can be flushed by
        another call to C{flushWarnings}.
        z
first messagerzsecond messageN)rr
rrrrrrr�test_multipleFlushes�s

z'FlushWarningsTests.test_multipleFlushescsxd�t���fdd�}d�t���fdd�}|�|�|�|j|gd���d�g�|�|j|gd���d�g�d	S)
z�
        The list returned by C{flushWarnings} includes only those
        warnings which refer to the source of the function passed as the value
        for C{offendingFunction}, if a value is passed for that parameter.
        zfirst warning textcstj��dd�dS�Nr)�
stacklevel�rr
r)r/r.rr�one�sz>FlushWarningsTests.test_filterOnOffendingFunction.<locals>.onez	some textcstj��dd�dSrZr\r)r1r0rr�two�sz>FlushWarningsTests.test_filterOnOffendingFunction.<locals>.two�ZoffendingFunctionsr,N)rr-r)r)rr]r^r)r/r.r1r0r�test_filterOnOffendingFunction�s 
�
�z1FlushWarningsTests.test_filterOnOffendingFunctioncCs,dd�}|�|�t|j|gd��d�dS)z�
        Verify that warnings emitted at the very edges of a function are still
        determined to be emitted from that function.
        cSs"t�d�t�d�t�d�dS)Nzfirst line warningzinternal line warningzlast line warningr\rrrr�warners

z:FlushWarningsTests.test_functionBoundaries.<locals>.warnerr_�N)rrr)rrarrr�test_functionBoundariess�z*FlushWarningsTests.test_functionBoundariescCs4t�d�|�t|jdg�|�t|���d�dS)z�
        If an object which is neither a function nor a method is included in the
        C{offendingFunctions} list, C{flushWarnings} raises L{ValueError}.  Such
        a call flushes no warnings.
        zoh noNr)rr
ZassertRaises�
ValueErrorrrrrrrr�test_invalidFilters
z%FlushWarningsTests.test_invalidFiltercCs�t|���d���d�}|��|�d��d�|�d��d�|��j�d�}t	j�
d|�|�t	jj|�ddl
m}|�t	jjd	�|�t	jj|j�|�d���|��|�t|�|jg��d
�dS)zx
        Warnings emitted by a function the source code of which is not
        available can still be flushed.
        �utf-8�twisted_private_helper�__init__.py�smissingsourcefile.py�7
import warnings
def foo():
    warnings.warn("oh no")
r)�missingsourcefile�twisted_private_helperrN)r�mktemp�encode�child�makedirs�
setContent�parent�path�decoderI�insert�
addCleanup�removerlrk�modules�popr�foorrr)r�package�	pathEntryrkrrr�test_missingSourcesz%FlushWarningsTests.test_missingSourcecCst|���d���d�}|��|�d��d�|�d��d�|��j�d�}t	j�
d|�|�t	jj|�ddl
m}t	jd	=t	j|j=zdd
lm}Wntk
r�YnX|�|�|�d��ddlm}|�t	jjd�|�t	jj|j�|��|�t|�|jg��d
�dS)a

        Warnings emitted by a function defined in a file which has been renamed
        since it was initially compiled can still be flushed.

        This is testing the code which specifically supports working around the
        unfortunate behavior of CPython to write a .py source file name into
        the .pyc files it generates and then trust that it is correct in
        various places.  If source files are renamed, .pyc files may not be
        regenerated, but they will contain incorrect filenames.
        rfrgrhris	module.pyrjr)�modulerl)�invalidate_cachesstwisted_renamed_helper�twisted_renamed_helperrN)rrmrnrorprqrrrsrtrIrurvrwrlr~rxr�	importlibr�ImportErrorZmoveToZsiblingr�ryrzrrr)rr{r|r~rrrr�test_renamedSource3s*
z%FlushWarningsTests.test_renamedSourceN)rrrrr%r)r*r2r7r8rrrWrXrYr`rcrer}r�rrrrr 0s"	r c@seZdZdS)�FakeWarningNrLrrrrr�lsr�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�CollectWarningsTestsz(
    Tests for L{_collectWarnings}.
    cs�d��ddd��tddd��g�����fdd�}t�j|�|��d	d
�|��dj��|��dj��|��djt���|��dd�|�t��d
�dS)zS
        L{_collectWarnings} calls the observer with each emitted warning.
        zdummy calls observer warningNrOrr4rbcs6��d�t���t���t�����d�dS)N�call�	returning)�appendrr
r��eventsr.r0ZthirdMessagerr�f}s




z2CollectWarningsTests.test_callsObserver.<locals>.frr�r9r��)rPrr�rr�strr)rr�rr�r�test_callsObserverusz'CollectWarningsTests.test_callsObservercCs:t�}|�td|�tdd�tjd�|�|��d�dS)z�
        Any warnings emitted by a call to a function passed to
        L{_collectWarnings} are not actually emitted to the warning system.
        rEcSsdS�Nr��xrrr�<lambda>�riz6CollectWarningsTests.test_suppresses.<locals>.<lambda>�textrFN)rGrHrIrrr
rrJ)rrKrrr�test_suppresses�sz$CollectWarningsTests.test_suppressescsXg�t����fdd�}tdd�|ddddd	�}|��d
ddd	�fg�|�|��dS)z�
        L{_collectWarnings} returns the result of calling the callable passed to
        it with the parameters given.
        cs��||f��Sr�)r�)�args�kwargs�Z	arguments�valuerrr��sz2CollectWarningsTests.test_callsFunction.<locals>.fcSsdSr�rr�rrrr��riz9CollectWarningsTests.test_callsFunction.<locals>.<lambda>rr'r4�d)r(�c)rr'N)�objectrrr=)rr�rBrr�r�test_callsFunction�sz'CollectWarningsTests.test_callsFunctioncCsdbdd�}t�d�|�g}t|j|�|�t|�d�|�|djd�|�t|���d�dS)z�
        Subsequent emissions of a warning from a particular source site can be
        collected by L{_collectWarnings}.  In particular, the per-module
        emitted-warning cache should be bypassed (I{__warningregistry__}).
        cSst�d�dS)Nrzr\rrrrr��sz>CollectWarningsTests.test_duplicateWarningCollected.<locals>.f�defaultrrrzN)	Z__warningregistry__rrRrr�rrrr)rr�r�rrr�test_duplicateWarningCollected�s
z3CollectWarningsTests.test_duplicateWarningCollectedcCs,t�}|tj|<|�tjj|�|��dS)z�
        L{_collectWarnings}'s behavior is not altered by the presence of an
        object which cannot have attributes set on it as a value in
        C{sys.modules}.
        N)r�rIrxrvryr��r�keyrrr�test_immutableObject�s
z)CollectWarningsTests.test_immutableObjectcsti�G�fdd�dt�}t�}t�}||��|<t�}t�}||��|<t��|�t||||g�t�����dS)a�
        If the dictionary passed to L{_setWarningRegistryToNone} changes size
        partway through the process, C{_setWarningRegistryToNone} continues to
        set C{__warningregistry__} to L{None} on the rest of the values anyway.


        This might be caused by C{sys.modules} containing something that's not
        really a module and imports things on setattr.  py.test does this, as
        does L{twisted.python.deprecate.deprecatedModuleAttribute}.
        cs eZdZdd�Z�fdd�ZdS)zKCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.AcSs||jd<dS)N�_key)�__dict__r�rrr�__init__�szTCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.A.__init__csd�|j<dSr�)r�)rr��item�r�rr�__setattr__�szWCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.A.__setattr__N)rrrr�r�rr�rr�A�sr�N)r�r	rr"�keys)rr�Zkey1Zkey2Zkey3Zkey4rr�r�+test_setWarningRegistryChangeWhileIterating�sz@CollectWarningsTests.test_setWarningRegistryChangeWhileIteratingN)
rrrrr�r�r�r�r�r�rrrrr�qsr�)rZ
__future__rrrIrZunittestrZtwisted.python.compatrrGZtwisted.python.filepathrZtwisted.trial.unittestrZtwisted.trial._synctestrr	r�r
r rPr�r�rrrr�<module>s>