o
    ¯bæD  ã                   @   s–   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ G dd	„ d	ƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZdS )z?
Tests for Trial's interaction with the Python warning system.
é    N)ÚStringIO)Ú
TestResult)ÚFilePath)Ú_collectWarningsÚ_setWarningRegistryToNone)ÚSynchronousTestCasec                   @   s    e Zd ZdZG dd„ deƒZdS )ÚMaskzQ
    Hide a test case definition from trial's automatic discovery mechanism.
    c                   @   s(   e Zd ZdZdZeZdd„ Zdd„ ZdS )z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 textc                 C   s   t  | j| j¡ dS )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!   s   zMask.MockTests.test_unflushedc                 C   s(   t  | j| j¡ |  t|  ¡ ƒd¡ dS )z:
            Generate a warning and flush it.
            é   N)r
   r   r   r   ÚassertEqualÚlenÚflushWarningsr   r   r   r   Útest_flushed'   s   zMask.MockTests.test_flushedN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚUserWarningr   r   r   r   r   r   r   Ú	MockTests   s    r   N)r   r   r   r   r   r   r   r   r   r   r      s    r   c                   @   s˜   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.
    c                 C   s&   |  ¡ D ]\}}|  || |¡ q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   ÚsetÚsubsetÚkÚvr   r   r   ÚassertDictSubset5   s   ÿz#FlushWarningsTests.assertDictSubsetc                 C   s8   |   t|ƒt|ƒ¡ t||ƒD ]
\}}|  ||¡ q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   Úzipr$   )r   ÚsetsÚsubsetsÚaÚbr   r   r   ÚassertDictSubsets=   s   ÿz$FlushWarningsTests.assertDictSubsetsc                 C   s   |   |  ¡ g ¡ dS )zg
        If no warnings are emitted by a test, C{flushWarnings} returns an empty
        list.
        N)r   r   r   r   r   r   Ú	test_noneG   s   zFlushWarningsTests.test_nonec                 C   sP   d}t }tj||d d}t}tj||d |  |  ¡ ||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   ÚRuntimeWarningr*   r   )r   ÚfirstMessageÚfirstCategoryÚsecondMessageÚsecondCategoryr   r   r   Útest_severalN   s   þþzFlushWarningsTests.test_severalc                 C   sD   d}t }tdƒD ]	}tj||d q|  |  ¡ ||dœgd ¡ dS )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   )r   r   r   Úir   r   r   Útest_repeatedc   s   ÿz FlushWarningsTests.test_repeatedc                 C   sB   d}t }tj||d |  |  ¡ ||dœg¡ |  |  ¡ g ¡ dS )z‰
        After a particular warning event has been returned by C{flushWarnings},
        it is not returned by subsequent calls.
        r4   r,   r-   N)r.   r
   r   r*   r   r   )r   r   r   r   r   r   Útest_clearedq   s   ÿzFlushWarningsTests.test_clearedc                 C   sª   t ƒ }t d¡}| |¡ |  tjjg¡}|  |d d d¡ |  |d d t¡ t	|ƒjj
}|j}|jd }|  |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Úwherer;   r<   r   r   r   r   ~   s   


z!FlushWarningsTests.test_unflushedc                 C   sJ   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Úsysr=   Úrestorer   Úgetvalue)r   rC   rD   ÚoutputÚmonkeyr   r   r   r   •   s   

zFlushWarningsTests.test_flushedc              
   C   s¸   G dd„ dt ƒ}tƒ }t d¡}||_tjdd… }z9t d¡ | |¡ |  	t
|jƒd¡ |  |jd d |¡ |  |jd d  ¡ d  d	¡¡ W |tjdd…< dS |tjdd…< 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 )zIFlushWarningsTests.test_warningsConfiguredAsErrors.<locals>.CustomWarningN©r   r   r   r   r   r   r   ÚCustomWarning©   ó    rQ   r   NÚerrorr   r   éÿÿÿÿz CustomWarning: some warning text)ÚWarningr   r   r   r   r
   ÚfiltersÚsimplefilterr=   r   r   Úerrorsr>   Ú
assertTrueÚ
splitlinesÚendswith©r   rQ   rC   rD   ÚoriginalWarningsr   r   r   Útest_warningsConfiguredAsErrors¢   s"   


ÿû"z2FlushWarningsTests.test_warningsConfiguredAsErrorsc              
   C   s|   G dd„ dt ƒ}tƒ }t d¡}||_tjdd… }zt d¡ | |¡ |  	|j
