aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/libregrtest/main.py')
-rw-r--r--Lib/test/libregrtest/main.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py
index beee0fa0950..a9725fa9673 100644
--- a/Lib/test/libregrtest/main.py
+++ b/Lib/test/libregrtest/main.py
@@ -306,7 +306,7 @@ class Regrtest:
else:
tracer = None
- save_modules = sys.modules.keys()
+ save_modules = set(sys.modules)
jobs = runtests.get_jobs()
if jobs is not None:
@@ -330,10 +330,18 @@ class Regrtest:
result = self.run_test(test_name, runtests, tracer)
- # Unload the newly imported modules (best effort finalization)
- for module in sys.modules.keys():
- if module not in save_modules and module.startswith("test."):
- support.unload(module)
+ # Unload the newly imported test modules (best effort finalization)
+ new_modules = [module for module in sys.modules
+ if module not in save_modules and
+ module.startswith(("test.", "test_"))]
+ for module in new_modules:
+ sys.modules.pop(module, None)
+ # Remove the attribute of the parent module.
+ parent, _, name = module.rpartition('.')
+ try:
+ delattr(sys.modules[parent], name)
+ except (KeyError, AttributeError):
+ pass
if result.must_stop(self.fail_fast, self.fail_env_changed):
break