Format: application/x-python-code
⬇ Download File (19.9 KB)
   1  o
   2  
   3  ��h�K�@sNdZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
   4  Zddlm
   5  Z
   6  ddlZddlmZddlmZddlmZddlmZdd	lmZd
   7  d�Zdd
   8  �Zdd�Zejdd��Zejdke_ej dd��Z!ejdke!_dd�Z"Gdd�dej#�Z$Gdd�dej#�Z%Gdd�dej&�Z'dd�Z(dd �Z)d!d"�Z*d#d$�Z+dS)%a+Tests for code execution (%run and related), which is particularly tricky.
   9  
  10  Because of how %run manages namespaces, and the fact that we are trying here to
  11  verify subtle object deletion and reference counting issues, the %run tests
  12  will be kept in this separate file.  This makes it easier to aggregate in one
  13  place the tricks needed to handle it; most other magics are much easier to test
  14  and we do so in a common test_magic file.
  15  
  16  Note that any test using `run -i` should make sure to do a `reset` afterwards,
  17  as otherwise it may influence later tests.
  18  �N)�join)�patch)�TemporaryDirectory)�debugger)�
  19  decorators)�tools)�capture_outputcC�dS)aVVery nasty problem with references held by multiple runs of a script.
  20      See: https://github.com/ipython/ipython/issues/141
  21  
  22      In [1]: _ip.clear_main_mod_cache()
  23      # random
  24  
  25      In [2]: %run refbug
  26  
  27      In [3]: call_f()
  28      lowercased: hello
  29  
  30      In [4]: %run refbug
  31  
  32      In [5]: call_f()
  33      lowercased: hello
  34      lowercased: hello
  35      N�r
  36  r
  37  r
  38  �Qc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/tests/test_run.py�doctest_refbug'�rcCr	)avCheck that %run doesn't damage __builtins__.
  39  
  40      In [1]: import tempfile
  41  
  42      In [2]: bid1 = id(__builtins__)
  43  
  44      In [3]: fname = tempfile.mkstemp('.py')[1]
  45  
  46      In [3]: f = open(fname, 'w', encoding='utf-8')
  47  
  48      In [4]: dummy= f.write('pass\n')
  49  
  50      In [5]: f.flush()
  51  
  52      In [6]: t1 = type(__builtins__)
  53  
  54      In [7]: %run $fname
  55  
  56      In [7]: f.close()
  57  
  58      In [8]: bid2 = id(__builtins__)
  59  
  60      In [9]: t2 = type(__builtins__)
  61  
  62      In [10]: t1 == t2
  63      Out[10]: True
  64  
  65      In [10]: bid1 == bid2
  66      Out[10]: True
  67  
  68      In [12]: try:
  69         ....:     os.unlink(fname)
  70         ....: except:
  71         ....:     pass
  72         ....:
  73      Nr
  74  r
  75  r
  76  r
  77  r�doctest_run_builtins;r
  78  rcCr	)z�Test option parser in %run.
  79  
  80      In [1]: %run print_argv.py
  81      []
  82  
  83      In [2]: %run print_argv.py print*.py
  84      ['print_argv.py']
  85  
  86      In [3]: %run -G print_argv.py print*.py
  87      ['print*.py']
  88  
  89      Nr
  90  r
  91  r
  92  r
  93  r�doctest_run_option_parserbr
  94  rcCr	)a,Test option parser in %run (Linux/OSX specific).
  95  
  96      You need double quote to escape glob in POSIX systems:
  97  
  98      In [1]: %run print_argv.py print\\*.py
  99      ['print*.py']
 100  
 101      You can't use quote to escape glob in POSIX systems:
 102  
 103      In [2]: %run print_argv.py 'print*.py'
 104      ['print_argv.py']
 105  
 106      Nr
 107  r
 108  r
 109  r
 110  r�#doctest_run_option_parser_for_posixqr
 111  r�win32cCr	)aTest option parser in %run (Windows specific).
 112  
 113      In Windows, you can't escape ``*` `by backslash:
 114  
 115      In [1]: %run print_argv.py print\\*.py
 116      ['print\\\\*.py']
 117  
 118      You can use quote to escape glob:
 119  
 120      In [2]: %run print_argv.py 'print*.py'
 121      ["'print*.py'"]
 122  
 123      Nr
 124  r
 125  r
 126  r
 127  r�%doctest_run_option_parser_for_windows�r
 128  rcCr	)a/Test that resetting doesn't cause errors in __del__ methods.
 129  
 130      In [2]: class A(object):
 131         ...:     def __del__(self):
 132         ...:         print(str("Hi"))
 133         ...:
 134  
 135      In [3]: a = A()
 136  
 137      In [4]: get_ipython().reset(); import gc; x = gc.collect(0)
 138      Hi
 139  
 140      In [5]: 1+1
 141      Out[5]: 2
 142      Nr
 143  r
 144  r
 145  r
 146  r�doctest_reset_del�r
 147  rc@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
 148  �Zdd�Zd
 149  d�Z	dd�Z
 150  dS)�TestMagicRunPasscCsd}|�|�dS)Nza = [1,2,3]
 151  b = 1)�mktmp)�self�contentr
 152  r
 153  r�setUp�szTestMagicRunPass.setUpcCst�}|�d|j�dS�N�run��get_ipython�run_line_magic�fname�r�_ipr
 154  r
 155  r�run_tmpfile�szTestMagicRunPass.run_tmpfilecCst�}|�dd|j�dS)Nrz-p %srrr
 156  r
 157  r�
 158  run_tmpfile_p�szTestMagicRunPass.run_tmpfile_pcCs:t�}t|jd�}|��t|jd�}||ksJ�dS)z,Check that %run doesn't damage __builtins__ �__builtins__N)r�id�user_nsr!)rr Zbid1Zbid2r
 159  r
 160  r�test_builtins_id�s
 161  z!TestMagicRunPass.test_builtins_idcCs,t�}|��t|jd�tt�ksJ�dS)aCheck that the type of __builtins__ doesn't change with %run.
 162  
 163          However, the above could pass if __builtins__ was already modified to
 164          be a dict (it should be a module) by a previous use of %run.  So we
 165          also check explicitly that it really is a module:
 166          r#N)rr!�typer%�sysrr
 167  r
 168  r�test_builtins_type�sz#TestMagicRunPass.test_builtins_typecCs|��dS)z^Test that the option -p, which invokes the profiler, do not
 169          crash by invoking execfileN)r"�rr
 170  r
 171  r�test_run_profile�sz!TestMagicRunPass.test_run_profilecCs�t�}t�dg��|�dd|j�Wd�n1swYt�dg��|�dd|j�Wd�dS1s=wYdS)N�cr�-d %s)r�tt�
 172  fake_inputrrrr
 173  r
 174  r�test_run_debug_twice�s�"�z%TestMagicRunPass.test_run_debug_twicec	Cs�t�}t�gd���|�dd|j�Wd�n1swYt�dg��/t�d��|�dd|j�Wd�n1sCwYWd�dSWd�dS1s[wYdS)zMake a valid python temp file.)zb 2r,r,rr-Nr,�KeyError)rr.r/rr�AssertNotPrintsrr
 175  r
 176  r�$test_run_debug_twice_with_breakpoint�s���"�z5TestMagicRunPass.test_run_debug_twice_with_breakpointN)�__name__�
 177  __module__�__qualname__rr!r"r&r)r+r0r3r
 178  r
 179  r
 180  rr�s	rc@s�eZdZdd�Zejje��dkdd�dd��Z	dd	�Z
 181  d
 182  d�Zdd
 183  �Zdd�Z
 184  dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd S)!�TestMagicRunSimplecCs@d}|�|�t�dt|j��t�d�tjddusJ�dS)z(Test that simple class definitions work.z%class foo: pass
 185  def f(): return foo()rzt = isinstance(f(), foo)�tTN)rr r�strr�run_cellr%�r�srcr
 186  r
 187  r�test_simpledef�s
 188  
 189  
 190  z!TestMagicRunSimple.test_simpledef�PyPyzGexpecting __del__ call on exit is unreliable and doesn't happen on PyPy)�reasoncCs&d}|�|�d}t�|jd|�dS)z6Test that object's __del__ methods are called on exit.zRclass A(object):
 191      def __del__(self):
 192          print('object A deleted')
 193  a = A()
 194  Nzobject A deleted)rr.�ipexec_validater)rr<�errr
 195  r
 196  r�test_obj_del�s
 197  zTestMagicRunSimple.test_obj_delcCs~t���1}|�d�dd|jf}|�|�t�dt|j��t�d�tjddks-J�Wd�dS1s8wYdS)zxTest that namespace cleanup is not too aggressive GH-238
 198  
 199          Returning from another run magic deletes the namespace�z�ip = get_ipython()
 200  for i in range(5):
 201     try:
 202         ip.run_line_magic(%r, %r)
 203     except NameError as e:
 204         print(i)
 205         break
 206  rzip == get_ipython()�i�N)	r.�
 207  TempFileMixinrrr rr9r:r%)r�emptyr<r
 208  r
 209  r�!test_aggressive_namespace_cleanups
 210  
 211  ��
 212  	
 213  "�z4TestMagicRunSimple.test_aggressive_namespace_cleanupcCsr|�d�t���&}|�d�t�d|j�t�d|j�tjd�dks'J�Wd�dS1s2wYdS)zBTest that running a second file doesn't clobber the first, gh-3547z$avar = 1
 214  def afunc():
 215    return avar
 216  rCrZafunc�N)rr.rFr rrr%)rrGr
 217  r
 218  r�test_run_seconds
 219  
 220  
 221  "�z"TestMagicRunSimple.test_run_secondcCsZtj�t�}tj�|d�}d|�d|�d|�d�}|�|d�d}d}t�|j||�dS)NZtclasszimport gc
 222  %run "z" C-first
 223  gc.collect(0)
 224  %run "z" C-second
 225  gc.collect(0)
 226  %run "z"" C-third
 227  gc.collect(0)
 228  %reset -f
 229  �.ipyz�ARGV 1-: ['C-first']
 230  ARGV 1-: ['C-second']
 231  tclass.py: deleting object: C-first
 232  ARGV 1-: ['C-third']
 233  tclass.py: deleting object: C-second
 234  tclass.py: deleting object: C-third
 235  )	�os�path�dirname�__file__rrr.r@r)r�mydirZtcr<�outrAr
 236  r
 237  r�test_tclass's���
 238  zTestMagicRunSimple.test_tclassc
 239  Cs�d}|�|�t�d�zt�dd|j�tjddksJ�Wt�dd�nt�dd�wt�d�zt�dd|j�tjddksFJ�Wt�dd�d	St�dd�w)
 240  z4Check that %run -i still works after %reset (gh-693)zyy = zz
 241  zzz = 23rz-i %s�yy��reset�-fN)rr r:rrr%r;r
 242  r
 243  r�test_run_i_after_reset@s
 244  
 245  
 246  z)TestMagicRunSimple.test_run_i_after_resetcCs<tj�t�}tj�|d�}t�d|�tjddksJ�dS)z/Check that files in odd encodings are accepted.znonascii.pyr�uu	Ўт№ФN)rLrMrNrOrr rr%)rrP�nar
 247  r
 248  r�test_unicodeRszTestMagicRunSimple.test_unicodecCsbd}|�|�t�}tj�d|�}t�d|j�tj�d|�}tjd|jks)J�||ks/J�dS)z:Test handling of `__file__` attribute in `%run <file>.py`.�
 249  t = __file__
 250  rOrr8N�r�objectr r%�getrr�rr<Z_missingZfile1Zfile2r
 251  r
 252  r�test_run_py_file_attributeYs
 253  z-TestMagicRunSimple.test_run_py_file_attributecCsfd}|j|dd�t�}tj�d|�}t�d|j�tj�d|�}tjd|jks+J�||ks1J�dS)z;Test handling of `__file__` attribute in `%run <file.ipy>`.r[rK��extrOrr8Nr\r_r
 254  r
 255  r�test_run_ipy_file_attributeisz.TestMagicRunSimple.test_run_ipy_file_attributecCs6d}|�|�t�dd|j�t�dd|j�dS)z> Test that %run -t -N<N> does not raise a TypeError for N > 1.�passrz
 256  -t -N 1 %sz-t -N 10 %sN)rr rrr;r
 257  r
 258  r�test_run_formattingys
 259  z&TestMagicRunSimple.test_run_formattingcCs�d}|�|�t�d��t�d|j�Wd�n1swYt�d��t�dd|j�Wd�dS1s=wYdS)z'Test the -e option to ignore sys.exit()zimport sys; sys.exit(1)�
 260  SystemExitrNz-e %s)rr.ZAssertPrintsr rrr2r;r
 261  r
 262  r�test_ignore_sys_exit�s
 263  �"�z'TestMagicRunSimple.test_ignore_sys_exitcCstt�d�ddlm}m}|j|�d�|�d�gd�}||dd�}|j|d	d
 264  �t	�
 265  d|j�t	jdd
 266  ks8J�dS)zTest %run notebook.ipynb�nbformatr��v4�writesz#The Ultimate Question of Everythingz	answer=42��cellsrE��version�.ipynbrarZanswer�*N)
 267  �pytest�importorskiprhrjrk�new_notebookZnew_markdown_cell�
 268  new_code_cellrr rrr%�rrjrk�nbr<r
 269  r
 270  r�test_run_nb�s
 271  ��zTestMagicRunSimple.test_run_nbcCs�t�d�ddlm}m}t�ttjd�t�ttjd�|j	|�
 272  d�gd�}||dd	�}|j|d
 273  d�t�ttjd|j�d
 274  S)zTest %run notebook.ipynb errorrhrrirzrun foobar.ipynbz0/0rlrErnrprazrun %sN)
 275  rrrsrhrjrkZraises�	Exceptionr �magicrtrurrrvr
 276  r
 277  r�test_run_nb_error�s
 278  ��z$TestMagicRunSimple.test_run_nb_errorcCs>d}|�|�d}t�dd�|j|��tjd|ksJ�dS)Nz&import sys
 279  a = " ".join(sys.argv[1:])
 280  z-x 3 --verboserz{0} {1}�a)rr r�formatrr%)rr<�	test_optsr
 281  r
 282  r�test_file_options�s
 283  
 284  z$TestMagicRunSimple.test_file_optionsN)r4r5r6r=rr�markZxfail�platform�python_implementationrBrHrJrRrWrZr`rcrergrxr{rr
 285  r
 286  r
 287  rr7�s&	
 288  �
 289  
 290  
 291  r7c@s~eZdZdd�Zdd�Zdd�Zddd	�Zd
 292  d�Zdd
 293  �Zdd�Z	dd�Z
 294  dd�Zedd��Zedd��Z
 295  dd�Zdd�ZdS)�TestMagicRunWithPackagecCsxtj�|jj|�}tj�|�}tj�|�st�|�t|ddd��}|�	t
 296  �|��Wd�dS1s5wYdS)N�w�utf-8��encoding)rLrMr�tempdir�namerN�isdir�makedirs�open�write�textwrap�dedent)rr�rrM�d�fr
 297  r
 298  r�	writefile�s
 299  "�z!TestMagicRunWithPackage.writefilecCs�d�d�dd�td�D���|_}	tt��d�|_t�|_t	�
 300  �|_tj
 301  �d|jj�|�t	j
 302  �|d�d�|�t	j
 303  �|d	�d
 304  �|j��|�t	j
 305  �|d�d�|�t	j
 306  �|d
 307  �d�|��|�t	j
 308  �|d�d�|��dS)Nztmp{0}rCcSsg|]}t�tj��qSr
 309  )�random�choice�string�
 310  ascii_letters)�.0rDr
 311  r
 312  r�
 313  <listcomp>�sz1TestMagicRunWithPackage.setUp.<locals>.<listcomp>�
 314  i'rz__init__.pyzsub.pyz
 315          x = {0!r}
 316          zrelative.pyz$
 317          from .sub import x
 318          zabsolute.pyz'
 319          from {0}.sub import x
 320          zargs.pyz?
 321          import sys
 322          a = " ".join(sys.argv[1:])
 323          )r}r�range�package�intr��valuerr�rL�getcwd�"_TestMagicRunWithPackage__orig_cwdr(rM�insertr�r�)rr�r
 324  r
 325  rr�s"$
 326  ���zTestMagicRunWithPackage.setUpcs8t��j��fdd�tjD�tjdd�<�j��dS)Ncsg|]
 327  }|�jjkr|�qSr
 328  )r�r�)r��pr*r
 329  rr��sz4TestMagicRunWithPackage.tearDown.<locals>.<listcomp>)rL�chdirr�r(rMr��cleanupr*r
 330  r*r�tearDown�sz TestMagicRunWithPackage.tearDownrCcCsFtj�dd�t�dd�|j||��|�tjd|jd�|��dS)N�xrz{2} -m {0}.{1}z-Variable `x` is not loaded from module `{0}`.)r r%�poprr}r��assertEqualr�)r�	submodule�optsr
 331  r
 332  r�check_run_submodule�s��z+TestMagicRunWithPackage.check_run_submodulecCs|�d�dS)N�absolute�r�r*r
 333  r
 334  r�'test_run_submodule_with_absolute_import�sz?TestMagicRunWithPackage.test_run_submodule_with_absolute_importcCs|�d�dS)z;Run submodule that has a relative import statement (#2727).�relativeNr�r*r
 335  r
 336  r�'test_run_submodule_with_relative_import�sz?TestMagicRunWithPackage.test_run_submodule_with_relative_importcC�|�dd�dS)Nr��-pr�r*r
 337  r
 338  r�(test_prun_submodule_with_absolute_import��z@TestMagicRunWithPackage.test_prun_submodule_with_absolute_importcCr�)Nr�r�r�r*r
 339  r
 340  r�(test_prun_submodule_with_relative_import�r�z@TestMagicRunWithPackage.test_prun_submodule_with_relative_importcst����fdd��}|S)NcsFt�tjdtt����|i|��Wd�S1swYdSr)rr]rZPdb�staticmethod�eval)�args�kwds��funcr
 341  r�wrapper�s$�z;TestMagicRunWithPackage.with_fake_debugger.<locals>.wrapper)�	functools�wraps)r�r�r
 342  r�r�with_fake_debugger�sz*TestMagicRunWithPackage.with_fake_debuggercCr�)Nr��-dr�r*r
 343  r
 344  r�-test_debug_run_submodule_with_absolute_import�zETestMagicRunWithPackage.test_debug_run_submodule_with_absolute_importcCr�)Nr�r�r�r*r
 345  r
 346  r�-test_debug_run_submodule_with_relative_importr�zETestMagicRunWithPackage.test_debug_run_submodule_with_relative_importcC�>tj�dd�d}t�dd�|j|��tjd|ksJ�dS)Nr|�-x abc -m testrz-m {0}.args {1}�r r%r�rr}r��rr~r
 347  r
 348  r�test_module_options�z+TestMagicRunWithPackage.test_module_optionscCr�)Nr|r�rz-m {0}.args -- {1}r�r�r
 349  r
 350  r�"test_module_options_with_separatorr�z:TestMagicRunWithPackage.test_module_options_with_separatorN)rC)r4r5r6r�rr�r�r�r�r�r�r�r�r�r�r�r
 351  r
 352  r
 353  rr��s 
 354  
 355  
 356  r�cCst��z}t|d�}t|ddd��
 357  }|�d�Wd�n1s wYtj�dd�t�dd�|��tj�d�d	ks?J�t�dd
 358  �|��tj�d�dksRJ�zt�dd�|��tj�d�dksfJ�Wt�d
 359  d�nt�d
 360  d�wWd�dS1s�wYdS)N�foo.pyr�r�r�zq = __name__�qr�{}�__main__z-n {}�fooz-i -n {}rUrV)	r�pjoinr�r�r r%r�rr})�tdrMr�r
 361  r
 362  r�test_run__name__s
 363  �"�r�c	Cs�t��k}t|d�}t|ddd��}|�d�gd���Wd�n1s%wYt��}t�dd	�|��Wd�n1sAwY|j	}d
 364  |vsOJ�d|vsUJ�|�
 365  d�d
 366  ks^J�tjd=tjd=Wd�dS1sqwYdS)zTest traceback offset in %runr�r�r�r��
 367  �z
 368  def foo():z    return bar()z
 369  def bar():z     raise RuntimeError('hello!')zfoo()Nrr��execfile�RuntimeError�---->��barr�)
 370  rr�r�r�rrr rr}�stdout�count�ipr%)r�rMr��iorQr
 371  r
 372  r�test_run_tb)s&
 373  ����
 374  "�r�c
 375  Cs&t���}tj�d�}dtjd<zkz_t|d�}t|ddd��
 376  }|�d�Wd�n1s-wYt��}t�	d|�t�
 377  d	�Wd�n1sKwY|j}d
 378  |vsYJ�d|vs_J�d|vseJ�|�d
 379  �dksnJ�Wn�W|tjd<n|tjd<wWd�dS1s�wYdS)z�Set we can run mutiprocesgin without messing up up main namespace
 380  
 381      Note that import `nose.tools as nt` modify the values
 382      sys.module['__mp_main__'] so we need to temporarily set it to None to test
 383      the issue.
 384      �__mp_main__Nztest.pyr�r�r�z#import multiprocessing
 385  print('hoy')rZ
 386  i_m_undefinedZhoy�AttributeError�	NameErrorr�rI)
 387  rr(�modulesr^r�r�r�rr rr:r�r�)r�ZmpmrMr�r�rQr
 388  r
 389  r�test_multiprocessing_runCs,
 390  
 391  ���"�r�c	Cs�t��K}t|d�}t|ddd��}|�d�gd���Wd�n1s%wYt�|�\}}d|vs7J�d	|vs=J�|�d
 392  �dksFJ�Wd�dS1sQwYdS)z,Test traceback offset in `ipython script.py`r�r�r�r�r�r�Nr�r�r�r�)rr�r�r�rr.Zipexecr�)r�rMr�rQrAr
 393  r
 394  r�test_script_tb_s
 395  ���"�r�),�__doc__r�rLr�r�r�r(r�ZunittestZos.pathrr�Z
 396  unittest.mockrrr�tempfilerZIPython.corerZIPython.testingr�decrr.ZIPython.utils.iorrrrZ
 397  skip_win32rZ__skip_doctest__Zskip_if_not_win32rrrFrr7ZTestCaser�r�r�r�r�r
 398  r
 399  r
 400  r�<module>sF'
 401  
 402  >O\