diff options
author | idomic <michael.ido@gmail.com> | 2020-09-19 19:13:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-19 19:13:29 (GMT) |
commit | 0c71a66b53f6ea262aa5a60784c8c625ae0bb98c (patch) | |
tree | 496b2a1bdaedda9a2fd849a8c4ffb8688b14b98c | |
parent | ae0d2a33ec05aece939a959d36fcf1df1e210a08 (diff) | |
download | cpython-0c71a66b53f6ea262aa5a60784c8c625ae0bb98c.zip cpython-0c71a66b53f6ea262aa5a60784c8c625ae0bb98c.tar.gz cpython-0c71a66b53f6ea262aa5a60784c8c625ae0bb98c.tar.bz2 |
bpo-33689: Blank lines in .pth file cause a duplicate sys.path entry (GH-20679)
-rw-r--r-- | Lib/site.py | 2 | ||||
-rw-r--r-- | Lib/test/test_site.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-06-06-14-09-55.bpo-33689.EFUDH7.rst | 4 |
3 files changed, 12 insertions, 1 deletions
diff --git a/Lib/site.py b/Lib/site.py index 4c09577..4d3b869 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -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) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 97b5c5d..d3ee68f 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -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 index 0000000..bc0756d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-06-06-14-09-55.bpo-33689.EFUDH7.rst @@ -0,0 +1,4 @@ +Ignore empty or whitespace-only lines in .pth files. This matches the
+documentated behavior. Before, empty lines caused the site-packages
+dir to appear multiple times in sys.path.
+By Ido Michael, contributors Malcolm Smith and Tal Einat.
|