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