Skip to content

Commit

Permalink
examples: split maturin and setuptools-rust examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Nov 10, 2020
1 parent 5daadd4 commit a169a71
Show file tree
Hide file tree
Showing 55 changed files with 1,136 additions and 43 deletions.
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",
]
File renamed without changes.
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
5 changes: 5 additions & 0 deletions examples/maturin_extension/maturin_extension/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .maturin_extension import *

from .register_submodules import _register_submodules

_register_submodules()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import sys
import importlib
import importlib.abc

# Import the local extension
from .maturin_extension import __file__ as rust_extension_path


class SubmoduleFinder(importlib.abc.MetaPathFinder):
def find_module(self, fullname, path):
if fullname.startswith("maturin_extension."):
return importlib.machinery.ExtensionFileLoader(
fullname, rust_extension_path
)


def _register_submodules():
"""Inject custom finder into sys.meta_path"""
sys.meta_path.append(SubmoduleFinder())
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 @ git+https://github.com/davidhewitt/maturin.git@f4dce9879999ea404379e2ebcceb9af661bcd5ec"]
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 @@
pip>=19.1
hypothesis>=3.55
pytest>=3.5.0
psutil>=5.6
File renamed without changes.
File renamed without changes.
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
34 changes: 34 additions & 0 deletions examples/maturin_extension/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use pyo3::prelude::*;
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))?;

Ok(())
}
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
from maturin_extension.buf_and_str import BytesExtractor

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

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,5 +1,5 @@
import pytest
from rustapi_module.test_dict import DictSize
from maturin_extension.dict_iter import DictSize


@pytest.mark.parametrize("size", [64, 128, 256])
Expand Down
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
maturin_extension.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
from maturin_extension.objstore import ObjStore

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

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
from maturin_extension import 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
from maturin_extension import pyclass_iter


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

from rustapi_module.subclassing import Subclassable
from maturin_extension.subclassing import Subclassable

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

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 fork with in-tree-build feature (soon to be merged to master)
python -m pip install --upgrade git+https://github.com/davidhewitt/pip.git@fede74f7439f6c940f19a2357738b500372abe3d
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
```
File renamed without changes.
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,5 @@
from .setuptools_rust_extension import *

from .register_submodules import _register_submodules

_register_submodules()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import sys
import importlib
import importlib.abc

# Import the local extension
from .setuptools_rust_extension import __file__ as rust_extension_path


class SubmoduleFinder(importlib.abc.MetaPathFinder):
def find_module(self, fullname, path):
if fullname.startswith("setuptools_rust_extension."):
return importlib.machinery.ExtensionFileLoader(
fullname, rust_extension_path
)


def _register_submodules():
"""Inject custom finder into sys.meta_path"""
sys.meta_path.append(SubmoduleFinder())
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

0 comments on commit a169a71

Please sign in to comment.