bpo-33689: Blank lines in .pth file cause a duplicate sys.path entry … · python/cpython@0c71a66 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -170,6 +170,8 @@ def addpackage(sitedir, name, known_paths):
170 170 for n, line in enumerate(f):
171 171 if line.startswith("#"):
172 172 continue
173 +if line.strip() == "":
174 +continue
173 175 try:
174 176 if line.startswith(("import ", "import\t")):
175 177 exec(line)
Original file line number Diff line number Diff line change
@@ -161,6 +161,12 @@ def test_addpackage_import_bad_exec(self):
161 161 self.assertRegex(err_out.getvalue(), 'Traceback')
162 162 self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
163 163
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 +
164 170 def test_addpackage_import_bad_pth_file(self):
165 171 # Issue 5258
166 172 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
@@ -595,7 +601,6 @@ def test_startup_interactivehook_isolated_explicit(self):
595 601 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
596 602 self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
597 603
598 -
599 604 @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
600 605 class _pthFileTests(unittest.TestCase):
601 606
Original file line number Diff line number Diff 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.