Skip to content

Commit 1492952

Browse files
committed
Allow native wrapper in 'PyTruffle_Set_Ptr'.
1 parent 7acb656 commit 1492952

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@
4141

4242
MUST_INLINE static void force_to_native(void* obj) {
4343
if (polyglot_is_value(obj)) {
44-
void* handle = truffle_deref_handle_for_managed(obj);
45-
if(!polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", obj, handle)) {
46-
truffle_release_handle(handle);
47-
}
44+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", obj, truffle_deref_handle_for_managed(obj));
4845
}
4946
}
5047

@@ -95,9 +92,9 @@ static void initialize_globals() {
9592
}
9693

9794
static void initialize_bufferprocs() {
98-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", to_java((PyObject*)&PyBytes_Type), (getbufferproc)bytes_buffer_getbuffer, (releasebufferproc)NULL);
99-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", to_java((PyObject*)&PyByteArray_Type), (getbufferproc)NULL, (releasebufferproc)NULL);
100-
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", to_java((PyObject*)&PyBuffer_Type), (getbufferproc)bufferdecorator_getbuffer, (releasebufferproc)NULL);
95+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java((PyObject*)&PyBytes_Type), (getbufferproc)bytes_buffer_getbuffer, (releasebufferproc)NULL);
96+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java((PyObject*)&PyByteArray_Type), (getbufferproc)NULL, (releasebufferproc)NULL);
97+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_SetBufferProcs", native_to_java((PyObject*)&PyBuffer_Type), (getbufferproc)bufferdecorator_getbuffer, (releasebufferproc)NULL);
10198
}
10299

103100
__attribute__((constructor))

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,17 @@ private GetClassNode getClassNode() {
11991199
abstract static class PyTruffle_Set_Ptr extends NativeBuiltin {
12001200

12011201
@Specialization
1202-
int doPythonObject(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1203-
nativeWrapper.setNativePointer(null);
1204-
nativeWrapper.setNativePointer(ptr);
1202+
int doPythonObject(PythonAbstractObject nativeWrapper, TruffleObject ptr) {
1203+
return doNativeWrapper(nativeWrapper.getNativeWrapper(), ptr);
1204+
}
1205+
1206+
@Specialization
1207+
int doNativeWrapper(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1208+
if (nativeWrapper.isNative()) {
1209+
PythonContext.getSingleNativeContextAssumption().invalidate();
1210+
} else {
1211+
nativeWrapper.setNativePointer(ptr);
1212+
}
12051213
return 0;
12061214
}
12071215
}
@@ -1211,12 +1219,16 @@ int doPythonObject(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
12111219
abstract static class PyTruffle_SetBufferProcs extends NativeBuiltin {
12121220

12131221
@Specialization
1214-
Object doPythonObject(PythonClass obj, Object getBufferProc, Object releaseBufferProc) {
1215-
PythonClassNativeWrapper nativeWrapper = obj.getNativeWrapper();
1222+
Object doNativeWrapper(PythonClassNativeWrapper nativeWrapper, Object getBufferProc, Object releaseBufferProc) {
12161223
nativeWrapper.setGetBufferProc(getBufferProc);
12171224
nativeWrapper.setReleaseBufferProc(releaseBufferProc);
12181225
return PNone.NO_VALUE;
12191226
}
1227+
1228+
@Specialization
1229+
Object doPythonObject(PythonClass obj, Object getBufferProc, Object releaseBufferProc) {
1230+
return doNativeWrapper(obj.getNativeWrapper(), getBufferProc, releaseBufferProc);
1231+
}
12201232
}
12211233

12221234
@Builtin(name = "PyTruffle_ThreadState_GetDict", fixedNumOfArguments = 0)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7676
import com.oracle.graal.python.nodes.object.GetClassNode;
7777
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
78+
import com.oracle.graal.python.runtime.PythonContext;
7879
import com.oracle.graal.python.runtime.interop.PythonMessageResolution;
7980
import com.oracle.graal.python.runtime.sequence.PSequence;
8081
import com.oracle.truffle.api.Assumption;
@@ -601,7 +602,7 @@ long doSlow(PythonNativeWrapper obj,
601602
}
602603

603604
protected Assumption getSingleNativeContextAssumption() {
604-
return getContext().getSingleNativeContextAssumption();
605+
return PythonContext.getSingleNativeContextAssumption();
605606
}
606607

607608
public static PAsPointerNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class PythonContext {
6969
private OutputStream out;
7070
private OutputStream err;
7171
@CompilationFinal private boolean capiWasLoaded = false;
72-
private final Assumption singleNativeContext = Truffle.getRuntime().createAssumption("single native context assumption");
72+
private final static Assumption singleNativeContext = Truffle.getRuntime().createAssumption("single native context assumption");
7373

7474
@CompilationFinal private HashingStorage.Equivalence slowPathEquivalence;
7575

@@ -222,7 +222,7 @@ public void initializeMainModule(String path) {
222222
}
223223
}
224224

225-
public Assumption getSingleNativeContextAssumption() {
225+
public static Assumption getSingleNativeContextAssumption() {
226226
return singleNativeContext;
227227
}
228228
}

0 commit comments

Comments
 (0)