B
    j]\n              	   @   s  d dl Z d dlm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Zd dlZd dlZd dlmZ ejd  dkZeedsdge_ejddkZd	d
 Zy2d dlZejdkrdd ZdTddZndd ZW n, ek
r
   d dlZefZ dd ZY nX ejdkrerynd dl!Z!d dl"Z"e!#d\Z$Z%y0e&e%dd Z%e"j'j()e% e"j'j(*e% W n e+e,fk
r   Y nX W n   Y nX ejdkrd dl-Z-dZ.G dd de-j/Z0e0e-_/yd dl1Z1W n   Y nX erej23drdd Z4e4e_5ejddkrPy(d dl6Z6xdD ]Z7e68e7d q(W W n   Y nX n(yej9d W n e:k
rv   Y nX d dl;m<Z= e=e_<d dl>Z>d dl>m?Z? G dd  d e?Z@e@e>_AyejBd!eCd"d# W n   Y nX y:d dlDZEd$eEjFjG_HejBd!eId%d&d' ejBd!eId(d&d' W n   Y nX yejBd!eId)d&d' W n   Y nX ejdkresy$d dlJZKeKjLjMZNd*d+ ZOeOeKjL_MW n ePk
r   Y nX G d,d- d-ej<ZQeQe_<d.d/ ZReRej<d0dUd3d4ZSeRej<d0d5d6 ZTeRej<d0d7d8 ZUeRej<d0d9d: ZVeRej<d0d;d< ZWeRej<d0d=d> ZXerDeRej<d0d?d@ ZYG dAdB dBeZZ[e[ejdCddDZ\dEdF Z]dGdH Z^dIdJ Z_dKejkre_  dLdM Z`dVdNdOZaeae_adWdPdQZbebe_byejdR ejdS< ejdR= W n e:k
r   Y nX dS )X    N)LooseVersion)get_ipython2argv ZSPY_EXTERNAL_INTERPRETERTruec              O   s   | dd}| dtj}| dd}|dd | D }tsbi }td| |d	 ||||d
 n|rtt|? |f nt|? |f d S )Nend
filesep c             S   s   g | ]}t |qS  )str).0objr   r   Glib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py
<listcomp>;   s    z_print.<locals>.<listcomp>zprintf = printZprintf)r
   r   r   )getsysstdoutjoinPY2execprint)Zobjectsoptionsr   r
   r   stringZ
local_dictr   r   r   _print7   s    
r   ntc             C   s   |  ddS )Nutf8replace)encode)ur   r   r   r    T   s    r    c             C   sR   |d k	r|n|}t |  d }t| tr6t| }n| }tt||d|| d S )Nr	   r   )builtinsopenread
isinstanceunicoder    r   compile)fnameZglobZlocZ
scripttextfilenamer   r   r   execfileV   s    

r*   c             G   s2   t | tr| t }n| }tj|f|  d S )N)r%   r&   r    r   getfilesystemencodingr"   r*   )r(   wherer)   r   r   r   r*   a   s    
c          	   C   s0   t | d}tt| | d| W d Q R X d S )Nrbr   )r#   r   r'   r$   )r)   	namespacefr   r   r   r*   k   s    ZLANG   i   c                   s   e Zd Z fddZ  ZS )SubprocessPopenc                s   t |d< tt| j|| d S )NZcreationflags)creation_flagsuperr1   __init__)selfargskwargs)	__class__r   r   r4      s    zSubprocessPopen.__init__)__name__
__module____qualname__r4   __classcell__r   r   )r8   r   r1      s   r1   Zlinuxc               C   s   dS )Nzutf-8r   r   r   r   r   _getfilesystemencoding_wrapper   s    r=   ZQT_APIZpyqt)ZQStringZQVariantZQDateZ	QDateTimeZQTextStreamZQTimeZQUrl)Pdb)TestProgramc               @   s   e Zd Zdd ZdS )IPyTesProgramc             O   s<   t jtjd}|d||d< d|d< tj| f|| d S )N)streamZ
testRunnerFexit)unittestZTextTestRunnerr   stderrpopr?   r4   )r5   r6   r7   Ztest_runnerr   r   r   r4      s    zIPyTesProgram.__init__N)r9   r:   r;   r4   r   r   r   r   r@      s   r@   ignorezipykernel.ipkernel)actioncategorymodulezutf-8zpandas.core.formatz .*invalid value encountered in.*)rG   rH   rI   messagezpandas.formats.formatznumpy.core._methodsc             C   s6   yt | S  tk
r0   tjd }d|_t | S X dS )z{
            Patched get_preparation_data to work when all variables are
            removed before execution.
            __main__r   N)_old_preparation_dataAttributeErrorr   modules__spec__)nameZmain_moduler   r   r   _patched_preparation_data	  s    
rQ   c               @   s,   e Zd ZdZdZdd Zdd Zdd ZdS )		SpyderPdbTc             C   s   | j rt j  dS )z=Ask Spyder for berkpoints before the first prompt is created.N)startingr   kernelZ_ask_spyder_for_breakpoints)r5   r   r   r   preloop$  s    zSpyderPdb.preloopc             C   s   |    xtjjD ]}|r|  qW dtj_i tj_d gtj_d}xRt| D ]B\}}t	
|rRx.|D ]&\}}|d7 }| j| |||d qjW qRW | jrd| _| j}|j}	| |jj}
|
r|	|
d krt	
|rt j  d S )N   r   )ZcondF)Zclear_all_breaksbdbZ
BreakpointZ
bpbynumberZdeleteMenextZbplistlistitemsospisfileZ	set_breakcanonicrS   curframef_linenoZget_file_breaksf_codeco_filenamer   rT   Zpdb_continue)r5   Zbreakpointsbpir(   dataZ
linenumberZ	conditionframelinenobreaksr   r   r   set_spyder_breakpoints*  s*    

z SpyderPdb.set_spyder_breakpointsc             C   s   |sd S t  j}| |jj}trFyt|d}W n tk
rD   Y nX |j}i }t	|t
rzt	|trzt|rzt||d}||_|  d S )Nzutf-8)r(   rf   )r   rT   r]   r`   ra   r   r&   	TypeErrorr_   r%   
basestringintr[   r\   dictZ	_pdb_stepZpublish_pdb_state)r5   re   rT   r(   rf   stepr   r   r   notify_spyderM  s     
zSpyderPdb.notify_spyderN)r9   r:   r;   send_initial_notificationrS   rU   rh   rn   r   r   r   r   rR     s
   #rR   c                s    fdd}|S )aB  
    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                sj   | j }t |d }|d k	rZd|f }t |d }|d krHt || ntd j |f t ||  | S )Nz
