Skip to content

GH-119169: Implement pathlib.Path.walk() using os.walk() #119573

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

Merged
merged 2 commits into from
May 29, 2024

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented May 26, 2024

For silly reasons, pathlib's generic implementation of walk() currently resides in glob._Globber. This commit moves it into pathlib._abc.PathBase.walk() where it really belongs, and makes pathlib.Path.walk() call though to os.walk() rather than using the generic impl.

Symlink handling is a little different between the two walk() implementations when followlinks=False. In pathlib it means never following symlinks, not even for distinguishing between files and directories. In os it means never walking into symlinks, including any symlinks created by the user between iterations. We smooth over these differences with a private sentinel - os._walk_symlinks_as_files - that enables the pathlib behaviour.

No change of behaviour.

For silly reasons, pathlib's generic implementation of `walk()` currently
resides in `glob._Globber`. This commit moves it into
`pathlib._abc.PathBase.walk()` where it really belongs, and makes
`pathlib.Path.walk()` call though to `os.walk()`.

Symlink handling is a little different between the two `walk()`
implementations when `followlinks=False`. In `pathlib` it means never
following symlinks, not even for distinguishing between files and
directories. In `os` it means never *walking* into symlinks, including any
symlinks created by the user between iterations. We smooth over these
differences with a private sentinel - `os._walk_symlinks_as_files` - that
enables the pathlib behaviour.
@barneygale
Copy link
Contributor Author

barneygale commented May 29, 2024

I'm intensely embarrassed by my decision to add a _Globber.walk() method to glob.py despite it having little to do with globbing, and so I'm going to merge this PR before anyone else notices my poor life choices.

@barneygale barneygale added the needs backport to 3.13 bugs and security fixes label May 29, 2024
@barneygale barneygale enabled auto-merge (squash) May 29, 2024 20:37
@barneygale barneygale merged commit 7ff61f5 into python:main May 29, 2024
32 checks passed
@miss-islington-app
Copy link

Thanks @barneygale for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 29, 2024
…ythonGH-119573)

For silly reasons, pathlib's generic implementation of `walk()` currently
resides in `glob._Globber`. This commit moves it into
`pathlib._abc.PathBase.walk()` where it really belongs, and makes
`pathlib.Path.walk()` call `os.walk()`.
(cherry picked from commit 7ff61f5)

Co-authored-by: Barney Gale <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented May 29, 2024

GH-119750 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 29, 2024
barneygale added a commit that referenced this pull request May 29, 2024
…H-119573) (#119750)

GH-119169: Implement `pathlib.Path.walk()` using `os.walk()` (GH-119573)

For silly reasons, pathlib's generic implementation of `walk()` currently
resides in `glob._Globber`. This commit moves it into
`pathlib._abc.PathBase.walk()` where it really belongs, and makes
`pathlib.Path.walk()` call `os.walk()`.
(cherry picked from commit 7ff61f5)

Co-authored-by: Barney Gale <[email protected]>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
…ython#119573)

For silly reasons, pathlib's generic implementation of `walk()` currently
resides in `glob._Globber`. This commit moves it into
`pathlib._abc.PathBase.walk()` where it really belongs, and makes
`pathlib.Path.walk()` call `os.walk()`.
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
…ython#119573)

For silly reasons, pathlib's generic implementation of `walk()` currently
resides in `glob._Globber`. This commit moves it into
`pathlib._abc.PathBase.walk()` where it really belongs, and makes
`pathlib.Path.walk()` call `os.walk()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant