Skip to content

Commit

Permalink
test: fix snapshot test of macro expansion to a specific OS
Browse files Browse the repository at this point in the history
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?
  • Loading branch information
antalsz committed Jul 2, 2024
1 parent ebd27e2 commit 497e4ca
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
27 changes: 24 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
}
38 changes: 20 additions & 18 deletions tests/wrapper_tests/mod.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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__,
)
}
Expand All @@ -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)]
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 497e4ca

Please sign in to comment.