Skip to content

Commit 7a300ec

Browse files
committed
Use nodes to consume current exception
1 parent 284550a commit 7a300ec

File tree

9 files changed

+185
-161
lines changed

9 files changed

+185
-161
lines changed

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

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiUnaryBuiltinNode;
8383
import com.oracle.graal.python.builtins.modules.cext.PythonCextFileBuiltins.PyFile_WriteObject;
8484
import com.oracle.graal.python.builtins.objects.PNone;
85+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ClearCurrentExceptionNode;
8586
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode;
8687
import com.oracle.graal.python.builtins.objects.cext.capi.PThreadState;
8788
import com.oracle.graal.python.builtins.objects.cext.common.NativePointer;
@@ -111,7 +112,7 @@
111112
import com.oracle.graal.python.nodes.WriteUnraisableNode;
112113
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
113114
import com.oracle.graal.python.nodes.call.CallNode;
114-
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles;
115+
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
115116
import com.oracle.graal.python.nodes.object.GetClassNode;
116117
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.GetCaughtExceptionNode;
117118
import com.oracle.graal.python.runtime.PythonContext;
@@ -121,6 +122,7 @@
121122
import com.oracle.graal.python.runtime.exception.PException;
122123
import com.oracle.graal.python.runtime.exception.PythonErrorType;
123124
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
125+
import com.oracle.truffle.api.CompilerAsserts;
124126
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
125127
import com.oracle.truffle.api.dsl.Bind;
126128
import com.oracle.truffle.api.dsl.Cached;
@@ -142,16 +144,17 @@ abstract static class PyErr_Restore extends CApiTernaryBuiltinNode {
142144

143145
@Specialization
144146
Object restore(Object typ, Object val, Object tb,
147+
@Bind("this") Node inliningTarget,
148+
@Cached GetThreadStateNode getThreadStateNode,
145149
@Cached PrepareExceptionNode prepareExceptionNode,
146-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
147-
PythonLanguage language = getLanguage();
148-
PythonContext.PythonThreadState threadState = getContext().getThreadState(language);
150+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
151+
@Cached ClearCurrentExceptionNode clearCurrentExceptionNode) {
149152
if (typ == PNone.NO_VALUE && val == PNone.NO_VALUE) {
150-
threadState.clearCurrentException();
153+
clearCurrentExceptionNode.execute(inliningTarget, getThreadStateNode.execute(inliningTarget));
151154
} else {
152155
Object exception = prepareExceptionNode.execute(null, typ, val);
153-
PException e = PException.fromExceptionInfo(exception, PythonOptions.isPExceptionWithJavaStacktrace(language));
154-
transformExceptionToNativeNode.execute(this, e, tb instanceof PTraceback ptb ? new LazyTraceback(ptb) : null);
156+
PException e = PException.fromExceptionInfo(exception, PythonOptions.isPExceptionWithJavaStacktrace(getLanguage()));
157+
transformExceptionToNativeNode.execute(inliningTarget, e, tb instanceof PTraceback ptb ? new LazyTraceback(ptb) : null);
155158
}
156159
return PNone.NO_VALUE;
157160
}
@@ -194,7 +197,8 @@ Object run(
194197
@Cached GetThreadStateNode getThreadStateNode,
195198
@Cached GetClassNode getClassNode,
196199
@Cached MaterializeLazyTracebackNode materializeTraceback,
197-
@Cached PythonObjectFactory factory) {
200+
@Cached PythonObjectFactory factory,
201+
@Cached ClearCurrentExceptionNode clearCurrentExceptionNode) {
198202
PythonContext.PythonThreadState threadState = getThreadStateNode.execute(inliningTarget);
199203
PException currentException = threadState.getCurrentException();
200204
Object result;
@@ -210,36 +214,21 @@ Object run(
210214
traceback = getNativeNull();
211215
}
212216
result = factory.createTuple(new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback});
213-
threadState.clearCurrentException();
217+
clearCurrentExceptionNode.execute(inliningTarget, threadState);
214218
}
215219
return result;
216220
}
217221
}
218222

