cpython: a8bcfa290e68 (original) (raw)
Mercurial > cpython
changeset 88941:a8bcfa290e68
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 21:01:35 +0100 |
parents | 6007e81f5867(current diff)a7b180d5df5f(diff) |
children | a5a622309a0b |
files | Lib/re.py Lib/test/test_re.py Misc/NEWS |
diffstat | 3 files changed, 26 insertions(+), 8 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 @@ -272,10 +272,12 @@ def escape(pattern): _MAXCACHE = 512 def _compile(pattern, flags): # internal: compile pattern
- bypass_cache = flags & DEBUG
- if not bypass_cache:
try:[](#l1.13)
return _cache[type(pattern), pattern, flags][](#l1.14)
except KeyError:[](#l1.15)
if isinstance(pattern, _pattern_type): if flags: raise ValueError(pass[](#l1.16)
@@ -284,9 +286,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 not bypass_cache:
if len(_cache) >= _MAXCACHE:[](#l1.28)
_cache.clear()[](#l1.29)
return p def _compile_repl(repl, pattern):_cache[type(pattern), pattern, flags] = p[](#l1.30)
--- 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, [](#l2.4)
cpython_only[](#l2.5)
cpython_only, captured_stdout[](#l2.6)
import io import re from re import Scanner @@ -1193,6 +1193,18 @@ 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)
+ class PatternReprTests(unittest.TestCase): def check(self, pattern, expected):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -16,6 +16,9 @@ Core and Builtins Library ------- +- Issue #20426: When passing the re.DEBUG flag, re.compile() displays the