Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change PyObject_AsCharBuffer() error message #54825

Closed
vstinner opened this issue Dec 3, 2010 · 8 comments
Closed

Change PyObject_AsCharBuffer() error message #54825

vstinner opened this issue Dec 3, 2010 · 8 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@vstinner
Copy link
Member

vstinner commented Dec 3, 2010

BPO 10616
Nosy @pitrou, @vstinner, @merwok, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2011-05-30.21:21:38.161>
created_at = <Date 2010-12-03.16:43:34.519>
labels = ['interpreter-core']
title = 'Change PyObject_AsCharBuffer() error message'
updated_at = <Date 2011-05-30.21:21:38.160>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2011-05-30.21:21:38.160>
actor = 'python-dev'
assignee = 'none'
closed = True
closed_date = <Date 2011-05-30.21:21:38.161>
closer = 'python-dev'
components = ['Interpreter Core']
creation = <Date 2010-12-03.16:43:34.519>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 10616
keywords = []
message_count = 8.0
messages = ['123263', '123265', '123266', '124316', '134386', '134390', '134545', '137332']
nosy_count = 7.0
nosy_names = ['pitrou', 'vstinner', 'eric.araujo', 'r.david.murray', 'sijinjoseph', 'santoso.wijaya', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue10616'
versions = ['Python 3.1', 'Python 3.2', 'Python 3.3']

@vstinner
Copy link
Member Author

vstinner commented Dec 3, 2010

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").

@vstinner vstinner added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Dec 3, 2010
@vstinner
Copy link
Member Author

vstinner commented Dec 3, 2010

See also bpo-6780.

@vstinner
Copy link
Member Author

vstinner commented Dec 3, 2010

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.

@bitdancer
Copy link
Member

Victor, I think you attached msg123266 to the wrong issue.

@pitrou
Copy link
Member

pitrou commented Apr 25, 2011

+1 to Victor's proposal ("expected bytes, bytearray or buffer compatible object").

@sijinjoseph
Copy link
Mannequin

sijinjoseph mannequin commented Apr 25, 2011

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) {
        PyErr_SetString(PyExc_TypeError,
                        "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?

@merwok
Copy link
Member

merwok commented Apr 27, 2011

+1 to Antoine’s +1.

@python-dev
Copy link
Mannequin

python-dev mannequin commented May 30, 2011

New changeset 7ed75bb4f37c by Victor Stinner in branch 'default':
Close bpo-10616: mention bytes and bytearray in PyObject_AsCharBuffer() error
http://hg.python.org/cpython/rev/7ed75bb4f37c

@python-dev python-dev mannequin closed this as completed May 30, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

4 participants