Skip to content

Commit f707d94

Browse files
authored
bpo-39968: Convert extension modules' macros of get_module_state() to inline functions (GH-19017)
1 parent 4ab362c commit f707d94

17 files changed

+279
-164
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use inline function to replace extension modules' get_module_state macros.

Modules/_csv.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,27 @@ typedef struct {
2121
long field_limit; /* max parsed field size */
2222
} _csvstate;
2323

24-
#define _csvstate(o) ((_csvstate *)PyModule_GetState(o))
24+
static inline _csvstate*
25+
get_csv_state(PyObject *module)
26+
{
27+
void *state = PyModule_GetState(module);
28+
assert(state != NULL);
29+
return (_csvstate *)state;
30+
}
2531

2632
static int
2733
_csv_clear(PyObject *m)
2834
{
29-
Py_CLEAR(_csvstate(m)->error_obj);
30-
Py_CLEAR(_csvstate(m)->dialects);
35+
Py_CLEAR(get_csv_state(m)->error_obj);
36+
Py_CLEAR(get_csv_state(m)->dialects);
3137
return 0;
3238
}
3339

3440
static int
3541
_csv_traverse(PyObject *m, visitproc visit, void *arg)
3642
{
37-
Py_VISIT(_csvstate(m)->error_obj);
38-
Py_VISIT(_csvstate(m)->dialects);
43+
Py_VISIT(get_csv_state(m)->error_obj);
44+
Py_VISIT(get_csv_state(m)->dialects);
3945
return 0;
4046
}
4147

@@ -1647,15 +1653,15 @@ PyInit__csv(void)
16471653
return NULL;
16481654

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

16531659
/* Add _dialects dictionary */
1654-
_csvstate(module)->dialects = PyDict_New();
1655-
if (_csvstate(module)->dialects == NULL)
1660+
get_csv_state(module)->dialects = PyDict_New();
1661+
if (get_csv_state(module)->dialects == NULL)
16561662
return NULL;
1657-
Py_INCREF(_csvstate(module)->dialects);
1658-
if (PyModule_AddObject(module, "_dialects", _csvstate(module)->dialects))
1663+
Py_INCREF(get_csv_state(module)->dialects);
1664+
if (PyModule_AddObject(module, "_dialects", get_csv_state(module)->dialects))
16591665
return NULL;
16601666

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

16731679
/* Add the CSV exception object to the module. */
1674-
_csvstate(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
1675-
if (_csvstate(module)->error_obj == NULL)
1680+
get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
1681+
if (get_csv_state(module)->error_obj == NULL)
16761682
return NULL;
1677-
Py_INCREF(_csvstate(module)->error_obj);
1678-
PyModule_AddObject(module, "Error", _csvstate(module)->error_obj);
1683+
Py_INCREF(get_csv_state(module)->error_obj);
1684+
PyModule_AddObject(module, "Error", get_csv_state(module)->error_obj);
16791685
return module;
16801686
}

Modules/_curses_panel.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ typedef struct {
2121
PyObject *PyCursesPanel_Type;
2222
} _curses_panelstate;
2323

24-
#define _curses_panelstate(o) ((_curses_panelstate *)PyModule_GetState(o))
24+
static inline _curses_panelstate*
25+
get_curses_panelstate(PyObject *module)
26+
{
27+
void *state = PyModule_GetState(module);
28+
assert(state != NULL);
29+
return (_curses_panelstate *)state;
30+
}
2531

2632
static int
2733
_curses_panel_clear(PyObject *m)
2834
{
29-
Py_CLEAR(_curses_panelstate(m)->PyCursesError);
35+
Py_CLEAR(get_curses_panelstate(m)->PyCursesError);
3036
return 0;
3137
}
3238

3339
static int
3440
_curses_panel_traverse(PyObject *m, visitproc visit, void *arg)
3541
{
36-
Py_VISIT(_curses_panelstate(m)->PyCursesError);
42+
Py_VISIT(get_curses_panelstate(m)->PyCursesError);
3743
return 0;
3844
}
3945

