classification
Title: Manage memory lifetime for all type-related objects.
Type: Stage: patch review
Components: Extension Modules Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: llllllllll
Priority: normal Keywords: patch

Created on 2019-06-13 21:31 by llllllllll, last changed 2019-06-13 21:33 by llllllllll.

Pull Requests
URL Status Linked Edit
PR 14066 open llllllllll, 2019-06-13 21:33
Messages (1)
msg345539 - (view) Author: Joe Jevnik (llllllllll) * Date: 2019-06-13 21:31
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.
History
Date User Action Args
2019-06-13 21:33:06llllllllllsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13925
2019-06-13 21:31:34llllllllllcreate