a zeO@sddlZddlZddlmZmZmZmZmZmZddl m Z m Z ddl Z ddl Z ddlZddlmZddlmZddZdd Zd d Zd d ZddZddZddZddZddZddZddZddZddZd d!ZGd"d#d#e Z!Gd$d%d%e j"Z#Gd&d'd'e j"Z$Gd(d)d)e j"Z%Gd*d+d+e j"Z&Gd,d-d-e j"Z'Gd.d/d/e j"Z(Gd0d1d1e j"Z)Gd2d3d3e j"Z*e+d4kre ,dS)5N)TESTFNTESTFN_UNICODE FS_NONASCIIrmtreeunlinkcaptured_stdoutassert_python_okassert_python_failure)Trace)testmodcCs|dr|dd}|S)z8Given a .pyc filename converts it to the appropriate .pyz.pycN)endswith)filenamerC/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_trace.py fix_ext_pys  rcCs$tjtjtd}tt|fS)z4The .py file and module name of this file (__file__)r)ospathsplitextbasename__file__r)modnamerrrmy_file_and_modnamesrcCs|jjSN)__code__co_firstlinenofuncrrrget_firstlinenosrcCs|}|}||}|Srr)xyabcrrrtraced_func_linear$sr%cCs|}tdD] }||7}q |SNrange)r r!r$irrrtraced_func_loop*s  r+cCs||tdS)N)r r)r r!rrrtraced_func_importing0sr-cCst||}||Srr%)r r$rrrtraced_func_simple_caller3s r/cCst|}|t||7}|Sr)r/r-)r krrrtraced_func_importing_caller7sr1ccs d}t|D]}||Vq dSr&r()numr$r*rrrtraced_func_generator<s r3cCsd}tdD] }||7}q dS)Nr )r3)r0r*rrrtraced_func_calling_generatorAs r5cCs|dSNr)r2rrrtraced_doublerFsr8cOs||fSrr)argskwargsrrrtraced_capturerIsr;cCsd}ddt|D}|S)Nr4cSsg|] }t|qSr)r8).0r*rrr Nz4traced_caller_list_comprehension..r()r0Zmylistrrr traced_caller_list_comprehensionLsr?cCs,dd}dd}||dd}|dS)NcSs|Srrfrrr decorator1Rsz-traced_decorated_function..decorator1cSs dd}|S)NcSs|Srrr@rrr decorator2UszGtraced_decorated_function..decorator_fabric..decorator2rrCrrrdecorator_fabricTsz3traced_decorated_function..decorator_fabriccSsdSrrrrrrrXsz'traced_decorated_function..funcr)rBrErrrrtraced_decorated_functionQs  rFc@s<eZdZddZddZddZeddZed d Z d S) TracedClasscCs ||_dSrr")selfr rrr__init__`szTracedClass.__init__cCs |j|SrrH)rIr!rrrinst_method_linearcsTracedClass.inst_method_linearcCs||}|t||Sr)rKr%)rIr r$rrrinst_method_callingfs TracedClass.inst_method_callingcCs|dSr6r)clsr!rrrclass_method_linearjszTracedClass.class_method_linearcCs|dSr6r)r!rrrstatic_method_linearnsz TracedClass.static_method_linearN) __name__ __module__ __qualname__rJrKrM classmethodrP staticmethodrQrrrrrG_s rGc@sPeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dS)TestLineCountsz/White-box testing of line-counting, via runfunccCs2|tjttddddd|_tt|_dS)Nr,rcounttrace countfuncs countcallers) addCleanupsyssettracegettracer tracerrrmy_py_filenamerIrrrsetUpxszTestLineCounts.setUpcCsb|jtdd}||di}tt}tddD]}d||j||f<q2||jj|dS)Nr7r'r,) rarunfuncr% assertEqualrr)rbresultscounts)rIresultexpected firstlinenor*rrrtest_traced_func_linear}s z&TestLineCounts.test_traced_func_linearc Csl|jtddtt}|j|dfd|j|dfd|j|dfd|j|dfdi}||jj|dS)Nr7r,r')rarfr+rrbrgrhrirIrlrkrrrtest_traced_func_loopsz$TestLineCounts.test_traced_func_loopcCs^|jtddtt}|j|dfdttjdfdttjdfdi}||j j |dS)Nr7r'r,rn) rarfr-rrbrr rrgrhrirqrrrtest_traced_func_importingsz)TestLineCounts.test_traced_func_importingc Cs|jttt}tt}|j|dfd|j|dfd|j|dfd|j|dfd|j|dfd|j|dfdi}||jj|dS)Nr,r7 rnr4) rarfr5rr3rbrgrhri)rIfirstlineno_callingZfirstlineno_genrkrrrtest_trace_func_generators z(TestLineCounts.test_trace_func_generatorc Csp|jttt}tt}|j|dfd|j|dfd|j|dfd|j|dfdi}||jj|dS)Nr,r7 rnr4) rarfr?rr8rbrgrhri)rIruZfirstlineno_calledrkrrrtest_trace_list_comprehensions z,TestLineCounts.test_trace_list_comprehensioncCs|jttt}|j|dfd|j|dfd|j|dfd|j|dfd|j|dfd|j|dfd|j|dfd|j|dfd|j|d fd|j|d fd|j|d fdi }||jj|dS) Nr,r7rnrpr'rore r4rt)rarfrFrrbrgrhrirqrrrtest_traced_decorated_functions  z-TestLineCounts.test_traced_decorated_functioncCshdD]^}tddddd}td}t||}||dt|}|j|dfdi}||j|qdS)N)rKr,rrX) r rGgetattrrfrrbrgrhri)rIZmethnameraZ traced_objmethodrlrkrrrtest_linear_methodss  z"TestLineCounts.test_linear_methodsN) rRrSrT__doc__rdrmrrrsrvrxr{rrrrrrWvs   rWc@s eZdZdZddZddZdS)TestRunExecCountsz8A simple sanity test of line-counting, via runctx (exec)cCs tt|_|tjtdSr)rrrbr]r^r_r`rcrrrrds zTestRunExecCounts.setUpc Cstddddd|_d}t|td}|j|tttt}|j |dfd|j |dfd|j |dfd |j |d fdi}| D] }| |j j |||qdS) Nr,rrXztraced_func_loop(2, 5)execr7rornr'rp)r racompilerZrunctxglobalsvarsrr+rbkeysrgrhri)rIcoderlrkr0rrrtest_exec_countss  z"TestRunExecCounts.test_exec_countsN)rRrSrTrrdrrrrrrsrc@sbeZdZdZddZddZddZdd Zd d Ze e e d oHe d ddZddZdS) TestFuncsz"White-box testing of funcs tracingcCs8|tjttdddd|_t|_t|_dS)Nrr,)rYrZr[) r]r^r_r`r rarfilemod_saved_tracefuncrcrrrrdszTestFuncs.setUpcCs|jdurt|jdSr)rr^r_rcrrrtearDowns zTestFuncs.tearDowncCs>|jtd|jdd|jddi}||jj|dS)Nr,r/r.)rarfr/rrgrh calledfuncsrIrkrrrtest_simple_caller s   zTestFuncs.test_simple_callercCs|jjtddddd}||ddddf|t |jjtddWdn1s\0Y|t|jWdn1s0YdS)Nr,r7rnrp)rIr)r,r7)rarg)rarfr;rgZ assertRaises TypeError)rIresrrrtest_arg_errorss  . zTestFuncs.test_arg_errorsc Csb|jtd|jdd|jdd|jdd|jddttjddfdi}||jj |dS)Nr,rr.r1r-r r) rarfr1rrr rrgrhrrrrrtest_loop_caller_importings    z$TestFuncs.test_loop_caller_importingr`3pre-existing trace function throws off measurementscCsRtd}|j|jd|jdd|jdd|jddi}||jj|dS)Nr}r,)rN)rLr.)rGrarfrMrrgrhr)rIobjrkrrrtest_inst_method_calling(s   z"TestFuncs.test_inst_method_callingc CsZ|jt|jdd|jdd|jdd|jdd|jddi}||jj|dS)N)rFr,)rErD)rBr)rarfrFrrgrhrrrrrr{5s      z(TestFuncs.test_traced_decorated_functionN)rRrSrTrrdrrrrunittestskipIfhasattrr^r`rr{rrrrrs   rc@s:eZdZdZddZeeedo(e dddZ dS) TestCallersz$White-box testing of callers tracingcCs.|tjttdddd|_t|_dS)Nrr,)rYrZr\)r]r^r_r`r rarrrcrrrrdDszTestCallers.setUpr`rc Cs|jtdtjtjddddf|jdfd|jd|jdfd|jd|jdfd|jd|jd fd|jd t t jd d ffdi}| |j j |dS) Nr,r.pyrZz Trace.runfuncrrr.rr r)rarfr1rrrrZrrrr rgrhZcallersrrrrrIs. z&TestCallers.test_loop_caller_importingN) rRrSrTrrdrrrr^r`rrrrrrBs rc@s>eZdZddZddZdddZdd Zd d Zd d ZdS) TestCoveragecCs|tjtdSr)r]r^r_r`rcrrrrd_szTestCoverage.setUpcCsttttdSr)rrrrcrrrrbszTestCoverage.tearDown_import test.support, test.test_pprint;test.support.run_unittest(test.test_pprint.QueryTestCase)cCs&|||}|jddtddS)NT)Z show_missingZsummaryZcoverdir)runrhZ write_resultsr)rIracmdrrrr _coveragefs zTestCoverage._coveragecCstjddd}t}||Wdn1s40Y|}|d||d|tt}|d||d|dS)Nrr,rZrYz pprint.pyzcase.pyz pprint.coverzunittest.case.cover) rZr rrgetvalueassertInrlistdirr)rIrastdoutfilesrrr test_coveragems(    zTestCoverage.test_coveragecCstjtjtj}tjtjtj |gddd}t }| |Wdn1sV0Ytj t rtt }||dgdS)Nrr,)Z ignoredirsrZrYz_importlib.cover)rrnormpathdirnamerrZr r^ base_prefixbase_exec_prefixrrexistsrrrg)rIZlibpathrarrrrrtest_coverage_ignorexs (  z!TestCoverage.test_coverage_ignorec Cstjddd}d}|tjvr$tj|=d}t}|||Wdn1sP0Y|d|i}|D]6}|dd\}}} t |t |ddf|| <qtt tj|j }| ||| ||ddS) Nrr,rztest.tracedmodules.testmodzrru^ # coding: iso-8859-15 1: x = 'spœm' 1: if []: print('unreachable') )rrrabspathrZrrrrrr rgrrrrrread)rIZtracedirZtracecoverpathargvstatusrstderrrArrr%test_cover_files_written_no_highlights  zCTestCoverageCommandLineOutput.test_cover_files_written_no_highlightcCszd|jg}t|\}}}|tj|jt|jdd&}| | t dWdn1sl0YdS)Nz-m trace --count --missingrru # coding: iso-8859-15 1: x = 'spœm' 1: if []: >>>>>> print('unreachable') ) rrr rrrrrrrgrrr)rIrrrrrArrr'test_cover_files_written_with_highlights zETestCoverageCommandLineOutput.test_cover_files_written_with_highlightN) rRrSrTrrrdrrrrrrrrs  rc@s4eZdZddZddZddZddZd d Zd S) TestCommandLinecCs:d}|D],^}}tddg|R^}}|||qdS)N))s3progname is missing: required with the main options-lz-T)s8cannot specify both --listfuncs and (--trace or --count)z-lc)s>argument -R/--no-report: not allowed with argument -r/--reportz-rR)sLmust specify one of --trace, --count, --report, --listfuncs, or --trackcallsz-g)s-r/--report requires -f/--filez-r)s3--summary can only be used with --count or --reportz-sT)sunrecognized arguments: -yz-y-mrZ)r r)rI_errorsmessager9_rrrr test_failuress zTestCommandLine.test_failurescCstd}tjt}t|dddl}|t||dtddd|dd \}}}| d |d |d |d }| | |Wdn1s0YdS)Nrrutf-8rza = 1 rrZrZPYTHONIOENCODINGsfunctions called:z filename: z, modulename: z, funcname: ) rrrrrr]rrr rencode)rIr modulenamefdrrrrkrrrtest_listfuncs_flag_successs      z+TestCommandLine.test_listfuncs_flag_successcCsttddd0}|tt|d|dWdn1sD0Ytt\}}}tdddtdd \}}}|||dS) Nrrrz import sys zprint(type(sys.argv)) rrZrr)rrr]rrr rstrip)rIrrZ direct_stdoutrZ trace_stdoutrrrtest_sys_argv_lists  (  z"TestCommandLine.test_sys_argv_listcCstd}td}tjt}t|ddd8}|t||t||t dWdn1sl0Yt ddd |dd \}}}| }| |d | d || d |d|d|dS)Nrz.coverrrrz x = 1 y = 2 def f(): return x + y for i in range(10): f() rrZz-csrrzlines cov% module (path)z 6 100% z ())rrrrrr]rrrrr decodergr)rIrZ coverfilenamerrrrrrrrtest_count_and_summarys     .    z&TestCommandLine.test_count_and_summarycCs(tdddddddtddddddS) NrrZrz--moduleZtimeitz-n1Znot_a_module_zzzrrcrrrtest_run_as_modulesz"TestCommandLine.test_run_as_moduleN)rRrSrTrrrrrrrrrrs    r__main__)-rr^Z test.supportrrrrrrZtest.support.script_helperr r rrrZr Ztest.tracedmodulesr rrrr%r+r-r/r1r3r5r8r;r?rFobjectrGZTestCaserWrrrrrrrrRmainrrrrsB    mC> 2C