Skip to content

Commit

Permalink
Adds Pyodide version check to prevent incorrect bundle release.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Feb 26, 2025
1 parent 0154d8c commit 31efac8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/pyodide/internal/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { default as SetupEmscripten } from 'internal:setup-emscripten';
import { default as UnsafeEval } from 'internal:unsafe-eval';
import { simpleRunPython } from 'pyodide-internal:util';
import { loadPackages } from 'pyodide-internal:loadPackage';
import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata';

/**
* After running `instantiateEmscriptenModule` but before calling into any C
Expand Down Expand Up @@ -64,6 +65,28 @@ function prepareWasmLinearMemory(Module: Module): void {
adjustSysPath(Module);
}

/**
* Verifies that the Pyodide version in our compat flag matches our actual version. This is to
* prevent us accidentally releasing a Pyodide bundle built against a different version than one
* we expect.
*/
function validatePyodideVersion(Module: Module) {
const expectedPyodideVersion = MetadataReader.getPyodideVersion();
if (expectedPyodideVersion == 'dev') {
return;
}
try {
simpleRunPython(
Module,
`from pyodide import __version__ as v; assert v == \'${expectedPyodideVersion}\'; del v`
);
} catch {
throw new Error(
`Pyodide version mismatch, expected '${expectedPyodideVersion}'`
);
}
}

export async function loadPyodide(
isWorkerd: boolean,
lockfile: PackageLock,
Expand Down Expand Up @@ -103,6 +126,8 @@ export async function loadPyodide(

finishSnapshotSetup(pyodide);

validatePyodideVersion(Module);

// Need to set these here so that the logs go to the right context. If we don't they will go
// to SetupEmscripten's context and end up being KJ_LOG'd, which we do not want.
Module.API.initializeStreams(
Expand Down

0 comments on commit 31efac8

Please sign in to comment.