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-159717: Use atomic groups for stars in glob.translate by digit50 · Pull Request #160580 · python/cpython

Conversation

@digit50

@digit50 digit50 commented Oct 24, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

closes #159717

glob.translate() kept only the parts list from fnmatch._translate() and dropped the star indices, so every * in a segment became a plain backtracking [^/]*. A failing match then took exponential time, which made PurePath.match, PurePath.full_match and Path.glob hang on short patterns such as '*a' * 8 + '*b'.

This change builds each segment from both the parts and the star indices and wraps every interior STAR fixed pair in an atomic group (?>[^/]*?fixed), the same rewrite fnmatch._join_translated_parts() uses. A pair is left as it was when its fixed part can match a separator. That happens only with a character class such as [!x] or [+-0], and there the atomic group could change the result: today *[!x]*x matches a/x because the class matches the /, and this change keeps that.

Tests:

  • test_glob.test_translate_slow and the pathlib test_full_match_slow and test_glob_slow are counterparts of test_slow_fnmatch that use the pattern from the issue against a 100 character name. Before this change none of them finished within a 30 second timeout; with it they take a few milliseconds.
  • test_translate_negated_class_matches_sep pins the current result for *[!x]*x.
  • The expected strings in the existing test_translate* tests are updated for the *.* segment, which now translates to (?>[^/]*?\.)[^/]*.

I also compared the old and new translation on 20,000 random patterns built from stars, literals, separators and character classes, with every combination of recursive, include_hidden and seps, against 30 random names each, and every result was the same.

  • Issue: pathlib match, full_match and glob take exponential time on short patterns because glob.translate skips fnmatch's atomic-group rewrite #159717

Sorry, something went wrong.

Uh oh!

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

All reactions
@digit50
pythongh-159717: Use atomic groups for stars in glob.translate
17c8efd
glob.translate() dropped the star indices from fnmatch._translate(), so every star in a segment became a plain backtracking group and matching took exponential time in pathlib's match, full_match and glob. Each interior star and the fixed part after it are now wrapped in an atomic group, as fnmatch already does. Pairs whose fixed part can match a separator are left unchanged, so the set of matching names stays the same.
@digit50
digit50 requested a review from barneygale as a code owner October 24, 2026 07:25
@bedevere-app bedevere-app Bot added the awaiting review label Oct 24, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Oct 24, 2026
pathlib match, full_match and glob take exponential time on short patterns because glob.translate skips fnmatch's atomic-group rewrite #159717
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@barneygale barneygale Awaiting requested review from barneygale barneygale is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

pathlib match, full_match and glob take exponential time on short patterns because glob.translate skips fnmatch's atomic-group rewrite

Uh oh!

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

1 participant

@digit50
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 01402520 — 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.