
    ^h[                     d   S r SSKJr  SSKJrJrJr  SrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKJrJr  SSKJr   " S S	5      r/ S
QrS rS rS rS rS rS \4S \4S \4/r\R9                  S S 4/5        S r " S S5      r/ SQrSS jr \SS4S jr!\ " S S\5      5       r"S r#g)a  IPython extension to reload modules before executing user code.

``autoreload`` reloads modules automatically before entering the execution of
code typed at the IPython prompt.

This makes for example the following workflow possible:

.. sourcecode:: ipython

   In [1]: %load_ext autoreload

   In [2]: %autoreload 2

   In [3]: from foo import some_function

   In [4]: some_function()
   Out[4]: 42

   In [5]: # open foo.py in an editor and change some_function to return 43

   In [6]: some_function()
   Out[6]: 43

The module was reloaded without reloading it explicitly, and the object
imported with ``from foo import ...`` was also updated.

Usage
=====

The following magic commands are provided:

``%autoreload``, ``%autoreload now``

    Reload all modules (except those excluded by ``%aimport``)
    automatically now.

``%autoreload 0``, ``%autoreload off``

    Disable automatic reloading.

``%autoreload 1``, ``%autoreload explicit``

    Reload all modules imported with ``%aimport`` every time before
    executing the Python code typed.

``%autoreload 2``, ``%autoreload all``

    Reload all modules (except those excluded by ``%aimport``) every
    time before executing the Python code typed.

``%autoreload 3``, ``%autoreload complete``

    Same as 2/all, but also adds any new objects in the module. See
    unit test at IPython/extensions/tests/test_autoreload.py::test_autoload_newly_added_objects

  Adding ``--print`` or ``-p`` to the ``%autoreload`` line will print autoreload activity to
  standard out. ``--log`` or ``-l`` will do it to the log at INFO level; both can be used
  simultaneously.

``%aimport``

    List modules which are to be automatically imported or not to be imported.

``%aimport foo``

    Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1``

``%aimport foo, bar``

    Import modules 'foo', 'bar' and mark them to be autoreloaded for ``%autoreload 1``

``%aimport -foo``

    Mark module 'foo' to not be autoreloaded.

Caveats
=======

Reloading Python modules in a reliable way is in general difficult,
and unexpected things may occur. ``%autoreload`` tries to work around
common pitfalls by replacing function code objects and parts of
classes previously in the module with new versions. This makes the
following things to work:

- Functions and classes imported via 'from xxx import foo' are upgraded
  to new versions when 'xxx' is reloaded.

- Methods and properties of classes are upgraded on reload, so that
  calling 'c.foo()' on an object 'c' created before the reload causes
  the new code for 'foo' to be executed.

Some of the known remaining caveats are:

- Replacing code objects does not always succeed: changing a @property
  in a class to an ordinary method or a method to a member variable
  can cause problems (but in old objects only).

- Functions that are removed (eg. via monkey-patching) from a module
  before it is reloaded are not upgraded.

- C extension modules cannot be reloaded, and so cannot be autoreloaded.

- While comparing Enum and Flag, the 'is' Identity Operator is used (even in the case '==' has been used (Similar to the 'None' keyword)).

- Reloading a module, or importing the same module by a different name, creates new Enums. These may look the same, but are not.
    )magic_arguments)Magicsmagics_class
