diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index e9c1f5e..6ed510f 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -201,7 +201,10 @@ class InteractiveShellTestCase(unittest.TestCase): def test_var_expand(self): ip = get_ipython() - ip.user_ns['f'] = 'Ca\xc3\xb1o' + ip.user_ns['f'] = u'Ca\xf1o' + self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o') + + ip.user_ns['f'] = b'Ca\xc3\xb1o' # This should not raise any exception: ip.var_expand(u'echo $f') diff --git a/IPython/utils/tests/test_text.py b/IPython/utils/tests/test_text.py index 1d0b70a..ac09c2b 100644 --- a/IPython/utils/tests/test_text.py +++ b/IPython/utils/tests/test_text.py @@ -107,3 +107,7 @@ def test_dollar_formatter(): nt.assert_equals(s, "12") s = f.format("$n/{stuff[:5]}", **ns) nt.assert_equals(s, "12/hello") + s = f.format("$n $$HOME", **ns) + nt.assert_equals(s, "12 $HOME") + s = f.format("${foo}", foo="HOME") + nt.assert_equals(s, "$HOME") diff --git a/IPython/utils/text.py b/IPython/utils/text.py index ba16e05..6ec0d7b 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -663,20 +663,26 @@ class DollarFormatter(FullEvalFormatter): In [4]: f.format('$a or {b}', a=1, b=2) Out[4]: u'1 or 2' """ - _dollar_pattern = re.compile("(.*)\$([\w\.]+)") + _dollar_pattern = re.compile("(.*?)\$(\$?[\w\.]+)") def parse(self, fmt_string): for literal_txt, field_name, format_spec, conversion \ in Formatter.parse(self, fmt_string): # Find $foo patterns in the literal text. continue_from = 0 + txt = "" for m in self._dollar_pattern.finditer(literal_txt): new_txt, new_field = m.group(1,2) - yield (new_txt, new_field, "", None) + # $$foo --> $foo + if new_field.startswith("$"): + txt += new_txt + new_field + else: + yield (txt + new_txt, new_field, "", None) + txt = "" continue_from = m.end() # Re-yield the {foo} style pattern - yield (literal_txt[continue_from:], field_name, format_spec, conversion) + yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion) def columnize(items, separator=' ', displaywidth=80):