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.

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

Created on 2020-02-13 03:23 by petdance, last changed 2022-04-11 14:59 by admin.

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 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.
msg361930 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-02-13 03:34
Note that there was an earlier attempt to make it const
https://github.com/python/cpython/commit/af68c874a6803b4e90b616077a602c0593719a1d

but this was reverted as part of https://github.com/python/cpython/commit/15e62742fad688b026ba80bf17d1345c4cbd423b
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.
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83801
2021-06-22 14:42:14petr.viktorinsetnosy: + petr.viktorin
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