
    fyX                     J   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmc mZ ddlmZ ddlmZ ddlmZmZ ddlmZ  ej6                         d	k(  r ej8                  d
d       d Z G d d      Z G d de      Zd Z  G d de      Z!y)z Tests for autoreload extension.
    N)StringIO)	dataclass)TestCase)AutoreloadMagics)EventManagerpre_run_cell)skipif_not_numpyPyPyz;Current autoreload implementation is extremely slow on PyPyT)allow_module_levelc                       y N )akws     c/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/extensions/tests/test_autoreload.py<lambda>r   -   s        c                   4    e Zd Zd ZexZZd Zd Zd Z	ddZ
y)	FakeShellc                     i | _         | j                   | _        i | _        t        | dt        h      | _        t        |       | _        | j
                  j                  d| j                  j                         y )Nr   )shell)	nsuser_nsuser_ns_hiddenr   r   eventsr   auto_magicsregisterselfs    r   __init__zFakeShell.__init__1   s\    ww "4.,)GH+$7^T-=-=-J-JKr   c                     | j                   j                  d       t        || j                         | j                  j                          y )Nr   )r   triggerexecr   r   post_execute_hook)r   codes     r   run_codezFakeShell.run_code;   s4    N+T4<< **,r   c                 :    | j                   j                  |       y r   )r   update)r   itemss     r   pushzFakeShell.push@   s    ur   c                 :    | j                   j                  |       y r   )r   
autoreload)r   	parameters     r   magic_autoreloadzFakeShell.magic_autoreloadC   s    ##I.r   Nc                 r    | j                   j                  ||       | j                   j                          y )Nstream)r   aimportr$   )r   r-   r1   s      r   magic_aimportzFakeShell.magic_aimportF   s-      6 :**,r   r   )__name__
__module____qualname__r    noopregister_magicsset_hookr&   r*   r.   r3   r   r   r   r   r   0   s(    L "&%Oh-
/-r   r   c                   :    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d Zy)	Fixturez&Fixture for creating test module filesN#abcdefghijklmopqrstuvwxyz0123456789c                     t        j                         | _        t        t        j
                        | _        t        j
                  j                  d| j                         t               | _	        y )Nr   )
tempfilemkdtemptest_dirlistsyspathold_sys_pathinsertr   r   r   s    r   setUpzFixture.setUpR   sA     ((* N4==)[
r   c                     t        j                  | j                         | j                  t        _        d | _        d | _        d | _        y r   )shutilrmtreer@   rD   rB   rC   r   r   s    r   tearDownzFixture.tearDownX   s5    dmm$$$ 
r   c                    ddj                  t        j                  | j                  d            z   }|t        j
                  v rt        j
                  |= t        j                  j                  | j                  |dz         }||fS )Ntmpmod_    z.py)	joinrandomsamplefilename_charsrB   modulesosrC   r@   )r   module_name	file_names      r   
get_modulezFixture.get_module`   sf    "''&--8K8KR*P"QQ#++%K(GGLLe0CD	I%%r   c                     t        j                  |      }t        j                  d       t	        |dd      5 }|j                  |       ddd       y# 1 sw Y   yxY w)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   filenamecontentfs       r   
write_filezFixture.write_fileg   sO     //'*

4 (C'2 	aGGG	 	 	s   AAc                     t        j                  |      }| j                         \  }}t        |dd      5 }|j	                  |       d d d        ||fS # 1 sw Y   ||fS xY w)NrY   rZ   r[   )r]   r^   rW   ra   rb   )r   r%   mod_namemod_fnre   s        r   
new_modulezFixture.new_module~   s`    t$??,&&#0 	AGGDM		s   AA!)r4   r5   r6   __doc__r@   rD   rR   rF   rJ   rW   rf   rj   r   r   r   r;   r;   K   s-    0HL:N!&. r   r;   c                     | j                   j                  }t        | dd      }t        j                  |   }|j                  d      D ]  }t        ||      } |S )z;
    Original issue comes from pickle; hence the name.
    r5   N.)	__class__r4   getattrrB   rS   split)objnamerU   obj2subpaths        r   pickle_get_current_classru      sW     ==!!D#|T2K;;{#D::c? &tW%&Kr   c                   Z    e Zd Zd Zd Zd Zed        Zd Zd Z	d Z
d Zdd	Zd
 Zd Zy)TestAutoreloadc                    | j                  t        j                  d            \  }}| j                  j	                  d       | j                  j                  |       | j                  |t        j                  d             t        j                  d|z  d      5  | j                  j                  d       d d d        y # 1 sw Y   y xY w)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)
rj   r]   r^   r   r.   r3   rf   ttAssertNotPrintsr&   r   rh   ri   s      r   test_reload_enumsz TestAutoreload.test_reload_enums   s    ??OO 	
& 	

