Skip to content

Commit eef6277

Browse files
Do not use static type references when compiling for RustPython (#5995)
* Remove static type references Merge adjacent extern blocks Fixes * Reduce extern_libpython blocks even more * Fix error * Fix setobject.rs * Fix `link_name` to only apply for PyPy * Correctly import `TracebackType` * Do not use `PyType_FastSubclass` when compiling for `RustPython`
1 parent f57bda7 commit eef6277

54 files changed

Lines changed: 713 additions & 94 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyo3-build-config/src/impl_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ fn default_lib_name_unix(
17721772
},
17731773

17741774
PythonImplementation::GraalPy => Ok("python-native".to_string()),
1775-
PythonImplementation::RustPython => Ok("rustpython-capi".to_string()),
1775+
PythonImplementation::RustPython => Ok("rustpython_capi".to_string()),
17761776
}
17771777
}
17781778

pyo3-ffi/src/boolobject.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ use crate::object::*;
44
use std::ffi::{c_int, c_long};
55

66
#[inline]
7+
#[cfg(not(RustPython))]
78
pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int {
89
Py_IS_TYPE(op, &raw mut PyBool_Type)
910
}
1011

1112
extern_libpython! {
13+
#[cfg(RustPython)]
14+
pub fn PyBool_Check(op: *mut PyObject) -> c_int;
15+
1216
#[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))]
1317
#[cfg_attr(PyPy, link_name = "_PyPy_FalseStruct")]
1418
static mut _Py_FalseStruct: PyLongObject;

pyo3-ffi/src/bytearrayobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::ffi::{c_char, c_int};
44

5+
#[cfg(not(RustPython))]
56
extern_libpython! {
67
#[cfg_attr(PyPy, link_name = "PyPyByteArray_Type")]
78
pub static mut PyByteArray_Type: PyTypeObject;
@@ -10,16 +11,23 @@ extern_libpython! {
1011
}
1112

1213
#[inline]
14+
#[cfg(not(RustPython))]
1315
pub unsafe fn PyByteArray_Check(op: *mut PyObject) -> c_int {
1416
PyObject_TypeCheck(op, &raw mut PyByteArray_Type)
1517
}
1618

1719
#[inline]
20+
#[cfg(not(RustPython))]
1821
pub unsafe fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int {
1922
Py_IS_TYPE(op, &raw mut PyByteArray_Type)
2023
}
2124

2225
extern_libpython! {
26+
#[cfg(RustPython)]
27+
pub fn PyByteArray_Check(op: *mut PyObject) -> c_int;
28+
#[cfg(RustPython)]
29+
pub fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int;
30+
2331
#[cfg_attr(PyPy, link_name = "PyPyByteArray_FromObject")]
2432
pub fn PyByteArray_FromObject(o: *mut PyObject) -> *mut PyObject;
2533
#[cfg_attr(PyPy, link_name = "PyPyByteArray_Concat")]

pyo3-ffi/src/bytesobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::ffi::{c_char, c_int};
44

5+
#[cfg(not(RustPython))]
56
extern_libpython! {
67
#[cfg_attr(PyPy, link_name = "PyPyBytes_Type")]
78
pub static mut PyBytes_Type: PyTypeObject;
89
pub static mut PyBytesIter_Type: PyTypeObject;
910
}
1011

1112
#[inline]
13+
#[cfg(not(RustPython))]
1214
pub unsafe fn PyBytes_Check(op: *mut PyObject) -> c_int {
1315
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
1416
}
1517

1618
#[inline]
19+
#[cfg(not(RustPython))]
1720
pub unsafe fn PyBytes_CheckExact(op: *mut PyObject) -> c_int {
1821
Py_IS_TYPE(op, &raw mut PyBytes_Type)
1922
}
2023

2124
extern_libpython! {
25+
#[cfg(RustPython)]
26+
pub fn PyBytes_Check(op: *mut PyObject) -> c_int;
27+
#[cfg(RustPython)]
28+
pub fn PyBytes_CheckExact(op: *mut PyObject) -> c_int;
29+
2230
#[cfg_attr(PyPy, link_name = "PyPyBytes_FromStringAndSize")]
2331
pub fn PyBytes_FromStringAndSize(arg1: *const c_char, arg2: Py_ssize_t) -> *mut PyObject;
2432
pub fn PyBytes_FromString(arg1: *const c_char) -> *mut PyObject;

pyo3-ffi/src/complexobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
use crate::object::*;
22
use std::ffi::{c_double, c_int};
33

4+
#[cfg(not(RustPython))]
45
extern_libpython! {
56
#[cfg_attr(PyPy, link_name = "PyPyComplex_Type")]
67
pub static mut PyComplex_Type: PyTypeObject;
78
}
89

910
#[inline]
11+
#[cfg(not(RustPython))]
1012
pub unsafe fn PyComplex_Check(op: *mut PyObject) -> c_int {
1113
PyObject_TypeCheck(op, &raw mut PyComplex_Type)
1214
}
1315

1416
#[inline]
17+
#[cfg(not(RustPython))]
1518
pub unsafe fn PyComplex_CheckExact(op: *mut PyObject) -> c_int {
1619
Py_IS_TYPE(op, &raw mut PyComplex_Type)
1720
}
1821

1922
extern_libpython! {
23+
#[cfg(RustPython)]
24+
pub fn PyComplex_Check(op: *mut PyObject) -> c_int;
25+
#[cfg(RustPython)]
26+
pub fn PyComplex_CheckExact(op: *mut PyObject) -> c_int;
27+
2028
// skipped non-limited PyComplex_FromCComplex
2129
#[cfg_attr(PyPy, link_name = "PyPyComplex_FromDoubles")]
2230
pub fn PyComplex_FromDoubles(real: c_double, imag: c_double) -> *mut PyObject;

pyo3-ffi/src/context.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use crate::object::{PyObject, PyTypeObject};
1+
use crate::object::PyObject;
2+
#[cfg(not(RustPython))]
3+
use crate::object::PyTypeObject;
4+
#[cfg(not(RustPython))]
25
use crate::Py_IS_TYPE;
36
use std::ffi::{c_char, c_int};
47

8+
#[cfg(not(RustPython))]
59
extern_libpython! {
610
pub static mut PyContext_Type: PyTypeObject;
711
// skipped non-limited opaque PyContext
@@ -12,21 +16,31 @@ extern_libpython! {
1216
}
1317

1418
#[inline]
19+
#[cfg(not(RustPython))]
1520
pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int {
1621
Py_IS_TYPE(op, &raw mut PyContext_Type)
1722
}
1823

1924
#[inline]
25+
#[cfg(not(RustPython))]
2026
pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int {
2127
Py_IS_TYPE(op, &raw mut PyContextVar_Type)
2228
}
2329

2430
#[inline]
31+
#[cfg(not(RustPython))]
2532
pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int {
2633
Py_IS_TYPE(op, &raw mut PyContextToken_Type)
2734
}
2835

2936
extern_libpython! {
37+
#[cfg(RustPython)]
38+
pub fn PyContext_CheckExact(op: *mut PyObject) -> c_int;
39+
#[cfg(RustPython)]
40+
pub fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int;
41+
#[cfg(RustPython)]
42+
pub fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int;
43+
3044
pub fn PyContext_New() -> *mut PyObject;
3145
pub fn PyContext_Copy(ctx: *mut PyObject) -> *mut PyObject;
3246
pub fn PyContext_CopyCurrent() -> *mut PyObject;

pyo3-ffi/src/descrobject.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl Default for PyGetSetDef {
3535
}
3636
}
3737

38+
#[cfg(not(RustPython))]
3839
extern_libpython! {
3940
#[cfg_attr(PyPy, link_name = "PyPyClassMethodDescr_Type")]
4041
pub static mut PyClassMethodDescr_Type: PyTypeObject;

pyo3-ffi/src/dictobject.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::ffi::{c_char, c_int};
44

5+
#[cfg(not(RustPython))]
56
extern_libpython! {
67
#[cfg_attr(PyPy, link_name = "PyPyDict_Type")]
78
pub static mut PyDict_Type: PyTypeObject;
89
}
910

1011
#[inline]
12+
#[cfg(not(RustPython))]
1113
pub unsafe fn PyDict_Check(op: *mut PyObject) -> c_int {
1214
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
1315
}
1416

1517
#[inline]
18+
#[cfg(not(RustPython))]
1619
pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int {
1720
Py_IS_TYPE(op, &raw mut PyDict_Type)
1821
}
1922

2023
extern_libpython! {
24+
#[cfg(RustPython)]
25+
pub fn PyDict_Check(op: *mut PyObject) -> c_int;
26+
#[cfg(RustPython)]
27+
pub fn PyDict_CheckExact(op: *mut PyObject) -> c_int;
28+
2129
#[cfg_attr(PyPy, link_name = "PyPyDict_New")]
2230
pub fn PyDict_New() -> *mut PyObject;
2331
#[cfg_attr(PyPy, link_name = "PyPyDict_GetItem")]
@@ -88,32 +96,46 @@ extern_libpython! {
8896
// skipped 3.10 / ex-non-limited PyObject_GenericGetDict
8997
}
9098

99+
#[cfg(not(RustPython))]
91100
extern_libpython! {
92101
pub static mut PyDictKeys_Type: PyTypeObject;
93102
pub static mut PyDictValues_Type: PyTypeObject;
94103
pub static mut PyDictItems_Type: PyTypeObject;
95104
}
96105

97106
#[inline]
107+
#[cfg(not(RustPython))]
98108
pub unsafe fn PyDictKeys_Check(op: *mut PyObject) -> c_int {
99109
PyObject_TypeCheck(op, &raw mut PyDictKeys_Type)
100110
}
101111

102112
#[inline]
113+
#[cfg(not(RustPython))]
103114
pub unsafe fn PyDictValues_Check(op: *mut PyObject) -> c_int {
104115
PyObject_TypeCheck(op, &raw mut PyDictValues_Type)
105116
}
106117

107118
#[inline]
119+
#[cfg(not(RustPython))]
108120
pub unsafe fn PyDictItems_Check(op: *mut PyObject) -> c_int {
109121
PyObject_TypeCheck(op, &raw mut PyDictItems_Type)
110122
}
111123

124+
extern_libpython! {
125+
#[cfg(RustPython)]
126+
pub fn PyDictKeys_Check(op: *mut PyObject) -> c_int;
127+
#[cfg(RustPython)]
128+
pub fn PyDictValues_Check(op: *mut PyObject) -> c_int;
129+
#[cfg(RustPython)]
130+
pub fn PyDictItems_Check(op: *mut PyObject) -> c_int;
131+
}
132+
112133
#[inline]
113134
pub unsafe fn PyDictViewSet_Check(op: *mut PyObject) -> c_int {
114135
(PyDictKeys_Check(op) != 0 || PyDictItems_Check(op) != 0) as c_int
115136
}
116137

138+
#[cfg(not(RustPython))]
117139
extern_libpython! {
118140
pub static mut PyDictIterKey_Type: PyTypeObject;
119141
pub static mut PyDictIterValue_Type: PyTypeObject;

pyo3-ffi/src/floatobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ use std::ffi::{c_double, c_int};
66
opaque_struct!(pub PyFloatObject);
77

88
extern_libpython! {
9+
#[cfg(not(RustPython))]
910
#[cfg_attr(PyPy, link_name = "PyPyFloat_Type")]
1011
pub static mut PyFloat_Type: PyTypeObject;
12+
13+
#[cfg(RustPython)]
14+
pub fn PyFloat_Check(op: *mut PyObject) -> c_int;
15+
#[cfg(RustPython)]
16+
pub fn PyFloat_CheckExact(op: *mut PyObject) -> c_int;
1117
}
1218

1319
#[inline]
20+
#[cfg(not(RustPython))]
1421
pub unsafe fn PyFloat_Check(op: *mut PyObject) -> c_int {
1522
PyObject_TypeCheck(op, &raw mut PyFloat_Type)
1623
}
1724

1825
#[inline]
26+
#[cfg(not(RustPython))]
1927
pub unsafe fn PyFloat_CheckExact(op: *mut PyObject) -> c_int {
2028
Py_IS_TYPE(op, &raw mut PyFloat_Type)
2129
}

pyo3-ffi/src/genericaliasobject.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#[cfg(Py_3_9)]
2-
use crate::object::{PyObject, PyTypeObject};
2+
use crate::PyObject;
3+
#[cfg(all(Py_3_9, not(RustPython)))]
4+
use crate::PyTypeObject;
35

46
extern_libpython! {
57
#[cfg(Py_3_9)]
68
#[cfg_attr(PyPy, link_name = "PyPy_GenericAlias")]
79
pub fn Py_GenericAlias(origin: *mut PyObject, args: *mut PyObject) -> *mut PyObject;
810

9-
#[cfg(Py_3_9)]
11+
#[cfg(all(Py_3_9, not(RustPython)))]
1012
pub static mut Py_GenericAliasType: PyTypeObject;
1113
}

0 commit comments

Comments
 (0)