(pattern, flags)
| 328 | assert _MAXCACHE2 < _MAXCACHE |
| 329 | |
| 330 | def _compile(pattern, flags): |
| 331 | # internal: compile pattern |
| 332 | if isinstance(flags, RegexFlag): |
| 333 | flags = flags.value |
| 334 | try: |
| 335 | return _cache2[type(pattern), pattern, flags] |
| 336 | except KeyError: |
| 337 | pass |
| 338 | |
| 339 | key = (type(pattern), pattern, flags) |
| 340 | # Item in _cache should be moved to the end if found. |
| 341 | p = _cache.pop(key, None) |
| 342 | if p is None: |
| 343 | if isinstance(pattern, Pattern): |
| 344 | if flags: |
| 345 | raise ValueError( |
| 346 | "cannot process flags argument with a compiled pattern") |
| 347 | return pattern |
| 348 | if not _compiler.isstring(pattern): |
| 349 | raise TypeError("first argument must be string or compiled pattern") |
| 350 | p = _compiler.compile(pattern, flags) |
| 351 | if flags & DEBUG: |
| 352 | return p |
| 353 | if len(_cache) >= _MAXCACHE: |
| 354 | # Drop the least recently used item. |
| 355 | # next(iter(_cache)) is known to have linear amortized time, |
| 356 | # but it is used here to avoid a dependency from using OrderedDict. |
| 357 | # For the small _MAXCACHE value it doesn't make much of a difference. |
| 358 | try: |
| 359 | del _cache[next(iter(_cache))] |
| 360 | except (StopIteration, RuntimeError, KeyError): |
| 361 | pass |
| 362 | # Append to the end. |
| 363 | _cache[key] = p |
| 364 | |
| 365 | if len(_cache2) >= _MAXCACHE2: |
| 366 | # Drop the oldest item. |
| 367 | try: |
| 368 | del _cache2[next(iter(_cache2))] |
| 369 | except (StopIteration, RuntimeError, KeyError): |
| 370 | pass |
| 371 | _cache2[key] = p |
| 372 | return p |
| 373 | |
| 374 | @functools.lru_cache(_MAXCACHE) |
| 375 | def _compile_template(pattern, repl): |
no test coverage detected