a
     ³Šav0  ã                   @   sÆ   d 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ZddlZddl	m
Z ddlZddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZmZ ej
 dd	¡ d
d„ ZG dd„ deejƒZdS )zShell Interpreteré    )Úprint_functionN)ÚInteractiveConsole)Ú	isdefined)ÚencodingÚprograms)Úis_text_string)Úremove_backslashesÚgetcwd_or_homeÚ c                 C   sŠ   t  | ¡r| S |  d¡s | d7 } tƒ gtj D ]V}t  || ¡}t  |¡rP|  S t  |d ¡rj|d   S t  |d ¡r.|d   S q.| S )zGuess filenamez.pyz.pyw)ÚospÚisfileÚendswithr	   ÚsysÚpathÚjoin)Úfilenamer   Úfname© r   úGlib/python3.9/site-packages/spyder/plugins/console/utils/interpreter.pyÚguess_filename    s    


r   c                   @   s¬   e Zd ZdZdZdZd*dd„Zdd	„ Zd
d„ Zd+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.d d!„Zd"d#„ Zd/d$d%„Zd&d'„ Zd(d)„ ZdS )0ÚInterpreterz*Interpreter, executed in a separate threadz>>> z... NFc                 C   sø   t  | |¡ tj | ¡ d| _d| _|| _d| _|durBt 	|¡ | j
| _d| jd< | j| jd< | j| jd< | j| jd< | j| jd< tj| _tj| _tj| _t ¡ \}}t |d	¡| _t |d
d¡| _|ƒ | _|ƒ | _t ¡ | _|| jƒ| _ |  !¡  dS )z|
        namespace: locals send to InteractiveConsole object
        commands: list of commands executed at startup
        NFÚ__main__Ú__name__ÚexecfileÚrunfileZ	raw_inputÚhelpÚrÚwbr   )"r   Ú__init__Ú	threadingÚThreadÚ_idÚ	exit_flagÚdebugÚmoreÚatexitÚregisterÚlocalsÚ	namespacer   r   Úraw_input_replacementÚhelp_replacementr   ÚstdoutÚinitial_stdoutÚstderrÚinitial_stderrÚstdinÚinitial_stdinÚosÚpipeÚfdopenÚ
stdin_readZstdin_writeÚstdout_writeÚstderr_writeZ	ConditionÚinput_conditionÚwidget_proxyÚredirect_stds)Úselfr(   ZexitfuncZOutputZWidgetProxyr#   ZprZpwr   r   r   r   4   s2    


zInterpreter.__init__c                 C   s"   | j s| jt_| jt_| jt_dS )zRedirects stdsN)r#   r5   r   r+   r6   r-   r4   r/   ©r:   r   r   r   r9   b   s    zInterpreter.redirect_stdsc                 C   s"   | j s| jt_| jt_| jt_dS )zRestore stdsN)r#   r,   r   r+   r.   r-   r0   r/   r;   r   r   r   Úrestore_stdsi   s    zInterpreter.restore_stdsr
   c                 C   sB   | j  |¡ | j ¡  | j  ¡ s,| j ¡  q| j j}| j ¡  |S )z(For raw_input builtin function emulation)r8   Z
wait_inputr7   ÚacquireZdata_availableÚwaitZ
input_dataÚrelease)r:   ÚpromptÚinpr   r   r   r)   p   s    


z!Interpreter.raw_input_replacementc              	   C   sÈ   |dur|st  |¡S |du rFdtjd tjd f }|  d| ¡ n<| ¡ }ztd| ƒ W n" ttfy€   t	d| ƒ Y n0 |  t
j¡ | j d¡ |  ¡ }| ¡ rº| j|d	d
 n
|  d¡ dS )z#For help builtin function emulationNz%d.%dr   é   a  
Welcome to Python %s!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://www.python.org/about/gettingstarted/

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
zpydoc.help(%s)z&no Python documentation found for '%r'zhelp> T)Úinteractivea  
You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
)Úpydocr   r   Úversion_infoÚwriteÚstripÚevalÚ	NameErrorÚSyntaxErrorÚprintr1   Úlinesepr8   Ú
new_promptr)   r*   )r:   ÚtextrC   ZpyverrA   r   r   r   r*   z   s$    
òzInterpreter.help_replacementTc              	   C   sD  |dkrd| _ |  d¡ dS d}t |d |¡}t d|¡}t d|¡}|r\d	| d
¡ }n:|r~t| ¡ d ƒ}dt|ƒ }n|r–d| ¡ d  ¡  }t |d |¡}t |d |¡}	t d|¡}
|rêt| ¡ d ƒ}| j	j
|dd n$|	r,t|	 ¡ d ƒ}t |¡r| j	 
|¡ n| j d| ¡ nâ|
r€|
 ¡ d  dd¡ d¡}|D ],}z| j |¡ W n tyx   Y n0 qPnŽ| d¡rt |d
