Skip to content

Commit a64d018

Browse files
ericsnowcurrentlygvanrossum
authored andcommitted
pythongh-98627: Use a Switch in PyModule_FromDefAndSpec2() (pythongh-98734)
This helps simplify some changes in follow-up PRs. It also matches what we're doing in PyModule_ExecDef().
1 parent c66cd30 commit a64d018

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

Objects/moduleobject.c

+18-14
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,27 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
291291
}
292292

293293
for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
294-
if (cur_slot->slot == Py_mod_create) {
295-
if (create) {
294+
switch (cur_slot->slot) {
295+
case Py_mod_create:
296+
if (create) {
297+
PyErr_Format(
298+
PyExc_SystemError,
299+
"module %s has multiple create slots",
300+
name);
301+
goto error;
302+
}
303+
create = cur_slot->value;
304+
break;
305+
case Py_mod_exec:
306+
has_execution_slots = 1;
307+
break;
308+
default:
309+
assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
296310
PyErr_Format(
297311
PyExc_SystemError,
298-
"module %s has multiple create slots",
299-
name);
312+
"module %s uses unknown slot ID %i",
313+
name, cur_slot->slot);
300314
goto error;
301-
}
302-
create = cur_slot->value;
303-
} else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
304-
PyErr_Format(
305-
PyExc_SystemError,
306-
"module %s uses unknown slot ID %i",
307-
name, cur_slot->slot);
308-
goto error;
309-
} else {
310-
has_execution_slots = 1;
311315
}
312316
}
313317

0 commit comments

Comments
 (0)