]> granicus.if.org Git - python/commitdiff
Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug output...
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 3 Feb 2014 19:59:59 +0000 (20:59 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 3 Feb 2014 19:59:59 +0000 (20:59 +0100)
Lib/re.py
Lib/test/test_re.py
Misc/NEWS

index fcea190d3377863844f1fc93fea1083a5a9373ca..a46ecc84f22fcac7de6f7c8f123cd7e443d8bca0 100644 (file)
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -267,10 +267,12 @@ _pattern_type = type(sre_compile.compile("", 0))
 _MAXCACHE = 512
 def _compile(pattern, flags):
     # internal: compile pattern
-    try:
-        return _cache[type(pattern), pattern, flags]
-    except KeyError:
-        pass
+    bypass_cache = flags & DEBUG
+    if not bypass_cache:
+        try:
+            return _cache[type(pattern), pattern, flags]
+        except KeyError:
+            pass
     if isinstance(pattern, _pattern_type):
         if flags:
             raise ValueError(
@@ -279,9 +281,10 @@ def _compile(pattern, flags):
     if not sre_compile.isstring(pattern):
         raise TypeError("first argument must be string or compiled pattern")
     p = sre_compile.compile(pattern, flags)
-    if len(_cache) >= _MAXCACHE:
-        _cache.clear()
-    _cache[type(pattern), pattern, flags] = p
+    if not bypass_cache:
+        if len(_cache) >= _MAXCACHE:
+            _cache.clear()
+        _cache[type(pattern), pattern, flags] = p
     return p
 
 def _compile_repl(repl, pattern):
index f093812442623d791741276af20a55ff548d35d3..1c6f45dfa652522ba8b33c4230320d58dbbe633b 100644 (file)
@@ -1,5 +1,5 @@
 from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
-        cpython_only
+        cpython_only, captured_stdout
 import io
 import re
 from re import Scanner
@@ -1064,6 +1064,19 @@ class ReTests(unittest.TestCase):
                 self.assertEqual(m.group(1), "")
                 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():
     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
     if verbose:
index 66d9db79524dbb0eb056b10f138cd024fb7404d1..3c078bde2a63a5030514777dc032bba0ffd20449 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -45,6 +45,9 @@ Core and Builtins
 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.
   Improved error handling in variables-related commands.