_old_%s_%sz%s.%s already exists.)r9   getattrsetattrKeyError)funcr(   Zold_funcZold_refZold_attr)cls
patch_namer   r   	decorator{  s    z%monkeypatch_method.<locals>.decoratorr   )rt   ru   rv   r   )rt   ru   r   monkeypatch_methodn  s    rw   r>   tabFc             C   s   |    d S )N)Z_old_Pdb___init__)r5   Zcompletekeystdinr   skipZnosigintr   r   r   r4     s    r4   c             C   s>   | j r.| j| |jjks$|jdkr(dS d| _ | || dS )z7This function is called when a return trap is set here.r   N)_wait_for_mainpyfile
mainpyfiler]   r`   ra   r_   Z_old_Pdb_user_return)r5   re   Zreturn_valuer   r   r   user_return  s    
r}   c             C   sb   |d k	r d|j jkr | d n>| || | jr<| | | | j| j  | 	  | 
  d S )Nzspydercustomize.pyrB   )r`   ra   runZsetupro   rn   Zprint_stack_entrystackZcurindex_cmdloopZforget)r5   re   	tracebackr   r   r   interaction  s    
r   c             C   sH   xByd| _ |   d| _ P W q tk
r>   td| jd Y qX qW d S )NTFzH--KeyboardInterrupt--
For copying text while debugging, use Ctrl+Shift+C)r
   )Zallow_kbdintZcmdloopKeyboardInterruptr   r   )r5   r   r   r   r     s    r   c             C   s   |    t j}||  d S )N)Z_old_Pdb_resetr   rT   Z_register_pdb_session)r5   rT   r   r   r   reset  s    r   c             C   s    d|kr|  | j | ||S )NZ_set_spyder_breakpoints)rn   r^   Z_old_Pdb_postcmd)r5   stopliner   r   r   postcmd  s    r   c             C   s   ddl m} | |jj}yt|d}W n tk
