diff options
author | Maxime “pep” Buquet <pep@bouah.net> | 2022-03-14 22:59:49 +0100 |
---|---|---|
committer | Maxime “pep” Buquet <pep@bouah.net> | 2022-08-21 17:19:58 +0200 |
commit | 8600e053e447091a16862aa91a58ba216a052c38 (patch) | |
tree | 8476c8f7d05353e7c308b03855a881b1e919a805 | |
parent | eef1d2041d9c6d213cc41640d57805cca6f1d3ef (diff) | |
download | poezio-8600e053e447091a16862aa91a58ba216a052c38.tar.gz poezio-8600e053e447091a16862aa91a58ba216a052c38.tar.bz2 poezio-8600e053e447091a16862aa91a58ba216a052c38.tar.xz poezio-8600e053e447091a16862aa91a58ba216a052c38.zip |
Move to PyO3
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
-rw-r--r-- | Cargo.toml | 2 | ||||
-rwxr-xr-x | setup.py | 2 | ||||
-rw-r--r-- | src/lib.rs | 51 |
3 files changed, 24 insertions, 31 deletions
@@ -7,7 +7,7 @@ authors = [ ] [dependencies] -cpython = "0.7" +pyo3 = { version = "0.16", features = ["extension-module"] } nom = "4" chrono = "0.4" ncurses = "5" @@ -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', @@ -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::<LogParseError>())?; + 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::<LogParseError>().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<PyObject> { 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)) } |