gh-139852: Add PyObject_GetDict() function by vstinner · Pull Request #139968 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
41 changes: 36 additions & 5 deletions Doc/c-api/object.rst
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ New features

(Contributed by Victor Stinner in :gh:`129813`.)

* Add :c:type:`PyObject_GetDict` function to get the
:py:attr:`~object.__dict__` of an object.
(Contributed by Victor Stinner in :gh:`139852`.)

* Add :c:func:`PyTuple_FromArray` to create a :class:`tuple` from an array.
(Contributed by Victor Stinner in :gh:`111489`.)

Expand Down Expand Up @@ -1017,6 +1021,10 @@ Deprecated C APIs
since 3.15 and will be removed in 3.17.
(Contributed by Nikita Sobolev in :gh:`136355`.)

* Deprecate the private :c:func:`_PyObject_GetDictPtr` function:
use public :c:func:`PyObject_GetDict`, :c:func:`PyObject_GetAttr`,
:c:func:`PyObject_SetAttr` or :c:func:`PyObject_ClearManagedDict` instead.
(Contributed by Victor Stinner in :gh:`139852`.)

.. Add C API deprecations above alphabetically, not here at the end.

Expand Down
3 changes: 2 additions & 1 deletion Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ PyAPI_FUNC(void) _PyObject_Dump(PyObject *);

PyAPI_FUNC(PyObject*) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);

PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
PyAPI_FUNC(int) PyObject_GetDict(PyObject *obj, PyObject **dict);
Py_DEPRECATED(3.15) PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);

Expand Down
47 changes: 47 additions & 0 deletions Lib/test/test_capi/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,52 @@ def func(x):

func(object())

def test_object_getdict(self):
# Test PyObject_GetDict()
object_getdict = _testcapi.object_getdict

class MyClass:
pass
obj = MyClass()

dict1 = object_getdict(obj)
dict2 = obj.__dict__
self.assertIs(dict1, dict2)

obj.attr = 123
self.assertEqual(dict1, {'attr': 123})

class NoDict:
__slots__ = ()
obj = NoDict()

self.assertEqual(object_getdict(obj), AttributeError)

# CRASHES object_getdict(NULL)

def test_object_genericgetdict(self):
# Test PyObject_GenericGetDict()
object_genericgetdict = _testcapi.object_genericgetdict

class MyClass:
pass
obj = MyClass()
obj.attr = 123

dict1 = object_genericgetdict(obj)
dict2 = obj.__dict__
self.assertIs(dict1, dict2)

class NoDict:
__slots__ = ()
obj = NoDict()

with self.assertRaisesRegex(AttributeError,
"This object has no __dict__"):
object_genericgetdict(obj)

# CRASHES object_genericgetdict(NULL)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:type:`PyObject_GetDict` function to get the :py:attr:`~object.__dict__`
of an object. Patch by Victor Stinner.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecate the private :c:func:`_PyObject_GetDictPtr` function: use public
:c:func:`PyObject_GetDict`, :c:func:`PyObject_GetAttr`,
:c:func:`PyObject_SetAttr` or :c:func:`PyObject_ClearManagedDict` instead.
41 changes: 41 additions & 0 deletions Modules/_testcapi/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,45 @@ is_uniquely_referenced(PyObject *self, PyObject *op)
}


static PyObject *
object_getdict(PyObject *self, PyObject *obj)
{
NULLABLE(obj);

PyObject *dict = UNINITIALIZED_PTR;
switch (PyObject_GetDict(obj, &dict)) {
case -1:
assert(dict == NULL);
return NULL;
case 0:
assert(dict == NULL);
return Py_NewRef(PyExc_AttributeError);
case 1:
return dict;
default:
Py_FatalError("PyObject_GetDict() returned invalid code");
Py_UNREACHABLE();
}
}


static PyObject *
object_genericgetdict(PyObject *self, PyObject *obj)
{
NULLABLE(obj);

PyObject *dict = PyObject_GenericGetDict(obj, NULL);
if (dict != NULL) {
return dict;
}

if (PyErr_Occurred()) {
return NULL;
}
return Py_NewRef(PyExc_AttributeError);
}


static PyMethodDef test_methods[] = {
{"call_pyobject_print", call_pyobject_print, METH_VARARGS},
{"pyobject_print_null", pyobject_print_null, METH_VARARGS},
Expand All @@ -511,6 +550,8 @@ static PyMethodDef test_methods[] = {
{"test_py_is_funcs", test_py_is_funcs, METH_NOARGS},
{"clear_managed_dict", clear_managed_dict, METH_O, NULL},
{"is_uniquely_referenced", is_uniquely_referenced, METH_O},
{"object_getdict", object_getdict, METH_O},
{"object_genericgetdict", object_genericgetdict, METH_O},
{NULL},
};

Expand Down
53 changes: 43 additions & 10 deletions Objects/object.c
Loading