summaryrefslogtreecommitdiff
path: root/Lib/re.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/re.py')
-rw-r--r--Lib/re.py51
1 files changed, 26 insertions, 25 deletions
diff --git a/Lib/re.py b/Lib/re.py
index dde8901c62..d321cff92c 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -119,9 +119,10 @@ This module also defines an exception 'error'.
"""
-import sys
+import enum
import sre_compile
import sre_parse
+import functools
try:
import _locale
except ImportError:
@@ -138,18 +139,26 @@ __all__ = [
__version__ = "2.2.1"
-# flags
-A = ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
-I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
-L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
-U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
-M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
-S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
-X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
-
-# sre extensions (experimental, don't rely on these)
-T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
-DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
+class RegexFlag(enum.IntFlag):
+ ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
+ IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
+ LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
+ UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
+ MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
+ DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
+ VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
+ A = ASCII
+ I = IGNORECASE
+ L = LOCALE
+ U = UNICODE
+ M = MULTILINE
+ S = DOTALL
+ X = VERBOSE
+ # sre extensions (experimental, don't rely on these)
+ TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
+ T = TEMPLATE
+ DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
+globals().update(RegexFlag.__members__)
# sre exception
error = sre_compile.error
@@ -226,7 +235,7 @@ def compile(pattern, flags=0):
def purge():
"Clear the regular expression caches"
_cache.clear()
- _cache_repl.clear()
+ _compile_repl.cache_clear()
def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object"
@@ -270,7 +279,6 @@ def escape(pattern):
# internals
_cache = {}
-_cache_repl = {}
_pattern_type = type(sre_compile.compile("", 0))
@@ -303,17 +311,10 @@ def _compile(pattern, flags):
_cache[type(pattern), pattern, flags] = p, loc
return p
+@functools.lru_cache(_MAXCACHE)
def _compile_repl(repl, pattern):
# internal: compile replacement pattern
- try:
- return _cache_repl[repl, pattern]
- except KeyError:
- pass
- p = sre_parse.parse_template(repl, pattern)
- if len(_cache_repl) >= _MAXCACHE:
- _cache_repl.clear()
- _cache_repl[repl, pattern] = p
- return p
+ return sre_parse.parse_template(repl, pattern)
def _expand(pattern, match, template):
# internal: match.expand implementation hook
@@ -353,7 +354,7 @@ class Scanner:
for phrase, action in lexicon:
gid = s.opengroup()
p.append(sre_parse.SubPattern(s, [
- (SUBPATTERN, (gid, sre_parse.parse(phrase, flags))),
+ (SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))),
]))
s.closegroup(gid, p[-1])
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])