B
    ߜwa              
   @   s  d dl Z edkr e jd e jd< yd dlT W n* ek
rV   ede jd edY nX e jdkry d dl	Z	dZ
e	d	e
 W n eeefk
r   Y nX d dlmZ ed
k re at  d dlmZ et ejdde td edd dlmZ d dlZd dlZd dlZd dlmZ d dlZd dl Z d dl!Z!d dl"m#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/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl,m6Z6 d dlm7Z7m8Z8m9Z9 d dl:m;Z; dZ<d Z=e ja>d:ddZ?da@dd  ZAeAd! dejBfd"d#ZCeCe_BG d$d% d%e0ZDG d&d' d'e3ZEG d(d) d)e)ZFG d*d+ d+e;ZGG d,d- d-e6jHZId.d/ ZJG d0d1 d1eZKG d2d3 d3e5ZLd4d5 ZMd6ZNd7d8 ZOedkreO  eAd9 dS );    N__main__zidlelib.pyshell)*zJ** IDLE can't import Tkinter.
Your Python may not be configured for Tk. **)file   Zwin32Zshcoreg      !@)fix_scalingzIdle Cannot Startz"Idle requires tcl/tk 8.5+, not %s.)parent)InteractiveInterpreter)python_version)TextWrapper)ColorDelegator)idleConf)debugger)
debugger_r)EditorWindowfixwordbreaks)FileList)OutputWindow)rpc)idle_formatwarningStdInputFileStdOutputFile)UndoDelegatorz	127.0.0.1c          	   C   sP   |dkrt }y&|t| ||||d |d W n ttfk
rJ   Y nX dS )a   Show Idle-format warning (after replacing warnings.showwarning).

    The differences are the formatter called, the file=None replacement,
    which can be None, the capture of the consequence AttributeError,
    and the output of a hard-coded prompt.
    N)linez>>> )warning_streamwriter   AttributeErrorOSError)messagecategoryfilenamelinenor   r    r!    lib/python3.7/idlelib/pyshell.pyidle_showwarningE   s    r#   c             C   s0   | rt dkr,tja tt_nt dk	r,t t_da dS )z>Replace warning.showwarning with idle_showwarning, or reverse.N)_warnings_showwarningwarningsshowwarningr#   )Zcapturer!   r!   r"   capture_warningsX   s    r'   Tc             C   s\   t j}i }x:t|D ].}|dd |dd  dkr||||< qW ||  || dS )ao  Extend linecache.checkcache to preserve the <pyshell#...> entries

    Rather than repeating the linecache code, patch it to save the
    <pyshell#...> entries, call the original linecache.checkcache()
    (skipping them), and then restore the saved entries.

    orig_checkcache is bound at definition time to the original
    method, allowing it to be patched.
    Nr   z<>)	linecachecachelistpopupdate)r   Zorig_checkcacher*   savekeyr!   r!   r"   extended_linecache_checkcacheg   s    r0   c               @   s~   e Zd ZdZdd Zdddddd	gZd ddZdd Zd!ddZd"ddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )#PyShellEditorWindowz6Regular text edit window in IDLE, supports breakpointsc             G   s   g | _ tj| f|  | jd| j | jd| j | jd| jj t	j
tjd| _| jj| fdd}| j| | jjr|   |   d S )Nz<<set-breakpoint-here>>z<<clear-breakpoint-here>>z<<open-python-shell>>zbreakpoints.lstc             S   s   |   |   d S )N)restore_file_breaks)Zold_hookselfr!   r!   r"   filename_changed_hook   s    z;PyShellEditorWindow.__init__.<locals>.filename_changed_hook)breakpointsr   __init__textbindset_breakpoint_hereclear_breakpoint_hereflist
open_shellospathjoinr   ZuserdirbreakpointPathioZfilename_change_hookZset_filename_change_hookr   r2   color_breakpoint_text)r3   argsr4   r!   r!   r"   r6      s    zPyShellEditorWindow.__init__)ZCutz<<cut>>rmenu_check_cut)ZCopyz<<copy>>Zrmenu_check_copy)ZPastez	<<paste>>rmenu_check_paste)NNN)zSet Breakpointz<<set-breakpoint-here>>N)zClear Breakpointz<<clear-breakpoint-here>>NTc             C   sD   | j dkrdS |r(t }t|d}n
ddd}| jd| dS )z,Turn colorizing of breakpoint text on or offNbreak )
foreground
backgroundBREAK)rA   r   CurrentThemeGetHighlightr7   Z
tag_config)r3   ZcolorthemeZcfgr!   r!   r"   rB      s    

