-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow extensions to be added via pyproject.toml
- Loading branch information
1 parent
646857a
commit 22e291e
Showing
13 changed files
with
460 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "hello-world" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
pyo3 = { version = "0.19.2", features = ["extension-module"] } | ||
|
||
[profile.release-lto] | ||
inherits = "release" | ||
lto = true | ||
|
||
[lib] | ||
# See https://github.com/PyO3/pyo3 for details | ||
name = "_lib" # private module to be nested into Python package | ||
crate-type = ["cdylib"] | ||
path = "rust/lib.rs" | ||
|
||
[[bin]] | ||
name = "print-hello" | ||
path = "rust/print_hello.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
graft python | ||
graft rust | ||
graft tests | ||
include Cargo.toml noxfile.py | ||
global-exclude */__pycache__/* | ||
global-exclude *.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from os.path import dirname | ||
|
||
import nox | ||
|
||
SETUPTOOLS_RUST = dirname(dirname(dirname(__file__))) | ||
|
||
|
||
@nox.session() | ||
def test(session: nox.Session): | ||
session.install(SETUPTOOLS_RUST, "wheel", "build", "pytest") | ||
# Ensure build works as intended | ||
session.install("--no-build-isolation", ".") | ||
# Test Rust binary | ||
session.run("print-hello") | ||
# Test script wrapper for Python entry-point | ||
session.run("sum-cli", "5", "7") | ||
session.run("rust-demo", "5", "7") | ||
# Test library | ||
session.run("pytest", "tests", *session.posargs) | ||
session.run("python", "-c", "from hello_world import _lib; print(_lib)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[build-system] | ||
requires = ["setuptools", "setuptools-rust"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "hello-world" | ||
version = "1.0" | ||
|
||
[project.scripts] | ||
# Python entry-point wrapper to be installed in `$venv/bin` | ||
sum-cli = "hello_world.sum_cli:main" # Python function that uses Rust | ||
rust-demo = "hello_world._lib:demo" # Rust function that uses Python | ||
|
||
[tool.setuptools.packages] | ||
# Pure Python packages/modules | ||
find = { where = ["python"] } | ||
|
||
[[tool.setuptools-rust.ext-modules]] | ||
# Private Rust extension module to be nested into Python package | ||
target = "hello_world._lib" # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml, | ||
# but you can add a prefix to nest it inside of a Python package. | ||
py-limited-api = "auto" # Default value, can be omitted | ||
binding = "PyO3" # Default value, can be omitted | ||
# See reference for RustExtension in https://setuptools-rust.readthedocs.io/en/latest/reference.html | ||
|
||
[[tool.setuptools-rust.bins]] | ||
# Rust executable to be installed in `$venv/bin` | ||
target = "print-hello" # Needs to match bin.name in Cargo.toml | ||
args = ["--profile", "release-lto"] # Extra args for Cargo | ||
# See reference for RustBin in https://setuptools-rust.readthedocs.io/en/latest/reference.html | ||
# Note that you can also use Python entry-points as alternative to Rust binaries |
3 changes: 3 additions & 0 deletions
3
examples/hello-world-pyprojecttoml/python/hello_world/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._lib import sum_as_string # export public parts of the binary extension | ||
|
||
__all__ = ["sum_as_string"] |
16 changes: 16 additions & 0 deletions
16
examples/hello-world-pyprojecttoml/python/hello_world/sum_cli.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import argparse | ||
import sys | ||
|
||
from ._lib import sum_as_string | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser("sum 2 integers") | ||
parser.add_argument("x", type=int) | ||
parser.add_argument("y", type=int) | ||
args = parser.parse_args() | ||
print(f"{args.x} + {args.y} = {sum_as_string(args.x, args.y)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.