Skip to content

Commit 41887ed

Browse files
committed
[GR-10719] Enable kmeans example
PullRequest: graalpython/138
2 parents 3e75e2e + 8eb8ab5 commit 41887ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1043
-243
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@
117117
#include "traceback.h"
118118
#include "classobject.h"
119119
#include "pythread.h"
120+
#include "funcobject.h"
120121
#include "iterobject.h"
122+
#include "datetime.h"
121123

122124
#define PY_TRUFFLE_CEXT ((void*)polyglot_import("python_cext"))
123125
#define PY_BUILTIN ((void*)polyglot_import("python_builtins"))
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* Copyright (c) 2018, Oracle and/or its affiliates.
2+
* Copyright (C) 1996-2017 Python Software Foundation
3+
*
4+
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
*/
6+
/* Function object interface */
7+
#ifndef Py_LIMITED_API
8+
#ifndef Py_FUNCOBJECT_H
9+
#define Py_FUNCOBJECT_H
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
/* Function objects and code objects should not be confused with each other:
15+
*
16+
* Function objects are created by the execution of the 'def' statement.
17+
* They reference a code object in their __code__ attribute, which is a
18+
* purely syntactic object, i.e. nothing more than a compiled version of some
19+
* source code lines. There is one code object per source code "fragment",
20+
* but each code object can be referenced by zero or many function objects
21+
* depending only on how many times the 'def' statement in the source was
22+
* executed so far.
23+
*/
24+
25+
typedef struct {
26+
PyObject_HEAD
27+
PyObject *func_code; /* A code object, the __code__ attribute */
28+
PyObject *func_globals; /* A dictionary (other mappings won't do) */
29+
PyObject *func_defaults; /* NULL or a tuple */
30+
PyObject *func_kwdefaults; /* NULL or a dict */
31+
PyObject *func_closure; /* NULL or a tuple of cell objects */
32+
PyObject *func_doc; /* The __doc__ attribute, can be anything */
33+
PyObject *func_name; /* The __name__ attribute, a string object */
34+
PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */
35+
PyObject *func_weakreflist; /* List of weak references */
36+
PyObject *func_module; /* The __module__ attribute, can be anything */
37+
PyObject *func_annotations; /* Annotations, a dict or NULL */
38+
PyObject *func_qualname; /* The qualified name */
39+
40+
/* Invariant:
41+
* func_closure contains the bindings for func_code->co_freevars, so
42+
* PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
43+
* (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
44+
*/
45+
} PyFunctionObject;
46+
47+
PyAPI_DATA(PyTypeObject) PyFunction_Type;
48+
49+
#define PyFunction_Check(op) (Py_TYPE(op) == &PyFunction_Type)
50+
51+
PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
52+
PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
53+
PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
54+
PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
55+
PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
56+
PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
57+
PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
58+
PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
59+
PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
60+
PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
61+
PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
62+
PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
63+
PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
64+
65+
#ifndef Py_LIMITED_API
66+
PyAPI_FUNC(PyObject *) _PyFunction_FastCallDict(
67+
PyObject *func,
68+
PyObject **args,
69+
Py_ssize_t nargs,
70+
PyObject *kwargs);
71+
72+
PyAPI_FUNC(PyObject *) _PyFunction_FastCallKeywords(
73+
PyObject *func,
74+
PyObject **stack,
75+
Py_ssize_t nargs,
76+
PyObject *kwnames);
77+
#endif
78+
79+
/* Macros for direct access to these values. Type checks are *not*
80+
done, so use with care. */
81+
#define PyFunction_GET_CODE(func) \
82+
(((PyFunctionObject *)func) -> func_code)
83+
#define PyFunction_GET_GLOBALS(func) \
84+
(((PyFunctionObject *)func) -> func_globals)
85+
#define PyFunction_GET_MODULE(func) \
86+
(((PyFunctionObject *)func) -> func_module)
87+
#define PyFunction_GET_DEFAULTS(func) \
88+
(((PyFunctionObject *)func) -> func_defaults)
89+
#define PyFunction_GET_KW_DEFAULTS(func) \
90+
(((PyFunctionObject *)func) -> func_kwdefaults)
91+
#define PyFunction_GET_CLOSURE(func) \
92+
(((PyFunctionObject *)func) -> func_closure)
93+
#define PyFunction_GET_ANNOTATIONS(func) \
94+
(((PyFunctionObject *)func) -> func_annotations)
95+
96+
/* The classmethod and staticmethod types lives here, too */
97+
PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
98+
PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
99+
100+
PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
101+
PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
102+
103+
#ifdef __cplusplus
104+
}
105+
#endif
106+
#endif /* !Py_FUNCOBJECT_H */
107+
#endif /* Py_LIMITED_API */

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
#include "capi.h"
4242

4343
typedef enum e_binop {
44-
ADD=0, SUB, MUL, TRUEDIV, LSHIFT, RSHIFT, OR, AND, XOR, FLOORDIV, MOD
44+
ADD=0, SUB, MUL, TRUEDIV, LSHIFT, RSHIFT, OR, AND, XOR, FLOORDIV, MOD,
45+
INPLACE_OFFSET,
4546
} BinOp;
4647