z)PyShellEditorWindow.color_breakpoint_textc             C   s   | j }| jj}|dd| d|d   y| j| W n  tk
rX   | j| Y nX y| jj	j
j}||| W n   Y nX d S )NrJ   z%d.0r   )r7   rA   r   tag_addr5   index
ValueErrorappendr;   pyshellinterpr   r9   )r3   r    r7   r   debugr!   r!   r"   set_breakpoint   s    z"PyShellEditorWindow.set_breakpointNc             C   s>   | j }| jj}|s|  d S tt|d}| | d S )Ninsert)r7   rA   r   bellintfloatrO   rU   )r3   eventr7   r   r    r!   r!   r"   r9      s    z'PyShellEditorWindow.set_breakpoint_herec             C   s   | j }| jj}|s|  d S tt|d}y| j| W n   Y nX |	ddd y| j
jjj}||| W n   Y nX d S )NrV   rJ   zinsert linestartzinsert lineend +1char)r7   rA   r   rW   rX   rY   rO   r5   remove
tag_remover;   rR   rS   r   r:   )r3   rZ   r7   r   r    rT   r!   r!   r"   r:      s"    z)PyShellEditorWindow.clear_breakpoint_herec             C   sd   | j r`| j}| jj}|s$|  d S g | _ |ddt y| jjj	j
}|| W n   Y nX d S )NrJ   z1.0)r5   r7   rA   r   rW   r\   ZENDr;   rR   rS   r   clear_file_breaks)r3   r7   r   rT   r!   r!   r"   r]      s    z%PyShellEditorWindow.clear_file_breaksc          
   C   s  | j }| jj}y$t| jd}| }W dQ R X W n tk
rJ   g }Y nX ynt| jdX}x$|D ]}||d sb|| qbW | 	  | j }|r||d t
| d  W dQ R X W nT tk
r } z4t| jddsd| j_tjd	d
t
| | jd W dd}~X Y nX dS )z#Save breakpoints when file is savedrNw=
breakpoint_error_displayedFTz
IDLE Errorz$Unable to update breakpoint list:
%s)titler   r   )r5   rA   r   openr@   	readlinesr   
startswithr   update_breakpointsstrgetattrrootrb   tkMessageBox	showerrorr7   )r3   Zbreaksr   fplinesZnew_filer   errr!   r!   r"   store_file_breaks   s.    

(z%PyShellEditorWindow.store_file_breaksc          	   C   s   | j   | jd krd S | jj}|d kr,d S tj| jrt| jd}|	 }W d Q R X xJ|D ]B}|
|d r`t|t|d d  }x|D ]}| | qW q`W d S )Nr^   r`   r   )r7   r-   rA   r   r=   r>   isfiler@   rd   re   rf   evallenrU   )r3   r   rm   rn   r   Zbreakpoint_linenumbersZbreakpoint_linenumberr!   r!   r"   r2     s    



z'PyShellEditorWindow.restore_file_breaksc             C   s$   | j }|d}| |}|| _dS )z3Retrieves all the breakpoints in the current windowrJ   N)r7   Z
tag_rangesranges_to_linenumbersr5   )r3   r7   rangesZlinenumber_listr!   r!   r"   rg     s    

z&PyShellEditorWindow.update_breakpointsc             C   sj   g }x`t dt|dD ]L}tt|| j}tt||d  j}x||k r`|| |d7 }qDW qW |S )Nr      r   )rangers   rX   rY   stringrQ   )r3   ru   rn   rO   r    endr!   r!   r"   rt   %  s    

z)PyShellEditorWindow.ranges_to_linenumbersc             C   s   |    t|  dS )z7Extend base method - clear breaks when module is closedN)r]   r   _close)r3   r!   r!   r"   rz   6  s    zPyShellEditorWindow._close)T)N)N)__name__
__module____qualname____doc__r6   rmenu_specsrB   rU   r9   r:   r]   rp   r2   rg   rt   rz   r!   r!   r!   r"   r1   ~   s$   

	
*r1   c               @   s"   e Zd ZdZeZdZdddZdS )PyShellFileListz8Extend base class: IDLE supports a shell and breakpointsNc             C   s8   | j r| j j  nt| | _ | j r2| j  s2d S | j S )N)rR   topwakeupPyShellbegin)r3   rZ   r!   r!   r"   r<   E  s    

zPyShellFileList.open_shell)N)r{   r|   r}   r~   r1   r   rR   r<   r!   r!   r!   r"   r   <  s   r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ModifiedColorDelegatorz8Extend base class: colorizer for the shell window itselfc             C   s   t |  |   d S )N)r   r6   LoadTagDefs)r3   r!   r!   r"   r6   S  s    
zModifiedColorDelegator.__init__c             C   s*   |  ddd | ddd t|  d S )NZTODOz1.0iomarkZSYNC)r\   rN   r   recolorize_main)r3   r!   r!   r"   r   W  s    z&ModifiedColorDelegator.recolorize_mainc          	   C   sJ   t |  t }| jd d dt|dt|dt|dd d S )N)rI   rH   stdoutstderrconsole)stdinr   r   r   )r   r   r   rK   tagdefsr-   rL   )r3   rM   r!   r!   r"   r   \  s    


z"ModifiedColorDelegator.LoadTagDefsc             C   s"   x| j D ]}| |dd qW d S )Nr   ry   )r   r\   )r3   tagr!   r!   r"   removecolorsf  s    z#ModifiedColorDelegator.removecolorsN)r{   r|   r}   r~   r6   r   r   r   r!   r!   r!   r"   r   P  s
   
r   c               @   s$   e Zd ZdZdddZdddZdS )	ModifiedUndoDelegatorz;Extend base class: forbid insert/delete before the I/O markNc             C   sL   y"| j |ddr | j   d S W n tk
r6   Y nX t| ||| d S )N<r   )delegatecomparerW   TclErrorr   rV   )r3   rO   charstagsr!   r!   r"   rV   n  s    
zModifiedUndoDelegator.insertc             C   sJ   y"| j |ddr | j   d S W n tk
r6   Y nX t| || d S )Nr   r   )r   r   rW   r   r   delete)r3   Zindex1Zindex2r!   r!   r"   r   w  s    
zModifiedUndoDelegator.delete)N)N)r{   r|   r}   r~   rV   r   r!   r!   r!   r"   r   k  s   
	r   c               @   s   e Zd Zdd ZdS )MyRPCClientc             C   s   t dS )z0Override the base class - just re-raise EOFErrorN)EOFError)r3   r!   r!   r"   
