summaryrefslogtreecommitdiffstats
path: root/Lib/string.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-24 20:30:46 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-24 20:30:46 (GMT)
commitb876df4cbb0b2b51ed4576ebbecea6f794d4abcd (patch)
treeac2e99846f819e01e74206d13654c91bae42acf4 /Lib/string.py
parentd9d769fcdd573ab12b628798288c02dceba53505 (diff)
parent8ffe917cee26b83fed4f227c4ed16d4eec15dcf9 (diff)
downloadcpython-b876df4cbb0b2b51ed4576ebbecea6f794d4abcd.zip
cpython-b876df4cbb0b2b51ed4576ebbecea6f794d4abcd.tar.gz
cpython-b876df4cbb0b2b51ed4576ebbecea6f794d4abcd.tar.bz2
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.
Diffstat (limited to 'Lib/string.py')
-rw-r--r--Lib/string.py29
1 files changed, 26 insertions, 3 deletions
diff --git a/Lib/string.py b/Lib/string.py
index 72a09f7..f3365c6 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -94,7 +94,11 @@ class Template(metaclass=_TemplateMetaclass):
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 # allow the "self" keyword be passed
if len(args) > 1:
raise TypeError('Too many positional arguments')
if not args:
@@ -120,7 +124,11 @@ class Template(metaclass=_TemplateMetaclass):
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 # allow the "self" keyword be passed
if len(args) > 1:
raise TypeError('Too many positional arguments')
if not args:
@@ -160,7 +168,22 @@ class Template(metaclass=_TemplateMetaclass):
# 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):
+ if not args:
+ raise TypeError("descriptor 'format' of 'Formatter' object "
+ "needs an argument")
+ self, *args = args # allow the "self" keyword be passed
+ try:
+ format_string, *args = args # allow the "format_string" keyword be passed
+ except ValueError:
+ if 'format_string' in kwargs:
+ format_string = kwargs.pop('format_string')
+ import warnings
+ warnings.warn("Passing 'format_string' as keyword argument is "
+ "deprecated", DeprecationWarning, stacklevel=2)
+ else:
+ raise TypeError("format() missing 1 required positional "
+ "argument: 'format_string'") from None
return self.vformat(format_string, args, kwargs)
def vformat(self, format_string, args, kwargs):