@@ -73,7 +73,7 @@ pub fn is_forwarded_args(signature: &FunctionSignature<'_>) -> bool {
7373 )
7474}
7575
76- fn check_arg_for_gil_refs (
76+ pub ( crate ) fn check_arg_for_gil_refs (
7777 tokens : TokenStream ,
7878 gil_refs_checker : syn:: Ident ,
7979 ctx : & Ctx ,
@@ -120,7 +120,11 @@ pub fn impl_arg_params(
120120 . iter ( )
121121 . enumerate ( )
122122 . map ( |( i, arg) | {
123- impl_arg_param ( arg, i, & mut 0 , & args_array, holders, ctx) . map ( |tokens| {
123+ let from_py_with =
124+ syn:: Ident :: new ( & format ! ( "from_py_with_{}" , i) , Span :: call_site ( ) ) ;
125+ let arg_value = quote ! ( #args_array[ 0 ] . as_deref( ) ) ;
126+
127+ impl_arg_param ( arg, from_py_with, arg_value, holders, ctx) . map ( |tokens| {
124128 check_arg_for_gil_refs (
125129 tokens,
126130 holders. push_gil_refs_checker ( arg. ty . span ( ) ) ,
@@ -161,14 +165,20 @@ pub fn impl_arg_params(
161165
162166 let num_params = positional_parameter_names. len ( ) + keyword_only_parameters. len ( ) ;
163167
164- let mut option_pos = 0 ;
168+ let mut option_pos = 0usize ;
165169 let param_conversion = spec
166170 . signature
167171 . arguments
168172 . iter ( )
169173 . enumerate ( )
170174 . map ( |( i, arg) | {
171- impl_arg_param ( arg, i, & mut option_pos, & args_array, holders, ctx) . map ( |tokens| {
175+ let from_py_with = syn:: Ident :: new ( & format ! ( "from_py_with_{}" , i) , Span :: call_site ( ) ) ;
176+ let arg_value = quote ! ( #args_array[ #option_pos] . as_deref( ) ) ;
177+ if arg. is_regular ( ) {
178+ option_pos += 1 ;
179+ }
180+
181+ impl_arg_param ( arg, from_py_with, arg_value, holders, ctx) . map ( |tokens| {
172182 check_arg_for_gil_refs ( tokens, holders. push_gil_refs_checker ( arg. ty . span ( ) ) , ctx)
173183 } )
174184 } )
@@ -234,11 +244,10 @@ pub fn impl_arg_params(
234244
235245/// Re option_pos: The option slice doesn't contain the py: Python argument, so the argument
236246/// index and the index in option diverge when using py: Python
237- fn impl_arg_param (
247+ pub ( crate ) fn impl_arg_param (
238248 arg : & FnArg < ' _ > ,
239- pos : usize ,
240- option_pos : & mut usize ,
241- args_array : & syn:: Ident ,
249+ from_py_with : syn:: Ident ,
250+ arg_value : TokenStream , // expected type: Option<&'a Bound<'py, PyAny>>
242251 holders : & mut Holders ,
243252 ctx : & Ctx ,
244253) -> Result < TokenStream > {
@@ -291,9 +300,6 @@ fn impl_arg_param(
291300 } ) ;
292301 }
293302
294- let arg_value = quote_arg_span ! ( #args_array[ #option_pos] ) ;
295- * option_pos += 1 ;
296-
297303 let mut default = arg. default . as_ref ( ) . map ( |expr| quote ! ( #expr) ) ;
298304
299305 // Option<T> arguments have special treatment: the default should be specified _without_ the
@@ -312,11 +318,10 @@ fn impl_arg_param(
312318 . map ( |attr| & attr. value )
313319 . is_some ( )
314320 {
315- let from_py_with = syn:: Ident :: new ( & format ! ( "from_py_with_{}" , pos) , Span :: call_site ( ) ) ;
316321 if let Some ( default) = default {
317322 quote_arg_span ! {
318323 #pyo3_path:: impl_:: extract_argument:: from_py_with_with_default(
319- #arg_value. as_deref ( ) ,
324+ #arg_value,
320325 #name_str,
321326 #from_py_with as fn ( _) -> _,
322327 #[ allow( clippy:: redundant_closure) ]
@@ -328,7 +333,7 @@ fn impl_arg_param(
328333 } else {
329334 quote_arg_span ! {
330335 #pyo3_path:: impl_:: extract_argument:: from_py_with(
331- & #pyo3_path:: impl_:: extract_argument:: unwrap_required_argument( #arg_value) ,
336+ #pyo3_path:: impl_:: extract_argument:: unwrap_required_argument( #arg_value) ,
332337 #name_str,
333338 #from_py_with as fn ( _) -> _,
334339 ) ?
@@ -338,7 +343,7 @@ fn impl_arg_param(
338343 let holder = holders. push_holder ( arg. name . span ( ) ) ;
339344 quote_arg_span ! {
340345 #pyo3_path:: impl_:: extract_argument:: extract_optional_argument(
341- #arg_value. as_deref ( ) ,
346+ #arg_value,
342347 & mut #holder,
343348 #name_str,
344349 #[ allow( clippy:: redundant_closure) ]
@@ -351,7 +356,7 @@ fn impl_arg_param(
351356 let holder = holders. push_holder ( arg. name . span ( ) ) ;
352357 quote_arg_span ! {
353358 #pyo3_path:: impl_:: extract_argument:: extract_argument_with_default(
354- #arg_value. as_deref ( ) ,
359+ #arg_value,
355360 & mut #holder,
356361 #name_str,
357362 #[ allow( clippy:: redundant_closure) ]
@@ -364,7 +369,7 @@ fn impl_arg_param(
364369 let holder = holders. push_holder ( arg. name . span ( ) ) ;
365370 quote_arg_span ! {
366371 #pyo3_path:: impl_:: extract_argument:: extract_argument(
367- & #pyo3_path:: impl_:: extract_argument:: unwrap_required_argument( #arg_value) ,
372+ #pyo3_path:: impl_:: extract_argument:: unwrap_required_argument( #arg_value) ,
368373 & mut #holder,
369374 #name_str
370375 ) ?
0 commit comments