summaryrefslogtreecommitdiffstats
path: root/Lib/string.py
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2007-09-04 23:04:22 (GMT)
committerEric Smith <eric@trueblade.com>2007-09-04 23:04:22 (GMT)
commit11529195cae2438a3ac003babcb1b11af67c4037 (patch)
tree2bf0ac780377e409f87ad51aca8d8bc7c882f015 /Lib/string.py
parent0af17617c51d6cae4c0b7ff225751e07183b96f2 (diff)
downloadcpython-11529195cae2438a3ac003babcb1b11af67c4037.zip
cpython-11529195cae2438a3ac003babcb1b11af67c4037.tar.gz
cpython-11529195cae2438a3ac003babcb1b11af67c4037.tar.bz2
Changed some ValueError's to KeyError and IndexError.
Corrected code for invalid conversion specifier. Added tests to verify. Modified string.Formatter to correctly expand format_spec's, and added a limit to recursion depth. Added _vformat() method to support both of these.
Diffstat (limited to 'Lib/string.py')
-rw-r--r--Lib/string.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/Lib/string.py b/Lib/string.py
index 9b00a62..03179fb 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -202,6 +202,13 @@ class Formatter:
def vformat(self, format_string, args, kwargs):
used_args = set()
+ 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):
+ if recursion_depth < 0:
+ raise ValueError('Max string recursion exceeded')
result = []
for literal_text, field_name, format_spec, conversion in \
self.parse(format_string):
@@ -223,10 +230,13 @@ class Formatter:
# do any conversion on the resulting object
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 the object and append to the result
result.append(self.format_field(obj, format_spec))
- self.check_unused_args(used_args, args, kwargs)
return ''.join(result)
@@ -251,9 +261,9 @@ class Formatter:
return repr(value)
elif conversion == 's':
return str(value)
- else:
- assert conversion is None
+ elif conversion is None:
return value
+ raise ValueError("Unknown converion specifier {0!s}".format(conversion))
# returns an iterable that contains tuples of the form: