From 23210de699f633728fa411423738271e9407d27f 2012-08-14 16:50:21 From: Thomas Kluyver <takowl@gmail.com> Date: 2012-08-14 16:50:21 Subject: [PATCH] Fix variable expansion on 'self' Closes gh-1878 (again) --- 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 dff0e22..634d503 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -267,7 +267,7 @@ class InteractiveShellTestCase(unittest.TestCase): ' classvar="see me"\n' ' def test(self):\n' ' res = !echo Variable: {self.classvar}\n' - ' return res\n') + ' return res[0]\n') nt.assert_in('see me', ip.user_ns['cTest']().test()) def test_bad_var_expand(self):