|
54 | 54 | import com.oracle.graal.python.builtins.objects.frame.PFrame;
|
55 | 55 | import com.oracle.graal.python.builtins.objects.function.PArguments;
|
56 | 56 | import com.oracle.graal.python.builtins.objects.function.PKeyword;
|
57 |
| -import com.oracle.graal.python.builtins.objects.ints.PInt; |
58 | 57 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
59 | 58 | import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
|
60 | 59 | import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
|
|
64 | 63 | import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
|
65 | 64 | import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
|
66 | 65 | 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; |
67 | 68 | import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
|
68 | 69 | import com.oracle.graal.python.runtime.PythonContext;
|
69 | 70 | import com.oracle.graal.python.runtime.PythonCore;
|
@@ -235,18 +236,16 @@ private void handleSystemExit(VirtualFrame frame, PBaseException pythonException
|
235 | 236 | return;
|
236 | 237 | }
|
237 | 238 | 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 | + } |
249 | 246 | throw new PythonExitException(this, exitcode);
|
| 247 | + } catch (CannotCastException e) { |
| 248 | + // fall through |
250 | 249 | }
|
251 | 250 | if (theContext.getOption(PythonOptions.AlwaysRunExcepthook)) {
|
252 | 251 | // If we failed to dig out the exit code we just print and leave
|
|
0 commit comments