Skip to content

Commit 861470c

Browse files
committed
#16518: Bring error messages in harmony with docs ("bytes-like object")
Some time ago we changed the docs to consistently use the term 'bytes-like object' in all the contexts where bytes, bytearray, memoryview, etc are used. This patch (by Ezio Melotti) completes that work by changing the error messages that previously reported that certain types did "not support the buffer interface" to instead say that a bytes-like object is required. (The glossary entry for bytes-like object references the discussion of the buffer protocol in the docs.)
1 parent d577cea commit 861470c

File tree

11 files changed

+30
-22
lines changed

11 files changed

+30
-22
lines changed

Doc/whatsnew/3.5.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ Changes in the Python API
393393
The *convert_charrefs* argument of :class:`~html.parser.HTMLParser` is
394394
now ``True`` by default (contributed by Berker Peksag in :issue:`21047`).
395395

396+
* Although it is not formally part of the API, it is worth noting for porting
397+
purposes (ie: fixing tests) that error messages that were previously of the
398+
form "'sometype' does not support the buffer protocol" are now of the form "a
399+
bytes-like object is required, not 'sometype'" (contributed by Ezio Melotti
400+
in :issue:`16518`).
401+
396402
Changes in the C API
397403
--------------------
398404

Lib/test/test_socket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,23 +711,23 @@ def testSendtoErrors(self):
711711
with self.assertRaises(TypeError) as cm:
712712
s.sendto('\u2620', sockname)
713713
self.assertEqual(str(cm.exception),
714-
"'str' does not support the buffer interface")
714+
"a bytes-like object is required, not 'str'")
715715
with self.assertRaises(TypeError) as cm:
716716
s.sendto(5j, sockname)
717717
self.assertEqual(str(cm.exception),
718-
"'complex' does not support the buffer interface")
718+
"a bytes-like object is required, not 'complex'")
719719
with self.assertRaises(TypeError) as cm:
720720
s.sendto(b'foo', None)
721721
self.assertIn('not NoneType',str(cm.exception))
722722
# 3 args
723723
with self.assertRaises(TypeError) as cm:
724724
s.sendto('\u2620', 0, sockname)
725725
self.assertEqual(str(cm.exception),
726-
"'str' does not support the buffer interface")
726+
"a bytes-like object is required, not 'str'")
727727
with self.assertRaises(TypeError) as cm:
728728
s.sendto(5j, 0, sockname)
729729
self.assertEqual(str(cm.exception),
730-
"'complex' does not support the buffer interface")
730+
"a bytes-like object is required, not 'complex'")
731731
with self.assertRaises(TypeError) as cm:
732732
s.sendto(b'foo', 0, None)
733733
self.assertIn('not NoneType', str(cm.exception))

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #16518: Use 'bytes-like object required' in error messages that
14+
previously used the far more cryptic "'x' does not support the buffer
15+
protocol.
16+
1317
- Issue #22470: Fixed integer overflow issues in "backslashreplace",
1418
"xmlcharrefreplace", and "surrogatepass" error handlers.
1519

