
    ^hZ                     Z   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rSSK	r	SSK
r
SSKJr  SSKJr  SSKJs  Jr  SSKJr  SSKJr  SSKJrJr  SSKJr  SS	KJr  \R<                  " 5       S
:X  a  \R>                  " SSS9  S r  " S S5      r! " S S\5      r"S r# " S S\"5      r$g)z Tests for autoreload extension.
    N)StringIO)	dataclass)TestCase)AutoreloadMagics)EventManagerpre_run_cell)skipif_not_numpy)ExecutionInfoPyPyz;Current autoreload implementation is extremely slow on PyPyT)allow_module_levelc                      g N )akws     b/home/james-whalen/.local/lib/python3.13/site-packages/IPython/extensions/tests/test_autoreload.py<lambda>r   /   s        c                   R    \ rS rSrS r\=rr     S
S jrS r	S r
S rSS jrS	rg)	FakeShell2   c                     0 U l         U R                   U l        0 U l        [        U S[        15      U l        [        U S9U l        U R
                  R                  SU R                  R                  5        g )Nr   )shell)	nsuser_nsuser_ns_hiddenr   r   eventsr   auto_magicsregisterselfs    r   __init__FakeShell.__init__3   s\    ww "4.,)GH+$7^T-=-=-J-JKr   Nc                 .    [         R                  " 5         g r   )	traceback	print_exc)r!   	exc_tuplefilename	tb_offsetexception_onlyrunning_compiled_codes         r   showtracebackFakeShell.showtraceback=   s     	r   c                     U R                   R                  S[        SSSSS S95        [        XR                  5        U R
                  R                  5         g )Nr    F)raw_cellstore_historysilentshell_futurescell_id)r   triggerr
   execr   r   post_execute_hook)r!   codes     r   run_codeFakeShell.run_codeG   sN    ##		
 	T<< **,r   c                 :    U R                   R                  U5        g r   )r   update)r!   itemss     r   pushFakeShell.pushU   s    ur   c                 :    U R                   R                  U5        g r   )r   
