]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33689: Blank lines in .pth file cause a duplicate sys.path entry (GH-20679)
authoridomic <michael.ido@gmail.com>
Sat, 19 Sep 2020 19:13:29 +0000 (15:13 -0400)
committerGitHub <noreply@github.com>
Sat, 19 Sep 2020 19:13:29 +0000 (22:13 +0300)
Lib/site.py
Lib/test/test_site.py
Misc/NEWS.d/next/Library/2020-06-06-14-09-55.bpo-33689.EFUDH7.rst [new file with mode: 0644]

index 4c095774729c5e6d7e08bc24466f2a7639c9fff9..4d3b869fff77a0e5d5b22e3867182da94df2c994 100644 (file)
@@ -170,6 +170,8 @@ def addpackage(sitedir, name, known_paths):
         for n, line in enumerate(f):
             if line.startswith("#"):
                 continue
+            if line.strip() == "":
+                continue
             try:
                 if line.startswith(("import ", "import\t")):
                     exec(line)
index 97b5c5de95bbc2425cc15c02008be3aa4f01b90f..d3ee68facdbc3defcde106ae5fd49e3f38922387 100644 (file)
@@ -161,6 +161,12 @@ class HelperFunctionsTests(unittest.TestCase):
         self.assertRegex(err_out.getvalue(), 'Traceback')
         self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
 
+    def test_addpackage_empty_lines(self):
+        # Issue 33689
+        pth_dir, pth_fn = self.make_pth("\n\n  \n\n")
+        known_paths = site.addpackage(pth_dir, pth_fn, set())
+        self.assertEqual(known_paths, set())
+
     def test_addpackage_import_bad_pth_file(self):
         # Issue 5258
         pth_dir, pth_fn = self.make_pth("abc\x00def\n")
@@ -595,7 +601,6 @@ class StartupImportTests(unittest.TestCase):
             'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
         self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
 
-
 @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
 class _pthFileTests(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2020-06-06-14-09-55.bpo-33689.EFUDH7.rst b/Misc/NEWS.d/next/Library/2020-06-06-14-09-55.bpo-33689.EFUDH7.rst
new file mode 100644 (file)
index 0000000..bc0756d
--- /dev/null
@@ -0,0 +1,4 @@
+Ignore empty or whitespace-only lines in .pth files. This matches the\r
+documentated behavior. Before, empty lines caused the site-packages\r
+dir to appear multiple times in sys.path.\r
+By Ido Michael, contributors Malcolm Smith and Tal Einat.\r