From b9f84f1812f6fbf7026c5ee9c1c136ce643de4b6 2012-07-20 12:09:18 From: Fernando Perez Date: 2012-07-20 12:09:18 Subject: [PATCH] Merge pull request #2168 from minrk/expandscript Expand line in cell magics. Previously failing test included for %%file $filename --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index cb6f8e1..4770c95 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2111,7 +2111,7 @@ class InteractiveShell(SingletonConfigurable): stack_depth = 2 magic_arg_s = self.var_expand(line, stack_depth) with self.builtin_trap: - result = fn(line, cell) + result = fn(magic_arg_s, cell) return result def find_line_magic(self, magic_name): diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 4536e5f..52e85ad 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -594,6 +594,21 @@ def test_file(): nt.assert_in('line1\n', s) nt.assert_in('line2', s) +def test_file_var_expand(): + """%%file $filename""" + ip = get_ipython() + with TemporaryDirectory() as td: + fname = os.path.join(td, 'file1') + ip.user_ns['filename'] = fname + ip.run_cell_magic("file", '$filename', u'\n'.join([ + 'line1', + 'line2', + ])) + with open(fname) as f: + s = f.read() + nt.assert_in('line1\n', s) + nt.assert_in('line2', s) + def test_file_unicode(): """%%file with unicode cell""" ip = get_ipython()