a ze@sdZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z ddlZGdddeZGdddeZGd d d eZGd d d eZdZd dZddZGdddejZGdddeZGdddZd3ddZe ddZeddfddZdZedZ d d!Z!d"d#Z"d$d%Z#d&d'Z$Gd(d)d)ej%Z&Gd*d+d+e&Z'Gd,d-d-e&Z(Gd.d/d/e&Z)Gd0d1d1e&Z*e+d2kre,dS)4a Test the bdb module. A test defines a list of tuples that may be seen as paired tuples, each pair being defined by 'expect_tuple, set_tuple' as follows: ([event, [lineno[, co_name[, eargs]]]]), (set_type, [sargs]) * 'expect_tuple' describes the expected current state of the Bdb instance. It may be the empty tuple and no check is done in that case. * 'set_tuple' defines the set_*() method to be invoked when the Bdb instance reaches this state. Example of an 'expect_tuple, set_tuple' pair: ('line', 2, 'tfunc_main'), ('step', ) Definitions of the members of the 'expect_tuple': event: Name of the trace event. The set methods that do not give back control to the tracer [1] do not trigger a tracer event and in that case the next 'event' may be 'None' by convention, its value is not checked. [1] Methods that trigger a trace event are set_step(), set_next(), set_return(), set_until() and set_continue(). lineno: Line number. Line numbers are relative to the start of the function when tracing a function in the test_bdb module (i.e. this module). co_name: Name of the function being currently traced. eargs: A tuple: * On an 'exception' event the tuple holds a class object, the current exception must be an instance of this class. * On a 'line' event, the tuple holds a dictionary and a list. The dictionary maps each breakpoint number that has been hit on this line to its hits count. The list holds the list of breakpoint number temporaries that are being deleted. Definitions of the members of the 'set_tuple': set_type: The type of the set method to be invoked. This may be the type of one of the Bdb set methods: 'step', 'next', 'until', 'return', 'continue', 'break', 'quit', or the type of one of the set methods added by test_bdb.Bdb: 'ignore', 'enable', 'disable', 'clear', 'up', 'down'. sargs: The arguments of the set method if any, packed in a tuple. N)contextmanager)islicerepeatc@s eZdZdS) BdbExceptionN)__name__ __module__ __qualname__r r A/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_bdb.pyr>rc@seZdZdZdS)BdbErrorz!Error raised by the Bdb instance.Nrrr__doc__r r r r r ?r r c@seZdZdZdS)BdbSyntaxErrorzSyntax error in the test case.Nr r r r r r@r rc@seZdZdZdS)BdbNotExpectedErrorzUnexpected result.Nr r r r r rAr rcCsdtj_itj_dgtj_dSN)_bdb BreakpointnextZbplist bpbynumberr r r r reset_BreakpointIsrc CsddtjjD}|sdSd}|D]v}|s2d}d}|jrOr z$info_breakpoints..Fz!BpNum Temp Enb Hits Ignore Where Tzyes zno Zyeszno z%-5d %s %s %-4d %-6d at %s:%dz stop only if %s )rrr temporaryenabledZnumberhitsignoreospathbasenamefilelinecond)bp_listZ header_addedrinfoZdispZenabr r r info_breakpointsNs& r)csjeZdZdZfddZdfdd Zfdd Zd d Zd d ZddZ ddZ ddZ ddZ Z S)Bdbz$Extend Bdb to enhance test coverage.csd|_t|||SN) currentbpsupertrace_dispatch)selfframeeventarg __class__r r r.fszBdb.trace_dispatchFNc st|trT|tkrt}nt|dd}|j}t||}|j} | j }| j }| j }t j |||||d} t| tr|t| | S)N)rr&funcname) isinstancestr__file__globals importlib import_module__dict__eval__code__ co_filenameco_firstlinenoco_namer- set_breakr ) r/filenamelinenorr&r6globals_modulefunccoderesr3r r rCjs     z Bdb.set_breakcs4t||\|_|_|j|jd|_|j|jfSNr)r- get_stackstackindexr0)r/ftr3r r rL~sz Bdb.get_stackcCs||}|jd7_dS)z8Increment the ignore count of Breakpoint number 'bpnum'.rN)get_bpbynumberr r/Zbpnumrr r r set_ignores zBdb.set_ignorecCs||}d|_dS)NTrQrrRr r r set_enables zBdb.set_enablecCs||}d|_dS)NFrTrRr r r set_disables zBdb.set_disablecCs|||}|rt|dSr+)Z clear_breakr )r/fnamerEerrr r r set_clears z Bdb.set_clearcCs2|jstd|jd8_|j|jd|_dS)zMove up in the frame stack.z Oldest framerrN)rNr rMr0r/r r r set_upsz Bdb.set_upcCs@|jdt|jkrtd|jd7_|j|jd|_dS)zMove down in the frame stack.rz Newest framerN)rNlenrMr r0rZr r r set_downsz Bdb.set_down)FNN)rrrrr.rCrLrSrUrVrYr[r] __classcell__r r r3r r*cs  r*cseZdZdZd,fdd ZddZfdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd-d&d'Zd(d)Zd*d+ZZS).Tracerz$A tracer for testing the bdb module.NFcs<tj|d||_||_|dur*d|nd|_|dS)NskipzDry-run results for %s:)r-__init__ expect_setdry_runheader init_test)r/rcrard test_caser3r r rbs zTracer.__init__cCsBd|_d|_d|_tt|jddd|_tt|jddd|_dS)Nrr) cur_except expect_set_nobreakpoint_hitslistrrc expected_listset_listrZr r r rfs zTracer.init_testc sv|jdur|j|dkrbzt|||}|WSty^}z||_|jWYd}~Sd}~00nt|||SdSN exception)rir-r.r)r/r0r1r2rJer3r r r.s zTracer.trace_dispatchcCs(||sdS|d|||dS)Ncall)Z stop_here process_eventnext_set_method)r/r0Z argument_listr r r user_calls zTracer.user_callcCsZ|d||jr@|jr@td}|dD]}td|q.|d|_|dS)Nr%rz ) rsrdrkr)stripsplitprintdelete_temporariesrt)r/r0r(r%r r r user_lines   zTracer.user_linecCs|d|||dS)Nreturn)rsrt)r/r0Z return_valuer r r user_returnszTracer.user_returncCs||_|d||dSro)exc_inforsrt)r/r0r}r r r user_exceptions zTracer.user_exceptioncCs|jg}||f|_dSr+)r,rk)r/r2r'r r r do_clearszTracer.do_clearcCs$|jr |jdD]}||qdSr)rkZclear_bpbynumber)r/nr r r ryszTracer.delete_temporariescCsT|jd7_z|jd|_Wn ty@td|jYn0|jd|_dS)Nrrz-expect_set list exhausted, cannot pop item %d)rjrmpopexpect IndexErrorrrn set_tuplerZr r r pop_nexts  zTracer.pop_nextcsd}|dkrjd}||jdurDjsDjg}|gf_|_jrhjdSj r j d|d |dvr dnt j dkr~|dkrFj d\}t}jsdj |jdd  fd d |Dfd d jdDd  t|jddn8|dkr~tjdj ds~djjfdS)NrprhrzWrong event type)rrr{r%z(No breakpoints hit at expect_set item %dzBreakpoint numbers do not matchcsg|] }|qSr r rr)bpsr r rr z(Tracer.process_event..csg|]}|jqSr )rQrrrZr r r szWrong breakpoint hit countrzWrong temporary breakpointsz/Wrong exception at expect_set item %d, got '%s')r}rLr,rkr1rrd print_staterer check_equalcheck_lno_namecheck_expect_max_sizer\sortedkeysraise_not_expectedrjr7)r/r1r0argstbr'Z temporariesZbpnumsr )rr/r rss\          zTracer.process_eventcCs&||kr dS|d||j|fdS)Nz"%s at expect_set item %d, got '%s')rrj)r/Zexpectedresultmsgr r r r,s  zTracer.check_equalcCsTt|j}|dkr.|}||jd|d|dkrP||jd|jjjddS)z+Check the line number and function co_name.rzWrong line numberrhzWrong function nameN)r\r lno_abs2relrr0f_coderB)r/srEr r r r2s zTracer.check_lno_namecCs&t|j|kr"td|j|jfdS)Nz'Invalid size of the %s expect tuple: %s)r\rrr1)r/sizer r r r<s zTracer.check_expect_max_sizecCs<||jjj}|jj}||tkr8||jjjdS|Sr)canonicr0rr@f_linenor9rAr/rWrEr r r rAs  zTracer.lno_abs2relcCs.|r*|||tkr*|jjj|dS|Sr)rr9r0rrArr r r lno_rel2absGs zTracer.lno_rel2abscCs|}|jjj}d|j||f}|jrd}|jdD],}|dkrJ|d7}|d|||jf7}q6|d7}d|dt|jdd }|d|7}n|jd kr|d|j dj 7}|d 7}| d t|j d S)Nz('%s', %d, '%s'{rz, z%s: %s}(r)rpz), ,) rr0rrBr1rkrQrr8r}rljustr)r/rErBstaterrr r r get_stateLs   zTracer.get_statecCs:|dur |jdkr tt|td|j|fdS)Nrz%d: %s)rjrxr)r/rer r r r^szTracer.print_statecCs6|d7}|dt|j7}|d|7}t|dS)Nrz Expected: %s z Got: )r8rrrr/rr r r rdszTracer.raise_not_expectedcCsZ|jd}t|jdkr"|jdnd}t|d|}|dvrF|dS|dvr\||jdS|dkrd}|r||jjj|d}||j|dS|r|dvs|d vrH|d vr|^}}}|||}||g}||||n"|d vr||n|d vr|d|_| |j r$| n|j r4| |d |ntd |jdS)NrrhrZset_)stepcontinuequit)rr{until)breakclearr enabledisable)updown)rr)r rrrz"%s" is an invalid set_tuple)rr\getattrr0rrr@extendr1rrdrrrrrtr)r/set_typerZ set_methodrErWZremainr r r rtjsR              zTracer.next_set_method)NFN)N)rrrrrbrfr.rurzr|r~rryrrsrrrrrrrrrtr^r r r3r r_s*    2  r_c@s,eZdZdZd ddZddZd ddZdS) TracerRunzAProvide a context for running a Tracer instance with a test case.NcCs2||_|j|_t|j||j|d|_d|_dS)N)rardrg)rgrdr_rcidtracer_original_tracer)r/rgrar r r rbs  zTracerRun.__init__cCstt|_|jSr+)rsysgettracerrrZr r r __enter__s zTracerRun.__enter__cCstt|jd}|jjr6|d7}|d|jj7}|durtt|rt |t r|j r|j d}|rr|d|7}|j rt |dS|j|n|r|j rt |n |j|dS)Nrz+All paired tuples have not been processed, zthe last one was number %drrT)rrsettracerrrnrj issubclassrr7 BaseExceptionrrdrxrgfail)r/type_value traceback not_emptyerr_msgr r r __exit__s*    zTracerRun.__exit__)N)NNN)rrrrrbrrr r r r rs rc Csdd}ddg}||t}d|_dd|_t|tdt||_t|Ft ||d }| t Wd n1s~0YWd n1s0Yd S) a{Run a test and print the dry-run results. 'modules': A dictionary mapping module names to their source code as a string. The dictionary MUST include one module named 'test_module' with a main() function. 'set_list': A list of set_type tuples to be run on the module. For example, running the following script outputs the following results: ***************************** SCRIPT ******************************** from test.test_bdb import run_test, break_in_func code = ''' def func(): lno = 3 def main(): func() lno = 7 ''' set_list = [ break_in_func('func', 'test_module.py'), ('continue', ), ('step', ), ('step', ), ('step', ), ('quit', ), ] modules = { 'test_module': code } run_test(modules, set_list) **************************** results ******************************** 1: ('line', 2, 'tfunc_import'), ('next',), 2: ('line', 3, 'tfunc_import'), ('step',), 3: ('call', 5, 'main'), ('break', ('test_module.py', None, False, None, 'func')), 4: ('None', 5, 'main'), ('continue',), 5: ('line', 3, 'func', ({1: 1}, [])), ('step',), BpNum Temp Enb Hits Ignore Where 1 no yes 1 0 at test_module.py:2 6: ('return', 3, 'func'), ('step',), 7: ('line', 7, 'main'), ('step',), 8: ('return', 7, 'main'), ('quit',), ************************************************************************* css<z"t|}t|}|V|VqWnty6YdS0dSr+)r StopIteration)abxyr r r gens  zrun_test..genrrTcSsdSr+r r r r r r zrun_test..r r`N) r BaseTestCaserdrrlriterrccreate_modulesrruncall tfunc_import)modulesrnrarsltestrr r r run_tests3    rc cstjtjtz|D]T}|d}t|d$}| t ||Wdn1sb0Yt |q"tdVW|D]}tj|qtjn"|D]}tj|qtj0Wdn1s0YdS)N.pyw)rsupportZtemp_cwdrr"appendr!getcwdopenwritetextwrapdedent linecache checkcacher;invalidate_cachesZforgetr)rmrWrOr r r rs   2  rFcCsd|d|||ffS)Nrr )r6rWrr&r r r break_in_func%srtest_module_for_bdbrcCsddl}|dSrK)rmain)rr r r r*srcCs d}ttd}d}d}dS)Nrh) tfunc_first tfunc_secondlnor r r tfunc_main.s rcCsd}d}d}dS)Nrhrr rr r r r6srcCsd}dS)Nrhr rr r r r;src@seZdZdZeZdddZdS)rzBase class for all tests.NcCs||ddSr+)ZfailureExceptionrr r r rCszBaseTestCase.fail)N)rrrrrdrr r r r r>src@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd S)! StateTestCasez;Test the step, next, return, until and quit 'set_' methods.cCs@gd|_t|}|tWdn1s20YdS)N)r%rhrrr%rrrrrrrrr%rhrrrcrrrr/rr r r test_stepKs  zStateTestCase.test_stepcCsdD]}|j|dzddddddtdffd d d dd d d igff|fddg |_t|}|tWdn1sv0YWdq1s0YqdS)N)rrrrrrrrr)Nonerrrr%rrr%rrr)subTestr9rcrrr)r/rrr r r test_step_next_on_last_statementUs  z.StateTestCase.test_step_next_on_last_statementcCs@gd|_t|}|tWdn1s20YdS)N) rrrrr%rrr)rrrrr)r%rhrrrrr r r test_nextcs  zStateTestCase.test_nextc Cstd}t|i}t|Lgd|_t|}|tWdn1sH0YWdn1sf0YdS)N9 def main(): lno = 3 )r%rhrrr%rrr TEST_MODULErrcrrrr/rIrrr r r test_next_over_importns    z#StateTestCase.test_next_over_importcCs@gd|_t|}|tWdn1s20YdS)N)rrrrrrrrrrr r r test_next_on_plain_statement|s  z*StateTestCase.test_next_on_plain_statementcCs@gd|_t|}|tWdn1s20YdS)N) rrrrrrrrrrrrrrr r r test_next_in_caller_frames  z'StateTestCase.test_next_in_caller_framecCs@gd|_t|}|tWdn1s20YdS)N) rrrrrrrr{r{rrrrrrrr r r test_returns  zStateTestCase.test_returncCs@gd|_t|}|tWdn1s20YdS)N) rrrrrrr r )r{rrrrrr r r test_return_in_caller_frames  z)StateTestCase.test_return_in_caller_framecCs@gd|_t|}|tWdn1s20YdS)N) rrrrrrr)r)rrrrrr r r test_untils  zStateTestCase.test_untilc Cs`dtddddddddigffdd d g|_t|}|tWdn1sR0YdS) Nrr)rrhrrr%rhr)r)i'r r)rrcrrrrr r r test_until_with_too_large_counts z-StateTestCase.test_until_with_too_large_countcCs@gd|_t|}|tWdn1s20YdS)N) rrrrrrr )r)r)r%rrrrrr r r test_until_in_caller_frames  z(StateTestCase.test_until_in_caller_framec Csd}t|i}t|\gd|_dddtf}t||d}|tWdn1sX0YWdn1sv0YdS)Nr)rrrrz importlib*Z zipimportz encodings.*r`r)r/rIrrarr r r test_skips   zStateTestCase.test_skipcCs"tdgd}||dddS)Nz anything*r`F)r*ZassertIsZis_skipped_module)r/bdbr r r test_skip_with_no_name_modules z+StateTestCase.test_skip_with_no_name_modulecCsFddg|_t| }|t|jtWdn1s80YdS)Nr)r)rcr assertRaisesr rrrr r r test_downs zStateTestCase.test_downcCs@gd|_t|}|tWdn1s20YdS)N)rrrrrrr rrrr r r test_ups  zStateTestCase.test_upN)rrrrrrrrrr r rrrrrrrrr r r r rHs          rc@s`eZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)BreakpointTestCasezTest the breakpoint set method.cCsFddg|_t| }|t|jtWdn1s80YdS)Nr)r)z/non/existent/module.pyr)rcrrr rrrr r r test_bp_on_non_existent_modules z1BreakpointTestCase.test_bp_on_non_existent_modulec Csd}t|i}t|Zddtdffg|_t| }|t|jtWdn1sV0YWdn1st0YdS)Nrrrr) rrTEST_MODULE_FNAMErcrrr rrrr r r test_bp_after_last_statements   z/BreakpointTestCase.test_bp_after_last_statementcCsd}t|i}t|dtdtddtdtddddddd d id gffddddd d id gffd g |_t|}|tWdn1s0YWdn1s0YdS) N def func(): lno = 3 def main(): for i in range(2): func() rrHTrrhrrr%rrrhrrrrrrcrrrrr r r test_temporary_bp s    z$BreakpointTestCase.test_temporary_bpcCsd}t|i}t|dtdtdtdtddddddd dd d igffd d d d ddd ddd idgffdd ddd dd digffdg|_t|}|tWdn1s0YWdn1s0YdS)N def func(): lno = 3 def main(): for i in range(3): func() rrHrT)rrhrr%rr)rr!rrrHrrrhrr$rrrr r r test_disabled_temporary_bp$s$   z-BreakpointTestCase.test_disabled_temporary_bpc Csd}t|i}t|ndtdtdddddd dd d igffd g|_t|}|tWdn1sj0YWdn1s0YdS) Nz def func(a): lno = 3 def main(): for i in range(3): func(i) rrHFza == 2rrr%rrrrrr r r test_bp_condition@s   z$BreakpointTestCase.test_bp_conditionc Csd}t|i}t|ndtdtdddddd dd d igffd g|_t|}|tWdn1sj0YWdn1s0YdS) Nzk def func(a): lno = 3 def main(): func(0) rrHFz1 / 0rrr%rrrrrr r r )test_bp_exception_on_condition_evaluationTs   zr)r{rhr/rzexec)rcrruncompilerr)r/rIrr r r test_run_steps  zRunTestCase.test_run_stepc Csd}t|i}t|\gd|_ddl}t|"}|dttWdn1sX0YWdn1sv0YdS)Nr) )r%rr/r)rrrhrr)r%rrr)r{rrr)r{rr/rrztest_module_for_bdb.main())rrrcrrZrunevalr:locals)r/rIrrrr r r test_runeval_steps   zRunTestCase.test_runeval_stepN)rrrrr3r5r r r r r.s r.c@s8eZdZdZddZddZddZdd Zd d Zd S) IssuesTestCasezTest fixed bdb issues.c Csd}d}t|d|i}t|rdtddddd d dd d igffd d d ddg |_t|}|tWdn1sv0YWdn1s0YdS)Nz from test_module_for_bdb_2 import func def main(): func() lno = 5 z9 def func(): lno = 3 Ztest_module_for_bdb_2rrHztest_module_for_bdb_2.pyrrr%rrr)r{rrH)r%rrr)rrrrcrrr)r/Zcode_1Zcode_2rrr r r +test_step_at_return_with_no_trace_in_callers  z:IssuesTestCase.test_step_at_return_with_no_trace_in_callerc Csd}t|i}dD]}|j|dt|dtdtdddd dd d igff|fg|_|d kr||jd d dtfdddgn|jddgt|}| t Wdn1s0YWdn1s0YWdq1s0YqdS)Nz def test_gen(): yield 0 lno = 4 return 123 def main(): it = test_gen() next(it) next(it) lno = 11 )rrr{rrtest_genrrr%rrr{rp rr)r{r9rrr%rr8) rrrrrrcrrrrr)r/rIrrrr r r #test_next_until_return_in_generators*    z2IssuesTestCase.test_next_until_return_in_generatorcCsd}t|i}t|dtdtdddddddigffd d d d d d d d ddtfdddg|_t|}|tWdn1s0YWdn1s0YdS)Nz def test_gen(): yield 0 lno = 4 yield 1 return 123 def main(): for i in test_gen(): lno = 10 lno = 11 rr8rrr%rrrr:)r%rr8)r%rr8rp rr)r% rr rrrrrrcrrrrr r r 'test_next_command_in_generator_for_loop#s    z6IssuesTestCase.test_next_command_in_generator_for_loopc Csd}t|i}t|~dtdtdddddddigffd d d d d d tfdddg |_t|}|tWdn1sz0YWdn1s0YdS)N4 def test_subgen(): yield 0 return 123 def test_gen(): x = yield from test_subgen() return 456 def main(): for i in test_gen(): lno = 12 lno = 13 rr8rrr%rrr)r%r8rpr=rrr% rrr>rr r r /test_next_command_in_generator_with_subiteratorBs   z>IssuesTestCase.test_next_command_in_generator_with_subiteratorc Csd}t|i}t|dtdtdddddddigffd d d d tfd d d dtfdddg |_t|}|tWdn1s0YWdn1s0YdS)Nr@rZ test_subgenrrr%rrr rprr8r=rrrBrr>rr r r 1test_return_command_in_generator_with_subiteratoras    z@IssuesTestCase.test_return_command_in_generator_with_subiteratorN) rrrrr7r;r?rDrEr r r r r6s )r6__main__)N)-rrrrr!Zunittestrr;r contextlibr itertoolsrrZ test.supportr Exceptionrr rrrdrr)r*r_rrrr9rrrrrrrZTestCaserrrr.r6rrr r r r sR2 @z. J  /B")