Skip to content

Commit 28be1bb

Browse files
committed
[GR-45350] Fixes for onnxruntime
PullRequest: graalpython/2726
2 parents 416136a + d6b18a0 commit 28be1bb

38 files changed

+2633
-2169
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2017 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2

graalpython/com.oracle.graal.python.cext/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2019, 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
@@ -77,6 +77,7 @@ def setup(*args, **kwargs):
7777
# wrap the distutil setup. since we're running in the same process, running
7878
# a full clean will fail the next build, since distutils thinks it already
7979
# created the "build" directory
80+
shutil.rmtree("build", ignore_errors=True)
8081
os.makedirs("build", exist_ok=False)
8182
return distutils_setup(*args, **kwargs)
8283

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

Lines changed: 1 addition & 18 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
@@ -56,13 +56,6 @@ static PyObject* null_error(void) {
5656
return NULL;
5757
}
5858

59-
int PyIter_Check(PyObject *obj) {
60-
PyTypeObject *tp = Py_TYPE(obj);
61-
return (tp->tp_iternext != NULL &&
62-
tp->tp_iternext != &_PyObject_NextNotImplemented &&
63-
((PyObject *)tp->tp_iternext) != Py_NotImplemented);
64-
}
65-
6659
static PyObject * do_unaryop(PyObject *v, UnaryOp unaryop) {
6760
return GraalPyTruffleNumber_UnaryOp(v, (int) unaryop);
6861
}
@@ -480,16 +473,6 @@ PyObject ** _PySequence_Fast_ITEMS(PyObject *o) {
480473
return PyList_Check(o) ? PyListObject_ob_item(o) : PyTupleObject_ob_item(o);
481474
}
482475