Modules/_operator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ PyDoc_STRVAR(compare_digest__doc__,
241241
"Return 'a == b'. This function uses an approach designed to prevent\n"
242242
"timing analysis, making it appropriate for cryptography.\n"
243243
"a and b must both be of the same type: either str (ASCII only),\n"
244-
"or any type that supports the buffer protocol (e.g. bytes).\n"
244+
"or any bytes-like object.\n"
245245
"\n"
246246
"Note: If a and b are of different lengths, or if an error occurs,\n"
247247
"a timing attack could theoretically reveal information about the\n"

Objects/abstract.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ PyObject_AsCharBuffer(PyObject *obj,
260260
pb = obj->ob_type->tp_as_buffer;
261261
if (pb == NULL || pb->bf_getbuffer == NULL) {
262262
PyErr_SetString(PyExc_TypeError,
263-
"expected bytes, bytearray "
264-
"or buffer compatible object");
263+
"expected a bytes-like object");
265264
return -1;
266265
}
267266
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
@@ -306,7 +305,7 @@ int PyObject_AsReadBuffer(PyObject *obj,
306305
if (pb == NULL ||
307306
pb->bf_getbuffer == NULL) {
308307
PyErr_SetString(PyExc_TypeError,
309-
"expected an object with a buffer interface");
308+
"expected a bytes-like object");
310309
return -1;
311310
}
312311

@@ -336,7 +335,7 @@ int PyObject_AsWriteBuffer(PyObject *obj,
336335
pb->bf_getbuffer == NULL ||
337336
((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
338337
PyErr_SetString(PyExc_TypeError,
339-
"expected an object with a writable buffer interface");
338+
"expected a writable bytes-like object");
340339
return -1;
341340
}
342341

@@ -355,7 +354,7 @@ PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
355354
{
356355
if (!PyObject_CheckBuffer(obj)) {
357356
PyErr_Format(PyExc_TypeError,
358-
"'%.100s' does not support the buffer interface",
357+
"a bytes-like object is required, not '%.100s'",
359358
Py_TYPE(obj)->tp_name);
360359
return -1;
361360
}
@@ -530,8 +529,8 @@ int PyObject_CopyData(PyObject *dest, PyObject *src)
530529
if (!PyObject_CheckBuffer(dest) ||
531530
!PyObject_CheckBuffer(src)) {
532531
PyErr_SetString(PyExc_TypeError,
533-
"both destination and source must have the "\
534-
"buffer interface");
532+
"both destination and source must be "\
533+
"bytes-like objects");
535534
return -1;
536535
}
537536

Objects/bytearrayobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ _getbuffer(PyObject *obj, Py_buffer *view)
8787
if (buffer == NULL || buffer->bf_getbuffer == NULL)
8888
{
8989
PyErr_Format(PyExc_TypeError,
90-
"Type %.100s doesn't support the buffer API",
90+
"a bytes-like object is required, not '%.100s'",
9191
Py_TYPE(obj)->tp_name);
9292
return -1;
9393
}

Objects/bytes_methods.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ _getbuffer(PyObject *obj, Py_buffer *view)
371371
if (buffer == NULL || buffer->bf_getbuffer == NULL)
372372
{
373373
PyErr_Format(PyExc_TypeError,
374-
"Type %.100s doesn't support the buffer API",
374+
"a bytes-like object is required, not '%.100s'",
375375
Py_TYPE(obj)->tp_name);
376376
return -1;
377377
}

Objects/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ _getbuffer(PyObject *obj, Py_buffer *view)
2929
if (bufferprocs == NULL || bufferprocs->bf_getbuffer == NULL)
3030
{
3131
PyErr_Format(PyExc_TypeError,
32-
"Type %.100s doesn't support the buffer API",
32+
"a bytes-like object is required, not '%.100s'",
3333
Py_TYPE(obj)->tp_name);
3434
return -1;
3535
}

Objects/longobject.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4873,9 +4873,7 @@ PyDoc_STRVAR(long_from_bytes_doc,
48734873
\n\
48744874
Return the integer represented by the given array of bytes.\n\
48754875
\n\
4876-
The bytes argument must either support the buffer protocol or be an\n\
4877-
iterable object producing bytes. Bytes and bytearray are examples of\n\
4878-
built-in objects that support the buffer protocol.\n\
4876+
The bytes argument must be a bytes-like object (e.g. bytes or bytearray).\n\
48794877
\n\
48804878
The byteorder argument determines the byte order used to represent the\n\
48814879
integer. If byteorder is 'big', the most significant byte is at the\n\

Objects/memoryobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ PyMemoryView_FromObject(PyObject *v)
792792
}
793793

794794
PyErr_Format(PyExc_TypeError,
795-
"memoryview: %.200s object does not have the buffer interface",
795+
"memoryview: a bytes-like object is required, not '%.200s'",
796796
Py_TYPE(v)->tp_name);
797797
return NULL;
798798
}

0 commit comments

Comments
 (0)