bpo-39968: Convert extension modules' macros of get_module_state() to inline functions (GH-19017)

This commit is contained in:
Hai Shi 2020-03-16 21:15:01 +08:00 committed by GitHub
parent 4ab362cec6
commit f707d94af6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 279 additions and 164 deletions

View File

@ -0,0 +1 @@
Use inline function to replace extension modules' get_module_state macros.

View File

@ -21,21 +21,27 @@ typedef struct {
long field_limit; /* max parsed field size */ long field_limit; /* max parsed field size */
} _csvstate; } _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 static int
_csv_clear(PyObject *m) _csv_clear(PyObject *m)
{ {
Py_CLEAR(_csvstate(m)->error_obj); Py_CLEAR(get_csv_state(m)->error_obj);
Py_CLEAR(_csvstate(m)->dialects); Py_CLEAR(get_csv_state(m)->dialects);
return 0; return 0;
} }
static int static int
_csv_traverse(PyObject *m, visitproc visit, void *arg) _csv_traverse(PyObject *m, visitproc visit, void *arg)
{ {
Py_VISIT(_csvstate(m)->error_obj); Py_VISIT(get_csv_state(m)->error_obj);
Py_VISIT(_csvstate(m)->dialects); Py_VISIT(get_csv_state(m)->dialects);
return 0; return 0;
} }
@ -1647,15 +1653,15 @@ PyInit__csv(void)
return NULL; return NULL;
/* Set the field limit */ /* 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? */ /* Do I still need to add this var to the Module Dict? */
/* Add _dialects dictionary */ /* Add _dialects dictionary */
_csvstate(module)->dialects = PyDict_New(); get_csv_state(module)->dialects = PyDict_New();
if (_csvstate(module)->dialects == NULL) if (get_csv_state(module)->dialects == NULL)
return NULL; return NULL;
Py_INCREF(_csvstate(module)->dialects); Py_INCREF(get_csv_state(module)->dialects);
if (PyModule_AddObject(module, "_dialects", _csvstate(module)->dialects)) if (PyModule_AddObject(module, "_dialects", get_csv_state(module)->dialects))
return NULL; return NULL;
/* Add quote styles into dictionary */ /* Add quote styles into dictionary */
@ -1671,10 +1677,10 @@ PyInit__csv(void)
return NULL; return NULL;
/* Add the CSV exception object to the module. */ /* Add the CSV exception object to the module. */
_csvstate(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL); get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
if (_csvstate(module)->error_obj == NULL) if (get_csv_state(module)->error_obj == NULL)
return NULL; return NULL;
Py_INCREF(_csvstate(module)->error_obj); Py_INCREF(get_csv_state(module)->error_obj);
PyModule_AddObject(module, "Error", _csvstate(module)->error_obj); PyModule_AddObject(module, "Error", get_csv_state(module)->error_obj);
return module; return module;
} }

View File

@ -21,19 +21,25 @@ typedef struct {
PyObject *PyCursesPanel_Type; PyObject *PyCursesPanel_Type;
} _curses_panelstate; } _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 static int
_curses_panel_clear(PyObject *m) _curses_panel_clear(PyObject *m)
{ {
Py_CLEAR(_curses_panelstate(m)->PyCursesError); Py_CLEAR(get_curses_panelstate(m)->PyCursesError);
return 0; return 0;
} }
static int static int
_curses_panel_traverse(PyObject *m, visitproc visit, void *arg) _curses_panel_traverse(PyObject *m, visitproc visit, void *arg)
{ {
Py_VISIT(_curses_panelstate(m)->PyCursesError); Py_VISIT(get_curses_panelstate(m)->PyCursesError);
return 0; return 0;
} }
@ -645,15 +651,15 @@ PyInit__curses_panel(void)
if (v == NULL) if (v == NULL)
goto fail; goto fail;
((PyTypeObject *)v)->tp_new = NULL; ((PyTypeObject *)v)->tp_new = NULL;
_curses_panelstate(m)->PyCursesPanel_Type = v; get_curses_panelstate(m)->PyCursesPanel_Type = v;
import_curses(); import_curses();
if (PyErr_Occurred()) if (PyErr_Occurred())
goto fail; goto fail;
/* For exception _curses_panel.error */ /* For exception _curses_panel.error */
_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL); get_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL);
PyDict_SetItemString(d, "error", _curses_panelstate(m)->PyCursesError); PyDict_SetItemString(d, "error", get_curses_panelstate(m)->PyCursesError);
/* Make the version available */ /* Make the version available */
v = PyUnicode_FromString(PyCursesVersion); v = PyUnicode_FromString(PyCursesVersion);
@ -661,8 +667,9 @@ PyInit__curses_panel(void)
PyDict_SetItemString(d, "__version__", v); PyDict_SetItemString(d, "__version__", v);
Py_DECREF(v); Py_DECREF(v);
Py_INCREF(_curses_panelstate(m)->PyCursesPanel_Type); Py_INCREF(get_curses_panelstate(m)->PyCursesPanel_Type);
PyModule_AddObject(m, "panel", (PyObject *)_curses_panelstate(m)->PyCursesPanel_Type); PyModule_AddObject(m, "panel",
(PyObject *)get_curses_panelstate(m)->PyCursesPanel_Type);
return m; return m;
fail: fail:
Py_XDECREF(m); Py_XDECREF(m);

View File