handle_EOF  s    zMyRPCClient.handle_EOFN)r{   r|   r}   r   r!   r!   r!   r"   r     s   r   c             C   s^   d|pd d}| t |krNt| t | d\}}|| d  | |d  S |dd S dS )zReturn width long restart line formatted with filename.

    Fill line with balanced '='s, with any extras and at least one at
    the beginning.  Do not end with a trailing space.
    z= RESTART: ZShellz =rv   r`   N)rs   divmod)widthr   r   Zdivmodr!   r!   r"   restart_line  s
    r   c               @   s  e Zd Zdd ZdZdZdZdd Zdd Zdd	 Z	d?ddZ
dd Zdd Zdd Zdd Zd@ddZdZdd ZdZdd Zdd Zdd Zd d! Zd"Zd#d$ ZdAd%d&Zd'd( Zd)d* Zd+d, ZdBd-d.Zd/d0 Zd1d2 Zd3d4 Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$d=d> Z%dS )CModifiedInterpreterc             C   sB   || _ tjd j}tj| |d d| _d | _t| _	| j
jj| _d S )Nr   )localsF)	tkconsolesysmodules__dict__r   r6   
restartingsubprocess_arglistPORTportcompilecompilerflagsoriginal_compiler_flags)r3   r   r   r!   r!   r"   r6     s    zModifiedInterpreter.__init__Nc             C   s&   | j d kr|  | _ t| j | _d S )N)r   build_subprocess_arglist
subprocessPopen
rpcsubproc)r3   r!   r!   r"   spawn_subprocess  s    

z$ModifiedInterpreter.spawn_subprocessc             C   s\   | j dkstddd tjD }tjddddd	d
}d|f }tjg| d|t| j g S )Nr   z/Socket should have been assigned a port number.c             S   s   g | ]}d | qS )z-Wr!   ).0sr!   r!   r"   
<listcomp>  s    z@ModifiedInterpreter.build_subprocess_arglist.<locals>.<listcomp>mainGeneralzdelete-exitfuncFbool)defaulttypez&__import__('idlelib.run').run.main(%r)z-c)r   AssertionErrorr   warnoptionsr   	GetOption
executablerh   )r3   r_   Z	del_exitfZcommandr!   r!   r"   r     s    


z,ModifiedInterpreter.build_subprocess_arglistc          	   C   sZ  t | jf}xLtdD ]4}t| yt|| _P W q tk
rF   Y qX qW |   d S | jj	
 d | _tdkr| jj	tjtjd |   | jj	d y| j  W n tjk
r   |   d S X | jd| j | jd| jj | jd| jj | jd| jj | jd	| jj | jd
t | jd|  | jdd |   | jS )N   r   r   
   r   r   r   r   r;   r)   rS   T)with_cwd)HOSTr   rw   timeZsleepr   rpccltr   display_port_binding_errorlistening_sockZgetsocknamer   Z
setsockoptsocketZ
SOL_SOCKETZSO_REUSEADDRr   Z
settimeoutaccepttimeoutdisplay_no_subprocess_errorregisterr   r   r   r   r;   r)   transfer_pathpoll_subprocess)r3   Zaddrir!   r!   r"   start_subprocess  s>    




z$ModifiedInterpreter.start_subprocessFrG   c             C   s0  | j r| jS d| _ |  }|r<yt| j W n   Y nX | j  |   | j}|j}d|_| 	  y| j
  W n tjk
r   |   d S X | j|d |  |jdd |d |t|j| |jdd |jdd | s|  |rt| j |  | j| jj_d| _ | jS )	NTF)r   r   zend-1cra   restartleft)r   r   getdebuggerr   Zclose_subprocess_debuggercloseterminate_subprocessr   	executingr   r   r   r   r   r   stop_readliner7   r   r   r   r   mark_setmark_gravity
showpromptZrestart_subprocess_debuggerload_breakpointsr   r   r   r   )r3   r   r   rT   r   Zwas_executingr!   r!   r"   restart_subprocess  sF    

z&ModifiedInterpreter.restart_subprocessc             C   s   | j dddi  d S )NexecZinterrupt_the_serverr!   )r   Z
remotecall)r3   r!   r!   r"   Z__request_interrupt  s    z'ModifiedInterpreter.__request_interruptc             C   s   t j| jd  d S )N)target)	threadingZThread'_ModifiedInterpreter__request_interruptstart)r3   r!   r!   r"   interrupt_subprocess  s    z(ModifiedInterpreter.interrupt_subprocessc             C   s~   | j d k	r| jj| j  y| jj  W n tk
r>   Y nX y| j  W n tk
rb   Y nX |   d| j_	d | _d S )NF)
_afteridr   r7   Zafter_cancelr   r   r   r   r   r   )r3   r!   r!   r"   kill_subprocess  s    
z#ModifiedInterpreter.kill_subprocessc             C   sL   y| j   W n tk
r"   dS X y| j   W n tk
rF   dS X dS )z"Make sure subprocess is terminatedN)r   killr   wait)r3   r!   r!   r"   r   !  s    z(ModifiedInterpreter.terminate_subprocessc             C   s2   |rdg}| tj ntj}| d|f  d S )NrG   zRif 1:
        import sys as _sys
        _sys.path = %r
        del _sys
        
)extendr   r>   
runcommand)r3   r   r>   r!   r!   r"   r   .  s    z!ModifiedInterpreter.transfer_pathc          
   C   s6  | j }|d krd S y|j| jdd}W n2 tttfk
rX   | jjrHd S d }|   Y nX |r| j	  d | _|\}}| jj
}|dkr|d k	rtt||d nH|dkr| jdr|   n*|dkrd}t||tjd t|||d y| j  W n tk
r   Y nX | jjs2| jj| jj| j| _d S )	Ng?)r   ZOK)r   Z	EXCEPTIONz<<toggle-jit-stack-viewer>>ERRORz/pyshell.ModifiedInterpreter: Subprocess ERROR:
)r   Zpollresponse
active_seqr   r   KeyboardInterruptr   closingr   resetoutputr   printreprgetvarremote_stack_viewerr   
__stderr__endexecutingr   r7   afterpollintervalr   r   )r3   ZcltresponseZhowZwhatr   errmsgr!   r!   r"   r   =  s@    


z#ModifiedInterpreter.poll_subprocessc             C   s
   || _ d S )N)r   )r3   r   r!   r!   r"   setdebuggere  s    zModifiedInterpreter.setdebuggerc             C   s   | j S )N)r   )r3   r!   r!   r"   r   h  s    zModifiedInterpreter.getdebuggerc             C   s   | j jd| j dS )a  Initiate the remote stack viewer from a separate thread.

        This method is called from the subprocess, and by returning from this
        method we allow the subprocess to unblock.  After a bit the shell
        requests the subprocess to open the remote stack viewer which returns a
        static object looking at the last exception.  It is queried through
        the RPC mechanism.

        i,  N)r   r7   r   r   )r3   r!   r!   r"   open_remote_stack_viewerk  s    
z,ModifiedInterpreter.open_remote_stack_viewerc             C   s   ddl m} | jdddi }|d kr6| jj  d S || j|}ddlm	}m
} t| jj}t }t|dd }|||dd	}	|	jjd
dd ||	jd |}
|
  d S )Nr   )
debugobj_rr   Zstackviewer)r;   )ScrolledCanvasTreeNodeZnormalrI   )ZbgZhighlightthicknessr   Zboth)expandZfill)idlelibr   r   remotequeuer   rj   rW   ZStubObjectTreeItemZidlelib.treer   r   ZToplevelr   rK   rL   frameZpackZcanvasr   )r3   r   Zoiditemr   r   r   rM   rI   ZscZnoder!   r!   r"   r   x  s    z'ModifiedInterpreter.remote_stack_viewerr   c             C   s   |  |}| || dS )z1Like runsource() but assumes complete exec sourceN)stuffsourceexecfile)r3   sourcer   r!   r!   r"   
execsource  s    
zModifiedInterpreter.execsourcec          	   C   s   |dkrFt |.}| }tr<dtj| d| d }W dQ R X yt||d}W nH tt	fk
r   | j
  td| j
jd t| | | j
  Y nX | | dS )zExecute an existing fileNz__file__ = r'''z'''
z
del __file__r   zB*** Error in script or command!
Traceback (most recent call last):)r   )tokenizerd   readuse_subprocessr=   r>   abspathr   OverflowErrorSyntaxErrorr   r   r   r   r   showsyntaxerrorr   runcode)r3   r   r  rm   coder!   r!   r"   r    s    &
zModifiedInterpreter.execfilec             C   s&   |  |}t|tstt| ||S )zBExtend base class method: Stuff the source in the line cache first)r  
isinstancerh   r   r   	runsource)r3   r  r   r!   r!   r"   r    s    
zModifiedInterpreter.runsourcec             C   s>   d| j  }| j d | _ |d}t|d d||ftj|< |S )z"Stuff source in the filename cachez<pyshell#%d>r   ra   r   )gidsplitrs   r)   r*   )r3   r  r   rn   r!   r!   r"   r    s
    

zModifiedInterpreter.stuffsourcec             C   s   |  d|f  dS )z>Prepend sys.path with file's directory if not already includeda(  if 1:
            _filename = %r
            import sys as _sys
            from os.path import dirname as _dirname
            _dir = _dirname(_filename)
            if not _dir in _sys.path:
                _sys.path.insert(0, _dir)
            del _filename, _sys, _dirname, _dir
            
N)r   )r3   r   r!   r!   r"   prepend_syspath  s    z#ModifiedInterpreter.prepend_syspathc             C   s   | j }|j}|ddd t \}}}t|ddp:|p:d}t|ddpJd}t|d	dpZd
}	|	d
krl|d7 }|dkrd|	d  }
nd|d |	d f }
|||
 |  | d|  |	  dS )zOverride Interactive Interpreter method: Use Colorizing

        Color the offending position instead of printing it and pointing at it
        with a caret.

        r   z1.0ry   msgrG   z<no detail available>r    r   offsetr   ziomark + %d charsz&iomark linestart + %d lines + %d charszSyntaxError: %s
N)
r   r7   r\   r   exc_infori   Zcolorize_syntax_errorr   r   r   )r3   r   r   r7   r   valuetbr  r    r  posr!   r!   r"   r    s"    z#ModifiedInterpreter.showsyntaxerrorc             C   s6   | j   |   t|  | j dr2| j   dS )z1Extend base class method to reset output properlyz<<toggle-jit-stack-viewer>>N)r   r   checklinecacher   showtracebackr   open_stack_viewer)r3   r!   r!   r"   r    s
    

z!ModifiedInterpreter.showtracebackc             C   sB   t j}x6t| D ]&}|d d |dd   dkr||= qW d S )Nr   r(   z<>)r)   r*   r+   keys)r3   cr/   r!   r!   r"   r    s    z"ModifiedInterpreter.checklinecachec             C   s@   | j jr|   dS | jr0| jdd|fi  nt|| j dS )z*Run the code without invoking the debuggerr   r   r  r   )r   r   display_executing_dialogr   r   r   r   )r3   r  r!   r!   r"   r     s    zModifiedInterpreter.runcommandc             C   sN  | j jr| j  |   | j}zyT| j   |sT| jdk	rT| jdd|fi | _	n |rh|
|| j nt|| j W n tk
r   | j jstjddd| j jdr q|   n Y n`   trtd| j jd	 |   | j   n,| j jr
d
| j _td| j jd	 n|   Y nX W dtsHy| j   W n tk
rF   Y nX X dS )zOverride base class methodNr   r  zExit?zDo you want to exit altogether?Zyes)r   r   z IDLE internal error in runcode())r   Fr   )r   r   rS   r   r  r   beginexecutingr   Z
asyncqueuer   runr   r   
SystemExitr   rk   Zaskyesnor7   r  r  r   r   r   canceledr   )r3   r  r   r!   r!   r"   r    sJ    




