Skip to content

Commit 1cd0353

Browse files
msimacekdavidhewitt
authored andcommitted
Fix struct layouts on GraalPy (#4802)
* Fix struct layouts on GraalPy * Add changelog item
1 parent 5a666ec commit 1cd0353

16 files changed

+46
-56
lines changed

newsfragments/4802.fixed.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed missing struct fields on GraalPy when subclassing builtin classes

pyo3-ffi/src/abstract_.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_
1717
extern "C" {
1818
#[cfg(all(
1919
not(PyPy),
20-
not(GraalPy),
2120
any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10
2221
))]
2322
#[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")]

pyo3-ffi/src/cpython/abstract_.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{PyObject, Py_ssize_t};
2-
use std::os::raw::{c_char, c_int};
2+
#[cfg(not(all(Py_3_11, GraalPy)))]
3+
use std::os::raw::c_char;
4+
use std::os::raw::c_int;
35

46
#[cfg(not(Py_3_11))]
57
use crate::Py_buffer;

pyo3-ffi/src/cpython/complexobject.rs

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub struct Py_complex {
1919
#[repr(C)]
2020
pub struct PyComplexObject {
2121
pub ob_base: PyObject,
22-
#[cfg(not(GraalPy))]
2322
pub cval: Py_complex,
2423
}
2524

pyo3-ffi/src/cpython/floatobject.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::os::raw::c_double;
66
#[repr(C)]
77
pub struct PyFloatObject {
88
pub ob_base: PyObject,
9-
#[cfg(not(GraalPy))]
109
pub ob_fval: c_double,
1110
}
1211

pyo3-ffi/src/cpython/genobject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::object::*;
22
use crate::PyFrameObject;
33
#[cfg(not(any(PyPy, GraalPy)))]
44
use crate::_PyErr_StackItem;
5-
#[cfg(Py_3_11)]
5+
#[cfg(all(Py_3_11, not(GraalPy)))]
66
use std::os::raw::c_char;
77
use std::os::raw::c_int;
88
use std::ptr::addr_of_mut;

pyo3-ffi/src/cpython/listobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use crate::object::*;
22
#[cfg(not(PyPy))]
33
use crate::pyport::Py_ssize_t;
44

5-
#[cfg(not(any(PyPy, GraalPy)))]
5+
#[cfg(not(PyPy))]
66
#[repr(C)]
77
pub struct PyListObject {
88
pub ob_base: PyVarObject,
99
pub ob_item: *mut *mut PyObject,
1010
pub allocated: Py_ssize_t,
1111
}
1212

13-
#[cfg(any(PyPy, GraalPy))]
13+
#[cfg(PyPy)]
1414
pub struct PyListObject {
1515
pub ob_base: PyObject,
1616
}

pyo3-ffi/src/cpython/object.rs

-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ pub type printfunc =
211211
#[derive(Debug)]
212212
pub struct PyTypeObject {
213213
pub ob_base: object::PyVarObject,
214-
#[cfg(GraalPy)]
215-
pub ob_size: Py_ssize_t,
216214
pub tp_name: *const c_char,
217215
pub tp_basicsize: Py_ssize_t,
218216
pub tp_itemsize: Py_ssize_t,

pyo3-ffi/src/cpython/objimpl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(not(all(Py_3_11, GraalPy)))]
12
use libc::size_t;
23
use std::os::raw::c_int;
34

pyo3-ffi/src/cpython/pyerrors.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ use crate::Py_ssize_t;
66
#[derive(Debug)]
77
pub struct PyBaseExceptionObject {
88
pub ob_base: PyObject,
9-
#[cfg(not(any(PyPy, GraalPy)))]
9+
#[cfg(not(PyPy))]
1010
pub dict: *mut PyObject,
11-
#[cfg(not(any(PyPy, GraalPy)))]
11+
#[cfg(not(PyPy))]
1212
pub args: *mut PyObject,
13-
#[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))]
13+
#[cfg(all(Py_3_11, not(PyPy)))]
1414
pub notes: *mut PyObject,
15-
#[cfg(not(any(PyPy, GraalPy)))]
15+
#[cfg(not(PyPy))]
1616
pub traceback: *mut PyObject,
17-
#[cfg(not(any(PyPy, GraalPy)))]
17+
#[cfg(not(PyPy))]
1818
pub context: *mut PyObject,
19-
#[cfg(not(any(PyPy, GraalPy)))]
19+
#[cfg(not(PyPy))]
2020
pub cause: *mut PyObject,
21-
#[cfg(not(any(PyPy, GraalPy)))]
21+
#[cfg(not(PyPy))]
2222
pub suppress_context: char,
2323
}
2424

@@ -134,19 +134,19 @@ pub struct PyOSErrorObject {
134134
#[derive(Debug)]
135135
pub struct PyStopIterationObject {
136136
pub ob_base: PyObject,
137-
#[cfg(not(any(PyPy, GraalPy)))]
137+
#[cfg(not(PyPy))]
138138
pub dict: *mut PyObject,
139-
#[cfg(not(any(PyPy, GraalPy)))]
139+
#[cfg(not(PyPy))]
140140
pub args: *mut PyObject,
141-
#[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))]
141+
#[cfg(all(Py_3_11, not(PyPy)))]
142142
pub notes: *mut PyObject,
143-
#[cfg(not(any(PyPy, GraalPy)))]
143+
#[cfg(not(PyPy))]
144144
pub traceback: *mut PyObject,
145-
#[cfg(not(any(PyPy, GraalPy)))]
145+
#[cfg(not(PyPy))]
146146
pub context: *mut PyObject,
147-
#[cfg(not(any(PyPy, GraalPy)))]
147+
#[cfg(not(PyPy))]
148148
pub cause: *mut PyObject,
149-
#[cfg(not(any(PyPy, GraalPy)))]
149+
#[cfg(not(PyPy))]
150150
pub suppress_context: char,
151151

152152
pub value: *mut PyObject,

pyo3-ffi/src/cpython/tupleobject.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::pyport::Py_ssize_t;
55
#[repr(C)]
66
pub struct PyTupleObject {
77
pub ob_base: PyVarObject,
8-
#[cfg(not(GraalPy))]
98
pub ob_item: [*mut PyObject; 1],
109
}
1110

pyo3-ffi/src/cpython/unicodeobject.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(not(any(PyPy, GraalPy)))]
1+
#[cfg(not(PyPy))]
22
use crate::Py_hash_t;
33
use crate::{PyObject, Py_UCS1, Py_UCS2, Py_UCS4, Py_ssize_t};
44
use libc::wchar_t;
@@ -250,9 +250,8 @@ impl From<PyASCIIObjectState> for u32 {
250250
#[repr(C)]
251251
pub struct PyASCIIObject {
252252
pub ob_base: PyObject,
253-
#[cfg(not(GraalPy))]
254253
pub length: Py_ssize_t,
255-
#[cfg(not(any(PyPy, GraalPy)))]
254+
#[cfg(not(PyPy))]
256255
pub hash: Py_hash_t,
257256
/// A bit field with various properties.
258257
///
@@ -265,9 +264,8 @@ pub struct PyASCIIObject {
265264
/// unsigned int ascii:1;
266265
/// unsigned int ready:1;
267266
/// unsigned int :24;
268-
#[cfg(not(GraalPy))]
269267
pub state: u32,
270-
#[cfg(not(any(Py_3_12, GraalPy)))]
268+
#[cfg(not(Py_3_12))]
271269
pub wstr: *mut wchar_t,
272270
}
273271

@@ -379,11 +377,9 @@ impl PyASCIIObject {
379377
#[repr(C)]
380378
pub struct PyCompactUnicodeObject {
381379
pub _base: PyASCIIObject,
382-
#[cfg(not(GraalPy))]
383380
pub utf8_length: Py_ssize_t,
384-
#[cfg(not(GraalPy))]
385381
pub utf8: *mut c_char,
386-
#[cfg(not(any(Py_3_12, GraalPy)))]
382+
#[cfg(not(Py_3_12))]
387383
pub wstr_length: Py_ssize_t,
388384
}
389385

@@ -398,7 +394,6 @@ pub union PyUnicodeObjectData {
398394
#[repr(C)]
399395
pub struct PyUnicodeObject {
400396
pub _base: PyCompactUnicodeObject,
401-
#[cfg(not(GraalPy))]
402397
pub data: PyUnicodeObjectData,
403398
}
404399

pyo3-ffi/src/datetime.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ use crate::PyCapsule_Import;
99
#[cfg(GraalPy)]
1010
use crate::{PyLong_AsLong, PyLong_Check, PyObject_GetAttrString, Py_DecRef};
1111
use crate::{PyObject, PyObject_TypeCheck, PyTypeObject, Py_TYPE};
12-
#[cfg(not(GraalPy))]
1312
use std::os::raw::c_char;
1413
use std::os::raw::c_int;
1514
use std::ptr;
1615
use std::sync::Once;
1716
use std::{cell::UnsafeCell, ffi::CStr};
18-
#[cfg(not(any(PyPy, GraalPy)))]
17+
#[cfg(not(PyPy))]
1918
use {crate::Py_hash_t, std::os::raw::c_uchar};
2019
// Type struct wrappers
2120
const _PyDateTime_DATE_DATASIZE: usize = 4;
@@ -27,13 +26,10 @@ const _PyDateTime_DATETIME_DATASIZE: usize = 10;
2726
/// Structure representing a `datetime.timedelta`.
2827
pub struct PyDateTime_Delta {
2928
pub ob_base: PyObject,
30-
#[cfg(not(any(PyPy, GraalPy)))]
29+
#[cfg(not(PyPy))]
3130
pub hashcode: Py_hash_t,
32-
#[cfg(not(GraalPy))]
3331
pub days: c_int,
34-
#[cfg(not(GraalPy))]
3532
pub seconds: c_int,
36-
#[cfg(not(GraalPy))]
3733
pub microseconds: c_int,
3834
}
3935

@@ -56,19 +52,17 @@ pub struct _PyDateTime_BaseTime {
5652
/// Structure representing a `datetime.time`.
5753
pub struct PyDateTime_Time {
5854
pub ob_base: PyObject,
59-
#[cfg(not(any(PyPy, GraalPy)))]
55+
#[cfg(not(PyPy))]
6056
pub hashcode: Py_hash_t,
61-
#[cfg(not(GraalPy))]
6257
pub hastzinfo: c_char,
63-
#[cfg(not(any(PyPy, GraalPy)))]
58+
#[cfg(not(PyPy))]
6459
pub data: [c_uchar; _PyDateTime_TIME_DATASIZE],
65-
#[cfg(not(any(PyPy, GraalPy)))]
60+
#[cfg(not(PyPy))]
6661
pub fold: c_uchar,
6762
/// # Safety
6863
///
6964
/// Care should be taken when reading this field. If the time does not have a
7065
/// tzinfo then CPython may allocate as a `_PyDateTime_BaseTime` without this field.
71-
#[cfg(not(GraalPy))]
7266
pub tzinfo: *mut PyObject,
7367
}
7468

@@ -77,11 +71,11 @@ pub struct PyDateTime_Time {
7771
/// Structure representing a `datetime.date`
7872
pub struct PyDateTime_Date {
7973
pub ob_base: PyObject,
80-
#[cfg(not(any(PyPy, GraalPy)))]
74+
#[cfg(not(PyPy))]
8175
pub hashcode: Py_hash_t,
82-
#[cfg(not(any(PyPy, GraalPy)))]
76+
#[cfg(not(PyPy))]
8377
pub hastzinfo: c_char,
84-
#[cfg(not(any(PyPy, GraalPy)))]
78+
#[cfg(not(PyPy))]
8579
pub data: [c_uchar; _PyDateTime_DATE_DATASIZE],
8680
}
8781

@@ -101,19 +95,17 @@ pub struct _PyDateTime_BaseDateTime {
10195
/// Structure representing a `datetime.datetime`.
10296
pub struct PyDateTime_DateTime {
10397
pub ob_base: PyObject,
104-
#[cfg(not(any(PyPy, GraalPy)))]
98+
#[cfg(not(PyPy))]
10599
pub hashcode: Py_hash_t,
106-
#[cfg(not(GraalPy))]
107100
pub hastzinfo: c_char,
108-
#[cfg(not(any(PyPy, GraalPy)))]
101+
#[cfg(not(PyPy))]
109102
pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE],
110-
#[cfg(not(any(PyPy, GraalPy)))]
103+
#[cfg(not(PyPy))]
111104
pub fold: c_uchar,
112105
/// # Safety
113106
///
114107
/// Care should be taken when reading this field. If the time does not have a
115108
/// tzinfo then CPython may allocate as a `_PyDateTime_BaseDateTime` without this field.
116-
#[cfg(not(GraalPy))]
117109
pub tzinfo: *mut PyObject,
118110
}
119111

pyo3-ffi/src/object.rs

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ pub struct PyVarObject {
129129
pub ob_base: PyObject,
130130
#[cfg(not(GraalPy))]
131131
pub ob_size: Py_ssize_t,
132+
// On GraalPy the field is physically there, but not always populated. We hide it to prevent accidental misuse
133+
#[cfg(GraalPy)]
134+
pub _ob_size_graalpy: Py_ssize_t,
132135
}
133136

134137
// skipped private _PyVarObject_CAST

pyo3-ffi/src/pyhash.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
1+
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
22
use crate::pyport::{Py_hash_t, Py_ssize_t};
33
#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
4-
use std::os::raw::{c_char, c_void};
4+
use std::os::raw::c_char;
5+
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
6+
use std::os::raw::c_void;
57

68
use std::os::raw::{c_int, c_ulong};
79

@@ -10,7 +12,7 @@ extern "C" {
1012
// skipped non-limited _Py_HashPointer
1113
// skipped non-limited _Py_HashPointerRaw
1214

13-
#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
15+
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
1416
pub fn _Py_HashBytes(src: *const c_void, len: Py_ssize_t) -> Py_hash_t;
1517
}
1618

src/types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use self::float::{PyFloat, PyFloatMethods};
2323
pub use self::frame::PyFrame;
2424
pub use self::frozenset::{PyFrozenSet, PyFrozenSetBuilder, PyFrozenSetMethods};
2525
pub use self::function::PyCFunction;
26-
#[cfg(all(not(Py_LIMITED_API), not(all(PyPy, not(Py_3_8))), not(GraalPy)))]
26+
#[cfg(all(not(Py_LIMITED_API), not(all(PyPy, not(Py_3_8)))))]
2727
pub use self::function::PyFunction;
2828
pub use self::iterator::PyIterator;
2929
pub use self::list::{PyList, PyListMethods};

0 commit comments

Comments
 (0)