From: Serhiy Storchaka Date: Tue, 24 Mar 2015 20:27:50 +0000 (+0200) Subject: Issue #23671: string.Template now allows to specify the "self" parameter as X-Git-Tag: v2.7.10rc1~104 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=40fd0e8d68352f9d28be2a6ad1d6d0ba93756dbf;p=thirdparty%2FPython%2Fcpython.git Issue #23671: string.Template now allows to specify the "self" parameter as keyword argument. string.Formatter now allows to specify the "self" and the "format_string" parameters as keyword arguments. --- diff --git a/Lib/string.py b/Lib/string.py index d762e040b112..23608b4acecb 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -145,7 +145,11 @@ class Template: 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[0], args[1:] # allow the "self" keyword be passed if len(args) > 1: raise TypeError('Too many positional arguments') if not args: @@ -171,7 +175,11 @@ class Template: 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[0], args[1:] # allow the "self" keyword be passed if len(args) > 1: raise TypeError('Too many positional arguments') if not args: @@ -535,7 +543,19 @@ except ImportError: # The field name parser is implemented in str._formatter_field_name_split class Formatter(object): - def format(self, format_string, *args, **kwargs): + def format(*args, **kwargs): + if not args: + raise TypeError("descriptor 'format' of 'Formatter' object " + "needs an argument") + self, args = args[0], args[1:] # allow the "self" keyword be passed + try: + format_string, args = args[0], args[1:] # allow the "format_string" keyword be passed + except IndexError: + if 'format_string' in kwargs: + format_string = kwargs.pop('format_string') + else: + raise TypeError("format() missing 1 required positional " + "argument: 'format_string'") return self.vformat(format_string, args, kwargs) def vformat(self, format_string, args, kwargs): diff --git a/Lib/test/test_pep292.py b/Lib/test/test_pep292.py index 5ff280d8be98..133cab7cfda3 100644 --- a/Lib/test/test_pep292.py +++ b/Lib/test/test_pep292.py @@ -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}') @@ -178,6 +179,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 @@ -196,6 +200,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 --git a/Lib/test/test_string.py b/Lib/test/test_string.py index e786ab6156e8..fdb3253a44e4 100644 --- a/Lib/test/test_string.py +++ b/Lib/test/test_string.py @@ -196,6 +196,18 @@ class ModuleTest(unittest.TestCase): self.assertRaises(ValueError, format, '', '#') self.assertRaises(ValueError, format, '', '#20') + 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}-") + self.assertEqual(fmt.format("-{format_string}-", format_string='test'), + '-test-') + self.assertRaises(KeyError, fmt.format, "-{format_string}-") + self.assertEqual(fmt.format(arg='test', format_string="-{arg}-"), + '-test-') + class BytesAliasTest(unittest.TestCase): def test_builtin(self): diff --git a/Misc/NEWS b/Misc/NEWS index e9714bbec485..56eb793c2e26 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -21,6 +21,10 @@ Core and Builtins Library ------- +- Issue #23671: string.Template now allows to specify the "self" parameter as + keyword argument. string.Formatter now allows to specify the "self" and + the "format_string" parameters as keyword arguments. + - Issue #21560: An attempt to write a data of wrong type no longer cause GzipFile corruption. Original patch by Wolfgang Maier.