Skip to content

Commit 059a357

Browse files
committed
Add a test for building pyo3 Python extension
1 parent 2cc53a9 commit 059a357

File tree

5 files changed

+361
-0
lines changed

5 files changed

+361
-0
lines changed

.github/workflows/e2e-extension.yml

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