Skip to content

bpo-40137: Add pycore_moduleobject.h internal header #25507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions Include/internal/pycore_moduleobject.h
Original file line number Diff line number Diff line change
@@ -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 */
1 change: 1 addition & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_interp.h \
$(srcdir)/Include/internal/pycore_list.h \
$(srcdir)/Include/internal/pycore_long.h \
$(srcdir)/Include/internal/pycore_moduleobject.h \
$(srcdir)/Include/internal/pycore_object.h \
$(srcdir)/Include/internal/pycore_pathconfig.h \
$(srcdir)/Include/internal/pycore_pyarena.h \
Expand Down
12 changes: 6 additions & 6 deletions Modules/Setup
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ posix -DPy_BUILD_CORE_BUILTIN -I$(srcdir)/Include/internal posixmodule.c # posix
errno errnomodule.c # posix (UNIX) errno values
pwd pwdmodule.c # this is needed to find out the user's home dir
# if $HOME is not set
_sre _sre.c # Fredrik Lundh's new regular expressions
_sre -DPy_BUILD_CORE_BUILTIN _sre.c # Fredrik Lundh's new regular expressions
_codecs _codecsmodule.c # access to the builtin codecs and codec registry
_weakref _weakref.c # weak references
_functools -DPy_BUILD_CORE_BUILTIN -I$(srcdir)/Include/internal _functoolsmodule.c # Tools for working with functions and callable objects
_operator _operator.c # operator.add() and similar goodies
_operator -DPy_BUILD_CORE_BUILTIN _operator.c # operator.add() and similar goodies
_collections _collectionsmodule.c # Container types
_abc _abc.c # Abstract base classes
_abc -DPy_BUILD_CORE_BUILTIN _abc.c # Abstract base classes
itertools itertoolsmodule.c # Functions creating iterators for efficient looping
atexit atexitmodule.c # Register functions to be run at interpreter-shutdown
_signal -DPy_BUILD_CORE_BUILTIN -I$(srcdir)/Include/internal signalmodule.c
Expand Down Expand Up @@ -166,17 +166,17 @@ _symtable symtablemodule.c

# Modules that should always be present (non UNIX dependent):

#array arraymodule.c # array objects
#array -DPy_BUILD_CORE_MODULE arraymodule.c # array objects
#cmath cmathmodule.c _math.c -DPy_BUILD_CORE_MODULE # -lm # complex math library functions
#math mathmodule.c _math.c -DPy_BUILD_CORE_MODULE # -lm # math library functions, e.g. sin()
#_contextvars _contextvarsmodule.c # Context Variables
#_struct _struct.c # binary structure packing/unpacking
#_struct -DPy_BUILD_CORE_MODULE _struct.c # binary structure packing/unpacking
#_weakref _weakref.c # basic weak reference support
#_testcapi _testcapimodule.c # Python C API test module
#_testinternalcapi _testinternalcapi.c -I$(srcdir)/Include/internal -DPy_BUILD_CORE_MODULE # Python internal C API test module
#_random _randommodule.c -DPy_BUILD_CORE_MODULE # Random number generator
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
#_pickle _pickle.c # pickle accelerator
#_pickle -DPy_BUILD_CORE_MODULE _pickle.c # pickle accelerator
#_datetime _datetimemodule.c # datetime accelerator
#_zoneinfo _zoneinfo.c -DPy_BUILD_CORE_MODULE # zoneinfo accelerator
#_bisect _bisectmodule.c # Bisection algorithms
Expand Down
3 changes: 2 additions & 1 deletion Modules/_abc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* ABCMeta implementation */

#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "clinic/_abc.c.h"

/*[clinic input]
Expand All @@ -27,7 +28,7 @@ typedef struct {
static inline _abcmodule_state*
get_abc_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_abcmodule_state *)state;
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_object.h" // _PyObject_GC_TRACK
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
Expand Down Expand Up @@ -35,7 +36,7 @@ typedef struct _functools_state {
static inline _functools_state *
get_functools_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_functools_state *)state;
}
Expand All @@ -52,8 +53,7 @@ get_functools_state_by_type(PyTypeObject *type)
if (module == NULL) {
return NULL;
}
_functools_state *state = get_functools_state(module);
return state;
return get_functools_state(module);
}

static PyObject *
Expand Down
5 changes: 2 additions & 3 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

#include "Python.h"

#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "clinic/_operator.c.h"

typedef struct {
Expand All @@ -12,7 +11,7 @@ typedef struct {
static inline _operator_state*
get_operator_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_operator_state *)state;
}
Expand Down
3 changes: 2 additions & 1 deletion Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif

#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef

PyDoc_STRVAR(pickle_module_doc,
Expand Down Expand Up @@ -182,7 +183,7 @@ static struct PyModuleDef _picklemodule;
static PickleState *
_Pickle_GetState(PyObject *module)
{
return (PickleState *)PyModule_GetState(module);
return (PickleState *)_PyModule_GetState(module);
}

/* Find the module instance imported in the currently running sub-interpreter
Expand Down
3 changes: 2 additions & 1 deletion Modules/_queuemodule.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <stddef.h> // offsetof()

Expand All @@ -10,7 +11,7 @@ typedef struct {
static simplequeue_state *
simplequeue_get_state(PyObject *module)
{
simplequeue_state *state = PyModule_GetState(module);
simplequeue_state *state = _PyModule_GetState(module);
assert(state);
return state;
}
Expand Down
5 changes: 3 additions & 2 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
/* ---------------------------------------------------------------*/

