##// END OF EJS Templates
Merge branch 'dollar-escape'
Thomas Kluyver -
r5376:16b9fed8 merge
parent child Browse files
Show More
@@ -201,7 +201,10 b' class InteractiveShellTestCase(unittest.TestCase):'
201
201
202 def test_var_expand(self):
202 def test_var_expand(self):
203 ip = get_ipython()
203 ip = get_ipython()
204 ip.user_ns['f'] = 'Ca\xc3\xb1o'
204 ip.user_ns['f'] = u'Ca\xf1o'
205 self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o')
206
207 ip.user_ns['f'] = b'Ca\xc3\xb1o'
205 # This should not raise any exception:
208 # This should not raise any exception:
206 ip.var_expand(u'echo $f')
209 ip.var_expand(u'echo $f')
207
210
@@ -107,3 +107,7 b' def test_dollar_formatter():'
107 nt.assert_equals(s, "12")
107 nt.assert_equals(s, "12")
108 s = f.format("$n/{stuff[:5]}", **ns)
108 s = f.format("$n/{stuff[:5]}", **ns)
109 nt.assert_equals(s, "12/hello")
109 nt.assert_equals(s, "12/hello")
110 s = f.format("$n $$HOME", **ns)
111 nt.assert_equals(s, "12 $HOME")
112 s = f.format("${foo}", foo="HOME")
113 nt.assert_equals(s, "$HOME")
@@ -663,20 +663,26 b' class DollarFormatter(FullEvalFormatter):'
663 In [4]: f.format('$a or {b}', a=1, b=2)
663 In [4]: f.format('$a or {b}', a=1, b=2)
664 Out[4]: u'1 or 2'
664 Out[4]: u'1 or 2'
665 """
665 """
666 _dollar_pattern = re.compile("(.*)\$([\w\.]+)")
666 _dollar_pattern = re.compile("(.*?)\$(\$?[\w\.]+)")
667 def parse(self, fmt_string):
667 def parse(self, fmt_string):
668 for literal_txt, field_name, format_spec, conversion \
668 for literal_txt, field_name, format_spec, conversion \
669 in Formatter.parse(self, fmt_string):
669 in Formatter.parse(self, fmt_string):
670
670
671 # Find $foo patterns in the literal text.
671 # Find $foo patterns in the literal text.
672 continue_from = 0
672 continue_from = 0
673 txt = ""
673 for m in self._dollar_pattern.finditer(literal_txt):
674 for m in self._dollar_pattern.finditer(literal_txt):
674 new_txt, new_field = m.group(1,2)
675 new_txt, new_field = m.group(1,2)
675 yield (new_txt, new_field, "", None)
676 # $$foo --> $foo
677 if new_field.startswith("$"):
678 txt += new_txt + new_field
679 else:
680 yield (txt + new_txt, new_field, "", None)
681 txt = ""
676 continue_from = m.end()
682 continue_from = m.end()
677
683
678 # Re-yield the {foo} style pattern
684 # Re-yield the {foo} style pattern
679 yield (literal_txt[continue_from:], field_name, format_spec, conversion)
685 yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion)
680
686
681
687
682 def columnize(items, separator=' ', displaywidth=80):
688 def columnize(items, separator=' ', displaywidth=80):
General Comments 0
You need to be logged in to leave comments. Login now