]> 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 aab5d37e589252dfe9b06f0ed3e8aa13def49eed..9b01f3e2281ab160a409aee85c9422f1de5c9bad 100644 (file)
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -225,11 +225,13 @@ _MAXCACHE = 100
 
 def _compile(*key):
     # internal: compile pattern
-    cachekey = (type(key[0]),) + key
-    p = _cache.get(cachekey)
-    if p is not None:
-        return p
     pattern, flags = key
+    bypass_cache = flags & DEBUG
+    if not bypass_cache:
+        cachekey = (type(key[0]),) + key
+        p = _cache.get(cachekey)
+        if p is not None:
+            return p
     if isinstance(pattern, _pattern_type):
         if flags:
             raise ValueError('Cannot process flags argument with a compiled pattern')
@@ -240,9 +242,10 @@ def _compile(*key):
         p = sre_compile.compile(pattern, flags)
     except error, v:
         raise error, v # invalid expression
-    if len(_cache) >= _MAXCACHE:
-        _cache.clear()
-    _cache[cachekey] = p
+    if not bypass_cache:
+        if len(_cache) >= _MAXCACHE:
+            _cache.clear()
+        _cache[cachekey] = p
     return p
 
 def _compile_repl(*key):
index 7ebbf05652fa117a1bb24988de18bb546a57f059..fe71c8477e2b22d4aab0cac2d30616035eb877cc 100644 (file)
@@ -1,5 +1,6 @@
 from test.test_support import verbose, run_unittest, import_module
 from test.test_support import precisionbigmemtest, _2G, cpython_only
+from test.test_support import captured_stdout
 import re
 from re import Scanner
 import sre_constants
@@ -920,6 +921,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 2b1dd0755bb37be09c7c1542d03fd5e6a85c9a85..ed01564d30c9ecca02a9d561b71c13cc562e96da 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,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 (in
   unicode strings only).  Improved error handling in variables-related commands.