LoLBench / cpython_15 /tests /private /eval_helpers.patch
lolbench26's picture
Publish LoLBench Harbor tasks from artifact branch (part 2)
a4edccc verified
Raw History Blame Contribute Delete
22 kB
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
index b9ffdcc65d1..2c3013e3d0c 100644
--- a/Modules/Setup.stdlib.in
+++ b/Modules/Setup.stdlib.in
@@ -175,7 +175,7 @@
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c
-@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c
+@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c _testcapi/module.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
diff --git a/Modules/_testcapi/heaptype.c b/Modules/_testcapi/heaptype.c
index 69dcf072da1..4fdcc850a33 100644
--- a/Modules/_testcapi/heaptype.c
+++ b/Modules/_testcapi/heaptype.c
@@ -528,6 +528,21 @@ pytype_getmodulebydef(PyObject *self, PyObject *type)
return Py_XNewRef(mod);
}
+static PyObject *
+pytype_getmodulebytoken(PyObject *self, PyObject *args)
+{
+ PyObject *type;
+ PyObject *py_token;
+ if (!PyArg_ParseTuple(args, "OO", &type, &py_token)) {
+ return NULL;
+ }
+ void *token = PyLong_AsVoidPtr(py_token);
+ if ((!token) && PyErr_Occurred()) {
+ return NULL;
+ }
+ return PyType_GetModuleByToken((PyTypeObject *)type, token);
+}
+
static PyMethodDef TestMethods[] = {
{"pytype_fromspec_meta", pytype_fromspec_meta, METH_O},
@@ -546,6 +561,7 @@ static PyMethodDef TestMethods[] = {
{"get_tp_token", get_tp_token, METH_O},
{"pytype_getbasebytoken", pytype_getbasebytoken, METH_VARARGS},
{"pytype_getmodulebydef", pytype_getmodulebydef, METH_O},
+ {"pytype_getmodulebytoken", pytype_getmodulebytoken, METH_VARARGS},
{NULL},
};
diff --git a/Modules/_testcapi/module.c b/Modules/_testcapi/module.c
new file mode 100644
index 00000000000..9349445351e
--- /dev/null
+++ b/Modules/_testcapi/module.c
@@ -0,0 +1,378 @@
+#include "parts.h"
+#include "util.h"
+
+// Test PyModule_* API
+
+/* unittest Cases that use these functions are in:
+ * Lib/test/test_capi/test_module.py
+ */
+
+static PyObject *
+module_from_slots_empty(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_null(PyObject *self, PyObject *spec)
+{
+ return PyModule_FromSlotsAndSpec(NULL, spec);
+}
+
+static PyObject *
+module_from_slots_name(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_name, "currently ignored..."},
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_doc(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_doc, "the docstring"},
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_size(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_state_size, (void*)123},
+ {0},
+ };
+ PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+ if (!mod) {
+ return NULL;
+ }
+ return mod;
+}
+
+static PyObject *
+a_method(PyObject *self, PyObject *arg)
+{
+ return PyTuple_Pack(2, self, arg);
+}
+
+static PyMethodDef a_methoddef_array[] = {
+ {"a_method", a_method, METH_O},
+ {0},
+};
+
+static PyObject *
+module_from_slots_methods(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_methods, a_methoddef_array},
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static int noop_traverse(PyObject *self, visitproc visit, void *arg) {
+ return 0;
+}
+static int noop_clear(PyObject *self) { return 0; }
+static void noop_free(void *self) { }
+
+static PyObject *
+module_from_slots_gc(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_state_traverse, noop_traverse},
+ {Py_mod_state_clear, noop_clear},
+ {Py_mod_state_free, noop_free},
+ {0},
+ };
+ PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+ if (!mod) {
+ return NULL;
+ }
+ if (PyModule_Add(mod, "traverse", PyLong_FromVoidPtr(&noop_traverse)) < 0) {
+ Py_DECREF(mod);
+ return NULL;
+ }
+ if (PyModule_Add(mod, "clear", PyLong_FromVoidPtr(&noop_clear)) < 0) {
+ Py_DECREF(mod);
+ return NULL;
+ }
+ if (PyModule_Add(mod, "free", PyLong_FromVoidPtr(&noop_free)) < 0) {
+ Py_DECREF(mod);
+ return NULL;
+ }
+ return mod;
+}
+
+static const char test_token;
+
+static PyObject *
+module_from_slots_token(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_token, (void*)&test_token},
+ {0},
+ };
+ PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+ if (!mod) {
+ return NULL;
+ }
+ void *got_token;
+ if (PyModule_GetToken(mod, &got_token) < 0) {
+ Py_DECREF(mod);
+ return NULL;
+ }
+ assert(got_token == &test_token);
+ return mod;
+}
+
+static int
+simple_exec(PyObject *module)
+{
+ return PyModule_AddIntConstant(module, "a_number", 456);
+}
+
+static PyObject *
+module_from_slots_exec(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_exec, simple_exec},
+ {0},
+ };
+ PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+ if (!mod) {
+ return NULL;
+ }
+ int res = PyObject_HasAttrStringWithError(mod, "a_number");
+ if (res < 0) {
+ Py_DECREF(mod);
+ return NULL;
+ }
+ assert(res == 0);
+ if (PyModule_Exec(mod) < 0) {
+ Py_DECREF(mod);
+ return NULL;
+ }
+ return mod;
+}
+
+static PyObject *
+create_attr_from_spec(PyObject *spec, PyObject *def)
+{
+ assert(!def);
+ return PyObject_GetAttrString(spec, "_gimme_this");
+}
+
+static PyObject *
+module_from_slots_create(PyObject *self, PyObject *spec)
+{
+ PyModuleDef_Slot slots[] = {
+ {Py_mod_create, create_attr_from_spec},
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+
+static int
+slot_from_object(PyObject *obj)
+{
+ PyObject *slot_id_obj = PyObject_GetAttrString(obj, "_test_slot_id");
+ if (slot_id_obj == NULL) {
+ return -1;
+ }
+ int slot_id = PyLong_AsInt(slot_id_obj);
+ if (PyErr_Occurred()) {
+ return -1;
+ }
+ return slot_id;
+}
+
+static PyObject *
+module_from_slots_repeat_slot(PyObject *self, PyObject *spec)
+{
+ int slot_id = slot_from_object(spec);
+ if (slot_id < 0) {
+ return NULL;
+ }
+ PyModuleDef_Slot slots[] = {
+ {slot_id, "anything"},
+ {slot_id, "anything else"},
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_null_slot(PyObject *self, PyObject *spec)
+{
+ int slot_id = slot_from_object(spec);
+ if (slot_id < 0) {
+ return NULL;
+ }
+ PyModuleDef_Slot slots[] = {
+ {slot_id, NULL},
+ {0},
+ };
+ return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_def_slot(PyObject *self, PyObject *spec)
+{
+ int slot_id = slot_from_object(spec);
+ if (slot_id < 0) {
+ return NULL;
+ }
+ PyModuleDef_Slot slots[] = {
+ {slot_id, "anything"},
+ {0},
+ };
+ PyModuleDef def = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "currently ignored",
+ .m_slots = slots,
+ };
+ // PyModuleDef is normally static; the real requirement is that it
+ // must outlive its module.
+ // Here, module creation fails, so it's fine on the stack.
+ PyObject *result = PyModule_FromDefAndSpec(&def, spec);
+ assert(result == NULL);
+ return result;
+}
+
+static int
+another_exec(PyObject *module)
+{
+ /* Make sure simple_exec was called */
+ assert(PyObject_HasAttrString(module, "a_number"));
+
+ /* Add or negate a global called 'another_number' */
+ PyObject *another_number;
+ if (PyObject_GetOptionalAttrString(module, "another_number",
+ &another_number) < 0) {
+ return -1;
+ }
+ if (!another_number) {
+ return PyModule_AddIntConstant(module, "another_number", 789);
+ }
+ PyObject *neg_number = PyNumber_Negative(another_number);
+ Py_DECREF(another_number);
+ if (!neg_number) {
+ return -1;
+ }
+ int result = PyObject_SetAttrString(module, "another_number",
+ neg_number);
+ Py_DECREF(neg_number);
+ return result;
+}
+
+static PyObject *
+module_from_def_multiple_exec(PyObject *self, PyObject *spec)
+{
+ static PyModuleDef_Slot slots[] = {
+ {Py_mod_exec, simple_exec},
+ {Py_mod_exec, another_exec},
+ {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+ {0},
+ };
+ static PyModuleDef def = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "currently ignored",
+ .m_slots = slots,
+ };
+ return PyModule_FromDefAndSpec(&def, spec);
+}
+
+static PyObject *
+pymodule_exec(PyObject *self, PyObject *module)
+{
+ if (PyModule_Exec(module) < 0) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+pymodule_get_token(PyObject *self, PyObject *module)
+{
+ void *token;
+ if (PyModule_GetToken(module, &token) < 0) {
+ return NULL;
+ }
+ return PyLong_FromVoidPtr(token);
+}
+
+static PyObject *
+pymodule_get_def(PyObject *self, PyObject *module)
+{
+ PyModuleDef *def = PyModule_GetDef(module);
+ if (!def && PyErr_Occurred()) {
+ return NULL;
+ }
+ return PyLong_FromVoidPtr(def);
+}
+
+static PyObject *
+pymodule_get_state_size(PyObject *self, PyObject *module)
+{
+ Py_ssize_t size;
+ if (PyModule_GetStateSize(module, &size) < 0) {
+ return NULL;
+ }
+ return PyLong_FromSsize_t(size);
+}
+
+static PyMethodDef test_methods[] = {
+ {"module_from_slots_empty", module_from_slots_empty, METH_O},
+ {"module_from_slots_null", module_from_slots_null, METH_O},
+ {"module_from_slots_name", module_from_slots_name, METH_O},
+ {"module_from_slots_doc", module_from_slots_doc, METH_O},
+ {"module_from_slots_size", module_from_slots_size, METH_O},
+ {"module_from_slots_methods", module_from_slots_methods, METH_O},
+ {"module_from_slots_gc", module_from_slots_gc, METH_O},
+ {"module_from_slots_token", module_from_slots_token, METH_O},
+ {"module_from_slots_exec", module_from_slots_exec, METH_O},
+ {"module_from_slots_create", module_from_slots_create, METH_O},
+ {"module_from_slots_repeat_slot", module_from_slots_repeat_slot, METH_O},
+ {"module_from_slots_null_slot", module_from_slots_null_slot, METH_O},
+ {"module_from_def_multiple_exec", module_from_def_multiple_exec, METH_O},
+ {"module_from_def_slot", module_from_def_slot, METH_O},
+ {"pymodule_get_token", pymodule_get_token, METH_O},
+ {"pymodule_get_def", pymodule_get_def, METH_O},
+ {"pymodule_get_state_size", pymodule_get_state_size, METH_O},
+ {"pymodule_exec", pymodule_exec, METH_O},
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_Module(PyObject *m)
+{
+#define ADD_INT_MACRO(C) if (PyModule_AddIntConstant(m, #C, C) < 0) return -1;
+ ADD_INT_MACRO(Py_mod_create);
+ ADD_INT_MACRO(Py_mod_exec);
+ ADD_INT_MACRO(Py_mod_multiple_interpreters);
+ ADD_INT_MACRO(Py_mod_gil);
+ ADD_INT_MACRO(Py_mod_name);
+ ADD_INT_MACRO(Py_mod_doc);
+ ADD_INT_MACRO(Py_mod_state_size);
+ ADD_INT_MACRO(Py_mod_methods);
+ ADD_INT_MACRO(Py_mod_state_traverse);
+ ADD_INT_MACRO(Py_mod_state_clear);
+ ADD_INT_MACRO(Py_mod_state_free);
+ ADD_INT_MACRO(Py_mod_token);
+#undef ADD_INT_MACRO
+ if (PyModule_Add(m, "module_test_token",
+ PyLong_FromVoidPtr((void*)&test_token)) < 0)
+ {
+ return -1;
+ }
+ return PyModule_AddFunctions(m, test_methods);
+}
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index 32915d04bd3..a7feca5bd96 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -66,5 +66,6 @@ int _PyTestCapi_Init_Import(PyObject *mod);
int _PyTestCapi_Init_Frame(PyObject *mod);
int _PyTestCapi_Init_Type(PyObject *mod);
int _PyTestCapi_Init_Function(PyObject *mod);
+int _PyTestCapi_Init_Module(PyObject *mod);
#endif // Py_TESTCAPI_PARTS_H
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index e29b9ae354b..22cd731d410 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3512,6 +3512,9 @@ _testcapi_exec(PyObject *m)
if (_PyTestCapi_Init_Function(m) < 0) {
return -1;
}
+ if (_PyTestCapi_Init_Module(m) < 0) {
+ return -1;
+ }
return 0;
}
diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c
index c2647d405e2..dede05960d7 100644
--- a/Modules/_testinternalcapi.c
+++ b/Modules/_testinternalcapi.c
@@ -2418,6 +2418,34 @@ set_vectorcall_nop(PyObject *self, PyObject *func)
Py_RETURN_NONE;
}
+static PyObject *
+module_get_gc_hooks(PyObject *self, PyObject *arg)
+{
+ PyModuleObject *mod = (PyModuleObject *)arg;
+ PyObject *traverse = NULL;
+ PyObject *clear = NULL;
+ PyObject *free = NULL;
+ PyObject *result = NULL;
+ traverse = PyLong_FromVoidPtr(mod->md_state_traverse);
+ if (!traverse) {
+ goto finally;
+ }
+ clear = PyLong_FromVoidPtr(mod->md_state_clear);
+ if (!clear) {
+ goto finally;
+ }
+ free = PyLong_FromVoidPtr(mod->md_state_free);
+ if (!free) {
+ goto finally;
+ }
+ result = PyTuple_FromArray((PyObject*[]){ traverse, clear, free }, 3);
+finally:
+ Py_XDECREF(traverse);
+ Py_XDECREF(clear);
+ Py_XDECREF(free);
+ return result;
+}
+
static PyMethodDef module_functions[] = {
{"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -2527,6 +2555,7 @@ static PyMethodDef module_functions[] = {
#endif
{"simple_pending_call", simple_pending_call, METH_O},
{"set_vectorcall_nop", set_vectorcall_nop, METH_O},
+ {"module_get_gc_hooks", module_get_gc_hooks, METH_O},
{NULL, NULL} /* sentinel */
};
diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c
index bfec0678e2c..220fa888e49 100644
--- a/Modules/_testmultiphase.c
+++ b/Modules/_testmultiphase.c
@@ -850,6 +850,28 @@ PyInit__testmultiphase_exec_unreported_exception(void)
return PyModuleDef_Init(&def_exec_unreported_exception);
}
+static int execfn_a1(PyObject*m) { return PyModule_AddIntConstant(m, "a", 1); }
+static int execfn_b2(PyObject*m) { return PyModule_AddIntConstant(m, "b", 2); }
+static int execfn_c3(PyObject*m) { return PyModule_AddIntConstant(m, "c", 3); }
+
+PyMODINIT_FUNC
+PyInit__testmultiphase_exec_multiple(void)
+{
+ static PyModuleDef_Slot slots[] = {
+ {Py_mod_exec, execfn_a1},
+ {Py_mod_exec, execfn_b2},
+ {Py_mod_exec, execfn_c3},
+ {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+ {0}
+ };
+ static PyModuleDef def = {
+ PyModuleDef_HEAD_INIT,
+ .m_name="_testmultiphase_exec_multiple",
+ .m_slots=slots,
+ };
+ return PyModuleDef_Init(&def);
+}
+
static int
meth_state_access_exec(PyObject *m)
{
@@ -993,3 +1015,156 @@ PyInit__test_no_multiple_interpreter_slot(void)
{
return PyModuleDef_Init(&no_multiple_interpreter_slot_def);
}
+
+
+/* PyModExport_* hooks */
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport(void)
+{
+ static PyModuleDef_Slot slots[] = {
+ {Py_mod_name, "_test_from_modexport"},
+ {0},
+ };
+ return slots;
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_null(void)
+{
+ return NULL;
+}
+
+PyMODINIT_FUNC
+PyModInit__test_from_modexport_null(void)
+{
+ // This is not called as fallback for failed PyModExport_*
+ assert(0);
+ PyErr_SetString(PyExc_AssertionError, "PyInit_ fallback called");
+ return NULL;
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_exception(void)
+{
+ PyErr_SetString(PyExc_ValueError, "failed as requested");
+ return NULL;
+}
+
+PyMODINIT_FUNC
+PyModInit__test_from_modexport_exception(void)
+{
+ // This is not called as fallback for failed PyModExport_*
+ assert(0);
+ PyErr_SetString(PyExc_AssertionError, "PyInit_ fallback called");
+ return NULL;
+}
+
+static PyObject *
+modexport_create_string(PyObject *spec, PyObject *def)
+{
+ assert(def == NULL);
+ return PyUnicode_FromString("is this \xf0\x9f\xa6\x8b... a module?");
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_create_nonmodule(void)
+{
+ static PyModuleDef_Slot slots[] = {
+ {Py_mod_name, "_test_from_modexport_create_nonmodule"},
+ {Py_mod_create, modexport_create_string},
+ {0},
+ };
+ return slots;
+}
+
+static PyModuleDef_Slot modexport_empty_slots[] = {
+ {0},
+};
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_empty_slots(void)
+{
+ return modexport_empty_slots;
+}
+
+static int
+modexport_smoke_exec(PyObject *mod)
+{
+ // "magic" values 147 & 258 are expected in the test
+ if (PyModule_AddIntConstant(mod, "number", 147) < 0) {
+ return 0;
+ }
+ int *state = PyModule_GetState(mod);
+ if (!state) {
+ return -1;
+ }
+ *state = 258;
+
+ PyObject *tp = PyType_FromModuleAndSpec(mod, &StateAccessType_spec, NULL);
+ if (PyModule_Add(mod, "Example", tp) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject *
+modexport_smoke_get_state_int(PyObject *mod, PyObject *arg)
+{
+ int *state = PyModule_GetState(mod);
+ if (!state) {
+ return NULL;
+ }
+ return PyLong_FromLong(*state);
+}
+
+static const char modexport_smoke_test_token;
+
+static PyObject *
+modexport_smoke_get_test_token(PyObject *mod, PyObject *arg)
+{
+ return PyLong_FromVoidPtr((void*)&modexport_smoke_test_token);
+}
+
+static PyObject *
+modexport_get_empty_slots(PyObject *mod, PyObject *arg)
+{
+ /* Get the address of modexport_empty_slots.
+ * This method would be in the `_test_from_modexport_empty_slots` module,
+ * if it had a methods slot.
+ */
+ return PyLong_FromVoidPtr(&modexport_empty_slots);
+}
+
+static void
+modexport_smoke_free(PyObject *mod)
+{
+ int *state = PyModule_GetState(mod);
+ if (!state) {
+ PyErr_FormatUnraisable("Exception ignored in module %R free", mod);
+ }
+ assert(*state == 258);
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_smoke(void)
+{
+ static PyMethodDef methods[] = {
+ {"get_state_int", modexport_smoke_get_state_int, METH_NOARGS},
+ {"get_test_token", modexport_smoke_get_test_token, METH_NOARGS},
+ {"get_modexport_empty_slots", modexport_get_empty_slots, METH_NOARGS},
+ {0},
+ };
+ static PyModuleDef_Slot slots[] = {
+ {Py_mod_name, "_test_from_modexport_smoke"},
+ {Py_mod_doc, "the expected docstring"},
+ {Py_mod_exec, modexport_smoke_exec},
+ {Py_mod_state_size, (void*)sizeof(int)},
+ {Py_mod_methods, methods},
+ {Py_mod_state_free, modexport_smoke_free},
+ {Py_mod_token, (void*)&modexport_smoke_test_token},
+ {0},
+ };
+ return slots;
+}
diff --git a/Modules/_testsinglephase.c b/Modules/_testsinglephase.c
index 2c59085d15b..ee38d61b43a 100644
--- a/Modules/_testsinglephase.c
+++ b/Modules/_testsinglephase.c
@@ -244,6 +244,8 @@ static inline module_state *
get_module_state(PyObject *module)
{
PyModuleDef *def = PyModule_GetDef(module);
+ assert(def);
+
if (def->m_size == -1) {
return &global_state.module;
}