Clarify Python module doc string

This commit is contained in:
messense 2017-06-18 23:15:10 +08:00
parent a993d8d7e0
commit 75d6684d25
No known key found for this signature in database
GPG Key ID: BB41A8A2C716CCA9
3 changed files with 14 additions and 4 deletions

View File

@ -29,9 +29,9 @@ use pyo3::{py, PyResult, Python, PyModule};
// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
#[py::modinit(rust2py)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
m.add(py, "__doc__", "This module is implemented in Rust.")?;
// pyo3 aware function. All of our python interface could be declared in a separate module.
// Note that the `#[pyfn()]` annotation automatically converts the arguments from
@ -55,6 +55,16 @@ fn sum_as_string(a:i64, b:i64) -> String {
The `modinit` procedural macro attribute takes care of exporting the initialization function of your module to Python. It takes one argument as the name of your module, it must be the name of the `.so` or `.pyd` file.
The [Rust doc comments](https://doc.rust-lang.org/stable/book/first-edition/comments.html) of the module initialization function will be applied automatically as the Python doc string of your module.
```python
import rust2py
print(rust2py.__doc__)
```
Which means that the above Python code will print `This module is implemented in Rust.`.
> On macOS, you will need to rename the output from `*.dylib` to `*.so`.
>
> On Windows, you will need to rename the output from `*.dll` to `*.pyd`.

View File

@ -52,7 +52,7 @@ fn hello(py: Python) -> PyResult<()> {
Example library with python bindings:
The following two files will build with `cargo build`, and will generate a python-compatible library.
On Mac OS, you will need to rename the output from \*.dylib to \*.so.
On macOS, you will need to rename the output from \*.dylib to \*.so.
On Windows, you will need to rename the output from \*.dll to \*.pyd.
**`Cargo.toml`:**
@ -77,9 +77,9 @@ use pyo3::{py, PyResult, Python, PyModule};
// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
#[py::modinit(rust2py)]
fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
m.add(py, "__doc__", "This module is implemented in Rust.")?;
#[pyfn(m, "sum_as_string")]
// pyo3 aware function. All of our python interface could be declared in a separate module.

View File

@ -122,7 +122,7 @@
//! ```bash
//! cp ./target/debug/libhello.so ./hello.so
//! ```
//! (Note: on Mac OS you will have to rename `libhello.dynlib` to `libhello.so`)
//! (Note: on macOS you will have to rename `libhello.dynlib` to `libhello.so`)
//!
//! The extension module can then be imported into Python:
//!