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 26, 2023
1 parent 90f3ab7 commit 9ab55e9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 34 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 = &s.as_borrowed();
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 = &obj.as_borrowed();
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 = &any.as_borrowed();

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 = &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();
});
})
}
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 = &any.as_borrowed();
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 = &obj.as_borrowed();
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
6 changes: 3 additions & 3 deletions pytests/src/pyfunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
4 changes: 2 additions & 2 deletions src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 9ab55e9

Please sign in to comment.