Skip to content

Commit 3688e02

Browse files
committed
Remove usage of members 'input'/'regex' of a regex result.
1 parent 19f473b commit 3688e02

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

graalpython/lib-graalpython/_sre.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ def new_compile(p, flags=0):
127127

128128

129129
class SRE_Match():
130-
def __init__(self, pattern, pos, endpos, result):
130+
def __init__(self, pattern, pos, endpos, result, input_str, compiled_regex):
131131
self.result = result
132-
self.compiled_regex = result.regex
132+
self.compiled_regex = compiled_regex
133133
self.re = pattern
134134
self.pos = pos
135135
self.endpos = endpos
136+
self.input_str = input_str
136137

137138
def end(self, groupnum=0):
138139
return self.result.end[groupnum]
@@ -166,7 +167,7 @@ def __group__(self, idx):
166167
if start < 0:
167168
return None
168169
else:
169-
return self.result.input[start:self.result.end[idxarg]]
170+
return self.input_str[start:self.result.end[idxarg]]
170171

171172
def groupdict(self, default=None):
172173
d = {}
@@ -187,7 +188,7 @@ def start(self, groupnum=0):
187188

188189
@property
189190
def string(self):
190-
return self.result.input
191+
return self.input_str
191192

192193
@property
193194
def lastgroup(self):
@@ -274,12 +275,14 @@ def __repr__(self):
274275

275276
def _search(self, pattern, string, pos, endpos, sticky=False):
276277
pattern = self.__tregex_compile(pattern, self.flags_str + ("y" if sticky else ""))
278+
input_str = string
277279
if endpos == -1 or endpos >= len(string):
278-
result = tregex_call_exec(pattern.exec, string, min(pos, len(string) + 1))
280+
result = tregex_call_exec(pattern.exec, input_str, min(pos, len(string) + 1))
279281
else:
280-
result = tregex_call_exec(pattern.exec, string[:endpos], min(pos, endpos % len(string) + 1))
282+
input_str = string[:endpos]
283+
result = tregex_call_exec(pattern.exec, input_str, min(pos, endpos % len(string) + 1))
281284
if result.isMatch:
282-
return SRE_Match(self, pos, endpos, result)
285+
return SRE_Match(self, pos, endpos, result, input_str, pattern)
283286
else:
284287
return None
285288

@@ -310,11 +313,12 @@ def finditer(self, string, pos=0, endpos=-1):
310313
elif endpos < 0:
311314
endpos = endpos % len(string) + 1
312315
while pos < endpos:
313-
result = tregex_call_exec(self.__tregex_compile(self.pattern).exec, string, pos)
316+
compiled_regex = self.__tregex_compile(self.pattern)
317+
result = tregex_call_exec(compiled_regex.exec, string, pos)
314318
if not result.isMatch:
315319
break
316320
else:
317-
yield SRE_Match(self, pos, endpos, result)
321+
yield SRE_Match(self, pos, endpos, result, string, compiled_regex)
318322
no_progress = (result.start[0] == result.end[0])
319323
pos = result.end[0] + no_progress
320324
return
@@ -327,15 +331,16 @@ def findall(self, string, pos=0, endpos=-1):
327331
endpos = endpos % len(string) + 1
328332
matchlist = []
329333
while pos < endpos:
330-
result = tregex_call_exec(self.__tregex_compile(self.pattern).exec, string, pos)
334+
compiled_regex = self.__tregex_compile(self.pattern)
335+
result = tregex_call_exec(compiled_regex.exec, string, pos)
331336
if not result.isMatch:
332337
break
333338
elif result.groupCount == 1:
334339
matchlist.append(self.__sanitize_out_type(string[result.start[0]:result.end[0]]))
335340
elif result.groupCount == 2:
336341
matchlist.append(self.__sanitize_out_type(string[result.start[1]:result.end[1]]))
337342
else:
338-
matchlist.append(tuple(map(self.__sanitize_out_type, SRE_Match(self, pos, endpos, result).groups())))
343+
matchlist.append(tuple(map(self.__sanitize_out_type, SRE_Match(self, pos, endpos, result, string, compiled_regex).groups())))
339344
no_progress = (result.start[0] == result.end[0])
340345
pos = result.end[0] + no_progress
341346
return matchlist
@@ -421,7 +426,7 @@ def sub(self, repl, string, count=0):
421426
if is_string_rep:
422427
result.append(self.__replace_groups(repl, string, match_result, pattern))
423428
else:
424-
_srematch = SRE_Match(self, pos, -1, match_result)
429+
_srematch = SRE_Match(self, pos, -1, match_result, string, pattern)
425430
_repl = repl(_srematch)
426431
result.append(_repl)
427432
pos = end

0 commit comments

Comments
 (0)