Your IP : 3.147.66.224


Current Path : /opt/alt/python34/lib64/python3.4/idlelib/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/idlelib/__pycache__/rpc.cpython-34.pyo

�
e f.Q�@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZdd�Zdd�Zddd�ZGd	d
�d
ej�ZddZd
ZGdd�dej�ZiZe
jd�Ze
jd�ZGdd�de�ZGdd�de�Zdd�Z Gdd�de�Z!Gdd�dej"e�Z#Gdd�de�Z$Gdd�de�Z%dd�Z&d d!�Z'Gd"d#�d#e�Z(d$d%�Z)dS)&aRPC 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.

�NcCstj|�}|S)N)�marshal�loads)�ms�co�r�0/opt/alt/python34/lib64/python3.4/idlelib/rpc.py�
unpickle_code/srcCstj|�}t|ffS)N)r�dumpsr)rrrrr�pickle_code4sr
cCs2tj�}t||�}|j|�|j�S)N)�io�BytesIO�CodePickler�dump�getvalue)�objZprotocol�f�prrrr	9s
r	c@s0eZdZieej6Zejej�dS)r
N)	�__name__�
__module__�__qualname__r
�types�CodeType�dispatch_table�update�copyregrrrrr
?sr
�iz	127.0.0.1c@sOeZdZddd�Zdd�Zdd�Zdd	�Zd
d�ZdS)�	RPCServerNcCs/|dkrt}ntjj|||�dS)N)�
RPCHandler�socketserver�	TCPServer�__init__)�self�addrZhandlerclassrrrr Hs	zRPCServer.__init__cCsdS)z@Override TCPServer method, no bind() phase for connecting entityNr)r!rrr�server_bindMszRPCServer.server_bindcCs|jj|j�dS)z�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)�socketZconnect�server_address)r!rrr�server_activateQszRPCServer.server_activatecCs|j|jfS)z:Override TCPServer method, return already connected socket)r$r%)r!rrr�get_requestZszRPCServer.get_requestcCs�y�Wn�tk
r�Yn�tj}tdddd|�tdd|�tdtj�jd|�td|d|�tdt|�d|�tj	d|�td	d|�tddd|�t
jd
�YnXdS)z�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.

        �
�-�(�filezUnhandled server exception!z
Thread: %szClient Address: z	Request: z#
*** Unrecoverable, server exiting!rN)�
SystemExit�sys�
__stderr__�print�	threading�current_thread�name�repr�	traceback�	print_exc�os�_exit)r!�requestZclient_addressZerfrrr�handle_error^s
	zRPCServer.handle_error)rrrr r#r&r'r9rrrrrFs
	rc@sreZdZdZdddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+Zd,ZdZd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Z d9d:�Z!dS);�SocketIOrNcCsdtj�|_|dk	r'||_n||_|dkrEt}n||_i|_i|_dS)N)	r0r1�
sockthread�	debugging�sock�objecttable�objtable�	responses�cvars)r!r=r?r<rrrr �s				zSocketIO.__init__cCs/|j}d|_|dk	r+|j�ndS)N)r=�close)r!r=rrrrB�s		zSocketIO.closecCstjd�dS)z!override for specific exit actionrN)r6r7)r!rrr�exithook�szSocketIO.exithookcGsi|js
dS|jdttj�j�}x"|D]}|dt|�}q4Wt|dtj�dS)N� r+)	r<�location�strr0r1r2r/r-r.)r!�args�s�arrr�debug�s	 
zSocketIO.debugcCs||j|<dS)N)r?)r!�oid�objectrrr�register�szSocketIO.registercCs'y|j|=Wntk
r"YnXdS)N)r?�KeyError)r!rKrrr�
unregister�s
zSocketIO.unregistercCs*|jd|�y|\}\}}}}Wntk
rDdSYnX||jkredd|ffS|j|}|dkr�i}	t||	�d|	fS|dkr�i}
t||
�d|
fSt||�s�dd|ffSt||�}y�|d	kr9|||�}t|t�r/t	|�}nd|fS|d
kret
j||||ff�dSdd|fSWn�tk
r��Yn�t
k
r��Yn�tk
r��Ynstk
r�}
zd
|
fSWYdd}
~
XnEd}t||||fdtj�tjdtj�dSYnXdS)Nz
localcall:�ERROR�Bad request formatzUnknown object id: %r�__methods__�OK�__attributes__zUnsupported method name: %r�CALL�QUEUE�QUEUEDzUnsupported message type: %s�CALLEXCzU*** Internal Error: rpc.py:SocketIO.localcall()

 Object: %s 
 Method: %s 
 Args: %s
