Skip to content

Commit a488d69

Browse files
committed
simplify some more jython emulation code
1 parent b9d58d8 commit a488d69

File tree

3 files changed

+19
-92
lines changed

3 files changed

+19
-92
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,14 +1068,6 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
10681068

10691069
@Fallback
10701070
boolean isInstance(VirtualFrame frame, Object instance, Object cls) {
1071-
if (getPythonLanguage().getEngineOption(PythonOptions.EmulateJython)) {
1072-
TruffleLanguage.Env env = getContext().getEnv();
1073-
if (env.isHostObject(cls)) {
1074-
Object hostCls = env.asHostObject(cls);
1075-
Object hostInstance = env.isHostObject(instance) ? env.asHostObject(instance) : instance;
1076-
return hostCls instanceof Class && ((Class<?>) hostCls).isAssignableFrom(hostInstance.getClass());
1077-
}
1078-
}
10791071
TriState check = isInstanceCheckInternal(frame, instance, cls);
10801072
if (check == TriState.UNDEFINED) {
10811073
return typeInstanceCheckNode.executeWith(frame, cls, instance);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ExceptNode.java

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oracle.truffle.api.instrumentation.InstrumentableNode;
5858
import com.oracle.truffle.api.instrumentation.ProbeNode;
5959
import com.oracle.truffle.api.interop.InteropLibrary;
60+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
6061
import com.oracle.truffle.api.library.CachedLibrary;
6162
import com.oracle.truffle.api.nodes.Node;
6263

@@ -104,15 +105,8 @@ public boolean matchesPException(VirtualFrame frame, PException e) {
104105

105106
public boolean matchesTruffleException(@SuppressWarnings("unused") VirtualFrame frame, AbstractTruffleException e) {
106107
assert !(e instanceof PException);
107-
// TODO: (tfel) should we allow catching with the meta-object of arbitrary truffle
108-
// exceptions?
109-
return exceptType == null;
110-
}
111-
112-
public boolean matchesHostException(VirtualFrame frame, Throwable e) {
113-
assert !(e instanceof AbstractTruffleException);
114108
if (exceptType == null) {
115-
return false; // host exceptions must be matched explicitly
109+
return true;
116110
}
117111
return getMatchNode().executeMatch(frame, e, exceptType.execute(frame));
118112
}
@@ -147,16 +141,14 @@ public boolean isInstrumentable() {
147141
}
148142
}
149143

150-
interface EmulateJythonNode {
151-
default boolean emulateJython(PythonLanguage language) {
152-
return language.getEngineOption(PythonOptions.EmulateJython);
153-
}
154-
}
155-
156144
@ImportStatic(PythonOptions.class)
157-
abstract class ValidExceptionNode extends Node implements EmulateJythonNode {
145+
abstract class ValidExceptionNode extends Node {
158146
protected abstract boolean execute(VirtualFrame frame, Object type);
159147

148+
protected static boolean emulateJython(PythonLanguage language) {
149+
return language.getEngineOption(PythonOptions.EmulateJython);
150+
}
151+
160152
protected static boolean isPythonExceptionType(PythonBuiltinClassType type) {
161153
PythonBuiltinClassType base = type;
162154
while (base != null) {
@@ -209,7 +201,7 @@ static ValidExceptionNode create() {
209201
}
210202

211203
@ImportStatic({PGuards.class, PythonOptions.class})
212-
abstract class ExceptMatchNode extends Node implements EmulateJythonNode {
204+
abstract class ExceptMatchNode extends Node {
213205
@Child private PRaiseNode raiseNode;
214206

215207
protected abstract boolean executeMatch(VirtualFrame frame, Object exception, Object clause);
@@ -238,38 +230,20 @@ boolean matchPythonSingle(VirtualFrame frame, PException e, Object clause,
238230
return isSubtype.execute(frame, plib.getLazyPythonClass(e.getUnreifiedException()), clause);
239231
}
240232

241-
@Specialization(guards = {"emulateJython(language)", "context.getEnv().isHostException(e)", "context.getEnv().isHostObject(clause)"})
242-
@SuppressWarnings("unused")
243-
boolean matchJava(VirtualFrame frame, Throwable e, Object clause,
244-
@Cached ValidExceptionNode isValidException,
245-
@CachedLanguage PythonLanguage language,
246-
@CachedContext(PythonLanguage.class) PythonContext context) {
247-
raiseIfNoException(frame, clause, isValidException);
248-
// cast must succeed due to ValidExceptionNode above
249-
Class<?> javaClause = (Class<?>) context.getEnv().asHostObject(clause);
250-
Throwable hostException = context.getEnv().asHostException(e);
251-
return javaClause.isInstance(hostException);
252-
}
253-
254-
@Specialization(guards = {"emulateJython(language)", "context.getEnv().isHostObject(clause)"})
233+
@Specialization(guards = {"eLib.isException(e)", "clauseLib.isMetaObject(clause)"}, limit = "3", replaces = "matchPythonSingle")
255234
@SuppressWarnings("unused")
256-
boolean doNotMatchPython(VirtualFrame frame, @SuppressWarnings("unused") PException e, Object clause,
257-
@CachedLanguage PythonLanguage language,
258-
@CachedContext(PythonLanguage.class) PythonContext context,
259-
@Cached ValidExceptionNode isValidException) {
260-
raiseIfNoException(frame, clause, isValidException);
261-
return false;
262-
}
263-
264-
@Specialization(guards = {"lib.isLazyPythonClass(clause)", "emulateJython(language)", "context.getEnv().isHostException(e)"})
265-
@SuppressWarnings("unused")
266-
boolean doNotMatchJava(VirtualFrame frame, @SuppressWarnings("unused") Throwable e, Object clause,
267-
@CachedLanguage PythonLanguage language,
268-
@CachedContext(PythonLanguage.class) PythonContext context,
235+
boolean matchJava(VirtualFrame frame, AbstractTruffleException e, Object clause,
269236
@Cached ValidExceptionNode isValidException,
270-
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
237+
@CachedLibrary("e") InteropLibrary eLib,
238+
@CachedLibrary("clause") InteropLibrary clauseLib) {
239+
// n.b.: we can only allow Java exceptions in clauses, because we cannot tell for other
240+
// foreign exception types if they *are* exception types
271241
raiseIfNoException(frame, clause, isValidException);
272-
return false;
242+
try {
243+
return clauseLib.isMetaInstance(clause, e);
244+
} catch (UnsupportedMessageException e1) {
245+
throw CompilerDirectives.shouldNotReachHere();
246+
}
273247
}
274248

275249
@Specialization

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/TryExceptNode.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.oracle.graal.python.nodes.literal.TupleLiteralNode;
4040
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.ExceptionState;
4141
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.SetCaughtExceptionNode;
42-
import com.oracle.graal.python.runtime.PythonOptions;
4342
import com.oracle.graal.python.runtime.exception.ExceptionHandledException;
4443
import com.oracle.graal.python.runtime.exception.PException;
4544
import com.oracle.graal.python.runtime.interop.InteropArray;
@@ -105,12 +104,6 @@ public void executeVoid(VirtualFrame frame) {
105104
} catch (ControlFlowException e) {
106105
throw e;
107106
} catch (Exception | StackOverflowError | AssertionError e) {
108-
if (shouldCatchJavaExceptions() && getContext().getEnv().isHostException(e)) {
109-
boolean handled = catchHostException(frame, e);
110-
if (handled) {
111-
return;
112-
}
113-
}
114107
PException pe = wrapJavaExceptionIfApplicable(e);
115108
if (pe != null) {
116109
boolean handled = catchPException(frame, pe);
@@ -185,34 +178,6 @@ private boolean catchTruffleException(VirtualFrame frame, AbstractTruffleExcepti
185178
return false;
186179
}
187180

188-
@ExplodeLoop(kind = LoopExplosionKind.FULL_EXPLODE_UNTIL_RETURN)
189-
private boolean catchHostException(VirtualFrame frame, Throwable exception) {
190-
assert !(exception instanceof AbstractTruffleException);
191-
try {
192-
for (ExceptNode exceptNode : exceptNodes) {
193-
if (everMatched.profile(exceptNode.matchesHostException(frame, exception))) {
194-
ExceptionState exceptionState = saveExceptionState(frame);
195-
try {
196-
exceptNode.executeExcept(frame, exception);
197-
} finally {
198-
restoreExceptionState(frame, exceptionState);
199-
}
200-
}
201-
}
202-
} catch (ExceptionHandledException eh) {
203-
return true;
204-
} catch (PException handlerException) {
205-
throw handlerException;
206-
} catch (Exception | StackOverflowError | AssertionError e) {
207-
PException handlerException = wrapJavaExceptionIfApplicable(e);
208-
if (handlerException == null) {
209-
throw e;
210-
}
211-
throw handlerException.getExceptionForReraise();
212-
}
213-
return false;
214-
}
215-
216181
public StatementNode getBody() {
217182
return body;
218183
}
@@ -327,8 +292,4 @@ public void setCatchesFunction(CatchesFunction catchesFunction) {
327292
CompilerDirectives.transferToInterpreterAndInvalidate();
328293
this.catchesFunction = catchesFunction;
329294
}
330-
331-
private boolean shouldCatchJavaExceptions() {
332-
return getPythonLanguage().getEngineOption(PythonOptions.EmulateJython);
333-
}
334295
}

0 commit comments

Comments
 (0)