Skip to content

GH-106747: Prepare pathlib globbing for dir_fd support. #116319

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

Closed
wants to merge 1 commit into from

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Mar 4, 2024

The present implementation of pathlib.Path.glob() creates a series of 'selectors' that each handle a part of the pattern. The selectors are connected together in glob(), without the use of recursion. One very subtle property of this scheme is that each selector is exhausted before its successor selector - for example when globbing */*.py, the selector for * is exhausted prior to the selector for *.py. This doesn't make any difference when globbing strings, but it does prevent us from adding dir_fd support, because there's no good moment to call os.close(fd) after opening a directory for scanning.

This patch refactors globbing to work much as it did in 3.12, where each selector is responsible for creating and feeding its own successor. This inverts the order of selector exhaustion, and so will make it much easier to add dir_fd support.

There's one behaviour change here: I've removed deduplication of results, and so in some quite specific circumstances (multiple non-consecutive ** segments in pattern, and either follow_symlinks=None or .. segments separating them), glob() can yield the same path more than once. Note that glob.glob() can also yield duplicate results - see GH-104269.

The present implementation of `pathlib.Path.glob()` creates a series of
'selectors' that each handle a part of the pattern. The selectors are
connected together in `glob()`, without the use of recursion. One very
subtle property of this scheme is that each selector is exhaused *before*
its successor selector - for example when globbing `*/*.py`, the selector
for `*` is exhausted prior to the selector for `*.py`. This doesn't make
any difference when globbing strings, but it does prevent us from adding
`dir_fd` support, because there's no good moment to call `os.close(fd)`
after opening a directory for scanning.

This patch refactors globbing to work much as it did in 3.12, where each
selector is responsible for creating and feeding its own successor. This
inverts the order of selector exhaustion, and so will make it much easier
to add `dir_fd` support.

There's one behaviour change here: I've removes deduplication of results,
and so in some very specific circumstances (multiple non-consecutive `**`
segments in pattern, and either `follow_symlinks=None` or `..` segments
separating them), `glob()` can yield the same path more than once. Note
that `glob.glob()` can also yield duplicate results - see pythonGH-104269.
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