a
    If6R                     @   sl  d 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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ZddlZeejd
 dsdD ]Zeee ejde = qdejd
 _dZzdZee_ee_W n e y2   Y n0 dBddZ!dCddZ"da#dd Z$e$d e% Z&e&fddZ'da(da)da*dDddZ+dd Z,dd Z-d d! Z.d"d# Z/d$d% Z0d&d' Zd(d) Z1d*d+ Z2d,Z3d-d. Z4d/d0 Z5G d1d2 d2ej6Z7G d3d4 d4ej8Z9G d5d6 d6e9Z:G d7d8 d8e9Z;G d9d: d:ej<Z=G d;d< d<Z>e?d=kr`dd>l@m+Z+ e+d?d@dA e$d dS )Ez idlelib.run

Simplified, pyshell.ModifiedInterpreter spawns a subprocess with
f'''{sys.executable} -c "__import__('idlelib.run').run.main()"'''
'.run' is needed because __import__ returns idlelib, not idlelib.run.
    N)autocomplete)calltip)
debugger_r)
debugobj_r)iomenu)rpc)stackviewerzidlelib.runfirstrun)ZsimpledialogZ
messageboxfontZdialogZ
filedialogZcommondialogZttkztkinter.Fz	127.0.0.1zCtrl-D (end-of-file)c                 C   sV   d}|d||f 7 }|du r(t ||}| }|r@|d| 7 }|d|j| f 7 }|S )zFormat warnings the IDLE way.z!
Warning (from warnings module):
z  File "%s", line %s
Nz    %s
z%s: %s
)	linecachegetlinestrip__name__)messagecategoryfilenamelinenolines r   lib/python3.9/idlelib/run.pyidle_formatwarning2   s    r   c                 C   s@   |du rt j}z|t| |||| W n ty:   Y n0 dS )zuShow Idle-format warning after replacing warnings.showwarning.

    The only difference is the formatter called.
    N)sysstderrwriter   OSError)r   r   r   r   filer   r   r   r   idle_showwarning_subproc?   s    

r   c                 C   s0   | rt du r,tja tt_nt dur,t t_da dS )zFReplace warning.showwarning with idle_showwarning_subproc, or reverse.N)_warnings_showwarningwarningsshowwarningr   )Zcapturer   r   r   capture_warningsO   s    r!   Tc                 C   s   |  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%   c                 C   s  | a z$ttjdksJ ttjd }W n   tdtjd Y dS 0 td dgtjdd< tj	t
dt|ffd	}d|_|  ztrz
t  W n ty   Y W qY n0 ztjjdd
d}W n tjy   d}Y n0 |r|\}\}}}||i |}tj||f nt  W q ty8   tr0daY qY q tyT   td  Y q   t \}	}
}zt  tj|df W n&   tj|	|
|tjd t  Y n0 Y qY q0 qdS )af  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. debugger_r.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 Z
SockThread)targetnameargs皙?)blockZtimeoutF)no_exitfunclenr   argvintprint
__stderr__r!   	threadingZThreadmanage_socket	LOCALHOSTZdaemonstartexit_nowexitKeyboardInterruptr   Zrequest_queuegetqueueZEmptyZresponse_queueZputr%   quitting
SystemExitexc_infoprint_exception	traceback)Zdel_exitfuncZportZ
sockthreadrequestseqmethodr,   kwargsZrettypevaluetbr   r   r   mainm   s`    


rJ   c                 C   s   t dD ]h}t| zt| t}W  qW q tyn } z,td|jd  d tj	d |}W Y d }~qd }~0 0 qtdtj	d t
||  dad S |  d S )N   zIDLE Subprocess: OSError: r&   z, retrying....r(   z8IDLE Subprocess: Connection to IDLE GUI failed, exiting.T)rangetimesleepMyRPCServer	MyHandlerr   r3   r,   r   r4   show_socket_errorr9   Zhandle_request)addressiZservererrZsocket_errorr   r   r   r6      s&    



r6   c                 C   sl   ddl }ddlm} | }t| |  |dd|d  d|d  d| j d	| j d
	|d |  dS )z(Display socket error from manage_socket.r   N)	showerrorzSubprocess Connection Errorz#IDLE's subprocess can't connect to :r&   z.
Fatal OSError #z: zz.
See the 'Startup failure' section of the IDLE doc, online at
https://docs.python.org/3/library/idle.html#startup-failure)parent)	tkinterZtkinter.messageboxrU   ZTkfix_scalingZwithdrawerrnostrerrorZdestroy)rT   rR   rX   rU   rootr   r   r   rQ      s    rQ   c                     sd   dd l } |   t  tj t  \}}}}|\t_t_t_t	  fdd||| d S )Nr   c           	         s    t| |j}|j}|d urNt|vrNt|||j td d n8|d ur|jst|vrt|||j td d |rt	|}td d d}t
|| tj| d t| |}|D ]}t|d d qd 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):)zrun.pyzrpc.pyzthreading.pyzqueue.pyzdebugger_r.pyzbdb.pyr)   )endr   )addid__context__	__cause__rG   __traceback__r3   __suppress_context__rB   
extract_tbcleanup_traceback
print_listformat_exception_only)	typexcrI   contextcauseZtbeexcludelinesr   Zefile	print_excseenr   r   ro      s4    


z"print_exception.<locals>.print_exc)
r   
checkcacheflush_stdoutr   r   r@   	last_type
last_valuelast_tracebackset)r   rh   valrI   excinfor   rn   r   rA      s    rA   c           
      C   s  | dd }| r:|D ]}| d d  |r q2qq:| d= q| rh|D ]}| d d  |rB q`qBqh| d= q:t| dkr|dd | dd< tdtjd tjd j}tt| D ]R}| | \}}}}	|dkrd}|	s|	d	r|
