@@ -603,10 +603,24 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
603
603
sys_path .append (abs_path )
604
604
return sys_path
605
605
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
+
606
622
@support .requires_subprocess ()
607
623
def test_underpth_basic (self ):
608
- libpath = test .support .STDLIB_DIR
609
- exe_prefix = os .path .dirname (sys .executable )
610
624
pth_lines = ['#.' , '# ..' , * sys .path , '.' , '..' ]
611
625
exe_file = self ._create_underpth_exe (pth_lines )
612
626
sys_path = self ._calc_sys_path_for_underpth_nosite (
@@ -628,12 +642,7 @@ def test_underpth_basic(self):
628
642
def test_underpth_nosite_file (self ):
629
643
libpath = test .support .STDLIB_DIR
630
644
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 )
637
646
exe_file = self ._create_underpth_exe (pth_lines )
638
647
sys_path = self ._calc_sys_path_for_underpth_nosite (
639
648
os .path .dirname (exe_file ),
@@ -657,13 +666,8 @@ def test_underpth_nosite_file(self):
657
666
def test_underpth_file (self ):
658
667
libpath = test .support .STDLIB_DIR
659
668
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 ))
667
671
sys_prefix = os .path .dirname (exe_file )
668
672
env = os .environ .copy ()
669
673
env ['PYTHONPATH' ] = 'from-env'
@@ -682,13 +686,8 @@ def test_underpth_file(self):
682
686
def test_underpth_dll_file (self ):
683
687
libpath = test .support .STDLIB_DIR
684
688
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 )
692
691
sys_prefix = os .path .dirname (exe_file )
693
692
env = os .environ .copy ()
694
693
env ['PYTHONPATH' ] = 'from-env'
0 commit comments