Skip to content

TST: add Rust extension module using PyO3#874

Draft
dnicolodi wants to merge 8 commits into
mesonbuild:mainfrom
dnicolodi:rust-pyo3
Draft

TST: add Rust extension module using PyO3#874
dnicolodi wants to merge 8 commits into
mesonbuild:mainfrom
dnicolodi:rust-pyo3

Conversation

@dnicolodi

Copy link
Copy Markdown
Member

No description provided.

@dnicolodi
dnicolodi force-pushed the rust-pyo3 branch 3 times, most recently from d803b5c to 75b9ffa Compare July 19, 2026 19:49
@dnicolodi
dnicolodi force-pushed the rust-pyo3 branch 24 times, most recently from 824c371 to 5872020 Compare July 20, 2026 09:57
This requires nightly Rust and unreleased Meson, and it currently
bumps into a Meson warning: ``WARNING: Library entry portable_atomic
has unexpected keys "doc-scrape-examples"``.
@dnicolodi
dnicolodi marked this pull request as draft July 21, 2026 09:41
@dnicolodi

Copy link
Copy Markdown
Member Author

This almost works with unreleased Meson 1.12.0 and nightly Rust toolchain. The missing bit is translating the build.rs in pyo3-ffi into an equivalent meson.build. @bonzini did a first attempt, but either I didn't integrate it properly or there is a bug. I'll open another PR with a test package using the Python C API directly from Rust.

@bonzini

bonzini commented Jul 21, 2026

Copy link
Copy Markdown

It did what it was intending to do:

"--check-cfg" "cfg(pyo3_use_raw_dylib)" "--check-cfg" "cfg(pyo3_dll,values(any()))" "--cfg" "pyo3_use_raw_dylib" "--cfg" "pyo3_dll=\"python312.dll\""

It clearly needs more work to exactly match build.rs, possibly by someone that does have access to a Windows toolchain unlike you and I...

@dnicolodi

Copy link
Copy Markdown
Member Author

As I was mentioning in the related issue, I get a segmentation fault on macOS when the extension module raises an error. I don't get the segfault when compiling with maturin. Thus there is something off in the compilation on macOS too. I haven't had time to investigate a way to compare the output of build.rs with the configuration generated by Meson.

@lucascolley

Copy link
Copy Markdown

As I was mentioning in the related issue, I get a segmentation fault on macOS when the extension module raises an error.

How to reproduce? It seems fine for me on osx-arm64:

>>> rust_pyo3.sum(1, 2.0)
Traceback (most recent call last):
  File "<python-input-6>", line 1, in <module>
    rust_pyo3.sum(1, 2.0)
    ~~~~~~~~~~~~~^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer
while processing 'b'

@bonzini

bonzini commented Jul 22, 2026

Copy link
Copy Markdown

Ot just what are the missing flags from build.rs on macOS?

@dnicolodi

Copy link
Copy Markdown
Member Author

I compared the cargo:rustc-cfg= lines emitted by build.rs with the --cfg options appended to extra_flags by meson.build and neglected everything else. I added this to meson.build:

extra_args += [
  '--cfg', 'cfg_select',
  '--cfg', 'const_is_null',
  '--cfg', 'fn_ptr_eq',
  '--cfg', 'from_bytes_with_nul_error',
]

I didn't check what these are for.

I cannot reproduce the segmentation fault now... With my knowledge of Rust and associated tooling and working on this in bits and pieces in between other things I probably made a mistake somewhere...

@lucascolley

Copy link
Copy Markdown

I can possibly debug on windows in a few days' time, if someone can tell me roughly what to look for.

@bonzini

bonzini commented Jul 22, 2026

Copy link
Copy Markdown

Ok, those are easy as they are simply based on the rustc version.

https://github.com/PyO3/pyo3/blob/2e49529008277e11419ac4572ef03c441b604f77/pyo3-build-config/src/lib.rs#L270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants