This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Change PyObject_AsCharBuffer() error message
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.1, Python 3.2, Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, pitrou, python-dev, r.david.murray, santoso.wijaya, sijinjoseph, vstinner
Priority: normal Keywords:

Created on 2010-12-03 16:43 by vstinner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg123263 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-12-03 16:43
b'abc'.partition(':') raises a confusing TypeError('expected an object with the buffer interface'): what is a buffer? what is the buffer interface?

The error comes from PyObject_AsCharBuffer() which is used by:
 - bytes methods: partition, rpartition, find, index, rfind, rindex, count, translate, replace, startswith, endswith
 - complex(): raise a better but incomplete error message on error ("complex() arg is not a string"), incomplete because number is not mentionned
 - float(): raise a better error message on error ("float() argument must be a string or a number")
 - PyArg_Parse*() with the "e" format -> posix.spawnvpe(), imp.load_compiled(), imp.load_source(), imp.load_package()

The error message should be changed to something mentioning classic Python terms. Eg. TypeError("expected bytes, bytearray or buffer compatible object").
msg123265 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-12-03 16:49
See also #6780.
msg123266 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-12-03 16:52
> complex(): raise a better but incomplete error message on error
> ("complex() arg is not a string"), incomplete because number is not
> mentionned

Fixed by r86977.
msg124316 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-12-18 20:38
Victor, I think you attached msg123266 to the wrong issue.
msg134386 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-04-25 14:50
+1 to Victor's proposal ("expected bytes, bytearray or buffer compatible object").
msg134390 - (view) Author: Sijin Joseph (sijinjoseph) Date: 2011-04-25 15:17
Looking at object.h the buffer interface is defined as 

/* buffer interface */
typedef struct bufferinfo {
    void *buf;
    PyObject *obj;        /* owned reference */
    Py_ssize_t len;
    Py_ssize_t itemsize;  /* This is Py_ssize_t so it can be
                             pointed to by strides in simple case.*/
    int readonly;
    int ndim;
    char *format;
    Py_ssize_t *shape;
    Py_ssize_t *strides;
    Py_ssize_t *suboffsets;
    Py_ssize_t smalltable[2];  /* static store for shape and strides of
                                  mono-dimensional buffers. */
    void *internal;
} Py_buffer;

typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
typedef void (*releasebufferproc)(PyObject *, Py_buffer *);

typedef struct {
     getbufferproc bf_getbuffer;
     releasebufferproc bf_releasebuffer;
} PyBufferProcs;

The code that's throwing that error looks like

if (pb == NULL || pb->bf_getbuffer == NULL) {
                        "expected an object with the buffer interface");

I would argue that the error message gives appropriate information because it is expecting the object to have a bf_getbuffer member.

Maybe the friendly error message is best handled at a higher level?
msg134545 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-04-27 10:11
+1 to Antoine’s +1.
msg137332 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-05-30 21:21
New changeset 7ed75bb4f37c by Victor Stinner in branch 'default':
Close #10616: mention bytes and bytearray in PyObject_AsCharBuffer() error
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54825
2011-05-30 21:21:38python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg137332

resolution: fixed
stage: resolved
2011-04-27 10:11:15eric.araujosetmessages: + msg134545
versions: + Python 3.3
2011-04-25 15:17:07sijinjosephsetnosy: + sijinjoseph
messages: + msg134390
2011-04-25 14:50:41pitrousetnosy: + pitrou
messages: + msg134386
2011-04-25 02:57:43santoso.wijayasetnosy: + santoso.wijaya
2010-12-18 20:38:35r.david.murraysetnosy: vstinner, eric.araujo, r.david.murray
messages: - msg124315
2010-12-18 20:38:25r.david.murraysetnosy: vstinner, eric.araujo, r.david.murray
messages: + msg124316
2010-12-18 20:36:53r.david.murraysetnosy: + r.david.murray
messages: + msg124315
2010-12-04 15:31:14eric.araujosetnosy: + eric.araujo
2010-12-03 16:52:07vstinnersetmessages: + msg123266
2010-12-03 16:49:04vstinnersetmessages: + msg123265
2010-12-03 16:43:34vstinnercreate