o
    û›h×$  ã                   @   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   úLc:\Users\shubh\Videos\agent\nova\Lib\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 r¹t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(
 ÿþýüûúùþ