ϪfIdZddlmZddlZddlZddlmZddlmZm Z m Z ddl m Z ddl mZddlmZmZmZdd lmZGd d Ze d Ze d ZGddeZGddeZGddeZy)z? Tests for Trial's interaction with the Python warning system. ) annotationsN)StringIO)MappingSequenceTypeVar) TestResult)FilePath)_collectWarnings_setWarningRegistryToNone_Warning)SynchronousTestCasec&eZdZdZGddeZy)MaskzQ Hide a test case definition from trial's automatic discovery mechanism. c4eZdZUdZdZeZded<ddZddZ y) 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]categorycXtj|j|jy)z@ Generate a warning and don't flush it. N)warningswarnmessagerselfs A/usr/lib/python3/dist-packages/twisted/trial/test/test_warning.pytest_unflushedzMask.MockTests.test_unflushed&s MM$,, 6ctj|j|j|j t |j dy)z: Generate a warning and flush it. N)rrrr assertEquallen flushWarningsrs r test_flushedzMask.MockTests.test_flushed,s8 MM$,, 6   S!3!3!56 :rNreturnNone) __name__ __module__ __qualname____doc__r UserWarningr__annotations__rr"rr MockTestsrs" &"--- 7  ;rr-N)r&r'r(r)r r-r,rrrrs;';rr_K_VceZdZdZddZ ddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZddZddZddZddZddZy)FlushWarningsTestsze Tests for C{flushWarnings}, an API for examining the warnings emitted so far in a test. c^|jD]\}}|j|||y)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)rsetsubsetkvs rassertDictSubsetz#FlushWarningsTests.assertDictSubset>s1 LLN (DAq   SVQ ' (rc|jt|t|t||D]\}}|j||y)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 zipr8)rsetssubsetsabs rassertDictSubsetsz$FlushWarningsTests.assertDictSubsetsFsF TCL1g& (DAq  ! !!Q ' (rcD|j|jgy)zg If no warnings are emitted by a test, C{flushWarnings} returns an empty list. N)rr!rs r test_nonezFlushWarningsTests.test_noneRs ++-r2rcd}t}tj||d}t}tj|||j |j ||d||dgy)z| If several warnings are emitted by a test, C{flushWarnings} returns a list containing all of them. zfirst warning messagerrzsecond warning messagerrN)r*rrRuntimeWarningr?r!)r firstMessage firstCategory secondMessagesecondCategorys r test_severalzFlushWarningsTests.test_severalYsc / #  l]C0 ' mnE     *|D+ F  rcd}t}tdD]}tj|||j |j ||dgdzy)z The same warning triggered twice from the same place is included twice in the list returned by C{flushWarnings}. the messagerCrDN)rErangerrr?r!)rrris r test_repeatedz FlushWarningsTests.test_repeatedns[  !q >A MM'H = >     W#M"NQR"R rcd}t}tj|||j|j ||dg|j |j gy)z After a particular warning event has been returned by C{flushWarnings}, it is not returned by subsequent calls. rLrCrDN)rErrr?r!r)rrrs r test_clearedzFlushWarningsTests.test_cleared|s[  ! g9     W#M"N  ++-r2rc*t}tjd}|j||j tjj g}|j |ddd|j|ddtt|j j}|j}|jdz}|j |dd||j |dd||j t|d y ) zx Any warnings emitted by a test which are not flushed are emitted to the Python warning system. rrrrrfilenamelinenorN)rrr-runr!rrassertIdenticalr*type__code__ co_filenameco_firstlinenor )rresultcase warningsShownwhererUrVs rrz!FlushWarningsTests.test_unflusheds ~~./ **DNN,I,I+JK  q))46IJ ]1-j9;GT ))22$$%%) q)*5x@ q)(3V< ]+Q/rct}tjd}t}|j t d|}|j ||j|j|jdy)zx Any warnings emitted by a test which are flushed are not emitted to the Python warning system. r"stdoutN) rrr-rpatchsysrWrestorergetvalue)rr]r^outputmonkeys rr"zFlushWarningsTests.test_flusheds_ ~~n-C62  *B/rcHGddt}t}tjd}||_t j dd} t jd|j||jt|jd|j|jdd||j|jddjdjd |t j ddy#|t j ddwxYw) 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 eZdZy)IFlushWarningsTests.test_warningsConfiguredAsErrors..CustomWarningNr&r'r(r,rr CustomWarningrl rrnrNerrorrrz CustomWarning: some warning text)Warningrrr-rrfilters simplefilterrWrr errorsrX assertTrue splitlinesendswithrrnr]r^originalWarningss rtest_warningsConfiguredAsErrorsz2FlushWarningsTests.test_warningsConfiguredAsErrorss G ~~./% #++A. 3  ! !' * HHV    S/ 3  q!1!!4d ; OO a #b"<=  #3H  Q "2H  Q s B.D D!cpGddt}t}tjd}||_t j dd} t jd|j||j|jg|t j ddy#|t j ddwxYw)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 eZdZy)PFlushWarningsTests.test_flushedWarningsConfiguredAsErrors..CustomWarningNrmr,rrrnr~rorrnr"Nrp) rrrrr-rrrsrtrWrrurys r&test_flushedWarningsConfiguredAsErrorsz9FlushWarningsTests.test_flushedWarningsConfiguredAsErrorss G ~~n-% #++A. 3  ! !' * HHV    V]]B /"2H  Q "2H  Q s AB B5ctjd|jt|j dtjd|jt|j dy)z Any warnings emitted after a call to C{flushWarnings} can be flushed by another call to C{flushWarnings}. z first messagerzsecond messageN)rrrr r!rs rtest_multipleFlushesz'FlushWarningsTests.test_multipleFlushessY  o& T//12A6 &' T//12A6rcdtdfd }dtdfd }|||j|j|gdg|j|j|gdgy) 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 textc6tjdyNr) stacklevelrr)rGrFsronez>FlushWarningsTests.test_filterOnOffendingFunction..ones MM, ! Drz some textc6tjdyrr)rIrHsrtwoz>FlushWarningsTests.test_filterOnOffendingFunction..twos MM-A FroffendingFunctionsrDNr#)r*rEr?r!)rrrrGrFrIrHs @@@@rtest_filterOnOffendingFunctionz1FlushWarningsTests.test_filterOnOffendingFunctions , #  E$ ' G      3%  8'L A B     3%  8(] C D rcrdd}||jt|j|gdy)z Verify that warnings emitted at the very edges of a function are still determined to be emitted from that function. ctjdtjdtjdy)Nzfirst line warningzinternal line warningzlast line warningrr,rrwarnerz:FlushWarningsTests.test_functionBoundaries..warners) MM. / MM1 2 MM- .rrNr#rr r!)rrs rtest_functionBoundariesz*FlushWarningsTests.test_functionBoundaries s3  /  T//F8/LMqQrctjd|jt|jdg|j t |j dy)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)rr assertRaises ValueErrorr!rr rs rtest_invalidFilterz%FlushWarningsTests.test_invalidFiltersH  g *d&8&84&A T//12A6rct|jjdjd}|j |jdj d|jdj d|j jjd}tjjd||jtjj|ddl m}|jtjj d |jtjj |j"|jdj|j%|j't)|j+|j$gd y ) zx Warnings emitted by a function the source code of which is not available can still be flushed. utf-8twisted_private_helper __init__.pyrsmissingsourcefile.py7 import warnings def foo(): warnings.warn("oh no") r)missingsourcefiletwisted_private_helperrN)r mktempencodechildmakedirs setContentparentpathdecodereinsert addCleanupremoverrmodulespopr&foorr r!)rpackage pathEntryrs rtest_missingSourcez%FlushWarningsTests.test_missingSource!s= 4;;=//89?? %   n%005 -.99  NN$))009  9% 3    )AB  ):)C)CD -.557 T//1B1F1F0GHI1Mrc(t|jjdjd}|j |jdj d|jdj d|j jjd}tjjd||jtjj|ddl m}tjd =tj|j = dd lm}||j)|j+d ddlm}|jtjj.d |jtjj.|j |j1|j3t5|j7|j0gd y#t&$rYwxYw)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. rrrrs module.pyrr)moduler)invalidate_cachesstwisted_renamed_helpertwisted_renamed_helperrN)r rrrrrrrrrerrrrrrr& importlibr ImportErrormoveTosiblingrrrrr r!)rrrrrs rtest_renamedSourcez%FlushWarningsTests.test_renamedSourceDs~4;;=//89?? %   n%005 l#..  NN$))009  9% 3 2 KK0 1 KK (  3   w'@AB 2  )AB  9   T// =>B%   s.H HHcrddd}||jt|j|gdy)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. rc:|r|rtjdyyy)Nrr)r=r>s rrzCFlushWarningsTests.test_offendingFunctions_deep_branch..foosMM'* rN)rr)r=intr>rr$r%r)rrs r#test_offendingFunctions_deep_branchz6FlushWarningsTests.test_offendingFunctions_deep_branchs2   T//67;rN)r4Mapping[_K, _V]r5rr$r%)r;Sequence[Mapping[_K, _V]]r<rr$r%r#)r&r'r(r)r8r?rArJrPrRrr"r{rrrrrrrrr,rrr1r18sy ( (- (8Q (  (3 *   30. 03<3,7 < R7!NF:Cx.fs? MM& ! MM, ' MM- ( MM, ' MM+ &rrrrTrr#) rrr rr isinstancer rstrr )rrrrFrHrs @@@@rtest_callsObserverz'CollectWarningsTests.test_callsObservers6 $TrT* q!Q' ') ' ' * F+&)X... **L9&)X... **M:&)X... **C ,=> K0 Va(rct}|jtd|tdtj d|j |jdy)z Any warnings emitted by a call to a function passed to L{_collectWarnings} are not actually emitted to the warning system. rbcyNr,xs rz6CollectWarningsTests.test_suppresses..rtextrcN)rrdrer rrrrg)rrhs rtest_suppressesz$CollectWarningsTests.test_suppressessB  3&)? *B/rcgtd fd }td|dddd}|jddddfg|j|y ) z L{_collectWarnings} returns the result of calling the callable passed to it with the parameters given. c.j||fSr)r)argskwargs argumentsvalues rrz2CollectWarningsTests.test_callsFunction..fs   dF^ ,Lrcyrr,rs rrz9CollectWarningsTests.test_callsFunction..rrrr=rMd)r>c)rr=N)robjectrrr$r)rr rrX)rrr]rrs @@rtest_callsFunctionz'CollectWarningsTests.test_callsFunctionsY   ".!QqCH hac0B%C$DE VU+rc<bdd}tjd|g}t|j||j t |d|j |djd|j t |jdy)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.tjdy)Nrrr,rrrz>CollectWarningsTests.test_duplicateWarningCollected..fs MM% rdefaultrrrNr#) __warningregistry__rrtr rrr rr!)rrrs rtest_duplicateWarningCollectedz3CollectWarningsTests.test_duplicateWarningCollecteds  ! i( !#* Va( **E2 T//12A6rct}|tj|<|jtjj||j y)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)rrerrrrrkeys rtest_immutableObjectz)CollectWarningsTests.test_immutableObjects; h C  - ++-rciGfdd}t}t}|||<t}t}|||<t|j||||htj y)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"eZdZddZdfd Zy)KCollectWarningsTests.test_setWarningRegistryChangeWhileIterating..Ac"||jd<y)N_key)__dict__rs r__init__zTCollectWarningsTests.test_setWarningRegistryChangeWhileIterating..A.__init__ s(+ f%rc$d|j<yr)r)rritemrs r __setattr__zWCollectWarningsTests.test_setWarningRegistryChangeWhileIterating..A.__setattr__ s#$)) rN)rrr$r%)rrrrr$r%)r&r'r(rr)rsrArs  , $rrN)rr rr4keys)rrkey1key2key3key4rs @r+test_setWarningRegistryChangeWhileIteratingz@CollectWarningsTests.test_setWarningRegistryChangeWhileIteratingsy%' $ $xxD'$xxD'$!!$ $dD13qvvx=ArNr#) r&r'r(r)rrrrrrr,rrrrs&)80, 72 . Brr)r) __future__rreriortypingrrrunittestrtwisted.python.filepathr twisted.trial._synctestr r r twisted.trial.unittestr rr.r/r1rrrrr,rrrs~# --, 7;;8 T] T]]<,]<@  ' B.Br