2014-03-29 21:28:34 -04:00
|
|
|
PEP: 467
|
2014-08-16 02:59:02 -04:00
|
|
|
Title: Minor API improvements for binary sequences
|
2014-03-29 21:28:34 -04:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2018-02-21 22:18:38 -05:00
|
|
|
Author: Nick Coghlan <ncoghlan@gmail.com>, Ethan Furman <ethan@stoneleaf.us>
|
2021-04-13 19:21:06 -04:00
|
|
|
Status: Draft
|
2014-03-29 21:28:34 -04:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
2021-02-09 11:54:26 -05:00
|
|
|
Created: 30-Mar-2014
|
2022-08-24 18:39:36 -04:00
|
|
|
Python-Version: 3.12
|
2022-03-09 11:04:44 -05:00
|
|
|
Post-History: 30-Mar-2014, 15-Aug-2014, 16-Aug-2014, 07-Jun-2016, 01-Sep-2016,
|
|
|
|
13-Apr-2021, 03-Nov-2021
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
This PEP proposes five small adjustments to the APIs of the ``bytes`` and
|
|
|
|
``bytearray`` types to make it easier to operate entirely in the binary domain:
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-11-03 00:37:35 -04:00
|
|
|
* Add ``fromsize`` alternative constructor
|
|
|
|
* Add ``fromint`` alternative constructor
|
|
|
|
* Add ``ascii`` alternative constructor
|
|
|
|
* Add ``getbyte`` byte retrieval method
|
|
|
|
* Add ``iterbytes`` alternative iterator
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
Rationale
|
2014-08-16 01:05:16 -04:00
|
|
|
=========
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
During the initial development of the Python 3 language specification, the
|
|
|
|
core ``bytes`` type for arbitrary binary data started as the mutable type
|
|
|
|
that is now referred to as ``bytearray``. Other aspects of operating in
|
|
|
|
the binary domain in Python have also evolved over the course of the Python
|
2022-01-21 06:03:51 -05:00
|
|
|
3 series, for example with :pep:`461`.
|
2014-08-15 01:34:40 -04:00
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
Motivation
|
|
|
|
==========
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
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.
|
2019-04-21 11:59:45 -04:00
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
This area of programming is characterized by a mixture of binary data and
|
|
|
|
ASCII compatible segments of text (aka ASCII-encoded text). The addition of
|
|
|
|
the new constructors, methods, and iterators will aid both in writing new
|
|
|
|
wire format code, and in porting any remaining Python 2 wire format code.
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
Common use-cases include ``dbf`` and ``pdf`` file formats, ``email``
|
|
|
|
formats, and ``FTP`` and ``HTTP`` communications, among many others.
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
2021-10-24 01:39:28 -04:00
|
|
|
Proposals
|
|
|
|
=========
|
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
Addition of explicit "count and byte initialised sequence" constructors
|
|
|
|
-----------------------------------------------------------------------
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-08-02 21:12:32 -04:00
|
|
|
To replace the now discouraged behavior, this PEP proposes the addition of an
|
2018-02-21 22:18:38 -05:00
|
|
|
explicit ``fromsize`` alternative constructor as a class method on both
|
|
|
|
``bytes`` and ``bytearray`` whose first argument is the count, and whose
|
|
|
|
second argument is the fill byte to use (defaults to ``\x00``)::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
>>> bytes.fromsize(3)
|
2014-08-16 01:05:16 -04:00
|
|
|
b'\x00\x00\x00'
|
2018-02-21 22:18:38 -05:00
|
|
|
>>> bytearray.fromsize(3)
|
2014-08-16 01:05:16 -04:00
|
|
|
bytearray(b'\x00\x00\x00')
|
2018-02-21 22:18:38 -05:00
|
|
|
>>> bytes.fromsize(5, b'\x0a')
|
|
|
|
b'\x0a\x0a\x0a\x0a\x0a'
|
2021-08-02 21:12:32 -04:00
|
|
|
>>> bytearray.fromsize(5, fill=b'\x0a')
|
2018-02-21 22:18:38 -05:00
|
|
|
bytearray(b'\x0a\x0a\x0a\x0a\x0a')
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2019-04-21 11:59:45 -04:00
|
|
|
``fromsize`` will behave just as the current constructors behave when passed a
|
|
|
|
single integer, while allowing for non-zero fill values when needed.
|
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-09-09 17:44:11 -04:00
|
|
|
Addition of explicit "single byte" constructors
|
|
|
|
-----------------------------------------------
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
As binary counterparts to the text ``chr`` function, this PEP proposes
|
2021-09-09 17:44:11 -04:00
|
|
|
the addition of an explicit ``fromint`` alternative constructor as a class
|
|
|
|
method on both ``bytes`` and ``bytearray``::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-08-02 21:12:32 -04:00
|
|
|
>>> bytes.fromint(65)
|
2018-02-21 22:18:38 -05:00
|
|
|
b'A'
|
2021-08-02 21:12:32 -04:00
|
|
|
>>> bytearray.fromint(65)
|
2018-02-21 22:18:38 -05:00
|
|
|
bytearray(b'A')
|
2014-08-16 01:05:16 -04:00
|
|
|
|
|
|
|
These methods will only accept integers in the range 0 to 255 (inclusive)::
|
|
|
|
|
2021-08-02 21:12:32 -04:00
|
|
|
>>> bytes.fromint(512)
|
2014-03-29 21:28:34 -04:00
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
2018-02-21 22:18:38 -05:00
|
|
|
ValueError: integer must be in range(0, 256)
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-08-02 21:12:32 -04:00
|
|
|
>>> bytes.fromint(1.0)
|
2014-08-16 01:05:16 -04:00
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
TypeError: 'float' object cannot be interpreted as an integer
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
The documentation of the ``ord`` builtin will be updated to explicitly note
|
2021-09-09 17:44:11 -04:00
|
|
|
that ``bytes.fromint`` is the primary inverse operation for binary data, while
|
|
|
|
``chr`` is the inverse operation for text data, and that ``bytearray.fromint``
|
|
|
|
also exists.
|
2014-03-29 21:54:55 -04:00
|
|
|
|
2021-08-02 21:12:32 -04:00
|
|
|
Behaviorally, ``bytes.fromint(x)`` will be equivalent to the current
|
2014-08-16 01:05:16 -04:00
|
|
|
``bytes([x])`` (and similarly for ``bytearray``). The new spelling is
|
|
|
|
expected to be easier to discover and easier to read (especially when used
|
|
|
|
in conjunction with indexing operations on binary sequence types).
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
As a separate method, the new spelling will also work better with higher
|
|
|
|
order functions like ``map``.
|
2014-03-29 21:54:55 -04:00
|
|
|
|
2021-09-09 17:44:11 -04:00
|
|
|
These new methods intentionally do NOT offer the same level of general integer
|
|
|
|
support as the existing ``int.to_bytes`` conversion method, which allows
|
2021-09-17 14:18:24 -04:00
|
|
|
arbitrarily large integers to be converted to arbitrarily long bytes objects. The
|
2021-09-09 17:44:11 -04:00
|
|
|
restriction to only accept positive integers that fit in a single byte means
|
|
|
|
that no byte order information is needed, and there is no need to handle
|
|
|
|
negative numbers. The documentation of the new methods will refer readers to
|
|
|
|
``int.to_bytes`` for use cases where handling of arbitrary integers is needed.
|
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2021-11-03 00:37:35 -04:00
|
|
|
Addition of "ascii" constructors
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
In Python 2 converting an object, such as the integer ``123``, to bytes (aka the
|
|
|
|
Python 2 ``str``) was as simple as::
|
|
|
|
|
|
|
|
>>> str(123)
|
|
|
|
'123'
|
2022-08-17 01:25:53 -04:00
|
|
|
|
2021-11-03 00:37:35 -04:00
|
|
|
With Python 3 that became the more verbose::
|
|
|
|
|
2021-11-04 02:32:54 -04:00
|
|
|
>>> b'%d' % 123
|
|
|
|
|
|
|
|
or even::
|
|
|
|
|
2021-11-03 00:37:35 -04:00
|
|
|
>>> str(123).encode('ascii')
|
|
|
|
|
|
|
|
This PEP proposes that an ``ascii`` method be added to ``bytes`` and ``bytearray``
|
|
|
|
to handle this use-case::
|
|
|
|
|
|
|
|
>>> bytes.ascii(123)
|
|
|
|
b'123'
|
|
|
|
|
2021-11-04 02:32:54 -04:00
|
|
|
Note that ``bytes.ascii()`` would handle simple ascii-encodable text correctly,
|
2022-01-21 06:03:51 -05:00
|
|
|
unlike the ``ascii()`` built-in::
|
2021-11-04 02:32:54 -04:00
|
|
|
|
|
|
|
>>> ascii("hello").encode('ascii')
|
|
|
|
b"'hello'"
|
|
|
|
|
2021-11-03 00:37:35 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
Addition of "getbyte" method to retrieve a single byte
|
|
|
|
------------------------------------------------------
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
This PEP proposes that ``bytes`` and ``bytearray`` gain the method ``getbyte``
|
|
|
|
which will always return ``bytes``::
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
>>> b'abc'.getbyte(0)
|
|
|
|
b'a'
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
If an index is asked for that doesn't exist, ``IndexError`` is raised::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
>>> b'abc'.getbyte(9)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
IndexError: index out of range
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
Addition of optimised iterator methods that produce ``bytes`` objects
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
|
2020-08-12 11:05:43 -04:00
|
|
|
This PEP proposes that ``bytes`` and ``bytearray`` gain an optimised
|
2018-02-21 22:18:38 -05:00
|
|
|
``iterbytes`` method that produces length 1 ``bytes`` objects rather than
|
|
|
|
integers::
|
|
|
|
|
|
|
|
for x in data.iterbytes():
|
|
|
|
# x is a length 1 ``bytes`` object, rather than an integer
|
2014-03-30 03:03:44 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
For example::
|
2014-03-30 03:03:44 -04:00
|
|
|
|
2018-02-21 22:18:38 -05:00
|
|
|
>>> tuple(b"ABC".iterbytes())
|
|
|
|
(b'A', b'B', b'C')
|
2014-08-16 01:05:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
Design discussion
|
|
|
|
=================
|
|
|
|
|
|
|
|
Why not rely on sequence repetition to create zero-initialised sequences?
|
|
|
|
-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Zero-initialised sequences can be created via sequence repetition::
|
|
|
|
|
|
|
|
>>> b'\x00' * 3
|
|
|
|
b'\x00\x00\x00'
|
|
|
|
>>> bytearray(b'\x00') * 3
|
|
|
|
bytearray(b'\x00\x00\x00')
|
|
|
|
|
|
|
|
However, this was also the case when the ``bytearray`` type was originally
|
|
|
|
designed, and the decision was made to add explicit support for it in the
|
|
|
|
type constructor. The immutable ``bytes`` type then inherited that feature
|
2022-01-21 06:03:51 -05:00
|
|
|
when it was introduced in :pep:`3137`.
|
2014-08-16 01:05:16 -04:00
|
|
|
|
|
|
|
This PEP isn't revisiting that original design decision, just changing the
|
2021-08-02 21:12:32 -04:00
|
|
|
spelling as users sometimes find the current behavior of the binary sequence
|
2014-08-16 01:05:16 -04:00
|
|
|
constructors surprising. In particular, there's a reasonable case to be made
|
|
|
|
that ``bytes(x)`` (where ``x`` is an integer) should behave like the
|
2021-08-02 21:12:32 -04:00
|
|
|
``bytes.fromint(x)`` proposal in this PEP. Providing both behaviors as separate
|
2014-08-16 01:05:16 -04:00
|
|
|
class methods avoids that ambiguity.
|
2014-03-30 03:03:44 -04:00
|
|
|
|
|
|
|
|
2021-09-09 17:44:11 -04:00
|
|
|
Omitting the originally proposed builtin function
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
When submitted to the Steering Council, this PEP proposed the introduction of
|
|
|
|
a ``bchr`` builtin (with the same behaviour as ``bytes.fromint``), recreating
|
|
|
|
the ``ord``/``chr``/``unichr`` trio from Python 2 under a different naming
|
|
|
|
scheme (``ord``/``bchr``/``chr``).
|
|
|
|
|
|
|
|
The SC indicated they didn't think this functionality was needed often enough
|
|
|
|
to justify offering two ways of doing the same thing, especially when one of
|
|
|
|
those ways was a new builtin function. That part of the proposal was therefore
|
|
|
|
dropped as being redundant with the ``bytes.fromint`` alternate constructor.
|
|
|
|
|
|
|
|
Developers that use this method frequently will instead have the option to
|
|
|
|
define their own ``bchr = bytes.fromint`` aliases.
|
|
|
|
|
2019-04-21 11:59:45 -04:00
|
|
|
|
2021-09-09 17:44:11 -04:00
|
|
|
Scope limitation: memoryview
|
|
|
|
----------------------------
|
2018-02-21 22:18:38 -05:00
|
|
|
|
2021-09-09 17:44:11 -04:00
|
|
|
Updating ``memoryview`` with the new item retrieval methods is outside the scope
|
|
|
|
of this PEP.
|
2018-02-21 22:18:38 -05:00
|
|
|
|
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2022-08-17 01:25:53 -04:00
|
|
|
* `Initial March 2014 discussion thread on python-ideas <https://mail.python.org/pipermail/python-ideas/2014-March/027295.html>`_
|
|
|
|
* `Guido's initial feedback in that thread <https://mail.python.org/pipermail/python-ideas/2014-March/027376.html>`_
|
|
|
|
* `Issue proposing moving zero-initialised sequences to a dedicated API <https://github.com/python/cpython/issues/65094>`_
|
|
|
|
* `Issue proposing to use calloc() for zero-initialised binary sequences <https://github.com/python/cpython/issues/65843>`_
|
|
|
|
* `August 2014 discussion thread on python-dev <https://mail.python.org/pipermail/python-ideas/2014-March/027295.html>`_
|
|
|
|
* `June 2016 discussion thread on python-dev <https://mail.python.org/pipermail/python-dev/2016-June/144875.html>`_
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|