Skip to content

Commit ba44a2e

Browse files
committed
gh-146063: Add PyObject_CallFinalizer() to the limited C API
Add PyObject_CallFinalizer() and PyObject_CallFinalizerFromDealloc() to the limited C API. Add a test on PyObject_CallFinalizer().
1 parent 52c0186 commit ba44a2e

File tree

10 files changed

+49
-10
lines changed

10 files changed

+49
-10
lines changed

Doc/data/stable_abi.dat

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/whatsnew/3.15.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,10 @@ New features
16621662

16631663
(Contributed by Victor Stinner in :gh:`141510`.)
16641664

1665+
* Add :c:func:`PyObject_CallFinalizer` and
1666+
:c:func:`PyObject_CallFinalizerFromDealloc` functions to the limited C API.
1667+
(Contributed by Victor Stinner in :gh:`146063`.)
1668+
16651669
* Add :c:func:`PySys_GetAttr`, :c:func:`PySys_GetAttrString`,
16661670
:c:func:`PySys_GetOptionalAttr`, and :c:func:`PySys_GetOptionalAttrString`
16671671
functions as replacements for :c:func:`PySys_GetObject`.

Include/cpython/object.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ PyAPI_FUNC(void) PyObject_Dump(PyObject *);
303303
Py_DEPRECATED(3.15) PyAPI_FUNC(PyObject*) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
304304

305305
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
306-
PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
307-
PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
308306

309307
PyAPI_FUNC(void) PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *);
310308

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,9 @@ PyAPI_FUNC(int) PyType_Freeze(PyTypeObject *type);
856856
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)
857857
PyAPI_FUNC(PyObject *) PyType_GetModuleByToken(PyTypeObject *type,
858858
const void *token);
859+
860+
PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
861+
PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
859862
#endif
860863

861864
#ifdef __cplusplus

Lib/test/test_capi/test_object.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,21 @@ def test_pyobject_dump(self):
305305
output = self.pyobject_dump(NULL)
306306
self.assertRegex(output, r'<object at .* is freed>')
307307

308+
def test_pyobject_callfinalizer(self):
309+
# Test PyObject_CallFinalizer()
310+
pyobject_callfinalizer = _testlimitedcapi.pyobject_callfinalizer
311+
312+
class Finalizer:
313+
def __init__(self):
314+
self.finalized = False
315+
def __del__(self):
316+
self.finalized = True
317+
318+
obj = Finalizer()
319+
self.assertFalse(obj.finalized)
320+
pyobject_callfinalizer(obj)
321+
self.assertTrue(obj.finalized)
322+
308323

309324
if __name__ == "__main__":
310325
unittest.main()

Lib/test/test_stable_abi_ctypes.py

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyObject_CallFinalizer` and
2+
:c:func:`PyObject_CallFinalizerFromDealloc` functions to the limited C API.
3+
Patch by Victor Stinner.

Misc/stable_abi.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,6 @@
26682668
added = '3.15'
26692669

26702670
# PEP 757 import/export API.
2671-
26722671
[function.PyLong_GetNativeLayout]
26732672
added = '3.15'
26742673
[function.PyLong_Export]
@@ -2692,3 +2691,8 @@
26922691
# Note: The `_reserved` member of this struct is for interal use only.
26932692
# (The definition of 'full-abi' was clarified when this entry was added.)
26942693
struct_abi_kind = 'full-abi'
2694+
2695+
[function.PyObject_CallFinalizer]
2696+
added = '3.15'
2697+
[function.PyObject_CallFinalizerFromDealloc]
2698+
added = '3.15'

Modules/_testlimitedcapi/object.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Need limited C API version 3.13 for Py_GetConstant()
1+
// Need limited C API version 3.15 for PyObject_CallFinalizer()
22
#include "pyconfig.h" // Py_GIL_DISABLED
33
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
4-
# define Py_LIMITED_API 0x030d0000
4+
# define Py_LIMITED_API 0x030f0000
55
#endif
66

77
#include "parts.h"
@@ -62,19 +62,25 @@ test_constants(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
6262
Py_RETURN_NONE;
6363
}
6464

65+
66+
static PyObject *
67+
pyobject_callfinalizer(PyObject *Py_UNUSED(module), PyObject *obj)
68+
{
69+
PyObject_CallFinalizer(obj);
70+
Py_RETURN_NONE;
71+
}
72+
73+
6574
static PyMethodDef test_methods[] = {
6675
{"get_constant", get_constant, METH_VARARGS},
6776
{"get_constant_borrowed", get_constant_borrowed, METH_VARARGS},
6877
{"test_constants", test_constants, METH_NOARGS},
78+
{"pyobject_callfinalizer", pyobject_callfinalizer, METH_O},
6979
{NULL},
7080
};
7181

7282
int
7383
_PyTestLimitedCAPI_Init_Object(PyObject *m)
7484
{
75-
if (PyModule_AddFunctions(m, test_methods) < 0) {
76-
return -1;
77-
}
78-
79-
return 0;
85+
return PyModule_AddFunctions(m, test_methods);
8086
}

PC/python3dll.c

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)