Skip to content

Commit 96c31e7

Browse files
authored
Add example Cargo.toml and build.rs for exposing pyo3 cfgs (#4641)
* Add example Cargo.toml and build.rs for exposing pyo3 cfgs * Document all of the config attributes * fix pyo3-ffi tests * ignore the code examples instead of adding a dev dependency
1 parent 9a4a9a2 commit 96c31e7

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pyo3-ffi/src/lib.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,39 @@
4343
//! PyO3 uses `rustc`'s `--cfg` flags to enable or disable code used for different Python versions.
4444
//! If you want to do this for your own crate, you can do so with the [`pyo3-build-config`] crate.
4545
//!
46-
//! - `Py_3_7`, `Py_3_8`, `Py_3_9`, `Py_3_10`: Marks code that is only enabled when
47-
//! compiling for a given minimum Python version.
46+
//! - `Py_3_7`, `Py_3_8`, `Py_3_9`, `Py_3_10`, `Py_3_11`, `Py_3_12`, `Py_3_13`: Marks code that is
47+
//! only enabled when compiling for a given minimum Python version.
4848
//! - `Py_LIMITED_API`: Marks code enabled when the `abi3` feature flag is enabled.
49+
//! - `Py_GIL_DISABLED`: Marks code that runs only in the free-threaded build of CPython.
4950
//! - `PyPy` - Marks code enabled when compiling for PyPy.
51+
//! - `GraalPy` - Marks code enabled when compiling for GraalPy.
52+
//!
53+
//! Additionally, you can query for the values `Py_DEBUG`, `Py_REF_DEBUG`,
54+
//! `Py_TRACE_REFS`, and `COUNT_ALLOCS` from `py_sys_config` to query for the
55+
//! corresponding C build-time defines. For example, to conditionally define
56+
//! debug code using `Py_DEBUG`, you could do:
57+
//!
58+
//! ```rust,ignore
59+
//! #[cfg(py_sys_config = "Py_DEBUG")]
60+
//! println!("only runs if python was compiled with Py_DEBUG")
61+
//! ```
62+
//!
63+
//! To use these attributes, add [`pyo3-build-config`] as a build dependency in
64+
//! your `Cargo.toml`:
65+
//!
66+
//! ```toml
67+
//! [build-dependency]
68+
//! pyo3-build-config = "VER"
69+
//! ```
70+
//!
71+
//! And then either create a new `build.rs` file in the project root or modify
72+
//! the existing `build.rs` file to call `use_pyo3_cfgs()`:
73+
//!
74+
//! ```rust,ignore
75+
//! fn main() {
76+
//! pyo3_build_config::use_pyo3_cfgs();
77+
//! }
78+
//! ```
5079
//!
5180
//! # Minimum supported Rust and Python versions
5281
//!

0 commit comments

Comments
 (0)