##C(

  *OO 	
 (83h
 	( JJ'	( 	( 	(s   CCc                 f   | j                   j                  d       | j                  d      \  }}d| j                   j                  vsJ d| j                   j                  vsJ | j                   j	                  d|z         | j                   j	                  d       | j                  |d       | j                   j                  d   }| j                   j	                  d       t        |      }t        ||      sJ | j                   j	                  d	       | j                   j	                  d
       y )Nry   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   r.   rj   r   r&   rf   ru   
isinstance)r   rh   ri   test_object
test_classs        r   test_reload_class_typez%TestAutoreload.test_reload_class_type   s   

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

1H<=

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

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

O,

45r   c                    | j                   j                  d       | j                  t        j                  d            \  }}| j                   j                  d|z         | j                   j                  d       | j                   j                  d       | j                  t              5  | j                   j                  d       d d d        | j                  t              5  | j                   j                  d       d d d        | j                   j                  d       | j                  t              5  | j                   j                  d	       d d d        | j                  |t        j                  d
             | j                   j                  d       dD ]  }| j                   j                  | d       | j                  t              5  | j                   j                  | d       d d d        | j                  t              5  | j                   j                  | d       d d d        | j                   j                  | d       | j                   j                  | d       | j                  t              5  | j                   j                  | d       d d d         y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   |xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   _xY w)Nry   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   r.   rj   r]   r^   r&   assertRaisesAttributeErrorrf   )r   rh   ri   object_names       r   test_reload_class_attributesz+TestAutoreload.test_reload_class_attributes   st   

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

4x?@

01

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

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

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

##{m7$;<="">2 ?

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

##{m5$9:; ;		;;	0 	0	2 	2	. 	.4= =? ?; ;sH   ,J5%K9KKK(
K45J?KKK%	(K1	4K>	c                    | j                   j                  d       | j                  t        j                  d            \  }}| j                   j                  d|z         | j                   j                  d       | j                  |t        j                  d             t        j                  d|z  d      5  | j                   j                  d	       d d d        y # 1 sw Y   y xY w)
Nry   z
                                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)))
                            rz   r{   r|   r~   )	r   r.   rj   r]   r^   r&   rf   r   r   r   s      r   test_comparing_numpy_structuresz.TestAutoreload.test_comparing_numpy_structures  s    

##C(??OO 	
& 	

4x?@

/0 	OO 
	
 (83h
 	( JJ'	( 	( 	(s   ;C  C)c                 
   | j                   j                  d       d}| j                  t        j                  |            \  }}| j                   j                  d| d       | j                   j                  d       | j                  t              5  | j                   j                  d       d d d        | j                  t              5  | j                   j                  d       d d d        | j                  t              5  | j                   j                  d       d d d        d	}| j                  |t        j                  |             | j                   j                  d       | j                   j                  d
| d       | j                   j                  d       | j                   j                  d       | j                   j                  d       d}| j                  |t        j                  |             | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                   j                  d       t        j                  dk  r| j                   j                  d       d}| j                  t        j                  |            \  }}d| d}| j                  |t        j                  |             | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                   j                  d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   XxY w)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   r.   rj   r]   r^   r&   r   	NameErrorrf   rB   version_info)	r   mod_coderh   ri   new_codenew_mod_codenew_mod_name
new_mod_fncurrent_mod_codes	            r   !test_autoload_newly_added_objectsz0TestAutoreload.test_autoload_newly_added_objects9  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 j ^ 	 	0@ AB

9:

89

EF

12U	+ 	+	. 	.	* 	*s$   MM+M8M(+M58Nc                     t          G d d              fd} |d       |d      k(  sJ  |d       |d      k(  sJ  |d       |d      k(  sJ  |d	       |d
      k(  sJ  |d       |d      k(  sJ  j                  t              5   j                  j	                  d       d d d        y # 1 sw Y   y xY w)Nc                   ,    e Zd ZU eed<   eed<   eed<   y)=TestAutoreload.test_verbose_names.<locals>.AutoreloadSettings	check_allenabledautoload_objN)r4   r5   r6   bool__annotations__r   r   r   AutoreloadSettingsr     s    OMr   r   c                     j                   j                  |        j                   j                  j                  } |j                  |j
                  |j                        S r   )r   r.   r   	_reloaderr   r   r   )modemodule_reloaderr   r   s     r   gather_settingsz:TestAutoreload.test_verbose_names.<locals>.gather_settings  sR    JJ''-"jj44>>O%))'',, r   0offOFF1explicitry   allr   complete4)r   r   
ValueErrorr   r.   )r   r   r   s   ` @r   test_verbose_namesz!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   B55B>c                 n   | j                   j                  j                  }| j                   j                  d       |j                  d   du sJ d|j
                  j                         vsJ | j                   j                  d       |j
                  d   du sJ d|j                  j                         vsJ | j                   j                  d       |j                  d   du sJ d|j
                  j                         vsJ |j
                  d   du sJ d|j                  j                         vsJ y )NrT   Tz-mathmathz	-os, math)r   r   r   r3   rS   skip_moduleskeys)r   r   s     r   test_aimport_parsingz#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                    | j                   j                  d       d}| j                  |      \  }}| j                   j                  d|        t	        j
                  dd      5  | j                   j                  d       d d d        | j                   j                  d       | j                  ||       t	        j
                  d	| d
d      5  | j                   j                  d       d d d        | j                   j                  d       | j                  ||       t	        j
                  d	| d
d      5  | j                   j                  d       d d d        | j                   j                  d       | j                  ||       t	        j
                  d	| d
d      5  | j                   j                  d       d d d        | j                   j                  d       | j                  ||       | j                  d      5 }| j                   j                  d       d d d        j                  d| d
gk(  sJ | j                   j                  d       | j                  ||       | j                  d      5 }| j                   j                  d       d d d        |j                  d| d
gk(  sJ y # 1 sw Y   AxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   $xY w# 1 sw Y   xY w# 1 sw Y   `xY w)Nr   r   zimport rM   stdoutr|   r~   zcomplete --printzReloading 'z'.zcomplete -pzcomplete --print --logr,   )loggerzINFO:autoreload:Reloading 'zcomplete -l)	r   r.   rj   r&   r   AssertPrintsrf   
assertLogsoutput)r   r   rh   ri   los        r   test_autoreload_outputz%TestAutoreload.test_autoreload_output  s   

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

