Skip to content

Commit 6a74bea

Browse files
committed
add tests
1 parent ab905ba commit 6a74bea

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lib/multiprocessing/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def is_abstract_socket_namespace(address):
173173
len(os.path.sep) + _TMPPYMP_PREFIXLEN + _TMPPYMP_SUFFIXLEN +
174174
len(os.path.sep) + _TMPSOCK_PREFIXLEN + _TMPSOCK_SUFFIXLEN
175175
)
176+
assert _SUN_PATH_LEN_RESERVED < _SUN_PATH_MAX
176177

177178

178179
def _remove_temp_dir(rmtree, tempdir):
@@ -184,6 +185,7 @@ def _remove_temp_dir(rmtree, tempdir):
184185
if current_process is not None:
185186
current_process._config['tempdir'] = None
186187

188+
187189
def _get_base_temp_dir(tempfile):
188190
"""Get a temporary directory where socket files will be created.
189191
@@ -232,6 +234,7 @@ def _get_base_temp_dir(tempfile):
232234
assert len(base_system_tempdir) + _SUN_PATH_LEN_RESERVED < _SUN_PATH_MAX
233235
return base_system_tempdir
234236

237+
235238
def get_temp_dir():
236239
# get name of a temp directory which will be automatically cleaned up
237240
tempdir = process.current_process()._config.get('tempdir')
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1+
import os
2+
import re
13
import unittest
4+
from test.support import os_helper
5+
from multiprocessing.util import _SUN_PATH_MAX
26
from test._test_multiprocessing import install_tests_in_module_dict
7+
from test.support import script_helper
38

49
install_tests_in_module_dict(globals(), 'forkserver', exclude_types=True)
510

11+
12+
class TestForkServerConfiguration(unittest.TestCase):
13+
def test_respect_sun_path_max(self):
14+
# Ensure that the calculation for temporary filepath lengths is correct.
15+
# See https://github.com/python/cpython/issues/149527.
16+
17+
cmd = '''if 1:
18+
from multiprocessing.connection import arbitrary_address
19+
from multiprocessing.util import get_temp_dir
20+
if __name__ == "__main__":
21+
print(get_temp_dir())
22+
print(arbitrary_address("AF_UNIX"))
23+
'''
24+
with os_helper.temp_dir() as root:
25+
self.assertLess(len(root), _SUN_PATH_MAX)
26+
_, out, _ = script_helper.assert_python_ok('-c', cmd, TMPDIR=root)
27+
res = out.decode().strip().splitlines()
28+
self.assertEqual(len(res), 2)
29+
30+
temp_pymp = res[0]
31+
temp_pymp_regex = os.path.join(re.escape(root), r"pymp-\w{8}")
32+
self.assertRegex(temp_pymp, temp_pymp_regex)
33+
34+
temp_sock = res[1]
35+
temp_sock_regex = os.path.join(temp_pymp_regex, r"sock-[0-9a-fA-F]{12}")
36+
self.assertRegex(temp_sock, temp_sock_regex)
37+
38+
639
if __name__ == '__main__':
740
unittest.main()

0 commit comments

Comments
 (0)