summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py42
-rw-r--r--Lib/test/test_string.py23
-rw-r--r--Lib/test/test_unicode.py9
3 files changed, 65 insertions, 9 deletions
diff --git a/Lib/string.py b/Lib/string.py
index 9df78af..edf5ae2 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -200,31 +200,59 @@ class Template(metaclass=_TemplateMetaclass):
# exposed here via the sys module. sys was chosen because it's always
# available and doesn't have to be dynamically loaded.
-# The parser is implemented in sys._formatter_parser.
-# The "object lookup" is implemented in sys._formatter_lookup
+# The overall parser is implemented in sys._formatter_parser.
+# The field name parser is implemented in sys._formatter_field_name_split
-from sys import _formatter_parser, _formatter_lookup
+from sys import _formatter_parser, _formatter_field_name_split
class Formatter:
def format(self, format_string, *args, **kwargs):
return self.vformat(format_string, args, kwargs)
def vformat(self, format_string, args, kwargs):
+ used_args = set()
result = []
for (is_markup, literal, field_name, format_spec, conversion) in \
_formatter_parser(format_string):
if is_markup:
- # find the object
- index, name, obj = _formatter_lookup(field_name, args, kwargs)
+ # given the field_name, find the object it references
+
+ # split it into the first part, and and iterator that
+ # looks over the rest
+ first, rest = _formatter_field_name_split(field_name)
+
+ used_args.add(first)
+ obj = self.get_value(first, args, kwargs)
+
+ # loop through the rest of the field_name, doing
+ # getattr or getitem as needed
+ for is_attr, i in rest:
+ if is_attr:
+ obj = getattr(obj, i)
+ else:
+ obj = obj[i]
+
+ # do any conversion on the resulting object
+ if conversion == 'r':
+ obj = repr(obj)
+ elif conversion == 's':
+ obj = str(obj)
+
+ # format the object and append to the result
+ result.append(self.format_field(obj, format_spec))
else:
result.append(literal)
+ self.check_unused_args(used_args, args, kwargs)
return ''.join(result)
def get_value(self, key, args, kwargs):
- pass
+ if isinstance(key, int):
+ return args[key]
+ else:
+ return kwargs[key]
def check_unused_args(self, used_args, args, kwargs):
pass
def format_field(self, value, format_spec):
- pass
+ return format(value, format_spec)
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index ce9fe23..1706d2a 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -19,8 +19,27 @@ class ModuleTest(unittest.TestCase):
fmt = string.Formatter()
self.assertEqual(fmt.format("foo"), "foo")
- # Formatter not working you for lookups
- #self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
+ self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
+ self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
+ self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
+
+ class NamespaceFormatter(string.Formatter):
+ def __init__(self, namespace={}):
+ string.Formatter.__init__(self)
+ self.namespace = namespace
+
+ def get_value(self, key, args, kwds):
+ if isinstance(key, str):
+ try:
+ # Check explicitly passed arguments first
+ return kwds[key]
+ except KeyError:
+ return self.namespace[key]
+ else:
+ string.Formatter.get_value(key, args, kwds)
+
+ fmt = NamespaceFormatter({'greeting':'hello'})
+ self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!')
def test_maketrans(self):
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index ff0e015..c39ec09 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -449,6 +449,10 @@ class UnicodeTest(
self.assertEqual('}}{{'.format(), '}{')
self.assertEqual('}}x{{'.format(), '}x{')
+ # weird field names
+ self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
+ self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
+
self.assertEqual('{foo._x}'.format(foo=C(20)), '20')
self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
@@ -539,7 +543,11 @@ class UnicodeTest(
self.assertRaises(ValueError, "}".format)
self.assertRaises(ValueError, "abc{0:{}".format)
self.assertRaises(ValueError, "{0".format)
+ self.assertRaises(ValueError, "{0.}".format)
+ self.assertRaises(ValueError, "{0[}".format)
+ self.assertRaises(ValueError, "{0]}".format)
self.assertRaises(ValueError, "{0.[]}".format)
+ self.assertRaises(ValueError, "{0..foo}".format, 0)
self.assertRaises(ValueError, "{0[0}".format)
self.assertRaises(ValueError, "{0[0:foo}".format)
self.assertRaises(ValueError, "{c]}".format)
@@ -551,6 +559,7 @@ class UnicodeTest(
self.assertRaises(ValueError, "{0!rs}".format)
self.assertRaises(ValueError, "{!}".format)
self.assertRaises(ValueError, "{:}".format)
+ self.assertRaises(ValueError, "{:s}".format)
self.assertRaises(ValueError, "{}".format)
# can't have a replacement on the field name portion