]> granicus.if.org Git - python/commitdiff
#16518: Bring error messages in harmony with docs ("bytes-like object")
authorR David Murray <rdmurray@bitdance.com>
Sun, 5 Oct 2014 15:47:01 +0000 (11:47 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sun, 5 Oct 2014 15:47:01 +0000 (11:47 -0400)
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.)

Doc/whatsnew/3.5.rst
Lib/test/test_socket.py
Misc/NEWS
Modules/_operator.c
Objects/abstract.c
Objects/bytearrayobject.c
Objects/bytes_methods.c
Objects/bytesobject.c
Objects/longobject.c
Objects/memoryobject.c
Python/getargs.c

index 3e44aa242df2bda9f45f31ae7436a6a1546a6197..4530a13d765db8a2bdae917aa5922e5510d7694a 100644 (file)
@@ -393,6 +393,12 @@ Changes in the Python API
   The *convert_charrefs* argument of :class:`~html.parser.HTMLParser` is
   now ``True`` by default (contributed by Berker Peksag in :issue:`21047`).
 
+* Although it is not formally part of the API, it is worth noting for porting
+  purposes (ie: fixing tests) that error messages that were previously of the
+  form "'sometype' does not support the buffer protocol" are now of the form "a
+  bytes-like object is required, not 'sometype'" (contributed by Ezio Melotti
+  in :issue:`16518`).
+
 Changes in the C API
 --------------------
 
index 98ee615065e6857e4b0365305f35e6ca1beb64a2..3f2057bc9e55e01a4e075a5afe4b31ef85f59840 100644 (file)
@@ -711,11 +711,11 @@ class GeneralModuleTests(unittest.TestCase):
         with self.assertRaises(TypeError) as cm:
             s.sendto('\u2620', sockname)
         self.assertEqual(str(cm.exception),
-                         "'str' does not support the buffer interface")
+                         "a bytes-like object is required, not 'str'")
         with self.assertRaises(TypeError) as cm:
             s.sendto(5j, sockname)
         self.assertEqual(str(cm.exception),
-                         "'complex' does not support the buffer interface")
+                         "a bytes-like object is required, not 'complex'")
         with self.assertRaises(TypeError) as cm:
             s.sendto(b'foo', None)
         self.assertIn('not NoneType',str(cm.exception))
@@ -723,11 +723,11 @@ class GeneralModuleTests(unittest.TestCase):
         with self.assertRaises(TypeError) as cm:
             s.sendto('\u2620', 0, sockname)
         self.assertEqual(str(cm.exception),
-                         "'str' does not support the buffer interface")
+                         "a bytes-like object is required, not 'str'")
         with self.assertRaises(TypeError) as cm:
             s.sendto(5j, 0, sockname)
         self.assertEqual(str(cm.exception),
-                         "'complex' does not support the buffer interface")
+                         "a bytes-like object is required, not 'complex'")
         with self.assertRaises(TypeError) as cm:
             s.sendto(b'foo', 0, None)
         self.assertIn('not NoneType', str(cm.exception))
index c23186df96f1de051a584ca004a8d8dd4831f24f..3aa92e89a411b63f9c17c1e1e916d1e2cbfc4e4d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ Release date: TBA
 Core and Builtins
 -----------------
 
+- Issue #16518: Use 'bytes-like object required' in error messages that
+  previously used the far more cryptic "'x' does not support the buffer
+  protocol.
+
 - Issue #22470: Fixed integer overflow issues in "backslashreplace",
   "xmlcharrefreplace", and "surrogatepass" error handlers.
 
index 9c5c0d206ad4a0d556c2ab58ad22fcbcc5979abb..8f524a6449b8f8e1cea6b66b440f751b242e8825 100644 (file)
@@ -241,7 +241,7 @@ PyDoc_STRVAR(compare_digest__doc__,
 "Return 'a == b'.  This function uses an approach designed to prevent\n"
 "timing analysis, making it appropriate for cryptography.\n"
 "a and b must both be of the same type: either str (ASCII only),\n"
-"or any type that supports the buffer protocol (e.g. bytes).\n"
+"or any bytes-like object.\n"
 "\n"
 "Note: If a and b are of different lengths, or if an error occurs,\n"
 "a timing attack could theoretically reveal information about the\n"
index 177c34a4a4e8f56106aa8a9b6f362d6d5c1a1a58..323c985b3ecb4f94c9cb634b21de6d67ab821684 100644 (file)
@@ -260,8 +260,7 @@ PyObject_AsCharBuffer(PyObject *obj,
     pb = obj->ob_type->tp_as_buffer;
     if (pb == NULL || pb->bf_getbuffer == NULL) {
         PyErr_SetString(PyExc_TypeError,
-                        "expected bytes, bytearray "
-                        "or buffer compatible object");
+                        "expected a bytes-like object");
         return -1;
     }
     if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
@@ -306,7 +305,7 @@ int PyObject_AsReadBuffer(PyObject *obj,
     if (pb == NULL ||
         pb->bf_getbuffer == NULL) {
         PyErr_SetString(PyExc_TypeError,
-                        "expected an object with a buffer interface");
+                        "expected a bytes-like object");
         return -1;
     }
 
@@ -336,7 +335,7 @@ int PyObject_AsWriteBuffer(PyObject *obj,
         pb->bf_getbuffer == NULL ||
         ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
         PyErr_SetString(PyExc_TypeError,
-                        "expected an object with a writable buffer interface");
+                        "expected a writable bytes-like object");
         return -1;
     }
 
