a ze<@sZddlmZddlZddlZddlZddlZddlZddlmZddl Z ddl Z ddl Z ddl m Z ddlmZe dZddZdKd d ZdLd d Zejd dZddZifddZGdddeddZejddZGdddeddZGdddeddZddd d!Zd"d#Zd$d%Zd&d'Z Gd(d)d)e j!Z"Gd*d+d+e j!Z#Gd,d-d-e j!Z$Gd.d/d/e"Z%Gd0d1d1e"Z&Gd2d3d3e"Z'Gd4d5d5e"Z(Gd6d7d7e"Z)Gd8d9d9e"Z*Gd:d;d;e"Z+Gdd?d?e"Z-Gd@dAdAe"Z.GdBdCdCe"Z/GdDdEdEedEdFZ0e 1dGGdHdIdIe"Z2e3dJkrVe 4dS)M) namedtupleN)dedent)support) script_helperZ_xxsubinterpreterscCs:t\}}|dd}td|d|d}|t|fS)N z z- import contextlib with open(zU, 'w') as spipe: with contextlib.redirect_stdout(spipe): z )ospipereplaceropen)scriptrwZindentedwrappedrP/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test__xxsubinterpreters.py_captured_scripts   rcCsJt|\}}|&t||||WdS1s<0YdSN)r interpreters run_stringread)interpZrequestsharedr Zrpiperrr _run_output"s rcCsL|durtj}t}||}t|sHt|kr.runtargetr done) rr threadingThreadstartr r writejoin)rr r"tZspiperr!r_running7s   (r,cKst|||dSr) _run_interp)idsourcerrrr run_interpVsr0cCsFt|}t}||kr4t|kr(tt||nt|||dSr)rrget_main get_currentrexecr)r.r/rZ_mainnsmainrrrr-Zs  r-cs*eZdZeddZdfdd ZZS) InterpretercCs(t||r|St|tr ||StdSr) isinstancestrNotImplementedError)clsrawrrrfrom_rawhs   zInterpreter.from_rawNcst}||kr6|sd}n|dkr0td||}nX|durp|sHd}n|dkrXtdt|tjst|}n|r||dkrd}|}nt}t|||}|S)Nr4z)name mismatch (expected "main", got "{}")rz!name mismatch (unexpected "main")) rr1 ValueErrorformatr6 InterpreterIDcreatesuper__new__)r9namer.r4self __class__rrrAqs,   zInterpreter.__new__)NN)__name__ __module__ __qualname__ classmethodr;rA __classcell__rrrDrr5fs r5zname idccs0z dVWntjyYn0ds,JddS)NFzchannel not closed)rChannelClosedErrorrrrrexpect_channel_closeds  rLcs6eZdZd fdd ZddZddZdd ZZS) ChannelActionNcs&|sd}|sd}t||||}|S)Nbothr4r@rA)r9actionendrrCrDrrrAs zChannelAction.__new__cOsb|jdkr |jdvrJt|jn*|jdvr@|jdvrJt|jn t|j|jdvr^t|jdS)Nuse)sameoppositesendrecvclose force-close)rNrSrTrUrV)r4rSotherextra)rPrQr<r)rCargskwargsrrr__init__s        zChannelAction.__init__cCs2|jdkr|S|jdkr(|dkr$dSdS|jSdS)NrSrTrUrVrQrCrQrrr resolve_ends   zChannelAction.resolve_endcCst|jdkr|S|jdkr(|dur$t|S|jdkrB|dur>t|S|jdkrp|jdkrZ|S|rl|jdkrl|StdS)NrSrZr[r4)rrrB)rCrrZr[rrrresolve_interps      zChannelAction.resolve_interp)NN)rFrGrHrAr^rarbrJrrrDrrMs rMzaction end interpcsBeZdZdddfdd ZddZdd Zd d d d ZZS) ChannelStaterFclosedcst|||}|SrrO)r9pendingrerCrDrrrAszChannelState.__new__cCst||jd|jdSNrdtyperfrerCrrrincrszChannelState.incrcCst||jd|jdSrgrirkrrrdecrszChannelState.decrTforcecCs2|jr|r|jdkr|St||r&dn|jddS)NrTrd)rerfrj)rCrorrrrXszChannelState.close)r)rFrGrHrArlrmrXrJrrrDrrcsrczpending closedT hideclosedcCsr|jr(|dkr"|dkr"|jr"d}q,d}nd}zt||||}Wn&tjyd|sX|sX|}Yn 0|rnd|S)NrRrVFT.)rerf _run_actionrrKrX)cidrPrQstaterqZ expectfailresultrrr run_actionsrvcCs|dkr|dkr$t|d|S|dkrx|jsdzt|WntjyX|YS0tdqt||Snt|n~|dkri}|dvrd||<tj |fi|| S|d krd di}|dvrd||<tj |fi||j dd St|dS) NrRrUspamrVzexpected ChannelEmptyErrorrXrVrUTrYrorn) r channel_sendrlrf channel_recvChannelEmptyError Exceptionrmr< channel_closerX)rsrPrQrtr]rrrrrs8        rrc Cs>tD]0}|dkrqzt|Wqty6Yq0qdS)Nr)rlist_alldestroyrr.rrrclean_up_interpreterss  rc Cs6tD](}zt|Wqtjy.Yq0qdSr)rchannel_list_allchannel_destroyChannelNotFoundErrorrsrrrclean_up_channelss  rc@seZdZddZdS)TestBasecCsttdSrrrrkrrrtearDown)szTestBase.tearDownN)rFrGrHrrrrrr'src@seZdZddZddZdS)IsShareableTestsc CsPgd}|D]>}|| |t|Wdq 1s@0Yq dS)N)Nrwspam i)subTest assertTruer is_shareable)rCZ shareablesobjrrrtest_default_shareables3s   z(IsShareableTests.test_default_shareablesc CsGddd}Gdddt}ddtdttttd||d |d g }|D]B}|t| |t |WdqJ1s0YqJdS) Nc@seZdZddZddZdS)z3IsShareableTests.test_not_shareable..CheesecSs ||_dSrrB)rCrBrrrr^Dsz.Cheese.__init__cSs|jSrrrkrrr__str__Fsz;IsShareableTests.test_not_shareable..Cheese.__str__N)rFrGrHr^rrrrrCheeseCsrc@seZdZdZdS)z5IsShareableTests.test_not_shareable..SubByteszA subclass of a shareable type.N)rFrGrH__doc__rrrrSubBytesIsrTF.gY@Z Wensleydalerw) bytesNotImplementedrjobjectr|rrepr assertFalserr)rCrrZnot_shareablesrrrrtest_not_shareableBs(z#IsShareableTests.test_not_shareableN)rFrGrHrrrrrrr1srcs`eZdZfddZfddZddZddZd d Zd d Zd dZ ddZ ddZ Z S)ShareableTypeTestscstt|_dSr)r@setUprchannel_creatersrkrDrrres zShareableTypeTests.setUpcst|jtdSr)rrrsr@rrkrDrrris zShareableTypeTests.tearDownc Csr|D]h}||Jt|j|t|j}||||t|t|Wdq1sb0YqdSr)rrryrsrz assertEqualassertIsrj)rCvaluesrgotrrr_assert_valuesms    z!ShareableTypeTests._assert_valuesc Cs^dD]T}||6t|j|t|j}|||Wdq1sN0YqdS)Nr)rrryrsrzr)rCrrrrrtest_singletonsxs   z"ShareableTypeTests.test_singletonscCs|dd|jgdS)Nrwi')rrsrkrrr test_typess zShareableTypeTests.test_typescCs|ddtddDdS)Ncss|]}|jddddVqdS)littleT)signedN)to_bytes).0irrr sz0ShareableTypeTests.test_bytes..)rrangerkrrr test_bytess zShareableTypeTests.test_bytescCs|gddS)N)z hello worldu 你好世界)rrkrrr test_strsszShareableTypeTests.test_strscCs*|ttddtjtj dgdS)Nrrrh)r itertoolschainrsysmaxsizerkrrrtest_intszShareableTypeTests.test_intc Cstjdtj dddg}|D]f}||H|tt|j|Wdn1s^0YWdq 1s|0Yq dS)Nrhr)rrr assertRaises OverflowErrorrryrs)rCZintsrrrrtest_non_shareable_ints   z)ShareableTypeTests.test_non_shareable_int) rFrGrHrrrrrrrrrrJrrrDrrcs    rc@s$eZdZddZddZddZdS) ListAllTestscCs"t}t}|||gdSr)rr1r~r)rCr4idsrrr test_initialszListAllTests.test_initialcCs6t}t}t}t}|||||gdSr)rr1r?r~rrCr4firstsecondrrrrtest_after_creatings z ListAllTests.test_after_creatingcCs>t}t}t}t|t}||||gdSr)rr1r?rr~rrrrrtest_after_destroyings  z"ListAllTests.test_after_destroyingN)rFrGrHrrrrrrrrsrc@seZdZddZddZdS)GetCurrentTestscCs.t}t}|||||tjdSr)rr1r2rassertIsInstancer>)rCr4currrr test_mains zGetCurrentTests.test_maincCsRt}t}t|td}t|}t\}}|||| ||dS)Nz import _xxsubinterpreters as _interpreters cur = _interpreters.get_current() print(cur) assert isinstance(cur, _interpreters.InterpreterID) ) rr1r?rrintstripr~rZassertNotEqual)rCr4routr_expectedrrrtest_subinterpreters   z#GetCurrentTests.test_subinterpreterN)rFrGrHrrrrrrrsrc@seZdZddZddZdS) GetMainTestscCs0t\}t}|||||tjdSr)rr~r1rrr>)rCrr4rrrtest_from_mains  zGetMainTests.test_from_maincCs<t\}t}t|td}t|}|||dS)Nz import _xxsubinterpreters as _interpreters main = _interpreters.get_main() print(main) assert isinstance(main, _interpreters.InterpreterID) )rr~r?rrrrr)rCrrrr4rrrtest_from_subinterpreters   z%GetMainTests.test_from_subinterpreterN)rFrGrHrrrrrrrsrc@s<eZdZddZddZddZddZd d Zd d Zd S)IsRunningTestscCst}|t|dSr)rr1rr)rCr4rrrrszIsRunningTests.test_maincCsdt}|t|t| |t|Wdn1sF0Y|t|dSr)rr?rrr,rrCrrrrrs  .z"IsRunningTests.test_subinterpretercCs2t}t|td|d}||ddS)Nz` import _xxsubinterpreters as _interpreters if _interpreters.is_running(zZ): print(True) else: print(False) True)rr?rrrr)rCrrrrrrs  z'IsRunningTests.test_from_subinterpretercCsJt}t||tt|Wdn1s<0YdSr)rr?rrrrrrrrtest_already_destroyeds  z%IsRunningTests.test_already_destroyedcCs8|ttdWdn1s*0YdSNi@B)rrrrrkrrrtest_does_not_exists z"IsRunningTests.test_does_not_existcCs8|ttdWdn1s*0YdSNr)rr<rrrkrrr test_bad_ids zIsRunningTests.test_bad_idN) rFrGrHrrrrrrrrrrrs  rc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)InterpreterIDTestscCs"tjddd}|t|ddS)NrTrn)rr>rrrCr.rrr test_with_intsz InterpreterIDTests.test_with_intcCs4Gdddt}tj|dd}|t|ddS)Nc@seZdZddZdS)z.InterpreterIDTests.test_coerce_id..IntcSsdSNrrrkrrr __index__sz8InterpreterIDTests.test_coerce_id..Int.__index__NrFrGrHrrrrrIntsrTrnr)r7rr>rr)rCrr.rrrtest_coerce_idsz!InterpreterIDTests.test_coerce_idcCsf|ttjt|ttjd|ttjd|ttjd|ttjd|ttjddSNg$@10s10rl)r TypeErrorrr>rr<rrkrrrrs zInterpreterIDTests.test_bad_idcCsHt}|t"tt|dWdn1s:0YdSNrh)rrrrr>rrrrrr$s z&InterpreterIDTests.test_does_not_existcCs"tjddd}|t|ddSNrTrnr)rr>rr7rrrrtest_str)szInterpreterIDTests.test_strcCs"tjddd}|t|ddS)NrTrnzInterpreterID(10))rr>rrrrrr test_repr-szInterpreterIDTests.test_reprcCs(t}tt|}t}|||k|||k||t|k|t||k||tt|k|tt||k||tt|dk||tt|k||ddk||tdk||dk|||k|||k|||k|||kdSN皙?rrinfr)rr?r>rrfloatrr7rCid1id2id3rrr test_equality1s$z InterpreterIDTests.test_equalityN) rFrGrHrrrrrrrrrrrr src@sNeZdZddZedddZddZdd Zd d Z d d Z ddZ dS) CreateTestscCs*t}||tj||tdSr)rr?rr>assertInr~rrrr test_in_mainJszCreateTests.test_in_mainz*enable this test when working on pystate.ccCsDt}tdD] }t}t|||q|t|ddS)Nd)setrrr?raddrlen)rCseenrr.rrrtest_unique_idPs    zCreateTests.test_unique_idcsntdfdd}tj|d}|Wdn1sH0Y||tdS)NcstdSr)rr?acquirereleaserr.lockrrf]sz%CreateTests.test_in_thread..fr#)r&Lockr'r(r*rrr~rCrr+rrrtest_in_threadZs &zCreateTests.test_in_threadcCsJt\}t}t|td}t|}|tt|||hdS)Nz import _xxsubinterpreters as _interpreters id = _interpreters.create() print(id) assert isinstance(id, _interpreters.InterpreterID) ) rr~r?rrrrrr)rCr4rrrrrrtest_in_subinterpreteris   z"CreateTests.test_in_subinterpretercs^t\}tdfdd}tj|d}|||tt|hdS)Ncsttd}t|dS)Nz import _xxsubinterpreters as _interpreters id = _interpreters.create() print(id) )rrrr)rrrrrrzsz6CreateTests.test_in_threaded_subinterpreter..fr#) rr~r?r&r'r(r*rr)rCr4rr+rrrtest_in_threaded_subinterpretervs  z+CreateTests.test_in_threaded_subinterpretercCsjtt}g}tdD]}t}||q|D]}t|q4t}|tt||hBdSN)rrr~rr?appendrrrCbeforerrr.rrrtest_after_destroy_alls    z"CreateTests.test_after_destroy_allcCs`tt}t}t}t}t|t|t}|tt|||hBdSr)rrr~r?rr)rCr rrrr.rrrtest_after_destroy_somes   z#CreateTests.test_after_destroy_someN) rFrGrHrunittestskiprrrrr r rrrrrHs  rc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) DestroyTestscCsft}t}t}||tt|||t||t||tdSr)rr?rr~r assertNotInrrrrtest_ones zDestroyTests.test_onecCsvtt}t}tdD]}t}||q|tt||B|D]}t|qN|tt|dSr)rrr~rr?rrrr rrrtest_alls    zDestroyTests.test_allcslt\ttWdn1s40Yfdd}tj|d}||dS)Ncs8ttWdn1s*0YdSrrrrrrr4rCrrrs z!DestroyTests.test_main..fr#) rr~rrrr&r'r(r*rrrrrs  ( zDestroyTests.test_maincCsJt}t||tt|Wdn1s<0YdSr)rr?rrrrrrrrs  z#DestroyTests.test_already_destroyedcCs8|ttdWdn1s*0YdSrrrkrrrrs z DestroyTests.test_does_not_existcCs8|ttdWdn1s*0YdSr)rr<rrrkrrrrs zDestroyTests.test_bad_idcCsJt\}t}td|d}t|||tt||hdS)Nzo import _xxsubinterpreters as _interpreters try: _interpreters.destroy(zD) except RuntimeError: pass rr~r?rrrr)rCr4r.r rrrtest_from_currents   zDestroyTests.test_from_currentcCsRt\}t}t}td|d}t|||tt||hdS)NzZ import _xxsubinterpreters as _interpreters _interpreters.destroy() r)rCr4rrr rrrtest_from_siblings   zDestroyTests.test_from_siblingcs4tfdd}tj|d}||dS)NcstdSr)rrrrrrrsz.DestroyTests.test_from_other_thread..fr#)rr?r&r'r(r*rrrrtest_from_other_threads   z#DestroyTests.test_from_other_threadc Cst\}t}t|||jt|d|dd|jtd|ddt|Wdn1sn0Y|t|Wdn1s0YdS)NzInterp z& should be running before destruction.)msgz%Should not be able to destroy interp z while it's still running.) rr~r?r,rrrrr)rCr4rrrrtest_still_runnings     (zDestroyTests.test_still_runningN) rFrGrHrrrrrrrrrrrrrrrs     rcseZdZedZdZfddZfddZddZd d Z d d Z e e ed dddZddZddZddZddZddZddZejd5ddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Z d0d1Z!e "d2d3d4Z#Z$S)6RunStringTestszJ with open('{}', 'w') as out: out.write('{}') rcstt|_d|_dSr)r@rrr?r._fsrkrDrrrs  zRunStringTests.setUpcs"|jdur|jtdSr)rrXr@rrkrDrrrs  zRunStringTests.tearDowncCsVtd\}}|&t|j||}Wdn1s<0Y||ddS)Nprint("it worked!", end="") it worked!)rrrr.rr)rCr filerrrr test_successs  &zRunStringTests.test_successcsrtd\}|Bfdd}tj|d}|||}Wdn1sX0Y|ddS)NrcstjdSr)rrr.rr rCrrr$sz(RunStringTests.test_in_thread..fr#r)rr&r'r(r*rr)rCr rr+rrr"rr!s  &zRunStringTests.test_in_threadcCs`tjdd}td\}}|$t|||}Wdn1sF0Y||ddS)NF)isolatedz import threading def f(): print('it worked!', end='') t = threading.Thread(target=f) t.start() t.join() r)rr?rrrr)rCZ subinterpr r rrrrtest_create_thread.s    &z!RunStringTests.test_create_threadforkztest needs os.fork()cCsddl}|dj}|d|d}td|jd|d}t|j|| d| }| ||Wdn1s0YdS)Nrzw+rzspam spam spam spam spamz import os try: os.fork() except RuntimeError: with open('z3', 'w') as out: out.write('z') ) tempfileNamedTemporaryFiler)flushrrBrrr.seekrr)rCr&r rr Zcontentrrr test_fork?s    zRunStringTests.test_forkc Csft|jH|tt|jdWdn1s:0YWdn1sX0YdS)N print("spam"))r,r.rrrrrkrrrtest_already_runningUs  z#RunStringTests.test_already_runningcCsTd}|tvr|d7}q|tt|dWdn1sF0YdS)Nrrhr+)rr~rrrrrrrrZs    z"RunStringTests.test_does_not_existcCs:|ttddWdn1s,0YdS)Nrr+)rr<rrrkrrr test_error_idas zRunStringTests.test_error_idcCs:|ttddWdn1s,0YdS)Nrr+)rrrrrkrrrres zRunStringTests.test_bad_idcCs<|tt|jdWdn1s.0YdSrrrrrr.rkrrrtest_bad_scriptis zRunStringTests.test_bad_scriptcCs<|tt|jdWdn1s.0YdS)Ns print("spam")r.rkrrrtest_bytes_for_scriptms z$RunStringTests.test_bytes_for_scriptNccsz|tj}dVWdn1s(0Y|dur\|t|jddt|n|t|jd||dS)N:rz{}: {})rrRunFailedErrorrr7 exceptionsplitr=)rCexctyperZcaughtrrrassert_run_failedqs$  z RunStringTests.assert_run_failedcCs<|tt|jdWdn1s.0YdS)Nz print("spam")r6 SyntaxErrorrrr.rkrrrtest_invalid_syntax|s z"RunStringTests.test_invalid_syntaxcCs>|tdt|jdWdn1s00YdS)Nrzraise Exception("spam"))r6r|rrr.rkrrr test_failureszRunStringTests.test_failurecCs>|tdt|jdWdn1s00YdS)N42zraise SystemExit(42))r6 SystemExitrrr.rkrrrtest_SystemExitszRunStringTests.test_SystemExitcCs~|t"t|jtdWdn1s20Y|td"t|jtdWdn1sp0YdS)NzG import sys sys.exit() r:zI import sys sys.exit(42) )r6r;rrr.rrkrrr test_sys_exits 0zRunStringTests.test_sys_exitcCst\}}dddd}td|d}t|j||t|d}t|}Wdn1sb0Y| |dd| |d d | |d d | |d dS)Nhams-1)reggscheddarz eggs = int(eggs) spam = 42 result = spam + eggs ns = dict(vars()) del ns['__builtins__'] import pickle with open(C, 'wb') as chan: pickle.dump(ns, chan) rbr*r?rru)r@) rrrrrr.r pickleloadrZ assertIsNonerCr r rr channsrrrtest_with_shareds   (zRunStringTests.test_with_sharedcCst|jtdddi}td}t|j||t\}}td|d}t|j|t|d}t|}Wdn1s0Y| |ddd | |d dd| |dddS) Nzj spam = 'eggs' ns1 = dict(vars()) del ns1['__builtins__'] rr>zL ns2 = dict(vars()) del ns2['__builtins__'] zr ns = dict(vars()) del ns['__builtins__'] import pickle with open(rArBns1r?ns2) rrr.rrrr rErFr)rCrr r r rHrIrrrtest_shared_overwritess   (z%RunStringTests.test_shared_overwritescCs|t\}}ddi}td|d}t|j||t|d}t|}Wdn1s^0Y| |dddS)NrFs not __main__z spam = 42 ns = dict(vars()) del ns['__builtins__'] import pickle with open(rArB) rrrrrr.r rErFrrGrrr#test_shared_overwrites_default_varss   (z2RunStringTests.test_shared_overwrites_default_varscCst\}}t|jtd|dt|d}t|}Wdn1sP0Yt\}}t|jtd|dt|d}t|}Wdn1s0Y| d|| d|| d|| d|dS)Nz spam = True ns = dict(vars()) del ns['__builtins__'] import pickle with open(zd, 'wb') as chan: pickle.dump(ns, chan) del ns, pickle, chan rBz eggs = False ns = dict(vars()) del ns['__builtins__'] import pickle with open(rArr?) rrrrr.rr rErFrr)rCr r rHrKrLrrrtest_main_reuseds    (   (   zRunStringTests.test_main_reusedc Cst\}}td|d}t|j|t|d}t|}Wdn1sT0Y| d| d| |diddddddS) Nz spam = 42 ns = dict(vars()) ns['__builtins__'] = str(ns['__builtins__']) import pickle with open(rArB __builtins__ __loader____main__rC)rF__annotations__r __package____spec__r) rrrrrr.r rErFpopr)rCr r r rHrIrrr test_execution_namespace_is_mains"   (  z/RunStringTests.test_execution_namespace_is_mainzblocking foreverc Cstd}tP}t|d|}t|}|}Wdn1sH0YWdn1sf0Y||ddS)Na from textwrap import dedent import threading import _xxsubinterpreters as _interpreters id = _interpreters.create() def f(): _interpreters.run_string(id, dedent(''' import time # Give plenty of time for the main interpreter to finish. time.sleep(1_000_000) ''')) t = threading.Thread(target=f) t.start() rr)rrZtemp_dirrZ make_scriptZ spawn_pythonwaitr)rCr dirnamefilenameprocretcoderrrtest_still_running_at_exit s   Dz)RunStringTests.test_still_running_at_exit)N)%rFrGrHrZSCRIPTZFILENAMErrr!rr$r Z skipUnlesshasattrrr*r,rr-rr/r0 contextlibcontextmanagerr6r8r9r<r=rJrMrNrOrWrr]rJrrrDrrs8      !rc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)ChannelIDTestscCs0tjddd}|t|d||jddS)NrTrnrN)r _channel_idrrrQrCrsrrrtest_default_kwargs>sz"ChannelIDTests.test_default_kwargscCstjdddd}||jdtjddddd}||jdtjdddd}||jdtjddddd }||jdtjddddd}||jd dS) NrTrUrorUFrUrVrorVrorV)rVrUrorN)rrbrrQrcrrrtest_with_kwargsDszChannelIDTests.test_with_kwargscCs4Gdddt}tj|dd}|t|ddS)Nc@seZdZddZdS)z*ChannelIDTests.test_coerce_id..IntcSsdSrrrkrrrrVsz4ChannelIDTests.test_coerce_id..Int.__index__NrrrrrrUsrTrnr)r7rrbrr)rCrrsrrrrTszChannelIDTests.test_coerce_idcCsf|ttjt|ttjd|ttjd|ttjd|ttjd|ttjddSr)rrrrbrr<rrkrrrr\s zChannelIDTests.test_bad_idcCs>|t tjddddWdn1s00YdS)NrFrUrV)rr<rrbrkrrrtest_bad_kwargsds zChannelIDTests.test_bad_kwargscCsJt}|tj"tt|dWdn1s<0YdSr)rrrrrbrrcrrrrhsz"ChannelIDTests.test_does_not_existcCs"tjddd}|t|ddSr)rrbrr7rcrrrrmszChannelIDTests.test_strcCstjddd}|t|dtjdddd}|t|dtjdddd}|t|dtjddddd }|t|ddS) NrTrnz ChannelID(10)rezChannelID(10, send=True)rgzChannelID(10, recv=True)rf)rrbrrrcrrrrqszChannelIDTests.test_reprcCs(t}tt|}t}|||k|||k||t|k|t||k||tt|k|tt||k||tt|dk||tt|k||ddk||tdk||dk|||k|||k|||k|||kdSr)rrrbrrrrr7)rCcid1cid2Zcid3rrrr~s$zChannelIDTests.test_equalityN) rFrGrHrdrhrrrjrrrrrrrrra<s rac@s>eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zed)d*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Z d:d;Z!dd?Z#d@dAZ$dBdCZ%dDdEZ&dFdGZ'dHdIZ(dJdKZ)dLS)M ChannelTestscCst}||tjdSr)rrrZ ChannelIDrcrrrtest_create_cidszChannelTests.test_create_cidcCsrt}t}t}t}t}||t|d||t|d|t|t||||hdSr)rrrrrr)rCr rrrZafterrrrtest_sequential_idssz ChannelTests.test_sequential_idscCs\t}t|td}t|}t}t|td}t|}||t|ddS)Nz import _xxsubinterpreters as _interpreters cid = _interpreters.channel_create() print(cid) rh)rr?rrrrr)rCrrrkrrlrrrtest_ids_globals  zChannelTests.test_ids_globalcCs@t}tj|dd}tj|dd}||g||gdS)z=Test listing interpreters for a channel with no associations.TrUFN)rrchannel_list_interpretersr)rCrs send_interps recv_interpsrrr#test_channel_list_interpreters_nones  z0ChannelTests.test_channel_list_interpreters_nonecCst}t}t|dtj|dd}tj|dd}|||g||gt}t|td|dtj|dd}tj|dd}|||g|||gdS)z(Test basic listing channel interpreters.rUTrqFe import _xxsubinterpreters as _interpreters obj = _interpreters.channel_recv(rN) rr1rryrrrr?rr)rCinterp0rsrsrtinterp1rrr$test_channel_list_interpreters_basics   z1ChannelTests.test_channel_list_interpreters_basiccCst}t}t}t}t}t|dt|td|dt|td|dt|td|dtj|dd}tj|dd}|t |||h|t |||hd S) z?Test listing interpreters for a channel with many associations.rU_ import _xxsubinterpreters as _interpreters _interpreters.channel_send(z, "send") rvrTrqFN) rr1r?rryrrrrrr)rCrwrxinterp2interp3rsrsrtrrr'test_channel_list_interpreters_multiples&    z4ChannelTests.test_channel_list_interpreters_multiplecCst}t}t}t|dt|td|dtj|dd}tj|dd}|||g|||gt |tj|dd}tj|dd}|||g||gdS)z?Test listing channel interpreters with a destroyed interpreter.rUrvrTrqFN) rr1r?rryrrrrrrrCrwrxrsrsrtrrr(test_channel_list_interpreters_destroyeds    z5ChannelTests.test_channel_list_interpreters_destroyedcCs>t}t}t}t}t|dt|td|dt|dt|td|dtj|dd}tj|dd}|t |d|t |dtj |ddtj|dd}tj|dd}|t |d |t |dt|td |dtj|dd}tj|dd}|t |d |||gd S) z:Test listing channel interpreters with a released channel.datarvrTrqFrhrrb import _xxsubinterpreters as _interpreters _interpreters.channel_release(N) rr1r?rryrrrrrrchannel_release)rCrwrxr{rsrsrtrrr'test_channel_list_interpreters_released s8     z4ChannelTests.test_channel_list_interpreters_releasedcCst}t}t}t|dtj|dd}tj|dd}|t|d|t|dtj|dd| tj tj|ddWdn1s0Y| tj tj|ddWdn1s0YdS) z8Test listing channel interpreters with a closed channel.rUTrqFrhrrnN) rr1r?rryrrrrr}rrKr~rrr%test_channel_list_interpreters_closed4s ,z2ChannelTests.test_channel_list_interpreters_closedcCsXt}t}t}t|dtj|dd}tj|dd}|t|d|t|dtj|dd| tj tj|ddWdn1s0Ytj|dd}|t|dt |t d|d | tj tj|ddWdn1s0Y| tj tj|ddWdn1sJ0YdS) zCTest listing channel interpreters with a channel's send end closed.rUTrqFrhrN` import _xxsubinterpreters as _interpreters _interpreters.channel_close(, force=True) ) rr1r?rryrrrrr}rrKrrr~rrr.test_channel_list_interpreters_closed_send_endJs( , .z;ChannelTests.test_channel_list_interpreters_closed_send_endcCs>t}d}t||t|}||||||dSNrw)rrryrzrZ assertIsNot)rCrsZorigrrrrtest_send_recv_mainns    z ChannelTests.test_send_recv_maincCst}t|td}dS)Na6 import _xxsubinterpreters as _interpreters cid = _interpreters.channel_create() orig = b'spam' _interpreters.channel_send(cid, orig) obj = _interpreters.channel_recv(cid) assert obj is not orig assert obj == orig )rr?rr)rCrrrrrtest_send_recv_same_interpreterwsz,ChannelTests.test_send_recv_same_interpretercCs@t}t}t|td|d}t|}||ddS)Nrz, b'spam') rw)rrr?rrrzr)rCrsrrrrrr%test_send_recv_different_interpreterss  z2ChannelTests.test_send_recv_different_interpreterscsVtfdd}tj|d}|td|t}||ddS)NcsDzt}Wq4Wqtjy0tdYq0qt|dS)Nr)rrzr{rrry)rrrrrs  z8ChannelTests.test_send_recv_different_threads..fr#rw) rrr&r'r(ryr*rzrrCrr+rrrr test_send_recv_different_threadss    z-ChannelTests.test_send_recv_different_threadscsfttdfdd}tj|d}|td|t}| |ddS)Ncs ttddddS)Nz import time import _xxsubinterpreters as _interpreters while True: try: obj = _interpreters.channel_recv(z) break except _interpreters.ChannelEmptyError: time.sleep(0.1) assert(obj == b'spam') _interpreters.channel_send(z, b'eggs') )rrrrsrrrrrs  zIChannelTests.test_send_recv_different_interpreters_and_threads..fr#rweggs) rrr?r&r'r(ryr*rzrrrrr1test_send_recv_different_interpreters_and_threadss   z>ChannelTests.test_send_recv_different_interpreters_and_threadscCs<|tjtddWdn1s.0YdS)Nrrw)rrrryrkrrrtest_send_not_foundsz ChannelTests.test_send_not_foundcCs:|tjtdWdn1s,0YdSr)rrrrzrkrrrtest_recv_not_foundsz ChannelTests.test_recv_not_foundcCsBt}|tjt|Wdn1s40YdSr)rrrr{rzrcrrrtest_recv_emptyszChannelTests.test_recv_emptyc Cst}t}t||}t|dt|dt|dt|dt||}t||}t|}t||}t||}|||||d||d||d||d|||dS)Nrhrwr)rrrrzryrr) rCdefaultrsZobj1obj2Zobj3Zobj4Zobj5Zobj6rrrtest_recv_defaults$               zChannelTests.test_recv_defaultcCsRt}t}t|tdt|jd}t|}||d|| ddS)Nz import _xxsubinterpreters as _interpreters print(cid.end) _interpreters.channel_send(cid, b'spam') rrwrU) rrr?rrdictrUrzrrrCrsrrrrrrtest_run_string_arg_unresolveds    z+ChannelTests.test_run_string_arg_unresolvedz(disabled until high-level channels existcCs`t}tj|dd}t}t|tdt|jd}t|}| |d| | ddS)NT)_resolvez import _xxsubinterpreters as _interpreters print(chan.id.end) _interpreters.channel_send(chan.id, b'spam') )rHrwrU) rrrbr?rrrrUrzrrrrrrtest_run_string_arg_resolveds    z)ChannelTests.test_run_string_arg_resolvedcCst}t|dt|t||tjt|dWdn1sV0Y|tjt|Wdn1s0YdSNrwrrrryrzr}rrKrcrrrtest_close_single_users   *z#ChannelTests.test_close_single_usercCst}t}t}t|td|dt|td|dt||tj(}t|td|dWdn1s0Y|dt |j |tj(}t|td|dWdn1s0Y|dt |j dS)Nrzrz_ import _xxsubinterpreters as _interpreters _interpreters.channel_recv(rz, _interpreters.channel_send(z, b'spam') rK) rrr?rrr}rr2rr7r3)rCrsrrcmrrrtest_close_multiple_userss(      * *z&ChannelTests.test_close_multiple_userscCsbt}t|dt|t||tjt|Wdn1sT0YdSrrrcrrrtest_close_multiple_times(s    z&ChannelTests.test_close_multiple_timesc Csgd}|D]\}}|||ft}t|dt|tj|||d|tjt|dWdn1s0Y|tjt|Wdn1s0YWdq 1s0Yq dS)N))FFTF)FT)TTrwrir)rrrryrzr}rrK)rCZtestsrUrVrsrrrtest_close_empty1s   *zChannelTests.test_close_emptycCspt}t|dt|d|tjt|Wdn1sL0Yt|t|ddS)Nrwr>rrrryrZChannelNotEmptyErrorr}rzrcrrr%test_close_defaults_with_unused_itemsDs  ( z2ChannelTests.test_close_defaults_with_unused_itemscCst}t|dt|d|tjtj|ddWdn1sP0Yt|t|dt|t|tj|dddS)Nrwr>TrVrrrcrrr*test_close_recv_with_unused_items_unforcedNs  ,    z7ChannelTests.test_close_recv_with_unused_items_unforcedcCst}t|dt|dtj|dd|tjt|dWdn1s\0Yt|t||tjt|Wdn1s0YdS)Nrwr>Trqrrrryr}rrKrzrcrrr*test_close_send_with_unused_items_unforced[s  *  z7ChannelTests.test_close_send_with_unused_items_unforcedcCst}t|dt|d|tj tj|dddWdn1sR0Yt|t|dt|t|tj|dddS)Nrwr>Trxrrrrcrrr*test_close_both_with_unused_items_unforcedhs  .    z7ChannelTests.test_close_both_with_unused_items_unforcedcCst}t|dt|dtj|ddd|tjt|dWdn1s^0Y|tjt|Wdn1s0YdS)Nrwr>Trgrrrcrrr(test_close_recv_with_unused_items_forcedus  *z5ChannelTests.test_close_recv_with_unused_items_forcedcCst}t|dt|dtj|ddd|tjt|dWdn1s^0Y|tjt|Wdn1s0YdS)Nrwr>Trerrrcrrr(test_close_send_with_unused_items_forceds  *z5ChannelTests.test_close_send_with_unused_items_forcedcCst}t|dt|dtj|dddd|tjt|dWdn1s`0Y|tjt|Wdn1s0YdS)Nrwr>Trfrrrcrrr(test_close_both_with_unused_items_forceds  *z5ChannelTests.test_close_both_with_unused_items_forcedcCst}t||tjt|dWdn1s@0Y|tjt|Wdn1sv0YdSr)rrr}rrKryrzrcrrrtest_close_never_useds  *z"ChannelTests.test_close_never_usedcCst}t|dt}t|td|d|tjt|Wdn1s`0Y|tjt |Wdn1s0YdS)Nrwrr) rrryr?rrrrKrzr}rCrsrrrr!test_close_by_unassociated_interps   (z.ChannelTests.test_close_by_unassociated_interpcCst}t|dt|dt|dt|tj|dd|tjt|dWdn1sr0Y|tjt|Wdn1s0YdS)NrwTrnrrrcrrr-test_close_used_multiple_times_by_single_users    *z:ChannelTests.test_close_used_multiple_times_by_single_usercCst}|tjtjdddWdn1s80Yt||tjtj|ddWdn1s|0YdS)NrTrq)rrrrrrr}rKrcrrr.test_channel_list_interpreters_invalid_channels , z;ChannelTests.test_channel_list_interpreters_invalid_channelcCs@t}|tt|Wdn1s20YdSr)rrrrrrrcrrr+test_channel_list_interpreters_invalid_argss z8ChannelTests.test_channel_list_interpreters_invalid_argsN)*rFrGrHrnrorpruryr}rrrrrrrrrrrrrrr rrrrrrrrrrrrrrrrrrrrrrrmsL  +$                 rmc@s`eZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)ChannelReleaseTestsa - main / interp / other - run in: current thread / new thread / other thread / different threads - end / opposite - force / no force - used / not used (associated / not associated) - empty / emptied / never emptied / partly emptied - closed / not closed - released / not released - creator (interp) / other - associated interpreter not running - associated interpreter destroyed cCst}t|dt|tj|ddd|tjt|dWdn1s\0Y|tjt|Wdn1s0YdSNrwTrirrrryrzrrrKrcrrrtest_single_users  *z$ChannelReleaseTests.test_single_usercCsxt}t}t}t|td|dt|td|d|d}t|td|d||ddS) Nrzrrvz,) _interpreters.channel_release(z+) print(repr(obj)) z+ _interpreters.channel_release(rzb'spam')rrr?rrrrr)rCrsrrrrrrtest_multiple_userss     z'ChannelReleaseTests.test_multiple_userscCst}t|dt|t||tjt|dWdn1sV0Y|tjt|Wdn1s0YdSrrrcrrrtest_no_kwargss   *z"ChannelReleaseTests.test_no_kwargscCsnt}t|dt|tj|ddd|tj tj|dddWdn1s`0YdS)NrwTrirrcrrrtest_multiple_times s   z'ChannelReleaseTests.test_multiple_timescCsjt}t|dt|dtj|ddd|tjt|Wdn1s\0YdS)Nrwr>Tri)rrryrrrKrzrcrrrtest_with_unused_items)s   z*ChannelReleaseTests.test_with_unused_itemscCst}t||tjt|dWdn1s@0Y|tjt|Wdn1sv0YdSr)rrrrrKryrzrcrrrtest_never_used2s  *z#ChannelReleaseTests.test_never_usedcCst}t|dt}t|td|dt|}t||tj t|dWdn1sv0Y| |ddS)Nrwrrr) rrryr?rrrzrrrKr)rCrsrrrrrtest_by_unassociated_interp;s     *z/ChannelReleaseTests.test_by_unassociated_interpc Csht}t}t|td|d|d|tjt|Wdn1sZ0YdS)Nze import _xxsubinterpreters as _interpreters obj = _interpreters.channel_send(z5, b'spam') _interpreters.channel_release(r)rrr?rrrrKrzrrrrtest_close_if_unassociatedJs  z.ChannelReleaseTests.test_close_if_unassociatedcCsRt}t|dt|t|dtj|ddt|}||ddS)NrwTrq)rrryrzrr)rCrsrrrrtest_partiallyWs    z"ChannelReleaseTests.test_partiallycCst}t|dt|dt|dt|tj|ddd|tjt|dWdn1st0Y|tjt|Wdn1s0YdSrrrcrrr'test_used_multiple_times_by_single_userbs    *z;ChannelReleaseTests.test_used_multiple_times_by_single_userN)rFrGrHrrrrrrrrrrrrrrrrs'       rcszeZdZdZfddZeddZeddZdd Zdd d Z d dZ ddZ ddZ ddZ ddZddZZS)ChannelCloseFixtureFcs|dvs J|jri}n4t|}t|}t|}|j||j||j|i}|sTd}t||||||}t|_t|_ ||_ |S)NrirS) QUICKr5r;rBr@rAr_preppedrc_state_known)r9rQrrZr[creatorZknownrCrDrrrAws"    zChannelCloseFixture.__new__cCs|jSrrrkrrrrtszChannelCloseFixture.statecCs>z|jWSty8||j}|||_|jYS0dSr)_cidAttributeError_get_interpreterr _new_channel)rCrrrrrss    zChannelCloseFixture.cidcCs||}|||Sr)r_prep_interpreterrrrrget_interpreters  z#ChannelCloseFixture.get_interpreterNcCs2|dur|j}|dkr&|jjdkr&dSt|jjS)NrVrUF)rQrtreboolr`rrrexpect_closed_errors z'ChannelCloseFixture.expect_closed_errorcCs||dSr)rrrrrprep_interpretersz$ChannelCloseFixture.prep_interpretercCs ||_dSrr)rCrPrurrr record_actionsz!ChannelCloseFixture.record_actioncCsttdSrrrkrrrclean_upszChannelCloseFixture.clean_upcCs@|jdkrtSt}t|jd|dt||_|jS)Nr4a import _xxsubinterpreters cid = _xxsubinterpreters.channel_create() # We purposefully send back an int to avoid tying the # channel to the other interpreter. _xxsubinterpreters.channel_send(zC, int(cid)) del _xxsubinterpreters )rBrrr0r.rzr)rCrZchrrrrs   z ChannelCloseFixture._new_channelcCsj|dvr|jS|dkr|jS|dkr*|jS|}z|j|}Wn$ty`t|}|j|<Yn0|SdS)N)rSrrZr[)rrZr[rKeyErrorr5)rCrrBrrrrs z$ChannelCloseFixture._get_interpretercCsF|j|jvrdS|j|j|jdkr,dSt|jd|jddS)Nr4a import _xxsubinterpreters as interpreters import test.test__xxsubinterpreters as helpers ChannelState = helpers.ChannelState try: cid except NameError: cid = interpreters._channel_id(r)r.rrrBr0rsrrrrrs  z%ChannelCloseFixture._prep_interpreter)N)rFrGrHrrApropertyrtrsrrrrrrrrrJrrrDrrps    rzend interp other extra creatorz%these tests take several hours to runc@seZdZdZddZddZddZdd Zd d Zd d ddZ ddZ ddZ d"ddZ ddZ d#ddZddZddZd d!ZdS)$ExhaustiveChannelTestsa - main / interp / other - run in: current thread / new thread / other thread / different threads - end / opposite - force / no force - used / not used (associated / not associated) - empty / emptied / never emptied / partly emptied - closed / not closed - released / not released - creator (interp) / other - associated interpreter not running - associated interpreter destroyed - close after unbound ccsgV|ddD]"}|V|D]}||Vq$q|ddD]"}|V|D]}||VqTqB|ddD]z}|V|ddD]*}||}|V|D]}||Vqq|ddD]*}||}|V|D]}||Vqqqr|ddD]}|V|ddD].}||}|V|D]}||Vq*q|ddD].}||}|V|D]}||VqfqLqdS)NrSrZr[)_iter_close_action_sets_iter_post_close_action_sets_iter_use_action_sets)rCZ closeactionsZ postactionsZ useactionsactionsrrriter_action_sets sB      z'ExhaustiveChannelTests.iter_action_setsc cs||f}tdd|gVtdd|gVtdd|tdd|gVtdd|gVtdd|gVtdd|tdd|gV|D]<}|D]2}|D](}tdd|tdd|tdd|gVqqq|D]P}|D]F}|D]<}|D]2}tdd|tdd|tdd|tdd|gVqqqqdS)NrRrVrUrM)rCrxr{interpsr|Zinterp4rrrr9s>               z,ExhaustiveChannelTests._iter_use_action_setsc csd}||f}dD]4}|rdnd}|D]}|D]}t|||gVq,q$qdD]<} dD]2} |D](} |D]} t| d| t| d| gVqbqZqRqJdS)NrxrrYrXrWrVrUr) rCrxr{ZendsrrooprrQZrecvopZsendoprVrUrrrrjs    z.ExhaustiveChannelTests._iter_close_action_setsccs.dD]$}tdd|gVtdd|gVqdS)N)rSr[rZrRrVrUrrrrrr}s   z3ExhaustiveChannelTests._iter_post_close_action_setscCs|D]}|||qdSr)rv)rCfixrrPrrr run_actionssz"ExhaustiveChannelTests.run_actionsTrpcCs||j}||j|j|j}|||jdkrZt|j |j ||j |d}| ||nt }t|jd|j dt|j dt|dt|j d|d|d|dttt |d tt |d }| ||dS) Nr4rpzB result = helpers.run_action( z, z!, hideclosed=zB, ) interpreters.channel_send(zR, result.pending.to_bytes(1, 'little')) interpreters.channel_send(z2, b'X' if result.closed else b'') r)rfre)rarQrbrrZr[rrBrvrsrPrtrrrr0r.rrcr from_bytesrzr)rCrrPrqrQrrurrrrrvsB      z!ExhaustiveChannelTests.run_actionc csBgd}|D]0\}}}dD] }dD]}t|||||Vq"qq dS)N))r4rr[)rr4r[)rxr{r[)rxr{r4)rSrZrri)r)rCrrrZr[rrQrrr iter_fixturess z$ExhaustiveChannelTests.iter_fixturescCst|rdnd}t||jd}|s4|j||ddn<|tj |j||ddWdn1sf0YdS)NrYrXrSFrp)rMrQrrvrrrK)rCrrorrXrrr_closes  zExhaustiveChannelTests._closeNcCs.|dus|jdkr|tjt|jWdn1s@0Y|tjt|jdWdn1sz0Y|tjt|jWdn1s0Y|tj tj|jddWdn1s0Yn0t|j dt|j dt|j dt|j ddS) Nr4rwTrnzz with helpers.expect_channel_closed(): interpreters.channel_recv(cid) z with helpers.expect_channel_closed(): interpreters.channel_send(cid, b'spam') z{ with helpers.expect_channel_closed(): interpreters.channel_close(cid) z with helpers.expect_channel_closed(): interpreters.channel_close(cid, force=True) ) rBrrrKrzrsryr}r0r.)rCrrrrr_assert_closed_in_interps*,*0   z/ExhaustiveChannelTests._assert_closed_in_interpcCs|||jjt|jjD]}t|jq||dD]&}| |}|j dkrTq:|||q:| d}|||dS)N)rSrZr4Zfresh) rrtrerrfrrzrsrrrB)rCrrrrrr_assert_closeds    z%ExhaustiveChannelTests._assert_closedFc csd}|D]}t|D]}|d7}|dkr:dS|rl|dddkrTtt||dt|n2|dddkrtdddtd ddtj|||fVq|r td q tdS) Nrrhrz ({} actions) rr_.z---)rprintrr=rrstdoutr()rCverboserrrrrr_iter_close_testss$    z(ExhaustiveChannelTests._iter_close_testscCsdt_|D] \}}}qdS)NT)rrrrCrrrrrr_skim_close_tests sz(ExhaustiveChannelTests._skim_close_testsc Cs|D]v\}}}|d|||@||j||||j|dd||Wdn1sl0Y|qdS)N {} {} {}Frn rrr=rrrrrrrrrr test_close s  (z!ExhaustiveChannelTests.test_closec Cs|D]v\}}}|d|||@||j||||j|dd||Wdn1sl0Y|qdS)NrTrnrrrrrtest_force_close s  (z'ExhaustiveChannelTests.test_force_close)N)F)rFrGrHrrrrrrrvrrrrrrrrrrrrrs(-1      rrR)N)N)5 collectionsrr_rrrErtextwraprr&rr testrZ test.supportr import_modulerrrr r`r,r0r-r5rLrMrcrvrrrrZTestCaserrrrrrrrrrrrarmrrrrrFr4rrrrst         & 3$  2?(;]b7Y:&q@