Show More
@@ -0,0 +1,22 | |||||
|
1 | // dagops.rs | |||
|
2 | // | |||
|
3 | // Copyright 2024 Georges Racinet <georges.racinet@cloudcrane.io> | |||
|
4 | // | |||
|
5 | // This software may be used and distributed according to the terms of the | |||
|
6 | // GNU General Public License version 2 or any later version. | |||
|
7 | ||||
|
8 | //! Bindings for the `hg::dagops` module provided by the | |||
|
9 | //! `hg-core` package. | |||
|
10 | //! | |||
|
11 | //! From Python, this will be seen as `mercurial.pyo3-rustext.dagop` | |||
|
12 | use pyo3::prelude::*; | |||
|
13 | ||||
|
14 | use crate::util::new_submodule; | |||
|
15 | ||||
|
16 | pub fn init_module<'py>( | |||
|
17 | py: Python<'py>, | |||
|
18 | package: &str, | |||
|
19 | ) -> PyResult<Bound<'py, PyModule>> { | |||
|
20 | let m = new_submodule(py, package, "dagop")?; | |||
|
21 | Ok(m) | |||
|
22 | } |
@@ -0,0 +1,28 | |||||
|
1 | use pyo3::prelude::*; | |||
|
2 | use pyo3::types::PyDict; | |||
|
3 | /// Create the module, with `__package__` given from parent | |||
|
4 | /// | |||
|
5 | /// According to PyO3 documentation, which links to | |||
|
6 | /// <https://github.com/PyO3/pyo3/issues/1517>, the same convoluted | |||
|
7 | /// write to sys.modules has to be made as with the `cpython` crate. | |||
|
8 | pub(crate) fn new_submodule<'py>( | |||
|
9 | py: Python<'py>, | |||
|
10 | package_name: &str, | |||
|
11 | name: &str, | |||
|
12 | ) -> PyResult<Bound<'py, PyModule>> { | |||
|
13 | let dotted_name = &format!("{}.{}", package_name, name); | |||
|
14 | let m = PyModule::new(py, name)?; | |||
|
15 | m.add("__package__", package_name)?; | |||
|
16 | m.add("__doc__", "DAG operations - Rust implementation")?; | |||
|
17 | ||||
|
18 | let sys = PyModule::import(py, "sys")?; | |||
|
19 | // according to the doc, we could make a static PyString out of | |||
|
20 | // "modules" with the `intern!` macro, but this is used only at | |||
|
21 | // registration so it may not be worth the effort. | |||
|
22 | let sys_modules: Bound<'_, PyDict> = sys.getattr("modules")?.extract()?; | |||
|
23 | sys_modules.set_item(dotted_name, &m)?; | |||
|
24 | // Example C code (see pyexpat.c and import.c) will "give away the | |||
|
25 | // reference", but we won't because it will be consumed once the | |||
|
26 | // Rust PyObject is dropped. | |||
|
27 | Ok(m) | |||
|
28 | } |
@@ -1,6 +1,19 | |||||
1 | use pyo3::prelude::*; |
|
1 | use pyo3::prelude::*; | |
2 |
|
2 | |||
|
3 | mod dagops; | |||
|
4 | mod util; | |||
|
5 | ||||
3 | #[pymodule] |
|
6 | #[pymodule] | |
4 |
fn pyo3_rustext( |
|
7 | fn pyo3_rustext(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { | |
|
8 | m.add( | |||
|
9 | "__doc__", | |||
|
10 | "Mercurial core concepts - Rust implementation exposed via PyO3", | |||
|
11 | )?; | |||
|
12 | // the module's __name__ is pyo3_rustext, not mercurial.pyo3_rustext | |||
|
13 | // (at least at this point). | |||
|
14 | let name: String = m.getattr("__name__")?.extract()?; | |||
|
15 | let dotted_name = format!("mercurial.{}", name); | |||
|
16 | ||||
|
17 | m.add_submodule(&dagops::init_module(py, &dotted_name)?)?; | |||
5 | Ok(()) |
|
18 | Ok(()) | |
6 | } |
|
19 | } |
General Comments 0
You need to be logged in to leave comments.
Login now