ghZ01__R2 	(JJ'	( 	

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

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

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

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

##M2)__L_1 	(RJJ'	(yy:8*BGHHHHE	( 	(
	( 	(	( 	(	( 	(	( 	(	( 	(sH   'KK?K+K)K6LKKK&)K36K?Lc                    	  j                  d      \  }}|r j                  j                  d        j                  j                  |       t	               } j                  j                  d|        j                  d|z  |j                                 j                  t              5   j                  j                  d       ddd       n j                  j                  d        j                  j                  d	|z         t	               } j                  j                  d|        j                  d
|j                         v         j                  | j                  j                         t        j                  |   j                  j                  d      j!                         		 fd} |         j#                  |d       t%        j&                  d|z  d      5   j                  j                  d       ddd       t%        j(                  d|z  d      5   j                  j                  d       ddd        |         j#                  |d       	 fd} j                  j                  d        |        t+        j,                  |        j                  j                  d        |        |r j                  j                  d|z          t	               } j                  j                  d|        j                  d|z  |j                         v         j                  j                  d       n j                  j                  d        j#                  |d        j                  j                  d        j                  j                  d        |        |r j                  j                  |       n j                  j                  d        j                  j                  d        j/                  j0                  d       y# 1 sw Y   xY w# 1 sw Y   2xY w# 1 sw Y   xY w)zt
        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   rM   r0   zModules to reload:
%stmpmod_as318989e89dsNry   z	import %sz%Modules to reload:
all-except-skipped	   c                     j                  j                  d       j                  j                  d       j                   d      d       j                  j                  d      d       j	                  d      } j                  j                  d      d       j                  | j                  d      d       j                  | j                  d       j                  | j                         d       j                         }j                  j                         d       j                  |j                         d       y )	Nr   {   r   r      
   *   c   )	assertEqualxzfooBazbarquuxzzzBarrq   rs   modold_fooold_objold_obj2r   s     r   check_module_contentsz>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
rz   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                     j                  j                  d       j                  t        d             j                   d      d       j                  j	                  d      d       j                  d      } j                  j                  d      d       j                  | j                  d      d       j                  j                  d       j                  | j                  d       j                  t        d	             j                  t        | d	             j                         }j                  j	                         d
       j                  |j	                         d
       y )Nr   r   r      r   r      +   r      )	r   r   assertFalsehasattrr   r   r   r   r   r   s     r   r   z>TestAutoreload._check_smoketest.<locals>.check_module_contentsi  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)rj   r   r.   r3   r   assertIngetvaluer   ImportErrorr&   
assertTruer   rB   rS   r   r   r   rf   r   r   r   rT   unlinkr   r   )
r   use_aimportrh   ri   r1   r   r   r   r   r   s
   `     @@@@r   _check_smoketestzTestAutoreload._check_smoketest  s     ??
&< JJ'',JJ$$X.ZFJJ$$R$7MM3h>ARS"";/ A

(()?@A A 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A Af	( 	(	( 	(s$   1P89Q7Q8QQQc                 (    | j                  d       y )NTr   r   r   s    r   test_smoketest_aimportz%TestAutoreload.test_smoketest_aimport  s    $/r   c                 (    | j                  d       y )NFr   r   r   s    r   test_smoketest_autoreloadz(TestAutoreload.test_smoketest_autoreload  s    %0r   N)T)r4   r5   r6   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   rw   rw      sQ    (<"6H;;z ( (BS3j-6:&)IVD%L01r   rw   )"rk   rT   platformpytestrB   r>   r]   rH   rP   r_   ior   dataclassesr   IPython.testing.toolstestingtoolsr   unittestr   IPython.extensions.autoreloadr   IPython.core.eventsr   r   IPython.testing.decoratorsr	   python_implementationskipr7   r   r;   ru   rw   r   r   r   <module>r     s    
   
       ! " "  : : 7!8!!#v-FKKE - -68 h 8 @	Z1W Z1r   