Skip to content

Commit ada43c4

Browse files
Add PyABIInfo_Check to the c-api (#8941)
1 parent c8d63e8 commit ada43c4

2 files changed

Lines changed: 59 additions & 31 deletions

File tree

‎crates/capi/src/modsupport.rs‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,54 @@ use crate::object::PyTypeObject;
33
use crate::pystate::with_vm;
44
use crate::util::{CStrExt, FfiPtrExt};
55
use core::ffi::{c_char, c_int, c_long};
6-
use rustpython_vm::AsObject;
76
use rustpython_vm::builtins::PyModule;
7+
use rustpython_vm::{AsObject, PyResult, VirtualMachine};
8+
9+
#[repr(C)]
10+
#[derive(Debug, Copy, Clone)]
11+
pub struct PyABIInfo {
12+
pub abiinfo_major_version: u8,
13+
pub abiinfo_minor_version: u8,
14+
pub flags: u16,
15+
pub build_version: u32,
16+
pub abi_version: u32,
17+
}
18+
19+
impl PyABIInfo {
20+
pub(crate) fn is_supported(
21+
&self,
22+
vm: &VirtualMachine,
23+
module_name: Option<&str>,
24+
) -> PyResult<()> {
25+
const PY_ABIINFO_STABLE: u16 = 0x0001;
26+
const PY_ABIINFO_FREETHREADED: u16 = 0x0004;
27+
28+
let module_name = module_name.unwrap_or("<unknown>");
29+
30+
if self.abiinfo_major_version == 0 {
31+
return Ok(());
32+
}
33+
34+
if self.abiinfo_major_version > 1 {
35+
return Err(
36+
vm.new_import_error("PyABIInfo version too high", vm.ctx.new_str(module_name))
37+
);
38+
}
39+
40+
if self.flags & PY_ABIINFO_STABLE == 0 {
41+
return Err(vm.new_import_error("not using stable ABI", vm.ctx.new_str(module_name)));
42+
}
43+
44+
if self.flags & PY_ABIINFO_FREETHREADED == 0 {
45+
return Err(vm.new_import_error(
46+
"incompatible with free-threaded python",
47+
vm.ctx.new_str(module_name),
48+
));
49+
}
50+
51+
Ok(())
52+
}
53+
}
854

955
#[unsafe(no_mangle)]
1056
pub unsafe extern "C" fn PyModule_AddObjectRef(
@@ -84,6 +130,17 @@ pub unsafe extern "C" fn PyModule_AddType(
84130
})
85131
}
86132

133+
#[unsafe(no_mangle)]
134+
pub unsafe extern "C" fn PyABIInfo_Check(
135+
info: *mut PyABIInfo,
136+
module_name: *const c_char,
137+
) -> c_int {
138+
with_vm(|vm| {
139+
let module_name = unsafe { module_name.try_as_str_opt(vm) }?;
140+
unsafe { &*info }.is_supported(vm, module_name)
141+
})
142+
}
143+
87144
#[cfg(test)]
88145
mod tests {
89146
use pyo3::prelude::*;

‎crates/capi/src/slots.rs‎

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::PyObject;
22
use crate::methodobject::PyMethodDef;
3+
use crate::modsupport::PyABIInfo;
34
use crate::object::PyType_Slot;
45
use crate::util::CStrExt;
56
use core::ffi::{c_char, c_int, c_void};
@@ -129,36 +130,6 @@ pub union PySlotValue {
129130
pub sl_uint64: u64,
130131
}
131132

132-
#[repr(C)]
133-
#[derive(Debug, Copy, Clone)]
134-
pub struct PyABIInfo {
135-
pub abiinfo_major_version: u8,
136-
pub abiinfo_minor_version: u8,
137-
pub flags: u16,
138-
pub build_version: u32,
139-
pub abi_version: u32,
140-
}
141-
142-
impl PyABIInfo {
143-
#[allow(dead_code)]
144-
#[must_use]
145-
pub(crate) fn is_supported(&self) -> bool {
146-
const PY_ABIINFO_STABLE: u16 = 0x0001;
147-
const PY_ABIINFO_FREETHREADED: u16 = 0x0004;
148-
149-
if self.abiinfo_major_version != 1 || self.abiinfo_minor_version != 0 {
150-
return false;
151-
}
152-
153-
// Only accept abi3t
154-
if self.flags & PY_ABIINFO_STABLE == 0 || self.flags & PY_ABIINFO_FREETHREADED == 0 {
155-
return false;
156-
}
157-
158-
true
159-
}
160-
}
161-
162133
#[allow(dead_code)]
163134
#[derive(Debug, Copy, Clone)]
164135
pub(crate) enum PySlotModule<'a> {

0 commit comments

Comments
 (0)