bpo-31671: re: Convert RegexFlag to int before compile (GH-3862)
sre_compile does bit test (e.g. `flags & SRE_FLAG_IGNORECASE`) in loop.
`IntFlag.__and__` and `IntFlag.__new__` made it slower.
So this commit convert it to normal int before passing flags to `sre_compile()`.
diff --git a/Lib/re.py b/Lib/re.py
index d772979..abbf8d6 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -275,6 +275,8 @@
_MAXCACHE = 512
def _compile(pattern, flags):
# internal: compile pattern
+ if isinstance(flags, RegexFlag):
+ flags = flags.value
try:
return _cache[type(pattern), pattern, flags]
except KeyError:
@@ -331,6 +333,8 @@
class Scanner:
def __init__(self, lexicon, flags=0):
from sre_constants import BRANCH, SUBPATTERN
+ if isinstance(flags, RegexFlag):
+ flags = flags.value
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []