File size: 20,208 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | diff --git a/Lib/test/test_capi/test_module.py b/Lib/test/test_capi/test_module.py
new file mode 100644
index 00000000000..7ec23e637d7
--- /dev/null
+++ b/Lib/test/test_capi/test_module.py
@@ -0,0 +1,185 @@
+# The C functions used by this module are in:
+# Modules/_testcapi/module.c
+
+import unittest
+import types
+from test.support import import_helper, subTests
+
+# Skip this test if the _testcapi module isn't available.
+_testcapi = import_helper.import_module('_testcapi')
+
+
+class FakeSpec:
+ name = 'testmod'
+
+DEF_SLOTS = (
+ 'Py_mod_name', 'Py_mod_doc', 'Py_mod_state_size', 'Py_mod_methods',
+ 'Py_mod_state_traverse', 'Py_mod_state_clear', 'Py_mod_state_free',
+ 'Py_mod_token',
+)
+
+def def_and_token(mod):
+ return (
+ _testcapi.pymodule_get_def(mod),
+ _testcapi.pymodule_get_token(mod),
+ )
+
+class TestModFromSlotsAndSpec(unittest.TestCase):
+ def test_empty(self):
+ mod = _testcapi.module_from_slots_empty(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ size = _testcapi.pymodule_get_state_size(mod)
+ self.assertEqual(size, 0)
+
+ def test_null_slots(self):
+ with self.assertRaises(SystemError):
+ _testcapi.module_from_slots_null(FakeSpec())
+
+ def test_none_spec(self):
+ # The spec currently must contain a name
+ with self.assertRaises(AttributeError):
+ _testcapi.module_from_slots_empty(None)
+ with self.assertRaises(AttributeError):
+ _testcapi.module_from_slots_name(None)
+
+ def test_name(self):
+ # Py_mod_name (and PyModuleDef.m_name) are currently ignored when
+ # spec is given.
+ # We still test that it's accepted.
+ mod = _testcapi.module_from_slots_name(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, None)
+
+ def test_doc(self):
+ mod = _testcapi.module_from_slots_doc(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, 'the docstring')
+
+ def test_size(self):
+ mod = _testcapi.module_from_slots_size(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, None)
+ size = _testcapi.pymodule_get_state_size(mod)
+ self.assertEqual(size, 123)
+
+ def test_methods(self):
+ mod = _testcapi.module_from_slots_methods(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, None)
+ self.assertEqual(mod.a_method(456), (mod, 456))
+
+ def test_gc(self):
+ mod = _testcapi.module_from_slots_gc(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, None)
+
+ # Check that the requested hook functions (which module_from_slots_gc
+ # stores as attributes) match what's in the module (as retrieved by
+ # _testinternalcapi.module_get_gc_hooks)
+ _testinternalcapi = import_helper.import_module('_testinternalcapi')
+ traverse, clear, free = _testinternalcapi.module_get_gc_hooks(mod)
+ self.assertEqual(traverse, mod.traverse)
+ self.assertEqual(clear, mod.clear)
+ self.assertEqual(free, mod.free)
+
+ def test_token(self):
+ mod = _testcapi.module_from_slots_token(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, _testcapi.module_test_token))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, None)
+
+ def test_exec(self):
+ mod = _testcapi.module_from_slots_exec(FakeSpec())
+ self.assertIsInstance(mod, types.ModuleType)
+ self.assertEqual(def_and_token(mod), (0, 0))
+ self.assertEqual(mod.__name__, 'testmod')
+ self.assertEqual(mod.__doc__, None)
+ self.assertEqual(mod.a_number, 456)
+
+ def test_create(self):
+ spec = FakeSpec()
+ spec._gimme_this = "not a module object"
+ mod = _testcapi.module_from_slots_create(spec)
+ self.assertIsInstance(mod, str)
+ self.assertEqual(mod, "not a module object")
+ with self.assertRaises(TypeError):
+ _testcapi.pymodule_get_def(mod),
+ with self.assertRaises(TypeError):
+ _testcapi.pymodule_get_token(mod)
+
+ def test_def_slot(self):
+ """Slots that replace PyModuleDef fields can't be used with PyModuleDef
+ """
+ for name in DEF_SLOTS:
+ with self.subTest(name):
+ spec = FakeSpec()
+ spec._test_slot_id = getattr(_testcapi, name)
+ with self.assertRaises(SystemError) as cm:
+ _testcapi.module_from_def_slot(spec)
+ self.assertIn(name, str(cm.exception))
+ self.assertIn("PyModuleDef", str(cm.exception))
+
+ def test_repeated_def_slot(self):
+ """Slots that replace PyModuleDef fields can't be repeated"""
+ for name in (*DEF_SLOTS, 'Py_mod_exec'):
+ with self.subTest(name):
+ spec = FakeSpec()
+ spec._test_slot_id = getattr(_testcapi, name)
+ with self.assertRaises(SystemError) as cm:
+ _testcapi.module_from_slots_repeat_slot(spec)
+ self.assertIn(name, str(cm.exception))
+ self.assertIn("more than one", str(cm.exception))
+
+ def test_null_def_slot(self):
+ """Slots that replace PyModuleDef fields can't be NULL"""
+ for name in (*DEF_SLOTS, 'Py_mod_exec'):
+ with self.subTest(name):
+ spec = FakeSpec()
+ spec._test_slot_id = getattr(_testcapi, name)
+ with self.assertRaises(SystemError) as cm:
+ _testcapi.module_from_slots_null_slot(spec)
+ self.assertIn(name, str(cm.exception))
+ self.assertIn("NULL", str(cm.exception))
+
+ def test_def_multiple_exec(self):
+ """PyModule_Exec runs all exec slots of PyModuleDef-defined module"""
+ mod = _testcapi.module_from_def_multiple_exec(FakeSpec())
+ self.assertFalse(hasattr(mod, 'a_number'))
+ _testcapi.pymodule_exec(mod)
+ self.assertEqual(mod.a_number, 456)
+ self.assertEqual(mod.another_number, 789)
+ _testcapi.pymodule_exec(mod)
+ self.assertEqual(mod.a_number, 456)
+ self.assertEqual(mod.another_number, -789)
+ def_ptr, token = def_and_token(mod)
+ self.assertEqual(def_ptr, token)
+
+ def test_def_token(self):
+ """In PyModuleDef-defined modules, the def is the token"""
+ mod = _testcapi.module_from_def_multiple_exec(FakeSpec())
+ def_ptr, token = def_and_token(mod)
+ self.assertEqual(def_ptr, token)
+ self.assertGreater(def_ptr, 0)
+
+ @subTests('name, expected_size', [
+ (__name__, 0), # Python module
+ ('_testsinglephase', -1), # single-phase init
+ ('sys', -1),
+ ])
+ def test_get_state_size(self, name, expected_size):
+ mod = import_helper.import_module(name)
+ size = _testcapi.pymodule_get_state_size(mod)
+ self.assertEqual(size, expected_size)
diff --git a/Lib/test/test_capi/test_type.py b/Lib/test/test_capi/test_type.py
index 93874fbee32..e6a8ef9eed6 100644
--- a/Lib/test/test_capi/test_type.py
+++ b/Lib/test/test_capi/test_type.py
@@ -195,6 +195,24 @@ class TypeTests(unittest.TestCase):
with self.assertRaises(TypeError):
_testcapi.pytype_getmodulebydef(H2)
+ def test_get_module_by_token(self):
+ token = _testcapi.pymodule_get_token(_testcapi)
+
+ heaptype = _testcapi.create_type_with_token('_testcapi.H', 0)
+ mod = _testcapi.pytype_getmodulebytoken(heaptype, token)
+ self.assertIs(mod, _testcapi)
+
+ class H1(heaptype): pass
+ mod = _testcapi.pytype_getmodulebytoken(H1, token)
+ self.assertIs(mod, _testcapi)
+
+ with self.assertRaises(TypeError):
+ _testcapi.pytype_getmodulebytoken(int, token)
+
+ class H2(int): pass
+ with self.assertRaises(TypeError):
+ _testcapi.pytype_getmodulebytoken(H2, token)
+
def test_freeze(self):
# test PyType_Freeze()
type_freeze = _testcapi.type_freeze
diff --git a/Lib/test/test_cext/__init__.py b/Lib/test/test_cext/__init__.py
index fb93c6ccbb6..a52c2241f5d 100644
--- a/Lib/test/test_cext/__init__.py
+++ b/Lib/test/test_cext/__init__.py
@@ -14,7 +14,6 @@ from test import support
SOURCES = [
os.path.join(os.path.dirname(__file__), 'extension.c'),
- os.path.join(os.path.dirname(__file__), 'create_moduledef.c'),
]
SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
diff --git a/Lib/test/test_cext/create_moduledef.c b/Lib/test/test_cext/create_moduledef.c
deleted file mode 100644
index 249c3163552..00000000000
--- a/Lib/test/test_cext/create_moduledef.c
+++ /dev/null
@@ -1,29 +0,0 @@
-// Workaround for testing _Py_OPAQUE_PYOBJECT.
-// See end of 'extension.c'
-
-
-#undef _Py_OPAQUE_PYOBJECT
-#undef Py_LIMITED_API
-#include "Python.h"
-
-
-// (repeated definition to avoid creating a header)
-extern PyObject *testcext_create_moduledef(
- const char *name, const char *doc,
- PyMethodDef *methods, PyModuleDef_Slot *slots);
-
-PyObject *testcext_create_moduledef(
- const char *name, const char *doc,
- PyMethodDef *methods, PyModuleDef_Slot *slots) {
-
- static struct PyModuleDef _testcext_module = {
- PyModuleDef_HEAD_INIT,
- };
- if (!_testcext_module.m_name) {
- _testcext_module.m_name = name;
- _testcext_module.m_doc = doc;
- _testcext_module.m_methods = methods;
- _testcext_module.m_slots = slots;
- }
- return PyModuleDef_Init(&_testcext_module);
-}
diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c
index 73fc67ae59d..0f668c1da32 100644
--- a/Lib/test/test_cext/extension.c
+++ b/Lib/test/test_cext/extension.c
@@ -78,7 +78,7 @@ _testcext_exec(
return 0;
}
-#define _FUNC_NAME(NAME) PyInit_ ## NAME
+#define _FUNC_NAME(NAME) PyModExport_ ## NAME
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
// Converting from function pointer to void* has undefined behavior, but
@@ -88,58 +88,40 @@ _testcext_exec(
_Py_COMP_DIAG_PUSH
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wpedantic"
+#pragma GCC diagnostic ignored "-Wcast-qual"
#elif defined(__clang__)
#pragma clang diagnostic ignored "-Wpedantic"
+#pragma clang diagnostic ignored "-Wcast-qual"
#endif
+PyDoc_STRVAR(_testcext_doc, "C test extension.");
+
static PyModuleDef_Slot _testcext_slots[] = {
+ {Py_mod_name, STR(MODULE_NAME)},
+ {Py_mod_doc, (void*)(char*)_testcext_doc},
{Py_mod_exec, (void*)_testcext_exec},
+ {Py_mod_methods, _testcext_methods},
{0, NULL}
};
_Py_COMP_DIAG_POP
-PyDoc_STRVAR(_testcext_doc, "C test extension.");
-
-#ifndef _Py_OPAQUE_PYOBJECT
-
-static struct PyModuleDef _testcext_module = {
- PyModuleDef_HEAD_INIT, // m_base
- STR(MODULE_NAME), // m_name
- _testcext_doc, // m_doc
- 0, // m_size
- _testcext_methods, // m_methods
- _testcext_slots, // m_slots
- NULL, // m_traverse
- NULL, // m_clear
- NULL, // m_free
-};
-
-
-PyMODINIT_FUNC
+PyMODEXPORT_FUNC
FUNC_NAME(MODULE_NAME)(void)
{
- return PyModuleDef_Init(&_testcext_module);
+ return _testcext_slots;
}
-#else // _Py_OPAQUE_PYOBJECT
-
-// Opaque PyObject means that PyModuleDef is also opaque and cannot be
-// declared statically. See PEP 793.
-// So, this part of module creation is split into a separate source file
-// which uses non-limited API.
-
-// (repeated definition to avoid creating a header)
-extern PyObject *testcext_create_moduledef(
- const char *name, const char *doc,
- PyMethodDef *methods, PyModuleDef_Slot *slots);
+// Also define the soft-deprecated entrypoint to ensure it isn't called
+#define _INITFUNC_NAME(NAME) PyInit_ ## NAME
+#define INITFUNC_NAME(NAME) _INITFUNC_NAME(NAME)
PyMODINIT_FUNC
-FUNC_NAME(MODULE_NAME)(void)
+INITFUNC_NAME(MODULE_NAME)(void)
{
- return testcext_create_moduledef(
- STR(MODULE_NAME), _testcext_doc, _testcext_methods, _testcext_slots);
+ PyErr_SetString(
+ PyExc_AssertionError,
+ "PyInit_* function called while a PyModExport_* one is available");
+ return NULL;
}
-
-#endif // _Py_OPAQUE_PYOBJECT
diff --git a/Lib/test/test_cext/setup.py b/Lib/test/test_cext/setup.py
index 4d71e4751f7..67dfddec751 100644
--- a/Lib/test/test_cext/setup.py
+++ b/Lib/test/test_cext/setup.py
@@ -99,7 +99,6 @@ def main():
# Define _Py_OPAQUE_PYOBJECT macro
if opaque_pyobject:
cflags.append(f'-D_Py_OPAQUE_PYOBJECT')
- sources.append('create_moduledef.c')
if internal:
cflags.append('-DTEST_INTERNAL_C_API=1')
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 072021e5959..e87d8b7e7bb 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -2497,6 +2497,21 @@ class SubinterpImportTests(unittest.TestCase):
self.check_compatible_here(
modname, filename, strict=False, isolated=False)
+ @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
+ def test_testmultiphase_exec_multiple(self):
+ modname = '_testmultiphase_exec_multiple'
+ filename = _testmultiphase.__file__
+ module = import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+ # All three exec's were called.
+ self.assertEqual(module.a, 1)
+ self.assertEqual(module.b, 2)
+ self.assertEqual(module.c, 3)
+ # They were called in order.
+ keys = list(module.__dict__)
+ self.assertLess(keys.index('a'), keys.index('b'))
+ self.assertLess(keys.index('b'), keys.index('c'))
+
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_python_compat(self):
module = 'threading'
@@ -3394,6 +3409,83 @@ class SinglephaseInitTests(unittest.TestCase):
# * module's global state was initialized, not reset
+@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
+class ModexportTests(unittest.TestCase):
+ def test_from_modexport(self):
+ modname = '_test_from_modexport'
+ filename = _testmultiphase.__file__
+ module = import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+
+ self.assertEqual(module.__name__, modname)
+
+ def test_from_modexport_null(self):
+ modname = '_test_from_modexport_null'
+ filename = _testmultiphase.__file__
+ with self.assertRaises(SystemError):
+ import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+
+ def test_from_modexport_exception(self):
+ modname = '_test_from_modexport_exception'
+ filename = _testmultiphase.__file__
+ with self.assertRaises(ValueError):
+ import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+
+ def test_from_modexport_create_nonmodule(self):
+ modname = '_test_from_modexport_create_nonmodule'
+ filename = _testmultiphase.__file__
+ module = import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+ self.assertIsInstance(module, str)
+
+ def test_from_modexport_smoke(self):
+ # General positive test for sundry features
+ # (PyModule_FromSlotsAndSpec tests exercise these more carefully)
+ modname = '_test_from_modexport_smoke'
+ filename = _testmultiphase.__file__
+ module = import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+ self.assertEqual(module.__doc__, "the expected docstring")
+ self.assertEqual(module.number, 147)
+ self.assertEqual(module.get_state_int(), 258)
+ self.assertGreater(module.get_test_token(), 0)
+
+ def test_from_modexport_smoke_token(self):
+ _testcapi = import_module("_testcapi")
+
+ modname = '_test_from_modexport_smoke'
+ filename = _testmultiphase.__file__
+ module = import_extension_from_file(modname, filename,
+ put_in_sys_modules=False)
+ token = module.get_test_token()
+ self.assertEqual(_testcapi.pymodule_get_token(module), token)
+
+ tp = module.Example
+ self.assertEqual(_testcapi.pytype_getmodulebytoken(tp, token), module)
+ class Sub(tp):
+ pass
+ self.assertEqual(_testcapi.pytype_getmodulebytoken(Sub, token), module)
+
+ def test_from_modexport_empty_slots(self):
+ # Module to test that:
+ # - no slots are mandatory for PyModExport
+ # - the slots array is used as the default token
+ modname = '_test_from_modexport_empty_slots'
+ filename = _testmultiphase.__file__
+ module = import_extension_from_file(
+ modname, filename, put_in_sys_modules=False)
+
+ self.assertEqual(module.__name__, modname)
+ self.assertEqual(module.__doc__, None)
+
+ _testcapi = import_module("_testcapi")
+ smoke_mod = import_extension_from_file(
+ '_test_from_modexport_smoke', filename, put_in_sys_modules=False)
+ self.assertEqual(_testcapi.pymodule_get_token(module),
+ smoke_mod.get_modexport_empty_slots())
+
@cpython_only
class TestMagicNumber(unittest.TestCase):
def test_magic_number_endianness(self):
diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py
index cbec7e43a7c..7167646ecc6 100644
--- a/Lib/test/test_stable_abi_ctypes.py
+++ b/Lib/test/test_stable_abi_ctypes.py
@@ -469,8 +469,10 @@ SYMBOL_NAMES = (
"PyModule_AddStringConstant",
"PyModule_AddType",
"PyModule_Create2",
+ "PyModule_Exec",
"PyModule_ExecDef",
"PyModule_FromDefAndSpec2",
+ "PyModule_FromSlotsAndSpec",
"PyModule_GetDef",
"PyModule_GetDict",
"PyModule_GetFilename",
@@ -478,6 +480,8 @@ SYMBOL_NAMES = (
"PyModule_GetName",
"PyModule_GetNameObject",
"PyModule_GetState",
+ "PyModule_GetStateSize",
+ "PyModule_GetToken",
"PyModule_New",
"PyModule_NewObject",
"PyModule_SetDocString",
@@ -733,6 +737,7 @@ SYMBOL_NAMES = (
"PyType_GetFullyQualifiedName",
"PyType_GetModule",
"PyType_GetModuleByDef",
+ "PyType_GetModuleByToken",
"PyType_GetModuleName",
"PyType_GetModuleState",
"PyType_GetName",
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 1198c6d3511..3ceed019ac4 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1725,9 +1725,10 @@ class SizeofTest(unittest.TestCase):
check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
# module
if support.Py_GIL_DISABLED:
- check(unittest, size('PPPPPP'))
+ md_gil = 'P'
else:
- check(unittest, size('PPPPP'))
+ md_gil = ''
+ check(unittest, size('PPPP?' + md_gil + 'NPPPPP'))
# None
check(None, size(''))
# NotImplementedType
|