g ¡ W |tjdd…< dS |tjdd…< 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                   @   rO   )zPFlushWarningsTests.test_flushedWarningsConfiguredAsErrors.<locals>.CustomWarningNrP   r   r   r   r   rQ   Ç   rR   rQ   r   NrS   )rU   r   r   r   r   r
   rV   rW   r=   r   rX   r\   r   r   r   Ú&test_flushedWarningsConfiguredAsErrorsÀ   s   


"z9FlushWarningsTests.test_flushedWarningsConfiguredAsErrorsc                 C   s@   t  d¡ |  t|  ¡ ƒd¡ t  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   r   r   r   Útest_multipleFlushesÖ   s   

z'FlushWarningsTests.test_multipleFlushesc                    sx   d‰t ‰ ‡ ‡fdd„}d‰t‰‡‡fdd„}|ƒ  |ƒ  |  | j|gdˆ ˆdœg¡ |  | j|gdˆˆ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 textc                      ó   t jˆˆ dd d S ©Nr   )Ú
stacklevel©r
   r   r   )r0   r/   r   r   Úoneé   ó   z>FlushWarningsTests.test_filterOnOffendingFunction.<locals>.onez	some textc                      ra   rb   rd   r   )r2   r1   r   r   Útwoï   rf   z>FlushWarningsTests.test_filterOnOffendingFunction.<locals>.two©ÚoffendingFunctionsr-   N)r   r.   r*   r   )r   re   rg   r   )r0   r/   r2   r1   r   Útest_filterOnOffendingFunctionà   s    
þ
þz1FlushWarningsTests.test_filterOnOffendingFunctionc                 C   s,   dd„ }|ƒ  |   t| j|gdƒd¡ dS )z
        Verify that warnings emitted at the very edges of a function are still
        determined to be emitted from that function.
        c                   S   s"   t  d¡ t  d¡ t  d¡ d S )Nzfirst line warningzinternal line warningzlast line warningrd   r   r   r   r   Úwarner  s   

z:FlushWarningsTests.test_functionBoundaries.<locals>.warnerrh   é   N©r   r   r   )r   rk   r   r   r   Útest_functionBoundariesþ   s   z*FlushWarningsTests.test_functionBoundariesc                 C   s4   t  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   r   r   r   Útest_invalidFilter  s   
z%FlushWarningsTests.test_invalidFilterc                 C   sÐ   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__.pyó    s   missingsourcefile.pyó7   
import warnings
def foo():
    warnings.warn("oh no")
r   )ÚmissingsourcefileÚtwisted_private_helperr   N)r   ÚmktempÚencodeÚchildÚmakedirsÚ
setContentÚparentÚpathÚdecoderJ   ÚinsertÚ
addCleanupÚremovery   rx   ÚmodulesÚpopr   Úfoor   r   r   )r   ÚpackageÚ	pathEntryrx   r   r   r   Útest_missingSource  s"   ÿ
ÿz%FlushWarningsTests.test_missingSourcec                 C   s  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= zdd
lm} W n	 tyW   Y nw |ƒ  | | d¡¡ ddlm} |  t	jjd¡ |  t	jj|j¡ | ¡  |  t|  |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.
        rs   rt   ru   rv   s	   module.pyrw   r   )Úmodulery   )Úinvalidate_cachess   twisted_renamed_helperÚtwisted_renamed_helperr   N)r   rz   r{   r|   r}   r~   r   r€   r   rJ   r‚   rƒ   r„   ry   r‹   r…   r   Ú	importlibrŒ   ÚImportErrorÚmoveToÚsiblingr   r†   r‡   r   r   r   )r   rˆ   r‰   r‹   rŒ   r   r   r   Útest_renamedSource3  s4   ÿ
ÿ
ÿz%FlushWarningsTests.test_renamedSourcec                 C   s,   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   c                 S   s    | r|rt  d¡ d S 	 d S d S )Nro   rd   )r(   r)   r   r   r   r‡   y  s
   üzCFlushWarningsTests.test_offendingFunctions_deep_branch.<locals>.fooN)r   r   rm   )r   r‡   r   r   r   Ú#test_offendingFunctions_deep_brancho  s   

