| 270 | |
| 271 | _MAXCACHE = 512 |
| 272 | def _compile(pattern, flags): |
| 273 | # internal: compile pattern |
| 274 | if isinstance(flags, RegexFlag): |
| 275 | flags = flags.value |
| 276 | try: |
| 277 | return _cache[type(pattern), pattern, flags] |
| 278 | except KeyError: |
| 279 | pass |
| 280 | if isinstance(pattern, Pattern): |
| 281 | if flags: |
| 282 | raise ValueError( |
| 283 | "cannot process flags argument with a compiled pattern") |
| 284 | return pattern |
| 285 | if not _compiler.isstring(pattern): |
| 286 | raise TypeError("first argument must be string or compiled pattern") |
| 287 | if flags & T: |
| 288 | import warnings |
| 289 | warnings.warn("The re.TEMPLATE/re.T flag is deprecated " |
| 290 | "as it is an undocumented flag " |
| 291 | "without an obvious purpose. " |
| 292 | "Don't use it.", |
| 293 | DeprecationWarning) |
| 294 | p = _compiler.compile(pattern, flags) |
| 295 | if not (flags & DEBUG): |
| 296 | if len(_cache) >= _MAXCACHE: |
| 297 | # Drop the oldest item |
| 298 | try: |
| 299 | del _cache[next(iter(_cache))] |
| 300 | except (StopIteration, RuntimeError, KeyError): |
| 301 | pass |
| 302 | _cache[type(pattern), pattern, flags] = p |
| 303 | return p |
| 304 | |
| 305 | @functools.lru_cache(_MAXCACHE) |
| 306 | def _compile_repl(repl, pattern): |