LoLBench / cpython_15 /tests /private /eval_tests.patch
lolbench26's picture
Publish LoLBench Harbor tasks from artifact branch (part 2)
a4edccc verified
Raw History Blame Contribute Delete
20.2 kB
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