Skip to content

Commit bb7961b

Browse files
committed
bpo-1635741: Port symtable module to multiphase initialization
Signed-off-by: Christian Heimes <christian@python.org>
1 parent ccdcb20 commit bb7961b

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Port :mod:`symtable` extension module to multiphase initialization
2+
(:pep:`489`)

Modules/symtablemodule.c

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,57 @@ static PyMethodDef symtable_methods[] = {
7171
{NULL, NULL} /* sentinel */
7272
};
7373

74+
static int
75+
symtable_init_stentry_type(PyObject *m)
76+
{
77+
return PyType_Ready(&PySTEntry_Type);
78+
}
79+
80+
static int
81+
symtable_init_constants(PyObject *m)
82+
{
83+
if (PyModule_AddIntMacro(m, USE) < 0) return -1;
84+
if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1;
85+
if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
86+
if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
87+
if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
88+
if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1;
89+
if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
90+
if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
91+
if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1;
92+
if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1;
93+
94+
if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
95+
return -1;
96+
if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1;
97+
if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
98+
return -1;
99+
100+
if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1;
101+
if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1;
102+
if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1;
103+
if (PyModule_AddIntMacro(m, FREE) < 0) return -1;
104+
if (PyModule_AddIntMacro(m, CELL) < 0) return -1;
105+
106+
if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1;
107+
if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1;
108+
109+
return 0;
110+
}
111+
112+
static PyModuleDef_Slot symtable_slots[] = {
113+
{Py_mod_exec, symtable_init_stentry_type},
114+
{Py_mod_exec, symtable_init_constants},
115+
{0, NULL}
116+
};
117+
74118
static struct PyModuleDef symtablemodule = {
75119
PyModuleDef_HEAD_INIT,
76120
"_symtable",
77121
NULL,
78-
-1,
122+
0,
79123
symtable_methods,
80-
NULL,
124+
symtable_slots,
81125
NULL,
82126
NULL,
83127
NULL
@@ -86,41 +130,5 @@ static struct PyModuleDef symtablemodule = {
86130
PyMODINIT_FUNC
87131
PyInit__symtable(void)
88132
{
89-
PyObject *m;
90-
91-
if (PyType_Ready(&PySTEntry_Type) < 0)
92-
return NULL;
93-
94-
m = PyModule_Create(&symtablemodule);
95-
if (m == NULL)
96-
return NULL;
97-
PyModule_AddIntMacro(m, USE);
98-
PyModule_AddIntMacro(m, DEF_GLOBAL);
99-
PyModule_AddIntMacro(m, DEF_NONLOCAL);
100-
PyModule_AddIntMacro(m, DEF_LOCAL);
101-
PyModule_AddIntMacro(m, DEF_PARAM);
102-
PyModule_AddIntMacro(m, DEF_FREE);
103-
PyModule_AddIntMacro(m, DEF_FREE_CLASS);
104-
PyModule_AddIntMacro(m, DEF_IMPORT);
105-
PyModule_AddIntMacro(m, DEF_BOUND);
106-
PyModule_AddIntMacro(m, DEF_ANNOT);
107-
108-
PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
109-
PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
110-
PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
111-
112-
PyModule_AddIntMacro(m, LOCAL);
113-
PyModule_AddIntMacro(m, GLOBAL_EXPLICIT);
114-
PyModule_AddIntMacro(m, GLOBAL_IMPLICIT);
115-
PyModule_AddIntMacro(m, FREE);
116-
PyModule_AddIntMacro(m, CELL);
117-
118-
PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET);
119-
PyModule_AddIntMacro(m, SCOPE_MASK);
120-
121-
if (PyErr_Occurred()) {
122-
Py_DECREF(m);
123-
m = 0;
124-
}
125-
return m;
133+
return PyModuleDef_Init(&symtablemodule);
126134
}

0 commit comments

Comments
 (0)