Commit c0799e3a authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22423: Fixed debugging output of the GROUPREF_EXISTS opcode in the re

module.
parent 91943460
...@@ -94,33 +94,42 @@ class SubPattern: ...@@ -94,33 +94,42 @@ class SubPattern:
self.data = data self.data = data
self.width = None self.width = None
def dump(self, level=0): def dump(self, level=0):
nl = 1 seqtypes = (tuple, list)
seqtypes = type(()), type([])
for op, av in self.data: for op, av in self.data:
print level*" " + op,; nl = 0 print level*" " + op,
if op == "in": if op == IN:
# member sublanguage # member sublanguage
print; nl = 1 print
for op, a in av: for op, a in av:
print (level+1)*" " + op, a print (level+1)*" " + op, a
elif op == "branch": elif op == BRANCH:
print; nl = 1 print
i = 0 for i, a in enumerate(av[1]):
for a in av[1]: if i:
if i > 0:
print level*" " + "or" print level*" " + "or"
a.dump(level+1); nl = 1 a.dump(level+1)
i = i + 1 elif op == GROUPREF_EXISTS:
elif type(av) in seqtypes: condgroup, item_yes, item_no = av
print condgroup
item_yes.dump(level+1)
if item_no:
print level*" " + "else"
item_no.dump(level+1)
elif isinstance(av, seqtypes):
nl = 0
for a in av: for a in av:
if isinstance(a, SubPattern): if isinstance(a, SubPattern):
if not nl: print if not nl:
a.dump(level+1); nl = 1 print
a.dump(level+1)
nl = 1
else: else:
print a, ; nl = 0 print a,
nl = 0
if not nl:
print
else: else:
print av, ; nl = 0 print av
if not nl: print
def __repr__(self): def __repr__(self):
return repr(self.data) return repr(self.data)
def __len__(self): def __len__(self):
......
...@@ -930,16 +930,33 @@ class ReTests(unittest.TestCase): ...@@ -930,16 +930,33 @@ class ReTests(unittest.TestCase):
self.assertEqual(m.group(2), "y") self.assertEqual(m.group(2), "y")
def test_debug_flag(self): def test_debug_flag(self):
pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
with captured_stdout() as out: with captured_stdout() as out:
re.compile('foo', re.DEBUG) re.compile(pat, re.DEBUG)
self.assertEqual(out.getvalue().splitlines(), dump = '''\
['literal 102', 'literal 111', 'literal 111']) subpattern 1
literal 46
subpattern None
branch
in
literal 99
literal 104
or
literal 112
literal 121
subpattern None
groupref_exists 1
at at_end
else
literal 58
literal 32
'''
self.assertEqual(out.getvalue(), dump)
# Debug output is output again even a second time (bypassing # Debug output is output again even a second time (bypassing
# the cache -- issue #20426). # the cache -- issue #20426).
with captured_stdout() as out: with captured_stdout() as out:
re.compile('foo', re.DEBUG) re.compile(pat, re.DEBUG)
self.assertEqual(out.getvalue().splitlines(), self.assertEqual(out.getvalue(), dump)
['literal 102', 'literal 111', 'literal 111'])
def test_keyword_parameters(self): def test_keyword_parameters(self):
# Issue #20283: Accepting the string keyword parameter. # Issue #20283: Accepting the string keyword parameter.
......
...@@ -22,6 +22,9 @@ Core and Builtins ...@@ -22,6 +22,9 @@ Core and Builtins
Library Library
------- -------
- Issue #22423: Fixed debugging output of the GROUPREF_EXISTS opcode in the re
module.
- Issue #22423: Unhandled exception in thread no longer causes unhandled - Issue #22423: Unhandled exception in thread no longer causes unhandled
AttributeError when sys.stderr is None. AttributeError when sys.stderr is None.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment