Skip to content

Commit

Permalink
refactor: switch to declarative module
Browse files Browse the repository at this point in the history
It may be possible that "imperative" module declarations will get
deprecated, according to [1], so switch to declarative syntax even
though it means more boilerplate due to missing `#[pymodule_export]`
capability on glob `use`s.

[1]: PyO3/pyo3#3900
  • Loading branch information
xen0n committed Jun 27, 2024
1 parent 0fa6a41 commit 71fefe9
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,61 @@ mod syntax;
mod values;

#[pymodule]
fn xingque(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add(
"VERSION",
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
)?;
m.add("STARLARK_RUST_VERSION", "0.12.0")?; // TODO: query this from Cargo
m.add_class::<codemap::PyCodeMap>()?;
m.add_class::<codemap::PyFileSpan>()?;
m.add_class::<codemap::PyPos>()?;
m.add_class::<codemap::PyResolvedFileLine>()?;
m.add_class::<codemap::PyResolvedFileSpan>()?;
m.add_class::<codemap::PyResolvedPos>()?;
m.add_class::<codemap::PyResolvedSpan>()?;
m.add_class::<codemap::PySpan>()?;
m.add_class::<environment::PyFrozenModule>()?;
m.add_class::<environment::PyGlobals>()?;
m.add_class::<environment::PyGlobalsBuilder>()?;
m.add_class::<environment::PyLibraryExtension>()?;
m.add_class::<environment::PyModule>()?;
m.add_class::<eval::PyDictFileLoader>()?;
m.add_class::<eval::PyEvaluator>()?;
m.add_class::<syntax::PyAstModule>()?;
m.add_class::<syntax::PyDialect>()?;
m.add_class::<syntax::PyDialectTypes>()?;
m.add_class::<values::PyFrozenValue>()?;
m.add_class::<values::PyHeap>()?;
m.add_class::<values::PyHeapSummary>()?;
m.add_class::<values::PyValue>()?;
Ok(())
mod xingque {
use super::*;

#[pymodule_export]
use codemap::PyCodeMap;
#[pymodule_export]
use codemap::PyFileSpan;
#[pymodule_export]
use codemap::PyPos;
#[pymodule_export]
use codemap::PyResolvedFileLine;
#[pymodule_export]
use codemap::PyResolvedFileSpan;
#[pymodule_export]
use codemap::PyResolvedPos;
#[pymodule_export]
use codemap::PyResolvedSpan;
#[pymodule_export]
use codemap::PySpan;
#[pymodule_export]
use environment::PyFrozenModule;
#[pymodule_export]
use environment::PyGlobals;
#[pymodule_export]
use environment::PyGlobalsBuilder;
#[pymodule_export]
use environment::PyLibraryExtension;
#[pymodule_export]
use environment::PyModule;
#[pymodule_export]
use eval::PyDictFileLoader;
#[pymodule_export]
use eval::PyEvaluator;
#[pymodule_export]
use syntax::PyAstModule;
#[pymodule_export]
use syntax::PyDialect;
#[pymodule_export]
use syntax::PyDialectTypes;
#[pymodule_export]
use values::PyFrozenValue;
#[pymodule_export]
use values::PyHeap;
#[pymodule_export]
use values::PyHeapSummary;
#[pymodule_export]
use values::PyValue;

#[pymodule_init]
fn init(m: &Bound<'_, pyo3::types::PyModule>) -> PyResult<()> {
m.add(
"VERSION",
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
)?;
m.add("STARLARK_RUST_VERSION", "0.12.0")?; // TODO: query this from Cargo
Ok(())
}
}

0 comments on commit 71fefe9

Please sign in to comment.