Skip to content

Commit 17802e9

Browse files
authored
Allow for runpy returning non-integer results. (#65)
* Allow for runpy returning non-integer results. * Rework exit path for SystemExit. * Ensure no extra output for a SystemExit with error string. * Handle the case of SystemExit()/SystemExit(None).
1 parent d5f7911 commit 17802e9

File tree

1 file changed

+18
-5
lines changed
  • {{ cookiecutter.format }}/{{ cookiecutter.class_name }}

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,36 @@ int main(int argc, char *argv[]) {
247247
exit(-5);
248248
}
249249

250+
traceback_str = NULL;
250251
if (PyErr_GivenExceptionMatches(exc_value, PyExc_SystemExit)) {
251252
systemExit_code = PyObject_GetAttrString(exc_value, "code");
252253
if (systemExit_code == NULL) {
253-
debug_log(@"Could not determine exit code");
254+
traceback_str = @"Could not determine exit code";
254255
ret = -10;
255-
}
256-
else {
256+
} else if (systemExit_code == Py_None) {
257+
// SystemExit with a code of None; documented as a
258+
// return code of 0.
259+
ret = 0;
260+
} else if (PyLong_Check(systemExit_code)) {
261+
// SystemExit with error code
257262
ret = (int) PyLong_AsLong(systemExit_code);
263+
} else {
264+
// Any other SystemExit value - convert to a string, and
265+
// use the string as the traceback, and use the
266+
// documented SystemExit return value of 1.
267+
ret = 1;
268+
traceback_str = [NSString stringWithUTF8String:PyUnicode_AsUTF8(PyObject_Str(systemExit_code))];
258269
}
259270
} else {
260271
// Non-SystemExit; likely an uncaught exception
261-
ret = -6;
262272
info_log(@"---------------------------------------------------------------------------");
263273
info_log(@"Application quit abnormally!");
274+
ret = -6;
275+
traceback_str = format_traceback(exc_type, exc_value, exc_traceback);
276+
}
264277

278+
if (traceback_str != NULL) {
265279
// Display stack trace in the crash dialog.
266-
traceback_str = format_traceback(exc_type, exc_value, exc_traceback);
267280
crash_dialog(traceback_str);
268281
}
269282
}

0 commit comments

Comments
 (0)