Skip to content

Commit 6262cdc

Browse files
authored
Allow for runpy returning non-integer results. (#45)
* Allow for runpy returning non-integer results. * Correct error handling comment. * Rework exit path for SystemExit. * Ensure no extra output for a SystemExit with error string. * Handle the case of SystemExit()/SystemExit(None).
1 parent ddadae0 commit 6262cdc

File tree

1 file changed

+19
-16
lines changed
  • {{ cookiecutter.format }}/{{ cookiecutter.class_name }}

1 file changed

+19
-16
lines changed

{{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,34 +237,37 @@ int main(int argc, char *argv[]) {
237237
exit(-5);
238238
}
239239

240+
traceback_str = NULL;
240241
if (PyErr_GivenExceptionMatches(exc_value, PyExc_SystemExit)) {
241242
systemExit_code = PyObject_GetAttrString(exc_value, "code");
242243
if (systemExit_code == NULL) {
243-
NSLog(@"Could not determine exit code");
244+
traceback_str = @"Could not determine exit code";
244245
ret = -10;
245-
}
246-
else {
246+
} else if (systemExit_code == Py_None) {
247+
// SystemExit with a code of None; documented as a
248+
// return code of 0.
249+
ret = 0;
250+
} else if (PyLong_Check(systemExit_code)) {
251+
// SystemExit with error code
247252
ret = (int) PyLong_AsLong(systemExit_code);
253+
} else {
254+
// Any other SystemExit value - convert to a string, and
255+
// use the string as the traceback, and use the
256+
// documented SystemExit return value of 1.
257+
ret = 1;
258+
traceback_str = [NSString stringWithUTF8String:PyUnicode_AsUTF8(PyObject_Str(systemExit_code))];
248259
}
249260
} else {
250-
ret = -6;
251-
}
252-
253-
if (ret != 0) {
261+
// Non-SystemExit; likely an uncaught exception
262+
NSLog(@"---------------------------------------------------------------------------\n");
254263
NSLog(@"Application quit abnormally (Exit code %d)!", ret);
255-
264+
ret = -6;
256265
traceback_str = format_traceback(exc_type, exc_value, exc_traceback);
266+
}
257267

258-
// Restore the error state of the interpreter.
259-
PyErr_Restore(exc_type, exc_value, exc_traceback);
260-
261-
// Print exception to stderr.
262-
// In case of SystemExit, this will call exit()
263-
PyErr_Print();
264-
268+
if (traceback_str != NULL) {
265269
// Display stack trace in the crash dialog.
266270
crash_dialog(traceback_str);
267-
exit(ret);
268271
}
269272
} else {
270273
// In a normal iOS application, the following line is what

0 commit comments

Comments
 (0)