From 0b9dd2682fb25035b14b7d3c36f8961313d271a8 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sat, 23 Dec 2023 18:24:05 +0000 Subject: [PATCH] fix compile errors --- pyo3-benches/benches/bench_comparisons.rs | 8 ++++---- pyo3-benches/benches/bench_extract.rs | 15 +++++++------- pyo3-benches/benches/bench_frompyobject.rs | 23 ++++++++-------------- pyo3-benches/benches/bench_list.rs | 2 +- pyo3-benches/benches/bench_tuple.rs | 2 +- pytests/src/pyfunctions.rs | 6 +++--- src/impl_/extract_argument.rs | 4 ++-- 7 files changed, 26 insertions(+), 34 deletions(-) diff --git a/pyo3-benches/benches/bench_comparisons.rs b/pyo3-benches/benches/bench_comparisons.rs index dd08558b148..7ce87b1506d 100644 --- a/pyo3-benches/benches/bench_comparisons.rs +++ b/pyo3-benches/benches/bench_comparisons.rs @@ -47,11 +47,11 @@ fn bench_ordered_dunder_methods(b: &mut Bencher<'_>) { Python::with_gil(|py| { let obj1 = Py::new(py, OrderedDunderMethods(0)) .unwrap() - .attach_into(py) + .into_bound(py) .into_any(); let obj2 = Py::new(py, OrderedDunderMethods(1)) .unwrap() - .attach_into(py) + .into_bound(py) .into_any(); b.iter(|| obj2.gt(&obj1).unwrap()); @@ -62,11 +62,11 @@ fn bench_ordered_richcmp(b: &mut Bencher<'_>) { Python::with_gil(|py| { let obj1 = Py::new(py, OrderedRichcmp(0)) .unwrap() - .attach_into(py) + .into_bound(py) .into_any(); let obj2 = Py::new(py, OrderedRichcmp(1)) .unwrap() - .attach_into(py) + .into_bound(py) .into_any(); b.iter(|| obj2.gt(&obj1).unwrap()); diff --git a/pyo3-benches/benches/bench_extract.rs b/pyo3-benches/benches/bench_extract.rs index 08879e5ecb6..f04bfee285a 100644 --- a/pyo3-benches/benches/bench_extract.rs +++ b/pyo3-benches/benches/bench_extract.rs @@ -8,7 +8,7 @@ use pyo3::{ fn extract_str_extract_success(bench: &mut Bencher<'_>) { Python::with_gil(|py| { - let s = PyString::new(py, "Hello, World!") as &PyAny; + let s = PyString::new(py, "Hello, World!").into_gil_ref(); bench.iter(|| { let v = black_box(s).extract::<&str>().unwrap(); @@ -30,11 +30,10 @@ fn extract_str_extract_fail(bench: &mut Bencher<'_>) { fn extract_str_downcast_success(bench: &mut Bencher<'_>) { Python::with_gil(|py| { - let s = PyString::new(py, "Hello, World!") as &PyAny; - let s = &s.as_borrowed(); + let s = PyString::new(py, "Hello, World!").into_any(); bench.iter(|| { - let py_str = black_box(s).downcast::().unwrap(); + let py_str = black_box(&s).downcast::().unwrap(); let v = py_str.to_str().unwrap(); black_box(v); }); @@ -56,7 +55,7 @@ fn extract_str_downcast_fail(bench: &mut Bencher<'_>) { fn extract_int_extract_success(bench: &mut Bencher<'_>) { Python::with_gil(|py| { let int_obj: PyObject = 123.into_py(py); - let int = int_obj.attach(py); + let int = int_obj.bind(py); bench.iter(|| { let v = black_box(int).extract::().unwrap(); @@ -80,7 +79,7 @@ fn extract_int_extract_fail(bench: &mut Bencher<'_>) { fn extract_int_downcast_success(bench: &mut Bencher<'_>) { Python::with_gil(|py| { let int_obj: PyObject = 123.into_py(py); - let int = int_obj.attach(py); + let int = int_obj.bind(py); bench.iter(|| { let py_int = black_box(int).downcast::().unwrap(); @@ -105,7 +104,7 @@ fn extract_int_downcast_fail(bench: &mut Bencher<'_>) { fn extract_float_extract_success(bench: &mut Bencher<'_>) { Python::with_gil(|py| { let float_obj: PyObject = 23.42.into_py(py); - let float = float_obj.attach(py); + let float = float_obj.bind(py); bench.iter(|| { let v = black_box(float).extract::().unwrap(); @@ -129,7 +128,7 @@ fn extract_float_extract_fail(bench: &mut Bencher<'_>) { fn extract_float_downcast_success(bench: &mut Bencher<'_>) { Python::with_gil(|py| { let float_obj: PyObject = 23.42.into_py(py); - let float = float_obj.attach(py); + let float = float_obj.bind(py); bench.iter(|| { let py_int = black_box(float).downcast::().unwrap(); diff --git a/pyo3-benches/benches/bench_frompyobject.rs b/pyo3-benches/benches/bench_frompyobject.rs index 6eb170ba1d4..ddf04293971 100644 --- a/pyo3-benches/benches/bench_frompyobject.rs +++ b/pyo3-benches/benches/bench_frompyobject.rs @@ -14,8 +14,7 @@ enum ManyTypes { fn enum_from_pyobject(b: &mut Bencher<'_>) { Python::with_gil(|py| { - let obj = PyString::new(py, "hello world"); - let obj = &obj.as_borrowed(); + let obj = PyString::new(py, "hello world").into_any(); b.iter(|| { let _: ManyTypes = obj.extract().unwrap(); }); @@ -46,22 +45,18 @@ fn list_via_extract(b: &mut Bencher<'_>) { fn not_a_list_via_downcast(b: &mut Bencher<'_>) { Python::with_gil(|py| { - let any: &PyAny = PyString::new(py, "foobar").into(); - let any = &any.as_borrowed(); - + let any = PyString::new(py, "foobar").into_any(); b.iter(|| { - black_box(any).downcast::().unwrap_err(); + black_box(&any).downcast::().unwrap_err(); }); }) } fn not_a_list_via_extract(b: &mut Bencher<'_>) { Python::with_gil(|py| { - let any: &PyAny = PyString::new(py, "foobar").into(); - let any = &any.as_borrowed(); - + let any = PyString::new(py, "foobar").into_any(); b.iter(|| { - black_box(any).extract::<&PyList>().unwrap_err(); + black_box(&any).extract::<&PyList>().unwrap_err(); }); }) } @@ -74,10 +69,9 @@ enum ListOrNotList<'a> { fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) { Python::with_gil(|py| { - let any: &PyAny = PyString::new(py, "foobar").into(); - let any = &any.as_borrowed(); + let any = PyString::new(py, "foobar").into_any(); - b.iter(|| match black_box(any).extract::>() { + b.iter(|| match black_box(&any).extract::>() { Ok(ListOrNotList::List(_list)) => panic!(), Ok(ListOrNotList::NotList(_any)) => (), Err(_) => panic!(), @@ -87,8 +81,7 @@ fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) { fn f64_from_pyobject(b: &mut Bencher<'_>) { Python::with_gil(|py| { - let obj = PyFloat::new(py, 1.234); - let obj = &obj.as_borrowed(); + let obj = PyFloat::new(py, 1.234).into_any(); b.iter(|| { let _: f64 = obj.extract().unwrap(); }); diff --git a/pyo3-benches/benches/bench_list.rs b/pyo3-benches/benches/bench_list.rs index 34aeeeb2462..a2862aa9e57 100644 --- a/pyo3-benches/benches/bench_list.rs +++ b/pyo3-benches/benches/bench_list.rs @@ -60,7 +60,7 @@ fn sequence_from_list(b: &mut Bencher<'_>) { Python::with_gil(|py| { const LEN: usize = 50_000; let list = PyList::new(py, 0..LEN).to_object(py); - let list = list.attach(py); + let list = list.bind(py); b.iter(|| { let _ = list.downcast::().unwrap(); }); diff --git a/pyo3-benches/benches/bench_tuple.rs b/pyo3-benches/benches/bench_tuple.rs index 1e08ac4f8c5..fce503398d4 100644 --- a/pyo3-benches/benches/bench_tuple.rs +++ b/pyo3-benches/benches/bench_tuple.rs @@ -60,7 +60,7 @@ fn sequence_from_tuple(b: &mut Bencher<'_>) { Python::with_gil(|py| { const LEN: usize = 50_000; let tuple = PyTuple::new(py, 0..LEN).to_object(py); - let tuple = tuple.attach(py); + let tuple = tuple.bind(py); b.iter(|| tuple.downcast::().unwrap()); }); } diff --git a/pytests/src/pyfunctions.rs b/pytests/src/pyfunctions.rs index 5629441c3a5..09d9468f925 100644 --- a/pytests/src/pyfunctions.rs +++ b/pytests/src/pyfunctions.rs @@ -4,9 +4,9 @@ use pyo3::types::{PyDict, PyTuple}; #[pyfunction(signature = ())] fn none() {} -type Any<'py> = Py2<'py, pyo3::PyAny>; -type Dict<'py> = Py2<'py, PyDict>; -type Tuple<'py> = Py2<'py, PyTuple>; +type Any<'py> = Bound<'py, pyo3::PyAny>; +type Dict<'py> = Bound<'py, PyDict>; +type Tuple<'py> = Bound<'py, PyTuple>; #[pyfunction(signature = (a, b = None, *, c = None))] fn simple<'py>( diff --git a/src/impl_/extract_argument.rs b/src/impl_/extract_argument.rs index 515035c24d7..15506551506 100644 --- a/src/impl_/extract_argument.rs +++ b/src/impl_/extract_argument.rs @@ -30,7 +30,7 @@ impl<'py> PyArg<'py> { } pub fn from_gil_ref(instance: &'py PyAny) -> Self { - Self(Borrowed::from_gil_ref(instance)) + Self(instance.as_borrowed()) } pub fn as_gil_ref(self) -> &'py PyAny { @@ -757,7 +757,7 @@ impl<'py> VarkeywordsHandler<'py> for DictVarkeywords { _function_description: &FunctionDescription, ) -> PyResult<()> { varkeywords - .get_or_insert_with(|| Bound::borrowed_from_gil_ref(&PyDict::new(name.0.py())).clone()) + .get_or_insert_with(|| PyDict::new(name.0.py()).as_borrowed().to_owned()) .set_item(name, value) } }