Plan 9 from Bell Labs’s /usr/web/sources/contrib/stallion/root/sys/lib/python2.7/idlelib/rpc.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�`^c@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZd�Zd�Zeje
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�NcCs+tj|�}t|tj�s't�|S(N(tmarshaltloadst
isinstancettypestCodeTypetAssertionError(tmstco((s!/sys/lib/python2.7/idlelib/rpc.pyt
unpickle_code-scCs4t|tj�st�tj|�}t|ffS(N(RRRRRtdumpsR(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
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.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(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.pyREs
				itSocketIOcBseZdZddd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d�Zd	�Zd
�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(
RR t
sockthreadRt	debuggingtsocktobjecttabletobjtablet	responsestcvars(RR0R2R/((s!/sys/lib/python2.7/idlelib/rpc.pyR�s				cCs/|j}d|_|dk	r+|j�ndS(N(R0Rtclose(RR0((s!/sys/lib/python2.7/idlelib/rpc.pyR5�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/tlocationtstrRR R!RR(Rtargststa((s!/sys/lib/python2.7/idlelib/rpc.pytdebug�s	#
cCs||j|<dS(N(R2(Rtoidtobject((s!/sys/lib/python2.7/idlelib/rpc.pytregister�scCs&y|j|=Wntk
r!nXdS(N(R2tKeyError(RR>((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(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	localcall�sL







cCs8|jd||�|j||||�}|j|�S(Nsremotecall:asynccall: (R=t	asynccalltasyncreturn(RR>RWR:RXRU((s!/sys/lib/python2.7/idlelib/rpc.pyt
remotecall�scCs8|jd||�|j||||�}|j|�S(Nsremotequeue:asyncqueue: (R=t
asyncqueueRa(RR>RWR:RXRU((s!/sys/lib/python2.7/idlelib/rpc.pytremotequeue�scCs�d||||ff}|j�}tj�|jkrUtj�}||j|<n|jd|||||�|j||f�|S(NRGs
asynccall:%d:(tnewseqRR R.t	ConditionR4R=t
putmessage(RR>RWR:RXR'RUtcvar((s!/sys/lib/python2.7/idlelib/rpc.pyR`�scCs�d||||ff}|j�}tj�|jkrUtj�}||j|<n|jd|||||�|j||f�|S(NRHsasyncqueue:%d:(ReRR R.RfR4R=Rg(RR>RWR:RXR'RURh((s!/sys/lib/python2.7/idlelib/rpc.pyRc�scCsG|jd|�|j|dd�}|jd||�|j|�S(Ns#asyncreturn:%d:call getresponse(): twaitg����sasyncreturn:%d:response: (R=tgetresponsetdecoderesponse(RRUtresponse((s!/sys/lib/python2.7/idlelib/rpc.pyRa�scCs�|\}}|dkr|S|dkr,dS|dkrI|jd�dS|dkrp|jd�|j�dS|dkr�|jd|�t|�nt||f�dS(	NRERIRJsdecoderesponse: EXCEPTIONtEOFsdecoderesponse: EOFRCsdecoderesponse: Internal ERROR:(RR=tdecode_interrupthooktRuntimeErrortSystemError(RRlRVtwhat((s!/sys/lib/python2.7/idlelib/rpc.pyRk�s 


cCs
t�dS(tN(tEOFError(R((s!/sys/lib/python2.7/idlelib/rpc.pyRnscCs@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.

        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|t�rt||j�St|tj�rAt|j|�S|S(N(RtRemoteProxytRPCProxyR>RtListTypetmapRw(RRY((s!/sys/lib/python2.7/idlelib/rpc.pyRws
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(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|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(R=tpickleR	t
PicklingErrorRRR"tstructtpacktlentselectR0tsendtBUFSIZEtAttributeErrorRKtIOErrorRRT(RtmessageR;trtwtxtn((s!/sys/lib/python2.7/idlelib/rpc.pyRgAs $RricCs�|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�R0tfilenoRtrecvR�RRTRst_stage1(RRiR�R�R�R;((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�(RR;((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�(Rtpacket((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�RR�RtUnpicklingErrorRRR"R#tprint_stack(RRiR�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|jj|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.

        iREspollresponse:%d:myseq:%sRGRHspollresponse:%d:localcall:call:s%pollresponse:%d:localcall:response:%sN(sCALLsQUEUE(tresponse_queuetgettQueuetEmptyRgR�RRst
handle_EOFR�R=R_R4R}R3tnotifyR~(
RRtRitqmsgRURlR�tresqRVtcv((s!/sys/lib/python2.7/idlelib/rpc.pyR|�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�RmN(sEOFN(	tEOFhookR=R4R}RR3R�R~R6(RtkeyR�((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,RRRR5R6R=R@RBR_RbRdR`RcRaRkRnRuRjRwRvReRgR�R�R�R�R�R�R�R|R�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyR-|s>						*			
	
				
										I	
RPcBseZRS((R+R,(((s!/sys/lib/python2.7/idlelib/rpc.pyRP�scCs t|�}|t|<t|�S(N(tidR1Rx(RYR>((s!/sys/lib/python2.7/idlelib/rpc.pyRQ�s
RxcBseZd�ZRS(cCs
||_dS(N(R>(RR>((s!/sys/lib/python2.7/idlelib/rpc.pyR�s(R+R,R(((s!/sys/lib/python2.7/idlelib/rpc.pyRx�sR
cBs/eZeZdZd�Zd�Zd�ZRS(s#ScCs6||_tj||�tjj||||�dS(N(tcurrent_handlerR-RRtBaseRequestHandler(RR0Rtsvr((s!/sys/lib/python2.7/idlelib/rpc.pyR�s	cCs|j�dS(s(handle() method required by SocketServerN(Ru(R((s!/sys/lib/python2.7/idlelib/rpc.pythandle�scCs
t||�S(N(Ry(RR>((s!/sys/lib/python2.7/idlelib/rpc.pytget_remote_proxy�s(R+R,tFalseR/R8RR�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(Rtaddresstfamilyttype((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-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.pyR�s(R+R,R�R/R8RRtAF_INETtSOCK_STREAMRR�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyR�s	
RycBs8eZdZdZd�Zd�Zd�Zd�ZRS(cCs||_||_dS(N(tsockioR>(RR�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__methodsRt_RPCProxy__getmethodsR�tMethodProxyR�R>t_RPCProxy__attributest_RPCProxy__getattributesRbR�(Rtnametvalue((s!/sys/lib/python2.7/idlelib/rpc.pyt__getattr__"s

cCs%|jj|jddi�|_dS(NRF((R�RbR>R�(R((s!/sys/lib/python2.7/idlelib/rpc.pyt__getattributes0scCs%|jj|jddi�|_dS(NRD((R�RbR>R�(R((s!/sys/lib/python2.7/idlelib/rpc.pyt__getmethods4sN(	R+R,RR�R�RR�R�R�(((s!/sys/lib/python2.7/idlelib/rpc.pyRys			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(
tdirRORNR�RtInstanceTypeRLt	__class__t	ClassTypet	__bases__(RYRZR�tattrtsuper((s!/sys/lib/python2.7/idlelib/rpc.pyRL8scCsFx?t|�D]1}t||�}t|d�s
d||<q
q
WdS(NR�i(R�RORN(RYR[R�R�((s!/sys/lib/python2.7/idlelib/rpc.pyRMEsR�cBseZd�Zd�ZRS(cCs||_||_||_dS(N(R�R>R�(RR�R>R�((s!/sys/lib/python2.7/idlelib/rpc.pyRMs		cOs%|jj|j|j||�}|S(N(R�RbR>R�(RR:RXR�((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�RR�tcPickleR�RR�R#tcopy_regRRRR
RR�R�RRR1RRR�R?R-RPRQRxR�R
R�RyRLRMR�(((s!/sys/lib/python2.7/idlelib/rpc.pyt<module>sB		

2�		
	

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].