#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#ifdef HAVE_PROCESS_H
# include <process.h> // getpid()
#endif
Expand All @@ -86,7 +87,7 @@ typedef struct {
static inline _randomstate*
get_random_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_randomstate *)state;
}
Expand Down Expand Up @@ -538,7 +539,7 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

if (PyTuple_GET_SIZE(args) == 1)
arg = PyTuple_GET_ITEM(args, 0);

tmp = random_seed(self, arg);
if (tmp == NULL) {
Py_DECREF(self);
Expand Down
3 changes: 2 additions & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static const char copyright[] =

#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef

#include "sre.h"
Expand Down Expand Up @@ -258,7 +259,7 @@ typedef struct {
static _sremodulestate *
get_sre_module_state(PyObject *m)
{
_sremodulestate *state = (_sremodulestate *)PyModule_GetState(m);
_sremodulestate *state = (_sremodulestate *)_PyModule_GetState(m);
assert(state);
return state;
}
Expand Down
3 changes: 2 additions & 1 deletion Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <ctype.h>

Expand All @@ -24,7 +25,7 @@ typedef struct {
static inline _structmodulestate*
get_struct_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_structmodulestate *)state;
}
Expand Down
5 changes: 3 additions & 2 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_winapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/* See http://www.python.org/2.4/license for licensing details. */

#include "Python.h"
#include "moduleobject.h" // PyModuleDef_Slot
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef


Expand Down Expand Up @@ -87,7 +87,7 @@ typedef struct {
static inline WinApiState*
winapi_get_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (WinApiState *)state;
}
Expand Down
3 changes: 2 additions & 1 deletion Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <stddef.h> // offsetof()

Expand Down Expand Up @@ -63,7 +64,7 @@ typedef struct {
static array_state *
get_array_state(PyObject *module)
{
return (array_state *)PyModule_GetState(module);
return (array_state *)_PyModule_GetState(module);
}

#define find_array_state_by_type(tp) \
Expand Down
3 changes: 2 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "Python.h"
#include "pycore_fileutils.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#ifdef MS_WINDOWS
/* include <windows.h> early to avoid conflict with pycore_condvar.h:

Expand Down Expand Up @@ -994,7 +995,7 @@ typedef struct {
static inline _posixstate*
get_posix_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_posixstate *)state;
}
Expand Down
3 changes: 2 additions & 1 deletion Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Python.h"
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()

#include "frameobject.h" // PyFrameObject
Expand Down Expand Up @@ -1176,7 +1177,7 @@ _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
PyObject *builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__);
if (builtins) {
if (PyModule_Check(builtins)) {
builtins = PyModule_GetDict(builtins);
builtins = _PyModule_GetDict(builtins);
assert(builtins != NULL);
}
return builtins;
Expand Down
19 changes: 4 additions & 15 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_moduleobject.h" // _PyModule_GetDef()
#include "structmember.h" // PyMemberDef

static Py_ssize_t max_module_number;
Expand All @@ -12,15 +13,6 @@ _Py_IDENTIFIER(__doc__);
_Py_IDENTIFIER(__name__);
_Py_IDENTIFIER(__spec__);

typedef struct {
PyObject_HEAD
PyObject *md_dict;
struct PyModuleDef *md_def;
void *md_state;
PyObject *md_weaklist;
PyObject *md_name; /* for logging purposes after md_dict is cleared */
} PyModuleObject;

static PyMemberDef module_members[] = {
{"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
{0}
Expand Down Expand Up @@ -469,14 +461,11 @@ PyModule_SetDocString(PyObject *m, const char *doc)
PyObject *
PyModule_GetDict(PyObject *m)
{
PyObject *d;
if (!PyModule_Check(m)) {
PyErr_BadInternalCall();
return NULL;
}
d = ((PyModuleObject *)m) -> md_dict;
assert(d != NULL);
return d;
return _PyModule_GetDict(m);
}

PyObject*
Expand Down Expand Up @@ -556,7 +545,7 @@ PyModule_GetDef(PyObject* m)
PyErr_BadArgument();
return NULL;
}
return ((PyModuleObject *)m)->md_def;
return _PyModule_GetDef(m);
}

void*
Expand All @@ -566,7 +555,7 @@ PyModule_GetState(PyObject* m)
PyErr_BadArgument();
return NULL;
}
return ((PyModuleObject *)m)->md_state;
return _PyModule_GetState(m);
}

void
Expand Down
Loading