Skip to content

Commit 472ecb1

Browse files
davidhewittTpt
andauthored
use Borrowed in extract_argument functions (#5695)
* use `Borrowed` in `extract_argument` functions * fixup * Update src/impl_/extract_argument.rs Co-authored-by: Thomas Tanon <[email protected]> * correct doc further --------- Co-authored-by: Thomas Tanon <[email protected]>
1 parent 1f125b7 commit 472ecb1

File tree

5 files changed

+131
-44
lines changed

5 files changed

+131
-44
lines changed

pyo3-macros-backend/src/method.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,9 @@ impl SelfType {
346346
let py = syn::Ident::new("py", Span::call_site());
347347
let slf = syn::Ident::new("_slf", Span::call_site());
348348
let Ctx { pyo3_path, .. } = ctx;
349-
let bound_ref =
350-
quote! { unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &#slf) } };
351349
match self {
352350
SelfType::Receiver { span, mutable } => {
351+
let arg = quote! { unsafe { #pyo3_path::impl_::extract_argument::cast_function_argument(#py, #slf) } };
353352
let method = if *mutable {
354353
syn::Ident::new("extract_pyclass_ref_mut", *span)
355354
} else {
@@ -360,14 +359,15 @@ impl SelfType {
360359
error_mode.handle_error(
361360
quote_spanned! { *span =>
362361
#pyo3_path::impl_::extract_argument::#method::<#cls>(
363-
#bound_ref.0,
362+
#arg,
364363
&mut #holder,
365364
)
366365
},
367366
ctx,
368367
)
369368
}
370369
SelfType::TryFromBoundRef(span) => {
370+
let bound_ref = quote! { unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &#slf) } };
371371
let pyo3_path = pyo3_path.to_tokens_spanned(*span);
372372
error_mode.handle_error(
373373
quote_spanned! { *span =>
@@ -698,15 +698,15 @@ impl<'a> FnSpec<'a> {
698698
Span::call_site(),
699699
);
700700
quote! {{
701-
let _slf = unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &_slf) }.to_owned().unbind();
701+
let _slf = unsafe { #pyo3_path::impl_::extract_argument::cast_function_argument(py, _slf) }.to_owned().unbind();
702702
#(let #arg_names = #args;)*
703703
async move {
704704
// SAFETY: attached when future is polled (see `Coroutine::poll`)
705705
let assume_attached = unsafe { #pyo3_path::impl_::coroutine::AssumeAttachedInCoroutine::new() };
706706
let py = assume_attached.py();
707707
let mut holder = None;
708708
let future = function(
709-
#pyo3_path::impl_::extract_argument::#method(_slf.bind(py), &mut holder)?,
709+
#pyo3_path::impl_::extract_argument::#method(_slf.bind_borrowed(py), &mut holder)?,
710710
#(#arg_names),*
711711
);
712712
drop(py);

pyo3-macros-backend/src/params.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ pub fn impl_arg_params(
8080
.collect();
8181
return (
8282
quote! {
83-
let _args = unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &_args) };
84-
let _kwargs = #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr_or_opt(py, &_kwargs);
83+
let _args = unsafe { #pyo3_path::impl_::extract_argument::cast_function_argument(py, _args) };
84+
let _kwargs = unsafe { #pyo3_path::impl_::extract_argument::cast_optional_function_argument(py, _kwargs) };
8585
#from_py_with
8686
},
8787
arg_convert,
@@ -191,7 +191,7 @@ fn impl_arg_param(
191191
match arg {
192192
FnArg::Regular(arg) => {
193193
let from_py_with = format_ident!("from_py_with_{}", pos);
194-
let arg_value = quote!(#args_array[#option_pos].as_deref());
194+
let arg_value = quote!(#args_array[#option_pos]);
195195
*option_pos += 1;
196196
impl_regular_arg_param(arg, from_py_with, arg_value, holders, ctx)
197197
}
@@ -200,7 +200,7 @@ fn impl_arg_param(
200200
let name_str = arg.name.to_string();
201201
quote_spanned! { arg.name.span() =>
202202
#pyo3_path::impl_::extract_argument::extract_argument(
203-
&_args,
203+
_args.as_any().as_borrowed(),
204204
&mut #holder,
205205
#name_str
206206
)?
@@ -211,7 +211,7 @@ fn impl_arg_param(
211211
let name_str = arg.name.to_string();
212212
quote_spanned! { arg.name.span() =>
213213
#pyo3_path::impl_::extract_argument::extract_argument_with_default(
214-
_kwargs.as_deref(),
214+
_kwargs.as_ref().map(|d| d.as_any().as_borrowed()),
215215
&mut #holder,
216216
#name_str,
217217
|| ::std::option::Option::None
@@ -261,7 +261,7 @@ pub(crate) fn impl_regular_arg_param(
261261
if let Some(default) = default {
262262
quote_arg_span! {
263263
#pyo3_path::impl_::extract_argument::from_py_with_with_default(
264-
#arg_value,
264+
#arg_value.as_deref(),
265265
#name_str,
266266
#extractor,
267267
#[allow(clippy::redundant_closure, reason = "wrapping user-provided default expression")]
@@ -271,7 +271,7 @@ pub(crate) fn impl_regular_arg_param(
271271
)?
272272
}
273273
} else {
274-
let unwrap = quote! {unsafe { #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value) }};
274+
let unwrap = quote! {unsafe { #pyo3_path::impl_::extract_argument::unwrap_required_argument_bound(#arg_value.as_deref()) }};
275275
quote_arg_span! {
276276
#pyo3_path::impl_::extract_argument::from_py_with(
277277
#unwrap,

pyo3-macros-backend/src/pymethod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ pub fn impl_py_setter_def(
649649
let extract = impl_regular_arg_param(
650650
arg,
651651
ident,
652-
quote!(::std::option::Option::Some(_value.into())),
652+
quote!(::std::option::Option::Some(_value)),
653653
&mut holders,
654654
ctx,
655655
);
@@ -671,7 +671,7 @@ pub fn impl_py_setter_def(
671671
quote! {
672672
#[allow(unused_imports, reason = "`Probe` trait used on negative case only")]
673673
use #pyo3_path::impl_::pyclass::Probe as _;
674-
let _val = #pyo3_path::impl_::extract_argument::extract_argument(_value.into(), &mut #holder, #name)?;
674+
let _val = #pyo3_path::impl_::extract_argument::extract_argument(_value, &mut #holder, #name)?;
675675
}
676676
}
677677
};
@@ -702,7 +702,7 @@ pub fn impl_py_setter_def(
702702
_value: *mut #pyo3_path::ffi::PyObject,
703703
) -> #pyo3_path::PyResult<::std::ffi::c_int> {
704704
use ::std::convert::Into;
705-
let _value = #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &_value);
705+
let _value = #pyo3_path::impl_::extract_argument::cast_function_argument(py, _value);
706706
#init_holders
707707
#extract
708708
#warnings
@@ -1086,15 +1086,17 @@ impl Ty {
10861086
extract_error_mode,
10871087
holders,
10881088
arg,
1089-
format_ident!("ref_from_ptr"),
1089+
REF_FROM_PTR,
1090+
CAST_FUNCTION_ARGUMENT,
10901091
quote! { #ident },
10911092
ctx
10921093
),
10931094
Ty::MaybeNullObject => extract_object(
10941095
extract_error_mode,
10951096
holders,
10961097
arg,
1097-
format_ident!("ref_from_ptr"),
1098+
REF_FROM_PTR,
1099+
CAST_FUNCTION_ARGUMENT,
10981100
quote! {
10991101
if #ident.is_null() {
11001102
#pyo3_path::ffi::Py_None()
@@ -1108,15 +1110,17 @@ impl Ty {
11081110
extract_error_mode,
11091111
holders,
11101112
arg,
1111-
format_ident!("ref_from_non_null"),
1113+
REF_FROM_NON_NULL,
1114+
CAST_NON_NULL_FUNCTION_ARGUMENT,
11121115
quote! { #ident },
11131116
ctx
11141117
),
11151118
Ty::IPowModulo => extract_object(
11161119
extract_error_mode,
11171120
holders,
11181121
arg,
1119-
format_ident!("ref_from_ptr"),
1122+
REF_FROM_PTR,
1123+
CAST_FUNCTION_ARGUMENT,
11201124
quote! { #ident.as_ptr() },
11211125
ctx
11221126
),
@@ -1142,11 +1146,19 @@ impl Ty {
11421146
}
11431147
}
11441148

1149+
const REF_FROM_PTR: StaticIdent = StaticIdent::new("ref_from_ptr");
1150+
const REF_FROM_NON_NULL: StaticIdent = StaticIdent::new("ref_from_non_null");
1151+
1152+
const CAST_FUNCTION_ARGUMENT: StaticIdent = StaticIdent::new("cast_function_argument");
1153+
const CAST_NON_NULL_FUNCTION_ARGUMENT: StaticIdent =
1154+
StaticIdent::new("cast_non_null_function_argument");
1155+
11451156
fn extract_object(
11461157
extract_error_mode: ExtractErrorMode,
11471158
holders: &mut Holders,
11481159
arg: &FnArg<'_>,
1149-
ref_from_method: Ident,
1160+
ref_from_method: StaticIdent,
1161+
cast_method: StaticIdent,
11501162
source_ptr: TokenStream,
11511163
ctx: &Ctx,
11521164
) -> TokenStream {
@@ -1175,7 +1187,7 @@ fn extract_object(
11751187
#[allow(unused_imports, reason = "`Probe` trait used on negative case only")]
11761188
use #pyo3_path::impl_::pyclass::Probe as _;
11771189
#pyo3_path::impl_::extract_argument::extract_argument(
1178-
unsafe { #pyo3_path::impl_::pymethods::BoundRef::#ref_from_method(py, &#source_ptr).0 },
1190+
unsafe { #pyo3_path::impl_::extract_argument::#cast_method(py, #source_ptr) },
11791191
&mut #holder,
11801192
#name
11811193
)

0 commit comments

Comments
 (0)