Skip to content
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

bpo-39968: move extension modules' macros of get_module_state() to inline function. #19017

Merged
merged 3 commits into from
Mar 16, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use inline function to replace extension modules' get_module_state macros.
34 changes: 20 additions & 14 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ typedef struct {
long field_limit; /* max parsed field size */
} _csvstate;

#define _csvstate(o) ((_csvstate *)PyModule_GetState(o))
static inline _csvstate*
get_csv_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_csvstate *)state;
}

static int
_csv_clear(PyObject *m)
{
Py_CLEAR(_csvstate(m)->error_obj);
Py_CLEAR(_csvstate(m)->dialects);
Py_CLEAR(get_csv_state(m)->error_obj);
Py_CLEAR(get_csv_state(m)->dialects);
return 0;
}

static int
_csv_traverse(PyObject *m, visitproc visit, void *arg)
{
Py_VISIT(_csvstate(m)->error_obj);
Py_VISIT(_csvstate(m)->dialects);
Py_VISIT(get_csv_state(m)->error_obj);
Py_VISIT(get_csv_state(m)->dialects);
return 0;
}

Expand Down Expand Up @@ -1647,15 +1653,15 @@ PyInit__csv(void)
return NULL;

/* Set the field limit */
_csvstate(module)->field_limit = 128 * 1024;
get_csv_state(module)->field_limit = 128 * 1024;
/* Do I still need to add this var to the Module Dict? */

/* Add _dialects dictionary */
_csvstate(module)->dialects = PyDict_New();
if (_csvstate(module)->dialects == NULL)
get_csv_state(module)->dialects = PyDict_New();
if (get_csv_state(module)->dialects == NULL)
return NULL;
Py_INCREF(_csvstate(module)->dialects);
if (PyModule_AddObject(module, "_dialects", _csvstate(module)->dialects))
Py_INCREF(get_csv_state(module)->dialects);
if (PyModule_AddObject(module, "_dialects", get_csv_state(module)->dialects))
return NULL;

/* Add quote styles into dictionary */
Expand All @@ -1671,10 +1677,10 @@ PyInit__csv(void)
return NULL;

/* Add the CSV exception object to the module. */
_csvstate(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
if (_csvstate(module)->error_obj == NULL)
get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
if (get_csv_state(module)->error_obj == NULL)
return NULL;
Py_INCREF(_csvstate(module)->error_obj);
PyModule_AddObject(module, "Error", _csvstate(module)->error_obj);
Py_INCREF(get_csv_state(module)->error_obj);
PyModule_AddObject(module, "Error", get_csv_state(module)->error_obj);
return module;
}
23 changes: 15 additions & 8 deletions Modules/_curses_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ typedef struct {
PyObject *PyCursesPanel_Type;
} _curses_panelstate;

#define _curses_panelstate(o) ((_curses_panelstate *)PyModule_GetState(o))
static inline _curses_panelstate*
get_curses_panelstate(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_curses_panelstate *)state;
}

static int
_curses_panel_clear(PyObject *m)
{
Py_CLEAR(_curses_panelstate(m)->PyCursesError);
Py_CLEAR(get_curses_panelstate(m)->PyCursesError);
return 0;
}

static int
_curses_panel_traverse(PyObject *m, visitproc visit, void *arg)
{
Py_VISIT(_curses_panelstate(m)->PyCursesError);
Py_VISIT(get_curses_panelstate(m)->PyCursesError);
return 0;
}

Expand Down Expand Up @@ -645,24 +651,25 @@ PyInit__curses_panel(void)
if (v == NULL)
goto fail;
((PyTypeObject *)v)->tp_new = NULL;
_curses_panelstate(m)->PyCursesPanel_Type = v;
get_curses_panelstate(m)->PyCursesPanel_Type = v;

import_curses();
if (PyErr_Occurred())
goto fail;

/* For exception _curses_panel.error */
_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL);
PyDict_SetItemString(d, "error", _curses_panelstate(m)->PyCursesError);
get_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL);
PyDict_SetItemString(d, "error", get_curses_panelstate(m)->PyCursesError);

/* Make the version available */
v = PyUnicode_FromString(PyCursesVersion);
PyDict_SetItemString(d, "version", v);
PyDict_SetItemString(d, "__version__", v);
Py_DECREF(v);

