summaryrefslogtreecommitdiff
path: root/Lib/string.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/string.py')
-rw-r--r--Lib/string.py56
1 files changed, 48 insertions, 8 deletions
diff --git a/Lib/string.py b/Lib/string.py
index b57c79b75e..ef0787f52b 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,16 +168,29 @@ 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')
+ 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):
used_args = set()
- result = self._vformat(format_string, args, kwargs, used_args, 2)
+ result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
self.check_unused_args(used_args, args, kwargs)
return result
- def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
+ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
+ auto_arg_index=0):
if recursion_depth < 0:
raise ValueError('Max string recursion exceeded')
result = []
@@ -185,6 +206,23 @@ class Formatter:
# this is some markup, find the object and do
# the formatting
+ # handle arg indexing when empty field_names are given.
+ if field_name == '':
+ if auto_arg_index is False:
+ raise ValueError('cannot switch from manual field '
+ 'specification to automatic field '
+ 'numbering')
+ field_name = str(auto_arg_index)
+ auto_arg_index += 1
+ elif field_name.isdigit():
+ if auto_arg_index:
+ raise ValueError('cannot switch from manual field '
+ 'specification to automatic field '
+ 'numbering')
+ # disable auto arg incrementing, if it gets
+ # used later on, then an exception will be raised
+ auto_arg_index = False
+
# given the field_name, find the object it references
# and the argument it came from
obj, arg_used = self.get_field(field_name, args, kwargs)
@@ -194,13 +232,15 @@ class Formatter:
obj = self.convert_field(obj, conversion)
# expand the format spec, if needed
- format_spec = self._vformat(format_spec, args, kwargs,
- used_args, recursion_depth-1)
+ format_spec, auto_arg_index = self._vformat(
+ format_spec, args, kwargs,
+ used_args, recursion_depth-1,
+ auto_arg_index=auto_arg_index)
# format the object and append to the result
result.append(self.format_field(obj, format_spec))
- return ''.join(result)
+ return ''.join(result), auto_arg_index
def get_value(self, key, args, kwargs):