a 4dg1@sdZddlZddlZddlZddlZddlZz ddlZWneyNdZYn0ddlm Z m Z m Z m Z ddZ ddZdd ZGd d d ejZd d ZGdddejZGdddejZe ddrredurrejZedejZejZefe_eje_ejZ 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_detailccs4|D]*}t|}|dr(d|jVq|VqdS)z.Yield a doctest-safe sequence of object reprs.z N)repr startswithco_name)teltrr */usr/local/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 r dumpsrcCsdt|S)NzForeign getitem: )super __getitem__)selfir r r external_getitemsrc@s4eZdZeddZeddZddZddZd S) CodeTestcCsDddl}|ddd}||jd||jd||jddS)Nrfilenamefuncname) _testcapiZ code_newempty assertEqual co_filenamerco_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 r zICodeTest.test_closure_injection..create_closure..) __closure__r)r r)r create_closuresz7CodeTest.test_closure_injection..create_closurecSs|j|jddS)z9A new code object with a __class__ cell added to freevarsr)) co_freevars)replacer/)cr r r new_codesz1CodeTest.test_closure_injection..new_codec s6|j}|}|j}t|||t|||dSr()__code__ __defaults__setattrglobals)clsrfcodeZclosuredefaultsr'r.r2r r add_foreign_methods z;CodeTest.test_closure_injection..add_foreign_methodc@s eZdZdS)z-CodeTest.test_closure_injection..ListN)__name__ __module__ __qualname__r r r r Listsr@r)zForeign getitem: 1)typesr'listrrr- cell_contentsZassertIs assertFalser3co_flagsinspectZ CO_NOFREEhexr#)rr<r@functionZ class_refobjr r;r test_closure_injections      zCodeTest.test_closure_injectioncCs\dd}|j}t|}||j|j|j|j|j|j|j|j |j |j |j |j |j|j|j|jS)NcSsdSr(r r r r r funcr,z'CodeTest.test_constructor..func)r3type co_argcountco_posonlyargcountco_kwonlyargcount co_nlocals co_stacksizerHco_coderco_names co_varnamesr$rr% co_lnotabr/ co_cellvars)rrNrCodeTyper r r test_constructors(zCodeTest.test_constructorcCsdd}|j}dd}|j}ddddd d |jtjBfd d |jfd |jfdd|jfddddd|jffD]\\}}|j||d6|j fi||i}| t |||Wdqd1s0YqddS)NcSsd}|S)NrAr )xr r r rNsz#CodeTest.test_replace..funccSsd}|S)NrBr )yr r r func2sz$CodeTest.test_replace..func2)rPr)rQr)rRr)rSr)rTrrH)r%drUr)rV)ZmynamerW)r/)Zfreevar)rY)Zcellvar)r$Z newfilename)rZnewnamerX)rvalue) r3rHrIZ CO_COROUTINErUrrWrXZsubTestr0r#r)rrNr9r^code2rr`r2r r r test_replaces0 zCodeTest.test_replaceN)r=r>r?rr&rMr[rbr r r r rs   $rcCs|td|ddduS)N_rA)sysintern)sr r r isinterned srhc@s`eZdZddZddZddZeddZed d Zed d Z ed dZ eddZ dS)CodeConstsTestcCs4|D]}||kr|Sq||||ddS)NzShould never be reached)ZassertInfail)rrr`vr r r find_consts   zCodeConstsTest.find_constcCst|s|d|fdS)NzString %r is not internedrhrjrrgr r r assertIsInternedszCodeConstsTest.assertIsInternedcCst|r|d|fdS)NzString %r is internedrmrnr r r assertIsNotInternedsz"CodeConstsTest.assertIsNotInternedcCs(tddd}||jd}||dS)Nzres = "str_value"?exec str_valuecompilerlrrorrrkr r r test_interned_string s z#CodeConstsTest.test_interned_stringcCs,tddd}||jd}||ddS)Nzres = ("str_value",)rqrrrsrrtrvr r r test_interned_string_in_tuple&s z,CodeConstsTest.test_interned_string_in_tuplecCs4tddd}||jtd}|t|ddS)Nzres = a in {"str_value"}rqrrrxr)rurlr frozensetrorrvr r r !test_interned_string_in_frozenset,s z0CodeConstsTest.test_interned_string_in_frozensetcCsddd}||dS)NrscSs|Sr(r )ar r r r84sz6CodeConstsTest.test_interned_string_default..f)rs)rorr8r r r test_interned_string_default2s z+CodeConstsTest.test_interned_string_defaultcCs(tddd}||jd}||dS)Nzres = "str\0value!"rqrrz strvalue!)rurlrrprvr r r test_interned_string_with_null8s z-CodeConstsTest.test_interned_string_with_nullN) r=r>r?rlrorprrwryr{r~rr r r r ris    ric@seZdZddZdS)CodeWeakRefTestcsni}tdt||d}~d_fdd}t|j|}t|~t|jdS)Nz def f(): passr8Fcs d_dS)NT)called)r9rr r callbackJsz,CodeWeakRefTest.test_basic..callback) rrr6rweakrefrefr3Z assertTrueboolrG)r namespacer8rZcoderefr rr test_basicAs zCodeWeakRefTest.test_basicN)r=r>r?rr r r r r?srTZcpythoncCs|adSr() LAST_FREED)Zptrr r r myfreejsrc@s<eZdZddZddZddZddZd d Zd d Zd S)CoExtracCstdS)Nz lambda:42)evalrr r r get_funcrszCoExtra.get_funcc Cs<|}|ttdttd|ttdttddS)N*r_)r assertRaises SystemErrorSetExtra FREE_INDEXctypesc_voidpGetExtrar}r r r test_get_non_codexs  zCoExtra.test_get_non_codec CsJ|}|tt|jtdtd|t |jtdtdddS)Nr_r) rrrrr3rrrr#rr}r r r test_bad_indexs zCoExtra.test_bad_indexcCs.|}t|jttd~|tddS)Nr_)rrr3rrrr#rr}r r r test_free_calledszCoExtra.test_free_calledcCsn|}t}t|jttdt|jttd|tdt}t|jt|||j d~dS)Ni,) rrrrr3rr#rrr`)rr8extrar 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__r8test)rr8rr)r r rs z?CoExtra.test_free_different_thread..ThreadTest.__init__cSs|`|jtddS)N)r8rr#rrr r r runsz:CoExtra.test_free_different_thread..ThreadTest.run)r=r>r?rr __classcell__r r r)r ThreadTests rr) r threadingThreadrr3rrrstartjoinr#r)rr8rttr r r test_free_different_threads  z"CoExtra.test_free_different_threadN) r=r>r?rrrrrrr r r r rqs  rcCsHddlm}t||tttg}tddrsN     i0 D