Skip to content

Commit b8590ba

Browse files
committed
python: add a setuptools-rust test
mainly tests that our Python works with PyO3
1 parent 66e08b0 commit b8590ba

File tree

9 files changed

+236
-0
lines changed

9 files changed

+236
-0
lines changed

gha-matrix-gen/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ runs:
5050
if 'clang-aarch64' not in entry['prefix']:
5151
entry['packages'].append(f"{ entry['prefix'] }-gdb")
5252
53+
if 'i686' not in entry['prefix']:
54+
entry['packages'].append(f"{ entry['prefix'] }-python-setuptools-rust")
55+
5356
if entry['cc'] != "gcc":
5457
entry['packages'].extend([
5558
f"{ entry['prefix'] }-lld",
@@ -78,6 +81,7 @@ runs:
7881
'gcc',
7982
'python',
8083
'python-setuptools',
84+
'python-setuptools-rust',
8185
'cython',
8286
'autotools',
8387
'ruby',

python/setuptools-rust/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
target/
2+
setuptools_rust_test/__pycache__/
3+
setuptools_rust_test/*.dll
4+
setuptools_rust_test/*.pyd
5+
build/

python/setuptools-rust/Cargo.lock

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/setuptools-rust/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "setuptools_rust_test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
name = "_lib"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
pyo3 = { version = "0.27.2" }

python/setuptools-rust/setup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
from setuptools import setup
4+
from setuptools_rust import Binding, RustExtension
5+
6+
setup(
7+
name="setuptools_rust_test",
8+
version="0.1.0",
9+
rust_extensions=[RustExtension("setuptools_rust_test._lib", binding=Binding.PyO3)],
10+
packages=["setuptools_rust_test"],
11+
zip_safe=False,
12+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ._lib import hello
2+
3+
if __name__ == "__main__":
4+
print(hello())

python/setuptools-rust/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyfunction]
4+
fn hello() -> PyResult<String> {
5+
Ok("Hello from Rust!".to_string())
6+
}
7+
8+
#[pymodule]
9+
fn _lib(m: &Bound<'_, PyModule>) -> PyResult<()> {
10+
m.add_function(wrap_pyfunction!(hello, m)?)?;
11+
Ok(())
12+
}

python/setuptools-rust/test.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
if [[ "$MSYSTEM" == "MINGW32" ]]; then
6+
echo "skipped on $MSYSTEM"
7+
exit 0;
8+
fi
9+
10+
# should be fixed in the next pyo3 version (0.28)
11+
link_arg=$(python -c "import sysconfig; print(sysconfig.get_config_var('LIBPYTHON'))")
12+
export RUSTFLAGS="-C link-arg=$link_arg"
13+
14+
./setup.py build_rust --inplace
15+
python -m setuptools_rust_test

python/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ python --version
66

77
(cd setuptools-cext && ./test.sh)
88
(cd setuptools-cython && ./test.sh)
9+
(cd setuptools-rust && ./test.sh)
910

1011
if [[ "$MSYSTEM" == "MSYS" ]]; then
1112
# Make sure python does not give away that we are not cygwin

0 commit comments

Comments
 (0)