◐ Shell
clean mode source ↗

GH-72904: Add `glob.translate()` function by barneygale · Pull Request #106703 · python/cpython

If a sequence of path separators is given to the new argument,
`translate()` produces a pattern that matches similarly to
`pathlib.Path.glob()`. Specifically:

- A `*` pattern segment matches precisely one path segment.
- A `**` pattern segment matches any number of path segments
- If `**` appears in any other position within the pattern, `ValueError` is
  raised.
- `*` and `?` wildcards in other positions don't match path separators.

This change allows us to factor out a lot of complex code in pathlib.

@barneygale

@barneygale barneygale changed the title GH-72904: Add optional *seps* argument to fnmatch.translate() GH-72904: Add optional *sep* argument to fnmatch.translate()

Jul 26, 2023

jaraco

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>

jaraco

@barneygale barneygale changed the title GH-72904: Add optional *sep* argument to fnmatch.translate() GH-72904: Add glob.translate() function

Aug 12, 2023

AA-Turner

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>

@barneygale

aisk pushed a commit to aisk/cpython that referenced this pull request

Feb 11, 2024
Add `glob.translate()` function that converts a pathname with shell wildcards to a regular expression. The regular expression is used by pathlib to implement `match()` and `glob()`.

This function differs from `fnmatch.translate()` in that wildcards do not match path separators by default, and that a `*` pattern segment matches precisely one path segment. When *recursive* is set to true, `**` pattern segments match any number of path segments, and `**` cannot appear outside its own segment.

In pathlib, this change speeds up directory walking (because `_make_child_relpath()` does less work), makes path objects smaller (they don't need a `_lines` slot), and removes the need for some gnarly code.

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>

Glyphack pushed a commit to Glyphack/cpython that referenced this pull request

Sep 2, 2024
Add `glob.translate()` function that converts a pathname with shell wildcards to a regular expression. The regular expression is used by pathlib to implement `match()` and `glob()`.

This function differs from `fnmatch.translate()` in that wildcards do not match path separators by default, and that a `*` pattern segment matches precisely one path segment. When *recursive* is set to true, `**` pattern segments match any number of path segments, and `**` cannot appear outside its own segment.

In pathlib, this change speeds up directory walking (because `_make_child_relpath()` does less work), makes path objects smaller (they don't need a `_lines` slot), and removes the need for some gnarly code.

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>