diff --git a/benches/bench_frompyobject.rs b/benches/bench_frompyobject.rs index f043ec2e..587a89bc 100644 --- a/benches/bench_frompyobject.rs +++ b/benches/bench_frompyobject.rs @@ -21,12 +21,12 @@ fn enum_from_pyobject(b: &mut Bencher<'_>) { }) } -fn list_via_cast_as(b: &mut Bencher<'_>) { +fn list_via_downcast(b: &mut Bencher<'_>) { Python::with_gil(|py| { let any: &PyAny = PyList::empty(py).into(); b.iter(|| { - let _list: &PyList = black_box(any).cast_as().unwrap(); + let _list: &PyList = black_box(any).downcast().unwrap(); }); }) } @@ -41,12 +41,12 @@ fn list_via_extract(b: &mut Bencher<'_>) { }) } -fn not_a_list_via_cast_as(b: &mut Bencher<'_>) { +fn not_a_list_via_downcast(b: &mut Bencher<'_>) { Python::with_gil(|py| { let any: &PyAny = PyString::new(py, "foobar").into(); b.iter(|| { - black_box(any).cast_as::().unwrap_err(); + black_box(any).downcast::().unwrap_err(); }); }) } @@ -81,9 +81,9 @@ fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) { fn criterion_benchmark(c: &mut Criterion) { c.bench_function("enum_from_pyobject", enum_from_pyobject); - c.bench_function("list_via_cast_as", list_via_cast_as); + c.bench_function("list_via_downcast", list_via_downcast); c.bench_function("list_via_extract", list_via_extract); - c.bench_function("not_a_list_via_cast_as", not_a_list_via_cast_as); + c.bench_function("not_a_list_via_downcast", not_a_list_via_downcast); c.bench_function("not_a_list_via_extract", not_a_list_via_extract); c.bench_function("not_a_list_via_extract_enum", not_a_list_via_extract_enum); } diff --git a/guide/src/python_from_rust.md b/guide/src/python_from_rust.md index 7b56d626..6cccdac4 100644 --- a/guide/src/python_from_rust.md +++ b/guide/src/python_from_rust.md @@ -321,7 +321,7 @@ fn main() -> PyResult<()> { let path = Path::new("/usr/share/python_app"); let py_app = fs::read_to_string(path.join("app.py"))?; let from_python = Python::with_gil(|py| -> PyResult> { - let syspath: &PyList = py.import("sys")?.getattr("path")?.downcast::()?; + let syspath: &PyList = py.import("sys")?.getattr("path")?.downcast()?; syspath.insert(0, &path)?; let app: Py = PyModule::from_code(py, &py_app, "", "")? .getattr("run")? diff --git a/newsfragments/2734.added.md b/newsfragments/2734.added.md new file mode 100644 index 00000000..598ec263 --- /dev/null +++ b/newsfragments/2734.added.md @@ -0,0 +1 @@ +Added `Py::downcast()` as a companion to `PyAny::downcast()`. diff --git a/newsfragments/2734.changed.md b/newsfragments/2734.changed.md new file mode 100644 index 00000000..cc5b4fba --- /dev/null +++ b/newsfragments/2734.changed.md @@ -0,0 +1,2 @@ +`PyAny::cast_as()` and `Py::cast_as()` are now deprecated in favor of +`PyAny::downcast()` and the new `Py::downcast()`. diff --git a/src/conversions/chrono.rs b/src/conversions/chrono.rs index f492df4f..6281061c 100644 --- a/src/conversions/chrono.rs +++ b/src/conversions/chrono.rs @@ -36,7 +36,7 @@ //! Python::with_gil(|py| { //! // Create an UTC datetime in python //! let py_tz = Utc.to_object(py); -//! let py_tz = py_tz.cast_as(py).unwrap(); +//! let py_tz = py_tz.downcast(py).unwrap(); //! let pydatetime = PyDateTime::new(py, 2022, 1, 1, 12, 0, 0, 0, Some(py_tz)).unwrap(); //! println!("PyDateTime: {}", pydatetime); //! // Now convert it to chrono's DateTime @@ -237,7 +237,7 @@ impl ToPyObject for DateTime { None => (ns / 1000, false), }; let tz = self.offset().fix().to_object(py); - let tz = tz.cast_as(py).unwrap(); + let tz = tz.downcast(py).unwrap(); let datetime = PyDateTime::new_with_fold(py, yy, mm, dd, h, m, s, ms, Some(tz), fold) .expect("Failed to construct datetime"); datetime.into() @@ -577,7 +577,7 @@ mod tests { let datetime = DateTime::::from_utc(datetime, Utc).to_object(py); let datetime: &PyDateTime = datetime.extract(py).unwrap(); let py_tz = Utc.to_object(py); - let py_tz = py_tz.cast_as(py).unwrap(); + let py_tz = py_tz.downcast(py).unwrap(); let py_datetime = PyDateTime::new_with_fold( py, year, @@ -617,7 +617,7 @@ mod tests { DateTime::::from_utc(datetime, offset).to_object(py); let datetime: &PyDateTime = datetime.extract(py).unwrap(); let py_tz = offset.to_object(py); - let py_tz = py_tz.cast_as(py).unwrap(); + let py_tz = py_tz.downcast(py).unwrap(); let py_datetime = PyDateTime::new_with_fold( py, year, @@ -652,7 +652,7 @@ mod tests { |name: &'static str, year, month, day, hour, minute, second, ms, py_ms, fold| { Python::with_gil(|py| { let py_tz = Utc.to_object(py); - let py_tz = py_tz.cast_as(py).unwrap(); + let py_tz = py_tz.downcast(py).unwrap(); let py_datetime = PyDateTime::new_with_fold( py, year, @@ -688,7 +688,7 @@ mod tests { Python::with_gil(|py| { let offset = FixedOffset::east_opt(3600).unwrap(); let py_tz = offset.to_object(py); - let py_tz = py_tz.cast_as(py).unwrap(); + let py_tz = py_tz.downcast(py).unwrap(); let py_datetime = PyDateTime::new_with_fold( py, year, @@ -721,14 +721,14 @@ mod tests { Python::with_gil(|py| { let py_tz = Utc.to_object(py); - let py_tz = py_tz.cast_as(py).unwrap(); + let py_tz = py_tz.downcast(py).unwrap(); let py_datetime = PyDateTime::new_with_fold(py, 2014, 5, 6, 7, 8, 9, 999_999, Some(py_tz), false) .unwrap(); assert!(py_datetime.extract::>().is_ok()); let offset = FixedOffset::east_opt(3600).unwrap(); let py_tz = offset.to_object(py); - let py_tz = py_tz.cast_as(py).unwrap(); + let py_tz = py_tz.downcast(py).unwrap(); let py_datetime = PyDateTime::new_with_fold(py, 2014, 5, 6, 7, 8, 9, 999_999, Some(py_tz), false) .unwrap(); diff --git a/src/conversions/std/array.rs b/src/conversions/std/array.rs index 7c8b4a95..fe920f04 100644 --- a/src/conversions/std/array.rs +++ b/src/conversions/std/array.rs @@ -386,7 +386,7 @@ mod tests { Python::with_gil(|py| { let array: [Foo; 8] = [Foo, Foo, Foo, Foo, Foo, Foo, Foo, Foo]; let pyobject = array.into_py(py); - let list: &PyList = pyobject.cast_as(py).unwrap(); + let list: &PyList = pyobject.downcast(py).unwrap(); let _cell: &crate::PyCell = list.get_item(4).unwrap().extract().unwrap(); }); } diff --git a/src/instance.rs b/src/instance.rs index f7c2dc54..3c80710e 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -995,11 +995,20 @@ impl PyObject { /// /// This can cast only to native Python types, not types implemented in Rust. For a more /// flexible alternative, see [`Py::extract`](struct.Py.html#method.extract). + pub fn downcast<'p, T>(&'p self, py: Python<'p>) -> Result<&T, PyDowncastError<'_>> + where + for<'py> T: PyTryFrom<'py>, + { + >::try_from(self.as_ref(py)) + } + + /// Casts the PyObject to a concrete Python object type. + #[deprecated(since = "0.18.0", note = "use downcast() instead")] pub fn cast_as<'p, D>(&'p self, py: Python<'p>) -> Result<&'p D, PyDowncastError<'_>> where D: PyTryFrom<'p>, { - >::try_from(unsafe { py.from_borrowed_ptr::(self.as_ptr()) }) + >::try_from(self.as_ref(py)) } } diff --git a/src/types/any.rs b/src/types/any.rs index 1706592b..eef7c395 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -64,29 +64,6 @@ pyobject_native_type_extract!(PyAny); pyobject_native_type_sized!(PyAny, ffi::PyObject); impl PyAny { - /// Converts this `PyAny` to a concrete Python type. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyAny, PyDict, PyList}; - /// - /// Python::with_gil(|py| { - /// let dict = PyDict::new(py); - /// assert!(dict.is_instance_of::().unwrap()); - /// let any: &PyAny = dict.as_ref(); - /// assert!(any.downcast::().is_ok()); - /// assert!(any.downcast::().is_err()); - /// }); - /// ``` - pub fn downcast(&self) -> Result<&T, PyDowncastError<'_>> - where - for<'py> T: PyTryFrom<'py>, - { - ::try_from(self) - } - /// Returns whether `self` and `other` point to the same object. To compare /// the equality of two objects (the `==` operator), use [`eq`](PyAny::eq). /// @@ -769,9 +746,8 @@ impl PyAny { unsafe { ffi::Py_TYPE(self.as_ptr()) } } - /// Casts `self` to a concrete Python object type. - /// - /// This can cast only to native Python types, not types implemented in Rust. + /// Converts this `PyAny` to a concrete Python type. + #[deprecated(since = "0.18.0", note = "use the equivalent .downcast()")] pub fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError<'_>> where D: PyTryFrom<'a>, @@ -779,6 +755,31 @@ impl PyAny { >::try_from(self) } + /// Converts this `PyAny` to a concrete Python type. + /// + /// This can cast only to native Python types, not types implemented in Rust. + /// + /// # Examples + /// + /// ```rust + /// use pyo3::prelude::*; + /// use pyo3::types::{PyAny, PyDict, PyList}; + /// + /// Python::with_gil(|py| { + /// let dict = PyDict::new(py); + /// assert!(dict.is_instance_of::().unwrap()); + /// let any: &PyAny = dict.as_ref(); + /// assert!(any.downcast::().is_ok()); + /// assert!(any.downcast::().is_err()); + /// }); + /// ``` + pub fn downcast(&self) -> Result<&T, PyDowncastError<'_>> + where + for<'py> T: PyTryFrom<'py>, + { + ::try_from(self) + } + /// Extracts some type from the Python object. /// /// This is a wrapper function around [`FromPyObject::extract()`]. diff --git a/src/types/dict.rs b/src/types/dict.rs index af139694..9e1893da 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -623,7 +623,7 @@ mod tests { let mut key_sum = 0; let mut value_sum = 0; for el in dict.items().iter() { - let tuple = el.cast_as::().unwrap(); + let tuple = el.downcast::().unwrap(); key_sum += tuple.get_item(0).unwrap().extract::().unwrap(); value_sum += tuple.get_item(1).unwrap().extract::().unwrap(); } diff --git a/src/types/mapping.rs b/src/types/mapping.rs index ad4a5029..4e11fd5e 100644 --- a/src/types/mapping.rs +++ b/src/types/mapping.rs @@ -288,7 +288,7 @@ mod tests { let mut key_sum = 0; let mut value_sum = 0; for el in mapping.items().unwrap().iter().unwrap() { - let tuple = el.unwrap().cast_as::().unwrap(); + let tuple = el.unwrap().downcast::().unwrap(); key_sum += tuple.get_item(0).unwrap().extract::().unwrap(); value_sum += tuple.get_item(1).unwrap().extract::().unwrap(); } diff --git a/src/types/mod.rs b/src/types/mod.rs index bf7f63cf..de2b92dd 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -57,7 +57,7 @@ pub use self::typeobject::PyType; /// /// # pub fn main() -> PyResult<()> { /// Python::with_gil(|py| { -/// let dict: &PyDict = py.eval("{'a':'b', 'c':'d'}", None, None)?.cast_as()?; +/// let dict: &PyDict = py.eval("{'a':'b', 'c':'d'}", None, None)?.downcast()?; /// /// for (key, value) in dict { /// println!("key: {}, value: {}", key, value); diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 9fa4010c..d9011666 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -430,7 +430,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(0, seq.len().unwrap()); let needle = 7i32.to_object(py); @@ -442,11 +442,11 @@ mod tests { fn test_seq_is_empty() { Python::with_gil(|py| { let list = vec![1].to_object(py); - let seq = list.cast_as::(py).unwrap(); + let seq = list.downcast::(py).unwrap(); assert!(!seq.is_empty().unwrap()); let vec: Vec = Vec::new(); let empty_list = vec.to_object(py); - let empty_seq = empty_list.cast_as::(py).unwrap(); + let empty_seq = empty_list.downcast::(py).unwrap(); assert!(empty_seq.is_empty().unwrap()); }); } @@ -456,7 +456,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(6, seq.len().unwrap()); let bad_needle = 7i32.to_object(py); @@ -475,7 +475,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(1, seq.get_item(0).unwrap().extract::().unwrap()); assert_eq!(1, seq.get_item(1).unwrap().extract::().unwrap()); assert_eq!(2, seq.get_item(2).unwrap().extract::().unwrap()); @@ -491,7 +491,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(1, seq[0].extract::().unwrap()); assert_eq!(1, seq[1].extract::().unwrap()); assert_eq!(2, seq[2].extract::().unwrap()); @@ -504,7 +504,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let _ = &seq[7]; }); } @@ -514,7 +514,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(vec![1, 2], seq[1..3].extract::>().unwrap()); assert_eq!(Vec::::new(), seq[3..3].extract::>().unwrap()); assert_eq!(vec![1, 2], seq[1..].extract::>().unwrap()); @@ -532,7 +532,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); seq[5..10].extract::>().unwrap(); }) } @@ -543,7 +543,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); seq[1..10].extract::>().unwrap(); }) } @@ -554,7 +554,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); #[allow(clippy::reversed_empty_ranges)] seq[2..1].extract::>().unwrap(); }) @@ -566,7 +566,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); seq[8..].extract::>().unwrap(); }) } @@ -576,7 +576,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert!(seq.del_item(10).is_err()); assert_eq!(1, seq[0].extract::().unwrap()); assert!(seq.del_item(0).is_ok()); @@ -600,7 +600,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(2, seq[1].extract::().unwrap()); assert!(seq.set_item(1, 10).is_ok()); assert_eq!(10, seq[1].extract::().unwrap()); @@ -614,7 +614,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 2]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert!(seq.set_item(1, &obj).is_ok()); assert!(seq[1].as_ptr() == obj.as_ptr()); }); @@ -629,7 +629,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!( [1, 2, 3], seq.get_slice(1, 4).unwrap().extract::<[i32; 3]>().unwrap() @@ -650,7 +650,7 @@ mod tests { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let w: Vec = vec![7, 4]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let ins = w.to_object(py); seq.set_slice(1, 4, ins.as_ref(py)).unwrap(); assert_eq!([1, 7, 4, 5, 8], seq.extract::<[i32; 5]>().unwrap()); @@ -664,7 +664,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); seq.del_slice(1, 4).unwrap(); assert_eq!([1, 5, 8], seq.extract::<[i32; 3]>().unwrap()); seq.del_slice(1, 100).unwrap(); @@ -677,7 +677,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(0, seq.index(1i32).unwrap()); assert_eq!(2, seq.index(2i32).unwrap()); assert_eq!(3, seq.index(3i32).unwrap()); @@ -693,7 +693,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert_eq!(2, seq.count(1i32).unwrap()); assert_eq!(1, seq.count(2i32).unwrap()); assert_eq!(1, seq.count(3i32).unwrap()); @@ -708,7 +708,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 1, 2, 3, 5, 8]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let mut idx = 0; for el in seq.iter().unwrap() { assert_eq!(v[idx], el.unwrap().extract::().unwrap()); @@ -723,7 +723,7 @@ mod tests { Python::with_gil(|py| { let v = vec!["It", "was", "the", "worst", "of", "times"]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let bad_needle = "blurst".to_object(py); assert!(!seq.contains(bad_needle).unwrap()); @@ -738,7 +738,7 @@ mod tests { Python::with_gil(|py| { let v: Vec = vec![1, 2, 3]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let concat_seq = seq.concat(seq).unwrap(); assert_eq!(6, concat_seq.len().unwrap()); let concat_v: Vec = vec![1, 2, 3, 1, 2, 3]; @@ -753,7 +753,7 @@ mod tests { Python::with_gil(|py| { let v = "string"; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let concat_seq = seq.concat(seq).unwrap(); assert_eq!(12, concat_seq.len().unwrap()); let concat_v = "stringstring".to_owned(); @@ -768,7 +768,7 @@ mod tests { Python::with_gil(|py| { let v = vec!["foo", "bar"]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let repeat_seq = seq.repeat(3).unwrap(); assert_eq!(6, repeat_seq.len().unwrap()); let repeated = vec!["foo", "bar", "foo", "bar", "foo", "bar"]; @@ -783,7 +783,7 @@ mod tests { Python::with_gil(|py| { let v = vec!["foo", "bar"]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let rep_seq = seq.in_place_repeat(3).unwrap(); assert_eq!(6, seq.len().unwrap()); assert!(seq.is(rep_seq)); @@ -799,7 +799,7 @@ mod tests { Python::with_gil(|py| { let v = vec!["foo", "bar"]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert!(seq.list().is_ok()); }); } @@ -819,7 +819,7 @@ mod tests { Python::with_gil(|py| { let v = ("foo", "bar"); let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert!(seq.tuple().is_ok()); }); } @@ -829,7 +829,7 @@ mod tests { Python::with_gil(|py| { let v = vec!["foo", "bar"]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); assert!(seq.tuple().is_ok()); }); } @@ -871,7 +871,7 @@ mod tests { Python::with_gil(|py| { let v = vec!["foo", "bar"]; let ob = v.to_object(py); - let seq = ob.cast_as::(py).unwrap(); + let seq = ob.downcast::(py).unwrap(); let type_ptr = seq.as_ref(); let seq_from = unsafe { ::try_from_unchecked(type_ptr) }; assert!(seq_from.list().is_ok()); diff --git a/src/types/string.rs b/src/types/string.rs index 0aac69cf..2de325aa 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -392,7 +392,7 @@ mod tests { fn test_string_data_ucs2() { Python::with_gil(|py| { let s = py.eval("'foo\\ud800'", None, None).unwrap(); - let py_string = s.cast_as::().unwrap(); + let py_string = s.downcast::().unwrap(); let data = unsafe { py_string.data().unwrap() }; assert_eq!(data, PyStringData::Ucs2(&[102, 111, 111, 0xd800])); diff --git a/tests/test_class_new.rs b/tests/test_class_new.rs index d06ccc5a..42755792 100644 --- a/tests/test_class_new.rs +++ b/tests/test_class_new.rs @@ -21,7 +21,7 @@ fn empty_class_with_new() { assert!(typeobj .call((), None) .unwrap() - .cast_as::>() + .downcast::>() .is_ok()); }); } @@ -44,7 +44,7 @@ fn unit_class_with_new() { assert!(typeobj .call((), None) .unwrap() - .cast_as::>() + .downcast::>() .is_ok()); }); } @@ -65,7 +65,7 @@ fn tuple_class_with_new() { Python::with_gil(|py| { let typeobj = py.get_type::(); let wrp = typeobj.call((42,), None).unwrap(); - let obj = wrp.cast_as::>().unwrap(); + let obj = wrp.downcast::>().unwrap(); let obj_ref = obj.borrow(); assert_eq!(obj_ref.0, 42); }); @@ -90,7 +90,7 @@ fn new_with_one_arg() { Python::with_gil(|py| { let typeobj = py.get_type::(); let wrp = typeobj.call((42,), None).unwrap(); - let obj = wrp.cast_as::>().unwrap(); + let obj = wrp.downcast::>().unwrap(); let obj_ref = obj.borrow(); assert_eq!(obj_ref._data, 42); }); @@ -121,7 +121,7 @@ fn new_with_two_args() { .call((10, 20), None) .map_err(|e| e.print(py)) .unwrap(); - let obj = wrp.cast_as::>().unwrap(); + let obj = wrp.downcast::>().unwrap(); let obj_ref = obj.borrow(); assert_eq!(obj_ref._data1, 10); assert_eq!(obj_ref._data2, 20); diff --git a/tests/test_proto_methods.rs b/tests/test_proto_methods.rs index b68a7e74..e04ce45a 100644 --- a/tests/test_proto_methods.rs +++ b/tests/test_proto_methods.rs @@ -529,7 +529,7 @@ struct GetItem {} #[pymethods] impl GetItem { fn __getitem__(&self, idx: &PyAny) -> PyResult<&'static str> { - if let Ok(slice) = idx.cast_as::() { + if let Ok(slice) = idx.downcast::() { let indices = slice.indices(1000)?; if indices.start == 100 && indices.stop == 200 && indices.step == 1 { return Ok("slice");