r<   Y nX || jkrLdS |j}|| j| krz|jj	}|| j| krzdS ||||\}}|r|j
| _|r|jr| t|j
 dS dS d S )Nr   )	effectivezutf-8FT)rW   r   r]   r`   ra   r&   ri   rg   r_   co_firstlinenoZnumberZ	currentbpZ	temporaryZdo_clearr   )r5   re   r   r)   rf   rb   flagr   r   r   
break_here  s(    

r   c               @   sJ   e Zd Z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 )UserModuleReloaderz
    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
    Nc       	      C   s   |d krg }n(y| d}W n tk
r4   g }Y nX dg}dddg}dddg}trdd	d
g}|| }|| | | | _|d krg }| || _ttj	 | _
g | _d| _|   tjdd}| dk| _tjdd}| dk| _d S )N,Zspyder_kernelsZ
matplotlibZtkinterZTkinterZpytorchZ	pythoncomZ
tensorflowZastropyZfastmatFZSPY_UMR_ENABLEDr   trueZSPY_UMR_VERBOSE)split	Exceptionr   namelistcreate_pathlistpathlistrY   r   rN   keysprevious_modulesmodnames_to_reload
has_cythonactivate_cythonosenvironr   lowerenabledverbose)	r5   r   r   Zspy_modulesZmpl_modulesZother_modulesZpy2_modulesr   r   r   r   r   r4     s0    


zUserModuleReloader.__init__c             C   sF   y,t  }|d |d |d |d g}|| S  tk
r@   |S X dS )zc
        Add to pathlist Python library paths to be skipped from module
        reloading.
        ZstdlibZpurelibZscriptsrd   N)	sysconfigZ	get_pathsr   )r5   Zinitial_pathlistpathsZ	lib_pathsr   r   r   r   )  s    
z"UserModuleReloader.create_pathlistc             C   s*   | j r
dS | |s| |r"dS dS dS )z(Decide if a module is reloadable or not.FTN)r   is_module_in_pathlistis_module_in_namelist)r5   rI   modnamer   r   r   is_module_reloadable9  s    

z'UserModuleReloader.is_module_reloadablec             C   s   t |dt | j@ S )z@Decide if a module can be reloaded or not according to its name..)setr   r   )r5   r   r   r   r   r   E  s    z(UserModuleReloader.is_module_in_namelistc                sn   t |dd  dkrdS t fdd| jD r4dS tjdksfddd	d
g} fdd|D r`dS dS ndS dS )z@Decide if a module can be reloaded or not according to its path.__file__NTc                s   g | ]}| kqS r   r   )r   p)modpathr   r   r   S  s    z<UserModuleReloader.is_module_in_pathlist.<locals>.<listcomp>r   z^/usr/lib.*z^/usr/local/lib.*z^/usr/.*/dist-packages/.*z^/Library/.*c                s   g | ]}t | r|qS r   )research)r   r   )r   r   r   r   b  s    F)rp   anyr   r   rP   )r5   rI   Zpatternsr   )r   r   r   I  s    
z(UserModuleReloader.is_module_in_pathlistc             C   s   t jddk}|rytd d| _W n tk
r:   Y nX | jrddl}i }yddl}| |d< W n tk
rz   Y nX |j	|dd dS )	z
        Activate Cython support.

        We need to run this here because if the support is
        active, we don't to run the UMR at all.
        ZSPY_RUN_CYTHONr   ZCythonTr   NZinclude_dirs)Z
