Skip to content

Commit 1ad7a3d

Browse files
committed
allow raising exceptions with causes more easily
1 parent 94d1d99 commit 1ad7a3d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ public Object next(Object iterator, PNone defaultObject,
12781278
return next.execute(iterator);
12791279
} catch (PException e) {
12801280
e.expectAttributeError(errorProfile);
1281-
throw raise(TypeError, "'%p' object is not an iterator", iterator);
1281+
throw raise(TypeError, e.getExceptionObject(), "'%p' object is not an iterator", iterator);
12821282
}
12831283
}
12841284

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PNodeWithContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
4646
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4747
import com.oracle.graal.python.builtins.objects.type.PythonClass;
48+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
4849
import com.oracle.graal.python.runtime.PythonContext;
4950
import com.oracle.graal.python.runtime.PythonCore;
5051
import com.oracle.graal.python.runtime.exception.PException;
@@ -62,6 +63,7 @@
6263

6364
public abstract class PNodeWithContext extends Node {
6465
@Child private PythonObjectFactory factory;
66+
@Child private WriteAttributeToObjectNode writeCause;
6567
@CompilationFinal private ContextReference<PythonContext> contextRef;
6668

6769
protected final PythonObjectFactory factory() {
@@ -88,6 +90,17 @@ public PException raise(LazyPythonClass exceptionType) {
8890
throw raise(factory().createBaseException(exceptionType));
8991
}
9092

93+
public final PException raise(PythonBuiltinClassType type, PBaseException cause, String format, Object... arguments) {
94+
assert format != null;
95+
PBaseException baseException = factory().createBaseException(type, format, arguments);
96+
if (writeCause == null) {
97+
CompilerDirectives.transferToInterpreterAndInvalidate();
98+
writeCause = insert(WriteAttributeToObjectNode.create());
99+
}
100+
writeCause.execute(baseException, SpecialAttributeNames.__CAUSE__, cause);
101+
throw raise(baseException);
102+
}
103+
91104
public final PException raise(PythonBuiltinClassType type, String format, Object... arguments) {
92105
assert format != null;
93106
throw raise(factory().createBaseException(type, format, arguments));

0 commit comments

Comments
 (0)