a
    zha                     @   sL   d dl Z d dlZd dlmZ d dlmZ dd Zdd ZG dd	 d	eZ	dS )
    N)get_ipython)PY2c                  C   s:   t  j r&t  jj} | j}| jj}nd}t  j}||fS )zReturn current namespace.N)r   kernelis_debugging_pdb_objcurframe_localscurframe	f_globalsuser_ns)pdb	ns_locals
ns_globals r   Ilib/python3.9/site-packages/spyder_kernels/customize/namespace_manager.py_get_globals_locals   s    

r   c                 C   sZ   t  j r6t  jj}|jj|  |rV|j| n t  j|  |rVt  j| dS )zUpdate current namespace.N)	r   r   r   r   r   r	   updater   r
   )r   r   r   r   r   r   _set_globals_locals   s    
r   c                   @   s*   e Zd ZdZd
ddZdd Zdd	 ZdS )NamespaceManagerz
    Get a namespace and set __file__ to filename for this namespace.

    The namespace is either namespace, the current namespace if
    current_namespace is True, or a new namespace.
    NFc                 C   s4   || _ || _d | _|| _d | _d | _d| _|| _d S )NF)filenamer   r   current_namespace_previous_filename_previous_main_reset_main
_file_code)selfr   	namespacer   Z	file_coder   r   r   __init__0   s    zNamespaceManager.__init__c                 C   s$  | j du r| jrBt \| _ | _d| j v r4| j d | _| j| j d< nHt }|| jd}|j| _ d| _dt	j
v rzt	j
d | _|t	j
d< d| _| j | jft j_| jdurtst| jtrz| j | _W n ty   d| _Y n0 | jdurt| jddd | j D | jftj| j< | j | jfS )z(
        Prepare the namespace.
        N__file____main__Tc                 S   s   g | ]}|d  qS )
r   ).0liner   r   r   
<listcomp>c       z.NamespaceManager.__enter__.<locals>.<listcomp>)r   r   r   r   r   r   r   Znew_main_mod__dict__sysmodulesr   r   r   _running_namespacer   r   
isinstancebytesdecodeUnicodeDecodeErrorlen
splitlines	linecachecache)r   Zipython_shellZmain_modr   r   r   	__enter__;   sB    







zNamespaceManager.__enter__c                 C   s   dt  j_| jr| j| jd< nd| jv r4| jd | jsHt| j| j | j	r\| j	t
jd< ndt
jv rt| jrtt
jd= | jtjv rtj| j dS )z&
        Reset the namespace.
        Nr   r   )r   r   r'   r   r   popr   r   r   r   r%   r&   r   r   r.   r/   )r   exc_typeZexc_valZexc_tbr   r   r   __exit__g   s    

zNamespaceManager.__exit__)NFN)__name__
__module____qualname____doc__r   r0   r3   r   r   r   r   r   (   s     
,r   )
r%   r.   ZIPython.core.getipythonr   Zspyder_kernels.py3compatr   r   r   objectr   r   r   r   r   <module>   s   