ó
‹²,]c           @  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	 j
 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
 j d	 d
 ƒ d „  Z d e e j f d „  ƒ  YZ d S(   s   Shell Interpreteriÿÿÿÿ(   t   print_functionN(   t   InteractiveConsole(   t	   isdefined(   t   encodingt   programs(   t   is_text_string(   t   remove_backslashest   getcwd_or_homei    t    c         C  s¬   t  j |  ƒ r |  S|  j d ƒ s/ |  d 7}  n  xv t ƒ  g t j D]a } t  j | |  ƒ } t  j | ƒ rn | St  j | d ƒ r‰ | d St  j | d ƒ rC | d SqC W|  S(   s   Guess filenames   .pys   .pyw(   t   ospt   isfilet   endswithR   t   syst   patht   join(   t   filenameR   t   fname(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   guess_filename    s    t   Interpreterc           B  sÔ   e  Z d  Z d Z d Z d d d d e d „ Z d „  Z d „  Z	 d d „ Z
 d e d „ Z e d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z e d „ Z d „  Z d „  Z RS(   s*   Interpreter, executed in a separate threads   >>> s   ... c         C  sc  t  j |  | ƒ t j j |  ƒ d
 |  _ t |  _ | |  _ t |  _	 | d
 k	 r` t
 j | ƒ n  |  j |  _ d |  j d <|  j |  j d <|  j |  j d <|  j |  j d <|  j |  j d <t j |  _ t j |  _ t j |  _ t j ƒ  \ } } t j | d ƒ |  _ t j | d d	 ƒ |  _ | ƒ  |  _ | ƒ  |  _ t j  ƒ  |  _! | |  j! ƒ |  _" |  j# ƒ  d
 S(   s|   
        namespace: locals send to InteractiveConsole object
        commands: list of commands executed at startup
        t   __main__t   __name__t   execfilet   runfilet	   raw_inputt   helpt   rt   wbi    N($   R   t   __init__t	   threadingt   Threadt   Nonet   _idt   Falset	   exit_flagt   debugt   moret   atexitt   registert   localst	   namespaceR   R   t   raw_input_replacementt   help_replacementR   t   stdoutt   initial_stdoutt   stderrt   initial_stderrt   stdint   initial_stdint   ost   pipet   fdopent
   stdin_readt   stdin_writet   stdout_writet   stderr_writet	   Conditiont   input_conditiont   widget_proxyt   redirect_stds(   t   selfR'   t   exitfunct   Outputt   WidgetProxyR"   t   prt   pw(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR   4   s2    				c         C  s4   |  j  s0 |  j t _ |  j t _ |  j t _ n  d S(   s   Redirects stdsN(   R"   R5   R   R*   R6   R,   R3   R.   (   R;   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR:   b   s    	c         C  s4   |  j  s0 |  j t _ |  j t _ |  j t _ n  d S(   s   Restore stdsN(   R"   R+   R   R*   R-   R,   R/   R.   (   R;   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   restore_stdsi   s    	R   c         C  s]   |  j  j | ƒ |  j j ƒ  x  |  j  j ƒ  s? |  j j ƒ  q  W|  j  j } |  j j ƒ  | S(   s(   For raw_input builtin function emulation(   R9   t
   wait_inputR8   t   acquiret   data_availablet   waitt
   input_datat   release(   R;   t   promptt   inp(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR(   p   s    c         C  s  | d
 k	 r  | r  t j | ƒ S| d
 k r^ d t j d t j d f } |  j d | ƒ nF | j ƒ  } y t d | ƒ Wn% t t	 f k
 r£ t
 d | ƒ n X|  j t j ƒ |  j j d ƒ |  j ƒ  } | j ƒ  rò |  j | d t ƒn |  j d	 ƒ d
 S(   s#   For help builtin function emulations   %d.%di    i   s  
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".
s   pydoc.help(%s)s&   no Python documentation found for '%r's   help> t   interactives  
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.
N(   R   t   pydocR   R   t   version_infot   writet   stript   evalt	   NameErrort   SyntaxErrort   printR0   t   linesepR9   t
   new_promptR(   R)   t   True(   R;   t   textRJ   t   pyverRI   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR)   z   s$    c         C  s  | d k r& t  |  _ |  j d ƒ d Sd } t j | d | ƒ } t j d | ƒ } t j d | ƒ } | r‚ d | j d	 ƒ } nR | r± t | j ƒ  d
 ƒ } d t | ƒ } n# | rÔ d | j ƒ  d
 j	 ƒ  } n  t j | d | ƒ } t j | d | ƒ }	 t j d | ƒ }
 | rGt | j ƒ  d
 ƒ } |  j
 j | d t  ƒn€|	 rœt |	 j ƒ  d
 ƒ } t j | ƒ r…|  j
 j | ƒ qÇ|  j j d | ƒ n+|
 r|
 j ƒ  d
 j d d ƒ j d ƒ } xý | D]. } y |  j j | ƒ WqÎt k
 rûqÎXqÎWnÄ | j d ƒ rµt j | d	 ƒ } t j | j j ƒ  j ƒ  ƒ } t j | j j ƒ  j ƒ  j ƒ  ƒ } | r€|  j j | ƒ n  | r™|  j j | ƒ n  |  j j d ƒ t |  _  n |  j! | ƒ |  _  | rõ|  j
 j" |  j  rè|  j# n |  j$ ƒ n  |  j  s|  j% ƒ  n  d S(   s   Run command in interpreters   exit()s   
Ns+   ^%s (?:r\')?(?:u\')?\"?\'?([a-zA-Z0-9_\.]+)t   runs   ^([a-zA-Z0-9_\.]+)\?$s   ^\!cd \"?\'?([a-zA-Z0-9_ \.]+)s   help(%s)i   i    s   runfile('%s', args=None)s   import os; os.chdir(r"%s")t   xeditt   edits   ^clear ([a-zA-Z0-9_, ]+)t   external_editors   No such file or directory: %s
t    R   t   ,t   !(&   RU   R!   RM   t   ret   matcht   groupR   t   groupsR   RN   R9   RZ   R	   R
   R6   t   replacet   splitR'   t   popt   KeyErrort
   startswithR   t   run_shell_commandR   t	   transcodeR*   t   readt   decodeR,   t   rstripR5   R    R#   t   pushRT   t   p2t   p1t   resetbuffer(   R;   t   cmdRT   t   special_patternt	   run_matcht
   help_matcht   cd_matchR   t   xedit_matcht
   edit_matcht   clear_matcht   varnamest   varnameR1   t   txt_outt   txt_err(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   run_command¢   sb    		%$(	c         C  s   x |  j  s |  j ƒ  q Wd S(   s   Wait for input and run itN(   R!   t   run_line(   R;   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyRX   ë   s    c         C  s1   |  j  j ƒ  } |  j r d  S|  j | d  ƒ d  S(   Niÿÿÿÿ(   R3   t   readlineR!   R}   (   R;   t   line(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR~   ð   s    	c         C  sW   |  j  d k rP x> t t j j ƒ  ƒ D]$ \ } } | |  k r% | |  _  q% q% Wn  |  j  S(   s   Return thread idN(   R   R   t   listR   t   _activet   items(   R;   t	   thread_idt   obj(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   get_thread_id÷   s
    "c         C  s:   |  j  ƒ  r2 t j j |  j ƒ  t j t ƒ ƒ t St Sd  S(   N(	   t   isAlivet   ctypest	   pythonapit   PyThreadState_SetAsyncExcR†   t	   py_objectt   KeyboardInterruptRU   R    (   R;   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   raise_keyboard_interruptÿ   s
    c         C  s   d S(   s5   Actions to be done before restarting this interpreterN(    (   R;   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   closing  s    c         C  s“   t  | d ƒ j ƒ  } yC y | j d ƒ } Wn t k
 rD d } n Xt | | d ƒ } Wn' t t f k
 r t j |  | ƒ n X|  j	 | ƒ d S(   s   Exec filenameR   t   asciis   <executed_script>t   execN(
   t   openRj   t   encodet   UnicodeEncodeErrort   compilet   OverflowErrorRQ   R   t   showsyntaxerrort   runcode(   R;   R   t   sourcet   namet   code(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR     s    
c         C  s¤   | d k	 r( t | ƒ r( t d ƒ ‚ n  | |  j d <| g t _ | d k	 rw x' | j ƒ  D] } t j j | ƒ qZ Wn  |  j | ƒ d g t _ |  j j	 d ƒ d S(   sL   
        Run filename
        args: command line arguments (string)
        s"   expected a character buffer objectt   __file__R   N(
   R   R   t	   TypeErrorR'   R   t   argvRd   t   appendR   Re   (   R;   R   t   argst   arg(    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR     s    c         C  sA   t  | ƒ s t ‚ y t | |  j ƒ t f SWn d t f SXd S(   s¼   
        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
        N(   R   t   AssertionErrorRO   R&   RU   R   R    (   R;   RV   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyRO   *  s
    c         C  s   t  | d | d |  j ƒS(   s    Return True if object is definedt   force_importR'   (   R   R&   (   R;   t   objtxtR¢   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt
   is_defined6  s    c         C  s   t  j |  d | ƒ S(   s¢  
        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()).
        s   #coding=utf-8
(   R   Rm   (   R;   R€   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyRm   >  s    c         C  s   t  j |  ƒ d S(   s6   Remove any unhandled source text from the input bufferN(   R   Rp   (   R;   (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyRp   M  s    N(   R   t
   __module__t   __doc__Ro   Rn   R   R    R   R:   RA   R(   R)   RU   R}   RX   R~   R†   R   RŽ   R   R   RO   R¤   Rm   Rp   (    (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyR   0   s*   -		
(I									(   R¦   t
   __future__R    R   R$   R   Rˆ   R0   R_   t   os.pathR   R	   RK   Rš   R   t   spyder_kernels.utils.dochelpersR   t   spyder.utilsR   R   t   spyder.py3compatR   t   spyder.utils.miscR   R   t   insertR   R   R   (    (    (    s1   lib/python2.7/site-packages/spyder/interpreter.pyt   <module>   s"   	