Skip to content

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

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port :mod:`symtable` extension module to multiphase initialization
(:pep:`489`)
94 changes: 49 additions & 45 deletions Modules/symtablemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,56 +71,60 @@ static PyMethodDef symtable_methods[] = {
{NULL, NULL} /* sentinel */
};

static int
symtable_init_stentry_type(PyObject *m)
{
return PyType_Ready(&PySTEntry_Type);
Copy link
Member

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?

Copy link
Member Author

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 the PySTEntry_Type is refactored.

}

static int
symtable_init_constants(PyObject *m)
{
Copy link
Member

Choose a reason for hiding this comment

The 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:

#define ADD_INT(value) \
    if (PyModule_AddIntConstant(module, #value, value) < 0) { \
        return -1; \
    }

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:

ADD_INT(USE);

You can add a second macro for PyModule_AddIntConstant() calls.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 PyModuleConstants_Def from #23286 has landed. The new feature will make module constant definitions easier.

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);
}