From 14084d21df061ca333f9871ddbbae23b77aca592 2012-10-03 21:58:39 From: Bradley M. Froehle Date: 2012-10-03 21:58:39 Subject: [PATCH] Add test for `__file__` behavior in `%run`. --- diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 26558fe..3b10d79 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -248,3 +248,35 @@ tclass.py: deleting object: C-third na = os.path.join(mydir, 'nonascii.py') _ip.magic('run "%s"' % na) nt.assert_equal(_ip.user_ns['u'], u'Ўт№Ф') + + def test_run_py_file_attribute(self): + """Test handling of `__file__` attribute in `%run .py`.""" + src = "t = __file__\n" + self.mktmp(src) + _missing = object() + file1 = _ip.user_ns.get('__file__', _missing) + _ip.magic('run %s' % self.fname) + file2 = _ip.user_ns.get('__file__', _missing) + + # Check that __file__ was equal to the filename in the script's + # namespace. + nt.assert_equal(_ip.user_ns['t'], self.fname) + + # Check that __file__ was not leaked back into user_ns. + nt.assert_equal(file1, file2) + + def test_run_ipy_file_attribute(self): + """Test handling of `__file__` attribute in `%run `.""" + src = "t = __file__\n" + self.mktmp(src, ext='.ipy') + _missing = object() + file1 = _ip.user_ns.get('__file__', _missing) + _ip.magic('run %s' % self.fname) + file2 = _ip.user_ns.get('__file__', _missing) + + # Check that __file__ was equal to the filename in the script's + # namespace. + nt.assert_equal(_ip.user_ns['t'], self.fname) + + # Check that __file__ was not leaked back into user_ns. + nt.assert_equal(file1, file2)