zModifiedInterpreter.runcodec             C   s   | j j|S )zOverride base class method)r   r   r   )r3   r   r!   r!   r"   r   #  s    zModifiedInterpreter.writec             C   s   t jdd| jjd d S )NzPort Binding ErroraA  IDLE can't bind to a TCP/IP port, which is necessary to communicate with its Python execution server.  This might be because no networking is installed on this computer.  Run IDLE with the -n command line switch to start without a subprocess and refer to Help/IDLE Help 'Running without a subprocess' for further details.)r   )rk   rl   r   r7   )r3   r!   r!   r"   r   '  s    z.ModifiedInterpreter.display_port_binding_errorc             C   s   t jdd| jjd d S )NzSubprocess Connection ErrorzIDLE's subprocess didn't make connection.
See the 'Startup failure' section of the IDLE doc, online at
https://docs.python.org/3/library/idle.html#startup-failure)r   )rk   rl   r   r7   )r3   r!   r!   r"   r   2  s    z/ModifiedInterpreter.display_no_subprocess_errorc             C   s   t jdd| jjd d S )NzAlready executingzYThe Python Shell window is already executing a command; please wait until it is finished.)r   )rk   rl   r   r7   )r3   r!   r!   r"   r  :  s    z,ModifiedInterpreter.display_executing_dialog)FrG   )F)N)N)&r{   r|   r}   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r   r  r   r   r   r  r!   r!   r!   r"   r     sF   	+
,
&
	
