2014-01-14 14:23:36 -05:00
|
|
|
PEP: 461
|
2014-02-22 20:53:31 -05:00
|
|
|
Title: Adding % formatting to bytes and bytearray
|
2014-01-14 14:04:10 -05:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
|
|
|
Author: Ethan Furman <ethan@stoneleaf.us>
|
2015-02-01 00:59:42 -05:00
|
|
|
Status: Final
|
2014-01-14 14:04:10 -05:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 2014-01-13
|
|
|
|
Python-Version: 3.5
|
2014-03-27 16:42:38 -04:00
|
|
|
Post-History: 2014-01-14, 2014-01-15, 2014-01-17, 2014-02-22, 2014-03-25,
|
|
|
|
2014-03-27
|
2014-03-27 17:20:20 -04:00
|
|
|
Resolution: http://mail.python.org/pipermail/python-dev/2014-March/133621.html
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2014-01-17 12:07:32 -05:00
|
|
|
This PEP proposes adding % formatting operations similar to Python 2's ``str``
|
2014-02-22 20:53:31 -05:00
|
|
|
type to ``bytes`` and ``bytearray`` [1]_ [2]_.
|
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
|
|
|
While interpolation is usually thought of as a string operation, there are
|
|
|
|
cases where interpolation on ``bytes`` or ``bytearrays`` make sense, and the
|
|
|
|
work needed to make up for this missing functionality detracts from the overall
|
|
|
|
readability of the code.
|
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
|
|
|
With Python 3 and the split between ``str`` and ``bytes``, one small but
|
|
|
|
important area of programming became slightly more difficult, and much more
|
|
|
|
painful -- wire format protocols [3]_.
|
|
|
|
|
|
|
|
This area of programming is characterized by a mixture of binary data and
|
|
|
|
ASCII compatible segments of text (aka ASCII-encoded text). Bringing back a
|
|
|
|
restricted %-interpolation for ``bytes`` and ``bytearray`` will aid both in
|
|
|
|
writing new wire format code, and in porting Python 2 wire format code.
|
2014-01-15 19:12:41 -05:00
|
|
|
|
2014-03-25 18:33:49 -04:00
|
|
|
Common use-cases include ``dbf`` and ``pdf`` file formats, ``email``
|
|
|
|
formats, and ``FTP`` and ``HTTP`` communications, among many others.
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
|
2014-02-22 20:53:31 -05:00
|
|
|
Proposed semantics for ``bytes`` and ``bytearray`` formatting
|
2014-03-02 01:41:09 -05:00
|
|
|
=============================================================
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
%-interpolation
|
|
|
|
---------------
|
|
|
|
|
2014-03-26 10:46:34 -04:00
|
|
|
All the numeric formatting codes (``d``, ``i``, ``o``, ``u``, ``x``, ``X``,
|
2014-03-27 06:34:18 -04:00
|
|
|
``e``, ``E``, ``f``, ``F``, ``g``, ``G``, and any that are subsequently added
|
2014-03-26 10:46:34 -04:00
|
|
|
to Python 3) will be supported, and will work as they do for str, including
|
|
|
|
the padding, justification and other related modifiers (currently ``#``, ``0``,
|
|
|
|
``-``, `` `` (space), and ``+`` (plus any added to Python 3)). The only
|
2014-03-27 14:15:06 -04:00
|
|
|
non-numeric codes allowed are ``c``, ``b``, ``a``, and ``s`` (which is a
|
|
|
|
synonym for b).
|
2014-03-26 10:46:34 -04:00
|
|
|
|
|
|
|
For the numeric codes, the only difference between ``str`` and ``bytes`` (or
|
|
|
|
``bytearray``) interpolation is that the results from these codes will be
|
|
|
|
ASCII-encoded text, not unicode. In other words, for any numeric formatting
|
|
|
|
code `%x`::
|
2014-03-25 18:33:49 -04:00
|
|
|
|
|
|
|
b"%x" % val
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-27 07:22:38 -04:00
|
|
|
is equivalent to::
|
2014-03-25 18:33:49 -04:00
|
|
|
|
|
|
|
("%x" % val).encode("ascii")
|
|
|
|
|
|
|
|
Examples::
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
>>> b'%4x' % 10
|
|
|
|
b' a'
|
|
|
|
|
2014-03-25 18:33:49 -04:00
|
|
|
>>> b'%#4x' % 10
|
2014-01-17 12:07:32 -05:00
|
|
|
' 0xa'
|
|
|
|
|
2014-03-25 18:33:49 -04:00
|
|
|
>>> b'%04X' % 10
|
2014-01-17 12:07:32 -05:00
|
|
|
'000A'
|
|
|
|
|
|
|
|
``%c`` will insert a single byte, either from an ``int`` in range(256), or from
|
|
|
|
a ``bytes`` argument of length 1, not from a ``str``.
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-25 18:33:49 -04:00
|
|
|
Examples::
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
>>> b'%c' % 48
|
|
|
|
b'0'
|
|
|
|
|
|
|
|
>>> b'%c' % b'a'
|
|
|
|
b'a'
|
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
``%b`` will insert a series of bytes. These bytes are collected in one of two
|
2015-03-14 15:46:53 -04:00
|
|
|
ways::
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- input type supports ``Py_buffer`` [4]_?
|
|
|
|
use it to collect the necessary bytes
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- input type is something else?
|
|
|
|
use its ``__bytes__`` method [5]_ ; if there isn't one, raise a ``TypeError``
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
In particular, ``%b`` will not accept numbers nor ``str``. ``str`` is rejected
|
2014-03-27 14:06:08 -04:00
|
|
|
as the string to bytes conversion requires an encoding, and we are refusing to
|
|
|
|
guess; numbers are rejected because:
|
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- what makes a number is fuzzy (float? Decimal? Fraction? some user type?)
|
2014-03-27 14:06:08 -04:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- allowing numbers would lead to ambiguity between numbers and textual
|
|
|
|
representations of numbers (3.14 vs '3.14')
|
2014-03-27 14:06:08 -04:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- given the nature of wire formats, explicit is definitely better than implicit
|
2014-03-25 18:33:49 -04:00
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
``%s`` is included as a synonym for ``%b`` for the sole purpose of making 2/3 code
|
|
|
|
bases easier to maintain. Python 3 only code should use ``%b``.
|
|
|
|
|
2014-02-22 21:41:14 -05:00
|
|
|
Examples::
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
>>> b'%b' % b'abc'
|
2014-01-14 14:04:10 -05:00
|
|
|
b'abc'
|
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
>>> b'%b' % 'some string'.encode('utf8')
|
2014-03-25 18:33:49 -04:00
|
|
|
b'some string'
|
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
>>> b'%b' % 3.14
|
2014-01-14 21:23:03 -05:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-27 14:15:06 -04:00
|
|
|
TypeError: b'%b' does not accept 'float'
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
>>> b'%b' % 'hello world!'
|
2014-01-14 14:04:10 -05:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-27 14:15:06 -04:00
|
|
|
TypeError: b'%b' does not accept 'str'
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-02-22 20:53:31 -05:00
|
|
|
|
2014-03-26 10:46:34 -04:00
|
|
|
``%a`` will give the equivalent of
|
|
|
|
``repr(some_obj).encode('ascii', 'backslashreplace')`` on the interpolated
|
|
|
|
value. Use cases include developing a new protocol and writing landmarks
|
|
|
|
into the stream; debugging data going into an existing protocol to see if
|
|
|
|
the problem is the protocol itself or bad data; a fall-back for a serialization
|
|
|
|
format; or any situation where defining ``__bytes__`` would not be appropriate
|
2014-03-27 14:06:08 -04:00
|
|
|
but a readable/informative representation is needed [6]_.
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2015-03-14 15:46:53 -04:00
|
|
|
``%r`` is included as a synonym for ``%a`` for the sole purpose of making 2/3
|
|
|
|
code bases easier to maintain. Python 3 only code use ``%a`` [7]_.
|
|
|
|
|
2014-03-26 10:46:34 -04:00
|
|
|
Examples::
|
|
|
|
|
|
|
|
>>> b'%a' % 3.14
|
|
|
|
b'3.14'
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-26 10:46:34 -04:00
|
|
|
>>> b'%a' % b'abc'
|
2014-03-26 18:47:12 -04:00
|
|
|
b"b'abc'"
|
2014-03-26 10:46:34 -04:00
|
|
|
|
|
|
|
>>> b'%a' % 'def'
|
|
|
|
b"'def'"
|
2014-01-17 12:07:32 -05:00
|
|
|
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
|
2014-03-27 15:46:04 -04:00
|
|
|
Compatibility with Python 2
|
|
|
|
===========================
|
|
|
|
|
2015-03-14 15:46:53 -04:00
|
|
|
As noted above, ``%s`` and ``%r`` are being included solely to help ease
|
|
|
|
migration from, and/or have a single code base with, Python 2. This is
|
|
|
|
important as there are modules both in the wild and behind closed doors that
|
|
|
|
currently use the Python 2 ``str`` type as a ``bytes`` container, and hence
|
|
|
|
are using ``%s`` as a bytes interpolator.
|
2014-03-27 15:46:04 -04:00
|
|
|
|
2015-03-14 15:46:53 -04:00
|
|
|
However, ``%b`` and ``%a`` should be used in new, Python 3 only code, so ``%s``
|
|
|
|
and ``%r`` will immediately be deprecated, but not removed from the 3.x series
|
|
|
|
[7]_.
|
2014-03-27 15:46:04 -04:00
|
|
|
|
2014-01-14 14:04:10 -05:00
|
|
|
Proposed variations
|
|
|
|
===================
|
|
|
|
|
2014-01-17 12:07:32 -05:00
|
|
|
It has been proposed to automatically use ``.encode('ascii','strict')`` for
|
2014-03-27 14:15:06 -04:00
|
|
|
``str`` arguments to ``%b``.
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- Rejected as this would lead to intermittent failures. Better to have the
|
|
|
|
operation always fail so the trouble-spot can be correctly fixed.
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-03-27 14:15:06 -04:00
|
|
|
It has been proposed to have ``%b`` return the ascii-encoded repr when the
|
|
|
|
value is a ``str`` (b'%b' % 'abc' --> b"'abc'").
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- Rejected as this would lead to hard to debug failures far from the problem
|
|
|
|
site. Better to have the operation always fail so the trouble-spot can be
|
|
|
|
easily fixed.
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-02-22 20:53:31 -05:00
|
|
|
Originally this PEP also proposed adding format-style formatting, but it was
|
|
|
|
decided that format and its related machinery were all strictly text (aka
|
|
|
|
``str``) based, and it was dropped.
|
2014-01-17 12:07:32 -05:00
|
|
|
|
|
|
|
Various new special methods were proposed, such as ``__ascii__``,
|
|
|
|
``__format_bytes__``, etc.; such methods are not needed at this time, but can
|
|
|
|
be visited again later if real-world use shows deficiencies with this solution.
|
|
|
|
|
2015-03-14 15:46:53 -04:00
|
|
|
A competing PEP, ``PEP 460 Add binary interpolation and formatting`` [8]_,
|
2014-03-27 07:22:38 -04:00
|
|
|
also exists.
|
2014-03-26 10:46:34 -04:00
|
|
|
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-02-22 21:41:14 -05:00
|
|
|
Objections
|
|
|
|
==========
|
|
|
|
|
2014-03-27 14:06:08 -04:00
|
|
|
The objections raised against this PEP were mainly variations on two themes:
|
2014-02-22 21:41:14 -05:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- the ``bytes`` and ``bytearray`` types are for pure binary data, with no
|
|
|
|
assumptions about encodings
|
2014-03-27 14:06:08 -04:00
|
|
|
|
2016-05-03 04:18:02 -04:00
|
|
|
- offering %-interpolation that assumes an ASCII encoding will be an
|
|
|
|
attractive nuisance and lead us back to the problems of the Python 2
|
|
|
|
``str``/``unicode`` text model
|
2014-02-22 21:41:14 -05:00
|
|
|
|
|
|
|
As was seen during the discussion, ``bytes`` and ``bytearray`` are also used
|
|
|
|
for mixed binary data and ASCII-compatible segments: file formats such as
|
|
|
|
``dbf`` and ``pdf``, network protocols such as ``ftp`` and ``email``, etc.
|
|
|
|
|
|
|
|
``bytes`` and ``bytearray`` already have several methods which assume an ASCII
|
|
|
|
compatible encoding. ``upper()``, ``isalpha()``, and ``expandtabs()`` to name
|
|
|
|
just a few. %-interpolation, with its very restricted mini-language, will not
|
2014-03-25 18:33:49 -04:00
|
|
|
be any more of a nuisance than the already existing methods.
|
2014-02-22 20:53:31 -05:00
|
|
|
|
2014-03-25 18:33:49 -04:00
|
|
|
Some have objected to allowing the full range of numeric formatting codes with
|
|
|
|
the claim that decimal alone would be sufficient. However, at least two
|
|
|
|
formats (dbf and pdf) make use of non-decimal numbers.
|
2014-02-22 20:53:31 -05:00
|
|
|
|
|
|
|
|
2014-01-15 19:12:41 -05:00
|
|
|
Footnotes
|
|
|
|
=========
|
2014-01-14 14:04:10 -05:00
|
|
|
|
2014-01-17 12:07:32 -05:00
|
|
|
.. [1] http://docs.python.org/2/library/stdtypes.html#string-formatting
|
2014-02-22 20:53:31 -05:00
|
|
|
.. [2] neither string.Template, format, nor str.format are under consideration
|
|
|
|
.. [3] https://mail.python.org/pipermail/python-dev/2014-January/131518.html
|
2014-03-27 14:06:08 -04:00
|
|
|
.. [4] http://docs.python.org/3/c-api/buffer.html
|
2014-02-22 20:53:31 -05:00
|
|
|
examples: ``memoryview``, ``array.array``, ``bytearray``, ``bytes``
|
2014-03-27 14:06:08 -04:00
|
|
|
.. [5] http://docs.python.org/3/reference/datamodel.html#object.__bytes__
|
|
|
|
.. [6] https://mail.python.org/pipermail/python-dev/2014-February/132750.html
|
2015-03-14 15:46:53 -04:00
|
|
|
.. [7] http://bugs.python.org/issue23467 -- originally ``%r`` was not allowed,
|
|
|
|
but was added for consistency during the 3.5 alpha stage.
|
|
|
|
.. [8] http://python.org/dev/peps/pep-0460/
|
2014-01-14 14:04:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|
|
|
|
End:
|