Skip to content

Commit ae7b176

Browse files
gh-119584: Fix test_import Failed Assertion (gh-119623)
The fix in gh-119561 introduced an assertion that doesn't hold true if any of the three new test extension modules are loaded more than once. This is fine normally but breaks if the new test_check_state_first() is run more than once, which happens for refleak checking and with the regrtest --forever flag. We fix that here by clearing each of the three modules after loading them. We also tweak a check in _modules_by_index_check().
1 parent 0bd0d40 commit ae7b176

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

Lib/test/test_import/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -2887,12 +2887,15 @@ def test_with_reinit_reloaded(self):
28872887

28882888
self.assertIs(reloaded.snapshot.cached, reloaded.module)
28892889

2890+
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
28902891
def test_check_state_first(self):
28912892
for variant in ['', '_with_reinit', '_with_state']:
28922893
name = f'{self.NAME}{variant}_check_cache_first'
28932894
with self.subTest(name):
28942895
mod = self._load_dynamic(name, self.ORIGIN)
28952896
self.assertEqual(mod.__name__, name)
2897+
sys.modules.pop(name, None)
2898+
_testinternalcapi.clear_extension(name, self.ORIGIN)
28962899

28972900
# Currently, for every single-phrase init module loaded
28982901
# in multiple interpreters, those interpreters share a

Modules/_testsinglephase.c

+3
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ PyInit__testsinglephase_with_state(void)
682682
/* the _testsinglephase_*_check_cache_first modules */
683683
/****************************************************/
684684

685+
/* Each of these modules should only be freshly loaded. That means
686+
clearing the caches and each module def's m_base after each load. */
687+
685688
static struct PyModuleDef _testsinglephase_check_cache_first = {
686689
PyModuleDef_HEAD_INIT,
687690
.m_name = "_testsinglephase_check_cache_first",

Python/import.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ _modules_by_index_check(PyInterpreterState *interp, Py_ssize_t index)
494494
if (MODULES_BY_INDEX(interp) == NULL) {
495495
return "Interpreters module-list not accessible.";
496496
}
497-
if (index > PyList_GET_SIZE(MODULES_BY_INDEX(interp))) {
497+
if (index >= PyList_GET_SIZE(MODULES_BY_INDEX(interp))) {
498498
return "Module index out of bounds.";
499499
}
500500
return NULL;
@@ -2183,7 +2183,7 @@ clear_singlephase_extension(PyInterpreterState *interp,
21832183
/* Clear data set when the module was initially loaded. */
21842184
def->m_base.m_init = NULL;
21852185
Py_CLEAR(def->m_base.m_copy);
2186-
// We leave m_index alone since there's no reason to reset it.
2186+
def->m_base.m_index = 0;
21872187

21882188
/* Clear the PyState_*Module() cache entry. */
21892189
Py_ssize_t index = _get_cached_module_index(cached);

0 commit comments

Comments
 (0)