483-
int _PyIter_Check(PyObject* obj) {
484-
iternextfunc func = PyTypeObject_tp_iternext(Py_TYPE(obj));
485-
return func != NULL && func != &_PyObject_NextNotImplemented;
486-
487-
}
488-
int _PyIndex_Check(PyObject* obj) {
489-
PyNumberMethods* methods = PyTypeObject_tp_as_number(Py_TYPE(obj));
490-
return methods != NULL && PyNumberMethods_nb_index(methods) != NULL;
491-
492-
}
493476
PyObject* _PySequence_ITEM(PyObject* obj, Py_ssize_t index) {
494477
PySequenceMethods* methods = PyTypeObject_tp_as_sequence(Py_TYPE(obj));
495478
return PySequenceMethods_sq_item(methods)(obj, index);

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

Lines changed: 15 additions & 1 deletion
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
@@ -514,3 +514,17 @@ _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr,
514514

515515
return str;
516516
}
517+
518+
PyObject* bytes_subtype_new(PyTypeObject *type, int8_t* contents, Py_ssize_t n) {
519+
PyObject* bytes = type->tp_alloc(type, n);
520+
if (bytes != NULL) {
521+
char* dst = ((PyBytesObject*)bytes)->ob_sval;
522+
memcpy(dst, contents, n);
523+
dst[n] = '\0';
524+
}
525+
return bytes;
526+
}
527+
528+
void* PyTruffle_NativeBytesItems(PyBytesObject* bytes) {
529+
return polyglot_from_i8_array((int8_t*)bytes->ob_sval, bytes->ob_base.ob_size);
530+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,10 @@ PyAPI_FUNC(int) PyIndex_Check(PyObject* a) {
16331633
PyAPI_FUNC(PyObject*) PyInstanceMethod_New(PyObject* a) {
16341634
return GraalPyInstanceMethod_New(a);
16351635
}
1636+
#undef PyIter_Check
1637+
PyAPI_FUNC(int) PyIter_Check(PyObject* a) {
1638+
return GraalPyIter_Check(a);
1639+
}
16361640
#undef PyIter_Next
16371641
PyAPI_FUNC(PyObject*) PyIter_Next(PyObject* a) {
16381642
return GraalPyIter_Next(a);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ typedef struct {
166166
BUILTIN(PyImport_ImportModuleNoBlock, PyObject*, const char*) \
167167
BUILTIN(PyIndex_Check, int, PyObject*) \
168168
BUILTIN(PyInstanceMethod_New, PyObject*, PyObject*) \
169+
BUILTIN(PyIter_Check, int, PyObject*) \
169170
BUILTIN(PyIter_Next, PyObject*, PyObject*) \
170171
BUILTIN(PyList_Append, int, PyObject*, PyObject*) \
171172
BUILTIN(PyList_AsTuple, PyObject*, PyObject*) \

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,9 +2689,8 @@ PyAPI_FUNC(PyInterpreterState*) PyInterpreterState_Next(PyInterpreterState* a) {
26892689
PyAPI_FUNC(PyThreadState*) PyInterpreterState_ThreadHead(PyInterpreterState* a) {
26902690
unimplemented("PyInterpreterState_ThreadHead"); exit(-1);
26912691
}
2692-
int (*__target__PyIter_Check)(PyObject*) = NULL;
26932692
PyAPI_FUNC(int) PyIter_Check(PyObject* a) {
2694-
int result = (int) __target__PyIter_Check(a);
2693+
int result = (int) GraalPyIter_Check(a);
26952694
return result;
26962695
}
26972696
PyAPI_FUNC(PyObject*) PyIter_Next(PyObject* a) {
@@ -6229,7 +6228,6 @@ void initializeCAPIForwards(void* (*getAPI)(const char*)) {
62296228
__target__PyInterpreterState_GetID = getAPI("PyInterpreterState_GetID");
62306229
__target__PyInterpreterState_GetIDFromThreadState = getAPI("PyInterpreterState_GetIDFromThreadState");
62316230
__target__PyInterpreterState_Main = getAPI("PyInterpreterState_Main");
6232-
__target__PyIter_Check = getAPI("PyIter_Check");
62336231
__target__PyLong_FromString = getAPI("PyLong_FromString");
62346232
__target__PyMapping_GetItemString = getAPI("PyMapping_GetItemString");
62356233
__target__PyMemoryView_FromBuffer = getAPI("PyMemoryView_FromBuffer");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def CPyExtType(name, code, **kwargs):
575575
PyVarObject_HEAD_INIT(NULL, 0)
576576
"{name}.{name}",
577577
sizeof({name}Object), /* tp_basicsize */
578-
0, /* tp_itemsize */
578+
{tp_itemsize}, /* tp_itemsize */
579579
{tp_dealloc}, /* tp_dealloc */
580580
{tp_vectorcall_offset},
581581
{tp_getattr},
@@ -644,7 +644,7 @@ def CPyExtType(name, code, **kwargs):
644644
kwargs["code"] = code
645645
kwargs.setdefault("ready_code", "")
646646
kwargs.setdefault("post_ready_code", "")
647-
kwargs.setdefault("tp_methods", "{NULL, NULL, 0, NULL}")
647+
kwargs.setdefault("tp_itemsize", "0")
648648
kwargs.setdefault("tp_new", "PyType_GenericNew")
649649
kwargs.setdefault("tp_alloc", "PyType_GenericAlloc")
650650
kwargs.setdefault("tp_free", "PyObject_Del")

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

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ class CIter:
7272
def __iter__(self):
7373
return iter([1, 2, 3])
7474

75+
76+
BytesSubclass = CPyExtType(
77+
"BytesSubclass",
78+
'',
79+
struct_base='PyBytesObject bytes',
80+
tp_itemsize='sizeof(char)',
81+
tp_base='&PyBytes_Type',
82+
tp_new='0',
83+
tp_alloc='0',
84+
tp_free='0',
85+
)
86+
87+
7588
class TestPyBytes(CPyExtTestCase):
7689

7790
def compile_module(self, name):
@@ -115,7 +128,11 @@ def compile_module(self, name):
115128
# PyBytes_AsString
116129
test_PyBytes_AsString = CPyExtFunction(
117130
lambda b: b[0].decode(),
118-
lambda: ((b"hello",), (b"world",)),
131+
lambda: (
132+
(b"hello",),
133+
(b"world",),
134+
(BytesSubclass(b"hello"),),
135+
),
119136
resultspec="s",
120137
argspec="O",
121138
arguments=["PyObject* arg"],
@@ -124,7 +141,11 @@ def compile_module(self, name):
124141
# PyBytes_AsStringAndSize
125142
test_PyBytes_AsStringAndSize = CPyExtFunctionOutVars(
126143
_as_string_and_size,
127-
lambda: ((b"hello",), (b"world",)),
144+
lambda: (
145+
(b"hello",),
146+
(b"world",),
147+
(BytesSubclass(b"hello"),),
148+
),
128149
resultspec="isn",
129150
argspec="O",
130151
arguments=["PyObject* arg"],
@@ -160,7 +181,12 @@ def compile_module(self, name):
160181
# PyBytes_Size
161182
test_PyBytes_Size = CPyExtFunction(
162183
lambda b: len(b[0]),
163-
lambda: ((b"hello",), (b"hello world",), (b"",)),
184+
lambda: (
185+
(b"hello",),
186+
(b"hello world",),
187+
(b"",),
188+
(BytesSubclass(b"hello"),),
189+
),
164190
resultspec="n",
165191
argspec="O",
166192
arguments=["PyObject* arg"],
@@ -169,7 +195,12 @@ def compile_module(self, name):
169195
# PyBytes_GET_SIZE
170196
test_PyBytes_GET_SIZE = CPyExtFunction(
171197
lambda b: len(b[0]),
172-
lambda: ((b"hello",), (b"hello world",), (b"",)),
198+
lambda: (
199+
(b"hello",),
200+
(b"hello world",),
201+
(b"",),
202+
(BytesSubclass(b"hello"),),
203+
),
173204
resultspec="n",
174205
argspec="O",
175206
arguments=["PyObject* arg"],
@@ -252,6 +283,7 @@ def compile_module(self, name):
252283
(b"hello",),
253284
("hello",),
254285
("hellö".encode(),),
286+
(BytesSubclass(b"hello"),),
255287
),
256288
resultspec="i",
257289
argspec='O',
@@ -260,7 +292,7 @@ def compile_module(self, name):
260292
)
261293

262294
test_PyBytes_CheckExact = CPyExtFunction(
263-
lambda args: isinstance(args[0], bytes),
295+
lambda args: type(args[0]) == bytes,
264296
lambda: (
265297
(b"hello",),
266298
(bytes(),),
@@ -269,6 +301,7 @@ def compile_module(self, name):
269301
(1,),
270302
(dict(),),
271303
(tuple(),),
304+
(BytesSubclass(b"hello"),),
272305
),
273306
resultspec="i",
274307
argspec='O',
@@ -283,34 +316,15 @@ def compile_module(self, name):
283316
(b"hello",),
284317
("hellö".encode("utf-8"),),
285318
(b"hello world",),
286-
(b"",)
319+
(b"",),
320+
(BytesSubclass(b"hello"),),
287321
),
288322
resultspec="s",
289323
argspec="O",
290324
arguments=["PyObject* arg"],
291325
cmpfunc=unhandled_error_compare
292326
)
293327

294-
test_PyBytes_Mutation = CPyExtFunction(
295-
lambda args: args[1],
296-
lambda: (
297-
(b"hello", b"hallo"),
298-
),
299-
code="""PyObject* mutate_bytes(PyObject* bytesObj, PyObject* expected) {
300-
char* content = PyBytes_AS_STRING(bytesObj);
301-
char* copy = (char*) malloc(PyBytes_Size(bytesObj)+1);
302-
content[1] = 'a';
303-
memcpy(copy, content, PyBytes_Size(bytesObj)+1);
304-
return PyBytes_FromString(copy);
305-
}
306-
""",
307-
resultspec="O",
308-
argspec="OO",
309-
arguments=["PyObject* bytesObj", "PyObject* expected"],
310-
callfunction="mutate_bytes",
311-
cmpfunc=unhandled_error_compare
312-
)
313-
314328
test_PyBytes_Resize = CPyExtFunction(
315329
lambda args: args[0][:args[1]],
316330
lambda: (
@@ -509,3 +523,36 @@ def test_create_from_buffer_exception(self):
509523
)
510524
self.assertRaises(ValueError, bytes, TestType())
511525
self.assertRaises(ValueError, bytearray, TestType())
526+
527+
528+
class TestNativeSubclass(unittest.TestCase):
529+
def test_builtins(self):
530+
b = BytesSubclass(b"hello")
531+
assert type(b) == BytesSubclass
532+
assert b
533+
assert not BytesSubclass(b'')
534+
assert len(b) == 5
535+
assert b[1] == ord('e')
536+
assert b[1:] == b"ello"
537+
assert b == b"hello"
538+
assert b < b"xxxxx"
539+
assert b > b"aaaaa"
540+
assert ord('e') in b
541+
assert b + b" world" == b"hello world"
542+
assert b * 2 == b"hellohello"
543+
assert list(b) == [ord('h'), ord('e'), ord('l'), ord('l'), ord('o')]
544+
assert repr(b) == "b'hello'"
545+
assert b.index(ord('l')) == 2
546+
assert b.count(ord('l')) == 2
547+
assert b.decode('ascii') == 'hello'
548+
assert BytesSubclass(b'hello ').strip() == b'hello'
549+
assert BytesSubclass(b',').join([b'a', BytesSubclass(b'b')]) == b'a,b'
550+
assert hash(b) == hash(b'hello')
551+
assert BytesSubclass(b'(%s)') % b'a' == b'(a)'
552+
assert b.startswith(b'h')
553+
assert b.endswith(b'o')
554+
assert b.hex() == b'hello'.hex()
555+
assert b.islower()
556+
assert b.replace(b'e', b'a') == b'hallo'
557+
assert b.upper() == b'HELLO'
558+
assert BytesSubclass(b'a,b').split(BytesSubclass(b',')) == [b'a', b'b']

0 commit comments

Comments
 (0)