o
    Zh$                     @   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 _d S |t _w )N)builtin_mod
__import__)Z
new_importZsaved_import r   M/var/www/html/lang_env/lib/python3.10/site-packages/IPython/lib/deepreload.pyreplace_import_hook(   s   r	   c                 C   s  |}|r	t | tsdS | dd}|dur1t|dstdt|dkr.|dkr,tddS |}n<d| vr7dS | d }d	| v rF| | d< }n'|d
}|d  k rX|k rYtd |dk rcd| d< dS |d|  | d< }t|}t|ddD ]}z	|d
d|}W qw ty }	 ztd|	d}	~	ww |d| }z
t	j
| }
W |
|fS  ty }	 z|dk rtd|  d}
ntd| |	W Y d}	~	|
|fS d}	~	ww )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_levelZpkgnamenamemodnameZlastdotdotxeparentr   r   r   
get_parent1   sl   


	r&   c                 C   s   t |dkr| d|fS |d}|dkrtd|dk r!|}d}n|d| }||d d }|dkr7|d7 }||7 }t| ||}|du rU| |krUt|||}|durU|}|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*   


r0   c                 C   s   |t v r|tjv rtj| }|S td| dt |< tj|d}z|dur+t|}nt|| }W n   |r=|tj|<  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submodr7   r-   r   r   r   r6      s   
r6   c              	   C   s   t | dsdS |D ]=}t |dstd|dkr7|rq	z| j}W n	 ty)   Y q	w t| ||d}|s6 dS q	t | |sFt| ||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   sx   t ||\}}t||dk rdn|| |\}} }|}| r't||| |\}} }| s|du r/td|s3|S t|||d |S )zReplacement for __import__()r   Nr'   )r&   r0   r   r>   )	r    r   localsr?   r   r%   r,   headtailr   r   r   deep_import_hook   s    rG   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 zzt	
| }W n	   | tj|<  W t  |S t  w )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_reloadingr3   r4   clear)r8   r    Znewmr   r   r   deep_reload_hook   s(   





rK   r   zos.pathbuiltins__main__numpyznumpy._globalsc                 C   sZ   |D ]}dt |< qz tt t| W  d   W i a S 1 s w   Y  W i a dS i a w )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)r1   r	   rG   rK   )moduleexcludeir   r   r   r4     s   

r4   )NNNr   )__doc__rL   r   
contextlibr   r3   r   rH   r   warningsr   r   Zoriginal_importr	   r&   r0   r1   r)   r6   r>   rG   rI   rK   r4   Zoriginal_reloadbuiltin_module_namesr   r   r   r   <module>   sJ   
I(
 