Skip to content

Commit b2866c6

Browse files
committed
[GR-10431] Fixes for R-Python/Numpy interop.
PullRequest: graalpython/87
2 parents 14ec799 + 6ecf36f commit b2866c6

File tree

19 files changed

+313
-360
lines changed

19 files changed

+313
-360
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ POLYGLOT_DECLARE_TYPE(PyBufferDecorator);
9090

9191
extern void* to_java(PyObject* obj);
9292
extern void* to_java_type(PyTypeObject* cls);
93+
void* native_to_java(PyObject* obj);
9394
extern PyObject* to_sulong(void *o);
9495
extern PyObject* explicit_cast(PyObject* cobj);
9596
#define as_char_pointer(obj) polyglot_invoke(PY_TRUFFLE_CEXT, "to_char_pointer", to_java(obj))

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ PyObject* PyObject_CallObject(PyObject* callable, PyObject* args) {
538538
PyObject* PyObject_CallFunction(PyObject* callable, const char* fmt, ...) {
539539
PyObject* args;
540540
CALL_WITH_VARARGS(args, Py_BuildValue, 2, fmt);
541-
args = to_sulong(args);
542541
if (strlen(fmt) < 2) {
543542
PyObject* singleArg = args;
544543
args = PyTuple_New(strlen(fmt));

graalpython/com.oracle.graal.python.test/src/tests/cpyext/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def setUp(self):
6969

7070
def ccompile(self, name):
7171
from distutils.core import setup, Extension
72-
module = Extension(name, sources=['%s/%s.c' % (__dir__, name)])
72+
source_file = '%s/%s.c' % (__dir__, name)
73+
module = Extension(name, sources=[source_file])
7374
args = ['--quiet', 'build', 'install_lib', '-f', '--install-dir=%s' % __dir__]
7475
setup(
7576
script_name='setup',
@@ -79,6 +80,13 @@ def ccompile(self, name):
7980
description='',
8081
ext_modules=[module]
8182
)
83+
# ensure file was really written
84+
try:
85+
stat_result = os.stat(source_file)
86+
if stat_result[6] == 0:
87+
raise SystemError("empty source file %s" % (source_file,))
88+
except FileNotFoundError:
89+
raise SystemError("source file %s not available" % (source_file,))
8290

8391

8492
c_template = """
@@ -377,6 +385,7 @@ def create_module(self, name=None):
377385

378386

379387
class UnseenFormatter(Formatter):
388+
380389
def get_value(self, key, args, kwds):
381390
if isinstance(key, str):
382391
try:

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@
6060
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
6161
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
6262
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
63-
import com.oracle.graal.python.builtins.objects.cext.PythonClassNativeWrapper;
64-
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapper;
63+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
64+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
65+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
6566
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
6667
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
6768
import com.oracle.graal.python.builtins.objects.complex.PComplex;
@@ -231,9 +232,9 @@ long run(PFloat value) {
231232
}
232233

233234
@Specialization
234-
Object run(PythonObjectNativeWrapper value,
235+
Object run(PythonNativeWrapper value,
235236
@Cached("create()") AsLong recursive) {
236-
return recursive.executeWith(value.getPythonObject());
237+
return recursive.executeWith(value.getDelegate());
237238
}
238239

239240
private BuiltinConstructors.IntNode getIntNode() {
@@ -634,8 +635,8 @@ private Object checkFunctionResult(Object result) {
634635
abstract static class PNativeToPTypeNode extends PForeignToPTypeNode {
635636

636637
@Specialization
637-
protected static PythonAbstractObject fromNativeNone(PythonObjectNativeWrapper nativeWrapper) {
638-
return nativeWrapper.getPythonObject();
638+
protected static Object fromNativeNone(PythonNativeWrapper nativeWrapper) {
639+
return nativeWrapper.getDelegate();
639640
}
640641

641642
public static PNativeToPTypeNode create() {
@@ -1040,8 +1041,8 @@ abstract static class PyTruffle_GetTpFlags extends NativeBuiltin {
10401041
@Child private GetClassNode getClassNode;
10411042

10421043
@Specialization
1043-
long doPythonObject(PythonObjectNativeWrapper nativeWrapper) {
1044-
PythonClass pclass = getClassNode().execute(nativeWrapper.getPythonObject());
1044+
long doPythonObject(PythonNativeWrapper nativeWrapper) {
1045+
PythonClass pclass = getClassNode().execute(nativeWrapper.getDelegate());
10451046
return pclass.getFlags();
10461047
}
10471048

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects;
4040

41-
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapper;
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
4242
import com.oracle.graal.python.runtime.interop.PythonMessageResolutionForeign;
4343
import com.oracle.truffle.api.interop.ForeignAccess;
4444
import com.oracle.truffle.api.interop.TruffleObject;

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

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AsPythonObjectNodeGen;
4646
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToJavaNodeGen;
4747
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToSulongNodeGen;
48+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
49+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
50+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
51+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.TruffleObjectNativeWrapper;
4852
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4953
import com.oracle.graal.python.nodes.PBaseNode;
5054
import com.oracle.graal.python.nodes.PGuards;
@@ -77,42 +81,42 @@ public abstract static class ToSulongNode extends PBaseNode {
7781
* passed from Python into C code need to wrap Strings into PStrings.
7882
*/
7983
@Specialization
80-
Object run(String str) {
84+
Object doString(String str) {
8185
return PythonObjectNativeWrapper.wrap(factory().createString(str));
8286
}
8387

8488
@Specialization
85-
Object run(boolean b) {
89+
Object doBoolean(boolean b) {
8690
return PythonObjectNativeWrapper.wrap(factory().createInt(b));
8791
}
8892

8993
@Specialization
90-
Object run(int integer) {
91-
return PythonObjectNativeWrapper.wrap(factory().createInt(integer));
94+
Object doInteger(int i) {
95+
return PythonObjectNativeWrapper.wrap(factory().createInt(i));
9296
}
9397

9498
@Specialization
95-
Object run(long integer) {
96-
return PythonObjectNativeWrapper.wrap(factory().createInt(integer));
99+
Object doLong(long l) {
100+
return PythonObjectNativeWrapper.wrap(factory().createInt(l));
97101
}
98102

99103
@Specialization
100-
Object run(double number) {
101-
return PythonObjectNativeWrapper.wrap(factory().createFloat(number));
104+
Object doDouble(double d) {
105+
return PythonObjectNativeWrapper.wrap(factory().createFloat(d));
102106
}
103107

104108
@Specialization
105-
Object runNativeClass(PythonNativeClass object) {
106-
return object.object;
109+
Object doNativeClass(PythonNativeClass nativeClass) {
110+
return nativeClass.object;
107111
}
108112

109113
@Specialization
110-
Object runNativeObject(PythonNativeObject object) {
111-
return object.object;
114+
Object doNativeObject(PythonNativeObject nativeObject) {
115+
return nativeObject.object;
112116
}
113117

114118
@Specialization(guards = "!isNativeClass(object)")
115-
Object runNativeObject(PythonClass object) {
119+
Object doPythonClass(PythonClass object) {
116120
return PythonClassNativeWrapper.wrap(object);
117121
}
118122

@@ -122,6 +126,11 @@ Object runNativeObject(PythonAbstractObject object) {
122126
return PythonObjectNativeWrapper.wrap(object);
123127
}
124128

129+
@Specialization(guards = {"isForeignObject(object)"})
130+
Object doPythonClass(TruffleObject object) {
131+
return TruffleObjectNativeWrapper.wrap(object);
132+
}
133+
125134
@Fallback
126135
Object run(Object obj) {
127136
assert obj != null : "Java 'null' cannot be a Sulong value";
@@ -140,6 +149,10 @@ protected static boolean isNativeObject(PythonAbstractObject o) {
140149
return o instanceof PythonNativeObject;
141150
}
142151

152+
protected static boolean isForeignObject(Object o) {
153+
return !(o instanceof PythonAbstractObject);
154+
}
155+
143156
public static ToSulongNode create() {
144157
return ToSulongNodeGen.create();
145158
}
@@ -156,11 +169,11 @@ public abstract static class AsPythonObjectNode extends PBaseNode {
156169
@Child GetClassNode getClassNode;
157170

158171
@Specialization
159-
PythonAbstractObject doNativeWrapper(PythonObjectNativeWrapper object) {
160-
return object.getPythonObject();
172+
Object doNativeWrapper(PythonNativeWrapper object) {
173+
return object.getDelegate();
161174
}
162175

163-
@Specialization(guards = "isForeignObject(object)")
176+
@Specialization(guards = {"isForeignObject(object)", "!isNativeWrapper(object)"})
164177
PythonAbstractObject doNativeObject(TruffleObject object) {
165178
return factory().createNativeObjectWrapper(object);
166179
}
@@ -204,10 +217,14 @@ protected boolean isForeignObject(TruffleObject obj) {
204217
return getClassNode.execute(obj) == getCore().getForeignClass();
205218
}
206219

220+
protected boolean isNativeWrapper(Object obj) {
221+
return obj instanceof PythonNativeWrapper;
222+
}
223+
207224
@TruffleBoundary
208225
public static Object doSlowPath(Object object) {
209-
if (object instanceof PythonObjectNativeWrapper) {
210-
return ((PythonObjectNativeWrapper) object).getPythonObject();
226+
if (object instanceof PythonNativeWrapper) {
227+
return ((PythonNativeWrapper) object).getDelegate();
211228
} else if (GetClassNode.getItSlowPath(object) == PythonLanguage.getCore().getForeignClass()) {
212229
throw new AssertionError("Unsupported slow path operation: converting 'to_java(" + object + ")");
213230
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
import com.oracle.truffle.api.interop.TruffleObject;
4343

4444
/**
45-
* Unlike a {@link PythonObjectNativeWrapper} object that wraps a Python unicode object, this
46-
* wrapper let's a Java String look like a {@code char*}.
45+
* Unlike a
46+
* {@link com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper}
47+
* object that wraps a Python unicode object, this wrapper let's a Java String look like a
48+
* {@code char*}.
4749
*/
4850
public class CStringWrapper implements TruffleObject {
4951

0 commit comments

Comments
 (0)