From 497e4cabdb8889d87c180ad30c45e5f703875316 Mon Sep 17 00:00:00 2001 From: Antal Spector-Zabusky Date: Tue, 2 Jul 2024 16:23:00 -0700 Subject: [PATCH] test: fix snapshot test of macro expansion to a specific OS We have to specify a specific OS target because until PyO3 v0.22, PyO3 transitively depends on the [rust-ctor](https://crates.io/crates/ctor) crate, which generates different output on different OSes. Once we're doing *that*, we have to specify a specific Python ABI so that PyO3 doesn't get alarmed about cross-compilation. This is all a minor headache. One concern about this is that everyone testing locally has to run `rustup target add x864_64-unknown-linuxgnu`. CI doesn't, because I deliberately picked the triple that was running on CI, but any local development that isn't on that OS does. Is this worth it? --- tests/tests.rs | 27 +++++++++++++++++--- tests/wrapper_tests/mod.expanded.rs | 38 +++++++++++++++-------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/tests/tests.rs b/tests/tests.rs index e051190..ecf1d26 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -39,7 +39,28 @@ assert TestUnionEnum.new_unit().is_unit() #[test] fn test_macro_expansion() { - // To regenerate the snapshot, delete the generated - // `tests/wrapper_tests/mod.expanded.rs` file and retun the test. - macrotest::expand("tests/wrapper_tests/mod.rs") + // To regenerate the snapshot, run this test with the environment variable + // `MACROTEST=overwrite`, or alternatively delete the generated + // `tests/wrapper_tests/mod.expanded.rs` file and rerun this test. + macrotest::expand_args( + "tests/wrapper_tests/mod.rs", + // We have to specify a specific OS target because until PyO3 v0.22, + // PyO3 transitively depends on the + // [rust-ctor](https://crates.io/crates/ctor) crate, which generates + // different output on different OSes. Once we're doing *that*, we have + // to specify a specific Python ABI so that PyO3 doesn't get alarmed + // about cross-compilation. This is all a minor headache. + // + // In particular, this means that if you are running these tests on a + // different OS, you will need to install the specified target. The + // target is specifically chosen to be the one we use on CI, so CI does + // not need an extra `rustup target add`, but some developers will. + &[ + "--target", + "x86_64-unknown-linux-gnu", + "--no-default-features", + "--features", + "pyo3/abi3-py311", + ], + ) } diff --git a/tests/wrapper_tests/mod.expanded.rs b/tests/wrapper_tests/mod.expanded.rs index 8d86482..5ab2f05 100644 --- a/tests/wrapper_tests/mod.expanded.rs +++ b/tests/wrapper_tests/mod.expanded.rs @@ -370,13 +370,14 @@ pub mod python { ::inventory::core::option::Option::None, ), }; + #[link_section = ".text.startup"] unsafe extern "C" fn __ctor() { unsafe { ::inventory::ErasedNode::submit(__INVENTORY.value, &__INVENTORY) } } #[used] - #[link_section = "__DATA,__mod_init_func"] + #[link_section = ".init_array"] static __CTOR: unsafe extern "C" fn() = __ctor; }; #[doc(hidden)] @@ -415,20 +416,18 @@ pub mod python { Inventory::new(_pyo3::impl_::pyclass::PyClassItems { methods: &[ _pyo3::class::PyMethodDefType::Static( - _pyo3::impl_::pymethods::PyMethodDef::fastcall_cfunction_with_keywords( + _pyo3::impl_::pymethods::PyMethodDef::cfunction_with_keywords( "from_string\0", - _pyo3::impl_::pymethods::PyCFunctionFastWithKeywords({ + _pyo3::impl_::pymethods::PyCFunctionWithKeywords({ 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, + _args: *mut _pyo3::ffi::PyObject, + _kwargs: *mut _pyo3::ffi::PyObject, ) -> *mut _pyo3::ffi::PyObject { - _pyo3::impl_::trampoline::fastcall_with_keywords( + _pyo3::impl_::trampoline::cfunction_with_keywords( _slf, _args, - _nargs, - _kwnames, + _kwargs, PyTestUnionEnum::__pymethod_from_string__, ) } @@ -446,13 +445,14 @@ pub mod python { ::inventory::core::option::Option::None, ), }; + #[link_section = ".text.startup"] unsafe extern "C" fn __ctor() { unsafe { ::inventory::ErasedNode::submit(__INVENTORY.value, &__INVENTORY) } } #[used] - #[link_section = "__DATA,__mod_init_func"] + #[link_section = ".init_array"] static __CTOR: unsafe extern "C" fn() = __ctor; }; #[doc(hidden)] @@ -461,9 +461,8 @@ pub mod python { unsafe fn __pymethod_from_string__<'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, + _args: *mut _pyo3::ffi::PyObject, + _kwargs: *mut _pyo3::ffi::PyObject, ) -> _pyo3::PyResult<*mut _pyo3::ffi::PyObject> { let function = PyTestUnionEnum::from_string; const DESCRIPTION: _pyo3::impl_::extract_argument::FunctionDescription = _pyo3::impl_::extract_argument::FunctionDescription { @@ -478,10 +477,10 @@ pub mod python { }; let mut output = [::std::option::Option::None; 1usize]; let (_args, _kwargs) = DESCRIPTION - .extract_arguments_fastcall::< + .extract_arguments_tuple_dict::< _pyo3::impl_::extract_argument::NoVarargs, _pyo3::impl_::extract_argument::NoVarkeywords, - >(py, _args, _nargs, _kwnames, &mut output)?; + >(py, _args, _kwargs, &mut output)?; _pyo3::impl_::wrap::OkWrap::wrap( function( py, @@ -745,13 +744,14 @@ pub mod python { ::inventory::core::option::Option::None, ), }; + #[link_section = ".text.startup"] unsafe extern "C" fn __ctor() { unsafe { ::inventory::ErasedNode::submit(__INVENTORY.value, &__INVENTORY) } } #[used] - #[link_section = "__DATA,__mod_init_func"] + #[link_section = ".init_array"] static __CTOR: unsafe extern "C" fn() = __ctor; }; #[doc(hidden)] @@ -2175,13 +2175,14 @@ pub mod python { ::inventory::core::option::Option::None, ), }; + #[link_section = ".text.startup"] unsafe extern "C" fn __ctor() { unsafe { ::inventory::ErasedNode::submit(__INVENTORY.value, &__INVENTORY) } } #[used] - #[link_section = "__DATA,__mod_init_func"] + #[link_section = ".init_array"] static __CTOR: unsafe extern "C" fn() = __ctor; }; #[doc(hidden)] @@ -2338,13 +2339,14 @@ pub mod python { ::inventory::core::option::Option::None, ), }; + #[link_section = ".text.startup"] unsafe extern "C" fn __ctor() { unsafe { ::inventory::ErasedNode::submit(__INVENTORY.value, &__INVENTORY) } } #[used] - #[link_section = "__DATA,__mod_init_func"] + #[link_section = ".init_array"] static __CTOR: unsafe extern "C" fn() = __ctor; }; #[doc(hidden)]