Skip to content

Commit 417f1a8

Browse files
committed
Use enum for error result
1 parent b8f29f0 commit 417f1a8

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FastCallWithKeywordsArgsToSulongNode;
122122
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FromCharPointerNode;
123123
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode;
124+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.MayRaiseErrorResult;
124125
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.MayRaiseNode;
125126
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ObjectUpcallNode;
126127
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PCallCapiFunction;
@@ -2424,18 +2425,18 @@ static Object doDirect(VirtualFrame frame, @SuppressWarnings("unused") PythonMod
24242425
abstract static class MakeMayRaiseWrapperNode extends PythonBuiltinNode {
24252426
private static final WeakHashMap<RootCallTarget, WeakReference<RootCallTarget>> weakCallTargetMap = new WeakHashMap<>();
24262427

2427-
private static final RootCallTarget createWrapperCt(PFunction func, Object errorResult) {
2428+
private static RootCallTarget createWrapperCt(PFunction func, MayRaiseErrorResult errorResult) {
24282429
CompilerDirectives.transferToInterpreter();
2429-
assert errorResult instanceof Integer || errorResult instanceof Long || errorResult instanceof Double || errorResult == PNone.NONE ||
2430-
InteropLibrary.getUncached().isNull(errorResult) : "invalid wrap";
24312430
PythonLanguage lang = PythonLanguage.getCurrent();
24322431
RootNode rootNode = new MayRaiseNode(lang, func.getSignature(), func.getCallTarget(), errorResult);
24332432
return PythonUtils.getOrCreateCallTarget(rootNode);
24342433
}
24352434

24362435
@Specialization
24372436
@TruffleBoundary
2438-
Object make(PFunction func, Object errorResult) {
2437+
Object make(PFunction func, Object errorResultObj) {
2438+
MayRaiseErrorResult errorResult = convertToEnum(errorResultObj);
2439+
24392440
RootCallTarget wrappedCt = func.getCallTarget();
24402441
WeakReference<RootCallTarget> wrapperCtRef = weakCallTargetMap.get(wrappedCt);
24412442
RootCallTarget wrapperCt = null;
@@ -2457,6 +2458,25 @@ Object make(PFunction func, Object errorResult) {
24572458
wrapperCode, func.getGlobals(), func.getDefaults(), func.getKwDefaults(),
24582459
func.getClosure(), func.getCodeStableAssumption(), func.getDefaultsStableAssumption());
24592460
}
2461+
2462+
private MayRaiseErrorResult convertToEnum(Object object) {
2463+
if (PGuards.isNone(object) ) {
2464+
return MayRaiseErrorResult.NONE;
2465+
} else if (object instanceof Integer) {
2466+
int i = (int) object;
2467+
if (i == -1) {
2468+
return MayRaiseErrorResult.INT;
2469+
}
2470+
} else if (object instanceof Double) {
2471+
double i = (double) object;
2472+
if (i == -1.0) {
2473+
return MayRaiseErrorResult.FLOAT;
2474+
}
2475+
} else if (object instanceof PythonNativeNull) {
2476+
return MayRaiseErrorResult.NATIVE_NULL;
2477+
}
2478+
throw raise(PythonErrorType.TypeError, "invalid error result value");
2479+
}
24602480
}
24612481

24622482
@Builtin(name = "to_long", minNumOfPositionalArgs = 1)

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.DirectUpcallNodeGen;
7474
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.FastCallArgsToSulongNodeGen;
7575
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.FastCallWithKeywordsArgsToSulongNodeGen;
76+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.GetNativeNullNodeGen;
7677
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.GetTypeMemberNodeGen;
7778
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.IsPointerNodeGen;
7879
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.ObjectUpcallNodeGen;
@@ -139,6 +140,7 @@
139140
import com.oracle.truffle.api.Assumption;
140141
import com.oracle.truffle.api.CompilerAsserts;
141142
import com.oracle.truffle.api.CompilerDirectives;
143+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
142144
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
143145
import com.oracle.truffle.api.RootCallTarget;
144146
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
@@ -2473,18 +2475,24 @@ public static PCallCapiFunction getUncached() {
24732475
return CExtNodesFactory.PCallCapiFunctionNodeGen.getUncached();
24742476
}
24752477
}
2478+
2479+
public enum MayRaiseErrorResult {
2480+
NATIVE_NULL, NONE, INT, FLOAT
2481+
}
24762482

24772483
// -----------------------------------------------------------------------------------------------------------------
24782484
@Builtin(takesVarArgs = true)
24792485
public static class MayRaiseNode extends PRootNode {
24802486
@Child private CallTargetInvokeNode callTargetInvokeNode;
24812487
@Child private TransformExceptionToNativeNode transformExceptionToNativeNode;
24822488
@Child private CalleeContext calleeContext;
2489+
2490+
@Child private GetNativeNullNode getNativeNullNode;
24832491

24842492
private final Signature signature;
2485-
private final Object errorResult;
2493+
private final MayRaiseErrorResult errorResult;
24862494

2487-
public MayRaiseNode(PythonLanguage lang, Signature sign, RootCallTarget ct, Object errorResult) {
2495+
public MayRaiseNode(PythonLanguage lang, Signature sign, RootCallTarget ct, MayRaiseErrorResult errorResult) {
24882496
super(lang);
24892497
this.signature = sign;
24902498
this.callTargetInvokeNode = CallTargetInvokeNode.create(ct, false, false);
@@ -2508,7 +2516,7 @@ public final Object execute(VirtualFrame frame) {
25082516
} catch (PException e) {
25092517
// transformExceptionToNativeNode acts as a branch profile
25102518
ensureTransformExceptionToNativeNode().execute(frame, e);
2511-
return errorResult;
2519+
return getErrorResult();
25122520
} finally {
25132521
calleeContext.exit(frame, this);
25142522
}
@@ -2521,6 +2529,24 @@ private TransformExceptionToNativeNode ensureTransformExceptionToNativeNode() {
25212529
}
25222530
return transformExceptionToNativeNode;
25232531
}
2532+
2533+
private Object getErrorResult() {
2534+
switch(errorResult) {
2535+
case INT:
2536+
return -1;
2537+
case FLOAT:
2538+
return -1.0;
2539+
case NONE:
2540+
return PNone.NONE;
2541+
case NATIVE_NULL:
2542+
if (getNativeNullNode == null) {
2543+
CompilerDirectives.transferToInterpreterAndInvalidate();
2544+
getNativeNullNode = insert(GetNativeNullNodeGen.create());
2545+
}
2546+
return getNativeNullNode.execute();
2547+
}
2548+
throw CompilerDirectives.shouldNotReachHere();
2549+
}
25242550

25252551
@Override
25262552
public Signature getSignature() {

0 commit comments

Comments
 (0)