Skip to content

Commit d83de92

Browse files
committed
Fix: 'SRE_Pattern.findall' used invalid group count.
1 parent 08b0ce5 commit d83de92

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_re.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,32 @@ def test_re_split(self):
298298
# with self.subTest(sep=sep), self.assertRaises(ValueError):
299299
# self.assertTypedEqual(re.split(sep, ':a:b::c'), expected)
300300

301+
def test_re_findall(self):
302+
self.assertEqual(re.findall(":+", "abc"), [])
303+
for string in "a:b::c:::d", S("a:b::c:::d"):
304+
self.assertTypedEqual(re.findall(":+", string),
305+
[":", "::", ":::"])
306+
self.assertTypedEqual(re.findall("(:+)", string),
307+
[":", "::", ":::"])
308+
self.assertTypedEqual(re.findall("(:)(:*)", string),
309+
[(":", ""), (":", ":"), (":", "::")])
310+
for string in (b"a:b::c:::d", B(b"a:b::c:::d"), bytearray(b"a:b::c:::d"),
311+
memoryview(b"a:b::c:::d")):
312+
self.assertTypedEqual(re.findall(b":+", string),
313+
[b":", b"::", b":::"])
314+
self.assertTypedEqual(re.findall(b"(:+)", string),
315+
[b":", b"::", b":::"])
316+
self.assertTypedEqual(re.findall(b"(:)(:*)", string),
317+
[(b":", b""), (b":", b":"), (b":", b"::")])
318+
for x in ("\xe0", "\u0430", "\U0001d49c"):
319+
xx = x * 2
320+
xxx = x * 3
321+
string = "a%sb%sc%sd" % (x, xx, xxx)
322+
self.assertEqual(re.findall("%s+" % x, string), [x, xx, xxx])
323+
self.assertEqual(re.findall("(%s+)" % x, string), [x, xx, xxx])
324+
self.assertEqual(re.findall("(%s)(%s*)" % (x, x), string),
325+
[(x, ""), (x, x), (x, xx)])
326+
301327
def test_ignore_case_set(self):
302328
self.assertTrue(re.match(r'[19A]', 'A', re.I))
303329
self.assertTrue(re.match(r'[19a]', 'a', re.I))

graalpython/lib-graalpython/_sre.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,13 @@ def findall(self, string, pos=0, endpos=-1):
255255
result = tregex_call_safe(pattern.exec, string, pos)
256256
if not result.isMatch:
257257
break
258-
elif self.num_groups == 0:
259-
matchlist.append("")
260-
elif self.num_groups == 1:
261-
matchlist.append(string[result.start[1]:result.end[1]])
258+
elif result.groupCount == 0:
259+
assert False, "inconsistent regex result"
260+
matchlist.append('')
261+
elif result.groupCount == 1:
262+
matchlist.append(str(string[result.start[0]:result.end[0]]))
263+
elif result.groupCount == 2:
264+
matchlist.append(str(string[result.start[1]:result.end[1]]))
262265
else:
263266
matchlist.append(SRE_Match(self, pos, endpos, result).groups())
264267
no_progress = (result.start[0] == result.end[0])

0 commit comments

Comments
 (0)