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

@ -3,8 +3,9 @@
/* Interface to Sjoerd's portable C thread library */
#include "Python.h"
#include "pycore_pylifecycle.h"
#include "pycore_interp.h" // _PyInterpreterState.num_threads
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h" // _PyThreadState_Init()
#include <stddef.h> // offsetof()
#include "structmember.h" // PyMemberDef
@ -35,7 +36,7 @@ typedef struct {
static inline thread_module_state*
get_thread_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (thread_module_state *)state;
}