-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-1635741: Port symtable module to multiphase initialization #23361
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Port :mod:`symtable` extension module to multiphase initialization | ||
(:pep:`489`) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,56 +71,60 @@ static PyMethodDef symtable_methods[] = { | |
{NULL, NULL} /* sentinel */ | ||
}; | ||
|
||
static int | ||
symtable_init_stentry_type(PyObject *m) | ||
{ | ||
return PyType_Ready(&PySTEntry_Type); | ||
} | ||
|
||
static int | ||
symtable_init_constants(PyObject *m) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to slowly use macros to factorize the code, something like:
A more generic machinery like the one you proposed would avoid such macro, and until there is such API, IMO using a macro makes the code more readable:
You can add a second macro for PyModule_AddIntConstant() calls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather not spend more time then necessary. This was a simple search and replace job. Let's consider refactoring after |
||
if (PyModule_AddIntMacro(m, USE) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1; | ||
|
||
if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0) | ||
return -1; | ||
if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1; | ||
if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0) | ||
return -1; | ||
|
||
if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, FREE) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, CELL) < 0) return -1; | ||
|
||
if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1; | ||
if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1; | ||
|
||
return 0; | ||
} | ||
|
||
static PyModuleDef_Slot symtable_slots[] = { | ||
{Py_mod_exec, symtable_init_stentry_type}, | ||
{Py_mod_exec, symtable_init_constants}, | ||
{0, NULL} | ||
}; | ||
|
||
static struct PyModuleDef symtablemodule = { | ||
PyModuleDef_HEAD_INIT, | ||
"_symtable", | ||
NULL, | ||
-1, | ||
symtable_methods, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL | ||
.m_name = "_symtable", | ||
.m_size = 0, | ||
.m_methods = symtable_methods, | ||
.m_slots = symtable_slots, | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
PyInit__symtable(void) | ||
{ | ||
PyObject *m; | ||
|
||
if (PyType_Ready(&PySTEntry_Type) < 0) | ||
return NULL; | ||
|
||
m = PyModule_Create(&symtablemodule); | ||
if (m == NULL) | ||
return NULL; | ||
PyModule_AddIntMacro(m, USE); | ||
PyModule_AddIntMacro(m, DEF_GLOBAL); | ||
PyModule_AddIntMacro(m, DEF_NONLOCAL); | ||
PyModule_AddIntMacro(m, DEF_LOCAL); | ||
PyModule_AddIntMacro(m, DEF_PARAM); | ||
PyModule_AddIntMacro(m, DEF_FREE); | ||
PyModule_AddIntMacro(m, DEF_FREE_CLASS); | ||
PyModule_AddIntMacro(m, DEF_IMPORT); | ||
PyModule_AddIntMacro(m, DEF_BOUND); | ||
PyModule_AddIntMacro(m, DEF_ANNOT); | ||
|
||
PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock); | ||
PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock); | ||
PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock); | ||
|
||
PyModule_AddIntMacro(m, LOCAL); | ||
PyModule_AddIntMacro(m, GLOBAL_EXPLICIT); | ||
PyModule_AddIntMacro(m, GLOBAL_IMPLICIT); | ||
PyModule_AddIntMacro(m, FREE); | ||
PyModule_AddIntMacro(m, CELL); | ||
|
||
PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET); | ||
PyModule_AddIntMacro(m, SCOPE_MASK); | ||
|
||
if (PyErr_Occurred()) { | ||
Py_DECREF(m); | ||
m = 0; | ||
} | ||
return m; | ||
return PyModuleDef_Init(&symtablemodule); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the value of having two exec slots. Why not simply putting all exec code into a single function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do different things. Also
PySTEntry_Type
is define somewhere else and we might want to move the type initialization when thePySTEntry_Type
is refactored.