Skip to content

Commit 83775d3

Browse files
committed
Fix propagating exception from memoryview constructor on native objects
1 parent 3088273 commit 83775d3

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbols.FUN_ADD_NATIVE_SLOTS;
2929
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbols.FUN_PY_OBJECT_NEW;
30+
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbols.FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT;
3031
import static com.oracle.graal.python.builtins.objects.range.RangeUtils.canBeInt;
3132
import static com.oracle.graal.python.builtins.objects.range.RangeUtils.canBePint;
3233
import static com.oracle.graal.python.builtins.objects.type.TypeBuiltins.TYPE_ITEMSIZE;
@@ -121,7 +122,6 @@
121122
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes;
122123
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PCallCapiFunction;
123124
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory;
124-
import com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbols;
125125
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
126126
import com.oracle.graal.python.builtins.objects.code.PCode;
127127
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
@@ -3321,10 +3321,10 @@ public boolean hasSetItem(Object object) {
33213321
@Builtin(name = MEMORYVIEW, minNumOfPositionalArgs = 2, parameterNames = {"$cls", "object"}, constructsClass = PythonBuiltinClassType.PMemoryView)
33223322
@GenerateNodeFactory
33233323
public abstract static class MemoryViewNode extends PythonBuiltinNode {
3324-
public abstract PMemoryView execute(Object cls, Object object);
3324+
public abstract PMemoryView execute(VirtualFrame frame, Object cls, Object object);
33253325

3326-
public final PMemoryView execute(Object object) {
3327-
return execute(PythonBuiltinClassType.PMemoryView, object);
3326+
public final PMemoryView execute(VirtualFrame frame, Object object) {
3327+
return execute(frame, PythonBuiltinClassType.PMemoryView, object);
33283328
}
33293329

33303330
// TODO arrays should support buffer protocol too, but their implementation would be
@@ -3358,11 +3358,20 @@ PMemoryView fromMemoryView(@SuppressWarnings("unused") Object cls, PMemoryView o
33583358
}
33593359

33603360
@Specialization
3361-
static PMemoryView fromNative(@SuppressWarnings("unused") Object cls, PythonAbstractNativeObject object,
3361+
PMemoryView fromNative(VirtualFrame frame, @SuppressWarnings("unused") Object cls, PythonAbstractNativeObject object,
3362+
@Cached ForeignCallContext foreignCallContext,
33623363
@Cached CExtNodes.ToSulongNode toSulongNode,
33633364
@Cached CExtNodes.AsPythonObjectNode asPythonObjectNode,
3364-
@Cached PCallCapiFunction callCapiFunction) {
3365-
return (PMemoryView) asPythonObjectNode.execute(callCapiFunction.call(NativeCAPISymbols.FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT, toSulongNode.execute(object)));
3365+
@Cached PCallCapiFunction callCapiFunction,
3366+
@Cached PythonCextBuiltins.DefaultCheckFunctionResultNode checkFunctionResultNode) {
3367+
Object state = foreignCallContext.enter(frame, getContext(), this);
3368+
try {
3369+
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT, toSulongNode.execute(object));
3370+
checkFunctionResultNode.execute(FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT, result);
3371+
return (PMemoryView) asPythonObjectNode.execute(result);
3372+
} finally {
3373+
foreignCallContext.exit(frame, getContext(), state);
3374+
}
33663375
}
33673376

33683377
@Fallback

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ Object wrap(VirtualFrame frame, Object object,
15541554
@Cached BuiltinConstructors.MemoryViewNode memoryViewNode,
15551555
@Cached GetNativeNullNode getNativeNullNode) {
15561556
try {
1557-
return memoryViewNode.execute(object);
1557+
return memoryViewNode.execute(frame, object);
15581558
} catch (PException e) {
15591559
transformToNative(frame, e);
15601560
return getNativeNullNode.execute();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/MemoryViewBuiltins.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
9191
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
9292
import com.oracle.graal.python.runtime.AsyncHandler;
93-
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
93+
import com.oracle.graal.python.runtime.ExecutionContext;
9494
import com.oracle.graal.python.runtime.PythonContext;
9595
import com.oracle.graal.python.runtime.PythonCore;
9696
import com.oracle.graal.python.runtime.exception.PException;
@@ -254,7 +254,7 @@ Object setitem(VirtualFrame frame, PMemoryView self, PSlice slice, Object object
254254
if (self.getDimensions() != 1) {
255255
throw raise(NotImplementedError, ErrorMessages.MEMORYVIEW_SLICE_ASSIGNMENT_RESTRICTED_TO_DIM_1);
256256
}
257-
PMemoryView srcView = createMemoryView.execute(object);
257+
PMemoryView srcView = createMemoryView.execute(frame, object);
258258
PMemoryView destView = (PMemoryView) getItemNode.execute(frame, self, slice);
259259
if (srcView.getDimensions() != destView.getDimensions() || srcView.getBufferShape()[0] != destView.getBufferShape()[0] || srcView.getFormat() != destView.getFormat()) {
260260
throw raise(ValueError, ErrorMessages.MEMORYVIEW_DIFFERENT_STRUCTURES);
@@ -341,7 +341,7 @@ Object eq(VirtualFrame frame, PMemoryView self, Object other,
341341
@Cached MemoryViewNodes.ReadItemAtNode readOther) {
342342
PMemoryView memoryView;
343343
try {
344-
memoryView = memoryViewNode.execute(other);
344+
memoryView = memoryViewNode.execute(frame, other);
345345
} catch (PException e) {
346346
return PNotImplemented.NOT_IMPLEMENTED;
347347
}
@@ -762,15 +762,16 @@ Object releaseManaged(PMemoryView self,
762762

763763
@Specialization(guards = {"self.getReference() != null", "self.getManagedBuffer().isForNative()"})
764764
Object releaseNative(VirtualFrame frame, PMemoryView self,
765+
@Cached ExecutionContext.ForeignCallContext foreignCallContext,
765766
@Cached CExtNodes.PCallCapiFunction callRelease) {
766767
checkExports(self);
767768
if (checkShouldReleaseBuffer(self)) {
768-
Object state = IndirectCallContext.enter(frame, getContext(), this);
769+
Object state = foreignCallContext.enter(frame, getContext(), this);
769770
ManagedBuffer buffer = self.getManagedBuffer();
770771
try {
771772
callRelease.call(NativeCAPISymbols.FUN_PY_TRUFFLE_RELEASE_BUFFER, buffer.getBufferStructPointer());
772773
} finally {
773-
IndirectCallContext.exit(frame, getContext(), state);
774+
foreignCallContext.exit(frame, getContext(), state);
774775
}
775776
}
776777
self.setReleased();

0 commit comments

Comments
 (0)