@@ -13,7 +13,11 @@ import {
13
13
MEMORY_SNAPSHOT_READER ,
14
14
REQUIREMENTS ,
15
15
} from 'pyodide-internal:metadata' ;
16
- import { reportError , simpleRunPython } from 'pyodide-internal:util' ;
16
+ import {
17
+ reportError ,
18
+ simpleRunPython ,
19
+ SimpleRunPythonError ,
20
+ } from 'pyodide-internal:util' ;
17
21
import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata' ;
18
22
19
23
let LOADED_BASELINE_SNAPSHOT : number ;
@@ -262,8 +266,22 @@ function memorySnapshotDoImports(Module: Module): string[] {
262
266
const deduplicatedModules = [ ...new Set ( importedModules ) ] ;
263
267
264
268
// Import the modules list so they are included in the snapshot.
265
- if ( deduplicatedModules . length > 0 ) {
266
- simpleRunPython ( Module , 'import ' + deduplicatedModules . join ( ',' ) ) ;
269
+ for ( const mod of deduplicatedModules ) {
270
+ try {
271
+ // If the module manipulates sys.path or sys.meta_path, we hopefully won't be able to import
272
+ // some of the things it imports. If they import a package that attempts to import from js,
273
+ // then this might leave the package in an unusable state. TODO: finalizeBootstrap before
274
+ // taking the snapshot.
275
+ simpleRunPython ( Module , `import ${ mod } ` ) ;
276
+ } catch ( e ) {
277
+ if ( ! ( e instanceof SimpleRunPythonError ) ) {
278
+ // This probably means we segfaulted.
279
+ throw e ;
280
+ }
281
+ continue ;
282
+ }
283
+ // Delete the imported module to avoid polluting the namespace
284
+ simpleRunPython ( Module , `del ${ mod . split ( '.' , 1 ) [ 0 ] } ` ) ;
267
285
}
268
286
269
287
return deduplicatedModules ;
0 commit comments