Basic GraalPy Support (#3247)

* graalpy: recognize graalpy implementation when building

* graalpy: global Ellipse, None, NotImplemented, True, and False are only available as pointers

* graalpy: PyObject struct is opaque, use functions for everything

* graalpy: missing many of the same functions as pypy

* graalpy: do not have 128bit conversion functions

* graalpy: add functions for datetime accessor macros

* graalpy: add implementations for list macro functions

* graalpy: skip tuple macros

* graalpy: always use extern Py_CompileString function

* graalpy: disable assertion that does not apply to graalpy

* graalpy: floatobject structure is opaque on graalpy

* graalpy: ignore gc dependent test

* graalpy: add CI config

* graalpy: run rust fmt

* graalpy: add changelog entry

* graalpy: discover interpreter on PATH

* graalpy: interpreter id is not applicable to graalpy (just like pypy)

* graalpy: skip tests that cannot work on GraalPy

* graalpy: fix constructing normalized Err instances

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* graalpy: correct capi library name, but skip rust tests due to missing symbols

* graalpy: no support for C extensions on windows in latest release

* graalpy: declare support versions

* graalpy: frame, code, method, and function objects access from C API is mostly missing

* graalpy: take care only to expose C structure that GraalPy allocates

* graalpy: Bail out if graalpy version is less than what we support

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
This commit is contained in:
Tim Felgentreff 2024-03-25 19:54:52 +01:00 committed by GitHub
parent 54ffaecd65
commit 9a38e709bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
66 changed files with 731 additions and 308 deletions

View File

@ -24,6 +24,7 @@ jobs:
build:
continue-on-error: ${{ endsWith(inputs.python-version, '-dev') || contains(fromJSON('["3.7", "pypy3.7"]'), inputs.python-version) || inputs.rust == 'beta' || inputs.rust == 'nightly' }}
runs-on: ${{ inputs.os }}
if: ${{ !(startsWith(inputs.python-version, 'graalpy') && startsWith(inputs.os, 'windows')) }}
steps:
- uses: actions/checkout@v4
@ -65,6 +66,7 @@ jobs:
run: nox -s docs
- name: Build (no features)
if: ${{ !startsWith(inputs.python-version, 'graalpy') }}
run: cargo build --lib --tests --no-default-features
# --no-default-features when used with `cargo build/test -p` doesn't seem to work!
@ -74,7 +76,7 @@ jobs:
cargo build --no-default-features
# Run tests (except on PyPy, because no embedding API).
- if: ${{ !startsWith(inputs.python-version, 'pypy') }}
- if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }}
name: Test (no features)
run: cargo test --no-default-features --lib --tests
@ -85,6 +87,7 @@ jobs:
cargo test --no-default-features
- name: Build (all additive features)
if: ${{ !startsWith(inputs.python-version, 'graalpy') }}
run: cargo build --lib --tests --no-default-features --features "full ${{ inputs.extra-features }}"
- if: ${{ startsWith(inputs.python-version, 'pypy') }}
@ -92,17 +95,17 @@ jobs:
run: cargo build --lib --tests --no-default-features --features "abi3-py37 full ${{ inputs.extra-features }}"
# Run tests (except on PyPy, because no embedding API).
- if: ${{ !startsWith(inputs.python-version, 'pypy') }}
- if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }}
name: Test
run: cargo test --no-default-features --features "full ${{ inputs.extra-features }}"
# Run tests again, but in abi3 mode
- if: ${{ !startsWith(inputs.python-version, 'pypy') }}
- if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }}
name: Test (abi3)
run: cargo test --no-default-features --features "abi3 full ${{ inputs.extra-features }}"
# Run tests again, for abi3-py37 (the minimal Python version)
- if: ${{ (!startsWith(inputs.python-version, 'pypy')) && (inputs.python-version != '3.7') }}
- if: ${{ (!startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy')) && (inputs.python-version != '3.7') }}
name: Test (abi3-py37)
run: cargo test --no-default-features --features "abi3-py37 full ${{ inputs.extra-features }}"
@ -120,7 +123,7 @@ jobs:
- uses: dorny/paths-filter@v3
# pypy 3.7 and 3.8 are not PEP 3123 compliant so fail checks here
if: ${{ inputs.rust == 'stable' && inputs.python-version != 'pypy3.7' && inputs.python-version != 'pypy3.8' }}
if: ${{ inputs.rust == 'stable' && inputs.python-version != 'pypy3.7' && inputs.python-version != 'pypy3.8' && !startsWith(inputs.python-version, 'graalpy') }}
id: ffi-changes
with:
base: ${{ github.event.pull_request.base.ref || github.event.merge_group.base_ref }}
@ -135,7 +138,7 @@ jobs:
- name: Run pyo3-ffi-check
# pypy 3.7 and 3.8 are not PEP 3123 compliant so fail checks here, nor
# is pypy 3.9 on windows
if: ${{ endsWith(inputs.python-version, '-dev') || (steps.ffi-changes.outputs.changed == 'true' && inputs.rust == 'stable' && inputs.python-version != 'pypy3.7' && inputs.python-version != 'pypy3.8' && !(inputs.python-version == 'pypy3.9' && contains(inputs.os, 'windows'))) }}
if: ${{ endsWith(inputs.python-version, '-dev') || (steps.ffi-changes.outputs.changed == 'true' && inputs.rust == 'stable' && inputs.python-version != 'pypy3.7' && inputs.python-version != 'pypy3.8' && !startsWith(inputs.python-version, 'graalpy') && !(inputs.python-version == 'pypy3.9' && contains(inputs.os, 'windows'))) }}
run: nox -s ffi-check
env:

View File

@ -228,6 +228,7 @@ jobs:
"pypy3.8",
"pypy3.9",
"pypy3.10",
"graalpy24.0",
]
platform:
[

View File

@ -17,7 +17,7 @@
## Usage
PyO3 supports the following software versions:
- Python 3.7 and up (CPython and PyPy)
- Python 3.7 and up (CPython, PyPy, and GraalPy)
- Rust 1.56 and up
You can use PyO3 to write a native Python module in Rust, or to embed Python in a Rust binary. The following sections explain each of these in turn.

View File

@ -0,0 +1 @@
Added support for running PyO3 extensions on GraalPy.

View File

@ -32,6 +32,12 @@ use crate::{
/// Minimum Python version PyO3 supports.
const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 };
/// GraalPy may implement the same CPython version over multiple releases.
const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
major: 24,
minor: 0,
};
/// Maximum Python version that can be used as minimum required Python version with abi3.
const ABI3_MAX_MINOR: u8 = 12;
@ -173,6 +179,11 @@ impl InterpreterConfig {
See https://foss.heptapod.net/pypy/pypy/-/issues/3397 for more information."
));
}
} else if self.implementation.is_graalpy() {
println!("cargo:rustc-cfg=GraalPy");
if self.abi3 {
warn!("GraalPy does not support abi3 so the build artifacts will be version-specific.");
}
} else if self.abi3 {
out.push("cargo:rustc-cfg=Py_LIMITED_API".to_owned());
}
@ -197,6 +208,12 @@ import sys
from sysconfig import get_config_var, get_platform
PYPY = platform.python_implementation() == "PyPy"
GRAALPY = platform.python_implementation() == "GraalVM"
if GRAALPY:
graalpy_ver = map(int, __graalpython__.get_graalvm_version().split('.'));
print("graalpy_major", next(graalpy_ver))
print("graalpy_minor", next(graalpy_ver))
# sys.base_prefix is missing on Python versions older than 3.3; this allows the script to continue
# so that the version mismatch can be reported in a nicer way later.
@ -226,7 +243,7 @@ SHARED = bool(get_config_var("Py_ENABLE_SHARED"))
print("implementation", platform.python_implementation())
print("version_major", sys.version_info[0])
print("version_minor", sys.version_info[1])
print("shared", PYPY or ANACONDA or WINDOWS or FRAMEWORK or SHARED)
print("shared", PYPY or GRAALPY or ANACONDA or WINDOWS or FRAMEWORK or SHARED)
print_if_set("ld_version", get_config_var("LDVERSION"))
print_if_set("libdir", get_config_var("LIBDIR"))
print_if_set("base_prefix", base_prefix)
@ -244,6 +261,23 @@ print("ext_suffix", get_config_var("EXT_SUFFIX"))
interpreter.as_ref().display()
);
if let Some(value) = map.get("graalpy_major") {
let graalpy_version = PythonVersion {
major: value
.parse()
.context("failed to parse GraalPy major version")?,
minor: map["graalpy_minor"]
.parse()
.context("failed to parse GraalPy minor version")?,
};
ensure!(
graalpy_version >= MINIMUM_SUPPORTED_VERSION_GRAALPY,
"At least GraalPy version {} needed, got {}",
MINIMUM_SUPPORTED_VERSION_GRAALPY,
graalpy_version
);
};
let shared = map["shared"].as_str() == "True";
let version = PythonVersion {
@ -588,7 +622,7 @@ print("ext_suffix", get_config_var("EXT_SUFFIX"))
/// Lowers the configured version to the abi3 version, if set.
fn fixup_for_abi3_version(&mut self, abi3_version: Option<PythonVersion>) -> Result<()> {
// PyPy doesn't support abi3; don't adjust the version
if self.implementation.is_pypy() {
if self.implementation.is_pypy() || self.implementation.is_graalpy() {
return Ok(());
}
@ -647,6 +681,7 @@ impl FromStr for PythonVersion {
pub enum PythonImplementation {
CPython,
PyPy,
GraalPy,
}
impl PythonImplementation {
@ -655,12 +690,19 @@ impl PythonImplementation {
self == PythonImplementation::PyPy
}
#[doc(hidden)]
pub fn is_graalpy(self) -> bool {
self == PythonImplementation::GraalPy
}
#[doc(hidden)]
pub fn from_soabi(soabi: &str) -> Result<Self> {
if soabi.starts_with("pypy") {
Ok(PythonImplementation::PyPy)
} else if soabi.starts_with("cpython") {
Ok(PythonImplementation::CPython)
} else if soabi.starts_with("graalpy") {
Ok(PythonImplementation::GraalPy)
} else {
bail!("unsupported Python interpreter");
}
@ -672,6 +714,7 @@ impl Display for PythonImplementation {
match self {
PythonImplementation::CPython => write!(f, "CPython"),
PythonImplementation::PyPy => write!(f, "PyPy"),
PythonImplementation::GraalPy => write!(f, "GraalVM"),
}
}
}
@ -682,6 +725,7 @@ impl FromStr for PythonImplementation {
match s {
"CPython" => Ok(PythonImplementation::CPython),
"PyPy" => Ok(PythonImplementation::PyPy),
"GraalVM" => Ok(PythonImplementation::GraalPy),
_ => bail!("unknown interpreter: {}", s),
}
}
@ -760,7 +804,7 @@ pub struct CrossCompileConfig {
/// The version of the Python library to link against.
version: Option<PythonVersion>,
/// The target Python implementation hint (CPython or PyPy)
/// The target Python implementation hint (CPython, PyPy, GraalPy, ...)
implementation: Option<PythonImplementation>,
/// The compile target triple (e.g. aarch64-unknown-linux-gnu)
@ -1264,6 +1308,15 @@ fn is_pypy_lib_dir(path: &str, v: &Option<PythonVersion>) -> bool {
path == "lib_pypy" || path.starts_with(&pypy_version_pat)
}
fn is_graalpy_lib_dir(path: &str, v: &Option<PythonVersion>) -> bool {
let graalpy_version_pat = if let Some(v) = v {
format!("graalpy{}", v)
} else {
"graalpy2".into()
};
path == "lib_graalpython" || path.starts_with(&graalpy_version_pat)
}
fn is_cpython_lib_dir(path: &str, v: &Option<PythonVersion>) -> bool {
let cpython_version_pat = if let Some(v) = v {
format!("python{}", v)
@ -1297,6 +1350,7 @@ fn search_lib_dir(path: impl AsRef<Path>, cross: &CrossCompileConfig) -> Vec<Pat
search_lib_dir(f.path(), cross)
} else if is_cpython_lib_dir(&file_name, &cross.version)
|| is_pypy_lib_dir(&file_name, &cross.version)
|| is_graalpy_lib_dir(&file_name, &cross.version)
{
search_lib_dir(f.path(), cross)
} else {
@ -1418,7 +1472,7 @@ fn default_cross_compile(cross_compile_config: &CrossCompileConfig) -> Result<In
///
/// Must be called from a PyO3 crate build script.
fn default_abi3_config(host: &Triple, version: PythonVersion) -> InterpreterConfig {
// FIXME: PyPy does not support the Stable ABI yet.
// FIXME: PyPy & GraalPy do not support the Stable ABI.
let implementation = PythonImplementation::CPython;
let abi3 = true;
@ -1524,7 +1578,7 @@ fn default_lib_name_windows(
// CPython bug: linking against python3_d.dll raises error
// https://github.com/python/cpython/issues/101614
format!("python{}{}_d", version.major, version.minor)
} else if abi3 && !implementation.is_pypy() {
} else if abi3 && !(implementation.is_pypy() || implementation.is_graalpy()) {
WINDOWS_ABI3_LIB_NAME.to_owned()
} else if mingw {
// https://packages.msys2.org/base/mingw-w64-python
@ -1562,6 +1616,7 @@ fn default_lib_name_unix(
format!("pypy{}-c", version.major)
}
}
PythonImplementation::GraalPy => "python-native".to_string(),
}
}
@ -1662,7 +1717,9 @@ pub fn find_interpreter() -> Result<PathBuf> {
.find(|bin| {
if let Ok(out) = Command::new(bin).arg("--version").output() {
// begin with `Python 3.X.X :: additional info`
out.stdout.starts_with(b"Python 3") || out.stderr.starts_with(b"Python 3")
out.stdout.starts_with(b"Python 3")
|| out.stderr.starts_with(b"Python 3")
|| out.stdout.starts_with(b"GraalPy 3")
} else {
false
}

View File

@ -7,7 +7,7 @@ use python3_dll_a::ImportLibraryGenerator;
use target_lexicon::{Architecture, OperatingSystem, Triple};
use super::{PythonImplementation, PythonVersion};
use crate::errors::{Context, Result};
use crate::errors::{Context, Error, Result};
/// Generates the `python3.dll` or `pythonXY.dll` import library for Windows targets.
///
@ -42,6 +42,9 @@ pub(super) fn generate_import_lib(
let implementation = match py_impl {
PythonImplementation::CPython => python3_dll_a::PythonImplementation::CPython,
PythonImplementation::PyPy => python3_dll_a::PythonImplementation::PyPy,
PythonImplementation::GraalPy => {
return Err(Error::from("No support for GraalPy on Windows"))
}
};
ImportLibraryGenerator::new(&arch, &env)

View File

@ -38,6 +38,7 @@ use target_lexicon::OperatingSystem;
/// | `#[cfg(Py_3_7)]`, `#[cfg(Py_3_8)]`, `#[cfg(Py_3_9)]`, `#[cfg(Py_3_10)]` | These attributes mark code only for a given Python version and up. For example, `#[cfg(Py_3_7)]` marks code which can run on Python 3.7 **and newer**. |
/// | `#[cfg(Py_LIMITED_API)]` | This marks code which is run when compiling with PyO3's `abi3` feature enabled. |
/// | `#[cfg(PyPy)]` | This marks code which is run when compiling for PyPy. |
/// | `#[cfg(GraalPy)]` | This marks code which is run when compiling for GraalPy. |
///
/// For examples of how to use these attributes, [see PyO3's guide](https://pyo3.rs/latest/building-and-distribution/multiple_python_versions.html).
#[cfg(feature = "resolve-config")]

View File

@ -29,6 +29,17 @@ const SUPPORTED_VERSIONS_PYPY: SupportedVersions = SupportedVersions {
},
};
const SUPPORTED_VERSIONS_GRAALPY: SupportedVersions = SupportedVersions {
min: PythonVersion {
major: 3,
minor: 10,
},
max: PythonVersion {
major: 3,
minor: 11,
},
};
fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> {
// This is an undocumented env var which is only really intended to be used in CI / for testing
// and development.
@ -73,6 +84,24 @@ fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> {
std::env::var("CARGO_PKG_VERSION").unwrap()
);
}
PythonImplementation::GraalPy => {
let versions = SUPPORTED_VERSIONS_GRAALPY;
ensure!(
interpreter_config.version >= versions.min,
"the configured GraalPy interpreter version ({}) is lower than PyO3's minimum supported version ({})",
interpreter_config.version,
versions.min,
);
// GraalPy does not support abi3, so we cannot offer forward compatibility
ensure!(
interpreter_config.version <= versions.max,
"the configured GraalPy interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\
= help: please check if an updated version of PyO3 is available. Current version: {}",
interpreter_config.version,
versions.max,
std::env::var("CARGO_PKG_VERSION").unwrap()
);
}
}
Ok(())

View File

@ -23,6 +23,7 @@ pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_
extern "C" {
#[cfg(all(
not(PyPy),
not(GraalPy),
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
))]
#[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")]

View File

@ -1,3 +1,4 @@
#[cfg(not(GraalPy))]
use crate::longobject::PyLongObject;
use crate::object::*;
use std::os::raw::{c_int, c_long};
@ -16,20 +17,33 @@ pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int {
#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg(not(GraalPy))]
#[cfg_attr(PyPy, link_name = "_PyPy_FalseStruct")]
static mut _Py_FalseStruct: PyLongObject;
#[cfg(not(GraalPy))]
#[cfg_attr(PyPy, link_name = "_PyPy_TrueStruct")]
static mut _Py_TrueStruct: PyLongObject;
#[cfg(GraalPy)]
static mut _Py_FalseStructReference: *mut PyObject;
#[cfg(GraalPy)]
static mut _Py_TrueStructReference: *mut PyObject;
}
#[inline]
pub unsafe fn Py_False() -> *mut PyObject {
addr_of_mut!(_Py_FalseStruct) as *mut PyObject
#[cfg(not(GraalPy))]
return addr_of_mut!(_Py_FalseStruct) as *mut PyObject;
#[cfg(GraalPy)]
return _Py_FalseStructReference;
}
#[inline]
pub unsafe fn Py_True() -> *mut PyObject {
addr_of_mut!(_Py_TrueStruct) as *mut PyObject
#[cfg(not(GraalPy))]
return addr_of_mut!(_Py_TrueStruct) as *mut PyObject;
#[cfg(GraalPy)]
return _Py_TrueStructReference;
}
#[inline]

View File

@ -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 {
@ -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"))]

View File

@ -30,6 +30,7 @@ extern "C" {
// non-limited
pub struct PyComplexObject {
pub ob_base: PyObject,
#[cfg(not(GraalPy))]
pub cval: Py_complex,
}

View File

@ -6,7 +6,7 @@ use crate::Py_buffer;
#[cfg(Py_3_8)]
use crate::pyport::PY_SSIZE_T_MAX;
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
use crate::{
vectorcallfunc, PyCallable_Check, PyThreadState, PyThreadState_GET, PyTuple_Check,
PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL,
@ -15,15 +15,15 @@ use crate::{
use libc::size_t;
extern "C" {
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
pub fn _PyStack_AsDict(values: *const *mut PyObject, kwnames: *mut PyObject) -> *mut PyObject;
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
const _PY_FASTCALL_SMALL_STACK: size_t = 5;
extern "C" {
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
pub fn _Py_CheckFunctionResult(
tstate: *mut PyThreadState,
callable: *mut PyObject,
@ -31,7 +31,7 @@ extern "C" {
where_: *const c_char,
) -> *mut PyObject;
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
pub fn _PyObject_MakeTpCall(
tstate: *mut PyThreadState,
callable: *mut PyObject,
@ -52,7 +52,7 @@ pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t {
(n as Py_ssize_t) & !PY_VECTORCALL_ARGUMENTS_OFFSET
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc> {
assert!(!callable.is_null());
@ -67,7 +67,7 @@ pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcal
*ptr
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn _PyObject_VectorcallTstate(
tstate: *mut PyThreadState,
@ -91,7 +91,7 @@ pub unsafe fn _PyObject_VectorcallTstate(
}
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn PyObject_Vectorcall(
callable: *mut PyObject,
@ -114,7 +114,10 @@ extern "C" {
) -> *mut PyObject;
#[cfg(Py_3_8)]
#[cfg_attr(all(not(PyPy), not(Py_3_9)), link_name = "_PyObject_VectorcallDict")]
#[cfg_attr(
all(not(any(PyPy, GraalPy)), not(Py_3_9)),
link_name = "_PyObject_VectorcallDict"
)]
#[cfg_attr(all(PyPy, not(Py_3_9)), link_name = "_PyPyObject_VectorcallDict")]
#[cfg_attr(all(PyPy, Py_3_9), link_name = "PyPyObject_VectorcallDict")]
pub fn PyObject_VectorcallDict(
@ -134,7 +137,7 @@ extern "C" {
) -> *mut PyObject;
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn _PyObject_FastCallTstate(
tstate: *mut PyThreadState,
@ -145,7 +148,7 @@ pub unsafe fn _PyObject_FastCallTstate(
_PyObject_VectorcallTstate(tstate, func, args, nargs as size_t, std::ptr::null_mut())
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn _PyObject_FastCall(
func: *mut PyObject,
@ -155,7 +158,7 @@ pub unsafe fn _PyObject_FastCall(
_PyObject_FastCallTstate(PyThreadState_GET(), func, args, nargs)
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject {
_PyObject_VectorcallTstate(
@ -173,7 +176,7 @@ extern "C" {
pub fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject;
}
#[cfg(all(Py_3_8, not(PyPy)))]
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject {
assert!(!arg.is_null());
@ -185,7 +188,7 @@ pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *m
}
extern "C" {
#[cfg(all(Py_3_9, not(PyPy)))]
#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
pub fn PyObject_VectorcallMethod(
name: *mut PyObject,
args: *const *mut PyObject,
@ -194,7 +197,7 @@ extern "C" {
) -> *mut PyObject;
}
#[cfg(all(Py_3_9, not(PyPy)))]
#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn PyObject_CallMethodNoArgs(
self_: *mut PyObject,
@ -208,7 +211,7 @@ pub unsafe fn PyObject_CallMethodNoArgs(
)
}
#[cfg(all(Py_3_9, not(PyPy)))]
#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
#[inline(always)]
pub unsafe fn PyObject_CallMethodOneArg(
self_: *mut PyObject,
@ -236,7 +239,7 @@ extern "C" {
pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t;
#[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11
#[cfg(all(Py_3_9, not(PyPy)))]
#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))]
pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int;
}
@ -308,7 +311,7 @@ pub const PY_ITERSEARCH_INDEX: c_int = 2;
pub const PY_ITERSEARCH_CONTAINS: c_int = 3;
extern "C" {
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub fn _PySequence_IterSearch(
seq: *mut PyObject,
obj: *mut PyObject,

View File

@ -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 {
@ -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" {

View File

@ -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)]
@ -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 {
@ -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 {
@ -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 {
@ -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
}
@ -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,
@ -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,
@ -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

View File

@ -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 {
@ -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 {

View File

@ -7,6 +7,7 @@ opaque_struct!(PyDictKeysObject);
#[cfg(Py_3_11)]
opaque_struct!(PyDictValues);
#[cfg(not(GraalPy))]
#[repr(C)]
#[derive(Debug)]
pub struct PyDictObject {

View File

@ -1,9 +1,12 @@
#[cfg(GraalPy)]
use crate::PyFloat_AsDouble;
use crate::{PyFloat_Check, PyObject};
use std::os::raw::c_double;
#[repr(C)]
pub struct PyFloatObject {
pub ob_base: PyObject,
#[cfg(not(GraalPy))]
pub ob_fval: c_double,
}
@ -15,7 +18,10 @@ pub unsafe fn _PyFloat_CAST(op: *mut PyObject) -> *mut PyFloatObject {
#[inline]
pub unsafe fn PyFloat_AS_DOUBLE(op: *mut PyObject) -> c_double {
(*_PyFloat_CAST(op)).ob_fval
#[cfg(not(GraalPy))]
return (*_PyFloat_CAST(op)).ob_fval;
#[cfg(GraalPy)]
return PyFloat_AsDouble(op);
}
// skipped PyFloat_Pack2

View File

@ -1,17 +1,19 @@
#[cfg(not(GraalPy))]
use crate::cpython::code::PyCodeObject;
use crate::object::*;
#[cfg(not(GraalPy))]
use crate::pystate::PyThreadState;
#[cfg(not(any(PyPy, Py_3_11)))]
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::ptr::addr_of_mut;
#[cfg(not(any(PyPy, Py_3_11)))]
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
pub type PyFrameState = c_char;
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg(not(any(PyPy, Py_3_11)))]
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
pub struct PyTryBlock {
pub b_type: c_int,
pub b_handler: c_int,
@ -20,7 +22,7 @@ pub struct PyTryBlock {
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg(not(any(PyPy, Py_3_11)))]
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
pub struct PyFrameObject {
pub ob_base: PyVarObject,
pub f_back: *mut PyFrameObject,
@ -51,7 +53,7 @@ pub struct PyFrameObject {
pub f_localsplus: [*mut PyObject; 1],
}
#[cfg(any(PyPy, Py_3_11))]
#[cfg(any(PyPy, GraalPy, Py_3_11))]
opaque_struct!(PyFrameObject);
// skipped _PyFrame_IsRunnable
@ -69,6 +71,7 @@ pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int {
}
extern "C" {
#[cfg(not(GraalPy))]
#[cfg_attr(PyPy, link_name = "PyPyFrame_New")]
pub fn PyFrame_New(
tstate: *mut PyThreadState,
@ -79,7 +82,7 @@ extern "C" {
// skipped _PyFrame_New_NoTrack
pub fn PyFrame_BlockSetup(f: *mut PyFrameObject, _type: c_int, handler: c_int, level: c_int);
#[cfg(not(any(PyPy, Py_3_11)))]
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
pub fn PyFrame_BlockPop(f: *mut PyFrameObject) -> *mut PyTryBlock;
pub fn PyFrame_LocalsToFast(f: *mut PyFrameObject, clear: c_int);

View File

@ -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,
@ -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,
@ -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))))]

View File

@ -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 {

View File

@ -2,7 +2,7 @@ use crate::object::*;
#[cfg(not(PyPy))]
use crate::pyport::Py_ssize_t;
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PyListObject {
@ -11,7 +11,7 @@ pub struct PyListObject {
pub allocated: Py_ssize_t,
}
#[cfg(PyPy)]
#[cfg(any(PyPy, GraalPy))]
pub struct PyListObject {
pub ob_base: PyObject,
}
@ -22,14 +22,14 @@ pub struct PyListObject {
/// Macro, trading safety for speed
#[inline]
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub unsafe fn PyList_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
*(*(op as *mut PyListObject)).ob_item.offset(i)
}
/// Macro, *only* to be used to fill in brand new lists
#[inline]
#[cfg(not(PyPy))]
#[cfg(not(any(PyPy, GraalPy)))]
pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
*(*(op as *mut PyListObject)).ob_item.offset(i) = v;
}

View File

@ -1,8 +1,10 @@
use crate::object::*;
#[cfg(not(GraalPy))]
use crate::{PyCFunctionObject, PyMethodDefPointer, METH_METHOD, METH_STATIC};
use std::os::raw::c_int;
use std::ptr::addr_of_mut;
#[cfg(not(GraalPy))]
pub struct PyCMethodObject {
pub func: PyCFunctionObject,
pub mm_class: *mut PyTypeObject,
@ -23,6 +25,7 @@ pub unsafe fn PyCMethod_Check(op: *mut PyObject) -> c_int {
PyObject_TypeCheck(op, addr_of_mut!(PyCMethod_Type))
}
#[cfg(not(GraalPy))]
#[inline]
pub unsafe fn PyCFunction_GET_FUNCTION(func: *mut PyObject) -> PyMethodDefPointer {
debug_assert_eq!(PyCMethod_Check(func), 1);
@ -31,6 +34,7 @@ pub unsafe fn PyCFunction_GET_FUNCTION(func: *mut PyObject) -> PyMethodDefPointe
(*(*func).m_ml).ml_meth
}
#[cfg(not(GraalPy))]
#[inline]
pub unsafe fn PyCFunction_GET_SELF(func: *mut PyObject) -> *mut PyObject {
debug_assert_eq!(PyCMethod_Check(func), 1);
@ -43,6 +47,7 @@ pub unsafe fn PyCFunction_GET_SELF(func: *mut PyObject) -> *mut PyObject {
}
}
#[cfg(not(GraalPy))]
#[inline]
pub unsafe fn PyCFunction_GET_FLAGS(func: *mut PyObject) -> c_int {
debug_assert_eq!(PyCMethod_Check(func), 1);
@ -51,6 +56,7 @@ pub unsafe fn PyCFunction_GET_FLAGS(func: *mut PyObject) -> c_int {
(*(*func).m_ml).ml_flags
}
#[cfg(not(GraalPy))]
#[inline]
pub unsafe fn PyCFunction_GET_CLASS(func: *mut PyObject) -> *mut PyTypeObject {
debug_assert_eq!(PyCMethod_Check(func), 1);