Generates python wrapper over a function that allows adding it to a python module as a python function
(
func: &mut syn::ItemFn,
options: PyFunctionOptions,
)
| 337 | /// Generates python wrapper over a function that allows adding it to a python module as a python |
| 338 | /// function |
| 339 | pub fn impl_wrap_pyfunction( |
| 340 | func: &mut syn::ItemFn, |
| 341 | options: PyFunctionOptions, |
| 342 | ) -> syn::Result<TokenStream> { |
| 343 | check_generic(&func.sig)?; |
| 344 | let PyFunctionOptions { |
| 345 | pass_module, |
| 346 | name, |
| 347 | signature, |
| 348 | text_signature, |
| 349 | krate, |
| 350 | warnings, |
| 351 | } = options; |
| 352 | |
| 353 | let ctx = &Ctx::new(&krate, Some(&func.sig)); |
| 354 | let Ctx { pyo3_path, .. } = &ctx; |
| 355 | |
| 356 | let python_name = name |
| 357 | .as_ref() |
| 358 | .map_or_else(|| &func.sig.ident, |name| &name.value.0) |
| 359 | .unraw(); |
| 360 | |
| 361 | let tp = if pass_module.is_some() { |
| 362 | let span = match func.sig.inputs.first() { |
| 363 | Some(syn::FnArg::Typed(first_arg)) => first_arg.ty.span(), |
| 364 | Some(syn::FnArg::Receiver(_)) | None => bail_spanned!( |
| 365 | func.sig.paren_token.span.join() => "expected `&PyModule` or `Py<PyModule>` as first argument with `pass_module`" |
| 366 | ), |
| 367 | }; |
| 368 | method::FnType::FnModule(span) |
| 369 | } else { |
| 370 | method::FnType::FnStatic |
| 371 | }; |
| 372 | |
| 373 | let arguments = func |
| 374 | .sig |
| 375 | .inputs |
| 376 | .iter_mut() |
| 377 | .skip(if tp.skip_first_rust_argument_in_python_signature() { |
| 378 | 1 |
| 379 | } else { |
| 380 | 0 |
| 381 | }) |
| 382 | .map(FnArg::parse) |
| 383 | .try_combine_syn_errors()?; |
| 384 | |
| 385 | let signature = if let Some(signature) = signature { |
| 386 | FunctionSignature::from_arguments_and_attribute(arguments, signature)? |
| 387 | } else { |
| 388 | FunctionSignature::from_arguments(arguments) |
| 389 | }; |
| 390 | |
| 391 | let spec = method::FnSpec { |
| 392 | tp, |
| 393 | name: &func.sig.ident, |
| 394 | python_name, |
| 395 | signature, |
| 396 | text_signature, |
no test coverage detected