Skip to content

Commit 533dbb9

Browse files
committed
Handling situation, when no zip file is entered to zipimporter.
1 parent 7ab8cff commit 533dbb9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ void findFirstEntryPosition() throws IOException {
170170
while (positions.isEmpty() && read() != -1) {
171171
// do nothing here, just read until the first LOC is found
172172
}
173-
pos -= 4;
174-
readFirstLoc = true;
173+
if (!positions.isEmpty()) {
174+
pos -= 4;
175+
readFirstLoc = true;
176+
}
175177
}
176178
}
177179

@@ -221,6 +223,10 @@ private void initZipImporter(PZipImporter self, String path) {
221223
try {
222224
locis = new LOCZipEntryStream(tfile.newInputStream(StandardOpenOption.READ));
223225
locis.findFirstEntryPosition(); // find location of the first zip entry
226+
if (locis.positions.isEmpty()) {
227+
// no PK\003\004 found -> not a correct zip file
228+
throw raise(PythonErrorType.ZipImportError, "not a Zip file: '%s'", archive);
229+
}
224230
zis = new ZipInputStream(locis); // and create new ZipInput stream from this
225231
// location
226232
ZipEntry entry;
@@ -262,7 +268,7 @@ private void initZipImporter(PZipImporter self, String path) {
262268
}
263269
}
264270
} catch (IOException ex) {
265-
throw raise(PythonErrorType.ZipImportError, "not a Zip file");
271+
throw raise(PythonErrorType.ZipImportError, "not a Zip file: '%s'", archive);
266272
} finally {
267273
if (zis != null) {
268274
try {
@@ -288,7 +294,7 @@ private void initZipImporter(PZipImporter self, String path) {
288294
self.setFiles((PDict) files);
289295

290296
} else {
291-
throw raise(PythonErrorType.ZipImportError, "not a Zip file");
297+
throw raise(PythonErrorType.ZipImportError, "not a Zip file: '%s'", archive);
292298
}
293299

294300
}

0 commit comments

Comments
 (0)