r+�	EXCEPTION)rPrQ)rWN)rYN)rJ�	TypeErrorr?�_getmethods�_getattributes�hasattr�getattr�
isinstance�RemoteObject�	remoteref�
request_queueZputr,�KeyboardInterrupt�OSError�	Exceptionr/r-r.r4r5)r!�seqr8�howrK�
methodnamerG�kwargsr�methods�
attributes�methodZretZex�msgrrr�	localcall�sT
	








 zSocketIO.localcallcCs8|jd||�|j||||�}|j|�S)Nzremotecall:asynccall: )rJ�	asynccall�asyncreturn)r!rKrhrGrirfrrr�
remotecall�szSocketIO.remotecallcCs8|jd||�|j||||�}|j|�S)Nzremotequeue:asyncqueue: )rJ�
asyncqueuerp)r!rKrhrGrirfrrr�remotequeue�szSocketIO.remotequeuecCs�d||||ff}|j�}tj�|jkrUtj�}||j|<n|jd|||||�|j||f�|S)NrUz
asynccall:%d:)�newseqr0r1r;�	ConditionrArJ�
putmessage)r!rKrhrGrir8rf�cvarrrrro�szSocketIO.asynccallcCs�d||||ff}|j�}tj�|jkrUtj�}||j|<n|jd|||||�|j||f�|S)NrVzasyncqueue:%d:)rtr0r1r;rurArJrv)r!rKrhrGrir8rfrwrrrrr�szSocketIO.asyncqueuecCsG|jd|�|j|dd�}|jd||�|j|�S)Nz#asyncreturn:%d:call getresponse(): �waitg�������?zasyncreturn:%d:response: )rJ�getresponse�decoderesponse)r!rf�responserrrrp�szSocketIO.asyncreturncCs�|\}}|dkr|S|dkr,dS|dkrI|jd�dS|dkrp|jd�|j�dS|dkr�|jd|�t|��n|d	kr�|jd
|�|�nt||��dS)NrSrWrYzdecoderesponse: EXCEPTION�EOFzdecoderesponse: EOFrPzdecoderesponse: Internal ERROR:rXzdecoderesponse: Call Exception:)rJ�decode_interrupthook�RuntimeError�SystemError)r!r{rg�whatrrrrz�s&


	zSocketIO.decoderesponsecCs
t�dS)�N)�EOFError)r!rrrr}szSocketIO.decode_interrupthookcCsDy|jdddd�Wn#tk
r?|jd�dSYnXdS)z�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.

        �myseqNrxg�������?zmainloop:return)ryr�rJ)r!rrr�mainloops


