Skip to content

Commit

Permalink
Support free threaded Python versions like '3.13t'
Browse files Browse the repository at this point in the history
Python wheels, pyenv, and a number of other tools use 't' in the Python
version number to identify free threaded builds. For example, '3.13t',
'3.14.0a1', '3.14t-dev'.

This PR supports that syntax in `actions/setup-python`, strips the "t",
and adds "-freethreading" to the architecture to select the correct
Python version.

See #771
  • Loading branch information
colesbury committed Oct 29, 2024
1 parent 0b93645 commit eb2a653
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
27 changes: 25 additions & 2 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91038,9 +91038,15 @@ function useCpythonVersion(version, architecture, updateEnvironment, checkLatest
return __awaiter(this, void 0, void 0, function* () {
var _a;
let manifest = null;
const desugaredVersionSpec = desugarDevVersion(version);
let semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec, allowPreReleases);
const [desugaredVersionSpec, freethreaded] = desugarFreeThreadedVersion(version);
const desugaredVersionSpec2 = desugarDevVersion(desugaredVersionSpec);
let semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec2, allowPreReleases);
core.debug(`Semantic version spec of ${version} is ${semanticVersionSpec}`);
if (freethreaded) {
// Free threaded versions use an architecture suffix like `x64-freethreaded`
core.debug(`Using freethreaded version of ${semanticVersionSpec}`);
architecture += freethreaded;
}
if (checkLatest) {
manifest = yield installer.getManifest();
const resolvedVersion = (_a = (yield installer.findReleaseFromManifest(semanticVersionSpec, architecture, manifest))) === null || _a === void 0 ? void 0 : _a.version;
Expand Down Expand Up @@ -91115,6 +91121,23 @@ function useCpythonVersion(version, architecture, updateEnvironment, checkLatest
});
}
exports.useCpythonVersion = useCpythonVersion;
/* Identify freethreaded versions like, 3.13t, 3.13t-dev, 3.14.0a1t. Returns
* the version without the `t` and the architectures suffix, if freethreaded */
function desugarFreeThreadedVersion(versionSpec) {
const prereleaseVersion = /(\d+\.\d+\.\d+)(t)((?:a|b|rc)\d*)/g;
if (prereleaseVersion.test(versionSpec)) {
return [versionSpec.replace(prereleaseVersion, '$1$3'), '-freethreaded'];
}
const majorMinor = /^(\d+\.\d+)(t)$/;
if (majorMinor.test(versionSpec)) {
return [versionSpec.replace(majorMinor, '$1'), '-freethreaded'];
}
const devVersion = /^(\d+\.\d+)(t)(-dev)$/;
if (devVersion.test(versionSpec)) {
return [versionSpec.replace(devVersion, '$1$3'), '-freethreaded'];
}
return [versionSpec, ''];
}
/** Convert versions like `3.8-dev` to a version like `~3.8.0-0`. */
function desugarDevVersion(versionSpec) {
const devVersion = /^(\d+)\.(\d+)-dev$/;
Expand Down
30 changes: 28 additions & 2 deletions src/find-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ export async function useCpythonVersion(
allowPreReleases: boolean
): Promise<InstalledVersion> {
let manifest: tc.IToolRelease[] | null = null;
const desugaredVersionSpec = desugarDevVersion(version);
const [desugaredVersionSpec, freethreaded] =
desugarFreeThreadedVersion(version);
const desugaredVersionSpec2 = desugarDevVersion(desugaredVersionSpec);
let semanticVersionSpec = pythonVersionToSemantic(
desugaredVersionSpec,
desugaredVersionSpec2,
allowPreReleases
);
core.debug(`Semantic version spec of ${version} is ${semanticVersionSpec}`);

if (freethreaded) {
// Free threaded versions use an architecture suffix like `x64-freethreaded`
core.debug(`Using freethreaded version of ${semanticVersionSpec}`);
architecture += freethreaded;
}

if (checkLatest) {
manifest = await installer.getManifest();
const resolvedVersion = (
Expand Down Expand Up @@ -159,6 +167,24 @@ export async function useCpythonVersion(
return {impl: 'CPython', version: installed};
}

/* Identify freethreaded versions like, 3.13t, 3.13t-dev, 3.14.0a1t. Returns
* the version without the `t` and the architectures suffix, if freethreaded */
function desugarFreeThreadedVersion(versionSpec: string) {
const prereleaseVersion = /(\d+\.\d+\.\d+)(t)((?:a|b|rc)\d*)/g;
if (prereleaseVersion.test(versionSpec)) {
return [versionSpec.replace(prereleaseVersion, '$1$3'), '-freethreaded'];
}
const majorMinor = /^(\d+\.\d+)(t)$/;
if (majorMinor.test(versionSpec)) {
return [versionSpec.replace(majorMinor, '$1'), '-freethreaded'];
}
const devVersion = /^(\d+\.\d+)(t)(-dev)$/;
if (devVersion.test(versionSpec)) {
return [versionSpec.replace(devVersion, '$1$3'), '-freethreaded'];
}
return [versionSpec, ''];
}

/** Convert versions like `3.8-dev` to a version like `~3.8.0-0`. */
function desugarDevVersion(versionSpec: string) {
const devVersion = /^(\d+)\.(\d+)-dev$/;
Expand Down

0 comments on commit eb2a653

Please sign in to comment.