4748
typedef enum e_unaryop {
@@ -71,6 +72,10 @@ static PyObject * do_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_na
7172
return UPCALL_CEXT_O("PyNumber_BinOp", native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
7273
}
7374

75+
static PyObject * do_inplace_binop(PyObject *v, PyObject *w, BinOp binop, char *binop_name) {
76+
return UPCALL_CEXT_O("PyNumber_InPlaceBinOp", native_to_java(v), native_to_java(w), binop, polyglot_from_string(binop_name, SRC_CS));
77+
}
78+
7479
PyObject * PyNumber_Add(PyObject *o1, PyObject *o2) {
7580
return do_binop(o1, o2, ADD, "+");
7681
}
@@ -134,6 +139,10 @@ PyObject * PyNumber_Index(PyObject *o) {
134139
return UPCALL_CEXT_O("PyNumber_Index", native_to_java(o));
135140
}
136141

142+
PyObject * PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2) {
143+
return do_inplace_binop(o1, o2, TRUEDIV, "/");
144+
}
145+
137146
Py_ssize_t PyNumber_AsSsize_t(PyObject *item, PyObject *err) {
138147
Py_ssize_t result;
139148
PyObject *runerr;
@@ -208,6 +217,10 @@ Py_ssize_t PySequence_Size(PyObject *s) {
208217
return UPCALL_CEXT_L("PyObject_Size", native_to_java(s));
209218
}
210219

220+
int PySequence_Contains(PyObject *seq, PyObject *obj) {
221+
return UPCALL_CEXT_I("PySequence_Contains", native_to_java(seq), native_to_java(obj));
222+
}
223+
211224
// taken from CPython "Objects/abstract.c"
212225
#undef PySequence_Length
213226
Py_ssize_t PySequence_Length(PyObject *s) {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ declare_type(PySet_Type, set, PySetObject);
9797
declare_type(PyFloat_Type, float, PyFloatObject);
9898
declare_type(PySlice_Type, slice, PySliceObject);
9999
declare_type(PyByteArray_Type, bytearray, PyByteArrayObject);
100-
declare_type(PyCFunction_Type, function, PyCFunctionObject);
100+
declare_type(PyCFunction_Type, builtin_function_or_method, PyCFunctionObject);
101+
declare_type(PyMethodDescr_Type, method_descriptor, PyMethodDescrObject);
102+
declare_type(PyGetSetDescr_Type, getset_descriptor, PyGetSetDescrObject);
103+
declare_type(PyWrapperDescr_Type, wrapper_descriptor, PyWrapperDescrObject);
104+
declare_type(PyMemberDescr_Type, member_descriptor, PyMemberDescrObject);
101105
declare_type(_PyExc_BaseException, BaseException, PyBaseExceptionObject);
102106
declare_type(PyBuffer_Type, buffer, PyBufferDecorator);
107+
declare_type(PyFunction_Type, function, PyFunctionObject);
103108
declare_type(PyMethod_Type, method, PyMethodObject);
104109
declare_type(PyCode_Type, code, PyCodeObject);
105110
declare_type(PyFrame_Type, frame, PyFrameObject);
@@ -592,3 +597,7 @@ void* wrap_fastcall(_PyCFunctionFast fun, PyObject *self, PyObject **args, PyObj
592597
void* wrap_unsupported(void *fun, ...) {
593598
return NULL;
594599
}
600+
601+
int truffle_ptr_compare(void* x, void* y) {
602+
return x == y;
603+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
#include "capi.h"
42+
43+
POLYGLOT_DECLARE_TYPE(PyDateTime_CAPI);
44+
45+
/** to be used from Java code only; returns the type ID for a PyDateTime_CAPI */
46+
extern PyObject* set_PyDateTime_CAPI_typeid(PyTypeObject* type) {
47+
polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_SulongType", type, polyglot_PyDateTime_CAPI_typeid());
48+
return Py_True;
49+
}
50+

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ typedef struct {
4646
PyObject *mapping;
4747
} mappingproxyobject;
4848

49+
PyTypeObject PyGetSetDescr_Type = PY_TRUFFLE_TYPE("getset_descriptor", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyGetSetDescrObject));;
50+
PyTypeObject PyWrapperDescr_Type = PY_TRUFFLE_TYPE("wrapper_descriptor", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyWrapperDescrObject));;
51+
PyTypeObject PyMemberDescr_Type = PY_TRUFFLE_TYPE("member_descriptor", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyMemberDescrObject));;
52+
PyTypeObject PyMethodDescr_Type = PY_TRUFFLE_TYPE("method_descriptor", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyMethodDescrObject));;
4953
PyTypeObject PyDictProxy_Type = PY_TRUFFLE_TYPE("mappingproxy", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(mappingproxyobject));
5054

5155
POLYGLOT_DECLARE_TYPE(mappingproxyobject);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
#include "capi.h"
4242

43+
PyTypeObject PyFunction_Type = PY_TRUFFLE_TYPE("function", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyFunctionObject));
44+
4345
PyObject* PyClassMethod_New(PyObject* method) {
4446
return UPCALL_O(PY_BUILTIN, "classmethod", native_to_java(method));
4547
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
#include "capi.h"
42+
43+
PyObject* PySeqIter_New(PyObject *seq) {
44+
if (!PySequence_Check(seq)) {
45+
PyErr_BadInternalCall();
46+
return NULL;
47+
}
48+
return UPCALL_CEXT_O("PyTruffle_SeqIter_New", native_to_java(seq));
49+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
4444

4545
PyTypeObject PyCFunction_Type = PY_TRUFFLE_TYPE("builtin_function_or_method", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyCFunctionObject));
46-
PyTypeObject PyWrapperDescr_Type = PY_TRUFFLE_TYPE("builtin_function_or_method", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, sizeof(PyWrapperDescr_Type));;
4746

4847
PyObject* PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
4948
PyObject* func = to_sulong(polyglot_invoke(PY_TRUFFLE_CEXT,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
323323
return 0;
324324
}
325325
} else {
326+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
326327
PyTruffle_WriteOut(output_idx, PyObject*, arg);
327328
}
328329
break;

0 commit comments

Comments
 (0)