Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature gate deprecated APIs for PyDict #4108

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ mod test_no_clone {}

#[cfg(test)]
mod tests {
#[cfg(feature = "gil-refs")]
#[allow(deprecated)]
mod deprecated {
use super::super::PyTryFrom;
Expand Down
6 changes: 3 additions & 3 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
});

Expand All @@ -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);
Expand Down
86 changes: 39 additions & 47 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)))]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -821,7 +815,6 @@ where
}

#[cfg(test)]
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
mod tests {
use super::*;
#[cfg(not(any(PyPy, GraalPy)))]
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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());
});
}

Expand Down Expand Up @@ -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());
});
}
Expand All @@ -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());
});
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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!(
Expand All @@ -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());
Expand All @@ -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!
Expand All @@ -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());
Expand All @@ -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!
});
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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())));
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
})
}
Expand All @@ -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());
})
}
Expand All @@ -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());
})
}
Expand Down
9 changes: 5 additions & 4 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ mod tests {
}

#[test]
#[cfg(feature = "gil-refs")]
#[allow(deprecated)]
fn test_mapping_try_from() {
use crate::PyTryFrom;
Expand Down
Loading