z6FlushWarningsTests.test_offendingFunctions_deep_branchN)r   r   r   r   r$   r*   r+   r3   r8   r9   r   r   r^   r_   r`   rj   rn   rr   rŠ   r’   r“   r   r   r   r   r   /   s&    


<r   c                   @   rO   )ÚFakeWarningNrP   r   r   r   r   r”   ‡  rR   r”   c                   @   s@   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}.
    c                    s®   d‰ˆddd… ‰t dddƒ‰g ‰ ‡ ‡‡‡fdd„}tˆ j|ƒ |  ˆ d	 d
¡ |  ˆ d jˆ¡ |  ˆ d jˆ¡ |  ˆ d jtˆƒ¡ |  ˆ d d¡ |  tˆ ƒd¡ dS )zS
        L{_collectWarnings} calls the observer with each emitted warning.
        zdummy calls observer warningNrT   r   r5   rl   c                      s6   ˆ   d¡ t ˆ¡ t ˆ¡ t ˆ¡ ˆ   d¡ d S )NÚcallÚ	returning)Úappendr
   r   r   ©Úeventsr/   r1   ÚthirdMessager   r   Úf™  s
   



z2CollectWarningsTests.test_callsObserver.<locals>.fr   r–   r:   r—   é   )rU   r   r˜   r   r   Ústrr   )r   rœ   r   r™   r   Útest_callsObserver  s   z'CollectWarningsTests.test_callsObserverc                 C   s:   t ƒ }|  td|¡ td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.
        rG   c                 S   ó   d S ©Nr   ©Úxr   r   r   Ú<lambda>°  ó    z6CollectWarningsTests.test_suppresses.<locals>.<lambda>ÚtextrH   N)r   rI   rJ   r   r
   r   r   rL   )r   rM   r   r   r   Útest_suppresses©  s   z$CollectWarningsTests.test_suppressesc                    sX   g ‰ t ƒ ‰‡ ‡fdd„}tdd„ |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.
        c                     s   ˆ   | |f¡ ˆS r¡   )r˜   )ÚargsÚkwargs©Ú	argumentsÚvaluer   r   rœ   »  s   z2CollectWarningsTests.test_callsFunction.<locals>.fc                 S   r    r¡   r   r¢   r   r   r   r¤   ¿  r¥   z9CollectWarningsTests.test_callsFunction.<locals>.<lambda>r   r(   r5   Úd)r)   Úc)r   r(   N)Úobjectr   r   r>   )r   rœ   rC   r   rª   r   Útest_callsFunction³  s   z'CollectWarningsTests.test_callsFunctionc                 C   sd   b dd„ }t 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__}).
        c                   S   s   t  d¡ d S )Nr‡   rd   r   r   r   r   rœ   Ñ  ó   z>CollectWarningsTests.test_duplicateWarningCollected.<locals>.fÚdefaultr   r   r‡   N)	Ú__warningregistry__r
   rW   r   r˜   r   r   r   r   )r   rœ   rš   r   r   r   Útest_duplicateWarningCollectedÃ  s   
z3CollectWarningsTests.test_duplicateWarningCollectedc                 C   s,   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¯   rJ   r…   rƒ   r†   r´   ©r   Úkeyr   r   r   Útest_immutableObjectÜ  s   
z)CollectWarningsTests.test_immutableObjectc                    sn   i ‰ G ‡ fd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                       s    e Zd Zdd„ Z‡ fdd„ZdS )zKCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.Ac                 S   s   || j d< d S )NÚ_key)Ú__dict__rµ   r   r   r   Ú__init__õ  r±   zTCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.A.__init__c                    s   d ˆ | j < d S r¡   )r¸   )r   r¬   Úitem©r­   r   r   Ú__setattr__ø  r±   zWCollectWarningsTests.test_setWarningRegistryChangeWhileIterating.<locals>.A.__setattr__N)r   r   r   rº   r½   r   r¼   r   r   ÚAô  s    r¾   N)r¯   r   r   r    Úkeys)r   r¾   Úkey1Úkey2Úkey3Úkey4r   r¼   r   Ú+test_setWarningRegistryChangeWhileIteratingç  s    z@CollectWarningsTests.test_setWarningRegistryChangeWhileIteratingN)
r   r   r   r   rŸ   r§   r°   r´   r·   rÄ   r   r   r   r   r•   ‹  s    
r•   )r   rJ   r
   Úior   Úunittestr   Útwisted.python.filepathr   Útwisted.trial._synctestr   r   Útwisted.trial.unittestr   r   r   rU   r”   r•   r   r   r   r   Ú<module>   s     Z