Skip to content

Commit ccd98a3

Browse files
gh-101476: Add _PyType_GetModuleState (GH-101477)
For fast module state access from heap type methods.
1 parent d43c265 commit ccd98a3

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
extern "C" {
55
#endif
66

7+
#include "pycore_moduleobject.h"
8+
79
#ifndef Py_BUILD_CORE
810
# error "this header requires Py_BUILD_CORE define"
911
#endif
@@ -62,6 +64,20 @@ _PyStaticType_GET_WEAKREFS_LISTPTR(static_builtin_state *state)
6264
return &state->tp_weaklist;
6365
}
6466

67+
/* Like PyType_GetModuleState, but skips verification
68+
* that type is a heap type with an associated module */
69+
static inline void *
70+
_PyType_GetModuleState(PyTypeObject *type)
71+
{
72+
assert(PyType_Check(type));
73+
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
74+
PyHeapTypeObject *et = (PyHeapTypeObject *)type;
75+
assert(et->ht_module);
76+
PyModuleObject *mod = (PyModuleObject *)(et->ht_module);
77+
assert(mod != NULL);
78+
return mod->md_state;
79+
}
80+
6581
struct types_state {
6682
struct type_cache type_cache;
6783
size_t num_builtins_initialized;

Modules/itertoolsmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "pycore_call.h" // _PyObject_CallNoArgs()
44
#include "pycore_long.h" // _PyLong_GetZero()
55
#include "pycore_moduleobject.h" // _PyModule_GetState()
6+
#include "pycore_typeobject.h" // _PyType_GetModuleState()
67
#include "pycore_object.h" // _PyObject_GC_TRACK()
78
#include "pycore_tuple.h" // _PyTuple_ITEMS()
89
#include "structmember.h" // PyMemberDef
@@ -48,7 +49,7 @@ get_module_state(PyObject *mod)
4849
static inline itertools_state *
4950
get_module_state_by_cls(PyTypeObject *cls)
5051
{
51-
void *state = PyType_GetModuleState(cls);
52+
void *state = _PyType_GetModuleState(cls);
5253
assert(state != NULL);
5354
return (itertools_state *)state;
5455
}

0 commit comments

Comments
 (0)