Skip to content

Commit e7d1430

Browse files
committed
TST: add Rust extension module using PyO3
This requires nightly Rust and unreleased Meson.
1 parent 47ea86f commit e7d1430

7 files changed

Lines changed: 195 additions & 0 deletions

File tree

tests/packages/rust-pyo3/Cargo.lock

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2026 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
[package]
6+
name = "rust-pyo3"
7+
version = "1.0.0"
8+
edition = "2024"
9+
10+
[lib]
11+
name = "rust_pyo3"
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
pyo3 = "0.29.0"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: 2026 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
project('rust-pyo3', 'rust', version: '1.0.0', meson_version: '>= 1.11.0', default_options: ['rust_std=2024'])
6+
7+
py = import('python').find_installation(pure: false)
8+
pkg = import('rust').workspace().package()
9+
10+
py.extension_module(
11+
'rust_pyo3',
12+
'src/lib.rs',
13+
rust_dependency_map : pkg.rust_dependency_map(),
14+
rust_args: pkg.rust_args(),
15+
dependencies : pkg.dependencies(),
16+
install: true,
17+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2021 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
[build-system]
6+
build-backend = "mesonpy"
7+
requires = ["meson-python"]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: 2026 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
use pyo3::prelude::*;
6+
7+
#[pymodule]
8+
mod rust_pyo3 {
9+
use pyo3::prelude::*;
10+
11+
/// Sum two numbers.
12+
#[pyfunction]
13+
fn sum(a: i64, b: i64) -> PyResult<i64> {
14+
Ok(a + b)
15+
}
16+
}

tests/packages/rust-pyo3/subprojects/.wraplock

Whitespace-only changes.

tests/test_wheel.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,11 @@ def test_limited_api_free_threaded(wheel_limited_api_free_threaded):
413413
assert name.group('pyver') == INTERPRETER
414414
assert name.group('abi') == 'abi3.abi3t' if FREE_THREADED_BUILD else 'abi3'
415415
assert name.group('plat') == PLATFORM
416+
417+
418+
@pytest.mark.skipif(MESON_VERSION < (1, 12, 0), reason='meson too old')
419+
@pytest.mark.skipif(not shutil.which('rustc'), reason='rust compiler not found')
420+
def test_rust_pyo3(venv, wheel_rust_pyo3):
421+
venv.pip('install', wheel_rust_pyo3)
422+
output = venv.python('-c', 'import rust_pyo3; print(rust_pyo3.sum(1, 2))')
423+
assert int(output) == 3

0 commit comments

Comments
 (0)