Skip to content

Commit

Permalink
Add free-threaded Python support
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Nov 17, 2024
1 parent 0d5e013 commit 103c93d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 18 deletions.
30 changes: 22 additions & 8 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,26 +1244,35 @@ fn find_interpreter_in_sysconfig(
let mut interpreters = Vec::new();
for interp in interpreter {
let python = interp.display().to_string();
let (python_impl, python_ver) = if let Some(ver) = python.strip_prefix("pypy") {
(InterpreterKind::PyPy, ver.strip_prefix('-').unwrap_or(ver))
} else if let Some(ver) = python.strip_prefix("graalpy") {
let (python_impl, python_ver, abiflags) = if let Some(ver) = python.strip_prefix("pypy") {
(
InterpreterKind::GraalPy,
InterpreterKind::PyPy,
ver.strip_prefix('-').unwrap_or(ver),
"",
)
} else if let Some(ver) = python.strip_prefix("python") {
} else if let Some(ver) = python.strip_prefix("graalpy") {
(
InterpreterKind::CPython,
InterpreterKind::GraalPy,
ver.strip_prefix('-').unwrap_or(ver),
"",
)
} else if let Some(ver) = python.strip_prefix("python") {
// Also accept things like `python3.13t` for free-threaded python
let (ver, abiflags) =
if let Some(ver) = ver.strip_prefix('-').unwrap_or(ver).strip_suffix('t') {
(ver, "t")
} else {
(ver, "")
};
(InterpreterKind::CPython, ver, abiflags)
} else if python
.chars()
.next()
.map(|c| c.is_ascii_digit())
.unwrap_or(false)
{
// Eg: -i 3.9 without interpreter kind, assume it's CPython
(InterpreterKind::CPython, &*python)
(InterpreterKind::CPython, &*python, "")
} else {
// if interpreter not known
if std::path::Path::new(&python).is_file() {
Expand All @@ -1284,7 +1293,12 @@ fn find_interpreter_in_sysconfig(
let ver_minor = ver_minor.parse::<usize>().with_context(|| {
format!("Invalid python interpreter minor version '{ver_minor}', expect a digit")
})?;
let sysconfig = InterpreterConfig::lookup_one(target, python_impl, (ver_major, ver_minor))

if (ver_major, ver_minor) < (3, 13) && abiflags == "t" {
bail!("Free-threaded Python interpreter is only supported on 3.13 and later.");
}

let sysconfig = InterpreterConfig::lookup_one(target, python_impl, (ver_major, ver_minor), abiflags)
.with_context(|| {
format!("Failed to find a {python_impl} {ver_major}.{ver_minor} interpreter in known sysconfig")
})?;
Expand Down
Loading

0 comments on commit 103c93d

Please sign in to comment.