Decide that bytes/buffer items are ints.

Also:
- Add mutable sequence methods.
- Add fromhex class method.
- "Supports" -> "implements" consistently.
This commit is contained in:
Guido van Rossum 2007-09-27 17:54:01 +00:00
parent a3b2617793
commit 6cae3f4c44
1 changed files with 20 additions and 16 deletions

View File

@ -88,9 +88,9 @@ PEP 3118 Buffer API
-------------------
Both bytes and buffer implement the PEP 3118 buffer API. The bytes
type only supports read-only requests; the buffer type allows writable
and data-locked requests as well. The element data type is always 'B'
(i.e. unsigned byte).
type only implements read-only requests; the buffer type allows
writable and data-locked requests as well. The element data type is
always 'B' (i.e. unsigned byte).
Constructors
------------
@ -140,17 +140,12 @@ range(256).
Indexing
--------
**Open Issue:** I'm undecided on whether indexing bytes and buffer
objects should return small ints (like the bytes type in 3.0a1, and
like lists or array.array('B')), or bytes/buffer objects of length 1
(like the str type). The latter (str-like) approach will ease porting
code from Python 2.x; but it makes it harder to extract values from a
bytes array.
Indexing bytes and buffer returns small ints (like the bytes type in
3.0a1, and like lists or array.array('B')).
Assignment to an item of a mutable buffer object accepts an int in
range(256); if we choose the str-like approach for indexing above, it
also accepts an object implementing the PEP 3118 buffer API, if it has
length 1.
range(256). (To assing from a bytes sequence, use a slice
assignment.)
Str() and Repr()
----------------
@ -162,7 +157,7 @@ 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,
The following operators are implemented by the bytes and buffer types,
except where mentioned:
- ``b1 + b2``: concatenation. With mixed bytes/buffer operands,
@ -186,13 +181,13 @@ except where mentioned:
- ``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.
Note that the % operator is *not* implemented. It does not appear
worth the complexity.
Methods
-------
The following methods are supported by bytes as well as buffer, with
The following methods are implemented by bytes as well as buffer, with
similar semantics. They accept anything that implements the PEP 3118
buffer API for bytes arguments, and return the same type as the object
whose method is called ("self")::
@ -216,6 +211,15 @@ encoding and decoding in Python 3000: encoding always takes a Unicode
string and returns a bytes sequence, and decoding always takes a bytes
sequence and returns a Unicode string.
In addition, both types implement the class method ``.fromhex()``,
which constructs an object from a string containing hexadecimal values
(with or without spaces between the bytes).
The buffer type implements these additional methods from the
MutableSequence ABC (see PEP 3119):
.extend(), .insert(), .append(), .reverse(), .pop(), .remove().
Bytes and the Str Type
----------------------