emit `cargo:rustc-check-cfg=CHECK_CFG` for `pyo3`s config names (#4163)

This commit is contained in:
Icxolu 2024-05-08 07:46:00 +02:00 committed by GitHub
parent 7263fa92ef
commit 72be1cddba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 32 additions and 10 deletions

View File

@ -46,6 +46,7 @@ fn configure_pyo3() -> Result<()> {
}
fn main() {
pyo3_build_config::print_expected_cfgs();
if let Err(e) = configure_pyo3() {
eprintln!("error: {}", e.report());
std::process::exit(1)

View File

@ -339,12 +339,12 @@ To make PyO3's core functionality continue to work while the GIL Refs API is in
PyO3 0.21 has introduced the [`PyBackedStr`]({{#PYO3_DOCS_URL}}/pyo3/pybacked/struct.PyBackedStr.html) and [`PyBackedBytes`]({{#PYO3_DOCS_URL}}/pyo3/pybacked/struct.PyBackedBytes.html) types to help with this case. The easiest way to avoid lifetime challenges from extracting `&str` is to use these. For more complex types like `Vec<&str>`, is now impossible to extract directly from a Python object and `Vec<PyBackedStr>` is the recommended upgrade path.
A key thing to note here is because extracting to these types now ties them to the input lifetime, some extremely common patterns may need to be split into multiple Rust lines. For example, the following snippet of calling `.extract::<&str>()` directly on the result of `.getattr()` needs to be adjusted when deactivating the `gil-refs-migration` feature.
A key thing to note here is because extracting to these types now ties them to the input lifetime, some extremely common patterns may need to be split into multiple Rust lines. For example, the following snippet of calling `.extract::<&str>()` directly on the result of `.getattr()` needs to be adjusted when deactivating the `gil-refs` feature.
Before:
```rust
# #[cfg(feature = "gil-refs-migration")] {
# #[cfg(feature = "gil-refs")] {
# use pyo3::prelude::*;
# use pyo3::types::{PyList, PyType};
# fn example<'py>(py: Python<'py>) -> PyResult<()> {

View File

@ -30,7 +30,7 @@ use crate::{
};
/// Minimum Python version PyO3 supports.
const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 };
pub(crate) const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 };
/// GraalPy may implement the same CPython version over multiple releases.
const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
@ -39,7 +39,7 @@ const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
};
/// Maximum Python version that can be used as minimum required Python version with abi3.
const ABI3_MAX_MINOR: u8 = 12;
pub(crate) const ABI3_MAX_MINOR: u8 = 12;
/// Gets an environment variable owned by cargo.
///

View File

@ -43,6 +43,7 @@ use target_lexicon::OperatingSystem;
/// For examples of how to use these attributes, [see PyO3's guide](https://pyo3.rs/latest/building-and-distribution/multiple_python_versions.html).
#[cfg(feature = "resolve-config")]
pub fn use_pyo3_cfgs() {
print_expected_cfgs();
for cargo_command in get().build_script_outputs() {
println!("{}", cargo_command)
}
@ -153,6 +154,25 @@ pub fn print_feature_cfgs() {
}
}
/// Registers `pyo3`s config names as reachable cfg expressions
///
/// - <https://github.com/rust-lang/cargo/pull/13571>
/// - <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg>
#[doc(hidden)]
pub fn print_expected_cfgs() {
println!("cargo:rustc-check-cfg=cfg(Py_LIMITED_API)");
println!("cargo:rustc-check-cfg=cfg(PyPy)");
println!("cargo:rustc-check-cfg=cfg(GraalPy)");
println!("cargo:rustc-check-cfg=cfg(py_sys_config, values(\"Py_DEBUG\", \"Py_REF_DEBUG\", \"Py_TRACE_REFS\", \"COUNT_ALLOCS\"))");
println!("cargo:rustc-check-cfg=cfg(invalid_from_utf8_lint)");
// allow `Py_3_*` cfgs from the minimum supported version up to the
// maximum minor version (+1 for development for the next)
for i in impl_::MINIMUM_SUPPORTED_VERSION.minor..=impl_::ABI3_MAX_MINOR + 1 {
println!("cargo:rustc-check-cfg=cfg(Py_3_{i})");
}
}
/// Private exports used in PyO3's build.rs
///
/// Please don't use these - they could change at any time.

View File

@ -205,6 +205,7 @@ fn print_config_and_exit(config: &InterpreterConfig) {
}
fn main() {
pyo3_build_config::print_expected_cfgs();
if let Err(e) = configure_pyo3() {
eprintln!("error: {}", e.report());
std::process::exit(1)

View File

@ -96,7 +96,7 @@
//! enabled, `Ungil` is defined as the following:
//!
//! ```rust
//! # #[cfg(FALSE)]
//! # #[cfg(any())]
//! # {
//! #![feature(auto_traits, negative_impls)]
//!

View File

@ -39,11 +39,11 @@ pub enum Enum {
#[pyo3(crate = "crate")]
pub struct Foo3 {
#[pyo3(get, set)]
#[cfg(FALSE)]
#[cfg(any())]
field: i32,
#[pyo3(get, set)]
#[cfg(not(FALSE))]
#[cfg(not(any()))]
field: u32,
}
@ -51,11 +51,11 @@ pub struct Foo3 {
#[pyo3(crate = "crate")]
pub struct Foo4 {
#[pyo3(get, set)]
#[cfg(FALSE)]
#[cfg(not(FALSE))]
#[cfg(any())]
#[cfg(not(any()))]
field: i32,
#[pyo3(get, set)]
#[cfg(not(FALSE))]
#[cfg(not(any()))]
field: u32,
}