Skip to content
Prev Previous commit
Next Next commit
avoid \, use if 1: modernize the nearby test.
  • Loading branch information
gpshead committed Jul 6, 2023
commit b2e1c13ed9fa90f86eb3cdd985039c9ceadcabf1
30 changes: 14 additions & 16 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5839,29 +5839,27 @@ def test_namespace(self):


class TestNamedResource(unittest.TestCase):
@only_run_in_spawn_testsuite("spawn specific test.")
def test_global_named_resource_spawn(self):
#
# gh-90549: Check that global named resources in main module
# will not leak by a subprocess, in spawn context.
#
testfn = os_helper.TESTFN
self.addCleanup(os_helper.unlink, testfn)
with open(testfn, 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\
import multiprocessing as mp
test_source = """if 1:
import multiprocessing as mp

ctx = mp.get_context('spawn')
ctx = mp.get_context('spawn')

global_resource = ctx.Semaphore()
global_resource = ctx.Semaphore()

def submain(): pass
def submain(): pass

if __name__ == '__main__':
p = ctx.Process(target=submain)
p.start()
p.join()
'''))
rc, out, err = test.support.script_helper.assert_python_ok(testfn)
if __name__ == '__main__':
p = ctx.Process(target=submain)
p.start()
p.join()
"""
rc, out, err = script_helper.assert_python_ok("-c", test_source)
# on error, err = 'UserWarning: resource_tracker: There appear to
# be 1 leaked semaphore objects to clean up at shutdown'
self.assertEqual(err, b'')
Expand All @@ -5879,8 +5877,8 @@ def test_spawn_sys_executable_none_allows_import(self):
# https://github.com/python/cpython/issues/90876 that caused an
# ImportError in multiprocessing when sys.executable was None.
# This can be true in embedded environments.
rc, _, err = script_helper.assert_python_ok(
"-c", """\
rc, out, err = script_helper.assert_python_ok(
"-c", """if 1:
import sys
sys.executable = None
assert "multiprocessing" not in sys.modules, "mp already imported!"
Expand Down