Skip to content

Commit

Permalink
fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 23, 2023
1 parent fa6508f commit 9693e6a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
8 changes: 4 additions & 4 deletions pyo3-benches/benches/bench_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
15 changes: 7 additions & 8 deletions pyo3-benches/benches/bench_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 = Bound::borrowed_from_gil_ref(&s);
let s = PyString::new(py, "Hello, World!").into_any();

bench.iter(|| {
let py_str = black_box(s).downcast::<PyString>().unwrap();
let py_str = black_box(&s).downcast::<PyString>().unwrap();
let v = py_str.to_str().unwrap();
black_box(v);
});
Expand All @@ -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::<i64>().unwrap();
Expand All @@ -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::<PyInt>().unwrap();
Expand All @@ -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::<f64>().unwrap();
Expand All @@ -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::<PyFloat>().unwrap();
Expand Down
23 changes: 8 additions & 15 deletions pyo3-benches/benches/bench_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = Bound::<PyString>::borrowed_from_gil_ref(&obj);
let obj = PyString::new(py, "hello world").into_any();
b.iter(|| {
let _: ManyTypes = obj.extract().unwrap();
});
Expand Down Expand Up @@ -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 = Bound::borrowed_from_gil_ref(&any);

let any = PyString::new(py, "foobar").into_any();
b.iter(|| {
black_box(any).downcast::<PyList>().unwrap_err();
black_box(&any).downcast::<PyList>().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 = Bound::borrowed_from_gil_ref(&any);

let any = PyString::new(py, "foobar").into_any();
b.iter(|| {
black_box(any).extract::<&PyList>().unwrap_err();
black_box(&any).extract::<&PyList>().unwrap_err();
});
})
}
Expand All @@ -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 = Bound::borrowed_from_gil_ref(&any);
let any = PyString::new(py, "foobar").into_any();

b.iter(|| match black_box(any).extract::<ListOrNotList<'_>>() {
b.iter(|| match black_box(&any).extract::<ListOrNotList<'_>>() {
Ok(ListOrNotList::List(_list)) => panic!(),
Ok(ListOrNotList::NotList(_any)) => (),
Err(_) => panic!(),
Expand All @@ -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 = Bound::<PyFloat>::borrowed_from_gil_ref(&obj);
let obj = PyFloat::new(py, 1.234).into_any();
b.iter(|| {
let _: f64 = obj.extract().unwrap();
});
Expand Down
2 changes: 1 addition & 1 deletion pyo3-benches/benches/bench_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<PySequence>().unwrap();
});
Expand Down
2 changes: 1 addition & 1 deletion pyo3-benches/benches/bench_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<PySequence>().unwrap());
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ fn py_time_to_naive_time(py_time: &Bound<'_, PyAny>) -> PyResult<NaiveTime> {

#[cfg(Py_LIMITED_API)]
fn check_type(value: &Bound<'_, PyAny>, t: &PyObject, type_name: &'static str) -> PyResult<()> {
if !value.is_instance(t.attach(value.py()))? {
if !value.is_instance(t.bind(value.py()))? {
return Err(DowncastError::new(value, type_name).into());
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ impl<T: PyClass> PyCell<T> {
/// Variant of [`try_borrow`][Self::try_borrow] which fails instead of panicking if called from the wrong thread
pub(crate) fn try_borrow_threadsafe(&self) -> Result<PyRef<'_, T>, PyBorrowError> {
self.check_threadsafe()?;
self.borrow_checker()
.try_borrow()
.map(|_| PyRef { inner: self })
self.borrow_checker().try_borrow().map(|_| PyRef {
inner: Bound::borrowed_from_gil_ref(&self).clone(),
})
}

/// Mutably borrows the value `T`, returning an error if the value is currently borrowed.
Expand Down

0 comments on commit 9693e6a

Please sign in to comment.