-
Notifications
You must be signed in to change notification settings - Fork 23
Linker errors with #[gen_stub_pyfunction] #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
same issue |
@chingiztob Did you try following Building and Distribution? |
Which exact part of "Building and Distribution" should I check? This section is huge, and I have no build issues with the Maturin/PyO3 project itself—only with stub_gen, which is compiled separately. I will try to create a minimal reproducible example soon. |
@chingiztob Excuse me for not providing the correct section, updated the link in the original reply. This might help as well: |
Unfortunately, none of this helps. I probably should have mentioned that the errors are occurring on a Linux machine, not a Mac. As for a reproducible example, it turns out it's not so straightforward—it works on a basic "sum_as_string" project. I'll dig further. use pyo3::prelude::*;
use pyo3_stub_gen::{define_stub_info_gatherer, derive::gen_stub_pyfunction};
/// Formats the sum of two numbers as string.
#[gen_stub_pyfunction]
#[pyfunction]
#[pyo3(name = "bar", signature = (a,b = 2))]
pub fn sum_as_string( py: Python<'_>, a: usize, b: usize) -> PyResult<String> {
py.allow_threads(|| {
Ok((a + b).to_string())
})
}
/// A Python module implemented in Rust.
#[pymodule]
fn error_test(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
define_stub_info_gatherer!(stub_info); |
This should be because
|
Okay, the linker errors are gone now, but there's a new error: target/release/stub_gen: error while loading shared libraries: libpython3.13.so.1.0: cannot open shared object file: No such file or directory Could this be caused by a difference between the Python version in the Venv (3.13.1) and the system Python version (3.12.3)? |
First, PyO3 asks where python -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))' This returns the directory where |
Reinstalling the UV-managed Python version helped—thank you very much! Also, it seems to behave differently (for some reason) when running |
Well, now the GitHub Maturin action is broken for PyPy. Should I open a separate issue? Compiling pyo3-build-config v0.24.1
Compiling pyo3-macros-backend v0.24.1
Compiling pyo3-ffi v0.24.1
Compiling pyo3 v0.24.1
Compiling numpy v0.24.0
Compiling pyo3-macros v0.24.1
Compiling pyo3-log v0.12.3
Compiling pyo3-stub-gen v0.7.0
Compiling ferrobus v0.1.2 (/home/runner/work/ferrobus/ferrobus)
error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
--> src/model.rs:99:1
|
99 | #[gen_stub_pyfunction]
| ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
|
note: found an item that was configured out
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
|
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
|
345 | #[cfg(not(any(PyPy, GraalPy)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
--> src/routing.rs:126:1
|
126 | #[gen_stub_pyfunction]
| ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
|
note: found an item that was configured out
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
|
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
|
345 | #[cfg(not(any(PyPy, GraalPy)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
--> src/routing.rs:199:1
|
199 | #[gen_stub_pyfunction]
| ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
|
note: found an item that was configured out
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
|
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
|
345 | #[cfg(not(any(PyPy, GraalPy)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find function `prepare_freethreaded_python` in crate `pyo3`
--> src/routing.rs:258:1
|
258 | #[gen_stub_pyfunction]
| ^^^^^^^^^^^^^^^^^^^^^^ not found in `pyo3`
|
note: found an item that was configured out
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:346:22
|
346 | pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
--> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.24.1/src/lib.rs:345:1
|
345 | #[cfg(not(any(PyPy, GraalPy)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `gen_stub_pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0425`.
error: could not compile `ferrobus` (lib) due to 4 previous errors
💥 maturin failed
Caused by: Failed to build a native library through cargo
Caused by: Cargo build finished with "exit status: 101": `env -u CARGO CARGO_ENCODED_RUSTFLAGS="-C\u{1f}target-feature=-crt-static" PYO3_CONFIG_FILE="/home/runner/work/ferrobus/ferrobus/target/maturin/pyo3-config-aarch64-unknown-linux-musl-3.9.txt" "cargo" "rustc" "--features" "pyo3/extension-module" "--target" "aarch64-unknown-linux-musl" "--message-format" "json-render-diagnostics" "--manifest-path" "/home/runner/work/ferrobus/ferrobus/Cargo.toml" "--release" "--lib" "--crate-type" "cdylib"`
Error: The process '/usr/bin/docker' failed with exit code 1
at ExecState._setResult (/home/runner/work/_actions/PyO3/maturin-action/v1/dist/index.js:1702:25)
Error: The process '/usr/bin/docker' failed with exit code 1
at ExecState.CheckComplete (/home/runner/work/_actions/PyO3/maturin-action/v1/dist/index.js:1685:18)
at ChildProcess.<anonymous> (/home/runner/work/_actions/PyO3/maturin-action/v1/dist/index.js:1579:27)
at ChildProcess.emit (node:events:524:28)
at maybeClose (node:internal/child_process:1104:16)
at ChildProcess._handle.onexit (node:internal/child_process:304:5) |
@Kile Since stubgen is not necessary for maturin wheel builds. I fixed this by hiding all stubgen-related stuff behind a feature gate with a macro use proc_macro::TokenStream;
use quote::quote;
use syn::{Item, parse_macro_input};
#[proc_macro_attribute]
pub fn stubgen(_attrs: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as Item);
// Wrap the item with #[cfg_attr(feature = "stubgen", ...)]
let output = match input {
Item::Struct(s) => {
quote! {
#[cfg_attr(feature = "stubgen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#s
}
}
Item::Impl(i) => {
quote! {
#[cfg_attr(feature = "stubgen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#i
}
}
Item::Fn(f) => {
quote! {
#[cfg_attr(feature = "stubgen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#f
}
}
_ => {
quote! {
#input
}
}
};
output.into()
} |
Oh awesome thanks for the workaround! In which file does this go? Also would still be nice to have an official patch 😅 |
@Kile You would need to create a new lib crate in the workspace, place this macro there, replace all #[gen ...] with this macro, and also gate the Then make pyo3-stub-gen an optional dependency, and modify stub_gen.rs as well: #[cfg(feature = "stubgen")]
use pyo3_stub_gen::Result;
#[cfg(feature = "stubgen")]
fn main() -> Result<()> {
let stub = you_crate::stub_info()?;
stub.generate()?;
Ok(())
}
#[cfg(not(feature = "stubgen"))]
fn main() {
eprintln!("The 'stubgen' feature is not enabled. Enable it with `--features stubgen`.");
} This is kinda cringe solution, but it will work untill patch |
@chingiztob thanks, wow that really is cringe. Any way to keep it in the same repo? Or do I need to completely restructure my repo to contain two crates? |
@Kile If you are not using workspace - something like this: my_crate/
├── Cargo.toml
├── src/
│ └── lib.rs
├── my_crate_stubgen/
│ ├── Cargo.toml
│ └── src/
│ └── lib.rs |
Ah cool thanks |
Did you add this line to
I just reproduce the error by deleting the line, but it is no-op on Linux, lol. |
I get compile errors if the derive macro #[gen_stub_pyfunction] is placed above #[pyfunction(...)] or #[pyo3(...)]. When placed under #[pyfunction(...)], the error does not occur, but the .pyi signature is incorrect, as it ignores the signature and name attributes (i.e., #[pyfunction(name = "foo_bar")]).
PyO3: 0.24
PyO3-stub-gen: 0.7
CC: 1.2.17
The text was updated successfully, but these errors were encountered: