@@ -638,6 +638,30 @@ test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
638638 Py_RETURN_NONE ;
639639}
640640
641+ static PyObject *
642+ test_get_type_dict (PyObject * self , PyObject * Py_UNUSED (ignored ))
643+ {
644+ /* Test for PyType_GetDict */
645+
646+ // Assert ints have a `to_bytes` method
647+ PyObject * long_dict = PyType_GetDict (& PyLong_Type );
648+ assert (long_dict );
649+ assert (PyDict_GetItemString (long_dict , "to_bytes" )); // borrowed ref
650+ Py_DECREF (long_dict );
651+
652+ // Make a new type, add an attribute to it and assert it's there
653+ PyObject * HeapTypeNameType = PyType_FromSpec (& HeapTypeNameType_Spec );
654+ assert (HeapTypeNameType );
655+ assert (PyObject_SetAttrString (
656+ HeapTypeNameType , "new_attr" , Py_NewRef (Py_None )) >= 0 );
657+ PyObject * type_dict = PyType_GetDict ((PyTypeObject * )HeapTypeNameType );
658+ assert (type_dict );
659+ assert (PyDict_GetItemString (type_dict , "new_attr" )); // borrowed ref
660+ Py_DECREF (HeapTypeNameType );
661+ Py_DECREF (type_dict );
662+ Py_RETURN_NONE ;
663+ }
664+
641665static PyObject *
642666pyobject_repr_from_null (PyObject * self , PyObject * Py_UNUSED (ignored ))
643667{
@@ -3472,6 +3496,7 @@ static PyMethodDef TestMethods[] = {
34723496 {"test_get_statictype_slots" , test_get_statictype_slots , METH_NOARGS },
34733497 {"test_get_type_name" , test_get_type_name , METH_NOARGS },
34743498 {"test_get_type_qualname" , test_get_type_qualname , METH_NOARGS },
3499+ {"test_get_type_dict" , test_get_type_dict , METH_NOARGS },
34753500 {"_test_thread_state" , test_thread_state , METH_VARARGS },
34763501#ifndef MS_WINDOWS
34773502 {"_spawn_pthread_waiter" , spawn_pthread_waiter , METH_NOARGS },
0 commit comments