@ -101,7 +101,13 @@ static struct PyModuleDef elementtreemodule;
/* Given a module object (assumed to be _elementtree), get its per-module /* Given a module object (assumed to be _elementtree), get its per-module
* state. * 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 /* Find the module instance imported in the currently running sub-interpreter
* and get its state. * and get its state.
@ -112,7 +118,7 @@ static struct PyModuleDef elementtreemodule;
static int static int
elementtree_clear(PyObject *m) elementtree_clear(PyObject *m)
{ {
elementtreestate *st = ET_STATE(m); elementtreestate *st = get_elementtree_state(m);
Py_CLEAR(st->parseerror_obj); Py_CLEAR(st->parseerror_obj);
Py_CLEAR(st->deepcopy_obj); Py_CLEAR(st->deepcopy_obj);
Py_CLEAR(st->elementpath_obj); Py_CLEAR(st->elementpath_obj);
@ -124,7 +130,7 @@ elementtree_clear(PyObject *m)
static int static int
elementtree_traverse(PyObject *m, visitproc visit, void *arg) 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->parseerror_obj);
Py_VISIT(st->deepcopy_obj); Py_VISIT(st->deepcopy_obj);
Py_VISIT(st->elementpath_obj); Py_VISIT(st->elementpath_obj);
@ -4377,7 +4383,7 @@ PyInit__elementtree(void)
m = PyModule_Create(&elementtreemodule); m = PyModule_Create(&elementtreemodule);
if (!m) if (!m)
return NULL; return NULL;
st = ET_STATE(m); st = get_elementtree_state(m);
if (!(temp = PyImport_ImportModule("copy"))) if (!(temp = PyImport_ImportModule("copy")))
return NULL; return NULL;

View File

@ -52,7 +52,14 @@ typedef struct {
PyTypeObject *EVPtype; PyTypeObject *EVPtype;
} _hashlibstate; } _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))) #define _hashlibstate_global ((_hashlibstate *)PyModule_GetState(PyState_FindModule(&_hashlibmodule)))
@ -1112,7 +1119,7 @@ static struct PyMethodDef EVP_functions[] = {
static int static int
hashlib_traverse(PyObject *m, visitproc visit, void *arg) hashlib_traverse(PyObject *m, visitproc visit, void *arg)
{ {
_hashlibstate *state = _hashlibstate(m); _hashlibstate *state = get_hashlib_state(m);
Py_VISIT(state->EVPtype); Py_VISIT(state->EVPtype);
return 0; return 0;
} }
@ -1120,7 +1127,7 @@ hashlib_traverse(PyObject *m, visitproc visit, void *arg)
static int static int
hashlib_clear(PyObject *m) hashlib_clear(PyObject *m)
{ {
_hashlibstate *state = _hashlibstate(m); _hashlibstate *state = get_hashlib_state(m);
Py_CLEAR(state->EVPtype); Py_CLEAR(state->EVPtype);
return 0; return 0;
} }
@ -1168,7 +1175,7 @@ PyInit__hashlib(void)
PyTypeObject *EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec); PyTypeObject *EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
if (EVPtype == NULL) if (EVPtype == NULL)
return NULL; return NULL;
_hashlibstate(m)->EVPtype = EVPtype; get_hashlib_state(m)->EVPtype = EVPtype;
openssl_md_meth_names = generate_hash_name_list(); openssl_md_meth_names = generate_hash_name_list();
if (openssl_md_meth_names == NULL) { if (openssl_md_meth_names == NULL) {
@ -1180,8 +1187,8 @@ PyInit__hashlib(void)
return NULL; return NULL;
} }
Py_INCREF((PyObject *)_hashlibstate(m)->EVPtype); Py_INCREF((PyObject *)get_hashlib_state(m)->EVPtype);
PyModule_AddObject(m, "HASH", (PyObject *)_hashlibstate(m)->EVPtype); PyModule_AddObject(m, "HASH", (PyObject *)get_hashlib_state(m)->EVPtype);
PyState_AddModule(m, &_hashlibmodule); PyState_AddModule(m, &_hashlibmodule);
return m; return m;

View File

@ -573,13 +573,20 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err)
return result; 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_State *
_PyIO_get_module_state(void) _PyIO_get_module_state(void)
{ {
PyObject *mod = PyState_FindModule(&_PyIO_Module); PyObject *mod = PyState_FindModule(&_PyIO_Module);
_PyIO_State *state; _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, PyErr_SetString(PyExc_RuntimeError,
"could not find io module state " "could not find io module state "
"(interpreter shutdown?)"); "(interpreter shutdown?)");
@ -615,7 +622,7 @@ _PyIO_get_locale_module(_PyIO_State *state)
static int static int
iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { 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) if (!state->initialized)
return 0; return 0;
if (state->locale_module != NULL) { if (state->locale_module != NULL) {
@ -628,7 +635,7 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
static int static int
iomodule_clear(PyObject *mod) { iomodule_clear(PyObject *mod) {
_PyIO_State *state = IO_MOD_STATE(mod); _PyIO_State *state = get_io_state(mod);
if (!state->initialized) if (!state->initialized)
return 0; return 0;
if (state->locale_module != NULL) if (state->locale_module != NULL)
@ -674,7 +681,7 @@ PyInit__io(void)
_PyIO_State *state = NULL; _PyIO_State *state = NULL;
if (m == NULL) if (m == NULL)
return NULL; return NULL;
state = IO_MOD_STATE(m); state = get_io_state(m);
state->initialized = 0; state->initialized = 0;
#define ADD_TYPE(type, name) \ #define ADD_TYPE(type, name) \

View File

@ -68,8 +68,15 @@ typedef struct {
static struct PyModuleDef _posixsubprocessmodule; static struct PyModuleDef _posixsubprocessmodule;
#define _posixsubprocessstate(o) ((_posixsubprocessstate *)PyModule_GetState(o)) static inline _posixsubprocessstate*
#define _posixsubprocessstate_global _posixsubprocessstate(PyState_FindModule(&_posixsubprocessmodule)) 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. */ /* If gc was disabled, call gc.enable(). Return 0 on success. */
static int static int
@ -944,16 +951,16 @@ static PyMethodDef module_methods[] = {
static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) { static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(_posixsubprocessstate(m)->disable); Py_VISIT(get_posixsubprocess_state(m)->disable);
Py_VISIT(_posixsubprocessstate(m)->enable); Py_VISIT(get_posixsubprocess_state(m)->enable);
Py_VISIT(_posixsubprocessstate(m)->isenabled); Py_VISIT(get_posixsubprocess_state(m)->isenabled);
return 0; return 0;
} }
static int _posixsubprocess_clear(PyObject *m) { static int _posixsubprocess_clear(PyObject *m) {
Py_CLEAR(_posixsubprocessstate(m)->disable); Py_CLEAR(get_posixsubprocess_state(m)->disable);
Py_CLEAR(_posixsubprocessstate(m)->enable); Py_CLEAR(get_posixsubprocess_state(m)->enable);
Py_CLEAR(_posixsubprocessstate(m)->isenabled); Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
return 0; return 0;
} }
@ -989,9 +996,9 @@ PyInit__posixsubprocess(void)
return NULL; return NULL;
} }
_posixsubprocessstate(m)->disable = PyUnicode_InternFromString("disable"); get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
_posixsubprocessstate(m)->enable = PyUnicode_InternFromString("enable"); get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
_posixsubprocessstate(m)->isenabled = PyUnicode_InternFromString("isenabled"); get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
PyState_AddModule(m, &_posixsubprocessmodule); PyState_AddModule(m, &_posixsubprocessmodule);
return m; return m;