d
d||fi }	||||	f| |< qdS )zARemove excluded traces from beginning/end of tb; get cached linesNr   r'   z** IDLE Internal Exception: r(   exec?z
-toplevel-z	<pyshell#r   r   )countr0   r3   r   r   r   Zobjecttable
rpchandlerrL   
startswithZ
remotecall)
rI   rl   Zorig_tbZrpcfiler|   rS   fnZlnZnmr   r   r   r   re     s4    re   c                   C   s   dS )zXXX How to do this now?Nr   r   r   r   r   rr      s    rr   c                  C   s*   t rddl} |   td t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/   atexitZ_clearr!   r   r:   )r   r   r   r   r:   #  s
    r:   c                 C   sn   ddl }t| jdd}|dkrj|j| D ]:}|jj| |dd}t|d }|dk r.td	| |d< q.dS )
zScale fonts on HiDPI displays.r   Ntkscalinggffffff?T)r\   r+   existssizeg      )	Ztkinter.fontfloatr   Zcallr
   namesZFontr2   round)r\   rX   r   r+   r
   r   r   r   r   rY   2  s    rY   c                 C   s2   | j d ur| j d nd}|tt| | _ d S )Nz

r)   )__doc__textwrapZfilldedent)ZfuntextZtemr   r   r   fixdoc>  s    r      c                      s|   t tjfddtdt d t tj fdd t dt dt d	 tt t  t_ t_d
S )z9Install wrappers to always add 30 to the recursion limit.c                     s\   |rt dz
| \}W n& ty<   t dt|  dY n0 |dksNtd |t S )Nz.setrecursionlimit() takes no keyword argumentsz0setrecursionlimit() takes exactly one argument (z given)r   z/recursion limit must be greater or equal than 1)	TypeError
ValueErrorr0   __wrapped__RECURSIONLIMIT_DELTA)r,   rF   limit)setrecursionlimitr   r   r   H  s    
z:install_recursionlimit_wrappers.<locals>.setrecursionlimitz#            This IDLE wrapper adds z7 to prevent possible
            uninterruptible loops.c                      s      t S N)r   r   r   )getrecursionlimitr   r   r   ]  s    z:install_recursionlimit_wrappers.<locals>.getrecursionlimitz(            This IDLE wrapper subtracts z# to compensate
            for the z" IDLE adds when setting the limit.N)	functoolswrapsr   r   r   r   r   r   r   )r   r   r   install_recursionlimit_wrappersD  s    



r   c                   C   sF   t tjddrBt tjddrBtjjt_tjjt_tt t  dS )zUninstall the recursion limit wrappers from the sys module.

    IDLE only uses this for tests. Users can import run and call
    this to remove the wrapping.
    r   N)getattrr   r   r   r   r   r   r   r   r   !uninstall_recursionlimit_wrappersl  s    