zSocketIO.mainloopcCsU|j||�}|dk	rQ|\}}|dkrQ||j|�f}qQn|S)NrS)�_getresponse�_proxify)r!r�rxr{rgr�rrrryszSocketIO.getresponsecCsHt|t�rt||j�St|t�rDtt|j|��S|S)N)r_�RemoteProxy�RPCProxyrK�list�mapr�)r!rrrrr�'s
zSocketIO._proxifycCs�|jd|�tj�|jkrQx�|j||�}|dk	r(|Sq(Wn}|j|}|j�x||jkr�|j�qkW|j|}|jd||f�|j|=|j|=|j	�|SdS)Nz_getresponse:myseq:z-_getresponse:%s: thread woke up: response: %s)
rJr0r1r;�pollresponserA�acquirer@rx�release)r!r�rxr{rwrrrr�/s"


	


zSocketIO._getresponsecCs|jd|_}|S)N�)�nextseq)r!rfrrrrtEszSocketIO.newseqcCs|jd|d�yt|�}Wn4tjk
r[tdt|�dtj��YnXtj	dt
|��|}x�t
|�dkryDtjg|jgg�\}}}|jj
|dt��}Wn$ttfk
r�td��YnX||d�}q{WdS)Nzputmessage:%d:rzCannot pickle:r+z<izsocket no longer exists)rJr	�pickleZ
PicklingErrorr/r3r-r.�structZpack�len�selectr=�send�BUFSIZE�AttributeErrorrZrd)r!�messagerH�r�w�x�nrrrrvIs$ zSocketIO.putmessage��cCs�|j�t|j�|jkr�tj|jj�ggg|�\}}}t|�dkredSy|jjt�}Wnt	k
r�t
�YnXt|�dkr�t
�n|j|7_|j�n|j�S)Nr)�_stage0r��buff�bufneedr�r=�filenoZrecvr�rdr��_stage1)r!rxr�r�r�rHrrr�
pollpacket]s
-
	
zSocketIO.pollpacketcCsv|jdkrrt|j�dkrr|jdd�}|jdd�|_tjd|�d|_d|_ndS)Nrr�z<i�)�bufstater�r�r�Zunpackr�)r!rHrrrr�ms
$zSocketIO._stage0cCsp|jdkrlt|j�|jkrl|jd|j�}|j|jd�|_d|_d|_|SdS)Nr�r�r)r�r�r�r�)r!�packetrrrr�ts'		zSocketIO._stage1cCs�|j|�}|dkrdSytj|�}Wnmtjk
r�tddtj�tdt|�dtj�tj	dtj�tddtj��YnX|S)Nz-----------------------r+zcannot unpickle packet:)
r�r�rZUnpicklingErrorr/r-r.r3r4�print_stack)r!rxr�r�rrr�pollmessage|szSocketIO.pollmessagec
Cs�x�ytjd�}Wntjk
r-Yn,X|\}}|d|ff}|j|�y#|j|�}|dkr{dSWn5tk
r�|j�dSYntk
r�dSYnX|\}}|d}|j	d||f�|d	kr^|j	d|�|j
||�}|j	d||f�|dkrI|j||f�q|dkrqqq||krn|S|jj|d�}	|	dk	r|	j�||j
|<|	j�|	j�qqqWdS)
aRHandle 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.

        rrSNzpollresponse:%d:myseq:%srUrVzpollresponse:%d:localcall:call:z%pollresponse:%d:localcall:response:%s)zCALLzQUEUE)�response_queue�get�queueZEmptyrvr�r��
handle_EOFr�rJrnrAr�r@�notifyr�)
r!r�rxZqmsgrfr{r�Zresqrg�cvrrrr��sN



	
	



zSocketIO.pollresponsecCsq|j�|jd�xI|jD]>}|j|}|j�d|j|<|j�|j�q!W|j�dS)z+action taken upon link being closed by peerr�r|N)zEOFN)�EOFhookrJrAr�r@r�r�rC)r!�keyr�rrrr��s





zSocketIO.handle_EOFcCsdS)zBClasses using rpc client/server can override to augment EOF actionNr)r!rrrr��szSocketIO.EOFhook)"rrrr�r rBrCrJrMrOrnrqrsrorrrprzr}r�ryr�r�rtrvr�r�r�r�r�r�r�r�r�r�rrrrr:}s>.


