Whole file
vidartf/globmatch
The author described this change as “Fix matching dotfiles”. It counts as a record because the check below fails on the code as it stood at 61d2d26d7 and passes on 254852544, with nothing else changed between the two runs.
Projectvidartf/globmatch
Fix saved2021-01-06
Sharing licenceBSD-3-Clause · LICENSE.md
Change size+5 −4
What the code was meant to do, written into the code itself as a save note
Fix matching dotfiles
The change
| 20 | 20 | ||
| 21 | 21 | # TODO: In the future use f-strings for formatting | |
| 22 | 22 | ||
| 23 | + | double_start_re = r'.*((?<=(/))|(?<=(\A)))' | |
| 23 | 24 | ||
| 25 | + | ||
| 24 | 26 | @lru_cache(maxsize=256, typed=True) | |
| 25 | 27 | def compile_pattern(pat, subentries_match=None): | |
| 26 | 28 | """Translate and compile a glob pattern to a regular expression matcher. | |
| ⋯ | |||
| 40 | 42 | res = translate_glob(os.path.normcase(pat), subentries_match=subentries_match) | |
| 41 | 43 | return re.compile(res).match | |
| 42 | 44 | ||
| 43 | - | ||
| 44 | 45 | def translate_glob(pat, subentries_match=None): | |
| 45 | 46 | """Translate a glob PATTERN to a regular expression.""" | |
| 46 | 47 | translated_parts = [] | |
| ⋯ | |||
| 60 | 61 | """ | |
| 61 | 62 | res = '' | |
| 62 | 63 | for part in translated_parts[:-1]: | |
| 63 | - | if part == '.*': | |
| 64 | + | if part == double_start_re: | |
| 64 | 65 | # drop separator, since it is optional | |
| 65 | 66 | # (** matches ZERO or more dirs) | |
| 66 | 67 | res += part | |
| 67 | 68 | else: | |
| 68 | 69 | res += part + os_sep_class | |
| 69 | 70 | ||
| 70 | - | if translated_parts[-1] == '.*': | |
| 71 | + | if translated_parts[-1] == double_start_re: | |
| 71 | 72 | # Final part is ** | |
| 72 | 73 | # Should not match directory: | |
| 73 | 74 | res += '.+' | |
| ⋯ | |||
| 91 | 92 | """Translate a glob PATTERN PART to a regular expression.""" | |
| 92 | 93 | # Code modified from Python 3 standard lib fnmatch: | |
| 93 | 94 | if pat == '**': | |
| 94 | - | return '.*' | |
| 95 | + | return double_start_re | |
| 95 | 96 | i, n = 0, len(pat) | |
| 96 | 97 | res = [] | |
| 97 | 98 | while i < n: | |
The check that tells the two apart
fail→pass·tests/test_match.py::test_match_dot_files
Check file tests/test_match.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it61d2d26d7845127b06ec5857a32d82dfde545136
Broken version dated2021-01-06
Moduleglobmatch.translation
Units changedjoin_translated, translate_glob_part
Fingerprintdc81c00e5c1b8694
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.