Skip to content

Commit a8c8526

Browse files
authored
gh-97669: Fix test_tools reference leak (#98216)
test_tools.test_sundry() now uses an unittest mock to prevent the logging module to register a real "atfork" function which kept the logging module dictionary alive. So the logging module can be properly unloaded. Previously, the logging module was loaded before test_sundry(), but it's no longer the case since recent test_tools sub-tests removals.
1 parent 0895c2a commit a8c8526

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

Lib/test/support/import_helper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,11 @@ def modules_cleanup(oldmodules):
246246
# do currently). Implicitly imported *real* modules should be left alone
247247
# (see issue 10556).
248248
sys.modules.update(oldmodules)
249+
250+
251+
def mock_register_at_fork(func):
252+
# bpo-30599: Mock os.register_at_fork() when importing the random module,
253+
# since this function doesn't allow to unregister callbacks and would leak
254+
# memory.
255+
from unittest import mock
256+
return mock.patch('os.register_at_fork', create=True)(func)

Lib/test/test_importlib/test_threaded_import.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import unittest
1616
from unittest import mock
1717
from test.support import verbose
18-
from test.support.import_helper import forget
18+
from test.support.import_helper import forget, mock_register_at_fork
1919
from test.support.os_helper import (TESTFN, unlink, rmtree)
2020
from test.support import script_helper, threading_helper
2121

@@ -41,12 +41,6 @@ def task(N, done, done_tasks, errors):
4141
if finished:
4242
done.set()
4343

44-
def mock_register_at_fork(func):
45-
# bpo-30599: Mock os.register_at_fork() when importing the random module,
46-
# since this function doesn't allow to unregister callbacks and would leak
47-
# memory.
48-
return mock.patch('os.register_at_fork', create=True)(func)
49-
5044
# Create a circular import structure: A -> C -> B -> D -> A
5145
# NOTE: `time` is already loaded and therefore doesn't threaten to deadlock.
5246

Lib/test/test_tools/test_sundry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class TestSundryScripts(unittest.TestCase):
2727

2828
skiplist = denylist + allowlist + other
2929

30-
def test_sundry(self):
30+
# import logging registers "atfork" functions which keep indirectly the
31+
# logging module dictionary alive. Mock the function to be able to unload
32+
# cleanly the logging module.
33+
@import_helper.mock_register_at_fork
34+
def test_sundry(self, mock_os):
3135
old_modules = import_helper.modules_setup()
3236
try:
3337
for fn in os.listdir(scriptsdir):

0 commit comments

Comments
 (0)