Skip to content

Commit 64cc4b9

Browse files
committed
Properly construct exception object from unknown type.
1 parent 8bc5a83 commit 64cc4b9

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BinasciiModuleBuiltins.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
4445
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4546

@@ -50,7 +51,6 @@
5051
import com.oracle.graal.python.builtins.Builtin;
5152
import com.oracle.graal.python.builtins.CoreFunctions;
5253
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
53-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
5454
import com.oracle.graal.python.builtins.PythonBuiltins;
5555
import com.oracle.graal.python.builtins.objects.PNone;
5656
import com.oracle.graal.python.builtins.objects.array.PArray;
@@ -66,10 +66,13 @@
6666
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
6767
import com.oracle.graal.python.nodes.ErrorMessages;
6868
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
69+
import com.oracle.graal.python.nodes.call.CallNode;
6970
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7071
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7172
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7273
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
74+
import com.oracle.graal.python.nodes.statement.RaiseNode;
75+
import com.oracle.graal.python.nodes.statement.RaiseNodeGen;
7376
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7477
import com.oracle.graal.python.runtime.PythonCore;
7578
import com.oracle.graal.python.runtime.exception.PException;
@@ -140,7 +143,9 @@ private byte[] b64decode(byte[] data) {
140143
@Builtin(name = "a2b_hex", minNumOfPositionalArgs = 2, declaresExplicitSelf = true)
141144
@GenerateNodeFactory
142145
abstract static class A2bHexNode extends PythonBinaryBuiltinNode {
143-
private ReadAttributeFromObjectNode readAttrNode;
146+
@Child private ReadAttributeFromObjectNode readAttrNode;
147+
@Child private RaiseNode raiseNode;
148+
@Child private CallNode callExceptionConstructor;
144149

145150
@Specialization
146151
@TruffleBoundary
@@ -204,11 +209,15 @@ private int digitValue(PythonModule self, char b) {
204209
}
205210

206211
private PException oddLengthError(PythonModule self) {
207-
throw getRaiseNode().execute(getAttrNode().execute(self, ERROR), PNone.NO_VALUE, ErrorMessages.ODD_LENGTH_STRING, new Object[0]);
212+
raiseObject(getAttrNode().execute(self, ERROR), ErrorMessages.ODD_LENGTH_STRING);
213+
CompilerDirectives.transferToInterpreterAndInvalidate();
214+
throw new IllegalStateException("should not be reached");
208215
}
209216

210217
private PException nonHexError(PythonModule self) {
211-
throw getRaiseNode().execute(getAttrNode().execute(self, ERROR), PNone.NO_VALUE, ErrorMessages.NON_HEX_DIGIT_FOUND, new Object[0]);
218+
raiseObject(getAttrNode().execute(self, ERROR), ErrorMessages.NON_HEX_DIGIT_FOUND);
219+
CompilerDirectives.transferToInterpreterAndInvalidate();
220+
throw new IllegalStateException("should not be reached");
212221
}
213222

214223
private ReadAttributeFromObjectNode getAttrNode() {
@@ -218,6 +227,18 @@ private ReadAttributeFromObjectNode getAttrNode() {
218227
}
219228
return readAttrNode;
220229
}
230+
231+
private void raiseObject(Object exceptionObject, String message) {
232+
if (callExceptionConstructor == null) {
233+
CompilerDirectives.transferToInterpreterAndInvalidate();
234+
callExceptionConstructor = insert(CallNode.create());
235+
}
236+
if (raiseNode == null) {
237+
CompilerDirectives.transferToInterpreterAndInvalidate();
238+
raiseNode = insert(RaiseNodeGen.create(null, null));
239+
}
240+
raiseNode.execute(callExceptionConstructor.execute(exceptionObject, message), PNone.NO_VALUE);
241+
}
221242
}
222243

223244
@Builtin(name = "b2a_base64", minNumOfPositionalArgs = 1, parameterNames = {"data", "newline"})

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/RaiseNode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
public abstract class RaiseNode extends StatementNode {
6060
private final BranchProfile baseCheckFailedProfile = BranchProfile.create();
6161

62+
public abstract void execute(VirtualFrame frame, Object typeOrExceptionObject, Object cause);
63+
64+
public final void execute(Object typeOrExceptionObject, Object cause) {
65+
execute(null, typeOrExceptionObject, cause);
66+
}
67+
6268
@ImportStatic(PGuards.class)
6369
public abstract static class SetExceptionCauseNode extends Node {
6470
public abstract void execute(VirtualFrame frame, PBaseException exception, Object cause);

0 commit comments

Comments
 (0)