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