Py_INCREF(_curses_panelstate(m)->PyCursesPanel_Type);
PyModule_AddObject(m, "panel", (PyObject *)_curses_panelstate(m)->PyCursesPanel_Type);
Py_INCREF(get_curses_panelstate(m)->PyCursesPanel_Type);
PyModule_AddObject(m, "panel",
(PyObject *)get_curses_panelstate(m)->PyCursesPanel_Type);
return m;
fail:
Py_XDECREF(m);
Expand Down
14 changes: 10 additions & 4 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ static struct PyModuleDef elementtreemodule;
/* Given a module object (assumed to be _elementtree), get its per-module
* state.
*/
#define ET_STATE(mod) ((elementtreestate *) PyModule_GetState(mod))
static inline elementtreestate*
get_elementtree_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (elementtreestate *)state;
}

/* Find the module instance imported in the currently running sub-interpreter
* and get its state.
Expand All @@ -112,7 +118,7 @@ static struct PyModuleDef elementtreemodule;
static int
elementtree_clear(PyObject *m)
{
elementtreestate *st = ET_STATE(m);
elementtreestate *st = get_elementtree_state(m);
Py_CLEAR(st->parseerror_obj);
Py_CLEAR(st->deepcopy_obj);
Py_CLEAR(st->elementpath_obj);
Expand All @@ -124,7 +130,7 @@ elementtree_clear(PyObject *m)
static int
elementtree_traverse(PyObject *m, visitproc visit, void *arg)
{
elementtreestate *st = ET_STATE(m);
elementtreestate *st = get_elementtree_state(m);
Py_VISIT(st->parseerror_obj);
Py_VISIT(st->deepcopy_obj);
Py_VISIT(st->elementpath_obj);
Expand Down Expand Up @@ -4377,7 +4383,7 @@ PyInit__elementtree(void)
m = PyModule_Create(&elementtreemodule);
if (!m)
return NULL;
st = ET_STATE(m);
st = get_elementtree_state(m);

if (!(temp = PyImport_ImportModule("copy")))
return NULL;
Expand Down
19 changes: 13 additions & 6 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ typedef struct {
PyTypeObject *EVPtype;
} _hashlibstate;

#define _hashlibstate(o) ((_hashlibstate *)PyModule_GetState(o))
static inline _hashlibstate*
get_hashlib_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_hashlibstate *)state;
}

#define _hashlibstate_global ((_hashlibstate *)PyModule_GetState(PyState_FindModule(&_hashlibmodule)))


Expand Down Expand Up @@ -1112,15 +1119,15 @@ static struct PyMethodDef EVP_functions[] = {
static int
hashlib_traverse(PyObject *m, visitproc visit, void *arg)
{
_hashlibstate *state = _hashlibstate(m);
_hashlibstate *state = get_hashlib_state(m);
Py_VISIT(state->EVPtype);
return 0;
}

static int
hashlib_clear(PyObject *m)
{
_hashlibstate *state = _hashlibstate(m);
_hashlibstate *state = get_hashlib_state(m);
Py_CLEAR(state->EVPtype);
return 0;
}
Expand Down Expand Up @@ -1168,7 +1175,7 @@ PyInit__hashlib(void)
PyTypeObject *EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
if (EVPtype == NULL)
return NULL;
_hashlibstate(m)->EVPtype = EVPtype;
get_hashlib_state(m)->EVPtype = EVPtype;

openssl_md_meth_names = generate_hash_name_list();
if (openssl_md_meth_names == NULL) {
Expand All @@ -1180,8 +1187,8 @@ PyInit__hashlib(void)
return NULL;
}

Py_INCREF((PyObject *)_hashlibstate(m)->EVPtype);
PyModule_AddObject(m, "HASH", (PyObject *)_hashlibstate(m)->EVPtype);
Py_INCREF((PyObject *)get_hashlib_state(m)->EVPtype);
PyModule_AddObject(m, "HASH", (PyObject *)get_hashlib_state(m)->EVPtype);

PyState_AddModule(m, &_hashlibmodule);
return m;
Expand Down
15 changes: 11 additions & 4 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,20 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err)
return result;
}

static inline _PyIO_State*
get_io_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_PyIO_State *)state;
}

_PyIO_State *
_PyIO_get_module_state(void)
{
PyObject *mod = PyState_FindModule(&_PyIO_Module);
_PyIO_State *state;
if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) {
if (mod == NULL || (state = get_io_state(mod)) == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"could not find io module state "
"(interpreter shutdown?)");
Expand Down Expand Up @@ -615,7 +622,7 @@ _PyIO_get_locale_module(_PyIO_State *state)

static int
iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
_PyIO_State *state = IO_MOD_STATE(mod);
_PyIO_State *state = get_io_state(mod);
if (!state->initialized)
return 0;
if (state->locale_module != NULL) {
Expand All @@ -628,7 +635,7 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {

static int
iomodule_clear(PyObject *mod) {
_PyIO_State *state = IO_MOD_STATE(mod);
_PyIO_State *state = get_io_state(mod);
if (!state->initialized)
return 0;
if (state->locale_module != NULL)
Expand Down Expand Up @@ -674,7 +681,7 @@ PyInit__io(void)
_PyIO_State *state = NULL;
if (m == NULL)
return NULL;
state = IO_MOD_STATE(m);
state = get_io_state(m);
state->initialized = 0;

#define ADD_TYPE(type, name) \
Expand Down
29 changes: 18 additions & 11 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ typedef struct {

static struct PyModuleDef _posixsubprocessmodule;

#define _posixsubprocessstate(o) ((_posixsubprocessstate *)PyModule_GetState(o))
#define _posixsubprocessstate_global _posixsubprocessstate(PyState_FindModule(&_posixsubprocessmodule))
static inline _posixsubprocessstate*
get_posixsubprocess_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_posixsubprocessstate *)state;
}

#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))

/* If gc was disabled, call gc.enable(). Return 0 on success. */
static int
Expand Down Expand Up @@ -944,16 +951,16 @@ static PyMethodDef module_methods[] = {


static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(_posixsubprocessstate(m)->disable);
Py_VISIT(_posixsubprocessstate(m)->enable);
Py_VISIT(_posixsubprocessstate(m)->isenabled);
Py_VISIT(get_posixsubprocess_state(m)->disable);
Py_VISIT(get_posixsubprocess_state(m)->enable);
Py_VISIT(get_posixsubprocess_state(m)->isenabled);
return 0;
}

