Skip to content

Commit 759172f

Browse files
committed
Fix interop exception handling and use explicit wrapper für TRegex.
1 parent 0ad923c commit 759172f

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SREModuleBuiltins.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.RuntimeError;
44+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
45+
4346
import java.io.UnsupportedEncodingException;
4447
import java.util.List;
4548
import java.util.regex.Matcher;
@@ -53,16 +56,26 @@
5356
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
5457
import com.oracle.graal.python.builtins.objects.str.PString;
5558
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
59+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5660
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5761
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5862
import com.oracle.truffle.api.CompilerAsserts;
5963
import com.oracle.truffle.api.CompilerDirectives;
6064
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6165
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
66+
import com.oracle.truffle.api.dsl.Cached;
6267
import com.oracle.truffle.api.dsl.Fallback;
6368
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6469
import com.oracle.truffle.api.dsl.NodeFactory;
6570
import com.oracle.truffle.api.dsl.Specialization;
71+
import com.oracle.truffle.api.interop.ArityException;
72+
import com.oracle.truffle.api.interop.ForeignAccess;
73+
import com.oracle.truffle.api.interop.Message;
74+
import com.oracle.truffle.api.interop.TruffleObject;
75+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
76+
import com.oracle.truffle.api.interop.UnsupportedTypeException;
77+
import com.oracle.truffle.api.nodes.Node;
78+
import com.oracle.truffle.api.profiles.BranchProfile;
6679

6780
@CoreFunctions(defineModule = "_sre")
6881
public class SREModuleBuiltins extends PythonBuiltins {
@@ -240,4 +253,29 @@ Object run(Object o) {
240253

241254
}
242255

256+
@Builtin(name = "tregex_call_safe", minNumOfArguments = 1, takesVariableArguments = true)
257+
@GenerateNodeFactory
258+
abstract static class TRegexCallSafe extends PythonBuiltinNode {
259+
@Specialization(guards = "isForeignObject(callable)")
260+
Object call(TruffleObject callable, Object[] arguments,
261+
@Cached("create()") BranchProfile runtimeError,
262+
@Cached("create()") BranchProfile typeError,
263+
@Cached("createExecute()") Node invokeNode) {
264+
try {
265+
return ForeignAccess.sendExecute(invokeNode, callable, arguments);
266+
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
267+
typeError.enter();
268+
throw raise(TypeError, e.getMessage());
269+
} catch (RuntimeException e) {
270+
runtimeError.enter();
271+
throw raise(RuntimeError, e.getMessage());
272+
}
273+
}
274+
275+
protected static Node createExecute() {
276+
return Message.createExecute(0).createNode();
277+
}
278+
279+
}
280+
243281
}

graalpython/lib-graalpython/_sre.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def __init__(self, pattern, flags, code, groups=0, groupindex=None, indexgroup=N
153153

154154
def __tregex_compile(self, pattern):
155155
if TREGEX_ENGINE is not None:
156-
return TREGEX_ENGINE(pattern, self.jsflags)
156+
return tregex_call_safe(TREGEX_ENGINE, pattern, self.jsflags)
157157
raise RuntimeError("TREGEX engine not available")
158158

159159

@@ -211,9 +211,9 @@ def _search(self, pattern, string, pos, endpos):
211211
pattern = self.__tregex_compile(pattern)
212212
string = self._decode_string(string)
213213
if endpos == -1 or endpos >= len(string):
214-
result = pattern.exec(string, pos)
214+
result = tregex_call_safe(pattern.exec, string, pos)
215215
else:
216-
result = pattern.exec(string[:endpos], pos)
216+
result = tregex_call_safe(pattern.exec, string[:endpos], pos)
217217
if result.isMatch:
218218
return SRE_Match(self, pos, endpos, result)
219219
else:
@@ -255,7 +255,7 @@ def findall(self, string, pos=0, endpos=-1):
255255
endpos = endpos % len(string) + 1
256256
matchlist = []
257257
while pos < endpos:
258-
result = pattern.exec(string, pos)
258+
result = tregex_call_safe(pattern.exec, string, pos)
259259
if not result.isMatch:
260260
break
261261
elif self.num_groups == 0:
@@ -338,7 +338,7 @@ def sub(self, repl, string, count=0):
338338
repl = _process_escape_sequences(repl)
339339
progress = True
340340
while (count == 0 or n < count) and pos <= len(string) and progress:
341-
match_result = pattern.exec(string, pos)
341+
match_result = tregex_call_safe(pattern.exec, string, pos)
342342
if not match_result.isMatch:
343343
break
344344
n += 1

0 commit comments

Comments
 (0)