tQc@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZdZdZe je jeeddZdZdejfd YZiZe j d Ze j d Zd efd YZd efdYZdZdefdYZdejefdYZdefdYZ 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 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+tj|}t|tjs't|S(N(tmarshaltloadst isinstancettypestCodeTypetAssertionError(tmstco((s!/sys/lib/python2.7/idlelib/rpc.pyt unpickle_code-scCs4t|tjsttj|}t|ffS(N(RRRRRtdumpsR(RR((s!/sys/lib/python2.7/idlelib/rpc.pyt pickle_code2siis 127.0.0.1t RPCServercBs8eZddZdZdZdZdZRS(cCs/|dkrt}ntjj|||dS(N(tNonet RPCHandlert SocketServert TCPServert__init__(tselftaddrt handlerclass((s!/sys/lib/python2.7/idlelib/rpc.pyRGs  cCsdS(s@Override TCPServer method, no bind() phase for connecting entityN((R((s!/sys/lib/python2.7/idlelib/rpc.pyt server_bindLscCs|jj|jdS(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!/sys/lib/python2.7/idlelib/rpc.pytserver_activatePscCs|j|jfS(s:Override TCPServer method, return already connected socket(RR(R((s!/sys/lib/python2.7/idlelib/rpc.pyt get_requestYscCsyWntk rntj}|dddIJ|dIJ|dtjjIJ|dI|IJ|dIt|IJtjd||d IJ|ddIJt j 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!/sys/lib/python2.7/idlelib/rpc.pyt handle_error]s    N(t__name__t __module__R RRRRR*(((s!/sys/lib/python2.7/idlelib/rpc.pyR Es    itSocketIOcBseZdZdddZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!RS(icCsdtj|_|dk r'||_n||_|dkrEt}n||_i|_i|_ dS(N( RR t sockthreadR t debuggingtsockt objecttabletobjtablet responsestcvars(RR0R2R/((s!/sys/lib/python2.7/idlelib/rpc.pyRs       cCs/|j}d|_|dk r+|jndS(N(R0R tclose(RR0((s!/sys/lib/python2.7/idlelib/rpc.pyR5s   cCstjddS(s!override for specific exit actioniN(R%R&(R((s!/sys/lib/python2.7/idlelib/rpc.pytexithookscGsf|js dS|jdttjj}x"|D]}|dt|}q7Wtj|IJdS(Nt (R/tlocationtstrRR R!RR(Rtargststa((s!/sys/lib/python2.7/idlelib/rpc.pytdebugs  # cCs||j|((s!/sys/lib/python2.7/idlelib/rpc.pyt unregisters cCs|jd|y|\}\}}}}Wntk r@dSX||jkradd|ffS|j|}|dkri} t|| d| fS|dkri} t|| d| fSt||sdd|ffSt||} y|d kr5| ||} t| tr+t | } nd| fS|d krat j || ||ffdSdd |fSWndt k rnQtjk rn;d } tj| || |fIJtjdtjdSXdS(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 TypeErrorR2t _getmethodst_getattributesthasattrtgetattrRt RemoteObjectt remotereft request_queuetputR RRterrorRRR#R$(RtseqR'thowR>t methodnameR:tkwargstobjtmethodst attributestmethodtrettmsg((s!/sys/lib/python2.7/idlelib/rpc.pyt localcallsL            cCs8|jd|||j||||}|j|S(Nsremotecall:asynccall: (R=t asynccallt asyncreturn(RR>RWR:RXRU((s!/sys/lib/python2.7/idlelib/rpc.pyt remotecallscCs8|jd|||j||||}|j|S(Nsremotequeue:asyncqueue: (R=t asyncqueueRa(RR>RWR:RXRU((s!/sys/lib/python2.7/idlelib/rpc.pyt remotequeuescCsd||||ff}|j}tj|jkrUtj}||j|RWR:RXR'RUtcvar((s!/sys/lib/python2.7/idlelib/rpc.pyR`s  cCsd||||ff}|j}tj|jkrUtj}||j|RWR:RXR'RURh((s!/sys/lib/python2.7/idlelib/rpc.pyRcs  cCsG|jd||j|dd}|jd|||j|S(Ns#asyncreturn:%d:call getresponse(): twaitg?sasyncreturn:%d:response: (R=t getresponsetdecoderesponse(RRUtresponse((s!/sys/lib/python2.7/idlelib/rpc.pyRascCs|\}}|dkr|S|dkr,dS|dkrI|jddS|dkrp|jd|jdS|dkr|jd|t|nt||fdS( NRERIRJsdecoderesponse: EXCEPTIONtEOFsdecoderesponse: EOFRCsdecoderesponse: Internal ERROR:(R R=tdecode_interrupthookt RuntimeErrort SystemError(RRlRVtwhat((s!/sys/lib/python2.7/idlelib/rpc.pyRks           cCs tdS(tN(tEOFError(R((s!/sys/lib/python2.7/idlelib/rpc.pyRnscCs@y|jddddWntk r;|jddSXdS(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. tmyseqRig?smainloop:returnN(RjR RsR=(R((s!/sys/lib/python2.7/idlelib/rpc.pytmainloop s   cCsU|j||}|dk rQ|\}}|dkrQ||j|f}qQn|S(NRE(t _getresponseR t_proxify(RRtRiRlRVRq((s!/sys/lib/python2.7/idlelib/rpc.pyRjs    cCsEt|trt||jSt|tjrAt|j|S|S(N(Rt RemoteProxytRPCProxyR>RtListTypetmapRw(RRY((s!/sys/lib/python2.7/idlelib/rpc.pyRws cCs|jd|tj|jkrPx|j||}|dk r(|Sq(n}|j|}|jx||jkr|j qjW|j|}|jd||f|j|=|j|=|j |SdS(Ns_getresponse:myseq:s-_getresponse:%s: thread woke up: response: %s( R=RR R.t pollresponseR R4tacquireR3Ritrelease(RRtRiRlRh((s!/sys/lib/python2.7/idlelib/rpc.pyRv's"         cCs|jd|_}|S(Ni(tnextseq(RRU((s!/sys/lib/python2.7/idlelib/rpc.pyRe=scCs|jd|dytj|}Wn0tjk rZtjdIt|IJnXtjdt ||}xt |dkry>t j g|j gg\}}}|j j |t }Wn6ttfk rtdqztjk rqzX||}qzWdS(Nsputmessage:%d:isCannot pickle:s}|j|}|jd|j|<|j|jq!W|jdS(s+action taken upon link being closed by peerRRmN(sEOFN( tEOFhookR=R4R}R R3RR~R6(RtkeyR((s!/sys/lib/python2.7/idlelib/rpc.pyRs      cCsdS(sBClasses using rpc client/server can override to augment EOF actionN((R((s!/sys/lib/python2.7/idlelib/rpc.pyRsN("R+R,RR RR5R6R=R@RBR_RbRdR`RcRaRkRnRuRjRwRvReRgRRRRRRRR|RR(((s!/sys/lib/python2.7/idlelib/rpc.pyR-|s>      *               I RPcBseZRS((R+R,(((s!/sys/lib/python2.7/idlelib/rpc.pyRPscCs t|}|t|((s!/sys/lib/python2.7/idlelib/rpc.pyRQs  RxcBseZdZRS(cCs ||_dS(N(R>(RR>((s!/sys/lib/python2.7/idlelib/rpc.pyRs(R+R,R(((s!/sys/lib/python2.7/idlelib/rpc.pyRxsR cBs/eZeZdZdZdZdZRS(s#ScCs6||_tj||tjj||||dS(N(tcurrent_handlerR-RRtBaseRequestHandler(RR0Rtsvr((s!/sys/lib/python2.7/idlelib/rpc.pyRs cCs|jdS(s(handle() method required by SocketServerN(Ru(R((s!/sys/lib/python2.7/idlelib/rpc.pythandlescCs t||S(N(Ry(RR>((s!/sys/lib/python2.7/idlelib/rpc.pytget_remote_proxys(R+R,tFalseR/R8RRR(((s!/sys/lib/python2.7/idlelib/rpc.pyR s   t RPCClientcBsAeZeZdZdZejejdZ dZ dZ RS(s#CicCs9tj|||_|jj||jjddS(Ni(Rtlistening_socktbindtlisten(Rtaddresstfamilyttype((s!/sys/lib/python2.7/idlelib/rpc.pyRscCsw|jj\}}|jr4tjdI|IJn|dtkrWtj||ntjdI|IJtj dS(Ns****** Connection request from is** Invalid host: ( RtacceptR/RRt LOCALHOSTR-RRRT(Rt working_sockR((s!/sys/lib/python2.7/idlelib/rpc.pyR s cCs t||S(N(Ry(RR>((s!/sys/lib/python2.7/idlelib/rpc.pyRs( R+R,RR/R8RRtAF_INETt SOCK_STREAMRRR(((s!/sys/lib/python2.7/idlelib/rpc.pyRs  RycBs8eZdZdZdZdZdZdZRS(cCs||_||_dS(N(tsockioR>(RRR>((s!/sys/lib/python2.7/idlelib/rpc.pyRs cCs|jdkr|jn|jj|rDt|j|j|S|jdkr`|jn||jkr|jj |jd|fi}|St |dS(Nt__getattribute__( t_RPCProxy__methodsR t_RPCProxy__getmethodsRt MethodProxyRR>t_RPCProxy__attributest_RPCProxy__getattributesRbR(Rtnametvalue((s!/sys/lib/python2.7/idlelib/rpc.pyt __getattr__"s  cCs%|jj|jddi|_dS(NRF((RRbR>R(R((s!/sys/lib/python2.7/idlelib/rpc.pyt__getattributes0scCs%|jj|jddi|_dS(NRD((RRbR>R(R((s!/sys/lib/python2.7/idlelib/rpc.pyt __getmethods4sN( R+R,R RRRRRR(((s!/sys/lib/python2.7/idlelib/rpc.pyRys    cCsx?t|D]1}t||}t|dr d||R(RRR>R((s!/sys/lib/python2.7/idlelib/rpc.pyRMs  cOs%|jj|j|j||}|S(N(RRbR>R(RR:RXR((s!/sys/lib/python2.7/idlelib/rpc.pyRRs!(R+R,RR(((s!/sys/lib/python2.7/idlelib/rpc.pyRKs (%t__doc__RR%RRRRtcPickleRRRR#tcopy_regRRRR RRRRR R1RRRR?R-RPRQRxRR RRyRLRMR(((s!/sys/lib/python2.7/idlelib/rpc.pytsB                2f