id dZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&ddl%m'Z'ddl(m)Z)ddl(m*Z*ddl(m+Z+erddl,m-Z-dd l,m.Z.e.d!Z/d"e"d#dfd$Z0d\d%Z1d&ed#dfd'Z2e d()d*efd+Z3Gd,d-ej4Z5Gd.d/ej4Z6Gd0d1e6Z7Gd2d3eZ8Gd4d5ej9eeZ:d6d7d8d9Z;Gd:d;e:e<Z=Gd<d=e:eZ>Gd>d?e>e?Z@Gd@dAe>e<ZAGdBdCe:eZBGdDdEeBe?ZCGdFdGeBe<ZDejEdHksereGdIdJeeeZFn&GdKdJejGdJdLdMgeeZFGdNdOeeZHdPdQd#eHe<fdRZIGdSdTZJGdUdVeeZKe&dWe'd#eeKe<ddffdXZLe&dWe'd#eeKe?ddffdYZMe&dWe'd#eeKe<ddffdZZNe&dWe'd#eeKe?ddffd[ZOdS)]z+Per-test stdout/stderr capturing mechanism.NUnsupportedOperation) TemporaryFile) TracebackType)Any)AnyStr)BinaryIO) Generator)Generic)Iterable)Iterator)List) NamedTuple)Optional)TextIO)Tuple)Type) TYPE_CHECKING)Union)final)Config)hookimpl)Parser)check_ispytest)fixture) SubRequest) Collector)File)Item)Final)Literalfdsysnotee-sysparserreturnc|d}|ddddgdd|d d d d d dS)Ngeneralz --capturestorer#methodr"z3Per-test capturing method: one of fd|sys|no|tee-sys)actiondefaultmetavarchoiceshelpz-s store_constr%capturezShortcut for --capture=no)r-constdestr1)getgroup _addoption)r'groups D/opt/cloudlinux/venv/lib/python3.11/site-packages/_pytest/capture.pypytest_addoptionr:-s OOI & &E ... B     ( cttjdr ddl}dS#t$rYdSwxYwdS)a Ensure colorama is imported so that it attaches to the correct stdio handles on Windows. colorama uses the terminal on import time. So if something does the first import of colorama while I/O capture is active, colorama will fail in various ways. win32rN)r$platform startswithcolorama ImportError)r@s r9_colorama_workaroundrB@sZ |w''  OOOOO    DD s ' 55streamctjdrttdrdSt|dsdSt|jdr |jjn|j}t |tjsdSfd}|tj dt_ |tj dt_ |tj dt_ dS) aWorkaround for Windows Unicode console handling. Python 3.6 implemented Unicode console handling for Windows. This works by reading/writing to the raw console handle using ``{Read,Write}ConsoleW``. The problem is that we are going to ``dup2`` over the stdio file descriptors when doing ``FDCapture`` and this will ``CloseHandle`` the handles used by Python to write to the console. Though there is still some weirdness and the console handle seems to only be closed randomly and not on the first call to ``CloseHandle``, or maybe it gets reopened with the same handle value when we suspend capturing. The workaround in this case will reopen stdio with a different fd which also means a different handle by replicating the logic in "Py_lifecycle.c:initstdio/create_stdio". :param stream: In practice ``sys.stdout`` or ``sys.stderr``, but given here as parameter for unittesting purposes. See https://github.com/pytest-dev/py/issues/103. r=pypy_version_infoNbufferrawcs|ddkrd}nd}tjttj||||j|j|j|j S)Nrw) io TextIOWrapperopenosdupfilenoencodingerrorsnewlinesline_buffering)fmode bufferingbuffereds r9 _reopen_stdioz3_windowsconsoleio_workaround.._reopen_stdiotsn DGsNNIII  ##T9 5 5 J H J     r;rbwb) r$r>r?hasattrrFrG isinstancerK_WindowsConsoleIOstdinstdoutstderr)rC raw_stdoutrYrXs @r9_windowsconsoleio_workaroundrcOs0 < " "7 + +wsrz)formatrr\rrrr )rrs r9rzSysCaptureBase.repr^sK9@@  I D& ! ! 5d49oo B K L    r;cd|jj|jt |drt |jpd|j|jSr) rrrrr\rrrr rs r9__repr__zSysCaptureBase.__repr__gsP9@@ N # I D& ! ! 5d49oo B K L    r;opstates.cdSr~r|rrrs r9 _assert_statezSysCaptureBase._assert_statepr;c|ddtt|j|jd|_dS)Nrr started)rsetattrr$rr rrs r9rzSysCaptureBase.startws: 7$4555TY --- r;c|dd|jdkrdStt|j|j|`|jd|_dS)Nrr r suspendedr)rrr r$rrr rrs r9rzSysCaptureBase.done|se 6#RSSS ;& FTY *** I  r;c|ddtt|j|jd|_dSNrrr#r#)rr r$rrrrs r9rzSysCaptureBase.suspends: 9&>???TY ***! r;c|dd|jdkrdStt|j|jd|_dSNrr&r)rrr r$rr rs r9rzSysCaptureBase.resumesL 8%=>>> ;) # # FTY --- r;r~r)rrrrrrrrrrrrrrrrrr|r;r9rrOs 37 $HM $ $ $ $ ( 0 $AE $  $ $ $ $ s s     #      U38_         """"       r;rc.eZdZdZdefdZdeddfdZdS)SysCaptureBinaryr;r(c|dd|jd|jj}|jd|j|SNrr&rrr rrFrrrress r9rzSysCaptureBinary.snapt 6#;<<< !l!&&(( !  r;rNc|dd|j|jj||jjdSNrr&)rrrrFrsrs r9rzSysCaptureBinary.writeorgsa :'?@@@  t$$$      r;)rrrrbytesrrr|r;r9r*r*sVLe!U!t!!!!!!r;r*c.eZdZdZdefdZdeddfdZdS) SysCapturerr(c|dd|j}|jd|j|Sr,)rr rrrr.s r9rzSysCapture.snaps[ 6#;<<<l##%% !  r;rNc|dd|j||jdSr2)rrrsrrs r9rzSysCapture.writeorgsE :'?@@@  r;)rrrrrrrr|r;r9r5r5sVLcSTr;r5cneZdZdeddfdZdefdZdedeedfddfd Zdd Z dd Z dd Z dd Z dS) FDCaptureBasetargetfdr(Ncr||_ tj|d|_nX#t$rKtjtjtj|_tj|j|YnwxYwtj ||_ |dkr5t tjd|_ t||_ n_ttddddd|_ |t vrt||j |_ nt#||_ d |_dS) Nrutf-8)rQ)rWrrT)rQrRrrr )r:rNfstattargetfd_invalidrrMdevnullO_RDWRdup2rO targetfd_saver r5 syscapturer{rr rr)rr:s r9rzFDCaptureBase.__init__s-   ) HX    %)D ! ! 5 5 54672:ry3Q3QD ! GD)8 4 4 4 4 4 5  VH-- q== W===DL0:80D0DDOO&***  " DL<''",Xt|"D"D"+H"5"5# s%AA:9A:cpd|jj|j|j|j|jS)Nz)<{} {} oldfd={} _state={!r} tmpfile={!r}>)rrrr:rBrr rs r9rzFDCaptureBase.__repr__s7:AA N # M   K L    r;rr.cdSr~r|rs r9rzFDCaptureBase._assert_staterr;c|ddtj|j|j|jd|_dS)z4Start capturing on targetfd using memorized tmpfile.rrrN) rrNrAr rPr:rCrrrs r9rzFDCaptureBase.startsZ 7$4555  ##%%t}555  r;c|dd|jdkrdStj|j|jtj|j|jB|j|jkrtj|jtj|j|j |j d|_dS)z_Stop capturing, restore streams, return original capture file, seeked to position zero.rr"N) rrrNrArBr:rr>rCrr rs r9rzFDCaptureBase.dones 6#RSSS ;& F "DM222 #$$$  ,$ 55''' HT* + + +   r;c|dd|jdkrdS|jt j|j|jd|_dSr%)rrrCrrNrArBr:rs r9rzFDCaptureBase.suspendsa 9&>??? ;+ % % F !!! "DM222! r;c|dd|jdkrdS|jt j|j|jd|_dSr() rrrCrrNrAr rPr:rs r9rzFDCaptureBase.resumesl 8%=>>> ;) # # F      ##%%t}555 r;r) rrrrrrrrrrrrrr|r;r9r9r9s'$'$'$'$'$'$R #      U38_          """"      r;r9c2eZdZdZdZdefdZdeddfdZdS)FDCaptureBinaryzWCapture IO to/from a given OS-level file descriptor. snap() produces `bytes`. r;r(c|dd|jd|jj}|jd|j|Sr,r-r.s r9rzFDCaptureBinary.snapr0r;rNcf|ddtj|j|dS)"Write to original file descriptor.rr&N)rrNrsrBrs r9rzFDCaptureBinary.writeorg's4 :'?@@@ #T*****r;)rrr__doc__rr3rrr|r;r9rKrKsb Le+U+t++++++r;rKc2eZdZdZdZdefdZdeddfdZdS) FDCapturezTCapture IO to/from a given OS-level file descriptor. snap() produces text. rr(c|dd|jd|j}|jd|j|Sr,)rr rrrr.s r9rzFDCapture.snap5sq 6#;<<< !l!! !  r;rNc|ddtj|j|ddS)rNrr&r<N)rrNrsrBencoders r9rzFDCapture.writeorg=s@ :'?@@@ #T[[%9%9:::::r;)rrrrOrrrrr|r;r9rQrQ-sb Lc;S;T;;;;;;r;rQ) c(eZdZUdZeed<eed<dS) CaptureResult2The result of :method:`CaptureFixture.readouterr`.rwrxN)rrrrOrrr|r;r9rXrXJs(@@   r;rXceZdZdZdZdS)rXrYr|N)rrrrOrr|r;r9rXrXSs A@ r;rwrxceZdZdZdZdeeedeeedeeeddfdZde fdZ dd Z de eeffd Z ddeddfd Zdd Zdd ZdefdZdeefdZdS) MultiCaptureNFin_rwrxr(c0||_||_||_dSr~r]rwrx)rr]rwrxs r9rzMultiCapture.__init___s 362525r;cfd|j|j|j|j|jS)NzH)rrwrxr]r _in_suspendedrs r9rzMultiCapture.__repr__is4Y`` H H H K      r;cd|_|jr|j|jr|j|jr|jdSdS)Nr)rr]rrwrxrs r9start_capturingzMultiCapture.start_capturingrsm 8  HNN    8  HNN    8  HNN       r;c|\}}|r|j||r|j|||fS)z?Pop current snapshot out/err capture and flush to orig streams.) readouterrrwrrxrrwrxs r9pop_outerr_to_origzMultiCapture.pop_outerr_to_orig{sZ??$$S  # H  c " " "  # H  c " " "Cxr;cd|_|jr|j|jr|j|r)|jr$|jd|_dSdSdS)Nr#T)rrwrrxr]rarr]s r9suspend_capturingzMultiCapture.suspend_capturings! 8  H      8  H       &48 & H     !%D    & & & &r;cd|_|jr|j|jr|j|jr"|jd|_dSdS)NrF)rrwrrxrar]rs r9resume_capturingzMultiCapture.resume_capturingsv 8  HOO    8  HOO      ' HOO   !&D    ' 'r;c |jdkrtdd|_|jr|j|jr|j|jr|jdSdS)z+Stop capturing and reset capturing streams.stoppedzwas already stoppedN)r ValueErrorrwrrxr]rs r9stop_capturingzMultiCapture.stop_capturings~ ;) # #233 3 8  HMMOOO 8  HMMOOO 8  HMMOOOOO  r;c|jdkS)z7Whether actively capturing -- not suspended or stopped.r)rrs r9 is_startedzMultiCapture.is_starteds{i''r;c|jr|jnd}|jr|jnd}t||Sr)rwrrxrXrfs r9rezMultiCapture.readouterrsF!%1dhmmooor!%1dhmmooorS#&&&r;rF)rrrrrarrrrrrrcrrgrrjrlrprrrXrer|r;r9r\r\[sS FM6 k&) *6k&) *6k&) * 6  6666 #     E&&.$9    &&T&d&&&& ' ' ' '    (D(((('M&1''''''r;r\r,_CaptureMethodc|dkr9ttdtdtdS|dkr9ttdtdtdS|dkrtdddS|dkr0tdtdd tdd Std |) Nr#rrrr_r$r%r&Trzunknown capturing method: )r\rQr5ro)r,s r9_get_multicapturerws ~~ ! )A,,IaLLQQQQ 5 1 :a==jQRmmTTTT 4$D9999 9  *QD111z!7N7N7N     <&<< = ==r;ceZdZdZd*dZdefdZdeeeffdZ defd Z d+d Z d+d Z d+d Z d,deddfdZd,deddfdZd+dZdeefdZd-dZd+dZd+dZd+dZd+dZd+dZejdedfdZejdedededfdZed !d"e fd#Z!ed !dededfd$Z"ed !dededfd%Z#ed !dededfd&Z$ed 'd+d(Z%ed 'd+d)Z&dS).rkaThe capture plugin. Manages that the appropriate capture method is enabled/disabled during collection and each test phase (setup, call, teardown). After each of those points, the captured output is obtained and attached to the collection/runtest report. There are two levels of capture: * global: enabled by default and can be suppressed by the ``-s`` option. This is always enabled/disabled during collection and each test phase. * fixture: when a test function or one of its fixture depend on the ``capsys`` or ``capfd`` fixtures. In this case special handling is needed to ensure the fixtures take precedence over the global capture. r,rur(Nc0||_d|_d|_dSr~)_method_global_capturing_capture_fixture)rr,s r9rzCaptureManager.__init__s$ >B?Cr;cNd|j|j|jS)NzJ)rrzr{r|rs r9rzCaptureManager.__repr__s)[bb L$0$2G   r;ch|rdS|jrd|jjjzSdS)Nglobalz fixture %sF)is_globally_capturingr|request fixturenamers r9 is_capturingzCaptureManager.is_capturings@  % % ' ' 8   L$"7"?"KK Kur;c|jdkS)Nr%)rzrs r9rz$CaptureManager.is_globally_capturings|t##r;cjt|j|_|jdSr~)rwrzr{rcrs r9roz%CaptureManager.start_global_capturings/!24>%    !0r;cd|_dSr~)r|rs r9 unset_fixturezCaptureManager.unset_fixtures $r;cJ|jr|jdSdS)z|If the current item is using ``capsys`` or ``capfd``, activate them so they take precedence over the global capture.N)r|_startrs r9activate_fixturezCaptureManager.activate_fixtures4   +  ! ( ( * * * * * + +r;cJ|jr|jdSdS)zDDeactivate the ``capsys`` or ``capfd`` fixture of this item, if any.N)r|rrs r9deactivate_fixturez!CaptureManager.deactivate_fixture$s2   *  ! ' ' ) ) ) ) ) * *r;cJ|jr|jdSdSr~)r|_suspendrs r9rzCaptureManager.suspend_fixture)s2   -  ! * * , , , , , - -r;cJ|jr|jdSdSr~)r|_resumers r9rzCaptureManager.resume_fixture-s2   ,  ! ) ) + + + + + , ,r;NNNc#K|jo|j}|r||jo|j}|r| dV|r||r|dSdS#|r||r|wwxYw)zLContext manager to temporarily disable global and current fixture capturing.N)r| _is_startedrr{rrrprr)r do_fixture do_globals r9global_and_fixture_disabledz*CaptureManager.global_and_fixture_disabled3s*Rt/D/P/P/R/R  #  " " "*Rt/E/P/P/R/R  *  ' ' ) ) ) & EEE -**,,, &##%%%%% & & -**,,, &##%%%% &s 0B$$/Cwhenitemc#K|| dV||dn/#||dwxYw|\}}||d|||d|dS)NFrr`ra)rrrrprradd_report_section)rrrrwrxs r9 item_capturezCaptureManager.item_captureDs ""$$$  3 EEE  # # % % %  ' 'E ' 2 2 2 2  # # % % %  ' 'E ' 2 2 2 2++--S h444 h44444s A,BTrd collectorc#jKt|tr|dV}||\}}|}|r|jd|f|r|jd|fdSdSdVdS)NzCaptured stdoutzCaptured stderr)r]rrrprr get_resultsectionsappend)rrrvrwrxreps r9pytest_make_collect_reportz)CaptureManager.pytest_make_collect_reportTs i & &   & & ( ( (eeG  ' ' ) ) )//11HC$$&&C > ##%6$<=== > ##%6$<===== > > EEEEEr;c#pK|d|5dVddddS#1swxYwYdS)Nsetuprrrs r9pytest_runtest_setupz#CaptureManager.pytest_runtest_setupcs   w - -   EEE                   +//c#pK|d|5dVddddS#1swxYwYdS)Ncallrrs r9pytest_runtest_callz"CaptureManager.pytest_runtest_callhs   vt , ,   EEE                  rc#pK|d|5dVddddS#1swxYwYdS)Nteardownrrs r9pytest_runtest_teardownz&CaptureManager.pytest_runtest_teardownms   z4 0 0   EEE                  r)tryfirstc.|dSr~rnrs r9pytest_keyboard_interruptz(CaptureManager.pytest_keyboard_interruptr ""$$$$$r;c.|dSr~rrs r9pytest_internalerrorz#CaptureManager.pytest_internalerrorvrr;)r,rur(Nrrt)rrr(N)'rrrrOrrrrrrrrornrrprrrXrrrrrrrr contextlibcontextmanagerr rrrrrrrrrrrr|r;r9rkrks$DDDD  #    eCI.$t$$$$1111 **** 6666 >>$>4>>>>))4)D)))) 3]3%73333 0 0 0 0%%%%++++ **** ----,,,, &Y7G-H&&&&  5 5D 5Y?O5P 5 5 5 5X$ I     X$)Temporarily disable capturing while inside the ``with`` block.rhN)rconfigrj getpluginr)r capmanagers r9disabledzCaptureFixture.disableds&*\%8%F%P%P & &  3 3 5 5   EEE                  sA  AAr)rrrrOrrrrrrrrrXrerrrrrr rr|r;r9rr{s7== D D D;v./ D D  D  D D D D,,,,!!!!9M&19999".... ---- T )$45r;rrc# K|jjd}tt|d}||||V||dS)aEnable text capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsys.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` objects. Returns an instance of :class:`CaptureFixture[str] `. Example: .. code-block:: python def test_output(capsys): print("hello") captured = capsys.readouterr() assert captured.out == "hello\n" rhTrN) rrjrrr5rrrrrrurs r9capsysrs&%^9CCDTUUF$ZDIIIO '''  r;c# K|jjd}tt|d}||||V||dS)a'Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``. The captured output is made available via ``capsysbinary.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``bytes`` objects. Returns an instance of :class:`CaptureFixture[bytes] `. Example: .. code-block:: python def test_output(capsysbinary): print("hello") captured = capsysbinary.readouterr() assert captured.out == b"hello\n" rhTrN) rrjrrr*rrrrrs r9 capsysbinaryrs&%^9CCDTUUF$%5w$OOOO '''  r;c# K|jjd}tt|d}||||V||dS)aEnable text capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``text`` objects. Returns an instance of :class:`CaptureFixture[str] `. Example: .. code-block:: python def test_system_echo(capfd): os.system('echo "hello"') captured = capfd.readouterr() assert captured.out == "hello\n" rhTrN) rrjrrrQrrrrrs r9capfdrs&%^9CCDTUUF$Y4HHHO '''  r;c# K|jjd}tt|d}||||V||dS)a-Enable bytes capturing of writes to file descriptors ``1`` and ``2``. The captured output is made available via ``capfd.readouterr()`` method calls, which return a ``(out, err)`` namedtuple. ``out`` and ``err`` will be ``byte`` objects. Returns an instance of :class:`CaptureFixture[bytes] `. Example: .. code-block:: python def test_system_echo(capfdbinary): os.system('echo "hello"') captured = capfdbinary.readouterr() assert captured.out == b"hello\n" rhTrN) rrjrrrKrrrrrs r9 capfdbinaryr s(%^9CCDTUUF$_gNNNO '''  r;r)PrOr collectionsrrKrNr$rtempfilertypesrtypingrrr r r r r rrrrrrrr_pytest.compatr_pytest.configrr_pytest.config.argparsingr_pytest.deprecatedr_pytest.fixturesrr _pytest.nodesrrrtyping_extensionsr r!rur:rBrcryrLr{rrrABCrr rrrr3r*r5r9rKrQ version_inforX namedtupler\rwrkrrrrrr|r;r9rs11  ######""""""  !!!!!!######,,,,,,------$$$$$$''''''######;''''''))))))9:NV&    5151D51515151p d2 1 1 1 1 1"" 1 1 1 66666 666$$$$$9$$$IIIIIIII^$$$$$#'76?$$$@xH55      C    2@ @ @ @ @ [(@ @ @ F!!!!!~e,!!!$$"_ _ _ _ _ K'_ _ _ D+++++mE*+++,;;;;; c";;;6w-  GFO U ??T'T'T'T'T'76?T'T'T'n >. ><3D > > > >"u%u%u%u%u%u%u%u%pKKKKKWV_KKKb J9^C-@$-L#M 6 *>%3H$PT3T)U 6 :)N3,?t,K"L 6  .2Gt2S(T r;