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 --- Cargo.toml | 2 +- setup.py | 2 +- src/lib.rs | 51 ++++++++++++++++++++++----------------------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27257012..da78abef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ authors = [ ] [dependencies] -cpython = "0.7" +pyo3 = { version = "0.16", features = ["extension-module"] } nom = "4" chrono = "0.4" ncurses = "5" diff --git a/setup.py b/setup.py index 4c568c38..54f082d9 100755 --- a/setup.py +++ b/setup.py @@ -130,7 +130,7 @@ setup( description="A console XMPP client", long_description=LONG_DESCRIPTION, ext_modules=[module_poopt], - rust_extensions=[RustExtension('poezio.libpoezio', binding=Binding.RustCPython)], + rust_extensions=[RustExtension('poezio.libpoezio', binding=Binding.PyO3)], url='https://poez.io/', license='GPL-3.0-or-later', download_url='https://dev.louiz.org/projects/poezio/files', 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