Merging the py3k-pep3137 branch back into the py3k branch. · pythoncapi/cpython@98297ee · GitHub
Skip to content

Commit 98297ee

Browse files
committed
Merging the py3k-pep3137 branch back into the py3k branch.
No detailed change log; just check out the change log for the py3k-pep3137 branch. The most obvious changes: - str8 renamed to bytes (PyString at the C level); - bytes renamed to buffer (PyBytes at the C level); - PyString and PyUnicode are no longer compatible. I.e. we now have an immutable bytes type and a mutable bytes type. The behavior of PyString was modified quite a bit, to make it more bytes-like. Some changes are still on the to-do list.
1 parent a19f80c commit 98297ee

148 files changed

Lines changed: 2528 additions & 3512 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Doc/library/array.rst

Lines changed: 7 additions & 2 deletions

Doc/library/exceptions.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,11 @@ module for more information.
405405

406406
Base class for warnings related to Unicode.
407407

408-
The class hierarchy for built-in exceptions is:
408+
.. exception:: BytesWarning
409+
410+
Base class for warnings related to :class:`bytes` and :class:`buffer`.
409411

410412

413+
The class hierarchy for built-in exceptions is:
414+
411415
.. literalinclude:: ../../Lib/test/exception_hierarchy.txt

Doc/library/functions.rst

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,44 @@ available. They are listed here in alphabetical order.
118118
.. index:: pair: Boolean; type
119119

120120

121-
.. function:: bytes([arg[, encoding[, errors]]])
121+
.. function:: buffer([arg[, encoding[, errors]]])
122122

123-
Return a new array of bytes. The :class:`bytes` type is a mutable sequence
123+
Return a new array of bytes. The :class:`buffer` type is an immutable sequence
124124
of integers in the range 0 <= x < 256. It has most of the usual methods of
125-
mutable sequences, described in :ref:`typesseq-mutable`, as well as a few
126-
methods borrowed from strings, described in :ref:`bytes-methods`.
125+
mutable sequences, described in :ref:`typesseq-mutable`, as well as most methods
126+
that the :class:`str` type has, see :ref:`bytes-methods`.
127127

128128
The optional *arg* parameter can be used to initialize the array in a few
129129
different ways:
130130

131131
* If it is a *string*, you must also give the *encoding* (and optionally,
132-
*errors*) parameters; :func:`bytes` then acts like :meth:`str.encode`.
132+
*errors*) parameters; :func:`buffer` then converts the Unicode string to
133+
bytes using :meth:`str.encode`.
133134

134135
* If it is an *integer*, the array will have that size and will be
135136
initialized with null bytes.
136137

137138
* If it is an object conforming to the *buffer* interface, a read-only buffer
138139
of the object will be used to initialize the bytes array.
139140

140-
* If it is an *iterable*, it must be an iterable of integers in the range 0
141-
<= x < 256, which are used as the initial contents of the array.
141+
* If it is an *iterable*, it must be an iterable of integers in the range
142+
``0 <= x < 256``, which are used as the initial contents of the array.
142143

143144
Without an argument, an array of size 0 is created.
144145

145146

147+
.. function:: bytes([arg[, encoding[, errors]]])
148+
149+
Return a new "bytes" object, which is an immutable sequence of integers in
150+
the range ``0 <= x < 256``. :class:`bytes` is an immutable version of
151+
:class:`buffer` -- it has the same non-mutating methods and the same indexing
152+
and slicing behavior.
153+
154+
Accordingly, constructor arguments are interpreted as for :func:`buffer`.
155+
156+
Bytes objects can also be created with literals, see :ref:`strings`.
157+
158+
146159
.. function:: chr(i)
147160

148161
Return the string of one character whose Unicode codepoint is the integer

Doc/library/stdtypes.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,9 +1313,11 @@ Bytes and Buffer Methods
13131313

13141314
Bytes and buffer objects, being "strings of bytes", have all methods found on
13151315
strings, with the exception of :func:`encode`, :func:`format` and
1316-
:func:`isidentifier`, which do not make sense with these types. Wherever one of
1317-
these methods needs to interpret the bytes as characters (e.g. the :func:`is...`
1318-
methods), the ASCII character set is assumed.
1316+
:func:`isidentifier`, which do not make sense with these types. For converting
1317+
the objects to strings, they have a :func:`decode` method.
1318+
1319+
Wherever one of these methods needs to interpret the bytes as characters
1320+
(e.g. the :func:`is...` methods), the ASCII character set is assumed.
13191321

