Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions pcre/pcre.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,16 +1729,40 @@ def _module_compile(pattern: Any, flags: FlagInput) -> Pattern:
return compile(pattern, flags=flags)


_ORIGINAL_MODULE_COMPILE = _module_compile


def _module_lookup(pattern: Any, string: Any, flags: FlagInput, method: int) -> Any:
compiled = _module_compile(pattern, flags)
state = _DEFAULT_COMPILE_LOCAL
hot = getattr(state, "module_hot", None)
fast_shape = False
if (
type(compiled) is Pattern
and type(compiled._pattern) is _C_PATTERN_TYPE
and type(pattern) in (str, bytes)
and type(flags) in (int, Flag)
_module_compile is _ORIGINAL_MODULE_COMPILE
and hot is not None
and pattern is hot[0]
and (type(flags) is int or type(flags) is Flag)
and flags == 0
and type(string) in (str, bytes)
and hot[3] == _MODULE_CACHE_EPOCH
and hot[1] is _DEFAULT_CONFIG
and hot[2] is get_thread_default
):
compiled = hot[4]
fast_shape = (
type(compiled) is Pattern
and type(compiled._pattern) is _C_PATTERN_TYPE
and type(string) in (str, bytes)
)
else:
compiled = _module_compile(pattern, flags)
fast_shape = (
type(compiled) is Pattern
and type(compiled._pattern) is _C_PATTERN_TYPE
and type(pattern) in (str, bytes)
and (type(flags) is int or type(flags) is Flag)
and flags == 0
and type(string) in (str, bytes)
)
if fast_shape:
raw_pattern = compiled._pattern
if method == _MODULE_FINDALL:
literal_capture = compiled._literal_findall
Expand Down
2 changes: 2 additions & 0 deletions tests/test_python_coverage_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ def subn(self, repl: Any, value: Any, *, count: int) -> tuple[Any, int]:
return (f"sub:{repl}:{value}", count)

legacy = LegacyPattern()
pcre.clear_cache()
assert pcre.match("x", "a") is None
monkeypatch.setattr(pcre_mod, "_module_compile", lambda *args: legacy)
assert pcre_mod.match("x", "a") == "match:a"
assert pcre_mod.search("x", "a") == "search:a"
Expand Down
Loading