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