d… ¡}t |j ¡  ¡ ¡}t |j ¡  ¡  ¡ ¡}|rÜ| j |¡ |rî| j |¡ | j d¡ d| _n|  |¡| _|r0| j	  | jr(| j!n| j"¡ | js@|  #¡  dS )zRun command in interpreterzexit()TÚ
Nz+^%s (?:r\')?(?:u\')?\"?\'?([a-zA-Z0-9_\.]+)Úrunz^([a-zA-Z0-9_\.]+)\?$z^\!cd \"?\'?([a-zA-Z0-9_ \.]+)zhelp(%s)rB   r   zrunfile('%s', args=None)zimport os; os.chdir(r"%s")ZxeditÚeditz^clear ([a-zA-Z0-9_, ]+))Zexternal_editorzNo such file or directory: %s
ú r
   ú,ú!F)$r"   rF   ÚreÚmatchÚgroupr   Úgroupsr   rG   r8   rQ   r   r   r6   ÚreplaceÚsplitr(   ÚpopÚKeyErrorÚ
startswithr   Zrun_shell_commandr   Z	transcoder+   ÚreadÚdecoder-   Úrstripr5   r$   ÚpushrM   Úp2Úp1Úresetbuffer)r:   ÚcmdrM   Zspecial_patternZ	run_matchZ
help_matchZcd_matchr   Zxedit_matchZ
edit_matchZclear_matchZvarnamesZvarnamer2   Ztxt_outZtxt_errr   r   r   Úrun_command¢   sd    
ÿzInterpreter.run_commandc                 C   s   | j s|  ¡  q dS )zWait for input and run itN)r"   Úrun_liner;   r   r   r   rP   ë   s    zInterpreter.runc                 C   s*   | j  ¡ }| jrd S |  |d d… ¡ d S )Néÿÿÿÿ)r4   Úreadliner"   rf   ©r:   Úliner   r   r   rg   ð   s    
zInterpreter.run_linec                 C   s6   | j du r0ttj ¡ ƒD ]\}}|| u r|| _ q| j S )zReturn thread idN)r!   Úlistr   Z_activeÚitems)r:   Z	thread_idÚobjr   r   r   Úget_thread_id÷   s
    
zInterpreter.get_thread_idc                 C   s,   |   ¡ r$tj |  ¡ t t¡¡ dS dS d S )NTF)ZisAliveÚctypesZ	pythonapiZPyThreadState_SetAsyncExcro   Z	py_objectÚKeyboardInterruptr;   r   r   r   Úraise_keyboard_interruptÿ   s    ÿz$Interpreter.raise_keyboard_interruptc                 C   s   dS )z5Actions to be done before restarting this interpreterNr   r;   r   r   r   Úclosing  s    zInterpreter.closingc              	   C   sv   t |dƒ ¡ }z6z| d¡}W n ty4   d}Y n0 t||dƒ}W n" ttfyf   t | |¡ Y n0 |  	|¡ dS )zExec filenamer   Úasciiz<executed_script>ÚexecN)
Úopenr^   ÚencodeÚUnicodeEncodeErrorÚcompileÚOverflowErrorrJ   r   ZshowsyntaxerrorZruncode)r:   r   ÚsourceÚnameÚcoder   r   r   r     s    
zInterpreter.execfilec                 C   sn   |durt |ƒstdƒ‚|| jd< |gt_|durL| ¡ D ]}tj |¡ q:|  |¡ dgt_| j d¡ dS )zL
        Run filename
        args: command line arguments (string)
        Nz"expected a character buffer objectÚ__file__r
   )	r   Ú	TypeErrorr(   r   ÚargvrZ   Úappendr   r[   )r:   r   ÚargsÚargr   r   r   r     s    

zInterpreter.runfilec                 C   s2   t |ƒsJ ‚zt|| jƒdfW S    Y dS 0 dS )z¼
        Evaluate text and return (obj, valid)
        where *obj* is the object represented by *text*
        and *valid* is True if object evaluation did not raise any exception
        T)NFN)r   rH   r'   )r:   rN   r   r   r   rH   *  s
    zInterpreter.evalc                 C   s   t ||| jdS )z Return True if object is defined)Úforce_importr(   )r   r'   )r:   Zobjtxtr„   r   r   r   Ú
is_defined6  s    ÿzInterpreter.is_definedc                 C   s   t  | d| ¡S )u“  
        Push a line of source text to the interpreter

        The line should not have a trailing newline; it may have internal
        newlines. The line is appended to a buffer and the interpreterâ€™s
        runsource() method is called with the concatenated contents of the
        buffer as source. If this indicates that the command was executed
        or invalid, the buffer is reset; otherwise, the command is incomplete,
        and the buffer is left as it was after the line was appended.
        The return value is True if more input is required, False if the line
        was dealt with in some way (this is the same as runsource()).
        z#coding=utf-8
)r   ra   rj   r   r   r   ra   >  s    zInterpreter.pushc                 C   s   t  | ¡ dS )z6Remove any unhandled source text from the input bufferN)r   rd   r;   r   r   r   rd   M  s    zInterpreter.resetbuffer)NNNNF)r
   )NF)T)N)F)r   Ú
__module__Ú__qualname__Ú__doc__rc   rb   r   r9   r<   r)   r*   rf   rP   rg   ro   rr   rs   r   r   rH   r…   ra   rd   r   r   r   r   r   0   s,     ÿ
.


(
I	

r   )rˆ   Z
__future__r   r   r%   r   rp   r1   rU   Úos.pathr   r   rD   r}   r   Zspyder_kernels.utils.dochelpersr   Zspyder.utilsr   r   Zspyder.py3compatr   Zspyder.utils.miscr   r	   Úinsertr   r    r   r   r   r   r   Ú<module>   s"   