
    tfI                       d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZ dd	lmZ  G d
 d          Z e
d          Z e
d          Z G d de          Z G d de          Z G d de          ZdS )z?
Tests for Trial's interaction with the Python warning system.
    )annotationsN)StringIO)MappingSequenceTypeVar)
TestResult)FilePath)_collectWarnings_setWarningRegistryToNone_Warning)SynchronousTestCasec                  .    e Zd ZdZ G d de          ZdS )MaskzQ
    Hide a test case definition from trial's automatic discovery mechanism.
    c                  6    e Zd ZU dZdZeZded<   d
dZd
dZ	d	S )Mask.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 textztype[Warning]categoryreturnNonec                D    t          j        | j        | j                   dS )z@
            Generate a warning and don't flush it.
            N)warningswarnmessager   selfs    ^/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/trial/test/test_warning.pytest_unflushedzMask.MockTests.test_unflushed&   s      M$,66666    c                    t          j        | j        | j                   |                     t          |                                           d           dS )z:
            Generate a warning and flush it.
               N)r   r   r   r   assertEquallenflushWarningsr   s    r   test_flushedzMask.MockTests.test_flushed,   sJ     M$,666S!3!3!5!566:::::r   Nr   r   )
__name__
__module____qualname____doc__r   UserWarningr   __annotations__r   r$    r   r   	MockTestsr      s`         	 	
 &"-----	7 	7 	7 	7	; 	; 	; 	; 	; 	;r   r-   N)r&   r'   r(   r)   r   r-   r,   r   r   r   r      sI         ; ; ; ; ;' ; ; ; ; ;r   r   _K_Vc                      e Zd ZdZddZddZddZddZddZddZ	ddZ
ddZddZddZddZddZddZddZddZddZddZdS )FlushWarningsTestsze
    Tests for C{flushWarnings}, an API for examining the warnings
    emitted so far in a test.
    setMapping[_K, _V]subsetr   r   c                r    |                                 D ]!\  }}|                     ||         |           "dS )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   r2   r4   kvs        r   assertDictSubsetz#FlushWarningsTests.assertDictSubset>   sF    
 LLNN 	( 	(DAqSVQ''''	( 	(r   setsSequence[Mapping[_K, _V]]subsetsc                    |                      t          |          t          |                     t          ||          D ]\  }}|                     ||           dS )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)r!   r"   zipr9   )r   r:   r<   abs        r   assertDictSubsetsz$FlushWarningsTests.assertDictSubsetsF   sf     	TCLL111g&& 	( 	(DAq!!!Q''''	( 	(r   c                V    |                      |                                 g            dS )zg
        If no warnings are emitted by a test, C{flushWarnings} returns an empty
        list.
        N)r!   r#   r   s    r   	test_nonezFlushWarningsTests.test_noneR   s,    
 	++--r22222r   c                    d}t           }t          j        ||           d}t          }t          j        ||           |                     |                                 ||d||dg           dS )z|
        If several warnings are emitted by a test, C{flushWarnings} returns a
        list containing all of them.
        zfirst warning messager   r   zsecond warning messager   r   N)r*   r   r   RuntimeWarningrA   r#   )r   firstMessagefirstCategorysecondMessagesecondCategorys        r   test_severalzFlushWarningsTests.test_severalY   s    
 /#l]CCCC0'mnEEEE  *|DD+FF	
 	
 	
 	
 	
r   c                    d}t           }t          d          D ]}t          j        ||           |                     |                                 ||dgdz             dS )z
        The same warning triggered twice from the same place is included twice
        in the list returned by C{flushWarnings}.
        the message   rE   rF   N)rG   ranger   r   rA   r#   )r   r   r   is       r   test_repeatedz FlushWarningsTests.test_repeatedn   s    
  !q 	> 	>AM'H=====  W#M#M"NQR"R	
 	
 	
 	
 	
r   c                    d}t           }t          j        ||           |                     |                                 ||dg           |                     |                                 g            dS )z
        After a particular warning event has been returned by C{flushWarnings},
        it is not returned by subsequent calls.
        rN   rE   rF   N)rG   r   r   rA   r#   r!   )r   r   r   s      r   test_clearedzFlushWarningsTests.test_cleared|   s    
  !g9999  W#M#M"N	
 	
 	
 	++--r22222r   c                ~   t                      }t                              d          }|                    |           |                     t          j        j        g          }|                     |d         d         d           |                     |d         d         t                     t          |          j        j
        }|j        }|j        dz   }|                     |d         d         |           |                     |d         d         |           |                     t          |          d	           d
S )zx
        Any warnings emitted by a test which are not flushed are emitted to the
        Python warning system.
        r   r   r   r   r      filenamelinenor    N)r   r   r-   runr#   r   r!   assertIdenticalr*   type__code__co_filenameco_firstlinenor"   )r   resultcasewarningsShownwhererW   rX   s          r   r   z!FlushWarningsTests.test_unflushed   s   
 ~~.//**DN,I+JKKq))46IJJJ]1-j9;GGGT

)2$ %)q)*5x@@@q)(3V<<<]++Q/////r   c                L   t                      }t                              d          }t                      }|                     t
          d|          }|                    |           |                                 |                     |	                                d           dS )zx
        Any warnings emitted by a test which are flushed are not emitted to the
        Python warning system.
        r$   stdout N)
r   r   r-   r   patchsysrY   restorer!   getvalue)r   r_   r`   outputmonkeys        r   r$   zFlushWarningsTests.test_flushed   s    
 ~~n--C622**B/////r   c                    G d dt                     }t                      }t                              d          }||_        t
          j        dd         }	 t          j        d           |                    |           | 	                    t          |j                  d           |                     |j        d         d         |           |                     |j        d         d                                         d                             d	                     |t
          j        dd<   dS # |t
          j        dd<   w xY w)
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                      e Zd ZdS )IFlushWarningsTests.test_warningsConfiguredAsErrors.<locals>.CustomWarningNr&   r'   r(   r,   r   r   CustomWarningrn              Dr   rp   r   Nerrorr    r   z CustomWarning: some warning text)Warningr   r   r-   r   r   filterssimplefilterrY   r!   r"   errorsrZ   
assertTrue
splitlinesendswithr   rp   r_   r`   originalWarningss        r   test_warningsConfiguredAsErrorsz2FlushWarningsTests.test_warningsConfiguredAsErrors   sH   	 	 	 	 	G 	 	 	 ~~.//%#+AAA.	3!'***HHVS//333  q!1!!4d;;;OO a #b"<==   #3HQQQ"2HQQQ2222s   C	D4 4Ec                    G d dt                     }t                      }t                              d          }||_        t
          j        dd         }	 t          j        d           |                    |           | 	                    |j
        g            |t
          j        dd<   dS # |t
          j        dd<   w xY w)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                      e Zd ZdS )PFlushWarningsTests.test_flushedWarningsConfiguredAsErrors.<locals>.CustomWarningNro   r,   r   r   rp   r      rq   r   rp   r$   Nrr   )rt   r   r   r-   r   r   ru   rv   rY   r!   rw   r{   s        r   &test_flushedWarningsConfiguredAsErrorsz9FlushWarningsTests.test_flushedWarningsConfiguredAsErrors   s    	 	 	 	 	G 	 	 	 ~~n--%#+AAA.	3!'***HHVV]B///"2HQQQ"2HQQQ2222s   AB/ /Cc                *   t          j        d           |                     t          |                                           d           t          j        d           |                     t          |                                           d           dS )z
        Any warnings emitted after a call to C{flushWarnings} can be flushed by
        another call to C{flushWarnings}.
        zfirst messager    zsecond messageN)r   r   r!   r"   r#   r   s    r   test_multipleFlushesz'FlushWarningsTests.test_multipleFlushes   s    
 	o&&&T//1122A666&'''T//1122A66666r   c                2   dt           d
fd}dt          d
fd} |              |             |                     |                     |g          dg           |                     |                     |g          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.
        zfirst warning textr   r   c                 6    t          j         d           d S Nr    )
stacklevelr   r   )rI   rH   s   r   onez>FlushWarningsTests.test_filterOnOffendingFunction.<locals>.one   s     M,!DDDDDDr   z	some textc                 6    t          j         d           d S r   r   )rK   rJ   s   r   twoz>FlushWarningsTests.test_filterOnOffendingFunction.<locals>.two   s     M-AFFFFFFr   offendingFunctionsrF   Nr%   )r*   rG   rA   r#   )r   r   r   rI   rH   rK   rJ   s      @@@@r   test_filterOnOffendingFunctionz1FlushWarningsTests.test_filterOnOffendingFunction   s    ,#	E 	E 	E 	E 	E 	E 	E $'	G 	G 	G 	G 	G 	G 	G 	3%88'LAAB	
 	
 	
 	3%88(]CCD	
 	
 	
 	
 	
r   c                    dd} |             |                      t          |                     |g                    d           dS )z
        Verify that warnings emitted at the very edges of a function are still
        determined to be emitted from that function.
        r   r   c                 ~    t          j        d           t          j        d           t          j        d           d S )Nzfirst line warningzinternal line warningzlast line warningr   r,   r   r   warnerz:FlushWarningsTests.test_functionBoundaries.<locals>.warner  s;    M.///M1222M-.....r   r      Nr%   r!   r"   r#   )r   r   s     r   test_functionBoundariesz*FlushWarningsTests.test_functionBoundaries	  sZ    	/ 	/ 	/ 	/
 	T//F8/LLMMqQQQQQr   c                    t          j        d           |                     t          | j        dg           |                     t          |                                           d           dS )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.
        oh noNr    )r   r   assertRaises
ValueErrorr#   r!   r"   r   s    r   test_invalidFilterz%FlushWarningsTests.test_invalidFilter  sa     	g*d&84&AAAT//1122A66666r   c                   t          |                                                     d                                        d          }|                                 |                    d                              d           |                    d                              d           |                                j                            d          }t          j        
                    d|           |                     t          j        j        |           ddlm} |                     t          j        j        d	           |                     t          j        j        |j                   |                    d                                           |                                 |                     t)          |                     |j        g                    d
           dS )zx
        Warnings emitted by a function the source code of which is not
        available can still be flushed.
        utf-8   twisted_private_helper   __init__.pyr   s   missingsourcefile.py7   
import warnings
def foo():
    warnings.warn("oh no")
r   )missingsourcefiletwisted_private_helperr    N)r	   mktempencodechildmakedirs
setContentparentpathdecoderg   insert
addCleanupremover   r   modulespopr&   foor!   r"   r#   )r   package	pathEntryr   s       r   test_missingSourcez%FlushWarningsTests.test_missingSource!  s   
 4;;==//8899??%
 
 	n%%00555-..99	
 	
 	
 NN$$)0099	9%%%333
	
 	
 	
 	
 	
 	
 	)ABBB):)CDDD-..55777T//1B1F0GHHII1MMMMMr   c                f   t          |                                                     d                                        d          }|                                 |                    d                              d           |                    d                              d           |                                j                            d          }t          j        
                    d|           |                     t          j        j        |           ddlm} t          j        d	= t          j        |j        = 	 dd
lm}  |             n# t&          $ r Y nw xY w|                    |                    d                     ddlm} |                     t          j        j        d           |                     t          j        j        |j                   |                                 |                     t5          |                     |j        g                    d           dS )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.
        r   r   r   r   s	   module.pyr   r   )moduler   )invalidate_cachess   twisted_renamed_helpertwisted_renamed_helperr    N)r	   r   r   r   r   r   r   r   r   rg   r   r   r   r   r   r   r&   	importlibr   ImportErrormoveTosiblingr   r   r   r!   r"   r#   )r   r   r   r   r   s        r   test_renamedSourcez%FlushWarningsTests.test_renamedSourceD  s    4;;==//8899??%
 
 	n%%00555l##..	
 	
 	
 NN$$)0099	9%%%333 	211111 K01K(
	 333333   	 	 	D	 	w'@AABBB 	211111)ABBB999 	

 	T//==>>BBBBBs   E 
E! E!c                    d	d
d} |             |                      t          |                     |g                    d           dS )a  
        In Python 3.6 the dis.findlinestarts documented behaviour
        was changed such that the reported lines might not be sorted ascending.
        In Python 3.10 PEP 626 introduced byte-code change such that the last
        line of a function wasn't always associated with the last byte-code.
        In the past flushWarning was not detecting that such a function was
        associated with any warnings.
        r    r?   intr@   r   r   c                >    | r|rt          j        d           d S d S d S )Nr   r   )r?   r@   s     r   r   zCFlushWarningsTests.test_offendingFunctions_deep_branch.<locals>.foo  s7      M'*****D	 r   N)r    r    )r?   r   r@   r   r   r   r   )r   r   s     r   #test_offendingFunctions_deep_branchz6FlushWarningsTests.test_offendingFunctions_deep_branch  s^    	 	 	 	 	 	 	T//6677;;;;;r   N)r2   r3   r4   r3   r   r   )r:   r;   r<   r;   r   r   r%   )r&   r'   r(   r)   r9   rA   rC   rL   rR   rT   r   r$   r}   r   r   r   r   r   r   r   r   r,   r   r   r1   r1   8   s        
( ( ( (
( 
( 
( 
(3 3 3 3
 
 
 
*
 
 
 
3 3 3 30 0 0 0.0 0 0 03 3 3 3<3 3 3 3,7 7 7 7
 
 
 
<R R R R7 7 7 7!N !N !N !NF:C :C :C :Cx< < < < < <r   r1   c                      e Zd ZdS )FakeWarningNro   r,   r   r   r   r     s        Dr   r   c                  B    e Zd ZdZddZddZddZddZddZdd	Z	d
S )CollectWarningsTestsz(
    Tests for L{_collectWarnings}.
    r   r   c                   dddd         t          ddd          g dfd	}t          j        |           |                     d
         d           t	          d         t
                    sJ |                     d         j                   t	          d         t
                    sJ |                     d         j                   t	          d         t
                    sJ |                     d         j        t                               |                     d         d           |                     t                    d           dS )zS
        L{_collectWarnings} calls the observer with each emitted warning.
        zdummy calls observer warningNrs   r    rO   r   r   r   c                                           d           t          j                   t          j                   t          j                                         d           d S )Ncall	returning)appendr   r   )eventsrH   rJ   thirdMessages   r   fz2CollectWarningsTests.test_callsObserver.<locals>.f  s]    MM&!!!M,'''M-(((M,'''MM+&&&&&r   r   r   rV   r      r%   )	rt   r
   r   r!   
isinstancer   r   strr"   )r   r   r   rH   rJ   r   s     @@@@r   test_callsObserverz'CollectWarningsTests.test_callsObserver  sw    6$TTrT*q!Q''')	' 	' 	' 	' 	' 	' 	' 	' 	' 	***F+++&)X.....*L999&)X.....*M:::&)X.....*C,=,=>>>K000Va(((((r   c                    t                      }|                     t          d|           t          d t          j        d           |                     |                                d           dS )z
        Any warnings emitted by a call to a function passed to
        L{_collectWarnings} are not actually emitted to the warning system.
        rd   c                    d S Nr,   xs    r   <lambda>z6CollectWarningsTests.test_suppresses.<locals>.<lambda>  s    4 r   textre   N)r   rf   rg   r
   r   r   r!   ri   )r   rj   s     r   test_suppressesz$CollectWarningsTests.test_suppresses  s_    
 

3&)))???**B/////r   c                    g t                      dfd}t          d |ddd	d
          }|                     dd	d
dfg           |                     |           dS )z
        L{_collectWarnings} returns the result of calling the callable passed to
        it with the parameters given.
        argsobjectkwargsr   c                 6                         | |f           S r   )r   )r   r   	argumentsvalues     r   r   z2CollectWarningsTests.test_callsFunction.<locals>.f  s     dF^,,,Lr   c                    d S r   r,   r   s    r   r   z9CollectWarningsTests.test_callsFunction.<locals>.<lambda>  s    D r   r    r?   rO   d)r@   c)r    r?   N)r   r   r   r   r   r   )r   r
   r!   rZ   )r   r   r_   r   r   s      @@r   test_callsFunctionz'CollectWarningsTests.test_callsFunction  s    
 		 	 	 	 	 	 	 "..!QqCHHHhac0B0B%C$DEEEVU+++++r   c                l   b d	d}t          j        d            |             g }t          |j        |           |                     t          |          d           |                     |d         j        d           |                     t          |                                           d           dS )
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__}).
        r   r   c                 .    t          j        d           d S )Nr   r   r,   r   r   r   z>CollectWarningsTests.test_duplicateWarningCollected.<locals>.f  s    M%     r   defaultr    r   r   Nr%   )	__warningregistry__r   rv   r
   r   r!   r"   r   r#   )r   r   r   s      r   test_duplicateWarningCollectedz3CollectWarningsTests.test_duplicateWarningCollected  s      	! 	! 	! 	! 	i(((	!#***Va(((*E222T//1122A66666r   c                    t                      }|t          j        |<   |                     t          j        j        |           |                                  dS )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   rg   r   r   r   r   r   keys     r   test_immutableObjectz)CollectWarningsTests.test_immutableObject  sI     hhC---++-----r   c                b   i  G fdd          }t                      }t                      } ||          |<   t                      }t                      } ||          |<   t                     |                     ||||ht                                                               dS )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}.
        c                  $    e Zd Zd
dZd fdZd	S )KCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.Ar   r   r   r   c                    || j         d<   d S )N_key)__dict__r   s     r   __init__zTCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.A.__init__	  s    (+f%%%r   r   itemc                    d | j         <   d S r   )r   )r   r   r   r   s      r   __setattr__zWCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.A.__setattr__  s    #$)r   N)r   r   r   r   )r   r   r   r   r   r   )r&   r'   r(   r   r   )r   s   r   Ar     sG        , , , ,$ $ $ $ $ $ $ $r   r   N)r   r   r!   r2   keys)r   r   key1key2key3key4r   s         @r   +test_setWarningRegistryChangeWhileIteratingz@CollectWarningsTests.test_setWarningRegistryChangeWhileIterating  s     %'	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ xxxx!D''$xxxx!D''$!!$$$ 	$dD13qvvxx==AAAAAr   Nr%   )
r&   r'   r(   r)   r   r   r   r   r   r   r,   r   r   r   r     s         ) ) ) )80 0 0 0, , , , 7 7 7 72	. 	. 	. 	. B  B  B  B  B  Br   r   )r)   
__future__r   rg   r   ior   typingr   r   r   unittestr   twisted.python.filepathr	   twisted.trial._synctestr
   r   r   twisted.trial.unittestr   r   r.   r/   r1   rt   r   r   r,   r   r   <module>r     s    # " " " " " 



        - - - - - - - - - -       , , , , , ,         
 7 6 6 6 6 6; ; ; ; ; ; ; ;8 WT]]WT]]]< ]< ]< ]< ]<, ]< ]< ]<@	 	 	 	 	' 	 	 	B B B B B. B B B B Br   