Skip to content

Commit 0c71a66

Browse files
authored
bpo-33689: Blank lines in .pth file cause a duplicate sys.path entry (GH-20679)
1 parent ae0d2a3 commit 0c71a66

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Lib/site.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def addpackage(sitedir, name, known_paths):
170170
for n, line in enumerate(f):
171171
if line.startswith("#"):
172172
continue
173+
if line.strip() == "":
174+
continue
173175
try:
174176
if line.startswith(("import ", "import\t")):
175177
exec(line)

Lib/test/test_site.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def test_addpackage_import_bad_exec(self):
161161
self.assertRegex(err_out.getvalue(), 'Traceback')
162162
self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
163163

164+
def test_addpackage_empty_lines(self):
165+
# Issue 33689
166+
pth_dir, pth_fn = self.make_pth("\n\n \n\n")
167+
known_paths = site.addpackage(pth_dir, pth_fn, set())
168+
self.assertEqual(known_paths, set())
169+
164170
def test_addpackage_import_bad_pth_file(self):
165171
# Issue 5258
166172
pth_dir, pth_fn = self.make_pth("abc\x00def\n")
@@ -595,7 +601,6 @@ def test_startup_interactivehook_isolated_explicit(self):
595601
'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
596602
self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
597603

598-
599604
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
600605
class _pthFileTests(unittest.TestCase):
601606

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Ignore empty or whitespace-only lines in .pth files. This matches the
2+
documentated behavior. Before, empty lines caused the site-packages
3+
dir to appear multiple times in sys.path.
4+
By Ido Michael, contributors Malcolm Smith and Tal Einat.

0 commit comments

Comments
 (0)