feature gate deprecated APIs for PyDict
(#4108)
This commit is contained in:
parent
c2ac9a98e2
commit
5d2f5b5702
|
@ -674,6 +674,7 @@ mod test_no_clone {}
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[cfg(feature = "gil-refs")]
|
||||
#[allow(deprecated)]
|
||||
mod deprecated {
|
||||
use super::super::PyTryFrom;
|
||||
|
|
|
@ -2034,7 +2034,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_call_for_non_existing_method() {
|
||||
Python::with_gil(|py| {
|
||||
let obj: PyObject = PyDict::new(py).into();
|
||||
let obj: PyObject = PyDict::new_bound(py).into();
|
||||
assert!(obj.call_method0(py, "asdf").is_err());
|
||||
assert!(obj
|
||||
.call_method(py, "nonexistent_method", (1,), None)
|
||||
|
@ -2047,7 +2047,7 @@ mod tests {
|
|||
#[test]
|
||||
fn py_from_dict() {
|
||||
let dict: Py<PyDict> = Python::with_gil(|py| {
|
||||
let native = PyDict::new(py);
|
||||
let native = PyDict::new_bound(py);
|
||||
Py::from(native)
|
||||
});
|
||||
|
||||
|
@ -2057,7 +2057,7 @@ mod tests {
|
|||
#[test]
|
||||
fn pyobject_from_py() {
|
||||
Python::with_gil(|py| {
|
||||
let dict: Py<PyDict> = PyDict::new(py).into();
|
||||
let dict: Py<PyDict> = PyDict::new_bound(py).unbind();
|
||||
let cnt = dict.get_refcnt(py);
|
||||
let p: PyObject = dict.into();
|
||||
assert_eq!(p.get_refcnt(py), cnt);
|
||||
|
|
|
@ -57,12 +57,10 @@ pyobject_native_type_core!(
|
|||
|
||||
impl PyDict {
|
||||
/// Deprecated form of [`new_bound`][PyDict::new_bound].
|
||||
#[cfg_attr(
|
||||
not(feature = "gil-refs"),
|
||||
deprecated(
|
||||
since = "0.21.0",
|
||||
note = "`PyDict::new` will be replaced by `PyDict::new_bound` in a future PyO3 version"
|
||||
)
|
||||
#[cfg(feature = "gil-refs")]
|
||||
#[deprecated(
|
||||
since = "0.21.0",
|
||||
note = "`PyDict::new` will be replaced by `PyDict::new_bound` in a future PyO3 version"
|
||||
)]
|
||||
#[inline]
|
||||
pub fn new(py: Python<'_>) -> &PyDict {
|
||||
|
@ -75,12 +73,10 @@ impl PyDict {
|
|||
}
|
||||
|
||||
/// Deprecated form of [`from_sequence_bound`][PyDict::from_sequence_bound].
|
||||
#[cfg_attr(
|
||||
all(not(any(PyPy, GraalPy)), not(feature = "gil-refs")),
|
||||
deprecated(
|
||||
since = "0.21.0",
|
||||
note = "`PyDict::from_sequence` will be replaced by `PyDict::from_sequence_bound` in a future PyO3 version"
|
||||
)
|
||||
#[cfg(feature = "gil-refs")]
|
||||
#[deprecated(
|
||||
since = "0.21.0",
|
||||
note = "`PyDict::from_sequence` will be replaced by `PyDict::from_sequence_bound` in a future PyO3 version"
|
||||
)]
|
||||
#[inline]
|
||||
#[cfg(not(any(PyPy, GraalPy)))]
|
||||
|
@ -751,12 +747,10 @@ pub(crate) use borrowed_iter::BorrowedDictIter;
|
|||
pub trait IntoPyDict: Sized {
|
||||
/// Converts self into a `PyDict` object pointer. Whether pointer owned or borrowed
|
||||
/// depends on implementation.
|
||||
#[cfg_attr(
|
||||
not(feature = "gil-refs"),
|
||||
deprecated(
|
||||
since = "0.21.0",
|
||||
note = "`IntoPyDict::into_py_dict` will be replaced by `IntoPyDict::into_py_dict_bound` in a future PyO3 version"
|
||||
)
|
||||
#[cfg(feature = "gil-refs")]
|
||||
#[deprecated(
|
||||
since = "0.21.0",
|
||||
note = "`IntoPyDict::into_py_dict` will be replaced by `IntoPyDict::into_py_dict_bound` in a future PyO3 version"
|
||||
)]
|
||||
fn into_py_dict(self, py: Python<'_>) -> &PyDict {
|
||||
Self::into_py_dict_bound(self, py).into_gil_ref()
|
||||
|
@ -821,7 +815,6 @@ where
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[cfg(not(any(PyPy, GraalPy)))]
|
||||
|
@ -853,8 +846,8 @@ mod tests {
|
|||
#[cfg(not(any(PyPy, GraalPy)))]
|
||||
fn test_from_sequence() {
|
||||
Python::with_gil(|py| {
|
||||
let items = PyList::new(py, &vec![("a", 1), ("b", 2)]);
|
||||
let dict = PyDict::from_sequence(items).unwrap();
|
||||
let items = PyList::new_bound(py, &vec![("a", 1), ("b", 2)]);
|
||||
let dict = PyDict::from_sequence_bound(&items).unwrap();
|
||||
assert_eq!(
|
||||
1,
|
||||
dict.get_item("a")
|
||||
|
@ -884,8 +877,8 @@ mod tests {
|
|||
#[cfg(not(any(PyPy, GraalPy)))]
|
||||
fn test_from_sequence_err() {
|
||||
Python::with_gil(|py| {
|
||||
let items = PyList::new(py, &vec!["a", "b"]);
|
||||
assert!(PyDict::from_sequence(items).is_err());
|
||||
let items = PyList::new_bound(py, &vec!["a", "b"]);
|
||||
assert!(PyDict::from_sequence_bound(&items).is_err());
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -913,11 +906,11 @@ mod tests {
|
|||
Python::with_gil(|py| {
|
||||
let mut v = HashMap::new();
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert_eq!(0, dict.len());
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict2: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict2 = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert_eq!(1, dict2.len());
|
||||
});
|
||||
}
|
||||
|
@ -928,7 +921,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert!(dict.contains(7i32).unwrap());
|
||||
assert!(!dict.contains(8i32).unwrap());
|
||||
});
|
||||
|
@ -940,7 +933,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert_eq!(
|
||||
32,
|
||||
dict.get_item(7i32)
|
||||
|
@ -961,7 +954,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast::<PyDict>(py).unwrap();
|
||||
assert_eq!(
|
||||
32,
|
||||
dict.get_item_with_error(7i32)
|
||||
|
@ -984,7 +977,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
|
||||
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
|
||||
assert_eq!(
|
||||
|
@ -1010,11 +1003,10 @@ mod tests {
|
|||
fn test_set_item_refcnt() {
|
||||
Python::with_gil(|py| {
|
||||
let cnt;
|
||||
let obj = py.eval("object()", None, None).unwrap();
|
||||
let obj = py.eval_bound("object()", None, None).unwrap();
|
||||
{
|
||||
let _pool = unsafe { crate::GILPool::new() };
|
||||
cnt = obj.get_refcnt();
|
||||
let _dict = [(10, obj)].into_py_dict_bound(py);
|
||||
let _dict = [(10, &obj)].into_py_dict_bound(py);
|
||||
}
|
||||
{
|
||||
assert_eq!(cnt, obj.get_refcnt());
|
||||
|
@ -1028,7 +1020,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
|
||||
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
|
||||
assert_eq!(32i32, v[&7i32]); // not updated!
|
||||
|
@ -1042,7 +1034,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert!(dict.del_item(7i32).is_ok());
|
||||
assert_eq!(0, dict.len());
|
||||
assert!(dict.get_item(7i32).unwrap().is_none());
|
||||
|
@ -1055,7 +1047,7 @@ mod tests {
|
|||
let mut v = HashMap::new();
|
||||
v.insert(7, 32);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
assert!(dict.del_item(7i32).is_ok()); // change
|
||||
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
|
||||
});
|
||||
|
@ -1069,7 +1061,7 @@ mod tests {
|
|||
v.insert(8, 42);
|
||||
v.insert(9, 123);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
||||
let mut key_sum = 0;
|
||||
let mut value_sum = 0;
|
||||
|
@ -1091,7 +1083,7 @@ mod tests {
|
|||
v.insert(8, 42);
|
||||
v.insert(9, 123);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
||||
let mut key_sum = 0;
|
||||
for el in dict.keys() {
|
||||
|
@ -1109,7 +1101,7 @@ mod tests {
|
|||
v.insert(8, 42);
|
||||
v.insert(9, 123);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
|
||||
let mut values_sum = 0;
|
||||
for el in dict.values() {
|
||||
|
@ -1127,7 +1119,7 @@ mod tests {
|
|||
v.insert(8, 42);
|
||||
v.insert(9, 123);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
let mut key_sum = 0;
|
||||
let mut value_sum = 0;
|
||||
for (key, value) in dict {
|
||||
|
@ -1168,7 +1160,7 @@ mod tests {
|
|||
v.insert(9, 123);
|
||||
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
|
||||
for (key, value) in dict {
|
||||
dict.set_item(key, value.extract::<i32>().unwrap() + 7)
|
||||
|
@ -1186,7 +1178,7 @@ mod tests {
|
|||
v.insert(i * 2, i * 2);
|
||||
}
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
|
||||
for (i, (key, value)) in dict.iter().enumerate() {
|
||||
let key = key.extract::<i32>().unwrap();
|
||||
|
@ -1211,7 +1203,7 @@ mod tests {
|
|||
v.insert(i * 2, i * 2);
|
||||
}
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
|
||||
for (i, (key, value)) in dict.iter().enumerate() {
|
||||
let key = key.extract::<i32>().unwrap();
|
||||
|
@ -1235,7 +1227,7 @@ mod tests {
|
|||
v.insert(8, 42);
|
||||
v.insert(9, 123);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
|
||||
let mut iter = dict.iter();
|
||||
assert_eq!(iter.size_hint(), (v.len(), Some(v.len())));
|
||||
|
@ -1261,7 +1253,7 @@ mod tests {
|
|||
v.insert(8, 42);
|
||||
v.insert(9, 123);
|
||||
let ob = v.to_object(py);
|
||||
let dict: &PyDict = ob.downcast(py).unwrap();
|
||||
let dict = ob.downcast_bound::<PyDict>(py).unwrap();
|
||||
let mut key_sum = 0;
|
||||
let mut value_sum = 0;
|
||||
for (key, value) in dict {
|
||||
|
@ -1404,7 +1396,7 @@ mod tests {
|
|||
let dict = abc_dict(py);
|
||||
let keys = dict.call_method0("keys").unwrap();
|
||||
assert!(keys
|
||||
.is_instance(&py.get_type::<PyDictKeys>().as_borrowed())
|
||||
.is_instance(&py.get_type_bound::<PyDictKeys>().as_borrowed())
|
||||
.unwrap());
|
||||
})
|
||||
}
|
||||
|
@ -1416,7 +1408,7 @@ mod tests {
|
|||
let dict = abc_dict(py);
|
||||
let values = dict.call_method0("values").unwrap();
|
||||
assert!(values
|
||||
.is_instance(&py.get_type::<PyDictValues>().as_borrowed())
|
||||
.is_instance(&py.get_type_bound::<PyDictValues>().as_borrowed())
|
||||
.unwrap());
|
||||
})
|
||||
}
|
||||
|
@ -1428,7 +1420,7 @@ mod tests {
|
|||
let dict = abc_dict(py);
|
||||
let items = dict.call_method0("items").unwrap();
|
||||
assert!(items
|
||||
.is_instance(&py.get_type::<PyDictItems>().as_borrowed())
|
||||
.is_instance(&py.get_type_bound::<PyDictItems>().as_borrowed())
|
||||
.unwrap());
|
||||
})
|
||||
}
|
||||
|
|
|
@ -158,7 +158,7 @@ mod tests {
|
|||
use super::PyIterator;
|
||||
use crate::exceptions::PyTypeError;
|
||||
use crate::gil::GILPool;
|
||||
use crate::types::{PyDict, PyList};
|
||||
use crate::types::{PyAnyMethods, PyDict, PyList};
|
||||
use crate::{Py, PyAny, Python, ToPyObject};
|
||||
|
||||
#[test]
|
||||
|
@ -244,10 +244,11 @@ def fibonacci(target):
|
|||
"#;
|
||||
|
||||
Python::with_gil(|py| {
|
||||
let context = PyDict::new(py);
|
||||
py.run(fibonacci_generator, None, Some(context)).unwrap();
|
||||
let context = PyDict::new_bound(py);
|
||||
py.run_bound(fibonacci_generator, None, Some(&context))
|
||||
.unwrap();
|
||||
|
||||
let generator = py.eval("fibonacci(5)", None, Some(context)).unwrap();
|
||||
let generator = py.eval_bound("fibonacci(5)", None, Some(&context)).unwrap();
|
||||
for (actual, expected) in generator.iter().unwrap().zip(&[1, 1, 2, 3, 5]) {
|
||||
let actual = actual.unwrap().extract::<usize>().unwrap();
|
||||
assert_eq!(actual, *expected)
|
||||
|
|
|
@ -435,6 +435,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "gil-refs")]
|
||||
#[allow(deprecated)]
|
||||
fn test_mapping_try_from() {
|
||||
use crate::PyTryFrom;
|
||||
|
|
Loading…
Reference in a new issue