a 5dg9@sPddlmZddlZddlZddlZddlZddlZddlmZddl Z ddl Z ddl Z ddl m Z ddlmZe dZddZdId d Zejd d Zd dZifddZGdddeddZejddZGdddeddZGdddeddZddddZd d!Zd"d#Zd$d%ZGd&d'd'e j Z!Gd(d)d)e j Z"Gd*d+d+e j Z#Gd,d-d-e!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.GdBdCdCedCdDZ/e 0dEGdFdGdGe!Z1e2dHkrLe 3dS)J) 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)scriptrwZindentedwrappedr8/usr/local/lib/python3.9/test/test__xxsubinterpreters.py_captured_scripts   rcCsJt|\}}|&t||||WdS1s<0YdSN)r interpreters run_stringread)interpZrequestsharedr Zrpiperrr _run_output"s rc#stt\}fdd}tj|d}|dVt|d}|dWdn1s^0Y|dS)NcsttdddS)Nz7 # wait for "signal" with open(z5) as rpipe: rpipe.read() )rrrrrr rrrun,s z_running..runtargetr done)rr threadingThreadstartr writejoin)rr rtZspiperrr_running)s   (r$cKst|||dSr) _run_interp)idsourcerrrr run_interpGsr(cCsFt|}t}||kr4t|kr(tt||nt|||dSr)rrget_main get_current RuntimeErrorexecr)r&r'rZ_mainnsmainrrrr%Ks  r%cs*eZdZeddZdfdd ZZS) InterpretercCs(t||r|St|tr ||StdSr) isinstancestrNotImplementedError)clsrawrrrfrom_rawYs   zInterpreter.from_rawNcst}||kr6|sd}n|dkr0td||}nX|durp|sHd}n|dkrXtdt|tjst|}n|r||dkrd}|}nt}t|||}|S)Nr-z)name mismatch (expected "main", got "{}")rz!name mismatch (unexpected "main")) rr) ValueErrorformatr/ InterpreterIDcreatesuper__new__)r2namer&r-self __class__rrr:bs,   zInterpreter.__new__)NN)__name__ __module__ __qualname__ classmethodr4r: __classcell__rrr=rr.Ws r.zname idccs$z dVWntjyYn0dSr)rChannelClosedErrorrrrrexpect_channel_closed}s  rEcs6eZdZd fdd ZddZddZdd ZZS) ChannelActionNcs&|sd}|sd}t||||}|S)Nbothr-r9r:)r2actionendrr<r=rrr:s zChannelAction.__new__cOsb|jdkr |jdvrJt|jn*|jdvr@|jdvrJt|jn t|j|jdvr^t|jdS)Nuse)sameoppositesendrecvclose force-close)rGrLrMrNrO)r-rLotherextra)rIrJr5r)r<argskwargsrrr__init__s        zChannelAction.__init__cCs2|jdkr|S|jdkr(|dkr$dSdS|jSdS)NrLrMrNrOrJr<rJrrr resolve_ends   zChannelAction.resolve_endcCst|jdkr|S|jdkr(|dur$t|S|jdkrB|dur>t|S|jdkrp|jdkrZ|S|rl|jdkrl|StdS)NrLrSrTr-)rr+r;)r<rrSrTrrrresolve_interps      zChannelAction.resolve_interp)NN)r?r@rAr:rWrZr[rCrrr=rrFs rFzaction end interpcsBeZdZdddfdd ZddZdd Zd d d d ZZS) ChannelStaterFclosedcst|||}|SrrH)r2pendingr^r<r=rrr:szChannelState.__new__cCst||jd|jdSNr]typer_r^r<rrrincrszChannelState.incrcCst||jd|jdSr`rbrdrrrdecrszChannelState.decrTforcecCs2|jr|r|jdkr|St||r&dn|jddS)NrTr])r^r_rc)r<rhrrrrQszChannelState.close)r)r?r@rAr:rerfrQrCrrr=rr\sr\zpending closedT hideclosedcCsr|jr(|dkr"|dkr"|jr"d}q,d}nd}zt||||}Wn&tjyd|sX|sX|}Yn 0|rnd|S)NrKrOFT.)r^r_ _run_actionrrDrQ)cidrIrJstaterjZ expectfailresultrrr run_actionsrocCs|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) NrKrNspamrOzexpected ChannelEmptyErrorrQrOrNTrRrhrg) r channel_sendrer_ channel_recvChannelEmptyError Exceptionrfr5 channel_closerQ)rlrIrJrmrVrrrrks8        rkc Cs>tD]0}|dkrqzt|Wqty6Yq0qdS)Nr)rlist_alldestroyr+r&rrrclean_up_interpreterss  rzc Cs6tD](}zt|Wqtjy.Yq0qdSr)rchannel_list_allchannel_destroyChannelNotFoundErrorrlrrrclean_up_channelss  rc@seZdZddZdS)TestBasecCsttdSrrzrrdrrrtearDownszTestBase.tearDownN)r?r@rArrrrrrsrc@seZdZddZddZdS)IsShareableTestsc CsPgd}|D]>}|| |t|Wdq 1s@0Yq dS)N)Nrpspam i)subTest assertTruer is_shareable)r<Z shareablesobjrrrtest_default_shareables$s   z(IsShareableTests.test_default_shareablesc CsGddd}Gdddt}ddtdttttd||d |d g }|D]B}|t| |t |WdqJ1s0YqJdS) Nc@seZdZddZddZdS)z3IsShareableTests.test_not_shareable..CheesecSs ||_dSrr;)r<r;rrrrW5sz.Cheese.__init__cSs|jSrrrdrrr__str__7sz;IsShareableTests.test_not_shareable..Cheese.__str__N)r?r@rArWrrrrrCheese4src@s eZdZdS)z5IsShareableTests.test_not_shareable..SubBytesN)r?r@rArrrrSubBytes:srTF.gY@Z Wensleydalerp) bytesNotImplementedrcobjectrurrepr assertFalserr)r<rrZnot_shareablesrrrrtest_not_shareable3s(z#IsShareableTests.test_not_shareableN)r?r@rArrrrrrr"srcs`eZdZfddZfddZddZddZd d Zd d Zd dZ ddZ ddZ Z S)ShareableTypeTestscstt|_dSr)r9setUprchannel_createrlrdr=rrrVs zShareableTypeTests.setUpcst|jtdSr)rr|rlr9rrdr=rrrZs zShareableTypeTests.tearDownc Csr|D]h}||Jt|j|t|j}||||t|t|Wdq1sb0YqdSr)rrrrrlrs assertEqualassertIsrc)r<valuesrgotrrr_assert_values^s    z!ShareableTypeTests._assert_valuesc Cs^dD]T}||6t|j|t|j}|||Wdq1sN0YqdS)Nr)rrrrrlrsr)r<rrrrrtest_singletonsis   z"ShareableTypeTests.test_singletonscCs|dd|jgdS)Nrpi')rrlrdrrr test_typesrs zShareableTypeTests.test_typescCs|ddtddDdS)Ncss|]}|jddddVqdS)littleT)signedN)to_bytes).0irrr zsz0ShareableTypeTests.test_bytes..)rrangerdrrr test_bytesys zShareableTypeTests.test_bytescCs|gddS)N)z hello worldu 你好世界)rrdrrr test_strs}szShareableTypeTests.test_strscCs*|ttddtjtj dgdS)Nrrra)r itertoolschainrsysmaxsizerdrrrtest_intszShareableTypeTests.test_intc Cstjdtj dddg}|D]f}||H|tt|j|Wdn1s^0YWdq 1s|0Yq dS)Nrar)rrr assertRaises OverflowErrorrrrrl)r<Zintsrrrrtest_non_shareable_ints   z)ShareableTypeTests.test_non_shareable_int) r?r@rArrrrrrrrrrCrrr=rrTs    rc@s$eZdZddZddZddZdS) ListAllTestscCs"t}t}|||gdSr)rr)rwr)r<r-idsrrr test_initialszListAllTests.test_initialcCs6t}t}t}t}|||||gdSr)rr)r8rwrr<r-firstsecondrrrrtest_after_creatings z ListAllTests.test_after_creatingcCs>t}t}t}t|t}||||gdSr)rr)r8rxrwrrrrrtest_after_destroyings  z"ListAllTests.test_after_destroyingN)r?r@rArrrrrrrrsrc@seZdZddZddZdS)GetCurrentTestscCs.t}t}|||||tjdSr)rr)r*rassertIsInstancer7)r<r-currrr 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) ) rr)r8rrintstriprwrZassertNotEqual)r<r-routr_expectedrrrtest_subinterpreters   z#GetCurrentTests.test_subinterpreterN)r?r@rArrrrrrrsrc@seZdZddZddZdS) GetMainTestscCs0t\}t}|||||tjdSr)rrwr)rrr7)r<rr-rrrtest_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) )rrwr8rrrrr)r<rrrr-rrrtest_from_subinterpreters   z%GetMainTests.test_from_subinterpreterN)r?r@rArrrrrrrsrc@s<eZdZddZddZddZddZd d Zd d Zd S)IsRunningTestscCst}|t|dSr)rr)r is_running)r<r-rrrrszIsRunningTests.test_maincCsdt}|t|t| |t|Wdn1sF0Y|t|dSr)rr8rrr$rr<rrrrrs  .z"IsRunningTests.test_subinterpretercCs2t}t|td|d}||ddS)Nz` import _xxsubinterpreters as _interpreters if _interpreters.is_running(zZ): print(True) else: print(False) True)rr8rrrr)r<rrrrrrs  z'IsRunningTests.test_from_subinterpretercCsJt}t||tt|Wdn1s<0YdSr)rr8rxrr+rrrrrtest_already_destroyeds  z%IsRunningTests.test_already_destroyedcCs8|ttdWdn1s*0YdSNi@B)rr+rrrdrrrtest_does_not_exists z"IsRunningTests.test_does_not_existcCs8|ttdWdn1s*0YdSNr)rr5rrrdrrr test_bad_ids zIsRunningTests.test_bad_idN) r?r@rArrrrrrrrrrrs  rc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)InterpreterIDTestscCs"tjddd}|t|ddS)NrTrg)rr7rrr<r&rrr test_with_intsz InterpreterIDTests.test_with_intcCs4Gdddt}tj|dd}|t|ddS)Nc@seZdZddZdS)z.InterpreterIDTests.test_coerce_id..IntcSsdSNrrrdrrr __index__sz8InterpreterIDTests.test_coerce_id..Int.__index__Nr?r@rArrrrrIntsrTrgr)r0rr7rr)r<rr&rrrtest_coerce_idsz!InterpreterIDTests.test_coerce_idcCsf|ttjt|ttjd|ttjd|ttjd|ttjd|ttjddSNg$@10s10rl)r TypeErrorrr7rr5rrdrrrr s zInterpreterIDTests.test_bad_idcCsHt}|t"tt|dWdn1s:0YdSNra)rrrr+r7rrrrrrs z&InterpreterIDTests.test_does_not_existcCs"tjddd}|t|ddSNrTrgr)rr7rr0rrrrtest_strszInterpreterIDTests.test_strcCs"tjddd}|t|ddS)NrTrgzInterpreterID(10))rr7rrrrrr test_reprszInterpreterIDTests.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)rr8r7rrfloatrr0r<id1id2id3rrr test_equality"s$z InterpreterIDTests.test_equalityN) r?r@rArrrrrrrrrrrrsrc@sNeZdZddZedddZddZdd Zd d Z d d Z ddZ dS) CreateTestscCs*t}||tj||tdSr)rr8rr7assertInrwrrrr test_in_main;szCreateTests.test_in_mainz*enable this test when working on pystate.ccCsDt}tdD] }t}t|||q|t|ddS)Nd)setrrr8rxaddrlen)r<seenrr&rrrtest_unique_idAs    zCreateTests.test_unique_idcsntdfdd}tj|d}|Wdn1sH0Y||tdS)NcstdSr)rr8acquirereleaserr&lockrrfNsz%CreateTests.test_in_thread..fr)rLockrr r"rrrwr<rr#rrrtest_in_threadKs &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) ) rrwr8rrrrrr)r<r-rrrrrrtest_in_subinterpreterZs   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)rrrrrrksz6CreateTests.test_in_threaded_subinterpreter..fr) rrwr8rrr r"rr)r<r-rr#rrrtest_in_threaded_subinterpretergs  z+CreateTests.test_in_threaded_subinterpretercCsjtt}g}tdD]}t}||q|D]}t|q4t}|tt||hBdSN)rrrwrr8appendrxrr<beforerrr&rrrtest_after_destroy_allzs    z"CreateTests.test_after_destroy_allcCs`tt}t}t}t}t|t|t}|tt|||hBdSr)rrrwr8rxr)r<rrrrr&rrrtest_after_destroy_somes   z#CreateTests.test_after_destroy_someN) r?r@rArunittestskiprrrrrrrrrrr9s  rc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) DestroyTestscCsft}t}t}||tt|||t||t||tdSr)rr8rrwrx assertNotInrrrrtest_ones zDestroyTests.test_onecCsvtt}t}tdD]}t}||q|tt||B|D]}t|qN|tt|dSr)rrrwrr8rrrxrrrrtest_alls    zDestroyTests.test_allcslt\ttWdn1s40Yfdd}tj|d}||dS)Ncs8ttWdn1s*0YdSrrr+rrxrr-r<rrrs z!DestroyTests.test_main..fr) rrwrr+rxrrr r"rrr rrs  ( zDestroyTests.test_maincCsJt}t||tt|Wdn1s<0YdSr)rr8rxrr+rrrrrs  z#DestroyTests.test_already_destroyedcCs8|ttdWdn1s*0YdSrr rdrrrrs z DestroyTests.test_does_not_existcCs8|ttdWdn1s*0YdSr)rr5rrxrdrrrrs zDestroyTests.test_bad_idcCsJt\}t}td|d}t|||tt||hdS)Nzo import _xxsubinterpreters as _interpreters try: _interpreters.destroy(zD) except RuntimeError: pass rrwr8rrrr)r<r-r&r rrrtest_from_currents   zDestroyTests.test_from_currentcCsRt\}t}t}td|d}t|||tt||hdS)NzZ import _xxsubinterpreters as _interpreters _interpreters.destroy() r)r<r-rrr rrrtest_from_siblings   zDestroyTests.test_from_siblingcs4tfdd}tj|d}||dS)NcstdSr)rrxrryrrrsz.DestroyTests.test_from_other_thread..fr)rr8rrr r"rrryrtest_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.) rrwr8r$rrrr+rx)r<r-rrrrtest_still_runnings     (zDestroyTests.test_still_runningN) r?r@rAr r rrrrrrrrrrrrrs     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)r9rrr8r&_fsrdr=rrrs  zRunStringTests.setUpcs"|jdur|jtdSr)rrQr9rrdr=rrrs  zRunStringTests.tearDowncCsVtd\}}|&t|j||}Wdn1s<0Y||ddS)Nprint("it worked!", end="") it worked!)rrrr&rr)r<r filerrrr test_success s  &zRunStringTests.test_successcsrtd\}|Bfdd}tj|d}|||}Wdn1sX0Y|ddS)NrcstjdSr)rrr&rr r<rrrsz(RunStringTests.test_in_thread..frr)rrrr r"rr)r<rrr#rrrrrs  &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)rr8rrrr)r<Z subinterpr rrrrrtest_create_threads    &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!flushrr;rrr&seekrr)r<rrrr Zcontentrrr test_fork0s    zRunStringTests.test_forkc Csft|jH|tt|jdWdn1s:0YWdn1sX0YdS)N print("spam"))r$r&rr+rrrdrrrtest_already_runningFs  z#RunStringTests.test_already_runningcCsTd}|tvr|d7}q|tt|dWdn1sF0YdS)Nrrar$)rrwrr+rrrrrrKs    z"RunStringTests.test_does_not_existcCs:|ttddWdn1s,0YdS)Nrr$)rr5rrrdrrr test_error_idRs zRunStringTests.test_error_idcCs:|ttddWdn1s,0YdS)Nrr$)rrrrrdrrrrVs zRunStringTests.test_bad_idcCs<|tt|jdWdn1s.0YdSrrrrrr&rdrrrtest_bad_scriptZs zRunStringTests.test_bad_scriptcCs<|tt|jdWdn1s.0YdS)Ns print("spam")r'rdrrrtest_bytes_for_script^s z$RunStringTests.test_bytes_for_scriptNccsz|tj}dVWdn1s(0Y|dur\|t|jddt|n|t|jd||dS)N:rz{}: {})rrRunFailedErrorrr0 exceptionsplitr6)r<exctyperZcaughtrrrassert_run_failedbs$  z RunStringTests.assert_run_failedcCs<|tt|jdWdn1s.0YdS)Nz print("spam")r/ SyntaxErrorrrr&rdrrrtest_invalid_syntaxms z"RunStringTests.test_invalid_syntaxcCs>|tdt|jdWdn1s00YdS)Nrzraise Exception("spam"))r/rurrr&rdrrr test_failurerszRunStringTests.test_failurecCs>|tdt|jdWdn1s00YdS)N42zraise SystemExit(42))r/ SystemExitrrr&rdrrrtest_SystemExitvszRunStringTests.test_SystemExitcCs~|t"t|jtdWdn1s20Y|td"t|jtdWdn1sp0YdS)NzG import sys sys.exit() r3zI import sys sys.exit(42) )r/r4rrr&rrdrrr test_sys_exitzs 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*r8rrn)r9) rrrrrr&r pickleloadrZ assertIsNoner<r 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__'] rr7zL ns2 = dict(vars()) del ns2['__builtins__'] zr ns = dict(vars()) del ns['__builtins__'] import pickle with open(r:r;ns1r8ns2) rrr&rrrr r>r?r)r<rr r r rArBrrrtest_shared_overwritess   (z%RunStringTests.test_shared_overwritescCs|t\}}ddi}td|d}t|j||t|d}t|}Wdn1s^0Y| |dddS)Nr?s not __main__z spam = 42 ns = dict(vars()) del ns['__builtins__'] import pickle with open(r:r;) rrrrrr&r r>r?rr@rrr#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 r;z eggs = False ns = dict(vars()) del ns['__builtins__'] import pickle with open(r:rr8) rrrrr&rr r>r?rr )r<r r rArDrErrrtest_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(r:r; __builtins__ __loader____main__r<)r?__annotations____doc__ __package____spec__r) rrrrrr&r r>r?popr)r<r r r rArBrrr 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)r<r dirnamefilenameprocretcoderrrtest_still_running_at_exits   Dz)RunStringTests.test_still_running_at_exit)N)%r?r@rArZSCRIPTZFILENAMErrrrrrZ skipUnlesshasattrrr#r%rr&rr(r) contextlibcontextmanagerr/r1r2r5r6rCrFrGrHrQrrWrCrrr=rrs8      !rc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS)ChannelIDTestscCs0tjddd}|t|d||jddS)NrTrgrG)r _channel_idrrrJr<rlrrrtest_default_kwargs/sz"ChannelIDTests.test_default_kwargscCstjdddd}||jdtjddddd}||jdtjdddd}||jdtjddddd }||jdtjddddd}||jd dS) NrTrNrhrNFrNrOrhrOrhrO)rOrNrhrG)rr\rrJr]rrrtest_with_kwargs5szChannelIDTests.test_with_kwargscCs4Gdddt}tj|dd}|t|ddS)Nc@seZdZddZdS)z*ChannelIDTests.test_coerce_id..IntcSsdSrrrdrrrrGsz4ChannelIDTests.test_coerce_id..Int.__index__NrrrrrrFsrTrgr)r0rr\rr)r<rrlrrrrEszChannelIDTests.test_coerce_idcCsf|ttjt|ttjd|ttjd|ttjd|ttjd|ttjddSr)rrrr\rr5rrdrrrrMs zChannelIDTests.test_bad_idcCs>|t tjddddWdn1s00YdS)NrFrNrO)rr5rr\rdrrrtest_bad_kwargsUs zChannelIDTests.test_bad_kwargscCsJt}|tj"tt|dWdn1s<0YdSr)rrrr}r\rr]rrrrYsz"ChannelIDTests.test_does_not_existcCs"tjddd}|t|ddSr)rr\rr0r]rrrr^szChannelIDTests.test_strcCstjddd}|t|dtjdddd}|t|dtjdddd}|t|dtjddddd }|t|ddS) NrTrgz ChannelID(10)r_zChannelID(10, send=True)razChannelID(10, recv=True)r`)rr\rrr]rrrrbszChannelIDTests.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)rrr\rrrrr0)r<cid1cid2Zcid3rrrros$zChannelIDTests.test_equalityN) r?r@rAr^rbrrrdrrrrrrrrr[-s r[c@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 ChannelIDr]rrrtest_create_cidszChannelTests.test_create_cidcCsrt}t}t}t}t}||t|d||t|d|t|t||||hdSr)rr{rrrr)r<rrrrZafterrrrtest_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) ra)rr8rrrrr)r<rrrerrfrrrtest_ids_globals  zChannelTests.test_ids_globalcCs@t}tj|dd}tj|dd}||g||gdS)NTrNF)rrchannel_list_interpretersr)r<rl 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)NrNTrkFe import _xxsubinterpreters as _interpreters obj = _interpreters.channel_recv(r) rr)rrrrlrr8rr)r<interp0rlrmrninterp1rrr$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 |||hdS) NrN_ import _xxsubinterpreters as _interpreters _interpreters.channel_send(z, "send") rprTrkF) rr)r8rrrrrrlrr)r<rqrrinterp2interp3rlrmrnrrr'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)NrNrprTrkF) rr)r8rrrrrrlrrxr<rqrrrlrmrnrrr(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 |||gdS) NdatarprTrkFrarrb import _xxsubinterpreters as _interpreters _interpreters.channel_release() rr)r8rrrrrrlrrchannel_release)r<rqrrrurlrmrnrrr'test_channel_list_interpreters_releaseds8     z4ChannelTests.test_channel_list_interpreters_releasedcCst}t}t}t|dtj|dd}tj|dd}|t|d|t|dtj|dd| tj tj|ddWdn1s0Y| tj tj|ddWdn1s0YdS)NrNTrkFrarrg) rr)r8rrrrlrrrvrrDrxrrr%test_channel_list_interpreters_closed%s ,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) NrNTrkFrar` import _xxsubinterpreters as _interpreters _interpreters.channel_close(, force=True) ) rr)r8rrrrlrrrvrrDrrrxrrr.test_channel_list_interpreters_closed_send_end;s( , .z;ChannelTests.test_channel_list_interpreters_closed_send_endcCs>t}d}t||t|}||||||dSNrp)rrrrrsrZ assertIsNot)r<rlZorigrrrrtest_send_recv_main_s    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 )rr8rr)r<rrrrrtest_send_recv_same_interpreterhsz,ChannelTests.test_send_recv_same_interpretercCs@t}t}t|td|d}t|}||ddS)Nrt, b'spam') rp)rrr8rrrsr)r<rlrrrrrr%test_send_recv_different_interpretersts  z2ChannelTests.test_send_recv_different_interpreterscsVtfdd}tj|d}|td|t}||ddS)NcsDzt}Wq4Wqtjy0tdYq0qt|dS)Nr)rrsrttimesleeprr)rr~rrrs  z8ChannelTests.test_send_recv_different_threads..frrp) rrrrr rrr"rsrr<rr#rrr~r 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') )rrrrlrrrrrs zIChannelTests.test_send_recv_different_interpreters_and_threads..frrpeggs) rrr8rrr rrr"rsrrrrr1test_send_recv_different_interpreters_and_threadss   z>ChannelTests.test_send_recv_different_interpreters_and_threadscCs<|tjtddWdn1s.0YdS)Nrrp)rrr}rrrdrrrtest_send_not_foundsz ChannelTests.test_send_not_foundcCs:|tjtdWdn1s,0YdSr)rrr}rsrdrrrtest_recv_not_foundsz ChannelTests.test_recv_not_foundcCsBt}|tjt|Wdn1s40YdSr)rrrrtrsr]rrrtest_recv_emptyszChannelTests.test_recv_emptyc Cst}t}t||}t|dt|dt|dt|dt||}t||}t|}t||}t||}|||||d||d||d||d|||dS)Nrarpr)rrrrsrrrr) r<defaultrlZobj1obj2Zobj3Zobj4Zobj5Zobj6rrrtest_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') r~rprN) rrr8rrdictrNrsrrr<rlrrrrrrtest_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') )rArprN) rrr\r8rrrrNrsrrrrrrtest_run_string_arg_resolveds    z)ChannelTests.test_run_string_arg_resolvedcCst}t|dt|t||tjt|dWdn1sV0Y|tjt|Wdn1s0YdSNrprrrrrrsrvrrDr]rrrtest_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)Nrtrz_ import _xxsubinterpreters as _interpreters _interpreters.channel_recv(rz, _interpreters.channel_send(z, b'spam') rD) rrr8rrrvrr+rr0r,)r<rlrrcmrrrtest_close_multiple_userss(      * *z&ChannelTests.test_close_multiple_userscCsbt}t|dt|t||tjt|Wdn1sT0YdSrrr]rrrtest_close_multiple_timess    z&ChannelTests.test_close_multiple_timesc Csgd}|D]\}}|||ft}t|dt|tj|||d|tjt|dWdn1s0Y|tjt|Wdn1s0YWdq 1s0Yq dS)N))FFTF)FT)TTrprcr)rrrrrrsrvrrD)r<ZtestsrNrOrlrrrtest_close_empty"s   *zChannelTests.test_close_emptycCspt}t|dt|d|tjt|Wdn1sL0Yt|t|ddS)Nrpr7rrrrrrZChannelNotEmptyErrorrvrsr]rrr%test_close_defaults_with_unused_items5s  ( z2ChannelTests.test_close_defaults_with_unused_itemscCst}t|dt|d|tjtj|ddWdn1sP0Yt|t|dt|t|tj|dddS)Nrpr7TrOrrr]rrr*test_close_recv_with_unused_items_unforced?s  ,    z7ChannelTests.test_close_recv_with_unused_items_unforcedcCst}t|dt|dtj|dd|tjt|dWdn1s\0Yt|t||tjt|Wdn1s0YdS)Nrpr7TrkrrrrrrvrrDrsr]rrr*test_close_send_with_unused_items_unforcedLs  *  z7ChannelTests.test_close_send_with_unused_items_unforcedcCst}t|dt|d|tj tj|dddWdn1sR0Yt|t|dt|t|tj|dddS)Nrpr7Trqrrrr]rrr*test_close_both_with_unused_items_unforcedYs  .    z7ChannelTests.test_close_both_with_unused_items_unforcedcCst}t|dt|dtj|ddd|tjt|dWdn1s^0Y|tjt|Wdn1s0YdS)Nrpr7Trarrr]rrr(test_close_recv_with_unused_items_forcedfs  *z5ChannelTests.test_close_recv_with_unused_items_forcedcCst}t|dt|dtj|ddd|tjt|dWdn1s^0Y|tjt|Wdn1s0YdS)Nrpr7Tr_rrr]rrr(test_close_send_with_unused_items_forcedqs  *z5ChannelTests.test_close_send_with_unused_items_forcedcCst}t|dt|dtj|dddd|tjt|dWdn1s`0Y|tjt|Wdn1s0YdS)Nrpr7Tr`rrr]rrr(test_close_both_with_unused_items_forced|s  *z5ChannelTests.test_close_both_with_unused_items_forcedcCst}t||tjt|dWdn1s@0Y|tjt|Wdn1sv0YdSr)rrrvrrDrrrsr]rrrtest_close_never_useds  *z"ChannelTests.test_close_never_usedcCst}t|dt}t|td|d|tjt|Wdn1s`0Y|tjt |Wdn1s0YdS)Nrprr) rrrrr8rrrrDrsrvr<rlrrrr!test_close_by_unassociated_interps   (z.ChannelTests.test_close_by_unassociated_interpcCst}t|dt|dt|dt|tj|dd|tjt|dWdn1sr0Y|tjt|Wdn1s0YdS)NrpTrgrrr]rrr-test_close_used_multiple_times_by_single_users    *z:ChannelTests.test_close_used_multiple_times_by_single_usercCst}|tjtjdddWdn1s80Yt||tjtj|ddWdn1s|0YdS)NrTrk)rrrr}rlrvrDr]rrr.test_channel_list_interpreters_invalid_channels , z;ChannelTests.test_channel_list_interpreters_invalid_channelcCs@t}|tt|Wdn1s20YdSr)rrrrrlr]rrr+test_channel_list_interpreters_invalid_argss z8ChannelTests.test_channel_list_interpreters_invalid_argsN)*r?r@rArhrirjrorsrwryr}r~rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrgsL  +$                 rgc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)ChannelReleaseTestscCst}t|dt|tj|ddd|tjt|dWdn1s\0Y|tjt|Wdn1s0YdSNrpTrcrrrrrrsr|rrDr]rrrtest_single_users  *z$ChannelReleaseTests.test_single_usercCsxt}t}t}t|td|dt|td|d|d}t|td|d||ddS) Nrtrrpz,) _interpreters.channel_release(z+) print(repr(obj)) z+ _interpreters.channel_release(rzb'spam')rrr8rrrrr)r<rlrrrrrrtest_multiple_userss     z'ChannelReleaseTests.test_multiple_userscCst}t|dt|t||tjt|dWdn1sV0Y|tjt|Wdn1s0YdSrrr]rrrtest_no_kwargss   *z"ChannelReleaseTests.test_no_kwargscCsnt}t|dt|tj|ddd|tj tj|dddWdn1s`0YdS)NrpTrcrr]rrrtest_multiple_timess   z'ChannelReleaseTests.test_multiple_timescCsjt}t|dt|dtj|ddd|tjt|Wdn1s\0YdS)Nrpr7Trc)rrrrr|rrDrsr]rrrtest_with_unused_itemss   z*ChannelReleaseTests.test_with_unused_itemscCst}t||tjt|dWdn1s@0Y|tjt|Wdn1sv0YdSr)rrr|rrDrrrsr]rrrtest_never_used#s  *z#ChannelReleaseTests.test_never_usedcCst}t|dt}t|td|dt|}t||tj t|dWdn1sv0Y| |ddS)Nrpr{rr) rrrrr8rrrsr|rrDr)r<rlrrrrrtest_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)rrr8rrrrDrsrrrrtest_close_if_unassociated;s  z.ChannelReleaseTests.test_close_if_unassociatedcCsRt}t|dt|t|dtj|ddt|}||ddS)NrpTrk)rrrrrsr|r)r<rlrrrrtest_partiallyHs    z"ChannelReleaseTests.test_partiallycCst}t|dt|dt|dt|tj|ddd|tjt|dWdn1st0Y|tjt|Wdn1s0YdSrrr]rrr'test_used_multiple_times_by_single_userSs    *z;ChannelReleaseTests.test_used_multiple_times_by_single_userN) r?r@rArrrrrrrrrrrrrrrs+       rcszeZdZdZfddZeddZeddZdd Zdd d Z d dZ ddZ ddZ ddZ ddZddZZS)ChannelCloseFixtureFcsx|jr i}n4t|}t|}t|}|j||j||j|i}|sHd}t||||||}t|_t|_ ||_ |S)NrL) QUICKr.r4r;r9r:r_preppedr\_state_known)r2rJrrSrTcreatorZknownr<r=rrr:hs    zChannelCloseFixture.__new__cCs|jSrrrdrrrrm}szChannelCloseFixture.statecCs>z|jWSty8||j}|||_|jYS0dSr)_cidAttributeError_get_interpreterr _new_channel)r<rrrrrls    zChannelCloseFixture.cidcCs||}|||Sr)r_prep_interpreterrrrrget_interpreters  z#ChannelCloseFixture.get_interpreterNcCs2|dur|j}|dkr&|jjdkr&dSt|jjS)NrOrNF)rJrmr^boolrYrrrexpect_closed_errors z'ChannelCloseFixture.expect_closed_errorcCs||dSr)rrrrrprep_interpretersz$ChannelCloseFixture.prep_interpretercCs ||_dSrr)r<rIrnrrr record_actionsz!ChannelCloseFixture.record_actioncCsttdSrrrdrrrclean_upszChannelCloseFixture.clean_upcCs@|jdkrtSt}t|jd|dt||_|jS)Nr-a 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 )r;rrr(r&rsr)r<rZchrrrrs   z ChannelCloseFixture._new_channelcCsj|dvr|jS|dkr|jS|dkr*|jS|}z|j|}Wn$ty`t|}|j|<Yn0|SdS)N)rLrrSrT)rrSrTrKeyErrorr.)r<rr;rrrrs z$ChannelCloseFixture._get_interpretercCsF|j|jvrdS|j|j|jdkr,dSt|jd|jddS)Nr-a import _xxsubinterpreters as interpreters import test.test__xxsubinterpreters as helpers ChannelState = helpers.ChannelState try: cid except NameError: cid = interpreters._channel_id(r)r&rrr;r(rlrrrrrs  z%ChannelCloseFixture._prep_interpreter)N)r?r@rArr:propertyrmrlrrrrrrrrrCrrr=rras    rzend interp other extra creatorz%these tests take several hours to runc@seZdZddZddZddZddZd d Zd d d dZddZ ddZ d!ddZ ddZ d"ddZ ddZddZdd ZdS)#ExhaustiveChannelTestsccsgV|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)NrLrSrT)_iter_close_action_sets_iter_post_close_action_sets_iter_use_action_sets)r<Z closeactionsZ postactionsZ useactionsactionsrrriter_action_setssB      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)NrKrOrNrF)r<rrruinterpsrvZinterp4rrrr*s>               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)NrqrrRrQrPrOrNr) r<rrruZendsrrhoprrJZrecvopZsendoprOrNrrrr[s    z.ExhaustiveChannelTests._iter_close_action_setsccs.dD]$}tdd|gVtdd|gVqdS)N)rLrTrSrKrOrNrrrrrrns   z3ExhaustiveChannelTests._iter_post_close_action_setscCs|D]}|||qdSr)ro)r<fixrrIrrr run_actionswsz"ExhaustiveChannelTests.run_actionsTricCs||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) Nr-rizB 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)r_r^)rZrJr[rrSrTrr;rorlrIrmrrrr(r&rr\r from_bytesrsr)r<rrIrjrJrrnrrrrro{sB     z!ExhaustiveChannelTests.run_actionc csBgd}|D]0\}}}dD] }dD]}t|||||Vq"qq dS)N))r-rrT)rr-rT)rrrurT)rrrur-)rLrSrrc)r)r<rrrSrTrrJrrr iter_fixturess z$ExhaustiveChannelTests.iter_fixturescCst|rdnd}t||jd}|s4|j||ddn<|tj |j||ddWdn1sf0YdS)NrRrQrLFri)rFrJrrorrrD)r<rrhrrQrrr_closes  zExhaustiveChannelTests._closeNcCs.|dus|jdkr|tjt|jWdn1s@0Y|tjt|jdWdn1sz0Y|tjt|jWdn1s0Y|tj tj|jddWdn1s0Yn0t|j dt|j dt|j dt|j ddS) Nr-rpTrgzz 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) ) r;rrrDrsrlrrrvr(r&)r<rrrrr_assert_closed_in_interps*,*0   z/ExhaustiveChannelTests._assert_closed_in_interpcCs|||jjt|jjD]}t|jq||dD]&}| |}|j dkrTq:|||q:| d}|||dS)N)rLrSr-Zfresh) rrmr^rr_rrsrlrrr;)r<rrrrrr_assert_closeds    z%ExhaustiveChannelTests._assert_closedFc csd}|D]}t|D]}|d7}|dkr:dS|rl|dddkrTtt||dt|n2|dddkrtdddtd ddtj|||fVq|r td q tdS) Nrrarz ({} actions) rrX.z---)rprintrr6rrstdoutr!)r<verboserrrrrr_iter_close_testss$    z(ExhaustiveChannelTests._iter_close_testscCsdt_|D] \}}}qdS)NT)rrrr<rrrrrr_skim_close_testssz(ExhaustiveChannelTests._skim_close_testsc Cs|D]v\}}}|d|||@||j||||j|dd||Wdn1sl0Y|qdS)N {} {} {}Frg rrr6rrrrrrrrrr test_closes  (z!ExhaustiveChannelTests.test_closec Cs|D]v\}}}|d|||@||j||||j|dd||Wdn1sl0Y|qdS)NrTrgrrrrrtest_force_close s  (z'ExhaustiveChannelTests.test_force_close)N)F)r?r@rArrrrrrorrrrrrrrrrrrrs+-1      rrK)N)4 collectionsrrYrrr>rtextwraprrrrtestrZ test.supportr import_modulerrrrZr$r(r%r.rErFr\rorkrzrZTestCaserrrrrrrrrrrr[rgrrrrr?r-rrrrsr        & 3$  2?(;]b7Y:&q@