Commit d2cc743c authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug...

Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug output every time it is called, regardless of the compilation cache.
parent b3de1a72
...@@ -267,10 +267,12 @@ _pattern_type = type(sre_compile.compile("", 0)) ...@@ -267,10 +267,12 @@ _pattern_type = type(sre_compile.compile("", 0))
_MAXCACHE = 512 _MAXCACHE = 512
def _compile(pattern, flags): def _compile(pattern, flags):
# internal: compile pattern # internal: compile pattern
try: bypass_cache = flags & DEBUG
return _cache[type(pattern), pattern, flags] if not bypass_cache:
except KeyError: try:
pass return _cache[type(pattern), pattern, flags]
except KeyError:
pass
if isinstance(pattern, _pattern_type): if isinstance(pattern, _pattern_type):
if flags: if flags:
raise ValueError( raise ValueError(
...@@ -279,9 +281,10 @@ def _compile(pattern, flags): ...@@ -279,9 +281,10 @@ def _compile(pattern, flags):
if not sre_compile.isstring(pattern): if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern") raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags) p = sre_compile.compile(pattern, flags)
if len(_cache) >= _MAXCACHE: if not bypass_cache:
_cache.clear() if len(_cache) >= _MAXCACHE:
_cache[type(pattern), pattern, flags] = p _cache.clear()
_cache[type(pattern), pattern, flags] = p
return p return p
def _compile_repl(repl, pattern): def _compile_repl(repl, pattern):
......
from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \ from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
cpython_only cpython_only, captured_stdout
import io import io
import re import re
from re import Scanner from re import Scanner
...@@ -1064,6 +1064,19 @@ class ReTests(unittest.TestCase): ...@@ -1064,6 +1064,19 @@ class ReTests(unittest.TestCase):
self.assertEqual(m.group(1), "") self.assertEqual(m.group(1), "")
self.assertEqual(m.group(2), "y") self.assertEqual(m.group(2), "y")
def test_debug_flag(self):
with captured_stdout() as out:
re.compile('foo', re.DEBUG)
self.assertEqual(out.getvalue().splitlines(),
['literal 102 ', 'literal 111 ', 'literal 111 '])
# Debug output is output again even a second time (bypassing
# the cache -- issue #20426).
with captured_stdout() as out:
re.compile('foo', re.DEBUG)
self.assertEqual(out.getvalue().splitlines(),
['literal 102 ', 'literal 111 ', 'literal 111 '])
def run_re_tests(): def run_re_tests():
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
if verbose: if verbose:
......
...@@ -45,6 +45,9 @@ Core and Builtins ...@@ -45,6 +45,9 @@ Core and Builtins
Library Library
------- -------
- Issue #20426: When passing the re.DEBUG flag, re.compile() displays the
debug output every time it is called, regardless of the compilation cache.
- Issue #20368: The null character now correctly passed from Tcl to Python. - Issue #20368: The null character now correctly passed from Tcl to Python.
Improved error handling in variables-related commands. Improved error handling in variables-related commands.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment