Generates the function that is called by the python interpreter to initialize the native module
(
function: &mut syn::ItemFn,
mut options: PyModuleOptions,
)
| 464 | /// Generates the function that is called by the python interpreter to initialize the native |
| 465 | /// module |
| 466 | pub fn pymodule_function_impl( |
| 467 | function: &mut syn::ItemFn, |
| 468 | mut options: PyModuleOptions, |
| 469 | ) -> Result<TokenStream> { |
| 470 | options.take_pyo3_options(&mut function.attrs)?; |
| 471 | process_functions_in_module(&options, function)?; |
| 472 | let ctx = &Ctx::new(&options.krate, None); |
| 473 | let Ctx { pyo3_path, .. } = ctx; |
| 474 | let ident = &function.sig.ident; |
| 475 | let name = options |
| 476 | .name |
| 477 | .map_or_else(|| ident.unraw(), |name| name.value.0); |
| 478 | let vis = &function.vis; |
| 479 | let doc = get_doc(&function.attrs, None); |
| 480 | |
| 481 | let gil_used = options.gil_used.is_some_and(|op| op.value.value); |
| 482 | |
| 483 | let initialization = module_initialization( |
| 484 | &name.to_string(), |
| 485 | &name, |
| 486 | ctx, |
| 487 | quote! { ModuleExec::__pyo3_module_exec }, |
| 488 | false, |
| 489 | gil_used, |
| 490 | doc.as_ref(), |
| 491 | )?; |
| 492 | |
| 493 | #[cfg(feature = "experimental-inspect")] |
| 494 | let introspection = module_introspection_code( |
| 495 | pyo3_path, |
| 496 | &name.unraw().to_string(), |
| 497 | &[], |
| 498 | &[], |
| 499 | doc.as_ref(), |
| 500 | true, |
| 501 | ); |
| 502 | #[cfg(not(feature = "experimental-inspect"))] |
| 503 | let introspection = quote! {}; |
| 504 | #[cfg(feature = "experimental-inspect")] |
| 505 | let introspection_id = introspection_id_const(); |
| 506 | #[cfg(not(feature = "experimental-inspect"))] |
| 507 | let introspection_id = quote! {}; |
| 508 | |
| 509 | // Module function called with optional Python<'_> marker as first arg, followed by the module. |
| 510 | let mut module_args = Vec::new(); |
| 511 | if function.sig.inputs.len() == 2 { |
| 512 | module_args.push(quote!(module.py())); |
| 513 | } |
| 514 | module_args.push(quote!(::core::convert::Into::into(module))); |
| 515 | |
| 516 | Ok(quote! { |
| 517 | #[doc(hidden)] |
| 518 | #vis mod #ident { |
| 519 | #initialization |
| 520 | #introspection |
| 521 | #introspection_id |
| 522 | } |
| 523 |
no test coverage detected