Skip to content

Commit 9d1f837

Browse files
committed
[GR-9858] Add scripts for cross-compiling numpy and add various missing pieces
PullRequest: graalpython-open/50
2 parents 9998dee + f6e02b9 commit 9d1f837

File tree

32 files changed

+1166
-147
lines changed

32 files changed

+1166
-147
lines changed

graalpython/com.oracle.graal.python.cext/include/Python.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,6 @@
145145
#undef Py_RETURN_NONE
146146
#define Py_RETURN_NONE return Py_None;
147147

148-
#define PyMem_RawMalloc(size) malloc(size)
149-
#define PyMem_RawCalloc(nelem, elsize) calloc(nelem, elsize)
150-
#define PyMem_RawRealloc(ptr, new_size) realloc(ptr, new_size)
151-
#define PyMem_RawFree(ptr) free(ptr)
152-
153148
#define _PyLong_FromTime_t(o) ((long)o)
154149

155150
extern int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const char *format, char** kwdnames, int outc, void *v0, void *v1, void *v2, void *v3, void *v4, void *v5, void *v6, void *v7, void *v8, void *v9, void *v10, void *v11, void *v12, void *v13, void *v14, void *v15, void *v16, void *v17, void *v18, void *v19);

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ static void initialize_globals() {
5555
void *jnotimpl = polyglot_as__object(to_sulong(polyglot_get_member(PY_BUILTIN, "NotImplemented")));
5656
truffle_assign_managed(&_Py_NotImplementedStruct, jnotimpl);
5757

58+
// Ellipsis
59+
void *jellipsis = polyglot_as__object(to_sulong(polyglot_invoke(PY_TRUFFLE_CEXT, "Py_Ellipsis")));
60+
truffle_assign_managed(&_Py_EllipsisObject, jellipsis);
61+
5862
// True, False
5963
void *jtrue = polyglot_invoke(PY_TRUFFLE_CEXT, "Py_True");
6064
truffle_assign_managed(&_Py_TrueStruct, polyglot_as__longobject(to_sulong(jtrue)));
@@ -89,6 +93,10 @@ static void initialize_capi() {
8993
initialize_type_structure(&_PyNotImplemented_Type, "NotImplementedType");
9094
initialize_type_structure(&PyCapsule_Type, "PyCapsule");
9195
initialize_type_structure(&PyMemoryView_Type, "memoryview");
96+
initialize_type_structure(&PyCFunction_Type, "function");
97+
initialize_type_structure(&PyFrozenSet_Type, "frozenset");
98+
initialize_type_structure(&PySet_Type, "set");
99+
initialize_type_structure(&PyEllipsis_Type, "ellipsis");
92100

93101
// initialize global variables like '_Py_NoneStruct', etc.
94102
initialize_globals();
@@ -98,10 +106,12 @@ static void initialize_capi() {
98106
}
99107

100108
void* native_to_java(PyObject* obj) {
101-
if (obj == Py_None) {
109+
if (obj == Py_None) {
102110
return Py_None;
103111
} else if (obj == NULL) {
104112
return Py_NoValue;
113+
} else if (polyglot_is_string(obj)) {
114+
return obj;
105115
} else if (truffle_is_handle_to_managed(obj)) {
106116
return truffle_managed_from_handle(obj);
107117
} else if (truffle_is_handle_to_managed(obj->ob_refcnt)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ POLYGLOT_DECLARE_TYPE(PyBytesObject);
7474
POLYGLOT_DECLARE_STRUCT(_longobject);
7575
POLYGLOT_DECLARE_TYPE(PyCapsule);
7676
POLYGLOT_DECLARE_TYPE(PyMemoryViewObject);
77+
POLYGLOT_DECLARE_TYPE(PySetObject);
7778

7879

7980
extern void* to_java(PyObject* obj);

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,34 @@
4141
PyTypeObject PyCapsule_Type = PY_TRUFFLE_TYPE("PyCapsule", &PyType_Type, 0);
4242

4343
PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor) {
44-
return (PyObject *)polyglot_as_PyCapsule(to_sulong(polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule", polyglot_from_string(name, "ascii"), pointer, destructor)));
44+
return (PyObject *)polyglot_as_PyCapsule(to_sulong(polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule", name ? polyglot_from_string(name, "ascii") : to_java(Py_None), pointer, destructor)));
4545
}
4646

47-
void * PyCapsule_GetContext(PyObject *o) {
48-
void *result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule_GetContext", to_java(o));
49-
if (result == ERROR_MARKER) {
50-
return NULL;
51-
}
52-
return (void *)as_long(result);
47+
void* PyCapsule_GetContext(PyObject *o) {
48+
void *result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule_GetContext", to_java(o));
49+
if (result == ERROR_MARKER) {
50+
return NULL;
51+
}
52+
return (void *)as_long(result);
5353
}
5454

55-
void * PyCapsule_GetPointer(PyObject *o, const char *name) {
56-
void *result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule_GetPointer", to_java(o), polyglot_from_string(name, "ascii"));
57-
if (result == ERROR_MARKER) {
58-
return NULL;
59-
}
60-
return (void *)as_long(result);
55+
void* PyCapsule_GetPointer(PyObject *o, const char *name) {
56+
void *result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule_GetPointer", to_java(o), polyglot_from_string(name, "ascii"));
57+
if (result == ERROR_MARKER) {
58+
return NULL;
59+
}
60+
return (void *)as_long(result);
6161
}
6262

63+
void* PyCapsule_Import(const char *name, int no_block) {
64+
// TODO (tfel): no_block is currently ignored
65+
void *result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule_Import", polyglot_from_string(name, "ascii"), no_block);
66+
if (result == ERROR_MARKER) {
67+
return NULL;
68+
}
69+
return (void*)to_sulong(result);
70+
}
6371

72+
int PyCapsule_IsValid(PyObject *o, const char *name) {
73+
return o != NULL && polyglot_invoke(PY_TRUFFLE_CEXT, "PyCapsule_IsValid", to_java(o), polyglot_from_string(name, "ascii"));
74+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ PyObject * PyDict_GetItemString(PyObject *v, const char *key) {
103103
}
104104

105105
int PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) {
106-
truffle_invoke(to_java(v), "__setitem__", PyUnicode_FromString(key), to_java(item));
106+
truffle_invoke(to_java(v), "__setitem__", to_java(PyUnicode_FromString(key)), to_java(item));
107107
return 0;
108108
}
109109

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "capi.h"
4040

4141
void _PyErr_BadInternalCall(const char *filename, int lineno) {
42-
truffle_invoke(PY_TRUFFLE_CEXT, "_PyErr_BadInternalCall", truffle_read_string(filename), lineno);
42+
polyglot_invoke(PY_TRUFFLE_CEXT, "_PyErr_BadInternalCall", polyglot_from_string(filename, "utf-8"), lineno, Py_NoValue);
4343
}
4444

4545
#undef PyErr_BadInternalCall

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@
3939
#include "capi.h"
4040

4141
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
42+
43+
PyTypeObject PyCFunction_Type = PY_TRUFFLE_TYPE("builtin_function_or_method", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow) {
5252
return -1;
5353
}
5454
long result = truffle_invoke_l(PY_TRUFFLE_CEXT, "PyLong_AsPrimitive", to_java(obj), true, sizeof(long), truffle_read_string("long"));
55-
*overflow = result == -1L && PyErr_Occurred();
55+
*overflow = result == -1L && PyErr_Occurred() != NULL;
5656
return result;
5757
}
5858

@@ -93,8 +93,13 @@ PyObject * PyLong_FromVoidPtr(void *p) {
9393
void * PyLong_AsVoidPtr(PyObject *obj){
9494
return (void *)PyLong_AsSsize_t(obj);
9595
}
96+
9697
PyObject * PyLong_FromLong(long n) {
97-
return PyLong_FromUnsignedLongLong(n);
98+
void *result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyLong_FromLongLong", n, true);
99+
if (result == ERROR_MARKER) {
100+
return NULL;
101+
}
102+
return to_sulong(result);
98103
}
99104

100105
PyObject * PyLong_FromLongLong(long long n) {

0 commit comments

Comments
 (0)