[issue28193] Consider using lru_cache for the re.py caches

Serhiy Storchaka report at bugs.python.org
Sun Sep 18 17:27:24 EDT 2016


Serhiy Storchaka added the comment:

Yes, raising an exception with a result as a payload is one option. Other option is to check a result. Something like:

def _compile_is_valid(value):
    p, loc = value
    return loc is None or loc == _locale.setlocale(_locale.LC_CTYPE)

def _compile_cache_if(value):
    p, loc = value
    return loc is not False

@lru_cache(_MAXCACHE, is_valid=_compile_is_valid, cache_if=_compile_cache_if)
def _compile1(pattern, flags):
    # internal: compile pattern
    if isinstance(pattern, _pattern_type):
        if flags:
            raise ValueError(
                "cannot process flags argument with a compiled pattern")
        return pattern, False
    if not sre_compile.isstring(pattern):
        raise TypeError("first argument must be string or compiled pattern")
    p = sre_compile.compile(pattern, flags)
    if flags & DEBUG:
        return p, False
    if not (p.flags & LOCALE):
        return p, None
    if not _locale:
        return p, False
    return p, _locale.setlocale(_locale.LC_CTYPE)

def _compile(pattern, flags):
    p, loc = _compile1(pattern, flags)
    return p

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28193>
_______________________________________


More information about the Python-bugs-list mailing list