Skip to content

Commit 6d9d321

Browse files
committed
[GR-9957] Various fixes for sklearn
PullRequest: graalpython/101
2 parents fcc1417 + 5df842b commit 6d9d321

File tree

8 files changed

+156
-50
lines changed

8 files changed

+156
-50
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include <langinfo.h>
6262
#include <assert.h>
6363
#include <unistd.h>
64+
#include <math.h>
6465

6566
#include "pyport.h"
6667
#include "pymacro.h"

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,31 @@
1111
#define Py_PYCONFIG_H
1212

1313

14-
// TRUFFLE DEFS
15-
#define SIZEOF_DOUBLE 8
16-
#define SIZEOF_FLOAT 4
17-
#define SIZEOF_FPOS_T 16
18-
#define SIZEOF_INT 4
19-
#define SIZEOF_LONG 8
20-
#define SIZEOF_LONG_DOUBLE 16
21-
#define SIZEOF_LONG_LONG 8
22-
#define SIZEOF_OFF_T 8
23-
#define SIZEOF_PID_T 4
24-
#define SIZEOF_PTHREAD_T 8
25-
#define SIZEOF_SHORT 2
26-
#define SIZEOF_SIZE_T 8
27-
#define SIZEOF_TIME_T 8
28-
#define SIZEOF_UINTPTR_T 8
29-
#define SIZEOF_VOID_P 8
30-
#define SIZEOF_WCHAR_T 2
14+
// defines based on Clang defines
15+
#define SIZEOF_DOUBLE __SIZEOF_DOUBLE__
16+
#define SIZEOF_FLOAT __SIZEOF_FLOAT__
17+
#define SIZEOF_FPOS_T __SIZEOF_INT128__
18+
#define SIZEOF_INT __SIZEOF_INT__
19+
#define SIZEOF_LONG __SIZEOF_LONG__
20+
#define SIZEOF_LONG_DOUBLE __SIZEOF_LONG_DOUBLE__
21+
#define SIZEOF_LONG_LONG __SIZEOF_LONG_LONG__
22+
#define SIZEOF_OFF_T __SIZEOF_SIZE_T__
23+
#define SIZEOF_PID_T __SIZEOF_INT__
24+
#define SIZEOF_PTHREAD_T __SIZEOF_LONG__
25+
#define SIZEOF_SHORT __SIZEOF_SHORT__
26+
#define SIZEOF_SIZE_T __SIZEOF_SIZE_T__
27+
#define SIZEOF_TIME_T __SIZEOF_POINTER__
28+
#define SIZEOF_UINTPTR_T __SIZEOF_POINTER__
29+
#define SIZEOF_VOID_P __SIZEOF_POINTER__
30+
#define SIZEOF_WCHAR_T __SIZEOF_WCHAR_T__
3131
#define SIZEOF__BOOL 1
32-
#define INT_MIN 0x80000000
33-
#define INT_MAX 0x7fffffff
34-
#define UINT_MAX 0xffffffff
35-
#define SHRT_MIN 0x8000
36-
#define SHRT_MAX 0x7fff
37-
#define USHRT_MAX 0xffff
32+
#define INT_MIN ((-__INT32_MAX__)-1)
33+
#define INT_MAX __INT32_MAX__
34+
#define UINT_MAX __UINT32_MAX__
35+
#define SHRT_MIN ((-__INT16_MAX__)-1)
36+
#define SHRT_MAX __INT16_MAX__
37+
#define USHRT_MAX __UINT16_MAX__
38+
#define CHAR_BIT __CHAR_BIT__
3839
// #define Py_LIMITED_API 1
3940
#define _Py_BEGIN_SUPPRESS_IPH
4041
#define _Py_END_SUPPRESS_IPH

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,34 @@ Py_UNICODE* PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) {
287287
}
288288
return NULL;
289289
}
290+
291+
/* Fast detection of the most frequent whitespace characters */
292+
const unsigned char _Py_ascii_whitespace[] = {
293+
0, 0, 0, 0, 0, 0, 0, 0,
294+
/* case 0x0009: * CHARACTER TABULATION */
295+
/* case 0x000A: * LINE FEED */
296+
/* case 0x000B: * LINE TABULATION */
297+
/* case 0x000C: * FORM FEED */
298+
/* case 0x000D: * CARRIAGE RETURN */
299+
0, 1, 1, 1, 1, 1, 0, 0,
300+
0, 0, 0, 0, 0, 0, 0, 0,
301+
/* case 0x001C: * FILE SEPARATOR */
302+
/* case 0x001D: * GROUP SEPARATOR */
303+
/* case 0x001E: * RECORD SEPARATOR */
304+
/* case 0x001F: * UNIT SEPARATOR */
305+
0, 0, 0, 0, 1, 1, 1, 1,
306+
/* case 0x0020: * SPACE */
307+
1, 0, 0, 0, 0, 0, 0, 0,
308+
0, 0, 0, 0, 0, 0, 0, 0,
309+
0, 0, 0, 0, 0, 0, 0, 0,
310+
0, 0, 0, 0, 0, 0, 0, 0,
311+
312+
0, 0, 0, 0, 0, 0, 0, 0,
313+
0, 0, 0, 0, 0, 0, 0, 0,
314+
0, 0, 0, 0, 0, 0, 0, 0,
315+
0, 0, 0, 0, 0, 0, 0, 0,
316+
0, 0, 0, 0, 0, 0, 0, 0,
317+
0, 0, 0, 0, 0, 0, 0, 0,
318+
0, 0, 0, 0, 0, 0, 0, 0,
319+
0, 0, 0, 0, 0, 0, 0, 0
320+
};

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@
6969
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
7070
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
7171
import com.oracle.graal.python.builtins.objects.cell.PCell;
72-
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.FromNativeSubclassNode;
73-
import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
74-
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
75-
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
7672
import com.oracle.graal.python.builtins.objects.common.HashingStorage.DictEntry;
7773
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
7874
import com.oracle.graal.python.builtins.objects.complex.PComplex;
@@ -155,6 +151,7 @@
155151
import com.oracle.truffle.api.dsl.Specialization;
156152
import com.oracle.truffle.api.dsl.TypeSystemReference;
157153
import com.oracle.truffle.api.nodes.UnexpectedResultException;
154+
import com.oracle.truffle.api.profiles.BranchProfile;
158155
import com.oracle.truffle.api.profiles.ConditionProfile;
159156

