summaryrefslogtreecommitdiffstats
path: root/Include/internal/pycore_moduleobject.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-04-21 22:52:52 (GMT)
committerGitHub <noreply@github.com>2021-04-21 22:52:52 (GMT)
commitcdad2724e6f7426372901cc5dedd8a462ba046a6 (patch)
treeca6a34bfdc6c05ea86d2ff2686ebd116750b2305 /Include/internal/pycore_moduleobject.h
parenta32f8fe7133aad4f3cf8946534e3b79a5f2659da (diff)
downloadcpython-cdad2724e6f7426372901cc5dedd8a462ba046a6.zip
cpython-cdad2724e6f7426372901cc5dedd8a462ba046a6.tar.gz
cpython-cdad2724e6f7426372901cc5dedd8a462ba046a6.tar.bz2
bpo-40137: Add pycore_moduleobject.h internal header (GH-25507)
Add pycore_moduleobject.h internal header file with static inline functions to access module members: * _PyModule_GetDict() * _PyModule_GetDef() * _PyModule_GetState() These functions don't check at runtime if their argument has a valid type and can be inlined even if Python is not built with LTO. _PyType_GetModuleByDef() uses _PyModule_GetDef(). Replace PyModule_GetState() with _PyModule_GetState() in the extension modules, considered as performance sensitive: * _abc * _functools * _operator * _pickle * _queue * _random * _sre * _struct * _thread * _winapi * array * posix The following extensions are now built with the Py_BUILD_CORE_MODULE macro defined, to be able to use the internal pycore_moduleobject.h header: _abc, array, _operator, _queue, _sre, _struct.
Diffstat (limited to 'Include/internal/pycore_moduleobject.h')
-rw-r--r--Include/internal/pycore_moduleobject.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/Include/internal/pycore_moduleobject.h b/Include/internal/pycore_moduleobject.h
new file mode 100644
index 0000000..e9978ab
--- /dev/null
+++ b/Include/internal/pycore_moduleobject.h
@@ -0,0 +1,42 @@
+#ifndef Py_INTERNAL_MODULEOBJECT_H
+#define Py_INTERNAL_MODULEOBJECT_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+# error "this header requires Py_BUILD_CORE define"
+#endif
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *md_dict;
+ struct PyModuleDef *md_def;
+ void *md_state;
+ PyObject *md_weaklist;
+ // for logging purposes after md_dict is cleared
+ PyObject *md_name;
+} PyModuleObject;
+
+static inline PyModuleDef* _PyModule_GetDef(PyObject *mod) {
+ assert(PyModule_Check(mod));
+ return ((PyModuleObject *)mod)->md_def;
+}
+
+static inline void* _PyModule_GetState(PyObject* mod) {
+ assert(PyModule_Check(mod));
+ return ((PyModuleObject *)mod)->md_state;
+}
+
+static inline PyObject* _PyModule_GetDict(PyObject *mod) {
+ assert(PyModule_Check(mod));
+ PyObject *dict = ((PyModuleObject *)mod) -> md_dict;
+ // _PyModule_GetDict(mod) must not be used after calling module_clear(mod)
+ assert(dict != NULL);
+ return dict;
+}
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_INTERNAL_MODULEOBJECT_H */