��Qc@s�dZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlZddlZddl
Z
ddlZd�Zd�Zeje
jee�ddZdZdejfd ��YZiZe
j
d
�Ze
j
d
�Zdefd��YZd
efd��YZd�Zdefd��YZdejefd��YZdefd��YZ defd��YZ!d�Z"d�Z#defd��YZ$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.
i�NcCstj|�}|S(N(tmarshaltloads(tmstco((s!/sys/lib/python2.7/idlelib/rpc.pyt
unpickle_code-scCstj|�}t|ffS(N(RtdumpsR(RR((s!/sys/lib/python2.7/idlelib/rpc.pytpickle_code2siis 127.0.0.1t RPCServercBs8eZdd�Zd�Zd�Zd�Zd�ZRS(cCs/|dkrt}ntjj|||�dS(N(tNonet
RPCHandlertSocketServert TCPServert__init__(tselftaddrthandlerclass((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.pytserver_bindLscCs|jj|j�dS(s�Override 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.pytget_requestYscCs�y�Wn�tk
r�n�tj}|dddIJ|dIJ|dtj�j�IJ|dI|IJ|dIt|�IJtjd|�|d IJ|ddIJt j
d
�nXdS(s�Override 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(R
trequesttclient_addressterf((s!/sys/lib/python2.7/idlelib/rpc.pythandle_error]s
N(t__name__t
__module__RRRRRR&(((s!/sys/lib/python2.7/idlelib/rpc.pyREs
itSocketIOcBseZdZddd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d�Zd �Zd
�Z
d�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdZdZdZd�Zd�Zd�Zd�Zd�Zd�Z d�Z!RS(icCsdtj�|_|dk r'||_n||_|dkrEt}n||_i|_i|_ dS(N(
RRt
sockthreadRt debuggingtsocktobjecttabletobjtablet responsestcvars(R
R,R.R+((s!/sys/lib/python2.7/idlelib/rpc.pyR�s cCs/|j}d|_|dk r+|j�ndS(N(R,Rtclose(R
R,((s!/sys/lib/python2.7/idlelib/rpc.pyR1�s cCstjd�dS(s!override for specific exit actioniN(R!R"(R
((s!/sys/lib/python2.7/idlelib/rpc.pytexithook�scGsf|js
dS|jdttj�j��}x"|D]}|dt|�}q7Wtj|IJdS(Nt (R+tlocationtstrRRRRR(R
targststa((s!/sys/lib/python2.7/idlelib/rpc.pytdebug�s #
cCs||j|<dS(N(R.(R
toidtobject((s!/sys/lib/python2.7/idlelib/rpc.pytregister�scCs&y|j|=Wntk
r!nXdS(N(R.tKeyError(R
R:((s!/sys/lib/python2.7/idlelib/rpc.pyt
unregister�s
cCs�|jd|�y|\}\}}}}Wntk
r@dSX||jkradd|ffS|j|}|dkr�i} t|| �d| fS|dkr�i}
t||
�d|
fSt||�s�d|ffSt||�}y�|d kr5|||�}t|t�r+t |�}nd|fS|d
krat
j||||ff�dSdd|fSWndt
k
r��nQtjk
r��n;d
}
tj|
|||fIJtjdtj�dSXdS(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(R?sBad request format(REN(RFN(R9t TypeErrorR.t_getmethodst_getattributesthasattrtgetattrt
isinstancetRemoteObjectt remotereft
request_queuetputRRRterrorRRRR (R
tseqR#thowR:t
methodnameR6tkwargstobjtmethodst
attributestmethodtrettmsg((s!/sys/lib/python2.7/idlelib/rpc.pyt localcall�sL
cCs8|jd||�|j||||�}|j|�S(Nsremotecall:asynccall: (R9t asynccalltasyncreturn(R
R:RTR6RURR((s!/sys/lib/python2.7/idlelib/rpc.pyt
remotecall�scCs8|jd||�|j||||�}|j|�S(Nsremotequeue:asyncqueue: (R9t
asyncqueueR^(R
R:RTR6RURR((s!/sys/lib/python2.7/idlelib/rpc.pytremotequeue�scCs�d||||ff}|j�}tj�|jkrUtj�}||j|<n|jd|||||�|j||f�|S(NRCs
asynccall:%d:(tnewseqRRR*t ConditionR0R9t
putmessage(R
R:RTR6RUR#RRtcvar((s!/sys/lib/python2.7/idlelib/rpc.pyR]�scCs�d||||ff}|j�}tj�|jkrUtj�}||j|<n|jd|||||�|j||f�|S(NRDsasyncqueue:%d:(RbRRR*RcR0R9Rd(R
R:RTR6RUR#RRRe((s!/sys/lib/python2.7/idlelib/rpc.pyR`�scCsG|jd|�|j|dd�}|jd||�|j|�S(Ns#asyncreturn:%d:call getresponse(): twaitg����sasyncreturn:%d:response: (R9tgetresponsetdecoderesponse(R
RRtresponse((s!/sys/lib/python2.7/idlelib/rpc.pyR^�scCs�|\}}|dkr|S|dkr,dS|dkrI|jd�dS|dkrp|jd�|j�dS|dkr�|jd|�t|�nt||f�dS( NRARERFsdecoderesponse: EXCEPTIONtEOFsdecoderesponse: EOFR?sdecoderesponse: Internal ERROR:(RR9tdecode_interrupthooktRuntimeErrortSystemError(R
RiRStwhat((s!/sys/lib/python2.7/idlelib/rpc.pyRh�s
cCs
t�dS(tN(tEOFError(R
((s!/sys/lib/python2.7/idlelib/rpc.pyRkscCs@y|jdddd�Wntk
r;|jd�dSXdS(s�Listen on socket until I/O not ready or EOF
pollresponse() will loop looking for seq number None, which
never comes, and exit on EOFError.
tmyseqRfg����smainloop:returnN(RgRRpR9(R
((s!/sys/lib/python2.7/idlelib/rpc.pytmainloop
s
cCsU|j||�}|dk rQ|\}}|dkrQ||j|�f}qQn|S(NRA(t_getresponseRt_proxify(R
RqRfRiRSRn((s!/sys/lib/python2.7/idlelib/rpc.pyRgscCsEt|t�rt||j�St|tj�rAt|j|�S|S(N(RLtRemoteProxytRPCProxyR:ttypestListTypetmapRt(R
RV((s!/sys/lib/python2.7/idlelib/rpc.pyRts
cCs�|jd|�tj�|jkrPx�|j||�}|dk r(|Sq(n}|j|}|j�x||jkr�|j �qjW|j|}|jd||f�|j|=|j|=|j
�|SdS(Ns_getresponse:myseq:s-_getresponse:%s: thread woke up: response: %s(R9RRR*tpollresponseRR0tacquireR/Rftrelease(R
RqRfRiRe((s!/sys/lib/python2.7/idlelib/rpc.pyRs's"
cCs|jd|_}|S(Ni(tnextseq(R
RR((s!/sys/lib/python2.7/idlelib/rpc.pyRb=scCs|jd|d�ytj|�}Wn0tjk
rZtjdIt|�IJ�nXtjdt |��|}x�t |�dkry>t
j
g|jgg�\}}}|jj|t
�}Wn6ttfk
r�td�qztjk
r�qzX||}qzWdS(Nsputmessage:%d:isCannot pickle:s<issocket no longer exists(R9tpickleRt
PicklingErrorRRRtstructtpacktlentselectR,tsendtBUFSIZEtAttributeErrorRGtIOErrorRRQ(R
tmessageR7trtwtxtn((s!/sys/lib/python2.7/idlelib/rpc.pyRdAs $RoicCs�|j�t|j�|jkr�tj|jj�ggg|�\}}}t|�dkredSy|jjt �}Wnt
jk
r�t�nXt|�dkr�t�n|j|7_|j�n|j
�S(Ni(t_stage0R�tbuffertbufneedR�R,tfilenoRtrecvR�RRQRpt_stage1(R
RfR�R�R�R7((s!/sys/lib/python2.7/idlelib/rpc.pyt
pollpacketXs
-
cCsj|jdkrft|j�dkrf|jd }|jd|_tjd|�d|_d|_ndS(Niis<ii(tbufstateR�R�R�tunpackR�(R
R7((s!/sys/lib/python2.7/idlelib/rpc.pyR�hs
$
cCsd|jdkr`t|j�|jkr`|j|j }|j|j|_d|_d|_|SdS(Niii(R�R�R�R�(R
tpacket((s!/sys/lib/python2.7/idlelib/rpc.pyR�os' cCs�|j|�}|dkrdSytj|�}Wn]tjk
r�tjdIJtjdIt|�IJtj dtj�tjdIJ�nX|S(Ns-----------------------scannot unpickle packet:R(
R�RR~RtUnpicklingErrorRRRRtprint_stack(R
RfR�R�((s!/sys/lib/python2.7/idlelib/rpc.pytpollmessagews
c
Cs�x�ytjd�}Wntjk
r,n,X|\}}|d|ff}|j|�y#|j|�}|dkrzdSWn-tk
r�|j�dSt k
r�dSX|\}}|d}|j
d||f�|d krU|j
d|�|j||�}|j
d||f�|dkr@|j||f�q|dkrqqq||kre|S|jj|d�} | dk r| j
�||j|<| j�| j�qqqdS(
sRHandle 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.
iRAspollresponse:%d:myseq:%sRCRDspollresponse:%d:localcall:call:s%pollresponse:%d:localcall:response:%sN(sCALLsQUEUE(tresponse_queuetgettQueuetEmptyRdR�RRpt
handle_EOFR�R9R\R0R{R/tnotifyR|(
R
RqRftqmsgRRRiR�tresqRStcv((s!/sys/lib/python2.7/idlelib/rpc.pyRz�sN
cCsq|j�|jd�xI|jD]>}|j|}|j�d|j|<|j�|j�q!W|j�dS(s+action taken upon link being closed by peerR�RjN(sEOFN( tEOFhookR9R0R{RR/R�R|R2(R
tkeyR�((s!/sys/lib/python2.7/idlelib/rpc.pyR��s
cCsdS(sBClasses using rpc client/server can override to augment EOF actionN((R
((s!/sys/lib/python2.7/idlelib/rpc.pyR��sN("R'R(R}RRR1R2R9R<R>R\R_RaR]R`R^RhRkRrRgRtRsRbRdR�R�R�R�R�R�R�RzR�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyR)|s> *
I
RMcBseZRS((R'R((((s!/sys/lib/python2.7/idlelib/rpc.pyRM�scCs t|�}|t|<t|�S(N(tidR-Ru(RVR:((s!/sys/lib/python2.7/idlelib/rpc.pyRN�s
RucBseZd�ZRS(cCs
||_dS(N(R:(R
R:((s!/sys/lib/python2.7/idlelib/rpc.pyR�s(R'R(R(((s!/sys/lib/python2.7/idlelib/rpc.pyRu�sR cBs/eZeZdZd�Zd�Zd�ZRS(s#ScCs6||_tj||�tjj||||�dS(N(tcurrent_handlerR)RR
tBaseRequestHandler(R
R,Rtsvr((s!/sys/lib/python2.7/idlelib/rpc.pyR�s cCs|j�dS(s(handle() method required by SocketServerN(Rr(R
((s!/sys/lib/python2.7/idlelib/rpc.pythandle�scCs
t||�S(N(Rv(R
R:((s!/sys/lib/python2.7/idlelib/rpc.pytget_remote_proxy�s(R'R(tFalseR+R4RR�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyR �s
t RPCClientcBsAeZeZdZdZejejd�Z d�Z
d�ZRS(s#CicCs9tj||�|_|jj|�|jjd�dS(Ni(Rtlistening_socktbindtlisten(R
taddresstfamilyttype((s!/sys/lib/python2.7/idlelib/rpc.pyRscCsw|jj�\}}|jr4tjdI|IJn|dtkrWtj||�ntjdI|IJtj �dS(Ns****** Connection request from is** Invalid host: (
R�tacceptR+RRt LOCALHOSTR)RRRQ(R
tworking_sockR�((s!/sys/lib/python2.7/idlelib/rpc.pyR�s cCs
t||�S(N(Rv(R
R:((s!/sys/lib/python2.7/idlelib/rpc.pyR�s(R'R(R�R+R4R}RtAF_INETtSOCK_STREAMRR�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyR�s
RvcBs8eZdZdZd�Zd�Zd�Zd�ZRS(cCs||_||_dS(N(tsockioR:(R
R�R:((s!/sys/lib/python2.7/idlelib/rpc.pyRs cCs�|jdkr|j�n|jj|�rDt|j|j|�S|jdkr`|j�n||jkr�|jj |jd|fi�}|St
|�dS(Nt__getattribute__(t_RPCProxy__methodsRt_RPCProxy__getmethodsR�tMethodProxyR�R:t_RPCProxy__attributest_RPCProxy__getattributesR_R�(R
tnametvalue((s!/sys/lib/python2.7/idlelib/rpc.pyt__getattr__"s
cCs%|jj|jddi�|_dS(NRB((R�R_R:R�(R
((s!/sys/lib/python2.7/idlelib/rpc.pyt__getattributes0scCs%|jj|jddi�|_dS(NR@((R�R_R:R�(R
((s!/sys/lib/python2.7/idlelib/rpc.pyt__getmethods4sN( R'R(RR�R�RR�R�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyRvs cCs�x?t|�D]1}t||�}t|d�r
d||<q
q
Wt|�tjkrjt|j|�nt|�tjkr�x!|j D]}t||�q�WndS(Nt__call__i(
tdirRKRJR�RwtInstanceTypeRHt __class__t ClassTypet __bases__(RVRWR�tattrtsuper((s!/sys/lib/python2.7/idlelib/rpc.pyRH8scCsFx?t|�D]1}t||�}t|d�s
d||<q
q
WdS(NR�i(R�RKRJ(RVRXR�R�((s!/sys/lib/python2.7/idlelib/rpc.pyRIEsR�cBseZd�Zd�ZRS(cCs||_||_||_dS(N(R�R:R�(R
R�R:R�((s!/sys/lib/python2.7/idlelib/rpc.pyRMs cOs%|jj|j|j||�}|S(N(R�R_R:R�(R
R6RUR�((s!/sys/lib/python2.7/idlelib/rpc.pyR�Rs!(R'R(RR�(((s!/sys/lib/python2.7/idlelib/rpc.pyR�Ks (%t__doc__RR!RR�R
R�tcPickleR~RR�Rtcopy_regRwRRRtCodeTypeR�R�RRR-ROR�R;R)RMRNRuR�R R�RvRHRIR�(((s!/sys/lib/python2.7/idlelib/rpc.pyt<module>sB
2�
|