Skip to content

Commit 372a51f

Browse files
committed
Fix: do not throw RuntimeException on I/O error.
1 parent 5754453 commit 372a51f

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.zipimporter;
2727

28-
import com.oracle.graal.python.builtins.objects.dict.PDict;
29-
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
30-
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
31-
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
32-
import com.oracle.truffle.api.CompilerDirectives;
33-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
34-
3528
import java.io.BufferedReader;
3629
import java.io.File;
3730
import java.io.IOException;
@@ -41,6 +34,13 @@
4134
import java.util.zip.ZipEntry;
4235
import java.util.zip.ZipFile;
4336

37+
import com.oracle.graal.python.builtins.objects.dict.PDict;
38+
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
39+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
40+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
41+
import com.oracle.truffle.api.CompilerDirectives;
42+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
43+
4444
public class PZipImporter extends PythonBuiltinObject {
4545

4646
public static final String SEPARATOR = File.separator;
@@ -197,9 +197,10 @@ protected String makePackagePath(String fullname) {
197197
*
198198
* @param filenameAndSuffix
199199
* @return code
200+
* @throws IOException
200201
*/
201202
@CompilerDirectives.TruffleBoundary
202-
private String getCode(String filenameAndSuffix) {
203+
private String getCode(String filenameAndSuffix) throws IOException {
203204
ZipFile zip = null;
204205
try {
205206
zip = new ZipFile(archive);
@@ -221,7 +222,7 @@ private String getCode(String filenameAndSuffix) {
221222
reader.close();
222223
return code.toString();
223224
} catch (IOException e) {
224-
throw new RuntimeException("Can not read code from " + makePackagePath(filenameAndSuffix), e);
225+
throw new IOException("Can not read code from " + makePackagePath(filenameAndSuffix), e);
225226
} finally {
226227
if (zip != null) {
227228
try {
@@ -271,7 +272,7 @@ protected final PZipImporter findModule(String fullname) {
271272
}
272273

273274
@TruffleBoundary
274-
protected final ModuleCodeData getModuleCode(String fullname) {
275+
protected final ModuleCodeData getModuleCode(String fullname) throws IOException {
275276
String path = makeFilename(fullname);
276277
String fullPath = makePackagePath(fullname);
277278

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.builtins.objects.code.PCode;
5151
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5252
import com.oracle.graal.python.builtins.objects.dict.PDict;
53+
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
5354
import com.oracle.graal.python.builtins.objects.function.PArguments;
5455
import com.oracle.graal.python.builtins.objects.list.PList;
5556
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -445,7 +446,12 @@ public PCode doit(VirtualFrame frame, PZipImporter self, String fullname,
445446
CompilerDirectives.transferToInterpreterAndInvalidate();
446447
compileNode = insert(CompileNode.create(false));
447448
}
448-
ModuleCodeData md = self.getModuleCode(fullname);
449+
ModuleCodeData md;
450+
try {
451+
md = self.getModuleCode(fullname);
452+
} catch (IOException e) {
453+
throw raiseOSError(frame, OSErrorEnum.EIO, e);
454+
}
449455
if (canNotFind.profile(md == null)) {
450456
throw raise(PythonErrorType.ZipImportError, " can't find module '%s'", fullname);
451457
}
@@ -529,13 +535,18 @@ public PBytes doit(PZipImporter self, String pathname) {
529535
public abstract static class GetFileNameNode extends PythonBinaryBuiltinNode {
530536

531537
@Specialization
532-
public Object doit(PZipImporter self, String fullname,
538+
public Object doit(VirtualFrame frame, PZipImporter self, String fullname,
533539
@Cached("createBinaryProfile()") ConditionProfile canNotFind,
534540
@Cached("createBinaryProfile()") ConditionProfile initWasNotCalled) {
535541
if (initWasNotCalled.profile(self.getPrefix() == null)) {
536542
throw raise(PythonErrorType.ValueError, INIT_WAS_NOT_CALLED);
537543
}
538-
ModuleCodeData moduleCodeData = self.getModuleCode(fullname);
544+
ModuleCodeData moduleCodeData;
545+
try {
546+
moduleCodeData = self.getModuleCode(fullname);
547+
} catch (IOException e) {
548+
throw raiseOSError(frame, OSErrorEnum.EIO, e);
549+
}
539550
if (canNotFind.profile(moduleCodeData == null)) {
540551
throw raise(PythonErrorType.ZipImportError, " can't find module '%s'", fullname);
541552
}
@@ -550,13 +561,18 @@ public Object doit(PZipImporter self, String fullname,
550561
public abstract static class GetSourceNode extends PythonBinaryBuiltinNode {
551562

552563
@Specialization
553-
public String doit(PZipImporter self, String fullname,
564+
public String doit(VirtualFrame frame, PZipImporter self, String fullname,
554565
@Cached("createBinaryProfile()") ConditionProfile canNotFind,
555566
@Cached("createBinaryProfile()") ConditionProfile initWasNotCalled) {
556567
if (initWasNotCalled.profile(self.getPrefix() == null)) {
557568
throw raise(PythonErrorType.ValueError, INIT_WAS_NOT_CALLED);
558569
}
559-
ModuleCodeData md = self.getModuleCode(fullname);
570+
ModuleCodeData md;
571+
try {
572+
md = self.getModuleCode(fullname);
573+
} catch (IOException e) {
574+
throw raiseOSError(frame, OSErrorEnum.EIO, e);
575+
}
560576
if (canNotFind.profile(md == null)) {
561577
throw raise(PythonErrorType.ZipImportError, "can't find module '%s'", fullname);
562578
}

0 commit comments

Comments
 (0)