Lib/fnmatch.py
cpython 3.14 @ ab2d84fe1023/Lib/fnmatch.py
fnmatch is a small pure-Python module. Its four public functions cover
shell-style pattern matching against filenames. The core primitive is
translate(pat), which converts a glob pattern into a Python regex
string; the matching functions compile that regex through re with an
lru_cache wrapper (_compile_pattern) to avoid recompiling the same
pattern on every call.
The metacharacter set is intentionally simpler than glob: * matches
any run of characters (including the empty string, and including /),
? matches exactly one character, and [seq]/[!seq] work like
POSIX bracket expressions. Unlike shell glob, there is no special
treatment of leading dots or path separators.
In CPython 3.14 the internal helpers were reorganized into _translate
(the per-character scanner) and _join_translated_parts (the
star-position folding step that inserts atomic groups). The public API
did not change.
Map
| Lines | Symbol | Role | gopy |
|---|---|---|---|
| 1-21 | module docstring, imports | Sets up os, posixpath, re, functools. | module/fnmatch/module.go |
| 22-52 | fnmatch | Normalizes name and pattern via os.path.normcase, then delegates to fnmatchcase. | module/fnmatch/module.go |
| 53-69 | filter | Returns the subset of names that match pat; shares the compiled pattern across all names. | module/fnmatch/module.go |
| 70-84 | filterfalse | Returns the subset of names that do not match pat. | module/fnmatch/module.go |
| 85-94 | fnmatchcase | Case-sensitive match; compiles pat through _compile_pattern and calls .match(name). | module/fnmatch/module.go |
| 95-108 | _compile_pattern, _re_setops_sub, _re_escape | LRU-cached compiler; _re_escape is lru_cache(re.escape), _re_setops_sub escapes `&~ | ` inside bracket expressions. |
| 109-185 | _translate | Per-character scanner that converts *, ?, [...] into regex parts and records star positions. | module/fnmatch/module.go |
| 186-220 | _join_translated_parts, translate | Folds star positions into atomic groups (?>.*?) and wraps the whole pattern in (?s:...)\z. | module/fnmatch/module.go |