Skip to content

Commit

Permalink
graalpy: take care only to expose C structure that GraalPy allocates
Browse files Browse the repository at this point in the history
  • Loading branch information
timfel committed Mar 25, 2024
1 parent 7e9987e commit 765ae0a
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 103 deletions.
4 changes: 2 additions & 2 deletions pyo3-ffi/src/bytearrayobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::pyport::Py_ssize_t;
use std::os::raw::{c_char, c_int};
use std::ptr::addr_of_mut;

#[cfg(not(any(PyPy, Py_LIMITED_API)))]
#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyByteArrayObject {
Expand All @@ -17,7 +17,7 @@ pub struct PyByteArrayObject {
pub ob_exports: c_int,
}

#[cfg(any(PyPy, Py_LIMITED_API))]
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
opaque_struct!(PyByteArrayObject);

#[cfg_attr(windows, link(name = "pythonXY"))]
Expand Down
1 change: 1 addition & 0 deletions pyo3-ffi/src/complexobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" {
// non-limited
pub struct PyComplexObject {
pub ob_base: PyObject,
#[cfg(not(GraalPy))]
pub cval: Py_complex,
}

Expand Down
6 changes: 3 additions & 3 deletions pyo3-ffi/src/cpython/bytesobject.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::object::*;
use crate::Py_ssize_t;
#[cfg(not(any(PyPy, Py_LIMITED_API)))]
#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
use std::os::raw::c_char;
use std::os::raw::c_int;

#[cfg(not(any(PyPy, Py_LIMITED_API)))]
#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyBytesObject {
Expand All @@ -13,7 +13,7 @@ pub struct PyBytesObject {
pub ob_sval: [c_char; 1],
}

#[cfg(any(PyPy, Py_LIMITED_API))]
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
opaque_struct!(PyBytesObject);

extern "C" {
Expand Down
24 changes: 14 additions & 10 deletions pyo3-ffi/src/cpython/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::pyport::Py_ssize_t;

#[allow(unused_imports)]
use std::os::raw::{c_char, c_int, c_short, c_uchar, c_void};
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
use std::ptr::addr_of_mut;

#[cfg(all(Py_3_8, not(PyPy), not(Py_3_11)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy)), not(Py_3_11)))]
opaque_struct!(_PyOpcache);

#[cfg(Py_3_12)]
Expand Down Expand Up @@ -73,10 +73,10 @@ pub struct _PyCoMonitoringData {
pub per_instruction_tools: *mut u8,
}

#[cfg(all(not(PyPy), not(Py_3_7)))]
#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_7)))]
opaque_struct!(PyCodeObject);

#[cfg(all(not(PyPy), Py_3_7, not(Py_3_8)))]
#[cfg(all(not(any(PyPy, GraalPy)), Py_3_7, not(Py_3_8)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyCodeObject {
Expand All @@ -102,7 +102,7 @@ pub struct PyCodeObject {
pub co_extra: *mut c_void,
}

#[cfg(all(not(PyPy), Py_3_8, not(Py_3_11)))]
#[cfg(all(not(any(PyPy, GraalPy)), Py_3_8, not(Py_3_11)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyCodeObject {
Expand Down Expand Up @@ -136,7 +136,7 @@ pub struct PyCodeObject {
pub co_opcache_size: c_uchar,
}

#[cfg(all(not(PyPy), Py_3_11))]
#[cfg(all(not(any(PyPy, GraalPy)), Py_3_11))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyCodeObject {
Expand Down Expand Up @@ -230,26 +230,26 @@ pub const CO_FUTURE_GENERATOR_STOP: c_int = 0x8_0000;

pub const CO_MAXBLOCKS: usize = 20;

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
pub static mut PyCode_Type: PyTypeObject;
}

#[inline]
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == addr_of_mut!(PyCode_Type)) as c_int
}

#[inline]
#[cfg(all(not(PyPy), Py_3_10, not(Py_3_11)))]
#[cfg(all(not(any(PyPy, GraalPy)), Py_3_10, not(Py_3_11)))]
pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t {
crate::PyTuple_GET_SIZE((*op).co_freevars)
}

#[inline]
#[cfg(all(not(Py_3_10), Py_3_11, not(PyPy)))]
#[cfg(all(not(Py_3_10), Py_3_11, not(any(PyPy, GraalPy))))]
pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> c_int {
(*op).co_nfreevars
}
Expand All @@ -265,6 +265,7 @@ extern "C" {
}

