Skip to content

Commit dff4df6

Browse files
committed
handle zero division in divmod
1 parent f560e40 commit dff4df6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,16 @@ public Object dir(Object object,
329329
@TypeSystemReference(PythonArithmeticTypes.class)
330330
@GenerateNodeFactory
331331
public abstract static class DivModNode extends PythonBuiltinNode {
332-
333-
@Specialization
334-
public PTuple doInt(int a, int b) {
332+
@Specialization(guards = "b != 0")
333+
public PTuple doLong(long a, long b) {
335334
return factory().createTuple(new Object[]{Math.floorDiv(a, b), Math.floorMod(a, b)});
336335
}
337336

338-
@Specialization
339-
public PTuple doInt(long a, long b) {
337+
@Specialization(replaces = "doLong")
338+
public PTuple doLongZero(long a, long b) {
339+
if (b == 0) {
340+
throw raise(PythonErrorType.ZeroDivisionError, "ZeroDivisionError: integer division or modulo by zero");
341+
}
340342
return factory().createTuple(new Object[]{Math.floorDiv(a, b), Math.floorMod(a, b)});
341343
}
342344

0 commit comments

Comments
 (0)