r   c                   @   s   e Zd Zdd ZdS )rO   c                 C   s   z W n t y    Y n ty6   dat  Y nv   tj}tt	dd dt
 j d| d|d	|d tjd|d	 tt	d
d |d dat  Y n0 dS )zrOverride RPCServer method for IDLE

        Interrupt the MainThread and exit server if link is dropped.

        Tz
            z(----------------------------------------zU
            Unhandled exception in user code execution server!'
            Thread: z"
            IDLE Client Address: z
            Request: r(   i)r   r   a  
            *** Unrecoverable, server exiting!

            Users should never see this message; it is likely transient.
            If this recurs, report this with a copy of the message
            and an explanation of how to make it repeat.
            N)r?   EOFErrorr9   threadinterrupt_mainr   r4   r3   r   r   r5   Zcurrent_threadr+   rB   ro   r>   )selfrC   Zclient_addressZerfr   r   r   handle_error}  s8    zMyRPCServer.handle_errorN)r   
__module____qualname__r   r   r   r   r   rO   {  s   rO   c                   @   sB   e Zd ZdddZedd Zedd Zed	d
 Zdd ZdS )	StdioFileutf-8strictc                 C   s   || _ || _|| _|| _d S r   )shelltags	_encoding_errors)r   r   r   encodingerrorsr   r   r   __init__  s    zStdioFile.__init__c                 C   s   | j S r   )r   r   r   r   r   r     s    zStdioFile.encodingc                 C   s   | j S r   )r   r   r   r   r   r     s    zStdioFile.errorsc                 C   s
   d| j  S )Nz<%s>)r   r   r   r   r   r+     s    zStdioFile.namec                 C   s   dS NTr   r   r   r   r   isatty  s    zStdioFile.isattyN)r   r   )	r   r   r   r   propertyr   r   r+   r   r   r   r   r   r     s   



r   c                   @   s   e Zd Zdd Zdd ZdS )StdOutputFilec                 C   s   dS r   r   r   r   r   r   writable  s    zStdOutputFile.writablec                 C   s<   | j rtdt|| j| j| j| j}| j|| j	S )Nzwrite to closed file)
closedr   strencoder   r   decoder   r   r   )r   r   r   r   r   r     s    zStdOutputFile.writeN)r   r   r   r   r   r   r   r   r   r     s   r   c                   @   s4   e Zd ZdZdd ZdddZdddZd	d
 ZdS )StdInputFiler)   c                 C   s   dS r   r   r   r   r   r   readable  s    zStdInputFile.readabler'   c                 C   s   | j rtd|d u rd}nt|ts8tdt|j | j}d| _|dk rf| j	  }r||7 }qLn@t
||k r| j	 }|sq||7 }qf||d  | _|d | }|S )Nread from closed filer'   must be int, not r)   r   )r   r   
isinstancer2   r   rG   r   _line_bufferr   readliner0   )r   r   resultr   r   r   r   read  s$    


zStdInputFile.readc                 C   s   | j rtd|d u rd}nt|ts8tdt|j | jpF| j	 }|dk rXt
|}|dd|}|dkrv|d }||d  | _|d | S )Nr   r'   r   r   
r&   )r   r   r   r2   r   rG   r   r   r   r   r0   find)r   r   r   Zeolr   r   r   r     s    
zStdInputFile.readlinec                 C   s   | j   d S r   )r   closer   r   r   r   r     s    zStdInputFile.closeN)r'   )r'   )r   r   r   r   r   r   r   r   r   r   r   r   r     s
   

r   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 | }| d| | d| _t| jdtjtjt_	t
| jdtjtjt_t
| jdtjdt_tjt_ddl}|j|_tj	| _t  | d	| _tjj| dd
d dS )zOverride base methodry   consolestdinstdoutr   backslashreplacer   Ninterpr-   )Zmyseqwait)	Executiveregisterget_remote_proxyr   r   r   r   r   r   r   r   r   r   r   displayhookpydocZ
plainpagerZpagerZ_keep_stdinr   r   
RPCHandlerZgetresponse)r   Z	executiver   r   r   r   handle  s&    zMyHandler.handlec                 C   s   t 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  dS )zEOverride SocketIO method - terminate wait on callback and exit threadTNr>   r   r   r   r   r   r   EOFhook  s    zMyHandler.EOFhookc                 C   s   da t  dS )zinterrupt awakened threadTNr   r   r   r   r   decode_interrupthook  s    zMyHandler.decode_interrupthookN)r   r   r   r   r   r   r   r   r   r   r   rP     s   rP   c                   @   sN   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   s8   || _ tjdu r.tj| _t | _t	 | _ni | _d S )NF)
r|   idlelibZtesting__main____dict__localsr   ZCalltipr   ZAutoComplete)r   r|   r   r   r   r   %  s    

zExecutive.__init__c              
   C   s  z*d | _ dazt|| j W danda0 W n ty } zD|jrr|jd }t|td tfsrt	dt
| tjd W Y d }~nd }~0    t | _ trt  tjtju rt  n.ztj| j   W n   t | _ t  Y n0 | jjd}|r| jj  Y n0 t  d S )NTFr   zSystemExit: r(   z<<toggle-jit-stack-viewer>>)user_exc_infointerruptablery   r   r?   r,   r   rG   r2   r3   r   r   r   r@   r>   r:   
excepthook__excepthook__rA   r|   r   Zgetvarr   Zopen_remote_stack_viewerrr   )r   codeeobZjitr   r   r   runcode.  s4    
*

zExecutive.runcodec                 C   s   t rt  d S r   )r   r   r   r   r   r   r   interrupt_the_serverO  s    zExecutive.interrupt_the_serverc                 C   s   t | j|S r   )r   Zstart_debuggerr|   )r   Zgui_adap_oidr   r   r   start_the_debuggerS  s    zExecutive.start_the_debuggerc                 C   s   | j | dS )zDUnregister the Idb Adapter.  Link objects and Idb then subject to GCN)r|   Z
unregister)r   Zidb_adap_oidr   r   r   stop_the_debuggerV  s    zExecutive.stop_the_debuggerc                 C   s   | j |S r   )r   Z	fetch_tip)r   r+   r   r   r   get_the_calltipZ  s    zExecutive.get_the_calltipc                 C   s   | j ||S r   )r   Zfetch_completions)r   Zwhatmoder   r   r   get_the_completion_list]  s    z!Executive.get_the_completion_listNc                 C   sn   | j r| j \}}}nd S d }|d ur0| j|}|rL|jjd dv rL|j}q0|t_|t_t	
||}t|S )Nr   )r   run)r   r|   r   tb_frame	f_globalstb_nextr   rs   rt   r   ZStackTreeItemr   Zremote_object_tree_item)r   Z	flist_oidrh   rw   rI   Zflistitemr   r   r   r   `  s    zExecutive.stackviewer)N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   #  s   	!r   r   )rJ   zidlelib.idle_test.test_run   )	verbosity)N)NN)F)Ar   r   ior   r=   r   r   rM   rB   _threadr   r5   r   r   r   r   r   r   r   r   r   r   rX   hasattrmodulesmoddelattrr	   r7   eofr:   quit	NameErrorr   r   r   r!   ZTclr$   r%   r9   r>   r   rJ   r6   rQ   rA   re   rr   rY   r   r   r   r   Z	RPCServerrO   
TextIOBaser   r   r   r   rP   r   r   Zunittestr   r   r   r   <module>   s   


 


M$((/-M