I
r:c@seZdZdS)r`N)rrrrrrrr`�sr`cCs t|�}|t|<t|�S)N)�idr>r�)rrKrrrra�s
rac@seZdZdd�ZdS)r�cCs
||_dS)N)rK)r!rKrrrr �szRemoteProxy.__init__N)rrrr rrrrr��sr�c@s@eZdZdZdZdd�Zdd�Zdd�Zd	S)
rFz#ScCs6||_tj||�tjj||||�dS)N)Zcurrent_handlerr:r r�BaseRequestHandler)r!r=r"Zsvrrrrr �s	zRPCHandler.__init__cCs|j�dS)z(handle() method required by socketserverN)r�)r!rrr�handle�szRPCHandler.handlecCs
t||�S)N)r�)r!rKrrr�get_remote_proxyszRPCHandler.get_remote_proxyN)rrrr<rEr r�r�rrrrr�s
rc@sReZdZdZdZdZejejdd�Z	dd�Z
dd	�Zd
S)�	RPCClientFz#Cr�cCs9tj||�|_|jj|�|jjd�dS)Nr�)r$�listening_sockZbindZlisten)r!�addressZfamily�typerrrr szRPCClient.__init__cCsz|jj�\}}|jr7td|dtj�n|dtkrZtj||�ntd|dtj�t	�dS)Nz****** Connection request from r+rz** Invalid host: )
r��acceptr<r/r-r.�	LOCALHOSTr:r rd)r!Zworking_sockr�rrrr�s	zRPCClient.acceptcCs
t||�S)N)r�)r!rKrrrr�szRPCClient.get_remote_proxyN)rrrr<rEr�r$ZAF_INETZSOCK_STREAMr r�r�rrrrr�s
r�c@sLeZdZdZdZdd�Zdd�Zdd�Zdd	�ZdS)
r�NcCs||_||_dS)N)�sockiorK)r!r�rKrrrr #s	zRPCProxy.__init__cCs�|jdkr|j�n|jj|�rDt|j|j|�S|jdkr`|j�n||jkr�|jj|jd|fi�}|St	|��dS)N�__getattribute__)
�_RPCProxy__methods�_RPCProxy__getmethodsr��MethodProxyr�rK�_RPCProxy__attributes�_RPCProxy__getattributesrqr�)r!r2�valuerrr�__getattr__'s

zRPCProxy.__getattr__cCs%|jj|jdfi�|_dS)NrT)r�rqrKr�)r!rrrZ__getattributes5szRPCProxy.__getattributescCs%|jj|jdfi�|_dS)NrR)r�rqrKr�)r!rrrZ__getmethods9szRPCProxy.__getmethods)	rrrr�r�r r�r�r�rrrrr�sr�cCsvx<t|�D].}t||�}t|�r
d||<q
q
Wt|t�rrx!|jD]}t||�qXWndS)Nr�)�dirr^�callabler_r��	__bases__r[)rrjr2�attr�superrrrr[=sr[cCsCx<t|�D].}t||�}t|�s
d||<q
q
WdS)Nr�)r�r^r�)rrkr2r�rrrr\Hsr\c@s(eZdZdd�Zdd�ZdS)r�cCs||_||_||_dS)N)r�rKr2)r!r�rKr2rrrr Ps		zMethodProxy.__init__cOs%|jj|j|j||�}|S)N)r�rqrKr2)r!rGrir�rrr�__call__Us!zMethodProxy.__call__N)rrrr r�rrrrr�Nsr�cCs�|dkrdSdt_t|�}ytjj|�WnLtk
r�d}|j|d�}|j|d�}tjj|�YnXtjjd�|t_dS)z9Override standard display hook to use non-locale encodingN�ascii�backslashreplace�strictr()	�builtins�_r3r-�stdout�write�UnicodeEncodeError�encode�decode)r��text�encoding�bytesrrr�displayhook]s	
r�)*�__doc__r-r6rr$r�rr�r�r0r�r4rrrr�rr
r	ZPicklerr
r�r�rrr>ZQueuerbr�rLr:r`rar�r�rr�r�r[r\r�r�rrrr�<module>sJ
2�j

?>