@@ -355,7 +354,7 @@ PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
 {
     if (!PyObject_CheckBuffer(obj)) {
         PyErr_Format(PyExc_TypeError,
-                     "'%.100s' does not support the buffer interface",
+                     "a bytes-like object is required, not '%.100s'",
                      Py_TYPE(obj)->tp_name);
         return -1;
     }
@@ -530,8 +529,8 @@ int PyObject_CopyData(PyObject *dest, PyObject *src)
     if (!PyObject_CheckBuffer(dest) ||
         !PyObject_CheckBuffer(src)) {
         PyErr_SetString(PyExc_TypeError,
-                        "both destination and source must have the "\
-                        "buffer interface");
+                        "both destination and source must be "\
+                        "bytes-like objects");
         return -1;
     }
 
index f6f370d42c3529e94b586779c39eb61911d0ef41..84447bc7eea9bf5b5be1b85d926988f5ab0fda4c 100644 (file)
@@ -87,7 +87,7 @@ _getbuffer(PyObject *obj, Py_buffer *view)
     if (buffer == NULL || buffer->bf_getbuffer == NULL)
     {
         PyErr_Format(PyExc_TypeError,
-                     "Type %.100s doesn't support the buffer API",
+                     "a bytes-like object is required, not '%.100s'",
                      Py_TYPE(obj)->tp_name);
         return -1;
     }
index 5314ab4c181e68b5aa183cb9e177eea0651ec541..1cf20c97d3fe8ad1cd8519b512a520965b3591bf 100644 (file)
@@ -371,7 +371,7 @@ _getbuffer(PyObject *obj, Py_buffer *view)
     if (buffer == NULL || buffer->bf_getbuffer == NULL)
     {
         PyErr_Format(PyExc_TypeError,
-                     "Type %.100s doesn't support the buffer API",
+                     "a bytes-like object is required, not '%.100s'",
                      Py_TYPE(obj)->tp_name);
         return -1;
     }
index 87c0a25608441be277d977fa28d0298962de2d86..ff99f936aa72bb9c0e347ab0f903db3a08b69ebb 100644 (file)
@@ -29,7 +29,7 @@ _getbuffer(PyObject *obj, Py_buffer *view)
     if (bufferprocs == NULL || bufferprocs->bf_getbuffer == NULL)
     {
         PyErr_Format(PyExc_TypeError,
-                     "Type %.100s doesn't support the buffer API",
+                     "a bytes-like object is required, not '%.100s'",
                      Py_TYPE(obj)->tp_name);
         return -1;
     }
index bb2eb17a09006b63e36770a490c0fb5f3d9ed8e8..27bee50b8ce74646f51e91c5753b5b666bf3e660 100644 (file)
@@ -4873,9 +4873,7 @@ PyDoc_STRVAR(long_from_bytes_doc,
 \n\
 Return the integer represented by the given array of bytes.\n\
 \n\
-The bytes argument must either support the buffer protocol or be an\n\
-iterable object producing bytes.  Bytes and bytearray are examples of\n\
-built-in objects that support the buffer protocol.\n\
+The bytes argument must be a bytes-like object (e.g. bytes or bytearray).\n\
 \n\
 The byteorder argument determines the byte order used to represent the\n\
 integer.  If byteorder is 'big', the most significant byte is at the\n\
index 5148ce6bef6e56ce3c4dbfa2db05588fd46429de..935da04cd79d0912bdc367b7321aa8b191b182b7 100644 (file)
@@ -792,7 +792,7 @@ PyMemoryView_FromObject(PyObject *v)
     }
 
     PyErr_Format(PyExc_TypeError,
-        "memoryview: %.200s object does not have the buffer interface",
+        "memoryview: a bytes-like object is required, not '%.200s'",
         Py_TYPE(v)->tp_name);
     return NULL;
 }
index c749bb643cb195eb975714b4aa9454ad6cbd464b..f7297da5dbc2704eb4cc7195b39fceedd83428ea 100644 (file)
@@ -1244,7 +1244,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
            supports it directly. */
         if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
             PyErr_Clear();
-            return converterr("read-write buffer", arg, msgbuf, bufsize);
+            return converterr("read-write bytes-like object",
+                              arg, msgbuf, bufsize);
         }
         if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
             PyBuffer_Release((Py_buffer*)p);
@@ -1282,7 +1283,7 @@ convertbuffer(PyObject *arg, void **p, char **errmsg)
     *errmsg = NULL;
     *p = NULL;
     if (pb != NULL && pb->bf_releasebuffer != NULL) {
-        *errmsg = "read-only pinned buffer";
+        *errmsg = "read-only bytes-like object";
         return -1;
     }
 
@@ -1298,7 +1299,7 @@ static int
 getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
 {
     if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
-        *errmsg = "bytes or buffer";
+        *errmsg = "bytes-like object";
         return -1;
     }
     if (!PyBuffer_IsContiguous(view, 'C')) {