| 1 |
""" |
| 2 |
This module defines the hyperscan backends for `.PathSpec`, revision 1, used in |
| 3 |
benchmarking, but not included in the released library. |
| 4 |
""" |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from collections.abc import ( |
| 8 |
Sequence) |
| 9 |
from typing import ( |
| 10 |
Any, |
| 11 |
Callable, # Replaced by `collections.abc.Callable` in 3.9.2. |
| 12 |
Optional) # Replaced by `X | None` in 3.10. |
| 13 |
from typing_extensions import ( |
| 14 |
override) # Added in 3.12. |
| 15 |
|
| 16 |
try: |
| 17 |
import hyperscan |
| 18 |
except ModuleNotFoundError: |
| 19 |
hyperscan = None |
| 20 |
|
| 21 |
from pathspec._backends.hyperscan._base import ( |
| 22 |
HS_FLAGS, |
| 23 |
HyperscanExprDat) |
| 24 |
from pathspec._backends.hyperscan.pathspec import ( |
| 25 |
HyperscanPsBackend) |
| 26 |
from pathspec.pattern import ( |
| 27 |
RegexPattern) |
| 28 |
|
| 29 |
|
| 30 |
class HyperscanPsR1BaseBackend(HyperscanPsBackend): |
| 31 |
""" |
| 32 |
The :class:`HyperscanPsR1BaseBackend` base class uses a hyperscan database in |
| 33 |
block mode for matching files. |
| 34 |
""" |
| 35 |
|
| 36 |
@override |
| 37 |
@staticmethod |
| 38 |
def _init_db( |
| 39 |
db: hyperscan.Database, |
| 40 |
debug: bool, |
| 41 |
patterns: list[tuple[int, RegexPattern]], |
| 42 |
sort_ids: Optional[Callable[[list[int]], None]], |
| 43 |
) -> list[HyperscanExprDat]: |
| 44 |
# NOTICE: This is the current implementation. |
| 45 |
|
| 46 |
# Prepare patterns. |
| 47 |
expr_data: list[HyperscanExprDat] = [] |
| 48 |
exprs: list[bytes] = [] |
| 49 |
for pattern_index, pattern in patterns: |
| 50 |
if pattern.include is None: |
| 51 |
continue |
| 52 |
|
| 53 |
# Encode regex. |
| 54 |
assert isinstance(pattern, RegexPattern), pattern |
| 55 |
regex = pattern.regex.pattern |
| 56 |
|
| 57 |
if isinstance(regex, bytes): |
| 58 |
regex_bytes = regex |
| 59 |
else: |
| 60 |
assert isinstance(regex, str), regex |
| 61 |
regex_bytes = regex.encode('utf8') |
| 62 |
|
| 63 |
expr_data.append(HyperscanExprDat( |
| 64 |
include=pattern.include, |
| 65 |
index=pattern_index, |
| 66 |
is_dir_pattern=False, |
| 67 |
)) |
| 68 |
exprs.append(regex_bytes) |
| 69 |
|
| 70 |
# Compile patterns. |
| 71 |
db.compile( |
| 72 |
expressions=exprs, |
| 73 |
ids=list(range(len(exprs))), |
| 74 |
elements=len(exprs), |
| 75 |
flags=HS_FLAGS, |
| 76 |
) |
| 77 |
return expr_data |
| 78 |
|
| 79 |
@override |
| 80 |
@staticmethod |
| 81 |
def _make_db() -> hyperscan.Database: |
| 82 |
raise NotImplementedError() |
| 83 |
|
| 84 |
|
| 85 |
class _HyperscanPsR1BlockBaseBackend(HyperscanPsR1BaseBackend): |
| 86 |
""" |
| 87 |
The :class:`_HyperscanPsR1BlockBaseBackend` base class uses a hyperscan |
| 88 |
database in block mode for matching files. |
| 89 |
""" |
| 90 |
|
| 91 |
@override |
| 92 |
@staticmethod |
| 93 |
def _make_db() -> hyperscan.Database: |
| 94 |
return hyperscan.Database(mode=hyperscan.HS_MODE_BLOCK) |
| 95 |
|
| 96 |
|
| 97 |
class HyperscanPsR1BlockClosureBackend(_HyperscanPsR1BlockBaseBackend): |
| 98 |
""" |
| 99 |
The :class:`HyperscanPsR1BlockClosureBackend` class uses a hyperscan database |
| 100 |
in block mode for matching files, and uses a closure to capture state. |
| 101 |
""" |
| 102 |
|
| 103 |
# Prevent accidental usage. |
| 104 |
_out: tuple[()] |
| 105 |
|
| 106 |
@override |
| 107 |
def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]: |
| 108 |
out_include = False |
| 109 |
out_index: Optional[int] = -1 |
| 110 |
|
| 111 |
def on_match( |
| 112 |
expr_id: int, _from: int, _to: int, _flags: int, _context: Any, |
| 113 |
) -> Optional[bool]: |
| 114 |
# WARNING: Hyperscan does not guarantee matches will be produced in order! |
| 115 |
nonlocal out_include, out_index |
| 116 |
expr_dat = self._expr_data[expr_id] |
| 117 |
index = expr_dat.index |
| 118 |
if index > out_index: |
| 119 |
out_include = expr_dat.include |
| 120 |
out_index = index |
| 121 |
|
| 122 |
self._db.scan(file.encode('utf8'), match_event_handler=on_match) |
| 123 |
|
| 124 |
if out_index == -1: |
| 125 |
out_index = None |
| 126 |
|
| 127 |
return (out_include, out_index) |
| 128 |
|
| 129 |
|
| 130 |
class HyperscanPsR1BlockStateBackend(_HyperscanPsR1BlockBaseBackend): |
| 131 |
""" |
| 132 |
The :class:`HyperscanPsR1BlockStateBackend` class uses a hyperscan database in |
| 133 |
block mode for matching files, and stores state in variables. |
| 134 |
""" |
| 135 |
|
| 136 |
def __init__(self, patterns: Sequence[RegexPattern]) -> None: |
| 137 |
super().__init__(patterns) |
| 138 |
self._out = (None, -1) |
| 139 |
|
| 140 |
@override |
| 141 |
def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]: |
| 142 |
self._out = (None, -1) |
| 143 |
self._db.scan(file.encode('utf8'), match_event_handler=self.__on_match) |
| 144 |
|
| 145 |
out_include, out_index = self._out |
| 146 |
if out_index == -1: |
| 147 |
out_index = None |
| 148 |
|
| 149 |
return (out_include, out_index) |
| 150 |
|
| 151 |
@override |
| 152 |
def __on_match( |
| 153 |
self, |
| 154 |
expr_id: int, |
| 155 |
_from: int, |
| 156 |
_to: int, |
| 157 |
_flags: int, |
| 158 |
_context: Any, |
| 159 |
) -> Optional[bool]: |
| 160 |
# WARNING: Hyperscan does not guarantee matches will be produced in order! |
| 161 |
expr_dat = self._expr_data[expr_id] |
| 162 |
index = expr_dat.index |
| 163 |
prev_index = self._out[1] |
| 164 |
if index > prev_index: |
| 165 |
self._out = (expr_dat.include, index) |
| 166 |
|
| 167 |
|
| 168 |
class _HyperscanPsR1StreamBaseBackend(HyperscanPsR1BaseBackend): |
| 169 |
""" |
| 170 |
The :class:`_HyperscanPsR1StreamBaseBackend` base class uses a hyperscan |
| 171 |
database in streaming mode for matching files. |
| 172 |
""" |
| 173 |
|
| 174 |
@override |
| 175 |
@staticmethod |
| 176 |
def _make_db() -> hyperscan.Database: |
| 177 |
return hyperscan.Database(mode=hyperscan.HS_MODE_STREAM) |
| 178 |
|
| 179 |
|
| 180 |
class HyperscanPsR1StreamClosureBackend(_HyperscanPsR1StreamBaseBackend): |
| 181 |
""" |
| 182 |
The :class:`HyperscanPsR1StreamClosureBackend` class uses a hyperscan database |
| 183 |
in streaming mode for matching files, and uses a closure to capture state. |
| 184 |
""" |
| 185 |
|
| 186 |
@override |
| 187 |
def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]: |
| 188 |
out_include = False |
| 189 |
out_index: Optional[int] = -1 |
| 190 |
|
| 191 |
def on_match( |
| 192 |
expr_id: int, _from: int, _to: int, _flags: int, _context: Any, |
| 193 |
) -> Optional[bool]: |
| 194 |
# WARNING: Hyperscan does not guarantee matches will be produced in order! |
| 195 |
nonlocal out_include, out_index |
| 196 |
expr_dat = self._expr_data[expr_id] |
| 197 |
index = expr_dat.index |
| 198 |
if index > out_index: |
| 199 |
out_include = expr_dat.include |
| 200 |
out_index = index |
| 201 |
|
| 202 |
with self._db.stream(match_event_handler=on_match) as stream: |
| 203 |
stream.scan(file.encode('utf8')) |
| 204 |
|
| 205 |
if out_index == -1: |
| 206 |
out_index = None |
| 207 |
|
| 208 |
return (out_include, out_index) |
| 209 |
|
| 210 |
|
| 211 |
# WARNING: This segfaults. |
| 212 |
class HyperscanPsR1StreamStateBackend(_HyperscanPsR1StreamBaseBackend): |
| 213 |
""" |
| 214 |
The :class:`HyperscanPsR1StreamStateBackend` class uses a hyperscan database |
| 215 |
in streaming mode for matching files, and stores state in variables. |
| 216 |
""" |
| 217 |
|
| 218 |
def __init__(self, patterns: Sequence[RegexPattern]) -> None: |
| 219 |
super().__init__(patterns) |
| 220 |
self._out = (None, -1) |
| 221 |
|
| 222 |
@override |
| 223 |
def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]: |
| 224 |
self._out = (None, -1) |
| 225 |
|
| 226 |
with self._db.stream(match_event_handler=self.__on_match) as stream: |
| 227 |
stream.scan(file.encode('utf8')) |
| 228 |
|
| 229 |
out_include, out_index = self._out |
| 230 |
if out_index == -1: |
| 231 |
out_index = None |
| 232 |
|
| 233 |
return (out_include, out_index) |
| 234 |
|
| 235 |
@override |
| 236 |
def __on_match( |
| 237 |
self, |
| 238 |
expr_id: int, |
| 239 |
_from: int, |
| 240 |
_to: int, |
| 241 |
_flags: int, |
| 242 |
_context: Any, |
| 243 |
) -> Optional[bool]: |
| 244 |
# WARNING: Hyperscan does not guarantee matches will be produced in order! |
| 245 |
expr_dat = self._expr_data[expr_id] |
| 246 |
index = expr_dat.index |
| 247 |
prev_index = self._out[1] |
| 248 |
if index > prev_index: |
| 249 |
self._out = (expr_dat.include, index) |