Skip to content

Commit 30e17ed

Browse files
committed
[GR-44953][GR-45159][GR-44691][GR-34614][GR-43627] Backport fixes for NumPy, SciPy, and PyTorch.
PullRequest: graalpython/2705
2 parents e796f9c + cababac commit 30e17ed

File tree

167 files changed

+3954
-3209
lines changed

Some content is hidden

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

167 files changed

+3954
-3209
lines changed

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,76 @@ PyAPI_FUNC(PyObject*) get_##NAME(RECEIVER obj) { \
428428
PyAPI_FUNC(RESULT) get_##NAME(RECEIVER obj) { \
429429
return obj->NAME; \
430430
}
431-
431+
#define PRIMITIVE_SUBFIELD_GETTER(RECEIVER, FIELD, RESULT, NAME) \
432+
PyAPI_FUNC(RESULT) get_##NAME(RECEIVER obj) { \
433+
return obj->FIELD? obj->FIELD->NAME : NULL; \
434+
}
432435
TYPE_FIELD_GETTER(PyObject*, ob_type)
433436
PRIMITIVE_FIELD_GETTER(PyObject*, Py_ssize_t, ob_refcnt)
434437
PRIMITIVE_FIELD_GETTER(PyVarObject*, Py_ssize_t, ob_size)
435438
OBJECT_FIELD_GETTER(PyTypeObject*, tp_dict)
436439
OBJECT_FIELD_GETTER(PyTypeObject*, tp_base)
437440
OBJECT_FIELD_GETTER(PyTypeObject*, tp_bases)
441+
442+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, reprfunc, tp_repr)
443+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, reprfunc, tp_str)
444+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, getattrofunc, tp_getattro)
445+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, setattrofunc, tp_setattro)
446+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, hashfunc, tp_hash)
447+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, ternaryfunc, tp_call)
448+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, getiterfunc, tp_iter)
449+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, iternextfunc, tp_iternext)
450+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, descrgetfunc, tp_descr_get)
451+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, descrsetfunc, tp_descr_set)
452+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, initproc, tp_init)
453+
PRIMITIVE_FIELD_GETTER(PyTypeObject*, richcmpfunc, tp_richcompare)
454+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_add)
455+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_subtract)
456+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_multiply)
457+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_remainder)
458+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_divmod)
459+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, ternaryfunc, nb_power)
460+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_negative)
461+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_positive)
462+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_absolute)
463+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, inquiry, nb_bool)
464+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_invert)
465+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_lshift)
466+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_rshift)
467+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_and)
468+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_xor)
469+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_or)
470+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_int)
471+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_float)
472+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_add)
473+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_subtract)
474+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_multiply)
475+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_remainder)
476+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, ternaryfunc, nb_inplace_power)
477+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_lshift)
478+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_rshift)
479+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_and)
480+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_xor)
481+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_or)
482+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_floor_divide)
483+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_true_divide)
484+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_floor_divide)
485+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_true_divide)
486+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, unaryfunc, nb_index)
487+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_matrix_multiply)
488+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_number, binaryfunc, nb_inplace_matrix_multiply)
489+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, lenfunc, sq_length)
490+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, binaryfunc, sq_concat)
491+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, ssizeargfunc, sq_repeat)
492+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, ssizeargfunc, sq_item)
493+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, ssizeobjargproc, sq_ass_item)
494+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, objobjproc, sq_contains)
495+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, binaryfunc, sq_inplace_concat)
496+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_sequence, ssizeargfunc, sq_inplace_repeat)
497+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_mapping, lenfunc, mp_length)
498+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_mapping, binaryfunc, mp_subscript)
499+
PRIMITIVE_SUBFIELD_GETTER(PyTypeObject*, tp_as_mapping, objobjargproc, mp_ass_subscript)
500+
438501
PRIMITIVE_FIELD_GETTER(PyTypeObject*, const char*, tp_name)
439502
OBJECT_FIELD_GETTER(PyTypeObject*, tp_mro)
440503
OBJECT_FIELD_GETTER(PyTypeObject*, tp_subclasses)
@@ -2118,10 +2181,18 @@ PyAPI_FUNC(PyObject*) PyWeakref_NewRef(PyObject* a, PyObject* b) {
21182181
PyAPI_FUNC(int) Py_AtExit(void (*a)(void)) {
21192182
return GraalPy_AtExit(a);
21202183
}
2184+
#undef Py_EnterRecursiveCall
2185+
PyAPI_FUNC(int) Py_EnterRecursiveCall(const char* a) {
2186+
return GraalPy_EnterRecursiveCall(a);
2187+
}
21212188
#undef Py_GenericAlias
21222189
PyAPI_FUNC(PyObject*) Py_GenericAlias(PyObject* a, PyObject* b) {
21232190
return GraalPy_GenericAlias(a, b);
21242191
}
2192+
#undef Py_LeaveRecursiveCall
2193+
PyAPI_FUNC(void) Py_LeaveRecursiveCall() {
2194+
GraalPy_LeaveRecursiveCall();
2195+
}
21252196
#undef _PyBytes_Join
21262197
PyAPI_FUNC(PyObject*) _PyBytes_Join(PyObject* a, PyObject* b) {
21272198
return Graal_PyBytes_Join(a, b);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ typedef struct {
391391
BUILTIN(PyWeakref_GetObject, PyObject*, PyObject*) \
392392
BUILTIN(PyWeakref_NewRef, PyObject*, PyObject*, PyObject*) \
393393
BUILTIN(Py_AtExit, int, void (*)(void)) \
394+
BUILTIN(Py_EnterRecursiveCall, int, const char*) \
394395
BUILTIN(Py_GenericAlias, PyObject*, PyObject*, PyObject*) \
396+
BUILTIN(Py_LeaveRecursiveCall, void) \
395397
BUILTIN(Py_get_PyASCIIObject_length, Py_ssize_t, PyASCIIObject*) \
396398
BUILTIN(Py_get_PyASCIIObject_state_ascii, unsigned int, PyASCIIObject*) \
397399
BUILTIN(Py_get_PyASCIIObject_state_compact, unsigned int, PyASCIIObject*) \

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -93,11 +93,3 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
9393
polyglot_from_PyObjectPtr_array(defs, defcount),
9494
kwdefs, closure);
9595
}
96-
97-
#undef Py_EnterRecursiveCall
98-
int Py_EnterRecursiveCall(const char *where) {
99-
return 0;
100-
}
101-
102-
#undef Py_LeaveRecursiveCall
103-
void Py_LeaveRecursiveCall(void) {}

graalpython/com.oracle.graal.python.jni/src/capi_forwards.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4543,9 +4543,8 @@ PyAPI_FUNC(char*) Py_EncodeLocale(const wchar_t* a, size_t* b) {
45434543
PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState* a) {
45444544
unimplemented("Py_EndInterpreter"); exit(-1);
45454545
}
4546-
int (*__target__Py_EnterRecursiveCall)(const char*) = NULL;
45474546
PyAPI_FUNC(int) Py_EnterRecursiveCall(const char* a) {
4548-
int result = (int) __target__Py_EnterRecursiveCall(a);
4547+
int result = (int) GraalPy_EnterRecursiveCall(a);
45494548
return result;
45504549
}
45514550
PyAPI_FUNC(void) Py_Exit(int a) {
@@ -4627,9 +4626,8 @@ PyAPI_FUNC(void) Py_InitializeEx(int a) {
46274626
PyAPI_FUNC(PyStatus) Py_InitializeFromConfig(const PyConfig* a) {
46284627
unimplemented("Py_InitializeFromConfig"); exit(-1);
46294628
}
4630-
void (*__target__Py_LeaveRecursiveCall)() = NULL;
46314629
PyAPI_FUNC(void) Py_LeaveRecursiveCall() {
4632-
__target__Py_LeaveRecursiveCall();
4630+
GraalPy_LeaveRecursiveCall();
46334631
}
46344632
PyAPI_FUNC(int) Py_Main(int a, wchar_t** b) {
46354633
unimplemented("Py_Main"); exit(-1);
@@ -6386,11 +6384,9 @@ void initializeCAPIForwards(void* (*getAPI)(const char*)) {
63866384
__target__PyUnicode_InternInPlace = getAPI("PyUnicode_InternInPlace");
63876385
__target__PyUnicode_New = getAPI("PyUnicode_New");
63886386
__target__PyVectorcall_Call = getAPI("PyVectorcall_Call");
6389-
__target__Py_EnterRecursiveCall = getAPI("Py_EnterRecursiveCall");
63906387
__target__Py_GetBuildInfo = getAPI("Py_GetBuildInfo");
63916388
__target__Py_GetCompiler = getAPI("Py_GetCompiler");
63926389
__target__Py_GetVersion = getAPI("Py_GetVersion");
6393-
__target__Py_LeaveRecursiveCall = getAPI("Py_LeaveRecursiveCall");
63946390
__target__Py_NewRef = getAPI("Py_NewRef");
63956391
__target__Py_XNewRef = getAPI("Py_XNewRef");
63966392
__target___PyASCIIObject_LENGTH = getAPI("_PyASCIIObject_LENGTH");

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

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -590,8 +590,69 @@ def test_object_size_err(self):
590590
except TypeError:
591591
assert True
592592
else:
593-
assert False
594-
593+
assert False
594+
595+
def test_sequence_mapping_inheritance(self):
596+
NativeMapping = CPyExtType(
597+
"NativeMapping",
598+
'''
599+
Py_ssize_t mp_length(PyObject* a) {
600+
return 1;
601+
}
602+
603+
PyObject* mp_subscript(PyObject* self, PyObject* key) {
604+
Py_INCREF(key);
605+
return key;
606+
}
607+
608+
PyObject* is_mapping(PyObject* self, PyObject* object) {
609+
return PyBool_FromLong(PyMapping_Check(object));
610+
}
611+
''',
612+
mp_length='mp_length',
613+
mp_subscript='mp_subscript',
614+
tp_methods='{"is_mapping", (PyCFunction)is_mapping, METH_O | METH_STATIC, ""}',
615+
)
616+
617+
NativeSequence = CPyExtType(
618+
"NativeSequence",
619+
'''
620+
Py_ssize_t sq_length(PyObject* a) {
621+
return 1;
622+
}
623+
624+
PyObject* sq_item(PyObject* self, Py_ssize_t index) {
625+
return PyLong_FromLong(index);
626+
}
627+
628+
PyObject* is_sequence(PyObject* self, PyObject* object) {
629+
return PyBool_FromLong(PySequence_Check(object));
630+
}
631+
''',
632+
sq_length='sq_length',
633+
sq_item='sq_item',
634+
tp_methods='{"is_sequence", (PyCFunction)is_sequence, METH_O | METH_STATIC, ""}',
635+
)
636+
637+
class MappingSubclass(NativeMapping):
638+
pass
639+
640+
mapping = MappingSubclass()
641+
assert len(mapping) == 1
642+
assert mapping['key'] == 'key'
643+
assert NativeMapping.is_mapping(mapping)
644+
assert not NativeSequence.is_sequence(mapping)
645+
646+
class SequenceSubclass(NativeSequence):
647+
pass
648+
649+
sequence = SequenceSubclass()
650+
assert len(sequence) == 1
651+
assert sequence[1] == 1
652+
assert NativeSequence.is_sequence(sequence)
653+
assert not NativeMapping.is_mapping(sequence)
654+
655+
595656
class TestAbstract(CPyExtTestCase):
596657

597658
def compile_module(self, name):
@@ -1405,4 +1466,4 @@ def compile_module(self, name):
14051466
cmpfunc=unhandled_error_compare
14061467
)
14071468

1408-
test_PyObject_Length = test_PyObject_Size
1469+
test_PyObject_Length = test_PyObject_Size

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ def _reference_getitem(args):
5959

6060

6161
def _reference_setitem(args):
62-
t = args[0]
62+
t = list(args[0])
6363
idx = args[1]
64+
value = args[2]
6465
if idx < 0 or idx >= len(t):
6566
raise IndexError('tuple index out of range')
66-
return None
67+
t[idx] = value
68+
return tuple(t)
6769

6870

6971
class MyStr(str):
@@ -155,47 +157,33 @@ def compile_module(self, name):
155157
((1, 2, 3), 3, str),
156158
),
157159
code="""PyObject* wrap_PyTuple_SetItem(PyObject* original, Py_ssize_t index, PyObject* value) {
158-
PyObject* tuple = PyTuple_New(PyTuple_Size(original));
159-
for (int i = 0; i < PyTuple_Size(original); i++) {
160-
PyTuple_SET_ITEM(tuple, i, Py_NewRef(PyTuple_GetItem(original, i)));
160+
Py_ssize_t size = PyTuple_Size(original);
161+
if (size < 0)
162+
return NULL;
163+
PyObject* tuple = PyTuple_New(size);
164+
if (!tuple)
165+
return NULL;
166+
for (int i = 0; i < size; i++) {
167+
PyObject* item = PyTuple_GetItem(original, i);
168+
if (!item) {
169+
Py_DECREF(tuple);
170+
return NULL;
171+
}
172+
PyTuple_SET_ITEM(tuple, i, item);
161173
}
162-
if (PyTuple_SetItem(tuple, index, value) == 0) {
163-
return Py_NewRef(Py_None);
164-
} else {
174+
Py_INCREF(value);
175+
if (PyTuple_SetItem(tuple, index, value) < 0) {
176+
Py_DECREF(tuple);
165177
return NULL;
166178
}
179+
return tuple;
167180
}
168181
""",
169182
resultspec="O",
170183
argspec='OnO',
171184
arguments=["PyObject* tuple", "Py_ssize_t index", "PyObject* value"],
172185
callfunction="wrap_PyTuple_SetItem",
173-
cmpfunc=unhandled_error_compare
174-
)
175-
176-
test_PyTuple_SET_ITEM = CPyExtFunction(
177-
lambda args: (None, args[1], None),
178-
lambda: (
179-
(1, str),
180-
(1, 0),
181-
(1, "asdf"),
182-
),
183-
code="""PyObject* wrap_PyTuple_SET_ITEM(Py_ssize_t index, PyObject* value) {
184-
PyObject* tuple = PyTuple_New(3);
185-
for (int i = 0; i < PyTuple_Size(tuple); i++) {
186-
if (i != index) {
187-
PyTuple_SET_ITEM(tuple, i, Py_NewRef(Py_None));
188-
}
189-
}
190-
PyTuple_SET_ITEM(tuple, index, Py_NewRef(value));
191-
return tuple;
192-
}
193-
""",
194-
resultspec="O",
195-
argspec='nO',
196-
arguments=["Py_ssize_t index", "PyObject* value"],
197-
callfunction="wrap_PyTuple_SET_ITEM",
198-
cmpfunc=unhandled_error_compare
186+
cmpfunc=unhandled_error_compare,
199187
)
200188

201189
# PyTuple_GetSlice

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,9 +909,8 @@ private void initializeImportlib() {
909909
if (zipimporter == PNone.NO_VALUE) {
910910
LOGGER.log(Level.FINE, () -> "# can't import zipimport.zipimporter");
911911
} else {
912-
SequenceStorageNodes.InsertItemNode insertItem = SequenceStorageNodes.InsertItemNode.getUncached();
913912
SequenceStorage store = pathHooksList.getSequenceStorage();
914-
pathHooksList.setSequenceStorage(insertItem.execute(store, 0, zipimporter));
913+
pathHooksList.setSequenceStorage(SequenceStorageNodes.InsertItemNode.executeUncached(store, 0, zipimporter));
915914
LOGGER.log(Level.FINE, () -> "# installed zipimport hook");
916915
}
917916
}

0 commit comments

Comments
 (0)