
)Ί]c        	   @   s  d  d l  Z  d  d l m Z d  d l Z d  d l Z d  d l j 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 e j d d k Z e e d  s d g e _ n  e j j d  d	 k Z e j j d
  d	 k Z d   Z yC d  d l Z e j d k rJd   Z d d d  Z n	 d   Z Wn/ e  k
 rd  d l Z e! f Z" d   Z n Xe j d k r-e r-y d  d l# Z# d  d l$ Z$ e# j% d  \ Z& Z' y: e( e' d  Z' e$ j) j* j+ e'  e$ j) j* j, e'  Wn e- e. f k
 rn XWq-q-Xn  e j d k rye ryd  d l/ Z/ d Z0 d e/ j1 f d     YZ2 e2 e/ _1 n  y d  d l3 Z3 Wn n Xe re j4 j5 d  rd   Z6 e6 e _7 n  e j j d  d k ry1 d  d l8 Z8 x dL D] Z9 e8 j: e9 d  qWWq>q>Xn( y e j j; d  Wn e< k
 r=n Xd  d! l= m> Z? e? e _> d  d l@ Z@ d  d" l@ mA ZA d# eA f d$     YZB eB e@ _C y  e jD d% d& d' eE d( d)  Wn n Xy8 d  d lF ZF d  d* lF mG ZG mH ZH d+   ZI eI eF _J Wn n Xyc d  d lK ZL d, eL jM jN _O e jD d% d& d' eP d( d- d. d/  e jD d% d& d' eP d( d0 d. d/  Wn n Xy& e jD d% d& d' eP d( d1 d. d/  Wn n Xe j d k re ry1 d  d lQ ZR eR jS jT ZU d2   ZV eV eR jS _T WqeW k
 rqXn  d3 e j> f d4     YZX eX e _> d5   ZY eY e j> d6  d7 d d d eZ d8   Z[ eY e j> d6  d9    Z\ eY e j> d6  d:    Z] eY e j> d6  d;    Z^ eY e j> d6  d<    Z_ eY e j> d6  d=    Z` e reY e j> d6  d>    Za n  d? eb f d@     YZc ec dA e j j dB d   Zd dC   Ze dD   Zf dE   Zg dF e j k raeg   n  dG   Zh d d d eZ dH  Zi ei e _i d d eZ dI  Zj ej e _j y" e j dJ e j dK <e j dJ =Wn e< k
 rn Xd S(M   iN(   t   LooseVersion(   t   get_ipythoni    t   2t   argvt    t   SPY_EXTERNAL_INTERPRETERt   Truet   SPY_HIDE_CMDc          N   s   | j  d d  } | j  d e j  } | j  d d  } | j g  |  D] } e |  ^ qF  } e s i  } d | U| d | d | d | d | n | r | | IJn
 | | Id  S(   Nt   ends   
t   filet   sept    s   printf = printt   printf(   t   gett   syst   stdoutt   joint   strt   PY2(   t   objectst   optionsR   R	   R
   t   objt   stringt
   local_dict(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   _print7   s    (#t   ntc         C   s   |  j  d d  S(   Nt   utf8t   replace(   t   encode(   t   u(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR   T   s    c         B   so   | d  k	 r | n | } e j |   j   d } e |  e  rO e |   } n |  } e | | d  | | Ud  S(   Ns   
t   exec(   t   Nonet   builtinst   opent   readt
   isinstancet   unicodeR   t   compile(   t   fnamet   globt   loct
   scripttextt   filename(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   execfileV   s    c         G   sA   t  |  t  r' |  j t j    } n |  } t j | |  d  S(   N(   R#   R$   R   R   t   getfilesystemencodingR    R+   (   R&   t   whereR*   (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR+   a   s    c         B   s6   e  |  d  ! } e | j   |  d  | UWd  QXd  S(   Nt   rbR   (   R!   R%   R"   (   R*   t	   namespacet   f(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR+   k   s    t   LANGi   i   t   SubprocessPopenc           B   s   e  Z d    Z RS(   c         O   s'   t  | d <t t |   j | |   d  S(   Nt   creationflags(   t   creation_flagt   superR2   t   __init__(   t   selft   argst   kwargs(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR6      s    
(   t   __name__t
   __module__R6   (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR2      s   t   linuxc           C   s   d S(   Ns   utf-8(    (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   _getfilesystemencoding_wrapper   s    t   QT_APIt   pyqtt   QStringt   QVariantt   QDatet	   QDateTimet   QTextStreamt   QTimet   QUrl(   t   Pdb(   t   TestProgramt   IPyTesProgramc           B   s   e  Z d    Z RS(   c         O   sL   t  j d t j  } | j d |  | d <t | d <t j |  | |  d  S(   Nt   streamt
   testRunnert   exit(   t   unittestt   TextTestRunnerR   t   stderrt   popt   FalseRH   R6   (   R7   R8   R9   t   test_runner(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR6      s    
(   R:   R;   R6   (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyRI      s   t   actiont   ignoret   categoryt   modules   ipykernel.ipkernel(   t   Screent
   Terminatorc           C   s5   y t    j   t t j _ Wn t k
 r0 n Xd  S(   N(   RW   t   byeR   t   turtlet   TurtleScreent   _RUNNINGRX   (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt
   spyder_bye   s
    s   utf-8s   pandas.core.formatt   messages    .*invalid value encountered in.*s   pandas.formats.formats   numpy.core._methodsc         C   sC   y t  |   SWn. t k
 r> t j d } d | _ t  |   SXd S(   s{   
            Patched get_preparation_data to work when all variables are
            removed before execution.
            t   __main__R   N(   t   _old_preparation_datat   AttributeErrorR   t   modulest   __spec__(   t   namet   main_module(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   _patched_preparation_data  s    	t	   SpyderPdbc           B   s/   e  Z e Z e Z d    Z d   Z d   Z RS(   c         C   s    |  j  r t   j j   n  d S(   s=   Ask Spyder for berkpoints before the first prompt is created.N(   t   startingR   t   kernelt   _ask_spyder_for_breakpoints(   R7   (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   preloop8  s    	c         C   sN  |  j    x' t j j D] } | r | j   q q Wd t j _ i  t j _ d  g t j _ d } xr t | j	    D]^ \ } } t
 j |  rt x@ | D]5 \ } } | d 7} |  j |  j |  | d | q Wqt qt W|  j rJt |  _ |  j } | j }	 |  j | j j  }
 |
 rJ|	 |
 d k rJt
 j |  rJt   j j   qJn  d  S(   Ni   i    t   cond(   t   clear_all_breakst   bdbt
   Breakpointt
   bpbynumbert   deleteMet   nextt   bplistR   t   listt   itemst   ospt   isfilet	   set_breakt   canonicRh   RQ   t   curframet   f_linenot   get_file_breakst   f_codet   co_filenameR   Ri   t   pdb_continue(   R7   t   breakpointst   bpt   iR&   t   datat
   linenumbert	   conditiont   framet   linenot   breaks(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   set_spyder_breakpoints>  s*    

				%c         C   s   | s
 d  St    j } |  j | j j  } t r[ y t | d  } Wq[ t k
 rW q[ Xn  | j } i  } t	 | t
  r t	 | t  r t j |  r t d | d |  } q n  | | _ | j   d  S(   Ns   utf-8R&   R   (   R   Ri   Ry   R}   R~   R   R$   t	   TypeErrorR{   R#   t
   basestringt   intRv   Rw   t   dictt	   _pdb_stept   publish_pdb_state(   R7   R   Ri   R&   R   t   step(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   notify_spydera  s     		(   R:   R;   R   t   send_initial_notificationRh   Rk   R   R   (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyRg   2  s
   		#c            s      f d   } | S(   sB  
    Add the decorated method to the given class; replace as needed.

    If the named method already exists on the given class, it will
    be replaced, and a reference to the old method is created as
    cls._old<patch_name><name>. If the "_old_<patch_name>_<name>" attribute
    already exists, KeyError is raised.
    c            s   |  j  } t   | d   } | d  k	 r d  | f } t   | d   } | d  k rh t   | |  q t d   j  | f   n  t   | |   |  S(   Ns
   _old_%s_%ss   %s.%s already exists.(   R:   t   getattrR   t   setattrt   KeyError(   t   funcR&   t   old_funct   old_reft   old_attr(   t   clst
   patch_name(    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt	   decorator  s    	(    (   R   R   R   (    (   R   R   sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   monkeypatch_method  s    RG   t   tabc         C   s   |  j    d  S(   N(   t   _old_Pdb___init__(   R7   t   completekeyt   stdinR   t   skipt   nosigint(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR6     s    c         C   sZ   |  j  rF |  j |  j | j j  k s6 | j d k r: d Sd |  _  n  |  j | |  d S(   s7   This function is called when a return trap is set here.i    N(   t   _wait_for_mainpyfilet
   mainpyfileRy   R}   R~   R{   t   _old_Pdb_user_return(   R7   R   t   return_value(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   user_return  s    	c         C   s   | d  k	 r. d | j j k r. |  j d  nT |  j | |  |  j rW |  j |  n  |  j |  j |  j	  |  j
   |  j   d  S(   Ns   spydercustomize.pyRL   (   R   R}   R~   t   runt   setupR   R   t   print_stack_entryt   stackt   curindext   _cmdloopt   forget(   R7   R   t	   traceback(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   interaction  s    	
c         C   sY   xR t  rT y! t  |  _ |  j   t |  _ PWq t k
 rP t d d |  j q Xq Wd  S(   NsH   --KeyboardInterrupt--
For copying text while debugging, use Ctrl+Shift+CR	   (   R   t   allow_kbdintt   cmdloopRQ   t   KeyboardInterruptR   R   (   R7   (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR     s    		
		c         C   s'   |  j    t   j } | j |   d  S(   N(   t   _old_Pdb_resetR   Ri   t   _register_pdb_session(   R7   Ri   (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   reset  s    
c         C   s/   d | k r |  j  |  j  n  |  j | |  S(   Nt   _set_spyder_breakpoints(   R   Rz   t   _old_Pdb_postcmd(   R7   t   stopt   line(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   postcmd  s    c         C   s   d d l  m } |  j | j j  } y t | d  } Wn t k
 rK n X| |  j k r_ t S| j	 } | |  j | k r | j j
 } | |  j | k r t Sn  | | | |  \ } } | r | j |  _ | r | j r |  j t | j   n  t St Sd  S(   Ni(   t	   effectives   utf-8(   Rn   R   Ry   R}   R~   R$   R   R   RQ   R{   t   co_firstlinenot   numbert	   currentbpt	   temporaryt   do_clearR   R   (   R7   R   R   R*   R   R   t   flag(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt
   break_here  s(    	t   UserModuleReloaderc           B   sS   e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   s   
    User Module Reloader (UMR) aims at deleting user modules
    to force Python to deeply reload them during import

    pathlist [list]: blacklist in terms of module path
    namelist [list]: blacklist in terms of module name
    c   	      C   sP  | d  k r g  } n- y | j d  } Wn t k
 rA g  } n Xd g } d d d g } d d d g } t r d	 d
 g } | | } n  | | | | |  _ | d  k r g  } n  |  j |  |  _ t t j	 j
    |  _ g  |  _ t |  _ |  j   t j j d d  } | j   d k |  _ t j j d d  } | j   d k |  _ d  S(   Nt   ,t   spyder_kernelst
   matplotlibt   tkintert   Tkintert   pytorcht	   pythoncomt
   tensorflowt   astropyt   fastmatt   SPY_UMR_ENABLEDR   t   truet   SPY_UMR_VERBOSE(   R   t   splitt	   ExceptionR   t   namelistt   create_pathlistt   pathlistRt   R   Rb   t   keyst   previous_modulest   modnames_to_reloadRQ   t
   has_cythont   activate_cythont   ost   environR   t   lowert   enabledt   verbose(	   R7   R   R   t   spy_modulest   mpl_modulest   other_modulest   py2_modulesR   R   (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR6     s0    	
				
c         C   s   y2 t  j   } | d | d | d | d g } Wn t k
 rK g  } n Xy[ d d l } t | d t  r | j   g } n' t | d t  r | j g } n g  } Wn t k
 r g  } n X| | | S(	   sc   
        Add to pathlist Python library paths to be skipped from module
        reloading.
        t   stdlibt   purelibt   scriptsR   iNt   getusersitepackagest	   USER_SITE(   t	   sysconfigt	   get_pathsR   t   siteR   RQ   R   R   (   R7   t   initial_pathlistt   pathst   standard_pathsR   t	   user_path(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR   =  s$    


c         C   s7   |  j  r t S|  j |  s+ |  j |  r/ t St Sd S(   s(   Decide if a module is reloadable or not.N(   R   RQ   t   is_module_in_pathlistt   is_module_in_namelistR   (   R7   RV   t   modname(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   is_module_reloadable_  s    	c         C   s    t  | j d   t  |  j  @S(   s@   Decide if a module can be reloaded or not according to its name.t   .(   t   setR   R   (   R7   R   (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR   k  s    c         C   s   t  | d d
  } | d
 k r" t St g  |  j D] } | | k ^ q/  rN t St j d k s d d d d d d d	 g } g  | D] } t j | |  r | ^ q r t St	 Sn t	 Sd
 S(   s@   Decide if a module can be reloaded or not according to its path.t   __file__R   s   ^/usr/lib.*s   ^/usr/local/lib.*s   ^/usr/.*/dist-packages/.*s   ^/home/.*/.local/lib.*s   ^/Library/.*s   ^/Users/.*/Library/.*s   ^/Users/.*/.local/.*N(
   R   R   R   t   anyR   R   Rd   t   ret   searchRQ   (   R7   RV   t   modpatht   pt   patterns(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR   o  s"    (	+c         C   s   t  j j d  d k } | r y t d  t |  _ Wn t k
 rH n X|  j r d d l } i  } y  d d l } | j	   | d <Wn t k
 r n X| j
 d | d t  q n  d S(	   s   
        Activate Cython support.

        We need to run this here because if the support is
        active, we don't to run the UMR at all.
        t   SPY_RUN_CYTHONR   t   CythoniNt   include_dirst
   setup_argst   reload_support(   R   R   R   t
   __import__R   R   R   t	   pyximportt   numpyt   get_includet   install(   R7   t
   run_cythonR  t   pyx_setup_argsR  (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR     s"    
	c         C   s   g  |  _  xg t t j j    D]P \ } } | |  j k r |  j | |  r |  j  j |  t j | =qo q q q W|  j r |  j  r |  j  } t	 d d d d j
 |  f  n  d S(   s   
        Delete user modules to force Python to deeply reload them

        Do not del modules which are considered as system modules, i.e.
        modules installed in subdirectories of Python interpreter's binary
        Do not del C modules
        s   [4;33m%s[24m%s[0ms   Reloaded moduless   : s   , N(   R   Rt   R   Rb   Ru   R   R   t   appendR   R   R   (   R7   R   RV   t   modnames(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR     s    	"
	N(   R:   R;   t   __doc__R   R6   R   R   R   R   R   R   (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyR     s   1	"			#	!R   t   SPY_UMR_NAMELISTc          C   s   t    }  |  j d d  d S(   sL   
    Remove the post mortem excepthook and replace with a standard one.
    N(    (   R   t   set_custom_excR   (   t   ipython_shell(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   clear_post_mortem  s    	c         C   s   t    t   } | j |  | | f  t j | j  } |  t k s t j d  t	 d d  t	 d  t	 d d  t
 | _ | j   | j } | } x | j r | } | j } q W| } t j d  | j | |  n  d S(   sb   
    For post mortem exception handling, print a banner and enable post
    mortem debugging.
    g?t   *i(   s!   Entering post mortem debugging...N(   R  R   t   showtracebackt   pdbRG   t   colorst   SyntaxErrort   timet   sleepR   RQ   R   R   t   tb_framet   f_backR   (   t   typet   valuet   tbR  R   R   t   prev(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   post_mortem_excepthook  s&    	
	
	c          C   s,   d d  }  t   } | j t f |   d S(   s6   
    Enable the post mortem debugging excepthook.
    c         S   s   t  | | |  d  S(   N(   R  (   t   shellt   etypet   evalueR  t	   tb_offset(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   ipython_post_mortem_debug  s    N(   R   R   R  R   (   R$  R  (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   set_post_mortem  s    	t   SPYDER_EXCEPTHOOKc          C   s   t    }  |  j S(   s   Return current namespace(   R   t   user_ns(   R  (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   _get_globals  s    	c         C   s  y |  j  d  }  Wn t t t f k
 r/ n Xt j rF t j   n  | d k	 rq t | t	  rq t d   n  | d k r t
   } n  |  | d <|  g t _ | d k	 r x* t j |  D] } t j j |  q Wn  | d k	 r$y | j  d  } Wn t t t f k
 rn Xt j |  n  | r4t   n  t j rt j |  d d ) } t   } | j d d | j    Wd QXn t |  |  t   d g t _ y | j d  Wn t k
 rn Xd S(   s   
    Run filename
    args: command line arguments (string)
    wdir: working directory
    post_mortem: boolean, whether to enter post-mortem mode on error
    s   utf-8s"   expected a character buffer objectR   t   encodingt   cythonR   N(   t   decodet   UnicodeErrorR   Ra   t   __umr__R   R   R   R#   R   R(  R   R   t   shlexR   R  R   t   chdirR%  R   t   ioR!   R   t   run_cell_magicR"   R+   R  RP   R   (   R*   R8   t   wdirR/   t   post_mortemt   argR0   R  (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   runfile  sD    	

		"c         C   sx   t  j   } | j |   }  d | _ |  | _ d | _ t j d k rZ |  j d d  }  n  | j	 d |  | | f  d S(   s   
    Debug filename
    args: command line arguments (string)
    wdir: working directory
    post_mortem: boolean, included for compatiblity with runfile
    i   i    R   s   \t   /s   runfile(%r, args=%r, wdir=%r)N(
   R  RG   Ry   R   R   t   _user_requested_quitR   Rd   R   R   (   R*   R8   R2  R3  t   debugger(    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt	   debugfileJ  s    			t   OLD_PYTHONPATHt
   PYTHONPATH(   R@   RA   RB   RC   RD   RE   RF   (k   Rn   t   distutils.versionR    R0  R   t   os.patht   pathRv   R  R   R.  R   R   R  t   warningst   IPython.core.getipythonR   t   versionR   t   hasattrR   R   R   t   IS_EXT_INTERPRETERt   HIDE_CMD_WINDOWSR   t   __builtin__R    Rd   R   R   R+   t   ImportErrorR   R   t   localet   ctypest   getdefaultlocalet   _tt   _cpR   t   windllt   kernel32t   SetConsoleCPt   SetConsoleOutputCPt
   ValueErrorR   t
   subprocessR4   t   PopenR2   t   sitecustomizet   platformt
   startswithR=   R,   t   sipt   qtypet   setapiRP   R   t   IPython.core.debuggerRG   t   ipyPdbRM   RH   RI   t   maint   filterwarningst   DeprecationWarningRZ   RW   RX   R]   RY   t   pandast   pdR   t   displayR)  t   RuntimeWarningt   multiprocessing.spawnt   multiprocessingt   spawnt   get_preparation_dataR`   Rf   R   Rg   R   RQ   R6   R   R   R   R   R   R   t   objectR   R-  R  R  R%  R(  R5  R9  (    (    (    sG   lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.pyt   <module>   s  			
	
				J					 			
	5		