.r   c                   s  e Zd Zde  d ZeZeZddddddgZ	e
jd	g Zd
ZddlmZ dTddZdd Zd
Zd
Zd
Zd
Zd
Zd
Zdd Zdd ZdUddZdd ZdVd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+ Z#d,Z$d-d. Z%d/d0 Z&d1d2 Z'd3d4 Z(dWd5d6Z)d7d8 Z*d9d: Z+d;d< Z,d=d> Z-d?d@ Z.dXdAdBZ/dYdCdDZ0dZdEdFZ1dGdH Z2dIdJ Z3dKdL Z4d[dNdOZ5 fdPdQZ6 fdRdSZ7  Z8S )\r   zPython z Shell)r   Z_File)ZeditZ_Edit)rT   Z_Debug)ZoptionsZ_Options)ZwindowZ_Window)helpZ_Help)ZSqueezez<<squeeze-current-text>>Fr   )HistoryNc             C   s$  t r&| j}|d d dkr&|dd t| | _|d krVt }t| |  t|}t	
| |d d  d| _d| _ttdrtjnd| _| jd	d
 | _| j| _| j}|jdd |d| j |d| j |d| j |d| j |d| j |d| j |d| j t r8|d| j |d| j |  | }|d|j! tj"| _#tj$| _%tj&| _'ddl(m)} t*| d|j+|j,| _&t-| d|j+|j,| _"t-| d|j+d| _$t-| d|j+|j,| _.t s| j"t_"| j$t_$| j&t_&ydd l/}|j0|_1W n   tj2t_$ Y nX | 3| j| _4d| _5d S )Nrv   r   shell)r%  ZShe_llT   ps1z>>> ra   r(   char)wrapz<<newline-and-indent>>z<<plain-newline-and-indent>>z<<interrupt-execution>>z<<end-of-file>>z<<open-stack-viewer>>z<<toggle-debugger>>z<<toggle-jit-stack-viewer>>z<<view-restart>>z<<restart-shell>>z<<squeeze-current-text>>)iomenur   r   r   backslashreplacer   2   )6r  
menu_specsrV   r   rS   Tkr   withdrawr   r   r6   ZusetabsZindentwidthhasattrr   r'  sys_ps1r  Zprompt_last_linepromptr7   Z	configurer8   enter_callbacklinefeed_callbackcancel_callbackeof_callbackr  toggle_debuggertoggle_jit_stack_viewerview_restart_markrestart_shellZSqueezerZsqueeze_current_text_eventr   save_stdoutr   save_stderrr   
save_stdinr   r*  r   encodingerrorsr   r   pydocZ
plainpagerZpagerr   r$  historyr   )r3   r;   Zmsrj   r7   Zsqueezerr*  r@  r!   r!   r"   r6   ^  sn    

