Skip to content

Commit 48ee24e

Browse files
committed
Add a test for building pyo3 Python extension
1 parent cae86b8 commit 48ee24e

File tree

5 files changed

+362
-0
lines changed

5 files changed

+362
-0
lines changed

.github/workflows/e2e-extension.yml

+27
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,30 @@ jobs:
4040
- name: Run C extension
4141
run: |
4242
${{ steps.setup-python.outputs.python-path }} -c "import helloworld; helloworld.say_hello('world')"
43+
python-pyo3-extension:
44+
name: Test building pyo3 extension (Python ${{ matrix.python-version}}, ${{ matrix.os }})
45+
runs-on: ${{ matrix.os }}
46+
strategy:
47+
fail-fast: false
48+
matrix:
49+
os: [ubuntu-latest, windows-latest, macos-latest]
50+
steps:
51+
- uses: actions/checkout@v4
52+
- name: Setup Python
53+
id: setup-python
54+
uses: ./
55+
with:
56+
python-version: |
57+
3.9
58+
pypy3.9
59+
cache: 'pip'
60+
- name: Build pyo3 extension
61+
uses: PyO3/maturin-action@v1
62+
with:
63+
args: --release --interpreter 'python3.9 pypy3.9'
64+
working-directory: __tests__/data/pyo3_extension/
65+
- name: Install pyo3 extension
66+
run: pip install --no-index --find-links __tests__/data/pyo3_extension/target/wheels/ helloworld
67+
- name: Run pyo3 extension
68+
run: |
69+
${{ steps.setup-python.outputs.python-path }} -c "import helloworld; helloworld.say_hello('world')"

__tests__/data/pyo3_extension/Cargo.lock

+295
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "helloworld"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
name = "helloworld"
9+
crate-type = ["cdylib"]
10+
11+
[dependencies.pyo3]
12+
version = "0.21.1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[build-system]
2+
requires = ["maturin>=1.6,<2.0"]
3+
build-backend = "maturin"
4+
5+
[project]
6+
name = "helloworld"
7+
requires-python = ">=3.8"
8+
classifiers = [
9+
"Programming Language :: Rust",
10+
"Programming Language :: Python :: Implementation :: CPython",
11+
"Programming Language :: Python :: Implementation :: PyPy",
12+
]
13+
dynamic = ["version"]
14+
15+
[tool.maturin]
16+
features = ["pyo3/extension-module"]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyfunction]
4+
fn say_hello(s: &str) {
5+
println!("Hello, {}!", s);
6+
}
7+
8+
#[pymodule]
9+
fn helloworld(m: &Bound<'_, PyModule>) -> PyResult<()> {
10+
m.add_function(wrap_pyfunction!(say_hello, m)?)?;
11+
Ok(())
12+
}

0 commit comments

Comments
 (0)