Skip to content

Commit

Permalink
Fix lib_with_disallowed_lib compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 16, 2021
1 parent b0c432b commit 82a7adf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 108 deletions.
22 changes: 20 additions & 2 deletions test-crates/lib_with_disallowed_lib/Cargo.lock

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

167 changes: 61 additions & 106 deletions test-crates/lib_with_disallowed_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,130 +10,85 @@ fn add(x: usize, y: usize) -> usize {

unsafe extern "C" fn __pyo3_raw_add(
_slf: *mut pyo3::ffi::PyObject,
_args: *mut pyo3::ffi::PyObject,
_kwargs: *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 {
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")
}
pyo3::callback::handle_panic(|_py| {
let _kwnames: Option<&pyo3::types::PyTuple> = _py.from_borrowed_ptr_or_opt(_kwnames);
let _args = _args as *const &pyo3::PyAny;
let _kwargs = if let Some(kwnames) = _kwnames {
std::slice::from_raw_parts(_args.offset(_nargs), kwnames.len())
} else {
&[]
};
let _args = std::slice::from_raw_parts(_args, _nargs as usize);
{
const DESCRIPTION: pyo3::derive_utils::FunctionDescription =
pyo3::derive_utils::FunctionDescription {
cls_name: None,
func_name: "add",
positional_parameter_names: &["x", "y"],
positional_only_parameters: 0,
required_positional_parameters: 2usize,
keyword_only_parameters: &[],
accept_varargs: false,
accept_varkeywords: false,
};
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",
)))
}
let mut output = [None; 2usize];
let (_args, _kwargs) = DESCRIPTION.extract_arguments(
_py,
_args.iter().copied(),
_kwnames.map(|kwnames| {
kwnames
.as_slice()
.iter()
.copied()
.zip(_kwargs.iter().copied())
}),
&mut output,
)?;
let arg0 = {
let _obj = output[0usize].expect("Failed to extract required method argument");
_obj.extract()
.map_err(|e| pyo3::derive_utils::argument_extraction_error(_py, "x", e))?
};
let arg1 = {
let _obj = output[1usize].expect("Failed to extract required method argument");
_obj.extract()
.map_err(|e| pyo3::derive_utils::argument_extraction_error(_py, "y", e))?
};
pyo3::callback::convert(_py, add(arg0, arg1))
}
};
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,
pyo3::class::methods::PyMethodDef::fastcall_cfunction_with_keywords(
"add\u{0}",
pyo3::class::methods::PyCFunctionFastWithKeywords(__pyo3_raw_add),
"\u{0}",
),
args.into(),
)
}

fn pyo3_pure(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(&__pyo3_get_function_add)?;
fn lib_with_disallowed_lib(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped (& (| py | { # [allow (dead_code)] enum ProcMacroHack { Value = ("[< __pyo3_get_function_ add >]" , 0) . 1 , } __pyo3_get_function_add } (py)))?;
Ok(())
}

#[no_mangle]
#[allow(non_snake_case)]
#[doc = r" This autogenerated function is called by the python interpreter when importing"]
#[doc = r" the module."]
/// This autogenerated function is called by the python interpreter when importing
/// the module.
pub unsafe extern "C" fn PyInit_lib_with_disallowed_lib() -> *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()
})
static NAME: &str = "lib_with_disallowed_lib\u{0}";
static DOC: &str = "\u{0}";
static MODULE_DEF: ModuleDef = unsafe { ModuleDef::new(NAME, DOC) };
pyo3::callback::handle_panic(|_py| MODULE_DEF.make_module(_py, lib_with_disallowed_lib))
}

0 comments on commit 82a7adf

Please sign in to comment.