zPyShell.__init__c             C   s   t jddS )NT)Z
shell_only)r   ZGetExtensions)r3   r!   r!   r"   get_standard_extension_names  s    z$PyShell.get_standard_extension_namesc             C   s   |a d S )N)r   )r3   streamr!   r!   r"   set_warning_stream  s    zPyShell.set_warning_streamc             C   s   t S )N)r   )r3   r!   r!   r"   get_warning_stream  s    zPyShell.get_warning_streamc             C   sH   | j r$tjdd| jd |   dS | j }|r<|   n|   d S )NzDon't debug nowz*You can only toggle the debugger when idle)r   rF   )	r   rk   rl   r7   set_debugger_indicatorrS   r   close_debuggeropen_debugger)r3   rZ   dbr!   r!   r"   r7    s    


zPyShell.toggle_debuggerc             C   s   | j  }| d|   d S )Nz<<toggle-debugger>>)rS   r   Zsetvar)r3   rI  r!   r!   r"   rF    s    
zPyShell.set_debugger_indicatorc             C   s   d S )Nr!   )r3   rZ   r!   r!   r"   r8    s    zPyShell.toggle_jit_stack_viewerc             C   sh   | j  }|r\| j d  |  | j jr8t| j j |   | j	d | j
| _|   |   d S )Nz[DEBUG OFF]
)rS   r   r   r   r   r   Zclose_remote_debuggerr   r   r   r1  r2  r   rF  )r3   rI  r!   r!   r"   rG    s    
zPyShell.close_debuggerc             C   sX   | j jrt| j j| }n
t| }| j | |  d| j | _	| 
  |   d S )Nz[DEBUG ON]
)rS   r   r   Zstart_remote_debuggerr   ZDebuggerr   r   r1  r2  r   rF  )r3   Zdbg_guir!   r!   r"   rH    s    

zPyShell.open_debuggerc             C   s   |    d| _dS )zHelper for ModifiedInterpreterTN)r   r   )r3   r!   r!   r"   r    s    zPyShell.beginexecutingc             C   s   d| _ d| _|   dS )zHelper for ModifiedInterpreterFN)r   r"  r   )r3   r!   r!   r"   r     s    zPyShell.endexecutingc             C   sD   | j r&tjddd| jd}|dkr&dS |   d| _d| _t| S )zExtend EditorWindow.close()zKill?z7Your program is still running!
 Do you want to kill it?ok)r   r   FZcancelT)	r   rk   Zaskokcancelr7   r   r"  r   r   r   )r3   r   r!   r!   r"   r     s    
zPyShell.closec             C   sV   |    tr| j  | jt_| jt_| j	t_
d| _d| _d| j_d| _t|  dS )zEExtend EditorWindow._close(), shut down debugger and execution serverN)rG  r  rS   r   r;  r   r   r<  r   r=  r   r   r;   rR   rA  r   rz   )r3   r!   r!   r"   rz     s    
zPyShell._closec             C   s   dS )z8Override EditorWindow method: never remove the colorizerTr!   )r3   r   r!   r!   r"   ispythonsource
  s    zPyShell.ispythonsourcec             C   s   | j S )N)shell_title)r3   r!   r!   r"   short_title  s    zPyShell.short_titlezHType "help", "copyright", "credits" or "license()" for more information.c             C   s   | j dd |   tr:d}| j }|sF|   dS nd}tjt	_| 
dt	jt	j| j|f  | j   |   dd l}d |_dS )	Nr   rV   rG   Fz==== No Subprocess ====

WARNING: Running IDLE without a Subprocess is deprecated
and will be removed in a later version. See Help/IDLE Help
for details.

zPython %s on %s
%s
%sr   T)r7   r   r   r  rS   r   r   r   displayhookr   r   versionplatform	COPYRIGHTZfocus_forcer   tkinterZ_default_root)r3   ZnosubZclientrR  r!   r!   r"   r     s"    

zPyShell.beginc             C   s   | j s
d S d| _| j  d S )NT)reading_stop_readline_flagr   quit)r3   r!   r!   r"   r   ,  s    zPyShell.stop_readlinec             C   s   | j }zd| _ | j  W d || _ X | jr4d| _dS | jdd}t|dkrRd}|   | jrnd| _t	snt
| jr~d| _d}|S )NTFrG   r   zend-1cr   ra   )rS  r   mainlooprT  r7   getrs   r   r"  r  r   	endoffile)r3   r.   r   r!   r!   r"   readline2  s(    zPyShell.readlinec             C   s   dS )NTr!   )r3   r!   r!   r"   isattyI  s    zPyShell.isattyc             C   s   y| j dddrd S W n   Y nX | jsR| jsR|   | jd |   dS d| _d| _	| jr| jj
r| j r| j  n
| j  | jr| j  dS )Nz	sel.firstz!=zsel.lastzKeyboardInterrupt
rF   FT)r7   r   r   rS  r   rS   r   r   rX  r"  r   r   r   r   r   rU  )r3   rZ   r!   r!   r"   r5  L  s&    


zPyShell.cancel_callbackc             C   sf   | j r| jsd S | jdddr0| jddds4d S | j sL|   |   nd| _d| _| j	  dS )Nr   z==rV   zend-1cFTrF   )
r   rS  r7   r   r   r   r"  rX  r   rU  )r3   rZ   r!   r!   r"   r6  b  s    

zPyShell.eof_callbackc             C   s0   | j r"| jdd | jd n
| | dS )NrV   ra   rF   )rS  r7   rV   seenewline_and_indent_event)r3   rZ   r!   r!   r"   r4  q  s
    
zPyShell.linefeed_callbackc             C   s8  | j r| jsd S y6| jdd}|rD| jdddrD| || dS W n   Y nX | jdddrV| jd	d}|r| jdd|d
 r| | j|d |d
 | dS | jd	d}|r| jdd|d r| | j|d |d
 | dS | jdd}|r<| j|d ddr<| | j|d
 d| n| | jdd| dS | jdddrv| jdd | jdd}|r|	 s| j
