Mercurial > hg
view rust/hg-cpython/src/utils.rs @ 49932:136aa80aa8b2
rust-clippy: disable some lints crate-wide for `hg-cpython`
`rust-cpython` creates some pretty funky code that also needs to be compatible
with pretty old Rust. This makes clippy quite useless in `hg-cpython` unless
you disable the lints that are always triggered by `py_class!` and related.
Maybe `clippy` will allow one day to exclude a dependency from its linting,
but this seems quite unlikely, so this is the best we've got at the moment.
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Mon, 09 Jan 2023 19:36:41 +0100 |
parents | c7fb9b74e753 |
children | 28a0eb21ff04 |
line wrap: on
line source
use cpython::exc::ValueError; use cpython::{PyBytes, PyDict, PyErr, PyObject, PyResult, PyTuple, Python}; use hg::revlog::Node; #[allow(unused)] pub fn print_python_trace(py: Python) -> PyResult<PyObject> { eprintln!("==============================="); eprintln!("Printing Python stack from Rust"); eprintln!("==============================="); let traceback = py.import("traceback")?; let sys = py.import("sys")?; let kwargs = PyDict::new(py); kwargs.set_item(py, "file", sys.get(py, "stderr")?)?; traceback.call(py, "print_stack", PyTuple::new(py, &[]), Some(&kwargs)) } // Necessary evil for the time being, could maybe be moved to // a TryFrom in Node itself const NODE_BYTES_LENGTH: usize = 20; type NodeData = [u8; NODE_BYTES_LENGTH]; /// Copy incoming Python bytes given as `PyObject` into `Node`, /// doing the necessary checks pub fn node_from_py_object<'a>( py: Python, bytes: &'a PyObject, ) -> PyResult<Node> { let as_py_bytes: &'a PyBytes = bytes.extract(py)?; node_from_py_bytes(py, as_py_bytes) } /// Clone incoming Python bytes given as `PyBytes` as a `Node`, /// doing the necessary checks. pub fn node_from_py_bytes(py: Python, bytes: &PyBytes) -> PyResult<Node> { <NodeData>::try_from(bytes.data(py)) .map_err(|_| { PyErr::new::<ValueError, _>( py, format!("{}-byte hash required", NODE_BYTES_LENGTH), ) }) .map(Into::into) }