File size: 9,042 Bytes
a4edccc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | 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
|