Skip to content

Commit c701101

Browse files
authored
bpo-1635741: Port symtable module to multiphase initialization (GH-23361)
Signed-off-by: Christian Heimes <christian@python.org>
1 parent d1e38d4 commit c701101

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
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: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,56 +71,60 @@ 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,
76-
"_symtable",
77-
NULL,
78-
-1,
79-
symtable_methods,
80-
NULL,
81-
NULL,
82-
NULL,
83-
NULL
120+
.m_name = "_symtable",
121+
.m_size = 0,
122+
.m_methods = symtable_methods,
123+
.m_slots = symtable_slots,
84124
};
85125

86126
PyMODINIT_FUNC
87127
PyInit__symtable(void)
88128
{
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;
129+
return PyModuleDef_Init(&symtablemodule);
126130
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy