Skip to content

Commit 11194f0

Browse files
committed
[GR-64171] Add support for ormsgpack and prepare upstreaming some changes
PullRequest: graalpython/3758
2 parents befdba7 + 97fb792 commit 11194f0

File tree

9 files changed

+421
-17
lines changed

9 files changed

+421
-17
lines changed

graalpython/com.oracle.graal.python.cext/include/cpython/tupleobject.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -39,14 +39,10 @@ PyAPI_FUNC(PyObject *) _PyTuple_GET_ITEM(PyObject *, Py_ssize_t);
3939
// GraalPy-specific
4040
PyAPI_FUNC(PyObject **) PyTruffleTuple_GetItems(PyObject *op);
4141

42-
/* Function *only* to be used to fill in brand new tuples */
43-
static inline void
44-
PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
45-
PyTruffleTuple_GetItems(op)[index] = value;
46-
}
4742
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
43+
PyAPI_FUNC(void) PyTuple_SET_ITEM(PyObject*, Py_ssize_t, PyObject*);
4844
#define PyTuple_SET_ITEM(op, index, value) \
49-
PyTuple_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
45+
do { PyTruffleTuple_GetItems(op)[index] = value; } while (0)
5046
#endif
5147

5248
PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -124,7 +124,9 @@ typedef struct {
124124

125125
// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
126126
PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
127+
#if 0 // GraalPy change
127128
#define Py_Is(x, y) ((x) == (y))
129+
#endif // GraalPy change
128130

129131

130132
PyAPI_FUNC(Py_ssize_t) PyTruffle_REFCNT(PyObject *ob);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,7 +2532,12 @@ Py_XNewRef(PyObject *obj)
25322532
// for the stable ABI.
25332533
int Py_Is(PyObject *x, PyObject *y)
25342534
{
2535+
#if 0 // GraalPy change
25352536
return (x == y);
2537+
#else
2538+
return (x == y) ||
2539+
(points_to_py_handle_space(x) && points_to_py_handle_space(y) && GraalPyTruffle_Is(x, y));
2540+
#endif
25362541
}
25372542

25382543
int Py_IsNone(PyObject *x)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2022 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -1424,3 +1424,9 @@ _PyTuple_GET_ITEM(PyObject* a, Py_ssize_t b) {
14241424
}
14251425
return NULL; // an exception has happend during transtion
14261426
}
1427+
1428+
#undef PyTuple_SET_ITEM
1429+
// Export PyTuple_SET_ITEM as regular API function to use in PyO3 and others
1430+
void PyTuple_SET_ITEM(PyObject* op, Py_ssize_t index, PyObject* value) {
1431+
PyTruffleTuple_GetItems(op)[index] = value;
1432+
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2025, 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
@@ -151,14 +151,15 @@ class TestPyTuple(CPyExtTestCase):
151151
((1, 2, 3), -1, []),
152152
((1, 2, 3), 3, str),
153153
),
154-
code="""PyObject* wrap_PyTuple_SetItem(PyObject* original, Py_ssize_t index, PyObject* value) {
154+
code="""
155+
PyObject* wrap_PyTuple_SetItem(PyObject* original, Py_ssize_t index, PyObject* value) {
155156
Py_ssize_t size = PyTuple_Size(original);
156157
if (size < 0)
157158
return NULL;
158159
PyObject* tuple = PyTuple_New(size);
159160
if (!tuple)
160161
return NULL;
161-
for (int i = 0; i < size; i++) {
162+
for (int i = 0; i < size / 2; i++) {
162163
PyObject* item = PyTuple_GetItem(original, i);
163164
if (!item) {
164165
Py_DECREF(tuple);
@@ -167,6 +168,22 @@ class TestPyTuple(CPyExtTestCase):
167168
Py_INCREF(item);
168169
PyTuple_SET_ITEM(tuple, i, item);
169170
}
171+
172+
#ifdef GRAALVM_PYTHON
173+
// test that we also have it as API function on GraalPy
174+
#undef PyTuple_SET_ITEM
175+
#endif
176+
177+
for (int i = size / 2; i < size; i++) {
178+
PyObject* item = PyTuple_GetItem(original, i);
179+
if (!item) {
180+
Py_DECREF(tuple);
181+
return NULL;
182+
}
183+
Py_INCREF(item);
184+
PyTuple_SET_ITEM(tuple, i, item);
185+
}
186+
170187
Py_INCREF(value);
171188
if (PyTuple_SetItem(tuple, index, value) < 0) {
172189
Py_DECREF(tuple);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextObjectBuiltins.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodSlotNode;
134134
import com.oracle.graal.python.nodes.object.GetClassNode;
135135
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
136+
import com.oracle.graal.python.nodes.object.IsNode;
136137
import com.oracle.graal.python.nodes.util.CannotCastException;
137138
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
138139
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
@@ -741,4 +742,13 @@ static Object getDict(Object object,
741742
return getDict.execute(inliningTarget, object);
742743
}
743744
}
745+
746+
@CApiBuiltin(ret = Int, args = {PyObject, PyObject}, call = Ignored)
747+
abstract static class PyTruffle_Is extends CApiBinaryBuiltinNode {
748+
@Specialization
749+
static int isTrue(Object a, Object b,
750+
@Cached IsNode isNode) {
751+
return isNode.execute(a, b) ? 1 : 0;
752+
}
753+
}
744754
}

graalpython/lib-graalpython/patches/metadata.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ version = '== 3.10.5'
353353
patch = 'orjson-3.10.5.patch'
354354
license = 'Apache-2.0 OR MIT'
355355

356+
[[ormsgpack.rules]]
357+
version = '>= 1.8.0, <= 1.9.1'
358+
patch = 'ormsgpack-1.8.0-1.9.1.patch'
359+
license = 'Apache-2.0 OR MIT'
360+
356361
[[overrides.rules]]
357362
version = '== 7.4.0'
358363
# Important: This patch esentially breaks the package, it's not upstreamable. The package relies on bytecode parsing

0 commit comments

Comments
 (0)