Skip to content
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: split maturin and setuptools-rust examples #1269

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ nightly = []
members = [
"pyo3-macros",
"pyo3-macros-backend",

"examples/pyo3_benchmarks",
"examples/rustapi_module",

"examples/maturin_extension",
"examples/setuptools_rust_extension",
"examples/word-count"
]
27 changes: 27 additions & 0 deletions examples/maturin_extension/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
authors = ["PyO3 Authors"]
name = "maturin_extension"
version = "0.1.0"
description = "A Python wrapper for the Rust API for purposes of testing"
edition = "2018"

[dependencies]

[dependencies.pyo3]
path = "../../"
features = ["extension-module"]

[lib]
name = "maturin_extension"
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",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rustapi_module
# maturin_extension

A simple extension module built using PyO3.

Expand Down
28 changes: 28 additions & 0 deletions examples/maturin_extension/build.rs
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");
}
}
1 change: 1 addition & 0 deletions examples/maturin_extension/maturin_extension/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .maturin_extension import *
3 changes: 3 additions & 0 deletions examples/maturin_extension/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["maturin"]
build-backend = "maturin"
4 changes: 4 additions & 0 deletions examples/maturin_extension/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
setuptools>=50
hypothesis>=3.55
pytest>=3.5.0
psutil>=5.6
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
use pyo3::types::PyDict;