View File

@ -84,11 +84,17 @@ typedef struct {
PyObject *Long___abs__; PyObject *Long___abs__;
} _randomstate; } _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; static struct PyModuleDef _randommodule;
#define _randomstate_global _randomstate(PyState_FindModule(&_randommodule)) #define _randomstate_global get_random_state(PyState_FindModule(&_randommodule))
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
@ -561,15 +567,15 @@ PyDoc_STRVAR(module_doc,
static int static int
_random_traverse(PyObject *module, visitproc visit, void *arg) _random_traverse(PyObject *module, visitproc visit, void *arg)
{ {
Py_VISIT(_randomstate(module)->Random_Type); Py_VISIT(get_random_state(module)->Random_Type);
return 0; return 0;
} }
static int static int
_random_clear(PyObject *module) _random_clear(PyObject *module)
{ {
Py_CLEAR(_randomstate(module)->Random_Type); Py_CLEAR(get_random_state(module)->Random_Type);
Py_CLEAR(_randomstate(module)->Long___abs__); Py_CLEAR(get_random_state(module)->Long___abs__);
return 0; return 0;
} }
@ -606,7 +612,7 @@ PyInit__random(void)
Py_DECREF(Random_Type); Py_DECREF(Random_Type);
return NULL; return NULL;
} }
_randomstate(m)->Random_Type = Random_Type; get_random_state(m)->Random_Type = Random_Type;
Py_INCREF(Random_Type); Py_INCREF(Random_Type);
PyModule_AddObject(m, "Random", Random_Type); PyModule_AddObject(m, "Random", Random_Type);
@ -624,7 +630,7 @@ PyInit__random(void)
Py_DECREF(longtype); Py_DECREF(longtype);
Py_DECREF(longval); Py_DECREF(longval);
_randomstate(m)->Long___abs__ = abs; get_random_state(m)->Long___abs__ = abs;
return m; return m;

View File

@ -20,11 +20,17 @@ typedef struct {
PyObject *StructError; PyObject *StructError;
} _structmodulestate; } _structmodulestate;
#define _structmodulestate(o) ((_structmodulestate *)PyModule_GetState(o)) static inline _structmodulestate*
get_struct_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_structmodulestate *)state;
}
static struct PyModuleDef _structmodule; static struct PyModuleDef _structmodule;
#define _structmodulestate_global _structmodulestate(PyState_FindModule(&_structmodule)) #define _structmodulestate_global get_struct_state(PyState_FindModule(&_structmodule))
/* The translation function for each format character is table driven */ /* The translation function for each format character is table driven */
typedef struct _formatdef { typedef struct _formatdef {
@ -2310,18 +2316,24 @@ The variable struct.error is an exception raised on errors.\n");
static int static int
_structmodule_traverse(PyObject *module, visitproc visit, void *arg) _structmodule_traverse(PyObject *module, visitproc visit, void *arg)
{ {
Py_VISIT(_structmodulestate(module)->PyStructType); _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module);
Py_VISIT(_structmodulestate(module)->unpackiter_type); if (state) {
Py_VISIT(_structmodulestate(module)->StructError); Py_VISIT(state->PyStructType);
Py_VISIT(state->unpackiter_type);
Py_VISIT(state->StructError);
}
return 0; return 0;
} }
static int static int
_structmodule_clear(PyObject *module) _structmodule_clear(PyObject *module)
{ {
Py_CLEAR(_structmodulestate(module)->PyStructType); _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module);
Py_CLEAR(_structmodulestate(module)->unpackiter_type); if (state) {
Py_CLEAR(_structmodulestate(module)->StructError); Py_CLEAR(state->PyStructType);
Py_CLEAR(state->unpackiter_type);
Py_CLEAR(state->StructError);
}
return 0; return 0;
} }
@ -2358,13 +2370,13 @@ PyInit__struct(void)
} }
Py_INCREF(PyStructType); Py_INCREF(PyStructType);
PyModule_AddObject(m, "Struct", PyStructType); PyModule_AddObject(m, "Struct", PyStructType);
_structmodulestate(m)->PyStructType = PyStructType; get_struct_state(m)->PyStructType = PyStructType;
PyObject *unpackiter_type = PyType_FromSpec(&unpackiter_type_spec); PyObject *unpackiter_type = PyType_FromSpec(&unpackiter_type_spec);
if (unpackiter_type == NULL) { if (unpackiter_type == NULL) {
return NULL; return NULL;
} }
_structmodulestate(m)->unpackiter_type = unpackiter_type; get_struct_state(m)->unpackiter_type = unpackiter_type;
/* Check endian and swap in faster functions */ /* Check endian and swap in faster functions */
{ {
@ -2411,7 +2423,7 @@ PyInit__struct(void)
return NULL; return NULL;
Py_INCREF(StructError); Py_INCREF(StructError);
PyModule_AddObject(m, "error", StructError); PyModule_AddObject(m, "error", StructError);
_structmodulestate(m)->StructError = StructError; get_struct_state(m)->StructError = StructError;
return m; return m;
} }

View File

