a
    ø£Be×$  ã                   @   sÒ   d Z 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	m
Z
 ddlZejZedd„ ƒZdd	„ Zd
d„ Zi add„ Zdd„ Zdd„ Zddd„Zi add„ ZejZg ej¢d‘d‘d‘d‘d‘d‘R fdd„ZdS ) aÅ  
Provides a reload() function that acts recursively.

Python's normal :func:`python:reload` function only reloads the module that it's
passed. The :func:`reload` function in this module also reloads everything
imported from that module, which is useful when you're changing files deep
inside a package.

To use this as your default reload function, type this::

    import builtins
    from IPython.lib import deepreload
    builtins.reload = deepreload.reload

A reference to the original :func:`python:reload` is stored in this module as
:data:`original_reload`, so you can restore it later.

This code is almost entirely based on knee.py, which is a Python
re-implementation of hierarchical module import.
é    N)Úcontextmanager)Ú
ModuleType)Úwarnc                 c   s*   t j}| t _zd V  W |t _n|t _0 d S )N)Úbuiltin_modÚ
__import__)Z
new_importZsaved_import© r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/lib/deepreload.pyÚreplace_import_hook(   s
    r	   c                 C   s°  |}|rt | tƒsdS |  dd¡}|durbt|dƒs<tdƒ‚t|ƒdkr\|dkrXtdƒ‚dS |}n|d| vrndS | d }d	| v rŒ| | d< }nR| d
¡}|d  k rª|k r¶n ntdƒ‚|dk rÊd| d< dS |d|…  | d< }t|ƒ}t|ddƒD ]H}z| d
d|¡}W qò t�y8 }	 ztdƒ|	‚W Y d}	~	qòd}	~	0 0 qò|d|… }zt	j
| }
W nP t�y¦ }	 z6|dk �r„td| ƒ d}
ntd| ƒ|	‚W Y d}	~	n
d}	~	0 0 |
|fS )aâ  
    parent, name = get_parent(globals, level)

    Return the package that an import is being performed in.  If globals comes
    from the module foo.bar.bat (not itself a package), this returns the
    sys.modules entry for foo.bar.  If globals is from a package's __init__.py,
    the package's entry in sys.modules is returned.

    If globals doesn't come from a package or a module in a package, or a
    corresponding entry is not found in sys.modules, None is returned.
    )NÚ Ú__package__NÚrindexz__package__ set to non-stringr   z(Attempted relative import in non-packageÚ__name__Ú__path__Ú.é   éÿÿÿÿz2attempted relative import beyond top-level packagez?Parent module '%.200s' not found while handling absolute importzAParent module '%.200s' not loaded, cannot perform relative import)Ú
isinstanceÚdictÚgetÚhasattrÚ
ValueErrorÚlenÚrfindÚranger   ÚsysÚmodulesÚBaseExceptionr   ÚSystemError)ÚglobalsÚlevelZ
orig_levelÚpkgnameÚnameÚmodnameZlastdotÚdotÚxÚeÚparentr   r   r   Ú
get_parent1   s^    

ÿ
ÿÿÿr'   c                 C   sÈ   t |ƒdkr| d|fS | d¡}|dkr0tdƒ‚|dk rB|}d}n|d|… }||d d… }|dkrn|d7 }||7 }t| ||ƒ}|du rª| |krªt|||ƒ}|durª|}|du r¾td| ƒ‚|||fS )zf
    mod, name, buf = load_next(mod, altmod, name, buf)

    altmod is either None or same as mod
    r   Nr   úEmpty module namer   r
   zNo module named %.200s)r   Úfindr   Úimport_submoduleÚImportError)ÚmodZaltmodr!   Úbufr#   ÚsubnameÚnextÚresultr   r   r   Ú	load_nextz   s*    

r1   c                 C   s’   |t v r|tjv rtj| }nptd|ƒ dt |< tj |d¡}z$|durTt |¡}nt || ¡}W n   |rx|tj|< ‚ Y n0 t| |||ƒ |S )z,m = import_submodule(mod, subname, fullname)Z	Reloadingr   N)	Ú	found_nowr   r   Úprintr   Ú	importlibÚreloadÚimport_moduleÚadd_submodule)r,   r.   ÚfullnameÚmZoldmr   r   r   r*   ¤   s    

r*   c                 C   s.   | du rdS |du rt j| }t| ||ƒ dS )zmod.{subname} = submodN)r   r   Úsetattr)r,   Zsubmodr8   r.   r   r   r   r7   Á   s    
r7   c              	   C   s’   t | dƒsdS |D ]z}t |dƒs(tdƒ‚|dkrn|r6qz
| j}W n tyR   Y qŒ0 t| ||dƒ}|sŒ dS qt | |ƒst| ||d | ƒ qdS )	z,Handle 'from module import a, b, c' imports.r   Nr   z"Item in ``from list'' not a stringÚ*r   r   r   )r   Ú	TypeErrorÚ__all__ÚAttributeErrorÚensure_fromlistr*   )r,   Úfromlistr-   Ú	recursiveÚitemÚallÚretr   r   r   r?   Í   s"    



r?   r   c           	      C   sv   t ||ƒ\}}t||dk rdn|| |ƒ\}} }|}| rLt||| |ƒ\}} }q2|du r\tdƒ‚|sd|S t|||dƒ |S )zReplacement for __import__()r   Nr(   )r'   r1   r   r?   )	r!   r   Úlocalsr@   r   r&   r-   ÚheadÚtailr   r   r   Údeep_import_hookâ   s     rH   c                 C   sž   | t u r| S t| tƒstdƒ‚| j}|tjvr:td| ƒ‚z
t| W S    | t|< Y n0 z4zt	 
| ¡}W n   | tj|< ‚ Y n0 W t ¡  n
t ¡  0 |S )zReplacement for reload().z reload() argument must be modulez*reload(): module %.200s not in sys.modules)Útypesr   r   r<   r   r   r   r+   Úmodules_reloadingr4   r5   Úclear)r9   r!   Znewmr   r   r   Údeep_reload_hookú   s$    




rL   r   zos.pathÚbuiltinsÚ__main__Únumpyznumpy._globalsc                 C   s\   |D ]}dt |< qz>ttƒ� t| ƒW  d  ƒ W i a S 1 s@0    Y  W i a ni a 0 dS )aM  Recursively reload all modules used in the given module.  Optionally
    takes a list of modules to exclude from reloading.  The default exclude
    list contains modules listed in sys.builtin_module_names with additional
    sys, os.path, builtins and __main__, to prevent, e.g., resetting
    display, exception, and io hooks.
    r   N)r2   r	   rH   rL   )ÚmoduleÚexcludeÚir   r   r   r5     s    

þr5   )NNNr   )Ú__doc__rM   r   Ú
contextlibr   r4   r   rI   r   Úwarningsr   r   Zoriginal_importr	   r'   r1   r2   r*   r7   r?   rH   rJ   rL   r5   Zoriginal_reloadÚbuiltin_module_namesr   r   r   r   Ú<module>   sH   
I(
 ÿþýüûúùþ