From 6946148462a7af548c3a630cce60c7e2abf11b5d Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Jul 2022 15:11:05 +0100 Subject: [PATCH 1/4] Expose functions to clear and visit managed dictionaries. --- Objects/dictobject.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index ebbd22ee7c145e..699505e62b80c6 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5583,6 +5583,35 @@ _PyObject_FreeInstanceAttributes(PyObject *self) free_values(*values_ptr); } +int +_PyObject_VisitManagedDict(PyObject *self, visitproc visit, void *arg) +{ + PyTypeObject *tp = Py_TYPE(self); + if((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) { + return 0; + } + assert(tp->tp_dictoffset); + int err = _PyObject_VisitInstanceAttributes(self, visit, arg); + if (err) { + return err; + } + Py_VISIT(*_PyObject_DictPointer(self)); + return 0; +} + + +void +_PyObject_ClearManagedDict(PyObject *self) +{ + PyTypeObject *tp = Py_TYPE(self); + if((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) { + return; + } + _PyObject_FreeInstanceAttributes(self); + *_PyObject_ValuesPointer(self) = NULL; + Py_CLEAR(*_PyObject_DictPointer(self)); +} + PyObject * PyObject_GenericGetDict(PyObject *obj, void *context) { From 7879a6a7555355915e3c4894c7b1e77fcbab4dbc Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Jul 2022 15:48:27 +0100 Subject: [PATCH 2/4] Add a test --- Include/cpython/dictobject.h | 3 +++ Lib/test/test_capi.py | 14 ++++++++++++++ Modules/_testcapimodule.c | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/Include/cpython/dictobject.h b/Include/cpython/dictobject.h index 565ad791a6cb28..c2e4a46e76195b 100644 --- a/Include/cpython/dictobject.h +++ b/Include/cpython/dictobject.h @@ -83,3 +83,6 @@ typedef struct { PyAPI_FUNC(PyObject *) _PyDictView_New(PyObject *, PyTypeObject *); PyAPI_FUNC(PyObject *) _PyDictView_Intersect(PyObject* self, PyObject *other); + +PyAPI_FUNC(int) _PyObject_VisitManagedDict(PyObject *self, visitproc visit, void *arg); +PyAPI_FUNC(void) _PyObject_ClearManagedDict(PyObject *self); diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index 7e571ab4f9e593..a88a17d3c55788 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -722,6 +722,20 @@ def test_export_symbols(self): with self.subTest(name=name): self.assertTrue(hasattr(ctypes.pythonapi, name)) + def test_clear_managed_dict(self): + + class C: + def __init__(self): + self.a = 1 + + c = C() + _testcapi.clear_managed_dict(c) + self.assertEqual(c.__dict__, {}) + c = C() + self.assertEqual(c.__dict__, {'a':1}) + _testcapi.clear_managed_dict(c) + self.assertEqual(c.__dict__, {}) + class TestPendingCalls(unittest.TestCase): diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 02635c427200d6..b9f75d154ee5c0 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -6009,6 +6009,13 @@ settrace_to_record(PyObject *self, PyObject *list) Py_RETURN_NONE; } +static PyObject * +clear_managed_dict(PyObject *self, PyObject *obj) +{ + _PyObject_ClearManagedDict(obj); + Py_RETURN_NONE; +} + static PyObject * test_macros(PyObject *self, PyObject *Py_UNUSED(args)) @@ -6347,6 +6354,7 @@ static PyMethodDef TestMethods[] = { {"test_code_api", test_code_api, METH_NOARGS, NULL}, {"settrace_to_record", settrace_to_record, METH_O, NULL}, {"test_macros", test_macros, METH_NOARGS, NULL}, + {"clear_managed_dict", clear_managed_dict, METH_O, NULL}, {NULL, NULL} /* sentinel */ }; From d322c68fed80eb98dcfd36f328f5cd24397c3870 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Jul 2022 15:54:34 +0100 Subject: [PATCH 3/4] add news --- .../next/C API/2022-07-25-15-54-27.gh-issue-92678.ziZpxz.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2022-07-25-15-54-27.gh-issue-92678.ziZpxz.rst diff --git a/Misc/NEWS.d/next/C API/2022-07-25-15-54-27.gh-issue-92678.ziZpxz.rst b/Misc/NEWS.d/next/C API/2022-07-25-15-54-27.gh-issue-92678.ziZpxz.rst new file mode 100644 index 00000000000000..52473c9dfab946 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-07-25-15-54-27.gh-issue-92678.ziZpxz.rst @@ -0,0 +1,3 @@ +Adds unstable C-API functions ``_PyObject_VisitManagedDict`` and +``_PyObject_ClearManagedDict`` to allow C extensions to allow the VM to +manage their object's dictionaries. From da8c7eb2825f6da308b1a7d700775516cb126bad Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Jul 2022 16:53:26 +0100 Subject: [PATCH 4/4] Use faster inline function. --- Objects/dictobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 699505e62b80c6..25e191fb8eadbe 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5595,7 +5595,7 @@ _PyObject_VisitManagedDict(PyObject *self, visitproc visit, void *arg) if (err) { return err; } - Py_VISIT(*_PyObject_DictPointer(self)); + Py_VISIT(*_PyObject_ManagedDictPointer(self)); return 0; } @@ -5609,7 +5609,7 @@ _PyObject_ClearManagedDict(PyObject *self) } _PyObject_FreeInstanceAttributes(self); *_PyObject_ValuesPointer(self) = NULL; - Py_CLEAR(*_PyObject_DictPointer(self)); + Py_CLEAR(*_PyObject_ManagedDictPointer(self)); } PyObject *