@ -28,7 +28,13 @@ typedef struct {
int callback_len; int callback_len;
} atexitmodule_state; } atexitmodule_state;
#define GET_ATEXIT_STATE(mod) ((atexitmodule_state*)PyModule_GetState(mod)) static inline atexitmodule_state*
get_atexit_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (atexitmodule_state *)state;
}
static void static void
@ -72,7 +78,7 @@ atexit_callfuncs(PyObject *module)
if (module == NULL) if (module == NULL)
return; return;
modstate = GET_ATEXIT_STATE(module); modstate = get_atexit_state(module);
if (modstate->ncallbacks == 0) if (modstate->ncallbacks == 0)
return; return;
@ -130,7 +136,7 @@ atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
atexit_callback *new_callback; atexit_callback *new_callback;
PyObject *func = NULL; PyObject *func = NULL;
modstate = GET_ATEXIT_STATE(self); modstate = get_atexit_state(self);
if (modstate->ncallbacks >= modstate->callback_len) { if (modstate->ncallbacks >= modstate->callback_len) {
atexit_callback **r; atexit_callback **r;
@ -197,7 +203,7 @@ Clear the list of previously registered exit functions.");
static PyObject * static PyObject *
atexit_clear(PyObject *self, PyObject *unused) atexit_clear(PyObject *self, PyObject *unused)
{ {
atexit_cleanup(GET_ATEXIT_STATE(self)); atexit_cleanup(get_atexit_state(self));
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -211,7 +217,7 @@ atexit_ncallbacks(PyObject *self, PyObject *unused)
{ {
atexitmodule_state *modstate; atexitmodule_state *modstate;
modstate = GET_ATEXIT_STATE(self); modstate = get_atexit_state(self);
return PyLong_FromSsize_t(modstate->ncallbacks); return PyLong_FromSsize_t(modstate->ncallbacks);
} }
@ -222,7 +228,7 @@ atexit_m_traverse(PyObject *self, visitproc visit, void *arg)
int i; int i;
atexitmodule_state *modstate; atexitmodule_state *modstate;
modstate = GET_ATEXIT_STATE(self); modstate = (atexitmodule_state *)PyModule_GetState(self);
if (modstate != NULL) { if (modstate != NULL) {
for (i = 0; i < modstate->ncallbacks; i++) { for (i = 0; i < modstate->ncallbacks; i++) {
atexit_callback *cb = modstate->atexit_callbacks[i]; atexit_callback *cb = modstate->atexit_callbacks[i];
@ -240,7 +246,7 @@ static int
atexit_m_clear(PyObject *self) atexit_m_clear(PyObject *self)
{ {
atexitmodule_state *modstate; atexitmodule_state *modstate;
modstate = GET_ATEXIT_STATE(self); modstate = (atexitmodule_state *)PyModule_GetState(self);
if (modstate != NULL) { if (modstate != NULL) {
atexit_cleanup(modstate); atexit_cleanup(modstate);
} }
@ -251,7 +257,7 @@ static void
atexit_free(PyObject *m) atexit_free(PyObject *m)
{ {
atexitmodule_state *modstate; atexitmodule_state *modstate;
modstate = GET_ATEXIT_STATE(m); modstate = (atexitmodule_state *)PyModule_GetState(m);
if (modstate != NULL) { if (modstate != NULL) {
atexit_cleanup(modstate); atexit_cleanup(modstate);
PyMem_Free(modstate->atexit_callbacks); PyMem_Free(modstate->atexit_callbacks);
@ -273,7 +279,7 @@ atexit_unregister(PyObject *self, PyObject *func)
atexit_callback *cb; atexit_callback *cb;
int i, eq; int i, eq;
modstate = GET_ATEXIT_STATE(self); modstate = get_atexit_state(self);
for (i = 0; i < modstate->ncallbacks; i++) for (i = 0; i < modstate->ncallbacks; i++)
{ {
@ -318,7 +324,7 @@ static int
atexit_exec(PyObject *m) { atexit_exec(PyObject *m) {
atexitmodule_state *modstate; atexitmodule_state *modstate;
modstate = GET_ATEXIT_STATE(m); modstate = get_atexit_state(m);
modstate->callback_len = 32; modstate->callback_len = 32;
modstate->ncallbacks = 0; modstate->ncallbacks = 0;
modstate->atexit_callbacks = PyMem_New(atexit_callback*, modstate->atexit_callbacks = PyMem_New(atexit_callback*,

View File

@ -37,8 +37,16 @@ static PyStructSequence_Desc struct_group_type_desc = {
typedef struct { typedef struct {
PyTypeObject *StructGrpType; PyTypeObject *StructGrpType;
} grpmodulestate; } grpmodulestate;
#define modulestate(o) ((grpmodulestate *)PyModule_GetState(o))
#define modulestate_global modulestate(PyState_FindModule(&grpmodule)) static inline grpmodulestate*
get_grp_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (grpmodulestate *)state;
}
#define modulestate_global get_grp_state(PyState_FindModule(&grpmodule))
static struct PyModuleDef grpmodule; static struct PyModuleDef grpmodule;
@ -320,12 +328,12 @@ according to the password database. Check both databases to get\n\
complete membership information.)"); complete membership information.)");
static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) { static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(modulestate(m)->StructGrpType); Py_VISIT(get_grp_state(m)->StructGrpType);
return 0; return 0;
} }
static int grpmodule_clear(PyObject *m) { static int grpmodule_clear(PyObject *m) {
Py_CLEAR(modulestate(m)->StructGrpType); Py_CLEAR(get_grp_state(m)->StructGrpType);
return 0; return 0;
} }

View File

@ -842,7 +842,14 @@ typedef struct {
static struct PyModuleDef posixmodule; static struct PyModuleDef posixmodule;
#define _posixstate(o) ((_posixstate *)PyModule_GetState(o)) static inline _posixstate*
get_posix_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_posixstate *)state;
}
#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule))) #define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
/* /*
@ -2103,48 +2110,48 @@ statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int static int
_posix_clear(PyObject *module) _posix_clear(PyObject *module)
{ {
Py_CLEAR(_posixstate(module)->billion); Py_CLEAR(get_posix_state(module)->billion);
Py_CLEAR(_posixstate(module)->DirEntryType); Py_CLEAR(get_posix_state(module)->DirEntryType);
Py_CLEAR(_posixstate(module)->ScandirIteratorType); Py_CLEAR(get_posix_state(module)->ScandirIteratorType);
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Py_CLEAR(_posixstate(module)->SchedParamType); Py_CLEAR(get_posix_state(module)->SchedParamType);
#endif #endif
Py_CLEAR(_posixstate(module)->StatResultType); Py_CLEAR(get_posix_state(module)->StatResultType);
Py_CLEAR(_posixstate(module)->StatVFSResultType); Py_CLEAR(get_posix_state(module)->StatVFSResultType);
Py_CLEAR(_posixstate(module)->TerminalSizeType); Py_CLEAR(get_posix_state(module)->TerminalSizeType);
Py_CLEAR(_posixstate(module)->TimesResultType); Py_CLEAR(get_posix_state(module)->TimesResultType);
Py_CLEAR(_posixstate(module)->UnameResultType); Py_CLEAR(get_posix_state(module)->UnameResultType);
#if defined(HAVE_WAITID) && !defined(__APPLE__) #if defined(HAVE_WAITID) && !defined(__APPLE__)
Py_CLEAR(_posixstate(module)->WaitidResultType); Py_CLEAR(get_posix_state(module)->WaitidResultType);
#endif #endif
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Py_CLEAR(_posixstate(module)->struct_rusage); Py_CLEAR(get_posix_state(module)->struct_rusage);
#endif #endif
Py_CLEAR(_posixstate(module)->st_mode); Py_CLEAR(get_posix_state(module)->st_mode);
return 0; return 0;
} }
static int static int
_posix_traverse(PyObject *module, visitproc visit, void *arg) _posix_traverse(PyObject *module, visitproc visit, void *arg)
{ {
Py_VISIT(_posixstate(module)->billion); Py_VISIT(get_posix_state(module)->billion);
Py_VISIT(_posixstate(module)->DirEntryType); Py_VISIT(get_posix_state(module)->DirEntryType);
Py_VISIT(_posixstate(module)->ScandirIteratorType); Py_VISIT(get_posix_state(module)->ScandirIteratorType);
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Py_VISIT(_posixstate(module)->SchedParamType); Py_VISIT(get_posix_state(module)->SchedParamType);
#endif #endif
Py_VISIT(_posixstate(module)->StatResultType); Py_VISIT(get_posix_state(module)->StatResultType);
Py_VISIT(_posixstate(module)->StatVFSResultType); Py_VISIT(get_posix_state(module)->StatVFSResultType);
Py_VISIT(_posixstate(module)->TerminalSizeType); Py_VISIT(get_posix_state(module)->TerminalSizeType);
Py_VISIT(_posixstate(module)->TimesResultType); Py_VISIT(get_posix_state(module)->TimesResultType);
Py_VISIT(_posixstate(module)->UnameResultType); Py_VISIT(get_posix_state(module)->UnameResultType);
#if defined(HAVE_WAITID) && !defined(__APPLE__) #if defined(HAVE_WAITID) && !defined(__APPLE__)
Py_VISIT(_posixstate(module)->WaitidResultType); Py_VISIT(get_posix_state(module)->WaitidResultType);
#endif #endif
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Py_VISIT(_posixstate(module)->struct_rusage); Py_VISIT(get_posix_state(module)->struct_rusage);
#endif #endif
Py_VISIT(_posixstate(module)->st_mode); Py_VISIT(get_posix_state(module)->st_mode);
return 0; return 0;
} }
@ -4673,7 +4680,7 @@ os_uname_impl(PyObject *module)
if (res < 0) if (res < 0)
return posix_error(); return posix_error();
PyObject *UnameResultType = _posixstate(module)->UnameResultType; PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
value = PyStructSequence_New((PyTypeObject *)UnameResultType); value = PyStructSequence_New((PyTypeObject *)UnameResultType);
if (value == NULL) if (value == NULL)
return NULL; return NULL;
@ -7840,7 +7847,7 @@ os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
if (si.si_pid == 0) if (si.si_pid == 0)
Py_RETURN_NONE; Py_RETURN_NONE;
PyObject *WaitidResultType = _posixstate(module)->WaitidResultType; PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
result = PyStructSequence_New((PyTypeObject *)WaitidResultType); result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
if (!result) if (!result)
return NULL; return NULL;
@ -12407,7 +12414,7 @@ get_terminal_size(PyObject *self, PyObject *args)
} }
#endif /* TERMSIZE_USE_CONIO */ #endif /* TERMSIZE_USE_CONIO */
PyObject *TerminalSizeType = _posixstate(self)->TerminalSizeType; PyObject *TerminalSizeType = get_posix_state(self)->TerminalSizeType;
termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
if (termsize == NULL) if (termsize == NULL)
return NULL; return NULL;
@ -13425,7 +13432,7 @@ os_scandir_impl(PyObject *module, path_t *path)
return NULL; return NULL;
} }
PyObject *ScandirIteratorType = _posixstate(module)->ScandirIteratorType; PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
if (!iterator) if (!iterator)
return NULL; return NULL;
@ -14643,7 +14650,7 @@ INITFUNC(void)
} }
Py_INCREF(WaitidResultType); Py_INCREF(WaitidResultType);
PyModule_AddObject(m, "waitid_result", WaitidResultType); PyModule_AddObject(m, "waitid_result", WaitidResultType);
_posixstate(m)->WaitidResultType = WaitidResultType; get_posix_state(m)->WaitidResultType = WaitidResultType;
#endif #endif
stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
@ -14656,7 +14663,7 @@ INITFUNC(void)
} }
Py_INCREF(StatResultType); Py_INCREF(StatResultType);
PyModule_AddObject(m, "stat_result", StatResultType); PyModule_AddObject(m, "stat_result", StatResultType);
_posixstate(m)->StatResultType = StatResultType; get_posix_state(m)->StatResultType = StatResultType;
structseq_new = ((PyTypeObject *)StatResultType)->tp_new; structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
((PyTypeObject *)StatResultType)->tp_new = statresult_new; ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
@ -14667,7 +14674,7 @@ INITFUNC(void)
} }
Py_INCREF(StatVFSResultType); Py_INCREF(StatVFSResultType);
PyModule_AddObject(m, "statvfs_result", StatVFSResultType); PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
_posixstate(m)->StatVFSResultType = StatVFSResultType; get_posix_state(m)->StatVFSResultType = StatVFSResultType;
#ifdef NEED_TICKS_PER_SECOND #ifdef NEED_TICKS_PER_SECOND
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK); ticks_per_second = sysconf(_SC_CLK_TCK);
@ -14686,7 +14693,7 @@ INITFUNC(void)
} }
Py_INCREF(SchedParamType); Py_INCREF(SchedParamType);
PyModule_AddObject(m, "sched_param", SchedParamType); PyModule_AddObject(m, "sched_param", SchedParamType);
_posixstate(m)->SchedParamType = SchedParamType; get_posix_state(m)->SchedParamType = SchedParamType;
((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
#endif #endif
@ -14697,14 +14704,14 @@ INITFUNC(void)
} }
Py_INCREF(TerminalSizeType); Py_INCREF(TerminalSizeType);
PyModule_AddObject(m, "terminal_size", TerminalSizeType); PyModule_AddObject(m, "terminal_size", TerminalSizeType);
_posixstate(m)->TerminalSizeType = TerminalSizeType; get_posix_state(m)->TerminalSizeType = TerminalSizeType;
/* initialize scandir types */ /* initialize scandir types */
PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec); PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
if (ScandirIteratorType == NULL) { if (ScandirIteratorType == NULL) {
return NULL; return NULL;
} }
_posixstate(m)->ScandirIteratorType = ScandirIteratorType; get_posix_state(m)->ScandirIteratorType = ScandirIteratorType;
PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec); PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
if (DirEntryType == NULL) { if (DirEntryType == NULL) {
@ -14712,7 +14719,7 @@ INITFUNC(void)
} }
Py_INCREF(DirEntryType); Py_INCREF(DirEntryType);
PyModule_AddObject(m, "DirEntry", DirEntryType); PyModule_AddObject(m, "DirEntry", DirEntryType);
_posixstate(m)->DirEntryType = DirEntryType; get_posix_state(m)->DirEntryType = DirEntryType;
times_result_desc.name = MODNAME ".times_result"; times_result_desc.name = MODNAME ".times_result";
PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc); PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
@ -14721,7 +14728,7 @@ INITFUNC(void)
} }
Py_INCREF(TimesResultType); Py_INCREF(TimesResultType);
PyModule_AddObject(m, "times_result", TimesResultType); PyModule_AddObject(m, "times_result", TimesResultType);
_posixstate(m)->TimesResultType = TimesResultType; get_posix_state(m)->TimesResultType = TimesResultType;
PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
if (UnameResultType == NULL) { if (UnameResultType == NULL) {
@ -14729,7 +14736,7 @@ INITFUNC(void)
} }
Py_INCREF(UnameResultType); Py_INCREF(UnameResultType);
PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
_posixstate(m)->UnameResultType = (PyObject *)UnameResultType; get_posix_state(m)->UnameResultType = (PyObject *)UnameResultType;
#ifdef __APPLE__ #ifdef __APPLE__
/* /*
@ -14769,15 +14776,15 @@ INITFUNC(void)
#endif /* __APPLE__ */ #endif /* __APPLE__ */
if ((_posixstate(m)->billion = PyLong_FromLong(1000000000)) == NULL) if ((get_posix_state(m)->billion = PyLong_FromLong(1000000000)) == NULL)
return NULL; return NULL;
#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
_posixstate(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage"); get_posix_state(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
if (_posixstate(m)->struct_rusage == NULL) if (get_posix_state(m)->struct_rusage == NULL)
return NULL; return NULL;
#endif #endif
_posixstate(m)->st_mode = PyUnicode_InternFromString("st_mode"); get_posix_state(m)->st_mode = PyUnicode_InternFromString("st_mode");
if (_posixstate(m)->st_mode == NULL) if (get_posix_state(m)->st_mode == NULL)
return NULL; return NULL;
/* suppress "function not used" warnings */ /* suppress "function not used" warnings */

View File

@ -50,8 +50,16 @@ exception is raised if the entry asked for cannot be found.");
typedef struct { typedef struct {
PyTypeObject *StructPwdType; PyTypeObject *StructPwdType;
} pwdmodulestate; } pwdmodulestate;
#define modulestate(o) ((pwdmodulestate *)PyModule_GetState(o))
#define modulestate_global modulestate(PyState_FindModule(&pwdmodule)) static inline pwdmodulestate*
get_pwd_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (pwdmodulestate *)state;
}
#define modulestate_global get_pwd_state(PyState_FindModule(&pwdmodule))
static struct PyModuleDef pwdmodule; static struct PyModuleDef pwdmodule;
@ -316,11 +324,11 @@ static PyMethodDef pwd_methods[] = {
}; };
static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) { static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(modulestate(m)->StructPwdType); Py_VISIT(get_pwd_state(m)->StructPwdType);
return 0; return 0;
} }
static int pwdmodule_clear(PyObject *m) { static int pwdmodule_clear(PyObject *m) {
Py_CLEAR(modulestate(m)->StructPwdType); Py_CLEAR(get_pwd_state(m)->StructPwdType);
return 0; return 0;
} }
static void pwdmodule_free(void *m) { static void pwdmodule_free(void *m) {

View File

@ -86,13 +86,18 @@ typedef struct {
PyObject *endidx; PyObject *endidx;
} readlinestate; } readlinestate;
static inline readlinestate*
#define readline_state(o) ((readlinestate *)PyModule_GetState(o)) get_readline_state(PyModule *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (readlinestate *)state;
}
static int static int
readline_clear(PyObject *m) readline_clear(PyObject *m)
{ {
readlinestate *state = readline_state(m); readlinestate *state = get_readline_state(m);
Py_CLEAR(state->completion_display_matches_hook); Py_CLEAR(state->completion_display_matches_hook);
Py_CLEAR(state->startup_hook); Py_CLEAR(state->startup_hook);
Py_CLEAR(state->pre_input_hook); Py_CLEAR(state->pre_input_hook);
@ -105,7 +110,7 @@ readline_clear(PyObject *m)
static int static int
readline_traverse(PyObject *m, visitproc visit, void *arg) readline_traverse(PyObject *m, visitproc visit, void *arg)
{ {
readlinestate *state = readline_state(m); readlinestate *state = get_readline_state(m);
Py_VISIT(state->completion_display_matches_hook); Py_VISIT(state->completion_display_matches_hook);
Py_VISIT(state->startup_hook); Py_VISIT(state->startup_hook);
Py_VISIT(state->pre_input_hook); Py_VISIT(state->pre_input_hook);

View File

@ -69,8 +69,15 @@ typedef struct {
static struct PyModuleDef selectmodule; static struct PyModuleDef selectmodule;
#define _selectstate(o) ((_selectstate *)PyModule_GetState(o)) static inline _selectstate*
#define _selectstate_global _selectstate(PyState_FindModule(&selectmodule)) get_select_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_selectstate *)state;
}
#define _selectstate_global get_select_state(PyState_FindModule(&selectmodule))
/*[clinic input] /*[clinic input]
module select module select
@ -2399,24 +2406,24 @@ On Windows, only sockets are supported; on Unix, all file descriptors.");
static int static int
_select_traverse(PyObject *module, visitproc visit, void *arg) _select_traverse(PyObject *module, visitproc visit, void *arg)
{ {
Py_VISIT(_selectstate(module)->close); Py_VISIT(get_select_state(module)->close);
Py_VISIT(_selectstate(module)->poll_Type); Py_VISIT(get_select_state(module)->poll_Type);
Py_VISIT(_selectstate(module)->devpoll_Type); Py_VISIT(get_select_state(module)->devpoll_Type);
Py_VISIT(_selectstate(module)->pyEpoll_Type); Py_VISIT(get_select_state(module)->pyEpoll_Type);
Py_VISIT(_selectstate(module)->kqueue_event_Type); Py_VISIT(get_select_state(module)->kqueue_event_Type);
Py_VISIT(_selectstate(module)->kqueue_queue_Type); Py_VISIT(get_select_state(module)->kqueue_queue_Type);
return 0; return 0;
} }
static int static int
_select_clear(PyObject *module) _select_clear(PyObject *module)
{ {
Py_CLEAR(_selectstate(module)->close); Py_CLEAR(get_select_state(module)->close);
Py_CLEAR(_selectstate(module)->poll_Type); Py_CLEAR(get_select_state(module)->poll_Type);
Py_CLEAR(_selectstate(module)->devpoll_Type); Py_CLEAR(get_select_state(module)->devpoll_Type);
Py_CLEAR(_selectstate(module)->pyEpoll_Type); Py_CLEAR(get_select_state(module)->pyEpoll_Type);
Py_CLEAR(_selectstate(module)->kqueue_event_Type); Py_CLEAR(get_select_state(module)->kqueue_event_Type);
Py_CLEAR(_selectstate(module)->kqueue_queue_Type); Py_CLEAR(get_select_state(module)->kqueue_queue_Type);
return 0; return 0;
} }
@ -2446,7 +2453,7 @@ PyInit_select(void)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
_selectstate(m)->close = PyUnicode_InternFromString("close"); get_select_state(m)->close = PyUnicode_InternFromString("close");
Py_INCREF(PyExc_OSError); Py_INCREF(PyExc_OSError);
PyModule_AddObject(m, "error", PyExc_OSError); PyModule_AddObject(m, "error", PyExc_OSError);
@ -2472,7 +2479,7 @@ PyInit_select(void)
PyObject *poll_Type = PyType_FromSpec(&poll_Type_spec); PyObject *poll_Type = PyType_FromSpec(&poll_Type_spec);
if (poll_Type == NULL) if (poll_Type == NULL)
return NULL; return NULL;
_selectstate(m)->poll_Type = (PyTypeObject *)poll_Type; get_select_state(m)->poll_Type = (PyTypeObject *)poll_Type;
Py_INCREF(poll_Type); Py_INCREF(poll_Type);
PyModule_AddIntMacro(m, POLLIN); PyModule_AddIntMacro(m, POLLIN);
@ -2508,7 +2515,7 @@ PyInit_select(void)
PyObject *devpoll_Type = PyType_FromSpec(&devpoll_Type_spec); PyObject *devpoll_Type = PyType_FromSpec(&devpoll_Type_spec);
if (devpoll_Type == NULL) if (devpoll_Type == NULL)
return NULL; return NULL;
_selectstate(m)->devpoll_Type = (PyTypeObject *)devpoll_Type; get_select_state(m)->devpoll_Type = (PyTypeObject *)devpoll_Type;
Py_INCREF(devpoll_Type); Py_INCREF(devpoll_Type);
#endif #endif
@ -2516,9 +2523,9 @@ PyInit_select(void)
PyObject *pyEpoll_Type = PyType_FromSpec(&pyEpoll_Type_spec); PyObject *pyEpoll_Type = PyType_FromSpec(&pyEpoll_Type_spec);
if (pyEpoll_Type == NULL) if (pyEpoll_Type == NULL)
return NULL; return NULL;
_selectstate(m)->pyEpoll_Type = (PyTypeObject *)pyEpoll_Type; get_select_state(m)->pyEpoll_Type = (PyTypeObject *)pyEpoll_Type;
Py_INCREF(pyEpoll_Type); Py_INCREF(pyEpoll_Type);
PyModule_AddObject(m, "epoll", (PyObject *)_selectstate(m)->pyEpoll_Type); PyModule_AddObject(m, "epoll", (PyObject *)get_select_state(m)->pyEpoll_Type);
PyModule_AddIntMacro(m, EPOLLIN); PyModule_AddIntMacro(m, EPOLLIN);
PyModule_AddIntMacro(m, EPOLLOUT); PyModule_AddIntMacro(m, EPOLLOUT);
@ -2563,15 +2570,15 @@ PyInit_select(void)
PyObject *kqueue_event_Type = PyType_FromSpec(&kqueue_event_Type_spec); PyObject *kqueue_event_Type = PyType_FromSpec(&kqueue_event_Type_spec);
if (kqueue_event_Type == NULL) if (kqueue_event_Type == NULL)
return NULL; return NULL;
_selectstate(m)->kqueue_event_Type = (PyTypeObject *)kqueue_event_Type; get_select_state(m)->kqueue_event_Type = (PyTypeObject *)kqueue_event_Type;
Py_INCREF(_selectstate(m)->kqueue_event_Type); Py_INCREF(get_select_state(m)->kqueue_event_Type);
PyModule_AddObject(m, "kevent", kqueue_event_Type); PyModule_AddObject(m, "kevent", kqueue_event_Type);
PyObject *kqueue_queue_Type = PyType_FromSpec(&kqueue_queue_Type_spec); PyObject *kqueue_queue_Type = PyType_FromSpec(&kqueue_queue_Type_spec);
if (kqueue_queue_Type == NULL) if (kqueue_queue_Type == NULL)
return NULL; return NULL;
_selectstate(m)->kqueue_queue_Type = (PyTypeObject *)kqueue_queue_Type; get_select_state(m)->kqueue_queue_Type = (PyTypeObject *)kqueue_queue_Type;
Py_INCREF(_selectstate(m)->kqueue_queue_Type); Py_INCREF(get_select_state(m)->kqueue_queue_Type);
PyModule_AddObject(m, "kqueue", kqueue_queue_Type); PyModule_AddObject(m, "kqueue", kqueue_queue_Type);
/* event filters */ /* event filters */

View File

@ -42,8 +42,16 @@ sys.stdin.fileno(), or a file object, such as sys.stdin itself.");
typedef struct { typedef struct {
PyObject *TermiosError; PyObject *TermiosError;
} termiosmodulestate; } termiosmodulestate;
#define modulestate(o) ((termiosmodulestate *)PyModule_GetState(o))
#define modulestate_global modulestate(PyState_FindModule(&termiosmodule)) static inline termiosmodulestate*
get_termios_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (termiosmodulestate *)state;
}
#define modulestate_global get_termios_state(PyState_FindModule(&termiosmodule))
static int fdconv(PyObject* obj, void* p) static int fdconv(PyObject* obj, void* p)
{ {
@ -976,12 +984,12 @@ static struct constant {
}; };
static int termiosmodule_traverse(PyObject *m, visitproc visit, void *arg) { static int termiosmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(modulestate(m)->TermiosError); Py_VISIT(get_termios_state(m)->TermiosError);
return 0; return 0;
} }
static int termiosmodule_clear(PyObject *m) { static int termiosmodule_clear(PyObject *m) {
Py_CLEAR(modulestate(m)->TermiosError); Py_CLEAR(get_termios_state(m)->TermiosError);
return 0; return 0;
} }
@ -1016,7 +1024,7 @@ PyInit_termios(void)
return NULL; return NULL;
} }
termiosmodulestate *state = PyModule_GetState(m); termiosmodulestate *state = get_termios_state(m);
state->TermiosError = PyErr_NewException("termios.error", NULL, NULL); state->TermiosError = PyErr_NewException("termios.error", NULL, NULL);
if (state->TermiosError == NULL) { if (state->TermiosError == NULL) {
return NULL; return NULL;

View File

@ -40,7 +40,14 @@ typedef struct {
PyObject *ZlibError; PyObject *ZlibError;
} _zlibstate; } _zlibstate;
#define _zlibstate(o) ((_zlibstate *)PyModule_GetState(o)) static inline _zlibstate*
get_zlib_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (_zlibstate *)state;
}
#define _zlibstate_global ((_zlibstate *)PyModule_GetState(PyState_FindModule(&zlibmodule))) #define _zlibstate_global ((_zlibstate *)PyModule_GetState(PyState_FindModule(&zlibmodule)))
typedef struct typedef struct
@ -1364,7 +1371,7 @@ PyDoc_STRVAR(zlib_module_documentation,
static int static int
zlib_clear(PyObject *m) zlib_clear(PyObject *m)
{ {
_zlibstate *state = _zlibstate(m); _zlibstate *state = get_zlib_state(m);
Py_CLEAR(state->Comptype); Py_CLEAR(state->Comptype);
Py_CLEAR(state->Decomptype); Py_CLEAR(state->Decomptype);
Py_CLEAR(state->ZlibError); Py_CLEAR(state->ZlibError);
@ -1374,7 +1381,7 @@ zlib_clear(PyObject *m)
static int static int
zlib_traverse(PyObject *m, visitproc visit, void *arg) zlib_traverse(PyObject *m, visitproc visit, void *arg)
{ {
_zlibstate *state = _zlibstate(m); _zlibstate *state = get_zlib_state(m);
Py_VISIT(state->Comptype); Py_VISIT(state->Comptype);
Py_VISIT(state->Decomptype); Py_VISIT(state->Decomptype);
Py_VISIT(state->ZlibError); Py_VISIT(state->ZlibError);
@ -1415,18 +1422,18 @@ PyInit_zlib(void)
PyTypeObject *Comptype = (PyTypeObject *)PyType_FromSpec(&Comptype_spec); PyTypeObject *Comptype = (PyTypeObject *)PyType_FromSpec(&Comptype_spec);
if (Comptype == NULL) if (Comptype == NULL)
return NULL; return NULL;
_zlibstate(m)->Comptype = Comptype; get_zlib_state(m)->Comptype = Comptype;
PyTypeObject *Decomptype = (PyTypeObject *)PyType_FromSpec(&Decomptype_spec); PyTypeObject *Decomptype = (PyTypeObject *)PyType_FromSpec(&Decomptype_spec);
if (Decomptype == NULL) if (Decomptype == NULL)
return NULL; return NULL;
_zlibstate(m)->Decomptype = Decomptype; get_zlib_state(m)->Decomptype = Decomptype;
PyObject *ZlibError = PyErr_NewException("zlib.error", NULL, NULL); PyObject *ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (ZlibError != NULL) { if (ZlibError != NULL) {
Py_INCREF(ZlibError); Py_INCREF(ZlibError);
PyModule_AddObject(m, "error", ZlibError); PyModule_AddObject(m, "error", ZlibError);
_zlibstate(m)->ZlibError = ZlibError; get_zlib_state(m)->ZlibError = ZlibError;
} }
PyModule_AddIntMacro(m, MAX_WBITS); PyModule_AddIntMacro(m, MAX_WBITS);
PyModule_AddIntMacro(m, DEFLATED); PyModule_AddIntMacro(m, DEFLATED);