MCPcopy Index your code
hub / github.com/RustPython/RustPython / _compile

Function _compile

Lib/re/__init__.py:330–372  ·  view source on GitHub ↗
(pattern, flags)

Source from the content-addressed store, hash-verified

328assert _MAXCACHE2 < _MAXCACHE
329
330def _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)
375def _compile_template(pattern, repl):

Callers 10

matchFunction · 0.70
fullmatchFunction · 0.70
searchFunction · 0.70
subFunction · 0.70
subnFunction · 0.70
splitFunction · 0.70
findallFunction · 0.70
finditerFunction · 0.70
compileFunction · 0.70
_get_codenameMethod · 0.50

Calls 6

isinstanceFunction · 0.85
lenFunction · 0.85
nextFunction · 0.85
iterFunction · 0.85
popMethod · 0.45
compileMethod · 0.45

Tested by

no test coverage detected