7
7
8
8
#include <stdio.h>
9
9
#include <stdlib.h>
10
+ #include <stdbool.h>
10
11
#include <unistd.h>
11
12
#include <dirent.h>
12
13
#include <jni.h>
@@ -70,7 +71,7 @@ static int file_exists(const char *filename) {
70
71
return 0 ;
71
72
}
72
73
73
- static int run_python (int argc , char * argv []) {
74
+ static int run_python (int argc , char * argv [], bool call_exit ) {
74
75
75
76
char * env_argument = NULL ;
76
77
char * env_entrypoint = NULL ;
@@ -324,24 +325,24 @@ static int run_python(int argc, char *argv[]) {
324
325
325
326
https://github.com/kivy/kivy/pull/6107#issue-246120816
326
327
*/
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.
336
338
*/
339
+ if (call_exit )
340
+ LOGP ("Unexpectedly reached python finalization" );
337
341
#if PY_MAJOR_VERSION < 3
338
342
Py_Finalize ();
339
- LOGP ("Unexpectedly reached Py_FinalizeEx(), but was successful." );
340
343
#else
341
344
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!" );
345
346
#endif
346
347
347
348
return ret ;
@@ -350,7 +351,7 @@ static int run_python(int argc, char *argv[]) {
350
351
#ifdef BOOTSTRAP_NAME_SDL2
351
352
int SDL_main (int argc , char * argv []) {
352
353
LOGP ("Entering SDL_main" );
353
- return run_python (argc , argv );
354
+ return run_python (argc , argv , true );
354
355
}
355
356
#endif
356
357
@@ -363,7 +364,8 @@ static int native_service_start(
363
364
jstring j_python_name ,
364
365
jstring j_python_home ,
365
366
jstring j_python_path ,
366
- jstring j_arg ) {
367
+ jstring j_arg ,
368
+ bool call_exit ) {
367
369
jboolean iscopy ;
368
370
const char * android_private =
369
371
(* env )-> GetStringUTFChars (env , j_android_private , & iscopy );
@@ -394,7 +396,7 @@ static int native_service_start(
394
396
/* ANDROID_ARGUMENT points to service subdir,
395
397
* so run_python() will run main.py from this dir
396
398
*/
397
- return run_python (1 , argv );
399
+ return run_python (1 , argv , call_exit );
398
400
}
399
401
400
402
JNIEXPORT int JNICALL Java_org_kivy_android_PythonService_nativeStart (
@@ -416,7 +418,8 @@ JNIEXPORT int JNICALL Java_org_kivy_android_PythonService_nativeStart(
416
418
j_python_name ,
417
419
j_python_home ,
418
420
j_python_path ,
419
- j_arg );
421
+ j_arg ,
422
+ true);
420
423
}
421
424
422
425
#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
455
458
argv [1 ] = NULL ;
456
459
/* status = SDL_main(1, argv); */
457
460
458
- return run_python (1 , argv );
461
+ return run_python (1 , argv , true );
459
462
460
463
/* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
461
464
/* exit(status); */
0 commit comments