Skip to content

Commit 824c6fd

Browse files
committed
Issue python#29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
2 parents 43fec9b + 5f9193a commit 824c6fd

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

Lib/test/test_site.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -511,16 +511,30 @@ def _cleanup_underpth_exe(self, exe_file):
511511
os.unlink(_pth_file)
512512
os.unlink(exe_file)
513513

514+
@classmethod
515+
def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
516+
sys_path = []
517+
for line in lines:
518+
if not line or line[0] == '#':
519+
continue
520+
abs_path = os.path.abspath(os.path.join(sys_prefix, line))
521+
sys_path.append(abs_path)
522+
return sys_path
523+
514524
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
515525
def test_underpth_nosite_file(self):
516526
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
517527
exe_prefix = os.path.dirname(sys.executable)
518-
exe_file = self._create_underpth_exe([
528+
pth_lines = [
519529
'fake-path-name',
520530
*[libpath for _ in range(200)],
531+
'',
521532
'# comment',
522-
'import site'
523-
])
533+
]
534+
exe_file = self._create_underpth_exe(pth_lines)
535+
sys_path = self._calc_sys_path_for_underpth_nosite(
536+
os.path.dirname(exe_file),
537+
pth_lines)
524538

525539
try:
526540
env = os.environ.copy()
@@ -529,14 +543,11 @@ def test_underpth_nosite_file(self):
529543
rc = subprocess.call([exe_file, '-c',
530544
'import sys; sys.exit(sys.flags.no_site and '
531545
'len(sys.path) > 200 and '
532-
'%r in sys.path and %r in sys.path and %r not in sys.path)' % (
533-
os.path.join(sys.prefix, 'fake-path-name'),
534-
libpath,
535-
os.path.join(sys.prefix, 'from-env'),
536-
)], env=env)
546+
'sys.path == %r)' % sys_path,
547+
], env=env)
537548
finally:
538549
self._cleanup_underpth_exe(exe_file)
539-
self.assertEqual(rc, 0)
550+
self.assertTrue(rc, "sys.path is incorrect")
540551

541552
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
542553
def test_underpth_file(self):
@@ -545,23 +556,26 @@ def test_underpth_file(self):
545556
exe_file = self._create_underpth_exe([
546557
'fake-path-name',
547558
*[libpath for _ in range(200)],
559+
'',
548560
'# comment',
549561
'import site'
550562
])
563+
sys_prefix = os.path.dirname(exe_file)
551564
try:
552565
env = os.environ.copy()
553566
env['PYTHONPATH'] = 'from-env'
554567
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
555568
rc = subprocess.call([exe_file, '-c',
556569
'import sys; sys.exit(not sys.flags.no_site and '
557-
'%r in sys.path and %r in sys.path and %r not in sys.path)' % (
558-
os.path.join(sys.prefix, 'fake-path-name'),
570+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
571+
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
572+
os.path.join(sys_prefix, 'fake-path-name'),
559573
libpath,
560-
os.path.join(sys.prefix, 'from-env'),
574+
os.path.join(sys_prefix, 'from-env'),
561575
)], env=env)
562576
finally:
563577
self._cleanup_underpth_exe(exe_file)
564-
self.assertEqual(rc, 0)
578+
self.assertTrue(rc, "sys.path is incorrect")
565579

566580

567581
if __name__ == "__main__":

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ John Interrante
684684
Bob Ippolito
685685
Roger Irwin
686686
Atsuo Ishimoto
687+
Alexey Izbyshev
687688
Kasia Jachim
688689
Adam Jackson
689690
Ben Jackson

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,8 @@ Library
871871
Windows
872872
-------
873873

874+
- Issue #29326: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)
875+
874876
- Issue #28164: Correctly handle special console filenames (patch by Eryk Sun)
875877

876878
- Issue #29409: Implement PEP 529 for io.FileIO (Patch by Eryk Sun)

PC/getpathp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ read_pth_file(const wchar_t *path, wchar_t *prefix, int *isolated, int *nosite)
560560
char *p = fgets(line, MAXPATHLEN + 1, sp_file);
561561
if (!p)
562562
break;
563-
if (*p == '\0' || *p == '#')
563+
if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#')
564564
continue;
565565
while (*++p) {
566566
if (*p == '\r' || *p == '\n') {

0 commit comments

Comments
 (0)