
Wi5                 @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 m
 Z
 d  d l	 m Z d  d l	 m Z d  d l	 m Z d  d l	 m Z d  d l	 m Z d  d l	 m Z d  d	 l	 m Z d  d l Z d
 Z d  d l Z d d d d  Z d a d d   Z e d  e j   Z e d d  Z d a d a d a d d d  Z d d   Z d d   Z  d d   Z! d d   Z" d d   Z# d d    Z$ Gd! d"   d" e j%  Z& Gd# d$   d$ e j'  Z( Gd% d&   d& e)  Z* e d  d S)'    N)CallTips)AutoComplete)RemoteDebugger)RemoteObjectBrowser)StackViewer)rpc)PyShell)	IOBindingz	127.0.0.1c             C   sT   | d k r t  j } y& | j t j |  | | | |   Wn t k
 rO Yn Xd S)zuShow Idle-format warning after replacing warnings.showwarning.

    The only difference is the formatter called.
    N)sysstderrwriter   Zidle_formatwarningIOError)messagecategoryfilenamelinenofileline r   8/afs/.cs/s/python-3.5.2/amd64_ubu14/lib/python3.5/run.pyidle_showwarning_subproc   s    	r   c             C   sF   |  r' t  d k rB t j a  t t _ n t  d k	 rB t  t _ d a  d S)zFReplace warning.showwarning with idle_showwarning_subproc, or reverse.N)_warnings_showwarningwarningsshowwarningr   )capturer   r   r   capture_warnings*   s    		r   Tc             C   s   |  j  d  d S)zProcess any tk events that are ready to be dispatched if tkinter
    has been imported, a tcl interpreter has been created and tk has been
    loaded.updateN)eval)tclr   r   r   handle_tk_events:   s    r   Fc             C   s  |  a  y2 t t j  d k s$ t  t t j d  } Wn t d d t j d SYn Xt d  d g t j d d  <t	 j
 d t d d	 d
 t | f f  } d | _ | j   x\y t r y t   Wn t k
 r w Yn Xy% t j j d d d d  \ } } Wn t j k
 r)t   w Yn X| \ } } } | | |   } t j j | | f  Wq t k
 rt r{d a w Yq t k
 rt d    Yq t j   \ }	 }
 } y! t   t j j | d f  Wn+ t j |	 |
 | d t j t   Yn Xw Yq Xq Wd S)aj  Start the Python execution server in a subprocess

    In the Python subprocess, RPCServer is instantiated with handlerclass
    MyHandler, which inherits register/unregister methods from RPCHandler via
    the mix-in class SocketIO.

    When the RPCServer 'server' is instantiated, the TCPServer initialization
    creates an instance of run.MyHandler and calls its handle() method.
    handle() instantiates a run.Executive object, passing it a reference to the
    MyHandler object.  That reference is saved as attribute rpchandler of the
    Executive instance.  The Executive methods have access to the reference and
    can pass it on to entities that they command
    (e.g. RemoteDebugger.Debugger.start_debugger()).  The latter, in turn, can
    call MyHandler(SocketIO) register/unregister methods via the reference to
    register and unregister themselves.

       z/IDLE Subprocess: no IP port passed in sys.argv.r   NT targetnameZ
SockThreadargsblocktimeoutg?F)no_exitfunclenr
   argvAssertionErrorintprint
__stderr__r   	threadingThreadmanage_socket	LOCALHOSTdaemonstartexit_nowexitKeyboardInterruptr   Zrequest_queuegetqueueEmptyr   Zresponse_queueputquitting
SystemExitexc_infoprint_exception	traceback)Zdel_exitfuncportZ
sockthreadseqrequestmethodr$   kwargsrettypevaluetbr   r   r   mainH   s\    	
	
	
%
rJ   c             C   s   x t  d  D]w } t j |  y t |  t  } PWq t k
 r } z- t d | j d d d t j	 | } WYd  d  } ~ Xq Xq Wt d d t j	 t
 | |   d a d  S| j   d  S)N   zIDLE Subprocess: OSError: r    z, retrying....r   z8IDLE Subprocess: Connection to IDLE GUI failed, exiting.T)rangetimesleepMyRPCServer	MyHandlerOSErrorr-   r$   r
   r.   show_socket_errorr5   Zhandle_request)addressiZservererrZsocket_errorr   r   r   r1      s    	
