Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail at upload time when module modifies sys.path #3409

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/pyodide/internal/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
MEMORY_SNAPSHOT_READER,
REQUIREMENTS,
} from 'pyodide-internal:metadata';
import { reportError, simpleRunPython } from 'pyodide-internal:util';
import {
reportError,
simpleRunPython,
SimpleRunPythonError,
} from 'pyodide-internal:util';
import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata';

let LOADED_BASELINE_SNAPSHOT: number;
Expand Down Expand Up @@ -262,8 +266,22 @@ function memorySnapshotDoImports(Module: Module): string[] {
const deduplicatedModules = [...new Set(importedModules)];

// Import the modules list so they are included in the snapshot.
if (deduplicatedModules.length > 0) {
simpleRunPython(Module, 'import ' + deduplicatedModules.join(','));
for (const mod of deduplicatedModules) {
try {
// If the module manipulates sys.path or sys.meta_path, we hopefully won't be able to import
// some of the things it imports. If they import a package that attempts to import from js,
// then this might leave the package in an unusable state. TODO: finalizeBootstrap before
// taking the snapshot.
simpleRunPython(Module, `import ${mod}`);
} catch (e) {
if (!(e instanceof SimpleRunPythonError)) {
// This probably means we segfaulted.
throw e;
}
continue;
}
// Delete the imported module to avoid polluting the namespace
simpleRunPython(Module, `del ${mod.split('.', 1)[0]}`);
}

return deduplicatedModules;
Expand Down
36 changes: 28 additions & 8 deletions src/pyodide/internal/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
export function reportError(e: any): never {
e.stack?.split('\n').forEach((s: any) => console.warn(s));
if (e instanceof SimpleRunPythonError) {
// PyRun_SimpleString will have written a Python traceback to stderr.
console.warn('Command failed:', e.pyCode);
console.warn('Error was:');
for (const line of e.pyTraceback.split('\n')) {
console.warn(line);
}
} else {
e.stack?.split('\n').forEach((s: any) => console.warn(s));
}
throw e;
}

function setErrorName(errClass: any) {
Object.defineProperty(errClass.prototype, 'name', {
value: errClass.name,
});
}

export class SimpleRunPythonError extends Error {
pyCode: string;
pyTraceback: string;
constructor(pyCode: string, pyTraceback: string) {
super('Failed to run Python code: ' + pyTraceback);
this.pyCode = pyCode;
this.pyTraceback = pyTraceback;
}
}
setErrorName(SimpleRunPythonError);

/**
* Simple as possible runPython function which works with no foreign function
* interface. We need to use this rather than the normal easier to use
Expand Down Expand Up @@ -36,13 +62,7 @@ export function simpleRunPython(
// status 0: Ok
// status -1: Error
if (status) {
// PyRun_SimpleString will have written a Python traceback to stderr.
console.warn('Command failed:', code);
console.warn('Error was:');
for (const line of err.split('\n')) {
console.warn(line);
}
throw new Error('Failed to run Python code: ' + err);
throw new SimpleRunPythonError(code, err);
}
return err;
}
Loading