Skip to content

Commit 9895282

Browse files
committed
Add abi3-py* features
1 parent 3b3ba4e commit 9895282

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ rustversion = "1.0"
3535
[features]
3636
default = ["macros"]
3737
macros = ["ctor", "indoc", "inventory", "paste", "pyo3cls", "unindent"]
38-
# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for
39-
# more.
38+
# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for more.
4039
abi3 = []
40+
# With abi3, we can manually set the minimum Python version.
41+
abi3-py36 = ["abi3"]
42+
abi3-py37 = ["abi3-py36"]
43+
abi3-py38 = ["abi3-py37"]
44+
abi3-py39 = ["abi3-py38"]
4145

4246
# Optimizes PyObject to Vec conversion and so on.
4347
nightly = []

build.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use std::{
99
str::FromStr,
1010
};
1111

12-
const PY3_MIN_MINOR: u8 = 5;
12+
/// Minimum required Python version.
13+
const PY3_MIN_MINOR: u8 = 6;
14+
/// Maximum Python version that can be used as minimum required Python version with abi3.
15+
const ABI3_MAX_MINOR: u8 = 9;
1316
const CFG_KEY: &str = "py_sys_config";
1417

1518
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
@@ -770,12 +773,18 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String> {
770773
bail!("Python 2 is not supported");
771774
}
772775

773-
if env::var_os("CARGO_FEATURE_ABI3").is_some() {
776+
let minor = if env::var_os("CARGO_FEATURE_ABI3").is_some() {
774777
println!("cargo:rustc-cfg=Py_LIMITED_API");
775-
}
778+
// Check any `abi3-py3*` feature is set. If not, use the interpreter version.
779+
(PY3_MIN_MINOR..=ABI3_MAX_MINOR)
780+
.find_map(|i| env::var_os(format!("CARGO_FEATURE_ABI3_PY3{}", i)).map(|_| i))
781+
.or(interpreter_config.version.minor)
782+
} else {
783+
interpreter_config.version.minor
784+
};
776785

777-
if let Some(minor) = interpreter_config.version.minor {
778-
for i in 6..=minor {
786+
if let Some(minor) = minor {
787+
for i in PY3_MIN_MINOR..=minor {
779788
println!("cargo:rustc-cfg=Py_3_{}", i);
780789
flags += format!("CFG_Py_3_{},", i).as_ref();
781790
}

0 commit comments

Comments
 (0)