13201322
.. note::
13211323

Doc/library/warnings.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ following warnings category classes are currently defined:
8080
| :exc:`UnicodeWarning` | Base category for warnings related to |
8181
| | Unicode. |
8282
+----------------------------------+-----------------------------------------------+
83+
| :exc:`BytesWarning` | Base category for warnings related to |
84+
| | :class:`bytes` and :class:`buffer`. |
85+
+----------------------------------+-----------------------------------------------+
86+
8387

8488
While these are technically built-in exceptions, they are documented here,
8589
because conceptually they belong to the warnings mechanism.

Doc/whatsnew/3.0.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ changes to rarely used features.)
131131
that if a file is opened using an incorrect mode or encoding, I/O
132132
will likely fail.
133133

134-
* Bytes aren't hashable, and don't support certain operations like
135-
``b.lower()``, ``b.strip()`` or ``b.split()``.
136-
For the latter two, use ``b.strip(b" \t\r\n\f")`` or
137-
``b.split(b" \t\r\n\f")``.
138-
139134
* ``map()`` and ``filter()`` return iterators. A quick fix is e.g.
140135
``list(map(...))``, but a better fix is often to use a list
141136
comprehension (especially when the original code uses ``lambda``).
@@ -158,13 +153,11 @@ Strings and Bytes
158153
* There is only one string type; its name is ``str`` but its behavior
159154
and implementation are more like ``unicode`` in 2.x.
160155

161-
* PEP 358: There is a new type, ``bytes``, to represent binary data
156+
* PEP 3137: There is a new type, ``bytes``, to represent binary data
162157
(and encoded text, which is treated as binary data until you decide
163158
to decode it). The ``str`` and ``bytes`` types cannot be mixed; you
164159
must always explicitly convert between them, using the ``.encode()``
165-
(str -> bytes) or ``.decode()`` (bytes -> str) methods. Comparing a
166-
bytes and a str instance for equality raises a TypeError; this
167-
catches common mistakes.
160+
(str -> bytes) or ``.decode()`` (bytes -> str) methods.
168161

169162
* PEP 3112: Bytes literals. E.g. b"abc".
170163

Include/abstract.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
259259
string representation on success, NULL on failure. This is
260260
the equivalent of the Python expression: repr(o).
261261
262-
Called by the repr() built-in function and by reverse quotes.
262+
Called by the repr() built-in function.
263263
264264
*/
265265

@@ -271,20 +271,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
271271
string representation on success, NULL on failure. This is
272272
the equivalent of the Python expression: str(o).)
273273
274-
Called by the str() built-in function and by the print
275-
statement.
276-
277-
*/
278-
279-
/* Implemented elsewhere:
280-
281-
PyObject *PyObject_Unicode(PyObject *o);
282-
283-
Compute the unicode representation of object, o. Returns the
284-
unicode representation on success, NULL on failure. This is
285-
the equivalent of the Python expression: unistr(o).)
286-
287-
Called by the unistr() built-in function.
274+
Called by the str() and print() built-in functions.
288275
289276
*/
290277

Include/object.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,8 @@ PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
431431
PyAPI_FUNC(void) _Py_BreakPoint(void);
432432
PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
433433
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
434-
PyAPI_FUNC(PyObject *) PyObject_ReprStr8(PyObject *);
435-
PyAPI_FUNC(PyObject *) _PyObject_Str(PyObject *);
436434
PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
437-
PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
435+
#define PyObject_Unicode PyObject_Str /* Compatibility */
438436
PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
439437
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
440438
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
@@ -478,7 +476,7 @@ PyAPI_FUNC(long) _Py_HashDouble(double);
478476
PyAPI_FUNC(long) _Py_HashPointer(void*);
479477

480478
/* Helper for passing objects to printf and the like */
481-
#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_ReprStr8(obj))
479+
#define PyObject_REPR(obj) PyUnicode_AsString(PyObject_Repr(obj))
482480

483481
/* Flag bits for printing: */
484482
#define Py_PRINT_RAW 1 /* No string quotes etc. */

Include/opcode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern "C" {
6565

6666
#define RETURN_VALUE 83
6767
#define IMPORT_STAR 84
68-
#define MAKE_BYTES 85
68+
6969
#define YIELD_VALUE 86
7070
#define POP_BLOCK 87
7171
#define END_FINALLY 88

Include/pydebug.h

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)