mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug output every time it is called, regardless of the compilation cache.
This commit is contained in:
commit
c49672f25e
3 changed files with 26 additions and 8 deletions
|
@ -272,6 +272,8 @@ _pattern_type = type(sre_compile.compile("", 0))
|
||||||
_MAXCACHE = 512
|
_MAXCACHE = 512
|
||||||
def _compile(pattern, flags):
|
def _compile(pattern, flags):
|
||||||
# internal: compile pattern
|
# internal: compile pattern
|
||||||
|
bypass_cache = flags & DEBUG
|
||||||
|
if not bypass_cache:
|
||||||
try:
|
try:
|
||||||
return _cache[type(pattern), pattern, flags]
|
return _cache[type(pattern), pattern, flags]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -284,6 +286,7 @@ def _compile(pattern, flags):
|
||||||
if not sre_compile.isstring(pattern):
|
if not sre_compile.isstring(pattern):
|
||||||
raise TypeError("first argument must be string or compiled pattern")
|
raise TypeError("first argument must be string or compiled pattern")
|
||||||
p = sre_compile.compile(pattern, flags)
|
p = sre_compile.compile(pattern, flags)
|
||||||
|
if not bypass_cache:
|
||||||
if len(_cache) >= _MAXCACHE:
|
if len(_cache) >= _MAXCACHE:
|
||||||
_cache.clear()
|
_cache.clear()
|
||||||
_cache[type(pattern), pattern, flags] = p
|
_cache[type(pattern), pattern, flags] = p
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
|
from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
|
||||||
cpython_only
|
cpython_only, captured_stdout
|
||||||
import io
|
import io
|
||||||
import re
|
import re
|
||||||
from re import Scanner
|
from re import Scanner
|
||||||
|
@ -1193,6 +1193,18 @@ class ReTests(unittest.TestCase):
|
||||||
self.assertEqual(m.group(1), "")
|
self.assertEqual(m.group(1), "")
|
||||||
self.assertEqual(m.group(2), "y")
|
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):
|
class PatternReprTests(unittest.TestCase):
|
||||||
def check(self, pattern, expected):
|
def check(self, pattern, expected):
|
||||||
|
|
|
@ -16,6 +16,9 @@ Core and Builtins
|
||||||
Library
|
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.
|
- Issue #20368: The null character now correctly passed from Tcl to Python.
|
||||||
Improved error handling in variables-related commands.
|
Improved error handling in variables-related commands.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue