.$SrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK r SSK r SSK r SSK r SSK r SSKrSSKrSrSrS#Sjr"SS\R&5rSrS r"S S \ R.5r0r\R4"S5r\R4"S5r"S S 5r"SS5rSr"SS5r "SS\ RB\5r""SS\5r#"SS5r$Sr%Sr&"SS5r'Sr(\)S:XaSSK*J+r+ \+"S S!S"9 gg)$aRPC Implementation, originally written for the Python Idle IDE For security reasons, GvR requested that Idle's Python execution server process connect to the Idle process, which listens for the connection. Since Idle has only one client per server, this was not a limitation. +---------------------------------+ +-------------+ | socketserver.BaseRequestHandler | | SocketIO | +---------------------------------+ +-------------+ ^ | register() | | | unregister()| | +-------------+ | ^ ^ | | | | + -------------------+ | | | | +-------------------------+ +-----------------+ | RPCHandler | | RPCClient | | [attribute of RPCServer]| | | +-------------------------+ +-----------------+ The RPCServer handler class is expected to provide register/unregister methods. RPCHandler inherits the mix-in class SocketIO, which provides these methods. See the Idle run.main() docstring for further information on how this was accomplished in Idle. Nc2[R"U5nU$)z*Return code object from marshal string ms.)marshalloads)mscos 2/opt/alt/python313/lib64/python3.13/idlelib/rpc.py unpickle_coder -s r B Ic@[R"U5n[U44$)zBReturn unpickle function and tuple with marshalled co code object.)rdumpsr )rrs r pickle_coder 3s r B 2% r c[R"5n[X!5nURU5 UR 5$)z.Return pickled (or marshalled) string for obj.)ioBytesIO CodePicklerdumpgetvalue)objprotocolfps rr r 9s0 AA AFF3K ::<r cH\rSrSr\R \0\RErSr g)rBN) __name__ __module__ __qualname____firstlineno__typesCodeTyper copyregdispatch_table__static_attributes__rr rrrBsnnkLW5K5KLNr ri z 127.0.0.1c6\rSrSrS SjrSrSrSrSrSr g) RPCServerINcVUc[n[RRXU5 gN) RPCHandler socketserver TCPServer__init__)selfaddr handlerclasss rr,RPCServer.__init__Ks#  %L''LAr cg)z@Override TCPServer method, no bind() phase for connecting entityNrr-s r server_bindRPCServer.server_bindP r cNURRUR5 g)zOverride TCPServer method, connect() instead of listen() Due to the reversed connection, self.server_address is actually the address of the Idle Client to which we are connecting. N)socketconnectserver_addressr2s rserver_activateRPCServer.server_activateTs D//0r c2URUR4$)z:Override TCPServer method, return already connected socket)r7r9r2s r get_requestRPCServer.get_request]s{{D////r c|e![a e [Rn[SUS9 [SUS9 [S[R "5R -US9 [SX#S9 [S[U5US9 [R"US9 [SUS9 [SUS9 [R"S 5 g =f) zOverride TCPServer method Error message goes to __stderr__. No error message if exiting normally or socket raised EOF. Other exceptions not handled in server code will cause os._exit. z) ----------------------------------------filezUnhandled server exception!z Thread: %szClient Address: z Request: z# *** Unrecoverable, server exiting!z(----------------------------------------rN) SystemExitsys __stderr__print threadingcurrent_threadnamerepr traceback print_excos_exit)r-requestclient_addresserfs r handle_errorRPCServer.handle_erroras     ..C -c * /c : ,!9!9!;!@!@@s K $n ? +tG}3 7   S ) 8s C &s # HHQKs B6B;rr() rrrrr,r3r:r=rQr#rr rr%r%IsB  10r r%c\rSrSrSrS"SjrSrSrSrSr S r S r S r S r S rSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSr Sr!S r"S!r#g)#SocketIOrNc[R"5UlUbX0lXlUc[ nX l0Ul0Ulgr() rFrG sockthread debuggingsock objecttableobjtable responsescvars)r-rYr[rXs rr,SocketIO.__init__s@#224  &N  "H  r cRURnSUlUbUR5 ggr()rYclose)r-rYs rr`SocketIO.closes&yy   JJL r c0[R"S5 g)z!override for specific exit actionrN)rLrMr2s rexithookSocketIO.exithooks   r cUR(dgURS-[[R"5R 5-nUHnUS-[U5-nM [ U[RS9 g)N r@) rXlocationstrrFrGrHrErCrD)r-argssas rdebugSocketIO.debugs]~~  MMC #i&>&>&@&E&E"F FAC#a& A acnn%r c X RU'gr()r[)r-oidobject_s rregisterSocketIO.registers$ cr c@URU g![a gf=fr()r[KeyErrorr-ros r unregisterSocketIO.unregisters%  c"   s  cURSU5 UunupEpgX@R;aSSU<34$URUnUS:Xa0n [X5 SU 4$US:Xa0n [ X5 SU 4$[ X5(dSSU<34$[ X5n US :Xa,U "U0UD6n [U [5(a [U 5n SU 4$US :Xa[RXXg445 g SS U-4$![a gf=f![a e[a e[a e[an S U 4sSn A $Sn A f Sn[!XX4-["R$S9 [&R("["R$S9 g=f)Nz localcall:)ERRORzBad request formatryzUnknown object id: __methods__OK__attributes__zUnsupported method name: CALLQUEUE)QUEUEDNzUnsupported message type: %sCALLEXCzU*** Internal Error: rpc.py:SocketIO.localcall() Object: %s Method: %s Args: %s r@) EXCEPTIONN)rl TypeErrorr[ _getmethods_getattributeshasattrgetattr isinstance RemoteObject remoteref request_queueputrBKeyboardInterruptOSError ExceptionrErCrDrJrK)r-seqrNhowro methodnamerikwargsrmethods attributesmethodretexmsgs r localcallSocketIO.localcalls <) 33: 0C0#4 mm #23':; ;mmC   &G  %'? " ) )J 3 +*% %s''8GH H) 'f}d-f-c<00#C.Cc{"!!3(>"?@&!?#!EFF5 32 36        #r? " '?##Cd#3 /#5A""8,,r c8Uup#US:XaU$US:XagUS:XaURS5 gUS:Xa"URS5 UR5 gUS:XaURSU5 [U5eUS :XaURS U5 Ue[X#5e) Nr{rrzdecoderesponse: EXCEPTIONEOFzdecoderesponse: EOFryzdecoderesponse: Internal ERROR:rzdecoderesponse: Call Exception:)rldecode_interrupthook RuntimeError SystemError)r-rrwhats rrSocketIO.decoderesponses  $;K (? +  JJ2 3 %< JJ, -  % % ' '> JJ8$ ?t$ $ )  JJ8$ ?J#$$r c[e))EOFErrorr2s rrSocketIO.decode_interrupthooksr chURSSS9 g![a URS5 gf=f)zListen on socket until I/O not ready or EOF pollresponse() will loop looking for seq number None, which never comes, and exit on EOFError. Nr)myseqrzmainloop:return)rrrlr2s rmainloopSocketIO.mainloops9    4d  3  JJ( )  s 11cfURX5nUbUupEUS:XaX@RU54nU$)Nr{) _getresponse_proxify)r-rrrrrs rrSocketIO.getresponse"s<$$U1   ICd{ d 33r c[U[5(a[XR5$[U[5(a[ [ UR U55$U$r()r RemoteProxyRPCProxyrolistmapr)r-rs rrSocketIO._proxify*sF c; ' 'D''* * c4 DMM3/0 0 r cURSU5 [R"5URLaUR X5nUbU$MUR UnUR 5 XR;a!UR5 XR;aM!URUnURSU<SU<35 URU UR U UR5 U$)Nz_getresponse:myseq:z _getresponse:z: thread woke up: response: ) rlrFrGrW pollresponser]acquirer\rrelease)r-rrrrs rrSocketIO._getresponse2s (%0  # # % 8,,U9'#O ::e$D LLN~~- ~~-~~e,H JJx) *u% 5! LLNOr c2URS-=UlnU$)N)nextseq)r-rs rrSocketIO.newseqHs!\\A-- s r c URSUS-5 [U5n[R"S[U55U-n[U5S:a_[R"/UR//5up4nURRUS[5nX&Sn[U5S:aM^gg![Ra$ [ S[ U5[ RS9 ef=f![[ 4a [#S5ef=f)Nzputmessage:%d:rzCannot pickle:r@DLL (mmTYY%5%5%7$8"b$GGA!1v{ IINN7+1v{ IINI LLN||~   s :C##C4cURS:Xad[UR5S:aJURSSnURSSUl[R"SU5SUlSUlggg)Nrrr)bufstaterrrunpackr)r-rjs rrSocketIO._stage0psc ==A #dii.A"5 "1 A !" DI!==q1!4DLDM #6 r cURS:Xak[UR5UR:aGURSURnURURSUlSUlSUlU$gg)Nrrr)rrrr)r-packets rrSocketIO._stage1wsg ==A #dii.DLL"@YY} -F $,,-0DIDLDMM #A r cxURU5nUcg[R"U5nU$![Rav [ S[ R S9 [ S[U5[ R S9 [R"[ R S9 [ S[ R S9 ef=f)Nz-----------------------r@zcannot unpickle packet:) rrrUnpicklingErrorrErCrDrIrJ print_stack)r-rrrs r pollmessageSocketIO.pollmessages& > ll6*G %%  +#.. A +T&\ O  ! !s~~ 6 +#.. A   s /B B9c[RS5nUupEUSU44nURU5 UR U5nUcgUupGUSnURSXA4-5 US;a\URSU-5 URXG5nURSXE4-5 US:XaURXE45 OUS :XaMXA:XaU$URRUS5n U b>U R5 XpRU'U R5 U R5 GM0![Ra GNf=f![ a UR5 g[a gf=f) aHandle messages received on the socket. Some messages received may be asynchronous 'call' or 'queue' requests, and some may be responses for other threads. 'call' requests are passed to self.localcall() with the expectation of immediate execution, during which time the socket is not serviced. 'queue' requests are used for tasks (which may block or hang) to be processed in a different thread. These requests are fed into request_queue by self.localcall(). Responses to queued requests are taken from response_queue and sent across the link with the associated sequence numbers. Messages in the queues are (sequence_number, request/response) tuples and code using this module removing messages from the request_queue is responsible for returning the correct sequence number in the response_queue. pollresponse() will loop until a response message with the myseq sequence number is received, and will save other responses in self.responses and notify the owning thread. rr{Nzpollresponse:%d:myseq:%s)r}r~zpollresponse:%d:localcall:call:z%pollresponse:%d:localcall:response:%sr}r~)response_queuegetrqueueEmptyrr handle_EOFrrlrr]rr\notifyr) r-rrqmsgrrrresqrcvs rrSocketIO.pollresponses~. )%))!,!% x 01( **40?# ICq'C JJ1SL@ A'' >#4 B!_-.&=OOSO4G^ ZZ^^C.>JJL*.NN3'IIKJJLY;;   !!  s(D1E 1E E  E4( E43E4c(UR5 URS5 URHQnURUnUR5 SURU'UR 5 UR 5 MS UR5 g)z+action taken upon link being closed by peerr)rNN)EOFhookrlr]rr\rrrc)r-keyr s rrSocketIO.handle_EOFsf  < ::CCB JJL"/DNN3  IIK JJL  r cg)zBClasses using rpc client/server can override to augment EOF actionNrr2s rr SocketIO.EOFhookr5r ) rrrr]rXrr[r\rYrW)NN)$rrrrrr,r`rcrlrqrvrrrrrrrrrrrrrrrrrrrrrrrr r#rr rrTrTsG  &% ,'\% % - %* , DGH  GR  r rTc\rSrSrSrg)rirN)rrrrr#rr rrrsr rc@[U5nU[U'[U5$r()idrZr)rros rrrs S'CK s r c\rSrSrSrSrg)ricXlgr(rorus rr,RemoteProxy.__init__sr rN)rrrrr,r#rr rrrsr rc.\rSrSrSrSrSrSrSrSr g) r)iFz#SczXl[RX5 [RRXX#5 gr()current_handlerrTr,r*BaseRequestHandler)r-rYr.svrs rr,RPCHandler.__init__s,"$%''00TGr c$UR5 g)z(handle() method required by socketserverN)rr2s rhandleRPCHandler.handles  r c[X5$r(rrus rget_remote_proxyRPCHandler.get_remote_proxy ""r rN) rrrrrXrgr,rr#r#rr rr)r)sIHH #r r)cb\rSrSrSrSrSr\R\R4Sjr Sr Sr Sr g ) RPCClienti Fz#Crc[R"X#5UlURRU5 URRS5 gNr)r7listening_sockbindlisten)r-addressfamilytypes rr,RPCClient.__init__s;$mmF9   ) ""1%r cURR5upUR(a[SU[R S9 US[ :Xa[RX5 g[SU[R S9 [e)Nz****** Connection request from r@rz** Invalid host: ) r*acceptrXrErCrD LOCALHOSTrTr,r)r- working_sockr-s rr2RPCClient.acceptsa $ 3 3 : : < >> 3W3>> R 1: "   d 1 %wS^^ DMr c[X5$r(r"rus rr#RPCClient.get_remote_proxy"r%r )r*N)rrrrrXrgrr7AF_INET SOCK_STREAMr,r2r#r#rr rr'r' s/IHG'-~~F> !     >>  d # #t{{DHHd; ;    $  " $$ $KK**4885G,07B8EL & &r c^URRURSS05Ulg)Nr|r)r<rrorCr2s r__getattributesRPCProxy.__getattributes=s' KK224880@"bJr c^URRURSS05Ulg)Nrzr)r<rror@r2s r __getmethodsRPCProxy.__getmethodsAs&//0=r2Gr ) __attributes __methodsror<) rrrrr@rCr,rFrDrAr#rr rrr&s"IL 'JGr rc[U5H$n[X5n[U5(dM SX'M& [U[5(aUR Hn[ XA5 M ggr))dirrcallablerr/ __bases__r)rrrHattrsupers rrrEsVCs! D>>GM#t]]E  '#r cj[U5H$n[X5n[U5(aM SX'M& gr))rQrrR)rrrHrTs rrrPs+Cs!~~ J r c \rSrSrSrSrSrg)rBiWc(XlX lX0lgr()r<rorH)r-r<rorHs rr,MethodProxy.__init__Ys  r chURRURURX5nU$r()r<rrorH)r-rirrEs r__call__MethodProxy.__call__^s' &&txxDI r )rHror<N)rrrrr,r[r#rr rrBrBWs  r rBcvUcgS[l[U5n[RR U5 [RR S5 U[lg![ aH SnURUS5nURUS5n[RR U5 N|f=f)z9Override standard display hook to use non-locale encodingNasciibackslashreplacestrict ) builtins_rIrCstdoutwriteUnicodeEncodeErrorencodedecode)rEtextencodingbytess r displayhookrlfs }HJ ;D JJTHJ  H&89||Hh/  sA&&AB87B8__main__)mainzidlelib.idle_test.test_rpcr) verbosityr(),__doc__rbr!rrrLrrrr7r*rrCrFrJrr r r Picklerrrr3r+r%rZQueuerrrTrrrrr)r'rrrrBrlrunittestrnrr rrts"8      M&..M   . &&.d  A Qe e R     #00(#$##4GG> (!  & z %4r