Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-92678: Expose managed dict clear and visit functions #95246

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/dictobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is quite a bit weird: these functions receive an object and then visit/clear the dict. This is not an API for dictionary objects, it's an API for instances so I think it should not be here but in object.h or some other header.

Looking at these functions, at first sight, it seems that "self" is the dictionary and not the oject.

PyAPI_FUNC(void) _PyObject_ClearManagedDict(PyObject *self);
14 changes: 14 additions & 0 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 */
};

Expand Down
29 changes: 29 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
}

PyObject *
PyObject_GenericGetDict(PyObject *obj, void *context)
{
Expand Down