Skip to content

Commit

Permalink
pythongh-102251: Explicitly free state for test modules with state in…
Browse files Browse the repository at this point in the history
… test_import (pythonGH-105085)

(cherry picked from commit a99b9d9)

Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
  • Loading branch information
2 people authored and miss-islington committed May 31, 2023
1 parent e7cb216 commit 4e1a1e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 10 additions & 4 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,7 @@ def test_variants(self):
self.add_module_cleanup(name)
with self.subTest(name):
loaded = self.load(name)
self.addCleanup(loaded.module._clear_module_state)

self.check_common(loaded)
self.assertIsNot(loaded.snapshot.state_initialized, None)
Expand Down Expand Up @@ -2379,14 +2380,19 @@ def test_with_reinit_reloaded(self):
# Keep a reference around.
basic = self.load(self.NAME)

for name in [
f'{self.NAME}_with_reinit', # m_size == 0
f'{self.NAME}_with_state', # m_size > 0
for name, has_state in [
(f'{self.NAME}_with_reinit', False), # m_size == 0
(f'{self.NAME}_with_state', True), # m_size > 0
]:
self.add_module_cleanup(name)
with self.subTest(name):
with self.subTest(name=name, has_state=has_state):
loaded = self.load(name)
if has_state:
self.addCleanup(loaded.module._clear_module_state)

reloaded = self.re_load(name, loaded.module)
if has_state:
self.addCleanup(reloaded.module._clear_module_state)

self.check_common(loaded)
self.check_common(reloaded)
Expand Down
22 changes: 21 additions & 1 deletion Modules/_testsinglephase.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ basic__clear_globals(PyObject *self, PyObject *Py_UNUSED(ignored))
basic__clear_globals_doc}


PyDoc_STRVAR(basic__clear_module_state_doc, "_clear_module_state()\n\
\n\
Free the module state and set it to uninitialized.");

static PyObject *
basic__clear_module_state(PyObject *self, PyObject *Py_UNUSED(ignored))
{
module_state *state = get_module_state(self);
if (state != NULL) {
clear_state(state);
}
Py_RETURN_NONE;
}

#define _CLEAR_MODULE_STATE_METHODDEF \
{"_clear_module_state", basic__clear_module_state, METH_NOARGS, \
basic__clear_module_state_doc}


/*********************************************/
/* the _testsinglephase module (and aliases) */
/*********************************************/
Expand Down Expand Up @@ -408,7 +427,7 @@ PyInit__testsinglephase_with_reinit(void)
/* the _testsinglephase_with_state module */
/******************************************/

/* This ia less typical of legacy extensions in the wild:
/* This is less typical of legacy extensions in the wild:
- single-phase init (same as _testsinglephase above)
- has some module state
- supports repeated initialization
Expand All @@ -424,6 +443,7 @@ static PyMethodDef TestMethods_WithState[] = {
LOOK_UP_SELF_METHODDEF,
SUM_METHODDEF,
STATE_INITIALIZED_METHODDEF,
_CLEAR_MODULE_STATE_METHODDEF,
{NULL, NULL} /* sentinel */
};

Expand Down

0 comments on commit 4e1a1e1

Please sign in to comment.