78
78
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PZipImporter )
79
79
public class ZipImporterBuiltins extends PythonBuiltins {
80
80
81
- /**
82
- * cache of the interesting files in the zipfile. exported in ZipimportModuleBuiltins
83
- */
84
- private static PDict zip_directory_cache ;
81
+ private static final String INIT_WAS_NOT_CALLED = "zipimporter.__init__() wasn't called" ;
85
82
86
83
@ Override
87
84
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
88
85
return ZipImporterBuiltinsFactory .getFactories ();
89
86
}
90
87
91
- public static PDict getCache ( PythonObjectFactory factory ) {
92
- if (zip_directory_cache == null ) {
93
- zip_directory_cache = factory . createDict ();
88
+ private static void checkInitCalled ( PZipImporter self ) {
89
+ if (self . getPrefix () == null ) {
90
+
94
91
}
95
- return zip_directory_cache ;
96
92
}
97
93
98
94
@ Builtin (name = __INIT__ , fixedNumOfPositionalArgs = 1 )
@@ -144,7 +140,7 @@ private void initZipImporter(PZipImporter self, String path) {
144
140
if (zipFile == null ) {
145
141
throw raise (PythonErrorType .ZipImportError , "not a Zip file" );
146
142
}
147
- Object files = zip_directory_cache .getItem (path );
143
+ Object files = self . getZipDirectoryCache () .getItem (path );
148
144
if (files == null ) {
149
145
PDict filesDict = factory ().createDict ();
150
146
Enumeration <? extends ZipEntry > entries = zipFile .entries ();
@@ -163,7 +159,7 @@ private void initZipImporter(PZipImporter self, String path) {
163
159
filesDict .setItem (entry .getName (), tuple );
164
160
}
165
161
files = filesDict ;
166
- zip_directory_cache .setItem (path , files );
162
+ self . getZipDirectoryCache () .setItem (path , files );
167
163
}
168
164
self .setArchive (archive );
169
165
self .setPrefix (prefix );
@@ -263,7 +259,11 @@ public abstract static class FindModuleNode extends PythonTernaryBuiltinNode {
263
259
* @return the zipimporter or none, if the module is not found
264
260
*/
265
261
@ Specialization
266
- public Object doit (PZipImporter self , String fullname , @ SuppressWarnings ("unused" ) Object path ) {
262
+ public Object doit (PZipImporter self , String fullname , @ SuppressWarnings ("unused" ) Object path ,
263
+ @ Cached ("createBinaryProfile()" ) ConditionProfile initWasNotCalled ) {
264
+ if (initWasNotCalled .profile (self .getPrefix () == null )) {
265
+ throw raise (PythonErrorType .ValueError , INIT_WAS_NOT_CALLED );
266
+ }
267
267
return self .findModule (fullname ) == null ? PNone .NONE : self ;
268
268
}
269
269
@@ -278,7 +278,11 @@ public abstract static class GetCodeNode extends PythonBinaryBuiltinNode {
278
278
279
279
@ Specialization
280
280
public PCode doit (PZipImporter self , String fullname ,
281
- @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ) {
281
+ @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ,
282
+ @ Cached ("createBinaryProfile()" ) ConditionProfile initWasNotCalled ) {
283
+ if (initWasNotCalled .profile (self .getPrefix () == null )) {
284
+ throw raise (PythonErrorType .ValueError , INIT_WAS_NOT_CALLED );
285
+ }
282
286
if (compileNode == null ) {
283
287
CompilerDirectives .transferToInterpreterAndInvalidate ();
284
288
compileNode = insert (CompileNode .create ());
@@ -304,6 +308,9 @@ public abstract static class GetDataNode extends PythonBinaryBuiltinNode {
304
308
@ Specialization
305
309
@ CompilerDirectives .TruffleBoundary
306
310
public PBytes doit (PZipImporter self , String pathname ) {
311
+ if (self .getPrefix () == null ) {
312
+ throw raise (PythonErrorType .ValueError , INIT_WAS_NOT_CALLED );
313
+ }
307
314
String archive = self .getArchive ();
308
315
int len = archive .length ();
309
316
int index = 0 ;
@@ -344,7 +351,11 @@ public abstract static class GetFileNameNode extends PythonBinaryBuiltinNode {
344
351
345
352
@ Specialization
346
353
public Object doit (PZipImporter self , String fullname ,
347
- @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ) {
354
+ @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ,
355
+ @ Cached ("createBinaryProfile()" ) ConditionProfile initWasNotCalled ) {
356
+ if (initWasNotCalled .profile (self .getPrefix () == null )) {
357
+ throw raise (PythonErrorType .ValueError , INIT_WAS_NOT_CALLED );
358
+ }
348
359
ModuleCodeData moduleCodeData = self .getModuleCode (fullname );
349
360
if (canNotFind .profile (moduleCodeData == null )) {
350
361
throw raise (PythonErrorType .ZipImportError , " can't find module '%s'" , fullname );
@@ -361,7 +372,11 @@ public abstract static class GetSourceNode extends PythonBinaryBuiltinNode {
361
372
362
373
@ Specialization
363
374
public String doit (PZipImporter self , String fullname ,
364
- @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ) {
375
+ @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ,
376
+ @ Cached ("createBinaryProfile()" ) ConditionProfile initWasNotCalled ) {
377
+ if (initWasNotCalled .profile (self .getPrefix () == null )) {
378
+ throw raise (PythonErrorType .ValueError , INIT_WAS_NOT_CALLED );
379
+ }
365
380
ModuleCodeData md = self .getModuleCode (fullname );
366
381
if (canNotFind .profile (md == null )) {
367
382
throw raise (PythonErrorType .ZipImportError , "can't find module '%s'" , fullname );
@@ -378,7 +393,11 @@ public abstract static class IsPackageNode extends PythonBinaryBuiltinNode {
378
393
379
394
@ Specialization
380
395
public boolean doit (PZipImporter self , String fullname ,
381
- @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ) {
396
+ @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ,
397
+ @ Cached ("createBinaryProfile()" ) ConditionProfile initWasNotCalled ) {
398
+ if (initWasNotCalled .profile (self .getPrefix () == null )) {
399
+ throw raise (PythonErrorType .ValueError , INIT_WAS_NOT_CALLED );
400
+ }
382
401
ModuleInfo moduleInfo = self .getModuleInfo (fullname );
383
402
if (canNotFind .profile (moduleInfo == ModuleInfo .NOT_FOUND )) {
384
403
throw raise (PythonErrorType .ZipImportError , "can't find module '%s'" , fullname );
@@ -396,7 +415,9 @@ public abstract static class LoadModuleNode extends PythonBinaryBuiltinNode {
396
415
@ Specialization
397
416
public Object doit (PZipImporter self , String fullname ,
398
417
@ Cached ("create()" ) GetCodeNode getCodeNode ,
399
- @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ) {
418
+ @ Cached ("createBinaryProfile()" ) ConditionProfile canNotFind ,
419
+ @ Cached ("createBinaryProfile()" ) ConditionProfile initWasNotCalled ) {
420
+ PCode code = getCodeNode .doit (self , fullname , canNotFind , initWasNotCalled );
400
421
401
422
PythonModule sysModule = getCore ().lookupBuiltinModule ("sys" );
402
423
PDict sysModules = (PDict ) sysModule .getAttribute ("modules" );
@@ -405,7 +426,7 @@ public Object doit(PZipImporter self, String fullname,
405
426
module = getCore ().factory ().createPythonModule (fullname );
406
427
sysModules .setItem (fullname , module );
407
428
}
408
- PCode code = getCodeNode . doit ( self , fullname , canNotFind );
429
+
409
430
module .setAttribute (SpecialAttributeNames .__LOADER__ , self );
410
431
module .setAttribute (SpecialAttributeNames .__FILE__ , code .getFilename ());
411
432
if (ModuleInfo .PACKAGE == self .getModuleInfo (fullname )) {
0 commit comments