]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-98627: Use a Switch in PyModule_FromDefAndSpec2() (gh-98734)
authorEric Snow <ericsnowcurrently@gmail.com>
Thu, 27 Oct 2022 03:20:54 +0000 (21:20 -0600)
committerGitHub <noreply@github.com>
Thu, 27 Oct 2022 03:20:54 +0000 (21:20 -0600)
This helps simplify some changes in follow-up PRs.  It also matches what we're doing in PyModule_ExecDef().

Objects/moduleobject.c

index dba20a0b36463d37f2a980429fb7d874a824ef6e..ee8ef7f5b4a5dcc62e5ea97dc07c2a5b3fe791df 100644 (file)
@@ -291,23 +291,27 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
     }
 
     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
-        if (cur_slot->slot == Py_mod_create) {
-            if (create) {
+        switch (cur_slot->slot) {
+            case Py_mod_create:
+                if (create) {
+                    PyErr_Format(
+                        PyExc_SystemError,
+                        "module %s has multiple create slots",
+                        name);
+                    goto error;
+                }
+                create = cur_slot->value;
+                break;
+            case Py_mod_exec:
+                has_execution_slots = 1;
+                break;
+            default:
+                assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
                 PyErr_Format(
                     PyExc_SystemError,
-                    "module %s has multiple create slots",
-                    name);
+                    "module %s uses unknown slot ID %i",
+                    name, cur_slot->slot);
                 goto error;
-            }
-            create = cur_slot->value;
-        } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
-            PyErr_Format(
-                PyExc_SystemError,
-                "module %s uses unknown slot ID %i",
-                name, cur_slot->slot);
-            goto error;
-        } else {
-            has_execution_slots = 1;
         }
     }