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.
This commit is contained in:
Victor Stinner 2021-04-22 00:52:52 +02:00 committed by GitHub
parent a32f8fe713
commit cdad2724e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 97 additions and 47 deletions

View file

@ -869,7 +869,8 @@ class PyBuildExt(build_ext):
#
# array objects
self.add(Extension('array', ['arraymodule.c']))
self.add(Extension('array', ['arraymodule.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# Context Variables
self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
@ -934,9 +935,11 @@ class PyBuildExt(build_ext):
self.add(Extension("_asyncio", ["_asynciomodule.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# _abc speedups
self.add(Extension("_abc", ["_abc.c"]))
self.add(Extension("_abc", ["_abc.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# _queue module
self.add(Extension("_queue", ["_queuemodule.c"]))
self.add(Extension("_queue", ["_queuemodule.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# _statistics module
self.add(Extension("_statistics", ["_statisticsmodule.c"]))
@ -2696,7 +2699,8 @@ def main():
'install_lib': PyBuildInstallLib},
# The struct module is defined here, because build_ext won't be
# called unless there's at least one extension module defined.
ext_modules=[Extension('_struct', ['_struct.c'])],
ext_modules=[Extension('_struct', ['_struct.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
# If you change the scripts installed here, you also need to
# check the PyBuildScripts command above, and change the links