[New-bugs-announce] [issue39620] PyObject_GetAttrString and tp_getattr do not agree

Andy Lester report at bugs.python.org
Wed Feb 12 22:23:38 EST 2020

New submission from Andy Lester <andy at petdance.com>:

PyObject_GetAttrString(PyObject *v, const char *name)

typedef PyObject *(*getattrfunc)(PyObject *, char *)

The outer PyObject_GetAttrString takes a const char *name, but then casts away the const when calling the underlying tp_getattr.  This means that an underlying function would be free to modify or free() the char* passed in to it, which might be, for example, a string literal, which would be a Bad Thing.

The setattr function pair has the same problem.

The API doc at https://docs.python.org/3/c-api/typeobj.html says that the tp_getattr and tp_setattr slots are deprecated.  If they're not going away soon, I would think this should be addressed.

Fixing this in the cPython code by making tp_getattr and tp_setattr take const char * pointers would be simple.  I don't have any idea how much outside code it would affect.

components: C API
messages: 361929
nosy: petdance
priority: normal
severity: normal
status: open
title: PyObject_GetAttrString and tp_getattr do not agree
type: enhancement

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list