Added language about operators and pickling.

Standardized on "implementing" the PEP 3118 buffer API rather than
"supporting" it.
This commit is contained in:
Guido van Rossum 2007-09-26 23:06:19 +00:00
parent 1eaf0ea59d
commit 74119bb932
1 changed files with 44 additions and 9 deletions

View File

@ -87,9 +87,9 @@ Functionality
PEP 3118 Buffer API PEP 3118 Buffer API
------------------- -------------------
Both bytes and buffer support the PEP 3118 buffer API. The bytes type Both bytes and buffer implement the PEP 3118 buffer API. The bytes
only supports read-only requests; the buffer type allows writable and type only supports read-only requests; the buffer type allows writable
data-locked requests as well. The element data type is always 'B' and data-locked requests as well. The element data type is always 'B'
(i.e. unsigned byte). (i.e. unsigned byte).
Constructors Constructors
@ -108,7 +108,7 @@ buffer:
The <encoding> argument is mandatory; <errors> is optional. The <encoding> argument is mandatory; <errors> is optional.
- ``bytes(<memory view>)``, ``buffer(<memory view>)``: construct a - ``bytes(<memory view>)``, ``buffer(<memory view>)``: construct a
bytes or buffer object from anything that supports the PEP 3118 bytes or buffer object from anything implementing the PEP 3118
buffer API. buffer API.
- ``bytes(<iterable of ints>)``, ``buffer(<iterable of ints>)``: - ``bytes(<iterable of ints>)``, ``buffer(<iterable of ints>)``:
@ -134,7 +134,7 @@ Slicing a bytes object returns a bytes object. Slicing a buffer
object returns a buffer object. object returns a buffer object.
Slice assignment to a mutable buffer object accept anything that Slice assignment to a mutable buffer object accept anything that
supports the PEP 3118 buffer API, or an iterable of integers in implements the PEP 3118 buffer API, or an iterable of integers in
range(256). range(256).
Indexing Indexing
@ -159,6 +159,36 @@ The str() and repr() functions return the same thing for these
objects. The repr() of a bytes object returns a b'...' style literal. objects. The repr() of a bytes object returns a b'...' style literal.
The repr() of a buffer returns a string of the form "buffer(b'...')". The repr() of a buffer returns a string of the form "buffer(b'...')".
Operators
---------
The following operators are supported by the bytes and buffer types,
except where mentioned:
- ``b1 + b2``: concatenation. With mixed bytes/buffer operands,
the return type is that of the first argument (this seems arbitrary
until you consider how ``+=`` works).
- ``b1 += b2'': mutates b1 if it is a buffer object.
- ``b * n``, ``n * b``: repetition; n must be an integer.
- ``b *= n``: mutates b if it is a buffer object.
- ``b1 in b2``, ``b1 not in b2``: substring test; b1 can be any
object implementing the PEP 3118 buffer API.
- ``i in b``, ``i not in b``: single-byte membership test; i must
be an integer (if it is a length-1 bytes array, it is considered
to be a substring test, with the same outcome).
- ``len(b)``: the number of bytes.
- ``hash(b)``: the hash value; only implemented by the bytes type.
Note that the % operator is *not* supported. It does not appear worth
the complexity.
Methods Methods
------- -------
@ -212,16 +242,21 @@ is just a special case of conversion to str. There is however no
promise that printing a bytes object interprets the individual bytes promise that printing a bytes object interprets the individual bytes
as characters (unlike in Python 2.x). as characters (unlike in Python 2.x).
The str type currently supports the PEP 3118 buffer API. While this is The str type currently implements the PEP 3118 buffer API. While this
perhaps occasionally convenient, it is also potentially confusing, is perhaps occasionally convenient, it is also potentially confusing,
because the bytes accessed via the buffer API represent a because the bytes accessed via the buffer API represent a
platform-depending encoding: depending on the platform byte order and platform-depending encoding: depending on the platform byte order and
a compile-time configuration option, the encoding could be UTF-16-BE, 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 UTF-16-LE, UTF-32-BE, or UTF-32-LE. Worse, a different implementation
of the str type might completely change the bytes representation, 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 e.g. to UTF-8, or even make it impossible to access the data as a
contiguous array of bytes at all. Therefore, support for the PEP 3118 contiguous array of bytes at all. Therefore, the PEP 3118 buffer API
buffer API will be removed from the str type. will be removed from the str type.
Pickling
--------
Left as an exercise for the reader.
Copyright Copyright
========= =========