#[pymodule]
fn test_dict(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn dict_iter(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<DictSize>()?;
Ok(())
}
Expand Down
49 changes: 49 additions & 0 deletions examples/maturin_extension/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::wrap_pymodule;

pub mod buf_and_str;
pub mod datetime;
pub mod dict_iter;
pub mod misc;
pub mod objstore;
pub mod othermod;
pub mod pyclass_iter;
pub mod subclassing;

use buf_and_str::*;
use datetime::*;
use dict_iter::*;
use misc::*;
use objstore::*;
use othermod::*;
use pyclass_iter::*;
use subclassing::*;

#[pymodule]
fn maturin_extension(py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pymodule!(buf_and_str))?;
m.add_wrapped(wrap_pymodule!(datetime))?;
m.add_wrapped(wrap_pymodule!(dict_iter))?;
m.add_wrapped(wrap_pymodule!(misc))?;
m.add_wrapped(wrap_pymodule!(objstore))?;
m.add_wrapped(wrap_pymodule!(othermod))?;
m.add_wrapped(wrap_pymodule!(pyclass_iter))?;
m.add_wrapped(wrap_pymodule!(subclassing))?;

// Inserting to sys.modules allows importing submodules nicely from Python
// e.g. import maturin_extension.buf_and_str as bas

let sys = PyModule::import(py, "sys")?;
let sys_modules: &PyDict = sys.getattr("modules")?.downcast()?;
sys_modules.set_item("maturin_extension.buf_and_str", m.getattr("buf_and_str")?)?;
sys_modules.set_item("maturin_extension.datetime", m.getattr("datetime")?)?;
sys_modules.set_item("maturin_extension.dict_iter", m.getattr("dict_iter")?)?;
sys_modules.set_item("maturin_extension.misc", m.getattr("misc")?)?;
sys_modules.set_item("maturin_extension.objstore", m.getattr("objstore")?)?;
sys_modules.set_item("maturin_extension.othermod", m.getattr("othermod")?)?;
sys_modules.set_item("maturin_extension.pyclass_iter", m.getattr("pyclass_iter")?)?;
sys_modules.set_item("maturin_extension.subclassing", m.getattr("subclassing")?)?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn double(x: i32) -> i32 {

#[pymodule]
fn othermod(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?;
m.add_wrapped(wrap_pyfunction!(double))?;

m.add_class::<ModClass>()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import psutil
import pytest
from rustapi_module.buf_and_str import BytesExtractor
import maturin_extension.buf_and_str as bf

PYPY = platform.python_implementation() == "PyPy"

Expand All @@ -16,7 +16,7 @@
)
def test_pybuffer_doesnot_leak_memory():
N = 10000
extractor = BytesExtractor()
extractor = bf.BytesExtractor()
process = psutil.Process(os.getpid())

def memory_diff(f):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys

import pytest
import rustapi_module.datetime as rdt
import maturin_extension.datetime as rdt
from hypothesis import given, example
from hypothesis import strategies as st

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest
from rustapi_module.test_dict import DictSize
import maturin_extension.dict_iter as rdi


@pytest.mark.parametrize("size", [64, 128, 256])
def test_size(size):
d = {}
for i in range(size):
d[i] = str(i)
assert DictSize(len(d)).iter_dict(d) == size
assert rdi.DictSize(len(d)).iter_dict(d) == size
6 changes: 6 additions & 0 deletions examples/maturin_extension/tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import maturin_extension.misc


def test_issue_219():
# Should not deadlock
misc.issue_219()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from rustapi_module.objstore import ObjStore
import maturin_extension.objstore

PYPY = platform.python_implementation() == "PyPy"

Expand All @@ -14,7 +14,7 @@ def test_objstore_doesnot_leak_memory():
N = 10000
message = b'\\(-"-;) Praying that memory leak would not happen..'
before = sys.getrefcount(message)
store = ObjStore()
store = objstore.ObjStore()
for _ in range(N):
store.push(message)
del store
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hypothesis import given, assume
from hypothesis import strategies as st

from rustapi_module import othermod
import maturin_extension.othermod

INTEGER32_ST = st.integers(min_value=(-(2 ** 31)), max_value=(2 ** 31 - 1))
USIZE_ST = st.integers(min_value=othermod.USIZE_MIN, max_value=othermod.USIZE_MAX)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from rustapi_module import pyclass_iter
import maturin_extension.pyclass_iter


def test_iter():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import platform

from rustapi_module.subclassing import Subclassable
import maturin_extension.subclassing

PYPY = platform.python_implementation() == "PyPy"


class SomeSubClass(Subclassable):
class SomeSubClass(subclassing.Subclassable):
def __str__(self):
return "SomeSubclass"

Expand Down
12 changes: 12 additions & 0 deletions examples/maturin_extension/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tox]
# can't install from sdist because local pyo3 repo can't be included in the sdist
skipsdist = true

[testenv]
description = Run the unit tests under {basepython}
deps = -rrequirements-dev.txt
commands =
# Use pip master with in-tree-build feature (to be released in pip 21.0)
python -m pip install --upgrade git+https://github.com/pypa/pip.git
python -m pip install . --use-feature=in-tree-build
pytest {posargs}
Empty file.
8 changes: 0 additions & 8 deletions examples/rustapi_module/src/lib.rs

This file was deleted.

6 changes: 0 additions & 6 deletions examples/rustapi_module/tests/test_misc.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
authors = ["PyO3 Authors"]
name = "rustapi-module"
name = "setuptools_rust_extension"
version = "0.1.0"
description = "A Python wrapper for the Rust API for purposes of testing"
edition = "2018"
Expand All @@ -12,5 +12,5 @@ path = "../../"
features = ["extension-module"]

[lib]
name = "rustapi_module"
name = "setuptools_rust_extension"
crate-type = ["cdylib"]
2 changes: 2 additions & 0 deletions examples/setuptools_rust_extension/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include pyproject.toml Cargo.toml
recursive-include src *
17 changes: 17 additions & 0 deletions examples/setuptools_rust_extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# setuptools_rust_extension

A simple extension module built using PyO3.

## Build

```shell
python setup.py install
```

## Testing

To test install tox globally and run

```shell
tox -e py
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ def get_py_version_cfgs():
return out_cfg


def make_rust_extension(module_name):
return RustExtension(
module_name, "Cargo.toml", rustc_flags=get_py_version_cfgs(), debug=True
)


setup(
name="rustapi-module",
name="setuptools_rust_extension",
version="0.1.0",
classifiers=[
"License :: OSI Approved :: MIT License",
Expand All @@ -37,16 +31,14 @@ def make_rust_extension(module_name):
"Operating System :: POSIX",
"Operating System :: MacOS :: MacOS X",
],
packages=["rustapi_module"],
packages=["setuptools_rust_extension"],
rust_extensions=[
make_rust_extension("rustapi_module.buf_and_str"),
make_rust_extension("rustapi_module.datetime"),
make_rust_extension("rustapi_module.misc"),
make_rust_extension("rustapi_module.objstore"),
make_rust_extension("rustapi_module.othermod"),
make_rust_extension("rustapi_module.pyclass_iter"),
make_rust_extension("rustapi_module.subclassing"),
make_rust_extension("rustapi_module.test_dict"),
RustExtension(
"setuptools_rust_extension.setuptools_rust_extension",
"Cargo.toml",
rustc_flags=get_py_version_cfgs(),
debug=True,
)
],
include_package_data=True,
zip_safe=False,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .setuptools_rust_extension import *
36 changes: 36 additions & 0 deletions examples/setuptools_rust_extension/src/buf_and_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Objects related to PyBuffer and PyStr
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyString};

/// This is for confirming that PyBuffer does not cause memory leak
#[pyclass]
struct BytesExtractor {}

#[pymethods]
impl BytesExtractor {
#[new]
pub fn __new__() -> Self {
BytesExtractor {}
}

pub fn from_bytes(&mut self, bytes: &PyBytes) -> PyResult<usize> {
let byte_vec: Vec<u8> = bytes.extract().unwrap();
Ok(byte_vec.len())
}

pub fn from_str(&mut self, string: &PyString) -> PyResult<usize> {
let rust_string: String = string.extract().unwrap();
Ok(rust_string.len())
}

pub fn from_str_lossy(&mut self, string: &PyString) -> PyResult<usize> {
let rust_string_lossy: String = string.to_string_lossy().to_string();
Ok(rust_string_lossy.len())
}
}

#[pymodule]
pub fn buf_and_str(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<BytesExtractor>()?;
Ok(())
}
Loading