From 8600e053e447091a16862aa91a58ba216a052c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 14 Mar 2022 22:59:49 +0100 Subject: Move to PyO3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index fae54570..1f4dea46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ -#[macro_use] -extern crate cpython; +extern crate pyo3; #[macro_use] extern crate nom; extern crate ncurses; @@ -10,46 +9,40 @@ extern crate enum_set; pub mod theming; use self::theming::{curses_attr, parse_attrs}; -use cpython::{PyErr, PyObject, PyResult, Python, PythonObject, ToPyObject}; - -py_module_initializer!(libpoezio, initlibpoezio, PyInit_libpoezio, |py, m| { - m.add( - py, - "to_curses_attr", - py_fn!(py, to_curses_attr(fg: i16, bg: i16, attrs: &str)), - )?; - Ok(()) -}); -py_exception!(libpoezio, LogParseError); +use pyo3::{ + conversion::{IntoPy, ToPyObject}, + create_exception, + marker::Python, + prelude::{pyfunction, pymodule, wrap_pyfunction, PyErr, PyModule, PyObject, PyResult}, +}; + +create_exception!(libpoezio, LogParseError, pyo3::exceptions::PyException); + +#[pymodule] +fn libpoezio(py: Python, m: &PyModule) -> PyResult<()> { + m.add("LogParseError", py.get_type::())?; + m.add_function(wrap_pyfunction!(to_curses_attr, m)?)?; + + Ok(()) +} -macro_rules! py_int { +macro_rules! py_object { ($py:ident, $i:expr) => { - $i.to_py_object($py).into_object() + $i.into_py($py).to_object($py) }; } fn nom_to_py_err(py: Python, err: nom::Err<&str>) -> PyErr { - PyErr { - ptype: py.get_type::().into_object(), - pvalue: Some( - LogParseError( - err.into_error_kind() - .description() - .to_py_object(py) - .into_object(), - ) - .into_object(), - ), - ptraceback: None, - } + LogParseError::new_err(py_object!(py, err.into_error_kind().description())) } +#[pyfunction] fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult { let attrs = match parse_attrs(attrs) { Ok(attrs) => attrs.1, Err(err) => return Err(nom_to_py_err(py, err)), }; let result = curses_attr(fg, bg, attrs); - Ok(py_int!(py, result)) + Ok(py_object!(py, result)) } -- cgit v1.2.3