Skip to content

Commit 93e9381

Browse files
[3.11] gh-113628: Fix test_site test with long stdlib paths (GH-113640) (#113672)
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 1c381ec commit 93e9381

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

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

0 commit comments

Comments
 (0)