[Python-checkins] cpython (merge 3.3 -> default): Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug
antoine.pitrou
python-checkins at python.org
Mon Feb 3 21:01:54 CET 2014
http://hg.python.org/cpython/rev/a8bcfa290e68
changeset: 88941:a8bcfa290e68
parent: 88939:6007e81f5867
parent: 88940:a7b180d5df5f
user: Antoine Pitrou <solipsis at pitrou.net>
date: Mon Feb 03 21:01:35 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, 26 insertions(+), 8 deletions(-)
diff --git a/Lib/re.py b/Lib/re.py
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -272,10 +272,12 @@
_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(
@@ -284,9 +286,10 @@
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):
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,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
@@ -1193,6 +1193,18 @@
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 '])
+
class PatternReprTests(unittest.TestCase):
def check(self, pattern, expected):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,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.
Improved error handling in variables-related commands.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list