Skip to content

Commit 3764fa4

Browse files
committed
Introduce record ExceptionState
1 parent 51f76eb commit 3764fa4

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiUnaryBuiltinNode;
8484
import com.oracle.graal.python.builtins.modules.cext.PythonCextFileBuiltins.PyFile_WriteObject;
8585
import com.oracle.graal.python.builtins.objects.PNone;
86+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ExceptionState;
8687
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PyErrFetchNode;
8788
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PyErrOccurredNode;
8889
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativeToPythonNode;
@@ -203,28 +204,26 @@ static Object doGeneric(Object pType, Object pValue, Object pTraceback,
203204
@Cached PythonToNativeNewRefNode toNativeNewRefNode,
204205
@Cached CStructAccess.WritePointerNode writePointerNode) {
205206
PythonContext.PythonThreadState threadState = getThreadStateNode.execute(inliningTarget);
206-
Object[] exceptionState = pyErrFetchNode.execute(inliningTarget, threadState);
207-
// null or [type, value, traceback]
208-
assert exceptionState == null || exceptionState.length == 3;
207+
ExceptionState exceptionState = pyErrFetchNode.execute(inliningTarget, threadState);
209208
if (exceptionState == null) {
210209
/*
211210
* This should be caught in native by checking 'PyErr_Occurred' and avoiding the
212211
* upcall. But let's be defensive and treat that case on a slow path.
213212
*/
214213
doNoException(pType, pValue, pTraceback);
215214
} else {
216-
assert exceptionState[0] != null;
217-
assert exceptionState[1] != null;
215+
assert exceptionState.type() != null;
216+
assert exceptionState.value() != null;
218217
/*
219218
* NOTE: We cannot use 'WriteObjectNewRefNode' because we are writing to out
220219
* variables (C type 'PyObject **out') where the previous value (i.e. '*out') of
221220
* those is unspecified. 'WriteObjectNewRefNode' would try to decref the previous
222221
* object and we MUST NOT do that. Therefore, we use the combination of
223222
* 'WritePointerNode' and 'PythonToNativeNewRefNode'.
224223
*/
225-
writePointerNode.write(pType, toNativeNewRefNode.execute(exceptionState[0]));
226-
writePointerNode.write(pValue, toNativeNewRefNode.execute(exceptionState[1]));
227-
writePointerNode.write(pTraceback, toNativeNewRefNode.execute(exceptionState[2] != null ? exceptionState[2] : PNone.NO_VALUE));
224+
writePointerNode.write(pType, toNativeNewRefNode.execute(exceptionState.type()));
225+
writePointerNode.write(pValue, toNativeNewRefNode.execute(exceptionState.value()));
226+
writePointerNode.write(pTraceback, toNativeNewRefNode.execute(exceptionState.traceback() != null ? exceptionState.traceback() : PNone.NO_VALUE));
228227
}
229228
return PNone.NO_VALUE;
230229
}
@@ -460,17 +459,15 @@ static Object raise(int set_sys_last_vars,
460459
if (err != nativeNull && IsBuiltinObjectProfile.profileObjectUncached(err, PythonBuiltinClassType.SystemExit)) {
461460
handleSystemExit(excInfoNode, getItemNode, isInstanceNode, restoreNode, (SysModuleBuiltins) sys.getBuiltins(), writeFileNode, exitNode);
462461
}
463-
Object[] fetched = PyErrFetchNode.executeUncached(threadState);
464-
// null or [type, value, traceback]
465-
assert fetched == null || fetched.length == 3;
462+
ExceptionState fetched = PyErrFetchNode.executeUncached(threadState);
466463
Object type = null;
467464
Object val = null;
468465
Object tb = null;
469466

470467
if (fetched != null) {
471-
type = fetched[0];
472-
val = fetched[1];
473-
tb = fetched[2];
468+
type = fetched.type();
469+
val = fetched.value();
470+
tb = fetched.traceback();
474471
}
475472
if (type == null || type == PNone.NONE) {
476473
return PNone.NONE;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,19 +1017,22 @@ static Object doGeneric(Node inliningTarget, PythonThreadState threadState,
10171017
}
10181018
}
10191019

1020+
public record ExceptionState(Object type, Object value, Object traceback) {
1021+
}
1022+
10201023
@GenerateInline
10211024
@GenerateCached(false)
10221025
@GenerateUncached
10231026
public abstract static class PyErrFetchNode extends Node {
10241027

1025-
public static Object[] executeUncached(PythonThreadState threadState) {
1028+
public static ExceptionState executeUncached(PythonThreadState threadState) {
10261029
return PyErrFetchNodeGen.getUncached().execute(null, threadState);
10271030
}
10281031

1029-
public abstract Object[] execute(Node inliningTarget, PythonThreadState threadState);
1032+
public abstract ExceptionState execute(Node inliningTarget, PythonThreadState threadState);
10301033

10311034
@Specialization
1032-
static Object[] doGeneric(Node inliningTarget, PythonThreadState threadState,
1035+
static ExceptionState doGeneric(Node inliningTarget, PythonThreadState threadState,
10331036
@Cached GetClassNode getClassNode,
10341037
@Cached MaterializeLazyTracebackNode materializeTraceback,
10351038
@Cached ClearCurrentExceptionNode clearCurrentExceptionNode) {
@@ -1047,7 +1050,7 @@ static Object[] doGeneric(Node inliningTarget, PythonThreadState threadState,
10471050
traceback = materializeTraceback.execute(inliningTarget, threadState.getCurrentTraceback());
10481051
}
10491052
clearCurrentExceptionNode.execute(inliningTarget, threadState);
1050-
return new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback};
1053+
return new ExceptionState(getClassNode.execute(inliningTarget, exception), exception, traceback);
10511054
}
10521055
}
10531056

0 commit comments

Comments
 (0)