r1   c             C   s   d d  l  } d d  l j } | j   } | j   |  j d d k rg d | } | j d | d | n! | j d d |  j d d | | j   d  S)Nr   =   zIDLE's subprocess can't connect to %s:%d.  This may be due to your personal firewall configuration.  It is safe to allow this internal connection because no data is visible on external ports.zIDLE Subprocess ErrorparentzSocket Error: %sr    )tkinterZtkinter.messageboxZ
messageboxZTkZwithdrawr$   Z	showerrorZdestroy)rU   rS   rX   tkMessageBoxrootmsgr   r   r   rR      s    
	rR   c                 s   d d  l  }  |  j   t   t j   t j   \ } } } } | \ t _ t _ t _ t	        f d d     | | |  d  S)Nr   c       	         s6   j  |  | j } | j } | d  k	 rc |  k rc  t |  | | j  t d d   nK | d  k	 r | j r |  k r  t |  | | j  t d d   | r t j |  } t d d   d } t	 | |  t j
 | d   t j |  |  } x$ | D] } t | d d d   qWd  S)NzF
The above exception was the direct cause of the following exception:
r   zE
During handling of the above exception, another exception occurred:
z"Traceback (most recent call last):run.pyrpc.pythreading.pyqueue.pyRemoteDebugger.pybdb.pyendr!   )r\   r]   r^   r_   r`   ra   )add__context__	__cause__rG   __traceback__r-   __suppress_context__r@   
extract_tbcleanup_traceback
print_listformat_exception_only)	typexcrI   contextcauseZtbeexcludelinesr   )efile	print_excseenr   r   rs      s.    			

	 z"print_exception.<locals>.print_exc)
	linecache
checkcacheflush_stdoutr
   r   r>   	last_type
last_valuelast_tracebackset)ru   rl   valrI   Zexcinfor   )rr   rs   rt   r   r?      s    
		r?   c       
      C   sn  |  d d  } x; |  rM x' | D] } |  d d j  |  r  Pq  WP|  d =q Wx; |  r x' | D] } |  d d j  |  r^ Pq^ WP|  d =qQ Wt |   d k r | d d  |  d d  <t d d t j t j d j } x t t |    D]x } |  | \ } } } }	 | d k rd } |	 rP| j	 d	  rP| j
 d
 d | | f i   }	 | | | |	 f |  | <q Wd S)zARemove excluded traces from beginning/end of tb; get cached linesNr   r    z** IDLE Internal Exception: r   exec?z
-toplevel-z	<pyshell#ru   getliner'   r'   )countr)   r-   r
   r   r   Zobjecttable
rpchandlerrL   
startswithZ
remotecall)
rI   rp   Zorig_tbZrpcfiler   rT   fnlnZnmr   r   r   r   ri      s2    		ri   c               C   s   d S)zXXX How to do this now?Nr   r   r   r   r   rw      s    rw   c              C   s7   t  r d d l }  |  j   t d  t j d  d S)zExit subprocess, possibly after first clearing exit functions.

    If config-main.cfg/.def 'General' 'delete-exitfunc' is True, then any
    functions registered with atexit will be removed before exiting.
    (VPython support)

    r   NF)r(   atexit_clearr   r
   r6   )r   r   r   r   r6      s
    

r6   c               @   s   e  Z d  Z d d   Z d S)rO   c             C   s	  y   Wn t  k
 r   Yn t k
 r? d a t j   Yn t j } t d d d d | t d d | t d t j	   j
 d | t d | d | t d	 t |  d | t j d |  t d
 d | t d d d | d a t j   Yn Xd S)zrOverride RPCServer method for IDLE

        Interrupt the MainThread and exit server if link is dropped.

        T
-(   r   zUnhandled server exception!z
Thread: %szClient Address: z	Request: z#
*** Unrecoverable, server exiting!N)r=   EOFErrorr5   threadinterrupt_mainr
   r.   r-   r/   current_threadr#   reprr@   rs   r<   )selfrC   Zclient_addresserfr   r   r   handle_error
  s&    	zMyRPCServer.handle_errorN)__name__
