Skip to content

Commit 103993f

Browse files
committed
Extend pyproject.toml example to include a Rust entry-point
1 parent 4dfe009 commit 103993f

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

examples/hello-world-pyprojecttoml/noxfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def test(session: nox.Session):
1414
session.run("print-hello")
1515
# Test script wrapper for Python entry-point
1616
session.run("sum-cli", "5", "7")
17+
session.run("rust-demo", "5", "7")
1718
# Test library
1819
session.run("pytest", "tests", *session.posargs)
1920
session.run("python", "-c", "from hello_world import _lib; print(_lib)")
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
[build-system]
2-
requires = ["setuptools", "wheel", "setuptools-rust"]
2+
requires = ["setuptools", "setuptools-rust"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "hello-world"
77
version = "1.0"
88

9-
[project.scripts] # Python entry-point wrapper to be installed in `$venv/bin`
10-
sum-cli = "hello_world.sum_cli:main"
9+
[project.scripts]
10+
# Python entry-point wrapper to be installed in `$venv/bin`
11+
sum-cli = "hello_world.sum_cli:main" # Python function that uses Rust
12+
rust-demo = "hello_world._lib:demo" # Rust function that uses Python
1113

12-
[tool.setuptools.packages] # pure Python packages/modules
14+
[tool.setuptools.packages]
15+
# Pure Python packages/modules
1316
find = { where = ["python"] }
1417

1518
[[tool.setuptools-rust.ext-modules]]
16-
target = "hello_world._lib" # private module to be nested into Python package
17-
py-limited-api = "auto"
18-
args = ["--profile", "release-lto"]
19+
# Private Rust extension module to be nested into Python package
20+
target = "hello_world._lib" # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml,
21+
# but you can add a prefix to nest it inside of a Python package.
22+
py-limited-api = "auto" # Default value, can be omitted
23+
binding = "PyO3" # Default value, can be omitted
24+
# See reference for RustExtension in https://setuptools-rust.readthedocs.io/en/latest/reference.html
1925

20-
[[tool.setuptools-rust.binaries]] # executable to be installed in `$venv/bin`
21-
target = "print-hello"
22-
args = ["--profile", "release-lto"]
26+
[[tool.setuptools-rust.binaries]]
27+
# Rust executable to be installed in `$venv/bin`
28+
target = "print-hello" # Needs to match bin.name in Cargo.toml
29+
args = ["--profile", "release-lto"] # Extra args for Cargo
30+
# See reference for RustBin in https://setuptools-rust.readthedocs.io/en/latest/reference.html
31+
# Note that you can also use Python entry-points as alternative to Rust binaries

examples/hello-world-pyprojecttoml/python/hello_world/sum_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import sys
33

4-
from . import sum_as_string
4+
from ._lib import sum_as_string
55

66

77
def main():
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
use pyo3::prelude::*;
2+
use std::env;
23

34
/// Formats the sum of two numbers as string.
45
#[pyfunction]
56
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
67
Ok((a + b).to_string())
78
}
89

10+
/// Calls Python (see https://pyo3.rs for details)
11+
#[pyfunction]
12+
fn demo(py: Python) -> PyResult<()> {
13+
let argv = env::args().collect::<Vec<_>>();
14+
println!("argv = {:?}", argv);
15+
// argv[0]: Python path, argv[1]: program name, argv[2..]: given args
16+
17+
let numbers: Vec<i32> = argv[2..].iter().map(|s| s.parse().unwrap()).collect();
18+
19+
Python::with_gil(|py| {
20+
let python_sum = PyModule::import(py, "builtins")?.getattr("sum")?;
21+
let total: i32 = python_sum.call1((numbers,))?.extract()?;
22+
println!("sum({}) = {:?}", argv[2..].join(", "), total);
23+
Ok(())
24+
})
25+
}
26+
927
/// A Python module implemented in Rust. The name of this function must match
1028
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
1129
/// import the module.
1230
#[pymodule]
1331
fn _lib(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
1432
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
33+
m.add_function(wrap_pyfunction!(demo, m)?)?;
1534
Ok(())
1635
}

0 commit comments

Comments
 (0)