-
Notifications
You must be signed in to change notification settings - Fork 105
Allow users to use pyproject.toml
define RustExtension
and RustBin
(instead of setup.py
)
#348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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]] | ||
abravalheri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.