Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add PyType_GetModule and PyType_GetModuleState accessors
  • Loading branch information
Marcel Plch authored and encukou committed May 5, 2020
commit d7c35512580d7c4445dd07178aa34fe99d9c8f8a
2 changes: 2 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
#endif

/* Generic type check */
Expand Down
34 changes: 34 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3170,6 +3170,40 @@ PyType_GetSlot(PyTypeObject *type, int slot)
return *(void**)(((char*)type) + slotoffsets[slot]);
}

PyObject *
PyType_GetModule(PyTypeObject *type)
{
assert(PyType_Check(type));
if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(
PyExc_TypeError,
"PyType_GetModule: Type '%s' is not a heap type",
type->tp_name);
return NULL;
}

PyHeapTypeObject* et = (PyHeapTypeObject*)type;
if (!et->ht_module) {
PyErr_Format(
PyExc_TypeError,
"PyType_GetModule: Type '%s' has no associated module",
type->tp_name);
return NULL;
}
return et->ht_module;

}

void *
PyType_GetModuleState(PyTypeObject *type)
{
PyObject *m = PyType_GetModule(type);
if (m == NULL) {
return NULL;
}
return PyModule_GetState(m);
}

/* Internal API to look for a name through the MRO, bypassing the method cache.
This returns a borrowed reference, and might set an exception.
'error' is set to: -1: error with exception; 1: error without exception; 0: ok */
Expand Down