From 257f33ba519cde795c07de81d080a6a0c95e2ab8 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Sat, 23 Dec 2023 15:36:52 -0800 Subject: [PATCH 1/6] PEP 467: ascii constructors removed --- peps/pep-0467.rst | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/peps/pep-0467.rst b/peps/pep-0467.rst index 50eaf43ac0c..d410bcc727c 100644 --- a/peps/pep-0467.rst +++ b/peps/pep-0467.rst @@ -20,7 +20,6 @@ This PEP proposes five small adjustments to the APIs of the ``bytes`` and * Add ``fromsize`` alternative constructor * Add ``fromint`` alternative constructor -* Add ``ascii`` alternative constructor * Add ``getbyte`` byte retrieval method * Add ``iterbytes`` alternative iterator @@ -120,36 +119,6 @@ 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. -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' - -With Python 3 that became the more verbose:: - - >>> b'%d' % 123 - -or even:: - - >>> 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' - -Note that ``bytes.ascii()`` would handle simple ascii-encodable text correctly, -unlike the ``ascii()`` built-in:: - - >>> ascii("hello").encode('ascii') - b"'hello'" - - Addition of "getbyte" method to retrieve a single byte ------------------------------------------------------ From 783c608fbc3a1722f8660c318695d356f92c6259 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 27 Dec 2023 11:01:31 -0800 Subject: [PATCH 2/6] PEP 467: include reference to current iter-bytes thread --- peps/pep-0467.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/peps/pep-0467.rst b/peps/pep-0467.rst index d410bcc727c..188007b0e7b 100644 --- a/peps/pep-0467.rst +++ b/peps/pep-0467.rst @@ -9,7 +9,7 @@ Content-Type: text/x-rst Created: 30-Mar-2014 Python-Version: 3.12 Post-History: 30-Mar-2014, 15-Aug-2014, 16-Aug-2014, 07-Jun-2016, 01-Sep-2016, - 13-Apr-2021, 03-Nov-2021 + 13-Apr-2021, 03-Nov-2021, 27-Dec-2023 Abstract @@ -177,6 +177,13 @@ that ``bytes(x)`` (where ``x`` is an integer) should behave like the ``bytes.fromint(x)`` proposal in this PEP. Providing both behaviors as separate class methods avoids that ambiguity. +Current Workarounds +------------------- + +After nearly a decade, there's seems to be no consensus on the best workarounds +for byte iteration, as demonstrated by +`Get single-byte bytes objects from bytes objects`_. + Omitting the originally proposed builtin function ------------------------------------------------- @@ -211,7 +218,7 @@ References * `Issue proposing to use calloc() for zero-initialised binary sequences `_ * `August 2014 discussion thread on python-dev `_ * `June 2016 discussion thread on python-dev `_ - +* `Get single-byte bytes objects from bytes objects `_ Copyright ========= From 68f8127d264543f62953350678ea7ac9108ff3de Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Thu, 28 Dec 2023 11:37:25 -0800 Subject: [PATCH 3/6] add discouraged use example --- peps/pep-0467.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/peps/pep-0467.rst b/peps/pep-0467.rst index 4cdcfe32782..7608f77fad4 100644 --- a/peps/pep-0467.rst +++ b/peps/pep-0467.rst @@ -56,10 +56,12 @@ Proposals Addition of explicit "count and byte initialised sequence" constructors ----------------------------------------------------------------------- -To replace the now discouraged behavior, this PEP proposes the addition of an -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``):: +To replace the discouraged behavior of creating zero-filled ``bytes``-like +objects from the basic constructors (i.e. ``bytes(1)`` --> ``b'\x00'``), this +PEP proposes the addition of an 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``):: >>> bytes.fromsize(3) b'\x00\x00\x00' From 8253cf552c681572286e39fe0885635044a405e6 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Fri, 29 Dec 2023 16:59:39 -0800 Subject: [PATCH 4/6] fix adjustment count --- peps/pep-0467.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0467.rst b/peps/pep-0467.rst index 7608f77fad4..ec9bbbe9069 100644 --- a/peps/pep-0467.rst +++ b/peps/pep-0467.rst @@ -16,7 +16,7 @@ Post-History: 30-Mar-2014, 15-Aug-2014, 16-Aug-2014, 07-Jun-2016, 01-Sep-2016, Abstract ======== -This PEP proposes five small adjustments to the APIs of the ``bytes`` and +This PEP proposes four small adjustments to the APIs of the ``bytes`` and ``bytearray`` types to make it easier to operate entirely in the binary domain: * Add ``fromsize`` alternative constructor From c9cc00606e3b6f1d179facda3cdc1dccde1c908e Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Sat, 30 Dec 2023 05:26:54 -0800 Subject: [PATCH 5/6] Update peps/pep-0467.rst Co-authored-by: Hugo van Kemenade --- peps/pep-0467.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0467.rst b/peps/pep-0467.rst index ec9bbbe9069..033ce087a45 100644 --- a/peps/pep-0467.rst +++ b/peps/pep-0467.rst @@ -16,7 +16,7 @@ Post-History: 30-Mar-2014, 15-Aug-2014, 16-Aug-2014, 07-Jun-2016, 01-Sep-2016, Abstract ======== -This PEP proposes four small adjustments to the APIs of the ``bytes`` and +This PEP proposes small adjustments to the APIs of the ``bytes`` and ``bytearray`` types to make it easier to operate entirely in the binary domain: * Add ``fromsize`` alternative constructor From b48368df253ae2ae389f76f34d3c8f3cea549266 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Fri, 9 Feb 2024 12:34:00 -0800 Subject: [PATCH 6/6] include memoryview in proposal also remove reference to python 2-3 conversion --- peps/pep-0467.rst | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/peps/pep-0467.rst b/peps/pep-0467.rst index 033ce087a45..95317713b4d 100644 --- a/peps/pep-0467.rst +++ b/peps/pep-0467.rst @@ -24,6 +24,8 @@ This PEP proposes small adjustments to the APIs of the ``bytes`` and * Add ``getbyte`` byte retrieval method * Add ``iterbytes`` alternative iterator +The last two (``getbyte`` and ``iterbytes``) will also be added to ``memoryview``. + Rationale ========= @@ -44,7 +46,7 @@ painful -- wire format protocols. 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. +wire format code, and in updating existing code. Common use-cases include ``dbf`` and ``pdf`` file formats, ``email`` formats, and ``FTP`` and ``HTTP`` communications, among many others. @@ -125,8 +127,8 @@ negative numbers. The documentation of the new methods will refer readers to Addition of "getbyte" method to retrieve a single byte ------------------------------------------------------ -This PEP proposes that ``bytes`` and ``bytearray`` gain the method ``getbyte`` -which will always return ``bytes``:: +This PEP proposes that ``bytes``, ``bytearray``, and ``memoryview`` gain the +method ``getbyte`` which will always return ``bytes``:: >>> b'abc'.getbyte(0) b'a' @@ -142,9 +144,9 @@ If an index is asked for that doesn't exist, ``IndexError`` is raised:: Addition of optimised iterator methods that produce ``bytes`` objects --------------------------------------------------------------------- -This PEP proposes that ``bytes`` and ``bytearray`` gain an optimised -``iterbytes`` method that produces length 1 ``bytes`` objects rather than -integers:: +This PEP proposes that ``bytes``, ``bytearray``, and ``memoryview`` gain an +optimised ``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 @@ -205,13 +207,6 @@ Developers that use this method frequently will instead have the option to define their own ``bchr = bytes.fromint`` aliases. -Scope limitation: memoryview ----------------------------- - -Updating ``memoryview`` with the new item retrieval methods is outside the scope -of this PEP. - - References ==========