Skip to content

Commit 72be1cd

Browse files
authored
emit cargo:rustc-check-cfg=CHECK_CFG for pyo3s config names (#4163)
1 parent 7263fa9 commit 72be1cd

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn configure_pyo3() -> Result<()> {
4646
}
4747

4848
fn main() {
49+
pyo3_build_config::print_expected_cfgs();
4950
if let Err(e) = configure_pyo3() {
5051
eprintln!("error: {}", e.report());
5152
std::process::exit(1)

guide/src/migration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ To make PyO3's core functionality continue to work while the GIL Refs API is in
339339

340340
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.
341341

342-
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.
342+
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.
343343

344344
Before:
345345

346346
```rust
347-
# #[cfg(feature = "gil-refs-migration")] {
347+
# #[cfg(feature = "gil-refs")] {
348348
# use pyo3::prelude::*;
349349
# use pyo3::types::{PyList, PyType};
350350
# fn example<'py>(py: Python<'py>) -> PyResult<()> {

pyo3-build-config/src/impl_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
};
3131

3232
/// Minimum Python version PyO3 supports.
33-
const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 };
33+
pub(crate) const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 };
3434

3535
/// GraalPy may implement the same CPython version over multiple releases.
3636
const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
@@ -39,7 +39,7 @@ const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
3939
};
4040

4141
/// Maximum Python version that can be used as minimum required Python version with abi3.
42-
const ABI3_MAX_MINOR: u8 = 12;
42+
pub(crate) const ABI3_MAX_MINOR: u8 = 12;
4343

4444
/// Gets an environment variable owned by cargo.
4545
///

pyo3-build-config/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use target_lexicon::OperatingSystem;
4343
/// For examples of how to use these attributes, [see PyO3's guide](https://pyo3.rs/latest/building-and-distribution/multiple_python_versions.html).
4444
#[cfg(feature = "resolve-config")]
4545
pub fn use_pyo3_cfgs() {
46+
print_expected_cfgs();
4647
for cargo_command in get().build_script_outputs() {
4748
println!("{}", cargo_command)
4849
}
@@ -153,6 +154,25 @@ pub fn print_feature_cfgs() {
153154
}
154155
}
155156

157+
/// Registers `pyo3`s config names as reachable cfg expressions
158+
///
159+
/// - <https://github.com/rust-lang/cargo/pull/13571>
160+
/// - <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg>
161+
#[doc(hidden)]
162+
pub fn print_expected_cfgs() {
163+
println!("cargo:rustc-check-cfg=cfg(Py_LIMITED_API)");
164+
println!("cargo:rustc-check-cfg=cfg(PyPy)");
165+
println!("cargo:rustc-check-cfg=cfg(GraalPy)");
166+
println!("cargo:rustc-check-cfg=cfg(py_sys_config, values(\"Py_DEBUG\", \"Py_REF_DEBUG\", \"Py_TRACE_REFS\", \"COUNT_ALLOCS\"))");
167+
println!("cargo:rustc-check-cfg=cfg(invalid_from_utf8_lint)");
168+
169+
// allow `Py_3_*` cfgs from the minimum supported version up to the
170+
// maximum minor version (+1 for development for the next)
171+
for i in impl_::MINIMUM_SUPPORTED_VERSION.minor..=impl_::ABI3_MAX_MINOR + 1 {
172+
println!("cargo:rustc-check-cfg=cfg(Py_3_{i})");
173+
}
174+
}
175+
156176
/// Private exports used in PyO3's build.rs
157177
///
158178
/// Please don't use these - they could change at any time.

pyo3-ffi/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ fn print_config_and_exit(config: &InterpreterConfig) {
205205
}
206206

207207
fn main() {
208+
pyo3_build_config::print_expected_cfgs();
208209
if let Err(e) = configure_pyo3() {
209210
eprintln!("error: {}", e.report());
210211
std::process::exit(1)

src/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
//! enabled, `Ungil` is defined as the following:
9797
//!
9898
//! ```rust
99-
//! # #[cfg(FALSE)]
99+
//! # #[cfg(any())]
100100
//! # {
101101
//! #![feature(auto_traits, negative_impls)]
102102
//!

src/tests/hygiene/pyclass.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,23 @@ pub enum Enum {
3939
#[pyo3(crate = "crate")]
4040
pub struct Foo3 {
4141
#[pyo3(get, set)]
42-
#[cfg(FALSE)]
42+
#[cfg(any())]
4343
field: i32,
4444

4545
#[pyo3(get, set)]
46-
#[cfg(not(FALSE))]
46+
#[cfg(not(any()))]
4747
field: u32,
4848
}
4949

5050
#[crate::pyclass]
5151
#[pyo3(crate = "crate")]
5252
pub struct Foo4 {
5353
#[pyo3(get, set)]
54-
#[cfg(FALSE)]
55-
#[cfg(not(FALSE))]
54+
#[cfg(any())]
55+
#[cfg(not(any()))]
5656
field: i32,
5757

5858
#[pyo3(get, set)]
59-
#[cfg(not(FALSE))]
59+
#[cfg(not(any()))]
6060
field: u32,
6161
}

0 commit comments

Comments
 (0)