[Python-checkins] cpython (2.7): Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug

antoine.pitrou python-checkins at python.org
Mon Feb 3 21:12:54 CET 2014


http://hg.python.org/cpython/rev/e47f6883dedf
changeset: 88942:e47f6883dedf
branch: 2.7
parent: 88936:129eb818d9b2
user: Antoine Pitrou <solipsis at pitrou.net>
date: Mon Feb 03 20:59:59 2014 +0100
summary:
 Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug output every time it is called, regardless of the compilation cache.
files:
 Lib/re.py | 17 ++++++++++-------
 Lib/test/test_re.py | 14 ++++++++++++++
 Misc/NEWS | 3 +++
 3 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/Lib/re.py b/Lib/re.py
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -225,11 +225,13 @@
 
 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 @@
 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):
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -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 @@
 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:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,9 @@
 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.
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /