New submission from Joe Jevnik <j...@quantopian.com>:
When using PyType_FromSpec, the memory for PyType_Spec.name, Py_tp_methods, and Py_tp_members needs to somehow outlive the resulting type. This makes it hard to use this interface to generate types without just leaking the memory for these arrays, which is bad if you are programatically creating short-lived types. I posted about this on capi-sig, and the response seemed to be that it would be to replace the things that currently hold pointers into the array with copies of the data. Remove internal usages of PyMethodDef and PyGetSetDef. For PyMethodDef, change PyCFunctionObject to replace the PyMethodDef* member with a PyCFunctionBase member. The PyCFunctionBase is a new struct to hold the managed values of a PyMethodDef. This type is shared between PyCFunction and the various callable descriptor objects. A PyCFunctionBase is like a PyMethodDef but replaces the char* members with PyObject* members. For PyGetSetDef, inline the members on the resulting PyGetSetDescrObject, replacing all char* members with PyObject* members. The memory for the closure is *not* managed, adding support for that would likely require an API change and can be done in a future change. For the tp_name field, instead of setting it directly to the value of PyType_Spec.name, set it to the result of PyUnicode_AsUTF8(ht_name), where ht_name is the PyUnicode object created from the original spec name. This is the same trick used to properly manage this pointer for heap types when the __name__ is reassigned. ---------- components: Extension Modules messages: 345539 nosy: llllllllll priority: normal severity: normal status: open title: Manage memory lifetime for all type-related objects. versions: Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue37270> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com