GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-101357: Suppress `OSError` from `pathlib.Path.exists()` and `is_*()` by barneygale · Pull Request #118243 · python/cpython

Conversation

@barneygale

@barneygale barneygale commented Apr 24, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Suppress all OSError exceptions from pathlib.Path.exists() and is_*() rather than a selection of more common errors as we do presently. Also adjust the implementations to call os.path.exists() etc, which are much faster on Windows thanks to GH-101196.

  • Issue: Improve Windows performance of pathlib.Path.is_file and friends #101357

📚 Documentation preview 📚: https://cpython-previews--118243.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@barneygale
pythonGH-101357: Suppress OSError from pathlib.Path.exists() and …
f1eb06c
…`is_*()`

Suppress all `OSError` exceptions from `pathlib.Path.exists()` and `is_*()`
rather than a selection of more common errors as we do presently. Also
adjust the implementations to call `os.path.exists()` etc, which are much
faster on Windows thanks to pythonGH-101196.
@bedevere-app bedevere-app Bot mentioned this pull request Apr 24, 2024
Improve Windows performance of pathlib.Path.is_file and friends #101357
Closed
@barneygale barneygale added performance Performance or resource usage topic-pathlib labels Apr 24, 2024
@nineteendo

nineteendo commented Apr 24, 2024

Copy link
Copy Markdown
Contributor

What do you intend to do with #117858? This pull request includes all its changes (except for the news entry).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Apr 24, 2024

Copy link
Copy Markdown
Contributor Author

They have some overlap but they're not quite the same:

  • The other PR depends on your lexist() speedup landing, whereas this PR does not
  • The other PR maintains pathlib's existing interface, whereas this PR changes something that might be important, and so this PR is more likely to be rejected by reviewers or otherwise spend a long time in review.
nineteendo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale added 2 commits May 8, 2024 19:45
@barneygale
Merge branch 'main' into pythongh-101357
5df1ee0
@barneygale
Add news
dbf9626
@barneygale
barneygale marked this pull request as ready for review May 8, 2024 18:51
@bedevere-app bedevere-app Bot added the awaiting core review label May 8, 2024
@barneygale
barneygale requested a review from zooba May 8, 2024 18:53
@zooba

zooba commented May 10, 2024

Copy link
Copy Markdown
Member

Is it worth pointing out that this brings them into line with the os.path equivalents? And maybe suggesting the right API (I assume stat/lstat) to get more detailed error.

There might also be value in some kind of conceptual comment that these functions mean what they say if they return True, but could mean multiple things if they return False, and hence if you're using them you should be testing for True?

barneygale and leikdga reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented May 10, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback!

There might also be value in some kind of conceptual comment that these functions mean what they say if they return True, but could mean multiple things if they return False, and hence if you're using them you should be testing for True?

It seems quite redundant to me, as the existing docs for each method are clear that False is returned if the path is inaccessible for any reason.

edit: re-reading this comment, I think I came across too strong. If you think it's important, I can include some conceptual remarks as you suggest.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Address review feedback
1842b6d
@zooba

zooba commented May 13, 2024

Copy link
Copy Markdown
Member

If you think it's important, I can include some conceptual remarks as you suggest.

I think it's helpful for people who come to our docs not just for reference, but to find out what they should do. But I also can't come up with great wording that doesn't have so many qualifiers that you'd need to be an expert to understand it anyway.

Maybe it's a matter of phrasing it more like: "Returns True if the path refers to a file; otherwise, False. Paths that are inaccessible will return False, as will those that are not files. Use exists to determine whether the path is accessible, and is_dir or is_link to check for directories or symlinks."

Basically, "if you use this, here's what you'll get; if you actually wanted this, here's what you should do". But it's more of a rewrite, and maybe not worth your time or interest. I'm not going to force it, the current PR is fine.

barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented May 13, 2024

Copy link
Copy Markdown
Contributor Author

Makes sense! I'll experiment and see if I can make that sort of thing work. Ta!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
Address review feedback?
fad9af4
zooba
zooba reviewed May 13, 2024
View reviewed changes
Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
More docs tweaks
a7f8372
zooba
zooba approved these changes May 13, 2024
View reviewed changes

@zooba zooba left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Love it. But do consider other feedback as well, if there's something that can make it even more approachable.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 13, 2024
@barneygale
exists() --> stat()
749731f
@barneygale

