Skip to content

Commit b94755c

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

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-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

+20-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_MIN_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,24 @@ 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() {
774-
println!("cargo:rustc-cfg=Py_LIMITED_API");
776+
fn get_abi3_min_python() -> Option<u8> {
777+
for i in PY3_MIN_MINOR..=ABI3_MAX_MIN_MINOR {
778+
if env::var_os(format!("CARGO_FEATURE_ABI{}", i)).is_some() {
779+
return Some(i);
780+
}
781+
}
782+
None
775783
}
776784

777-
if let Some(minor) = interpreter_config.version.minor {
778-
for i in 6..=minor {
785+
let minor = if env::var_os("CARGO_FEATURE_ABI3").is_some() {
786+
println!("cargo:rustc-cfg=Py_LIMITED_API");
787+
get_abi3_min_python().or(interpreter_config.version.minor)
788+
} else {
789+
interpreter_config.version.minor
790+
};
791+
792+
if let Some(minor) = minor {
793+
for i in PY3_MIN_MINOR..=minor {
779794
println!("cargo:rustc-cfg=Py_3_{}", i);
780795
flags += format!("CFG_Py_3_{},", i).as_ref();
781796
}

0 commit comments

Comments
 (0)