Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6632341

Browse files
committed
backout 9fcf4008b626 (#9179) for further consideration
1 parent b7138e2 commit 6632341

4 files changed

Lines changed: 12 additions & 67 deletions

File tree

Lib/re.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,10 @@ def __init__(self, lexicon, flags=0):
352352
s = sre_parse.Pattern()
353353
s.flags = flags
354354
for phrase, action in lexicon:
355-
gid = s.opengroup()
356355
p.append(sre_parse.SubPattern(s, [
357-
(SUBPATTERN, (gid, sre_parse.parse(phrase, flags))),
356+
(SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))),
358357
]))
359-
s.closegroup(gid, p[-1])
358+
s.groups = len(p)+1
360359
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
361360
self.scanner = sre_compile.compile(p)
362361
def scan(self, string):

Lib/sre_parse.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,24 @@ class Pattern:
6666
# master pattern object. keeps track of global attributes
6767
def __init__(self):
6868
self.flags = 0
69+
self.open = []
70+
self.groups = 1
6971
self.groupdict = {}
70-
self.subpatterns = [None] # group 0
71-
@property
72-
def groups(self):
73-
return len(self.subpatterns)
7472
def opengroup(self, name=None):
7573
gid = self.groups
76-
self.subpatterns.append(None)
74+
self.groups = gid + 1
7775
if name is not None:
7876
ogid = self.groupdict.get(name, None)
7977
if ogid is not None:
8078
raise error("redefinition of group name %s as group %d; "
8179
"was group %d" % (repr(name), gid, ogid))
8280
self.groupdict[name] = gid
81+
self.open.append(gid)
8382
return gid
84-
def closegroup(self, gid, p):
85-
self.subpatterns[gid] = p
83+
def closegroup(self, gid):
84+
self.open.remove(gid)
8685
def checkgroup(self, gid):
87-
return gid < self.groups and self.subpatterns[gid] is not None
86+
return gid < self.groups and gid not in self.open
8887

8988
class SubPattern:
9089
# a subpattern, in intermediate form
@@ -182,21 +181,7 @@ def getwidth(self):
182181
elif op in UNITCODES:
183182
lo = lo + 1
184183
hi = hi + 1
185-
elif op is GROUPREF:
186-
i, j = self.pattern.subpatterns[av].getwidth()
187-
lo = lo + i
188-
hi = hi + j
189-
elif op is GROUPREF_EXISTS:
190-
i, j = av[1].getwidth()
191-
if av[2] is not None:
192-
l, h = av[2].getwidth()
193-
i = min(i, l)
194-
j = max(j, h)
195-
else:
196-
i = 0
197-
lo = lo + i
198-
hi = hi + j
199-
elif op is SUCCESS:
184+
elif op == SUCCESS:
200185
break
201186
self.width = min(lo, MAXREPEAT - 1), min(hi, MAXREPEAT)
202187
return self.width
@@ -724,7 +709,7 @@ def _parse(source, state):
724709
if not sourcematch(")"):
725710
raise error("unbalanced parenthesis")
726711
if group is not None:
727-
state.closegroup(group, p)
712+
state.closegroup(group)
728713
subpatternappend((SUBPATTERN, (group, p)))
729714
else:
730715
while 1:

Lib/test/test_re.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def test_anyall(self):
557557
self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
558558
"a\n\nb")
559559

560-
def test_lookahead(self):
560+
def test_non_consuming(self):
561561
self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
562562
self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
563563
self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
@@ -571,42 +571,6 @@ def test_lookahead(self):
571571
self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
572572
self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
573573

574-
# Group reference.
575-
self.assertTrue(re.match(r'(a)b(?=\1)a', 'aba'))
576-
self.assertIsNone(re.match(r'(a)b(?=\1)c', 'abac'))
577-
# Conditional group reference.
578-
self.assertTrue(re.match('(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
579-
self.assertIsNone(re.match('(?:(a)|(x))b(?=(?(2)c|x))c', 'abc'))
580-
self.assertTrue(re.match('(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
581-
self.assertIsNone(re.match('(?:(a)|(x))b(?=(?(1)b|x))c', 'abc'))
582-
self.assertTrue(re.match('(?:(a)|(x))b(?=(?(1)c|x))c', 'abc'))
583-
# Group used before defined.
584-
self.assertTrue(re.match('(a)b(?=(?(2)x|c))(c)', 'abc'))
585-
self.assertIsNone(re.match('(a)b(?=(?(2)b|x))(c)', 'abc'))
586-
self.assertTrue(re.match('(a)b(?=(?(1)c|x))(c)', 'abc'))
587-
588-
def test_lookbehind(self):
589-
self.assertTrue(re.match('ab(?<=b)c', 'abc'))
590-
self.assertIsNone(re.match('ab(?<=c)c', 'abc'))
591-
self.assertIsNone(re.match('ab(?<!b)c', 'abc'))
592-
self.assertTrue(re.match('ab(?<!c)c', 'abc'))
593-
# Group reference.
594-
self.assertTrue(re.match(r'(a)a(?<=\1)c', 'aac'))
595-
self.assertIsNone(re.match(r'(a)b(?<=\1)a', 'abaa'))
596-
self.assertIsNone(re.match(r'(a)a(?<!\1)c', 'aac'))
597-
self.assertTrue(re.match(r'(a)b(?<!\1)a', 'abaa'))
598-
# Conditional group reference.
599-
self.assertIsNone(re.match('(?:(a)|(x))b(?<=(?(2)x|c))c', 'abc'))
600-
self.assertIsNone(re.match('(?:(a)|(x))b(?<=(?(2)b|x))c', 'abc'))
601-
self.assertTrue(re.match('(?:(a)|(x))b(?<=(?(2)x|b))c', 'abc'))
602-
self.assertIsNone(re.match('(?:(a)|(x))b(?<=(?(1)c|x))c', 'abc'))
603-
self.assertTrue(re.match('(?:(a)|(x))b(?<=(?(1)b|x))c', 'abc'))
604-
# Group used before defined.
605-
self.assertIsNone(re.match('(a)b(?<=(?(2)x|c))(c)', 'abc'))
606-
self.assertIsNone(re.match('(a)b(?<=(?(2)b|x))(c)', 'abc'))
607-
self.assertIsNone(re.match('(a)b(?<=(?(1)c|x))(c)', 'abc'))
608-
self.assertTrue(re.match('(a)b(?<=(?(1)b|x))(c)', 'abc'))
609-
610574
def test_ignore_case(self):
611575
self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
612576
self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC")

Misc/NEWS

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ Library
6868
- Issue #22821: Fixed fcntl() with integer argument on 64-bit big-endian
6969
platforms.
7070

71-
- Issues #814253, #9179: Group references and conditional group references now
72-
work in lookbehind assertions in regular expressions.
73-
7471
- Issue #22406: Fixed the uu_codec codec incorrectly ported to 3.x.
7572
Based on patch by Martin Panter.
7673

0 commit comments

Comments
 (0)