Commit ec358517 authored by Brett Cannon's avatar Brett Cannon

Issue #26587: Allow .pth files to specify file paths as well as

directories.

Thanks to Wolfgang Langner for the bug report and initial version of
the patch.
parent a3b5dc0f
...@@ -251,6 +251,14 @@ Previously, names of properties and slots which were not yet created on ...@@ -251,6 +251,14 @@ Previously, names of properties and slots which were not yet created on
an instance were excluded. (Contributed by Martin Panter in :issue:`25590`.) an instance were excluded. (Contributed by Martin Panter in :issue:`25590`.)
site
----
When specifying paths to add to :attr:`sys.path` in a `.pth` file,
you may now specify file paths on top of directories (e.g. zip files).
(Contributed by Wolfgang Langner in :issue:`26587`).
telnetlib telnetlib
--------- ---------
......
...@@ -131,13 +131,13 @@ def removeduppaths(): ...@@ -131,13 +131,13 @@ def removeduppaths():
def _init_pathinfo(): def _init_pathinfo():
"""Return a set containing all existing directory entries from sys.path""" """Return a set containing all existing file system items from sys.path."""
d = set() d = set()
for dir in sys.path: for item in sys.path:
try: try:
if os.path.isdir(dir): if os.path.exists(item):
dir, dircase = makepath(dir) _, itemcase = makepath(item)
d.add(dircase) d.add(itemcase)
except TypeError: except TypeError:
continue continue
return d return d
...@@ -150,9 +150,9 @@ def addpackage(sitedir, name, known_paths): ...@@ -150,9 +150,9 @@ def addpackage(sitedir, name, known_paths):
""" """
if known_paths is None: if known_paths is None:
known_paths = _init_pathinfo() known_paths = _init_pathinfo()
reset = 1 reset = True
else: else:
reset = 0 reset = False
fullname = os.path.join(sitedir, name) fullname = os.path.join(sitedir, name)
try: try:
f = open(fullname, "r") f = open(fullname, "r")
...@@ -190,9 +190,9 @@ def addsitedir(sitedir, known_paths=None): ...@@ -190,9 +190,9 @@ def addsitedir(sitedir, known_paths=None):
'sitedir'""" 'sitedir'"""
if known_paths is None: if known_paths is None:
known_paths = _init_pathinfo() known_paths = _init_pathinfo()
reset = 1 reset = True
else: else:
reset = 0 reset = False
sitedir, sitedircase = makepath(sitedir) sitedir, sitedircase = makepath(sitedir)
if not sitedircase in known_paths: if not sitedircase in known_paths:
sys.path.append(sitedir) # Add path component sys.path.append(sitedir) # Add path component
......
...@@ -75,7 +75,7 @@ class HelperFunctionsTests(unittest.TestCase): ...@@ -75,7 +75,7 @@ class HelperFunctionsTests(unittest.TestCase):
def test_init_pathinfo(self): def test_init_pathinfo(self):
dir_set = site._init_pathinfo() dir_set = site._init_pathinfo()
for entry in [site.makepath(path)[1] for path in sys.path for entry in [site.makepath(path)[1] for path in sys.path
if path and os.path.isdir(path)]: if path and os.path.exists(path)]:
self.assertIn(entry, dir_set, self.assertIn(entry, dir_set,
"%s from sys.path not found in set returned " "%s from sys.path not found in set returned "
"by _init_pathinfo(): %s" % (entry, dir_set)) "by _init_pathinfo(): %s" % (entry, dir_set))
......
...@@ -815,6 +815,7 @@ Torsten Landschoff ...@@ -815,6 +815,7 @@ Torsten Landschoff
Tino Lange Tino Lange
Glenn Langford Glenn Langford
Andrew Langmead Andrew Langmead
Wolfgang Langner
Detlef Lannert Detlef Lannert
Soren Larsen Soren Larsen
Amos Latteier Amos Latteier
......
...@@ -237,6 +237,9 @@ Core and Builtins ...@@ -237,6 +237,9 @@ Core and Builtins
Library Library
------- -------
- Issue #26587: the site module now allows .pth files to specify files to be
added to sys.path (e.g. zip files).
- Issue #25609: Introduce contextlib.AbstractContextManager and - Issue #25609: Introduce contextlib.AbstractContextManager and
typing.ContextManager. typing.ContextManager.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment