Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
clear module state after use
  • Loading branch information
sunmy2019 committed May 30, 2023
commit f4de1f7e2f5703468deaa75dc8f5f42193d1ad0d
8 changes: 8 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,9 @@ def test_variants(self):
self.assertIs(basic.look_up_self(), basic_lookedup)
self.assertEqual(basic.initialized_count(), expected_init_count)

loaded.module._clear_module_state()
Comment thread
sunmy2019 marked this conversation as resolved.
Outdated


Comment thread
sunmy2019 marked this conversation as resolved.
Outdated
def test_basic_reloaded(self):
# m_copy is copied into the existing module object.
# Global state is not changed.
Expand Down Expand Up @@ -2413,6 +2416,11 @@ def test_with_reinit_reloaded(self):

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

if name == f'{self.NAME}_with_state':
loaded.module._clear_module_state()
reloaded.module._clear_module_state()
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated


Comment thread
sunmy2019 marked this conversation as resolved.
Outdated
# Currently, for every single-phrase init module loaded
# in multiple interpreters, those interpreters share a
# PyModuleDef for that object, which can be a problem.
Expand Down
21 changes: 20 additions & 1 deletion Modules/_testsinglephase.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ 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)) {
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
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 +426,7 @@ PyInit__testsinglephase_with_reinit(void)
/* the _testsinglephase_with_state module */
/******************************************/

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

Expand Down