Lc@sdZddkZddkZddkZddkZddkZddkZddkZddk Z ddk Z ddk Z ddk Z ddk Z ddkZdZdZe ie ieeddZdZdeifd YZhZe i d Ze i d Zd efd YZd efdYZdZdefdYZdeiefdYZdefdYZ defdYZ!dZ"dZ#defdYZ$dS(sRPC Implemention, 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 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. iNcCs-ti|}t|tipt|S(N(tmarshaltloadst isinstancettypestCodeTypetAssertionError(tmstco((s#/usr/lib64/python2.6/idlelib/rpc.pyt unpickle_code-scCs6t|tiptti|}t|ffS(N(RRRRRtdumpsR(RR((s#/usr/lib64/python2.6/idlelib/rpc.pyt pickle_code2siis 127.0.0.1t RPCServercBs8eZddZdZdZdZdZRS(cCs1|djo t}ntii|||dS(N(tNonet RPCHandlert SocketServert TCPServert__init__(tselftaddrt handlerclass((s#/usr/lib64/python2.6/idlelib/rpc.pyRGs  cCsdS(s@Override TCPServer method, no bind() phase for connecting entityN((R((s#/usr/lib64/python2.6/idlelib/rpc.pyt server_bindLscCs|ii|idS(sOverride 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(tsockettconnecttserver_address(R((s#/usr/lib64/python2.6/idlelib/rpc.pytserver_activatePscCs|i|ifS(s:Override TCPServer method, return already connected socket(RR(R((s#/usr/lib64/python2.6/idlelib/rpc.pyt get_requestYscCsyWntj o nti}|dddIJ|dIJ|dtiiIJ|dI|IJ|dIt|IJtid||d IJ|ddIJt i d nXd S( sOverride 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. s t-i(sUnhandled server exception!s Thread: %ssClient Address: s Request: tfiles# *** Unrecoverable, server exiting!iN( t SystemExittsyst __stderr__t threadingt currentThreadtgetNametreprt tracebackt print_exctost_exit(Rtrequesttclient_addressterf((s#/usr/lib64/python2.6/idlelib/rpc.pyt handle_error]s   N(t__name__t __module__R RRRRR*(((s#/usr/lib64/python2.6/idlelib/rpc.pyR Es    itSocketIOcBseZdZdddZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!RS(icCshti|_|dj o ||_n||_|djo t}n||_h|_h|_ dS(N( RR t sockthreadR t debuggingtsockt objecttabletobjtablet responsestcvars(RR0R2R/((s#/usr/lib64/python2.6/idlelib/rpc.pyRs       cCs1|i}d|_|dj o|indS(N(R0R tclose(RR0((s#/usr/lib64/python2.6/idlelib/rpc.pyR5s   cCstidS(s!override for specific exit actionN(R%R&(R((s#/usr/lib64/python2.6/idlelib/rpc.pytexithookscGsh|ipdS|idttii}x"|D]}|dt|}q9Wti|IJdS(Nt (R/tlocationtstrRR R!RR(Rtargststa((s#/usr/lib64/python2.6/idlelib/rpc.pytdebugs #cCs||i|((s#/usr/lib64/python2.6/idlelib/rpc.pyt unregistersc Cs|id|y|\}\}}}}Wntj odSX|ii|pdd|ffS|i|}|djoh} t|| d| fS|djoh} t|| d| fSt||pdd|ffSt||} y|d jo:| ||} t| t ot | } nd| fS|d jo!t i || ||ffdSdd |fSWnhtj o nTtij o n<d } ti| || |fIJtidtidSXdS(Ns localcall:tERRORsBad request formatsUnknown object id: %rt __methods__tOKt__attributes__sUnsupported method name: %rtCALLtQUEUEtQUEUEDsUnsupported message type: %ssU*** Internal Error: rpc.py:SocketIO.localcall() Object: %s Method: %s Args: %s Rt EXCEPTION(RCsBad request format(RIN(RJN(R=t TypeErrorR2thas_keyt _getmethodst_getattributesthasattrtgetattrRt RemoteObjectt remotereft request_queuetputR RRterrorRRR#R$(RtseqR'thowR>t methodnameR:tkwargstobjtmethodst attributestmethodtrettmsg((s#/usr/lib64/python2.6/idlelib/rpc.pyt localcallsL          cCs8|id|||i||||}|i|S(Nsremotecall:asynccall: (R=t asynccallt asyncreturn(RR>RXR:RYRV((s#/usr/lib64/python2.6/idlelib/rpc.pyt remotecallscCs8|id|||i||||}|i|S(Nsremotequeue:asyncqueue: (R=t asyncqueueRb(RR>RXR:RYRV((s#/usr/lib64/python2.6/idlelib/rpc.pyt remotequeuescCsd||||ff}|i}ti|ijoti}||i|RXR:RYR'RVtcvar((s#/usr/lib64/python2.6/idlelib/rpc.pyRas  cCsd||||ff}|i}ti|ijoti}||i|RXR:RYR'RVRi((s#/usr/lib64/python2.6/idlelib/rpc.pyRds  cCsG|id||i|dd}|id|||i|S(Ns#asyncreturn:%d:call getresponse(): twaitg?sasyncreturn:%d:response: (R=t getresponsetdecoderesponse(RRVtresponse((s#/usr/lib64/python2.6/idlelib/rpc.pyRbscCs|\}}|djo|S|djodS|djo|iddS|djo|id|idS|djo|id|t|nt||fdS( NRERIRJsdecoderesponse: EXCEPTIONtEOFsdecoderesponse: EOFRCsdecoderesponse: Internal ERROR:(R R=tdecode_interrupthookt RuntimeErrort SystemError(RRmRWtwhat((s#/usr/lib64/python2.6/idlelib/rpc.pyRls           cCs tdS(tN(tEOFError(R((s#/usr/lib64/python2.6/idlelib/rpc.pyRoscCsBy|iddddWn!tj o|iddSXdS(sListen on socket until I/O not ready or EOF pollresponse() will loop looking for seq number None, which never comes, and exit on EOFError. tmyseqRjg?smainloop:returnN(RkR RtR=(R((s#/usr/lib64/python2.6/idlelib/rpc.pytmainloop s  cCsY|i||}|dj o6|\}}|djo||i|f}qUn|S(NRE(t _getresponseR t_proxify(RRuRjRmRWRr((s#/usr/lib64/python2.6/idlelib/rpc.pyRks    cCsIt|tot||iSt|tiot|i|S|S(N(Rt RemoteProxytRPCProxyR>RtListTypetmapRx(RRZ((s#/usr/lib64/python2.6/idlelib/rpc.pyRxs cCs|id|ti|ijo.x|i||}|dj o|Sq)n|i|}|ix"|ii |p|i qnW|i|}|id||f|i|=|i|=|i |SdS(Ns_getresponse:myseq:s-_getresponse:%s: thread woke up: response: %s( R=RR R.t pollresponseR R4tacquireR3RLRjtrelease(RRuRjRmRi((s#/usr/lib64/python2.6/idlelib/rpc.pyRw's$         cCs|id|_}|S(Ni(tnextseq(RRV((s#/usr/lib64/python2.6/idlelib/rpc.pyRf=sc Cs|id|dyti|}Wn2tij o#tidIt|IJnXtidt ||}xt |djoy>t i g|i gg\}}}|i i |t }Wn:ttfj otdq|tij o q|X||}q|WdS(Nsputmessage:%d:isCannot pickle:s}|i|}|id|i|<|i|iq!W|idS(s+action taken upon link being closed by peerRRnN(sEOFN( tEOFhookR=R4R~R R3RRR6(RtkeyR((s#/usr/lib64/python2.6/idlelib/rpc.pyRs       cCsdS(sBClasses using rpc client/server can override to augment EOF actionN((R((s#/usr/lib64/python2.6/idlelib/rpc.pyRsN("R+R,RR RR5R6R=R@RBR`RcReRaRdRbRlRoRvRkRxRwRfRhRRRRRRRR}RR(((s#/usr/lib64/python2.6/idlelib/rpc.pyR-|s>      *               I RQcBseZRS((R+R,(((s#/usr/lib64/python2.6/idlelib/rpc.pyRQscCs t|}|t|((s#/usr/lib64/python2.6/idlelib/rpc.pyRRs  RycBseZdZRS(cCs ||_dS(N(R>(RR>((s#/usr/lib64/python2.6/idlelib/rpc.pyRs(R+R,R(((s#/usr/lib64/python2.6/idlelib/rpc.pyRysR cBs/eZeZdZdZdZdZRS(s#ScCs6||_ti||tii||||dS(N(tcurrent_handlerR-RRtBaseRequestHandler(RR0Rtsvr((s#/usr/lib64/python2.6/idlelib/rpc.pyRs cCs|idS(s(handle() method required by SocketServerN(Rv(R((s#/usr/lib64/python2.6/idlelib/rpc.pythandlescCs t||S(N(Rz(RR>((s#/usr/lib64/python2.6/idlelib/rpc.pytget_remote_proxys(R+R,tFalseR/R8RRR(((s#/usr/lib64/python2.6/idlelib/rpc.pyR s   t RPCClientcBsAeZeZdZdZeieidZ dZ dZ RS(s#CicCs9ti|||_|ii||iiddS(Ni(Rtlistening_socktbindtlisten(Rtaddresstfamilyttype((s#/usr/lib64/python2.6/idlelib/rpc.pyRscCs{|ii\}}|iotidI|IJn|dtjoti||ntidI|IJti dS(Ns****** Connection request from is** Invalid host: ( RtacceptR/RRt LOCALHOSTR-RRRU(Rt working_sockR((s#/usr/lib64/python2.6/idlelib/rpc.pyR s cCs t||S(N(Rz(RR>((s#/usr/lib64/python2.6/idlelib/rpc.pyRs( R+R,RR/R8RRtAF_INETt SOCK_STREAMRRR(((s#/usr/lib64/python2.6/idlelib/rpc.pyRs  RzcBs8eZdZdZdZdZdZdZRS(cCs||_||_dS(N(tsockioR>(RRR>((s#/usr/lib64/python2.6/idlelib/rpc.pyRs cCs|idjo|in|ii|ot|i|i|S|idjo|in|ii |o&|ii |id|fh}|St |dS(Nt__getattribute__( t_RPCProxy__methodsR t_RPCProxy__getmethodsRt MethodProxyRR>t_RPCProxy__attributest_RPCProxy__getattributesRLRcR(Rtnametvalue((s#/usr/lib64/python2.6/idlelib/rpc.pyt __getattr__"scCs%|ii|iddh|_dS(NRF((RRcR>R(R((s#/usr/lib64/python2.6/idlelib/rpc.pyt__getattributes0scCs%|ii|iddh|_dS(NRD((RRcR>R(R((s#/usr/lib64/python2.6/idlelib/rpc.pyt __getmethods4sN( R+R,R RRRRRR(((s#/usr/lib64/python2.6/idlelib/rpc.pyRzs    cCsx>t|D]0}t||}t|od||t|D]0}t||}t|pd||R(RRR>R((s#/usr/lib64/python2.6/idlelib/rpc.pyRMs  cOs%|ii|i|i||}|S(N(RRcR>R(RR:RYR((s#/usr/lib64/python2.6/idlelib/rpc.pyt__call__Rs!(R+R,RR(((s#/usr/lib64/python2.6/idlelib/rpc.pyRKs (%t__doc__RR%RRRRtcPickleRRRR#tcopy_regRRRR RRRRR R1RSRR?R-RQRRRyRR RRzRMRNR(((s#/usr/lib64/python2.6/idlelib/rpc.pytsB                2f