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]
cpython = "0.7"
pyo3 = { version = "0.16", features = ["extension-module"] }
nom = "4"
chrono = "0.4"
ncurses = "5"

View file

@ -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',

View file

@ -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)),
)?;
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(())
});
}
py_exception!(libpoezio, LogParseError);
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))
}