This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients eric.smith, francismb, ncoghlan, terry.reedy
Date 2012-02-15.04:40:22
SpamBayes Score 3.330669e-16
Marked as misclassified No
Message-id <1329280825.16.0.853034290031.issue13579@psf.upfronthosting.co.za>
In-reply-to
Content
Doc/library/string.rst string.Formatter().format(fmt, 10)needs a couple of changes also.

1. "format(format_string, *args, **kwargs) 
format() is the primary API method. It takes a format template string,"

I think 'template' should be removed as we elsewhere (as in the next section) just call them 'format strings'.

Note: I consider this and the preceding sentences

"The built-in string class provides the ability to do complex variable substitutions and value formatting via the format() method described in PEP 3101. The Formatter class in the string module allows you to create and customize your own string formatting behaviors using the same implementation as the built-in format() method."

to imply that this method should accept any format string that str.format does, and that the patch is a bug fix rather than enhancement. There is no mention of any limitation or subseting.

2. "convert_field(value, conversion) 
Converts the value (returned by get_field()) given a conversion type (as in the tuple returned by the parse() method). The default version understands ‘r’ (repr) and ‘s’ (str) conversion types."

Change "‘r’ (repr) and ‘s’ (str)" to "‘s’ (str), ‘r’ (repr), and ‘a’ (ascii)". I consider this a bug in the doc.

---
I discovered that string.Formatter.format has another problem:
it does not auto-number fields

>>> '{0}'.format(10) == '{}'.format(10) == '10'
True
>>> from string import Formatter as F
>>> F().format('{0}', 10)
'10'
>>> F().format('{}', 10)
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    F().format('{}', 10)
  File "C:\Programs\Python32\lib\string.py", line 180, in format
    return self.vformat(format_string, args, kwargs)
  File "C:\Programs\Python32\lib\string.py", line 184, in vformat
    result = self._vformat(format_string, args, kwargs, used_args, 2)
  File "C:\Programs\Python32\lib\string.py", line 206, in _vformat
    obj, arg_used = self.get_field(field_name, args, kwargs)
  File "C:\Programs\Python32\lib\string.py", line 267, in get_field
    obj = self.get_value(first, args, kwargs)
  File "C:\Programs\Python32\lib\string.py", line 226, in get_value
    return kwargs[key]
KeyError: ''

So it seems that str.format is either calling something other than _string.formatter_parser or else pre or post processing.

The underlying problem is trying to keep separate codes that do the same thing in sync without testing both with the same set of examples. If examples were in a table of (fmt, args, kwargs, output) tuples, it would be easy to call fmt.format(*args, **kwargs) and F().vformat(fmt, args, kwargs) with each and compare to expected. There are also the %format torture tests in test_format.py. Do % and {} formatting use any of the same code?
History
Date User Action Args
2012-02-15 04:40:25terry.reedysetrecipients: + terry.reedy, ncoghlan, eric.smith, francismb
2012-02-15 04:40:25terry.reedysetmessageid: <1329280825.16.0.853034290031.issue13579@psf.upfronthosting.co.za>
2012-02-15 04:40:24terry.reedylinkissue13579 messages
2012-02-15 04:40:22terry.reedycreate