a
    ze6R                     @   sh  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y.   Y n0 dAddZ dBddZ!da"dd Z#e#d e$ Z%e%fddZ&da'da(da)dCddZ*dd Z+dd Z,dd  Z-d!d" Z.d#d$ Z/d%d& Zd'd( Z0d)d* Z1d+Z2d,d- Z3d.d/ Z4G d0d1 d1ej5Z6G d2d3 d3ej7Z8G d4d5 d5e8Z9G d6d7 d7e8Z:G d8d9 d9ej;Z<G d:d; d;Z=e>d<kr\d d=l?m*Z* e*d>d?d@ e#d dS )D    N)autocomplete)calltip)
debugger_r)
debugobj_r)iomenu)rpc)stackviewerzidlelib.runfirstrun)ZsimpledialogZ
messageboxfontZdialogZ
filedialogZcommondialog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 )Nz!
Warning (from warnings module):
z  File "%s", line %s
z    %s
z%s: %s
)	linecachegetlinestrip__name__)messagecategoryfilenamelinenolines r   !/usr/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 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 r   )_warnings_showwarningwarningsshowwarningr   )capturer   r   r   capture_warningsO   s    r$   Tc                 C   s   |  d d S )Nupdate)eval)tclr   r   r   handle_tk_events_   s    r(   c                 C   s  | a z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 qpY 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 qp ty$   trdaY qpY qp ty@   td
  Y qp   t \}	}
}zt  tj|d f W n&   tj|	|
|tjd t  Y n0 Y qpY qp0 qpd S )Nz/IDLE Subprocess: no IP port passed in sys.argv.r   T Z
SockThread)targetnameargs皙?)blocktimeoutF)no_exitfuncintr   argvprint
__stderr__r$   	threadingThreadmanage_socket	LOCALHOSTdaemonstartexit_nowexitKeyboardInterruptr   Zrequest_queuegetqueueEmptyZresponse_queueputr(   quitting
SystemExitexc_infoprint_exception	traceback)Zdel_exitfuncportZ
sockthreadrequestseqmethodr.   kwargsrettypevaluetbr   r   r   mainm   s^    


rR   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:    z, retrying....r*   z8IDLE Subprocess: Connection to IDLE GUI failed, exiting.T)rangetimesleepMyRPCServer	MyHandlerr   r5   r.   r   r6   show_socket_errorr=   Zhandle_request)addressiZservererrZsocket_errorr   r   r   r9      s&    



r9   c                 C   sl   dd l }ddlm} | }t| |  |dd|d  d|d  d| j d| j d		|d
 |  d S )Nr   )	showerrorzSubprocess Connection Errorz#IDLE's subprocess can't connect to :rT   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.messageboxr^   ZTkfix_scalingZwithdrawerrnostrerrorZdestroy)r]   r[   ra   r^   rootr   r   r   rZ      s    rZ   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__rO   __traceback__r5   __suppress_context__rH   
extract_tbcleanup_traceback
print_listformat_exception_only)	typexcrQ   contextcauseZtbeexcludelinesr   Zefile	print_excseenr   r   rx      s4    


z"print_exception.<locals>.print_exc)
r   
checkcacheflush_stdoutr   r   rF   	last_type
last_valuelast_tracebackset)r   rq   valrQ   excinfor   rw   r   rG      s    rG   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 )Nr   r)   z** IDLE Internal Exception: r*   exec?z
-toplevel-z	<pyshell#r   r   )countlenr5   r   r   r   Zobjecttable
rpchandlerrU   
startswithZ
remotecall)
rQ   ru   Zorig_tbZrpcfiler   r\   fnZlnZnmr   r   r   r   rn     s4    rn   c                   C   s   d S r   r   r   r   r   r   r{      s    r{   c                  C   s*   t rdd l} |   td td d S )Nr   F)r2   atexit_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 )	Nr   tkscalinggffffff?T)re   r-   existssizeg      )	Ztkinter.fontfloatr   callr
   namesZFontr3   round)re   ra   r   r-   r
   r   r   r   r   rb   2  s    rb   c                 C   s2   | j d ur| j d nd}|tt| | _ d S )Nz

r+   )__doc__textwrap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 )
Nc                     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
ValueErrorr   __wrapped__RECURSIONLIMIT_DELTA)r.   rM   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 r   )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.)	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 )Nr   )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 )rX   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 )NTz
            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.
            )rE   EOFErrorr=   threadinterrupt_mainr   r6   r5   r   r   r7   current_threadr-   rH   rx   rD   )selfrJ   Zclient_addresserfr   r   r   handle_error}  s8    zMyRPCServer.handle_errorN)r   
__module____qualname__r   r   r   r   r   rX   {  s   rX   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   
isinstancer3   r   rO   r   _line_bufferr   readliner   )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   
rT   )r   r   r   r3   r   rO   r   r   r   r   r   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 )
rY   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 )Nr   consolestdinstdoutr   backslashreplacer   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 )N
   )rV   rW   r   r   r   r   exithook  s    zMyHandler.exithookc                 C   s   da t  d S r   rD   r   r   r   r   r   r   EOFhook  s    zMyHandler.EOFhookc                 C   s   da t  d S r   r   r   r   r   r   decode_interrupthook  s    zMyHandler.decode_interrupthookN)r   r   r   r   r   r   r   r   r   r   r   rY     s   rY   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interruptabler   r   rE   r.   r   rO   r3   r5   r   r   r   rF   rD   r>   
excepthook__excepthook__rG   r   r   Zgetvarr   Zopen_remote_stack_viewerr{   )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 r   )r   
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   r|   r}   r   ZStackTreeItemr   Zremote_object_tree_item)r   Z	flist_oidrq   r   rQ   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   )rR   zidlelib.idle_test.test_run   )	verbosity)N)NN)F)@r   ior   rA   r   r   rV   rH   _threadr   r7   r!   r   r   r   r   r   r   r   r   r   ra   hasattrmodulesmoddelattrr	   r:   eofr>   quit	NameErrorr   r   r    r$   ZTclr'   r(   r=   rD   r   rR   r9   rZ   rG   rn   r{   rb   r   r   r   r   Z	RPCServerrX   
TextIOBaser   r   r   r   rY   r   r   Zunittestr   r   r   r   <module>   s   


 


M$((/-M
