|
5 | 5 | import unittest
|
6 | 6 | import unittest.mock
|
7 | 7 | import queue as pyqueue
|
| 8 | +import textwrap |
8 | 9 | import time
|
9 | 10 | import io
|
10 | 11 | import itertools
|
@@ -5763,28 +5764,30 @@ def test_namespace(self):
|
5763 | 5764 | class TestNamedResource(unittest.TestCase):
|
5764 | 5765 | def test_global_named_resource_spawn(self):
|
5765 | 5766 | #
|
5766 |
| - # Check that global named resources in main module |
| 5767 | + # gh-90549: Check that global named resources in main module |
5767 | 5768 | # will not leak by a subprocess, in spawn context.
|
5768 | 5769 | #
|
5769 |
| - with os_helper.temp_dir() as tmp_dir: |
5770 |
| - source = os.path.join(tmp_dir, 'source.py') |
5771 |
| - with open(source, 'w') as f: |
5772 |
| - f.write('''if 1: |
5773 |
| - import multiprocessing as mp |
| 5770 | + testfn = os_helper.TESTFN |
| 5771 | + self.addCleanup(os_helper.unlink, testfn) |
| 5772 | + with open(testfn, 'w', encoding='utf-8') as f: |
| 5773 | + f.write(textwrap.dedent('''\ |
| 5774 | + import multiprocessing as mp |
5774 | 5775 |
|
5775 |
| - ctx = mp.get_context('spawn') |
| 5776 | + ctx = mp.get_context('spawn') |
5776 | 5777 |
|
5777 |
| - global_resource = ctx.Semaphore() |
| 5778 | + global_resource = ctx.Semaphore() |
5778 | 5779 |
|
5779 |
| - def submain(): pass |
| 5780 | + def submain(): pass |
5780 | 5781 |
|
5781 |
| - if __name__ == '__main__': |
5782 |
| - p = ctx.Process(target=submain) |
5783 |
| - p.start() |
5784 |
| - p.join() |
5785 |
| - ''') |
5786 |
| - rc, out, err = test.support.script_helper.assert_python_ok(source) |
5787 |
| - self.assertNotRegex(err, b'resource_tracker: There appear to be .* leaked') |
| 5782 | + if __name__ == '__main__': |
| 5783 | + p = ctx.Process(target=submain) |
| 5784 | + p.start() |
| 5785 | + p.join() |
| 5786 | + ''')) |
| 5787 | + rc, out, err = test.support.script_helper.assert_python_ok(testfn) |
| 5788 | + # on error, err = 'UserWarning: resource_tracker: There appear to |
| 5789 | + # be 1 leaked semaphore objects to clean up at shutdown' |
| 5790 | + self.assertEqual(err, b'') |
5788 | 5791 |
|
5789 | 5792 |
|
5790 | 5793 | class MiscTestCase(unittest.TestCase):
|
|
0 commit comments