diff --git a/guide/src/class.md b/guide/src/class.md index 1fd1009d488..9a2e48d4a70 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -271,7 +271,7 @@ You can also inherit native types such as `PyDict`, if they implement However, because of some technical problems, we don't currently provide safe upcasting methods for types that inherit native types. Even in such cases, you can unsafely get a base class by raw pointer conversion. -```rust +```rust,no_run # use pyo3::prelude::*; use pyo3::types::PyDict; use pyo3::{AsPyPointer, PyNativeType}; diff --git a/pyo3-derive-backend/src/from_pyobject.rs b/pyo3-derive-backend/src/from_pyobject.rs index d4d097112ee..656532bea1e 100644 --- a/pyo3-derive-backend/src/from_pyobject.rs +++ b/pyo3-derive-backend/src/from_pyobject.rs @@ -249,7 +249,7 @@ impl<'a> Container<'a> { let self_ty = &self.path; let mut fields: Punctuated = Punctuated::new(); for i in 0..len { - fields.push(quote!(slice[#i].extract()?)); + fields.push(quote!(s.get_item(#i).extract()?)); } let msg = if self.is_enum_variant { quote!(format!( @@ -265,7 +265,6 @@ impl<'a> Container<'a> { if s.len() != #len { return Err(::pyo3::exceptions::PyValueError::new_err(#msg)) } - let slice = s.as_slice(); Ok(#self_ty(#fields)) ) } diff --git a/src/types/floatob.rs b/src/types/floatob.rs index 0b0b5341310..5b773237069 100644 --- a/src/types/floatob.rs +++ b/src/types/floatob.rs @@ -83,7 +83,9 @@ impl<'source> FromPyObject<'source> for f32 { mod test { #[cfg(not(Py_LIMITED_API))] use crate::ffi::PyFloat_AS_DOUBLE; - use crate::{AsPyPointer, Python, ToPyObject}; + #[cfg(not(Py_LIMITED_API))] + use crate::AsPyPointer; + use crate::{Python, ToPyObject}; macro_rules! num_to_py_object_and_back ( ($func_name:ident, $t1:ty, $t2:ty) => ( diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index aca7ef1ca4e..c40e751b1e8 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -1,3 +1,5 @@ +#![cfg(not(Py_LIMITED_API))] + use pyo3::buffer::PyBuffer; use pyo3::class::PyBufferProtocol; use pyo3::exceptions::PyBufferError; diff --git a/tests/test_dunder.rs b/tests/test_dunder.rs index 7b4ea91592d..e229e448181 100644 --- a/tests/test_dunder.rs +++ b/tests/test_dunder.rs @@ -457,6 +457,7 @@ fn test_cls_impl() { struct DunderDictSupport {} #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn dunder_dict_support() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -472,6 +473,7 @@ fn dunder_dict_support() { } #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn access_dunder_dict() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -493,6 +495,7 @@ struct InheritDict { } #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn inherited_dict() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -511,6 +514,7 @@ fn inherited_dict() { struct WeakRefDunderDictSupport {} #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn weakref_dunder_dict_support() { let gil = Python::acquire_gil(); let py = gil.python(); diff --git a/tests/test_gc.rs b/tests/test_gc.rs index b7aced4eecb..eb982887b93 100644 --- a/tests/test_gc.rs +++ b/tests/test_gc.rs @@ -150,6 +150,7 @@ fn gc_integration2() { struct WeakRefSupport {} #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn weakref_support() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -168,6 +169,7 @@ struct InheritWeakRef { } #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn inherited_weakref() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -269,6 +271,16 @@ impl PyGCProtocol for TraversableClass { } } +#[cfg(PyPy)] +unsafe fn get_type_traverse(tp: *mut pyo3::ffi::PyTypeObject) -> Option { + (*tp).tp_traverse +} + +#[cfg(not(PyPy))] +unsafe fn get_type_traverse(tp: *mut pyo3::ffi::PyTypeObject) -> Option { + std::mem::transmute(pyo3::ffi::PyType_GetSlot(tp, pyo3::ffi::Py_tp_traverse)) +} + #[test] fn gc_during_borrow() { let gil = Python::acquire_gil(); @@ -285,7 +297,7 @@ fn gc_during_borrow() { // get the traverse function let ty = TraversableClass::type_object(py).as_type_ptr(); - let traverse = (*ty).tp_traverse.unwrap(); + let traverse = get_type_traverse(ty).unwrap(); // create an object and check that traversing it works normally // when it's not borrowed diff --git a/tests/test_inheritance.rs b/tests/test_inheritance.rs index a2aad91a244..04fa732621f 100644 --- a/tests/test_inheritance.rs +++ b/tests/test_inheritance.rs @@ -168,7 +168,9 @@ impl SetWithName { } } +// Subclassing builtin types is not allowed in the LIMITED API. #[test] +#[cfg_attr(Py_LIMITED_API, should_panic)] fn inherit_set() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -195,7 +197,9 @@ impl DictWithName { } } +// Subclassing builtin types is not allowed in the LIMITED API. #[test] +#[cfg_attr(Py_LIMITED_API, should_panic)] fn inherit_dict() { let gil = Python::acquire_gil(); let py = gil.python(); diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index 89b4dbd01e3..f24f726682f 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -1,3 +1,4 @@ +#[cfg(not(Py_LIMITED_API))] use pyo3::buffer::PyBuffer; use pyo3::prelude::*; use pyo3::types::{PyCFunction, PyFunction}; @@ -23,6 +24,7 @@ fn test_optional_bool() { py_assert!(py, f, "f(None) == 'None'"); } +#[cfg(not(Py_LIMITED_API))] #[pyfunction] fn buffer_inplace_add(py: Python, x: PyBuffer, y: PyBuffer) { let x = x.as_mut_slice(py).unwrap(); @@ -33,6 +35,7 @@ fn buffer_inplace_add(py: Python, x: PyBuffer, y: PyBuffer) { } } +#[cfg(not(Py_LIMITED_API))] #[test] fn test_buffer_add() { let gil = Python::acquire_gil(); diff --git a/tests/test_unsendable_dict.rs b/tests/test_unsendable_dict.rs index 194b5888f83..86c974135d8 100644 --- a/tests/test_unsendable_dict.rs +++ b/tests/test_unsendable_dict.rs @@ -13,6 +13,7 @@ impl UnsendableDictClass { } #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn test_unsendable_dict() { let gil = Python::acquire_gil(); let py = gil.python(); @@ -32,6 +33,7 @@ impl UnsendableDictClassWithWeakRef { } #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn test_unsendable_dict_with_weakref() { let gil = Python::acquire_gil(); let py = gil.python(); diff --git a/tests/test_various.rs b/tests/test_various.rs index 8a80df7d251..25c5e7bcb97 100644 --- a/tests/test_various.rs +++ b/tests/test_various.rs @@ -149,6 +149,7 @@ fn add_module(py: Python, module: &PyModule) -> PyResult<()> { } #[test] +#[cfg_attr(Py_LIMITED_API, ignore)] fn test_pickle() { let gil = Python::acquire_gil(); let py = gil.python();