@@ -29,6 +29,7 @@ import { default as SetupEmscripten } from 'internal:setup-emscripten';
2929import { default as UnsafeEval } from 'internal:unsafe-eval' ;
3030import { simpleRunPython } from 'pyodide-internal:util' ;
3131import { loadPackages } from 'pyodide-internal:loadPackage' ;
32+ import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata' ;
3233
3334/**
3435 * After running `instantiateEmscriptenModule` but before calling into any C
@@ -64,6 +65,28 @@ function prepareWasmLinearMemory(Module: Module): void {
6465 adjustSysPath ( Module ) ;
6566}
6667
68+ /**
69+ * Verifies that the Pyodide version in our compat flag matches our actual version. This is to
70+ * prevent us accidentally releasing a Pyodide bundle built against a different version than one
71+ * we expect.
72+ */
73+ function validatePyodideVersion ( Module : Module ) {
74+ const expectedPyodideVersion = MetadataReader . getPyodideVersion ( ) ;
75+ if ( expectedPyodideVersion == 'dev' ) {
76+ return ;
77+ }
78+ try {
79+ simpleRunPython (
80+ Module ,
81+ `from pyodide import __version__ as v; assert v == \'${ expectedPyodideVersion } \'; del v`
82+ ) ;
83+ } catch {
84+ throw new Error (
85+ `Pyodide version mismatch, expected '${ expectedPyodideVersion } '`
86+ ) ;
87+ }
88+ }
89+
6790export async function loadPyodide (
6891 isWorkerd : boolean ,
6992 lockfile : PackageLock ,
@@ -103,6 +126,8 @@ export async function loadPyodide(
103126
104127 finishSnapshotSetup ( pyodide ) ;
105128
129+ validatePyodideVersion ( Module ) ;
130+
106131 // Need to set these here so that the logs go to the right context. If we don't they will go
107132 // to SetupEmscripten's context and end up being KJ_LOG'd, which we do not want.
108133 Module . API . initializeStreams (
0 commit comments