Skip to content
Merged
Changes from all commits
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
13 changes: 11 additions & 2 deletions Lib/test/test_threaded_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import time
import shutil
import unittest
from unittest import mock
from test.support import (
verbose, import_module, run_unittest, TESTFN, reap_threads,
forget, unlink, rmtree, start_threads)
Expand All @@ -37,6 +38,12 @@ def task(N, done, done_tasks, errors):
if finished:
done.set()

def mock_register_at_fork(func):
# bpo-30599: Mock os.register_at_fork() when importing the random module,
# since this function doesn't allow to unregister callbacks and would leak
# memory.
return mock.patch('os.register_at_fork', create=True)(func)

# Create a circular import structure: A -> C -> B -> D -> A
# NOTE: `time` is already loaded and therefore doesn't threaten to deadlock.

Expand Down Expand Up @@ -97,7 +104,8 @@ def tearDown(self):
if self.old_random is not None:
sys.modules['random'] = self.old_random

def check_parallel_module_init(self):
@mock_register_at_fork
def check_parallel_module_init(self, mock_os):
if imp.lock_held():
# This triggers on, e.g., from test import autotest.
raise unittest.SkipTest("can't run when import lock is held")
Expand Down Expand Up @@ -214,7 +222,8 @@ def import_ba():
t2.join()
self.assertEqual(set(results), {'a', 'b'})

def test_side_effect_import(self):
@mock_register_at_fork
def test_side_effect_import(self, mock_os):
code = """if 1:
import threading
def target():
Expand Down