static int _posixsubprocess_clear(PyObject *m) {
Py_CLEAR(_posixsubprocessstate(m)->disable);
Py_CLEAR(_posixsubprocessstate(m)->enable);
Py_CLEAR(_posixsubprocessstate(m)->isenabled);
Py_CLEAR(get_posixsubprocess_state(m)->disable);
Py_CLEAR(get_posixsubprocess_state(m)->enable);
Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
return 0;
}

Expand Down Expand Up @@ -989,9 +996,9 @@ PyInit__posixsubprocess(void)
return NULL;
}

_posixsubprocessstate(m)->disable = PyUnicode_InternFromString("disable");
_posixsubprocessstate(m)->enable = PyUnicode_InternFromString("enable");
_posixsubprocessstate(m)->isenabled = PyUnicode_InternFromString("isenabled");
get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");

PyState_AddModule(m, &_posixsubprocessmodule);
return m;
Expand Down
20 changes: 13 additions & 7 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ typedef struct {
PyObject *Long___abs__;
} _randomstate;

#define _randomstate(o) ((_randomstate *)PyModule_GetState(o))
static inline _randomstate*
get_random_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_randomstate *)state;
}

static struct PyModuleDef _randommodule;

#define _randomstate_global _randomstate(PyState_FindModule(&_randommodule))
#define _randomstate_global get_random_state(PyState_FindModule(&_randommodule))

typedef struct {
PyObject_HEAD
Expand Down Expand Up @@ -561,15 +567,15 @@ PyDoc_STRVAR(module_doc,
static int
_random_traverse(PyObject *module, visitproc visit, void *arg)
{
Py_VISIT(_randomstate(module)->Random_Type);
Py_VISIT(get_random_state(module)->Random_Type);
return 0;
}

static int
_random_clear(PyObject *module)
{
Py_CLEAR(_randomstate(module)->Random_Type);
Py_CLEAR(_randomstate(module)->Long___abs__);
Py_CLEAR(get_random_state(module)->Random_Type);
Py_CLEAR(get_random_state(module)->Long___abs__);
return 0;
}

Expand Down Expand Up @@ -606,7 +612,7 @@ PyInit__random(void)
Py_DECREF(Random_Type);
return NULL;
}
_randomstate(m)->Random_Type = Random_Type;
get_random_state(m)->Random_Type = Random_Type;

Py_INCREF(Random_Type);
PyModule_AddObject(m, "Random", Random_Type);
Expand All @@ -624,7 +630,7 @@ PyInit__random(void)

Py_DECREF(longtype);
Py_DECREF(longval);
_randomstate(m)->Long___abs__ = abs;
get_random_state(m)->Long___abs__ = abs;

return m;

Expand Down
Loading