extern "C" {
#[cfg(not(GraalPy))]
#[cfg_attr(PyPy, link_name = "PyPyCode_New")]
pub fn PyCode_New(
argcount: c_int,
Expand All @@ -283,6 +284,7 @@ extern "C" {
firstlineno: c_int,
lnotab: *mut PyObject,
) -> *mut PyCodeObject;
#[cfg(not(GraalPy))]
#[cfg(Py_3_8)]
pub fn PyCode_NewWithPosOnlyArgs(
argcount: c_int,
Expand All @@ -302,12 +304,14 @@ extern "C" {
firstlineno: c_int,
lnotab: *mut PyObject,
) -> *mut PyCodeObject;
#[cfg(not(GraalPy))]
#[cfg_attr(PyPy, link_name = "PyPyCode_NewEmpty")]
pub fn PyCode_NewEmpty(
filename: *const c_char,
funcname: *const c_char,
firstlineno: c_int,
) -> *mut PyCodeObject;
#[cfg(not(GraalPy))]
pub fn PyCode_Addr2Line(arg1: *mut PyCodeObject, arg2: c_int) -> c_int;
// skipped PyCodeAddressRange "for internal use only"
// skipped _PyCode_CheckLineNumber
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/cpython/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct PyCompilerFlags {

// skipped non-limited _PyCompilerFlags_INIT

#[cfg(all(Py_3_12, not(PyPy)))]
#[cfg(all(Py_3_12, not(any(PyPy, GraalPy))))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct _PyCompilerSrcLocation {
Expand All @@ -42,7 +42,7 @@ pub struct _PyCompilerSrcLocation {

// skipped SRC_LOCATION_FROM_AST

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyFutureFeatures {
Expand Down
1 change: 1 addition & 0 deletions pyo3-ffi/src/cpython/dictobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ opaque_struct!(PyDictKeysObject);
#[cfg(Py_3_11)]
opaque_struct!(PyDictValues);

#[cfg(not(GraalPy))]
#[repr(C)]
#[derive(Debug)]
pub struct PyDictObject {
Expand Down
9 changes: 7 additions & 2 deletions pyo3-ffi/src/cpython/funcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr::addr_of_mut;

use crate::PyObject;

#[cfg(all(not(PyPy), not(Py_3_10)))]
#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_10)))]
#[repr(C)]
pub struct PyFunctionObject {
pub ob_base: PyObject,
Expand All @@ -24,7 +24,7 @@ pub struct PyFunctionObject {
pub vectorcall: Option<crate::vectorcallfunc>,
}

#[cfg(all(not(PyPy), Py_3_10))]
#[cfg(all(not(any(PyPy, GraalPy)), Py_3_10))]
#[repr(C)]
pub struct PyFunctionObject {
pub ob_base: PyObject,
Expand Down Expand Up @@ -55,6 +55,11 @@ pub struct PyFunctionObject {
pub func_name: *mut PyObject,
}

#[cfg(GraalPy)]
pub struct PyFunctionObject {
pub ob_base: PyObject,
}

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg(not(all(PyPy, not(Py_3_8))))]
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/cpython/genobject.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::object::*;
use crate::PyFrameObject;
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
use crate::_PyErr_StackItem;
#[cfg(Py_3_11)]
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::ptr::addr_of_mut;

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyGenObject {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ pub use self::pystate::*;
pub use self::pythonrun::*;
pub use self::tupleobject::*;
pub use self::unicodeobject::*;
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub use self::weakrefobject::*;
2 changes: 2 additions & 0 deletions pyo3-ffi/src/cpython/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ pub struct PyTypeObject {
pub ob_size: Py_ssize_t,
#[cfg(not(all(PyPy, not(Py_3_9))))]
pub ob_base: object::PyVarObject,
#[cfg(GraalPy)]
pub ob_size: Py_ssize_t,
pub tp_name: *const c_char,
pub tp_basicsize: Py_ssize_t,
pub tp_itemsize: Py_ssize_t,
Expand Down
10 changes: 5 additions & 5 deletions pyo3-ffi/src/cpython/objimpl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc::size_t;
use std::os::raw::c_int;

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
use std::os::raw::c_void;

use crate::object::*;
Expand All @@ -14,7 +14,7 @@ extern "C" {
pub fn _Py_GetAllocatedBlocks() -> crate::Py_ssize_t;
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyObjectArenaAllocator {
Expand All @@ -23,7 +23,7 @@ pub struct PyObjectArenaAllocator {
pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, size: size_t)>,
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
impl Default for PyObjectArenaAllocator {
#[inline]
fn default() -> Self {
Expand All @@ -32,9 +32,9 @@ impl Default for PyObjectArenaAllocator {
}

extern "C" {
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub fn PyObject_GetArenaAllocator(allocator: *mut PyObjectArenaAllocator);
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub fn PyObject_SetArenaAllocator(allocator: *mut PyObjectArenaAllocator);

#[cfg(Py_3_9)]
Expand Down
42 changes: 21 additions & 21 deletions pyo3-ffi/src/cpython/pyerrors.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use crate::PyObject;
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
use crate::Py_ssize_t;

#[repr(C)]
#[derive(Debug)]
pub struct PyBaseExceptionObject {
pub ob_base: PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub dict: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub args: *mut PyObject,
#[cfg(all(Py_3_11, not(PyPy)))]
#[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))]
pub notes: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub traceback: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub context: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub cause: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub suppress_context: char,
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Debug)]
pub struct PySyntaxErrorObject {
Expand All @@ -48,7 +48,7 @@ pub struct PySyntaxErrorObject {
pub print_file_and_line: *mut PyObject,
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Debug)]
pub struct PyImportErrorObject {
Expand All @@ -69,7 +69,7 @@ pub struct PyImportErrorObject {
pub name_from: *mut PyObject,
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Debug)]
pub struct PyUnicodeErrorObject {
Expand All @@ -90,7 +90,7 @@ pub struct PyUnicodeErrorObject {
pub reason: *mut PyObject,
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Debug)]
pub struct PySystemExitObject {
Expand All @@ -107,7 +107,7 @@ pub struct PySystemExitObject {
pub code: *mut PyObject,
}

#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Debug)]
pub struct PyOSErrorObject {
Expand All @@ -134,26 +134,26 @@ pub struct PyOSErrorObject {
#[derive(Debug)]
pub struct PyStopIterationObject {
pub ob_base: PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub dict: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub args: *mut PyObject,
#[cfg(all(Py_3_11, not(PyPy)))]
#[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))]
pub notes: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub traceback: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub context: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub cause: *mut PyObject,
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub suppress_context: char,

pub value: *mut PyObject,
}

extern "C" {
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub fn _PyErr_ChainExceptions(typ: *mut PyObject, val: *mut PyObject, tb: *mut PyObject);
}

Expand Down

0 comments on commit 765ae0a

Please sign in to comment.