MCPcopy Create free account
hub / github.com/PyO3/pyo3 / impl_wrap_pyfunction

Function impl_wrap_pyfunction

pyo3-macros-backend/src/pyfunction.rs:339–485  ·  view source on GitHub ↗

Generates python wrapper over a function that allows adding it to a python module as a python function

(
    func: &mut syn::ItemFn,
    options: PyFunctionOptions,
)

Source from the content-addressed store, hash-verified

337/// Generates python wrapper over a function that allows adding it to a python module as a python
338/// function
339pub 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,

Callers 2

build_py_functionFunction · 0.85

Calls 15

check_genericFunction · 0.85
noneFunction · 0.85
get_docFunction · 0.85
introspection_id_constFunction · 0.85
mapMethod · 0.80
to_stringMethod · 0.80
get_wrapper_functionMethod · 0.80
get_methoddefMethod · 0.80
get_docMethod · 0.80

Tested by

no test coverage detected