Move to PyO3

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-03-14 22:59:49 +01:00
parent eef1d2041d
commit 8600e053e4
3 changed files with 23 additions and 30 deletions

View file

@ -7,7 +7,7 @@ authors = [
] ]
[dependencies] [dependencies]
cpython = "0.7" pyo3 = { version = "0.16", features = ["extension-module"] }
nom = "4" nom = "4"
chrono = "0.4" chrono = "0.4"
ncurses = "5" ncurses = "5"

View file

@ -130,7 +130,7 @@ setup(
description="A console XMPP client", description="A console XMPP client",
long_description=LONG_DESCRIPTION, long_description=LONG_DESCRIPTION,
ext_modules=[module_poopt], ext_modules=[module_poopt],
rust_extensions=[RustExtension('poezio.libpoezio', binding=Binding.RustCPython)], rust_extensions=[RustExtension('poezio.libpoezio', binding=Binding.PyO3)],
url='https://poez.io/', url='https://poez.io/',
license='GPL-3.0-or-later', license='GPL-3.0-or-later',
download_url='https://dev.louiz.org/projects/poezio/files', download_url='https://dev.louiz.org/projects/poezio/files',

View file

@ -1,5 +1,4 @@
#[macro_use] extern crate pyo3;
extern crate cpython;
#[macro_use] #[macro_use]
extern crate nom; extern crate nom;
extern crate ncurses; extern crate ncurses;
@ -10,46 +9,40 @@ extern crate enum_set;
pub mod theming; pub mod theming;
use self::theming::{curses_attr, parse_attrs}; use self::theming::{curses_attr, parse_attrs};
use cpython::{PyErr, PyObject, PyResult, Python, PythonObject, ToPyObject};
py_module_initializer!(libpoezio, initlibpoezio, PyInit_libpoezio, |py, m| { use pyo3::{
m.add( conversion::{IntoPy, ToPyObject},
py, create_exception,
"to_curses_attr", marker::Python,
py_fn!(py, to_curses_attr(fg: i16, bg: i16, attrs: &str)), 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(()) Ok(())
}); }
py_exception!(libpoezio, LogParseError); macro_rules! py_object {
macro_rules! py_int {
($py:ident, $i:expr) => { ($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 { fn nom_to_py_err(py: Python, err: nom::Err<&str>) -> PyErr {
PyErr { LogParseError::new_err(py_object!(py, err.into_error_kind().description()))
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,
}
} }
#[pyfunction]
fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult<PyObject> { fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult<PyObject> {
let attrs = match parse_attrs(attrs) { let attrs = match parse_attrs(attrs) {
Ok(attrs) => attrs.1, Ok(attrs) => attrs.1,
Err(err) => return Err(nom_to_py_err(py, err)), Err(err) => return Err(nom_to_py_err(py, err)),
}; };
let result = curses_attr(fg, bg, attrs); let result = curses_attr(fg, bg, attrs);
Ok(py_int!(py, result)) Ok(py_object!(py, result))
} }