From 13328af1d520ee5c3e9a472a20d62f8497ecdeb3 2017-09-28 19:00:48 From: Craig Citro Date: 2017-09-28 19:00:48 Subject: [PATCH] Always reset after `%run -i` in tests. Currently, `IPython/core/tests/run_tests.py` has a few tests that do `run -i`, which has side-effects on the IPython environment shared by all tests. In particular, setting `__file__` in the global namespace leads to traceback handling in `ultratb` getting confused, since it no longer knows which file to look in. This fixes existing uses of `run -i`, and adds a note reminding future travelers to be careful when adding new cases. --- diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index ef9bd3f..f8ba6c7 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -6,6 +6,9 @@ verify subtle object deletion and reference counting issues, the %run tests will be kept in this separate file. This makes it easier to aggregate in one place the tricks needed to handle it; most other magics are much easier to test and we do so in a common test_magic file. + +Note that any test using `run -i` should make sure to do a `reset` afterwards, +as otherwise it may influence later tests. """ # Copyright (c) IPython Development Team. @@ -317,13 +320,19 @@ tclass.py: deleting object: C-third src = "yy = zz\n" self.mktmp(src) _ip.run_cell("zz = 23") - _ip.magic('run -i %s' % self.fname) - nt.assert_equal(_ip.user_ns['yy'], 23) - _ip.magic('reset -f') + try: + _ip.magic('run -i %s' % self.fname) + nt.assert_equal(_ip.user_ns['yy'], 23) + finally: + _ip.magic('reset -f') + _ip.run_cell("zz = 23") - _ip.magic('run -i %s' % self.fname) - nt.assert_equal(_ip.user_ns['yy'], 23) - + try: + _ip.magic('run -i %s' % self.fname) + nt.assert_equal(_ip.user_ns['yy'], 23) + finally: + _ip.magic('reset -f') + def test_unicode(self): """Check that files in odd encodings are accepted.""" mydir = os.path.dirname(__file__) @@ -505,8 +514,11 @@ def test_run__name__(): _ip.magic('run -n {}'.format(path)) nt.assert_equal(_ip.user_ns.pop('q'), 'foo') - _ip.magic('run -i -n {}'.format(path)) - nt.assert_equal(_ip.user_ns.pop('q'), 'foo') + try: + _ip.magic('run -i -n {}'.format(path)) + nt.assert_equal(_ip.user_ns.pop('q'), 'foo') + finally: + _ip.magic('reset -f') def test_run_tb():