Skip to content

Commit d958d97

Browse files
committed
Add patch for Cython 3
1 parent ae6391b commit d958d97

File tree

2 files changed

+366
-0
lines changed

2 files changed

+366
-0
lines changed
Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
diff --git a/Cython/Build/Dependencies.py b/Cython/Build/Dependencies.py
2+
index 7de4065..e1589cd 100644
3+
--- a/Cython/Build/Dependencies.py
4+
+++ b/Cython/Build/Dependencies.py
5+
@@ -1127,6 +1127,8 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False,
6+
progress = progress_fmt.format(i+1, N)
7+
to_compile[i] = to_compile[i][1:] + (progress,)
8+
9+
+ nthreads = 0 # GraalVM: we don't want to spawn
10+
+
11+
if N <= 1:
12+
nthreads = 0
13+
if nthreads:
14+
diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py
15+
index 46dea92..12c0616 100644
16+
--- a/Cython/Compiler/Builtin.py
17+
+++ b/Cython/Compiler/Builtin.py
18+
@@ -53,11 +53,12 @@ class _BuiltinOverride(object):
19+
20+
21+
class BuiltinAttribute(object):
22+
- def __init__(self, py_name, cname=None, field_type=None, field_type_name=None):
23+
+ def __init__(self, py_name, cname=None, field_type=None, field_type_name=None, getter=None):
24+
self.py_name = py_name
25+
self.cname = cname or py_name
26+
self.field_type_name = field_type_name # can't do the lookup before the type is declared!
27+
self.field_type = field_type
28+
+ self.getter = getter
29+
30+
def declare_in_type(self, self_type):
31+
if self.field_type_name is not None:
32+
@@ -65,7 +66,7 @@ class BuiltinAttribute(object):
33+
field_type = builtin_scope.lookup(self.field_type_name).type
34+
else:
35+
field_type = self.field_type or PyrexTypes.py_object_type
36+
- entry = self_type.scope.declare(self.py_name, self.cname, field_type, None, 'private')
37+
+ entry = self_type.scope.declare(self.py_name, self.cname, field_type, None, 'private', getter=self.getter)
38+
entry.is_variable = True
39+
40+
41+
@@ -354,10 +355,11 @@ builtin_types_table = [
42+
utility_code=UtilityCode.load("py_dict_clear", "Optimize.c")),
43+
BuiltinMethod("copy", "T", "T", "PyDict_Copy")]),
44+
45+
- ("slice", "PySlice_Type", [BuiltinAttribute('start'),
46+
- BuiltinAttribute('stop'),
47+
- BuiltinAttribute('step'),
48+
+ ("slice", "PySlice_Type", [BuiltinAttribute('start', getter="PySlice_Start"),
49+
+ BuiltinAttribute('stop', getter="PySlice_Stop"),
50+
+ BuiltinAttribute('step', getter="PySlice_Step"),
51+
]),
52+
+
53+
# ("file", "PyFile_Type", []), # not in Py3
54+
55+
("set", "PySet_Type", [BuiltinMethod("clear", "T", "r", "PySet_Clear"),
56+
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
57+
index 455d8b6..2d66296 100644
58+
--- a/Cython/Compiler/ExprNodes.py
59+
+++ b/Cython/Compiler/ExprNodes.py
60+
@@ -7740,6 +7740,16 @@ class AttributeNode(ExprNode):
61+
if obj.type.is_builtin_type and self.entry and self.entry.is_variable:
62+
# accessing a field of a builtin type, need to cast better than result_as() does
63+
obj_code = obj.type.cast_code(obj.result(), to_object_struct = True)
64+
+ # GraalPy change: add getter functions for builtins
65+
+ if obj.type.is_builtin_type and self.entry and self.entry.getter:
66+
+ return "%s(%s)" % (self.entry.getter, obj_code)
67+
+ # GraalPy change: add a getter for array.data
68+
+ if (
69+
+ self.entry.name == 'data' and
70+
+ getattr(self.type, 'name', None) == '__data_union' and
71+
+ getattr(self.obj.type, 'name') == 'array'
72+
+ ):
73+
+ return self.type.cast_code("_PyArray_Data((PyObject*)%s)" % obj_code)
74+
return "%s%s%s" % (obj_code, self.op, self.member)
75+
76+
def generate_result_code(self, code):
77+
diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
78+
index 5f088dd..a258bdc 100644
79+
--- a/Cython/Compiler/Symtab.py
80+
+++ b/Cython/Compiler/Symtab.py
81+
@@ -243,6 +243,7 @@ class Entry(object):
82+
pytyping_modifiers = None
83+
enum_int_value = None
84+
vtable_type = None
85+
+ getter = None
86+
87+
def __init__(self, name, cname, type, pos = None, init = None):
88+
self.name = name
89+
@@ -510,7 +511,7 @@ class Scope(object):
90+
yield
91+
self.in_c_type_context = old_c_type_context
92+
93+
- def declare(self, name, cname, type, pos, visibility, shadow = 0, is_type = 0, create_wrapper = 0):
94+
+ def declare(self, name, cname, type, pos, visibility, shadow = 0, is_type = 0, create_wrapper = 0, getter = 0):
95+
# Create new entry, and add to dictionary if
96+
# name is not None. Reports a warning if already
97+
# declared.
98+
@@ -550,6 +551,7 @@ class Scope(object):
99+
error(pos, "'%s' redeclared " % name)
100+
entries[name].already_declared_here()
101+
entry = Entry(name, cname, type, pos = pos)
102+
+ entry.getter = getter
103+
entry.in_cinclude = self.in_cinclude
104+
entry.create_wrapper = create_wrapper
105+
if name:
106+
diff --git a/Cython/Includes/cpython/datetime.pxd b/Cython/Includes/cpython/datetime.pxd
107+
index 3dce395..77f8356 100644
108+
--- a/Cython/Includes/cpython/datetime.pxd
109+
+++ b/Cython/Includes/cpython/datetime.pxd
110+
@@ -404,15 +404,15 @@ cdef inline int datetime_fold(object o) noexcept:
111+
112+
# Get days of timedelta
113+
cdef inline int timedelta_days(object o) noexcept:
114+
- return (<PyDateTime_Delta*>o).days
115+
+ return PyDateTime_DELTA_GET_DAYS(o)
116+
117+
# Get seconds of timedelta
118+
cdef inline int timedelta_seconds(object o) noexcept:
119+
- return (<PyDateTime_Delta*>o).seconds
120+
+ return PyDateTime_DELTA_GET_SECONDS(o)
121+
122+
# Get microseconds of timedelta
123+
cdef inline int timedelta_microseconds(object o) noexcept:
124+
- return (<PyDateTime_Delta*>o).microseconds
125+
+ return PyDateTime_DELTA_GET_MICROSECONDS(o)
126+
127+
cdef inline double total_seconds(timedelta obj) noexcept:
128+
# Mirrors the "timedelta.total_seconds()" method.
129+
diff --git a/Cython/Includes/cpython/slice.pxd b/Cython/Includes/cpython/slice.pxd
130+
index 202dea7..1e83e66 100644
131+
--- a/Cython/Includes/cpython/slice.pxd
132+
+++ b/Cython/Includes/cpython/slice.pxd
133+
@@ -48,6 +48,13 @@ cdef extern from "Python.h":
134+
135+
int PySlice_Unpack(object slice, Py_ssize_t *start, Py_ssize_t *stop,
136+
Py_ssize_t *step) except -1
137+
+
138+
+ object PySlice_Start(object slice)
139+
+
140+
+ object PySlice_Stop(object slice)
141+
+
142+
+ object PySlice_Step(object slice)
143+
+
144+
# Extract the start, stop and step data members from a slice object as C
145+
# integers. Silently reduce values larger than PY_SSIZE_T_MAX to
146+
# PY_SSIZE_T_MAX, silently boost the start and stop values less than
147+
diff --git a/Cython/Utility/Complex.c b/Cython/Utility/Complex.c
148+
index c95511d..a68a884 100644
149+
--- a/Cython/Utility/Complex.c
150+
+++ b/Cython/Utility/Complex.c
151+
@@ -134,7 +134,7 @@ static {{type}} __Pyx_PyComplex_As_{{type_name}}(PyObject*);
152+
153+
static {{type}} __Pyx_PyComplex_As_{{type_name}}(PyObject* o) {
154+
Py_complex cval;
155+
-#if !CYTHON_COMPILING_IN_PYPY
156+
+#if !CYTHON_COMPILING_IN_PYPY && !CYTHON_COMPILING_IN_GRAAL
157+
if (PyComplex_CheckExact(o))
158+
cval = ((PyComplexObject *)o)->cval;
159+
else
160+
diff --git a/Cython/Utility/Coroutine.c b/Cython/Utility/Coroutine.c
161+
index 35f2e88..30b2d75 100644
162+
--- a/Cython/Utility/Coroutine.c
163+
+++ b/Cython/Utility/Coroutine.c
164+
@@ -539,8 +539,7 @@ static int __Pyx_PyGen__FetchStopIterationValue(PyThreadState *$local_tstate_cna
165+
}
166+
#if PY_VERSION_HEX >= 0x030300A0
167+
else if (likely(__Pyx_IS_TYPE(ev, (PyTypeObject*)PyExc_StopIteration))) {
168+
- value = ((PyStopIterationObject *)ev)->value;
169+
- Py_INCREF(value);
170+
+ value = PyObject_GetAttrString(ev, "value");
171+
Py_DECREF(ev);
172+
}
173+
#endif
174+
@@ -585,8 +584,7 @@ static int __Pyx_PyGen__FetchStopIterationValue(PyThreadState *$local_tstate_cna
175+
Py_XDECREF(tb);
176+
Py_DECREF(et);
177+
#if PY_VERSION_HEX >= 0x030300A0
178+
- value = ((PyStopIterationObject *)ev)->value;
179+
- Py_INCREF(value);
180+
+ value = PyObject_GetAttrString(ev, "value");
181+
Py_DECREF(ev);
182+
#else
183+
{
184+
diff --git a/Cython/Utility/CythonFunction.c b/Cython/Utility/CythonFunction.c
185+
index 3ea60f5..18d1842 100644
186+
--- a/Cython/Utility/CythonFunction.c
187+
+++ b/Cython/Utility/CythonFunction.c
188+
@@ -1789,9 +1789,9 @@ static PyObject* __Pyx_Method_ClassMethod(PyObject *method) {
189+
#if PY_VERSION_HEX < 0x03020000
190+
PyTypeObject *d_type = descr->d_type;
191+
#else
192+
- PyTypeObject *d_type = descr->d_common.d_type;
193+
+ PyTypeObject *d_type = PyDescrObject_GetType(method);
194+
#endif
195+
- return PyDescr_NewClassMethod(d_type, descr->d_method);
196+
+ return PyDescr_NewClassMethod(d_type, PyMethodDescrObject_GetMethod(method));
197+
}
198+
#endif
199+
else if (PyMethod_Check(method)) {
200+
diff --git a/Cython/Utility/ModuleSetupCode.c b/Cython/Utility/ModuleSetupCode.c
201+
index d8f60a4..8ede926 100644
202+
--- a/Cython/Utility/ModuleSetupCode.c
203+
+++ b/Cython/Utility/ModuleSetupCode.c
204+
@@ -995,7 +995,7 @@ static CYTHON_INLINE int __Pyx__IsSameCFunction(PyObject *func, void *cfunc) {
205+
#define __Pyx_PyFrame_SetLineNumber(frame, lineno)
206+
#else
207+
#define __Pyx_PyCode_HasFreeVars(co) (PyCode_GetNumFree(co) > 0)
208+
- #define __Pyx_PyFrame_SetLineNumber(frame, lineno) (frame)->f_lineno = (lineno)
209+
+ #define __Pyx_PyFrame_SetLineNumber(frame, lineno) _PyFrame_SetLineNumber((frame), (lineno))
210+
#endif
211+
212+
#if CYTHON_COMPILING_IN_LIMITED_API
213+
@@ -1413,11 +1413,7 @@ static CYTHON_INLINE PyObject * __Pyx_PyDict_GetItemStrWithError(PyObject *dict,
214+
#define __Pyx_PyType_AsAsync(obj) NULL
215+
#endif
216+
#ifndef __Pyx_PyAsyncMethodsStruct
217+
- typedef struct {
218+
- unaryfunc am_await;
219+
- unaryfunc am_aiter;
220+
- unaryfunc am_anext;
221+
- } __Pyx_PyAsyncMethodsStruct;
222+
+ #define __Pyx_PyAsyncMethodsStruct PyAsyncMethods
223+
#endif
224+
225+
226+
@@ -1678,7 +1674,7 @@ PyEval_InitThreads();
227+
static CYTHON_SMALL_CODE int __Pyx_check_single_interpreter(void) {
228+
#if PY_VERSION_HEX >= 0x030700A1
229+
static PY_INT64_T main_interpreter_id = -1;
230+
- PY_INT64_T current_id = PyInterpreterState_GetID(PyThreadState_Get()->interp);
231+
+ PY_INT64_T current_id = PyInterpreterState_GetIDFromThreadState(PyThreadState_Get());
232+
if (main_interpreter_id == -1) {
233+
main_interpreter_id = current_id;
234+
return (unlikely(current_id == -1)) ? -1 : 0;
235+
diff --git a/Cython/Utility/ObjectHandling.c b/Cython/Utility/ObjectHandling.c
236+
index 1021edf..6139fd2 100644
237+
--- a/Cython/Utility/ObjectHandling.c
238+
+++ b/Cython/Utility/ObjectHandling.c
239+
@@ -2034,8 +2034,8 @@ static int __Pyx_TryUnpackUnboundCMethod(__Pyx_CachedCFunction* target) {
240+
#endif
241+
{
242+
PyMethodDescrObject *descr = (PyMethodDescrObject*) method;
243+
- target->func = descr->d_method->ml_meth;
244+
- target->flag = descr->d_method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_STACKLESS);
245+
+ target->func = PyMethodDescrObject_GetMethod(method)->ml_meth;
246+
+ target->flag = PyMethodDescrObject_GetMethod(method)->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_STACKLESS);
247+
} else
248+
#endif
249+
// bound classmethods need special treatment
250+
diff --git a/Cython/Utility/StringTools.c b/Cython/Utility/StringTools.c
251+
index 0a50bc5..f93b3cc 100644
252+
--- a/Cython/Utility/StringTools.c
253+
+++ b/Cython/Utility/StringTools.c
254+
@@ -234,7 +234,7 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
255+
//@requires: BytesEquals
256+
257+
static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals) {
258+
-#if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API
259+
+#if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_GRAAL || CYTHON_COMPILING_IN_LIMITED_API
260+
return PyObject_RichCompareBool(s1, s2, equals);
261+
#else
262+
#if PY_MAJOR_VERSION < 3
263+
@@ -345,7 +345,7 @@ static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int eq
264+
//@requires: IncludeStringH
265+
266+
static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals) {
267+
-#if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API
268+
+#if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_GRAAL || CYTHON_COMPILING_IN_LIMITED_API
269+
return PyObject_RichCompareBool(s1, s2, equals);
270+
#else
271+
if (s1 == s2) {
272+
diff --git a/Cython/Utility/arrayarray.h b/Cython/Utility/arrayarray.h
273+
index a9e4923..cf704c9 100644
274+
--- a/Cython/Utility/arrayarray.h
275+
+++ b/Cython/Utility/arrayarray.h
276+
@@ -32,30 +32,31 @@ typedef struct arraydescr {
277+
#endif
278+
} arraydescr;
279+
280+
+typedef union {
281+
+ char *ob_item;
282+
+ float *as_floats;
283+
+ double *as_doubles;
284+
+ int *as_ints;
285+
+ unsigned int *as_uints;
286+
+ unsigned char *as_uchars;
287+
+ signed char *as_schars;
288+
+ char *as_chars;
289+
+ unsigned long *as_ulongs;
290+
+ long *as_longs;
291+
+#if PY_MAJOR_VERSION >= 3
292+
+ unsigned long long *as_ulonglongs;
293+
+ long long *as_longlongs;
294+
+#endif
295+
+ short *as_shorts;
296+
+ unsigned short *as_ushorts;
297+
+ Py_UNICODE *as_pyunicodes;
298+
+ void *as_voidptr;
299+
+} __data_union
300+
301+
struct arrayobject {
302+
PyObject_HEAD
303+
Py_ssize_t ob_size;
304+
- union {
305+
- char *ob_item;
306+
- float *as_floats;
307+
- double *as_doubles;
308+
- int *as_ints;
309+
- unsigned int *as_uints;
310+
- unsigned char *as_uchars;
311+
- signed char *as_schars;
312+
- char *as_chars;
313+
- unsigned long *as_ulongs;
314+
- long *as_longs;
315+
-#if PY_MAJOR_VERSION >= 3
316+
- unsigned long long *as_ulonglongs;
317+
- long long *as_longlongs;
318+
-#endif
319+
- short *as_shorts;
320+
- unsigned short *as_ushorts;
321+
- Py_UNICODE *as_pyunicodes;
322+
- void *as_voidptr;
323+
- } data;
324+
+ __data_union data;
325+
Py_ssize_t allocated;
326+
struct arraydescr *ob_descr;
327+
PyObject *weakreflist; /* List of weak references */
328+
@@ -109,6 +110,9 @@ PyObject* newarrayobject(PyTypeObject *type, Py_ssize_t size,
329+
// fast resize (reallocation to the point)
330+
// not designed for filing small increments (but for fast opaque array apps)
331+
static CYTHON_INLINE int resize(arrayobject *self, Py_ssize_t n) {
332+
+#ifdef CYTHON_COMPILING_IN_GRAAL
333+
+ return _PyArray_Resize((PyObject*)self, n);
334+
+#else
335+
void *items = (void*) self->data.ob_item;
336+
PyMem_Resize(items, char, (size_t)(n * self->ob_descr->itemsize));
337+
if (items == NULL) {
338+
@@ -119,10 +123,14 @@ static CYTHON_INLINE int resize(arrayobject *self, Py_ssize_t n) {
339+
__Pyx_SET_SIZE(self, n);
340+
self->allocated = n;
341+
return 0;
342+
+#endif
343+
}
344+
345+
// suitable for small increments; over allocation 50% ;
346+
static CYTHON_INLINE int resize_smart(arrayobject *self, Py_ssize_t n) {
347+
+#ifdef CYTHON_COMPILING_IN_GRAAL
348+
+ return _PyArray_Resize((PyObject*)self, n);
349+
+#else
350+
void *items = (void*) self->data.ob_item;
351+
Py_ssize_t newsize;
352+
if (n < self->allocated && n*4 > self->allocated) {
353+
@@ -143,6 +151,7 @@ static CYTHON_INLINE int resize_smart(arrayobject *self, Py_ssize_t n) {
354+
__Pyx_SET_SIZE(self, n);
355+
self->allocated = newsize;
356+
return 0;
357+
+#endif
358+
}
359+
360+
#endif

graalpython/lib-graalpython/patches/Cython/metadata.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
[[rules]]
2+
version = '== 3.0.10'
3+
patch = 'Cython-3.0.10.patch'
4+
5+
# Prefer pre-3 version for now
6+
[[rules]]
27
version = '== 0.29.37'
38
patch = 'Cython-0.29.37.patch'
9+
install-priority = 2
410

511
[[rules]]
612
version = '== 0.29.32'

0 commit comments

Comments
 (0)