219-
@CApiBuiltin(ret = PyObjectBorrowed, args = {}, call = Direct)
220-
abstract static class PyErr_Occurred extends CApiNullaryBuiltinNode {
221-
@Specialization
222-
Object run(
223-
@Bind("this") Node inliningTarget,
224-
@Cached GetThreadStateNode getThreadStateNode,
225-
@Cached GetClassNode getClassNode) {
226-
PException currentException = getThreadStateNode.execute(inliningTarget).getCurrentException();
227-
if (currentException != null) {
228-
// getClassNode acts as a branch profile
229-
return getClassNode.execute(inliningTarget, currentException.getUnreifiedException());
230-
}
231-
return getNativeNull();
232-
}
233-
}
234-
235-
@CApiBuiltin(ret = PyObjectBorrowed, args = {PyThreadState}, call = Direct)
236-
abstract static class _PyErr_Occurred extends CApiUnaryBuiltinNode {
223+
@CApiBuiltin(ret = PyObjectBorrowed, args = {PyThreadState}, call = Ignored)
224+
abstract static class _PyTruffleErr_Occurred extends CApiUnaryBuiltinNode {
237225
@Specialization
238226
Object run(PThreadState state,
239227
@Bind("this") Node inliningTarget,
240228
@Cached GetClassNode getClassNode) {
241229
PException currentException = state.getThreadState().getCurrentException();
242230
if (currentException != null) {
231+
// getClassNode acts as a branch profile
243232
return getClassNode.execute(inliningTarget, currentException.getUnreifiedException());
244233
}
245234
return getNativeNull();
@@ -423,7 +412,8 @@ abstract static class _PyErr_WriteUnraisableMsg extends CApiBinaryBuiltinNode {
423412
static Object write(Object msg, Object obj,
424413
@Bind("this") Node inliningTarget,
425414
@Cached GetThreadStateNode getThreadStateNode,
426-
@Cached WriteUnraisableNode writeUnraisableNode) {
415+
@Cached WriteUnraisableNode writeUnraisableNode,
416+
@Cached ClearCurrentExceptionNode clearCurrentExceptionNode) {
427417
PythonContext.PythonThreadState threadState = getThreadStateNode.execute(inliningTarget, PythonContext.get(inliningTarget));
428418
if (threadState.getCurrentException() == null) {
429419
// This means an invalid call, but this function is not supposed to raise exceptions
@@ -436,7 +426,7 @@ static Object write(Object msg, Object obj,
436426
m = (TruffleString) msg;
437427
}
438428
writeUnraisableNode.execute(exc, m, (obj == PNone.NO_VALUE) ? PNone.NONE : obj);
439-
threadState.clearCurrentException();
429+
clearCurrentExceptionNode.execute(inliningTarget, threadState);
440430
return PNone.NONE;
441431
}
442432
}
@@ -445,10 +435,8 @@ static Object write(Object msg, Object obj,
445435
abstract static class PyErr_PrintEx extends CApiUnaryBuiltinNode {
446436
@TruffleBoundary
447437
@Specialization
448-
Object raise(int set_sys_last_vars,
449-
@Bind("this") Node inliningTarget,
450-
@Cached BuiltinClassProfiles.IsBuiltinObjectProfile exceptionProfile,
451-
@Cached PyErr_Occurred errOccuredNode,
438+
static Object raise(int set_sys_last_vars,
439+
@Cached _PyTruffleErr_Occurred errOccuredNode,
452440
@Cached TupleBuiltins.GetItemNode getItemNode,
453441
@Cached IsInstanceNode isInstanceNode,
454442
@Cached ExcInfoNode excInfoNode,
@@ -457,11 +445,12 @@ Object raise(int set_sys_last_vars,
457445
@Cached ExitNode exitNode,
458446
@Cached PyTruffleErr_Fetch fetchNode,
459447
@Cached PyErr_Display errDisplayNode) {
460-
NativePointer nativeNull = getNativeNull();
448+
PythonContext context = PythonContext.get(null);
449+
NativePointer nativeNull = context.getNativeNull();
461450

462-
Object err = errOccuredNode.execute();
463-
PythonModule sys = getCore().getSysModule();
464-
if (err != nativeNull && exceptionProfile.profileObject(inliningTarget, err, PythonBuiltinClassType.SystemExit)) {
451+
Object err = errOccuredNode.execute(context.getThreadState(PythonLanguage.get(null)));
452+
PythonModule sys = context.getSysModule();
453+
if (err != nativeNull && IsBuiltinObjectProfile.profileObjectUncached(err, PythonBuiltinClassType.SystemExit)) {
465454
handleSystemExit(excInfoNode, getItemNode, isInstanceNode, restoreNode, (SysModuleBuiltins) sys.getBuiltins(), writeFileNode, exitNode);
466455
}
467456
Object fetched = fetchNode.execute(null);
@@ -495,8 +484,8 @@ Object raise(int set_sys_last_vars,
495484
return PNone.NONE;
496485
}
497486

498-
@TruffleBoundary
499487
private static void writeLastVars(PythonModule sys, Object type, Object val, Object tb, PyErr_Restore restoreNode) {
488+
CompilerAsserts.neverPartOfCompilation();
500489
try {
501490
WriteAttributeToObjectNode.getUncached().execute(sys, T_LAST_TYPE, type);
502491
WriteAttributeToObjectNode.getUncached().execute(sys, T_LAST_VALUE, val);
@@ -506,9 +495,9 @@ private static void writeLastVars(PythonModule sys, Object type, Object val, Obj
506495
}
507496
}
508497

509-
@TruffleBoundary
510498
private static void handleExceptHook(Object exceptHook, Object type, Object val, Object tb, ExcInfoNode excInfoNode,
511499
GetItemNode getItemNode, PythonModule sys, PyErr_Display errDisplayNode) {
500+
CompilerAsserts.neverPartOfCompilation();
512501
try {
513502
CallNode.getUncached().execute(exceptHook, type, val, tb);
514503
} catch (PException e) {
@@ -527,9 +516,9 @@ private static void handleExceptHook(Object exceptHook, Object type, Object val,
527516
}
528517
}
529518

530-
@TruffleBoundary
531519
private static void handleSystemExit(ExcInfoNode excInfoNode, TupleBuiltins.GetItemNode getItemNode, IsInstanceNode isInstanceNode,
532520
PyErr_Restore restoreNode, SysModuleBuiltins sys, PyFile_WriteObject writeFileNode, ExitNode exitNode) {
521+
CompilerAsserts.neverPartOfCompilation();
533522
PTuple sysInfo = excInfoNode.execute(null);
534523
int rc = 0;
535524
Object returnObject = null;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.builtins.objects.cext.capi;
4242

4343
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.CImpl;
44+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.Direct;
4445
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.Ignored;
4546
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.NotImplemented;
4647
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.CHAR;
@@ -245,6 +246,7 @@ public final class CApiFunction {
245246
@CApiBuiltin(name = "PyErr_GetExcInfo", ret = Void, args = {PyObjectPtr, PyObjectPtr, PyObjectPtr}, call = CImpl)
246247
@CApiBuiltin(name = "PyErr_NoMemory", ret = PyObject, args = {}, call = CImpl)
247248
@CApiBuiltin(name = "PyErr_NormalizeException", ret = Void, args = {PyObjectPtr, PyObjectPtr, PyObjectPtr}, call = CImpl)
249+
@CApiBuiltin(name = "PyErr_Occurred", ret = PyObjectBorrowed, args = {}, call = CImpl)
248250
@CApiBuiltin(name = "PyErr_Print", ret = Void, args = {}, call = CImpl)
249251
@CApiBuiltin(name = "PyErr_ResourceWarning", ret = Int, args = {PyObject, Py_ssize_t, ConstCharPtrAsTruffleString, VARARGS}, call = CImpl)
250252
@CApiBuiltin(name = "PyErr_SetFromErrno", ret = PyObject, args = {PyObject}, call = CImpl)

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@
103103
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CArrayWrapper;
104104
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CByteArrayWrapper;
105105
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CStringWrapper;
106-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.CheckFunctionResultNode;
107106
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.EnsureTruffleStringNode;
107+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionFromNativeNode;
108108
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
109109
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext.ModuleSpec;
110110
import com.oracle.graal.python.builtins.objects.cext.common.GetNextVaArgNode;
@@ -159,6 +159,7 @@
159159
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
160160
import com.oracle.graal.python.runtime.PythonContext;
161161
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
162+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
162163
import com.oracle.graal.python.runtime.PythonOptions;
163164
import com.oracle.graal.python.runtime.exception.PException;
164165
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -1053,6 +1054,31 @@ static void setCurrentException(Frame frame, Node inliningTarget, PException e,
10531054
}
10541055
}
10551056

1057+
@GenerateInline
1058+
@GenerateCached(false)
1059+
@GenerateUncached
1060+
public abstract static class ClearCurrentExceptionNode extends Node {
1061+
1062+
public abstract void execute(Node inliningTarget, PythonThreadState threadState);
1063+
1064+
public final PException getCurrentExceptionForReraise(Node inliningTarget, PythonThreadState threadState) {
1065+
PException exceptionForReraise = threadState.getCurrentExceptionForReraise();
1066+
execute(inliningTarget, threadState);
1067+
return exceptionForReraise;
1068+
}
1069+
1070+
@Specialization
1071+
static void doGeneric(Node inliningTarget, PythonThreadState threadState,
1072+
@Cached(inline = false) CStructAccess.WritePointerNode writePointerNode) {
1073+
threadState.clearCurrentException();
1074+
PThreadState nativeWrapper = threadState.getNativeWrapper();
1075+
if (nativeWrapper != null) {
1076+
Object nativeThreadState = PThreadState.getOrCreateNativeThreadState(threadState);
1077+
writePointerNode.write(nativeThreadState, CFields.PyThreadState__curexc_type, 0L);
1078+
}
1079+
}
1080+
}
1081+
10561082
@GenerateUncached
10571083
@GenerateCached
10581084
@GenerateInline(false)
@@ -1649,7 +1675,6 @@ public abstract static class CreateModuleNode extends MultiPhaseExtensionModuleI
16491675
static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, Object moduleDefWrapper, Object library,
16501676
@Bind("this") Node inliningTarget,
16511677
@Cached PythonObjectFactory factory,
1652-
@Cached InlinedConditionProfile errOccurredProfile,
16531678
@Cached CStructAccess.ReadPointerNode readPointer,
16541679
@Cached CStructAccess.ReadI64Node readI64,
16551680
@CachedLibrary(limit = "3") InteropLibrary interopLib,
@@ -1660,6 +1685,8 @@ static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, Object m
16601685
@Cached NativeToPythonTransferNode toJavaNode,
16611686
@Cached CStructAccess.ReadPointerNode readPointerNode,
16621687
@Cached CStructAccess.ReadI32Node readI32Node,
1688+
@Cached GetThreadStateNode getThreadStateNode,
1689+
@Cached TransformExceptionFromNativeNode transformExceptionFromNativeNode,
16631690
@Cached PRaiseNode.Lazy raiseNode) {
16641691
// call to type the pointer
16651692
Object moduleDef = moduleDefWrapper instanceof PythonAbstractNativeObject ? ((PythonAbstractNativeObject) moduleDefWrapper).getPtr() : moduleDefWrapper;
@@ -1723,8 +1750,9 @@ static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, Object m
17231750
} else {
17241751
result = interopLib.execute(createFunction, cArguments);
17251752
}
1726-
CheckFunctionResultNode.checkFunctionResult(inliningTarget, mName, interopLib.isNull(result), true, PythonLanguage.get(raiseNode), context, errOccurredProfile,
1727-
ErrorMessages.CREATION_FAILD_WITHOUT_EXCEPTION, ErrorMessages.CREATION_RAISED_EXCEPTION);
1753+
PythonThreadState threadState = getThreadStateNode.execute(inliningTarget);
1754+
transformExceptionFromNativeNode.execute(inliningTarget, threadState, mName, interopLib.isNull(result), true, ErrorMessages.CREATION_FAILD_WITHOUT_EXCEPTION,
1755+
ErrorMessages.CREATION_RAISED_EXCEPTION);
17281756
module = toJavaNode.execute(result);
17291757
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
17301758
throw shouldNotReachHere(e);
@@ -1795,6 +1823,8 @@ static int doGeneric(CApiContext capiContext, PythonModule module, Object module
17951823
@Cached CStructAccess.ReadI32Node readI32Node,
17961824
@CachedLibrary(limit = "3") InteropLibrary interopLib,
17971825
@CachedLibrary(limit = "1") SignatureLibrary signatureLibrary,
1826+
@Cached GetThreadStateNode getThreadStateNode,
1827+
@Cached TransformExceptionFromNativeNode transformExceptionFromNativeNode,
17981828
@Cached PRaiseNode raiseNode) {
17991829
InteropLibrary U = InteropLibrary.getUncached();
18001830
// call to type the pointer
@@ -1845,8 +1875,9 @@ static int doGeneric(CApiContext capiContext, PythonModule module, Object module
18451875
* and won't ignore this if no error is set. This is then the same
18461876
* behaviour if we would have a pointer return type and got 'NULL'.
18471877
*/
1848-
CheckFunctionResultNode.checkFunctionResult(inliningTarget, mName, iResult != 0, true, PythonLanguage.get(raiseNode), context,
1849-
InlinedConditionProfile.getUncached(), ErrorMessages.EXECUTION_FAILED_WITHOUT_EXCEPTION, ErrorMessages.EXECUTION_RAISED_EXCEPTION);
1878+
PythonThreadState threadState = getThreadStateNode.execute(inliningTarget);
1879+
transformExceptionFromNativeNode.execute(inliningTarget, threadState, mName, iResult != 0, true, ErrorMessages.EXECUTION_FAILED_WITHOUT_EXCEPTION,
1880+
ErrorMessages.EXECUTION_RAISED_EXCEPTION);
18501881
break;
18511882
default:
18521883
throw raiseNode.raise(SystemError, ErrorMessages.MODULE_INITIALIZED_WITH_UNKNOWN_SLOT, mName, slotId);

0 commit comments

Comments
 (0)