bpo-30134: fix BytesWarning doc, docstring and message (GH-12739) · python/cpython@4e8e8aa

6 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -526,7 +526,7 @@ module for more information.

526526
527527

.. exception:: BytesWarning

528528
529-

Base class for warnings related to :class:`str` and :class:`bytearray`.

529+

Base class for warnings related to bytes and bytearray.

530530
531531

.. versionadded:: 2.6

532532
Original file line numberDiff line numberDiff line change

@@ -92,7 +92,7 @@ following warnings category classes are currently defined:

9292

| | Unicode. |

9393

+----------------------------------+-----------------------------------------------+

9494

| :exc:`BytesWarning` | Base category for warnings related to |

95-

| | str and bytearray. |

95+

| | bytes and bytearray. |

9696

+----------------------------------+-----------------------------------------------+

9797
9898

While these are technically built-in exceptions, they are documented here,

Original file line numberDiff line numberDiff line change

@@ -104,7 +104,7 @@ PyAPI_FUNC(int) _PyCode_CheckLineNumber(PyCodeObject* co,

104104

*

105105

* Return (type(obj), obj, ...): a tuple with variable size (at least 2 items)

106106

* depending on the type and the value. The type is the first item to not

107-

* compare bytes and str which can raise a BytesWarning exception. */

107+

* compare bytes and unicode which can raise a BytesWarning exception. */

108108

PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);

109109
110110

PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,

Original file line numberDiff line numberDiff line change

@@ -1027,14 +1027,6 @@ bytearray_repr(PyByteArrayObject *self)

10271027

static PyObject *

10281028

bytearray_str(PyObject *op)

10291029

{

1030-

#if 0

1031-

if (Py_BytesWarningFlag) {

1032-

if (PyErr_WarnEx(PyExc_BytesWarning,

1033-

"str() on a bytearray instance", 1))

1034-

return NULL;

1035-

}

1036-

return bytearray_repr((PyByteArrayObject*)op);

1037-

#endif

10381030

return PyBytes_FromStringAndSize(((PyByteArrayObject*)op)->ob_bytes, Py_SIZE(op));

10391031

}

10401032

@@ -1059,7 +1051,7 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)

10591051

if (rc) {

10601052

if (Py_BytesWarningFlag && op == Py_EQ) {

10611053

if (PyErr_WarnEx(PyExc_BytesWarning,

1062-

"Comparison between bytearray and string", 1))

1054+

"Comparison between bytearray and unicode", 1))

10631055

return NULL;

10641056

}

10651057
Original file line numberDiff line numberDiff line change

@@ -2014,8 +2014,8 @@ SimpleExtendsException(PyExc_Warning, UnicodeWarning,

20142014

* BytesWarning extends Warning

20152015

*/

20162016

SimpleExtendsException(PyExc_Warning, BytesWarning,

2017-

"Base class for warnings about bytes and buffer related problems, mostly\n"

2018-

"related to conversion from str or comparing to str.");

2017+

"Base class for warnings about bytes and bytearray related problems, \n"

2018+

"mostly related to comparing to str.");

20192019
20202020

/* Pre-computed MemoryError instance. Best to create this as early as

20212021

* possible and not wait until a MemoryError is actually raised!

Original file line numberDiff line numberDiff line change

@@ -70,7 +70,7 @@ int Py_VerboseFlag; /* Needed by import.c */

7070

int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */

7171

int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */

7272

int Py_NoSiteFlag; /* Suppress 'import site' */

73-

int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */

73+

int Py_BytesWarningFlag; /* Warn on comparison between bytearray and unicode */

7474

int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */

7575

int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */

7676

int Py_FrozenFlag; /* Needed by getpath.c */