]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-92678: Expose managed dict clear and visit functions (#95246)
authorMark Shannon <mark@hotpy.org>
Mon, 25 Jul 2022 21:30:53 +0000 (22:30 +0100)
committerGitHub <noreply@github.com>
Mon, 25 Jul 2022 21:30:53 +0000 (22:30 +0100)
Include/cpython/dictobject.h
Lib/test/test_capi.py
Misc/NEWS.d/next/C API/2022-07-25-15-54-27.gh-issue-92678.ziZpxz.rst [new file with mode: 0644]
Modules/_testcapimodule.c
Objects/dictobject.c

index 565ad791a6cb286631796adb17122b44c81884a9..c2e4a46e76195bcf406e851e6999bc4f141bf792 100644 (file)
@@ -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);
index 7e571ab4f9e593e44e4f3d9114e26228de36c680..a88a17d3c55788cd3590bc33362f459a9d4ded1c 100644 (file)
@@ -722,6 +722,20 @@ class CAPITest(unittest.TestCase):
             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/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 (file)
index 0000000..52473c9
--- /dev/null
@@ -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.
index 02635c427200d66612c17d6879653911eae9ada1..b9f75d154ee5c05e4a06e356eedf7d31b815010c 100644 (file)
@@ -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 */
 };
 
index ebbd22ee7c145ead02dd50741df1e5ce1908d348..25e191fb8eadbe8568943ec70099c6901f1399af 100644 (file)
@@ -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_ManagedDictPointer(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_ManagedDictPointer(self));
+}
+
 PyObject *
 PyObject_GenericGetDict(PyObject *obj, void *context)
 {