PEP 461: updates to %s and Open Questions

This commit is contained in:
Ethan Furman 2014-01-14 18:23:03 -08:00
parent 348e6c78c2
commit 14aeed8b98
1 changed files with 16 additions and 10 deletions

View File

@ -44,13 +44,10 @@ Example:
>>> b'%c' % b'a'
b'a'
%s, because it is the most general, has the most convoluted resolution:
%s is a restricted in what it will accept::
- input type is bytes?
pass it straight through
- input type is numeric?
use its __xxx__ [1] [2] method and ascii-encode it (strictly)
- input type supports Py_buffer?
use it to collect the necessary bytes
- input type is something else?
use its __bytes__ method; if there isn't one, raise an exception [3]
@ -61,7 +58,9 @@ Examples:
b'abc'
>>> b'%s' % 3.14
b'3.14'
Traceback (most recent call last):
...
TypeError: 3.14 has no __bytes__ method
>>> b'%s' % 'hello world!'
Traceback (most recent call last):
@ -77,6 +76,11 @@ Examples:
'a string'.encode('latin-1')
Unsupported % format codes
^^^^^^^^^^^^^^^^^^^^^^^^^^
%r (which calls __repr__) is not supported
format
------
@ -88,14 +92,16 @@ for %-interpolation.
Open Questions
==============
For %s there has been some discussion of trying to use the buffer protocol
(Py_buffer) before trying __bytes__. This question should be answered before
the PEP is implemented.
Do we need no support all the numeric format codes? The floating point
exponential formats seem less appropriate, for example.
Proposed variations
===================
It was suggested to let %s accept numbers, but since numbers have their own
format codes this idea was discarded.
It has been suggested to use %b for bytes instead of %s.
- Rejected as %b does not exist in Python 2.x %-interpolation, which is