Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,36 +390,36 @@ def addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False)
if not sitedircase in known_paths:
sys.path.append(sitedir) # Add path component
known_paths.add(sitedircase)
try:
names = os.listdir(sitedir)
except OSError:
return
try:
names = os.listdir(sitedir)
except OSError:
return

# The following phases are defined by PEP 829.
# Phases 1-3: Read .pth files, accumulating paths and import lines.
pth_names = sorted(
name for name in names
if name.endswith(".pth") and not name.startswith(".")
)
for name in pth_names:
_read_pth_file(sitedir, name, known_paths)

# Phases 6-7: Discover .start files and accumulate their entry points.
# Import lines from .pth files with a matching .start file are discarded
# at flush time by _exec_imports().
start_names = sorted(
name for name in names
if name.endswith(".start") and not name.startswith(".")
)
for name in start_names:
_read_start_file(sitedir, name)

# Generally, when addsitedir() is called explicitly, we'll want to process
# all the startup file data immediately. However, when called through
# main(), we'll want to batch up all the startup file processing. main()
# will set this flag to True to defer processing.
if not defer_processing_start_files:
process_startup_files()
# The following phases are defined by PEP 829.
# Phases 1-3: Read .pth files, accumulating paths and import lines.
pth_names = sorted(
name for name in names
if name.endswith(".pth") and not name.startswith(".")
)
for name in pth_names:
_read_pth_file(sitedir, name, known_paths)

# Phases 6-7: Discover .start files and accumulate their entry points.
# Import lines from .pth files with a matching .start file are discarded
# at flush time by _exec_imports().
start_names = sorted(
name for name in names
if name.endswith(".start") and not name.startswith(".")
)
for name in start_names:
_read_start_file(sitedir, name)

# Generally, when addsitedir() is called explicitly, we'll want to process
# all the startup file data immediately. However, when called through
# main(), we'll want to batch up all the startup file processing. main()
# will set this flag to True to defer processing.
if not defer_processing_start_files:
process_startup_files()

if reset:
known_paths = None
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def pth_file_tests(self, pth_file):
"%s not in sys.modules" % pth_file.imported)
self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
self.assertFalse(os.path.exists(pth_file.bad_dir_path))
self.assertFalse(os.path.exists(pth_file.idempotent_fail_path))
Comment thread
johnslavik marked this conversation as resolved.

def test_addpackage(self):
# Make sure addpackage() imports if the line starts with 'import',
Expand Down Expand Up @@ -202,6 +203,16 @@ def test_addsitedir_explicit_flush(self):
site.process_startup_files()
self.pth_file_tests(pth_file)

def test_addsitedir_idempotent(self):
pth_file = PthFile()
pth_file.cleanup(prep=True)

with pth_file.create():
dirs = set()
dirs = site.addsitedir(pth_file.base_dir, dirs)
dirs = site.addsitedir(pth_file.base_dir, dirs)
self.pth_file_tests(pth_file)

def test_addsitedir_dotfile(self):
pth_file = PthFile('.dotfile')
# Ensure we have a clean slate.
Expand Down Expand Up @@ -411,6 +422,7 @@ def __init__(self, filename_base=TESTFN, imported="time",
self.bad_dirname = bad_dirname
self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
self.idempotent_fail_path = os.path.join(self.base_dir, 'idempotent')

@contextlib.contextmanager
def create(self):
Expand All @@ -427,6 +439,13 @@ def create(self):
try:
print("#import @bad module name", file=FILE)
print("\n", file=FILE)

PROG = f'''\
if {self.imported!r} in sys.modules:
open({self.idempotent_fail_path!r}, 'a+').close()
'''
print(f"import sys; exec({PROG!r})", file=FILE)
Comment on lines +443 to +447
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this (zero effort because dedent is already imported)

Suggested change
PROG = f'''\
if {self.imported!r} in sys.modules:
open({self.idempotent_fail_path!r}, 'a+').close()
'''
print(f"import sys; exec({PROG!r})", file=FILE)
PROG = dedent(f'''\
if {self.imported!r} in sys.modules:
open({self.idempotent_fail_path!r}, 'a+').close()
''')
print(f"import sys; exec({PROG!r})", file=FILE)


print("import %s" % self.imported, file=FILE)
print(self.good_dirname, file=FILE)
print(self.bad_dirname, file=FILE)
Expand Down Expand Up @@ -455,6 +474,8 @@ def cleanup(self, prep=False):
os.rmdir(self.good_dir_path)
if os.path.exists(self.bad_dir_path):
os.rmdir(self.bad_dir_path)
if os.path.exists(self.idempotent_fail_path):
os.remove(self.idempotent_fail_path)

class ImportSideEffectTests(unittest.TestCase):
"""Test side-effects from importing 'site'."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid re-executing ``.pth`` files when :func:`site.addsitedir` is called for a known directory.
Loading