line_magicTN)import_modulereload)source_from_cachec                   R    \ rS rSrSr Sr Sr SS jrS rS r	S r
S	 rSS
 jrSrg)ModuleReloader   FTNc                     0 U l         0 U l        0 U l        0 U l        0 U l        Xl        S U l        U R                  SSS9  SU l        g )Nc                     g N msgs    W/home/james-whalen/.local/lib/python3.13/site-packages/IPython/extensions/autoreload.py<lambda>)ModuleReloader.__init__.<locals>.<lambda>   s    4    TF)	check_all	do_reload)	failedmodulesskip_modulesold_objectsmodules_mtimesshell_reportcheckhide_errors)selfr   s     r   __init__ModuleReloader.__init__   sR     
 ( 	

TU
3 !r   c                 ^     U R                   U	 SU R                  U'   g! [         a     Nf = f)z-Skip reloading the named module in the futureTN)r   KeyErrorr   r"   module_names     r   mark_module_skipped"ModuleReloader.mark_module_skipped   s9    	[) *.+&  		    
,,c                 ^     U R                   U	 SU R                  U'   g! [         a     Nf = f)z9Reload the named module in the future (if it is imported)TN)r   r&   r   r'   s     r   mark_module_reloadable%ModuleReloader.mark_module_reloadable   s9    	!!+. %)[!  		r+   c                     U R                  U5        [        U5        UR                  S5      S   n[        R                  U   nX24$ )zImport a module, and mark it reloadable

Returns
-------
top_module : module
    The imported module if it is top-level, or the top-level
top_name : module
    Name of top_module

.r   )r-   r   splitsysr   )r"   r(   top_name
top_modules       r   aimport_moduleModuleReloader.aimport_module   sF     	##K0k"$$S)!,[[*
##r   c                    [        US5      (       a  UR                  c  g[        USS 5      S;   a  gUR                  n[        R                  R                  U5      u  p4UR                  5       S:X  a  UnO [        U5      n [        R                  " U5      R                  nXV4$ ! [         a     gf = f! [         a     gf = f)N__file__)NN__name__)N__mp_main____main__z.py)hasattrr8   getattrospathsplitextlowerr	   
ValueErrorstatst_mtimeOSError)r"   modulefilenamer?   extpy_filenamepymtimes          r   filename_and_mtime!ModuleReloader.filename_and_mtime   s    vz**foo.E6:t,0QQ??GG$$X.	99;%"K"/9	ggk*33G ##  "!"
  		s$   7B&  B6 &
