Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-113659: Skip hidden .pth files#113660
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 ourterms of service andprivacy 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 from1 commit
499fb957da1ed4348130ddfbc6ef59ee5f30cb35191cef7cdFile 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
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -74,6 +74,7 @@ | ||
| import builtins | ||
| import _sitebuiltins | ||
| import io | ||
| import stat | ||
| # Prefixes for site-packages; add additional prefixes like /usr/local here | ||
| PREFIXES = [sys.prefix, sys.exec_prefix] | ||
| @@ -168,6 +169,14 @@ def addpackage(sitedir, name, known_paths): | ||
| else: | ||
| reset = False | ||
| fullname = os.path.join(sitedir, name) | ||
| try: | ||
| st = os.lstat(fullname) | ||
| except OSError: | ||
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. Hum, maybe add a _trace() call to help debugging such issue. Something like: MemberAuthor 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. When If we want to add a 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. Ah, I didn't notice. In this case, would you mind to add a comment (in the 2 places) to explain that ignoring silently the file on OSError is a deliberate choice? MemberAuthor 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. Most 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. As you want, I already approved the PR. | ||
| return | ||
Comment on lines +172 to +175 Contributor 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. I'm slightly worried about the performance impact of calling stat on all On the other hand, the overhead should be neglectable as the inode needs to be read in memory anyway to actually read the contents. | ||
| if ((getattr(st, 'st_flags', 0) & stat.UF_HIDDEN) or | ||
Contributor 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. Which platforms support On macOS the flag is a hint that's ignored by command-line tools. E.g.: ronald@Pelargir[0]% touch foo.txt [~/Projects/Forks/cpython/build/X]ronald@Pelargir[0]% chflags hidden foo.txt [~/Projects/Forks/cpython/build/X]ronald@Pelargir[0]% ls -l (gh-53502-long-times)cpythontotal 0-rw-r--r-- 1 ronald staff 0 Jan 2 21:10 foo.txtronald@Pelargir[0]% ls -lO total 0-rw-r--r-- 1 ronald staff hidden 0 Jan 2 21:10 foo.txt This matches the documentation in #defineUF_HIDDEN 0x00008000/* hint that this item should not be *//* displayed in a GUI */ | ||
| (getattr(st, 'st_file_attributes', 0) & stat.FILE_ATTRIBUTE_HIDDEN)): | ||
| _trace(f"Skipping hidden .pth file: {fullname!r}") | ||
| return | ||
| _trace(f"Processing .pth file: {fullname!r}") | ||
| try: | ||
| # locale encoding is not ideal especially on Windows. But we have used | ||
| @@ -221,7 +230,8 @@ def addsitedir(sitedir, known_paths=None): | ||
| names = os.listdir(sitedir) | ||
| except OSError: | ||
| return | ||
| names = [name for name in names | ||
| if name.endswith(".pth") and not name.startswith(".")] | ||
| for name in sorted(names): | ||
| addpackage(sitedir, name, known_paths) | ||
| if reset: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -194,6 +194,31 @@ def test_addsitedir(self): | ||
| finally: | ||
| pth_file.cleanup() | ||
| def test_addsitedir_dotfile(self): | ||
| pth_file = PthFile('.dotfile') | ||
| pth_file.cleanup(prep=True) | ||
| try: | ||
| pth_file.create() | ||
| site.addsitedir(pth_file.base_dir, set()) | ||
| self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path) | ||
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. Maybe mock site.addpackage() and check that it's not called, rather than checking effects of site.addpackage() implementation. Same remark for the other added tests. MemberAuthor 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. It is not called for dotfiles, but is called for files with hidden attribute. It is rather an implementation detail where we add a filter. | ||
| self.assertIn(pth_file.base_dir, sys.path) | ||
| finally: | ||
| pth_file.cleanup() | ||
| @unittest.skipUnless(hasattr(os, 'chflags'), 'test needs os.chflags()') | ||
| def test_addsitedir_hidden_pth_file(self): | ||
| pth_file = PthFile() | ||
| pth_file.cleanup(prep=True) | ||
| try: | ||
| pth_file.create() | ||
| st = os.stat(pth_file.file_path) | ||
| os.chflags(target_file, st.st_flags | stat.UF_IMMUTABLE) | ||
| site.addsitedir(pth_file.base_dir, set()) | ||
| self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path) | ||
| self.assertIn(pth_file.base_dir, sys.path) | ||
| finally: | ||
| pth_file.cleanup() | ||
| # This tests _getuserbase, hence the double underline | ||
| # to distinguish from a test for getuserbase | ||
| def test__getuserbase(self): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Skip ``.pth`` files with names starting with a dot or hidden file attribute. |