autoreload)r!   	parameters     r   magic_autoreloadFakeShell.magic_autoreloadX   s    ##I.r   c                 j    U R                   R                  XS9  U R                   R                  5         g )Nstream)r   aimportr7   )r!   rB   rG   s      r   magic_aimportFakeShell.magic_aimport[   s+       :**,r   )r   r   r   r   r   )NNNFFr   )__name__
__module____qualname____firstlineno__r"   noopregister_magicsset_hookr,   r9   r>   rC   rI   __static_attributes__r   r   r   r   r   2   s>    L "&%Oh #-/-r   r   c                   B    \ rS rSrSrSrSrSrS rS r	S r
S rS	 rS
rg)Fixture`   z&Fixture for creating test module filesN#abcdefghijklmopqrstuvwxyz0123456789c                     [         R                  " 5       U l        [        [        R
                  5      U l        [        R
                  R                  SU R                  5        [        5       U l	        g )Nr   )
tempfilemkdtemptest_dirlistsyspathold_sys_pathinsertr   r   r    s    r   setUpFixture.setUpg   sA     ((* N4==)[
r   c                     [         R                  " U R                  5        U R                  [        l        S U l        S U l        S U l        g r   )shutilrmtreerZ   r^   r\   r]   r   r    s    r   tearDownFixture.tearDownm   s5    dmm$$$ 
r   c                    SSR                  [        R                  " U R                  S5      5      -   nU[        R
                  ;   a  [        R
                  U	 [        R                  R                  U R                  US-   5      nX4$ )Ntmpmod_r/      z.py)	joinrandomsamplefilename_charsr\   modulesosr]   rZ   )r!   module_name	file_names      r   
get_moduleFixture.get_moduleu   sd    "''&--8K8KR*P"QQ#++%K(GGLLe0CD	%%r   c                     [         R                  " U5      n[        R                  " S5        [	        USSS9 nUR                  U5        SSS5        g! , (       d  f       g= f)a  
Write a file, and force a timestamp difference of at least one second

Notes
-----
Python's .pyc files record the timestamp of their compilation
with a time resolution of one second.

Therefore, we need to force a timestamp difference between .py
and .pyc, without having the .py file be timestamped in the
future, and without changing the timestamp of the .pyc file
(because that is stored in the file).  The only reliable way
to achieve this seems to be to sleep.
g?wutf-8encodingN)textwrapdedenttimesleepopenwrite)r!   r(   contentfs       r   
write_fileFixture.write_file|   sF     //'*

4 (C'2aGGG 322s   A
A!c                     [         R                  " U5      nU R                  5       u  p#[        USSS9 nUR	                  U5        S S S 5        X#4$ ! , (       d  f       X#4$ = f)Nru   rv   rw   )ry   rz   rr   r}   r~   )r!   r8   mod_namemod_fnr   s        r   
new_moduleFixture.new_module   sX    t$??,&#0AGGDM 1 10s   A
A!)r^   r   rZ   )rK   rL   rM   rN   __doc__rZ   r^   rm   r`   re   rr   r   r   rR   r   r   r   rT   rT   `   s-    0HL:N!&. r   rT   c                     U R                   R                  n[        U SS5      n[        R                  U   nUR                  S5       H  n[        X45      nM     U$ )z3
Original issue comes from pickle; hence the name.
rL   N.)	__class__rK   getattrr\   rn   split)objnamerp   obj2subpaths        r   pickle_get_current_classr      sP     ==!!D#|T2K;;{#D::c?t% #Kr   c                   d    \ rS rSrS rS rS r\S 5       rS r	S r
S rS	 rSS
 jrS rS rSrg)TestAutoreload   c                    U R                  [        R                  " S5      5      u  pU R                  R	                  S5        U R                  R                  U5        U R                  U[        R                  " S5      5        [        R                  " SU-  SS9   U R                  R                  S5        S S S 5        g ! , (       d  f       g = f)Nz
                                from enum import Enum
                                class MyEnum(Enum):
                                    A = 'A'
                                    B = 'B'
                            2a  
                                from enum import Enum
                                class MyEnum(Enum):
                                    A = 'A'
                                    B = 'B'
                                    C = 'C'
                            [autoreload of %s failed:stderrchannelpass)
r   ry   rz   r   rC   rI   r   ttAssertNotPrintsr9   r!   r   r   s      r   test_reload_enums TestAutoreload.test_reload_enums   s    ??OO 	
 	

##C(

  *OO 	
 (83h
 JJ'
 
 
s   C
Cc                 t   U R                   R                  S5        U R                  S5      u  pSU R                   R                  ;  d   eSU R                   R                  ;  d   eU R                   R	                  SU-  5        U R                   R	                  S5        U R                  US5        U R                   R                  S   nU R                   R	                  S5        [        U5      n[        X45      (       d   eU R                   R	                  S	5        U R                   R	                  S
5        g )Nr   zd
            class Test():
                def meth(self):
                    return "old"
        testresultzfrom %s import Testztest = Test()zd
            class Test():
                def meth(self):
                    return "new"
        r   zimport picklezp = pickle.dumps(test))r   rC   r   r   r9   r   r   
isinstance)r!   r   r   test_object
test_classs        r   test_reload_class_type%TestAutoreload.test_reload_class_type   s    

##C(??
 TZZ]]***tzz}},,,

1H<=

O,	
 jjmmF+ 	

F#-k:
+2222 	

O,

45r   c                 F   U R                   R                  S5        U R                  [        R                  " S5      5      u  pU R                   R                  SU-  5        U R                   R                  S5        U R                   R                  S5        U R                  [        5         U R                   R                  S5        S S S 5        U R                  [        5         U R                   R                  S5        S S S 5        U R                   R                  S5        U R                  [        5         U R                   R                  S	5        S S S 5        U R                  U[        R                  " S
5      5        U R                   R                  S5        S GH  nU R                   R                  U S35        U R                  [        5         U R                   R                  U S35        S S S 5        U R                  [        5         U R                   R                  U S35        S S S 5        U R                   R                  U S35        U R                   R                  U S35        U R                  [        5         U R                   R                  U S35        S S S 5        GM     g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       GM  = f)Nr   a  
                                class MyClass:

                                    def __init__(self, a=10):
                                        self.a = a
                                        self.b = 22 
                                        # self.toto = 33

                                    def square(self):
                                        print('compute square')
                                        return self.a*self.a
                            from %s import MyClasszfirst = MyClass(5)zfirst.square()zfirst.cube()zfirst.power(5)zfirst.bz
first.totoa  
                            class MyClass:

                                def __init__(self, a=10):
                                    self.a = a
                                    self.b = 11

                                def power(self, p):
                                    print('compute power '+str(p))
                                    return self.a**p
                            zsecond = MyClass(5)>   firstsecondz	.power(5)z.cube()z	.square()z.bz.az.toto)	r   rC   r   ry   rz   r9   assertRaisesAttributeErrorr   )r!   r   r   object_names       r   test_reload_class_attributes+TestAutoreload.test_reload_class_attributes   s=   

##C(??OO 
  	

4x?@

01

,-~.JJ/ /~.JJ 01 /

I&~.JJ- /
 	OO
 	
" 	

12.KJJ;-y 9:"">2

##{m7$;< 3"">2

##{m9$=> 3JJ;-r 23JJ;-r 23"">2

##{m5$9: 32 /; /... /.4 3222 32sH   ,J8%K
9KK.K?L8
K

K
K+.
K<	?
L	
L 	c                    U R                   R                  S5        U R                   R                  S5        U R                  [        R
                  " S5      5      u  pU R                   R                  SU-  5        U R                   R                  S5        U R                  U[        R
                  " S5      5        [        R                  " SU-  SS	9   U R                   R                  S
5        S S S 5        g ! , (       d  f       g = f)Nr   z1+1z
                                import numpy as np
                                class MyClass:
                                    a = (np.array((.1, .2)),
                                         np.array((.2, .3)))
                            r   zfirst = MyClass()z
                                import numpy as np
                                class MyClass:
                                    a = (np.array((.3, .4)),
                                         np.array((.5, .6)))
                            r   r   r   r   )	r   rC   r9   r   ry   rz   r   r   r   r   s      r   test_comparing_numpy_structures.TestAutoreload.test_comparing_numpy_structures,  s    

##C(

E"??OO 	
 	

4x?@

/0 	OO 
	
 (83h
 JJ'
 
 
s   C::
Dc                 0   U R                   R                  S5        SnU R                  [        R                  " U5      5      u  p#U R                   R                  SU S35        U R                   R                  S5        U R                  [        5         U R                   R                  S5        S S S 5        U R                  [        5         U R                   R                  S5        S S S 5        U R                  [        5         U R                   R                  S5        S S S 5        S	nU R                  U[        R                  " U5      5        U R                   R                  S5        U R                   R                  S
U S35        U R                   R                  S5        U R                   R                  S5        U R                   R                  S5        SnU R                  U[        R                  " U5      5        U R                   R                  S5        U R                   R                  S5        U R                   R                  S5        U R                   R                  S5        [        R                  S:  a  U R                   R                  S5        SnU R                  [        R                  " U5      5      u  pgSU S3nU R                  U[        R                  " U5      5        U R                   R                  S5        U R                   R                  S5        U R                   R                  S5        U R                   R                  S5        g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNk= f)N3#
        def func1(): pass
        zfrom z	 import *zfunc1()zfunc2()z
t = Test()numberz
        def func1(): pass
        def func2(): pass
        class Test: pass
        number = 0
        from enum import Enum
        class TestEnum(Enum):
            A = 'a'
        zimport sys; sys.modules['z
'].func2()z
TestEnum.Aa%  
        def func1(): return 'changed'
        def func2(): return 'changed'
        class Test:
            def new_func(self):
                return 'changed'
        number = 1
        from enum import Enum
        class TestEnum(Enum):
            A = 'a'
            B = 'added'
        zassert func1() == 'changed'zassert func2() == 'changed'z,t = Test(); assert t.new_func() == 'changed'zassert number == 1)      z"assert TestEnum.B.value == 'added'z
        from enum import Enum
        class Ext(Enum):
            A = 'ext'
        def ext_func():
            return 'ext'
        class ExtTest:
            def meth(self):
                return 'ext'
        ext_int = 2
        z
        from z import *
        zassert Ext.A.value == 'ext'zassert ext_func() == 'ext'z't = ExtTest(); assert t.meth() == 'ext'zassert ext_int == 2)r   rC   r   ry   rz   r9   r   	NameErrorr   r\   version_info)	r!   mod_coder   r   new_codenew_mod_codenew_mod_name
new_mod_fncurrent_mod_codes	            r   !test_autoload_newly_added_objects0TestAutoreload.test_autoload_newly_added_objectsO  s   

##C(  ??8??8+DE

eH:Y78

I&y)JJ	* *y)JJ- *y)JJ) *
 	 9: 	

I&

7zLM

L)

H%

L) 	 9:

9:

9:

JK

01g%JJ DE
 $(??8??<3P#Q  ^ 	 	0@ AB

9:

89

EF

12U *)))))s$   M"M4N"
M14
N
Nc                   ^ ^ [          " S S5      5       mUU 4S jnU" S5      U" S5      :X  d   eU" S5      U" S5      :X  d   eU" S5      U" S5      :X  d   eU" S	5      U" S
5      :X  d   eU" S5      U" S5      :X  d   eT R                  [        5         T R                  R	                  S5        S S S 5        g ! , (       d  f       g = f)Nc                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)=TestAutoreload.test_verbose_names.<locals>.AutoreloadSettingsi  	check_allenabledautoload_objr   N)rK   rL   rM   rN   bool__annotations__rR   r   r   r   AutoreloadSettingsr     s    OMr   r   c                    > TR                   R                  U 5        TR                   R                  R                  nT" UR                  UR
                  UR                  5      $ r   )r   rC   r   	_reloaderr   r   r   )modemodule_reloaderr   r!   s     r   gather_settings:TestAutoreload.test_verbose_names.<locals>.gather_settings  sR    JJ''-"jj44>>O%))'',, r   0offOFF1explicitr   allr   complete4)r   r   
ValueErrorr   rC   )r!   r   r   s   ` @r   test_verbose_names!TestAutoreload.test_verbose_names  s    		 	 
	
	 s#u'====s#u'====s#z'BBBBs#u'====s#z'BBBB z*JJ'', +**s   B;;