setup_argsZreload_support)
r   r   r   
__import__r   r   	pyximportnumpyZget_includeZinstall)r5   Z
run_cythonr   Zpyx_setup_argsr   r   r   r   r   i  s"    
z"UserModuleReloader.activate_cythonc             C   s   g | _ xHttj D ]6\}}|| jkr| ||r| j | tj|= qqqW | jr|| j r|| j }t	dddd
| f  dS )z
        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
        z[4;33m%s[24m%s[0mzReloaded modulesz: z, N)r   rY   r   rN   rZ   r   r   appendr   r   r   )r5   r   rI   modnamesr   r   r   r~     s    

zUserModuleReloader.run)NN)r9   r:   r;   __doc__r4   r   r   r   r   r   r~   r   r   r   r   r     s   
1 !r   ZSPY_UMR_NAMELIST)r   c              C   s   t  } | dd dS )zL
    Remove the post mortem excepthook and replace with a standard one.
    r   N)r   set_custom_exc)ipython_shellr   r   r   clear_post_mortem  s    r   c             C   s   t   t }|| ||f t|j}| tkstd t	d t	d t	d d|_
|  |j}|}x|jr~|}|j}qlW |}td ||| dS )zb
    For post mortem exception handling, print a banner and enable post
    mortem debugging.
    g?z(****************************************z!Entering post mortem debugging...FN)r   r   Zshowtracebackpdbr>   ZcolorsSyntaxErrortimeZsleepr   ro   r   tb_framef_backr   )typevaluetbr   r   re   prevr   r   r   post_mortem_excepthook  s&    


r   c              C   s"   ddd} t  }|tf|  dS )z6
    Enable the post mortem debugging excepthook.
    Nc             S   s   t ||| d S )N)r   )shelletypeZevaluer   Z	tb_offsetr   r   r   ipython_post_mortem_debug  s    z2set_post_mortem.<locals>.ipython_post_mortem_debug)N)r   r   r   )r   r   r   r   r   set_post_mortem  s    
r   ZSPYDER_EXCEPTHOOKc              C   s   t  } | jS )zReturn current namespace)r   Zuser_ns)r   r   r   r   _get_globals  s    r   c          
   C   s@  y|  d} W n tttfk
r(   Y nX tjr8t  |dk	rRt|tsRtd|dkr`t	 }| |d< | gt
_|dk	rxt|D ]}t
j| qW |dk	ry| d}W n tttfk
r   Y nX t| |rt  tjrtj| dd}t }|dd|  W dQ R X n
t| | t  dgt
_|d dS )z
    Run filename
    args: command line arguments (string)
    wdir: working directory
    post_mortem: boolean, whether to enter post-mortem mode on error
    zutf-8Nz"expected a character buffer objectr   )encodingZcythonr   )decodeUnicodeErrorri   rM   __umr__r   r~   r%   rj   r   r   r   shlexr   r   r   chdirr   r   ior#   r   Zrun_cell_magicr$   r*   r   rE   )r)   r6   wdirr.   post_mortemargr/   r   r   r   r   runfile  s>    

r   c             C   sR   t  }|| } d|_| |_d|_tjdkr:| dd} |	d| ||f  dS )z
    Debug filename
    args: command line arguments (string)
    wdir: working directory
    post_mortem: boolean, included for compatiblity with runfile
    rV   r   r   \/zrunfile(%r, args=%r, wdir=%r)N)
r   r>   r]   r{   r|   Z_user_requested_quitr   rP   r   r~   )r)   r6   r   r   Zdebuggerr   r   r   	debugfile  s    

r   ZOLD_PYTHONPATHZ
PYTHONPATH)NN)rx   NNNF)NNNF)NNF)crW   Zdistutils.versionr   r   r   os.pathpathr[   r   r   r   r   r   r   warningsZIPython.core.getipythonr   versionr   hasattrr   r   r   ZIS_EXT_INTERPRETERr   Z__builtin__r"   rP   r    r*   ImportErrorr   rj   ZlocaleZctypesZgetdefaultlocaleZ_tZ_cprk   ZwindllZkernel32ZSetConsoleCPZSetConsoleOutputCP
ValueErrorri   
subprocessr2   Popenr1   sitecustomizeplatform
startswithr=   r+   ZsipZqtypeZsetapirE   rr   ZIPython.core.debuggerr>   ZipyPdbrC   r?   r@   mainfilterwarningsDeprecationWarningZpandasZpdr   Zdisplayr   RuntimeWarningZmultiprocessing.spawnZmultiprocessingZspawnZget_preparation_datarL   rQ   r   rR   rw   r4   r}   r   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r   r   r   <module>   s  
	




J
 		  4


/
