8000 clear module state after use · python/cpython@f4de1f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit f4de1f7

Browse files
committed
clear module state after use
1 parent d14eb34 commit f4de1f7

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Lib/test/test_import/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,9 @@ def test_variants(self):
23302330
self.assertIs(basic.look_up_self(), basic_lookedup)
23312331
self.assertEqual(basic.initialized_count(), expected_init_count)
23322332

2333+
loaded.module._clear_module_state()
2334+
2335+
23332336
def test_basic_reloaded(self):
23342337
# m_copy is copied into the existing module object.
23352338
# Global state is not changed.
@@ -2413,6 +2416,11 @@ def test_with_reinit_reloaded(self):
24132416

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

2419+
if name == f'{self.NAME}_with_state':
2420+
loaded.module._clear_module_state()
2421+
reloaded.module._clear_module_state()
2422+
2423+
24162424
# Currently, for every single-phrase init module loaded
24172425
# in multiple interpreters, those interpreters share a
24182426
# PyModuleDef for that object, which can be a problem.

Modules/_testsinglephase.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ basic__clear_globals(PyObject *self, PyObject *Py_UNUSED(ignored))
248248
basic__clear_globals_doc}
249249

250250

251+
PyDoc_STRVAR(basic__clear_module_state_doc, "_clear_module_state()\n\
252+
\n\
253+
Free the module state and set it to uninitialized.");
254+
255+
static PyObject*
256+
basic__clear_module_state(PyObject *self, PyObject *Py_UNUSED(ignored)) {
257+
module_state *state = get_module_state(self);
258+
if (state != NULL) {
259+
clear_state(state);
260+
}
261+
Py_RETURN_NONE;
262+
}
263+
264+
#define _CLEAR_MODULE_STATE_METHODDEF \
265+
{"_clear_module_state", basic__clear_module_state, METH_NOARGS, \
266+
basic__clear_module_state_doc}
267+
268+
251269
/*********************************************/
252270
/* the _testsinglephase module (and aliases) */
253271
/*********************************************/
@@ -408,7 +426,7 @@ PyInit__testsinglephase_with_reinit(void)
408426
/* the _testsinglephase_with_state module */
409427
/******************************************/
410428

411-
/* This ia less typical of legacy extensions in the wild:
429+
/* This is a less typical of legacy extensions in the wild:
412430
- single-phase init (same as _testsinglephase above)
413431
- has some module state
414432
- supports repeated initialization
@@ -424,6 +442,7 @@ static PyMethodDef TestMethods_WithState[] = {
424442
LOOK_UP_SELF_METHODDEF,
425443
SUM_METHODDEF,
426444
STATE_INITIALIZED_METHODDEF,
445+
_CLEAR_MODULE_STATE_METHODDEF,
427446
{NULL, NULL} /* sentinel */
428447
};
429448

0 commit comments

Comments
 (0)
0