@@ -645,24 +651,25 @@ PyInit__curses_panel(void)
645651
if (v == NULL)
646652
goto fail;
647653
((PyTypeObject *)v)->tp_new = NULL;
648-
_curses_panelstate(m)->PyCursesPanel_Type = v;
654+
get_curses_panelstate(m)->PyCursesPanel_Type = v;
649655

650656
import_curses();
651657
if (PyErr_Occurred())
652658
goto fail;
653659

654660
/* For exception _curses_panel.error */
655-
_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL);
656-
PyDict_SetItemString(d, "error", _curses_panelstate(m)->PyCursesError);
661+
get_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL);
662+
PyDict_SetItemString(d, "error", get_curses_panelstate(m)->PyCursesError);
657663

658664
/* Make the version available */
659665
v = PyUnicode_FromString(PyCursesVersion);
660666
PyDict_SetItemString(d, "version", v);
661667
PyDict_SetItemString(d, "__version__", v);
662668
Py_DECREF(v);
663669

664-
Py_INCREF(_curses_panelstate(m)->PyCursesPanel_Type);
665-
PyModule_AddObject(m, "panel", (PyObject *)_curses_panelstate(m)->PyCursesPanel_Type);
670+
Py_INCREF(get_curses_panelstate(m)->PyCursesPanel_Type);
671+
PyModule_AddObject(m, "panel",
672+
(PyObject *)get_curses_panelstate(m)->PyCursesPanel_Type);
666673
return m;
667674
fail:
668675
Py_XDECREF(m);

Modules/_elementtree.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ static struct PyModuleDef elementtreemodule;
101101
/* Given a module object (assumed to be _elementtree), get its per-module
102102
* state.
103103
*/
104-
#define ET_STATE(mod) ((elementtreestate *) PyModule_GetState(mod))
104+
static inline elementtreestate*
105+
get_elementtree_state(PyObject *module)
106+
{
107+
void *state = PyModule_GetState(module);
108+
assert(state != NULL);
109+
return (elementtreestate *)state;
110+
}
105111

