Skip to content

Commit 329f8d1

Browse files
[3.12] gh-113628: Fix test_site test with long stdlib paths (GH-113640) (#113671)
gh-113628: Fix test_site test with long stdlib paths (GH-113640) (cherry picked from commit 5dc79e3) Co-authored-by: Itamar Oren <[email protected]>
1 parent 499f1d0 commit 329f8d1

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
@@ -603,10 +603,24 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
603603
sys_path.append(abs_path)
604604
return sys_path
605605

606+
def _get_pth_lines(self, libpath: str, *, import_site: bool):
607+
pth_lines = ['fake-path-name']
608+
# include 200 lines of `libpath` in _pth lines (or fewer
609+
# if the `libpath` is long enough to get close to 32KB
610+
# see https://github.com/python/cpython/issues/113628)
611+
encoded_libpath_length = len(libpath.encode("utf-8"))
612+
repetitions = min(200, 30000 // encoded_libpath_length)
613+
if repetitions <= 2:
614+
self.skipTest(
615+
f"Python stdlib path is too long ({encoded_libpath_length:,} bytes)")
616+
pth_lines.extend(libpath for _ in range(repetitions))
617+
pth_lines.extend(['', '# comment'])
618+
if import_site:
619+
pth_lines.append('import site')
620+
return pth_lines
621+
606622
@support.requires_subprocess()
607623
def test_underpth_basic(self):
608-
libpath = test.support.STDLIB_DIR
609-
exe_prefix = os.path.dirname(sys.executable)
610624
pth_lines = ['#.', '# ..', *sys.path, '.', '..']
611625
exe_file = self._create_underpth_exe(pth_lines)
612626
sys_path = self._calc_sys_path_for_underpth_nosite(
@@ -628,12 +642,7 @@ def test_underpth_basic(self):
628642
def test_underpth_nosite_file(self):
629643
libpath = test.support.STDLIB_DIR
630644
exe_prefix = os.path.dirname(sys.executable)
631-
pth_lines = [
632-
'fake-path-name',
633-
*[libpath for _ in range(200)],
634-
'',
635-
'# comment',
636-
]
645+
pth_lines = self._get_pth_lines(libpath, import_site=False)
637646
exe_file = self._create_underpth_exe(pth_lines)
638647
sys_path = self._calc_sys_path_for_underpth_nosite(
639648
os.path.dirname(exe_file),
@@ -657,13 +666,8 @@ def test_underpth_nosite_file(self):
657666
def test_underpth_file(self):
658667
libpath = test.support.STDLIB_DIR
659668
exe_prefix = os.path.dirname(sys.executable)
660-
exe_file = self._create_underpth_exe([
661-
'fake-path-name',
662-
*[libpath for _ in range(200)],
663-
'',
664-
'# comment',
665-
'import site'
666-
])
669+
exe_file = self._create_underpth_exe(
670+
self._get_pth_lines(libpath, import_site=True))
667671
sys_prefix = os.path.dirname(exe_file)
668672
env = os.environ.copy()
669673
env['PYTHONPATH'] = 'from-env'
@@ -682,13 +686,8 @@ def test_underpth_file(self):
682686
def test_underpth_dll_file(self):
683687
libpath = test.support.STDLIB_DIR
684688
exe_prefix = os.path.dirname(sys.executable)
685-
exe_file = self._create_underpth_exe([
686-
'fake-path-name',
687-
*[libpath for _ in range(200)],
688-
'',
689-
'# comment',
690-
'import site'
691-
], exe_pth=False)
689+
exe_file = self._create_underpth_exe(
690+
self._get_pth_lines(libpath, import_site=True), exe_pth=False)
692691
sys_prefix = os.path.dirname(exe_file)
693692
env = os.environ.copy()
694693
env['PYTHONPATH'] = 'from-env'

0 commit comments

Comments
 (0)