classification
Title: fill character cannot be '{'
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.araujo, eric.smith, flox, georg.brandl
Priority: normal Keywords:

Created on 2010-09-06 00:11 by flox, last changed 2010-09-06 15:09 by eric.smith. This issue is now closed.

Messages (6)
msg115678 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-09-06 00:11
According to the documentation:
"The fill character can be any character other than ‘}’ (which signifies the end of the field)."
http://docs.python.org/dev/library/string.html#format-specification-mini-language


However the format() builtin accepts both '{' and '}' characters:

>>> format(42, '}^6')
'}}42}}'

>>> format(42, '{^6')
'{{42{{'


And the string method rejects both characters.

>>> '{:}^6}'.format(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Single '}' encountered in format string

>>> '{:{^6}'.format(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unmatched '{' in format
msg115679 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-06 01:04
Looks more like a behavior bug than a doc bug to me. Does the doc comply with the PEP?
msg115681 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-09-06 01:21
The PEP 3101 does not prohibit any character for the 'fill' argument.

Another example which just works:

>>> '{:{fill}^6}'.format(42, fill='{')
'{{42{{'

>>> '{:{fill}^6}'.format(42, fill='}')
'}}42}}'


I don't care if '{' and '}' are prohibited when using simple formatting syntax.  This is not a common use case, and there are workarounds (either using format() builtin or the recursive formatting).

A documentation fix could be enough.
msg115685 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-06 01:57
Thanks for clarifying.
msg115692 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-09-06 06:50
Fixed docs in r84553.

(That builtin format() supports this is no surprise, and has no influence on the validity in format strings.)
msg115710 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-09-06 15:09
Sorry to respond late.

The reason for this is that the parsing of the string (as delimited by "{" and "}") happens before the results are then interpreted as format specifiers. There's no way around it, short of the parser understanding every object's formatting language, which is of course not possible. It could be special cased for string, int, and float format specifiers, but that doesn't make much sense.

I think the doc change is good.
History
Date User Action Args
2010-09-06 15:09:01eric.smithsetmessages: + msg115710
2010-09-06 06:50:15georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg115692

resolution: fixed
2010-09-06 01:57:36eric.araujosetmessages: + msg115685
2010-09-06 01:21:28floxsetmessages: + msg115681
2010-09-06 01:04:44eric.araujosetnosy: + eric.smith, eric.araujo
messages: + msg115679
2010-09-06 00:11:21floxcreate