Skip to content

Commit d499fd1

Browse files
committed
examples: split maturin and setuptools-rust examples
1 parent 5daadd4 commit d499fd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1133
-43
lines changed

examples/maturin_extension/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
authors = ["PyO3 Authors"]
3+
name = "maturin_extension"
4+
version = "0.1.0"
5+
description = "A Python wrapper for the Rust API for purposes of testing"
6+
edition = "2018"
7+
8+
[dependencies]
9+
10+
[dependencies.pyo3]
11+
path = "../../"
12+
features = ["extension-module"]
13+
14+
[lib]
15+
name = "maturin_extension"
16+
crate-type = ["cdylib"]
17+
18+
[package.metadata.maturin]
19+
classifier=[
20+
"License :: OSI Approved :: MIT License",
21+
"Development Status :: 3 - Alpha",
22+
"Intended Audience :: Developers",
23+
"Programming Language :: Python",
24+
"Programming Language :: Rust",
25+
"Operating System :: POSIX",
26+
"Operating System :: MacOS :: MacOS X",
27+
]

examples/rustapi_module/README.md renamed to examples/maturin_extension/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rustapi_module
1+
# maturin_extension
22

33
A simple extension module built using PyO3.
44

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .maturin_extension import *
2+
3+
from .register_submodules import _register_submodules
4+
_register_submodules()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import sys
3+
import importlib
4+
import importlib.abc
5+
6+
# Import the local extension
7+
from .maturin_extension import __file__ as rust_extension_path
8+
9+
10+
class SubmoduleFinder(importlib.abc.MetaPathFinder):
11+
def find_module(self, fullname, path):
12+
if fullname.startswith('maturin_extension.'):
13+
return importlib.machinery.ExtensionFileLoader(
14+
fullname,
15+
rust_extension_path
16+
)
17+
18+
19+
def _register_submodules():
20+
"""Inject custom finder into sys.meta_path"""
21+
sys.meta_path.append(SubmoduleFinder())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pip>=19.1
2+
hypothesis>=3.55
3+
pytest>=3.5.0
4+
psutil>=5.6

examples/rustapi_module/src/dict_iter.rs renamed to examples/maturin_extension/src/dict_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
33
use pyo3::types::PyDict;
44

55
#[pymodule]
6-
fn test_dict(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
6+
fn dict_iter(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
77
m.add_class::<DictSize>()?;
88
Ok(())
99
}

examples/maturin_extension/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use pyo3::prelude::*;
2+
use pyo3::wrap_pymodule;
3+
4+
pub mod buf_and_str;
5+
pub mod datetime;
6+
pub mod dict_iter;
7+
pub mod misc;
8+
pub mod objstore;
9+
pub mod othermod;
10+
pub mod pyclass_iter;
11+
pub mod subclassing;
12+
13+
use buf_and_str::*;
14+
use datetime::*;
15+
use dict_iter::*;
16+
use misc::*;
17+
use objstore::*;
18+
use othermod::*;
19+
use pyclass_iter::*;
20+
use subclassing::*;
21+
22+
#[pymodule]
23+
fn maturin_extension(_py: Python, m: &PyModule) -> PyResult<()> {
24+
m.add_wrapped(wrap_pymodule!(buf_and_str))?;
25+
m.add_wrapped(wrap_pymodule!(datetime))?;
26+
m.add_wrapped(wrap_pymodule!(dict_iter))?;
27+
m.add_wrapped(wrap_pymodule!(misc))?;
28+
m.add_wrapped(wrap_pymodule!(objstore))?;
29+
m.add_wrapped(wrap_pymodule!(othermod))?;
30+
m.add_wrapped(wrap_pymodule!(pyclass_iter))?;
31+
m.add_wrapped(wrap_pymodule!(subclassing))?;
32+
33+
Ok(())
34+
}

examples/rustapi_module/src/othermod.rs renamed to examples/maturin_extension/src/othermod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn double(x: i32) -> i32 {
3131

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

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

examples/rustapi_module/tests/test_buf_and_str.py renamed to examples/maturin_extension/tests/test_buf_and_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import psutil
66
import pytest
7-
from rustapi_module.buf_and_str import BytesExtractor
7+
from maturin_extension.buf_and_str import BytesExtractor
88

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

examples/rustapi_module/tests/test_datetime.py renamed to examples/maturin_extension/tests/test_datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66

77
import pytest
8-
import rustapi_module.datetime as rdt
8+
import maturin_extension.datetime as rdt
99
from hypothesis import given, example
1010
from hypothesis import strategies as st
1111

examples/rustapi_module/tests/test_dict_iter.py renamed to examples/maturin_extension/tests/test_dict_iter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from rustapi_module.test_dict import DictSize
2+
from maturin_extension.dict_iter import DictSize
33

44

55
@pytest.mark.parametrize("size", [64, 128, 256])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import maturin_extension.misc
2+
3+
4+
def test_issue_219():
5+
# Should not deadlock
6+
maturin_extension.misc.issue_219()

examples/rustapi_module/tests/test_objstore.py renamed to examples/maturin_extension/tests/test_objstore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from rustapi_module.objstore import ObjStore
7+
from maturin_extension.objstore import ObjStore
88

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

examples/rustapi_module/tests/test_othermod.py renamed to examples/maturin_extension/tests/test_othermod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from hypothesis import given, assume
22
from hypothesis import strategies as st
33

4-
from rustapi_module import othermod
4+
from maturin_extension import othermod
55

66
INTEGER32_ST = st.integers(min_value=(-(2 ** 31)), max_value=(2 ** 31 - 1))
77
USIZE_ST = st.integers(min_value=othermod.USIZE_MIN, max_value=othermod.USIZE_MAX)

examples/rustapi_module/tests/test_pyclass_iter.py renamed to examples/maturin_extension/tests/test_pyclass_iter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from rustapi_module import pyclass_iter
2+
from maturin_extension import pyclass_iter
33

44

55
def test_iter():

examples/rustapi_module/tests/test_subclassing.py renamed to examples/maturin_extension/tests/test_subclassing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import platform
22

3-
from rustapi_module.subclassing import Subclassable
3+
from maturin_extension.subclassing import Subclassable
44

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

examples/maturin_extension/tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tox]
2+
# can't install from sdist because local pyo3 repo can't be included in the sdist
3+
skipsdist = true
4+
5+
[testenv]
6+
description = Run the unit tests under {basepython}
7+
deps = -rrequirements-dev.txt
8+
commands =
9+
# Use pip fork with in-tree-build feature (soon to be merged to master)
10+
pip install --upgrade git+https://github.com/davidhewitt/pip.git@fede74f7439f6c940f19a2357738b500372abe3d
11+
pip install . --use-feature=in-tree-build
12+
pytest {posargs}

examples/rustapi_module/rustapi_module/__init__.py

Whitespace-only changes.

examples/rustapi_module/src/lib.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/rustapi_module/tests/test_misc.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/rustapi_module/Cargo.toml renamed to examples/setuptools_rust_extension/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
authors = ["PyO3 Authors"]
3-
name = "rustapi-module"
3+
name = "setuptools_rust_extension"
44
version = "0.1.0"
55
description = "A Python wrapper for the Rust API for purposes of testing"
66
edition = "2018"
@@ -12,5 +12,5 @@ path = "../../"
1212
features = ["extension-module"]
1313

1414
[lib]
15-
name = "rustapi_module"
15+
name = "setuptools_rust_extension"
1616
crate-type = ["cdylib"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include pyproject.toml Cargo.toml
2+
recursive-include src *
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# setuptools_rust_extension
2+
3+
A simple extension module built using PyO3.
4+
5+
## Build
6+
7+
```shell
8+
python setup.py install
9+
```
10+
11+
## Testing
12+
13+
To test install tox globally and run
14+
15+
```shell
16+
tox -e py
17+
```

examples/rustapi_module/setup.py renamed to examples/setuptools_rust_extension/setup.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ def get_py_version_cfgs():
1919
return out_cfg
2020

2121

22-
def make_rust_extension(module_name):
23-
return RustExtension(
24-
module_name, "Cargo.toml", rustc_flags=get_py_version_cfgs(), debug=True
25-
)
26-
27-
2822
setup(
29-
name="rustapi-module",
23+
name="setuptools_rust_extension",
3024
version="0.1.0",
3125
classifiers=[
3226
"License :: OSI Approved :: MIT License",
@@ -37,16 +31,14 @@ def make_rust_extension(module_name):
3731
"Operating System :: POSIX",
3832
"Operating System :: MacOS :: MacOS X",
3933
],
40-
packages=["rustapi_module"],
34+
packages=["setuptools_rust_extension"],
4135
rust_extensions=[
42-
make_rust_extension("rustapi_module.buf_and_str"),
43-
make_rust_extension("rustapi_module.datetime"),
44-
make_rust_extension("rustapi_module.misc"),
45-
make_rust_extension("rustapi_module.objstore"),
46-
make_rust_extension("rustapi_module.othermod"),
47-
make_rust_extension("rustapi_module.pyclass_iter"),
48-
make_rust_extension("rustapi_module.subclassing"),
49-
make_rust_extension("rustapi_module.test_dict"),
36+
RustExtension(
37+
'setuptools_rust_extension.setuptools_rust_extension',
38+
"Cargo.toml",
39+
rustc_flags=get_py_version_cfgs(),
40+
debug=True
41+
)
5042
],
5143
include_package_data=True,
5244
zip_safe=False,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .setuptools_rust_extension import *
2+
3+
from .register_submodules import _register_submodules
4+
_register_submodules()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import sys
3+
import importlib
4+
import importlib.abc
5+
6+
# Import the local extension
7+
from .setuptools_rust_extension import __file__ as rust_extension_path
8+
9+
10+
class SubmoduleFinder(importlib.abc.MetaPathFinder):
11+
def find_module(self, fullname, path):
12+
if fullname.startswith('setuptools_rust_extension.'):
13+
return importlib.machinery.ExtensionFileLoader(
14+
fullname,
15+
rust_extension_path
16+
)
17+
18+
19+
def _register_submodules():
20+
"""Inject custom finder into sys.meta_path"""
21+
sys.meta_path.append(SubmoduleFinder())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Objects related to PyBuffer and PyStr
2+
use pyo3::prelude::*;
3+
use pyo3::types::{PyBytes, PyString};
4+
5+
/// This is for confirming that PyBuffer does not cause memory leak
6+
#[pyclass]
7+
struct BytesExtractor {}
8+
9+
#[pymethods]
10+
impl BytesExtractor {
11+
#[new]
12+
pub fn __new__() -> Self {
13+
BytesExtractor {}
14+
}
15+
16+
pub fn from_bytes(&mut self, bytes: &PyBytes) -> PyResult<usize> {
17+
let byte_vec: Vec<u8> = bytes.extract().unwrap();
18+
Ok(byte_vec.len())
19+
}
20+
21+
pub fn from_str(&mut self, string: &PyString) -> PyResult<usize> {
22+
let rust_string: String = string.extract().unwrap();
23+
Ok(rust_string.len())
24+
}
25+
26+
pub fn from_str_lossy(&mut self, string: &PyString) -> PyResult<usize> {
27+
let rust_string_lossy: String = string.to_string_lossy().to_string();
28+
Ok(rust_string_lossy.len())
29+
}
30+
}
31+
32+
#[pymodule]
33+
pub fn buf_and_str(_py: Python, m: &PyModule) -> PyResult<()> {
34+
m.add_class::<BytesExtractor>()?;
35+
Ok(())
36+
}

0 commit comments

Comments
 (0)