Commit f5814112 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 942aaac7
...@@ -225,11 +225,13 @@ _MAXCACHE = 100 ...@@ -225,11 +225,13 @@ _MAXCACHE = 100
def _compile(*key): def _compile(*key):
# internal: compile pattern # internal: compile pattern
pattern, flags = key
bypass_cache = flags & DEBUG
if not bypass_cache:
cachekey = (type(key[0]),) + key cachekey = (type(key[0]),) + key
p = _cache.get(cachekey) p = _cache.get(cachekey)
if p is not None: if p is not None:
return p return p
pattern, flags = key
if isinstance(pattern, _pattern_type): if isinstance(pattern, _pattern_type):
if flags: if flags:
raise ValueError('Cannot process flags argument with a compiled pattern') raise ValueError('Cannot process flags argument with a compiled pattern')
...@@ -240,6 +242,7 @@ def _compile(*key): ...@@ -240,6 +242,7 @@ def _compile(*key):
p = sre_compile.compile(pattern, flags) p = sre_compile.compile(pattern, flags)
except error, v: except error, v:
raise error, v # invalid expression raise error, v # invalid expression
if not bypass_cache:
if len(_cache) >= _MAXCACHE: if len(_cache) >= _MAXCACHE:
_cache.clear() _cache.clear()
_cache[cachekey] = p _cache[cachekey] = p
......
from test.test_support import verbose, run_unittest, import_module from test.test_support import verbose, run_unittest, import_module
from test.test_support import precisionbigmemtest, _2G, cpython_only from test.test_support import precisionbigmemtest, _2G, cpython_only
from test.test_support import captured_stdout
import re import re
from re import Scanner from re import Scanner
import sre_constants import sre_constants
...@@ -920,6 +921,19 @@ class ReTests(unittest.TestCase): ...@@ -920,6 +921,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:
......
...@@ -38,6 +38,9 @@ Core and Builtins ...@@ -38,6 +38,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 (in - Issue #20368: The null character now correctly passed from Tcl to Python (in
unicode strings only). Improved error handling in variables-related commands. unicode strings only). 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