diff -r 642247a536d5 Lib/string.py --- a/Lib/string.py Sat Mar 14 20:17:38 2015 -0700 +++ b/Lib/string.py Sun Mar 15 10:20:13 2015 +0200 @@ -94,7 +94,11 @@ class Template(metaclass=_TemplateMetacl raise ValueError('Invalid placeholder in string: line %d, col %d' % (lineno, colno)) - def substitute(self, *args, **kws): + def substitute(*args, **kws): + if not args: + raise TypeError("descriptor 'substitute' of 'Template' object " + "needs an argument") + self, *args = args if len(args) > 1: raise TypeError('Too many positional arguments') if not args: @@ -120,7 +124,11 @@ class Template(metaclass=_TemplateMetacl self.pattern) return self.pattern.sub(convert, self.template) - def safe_substitute(self, *args, **kws): + def safe_substitute(*args, **kws): + if not args: + raise TypeError("descriptor 'safe_substitute' of 'Template' object " + "needs an argument") + self, *args = args if len(args) > 1: raise TypeError('Too many positional arguments') if not args: @@ -160,7 +168,15 @@ class Template(metaclass=_TemplateMetacl # The field name parser is implemented in _string.formatter_field_name_split class Formatter: - def format(self, format_string, *args, **kwargs): + def format(*args, **kwargs): + try: + self, format_string, *args = args + except ValueError: + if not args: + msg = "format() missing 2 required positional arguments" + else: + msg = "format() missing 1 required positional argument" + raise TypeError(msg) from None return self.vformat(format_string, args, kwargs) def vformat(self, format_string, args, kwargs): diff -r 642247a536d5 Lib/test/test_pep292.py --- a/Lib/test/test_pep292.py Sat Mar 14 20:17:38 2015 -0700 +++ b/Lib/test/test_pep292.py Sun Mar 15 10:20:13 2015 +0200 @@ -26,6 +26,7 @@ class TestTemplate(unittest.TestCase): self.assertEqual(s.substitute(dict(who='tim', what='ham')), 'tim likes to eat a bag of ham worth $100') self.assertRaises(KeyError, s.substitute, dict(who='tim')) + self.assertRaises(TypeError, Template.substitute) def test_regular_templates_with_braces(self): s = Template('$who likes ${what} for ${meal}') @@ -198,6 +199,9 @@ class TestTemplate(unittest.TestCase): eq(s.substitute(dict(mapping='one'), mapping='two'), 'the mapping is two') + s = Template('the self is $self') + eq(s.substitute(self='bozo'), 'the self is bozo') + def test_keyword_arguments_safe(self): eq = self.assertEqual raises = self.assertRaises @@ -216,6 +220,9 @@ class TestTemplate(unittest.TestCase): raises(TypeError, s.substitute, d, {}) raises(TypeError, s.safe_substitute, d, {}) + s = Template('the self is $self') + eq(s.safe_substitute(self='bozo'), 'the self is bozo') + def test_delimiter_override(self): eq = self.assertEqual raises = self.assertRaises diff -r 642247a536d5 Lib/test/test_string.py --- a/Lib/test/test_string.py Sat Mar 14 20:17:38 2015 -0700 +++ b/Lib/test/test_string.py Sun Mar 15 10:20:13 2015 +0200 @@ -31,6 +31,15 @@ class ModuleTest(unittest.TestCase): self.assertEqual(fmt.format("foo"), "foo") self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") + self.assertRaises(TypeError, fmt.format) + self.assertRaises(TypeError, string.Formatter.format) + + def test_format_keyword_arguments(self): + fmt = string.Formatter() + self.assertEqual(fmt.format("-{arg}-", arg='test'), '-test-') + self.assertRaises(KeyError, fmt.format, "-{arg}-") + self.assertEqual(fmt.format("-{self}-", self='test'), '-test-') + self.assertRaises(KeyError, fmt.format, "-{self}-") def test_auto_numbering(self): fmt = string.Formatter()