diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 845ed643f97b..19b766fd2ff2 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -326,6 +326,11 @@ Optimizations expressions `. Searching some patterns can now be up to 20 times faster. (Contributed by Serhiy Storchaka in :issue:`30285`.) +* :func:`re.compile` now converts ``flags`` parameter to int object if + it is ``RegexFlag``. It is now as fast as Python 3.5, and faster than + Python 3.6 about 10% depending on the pattern. + (Contributed by INADA Naoki in :issue:`31671`.) + * :meth:`selectors.EpollSelector.modify`, :meth:`selectors.PollSelector.modify` and :meth:`selectors.DevpollSelector.modify` may be around 10% faster under heavy loads. (Contributed by Giampaolo Rodola' in :issue:`30014`) diff --git a/Lib/re.py b/Lib/re.py index d772979f91e1..abbf8d6e290e 100644 --- a/Lib/re.py +++ b/Lib/re.py @@ -275,6 +275,8 @@ def escape(pattern): _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 @@ def _pickle(p): 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 = [] diff --git a/Misc/NEWS.d/next/Library/2017-10-04-21-28-44.bpo-31671.E-zfc9.rst b/Misc/NEWS.d/next/Library/2017-10-04-21-28-44.bpo-31671.E-zfc9.rst new file mode 100644 index 000000000000..b84dedd63edc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-10-04-21-28-44.bpo-31671.E-zfc9.rst @@ -0,0 +1,2 @@ +Now ``re.compile()`` converts passed RegexFlag to normal int object before +compiling. bm_regex_compile benchmark shows 14% performance improvements.