-
Notifications
You must be signed in to change notification settings - Fork 760
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
examples: maturin and setuptools_rust examples #1537
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
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
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,27 @@ | ||
[package] | ||
authors = ["PyO3 Authors"] | ||
name = "maturin-starter" | ||
version = "0.1.0" | ||
description = "An example project to get started using PyO3 with maturin" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
|
||
[dependencies.pyo3] | ||
path = "../../" | ||
features = ["extension-module"] | ||
|
||
[lib] | ||
name = "maturin_starter" | ||
crate-type = ["cdylib"] | ||
|
||
[package.metadata.maturin] | ||
classifier=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Developers", | ||
"Programming Language :: Python", | ||
"Programming Language :: Rust", | ||
"Operating System :: POSIX", | ||
"Operating System :: MacOS :: MacOS X", | ||
] |
File renamed without changes.
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,24 @@ | ||
# maturin-starter | ||
|
||
An example of a basic Python extension module built using PyO3 and [`maturin`](https://github.com/PyO3/maturin). | ||
|
||
## Building and Testing | ||
|
||
To build this package, first install `maturin`: | ||
|
||
```shell | ||
pip install maturin | ||
``` | ||
|
||
To build and test use `maturin develop`: | ||
|
||
```shell | ||
pip install -r requirements-dev.txt | ||
maturin develop && pytest | ||
``` | ||
|
||
Alternatively, install tox and run the tests inside an isolated environment: | ||
|
||
```shell | ||
tox -e 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,7 @@ | ||
# import the contents of the Rust library into the Python extension | ||
from .maturin_starter import * | ||
|
||
|
||
class PythonClass: | ||
def __init__(self, value: int) -> None: | ||
self.value = value |
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 @@ | ||
[build-system] | ||
requires = ["maturin>=0.10,<0.11"] | ||
build-backend = "maturin" |
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 @@ | ||
pytest>=3.5.0 |
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,35 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyDict; | ||
use pyo3::wrap_pymodule; | ||
|
||
mod submodule; | ||
use submodule::*; | ||
|
||
#[pyclass] | ||
struct ExampleClass { | ||
#[pyo3(get, set)] | ||
value: i32, | ||
} | ||
|
||
#[pymethods] | ||
impl ExampleClass { | ||
#[new] | ||
pub fn new(value: i32) -> Self { | ||
ExampleClass { value } | ||
} | ||
} | ||
|
||
#[pymodule] | ||
fn maturin_starter(py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<ExampleClass>()?; | ||
m.add_wrapped(wrap_pymodule!(submodule))?; | ||
|
||
// Inserting to sys.modules allows importing submodules nicely from Python | ||
// e.g. from maturin_starter.submodule import SubmoduleClass | ||
|
||
let sys = PyModule::import(py, "sys")?; | ||
let sys_modules: &PyDict = sys.getattr("modules")?.downcast()?; | ||
sys_modules.set_item("maturin_starter.submodule", m.getattr("submodule")?)?; | ||
|
||
Ok(()) | ||
} |
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,22 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass] | ||
struct SubmoduleClass {} | ||
|
||
#[pymethods] | ||
impl SubmoduleClass { | ||
#[new] | ||
pub fn __new__() -> Self { | ||
SubmoduleClass {} | ||
} | ||
|
||
pub fn greeting(&self) -> &'static str { | ||
"Hello, world!" | ||
} | ||
} | ||
|
||
#[pymodule] | ||
pub fn submodule(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<SubmoduleClass>()?; | ||
Ok(()) | ||
} |
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,11 @@ | ||
from maturin_starter import PythonClass, ExampleClass | ||
|
||
|
||
def test_python_class() -> None: | ||
py_class = PythonClass(value=10) | ||
assert py_class.value == 10 | ||
|
||
|
||
def test_example_class() -> None: | ||
example = ExampleClass(value=11) | ||
assert example.value == 11 |
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 @@ | ||
from maturin_starter.submodule import SubmoduleClass | ||
|
||
|
||
def test_submodule_class() -> None: | ||
submodule_class = SubmoduleClass() | ||
assert submodule_class.greeting() == "Hello, world!" |
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
File renamed without changes.
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 @@ | ||
include Cargo.toml | ||
recursive-include src * | ||
recursive-include tests |
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,18 @@ | ||
# pyo3-benchmarks | ||
|
||
This extension module contains benchmarks for pieces of PyO3's API accessible from Python. | ||
|
||
## Running the benchmarks | ||
|
||
You can install the module in your Python environment and then run the benchmarks with pytest: | ||
|
||
```shell | ||
python setup.py develop | ||
pytest | ||
``` | ||
|
||
Or with tox: | ||
|
||
```shell | ||
tox -e py | ||
``` |
File renamed without changes.
2 changes: 0 additions & 2 deletions
2
...ples/pyo3_benchmarks/requirements-dev.txt → ...ples/pyo3-benchmarks/requirements-dev.txt
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
pip>=19.1 | ||
hypothesis>=3.55 | ||
pytest>=3.5.0 | ||
setuptools-rust>=0.10.2 | ||
psutil>=5.6 | ||
pytest-benchmark~=3.2 |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,27 @@ | ||
[package] | ||
authors = ["PyO3 Authors"] | ||
name = "pyo3-pytests" | ||
version = "0.1.0" | ||
description = "Python-based tests for PyO3" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
|
||
[dependencies.pyo3] | ||
path = "../../" | ||
features = ["extension-module"] | ||
|
||
[lib] | ||
name = "pyo3_pytests" | ||
crate-type = ["cdylib"] | ||
|
||
[package.metadata.maturin] | ||
classifier=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Developers", | ||
"Programming Language :: Python", | ||
"Programming Language :: Rust", | ||
"Operating System :: POSIX", | ||
"Operating System :: MacOS :: MacOS X", | ||
] |
File renamed without changes.
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,19 @@ | ||
# pyo3-pytests | ||
|
||
An extension module built using PyO3, used to test PyO3 from Python. | ||
|
||
## Testing | ||
|
||
This package is intended to be built using `maturin`. Once built, you can run the tests using `pytest`: | ||
|
||
```shell | ||
pip install maturin | ||
maturin develop | ||
pytest | ||
``` | ||
|
||
Alternatively, install tox and run the tests inside an isolated environment: | ||
|
||
```shell | ||
tox -e 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,28 @@ | ||
use std::process::Command; | ||
|
||
fn main() { | ||
let out = Command::new("python") | ||
.args(&["-c", "import sys; import platform; print(sys.version_info[1]); print(platform.python_implementation())"]) | ||
.output() | ||
.expect("python version did not print"); | ||
|
||
let output = String::from_utf8_lossy(&out.stdout); | ||
let mut lines = output.trim().lines(); | ||
|
||
println!("{}", output); | ||
|
||
let version: u8 = lines | ||
.next() | ||
.unwrap() | ||
.parse() | ||
.expect("python version was not parsed"); | ||
let implementation = lines.next().unwrap(); | ||
|
||
for each in 6..version { | ||
println!("cargo:rustc-cfg=Py_3_{}", each); | ||
} | ||
|
||
if implementation == "PyPy" { | ||
println!("cargo:rustc-cfg=PyPy"); | ||
} | ||
} |
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 @@ | ||
from .pyo3_pytests import * |
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 @@ | ||
[build-system] | ||
requires = ["maturin>=0.10,<0.11"] | ||
build-backend = "maturin" |
1 change: 0 additions & 1 deletion
1
examples/rustapi_module/requirements-dev.txt → examples/pyo3-pytests/requirements-dev.txt
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
pip>=19.1 | ||
hypothesis>=3.55 | ||
pytest>=3.5.0 | ||
setuptools-rust>=0.10.2 | ||
psutil>=5.6 |
File renamed without changes.
File renamed without changes.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This used to be in setup.py, but we now build the tests with maturin instead of setuptools-rust.
We need it because some tests only compile on certain python / PyPy combinations.
IMO this
build.rs
method is better anyway because it would still work if we changed the examples back tosetuptools_rust
.It's a little ugly to have to call a Python interpreter here. I have an idea how to split some of pyo3's
build.rs
logic into a separate cratepyo3-build-config,
which this build.rs could then re-use. I've been torn for a while on whether that refactoring is worth the additional complexity; maybe I should hurry up and push that PR for a while so that others can see what they think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to move this into a new issue so that it doesn't block this PR with all the new examples, which I think has value even if there's this this wart.