summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2007-08-29 03:22:59 (GMT)
committerEric Smith <eric@trueblade.com>2007-08-29 03:22:59 (GMT)
commit625cbf28eeeed3f2d8afffa6c442f10d58ae6607 (patch)
treed555d101cd7c3986a0e85cb25eef85b71fe6d10f /Lib
parent9600f93db6038ef6d5a1254295bac61e96fa56a2 (diff)
downloadcpython-625cbf28eeeed3f2d8afffa6c442f10d58ae6607.zip
cpython-625cbf28eeeed3f2d8afffa6c442f10d58ae6607.tar.gz
cpython-625cbf28eeeed3f2d8afffa6c442f10d58ae6607.tar.bz2
Modified parsing of format strings, so that we always return
a tuple (literal, field_name, format_spec, conversion). literal will always be a string, but might be of zero length. field_name will be None if there is no markup text format_spec will be a (possibly zero length) string if field_name is non-None conversion will be a one character string, or None This makes the Formatter class, and especially it's parse() method, easier to understand. Suggestion was by Jim Jewett, inspired by the "tail" of an elementtree node. Also, fixed a reference leak in fieldnameiter_next.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/string.py b/Lib/string.py
index cef5029..de364de 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -212,7 +212,13 @@ class Formatter:
result = []
for literal_text, field_name, format_spec, conversion in \
self.parse(format_string):
- if literal_text is None:
+
+ # output the literal text
+ if literal_text:
+ result.append(literal_text)
+
+ # if there's a field, output it
+ if field_name is not None:
# this is some markup, find the object and do
# the formatting
@@ -224,9 +230,7 @@ class Formatter:
# format the object and append to the result
result.append(self.format_field(obj, format_spec))
- else:
- # this is literal text, use it directly
- result.append(literal_text)
+
self.check_unused_args(used_args, args, kwargs)
return ''.join(result)
@@ -263,6 +267,11 @@ class Formatter:
# returns an iterable that contains tuples of the form:
# (literal_text, field_name, format_spec, conversion)
+ # literal_text can be zero length
+ # field_name can be None, in which case there's no
+ # object to format and output
+ # if field_name is not None, it is looked up, formatted
+ # with format_spec and conversion and then used
def parse(self, format_string):
return format_string._formatter_parser()