[Python-3000] PEP 3137: Immutable Bytes and Mutable Buffer

Guido van Rossum guido at python.org
Thu Sep 27 19:44:51 CEST 2007


On 9/27/07, Joel Bender <jjb5 at cornell.edu> wrote:
> First, please enforce that where these functions take a "string"
> parameter that they require an octet or octet string (I couldn't find
> what kinds of arguments these functions require in PEP 3118):
>
>      >>> x = b'123*45'
>      >>> x.find("*")
>      TypeError: expected an octet string or int
>
>      >>> x.find(b'*')
>      3
>      >>> x.find(42)
>      3

PEP 3118 has nothing to do with this, but one of the last paragraphs
of PEP 3137 spells it out:

"""
The str type currently implements the PEP 3118 buffer API.  While this
is perhaps occasionally convenient, it is also potentially confusing,
because the bytes accessed via the buffer API represent a
platform-depending encoding: depending on the platform byte order and
a compile-time configuration option, the encoding could be UTF-16-BE,
UTF-16-LE, UTF-32-BE, or UTF-32-LE.  Worse, a different implementation
of the str type might completely change the bytes representation,
e.g. to UTF-8, or even make it impossible to access the data as a
contiguous array of bytes at all.  Therefore, the PEP 3118 buffer API
will be removed from the str type.
"""

> Second, Please add slice operations and .append() to mutable octet strings:
>
>      >>> x[:0] = b'>'              # start of message
>      >>> x.append(sum(x) % 256)    # simple checksum

Slice operations area already in the PEP, under "Slicing":

"""
Slice assignment to a mutable buffer object accept anything that
implements the PEP 3118 buffer API, or an iterable of integers in
range(256).
"""

I agree that append() and a few other list methods (insert(),
extend()) should be added to the buffer type. The PyBytes
implementation already has these so it's just a matter of updating the
PEP.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list