__module____qualname__r   r   r   r   r   rO     s   rO   c               @   s@   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 S)
rP   c             C   s   t  |   } |  j d |  |  j d  |  _ t j |  j d t j  t _	 t j
 |  j d t j  t _ t j
 |  j d t j  t _ t j t _ d d l } | j | _ t j	 |  _ |  j d  |  _ t j j |  d	 d d
 d d S)zOverride base methodr}   consolestdinstdoutr   r   NinterpZmyseqwaitg?)	Executiveregisterget_remote_proxyr   r   ZPseudoInputFiler	   encodingr
   r   ZPseudoOutputFiler   r   r   displayhookpydocZ
plainpagerZpagerZ_keep_stdinr   
RPCHandlerZgetresponse)r   Z	executiver   r   r   r   handle(  s    zMyHandler.handlec             C   s   t  j d  d S)z>override SocketIO method - wait for MainThread to shut us down
   N)rM   rN   )r   r   r   r   exithook@  s    zMyHandler.exithookc             C   s   d a  t j   d S)zEOverride SocketIO method - terminate wait on callback and exit threadTN)r<   r   r   )r   r   r   r   EOFhookD  s    zMyHandler.EOFhookc             C   s   d a  t j   d S)zinterrupt awakened threadTN)r<   r   r   )r   r   r   r   decode_interrupthookJ  s    zMyHandler.decode_interrupthookN)r   r   r   r   r   r   r   r   r   r   r   rP   &  s   rP   c               @   ss   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d d  Z
 d S)r   c             C   s7   | |  _  t j |  _ t j   |  _ t j   |  _ d  S)N)r   __main____dict__localsr   calltipr   autocomplete)r   r   r   r   r   __init__S  s    	zExecutive.__init__c             C   s   y1 d  |  _  d a z t | |  j  Wd  d a XWng t k
 rE Yn] t j   |  _  t rd t   t	   |  j
 j j d  } | r |  j
 j j   Yn Xt   d  S)NTFz<<toggle-jit-stack-viewer>>)usr_exc_infointerruptabler}   r   r=   r
   r>   r<   r6   r?   r   r   Zgetvarr   Zopen_remote_stack_viewerrw   )r   codeZjitr   r   r   runcodeY  s"    	zExecutive.runcodec             C   s   t  r t j   d  S)N)r   r   r   )r   r   r   r   interrupt_the_serverr  s    zExecutive.interrupt_the_serverc             C   s   t  j |  j |  S)N)r   Zstart_debuggerr   )r   Zgui_adap_oidr   r   r   start_the_debuggerv  s    zExecutive.start_the_debuggerc             C   s   |  j  j |  d S)zDUnregister the Idb Adapter.  Link objects and Idb then subject to GCN)r   
unregister)r   Zidb_adap_oidr   r   r   stop_the_debuggery  s    zExecutive.stop_the_debuggerc             C   s   |  j  j |  S)N)r   Z	fetch_tip)r   r#   r   r   r   get_the_calltip}  s    zExecutive.get_the_calltipc             C   s   |  j  j | |  S)N)r   Zfetch_completions)r   Zwhatmoder   r   r   get_the_completion_list  s    z!Executive.get_the_completion_listNc             C   s   |  j  r |  j  \ } } } n d  Sd  } | d  k	 rF |  j j |  } x) | rq | j j d d k rq | j } qI W| t _ | t _ t	 j
 | |  } t j |  S)Nr   r   run)zrpczrun)r   r   r   tb_frame	f_globalstb_nextr
   rx   ry   r   ZStackTreeItemr   Zremote_object_tree_item)r   Z	flist_oidrl   r|   rI   flistitemr   r   r   stackviewer  s    			zExecutive.stackviewer)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Q  s   r   )+r
   ru   rM   r@   _threadr   r/   r9   rX   Zidlelibr   r   r   r   r   r   r   r	   r   r2   r   r   r   r   ZTclr   r   r5   r<   r   rJ   r1   rR   r?   ri   rw   r6   Z	RPCServerrO   r   rP   objectr   r   r   r   r   <module>   sL   

I$+A