#![feature(prelude_import)] #![cfg(all(feature = "testing", not(any(PyPy, GraalPy))))] #[prelude_import] use std::prelude::rust_2021::*; #[macro_use] extern crate std; use pyo3::prelude::*; fn o3_addone(num: isize) -> isize { num + 1 } fn py_addone(num: isize) -> isize { o3_addone(num) } #[doc(hidden)] mod py_addone { pub(crate) struct MakeDef; pub const _PYO3_DEF: ::pyo3::impl_::pymethods::PyMethodDef = MakeDef::_PYO3_DEF; } impl py_addone::MakeDef { const _PYO3_DEF: ::pyo3::impl_::pymethods::PyMethodDef = ::pyo3::impl_::pymethods::PyMethodDef::fastcall_cfunction_with_keywords( "addone\0", ::pyo3::impl_::pymethods::PyCFunctionFastWithKeywords({ unsafe extern "C" fn trampoline( _slf: *mut ::pyo3::ffi::PyObject, _args: *const *mut ::pyo3::ffi::PyObject, _nargs: ::pyo3::ffi::Py_ssize_t, _kwnames: *mut ::pyo3::ffi::PyObject, ) -> *mut ::pyo3::ffi::PyObject { ::pyo3::impl_::trampoline::fastcall_with_keywords( _slf, _args, _nargs, _kwnames, __pyfunction_py_addone, ) } trampoline }), "addone(num)\n--\n\n\0", ); } #[allow(non_snake_case)] unsafe fn __pyfunction_py_addone<'py>( py: ::pyo3::Python<'py>, _slf: *mut ::pyo3::ffi::PyObject, _args: *const *mut ::pyo3::ffi::PyObject, _nargs: ::pyo3::ffi::Py_ssize_t, _kwnames: *mut ::pyo3::ffi::PyObject, ) -> ::pyo3::PyResult<*mut ::pyo3::ffi::PyObject> { let _slf_ref = &_slf; let function = py_addone; const DESCRIPTION: ::pyo3::impl_::extract_argument::FunctionDescription = ::pyo3::impl_::extract_argument::FunctionDescription { cls_name: ::std::option::Option::None, func_name: "addone", positional_parameter_names: &["num"], positional_only_parameters: 0usize, required_positional_parameters: 1usize, keyword_only_parameters: &[], }; let mut output = [::std::option::Option::None; 1usize]; let (_args, _kwargs) = DESCRIPTION .extract_arguments_fastcall::< ::pyo3::impl_::extract_argument::NoVarargs, ::pyo3::impl_::extract_argument::NoVarkeywords, >(py, _args, _nargs, _kwnames, &mut output)?; #[allow(clippy::let_unit_value)] let mut holder_0 = ::pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT; let gil_refs_checker_0 = ::pyo3::impl_::deprecations::GilRefs::new(); let result = ::pyo3::impl_::wrap::map_result_into_ptr( py, ::pyo3::impl_::wrap::OkWrap::wrap(function(::pyo3::impl_::deprecations::inspect_type( ::pyo3::impl_::extract_argument::extract_argument( ::pyo3::impl_::extract_argument::unwrap_required_argument( output[0usize].as_deref(), ), &mut holder_0, "num", )?, &gil_refs_checker_0, ))) .map_err(::core::convert::Into::<::pyo3::PyErr>::into), ); gil_refs_checker_0.function_arg(); result } #[allow(clippy::used_underscore_binding)] fn py_adders(module: &Bound<'_, PyModule>) -> PyResult<()> { let check_gil_refs = ::pyo3::impl_::deprecations::GilRefs::new(); let module = ::pyo3::impl_::deprecations::inspect_type(module, &check_gil_refs); check_gil_refs.function_arg(); #[allow(unknown_lints, unused_imports, redundant_imports)] use ::pyo3::{types::PyModuleMethods, PyNativeType}; module.add_function({ use py_addone as wrapped_pyfunction; let check_gil_refs = ::pyo3::impl_::deprecations::GilRefs::new(); let py_or_module = ::pyo3::impl_::deprecations::inspect_type(module, &check_gil_refs); check_gil_refs.is_python(); ::pyo3::impl_::pyfunction::WrapPyFunctionArg::wrap_pyfunction( py_or_module, &wrapped_pyfunction::_PYO3_DEF, ) }?)?; Ok(()) } mod py_adders { #[doc(hidden)] pub const __PYO3_NAME: &'static str = "adders\u{0}"; pub(super) struct MakeDef; #[doc(hidden)] pub static _PYO3_DEF: ::pyo3::impl_::pymodule::ModuleDef = MakeDef::make_def(); /// This autogenerated function is called by the python interpreter when importing /// the module. #[doc(hidden)] #[export_name = "PyInit_adders"] pub unsafe extern "C" fn __pyo3_init() -> *mut ::pyo3::ffi::PyObject { ::pyo3::impl_::trampoline::module_init(|py| _PYO3_DEF.make_module(py)) } } impl py_adders::MakeDef { const fn make_def() -> ::pyo3::impl_::pymodule::ModuleDef { fn __pyo3_pymodule( module: &::pyo3::Bound<'_, ::pyo3::types::PyModule>, ) -> ::pyo3::PyResult<()> { py_adders(::std::convert::Into::into( ::pyo3::impl_::pymethods::BoundRef(module), )) } const INITIALIZER: ::pyo3::impl_::pymodule::ModuleInitializer = ::pyo3::impl_::pymodule::ModuleInitializer(__pyo3_pymodule); unsafe { ::pyo3::impl_::pymodule::ModuleDef::new(py_adders::__PYO3_NAME, "\0", INITIALIZER) } } } extern crate test; #[cfg(test)] #[rustc_test_marker = "test_pyo3test_without_macro"] pub const test_pyo3test_without_macro: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { name: test::StaticTestName("test_pyo3test_without_macro"), ignore: false, ignore_message: ::core::option::Option::None, source_file: "tests/test_pyo3test.rs", start_line: 25usize, start_col: 4usize, end_line: 25usize, end_col: 31usize, compile_fail: false, no_run: false, should_panic: test::ShouldPanic::No, test_type: test::TestType::IntegrationTest, }, testfn: test::StaticTestFn( #[coverage(off)] || test::assert_test_result(test_pyo3test_without_macro()), ), }; fn test_pyo3test_without_macro() { unsafe { if ::pyo3::ffi::Py_IsInitialized() != 0 { { ::core::panicking::panic_fmt(format_args!( "called `append_to_inittab` but a Python interpreter is already running.", )); }; } ::pyo3::ffi::PyImport_AppendInittab( py_adders::__PYO3_NAME.as_ptr() as *const ::std::os::raw::c_char, ::std::option::Option::Some(py_adders::__pyo3_init), ); }; pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { let adders = py.import_bound("adders").expect("Failed to import adders"); let addone = adders .getattr("addone") .expect("Failed to get addone function"); let result: PyResult = match addone.call1((1_isize,)) { Ok(r) => r.extract(), Err(e) => Err(e), }; let result = result.unwrap(); let expected_result = 2_isize; match (&result, &expected_result) { (left_val, right_val) => { if !(*left_val == *right_val) { let kind = ::core::panicking::AssertKind::Eq; ::core::panicking::assert_failed( kind, &*left_val, &*right_val, ::core::option::Option::None, ); } } }; }); } extern crate test; #[cfg(test)] #[rustc_test_marker = "test_pyo3test_simple_case"] pub const test_pyo3test_simple_case: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { name: test::StaticTestName("test_pyo3test_simple_case"), ignore: false, ignore_message: ::core::option::Option::None, source_file: "tests/test_pyo3test.rs", start_line: 46usize, start_col: 4usize, end_line: 46usize, end_col: 29usize, compile_fail: false, no_run: false, should_panic: test::ShouldPanic::No, test_type: test::TestType::IntegrationTest, }, testfn: test::StaticTestFn( #[coverage(off)] || test::assert_test_result(test_pyo3test_simple_case()), ), }; fn test_pyo3test_simple_case() { unsafe { if ::pyo3::ffi::Py_IsInitialized() != 0 { { ::core::panicking::panic_fmt(format_args!( "called `append_to_inittab` but a Python interpreter is already running.", )); }; } ::pyo3::ffi::PyImport_AppendInittab( py_adders::__PYO3_NAME.as_ptr() as *const ::std::os::raw::c_char, ::std::option::Option::Some(py_adders::__pyo3_init), ); }; pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { let adders = py.import_bound("adders").expect("Failed to import adders"); let addone = adders .getattr("addone") .expect("Failed to get addone function"); let result: PyResult = match addone.call1((1_isize,)) { Ok(r) => r.extract(), Err(e) => Err(e), }; let result = result.unwrap(); let expected_result = 2_isize; match (&result, &expected_result) { (left_val, right_val) => { if !(*left_val == *right_val) { let kind = ::core::panicking::AssertKind::Eq; ::core::panicking::assert_failed( kind, &*left_val, &*right_val, ::core::option::Option::None, ); } } }; }); } extern crate test; #[cfg(test)] #[rustc_test_marker = "test_pyo3test_import_module_only"] pub const test_pyo3test_import_module_only: test::TestDescAndFn = test::TestDescAndFn { desc: test::TestDesc { name: test::StaticTestName("test_pyo3test_import_module_only"), ignore: false, ignore_message: ::core::option::Option::None, source_file: "tests/test_pyo3test.rs", start_line: 58usize, start_col: 4usize, end_line: 58usize, end_col: 36usize, compile_fail: false, no_run: false, should_panic: test::ShouldPanic::No, test_type: test::TestType::IntegrationTest, }, testfn: test::StaticTestFn( #[coverage(off)] || test::assert_test_result(test_pyo3test_import_module_only()), ), }; fn test_pyo3test_import_module_only() { unsafe { if ::pyo3::ffi::Py_IsInitialized() != 0 { { ::core::panicking::panic_fmt(format_args!( "called `append_to_inittab` but a Python interpreter is already running.", )); }; } ::pyo3::ffi::PyImport_AppendInittab( py_adders::__PYO3_NAME.as_ptr() as *const ::std::os::raw::c_char, ::std::option::Option::Some(py_adders::__pyo3_init), ); }; pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { let adders = py.import_bound("adders").expect("Failed to import adders"); let result: isize = adders .getattr("addone") .unwrap() .call1((1_isize,)) .unwrap() .extract() .unwrap(); let expected_result = 2_isize; match (&result, &expected_result) { (left_val, right_val) => { if !(*left_val == *right_val) { let kind = ::core::panicking::AssertKind::Eq; ::core::panicking::assert_failed( kind, &*left_val, &*right_val, ::core::option::Option::None, ); } } }; }); } #[rustc_main] #[coverage(off)] pub fn main() -> () { extern crate test; test::test_main_static(&[ &test_pyo3test_import_module_only, &test_pyo3test_simple_case, &test_pyo3test_without_macro, ]) }