C	c                    U R                   R                  R                  nU R                   R                  S5        UR                  S   SL d   eSUR
                  R                  5       ;  d   eU R                   R                  S5        UR
                  S   SL d   eSUR                  R                  5       ;  d   eU R                   R                  S5        UR                  S   SL d   eSUR
                  R                  5       ;  d   eUR
                  S   SL d   eSUR                  R                  5       ;  d   eg )Nro   Tz-mathmathz	-os, math)r   r   r   rI   rn   skip_moduleskeys)r!   r   s     r   test_aimport_parsing#TestAutoreload.test_aimport_parsing  s*   **00::

  &&&t,444?77<<>>>>

  )++F3t;;;_4499;;;;

  	
 &&v.$666_99>>@@@@++D1T999?22779999r   c                 4   U R                   R                  S5        SnU R                  U5      u  p#U R                   R                  SU 35        [        R
                  " SSS9   U R                   R                  S5        S S S 5        U R                   R                  S5        U R                  X15        [        R
                  " S	U S
3SS9   U R                   R                  S5        S S S 5        U R                   R                  S5        U R                  X15        [        R
                  " S	U S
3SS9   U R                   R                  S5        S S S 5        U R                   R                  S5        U R                  X15        [        R
                  " S	U S
3SS9   U R                   R                  S5        S S S 5        U R                   R                  S5        U R                  X15        U R                  SS9 nU R                   R                  S5        S S S 5        WR                  SU S
3/:X  d   eU R                   R                  S5        U R                  X15        U R                  SS9 nU R                   R                  S5        S S S 5        UR                  SU S
3/:X  d   eg ! , (       d  f       GN7= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN2= f! , (       d  f       N= f! , (       d  f       N= f)Nr   r   zimport r/   stdoutr   r   zcomplete --printzReloading 'z'.zcomplete -pzcomplete --print --logrA   )loggerzINFO:autoreload:Reloading 'zcomplete -l)	r   rC   r   r9   r   AssertPrintsr   
assertLogsoutput)r!   r   r   r   los        r   test_autoreload_output%TestAutoreload.test_autoreload_output  so   

##J/  ??84

ghZ01__R2JJ' 3 	

##$67)__(2&
 JJ'

 	

##M2)__(2&
 JJ'

 	

##$<=)__(2&
 JJ'

 	

##$<=)__L_1RJJ' 2yy:8*BGHHHH

##M2)__L_1RJJ' 2yy:8*BGHHHHE 32

 

 

 
 21 21sH   $J0K6KK&>K84L	0
J?
K
K#&
K58
L	
Lc                   ^ ^^^^	 T R                  S5      u  p#U(       a  T R                  R                  S5        T R                  R                  U5        [	        5       nT R                  R                  SUS9  T R                  SU-  UR                  5       5        T R                  [        5         T R                  R                  S5        SSS5        OT R                  R                  S5        T R                  R                  S	U-  5        [	        5       nT R                  R                  SUS9  T R                  S
UR                  5       ;   5        T R                  UT R                  R                  5        [        R                  U   mTR                  mTR                  S5      mTR!                  5       m	UUUU	U 4S jnU" 5         T R#                  US5        [$        R&                  " SU-  SS9   T R                  R                  S5        SSS5        [$        R(                  " SU-  SS9   T R                  R                  S5        SSS5        U" 5         T R#                  US5        UUUU	U 4S jnT R                  R                  S5        U" 5         [*        R,                  " U5        T R                  R                  S5        U" 5         U(       a  T R                  R                  SU-   5        [	        5       nT R                  R                  SUS9  T R                  SU-  UR                  5       ;   5        T R                  R                  S5        OT R                  R                  S5        T R#                  US5        T R                  R                  S5        T R                  R                  S5        U" 5         U(       a  T R                  R                  U5        OT R                  R                  S5        T R                  R                  S5        T R/                  TR0                  S5        g! , (       d  f       GN= f! , (       d  f       GND= f! , (       d  f       GN= f)z\
Functional test for the automatic reloader using either
'%autoreload 1' or '%autoreload 2'
a  
x = 9

z = 123  # this item will be deleted

def foo(y):
    return y + 3

class Baz(object):
    def __init__(self, x):
        self.x = x
    def bar(self, y):
        return self.x + y
    @property
    def quux(self):
        return 42
    def zzz(self):
        '''This method will be deleted below'''
        return 99

class Bar:    # old-style class: weakref doesn't work for it on Python < 2.7
    def foo(self):
        return 1
r   r/   rF   zModules to reload:
%stmpmod_as318989e89dsNr   z	import %sz%Modules to reload:
all-except-skipped	   c                    > TR                  TR                  S5        TR                  TR                  S5        TR                  T" S5      S5        TR                  TR                  S5      S5        TR	                  S5      n TR                  TR                  S5      S5        TR                  U R                  S5      S5        TR                  U R                  S5        TR                  U R                  5       S5        TR                  5       nTR                  TR                  5       S5        TR                  UR                  5       S5        g )	Nr   {   r   r      
   *   c   )	assertEqualxzfooBazbarquuxzzzBarr   r   modold_fooold_objold_obj2r!   s     r   check_module_contents>TestAutoreload._check_smoketest.<locals>.check_module_contents=  s    SUUA&SUUC(WQZ+SWWQZ+''!*CW[[^R0SWWQZ,SXXr*SWWY+779DX\\^Q/TXXZ+r   z
a syntax error
r   r   r   r   a  
x = 10

def foo(y):
    return y + 4

class Baz(object):
    def __init__(self, x):
        self.x = x
    def bar(self, y):
        return self.x + y + 1
    @property
    def quux(self):
        return 43

class Bar:    # old-style class
    def foo(self):
        return 2
c                    > TR                  TR                  S5        TR                  [        TS5      5        TR                  T" S5      S5        TR                  TR	                  S5      S5        TR                  S5      n TR                  TR                  S5      S5        TR                  U R                  S5      S5        TR                  TR                  S5        TR                  U R                  S5        TR                  [        TS	5      5        TR                  [        U S	5      5        TR                  5       nTR                  TR	                  5       S
5        TR                  UR	                  5       S
5        g )Nr   r   r      r   r      +   r      )	r   r   assertFalsehasattrr   r   r   r   r   r  s     r   r  r    s   SUUB'WS#./WQZ+SWWQZ+''!*CW[[^R0SWWQZ,W\\2.SXXr*WWe45WS%01779DX\\^Q/TXXZ+r   -zModules to skip:
%sz-tmpmod_as318989e89dsr   z	
x = -99
i)r   r   rC   rI   r   assertIngetvaluer   ImportErrorr9   
assertTruer   r\   rn   r   r   r   r   r   r   r   ro   unlinkr   r   )
r!   use_aimportr   r   rG   r  r  r  r  r  s
   `     @@@@r   _check_smoketestTestAutoreload._check_smoketest  su     ??
< JJ'',JJ$$X.ZFJJ$$R$7MM3h>ARS"";/

(()?@ 0/ JJ'',JJh 67ZFJJ$$R$7OO8FOO<MM 	h

.kk(#
 ''''!*779	, 	," 	 		
 __(83h
 JJ'
 (83h
 JJ'
 	
 		
.	, 	,* 	

F#
 			&

F#
 JJ$$S8^4ZFJJ$$R$7OO3h>6??CTTU JJ$$%<=JJ'',	
 	

F#

F#
 JJ$$X.JJ''+

F#$s 0/f
 

 
s$   2P>7Q3Q">
Q
Q"
Q1c                 "    U R                  SS9  g )NTr  r  r    s    r   test_smoketest_aimport%TestAutoreload.test_smoketest_aimport  s    $/r   c                 "    U R                  SS9  g )NFr  r  r    s    r   test_smoketest_autoreload(TestAutoreload.test_smoketest_autoreload  s    %0r   r   N)T)rK   rL   rM   rN   r   r   r   r	   r   r   r   r   r   r  r  r  rR   r   r   r   r   r      sQ    (<"6H;;z  (  (DS3j-6:&)IVD%L01r   r   )%r   ro   platformpytestr\   rX   ry   rc   rk   r{   r%   ior   dataclassesr   IPython.testing.toolstestingtoolsr   unittestr   IPython.extensions.autoreloadr   IPython.core.eventsr   r   IPython.testing.decoratorsr	   IPython.core.interactiveshellr
   python_implementationskiprO   r   rT   r   r   r   r   r   <module>r.     s    
   
        ! " "  : : 7 7!!#v-
KKE +- +-\8 h 8 @	[1W [1r   