Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-75723: Idempotent .pth execution in site.addsitedir#147951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6c342df96ed0e8c44e192f077abdaeee2ff400547266ebfc9File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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)) | ||||||||||||||||||||||
| def test_addpackage(self): | ||||||||||||||||||||||
| # Make sure addpackage() imports if the line starts with 'import', | ||||||||||||||||||||||
| @@ -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. | ||||||||||||||||||||||
| @@ -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): | ||||||||||||||||||||||
| @@ -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
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this (zero effort because
Suggested change
| ||||||||||||||||||||||
| print("import %s" % self.imported, file=FILE) | ||||||||||||||||||||||
| print(self.good_dirname, file=FILE) | ||||||||||||||||||||||
| print(self.bad_dirname, file=FILE) | ||||||||||||||||||||||
| @@ -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'.""" | ||||||||||||||||||||||
| 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. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.