Kaydet (Commit) 625cbf28 authored tarafından Eric Smith's avatar Eric Smith

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.
üst 9600f93d
...@@ -212,7 +212,13 @@ class Formatter: ...@@ -212,7 +212,13 @@ class Formatter:
result = [] result = []
for literal_text, field_name, format_spec, conversion in \ for literal_text, field_name, format_spec, conversion in \
self.parse(format_string): 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 # this is some markup, find the object and do
# the formatting # the formatting
...@@ -224,9 +230,7 @@ class Formatter: ...@@ -224,9 +230,7 @@ class Formatter:
# format the object and append to the result # format the object and append to the result
result.append(self.format_field(obj, format_spec)) 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) self.check_unused_args(used_args, args, kwargs)
return ''.join(result) return ''.join(result)
...@@ -263,6 +267,11 @@ class Formatter: ...@@ -263,6 +267,11 @@ class Formatter:
# returns an iterable that contains tuples of the form: # returns an iterable that contains tuples of the form:
# (literal_text, field_name, format_spec, conversion) # (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): def parse(self, format_string):
return format_string._formatter_parser() return format_string._formatter_parser()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment