Skip to content

Commit 6d43dda

Browse files
committed
Implement raising native exceptions
1 parent 3f2a6ac commit 6d43dda

32 files changed

+282
-227
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/ConversionNodeTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.junit.rules.ExpectedException;
4747

4848
import com.oracle.graal.python.PythonLanguage;
49+
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
4950
import com.oracle.graal.python.builtins.objects.function.PArguments;
5051
import com.oracle.graal.python.builtins.objects.function.Signature;
5152
import com.oracle.graal.python.nodes.PRootNode;
@@ -109,7 +110,9 @@ public boolean isPythonInternal() {
109110
}
110111
} catch (PException e) {
111112
// materialize PException's error message since we are leaving Python
112-
e.setMessage(e.getUnreifiedException().getFormattedMessage());
113+
if (e.getUnreifiedException() instanceof PBaseException managedException) {
114+
e.setMessage(managedException.getFormattedMessage());
115+
}
113116
throw e;
114117
}
115118
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Object execute(VirtualFrame frame) {
119119

120120
@TruffleBoundary
121121
private static void handleException(PythonContext context, PException e) {
122-
PBaseException pythonException = e.getEscapedException();
122+
Object pythonException = e.getEscapedException();
123123
if (!InlineIsBuiltinClassProfile.profileClassSlowPath(GetClassNode.getUncached().execute(pythonException), PythonBuiltinClassType.SystemExit)) {
124124
PyObjectCallMethodObjArgs callWrite = PyObjectCallMethodObjArgs.getUncached();
125125
callWrite.execute(null, context.getStderr(), T_WRITE, toTruffleStringUncached("Error in atexit._run_exitfuncs:\n"));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,7 @@ public final Object varArgExecute(VirtualFrame frame, Object self, Object[] argu
29162916
throw VarargsBuiltinDirectInvocationNotSupported.INSTANCE;
29172917
}
29182918
if (arguments.length == 1) {
2919-
return initNoArgs(arguments[0], PythonUtils.EMPTY_OBJECT_ARRAY, keywords);
2919+
return execute(frame, arguments[0], PythonUtils.EMPTY_OBJECT_ARRAY, keywords);
29202920
}
29212921
Object[] argsWithoutSelf = PythonUtils.arrayCopyOfRange(arguments, 1, arguments.length);
29222922
return execute(frame, arguments[0], argsWithoutSelf, keywords);

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@
186186
import com.oracle.graal.python.builtins.objects.str.StringNodes;
187187
import com.oracle.graal.python.builtins.objects.str.StringUtils;
188188
import com.oracle.graal.python.builtins.objects.thread.PThread;
189-
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
190-
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
191189
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
192190
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
193191
import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
@@ -792,19 +790,15 @@ public PTuple run(VirtualFrame frame,
792790
@Bind("this") Node inliningTarget,
793791
@Cached InlinedGetClassNode getClassNode,
794792
@Cached GetCaughtExceptionNode getCaughtExceptionNode,
795-
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
793+
@Cached ExceptionNodes.GetTracebackNode getTracebackNode) {
796794
PException currentException = getCaughtExceptionNode.execute(frame);
797795
assert currentException != PException.NO_EXCEPTION;
798796
if (currentException == null) {
799797
return factory().createTuple(new PNone[]{PNone.NONE, PNone.NONE, PNone.NONE});
800798
} else {
801-
PBaseException exception = currentException.getEscapedException();
802-
LazyTraceback lazyTraceback = currentException.getTraceback();
803-
PTraceback traceback = null;
804-
if (lazyTraceback != null) {
805-
traceback = materializeLazyTracebackNode.execute(lazyTraceback);
806-
}
807-
return factory().createTuple(new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback == null ? PNone.NONE : traceback});
799+
Object exception = currentException.getEscapedException();
800+
Object traceback = getTracebackNode.execute(inliningTarget, exception);
801+
return factory().createTuple(new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback});
808802
}
809803
}
810804

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
import static com.oracle.graal.python.builtins.objects.thread.AbstractPythonLock.TIMEOUT_MAX;
4444
import static com.oracle.graal.python.nodes.BuiltinNames.J__THREAD;
4545
import static com.oracle.graal.python.nodes.BuiltinNames.T__THREAD;
46+
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
4647

47-
import java.io.PrintWriter;
4848
import java.lang.ref.WeakReference;
4949
import java.util.List;
5050

@@ -55,14 +55,14 @@
5555
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5656
import com.oracle.graal.python.builtins.PythonBuiltins;
5757
import com.oracle.graal.python.builtins.objects.PNone;
58-
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
5958
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6059
import com.oracle.graal.python.builtins.objects.module.PythonModule;
6160
import com.oracle.graal.python.builtins.objects.thread.PLock;
6261
import com.oracle.graal.python.builtins.objects.thread.PRLock;
6362
import com.oracle.graal.python.builtins.objects.thread.PThread;
6463
import com.oracle.graal.python.builtins.objects.thread.PThreadLocal;
6564
import com.oracle.graal.python.nodes.ErrorMessages;
65+
import com.oracle.graal.python.nodes.WriteUnraisableNode;
6666
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
6767
import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode;
6868
import com.oracle.graal.python.nodes.call.CallNode;
@@ -88,6 +88,7 @@
8888
import com.oracle.truffle.api.object.DynamicObjectLibrary;
8989
import com.oracle.truffle.api.object.HiddenKey;
9090
import com.oracle.truffle.api.profiles.ConditionProfile;
91+
import com.oracle.truffle.api.strings.TruffleString;
9192

9293
@CoreFunctions(defineModule = J__THREAD)
9394
public class ThreadModuleBuiltins extends PythonBuiltins {
@@ -209,6 +210,9 @@ long getStackSize(long stackSize) {
209210
@Builtin(name = "start_new", minNumOfPositionalArgs = 3, maxNumOfPositionalArgs = 4)
210211
@GenerateNodeFactory
211212
abstract static class StartNewThreadNode extends PythonBuiltinNode {
213+
214+
private static final TruffleString IN_THREAD_STARTED_BY = tsLiteral("in thread started by");
215+
212216
@Specialization
213217
@SuppressWarnings("try")
214218
long start(VirtualFrame frame, Object cls, Object callable, Object args, Object kwargs,
@@ -256,24 +260,14 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
256260
} catch (PythonThreadKillException e) {
257261
return;
258262
} catch (PException e) {
259-
dumpError(context, e.getUnreifiedException(), callable);
260-
// TODO (cbasca): when GR-30386 is completed use the intrinsified
261-
// sys.unraisablehook
262-
// WriteUnraisableNode.getUncached().execute(e.getUnreifiedException(), "in
263-
// thread started by", callable);
263+
WriteUnraisableNode.getUncached().execute(e.getUnreifiedException(), IN_THREAD_STARTED_BY, callable);
264264
}
265265
}, env.getContext(), context.getThreadGroup());
266266

267267
PThread pThread = factory().createPythonThread(cls, thread);
268268
pThread.start();
269269
return pThread.getId();
270270
}
271-
272-
@TruffleBoundary
273-
static void dumpError(PythonContext context, PBaseException exception, Object object) {
274-
PrintWriter err = new PrintWriter(context.getStandardErr());
275-
err.println(String.format("%s in thread started by %s", exception.toString(), object));
276-
}
277271
}
278272