B32B36
CCc           
      \   U R                   (       d  U(       d  gU(       d  U R                  (       a(  [        [        R                  R                  5       5      nO#[        U R                  R                  5       5      nU GH%  n[        R                  R                  US5      nX@R                  ;   a  M5  U R                  U5      u  pgUc  MM   XpR                  U   ::  a  Mb   U R                  R                  US5      U:X  a  M   XpR                  U'   U(       d  M  U R                  SU S35         U R                  (       a'  [        U[        U R                   U R"                  5        O[        U[        U R                   5        X`R                  ;   a  U R                  U	 GM%  GM(     g! [         a    XpR                  U'    GMG  f = f!   U R$                  (       d<  ['        SR)                  U[*        R,                  " S5      5      [        R.                  S9  XpR                  U'    GM  = f)z/Check whether some modules need to be reloaded.NzReloading 'z'.z[autoreload of {} failed: {}]
   )file)enabledr   listr2   r   keysgetr   rK   r   r   r&   r   autoload_objsuperreloadr   r   r   r!   printformat	traceback
format_excstderr)r"   r   r   r   modnamemrI   rJ   s           r   r    ModuleReloader.check   s    ||I3;;++-.G4<<,,./GG.A+++#'#:#:1#= K"11':: ; ;;??;5@ A ,3( y{7)2677((#Avt/?/?L#Avt/?/?@"kk1 KK4 2=   /6##G,&7++;BB ')=)=b)A "%	 07KK,s    F*2A/G
*GG
AH+)r   r   r!   r   r   r   r   r   r   )FT)r9   
__module____qualname____firstlineno__rP   r   rT   r#   r)   r-   r5   rK   r    __static_attributes__r   r   r   r   r      s8    G*IDL=!,.)$$$437r   r   )__code____defaults____doc____closure____globals____dict__c           	      x    [          H  n [        X[        X5      5        M     g! [        [        4 a     M1  f = f)z%Upgrade the code object of a functionN)
func_attrssetattrr=   AttributeError	TypeError)oldnewnames      r   update_functionrp   1  s9    	Cws12  	* 		s   %99c                     [         R                  " U 5      nU H*  n[        U5      U L d  M  [        R	                  USU5        M,     g)zUse garbage collector to find all instances that refer to the old
class definition and update their __class__ to point to the new class
definition	__class__N)gcget_referrerstypeobject__setattr__)rm   rn   refsrefs       r   update_instancesrz   :  s<    
 C D9sK5 r   c           	         [        U R                  R                  5       5       HM  n[        X5      n [        X5      nX4:H  SL a  M#   [        UW5      (       a  M7   [        X[        X5      5        MO     [        UR                  R                  5       5       HB  nU[        U R                  R                  5       5      ;  d  M,   [        X[        X5      5        MD     [        X5        g! [         a'     [        X5        O! [        [        4 a     Of = f M  [         a     Nf = f! [        [        4 a     GM  f = f! [        [        4 a     M  f = f)zfReplace stuff in the __dict__ of a class, and upgrade
method code objects, and add new methods, if anyTN)rQ   rg   rR   r=   rk   delattrrl   rB   update_genericrj   rz   )rm   rn   keyold_objnew_objs        r   update_classr   F  s?    CLL%%'(##	c'G "t+ , '7++	Cgc/01 )8 CLL%%'(d3<<,,.//'#"34 ) S9  	!"I.  	 	 	* 		 #I. s_   C*D)E*
D&5D D&DD&DD&	D&%D&)D>=D>EEc                     [        U R                  UR                  5        [        U R                  UR                  5        [        U R                  UR                  5        g)z+Replace get/set/del functions of a propertyN)r}   fdelfgetfset)rm   rn   s     r   update_propertyr   p  s8    388SXX&388SXX&388SXX&r   c                 <    [        X5      =(       a    [        X5      $ r   )
isinstance)abtyps      r   isinstance2r   w  s    a4*Q"44r   c                 "    [        X[        5      $ r   )r   ru   r   r   s     r   r   r   |  s    +aD)r   c                 6    [        X[        R                  5      $ r   )r   typesFunctionTyper   s     r   r   r   }  s    +aE$6$67r   c                 "    [        X[        5      $ r   )r   propertyr   s     r   r   r   ~  s    +aH-r   c                 6    [        X[        R                  5      $ r   )r   r   
MethodTyper   s     r   r   r     s    Q5+;+;<r   c                 B    [        U R                  UR                  5      $ r   )rp   __func__r   s     r   r   r     s    QZZ@r   c                 P    [          H  u  p#U" X5      (       d  M  U" X5          g   g)NTF)UPDATE_RULES)r   r   
type_checkupdates       r   r}   r}     s)    *
a1L + r   c                        \ rS rSrS rS rSrg)	StrongRefi  c                     Xl         g r   obj)r"   r   s     r   r#   StrongRef.__init__  s    r   c                     U R                   $ r   r   )r"   s    r   __call__StrongRef.__call__  s    xxr   r   N)r9   r^   r_   r`   r#   r   ra   r   r   r   r   r     s    r   r   )r9   rd   __package__
__loader____spec__r8   
__cached____builtins__c                 H   [        US5      =(       a    UR                  U R                  :H  nU(       a  U(       d  U[        ;   a  gOU(       d  gU R                  U4n UR	                  U/ 5      R                  [        R                  " U5      5        g! [         a     gf = f)Nr^   FT)	r<   r^   r9   	mod_attrs
setdefaultappendweakrefry   rl   )rF   dro   r   autoload	in_moduler~   s          r   
append_objr     s    \*Ps~~/PITY.??D
!C	S"$$W[[%56   s   5B 
B! B!c                    Uc  0 n[        U R                  R                  5       5       H\  u  pE[        XXE5      (       d  M  U R                  U4n UR                  U/ 5      R                  [        R                  " U5      5        M^      U R                  R                  5       nU R                  nU R                  R                  5         XR                  S'   US   U R                  S'    U" U 5      n [        U R                  R                  5       5       H  u  pIU R                  U4nXb;  a+  Ub  US:X  d  [        XXIS5      (       d  M5  XR                  U'   / n
X&    H+  nU" 5       nUc  M  U
R                  U5        [!        X5        M-     U
(       a  XU'   M  X&	 M     U $ ! [         a     GM  f = f! [        [        [        4 a     Nf = f!   U R                  R                  W5        e = f)a  Enhanced version of the builtin reload function.

superreload remembers objects previously in the module, and

- upgrades the class dictionary of every old class in the module
- upgrades the code object of every old function and method
- clears the module's namespace before reloading

r9   r   EnumT)rQ   rg   itemsr   r9   r   r   r   ry   rl   copyclearrk   r&   r   user_nsr}   )rF   r   r   r   ro   r   r~   old_dictold_namer   new_refsold_refr   s                r   rU   rU     s     &////12	&t99%	""3+227;;s3CD 3??'')??&.
#(0(>% foo3356%! 6>!&tdKK")MM$"'GiGOOG$7, ( ' / 72 M_  		 ~x0 
x(s1   5F#
A F5 +G #
F21F25GGG.c                   :  ^  \ rS rSrU 4S jr\\R                  " 5       \R                  " S\SSSS9\R                  " SS	S
SSS9\R                  " SSS
SSS9\R                  " SS
SSS9SS j5       5       5       5       5       5       r	\SS j5       r
S rS rSrU =r$ )AutoreloadMagicsi  c                    > [         TU ]  " U0 UD6  [        U R                  5      U l        SU R                  l        SU R                  l        [        [        R                  5      U l
        g )NF)superr#   r   r   	_reloaderr   rT   setr2   r   loaded_modules)r"   r   kwrr   s      r   r#   AutoreloadMagics.__init__  sN    !"r"'

3#( &+#!#++.r   modenow?aG  blank or 'now' - Reload all modules (except those excluded by %%aimport)
             automatically now.

             '0' or 'off' - Disable automatic reloading.

             '1' or 'explicit' - Reload only modules imported with %%aimport every
             time before executing the Python code typed.

             '2' or 'all' - Reload all modules (except those excluded by %%aimport)
             every time before executing the Python code typed.

             '3' or 'complete' - Same as 2/all, but also but also adds any new
             objects in the module.
             )ru   defaultnargshelpz-pz--print
store_trueFz1Show autoreload activity using `print` statements)actionr   r   z-lz--logz)Show autoreload activity using the loggerz--hide-errorszHide autoreload errorsc                 ^  ^^ [         R                  " U R                  U5      nUR                  R	                  5       n[
        m[        R                  " S5      nUR                  mUU4S jnUR
                  SL a"  UR                  SL a  S U R                  l        OaUR
                  SL a2  UR                  SL a  XPR                  l        O2TU R                  l        O UR                  SL a  TU R                  l        UR                  U R                  l        US:X  d  US:X  a  U R                  R                  S5        gUS:X  d  US	:X  a  SU R                  l        gUS
:X  d  US:X  a4  SU R                  l        SU R                  l        SU R                  l        gUS:X  d  US:X  a4  SU R                  l        SU R                  l        SU R                  l        gUS:X  d  US:X  a4  SU R                  l        SU R                  l        SU R                  l        g[#        SU S35      e)a  %autoreload => Reload modules automatically

%autoreload or %autoreload now
Reload all modules (except those excluded by %aimport) automatically
now.

%autoreload 0 or %autoreload off
Disable automatic reloading.

%autoreload 1 or %autoreload explicit
Reload only modules imported with %aimport every time before executing
the Python code typed.

%autoreload 2 or %autoreload all
Reload all modules (except those excluded by %aimport) every time
before executing the Python code typed.

%autoreload 3 or %autoreload complete
Same as 2/all, but also but also adds any new objects in the module. See
unit test at IPython/extensions/tests/test_autoreload.py::test_autoload_newly_added_objects

The optional arguments --print and --log control display of autoreload activity. The default
is to act silently; --print (or -p) will print out the names of modules that are being
reloaded, and --log (or -l) outputs them to the log at INFO level.

The optional argument --hide-errors hides any errors that can happen when trying to
reload code.

Reloading Python modules in a reliable way is in general
difficult, and unexpected things may occur. %autoreload tries to
work around common pitfalls by replacing function code objects and
parts of classes previously in the module with new versions. This
makes the following things to work:

- Functions and classes imported via 'from xxx import foo' are upgraded
  to new versions when 'xxx' is reloaded.

- Methods and properties of classes are upgraded on reload, so that
  calling 'c.foo()' on an object 'c' created before the reload causes
  the new code for 'foo' to be executed.

Some of the known remaining caveats are:

- Replacing code objects does not always succeed: changing a @property
  in a class to an ordinary method or a method to a member variable
  can cause problems (but in old objects only).

- Functions that are removed (eg. via monkey-patching) from a module
  before it is reloaded are not upgraded.

- C extension modules cannot be reloaded, and so cannot be
  autoreloaded.


autoreloadc                 &   > T" U 5        T" U 5        g r   r   )r   lps    r   pl'AutoreloadMagics.autoreload.<locals>.plv  s    cFcFr   Fc                     g r   r   r   s    r   r   -AutoreloadMagics.autoreload.<locals>.<lambda>{  s    r   T r   0off1explicit2all3completezUnrecognized autoreload mode "z".N)r   parse_argstringr   r   rA   rV   logging	getLoggerinfologr   r   r!   r    rP   r   rT   rB   )r"   lineargsr   loggerr   r   r   s         @@r   r   AutoreloadMagics.autoreload  s   B ..tEyy ""<0KK	 ::488u#4%5DNN"ZZ4xx4)+&)*&XX%&DNN"%)%5%5"2:NN  &S[DEM%*DNN"S[DJ.%)DNN"',DNN$*/DNN'S[DEM%)DNN"'+DNN$*/DNN'S[DJ.%)DNN"'+DNN$*.DNN'=dV2FGGr   c                 L   UnU(       d  [        U R                  R                  R                  5       5      n[        U R                  R                  R                  5       5      nUc  [
        R                  nU R                  R                  (       a  UR                  S5        O#UR                  SSR                  U5      -  5        UR                  SSR                  U5      -  5        gUR                  S5       Vs/ s H  ofR                  5       PM     sn H  nUR                  S5      (       a0  USS R                  5       nU R                  R                  U5        MI  U R                  R                  U5      u  pU R                  R!                  X05        M     gs  snf )	a  %aimport => Import modules for automatic reloading.

%aimport
List modules to automatically import and not to import.

%aimport foo
Import module 'foo' and mark it to be autoreloaded for %autoreload explicit

%aimport foo, bar
Import modules 'foo', 'bar' and mark them to be autoreloaded for %autoreload explicit

%aimport -foo, bar
Mark module 'foo' to not be autoreloaded for %autoreload explicit, all, or complete, and 'bar'
to be autoreloaded for mode explicit.
Nz&Modules to reload:
all-except-skipped
zModules to reload:
%s
 z
Modules to skip:
%s
,-   )sortedr   r   rR   r   r2   stdoutr   writejoinr1   strip
startswithr)   r5   r   push)
r"   parameter_sstreamr[   	to_reloadto_skip__moduler4   r3   s
             r   aimportAutoreloadMagics.aimport  s3   " t~~55::<=IT^^88==?@G~~~''GH7#((9:MMNLL3chhw6GGH/6}}S/AB/A!GGI/AB%%c**%abk//1GNN66w?+/>>+H+H+Q(J JJOOX$:; CBs   >F!c                     U R                   R                  (       a   U R                   R                  5         g g !    g = fr   )r   rP   r    )r"   r   s     r   pre_run_cellAutoreloadMagics.pre_run_cell  s3    >>!!$$& "s   9 =c                 .   [        [        R                  5      U R                  -
  nU HN  nU R                  R                  [        R                  U   5      u  p4Uc  M6  X@R                  R                  U'   MP     U R                  R                  U5        g)zFCache the modification times of any modules imported in this executionN)r   r2   r   r   r   rK   r   r   )r"   newly_loaded_modulesr[   r   rJ   s        r   post_execute_hook"AutoreloadMagics.post_execute_hook  st    "3;;/$2E2EE+G::3;;w;OPJA"9@--g6 ,
 	""#78r   )r   r   )r   )r   N)r9   r^   r_   r`   r#   r   r   argumentstrr   r   r   r  ra   __classcell__)rr   s   @r   r   r     s    / $$&( @ 8 %	aH) ' TaHF $< $<L9 9r   r   c                     [        U 5      nU R                  U5        U R                  R                  SUR                  5        U R                  R                  SUR
                  5        g)zLoad the extension in IPython.r   post_executeN)r   register_magicseventsregisterr   r  )ipauto_reloads     r   load_ipython_extensionr    sN    "2&K{#II~{'?'?@II~{'D'DEr   )F)$rd   IPython.corer   IPython.core.magicr   r   r   __skip_doctest__r>   r2   rX   r   r   rs   r   	importlibr   r   importlib.utilr	   r   ri   rp   rz   r   r   r   r   extendr}   r   r   r   rU   r   r  r   r   r   <module>r     s   iV ) ? ? $ 
 
    	  + ,O7 O7n
	6'T'5
 *<87I-?
    =@	
 		$  &4t CV K9v K9 K9\Fr   