diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-07-10 16:41:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-10 16:41:02 (GMT) |
commit | a840806d338805fe74a9de01081d30da7605a29f (patch) | |
tree | 06f91a8b4ff44d2ed848952332e7c279de74f6e1 /Modules/_testcapimodule.c | |
parent | 3e23fa71f43fb225ca29a931644d1100e2f4d6b8 (diff) | |
download | cpython-a840806d338805fe74a9de01081d30da7605a29f.zip cpython-a840806d338805fe74a9de01081d30da7605a29f.tar.gz cpython-a840806d338805fe74a9de01081d30da7605a29f.tar.bz2 |
gh-105227: Add PyType_GetDict() (GH-105747)
This compensates for static builtin types having `tp_dict` set to `NULL`.
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 50eaff9..dd2c9c7 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -639,6 +639,30 @@ test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored)) } static PyObject * +test_get_type_dict(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + /* Test for PyType_GetDict */ + + // Assert ints have a `to_bytes` method + PyObject *long_dict = PyType_GetDict(&PyLong_Type); + assert(long_dict); + assert(PyDict_GetItemString(long_dict, "to_bytes")); // borrowed ref + Py_DECREF(long_dict); + + // Make a new type, add an attribute to it and assert it's there + PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec); + assert(HeapTypeNameType); + assert(PyObject_SetAttrString( + HeapTypeNameType, "new_attr", Py_NewRef(Py_None)) >= 0); + PyObject *type_dict = PyType_GetDict((PyTypeObject*)HeapTypeNameType); + assert(type_dict); + assert(PyDict_GetItemString(type_dict, "new_attr")); // borrowed ref + Py_DECREF(HeapTypeNameType); + Py_DECREF(type_dict); + Py_RETURN_NONE; +} + +static PyObject * pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored)) { return PyObject_Repr(NULL); @@ -3472,6 +3496,7 @@ static PyMethodDef TestMethods[] = { {"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS}, {"test_get_type_name", test_get_type_name, METH_NOARGS}, {"test_get_type_qualname", test_get_type_qualname, METH_NOARGS}, + {"test_get_type_dict", test_get_type_dict, METH_NOARGS}, {"_test_thread_state", test_thread_state, METH_VARARGS}, #ifndef MS_WINDOWS {"_spawn_pthread_waiter", spawn_pthread_waiter, METH_NOARGS}, |