106112
/* Find the module instance imported in the currently running sub-interpreter
107113
* and get its state.
@@ -112,7 +118,7 @@ static struct PyModuleDef elementtreemodule;
112118
static int
113119
elementtree_clear(PyObject *m)
114120
{
115-
elementtreestate *st = ET_STATE(m);
121+
elementtreestate *st = get_elementtree_state(m);
116122
Py_CLEAR(st->parseerror_obj);
117123
Py_CLEAR(st->deepcopy_obj);
118124
Py_CLEAR(st->elementpath_obj);
@@ -124,7 +130,7 @@ elementtree_clear(PyObject *m)
124130
static int
125131
elementtree_traverse(PyObject *m, visitproc visit, void *arg)
126132
{
127-
elementtreestate *st = ET_STATE(m);
133+
elementtreestate *st = get_elementtree_state(m);
128134
Py_VISIT(st->parseerror_obj);
129135
Py_VISIT(st->deepcopy_obj);
130136
Py_VISIT(st->elementpath_obj);
@@ -4377,7 +4383,7 @@ PyInit__elementtree(void)
43774383
m = PyModule_Create(&elementtreemodule);
43784384
if (!m)
43794385
return NULL;
4380-
st = ET_STATE(m);
4386+
st = get_elementtree_state(m);
43814387

43824388
if (!(temp = PyImport_ImportModule("copy")))
43834389
return NULL;

Modules/_hashopenssl.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ typedef struct {
5252
PyTypeObject *EVPtype;
5353
} _hashlibstate;
5454

55-
#define _hashlibstate(o) ((_hashlibstate *)PyModule_GetState(o))
55+
static inline _hashlibstate*
56+
get_hashlib_state(PyObject *module)
57+
{
58+
void *state = PyModule_GetState(module);
59+
assert(state != NULL);
60+
return (_hashlibstate *)state;
61+
}
62+
5663
#define _hashlibstate_global ((_hashlibstate *)PyModule_GetState(PyState_FindModule(&_hashlibmodule)))
5764

5865

@@ -1112,15 +1119,15 @@ static struct PyMethodDef EVP_functions[] = {
11121119
static int
11131120
hashlib_traverse(PyObject *m, visitproc visit, void *arg)
11141121
{
1115-
_hashlibstate *state = _hashlibstate(m);
1122+
_hashlibstate *state = get_hashlib_state(m);
11161123
Py_VISIT(state->EVPtype);
11171124
return 0;
11181125
}
11191126

11201127
static int
11211128
hashlib_clear(PyObject *m)
11221129
{
1123-
_hashlibstate *state = _hashlibstate(m);
1130+
_hashlibstate *state = get_hashlib_state(m);
11241131
Py_CLEAR(state->EVPtype);
11251132
return 0;
11261133
}
@@ -1168,7 +1175,7 @@ PyInit__hashlib(void)
11681175
PyTypeObject *EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
11691176
if (EVPtype == NULL)
11701177
return NULL;
1171-
_hashlibstate(m)->EVPtype = EVPtype;
1178+
get_hashlib_state(m)->EVPtype = EVPtype;
11721179

11731180
openssl_md_meth_names = generate_hash_name_list();
11741181
if (openssl_md_meth_names == NULL) {
@@ -1180,8 +1187,8 @@ PyInit__hashlib(void)
11801187
return NULL;
11811188
}
11821189

1183-
Py_INCREF((PyObject *)_hashlibstate(m)->EVPtype);
1184-
PyModule_AddObject(m, "HASH", (PyObject *)_hashlibstate(m)->EVPtype);
1190+
Py_INCREF((PyObject *)get_hashlib_state(m)->EVPtype);
1191+
PyModule_AddObject(m, "HASH", (PyObject *)get_hashlib_state(m)->EVPtype);
11851192

11861193
PyState_AddModule(m, &_hashlibmodule);
11871194
return m;

Modules/_io/_iomodule.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,20 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err)
573573
return result;
574574
}
575575

576+
static inline _PyIO_State*
577+
get_io_state(PyObject *module)
578+
{
579+
void *state = PyModule_GetState(module);
580+
assert(state != NULL);
581+
return (_PyIO_State *)state;
582+
}
576583

577584
_PyIO_State *
578585
_PyIO_get_module_state(void)
579586
{
580587
PyObject *mod = PyState_FindModule(&_PyIO_Module);
581588
_PyIO_State *state;
582-
if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) {
589+
if (mod == NULL || (state = get_io_state(mod)) == NULL) {
583590
PyErr_SetString(PyExc_RuntimeError,
584591
"could not find io module state "
585592
"(interpreter shutdown?)");
@@ -615,7 +622,7 @@ _PyIO_get_locale_module(_PyIO_State *state)
615622

616623
static int
617624
iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
618-
_PyIO_State *state = IO_MOD_STATE(mod);
625+
_PyIO_State *state = get_io_state(mod);
619626
if (!state->initialized)
620627
return 0;
621628
if (state->locale_module != NULL) {
@@ -628,7 +635,7 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
628635

629636
static int
630637
iomodule_clear(PyObject *mod) {
631-
_PyIO_State *state = IO_MOD_STATE(mod);
638+
_PyIO_State *state = get_io_state(mod);
632639
if (!state->initialized)
633640
return 0;
634641
if (state->locale_module != NULL)
@@ -674,7 +681,7 @@ PyInit__io(void)
674681
_PyIO_State *state = NULL;
675682
if (m == NULL)
676683
return NULL;
677-
state = IO_MOD_STATE(m);
684+
state = get_io_state(m);
678685
state->initialized = 0;
679686

680687
#define ADD_TYPE(type, name) \

Modules/_posixsubprocess.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ typedef struct {
6868

6969
static struct PyModuleDef _posixsubprocessmodule;
7070

71-
#define _posixsubprocessstate(o) ((_posixsubprocessstate *)PyModule_GetState(o))
72-
#define _posixsubprocessstate_global _posixsubprocessstate(PyState_FindModule(&_posixsubprocessmodule))
71+
static inline _posixsubprocessstate*
72+
get_posixsubprocess_state(PyObject *module)
73+
{
74+
void *state = PyModule_GetState(module);
75+
assert(state != NULL);
76+
return (_posixsubprocessstate *)state;
77+
}
78+
79+
#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))
7380

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

945952

946953
static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
947-
Py_VISIT(_posixsubprocessstate(m)->disable);
948-
Py_VISIT(_posixsubprocessstate(m)->enable);
949-
Py_VISIT(_posixsubprocessstate(m)->isenabled);
954+
Py_VISIT(get_posixsubprocess_state(m)->disable);
955+
Py_VISIT(get_posixsubprocess_state(m)->enable);
956+
Py_VISIT(get_posixsubprocess_state(m)->isenabled);
950957
return 0;
951958
}
952959

953960
static int _posixsubprocess_clear(PyObject *m) {
954-
Py_CLEAR(_posixsubprocessstate(m)->disable);
955-
Py_CLEAR(_posixsubprocessstate(m)->enable);
956-
Py_CLEAR(_posixsubprocessstate(m)->isenabled);
961+
Py_CLEAR(get_posixsubprocess_state(m)->disable);
962+
Py_CLEAR(get_posixsubprocess_state(m)->enable);
963+
Py_CLEAR(get_posixsubprocess_state(m)->isenabled);
957964
return 0;
958965
}
959966

@@ -989,9 +996,9 @@ PyInit__posixsubprocess(void)
989996
return NULL;
990997
}
991998

992-
_posixsubprocessstate(m)->disable = PyUnicode_InternFromString("disable");
993-
_posixsubprocessstate(m)->enable = PyUnicode_InternFromString("enable");
994-
_posixsubprocessstate(m)->isenabled = PyUnicode_InternFromString("isenabled");
999+
get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
1000+
get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
1001+
get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");
9951002

9961003
PyState_AddModule(m, &_posixsubprocessmodule);
9971004
return m;

Modules/_randommodule.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,17 @@ typedef struct {
8484
PyObject *Long___abs__;
8585
} _randomstate;
8686

87-
#define _randomstate(o) ((_randomstate *)PyModule_GetState(o))
87+
static inline _randomstate*
88+
get_random_state(PyObject *module)
89+
{
90+
void *state = PyModule_GetState(module);
91+
assert(state != NULL);
92+
return (_randomstate *)state;
93+
}
8894

8995
static struct PyModuleDef _randommodule;
9096

91-
#define _randomstate_global _randomstate(PyState_FindModule(&_randommodule))
97+
#define _randomstate_global get_random_state(PyState_FindModule(&_randommodule))
9298

9399
typedef struct {
94100
PyObject_HEAD
@@ -561,15 +567,15 @@ PyDoc_STRVAR(module_doc,
561567
static int
562568
_random_traverse(PyObject *module, visitproc visit, void *arg)
563569
{
564-
Py_VISIT(_randomstate(module)->Random_Type);
570+
Py_VISIT(get_random_state(module)->Random_Type);
565571
return 0;
566572
}
567573

568574
static int
569575
_random_clear(PyObject *module)
570576
{
571-
Py_CLEAR(_randomstate(module)->Random_Type);
572-
Py_CLEAR(_randomstate(module)->Long___abs__);
577+
Py_CLEAR(get_random_state(module)->Random_Type);
578+
Py_CLEAR(get_random_state(module)->Long___abs__);
573579
return 0;
574580
}
575581

@@ -606,7 +612,7 @@ PyInit__random(void)
606612
Py_DECREF(Random_Type);
607613
return NULL;
608614
}
609-
_randomstate(m)->Random_Type = Random_Type;
615+
get_random_state(m)->Random_Type = Random_Type;
610616

611617
Py_INCREF(Random_Type);
612618
PyModule_AddObject(m, "Random", Random_Type);
@@ -624,7 +630,7 @@ PyInit__random(void)
624630

625631
Py_DECREF(longtype);
626632
Py_DECREF(longval);
627-
_randomstate(m)->Long___abs__ = abs;
633+
get_random_state(m)->Long___abs__ = abs;
628634

629635
return m;
630636

0 commit comments

Comments
 (0)