barneygale commented May 14, 2024

Copy link
Copy Markdown
Contributor Author

Thanks very much for the review!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
barneygale enabled auto-merge (squash) May 14, 2024 17:37
nineteendo
nineteendo reviewed May 14, 2024
View reviewed changes
Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@barneygale
barneygale merged commit fbe6a09 into python:main May 14, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label May 14, 2024
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
@barneygale @estyxx
pythonGH-101357: Suppress OSError from pathlib.Path.exists() and …
e570e2a
…`is_*()` (python#118243)

Suppress all `OSError` exceptions from `pathlib.Path.exists()` and `is_*()`
rather than a selection of more common errors as we do presently. Also
adjust the implementations to call `os.path.exists()` etc, which are much
faster on Windows thanks to pythonGH-101196.
cjwatson added a commit to cjwatson/b2-sdk-python that referenced this pull request Jan 9, 2026
@cjwatson
Fix test_dir_without_exec_permission on Python 3.14
1590692
One unit test fails on Python 3.14 as follows:

```
_____________ TestFolderTraversal.test_dir_without_exec_permission _____________

self = <test.unit.scan.test_folder_traversal.TestFolderTraversal object at 0x7f5530606350>
tmp_path = PosixPath('/tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0')
fs_perm_tool = <test.unit.conftest.UnixPermTool object at 0x7f55301ca510>

    @pytest.mark.skipif(
        platform.system() == 'Windows',
        reason='Unix-only filesystem permissions are tested',
    )
    def test_dir_without_exec_permission(self, tmp_path, fs_perm_tool):
        """Test that a excluded directory/file without permissions emits warnings."""
        no_perm_dir = tmp_path / 'no_perm_dir'
        no_perm_dir.mkdir()
        (no_perm_dir / 'file.txt').touch()
        (no_perm_dir / 'file2.txt').touch()
        # chmod -x no_perm_dir
        no_perm_dir.chmod(0o600)

        scan_policy = ScanPoliciesManager()
        reporter = ProgressReport(sys.stdout, False)

        folder = LocalFolder(str(tmp_path))
        local_paths = folder.all_files(reporter=reporter, policies_manager=scan_policy)
        absolute_paths = [path.absolute_path for path in local_paths]
        assert not absolute_paths

        # Check that no access warnings are issued for the excluded directory/file
>       assert set(reporter.warnings) == {
            f'WARNING: {tmp_path/"no_perm_dir/file.txt"} could not be accessed (no permissions to read?)',
            f'WARNING: {tmp_path/"no_perm_dir/file2.txt"} could not be accessed (no permissions to read?)',
        }
E       AssertionError: assert {'WARNING: /t...en symlink?)'} == {'WARNING: /t...ns to read?)'}
E
E         Extra items in the left set:
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file2.txt could not be accessed (broken symlink?)'
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file.txt could not be accessed (broken symlink?)'
E         Extra items in the right set:
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file.txt could not be accessed (no permissions to read?)'
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file2.txt could not be accessed (no permissions to read?)'
E         Use -v to get more diff

test/unit/scan/test_folder_traversal.py:721: AssertionError
```

This is due to python/cpython#118243.  I'm not
sure how important it is to preserve the exact same warning message
here, but the conservative approach is to do so;
https://docs.python.org/3/library/pathlib.html advises using `Path.stat`
to distinguish these cases.
cjwatson added a commit to cjwatson/b2-sdk-python that referenced this pull request Jan 9, 2026
@cjwatson
Fix test_dir_without_exec_permission on Python 3.14
be3497b
One unit test fails on Python 3.14 as follows:

```
_____________ TestFolderTraversal.test_dir_without_exec_permission _____________

self = <test.unit.scan.test_folder_traversal.TestFolderTraversal object at 0x7f5530606350>
tmp_path = PosixPath('/tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0')
fs_perm_tool = <test.unit.conftest.UnixPermTool object at 0x7f55301ca510>

    @pytest.mark.skipif(
        platform.system() == 'Windows',
        reason='Unix-only filesystem permissions are tested',
    )
    def test_dir_without_exec_permission(self, tmp_path, fs_perm_tool):
        """Test that a excluded directory/file without permissions emits warnings."""
        no_perm_dir = tmp_path / 'no_perm_dir'
        no_perm_dir.mkdir()
        (no_perm_dir / 'file.txt').touch()
        (no_perm_dir / 'file2.txt').touch()
        # chmod -x no_perm_dir
        no_perm_dir.chmod(0o600)

        scan_policy = ScanPoliciesManager()
        reporter = ProgressReport(sys.stdout, False)

        folder = LocalFolder(str(tmp_path))
        local_paths = folder.all_files(reporter=reporter, policies_manager=scan_policy)
        absolute_paths = [path.absolute_path for path in local_paths]
        assert not absolute_paths

        # Check that no access warnings are issued for the excluded directory/file
>       assert set(reporter.warnings) == {
            f'WARNING: {tmp_path/"no_perm_dir/file.txt"} could not be accessed (no permissions to read?)',
            f'WARNING: {tmp_path/"no_perm_dir/file2.txt"} could not be accessed (no permissions to read?)',
        }
E       AssertionError: assert {'WARNING: /t...en symlink?)'} == {'WARNING: /t...ns to read?)'}
E
E         Extra items in the left set:
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file2.txt could not be accessed (broken symlink?)'
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file.txt could not be accessed (broken symlink?)'
E         Extra items in the right set:
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file.txt could not be accessed (no permissions to read?)'
E         'WARNING: /tmp/pytest-of-sbuild/pytest-0/test_dir_without_exec_permissi0/no_perm_dir/file2.txt could not be accessed (no permissions to read?)'
E         Use -v to get more diff

test/unit/scan/test_folder_traversal.py:721: AssertionError
```

This is due to python/cpython#118243.  I'm not
sure how important it is to preserve the exact same warning message
here, but the conservative approach is to do so;
https://docs.python.org/3/library/pathlib.html advises using `Path.stat`
to distinguish these cases.
@cjwatson cjwatson mentioned this pull request Jan 9, 2026
Fix test_dir_without_exec_permission on Python 3.14 Backblaze/b2-sdk-python#560
Merged
olzhasar-reef pushed a commit to cjwatson/b2-sdk-python that referenced this pull request Feb 18, 2026
@cjwatson @olzhasar-reef
Use stat.S_ISDIR for local folder scanning instead of Path.is_dir
7d44ef0
`Path.is_dir` on Python 3.14 now returns False for folders without search permissions:
python/cpython#118243
Therefore, the logic in the catch block will be skipped.
@larsevj larsevj mentioned this pull request Mar 6, 2026
Add support for python 3.14 equinor/ert#12450
Merged
4 of 9 tasks
@dmamelin dmamelin mentioned this pull request May 1, 2026
HA 2026.5 support custom-components/pyscript#828
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jun 2, 2026
feat(slice-3): PR-S3-6 — operator CLI surface + CommsAdapterMCP Protocol stub (closes #134) alfred-os/AlfredOS#149
Merged
6 of 8 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Aug 3, 2026
fix(install): make provisioning total across filesystem faults bmad-code-org/bmad-loop#448
Merged
This was referenced Aug 14, 2026
feat(runtime): distribute canonical resumable capability kernel GlacierEQ/apex-cli#3
Merged
Item 52: distinguish pyproj_deps.py's genuine errors from its exit-1 not-found case mixmansoundude/Python_vs_Windows#445
Merged
fix(local-fs): make incremental sync actually incremental pipeshub-ai/pipeshub-ai#3036
Closed
@amd-vivekag amd-vivekag mentioned this pull request Aug 25, 2026
Make ROCm discovery layout-agnostic (classic + TheRock wheel) and add the non-gating latest-ROCm canary ROCm/aorta#387
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Aug 26, 2026
feat(health): report build provenance and gate the release on it agentmiddleware/agent-middleware-api#374
Merged
@qodo-code-review qodo-code-review Bot mentioned this pull request Sep 9, 2026
Refuse an --output directory inside a version-control checkout ryanduguid/accounting-review-pipeline#134
Merged
@toxicoder toxicoder mentioned this pull request Oct 27, 2026
feat: add //scripts:update-stack and organize the env templates toxicoder/ez-comfy-stack#333
Merged
@socketpair socketpair mentioned this pull request Oct 30, 2026
Pathlib is_* functions do not fail in case of errno=EIO #160959
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zooba zooba zooba approved these changes
+1 more reviewer
@nineteendo nineteendo nineteendo left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

performance Performance or resource usage topic-pathlib

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@barneygale @nineteendo @zooba
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.