Skip to content

Commit 7aa183d

Browse files
committed
Use cast node instead of if-else cascade when checking the type in handleSystemExit
1 parent aef836c commit 7aa183d

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/TopLevelExceptionHandler.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import com.oracle.graal.python.builtins.objects.frame.PFrame;
5555
import com.oracle.graal.python.builtins.objects.function.PArguments;
5656
import com.oracle.graal.python.builtins.objects.function.PKeyword;
57-
import com.oracle.graal.python.builtins.objects.ints.PInt;
5857
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5958
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6059
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
@@ -64,6 +63,8 @@
6463
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
6564
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6665
import com.oracle.graal.python.nodes.statement.ExceptionHandlingStatementNode;
66+
import com.oracle.graal.python.nodes.util.CannotCastException;
67+
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
6768
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
6869
import com.oracle.graal.python.runtime.PythonContext;
6970
import com.oracle.graal.python.runtime.PythonCore;
@@ -235,18 +236,16 @@ private void handleSystemExit(VirtualFrame frame, PBaseException pythonException
235236
return;
236237
}
237238
Object attribute = pythonException.getAttribute("code");
238-
Integer exitcode = null;
239-
if (attribute instanceof Number) {
240-
exitcode = ((Number) attribute).intValue();
241-
} else if (attribute instanceof PInt) {
242-
exitcode = ((PInt) attribute).intValue();
243-
} else if (attribute instanceof PNone) {
244-
exitcode = 0; // "goto done" case in CPython
245-
} else if (attribute instanceof Boolean) {
246-
exitcode = ((boolean) attribute) ? 1 : 0;
247-
}
248-
if (exitcode != null) {
239+
try {
240+
int exitcode = 0;
241+
if (attribute != PNone.NONE) {
242+
// CPython checks if the object is subclass of PyLong and only then calls
243+
// PyLong_AsLong, so it always skips __index__/__int__
244+
exitcode = (int) CastToJavaLongLossyNode.getUncached().execute(attribute);
245+
}
249246
throw new PythonExitException(this, exitcode);
247+
} catch (CannotCastException e) {
248+
// fall through
250249
}
251250
if (theContext.getOption(PythonOptions.AlwaysRunExcepthook)) {
252251
// If we failed to dig out the exit code we just print and leave

0 commit comments

Comments
 (0)