remove outdated workaround in module documentation (#2466)
* remove workaround section in module.md that is no longer needed after merging #2081 * update changelong * revert CHANGELOG
This commit is contained in:
parent
53b83cccbf
commit
cdb3b6ff32
|
@ -62,80 +62,6 @@ import my_extension
|
|||
print(my_extension.__doc__)
|
||||
```
|
||||
|
||||
## Organizing your module registration code
|
||||
|
||||
For most projects, it's adequate to centralize all your FFI code into a single Rust module.
|
||||
|
||||
However, for larger projects, it can be helpful to split your Rust code into several Rust modules to keep your code
|
||||
readable. Unfortunately, though, some of the macros like `wrap_pyfunction!` do not yet work when used on code defined
|
||||
in other modules ([#1709](https://github.com/PyO3/pyo3/issues/1709)). One way to work around this is to pass
|
||||
references to the `PyModule` so that each module registers its own FFI code. For example:
|
||||
|
||||
```rust
|
||||
// src/lib.rs
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
dirutil::register(py, m)?;
|
||||
osutil::register(py, m)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// src/dirutil.rs
|
||||
# mod dirutil {
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub(crate) fn register(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<SomeClass>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
struct SomeClass {/* ... */}
|
||||
# }
|
||||
|
||||
// src/osutil.rs
|
||||
# mod osutil {
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub(crate) fn register(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(determine_current_os, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn determine_current_os() -> String {
|
||||
"linux".to_owned()
|
||||
}
|
||||
# }
|
||||
```
|
||||
|
||||
Another workaround for splitting FFI code across multiple modules ([#1709](https://github.com/PyO3/pyo3/issues/1709))
|
||||
is to add `use module::*`, like this:
|
||||
|
||||
```rust
|
||||
// src/lib.rs
|
||||
use pyo3::prelude::*;
|
||||
use osutil::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(determine_current_os, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// src/osutil.rs
|
||||
# mod osutil {
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pyfunction]
|
||||
pub(crate) fn determine_current_os() -> String {
|
||||
"linux".to_owned()
|
||||
}
|
||||
# }
|
||||
```
|
||||
|
||||
## Python submodules
|
||||
|
||||
You can create a module hierarchy within a single extension module by using
|
||||
|
|
Loading…
Reference in New Issue