Skip to content

Commit 1555841

Browse files
itamaroaisk
authored andcommitted
pythongh-113628: Fix test_site test with long stdlib paths (python#113640)
1 parent 5199195 commit 1555841

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Lib/test/test_site.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,24 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
641641
sys_path.append(abs_path)
642642
return sys_path
643643

644+
def _get_pth_lines(self, libpath: str, *, import_site: bool):
645+
pth_lines = ['fake-path-name']
646+
# include 200 lines of `libpath` in _pth lines (or fewer
647+
# if the `libpath` is long enough to get close to 32KB
648+
# see https://github.com/python/cpython/issues/113628)
649+
encoded_libpath_length = len(libpath.encode("utf-8"))
650+
repetitions = min(200, 30000 // encoded_libpath_length)
651+
if repetitions <= 2:
652+
self.skipTest(
653+
f"Python stdlib path is too long ({encoded_libpath_length:,} bytes)")
654+
pth_lines.extend(libpath for _ in range(repetitions))
655+
pth_lines.extend(['', '# comment'])
656+
if import_site:
657+
pth_lines.append('import site')
658+
return pth_lines
659+
644660
@support.requires_subprocess()
645661
def test_underpth_basic(self):
646-
libpath = test.support.STDLIB_DIR
647-
exe_prefix = os.path.dirname(sys.executable)
648662
pth_lines = ['#.', '# ..', *sys.path, '.', '..']
649663
exe_file = self._create_underpth_exe(pth_lines)
650664
sys_path = self._calc_sys_path_for_underpth_nosite(
@@ -666,12 +680,7 @@ def test_underpth_basic(self):
666680
def test_underpth_nosite_file(self):
667681
libpath = test.support.STDLIB_DIR
668682
exe_prefix = os.path.dirname(sys.executable)
669-
pth_lines = [
670-
'fake-path-name',
671-
*[libpath for _ in range(200)],
672-
'',
673-
'# comment',
674-
]
683+
pth_lines = self._get_pth_lines(libpath, import_site=False)
675684
exe_file = self._create_underpth_exe(pth_lines)
676685
sys_path = self._calc_sys_path_for_underpth_nosite(
677686
os.path.dirname(exe_file),
@@ -695,13 +704,8 @@ def test_underpth_nosite_file(self):
695704
def test_underpth_file(self):
696705
libpath = test.support.STDLIB_DIR
697706
exe_prefix = os.path.dirname(sys.executable)
698-
exe_file = self._create_underpth_exe([
699-
'fake-path-name',
700-
*[libpath for _ in range(200)],
701-
'',
702-
'# comment',
703-
'import site'
704-
])
707+
exe_file = self._create_underpth_exe(
708+
self._get_pth_lines(libpath, import_site=True))
705709
sys_prefix = os.path.dirname(exe_file)
706710
env = os.environ.copy()
707711
env['PYTHONPATH'] = 'from-env'
@@ -720,13 +724,8 @@ def test_underpth_file(self):
720724
def test_underpth_dll_file(self):
721725
libpath = test.support.STDLIB_DIR
722726
exe_prefix = os.path.dirname(sys.executable)
723-
exe_file = self._create_underpth_exe([
724-
'fake-path-name',
725-
*[libpath for _ in range(200)],
726-
'',
727-
'# comment',
728-
'import site'
729-
], exe_pth=False)
727+
exe_file = self._create_underpth_exe(
728+
self._get_pth_lines(libpath, import_site=True), exe_pth=False)
730729
sys_prefix = os.path.dirname(exe_file)
731730
env = os.environ.copy()
732731
env['PYTHONPATH'] = 'from-env'

0 commit comments

Comments
 (0)