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

handle #[pyo3(from_py_with = "")] in #[setter] methods #3995

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions newsfragments/3995.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
handle `#[pyo3(from_py_with = "")]` in `#[setter]` methods
32 changes: 30 additions & 2 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
};
use crate::{quotes, utils};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, ToTokens};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{ext::IdentExt, spanned::Spanned, Result};

/// Generated code for a single pymethod item.
Expand Down Expand Up @@ -586,6 +586,34 @@ pub fn impl_py_setter_def(
}
};

let extract = if let PropertyType::Function { spec, .. } = &property_type {
Some(spec)
} else {
None
}
.and_then(|spec| {
let (_, args) = split_off_python_arg(&spec.signature.arguments);
let value_arg = &args[0];
let from_py_with = &value_arg.attrs.from_py_with.as_ref()?.value;
let name = value_arg.name.to_string();

Some(quote_spanned! { from_py_with.span() =>
let e = #pyo3_path::impl_::deprecations::GilRefs::new();
let from_py_with = #pyo3_path::impl_::deprecations::inspect_fn(#from_py_with, &e);
e.from_py_with_arg();
let _val = #pyo3_path::impl_::extract_argument::from_py_with(
&_value.into(),
#name,
from_py_with as fn(_) -> _,
)?;
})
})
.unwrap_or_else(|| {
quote! {
let _val = #pyo3_path::FromPyObject::extract_bound(_value.into())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose here we probably want to check if _val is a GIL Ref, via holders.push_gil_refs_checker?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Either way, I think worthy of its own PR and newsfragment, so I'll merge this PR now.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, maybe I'll take a look at that tomorrow

}
});

let mut cfg_attrs = TokenStream::new();
if let PropertyType::Descriptor { field, .. } = &property_type {
for attr in field
Expand All @@ -611,7 +639,7 @@ pub fn impl_py_setter_def(
.ok_or_else(|| {
#pyo3_path::exceptions::PyAttributeError::new_err("can't delete attribute")
})?;
let _val = #pyo3_path::FromPyObject::extract_bound(_value.into())?;
#extract
#init_holders
let result = #setter_impl;
#check_gil_refs
Expand Down
12 changes: 12 additions & 0 deletions tests/test_getter_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ impl ClassWithProperties {
self.num = value;
}

#[setter]
fn set_from_len(&mut self, #[pyo3(from_py_with = "extract_len")] value: i32) {
self.num = value;
}

#[getter]
fn get_data_list<'py>(&self, py: Python<'py>) -> Bound<'py, PyList> {
PyList::new_bound(py, [self.num])
}
}

fn extract_len(any: &Bound<'_, PyAny>) -> PyResult<i32> {
any.len().map(|len| len as i32)
}

#[test]
fn class_with_properties() {
Python::with_gil(|py| {
Expand All @@ -64,6 +73,9 @@ fn class_with_properties() {
py_run!(py, inst, "assert inst.get_num() == inst.unwrapped == 42");
py_run!(py, inst, "assert inst.data_list == [42]");

py_run!(py, inst, "inst.from_len = [0, 0, 0]");
py_run!(py, inst, "assert inst.get_num() == 3");

let d = [("C", py.get_type_bound::<ClassWithProperties>())].into_py_dict_bound(py);
py_assert!(py, *d, "C.DATA.__doc__ == 'a getter for data'");
});
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/deprecations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ impl MyClass {

#[staticmethod]
fn static_method_gil_ref(_any: &PyAny) {}

#[setter]
fn set_foo_gil_ref(&self, #[pyo3(from_py_with = "extract_gil_ref")] _value: i32) {}

#[setter]
fn set_foo_bound(&self, #[pyo3(from_py_with = "extract_bound")] _value: i32) {}
}

fn main() {}
Expand Down
58 changes: 32 additions & 26 deletions tests/ui/deprecations.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,70 +34,76 @@ error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::function_arg`
28 | fn static_method_gil_ref(_any: &PyAny) {}
| ^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::from_py_with_arg`: use `&Bound<'_, PyAny>` as the argument for this `from_py_with` extractor
--> tests/ui/deprecations.rs:31:53
|
31 | fn set_foo_gil_ref(&self, #[pyo3(from_py_with = "extract_gil_ref")] _value: i32) {}
| ^^^^^^^^^^^^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::function_arg`: use `&Bound<'_, T>` instead for this function argument
--> tests/ui/deprecations.rs:41:43
--> tests/ui/deprecations.rs:47:43
|
41 | fn pyfunction_with_module_gil_ref(module: &PyModule) -> PyResult<&str> {
47 | fn pyfunction_with_module_gil_ref(module: &PyModule) -> PyResult<&str> {
| ^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::function_arg`: use `&Bound<'_, T>` instead for this function argument
--> tests/ui/deprecations.rs:51:19
--> tests/ui/deprecations.rs:57:19
|
51 | fn module_gil_ref(m: &PyModule) -> PyResult<()> {
57 | fn module_gil_ref(m: &PyModule) -> PyResult<()> {
| ^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::function_arg`: use `&Bound<'_, T>` instead for this function argument
--> tests/ui/deprecations.rs:57:57
--> tests/ui/deprecations.rs:63:57
|
57 | fn module_gil_ref_with_explicit_py_arg(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
63 | fn module_gil_ref_with_explicit_py_arg(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
| ^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::from_py_with_arg`: use `&Bound<'_, PyAny>` as the argument for this `from_py_with` extractor
--> tests/ui/deprecations.rs:90:27
--> tests/ui/deprecations.rs:96:27
|
90 | #[pyo3(from_py_with = "extract_gil_ref")] _gil_ref: i32,
96 | #[pyo3(from_py_with = "extract_gil_ref")] _gil_ref: i32,
| ^^^^^^^^^^^^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::function_arg`: use `&Bound<'_, T>` instead for this function argument
--> tests/ui/deprecations.rs:96:29
|
96 | fn pyfunction_gil_ref(_any: &PyAny) {}
| ^
--> tests/ui/deprecations.rs:102:29
|
102 | fn pyfunction_gil_ref(_any: &PyAny) {}
| ^

error: use of deprecated method `pyo3::deprecations::OptionGilRefs::<std::option::Option<T>>::function_arg`: use `Option<&Bound<'_, T>>` instead for this function argument
--> tests/ui/deprecations.rs:99:36
|
99 | fn pyfunction_option_gil_ref(_any: Option<&PyAny>) {}
| ^^^^^^
--> tests/ui/deprecations.rs:105:36
|
105 | fn pyfunction_option_gil_ref(_any: Option<&PyAny>) {}
| ^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::from_py_with_arg`: use `&Bound<'_, PyAny>` as the argument for this `from_py_with` extractor
--> tests/ui/deprecations.rs:106:27
--> tests/ui/deprecations.rs:112:27
|
106 | #[pyo3(from_py_with = "PyAny::len", item("my_object"))]
112 | #[pyo3(from_py_with = "PyAny::len", item("my_object"))]
| ^^^^^^^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::from_py_with_arg`: use `&Bound<'_, PyAny>` as the argument for this `from_py_with` extractor
--> tests/ui/deprecations.rs:116:27
--> tests/ui/deprecations.rs:122:27
|
116 | #[pyo3(from_py_with = "PyAny::len")] usize,
122 | #[pyo3(from_py_with = "PyAny::len")] usize,
| ^^^^^^^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::from_py_with_arg`: use `&Bound<'_, PyAny>` as the argument for this `from_py_with` extractor
--> tests/ui/deprecations.rs:122:31
--> tests/ui/deprecations.rs:128:31
|
122 | Zip(#[pyo3(from_py_with = "extract_gil_ref")] i32),
128 | Zip(#[pyo3(from_py_with = "extract_gil_ref")] i32),
| ^^^^^^^^^^^^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<T>::from_py_with_arg`: use `&Bound<'_, PyAny>` as the argument for this `from_py_with` extractor
--> tests/ui/deprecations.rs:129:27
--> tests/ui/deprecations.rs:135:27
|
129 | #[pyo3(from_py_with = "extract_gil_ref")]
135 | #[pyo3(from_py_with = "extract_gil_ref")]
| ^^^^^^^^^^^^^^^^^

error: use of deprecated method `pyo3::deprecations::GilRefs::<pyo3::Python<'_>>::is_python`: use `wrap_pyfunction_bound!` instead
--> tests/ui/deprecations.rs:142:13
--> tests/ui/deprecations.rs:148:13
|
142 | let _ = wrap_pyfunction!(double, py);
148 | let _ = wrap_pyfunction!(double, py);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `wrap_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading