
    f$                         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        Zd Zd Zi ad	 Zd
 Zd ZddZi ad Zej.                  Zg ej2                  ddddddfdZy)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              #      K   t         j                  }| t         _        	 d  |t         _        y # |t         _        w xY ww)N)builtin_mod
__import__)
new_importsaved_imports     Q/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/lib/deepreload.pyreplace_import_hookr   (   s1     ))L'K.!-s   ?/ ?<?c                    |}|rt        | t              sy| j                  dd      }|9t        |d      st	        d      t        |      dk(  r|dkD  rt	        d      y|}nSd| vry| d   }d	| v r|x| d<   }n=|j                  d
      }|dcxk  r|k  rt	        d       |dk  rd| d<   y|d| x| d<   }t        |      }t        |dd      D ]  }	 |j                  d
d|      } |d| }	 t        j                  |   }
|
|fS # t        $ r}	t	        d      |	d}	~	ww xY w# t        $ r2}	|dk  rt        d|z         d}
nt        d|z        |	Y d}	~	|
|fS d}	~	ww xY 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level
orig_levelpkgnamenamemodnamelastdotdotxeparents              r   
get_parentr-   1   s    J
7D1kk-.Gw)<==w<1qy !KLL W$*% ,33GM"T mmC(G"U" !KLL #{)-&,3HW,==GM"T
d)C5!R  /	/++c1c*C/ :D	GT" 4<)  	/ ' (-./	/  G>  ! "F 8:>? @EFG  4<Gs0   D-D! 	DDD!	E*%EEc                 *   t        |      dk(  r| d|fS |j                  d      }|dk(  rt        d      |dk  r|}d}n|d| }||dz   d }|dk7  r|dz  }||z  }t        | ||      }|| |k7  rt        |||      }||}|t	        d|z        |||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)modaltmodr&   bufr)   subnamenextresults           r   	load_nextr9   z   s     4yA~ D#~
))C.C
ax,--
Qwt*CEF|
bys
7NCc7C0F~#-!&'7;C~2T9::4    c                 ~   |t         v r'|t        j                  v rt        j                  |   }|S t        d|       dt         |<   t        j                  j	                  |d      }	 |t        j                  |      }nt        j                  ||       }t        | |||       |S #  |r|t        j                  |<    xY w)z,m = import_submodule(mod, subname, fullname)	Reloadingr   N)		found_nowr   r   printr   	importlibreloadimport_moduleadd_submodule)r3   r6   fullnamemoldms        r   r1   r1      s     9S[[!8KK!& H# 	k8$	({{x.
	$$T*++GS9 	c1h0H	 (,H%s   &.B$ $B<c                 N    | y|t         j                  |   }t        | ||       y)zmod.{subname} = submodN)r   r   setattr)r3   submodrC   r6   s       r   rB   rB      s,    
{~X&C&!
r:   c                    t        | d      sy|D ]a  }t        |d      st        d      |dk(  r#|r"	 | j                  }t        | ||d      }|r@ yt        | |      rOt        | ||dz   |z          c y# t        $ r Y pw xY w)	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__ensure_fromlistAttributeErrorr1   )r3   fromlistr5   	recursiveitemallrets          r   rM   rM      s    3
# :tX&@AA3;kk &c3Q7d#S$c	D(89: " s   A55	B Bc                     t        ||      \  }}t        ||dk  rdn|| |      \  }} }|}| rt        ||| |      \  }} }| r|t        d      |s|S t        |||d       |S )zReplacement for __import__()r   Nr/   )r-   r9   r   rM   )	r&   r"   localsrO   r#   r,   r5   headtails	            r   deep_import_hookrX      s    We,KFC	vtSQOD$D
#D$c:dC  |,--D(C+Kr:   c                    | t         u r| S t        | t              st        d      | j                  }|t
        j                  vrt        d|z        	 t        |   S #  | t        |<   Y nxY w	 t        j                  |       }n#  | t
        j                  |<    xY w	 t        j                          |S # t        j                          w xY w)zReplacement for reload().z reload() argument must be modulez*reload(): module %.200s not in sys.modules)typesr   r   rK   r   r   r   r2   modules_reloadingr?   r@   clear)rD   r&   newms      r   deep_reload_hookr^      s     	Eza$:;;::D3;;FMNN$ &&$"#$""D 	
 	!K 	!s*   A A)-B B3 BB3 3C	r   zos.pathbuiltins__main__numpyznumpy._globalsc                     |D ]  }dt         |<    	 t        t              5  t        |       cddd       i a S # 1 sw Y   i a yxY w# i a w xY 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)r=   r   rX   r^   )moduleexcludeis      r   r@   r@     sZ    &  	! !12 	,#F+	, 	, 		, 	B	s$   A 8	A AA A A
)NNNr   )__doc__r_   r   
contextlibr   r?   r   rZ   r   warningsr   r   original_importr   r-   r9   r=   r1   rB   rM   rX   r[   r^   r@   original_reloadbuiltin_module_names r:   r   <module>rm      s   6  %  
   ((. .GR$P 	:
:*,  @ ""
		!	! 	 		
 	 	 	r:   