2014-03-29 21:28:34 -04:00
|
|
|
PEP: 467
|
2014-08-15 01:34:40 -04:00
|
|
|
Title: Minor API improvements for bytes and bytearray
|
2014-03-29 21:28:34 -04:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
|
|
|
Author: Nick Coghlan <ncoghlan@gmail.com>
|
|
|
|
Status: Draft
|
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 2014-03-30
|
|
|
|
Python-Version: 3.5
|
2014-08-15 01:34:40 -04:00
|
|
|
Post-History: 2014-03-30 2014-08-15
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
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
|
|
|
|
3 series.
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
This PEP proposes four small adjustments to the APIs of the ``bytes``,
|
|
|
|
``bytearray`` and ``memoryview`` types to make it easier to operate entirely
|
|
|
|
in the binary domain:
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
* Deprecate passing single integer values to ``bytes`` and ``bytearray``
|
|
|
|
* Add ``bytes.zeros`` and ``bytearray.zeros`` alternative constructors
|
|
|
|
* Add ``bytes.byte`` and ``bytearray.byte`` alternative constructors
|
|
|
|
* Add ``bytes.iterbytes``, ``bytearray.iterbytes`` and
|
|
|
|
``memoryview.iterbytes`` alternative iterators
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Proposals
|
|
|
|
=========
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Deprecation of current "zero-initialised sequence" behaviour
|
|
|
|
------------------------------------------------------------
|
2014-08-15 01:34:40 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Currently, the ``bytes`` and ``bytearray`` constructors accept an integer
|
|
|
|
argument and interpret it as meaning to create a zero-initialised sequence
|
|
|
|
of the given size::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
>>> bytes(3)
|
|
|
|
b'\x00\x00\x00'
|
|
|
|
>>> bytearray(3)
|
|
|
|
bytearray(b'\x00\x00\x00')
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
This PEP proposes to deprecate that behaviour in Python 3.5, and remove it
|
|
|
|
entirely in Python 3.6.
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
No other changes are proposed to the existing constructors.
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Addition of explicit "zero-initialised sequence" constructors
|
|
|
|
-------------------------------------------------------------
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
To replace the deprecated behaviour, this PEP proposes the addition of an
|
|
|
|
explicit ``zeros`` alternative constructor as a class method on both
|
|
|
|
``bytes`` and ``bytearray``::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
>>> bytes.zeros(3)
|
|
|
|
b'\x00\x00\x00'
|
|
|
|
>>> bytearray.zeros(3)
|
|
|
|
bytearray(b'\x00\x00\x00')
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
It will behave just as the current constructors behave when passed a single
|
|
|
|
integer.
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
The specific choice of ``zeros`` as the alternative constructor name is taken
|
|
|
|
from the corresponding initialisation function in NumPy (although, as these
|
|
|
|
are 1-dimensional sequence types rather than N-dimensional matrices, the
|
|
|
|
constructors take a length as input rather than a shape tuple)
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Addition of explicit "single byte" constructors
|
|
|
|
-----------------------------------------------
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
As binary counterparts to the text ``chr`` function, this PEP proposes the
|
|
|
|
addition of an explicit ``byte`` alternative constructor as a class method
|
|
|
|
on both ``bytes`` and ``bytearray``::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
|
|
|
>>> bytes.byte(3)
|
|
|
|
b'\x03'
|
2014-04-03 08:33:36 -04:00
|
|
|
>>> bytearray.byte(3)
|
|
|
|
bytearray(b'\x03')
|
2014-08-16 01:05:16 -04:00
|
|
|
|
|
|
|
These methods will only accept integers in the range 0 to 255 (inclusive)::
|
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
>>> bytes.byte(512)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
ValueError: bytes must be in range(0, 256)
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
>>> bytes.byte(1.0)
|
|
|
|
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
|
|
|
|
that ``bytes.byte`` is the inverse operation for binary data, while ``chr``
|
|
|
|
is the inverse operation for text data.
|
2014-03-29 21:54:55 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Behaviourally, ``bytes.byte(x)`` will be equivalent to the current
|
|
|
|
``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
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
Addition of optimised iterator methods that produce ``bytes`` objects
|
|
|
|
---------------------------------------------------------------------
|
2014-04-03 08:33:36 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
This PEP proposes that ``bytes``, ``bytearray`` and ``memoryview`` gain an
|
|
|
|
optimised ``iterbytes`` method that produces length 1 ``bytes`` objects
|
|
|
|
rather than integers::
|
2014-04-03 08:33:36 -04:00
|
|
|
|
|
|
|
for x in data.iterbytes():
|
|
|
|
# x is a length 1 ``bytes`` object, rather than an integer
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
The method can be used with arbitrary buffer exporting objects by wrapping
|
|
|
|
them in a ``memoryview`` instance first::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
for x in memoryview(data).iterbytes():
|
2014-03-29 21:28:34 -04:00
|
|
|
# x is a length 1 ``bytes`` object, rather than an integer
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
For ``memoryview``, the semantics of ``iterbytes()`` are defined such that::
|
2014-03-29 21:28:34 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
memview.tobytes() == b''.join(memview.iterbytes())
|
2014-03-30 03:03:44 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
This allows the raw bytes of the memory view to be iterated over without
|
|
|
|
needing to make a copy, regardless of the defined shape and format.
|
2014-03-30 03:03:44 -04:00
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
The main advantage this method offers over the ``map(bytes.byte, data)``
|
|
|
|
approach is that it is guaranteed *not* to fail midstream with a
|
|
|
|
``ValueError`` or ``TypeError``. By contrast, when using the ``map`` based
|
|
|
|
approach, the type and value of the individual items in the iterable are
|
|
|
|
only checked as they are retrieved and passed through the ``bytes.byte``
|
|
|
|
constructor.
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
when it was introduced in PEP 3137.
|
|
|
|
|
|
|
|
This PEP isn't revisiting that original design decision, just changing the
|
|
|
|
spelling as users sometimes find the current behaviour of the binary sequence
|
|
|
|
constructors surprising. In particular, there's a reasonable case to be made
|
|
|
|
that ``bytes(x)`` (where ``x`` is an integer) should behave like the
|
|
|
|
``bytes.byte(x)`` proposal in this PEP. Providing both behaviours as separate
|
|
|
|
class methods avoids that ambiguity.
|
2014-03-30 03:03:44 -04:00
|
|
|
|
|
|
|
|
2014-03-29 21:28:34 -04:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2014-08-16 01:05:16 -04:00
|
|
|
.. [1] Initial March 2014 discussion thread on python-ideas
|
|
|
|
(https://mail.python.org/pipermail/python-ideas/2014-March/027295.html)
|
|
|
|
.. [2] Guido's initial feedback in that thread
|
|
|
|
(https://mail.python.org/pipermail/python-ideas/2014-March/027376.html)
|
|
|
|
.. [3] Issue proposing moving zero-initialised sequences to a dedicated API
|
|
|
|
(http://bugs.python.org/issue20895)
|
|
|
|
.. [4] Issue proposing to use calloc() for zero-initialised binary sequences
|
|
|
|
(http://bugs.python.org/issue21644)
|
|
|
|
.. [5] August 2014 discussion thread on python-dev
|
|
|
|
(https://mail.python.org/pipermail/python-ideas/2014-March/027295.html)
|
2014-03-29 21:28:34 -04: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:
|