@@ -3,8 +3,54 @@ use crate::object::PyTypeObject;
33use crate :: pystate:: with_vm;
44use crate :: util:: { CStrExt , FfiPtrExt } ;
55use core:: ffi:: { c_char, c_int, c_long} ;
6- use rustpython_vm:: AsObject ;
76use 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) ]
1056pub 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) ]
88145mod tests {
89146 use pyo3:: prelude:: * ;
0 commit comments