From 289fc0189ea941903cd69f164b43ca39815b25c5 2012-08-14 17:44:11 From: Thomas Kluyver Date: 2012-08-14 17:44:11 Subject: [PATCH] Merge pull request #2302 from takluyver/i1878-again Fix variable expansion on 'self' --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index d0d4796..8c2b0e4 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2841,9 +2841,11 @@ class InteractiveShell(SingletonConfigurable): """ ns = self.user_ns.copy() ns.update(sys._getframe(depth+1).f_locals) - ns.pop('self', None) try: - cmd = formatter.format(cmd, **ns) + # We have to use .vformat() here, because 'self' is a valid and common + # name, and expanding **ns for .format() would make it collide with + # the 'self' argument of the method. + cmd = formatter.vformat(cmd, args=[], kwargs=ns) except Exception: # if formatter couldn't format, just let it go untransformed pass diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index fe01c40..634d503 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -258,6 +258,18 @@ class InteractiveShellTestCase(unittest.TestCase): ip.run_cell('makemacro()') nt.assert_in('macro_var_expand_locals', ip.user_ns) + def test_var_expand_self(self): + """Test variable expansion with the name 'self', which was failing. + + See https://github.com/ipython/ipython/issues/1878#issuecomment-7698218 + """ + ip.run_cell('class cTest:\n' + ' classvar="see me"\n' + ' def test(self):\n' + ' res = !echo Variable: {self.classvar}\n' + ' return res[0]\n') + nt.assert_in('see me', ip.user_ns['cTest']().test()) + def test_bad_var_expand(self): """var_expand on invalid formats shouldn't raise""" # SyntaxError