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

Don't use intocallback in method macros #2664

Merged
merged 8 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 3 additions & 8 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,12 @@ impl<'a> FnSpec<'a> {
quote!(#func_name)
};

// The method call is necessary to generate a decent error message.
let rust_call = |args: Vec<TokenStream>| {
quote! {
let mut ret = #rust_name(#self_arg #(#args),*);

if false {
use _pyo3::impl_::ghost::IntoPyResult;
ret.assert_into_py_result();
}

_pyo3::callback::convert(#py, ret)
let owned = _pyo3::callback::OkWrap::wrap(ret, #py);
owned.map(|obj| _pyo3::conversion::IntoPyPointer::into_ptr(obj))
.map_err(::core::convert::Into::into)
}
};

Expand Down
7 changes: 2 additions & 5 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,8 @@ fn impl_py_class_attribute(cls: &syn::Type, spec: &FnSpec<'_>) -> syn::Result<Me
fn #wrapper_ident(py: _pyo3::Python<'_>) -> _pyo3::PyResult<_pyo3::PyObject> {
#deprecations
let mut ret = #fncall;
if false {
use _pyo3::impl_::ghost::IntoPyResult;
ret.assert_into_py_result();
}
_pyo3::callback::convert(py, ret)
let owned = _pyo3::callback::OkWrap::wrap(ret, py);
owned.map_err(::core::convert::Into::into)
}
};

Expand Down
27 changes: 26 additions & 1 deletion src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ffi::{self, Py_hash_t};
use crate::impl_::panic::PanicTrap;
use crate::panic::PanicException;
use crate::{GILPool, IntoPyPointer};
use crate::{IntoPy, PyObject, Python};
use crate::{IntoPy, Py, PyAny, PyObject, Python};
use std::any::Any;
use std::os::raw::c_int;
use std::panic::UnwindSafe;
Expand Down Expand Up @@ -134,6 +134,31 @@ pub trait WrappingCastTo<T> {
fn wrapping_cast(self) -> T;
}

pub trait OkWrap<T> {
mejrs marked this conversation as resolved.
Show resolved Hide resolved
type Error;
fn wrap(self, py: Python<'_>) -> Result<Py<PyAny>, Self::Error>;
}

impl<T> OkWrap<T> for T
where
T: IntoPy<PyObject>,
{
type Error = PyErr;
fn wrap(self, py: Python<'_>) -> PyResult<Py<PyAny>> {
Ok(self.into_py(py))
}
}

impl<T, E> OkWrap<T> for Result<T, E>
where
T: IntoPy<PyObject>,
{
type Error = E;
fn wrap(self, py: Python<'_>) -> Result<Py<PyAny>, Self::Error> {
self.map(|o| o.into_py(py))
}
}

macro_rules! wrapping_cast {
($from:ty, $to:ty) => {
impl WrappingCastTo<$to> for $from {
Expand Down
1 change: 0 additions & 1 deletion src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod deprecations;
pub mod extract_argument;
pub mod freelist;
pub mod frompyobject;
pub mod ghost;
pub(crate) mod not_send;
pub mod panic;
pub mod pycell;
Expand Down
18 changes: 0 additions & 18 deletions src/impl_/ghost.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ impl PyTypeBuilder {
module_name: Option<&'static str>,
basicsize: usize,
) -> PyResult<*mut ffi::PyTypeObject> {
#![allow(clippy::useless_conversion)]

self.finalize_methods_and_properties();

if !self.has_new {
Expand Down Expand Up @@ -378,7 +380,6 @@ impl PyTypeBuilder {
itemsize: 0,
// `c_ulong` and `c_uint` have the same size
// on some platforms (like windows)
#[allow(clippy::useless_conversion)]
davidhewitt marked this conversation as resolved.
Show resolved Hide resolved
flags: (ffi::Py_TPFLAGS_DEFAULT | self.class_flags)
.try_into()
.unwrap(),
Expand Down
38 changes: 13 additions & 25 deletions tests/ui/invalid_result_conversion.stderr
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
error[E0599]: no method named `assert_into_py_result` found for enum `Result` in the current scope
error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
--> tests/ui/invalid_result_conversion.rs:21:1
|
21 | #[pyfunction]
| ^^^^^^^^^^^^^ method not found in `Result<(), MyError>`
| ^^^^^^^^^^^^^ the trait `From<MyError>` is not implemented for `PyErr`
|
note: the method `assert_into_py_result` exists on the type `()`
--> src/impl_/ghost.rs
|
| fn assert_into_py_result(&mut self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: the following other types implement trait `From<T>`:
<PyErr as From<&CancelledError>>
<PyErr as From<&IncompleteReadError>>
<PyErr as From<&InvalidStateError>>
<PyErr as From<&LimitOverrunError>>
<PyErr as From<&PanicException>>
<PyErr as From<&PyArithmeticError>>
<PyErr as From<&PyAssertionError>>
<PyErr as From<&PyAttributeError>>
and 77 others
= note: required because of the requirements on the impl of `Into<PyErr>` for `MyError`
= note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use the `?` operator to extract the `()` value, propagating a `Result::Err` value to the caller
|
21 | #[pyfunction]?
| +

error[E0277]: the trait bound `Result<(), MyError>: IntoPyCallbackOutput<_>` is not satisfied
--> tests/ui/invalid_result_conversion.rs:21:1
|
21 | #[pyfunction]
| ^^^^^^^^^^^^^ the trait `IntoPyCallbackOutput<_>` is not implemented for `Result<(), MyError>`
|
= help: the trait `IntoPyCallbackOutput<U>` is implemented for `Result<T, E>`
note: required by a bound in `pyo3::callback::convert`
--> src/callback.rs
|
| T: IntoPyCallbackOutput<U>,
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `pyo3::callback::convert`
= note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
63 changes: 18 additions & 45 deletions tests/ui/missing_intopy.stderr
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
error[E0599]: the method `assert_into_py_result` exists for struct `Blah`, but its trait bounds were not satisfied
--> tests/ui/missing_intopy.rs:3:1
|
1 | struct Blah;
| -----------
| |
| method `assert_into_py_result` not found for this struct
| doesn't satisfy `Blah: IntoPy<Py<PyAny>>`
| doesn't satisfy `Blah: IntoPyResult<Blah>`
2 |
3 | #[pyo3::pyfunction]
| ^^^^^^^^^^^^^^^^^^^ method cannot be called on `Blah` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Blah: IntoPy<Py<PyAny>>`
which is required by `Blah: IntoPyResult<Blah>`
note: the following trait must be implemented
--> src/conversion.rs
|
| pub trait IntoPy<T>: Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `pyo3::pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Blah: IntoPyCallbackOutput<_>` is not satisfied
--> tests/ui/missing_intopy.rs:3:1
|
3 | #[pyo3::pyfunction]
| ^^^^^^^^^^^^^^^^^^^ the trait `IntoPyCallbackOutput<_>` is not implemented for `Blah`
|
= help: the following other types implement trait `IntoPyCallbackOutput<Target>`:
<() as IntoPyCallbackOutput<()>>
<() as IntoPyCallbackOutput<i32>>
<*mut PyObject as IntoPyCallbackOutput<*mut PyObject>>
<HashCallbackOutput as IntoPyCallbackOutput<isize>>
<IterANextOutput<Py<PyAny>, Py<PyAny>> as IntoPyCallbackOutput<*mut PyObject>>
<IterANextOutput<T, U> as IntoPyCallbackOutput<IterANextOutput<Py<PyAny>, Py<PyAny>>>>
<IterNextOutput<Py<PyAny>, Py<PyAny>> as IntoPyCallbackOutput<*mut PyObject>>
<IterNextOutput<T, U> as IntoPyCallbackOutput<IterNextOutput<Py<PyAny>, Py<PyAny>>>>
and 7 others
note: required by a bound in `pyo3::callback::convert`
--> src/callback.rs
|
| T: IntoPyCallbackOutput<U>,
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `pyo3::callback::convert`
= note: this error originates in the attribute macro `pyo3::pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
mejrs marked this conversation as resolved.
Show resolved Hide resolved
error[E0277]: the trait bound `Blah: IntoPy<Py<PyAny>>` is not satisfied
--> tests/ui/missing_intopy.rs:3:1
|
3 | #[pyo3::pyfunction]
| ^^^^^^^^^^^^^^^^^^^ the trait `IntoPy<Py<PyAny>>` is not implemented for `Blah`
|
= help: the following other types implement trait `IntoPy<T>`:
<&'a OsString as IntoPy<Py<PyAny>>>
<&'a Path as IntoPy<Py<PyAny>>>
<&'a PathBuf as IntoPy<Py<PyAny>>>
<&'a PyErr as IntoPy<Py<PyAny>>>
<&'a String as IntoPy<Py<PyAny>>>
<&'a [u8] as IntoPy<Py<PyAny>>>
<&'a str as IntoPy<Py<PyAny>>>
<&'a str as IntoPy<Py<PyString>>>
and 173 others
= note: required because of the requirements on the impl of `OkWrap<Blah>` for `Blah`
= note: this error originates in the attribute macro `pyo3::pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)