Title: PyObject_GetAttrString and tp_getattr do not agree
Type: enhancement Stage:
Components: C API Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, loewis, petdance, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-02-13 03:23 by petdance, last changed 2020-02-13 05:34 by serhiy.storchaka.

Messages (4)
msg361929 - (view) Author: Andy Lester (petdance) * Date: 2020-02-13 03:23
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 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.
msg361930 - (view) Author: Ammar Askar (ammar2) * (Python triager) Date: 2020-02-13 03:34
Note that there was an earlier attempt to make it const

but this was reverted as part of
msg361931 - (view) Author: Andy Lester (petdance) * Date: 2020-02-13 03:47
Do you know why it was reverted?  (Granted, it was 15 years ago...)

It looks like the original changeset is trying to address at least two different problems with non-const string literals.  My ticket here is focusing only on getattrfunc and setattrfunc.  The handling of all the kwlist is a separate issue, that I'm about to write up as a different ticket.
msg361936 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-02-13 05:34
It was reverted because it is backward incompatible change. It breaks a code which assigns to the tp_getattr field without explicit type cast to getattrfunc.
Date User Action Args
2020-02-13 05:34:35serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg361936
2020-02-13 03:47:18petdancesetmessages: + msg361931
2020-02-13 03:34:12ammar2setnosy: + loewis, ammar2
messages: + msg361930
2020-02-13 03:23:38petdancecreate