Skip to content

Commit e9902f6

Browse files
committed
[GR-14599] Adapt to upcoming TRegex API changes.
PullRequest: graalpython/448
2 parents dd2b680 + 6f08d35 commit e9902f6

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

graalpython/lib-graalpython/_sre.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,29 @@ def maxsize():
4848
import sys
4949
return sys.maxsize
5050

51+
5152
class _RegexResult:
52-
def __init__(self, input, isMatch, groupCount, start, end, regex):
53-
self.input = input
53+
def __init__(self, pattern_input, isMatch, groupCount, start, end):
54+
self.input = pattern_input
5455
self.isMatch = isMatch
5556
self.groupCount = groupCount
5657
self.start = start
5758
self.end = end
58-
self.regex = regex
59+
60+
def getStart(self, grpidx):
61+
return self.start[grpidx]
62+
63+
def getEnd(self, grpidx):
64+
return self.end[grpidx]
65+
5966

6067
def _str_to_bytes(arg):
6168
buffer = bytearray(len(arg))
6269
for i, c in enumerate(arg):
6370
buffer[i] = ord(c)
6471
return bytes(buffer)
6572

73+
6674
def setup(sre_compiler, error_class, flags_table):
6775
global error
6876
error = error_class
@@ -82,22 +90,36 @@ def fallback_compiler(pattern, flags):
8290
bit_flags = bit_flags | FLAGS[flag]
8391

8492
compiled_pattern = sre_compiler(pattern if mode == "str" else _str_to_bytes(pattern), bit_flags)
85-
86-
def executable_pattern(regex_object, input, from_index):
87-
search_method = compiled_pattern.match if sticky else compiled_pattern.search
88-
result = search_method(input, from_index)
89-
is_match = result is not None
90-
group_count = 1 + compiled_pattern.groups
91-
return _RegexResult(
92-
input = input,
93-
isMatch = is_match,
94-
groupCount = group_count if is_match else 0,
95-
start = [result.start(i) for i in range(group_count)] if is_match else [],
96-
end = [result.end(i) for i in range(group_count)] if is_match else [],
97-
regex = regex_object
98-
)
99-
100-
return executable_pattern
93+
94+
# wraps a native 're.Pattern' object
95+
class ExecutablePattern:
96+
def __call__(self, *args):
97+
# deprecated
98+
return self.exec(*args)
99+
100+
def exec(self, *args):
101+
nargs = len(args)
102+
if nargs == 2:
103+
# new-style signature
104+
pattern_input, from_index = args
105+
elif nargs == 3:
106+
# old-style signature; deprecated
107+
_, pattern_input, from_index = args
108+
else:
109+
raise TypeError("invalid arguments: " + repr(args))
110+
search_method = compiled_pattern.match if sticky else compiled_pattern.search
111+
result = search_method(pattern_input, from_index)
112+
is_match = result is not None
113+
group_count = 1 + compiled_pattern.groups
114+
return _RegexResult(
115+
pattern_input = pattern_input,
116+
isMatch = is_match,
117+
groupCount = group_count if is_match else 0,
118+
start = [result.start(i) for i in range(group_count)] if is_match else [],
119+
end = [result.end(i) for i in range(group_count)] if is_match else []
120+
)
121+
122+
return ExecutablePattern()
101123

102124
return fallback_compiler
103125

0 commit comments

Comments
 (0)