279273
@Builtin(name = "_set_sentinel", minNumOfPositionalArgs = 0)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextErrBuiltins.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import com.oracle.graal.python.builtins.objects.function.PKeyword;
9595
import com.oracle.graal.python.builtins.objects.module.PythonModule;
9696
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
97-
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
9897
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
9998
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
10099
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
@@ -110,6 +109,7 @@
110109
import com.oracle.graal.python.nodes.call.CallNode;
111110
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
112111
import com.oracle.graal.python.nodes.object.GetClassNode;
112+
import com.oracle.graal.python.nodes.object.InlinedGetClassNode;
113113
import com.oracle.graal.python.nodes.object.IsNode;
114114
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.GetCaughtExceptionNode;
115115
import com.oracle.graal.python.runtime.PythonContext;
@@ -167,23 +167,22 @@ Object restore(Object typ, Object val, Object tb,
167167
@CApiBuiltin(ret = PyObjectTransfer, call = Ignored)
168168
abstract static class PyTruffleErr_Fetch extends CApiNullaryBuiltinNode {
169169
@Specialization
170-
Object run(@Cached GetThreadStateNode getThreadStateNode,
171-
@Cached GetClassNode getClassNode,
172-
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
170+
Object run(
171+
@Bind("this") Node inliningTarget,
172+
@Cached GetThreadStateNode getThreadStateNode,
173+
@Cached InlinedGetClassNode getClassNode,
174+
@Cached ExceptionNodes.GetTracebackNode getTracebackNode) {
173175
PException currentException = getThreadStateNode.getCurrentException();
174176
Object result;
175177
if (currentException == null) {
176178
result = getNativeNull();
177179
} else {
178-
PBaseException exception = currentException.getEscapedException();
179-
Object traceback = null;
180-
if (currentException.getTraceback() != null) {
181-
traceback = materializeLazyTracebackNode.execute(currentException.getTraceback());
182-
}
183-
if (traceback == null) {
180+
Object exception = currentException.getEscapedException();
181+
Object traceback = getTracebackNode.execute(inliningTarget, exception);
182+
if (traceback == PNone.NONE) {
184183
traceback = getNativeNull();
185184
}
186-
result = factory().createTuple(new Object[]{getClassNode.execute(exception), exception, traceback});
185+
result = factory().createTuple(new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback});
187186
getThreadStateNode.setCurrentException(null);
188187
}
189188
return result;
@@ -354,23 +353,23 @@ Object raise(TruffleString name, TruffleString doc, Object base, Object dict,
354353
abstract static class PyTruffleErr_GetExcInfo extends CApiNullaryBuiltinNode {
355354
@Specialization
356355
Object info(
356+
@Bind("this") Node inliningTarget,
357357
@Cached GetCaughtExceptionNode getCaughtExceptionNode,
358-
@Cached GetClassNode getClassNode,
359-
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode,
358+
@Cached InlinedGetClassNode getClassNode,
359+
@Cached ExceptionNodes.GetTracebackNode getTracebackNode,
360360
@Cached BranchProfile noExceptionProfile) {
361361
PException currentException = getCaughtExceptionNode.executeFromNative();
362362
if (currentException == null) {
363363
noExceptionProfile.enter();
364364
return getNativeNull();
365365
}
366366
assert currentException != PException.NO_EXCEPTION;
367-
PBaseException exception = currentException.getEscapedException();
368-
LazyTraceback lazyTraceback = currentException.getTraceback();
369-
PTraceback traceback = null;
370-
if (lazyTraceback != null) {
371-
traceback = materializeLazyTracebackNode.execute(lazyTraceback);
367+
Object exception = currentException.getEscapedException();
368+
Object traceback = getTracebackNode.execute(inliningTarget, exception);
369+
if (traceback == PNone.NONE) {
370+
traceback = getNativeNull();
372371
}
373-
return factory().createTuple(new Object[]{getClassNode.execute(exception), exception, traceback == null ? PNone.NONE : traceback});
372+
return factory().createTuple(new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback});
374373
}
375374
}
376375

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextUnicodeBuiltins.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,11 @@
127127
import com.oracle.graal.python.lib.PyObjectLookupAttr;
128128
import com.oracle.graal.python.lib.PySliceNew;
129129
import com.oracle.graal.python.nodes.ErrorMessages;
130-
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
131130
import com.oracle.graal.python.nodes.PGuards;
132131
import com.oracle.graal.python.nodes.PRaiseNode;
133132
import com.oracle.graal.python.nodes.StringLiterals;
134133
import com.oracle.graal.python.nodes.call.CallNode;
135134
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
136-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
137135
import com.oracle.graal.python.nodes.object.GetClassNode;
138136
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
139137
import com.oracle.graal.python.nodes.truffle.PythonTypes;
@@ -1012,10 +1010,8 @@ Object doGeneric(TruffleString errors,
10121010
abstract static class PyUnicodeDecodeError_Create extends CApi6BuiltinNode {
10131011
@Specialization
10141012
Object doit(Object encoding, Object object, int length, int start, int end, Object reason,
1015-
@Bind("this") Node inliningTarget,
1016-
@Cached IsBuiltinObjectProfile isUnicodeDecode,
1017-
@Cached PConstructAndRaiseNode raiseNode,
1018-
@Cached GetByteArrayNode getByteArrayNode) {
1013+
@Cached GetByteArrayNode getByteArrayNode,
1014+
@Cached CallNode callNode) {
10191015
PBytes bytes;
10201016
try {
10211017
bytes = factory().createBytes(getByteArrayNode.execute(object, length));
@@ -1024,12 +1020,7 @@ Object doit(Object encoding, Object object, int length, int start, int end, Obje
10241020
} catch (OverflowException e) {
10251021
throw raise(PythonErrorType.SystemError, ErrorMessages.NEGATIVE_SIZE_PASSED);
10261022
}
1027-
try {
1028-
throw raiseNode.executeWithArgsOnly(null, UnicodeDecodeError, new Object[]{encoding, bytes, start, end, reason});
1029-
} catch (PException e) {
1030-
e.expect(inliningTarget, UnicodeDecodeError, isUnicodeDecode);
1031-
return e.getEscapedException();
1032-
}
1023+
return callNode.execute(UnicodeDecodeError, encoding, bytes, start, end, reason);
10331024
}
10341025
}
10351026
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedReaderMixinBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static byte[] bufferedreaderRawRead(VirtualFrame frame, Node inliningTarget, PBu
151151
try {
152152
n = asSizeNode.executeExact(frame, res, ValueError);
153153
} catch (PException e) {
154-
throw lazyRaiseNode.get(inliningTarget).raise(OSError, e, ErrorMessages.RAW_READINTO_FAILED);
154+
throw lazyRaiseNode.get(inliningTarget).raiseWithCause(OSError, e, ErrorMessages.RAW_READINTO_FAILED);
155155
}
156156
if (osError.profile(inliningTarget, n < 0 || n > len)) {
157157
throw lazyRaiseNode.get(inliningTarget).raise(OSError, IO_S_INVALID_LENGTH, "readinto()", n, len);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PThreadState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static Object doPrev(PThreadState receiver, String key,
258258
@Specialization(guards = "eq(key, J_EXC_INFO)")
259259
static Object doExcInfo(PThreadState receiver, @SuppressWarnings("unused") String key) {
260260
PException currentException = receiver.threadState.getCaughtException();
261-
PBaseException caughtExceptionObject = null;
261+
Object caughtExceptionObject = null;
262262
if (currentException != null) {
263263
caughtExceptionObject = currentException.getEscapedException();
264264
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PyErrStackItem.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext.capi;
4242

43+
import com.oracle.graal.python.builtins.objects.PNone;
4344
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4445
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToSulongNode;
4546
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions;
46-
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
47-
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
48-
import com.oracle.graal.python.nodes.object.GetClassNode;
47+
import com.oracle.graal.python.builtins.objects.exception.ExceptionNodes;
48+
import com.oracle.graal.python.nodes.object.InlinedGetClassNode;
4949
import com.oracle.graal.python.runtime.PythonContext;
5050
import com.oracle.truffle.api.dsl.Bind;
5151
import com.oracle.truffle.api.dsl.Cached;
@@ -68,9 +68,9 @@ public final class PyErrStackItem extends PythonNativeWrapper {
6868
public static final String J_EXC_TRACEBACK = "exc_traceback";
6969
public static final String J_PREVIOUS_ITEM = "previous_item";
7070

71-
private final PBaseException exception;
71+
private final Object exception;
7272

73-
public PyErrStackItem(PBaseException exception) {
73+
public PyErrStackItem(Object exception) {
7474
this.exception = exception;
7575
}
7676

@@ -94,23 +94,21 @@ boolean isMemberReadable(String key) {
9494

9595
@ExportMessage
9696
Object readMember(String key,
97-
@Cached GetClassNode getClassNode,
98-
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode,
97+
@Bind("$node") Node inliningTarget,
98+
@Cached InlinedGetClassNode getClassNode,
99+
@Cached ExceptionNodes.GetTracebackNode getTracebackNode,
99100
@Cached ToSulongNode toSulongNode) {
100101
Object result = null;
101102
if (exception != null) {
102103
switch (key) {
103-
case J_EXC_TYPE:
104-
result = getClassNode.execute(exception);
105-
break;
106-
case J_EXC_VALUE:
107-
result = exception;
108-
break;
109-
case J_EXC_TRACEBACK:
110-
if (exception.getTraceback() != null) {
111-
result = materializeLazyTracebackNode.execute(exception.getTraceback());
104+
case J_EXC_TYPE -> result = getClassNode.execute(inliningTarget, exception);
105+
case J_EXC_VALUE -> result = exception;
106+
case J_EXC_TRACEBACK -> {
107+
result = getTracebackNode.execute(inliningTarget, exception);
108+
if (result == PNone.NONE) {
109+
result = null;
112110
}
113-
break;
111+
}
114112
}
115113
}
116114
if (result == null) {

0 commit comments

Comments
 (0)