160157
@CoreFunctions(defineModule = "builtins")
@@ -490,11 +487,13 @@ public Object floatFromNone(PythonClass cls, @SuppressWarnings("unused") PNone a
490487
}
491488

492489
@Specialization
493-
Object doPythonObject(PythonClass cls, PythonObject obj,
494-
@Cached("create(__FLOAT__)") LookupAndCallUnaryNode callFloatNode) {
490+
Object doPythonObject(PythonClass cls, Object obj,
491+
@Cached("create(__FLOAT__)") LookupAndCallUnaryNode callFloatNode,
492+
@Cached("create()") BranchProfile gotException) {
495493
try {
496494
return floatFromFloat(cls, callFloatNode.executeDouble(obj));
497495
} catch (UnexpectedResultException e) {
496+
gotException.enter();
498497
Object result = e.getResult();
499498
if (result == PNone.NO_VALUE) {
500499
throw raise(TypeError, "must be real number, not %p", obj);
@@ -507,21 +506,6 @@ Object doPythonObject(PythonClass cls, PythonObject obj,
507506
}
508507
}
509508

510-
protected static FromNativeSubclassNode cacheGetFloat() {
511-
return FromNativeSubclassNode.create(PythonBuiltinClassType.PFloat, NativeCAPISymbols.FUN_PY_FLOAT_AS_DOUBLE);
512-
}
513-
514-
@Specialization
515-
Object doNativeFloat(@SuppressWarnings("unused") PythonNativeClass cls, PythonNativeObject possibleBase,
516-
@Cached("cacheGetFloat()") FromNativeSubclassNode getFloat) {
517-
Object convertedFloat = getFloat.execute(possibleBase);
518-
if (convertedFloat instanceof Double) {
519-
return possibleBase; // TODO (tfel): we really need to call back into C
520-
} else {
521-
throw raise(TypeError, "must be real number, not %p", possibleBase);
522-
}
523-
}
524-
525509
@Fallback
526510
@TruffleBoundary
527511
public Object floatFromObject(@SuppressWarnings("unused") Object cls, Object arg) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ abstract static class FloatNode extends PythonUnaryBuiltinNode {
177177
PFloat doPFloat(PFloat self) {
178178
return self;
179179
}
180+
181+
protected static FromNativeSubclassNode cacheGetFloat() {
182+
return FromNativeSubclassNode.create(PythonBuiltinClassType.PFloat, NativeCAPISymbols.FUN_PY_FLOAT_AS_DOUBLE);
183+
}
184+
185+
@Specialization
186+
Object doNativeFloat(PythonNativeObject possibleBase,
187+
@Cached("cacheGetFloat()") FromNativeSubclassNode getFloat) {
188+
Object convertedFloat = getFloat.execute(possibleBase);
189+
if (convertedFloat instanceof Double) {
190+
return possibleBase;
191+
} else {
192+
throw raise(PythonErrorType.TypeError, "must be real number, not %p", possibleBase);
193+
}
194+
}
180195
}
181196

182197
@Builtin(name = __ADD__, fixedNumOfArguments = 2)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
5050
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5151
import com.oracle.graal.python.nodes.SpecialMethodNames;
52+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
53+
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
5254
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5355
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5456
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -60,6 +62,7 @@
6062
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6163
import com.oracle.truffle.api.CompilerDirectives;
6264
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
65+
import com.oracle.truffle.api.dsl.Cached;
6366
import com.oracle.truffle.api.dsl.Fallback;
6467
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6568
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -607,6 +610,10 @@ abstract static class RMulNode extends MulNode {
607610
@TypeSystemReference(PythonArithmeticTypes.class)
608611
abstract static class PowNode extends PythonTernaryBuiltinNode {
609612

613+
protected static PowNode create() {
614+
return null;
615+
}
616+
610617
@Specialization(guards = "right >= 0", rewriteOn = ArithmeticException.class)
611618
int doIntegerFast(int left, int right, @SuppressWarnings("unused") PNone none) {
612619
int result = 1;
@@ -688,6 +695,28 @@ PInt doPInt(PInt left, PInt right, @SuppressWarnings("unused") PNone none) {
688695
return factory().createInt((long) value);
689696
}
690697

698+
@Specialization
699+
Object powModulo(Object x, Object y, long z,
700+
@Cached("create(__POW__)") LookupAndCallTernaryNode powNode,
701+
@Cached("create(__MOD__)") LookupAndCallBinaryNode modNode) {
702+
Object result = powNode.execute(x, y, PNone.NO_VALUE);
703+
if (result == PNotImplemented.NOT_IMPLEMENTED) {
704+
return result;
705+
}
706+
return modNode.executeObject(result, z);
707+
}
708+
709+
@Specialization
710+
Object powModuloPInt(Object x, Object y, PInt z,
711+
@Cached("create(__POW__)") LookupAndCallTernaryNode powNode,
712+
@Cached("create(__MOD__)") LookupAndCallBinaryNode modNode) {
713+
Object result = powNode.execute(x, y, PNone.NO_VALUE);
714+
if (result == PNotImplemented.NOT_IMPLEMENTED) {
715+
return result;
716+
}
717+
return modNode.executeObject(result, z);
718+
}
719+
691720
@Fallback
692721
@SuppressWarnings("unused")
693722
Object doFallback(Object x, Object y, Object z) {

graalpython/lib-graalpython/_sre.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def TREGEX_ENGINE(*args): raise e
5858
class SRE_Match():
5959
def __init__(self, pattern, pos, endpos, result):
6060
self.result = result
61+
self.compiled_regex = result.regex
6162
self.re = pattern
6263
self.pos = pos
6364
self.endpos = endpos
@@ -67,26 +68,49 @@ def end(self, groupnum=0):
6768

6869
def group(self, *args):
6970
if not args:
70-
return self.result.input[self.result.start[0]:self.result.end[0]]
71+
return self.__group__(0)
7172
elif len(args) == 1:
72-
return self.result.input[self.result.start[args[0]]:self.result.end[args[0]]]
73+
return self.__group__(args[0])
7374
else:
7475
lst = []
7576
for arg in args:
76-
lst.append(self.result.input[self.result.start[arg]:self.result.end[arg]])
77+
lst.append(self.__group__(arg))
7778
return tuple(lst)
7879

7980
def groups(self, default=None):
8081
lst = []
8182
for arg in range(1, self.result.groupCount):
82-
lst.append(self.result.input[self.result.start[arg]:self.result.end[arg]])
83+
lst.append(self.__group__(arg))
8384
return tuple(lst)
8485

86+
def __groupidx__(self, idx):
87+
if isinstance(idx, str):
88+
return self.compiled_regex.groups[idx]
89+
else:
90+
return idx
91+
92+
def __group__(self, idx):
93+
idxarg = self.__groupidx__(idx)
94+
start = self.result.start[idxarg]
95+
if start < 0:
96+
return None
97+
else:
98+
return self.result.input[start:self.result.end[idxarg]]
99+
100+
def groupdict(self, default=None):
101+
d = {}
102+
for k in self.compiled_regex.groups:
103+
idx = self.compiled_regex.groups[k]
104+
d[k] = self.__group__(idx)
105+
return d
106+
85107
def span(self, groupnum=0):
86-
return (self.result.start[groupnum], self.result.end[groupnum])
108+
idxarg = self.__groupidx__(groupnum)
109+
return (self.result.start[idxarg], self.result.end[idxarg])
87110

88111
def start(self, groupnum=0):
89-
return self.result.start[groupnum]
112+
idxarg = self.__groupidx__(groupnum)
113+
return self.result.start[idxarg]
90114

91115
@property
92116
def string(self):

graalpython/lib-graalpython/sys.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def make_flags_class():
7373
flags = make_flags_class()(flags)
7474
del make_flags_class
7575

76+
7677
def make_float_info_class():
7778
from _descriptor import make_named_tuple_class
7879
return make_named_tuple_class(
@@ -93,6 +94,26 @@ def make_float_info_class():
9394
del make_float_info_class
9495

9596

97+
def make_hash_info_class():
98+
from _descriptor import make_named_tuple_class
99+
return make_named_tuple_class(
100+
"hash_info",
101+
["algorithm",
102+
"cutoff",
103+
"hash_bits",
104+
"imag",
105+
"inf",
106+
"modulus",
107+
"nan",
108+
"seed_bits",
109+
"width"]
110+
)
111+
hash_info = make_hash_info_class()(
112+
("java", 0, 64, 0, float('inf').__hash__(), 7, float('nan').__hash__(), 0, 64)
113+
)
114+
del make_hash_info_class
115+
116+
96117
meta_path = []
97118
path_hooks = []
98119
path_importer_cache = {}

0 commit comments

Comments
 (0)