diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c index 0f668c1..bb1844f 100644 --- a/Lib/test/test_cext/extension.c +++ b/Lib/test/test_cext/extension.c @@ -18,6 +18,7 @@ // - greenlet uses pycore_frame.h, pycore_interpframe_structs.h and // pycore_interpframe.h. # include "internal/pycore_frame.h" +# include "internal/pycore_importdl.h" # include "internal/pycore_gc.h" # include "internal/pycore_interp.h" # include "internal/pycore_interpframe.h" @@ -109,6 +110,30 @@ _Py_COMP_DIAG_POP PyMODEXPORT_FUNC FUNC_NAME(MODULE_NAME)(void) { +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +# define PEP793_RETURNS_SLOTS(FUNC) \ + _Generic((FUNC), PyModuleDef_Slot *(*)(void): 1, default: 0) + if (!PEP793_RETURNS_SLOTS(FUNC_NAME(MODULE_NAME))) { + PyErr_SetString(PyExc_AssertionError, + "PyMODEXPORT_FUNC must return PyModuleDef_Slot*"); + return NULL; + } + if (Py_mod_token != 13) { + PyErr_SetString(PyExc_AssertionError, + "Py_mod_token must keep its public slot id"); + return NULL; + } +# ifdef TEST_INTERNAL_C_API + PyModExportFunction export_func = FUNC_NAME(MODULE_NAME); + (void)export_func; + if (_Py_mod_LAST_SLOT < Py_mod_token) { + PyErr_SetString(PyExc_AssertionError, + "_Py_mod_LAST_SLOT must include Py_mod_token"); + return NULL; + } +# endif +# undef PEP793_RETURNS_SLOTS +#endif return _testcext_slots; } diff --git a/Lib/test/test_pep793_aug.py b/Lib/test/test_pep793_aug.py new file mode 100644 index 0000000..bb5cefc --- /dev/null +++ b/Lib/test/test_pep793_aug.py @@ -0,0 +1,54 @@ +import _imp +import importlib.util +import sys +import unittest +from importlib.machinery import ExtensionFileLoader + +from test.support import import_helper + +_testcapi = import_helper.import_module("_testcapi") +_testmultiphase = import_helper.import_module("_testmultiphase") + + +def import_extension_from_file(modname, filename): + loader = ExtensionFileLoader(modname, filename) + spec = importlib.util.spec_from_loader(modname, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +class PyModExportAugmentedTests(unittest.TestCase): + + def test_nonascii_modexport_uses_u_prefix(self): + modname = "_testmodexport_\u017elut\u00fd" + module = import_extension_from_file(modname, _testmultiphase.__file__) + self.assertEqual(module.__name__, modname) + self.assertEqual(module.__doc__, "Module loaded through PyModExportU") + + def test_slots_create_nonmodule_rejects_token(self): + with self.assertRaises(SystemError): + _testcapi.pep793_create_token_nonmodule() + + def test_zero_size_state_marks_slots_module_initialized(self): + module = import_extension_from_file( + "_testmodexport_zero_state", _testmultiphase.__file__) + self.assertEqual(module.exec_count, 1) + _imp.exec_dynamic(module) + self.assertEqual(module.exec_count, 1) + + def test_type_getmodulebytoken_returns_strong_reference(self): + token = _testcapi.pymodule_get_token(_testcapi) + heaptype = _testcapi.create_type_with_token( + "_testcapi.PEP793StrongRef", 0) + + before = sys.getrefcount(_testcapi) + mod = _testcapi.pytype_getmodulebytoken(heaptype, token) + self.assertIs(mod, _testcapi) + self.assertEqual(sys.getrefcount(_testcapi), before + 1) + del mod + self.assertEqual(sys.getrefcount(_testcapi), before) + + +if __name__ == "__main__": + unittest.main() diff --git a/Modules/_testcapi/import.c b/Modules/_testcapi/import.c index 27d3749..ca2d12a 100644 --- a/Modules/_testcapi/import.c +++ b/Modules/_testcapi/import.c @@ -30,9 +30,129 @@ pyimport_importmoduleattrstring(PyObject *self, PyObject *args) } +#ifdef Py_mod_token +static char pep793_token; + +static PyObject * +pep793_make_spec(const char *name) +{ + PyObject *types = PyImport_ImportModule("types"); + if (types == NULL) { + return NULL; + } + PyObject *ns_type = PyObject_GetAttrString(types, "SimpleNamespace"); + Py_DECREF(types); + if (ns_type == NULL) { + return NULL; + } + PyObject *spec = PyObject_CallNoArgs(ns_type); + Py_DECREF(ns_type); + if (spec == NULL) { + return NULL; + } + PyObject *pyname = PyUnicode_FromString(name); + if (pyname == NULL) { + Py_DECREF(spec); + return NULL; + } + int res = PyObject_SetAttrString(spec, "name", pyname); + Py_DECREF(pyname); + if (res < 0) { + Py_DECREF(spec); + return NULL; + } + return spec; +} + +static PyObject * +pep793_create_string(PyObject *spec, PyObject *def) +{ + return PyUnicode_FromString("not a module object"); +} + +static PyObject * +pep793_create_token_nonmodule(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *spec = pep793_make_spec("pep793_token_nonmodule"); + if (spec == NULL) { + return NULL; + } + PyModuleDef_Slot slots[] = { + {Py_mod_create, (void *)pep793_create_string}, + {Py_mod_token, (void *)&pep793_token}, + {0, NULL}, + }; + PyObject *module = PyModule_FromSlotsAndSpec(slots, spec); + Py_DECREF(spec); + return module; +} + +static int +pep793_count_exec(PyObject *module) +{ + long value = 0; + PyObject *count = PyObject_GetAttrString(module, "exec_count"); + if (count == NULL) { + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { + return -1; + } + PyErr_Clear(); + } + else { + value = PyLong_AsLong(count); + Py_DECREF(count); + if (value == -1 && PyErr_Occurred()) { + return -1; + } + } + PyObject *new_count = PyLong_FromLong(value + 1); + if (new_count == NULL) { + return -1; + } + int res = PyObject_SetAttrString(module, "exec_count", new_count); + Py_DECREF(new_count); + return res; +} + +static PyObject * +pep793_exec_zero_state_twice(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *spec = pep793_make_spec("pep793_zero_state"); + if (spec == NULL) { + return NULL; + } + PyModuleDef_Slot slots[] = { + {Py_mod_state_size, (void *)0}, + {Py_mod_exec, (void *)pep793_count_exec}, + {0, NULL}, + }; + PyObject *module = PyModule_FromSlotsAndSpec(slots, spec); + Py_DECREF(spec); + if (module == NULL) { + return NULL; + } + if (PyModule_Exec(module) < 0) { + Py_DECREF(module); + return NULL; + } + if (PyModule_Exec(module) < 0) { + Py_DECREF(module); + return NULL; + } + PyObject *count = PyObject_GetAttrString(module, "exec_count"); + Py_DECREF(module); + return count; +} +#endif + + static PyMethodDef test_methods[] = { {"PyImport_ImportModuleAttr", pyimport_importmoduleattr, METH_VARARGS}, {"PyImport_ImportModuleAttrString", pyimport_importmoduleattrstring, METH_VARARGS}, +#ifdef Py_mod_token + {"pep793_create_token_nonmodule", pep793_create_token_nonmodule, METH_NOARGS}, + {"pep793_exec_zero_state_twice", pep793_exec_zero_state_twice, METH_NOARGS}, +#endif {NULL}, }; diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c index bfec067..56dbd74 100644 --- a/Modules/_testmultiphase.c +++ b/Modules/_testmultiphase.c @@ -567,6 +567,60 @@ PyInitU_eckzbwbhc6jpgzcx415x(void) return PyModuleDef_Init(&def_nonascii_kana); } +#ifdef PyMODEXPORT_FUNC +static PyModuleDef_Slot modexport_nonascii_slots[] = { + {Py_mod_doc, "Module loaded through PyModExportU"}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, + {0, NULL}, +}; + +PyMODEXPORT_FUNC +PyModExportU__testmodexport_lut_n7b09w(void) +{ + return modexport_nonascii_slots; +} + +static int +modexport_zero_state_exec(PyObject *module) +{ + long value = 0; + PyObject *count = PyObject_GetAttrString(module, "exec_count"); + if (count == NULL) { + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { + return -1; + } + PyErr_Clear(); + } + else { + value = PyLong_AsLong(count); + Py_DECREF(count); + if (value == -1 && PyErr_Occurred()) { + return -1; + } + } + PyObject *new_count = PyLong_FromLong(value + 1); + if (new_count == NULL) { + return -1; + } + int res = PyObject_SetAttrString(module, "exec_count", new_count); + Py_DECREF(new_count); + return res; +} + +static PyModuleDef_Slot modexport_zero_state_slots[] = { + {Py_mod_name, "_testmodexport_zero_state"}, + {Py_mod_exec, modexport_zero_state_exec}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, + {0, NULL}, +}; + +PyMODEXPORT_FUNC +PyModExport__testmodexport_zero_state(void) +{ + return modexport_zero_state_slots; +} +#endif + /*** Module with a single-character name ***/ PyMODINIT_FUNC