Skip to content

Commit 9decb96

Browse files
rnixxrtibbles
authored andcommitted
Allow skipping sys.exit()
Adding python code to call `sys.exit` was added to workaround issues with restarting apps. However, that actually exits the entire application. In some scenarios that might be wrong, so provide a conditional to skip calling `sys.exit`.
1 parent 62c1de4 commit 9decb96

File tree

1 file changed

+22
-19
lines changed
  • pythonforandroid/bootstraps/common/build/jni/application/src

1 file changed

+22
-19
lines changed

pythonforandroid/bootstraps/common/build/jni/application/src/start.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdio.h>
99
#include <stdlib.h>
10+
#include <stdbool.h>
1011
#include <unistd.h>
1112
#include <dirent.h>
1213
#include <jni.h>
@@ -70,7 +71,7 @@ static int file_exists(const char *filename) {
7071
return 0;
7172
}
7273

73-
static int run_python(int argc, char *argv[]) {
74+
static int run_python(int argc, char *argv[], bool call_exit) {
7475

7576
char *env_argument = NULL;
7677
char *env_entrypoint = NULL;
@@ -324,24 +325,24 @@ static int run_python(int argc, char *argv[]) {
324325
325326
https://github.com/kivy/kivy/pull/6107#issue-246120816
326327
*/
327-
char terminatecmd[256];
328-
snprintf(
329-
terminatecmd, sizeof(terminatecmd),
330-
"import sys; sys.exit(%d)\n", ret
331-
);
332-
PyRun_SimpleString(terminatecmd);
333-
334-
/* This should never actually be reached, but we'll leave the clean-up
335-
* here just to be safe.
328+
if (call_exit) {
329+
char terminatecmd[256];
330+
snprintf(
331+
terminatecmd, sizeof(terminatecmd),
332+
"import sys; sys.exit(%d)\n", ret
333+
);
334+
PyRun_SimpleString(terminatecmd);
335+
}
336+
337+
/* This should never actually be reached with call_exit.
336338
*/
339+
if (call_exit)
340+
LOGP("Unexpectedly reached python finalization");
337341
#if PY_MAJOR_VERSION < 3
338342
Py_Finalize();
339-
LOGP("Unexpectedly reached Py_FinalizeEx(), but was successful.");
340343
#else
341344
if (Py_FinalizeEx() != 0) // properly check success on Python 3
342-
LOGP("Unexpectedly reached Py_FinalizeEx(), and got error!");
343-
else
344-
LOGP("Unexpectedly reached Py_FinalizeEx(), but was successful.");
345+
LOGP("Py_FinalizeEx() returned an error!");
345346
#endif
346347

347348
return ret;
@@ -350,7 +351,7 @@ static int run_python(int argc, char *argv[]) {
350351
#ifdef BOOTSTRAP_NAME_SDL2
351352
int SDL_main(int argc, char *argv[]) {
352353
LOGP("Entering SDL_main");
353-
return run_python(argc, argv);
354+
return run_python(argc, argv, true);
354355
}
355356
#endif
356357

@@ -363,7 +364,8 @@ static int native_service_start(
363364
jstring j_python_name,
364365
jstring j_python_home,
365366
jstring j_python_path,
366-
jstring j_arg) {
367+
jstring j_arg,
368+
bool call_exit) {
367369
jboolean iscopy;
368370
const char *android_private =
369371
(*env)->GetStringUTFChars(env, j_android_private, &iscopy);
@@ -394,7 +396,7 @@ static int native_service_start(
394396
/* ANDROID_ARGUMENT points to service subdir,
395397
* so run_python() will run main.py from this dir
396398
*/
397-
return run_python(1, argv);
399+
return run_python(1, argv, call_exit);
398400
}
399401

400402
JNIEXPORT int JNICALL Java_org_kivy_android_PythonService_nativeStart(
@@ -416,7 +418,8 @@ JNIEXPORT int JNICALL Java_org_kivy_android_PythonService_nativeStart(
416418
j_python_name,
417419
j_python_home,
418420
j_python_path,
419-
j_arg);
421+
j_arg,
422+
true);
420423
}
421424

422425
#if defined(BOOTSTRAP_NAME_WEBVIEW) || defined(BOOTSTRAP_NAME_SERVICEONLY)
@@ -455,7 +458,7 @@ int Java_org_kivy_android_PythonActivity_nativeInit(JNIEnv* env, jclass cls, job
455458
argv[1] = NULL;
456459
/* status = SDL_main(1, argv); */
457460

458-
return run_python(1, argv);
461+
return run_python(1, argv, true);
459462

460463
/* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
461464
/* exit(status); */

0 commit comments

Comments
 (0)