MCPcopy Index your code
hub / github.com/python/cpython / _compile

Function _compile

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

Source from the content-addressed store, hash-verified

330assert _MAXCACHE2 < _MAXCACHE
331
332def _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)
377def _compile_template(pattern, repl):

Callers 10

prefixmatchFunction · 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
test_Py_CompileStringMethod · 0.50

Calls 2

popMethod · 0.45
compileMethod · 0.45

Tested by 1

test_Py_CompileStringMethod · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…