Skip to content

Commit

Permalink
Fix some test-crates build (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
messense authored Nov 25, 2024
1 parent 65404af commit 48f7d33
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 395 deletions.
3 changes: 1 addition & 2 deletions test-crates/lib_with_path_dep/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
# Don't use the macros feature, which makes compilation much faster
pyo3 = { version = "0.23.1", default-features = false, features = ["extension-module"] }
pyo3 = { version = "0.23.1", features = ["extension-module"] }
some_path_dep = { path = "../some_path_dep" }
133 changes: 4 additions & 129 deletions test-crates/lib_with_path_dep/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,139 +1,14 @@
/// We speed up compilation by using the expanded output and then deselecting the macro feature
/// of pyo3, removing syn and proc macros from the dependencies
use pyo3::prelude::*;

#[pyfunction]
fn add(x: usize, y: usize) -> usize {
let sum = some_path_dep::add(x, y);
debug_assert!(some_path_dep::is_sum(x, y, sum));
sum
}

unsafe extern "C" fn __pyo3_raw_add(
_slf: *mut pyo3::ffi::PyObject,
_args: *mut pyo3::ffi::PyObject,
_kwargs: *mut pyo3::ffi::PyObject,
) -> *mut pyo3::ffi::PyObject {
const _LOCATION: &'static str = "add()";

let pool = ::pyo3::GILPool::new();
let unwind_safe_py = std::panic::AssertUnwindSafe(pool.python());
let result = match std::panic::catch_unwind(move || -> ::pyo3::PyResult<_> {
let _py = *unwind_safe_py;
::pyo3::callback::convert(_py, {
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
{
const PARAMS: &'static [pyo3::derive_utils::ParamDescription] = &[
pyo3::derive_utils::ParamDescription {
name: "x",
is_optional: false,
kw_only: false,
},
pyo3::derive_utils::ParamDescription {
name: "y",
is_optional: false,
kw_only: false,
},
];
let mut output = [None; 2usize];
let mut _args = _args;
let mut _kwargs = _kwargs;
let (_args, _kwargs) = pyo3::derive_utils::parse_fn_args(
Some(_LOCATION),
PARAMS,
_args,
_kwargs,
false,
false,
&mut output,
)?;
let arg0 = match output[0usize] {
Some(_obj) => _obj
.extract()
.map_err(|e| pyo3::derive_utils::argument_extraction_error(_py, "x", e))?,
None => {
panic!("Failed to extract required method argument")
}
};
let arg1 = match output[1usize] {
Some(_obj) => _obj
.extract()
.map_err(|e| pyo3::derive_utils::argument_extraction_error(_py, "y", e))?,
None => {
panic!("Failed to extract required method argument")
}
};
add(arg0, arg1)
}
})
}) {
Ok(result) => result,
Err(e) => {
if let Some(string) = e.downcast_ref::<String>() {
Err(::pyo3::panic::PanicException::new_err((string.clone(),)))
} else if let Some(s) = e.downcast_ref::<&str>() {
Err(::pyo3::panic::PanicException::new_err((s.to_string(),)))
} else {
Err(::pyo3::panic::PanicException::new_err((
"panic from Rust code",
)))
}
}
};
result.unwrap_or_else(|e| {
e.restore(pool.python());
::pyo3::callback::callback_error()
})
}
pub(crate) fn __pyo3_get_function_add<'a>(
args: impl Into<pyo3::derive_utils::PyFunctionArguments<'a>>,
) -> pyo3::PyResult<&'a pyo3::types::PyCFunction> {
let name = "add\u{0}";
let name = std::ffi::CStr::from_bytes_with_nul(name.as_bytes()).unwrap();
let doc = std::ffi::CStr::from_bytes_with_nul(b"\x00").unwrap();
pyo3::types::PyCFunction::internal_new(
name,
doc,
pyo3::class::PyMethodType::PyCFunctionWithKeywords(__pyo3_raw_add),
pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS,
args.into(),
)
}

fn pyo3_pure(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(&__pyo3_get_function_add)?;
#[pymodule]
fn lib_with_path_dep(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(add))?;
Ok(())
}

#[no_mangle]
#[allow(non_snake_case)]
#[doc = r" This autogenerated function is called by the python interpreter when importing"]
#[doc = r" the module."]
pub unsafe extern "C" fn PyInit_lib_with_path_dep() -> *mut pyo3::ffi::PyObject {
use pyo3::derive_utils::ModuleDef;
const NAME: &'static str = "pyo3_pure\u{0}";
static MODULE_DEF: ModuleDef = unsafe { ModuleDef::new(NAME) };
let pool = ::pyo3::GILPool::new();
let unwind_safe_py = std::panic::AssertUnwindSafe(pool.python());
let result = match std::panic::catch_unwind(move || -> ::pyo3::PyResult<_> {
let _py = *unwind_safe_py;
::pyo3::callback::convert(_py, MODULE_DEF.make_module("", pyo3_pure))
}) {
Ok(result) => result,
Err(e) => {
if let Some(string) = e.downcast_ref::<String>() {
Err(::pyo3::panic::PanicException::new_err((string.clone(),)))
} else if let Some(s) = e.downcast_ref::<&str>() {
Err(::pyo3::panic::PanicException::new_err((s.to_string(),)))
} else {
Err(::pyo3::panic::PanicException::new_err((
"panic from Rust code",
)))
}
}
};
result.unwrap_or_else(|e| {
e.restore(pool.python());
::pyo3::callback::callback_error()
})
}
81 changes: 81 additions & 0 deletions test-crates/sdist_with_path_dep/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions test-crates/sdist_with_path_dep/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
# Don't use the macros feature, which makes compilation much faster
pyo3 = { version = "0.23.1", default-features = false, features = ["extension-module"] }
pyo3 = { version = "0.23.1", features = ["extension-module"] }
some_path_dep = { path = "../some_path_dep" }
Loading

0 comments on commit 48f7d33

Please sign in to comment.