cpython: e47f6883dedf (original) (raw)
Mercurial > cpython
changeset 88942:e47f6883dedf 2.7
Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug output every time it is called, regardless of the compilation cache. [#20426]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Mon, 03 Feb 2014 20:59:59 +0100 |
parents | 129eb818d9b2 |
children | aed29f86bfdc |
files | Lib/re.py Lib/test/test_re.py Misc/NEWS |
diffstat | 3 files changed, 27 insertions(+), 7 deletions(-)[+] [-] Lib/re.py 17 Lib/test/test_re.py 14 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/re.py +++ b/Lib/re.py @@ -225,11 +225,13 @@ def escape(pattern): def _compile(*key): # internal: compile pattern
- cachekey = (type(key[0]),) + key
- p = _cache.get(cachekey)
- if p is not None:
pattern, flags = keyreturn p[](#l1.10)
- bypass_cache = flags & DEBUG
- if not bypass_cache:
cachekey = (type(key[0]),) + key[](#l1.14)
p = _cache.get(cachekey)[](#l1.15)
if p is not None:[](#l1.16)
if isinstance(pattern, _pattern_type): if flags: raise ValueError('Cannot process flags argument with a compiled pattern')return p[](#l1.17)
@@ -240,9 +242,10 @@ def _compile(*key): p = sre_compile.compile(pattern, flags) except error, v: raise error, v # invalid expression
- if not bypass_cache:
if len(_cache) >= _MAXCACHE:[](#l1.29)
_cache.clear()[](#l1.30)
return p def _compile_repl(*key):_cache[cachekey] = p[](#l1.31)
--- 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 @@ 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:[](#l2.15)
re.compile('foo', re.DEBUG)[](#l2.16)
self.assertEqual(out.getvalue().splitlines(),[](#l2.17)
['literal 102', 'literal 111', 'literal 111'])[](#l2.18)
# Debug output is output again even a second time (bypassing[](#l2.19)
# the cache -- issue #20426).[](#l2.20)
with captured_stdout() as out:[](#l2.21)
re.compile('foo', re.DEBUG)[](#l2.22)
self.assertEqual(out.getvalue().splitlines(),[](#l2.23)
['literal 102', 'literal 111', 'literal 111'])[](#l2.24)
+ + def run_re_tests(): from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR if verbose:
--- 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