dd | jdddr| | dS | jdd | jr| jdd | jd n
| | | jd	dd | j  | jr,| j  n|   dS )Nz	sel.firstzsel.lastz<=r   rF   rV   r   ziomark linestartr   r   r   zinsert lineendz>=r   zinsert linestartzend-1czend-1c linestartra   )r   rS  r7   rW  r   recallZtag_prevrangeZtag_nextranger   stripr   r\  rV   r[  rN   Zupdate_idletasksr   rU  runit)r3   rZ   selprevnextindicesr   r!   r!   r"   r3  z  sX    


zPyShell.enter_callbackc          	   C   s<  t dd|}t dd|}|d}| j  z| jddd | jdd	 | jd
d}| 	dr| 
| | jd
d}| jd|d   t|dkrt d|d d}t d|d}xJ|dd  D ]:}||r||t|d   }| jdd|   qW W d | jd | j  X d S )Nz^\s*\nrG   z\n\s*$ra   r`  z1.0ry   rV   zend-1czinsert linestart:r   r   z	^([ \t]*))resubr  r7   Zundo_block_startr\   r   rW  rstripendswithr\  rV   r^  rs   searchgrouprf   r[  Zundo_block_stop)r3   r   rZ   rn   prefixZorig_base_indentZnew_base_indentr   r!   r!   r"   r]    s*    


zPyShell.recallc             C   s   | j dd}t|}x$|dkr:||d  dkr:|d }qW |dkr\||d  dkr\|d }x$|dkr||d  dkr|d }q^W |d | }| j| d S )Nr   zend-1cr   r   z 	ra   )r7   rW  rs   rS   r  )r3   r   r   r!   r!   r"   r_    s    zPyShell.runitc             C   sX   | j jr| j  S y
tj W n   tjdd| jd d S ddlm	} || j
| j d S )NzNo stack tracez@There is no stack trace yet.
(sys.last_traceback is not defined))r   r   )StackBrowser)rS   r   r   r   last_tracebackrk   rl   r7   Zidlelib.stackviewerrl  rj   r;   )r3   rZ   rl  r!   r!   r"   r    s    


zPyShell.open_stack_viewerc             C   s   | j d | j d d S )Nr   r   )r7   r[  )r3   rZ   r!   r!   r"   r9    s    zPyShell.view_restart_markc             C   s   | j jdd dS )z&Callback for Run/Restart Shell Cntl-F6T)r   N)rS   r   )r3   rZ   r!   r!   r"   r:    s    zPyShell.restart_shellc             C   s:   |    | j| j | jdd |   | j  d S )NrV   zend-1c)	r   r   r   r2  r7   r   set_line_and_columnrA   Z
reset_undo)r3   r!   r!   r"   r     s
    zPyShell.showpromptc             C   sP   | j jj}t|ddd}d||}|ds:|d7 }| jj	d|d d S )Nr&  T)r   tabsizeZexpand_tabsra   ziomark linestartr   )
rS   r   r   r
   r?   r)  rh  ZperZbottomrV   )r3   r  r   wrapperZwrapped_msgr!   r!   r"   show_warning  s    

zPyShell.show_warningc             C   sb   | j dd}| jr | j| | j ddkr>| j dd | j dd |   | j  d S )Nr   zend-1czend-2cra   )	r7   rW  rA  ZstorerV   r   rn  ZctipZremove_calltip_window)r3   r  r!   r!   r"   r     s    zPyShell.resetoutputr!   c             C   sX   y0| j dd t| ||d}| j dd W n    Y nX | jrTd| _tsTt|S )Nr   rightr   F)r7   r   r   r   r"  r  r   )r3   r   r   countr!   r!   r"   r     s    zPyShell.writec                s8   y| j dddrdS W n tk
r,   dS X t  S )Nz	sel.firstr   r   disabled)r7   r   r   superrD   )r3   )	__class__r!   r"   rD     s    zPyShell.rmenu_check_cutc                s   | j dddrdS t  S )NrV   r   r   rt  )r7   r   ru  rE   )r3   )rv  r!   r"   rE   &  s    zPyShell.rmenu_check_paste)N)N)N)N)N)N)N)r!   )9r{   r|   r}   r	   rL  r   r   r   r   r-  r   r   Zallow_line_numbersZidlelib.historyr$  r6   rB  rS  r   r"  rX  r   rT  rD  rE  r7  rF  r8  rG  rH  r  r   r   rz   rK  rM  rQ  r   r   rY  rZ  r5  r6  r4  r3  r]  r_  r  r9  r:  r   rq  r   r   rD   rE   __classcell__r!   r!   )rv  r"   r   B  sl   
C


	?




r   c          
   C   s6   | j dkr2x&dD ]}| |dd| |d  qW dS )z6Make paste replace selection on x11.  See issue #5124.Zx11)ZTextZEntryZSpinboxz	<<Paste>>z%catch {%W delete sel.first sel.last}
N)Z_windowingsystemZ
bind_class)rj   clsr!   r!   r"   fix_x11_paste,  s    

ry  a  
USAGE: idle  [-deins] [-t title] [file]*
       idle  [-dns] [-t title] (-c cmd | -r file) [arg]*
       idle  [-dns] [-t title] - [arg]*

  -h         print this help message and exit
  -n         run IDLE without a subprocess (DEPRECATED,
             see Help/IDLE Help for details)

The following options will override the IDLE 'settings' configuration:

  -e         open an edit window
  -i         open a shell window

The following options imply -i and will open a shell:

  -c cmd     run the command in a shell, or
  -r file    run script from file

  -d         enable the debugger
  -s         run $IDLESTARTUP or $PYTHONSTARTUP before anything else
  -t title   set title of shell window

A default edit window will be bypassed when -c, -r, or - are used.

[arg]* are passed to the command (-c) or script (-r) in sys.argv[1:].

Examples:

idle
        Open an edit window or shell depending on IDLE's configuration.

idle foo.py foobar.py
        Edit the files, also open a shell if configured to start with shell.

idle -est "Baz" foo.py
        Run $IDLESTARTUP or $PYTHONSTARTUP, edit foo.py, and open a shell
        window with the title "Baz".

idle -c "import sys; print(sys.argv)" "foo"
        Open a shell window and run the command, passing "-c" in sys.argv[0]
        and "foo" in sys.argv[1].

idle -d -s -r foo.py "Hello World"
        Open a shell window, run a startup script, enable the debugger, and
        run foo.py, passing "foo.py" in sys.argv[0] and "Hello World" in
        sys.argv[1].

echo "import sys; print(sys.argv)" | idle - "foobar"
        Open a shell window, run the script piped in, passing '' in sys.argv[0]
        and "foobar" in sys.argv[1].
c           
      sn  dd l } ddlm} ddlm} ddlm} td dad}d}d}d }d }d}	y|  tj	dd  d\}
}W nD | j
k
r } z$td	|tf tjd
 td W d d }~X Y nX x|
D ]\}}|dkr|}d}|dkrd}d}|dkrd}|dkrtjt t  |dkrd}|dkr6tdtjd
 da|dkrj|}tj|rTntd| t  d}|dkr|d}	d}|dkr|t_d}qW |r|d dkrtj }d}x0tttjD ]}tjtj| tj|< qW |r|d dkrdg|dd   t_	n|r$dg| t_	n|r8|g| t_	n|rd}g }x |D ]}|tj| qLW xX|D ],}tj|}|tjkrntjd| qnW n"t }|tjkrtjd| tj ddddd}|p|}|p| }tr|st!  t"dda#t#$  dd l%m&} |t# tj'tjt(d!| d"kr`tj'd#}t#j)|d$ nV|* st+d%kr~d& d'}nd( d)} fd*d+|D }d,d+ |D }t#j,d0|  t-t# t.t# t/t#a0|1t#t0 |r,|s,|s,x0|d d  D ] }t02|d kr|3| qW |s,t04  |rbt05 }|sDd S |* rht0j6rh|j78  nt0j9}|rv|:  |	rtj;<d-ptj;<d.}|rtj|r|j=>| |s|r
|j=?d/tj	f  |r|j=@| n|rB|j=A| |j=>| n8|rB|Bt#}|r*|C| |D }|rB|C| xt0jErXt#F  qDW t#G  td d S )1Nr   )system)testing)macosxTFr   zc:deihnr:st:zError: %s
%s)r   rv   z-cz-dz-ez-hz-iz-nz: Warning: running IDLE without a subprocess is deprecated.z-rzNo script file: z-sz-t-rG   r   r   zeditor-on-startupr   )r   ZIdle)Z	className)r   ZIconsZWindowszidle.ico)r   g333333!@z.png)       0      z.gif)r~  r  r  c                s"   g | ]}t jd | f qS )z	idle_%d%s)r=   r>   r?   )r   size)exticondirr!   r"   r     s   zmain.<locals>.<listcomp>c             S   s   g | ]}t t|d qS ))Zmasterr   )Z
PhotoImagerj   )r   iconfiler!   r!   r"   r     s   ZIDLESTARTUPZPYTHONSTARTUPzbif 1:
            import sys as _sys
            _sys.argv = %r
            del _sys
            
)T)HgetoptrP  rz  r   r{  r|  r'   r  r   argverrorr   	usage_msgr   exitr   r   r=   r>   rq   r   rL  r   r  rw   rs   r  rQ   dirnamerV   getcwdr   r   ZNoDefaultRootr.  rj   r/  idlelib.runr   r?   __file__Zwm_iconbitmapZisAquaTk	TkVersionZwm_iconphotor   ry  r   r;   ZsetupApprd   r[   newr<   dictr   lowerrR   rH  environrW  rS   r  r   r  r  ZtkVersionWarningrq  ZpreferTabsPreferenceWarningZinversedictrV  Zdestroy)r  rz  r{  r|  Zenable_shellZenable_editrT   cmdZscriptZstartupZoptsrC   r  oar   Zpathxr   dirZ
edit_startr   r  ZsizesZ	iconfilesZiconsr%  ZtkversionwarningZprefer_tabs_preference_warningr!   )r  r  r"   r   m  s   


















r   F)NN)Pr   r{   r   rR  ImportErrorr   r   r!  rP  ZctypesZPROCESS_SYSTEM_DPI_AWAREZOleDLLZSetProcessDpiAwarenessr   r   Ztkinter.messageboxZ
messageboxrk   r  r.  rj   r/  r  r   rl   r  r   r)   r=   os.pathr	   re  r   r   textwrapr
   r   r   r  r%   Zidlelib.colorizerr   Zidlelib.configr   r   r   r   Zidlelib.editorr   r   Zidlelib.filelistr   Zidlelib.outwinr   r   r   r   r   Zidlelib.undor   r   r   r   r#   r$   r'   
checkcacher0   r1   r   r   r   Z	RPCClientr   r   r   r   ry  r  r   r!   r!   r!   r"   <module>   s   


 ?   0   m? 6
