Skip to content

Commit 9dda0ec

Browse files
committed
Fix getting match groups
1 parent dc2eb11 commit 9dda0ec

File tree

2 files changed

+26
-21
lines changed
  • graalpython

2 files changed

+26
-21
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_re.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
*graalpython.lib-python.3.test.test_re.ReTests.test_large_subn
5656
*graalpython.lib-python.3.test.test_re.ReTests.test_lookahead
5757
*graalpython.lib-python.3.test.test_re.ReTests.test_lookbehind
58+
*graalpython.lib-python.3.test.test_re.ReTests.test_match_getitem
5859
*graalpython.lib-python.3.test.test_re.ReTests.test_multiple_repeat
5960
*graalpython.lib-python.3.test.test_re.ReTests.test_not_literal
6061
*graalpython.lib-python.3.test.test_re.ReTests.test_nothing_to_repeat

graalpython/lib-graalpython/_sre.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,50 +171,54 @@ def end(self, groupnum=0):
171171

172172
def group(self, *args):
173173
if not args:
174-
return self.__group__(0)
174+
return self.__group(0)
175175
elif len(args) == 1:
176-
return self.__group__(args[0])
176+
return self.__group(args[0])
177177
else:
178178
lst = []
179179
for arg in args:
180-
lst.append(self.__group__(arg))
180+
lst.append(self.__group(arg))
181181
return tuple(lst)
182182

183183
def groups(self, default=None):
184184
lst = []
185185
for arg in range(1, self.compiled_regex.groupCount):
186-
lst.append(self.__group__(arg, default))
186+
lst.append(self.__group(arg, default))
187187
return tuple(lst)
188188

189-
def __groupidx__(self, idx):
190-
if isinstance(idx, str):
191-
return self.compiled_regex.groups[idx]
192-
else:
193-
return idx
194-
195-
def __group__(self, idx, default=None):
196-
idxarg = self.__groupidx__(idx)
189+
def __getitem__(self, item):
190+
return self.__group(item)
191+
192+
def __groupidx(self, idx):
193+
try:
194+
if isinstance(idx, str):
195+
return self.compiled_regex.groups[idx]
196+
elif 0 <= idx < self.compiled_regex.groupCount:
197+
return idx
198+
except Exception:
199+
pass
200+
raise IndexError("no such group")
201+
202+
def __group(self, idx, default=None):
203+
idxarg = self.__groupidx(idx)
197204
start = self.result.getStart(idxarg)
198205
if start < 0:
199206
return default
200207
else:
201208
return self.input_str[start:self.result.getEnd(idxarg)]
202209

203210
def groupdict(self, default=None):
204-
d = {}
205-
if self.compiled_regex.groups:
206-
assert dir(self.compiled_regex.groups)
207-
for k in dir(self.compiled_regex.groups):
208-
idx = self.compiled_regex.groups[k]
209-
d[k] = self.__group__(idx)
210-
return d
211+
groups = self.compiled_regex.groups
212+
if groups:
213+
return {name: self.__group(name, default) for name in dir(groups)}
214+
return {}
211215

212216
def span(self, groupnum=0):
213-
idxarg = self.__groupidx__(groupnum)
217+
idxarg = self.__groupidx(groupnum)
214218
return (self.result.getStart(idxarg), self.result.getEnd(idxarg))
215219

216220
def start(self, groupnum=0):
217-
idxarg = self.__groupidx__(groupnum)
221+
idxarg = self.__groupidx(groupnum)
218222
return self.result.getStart(idxarg)
219223

220224
@property

0 commit comments

Comments
 (0)