a ze1@sdZddlZddlZddlZddlZddlZz ddlZWneyNdZYn0ddlm Z m Z m Z m Z m Z ddZddZdd ZGd d d ejZd d ZGdddejZGdddejZe ddrvedurvejZedejZejZefe_eje_ej Z!ej"ejejfe!_ej#e!_ej$Z%ej"eje&ejfe%_ej#e%_da'ddZ(ee(Z)ee)Z*GdddejZ+dddZ,e-dkre,dS)aThis module includes tests of the code object representation. >>> def f(x): ... def g(y): ... return x + y ... return g ... >>> dump(f.__code__) name: f argcount: 1 posonlyargcount: 0 kwonlyargcount: 0 names: () varnames: ('x', 'g') cellvars: ('x',) freevars: () nlocals: 2 flags: 3 consts: ('None', '', "'f..g'") >>> dump(f(4).__code__) name: g argcount: 1 posonlyargcount: 0 kwonlyargcount: 0 names: () varnames: ('y',) cellvars: () freevars: ('x',) nlocals: 1 flags: 19 consts: ('None',) >>> def h(x, y): ... a = x + y ... b = x - y ... c = a * b ... return c ... >>> dump(h.__code__) name: h argcount: 2 posonlyargcount: 0 kwonlyargcount: 0 names: () varnames: ('x', 'y', 'a', 'b', 'c') cellvars: () freevars: () nlocals: 5 flags: 67 consts: ('None',) >>> def attrs(obj): ... print(obj.attr1) ... print(obj.attr2) ... print(obj.attr3) >>> dump(attrs.__code__) name: attrs argcount: 1 posonlyargcount: 0 kwonlyargcount: 0 names: ('print', 'attr1', 'attr2', 'attr3') varnames: ('obj',) cellvars: () freevars: () nlocals: 1 flags: 67 consts: ('None',) >>> def optimize_away(): ... 'doc string' ... 'not a docstring' ... 53 ... 0x53 >>> dump(optimize_away.__code__) name: optimize_away argcount: 0 posonlyargcount: 0 kwonlyargcount: 0 names: () varnames: () cellvars: () freevars: () nlocals: 0 flags: 67 consts: ("'doc string'", 'None') >>> def keywordonly_args(a,b,*,k1): ... return a,b,k1 ... >>> dump(keywordonly_args.__code__) name: keywordonly_args argcount: 2 posonlyargcount: 0 kwonlyargcount: 1 names: () varnames: ('a', 'b', 'k1') cellvars: () freevars: () nlocals: 3 flags: 67 consts: ('None',) >>> def posonly_args(a,b,/,c): ... return a,b,c ... >>> dump(posonly_args.__code__) name: posonly_args argcount: 3 posonlyargcount: 2 kwonlyargcount: 0 names: () varnames: ('a', 'b', 'c') cellvars: () freevars: () nlocals: 3 flags: 67 consts: ('None',) N) run_doctest run_unittest cpython_onlycheck_impl_detail gc_collectccs4|D]*}t|}|dr(d|jVq|VqdS)z.Yield a doctest-safe sequence of object reprs.z N)repr startswithco_name)teltrr B/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_code.pyconstss  rcCs<dD]}td|t|d|fqtdtt|jdS)z1Print out a text representation of a code object.) nameZargcountZposonlyargcountZkwonlyargcountnamesZvarnamesZcellvarsZfreevarsZnlocalsflagsz%s: %sZco_zconsts:N)printgetattrtupler co_consts)coattrr r rdumpsrcCsdt|S)NzForeign getitem: )super __getitem__)selfir r rexternal_getitemsrc@s4eZdZeddZeddZddZddZd S) CodeTestcCsDddl}|ddd}||jd||jd||jddS)Nrfilenamefuncname) _testcapiZ code_newempty assertEqual co_filenamer co_firstlineno)rr#rr r r test_newemptys zCodeTest.test_newemptycsddlmddddfdd}Gd d d t}||d t|j}|jdj}|||||j j t j @t |j j |gd }||dd dS)Nr) FunctionTypecsfddjS)NcsSNr r  __class__r rzICodeTest.test_closure_injection..create_closure..) __closure__r*r r*rcreate_closuresz7CodeTest.test_closure_injection..create_closurecSs|j|jddS)z9A new code object with a __class__ cell added to freevarsr*) co_freevars)replacer0)cr r rnew_codesz1CodeTest.test_closure_injection..new_codec s6|j}|}|j}t|||t|||dSr))__code__ __defaults__setattrglobals)clsrfcodeZclosuredefaultsr(r/r3r radd_foreign_methods z;CodeTest.test_closure_injection..add_foreign_methodc@s eZdZdS)z-CodeTest.test_closure_injection..ListN)__name__ __module__ __qualname__r r r rListsrAr)zForeign getitem: 1)typesr(listrrr. cell_contentsZassertIs assertFalser4co_flagsinspectZ CO_NOFREEhexr$)rr=rAfunctionZ class_refobjr r<rtest_closure_injections      zCodeTest.test_closure_injectioncCs`dd}|j}t|}||j|j|j|j|j|j|j|j |j |j |j |j |j|j|j|jdS)NcSsdSr)r r r r rfuncr-z'CodeTest.test_constructor..func)r4type co_argcountco_posonlyargcountco_kwonlyargcount co_nlocals co_stacksizerIco_coderco_names co_varnamesr%r r& co_lnotabr0 co_cellvars)rrOrCodeTyper r rtest_constructors(zCodeTest.test_constructorcCsdd}|j}dd}|j}ddddd d |jtjBfd d |jfd |jfdd|jfddddd|jffD]\\}}|j||d6|j fi||i}| t |||Wdqd1s0YqddS)NcSsd}|S)NrBr )xr r rrOsz#CodeTest.test_replace..funccSsd}|S)NrCr )yr r rfunc2sz$CodeTest.test_replace..func2)rQr)rRr)rSr)rTr)rUrrI)r&drVr)rW)ZmynamerX)r0)Zfreevar)rZ)Zcellvar)r%Z newfilename)r ZnewnamerY)rvalue) r4rIrJZ CO_COROUTINErVrrXrYZsubTestr1r$r)rrOr:r_code2rrar3r r r test_replaces0 zCodeTest.test_replaceN)r>r?r@rr'rNr\rcr r r rrs   $rcCs|td|ddduS)N_rB)sysintern)sr r r isinterned sric@s`eZdZddZddZddZeddZed d Zed d Z ed dZ eddZ dS)CodeConstsTestcCs4|D]}||kr|Sq||||ddS)NzShould never be reached)ZassertInfail)rrravr r r find_consts   zCodeConstsTest.find_constcCst|s|d|fdS)NzString %r is not internedrirkrrhr r rassertIsInternedszCodeConstsTest.assertIsInternedcCst|r|d|fdS)NzString %r is internedrnror r rassertIsNotInternedsz"CodeConstsTest.assertIsNotInternedcCs(tddd}||jd}||dS)Nzres = "str_value"?exec str_valuecompilermrrprrrlr r rtest_interned_string s z#CodeConstsTest.test_interned_stringcCs,tddd}||jd}||ddS)Nzres = ("str_value",)rrrsrtrrurwr r rtest_interned_string_in_tuple&s z,CodeConstsTest.test_interned_string_in_tuplecCs4tddd}||jtd}|t|ddS)Nzres = a in {"str_value"}rrrsryr)rvrmr frozensetrprrwr r r!test_interned_string_in_frozenset,s z0CodeConstsTest.test_interned_string_in_frozensetcCsddd}||dS)NrtcSs|Sr)r )ar r rr94sz6CodeConstsTest.test_interned_string_default..f)rt)rprr9r r rtest_interned_string_default2s z+CodeConstsTest.test_interned_string_defaultcCs(tddd}||jd}||dS)Nzres = "str\0value!"rrrsz strvalue!)rvrmrrqrwr r rtest_interned_string_with_null8s z-CodeConstsTest.test_interned_string_with_nullN) r>r?r@rmrprqrrxrzr|rrr r r rrjs    rjc@seZdZddZdS)CodeWeakRefTestcsti}tdt||d}~d_fdd}t|j|}t|~t t|jdS)Nz def f(): passr9Fcs d_dS)NT)called)r:rr rcallbackJsz,CodeWeakRefTest.test_basic..callback) rsr7rweakrefrefr4Z assertTrueboolrrH)r namespacer9rZcoderefr rr test_basicAs zCodeWeakRefTest.test_basicN)r>r?r@rr r r rr?srTZcpythoncCs|adSr)) LAST_FREED)Zptrr r rmyfreeksrc@s<eZdZddZddZddZddZd d Zd d Zd S)CoExtracCstdS)Nz lambda:42)evalrr r rget_funcsszCoExtra.get_funcc Cs<|}|ttdttd|ttdttddS)N*r`)r assertRaises SystemErrorSetExtra FREE_INDEXctypesc_voidpGetExtrar~r r rtest_get_non_codeys  zCoExtra.test_get_non_codec CsJ|}|tt|jtdtd|t |jtdtdddS)Nr`r) rrrrr4rrrr$rr~r r rtest_bad_indexs zCoExtra.test_bad_indexcCs.|}t|jttd~|tddS)Nr`)rrr4rrrr$rr~r r rtest_free_calledszCoExtra.test_free_calledcCsn|}t}t|jttdt|jttd|tdt}t|jt|||j d~dS)Ni,) rrrrr4rr$rrra)rr9extrar r r test_get_sets zCoExtra.test_get_setcCsZ|}Gdddtj}t|jttd|||}~|| | t ddS)Ncs$eZdZfddZddZZS)z6CoExtra.test_free_different_thread..ThreadTestcst||_||_dSr))r__init__r9test)rr9rr*r rrs z?CoExtra.test_free_different_thread..ThreadTest.__init__cSs|`|jtddS)N)r9rr$rrr r rrunsz:CoExtra.test_free_different_thread..ThreadTest.run)r>r?r@rr __classcell__r r r*r ThreadTests rr) r threadingThreadrr4rrrstartjoinr$r)rr9rttr r rtest_free_different_threads  z"CoExtra.test_free_different_threadN) r>r?r@rrrrrrr r r rrrs  rcCsHddlm}t||tttg}tddrr r r rsN     i0 D