Skip to content

Commit 7d5aa0b

Browse files
committed
Fixed bootstrap code to correctly load and run a python3 bundle
1 parent 6017030 commit 7d5aa0b

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

pythonforandroid/bootstraps/sdl2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def run_distribute(self):
119119
modules_build_dir = join(
120120
self.ctx.python_recipe.get_build_dir(arch.arch),
121121
'android-build',
122+
'build',
122123
'lib.linux-arm-3.7')
123124
module_filens = (glob.glob(join(modules_build_dir, '*.so')) +
124125
glob.glob(join(modules_build_dir, '*.py')))

pythonforandroid/bootstraps/sdl2/build/build.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def make_python_zip():
125125

126126
if not exists('private'):
127127
print('No compiled python is present to zip, skipping.')
128-
print('this should only be the case if you are using the CrystaX python')
128+
print('this should only be the case if you are using the CrystaX python or python3')
129129
return
130130

131131
global python_files
@@ -243,6 +243,8 @@ def make_package(args):
243243
tar_dirs.append('private')
244244
if exists('crystax_python'):
245245
tar_dirs.append('crystax_python')
246+
if exists('_python_bundle'):
247+
tar_dirs.append('_python_bundle')
246248

247249
if args.private:
248250
make_tar('src/main/assets/private.mp3', tar_dirs, args.ignore_path)

pythonforandroid/bootstraps/sdl2/build/jni/src/start.c

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ int main(int argc, char *argv[]) {
7676
int ret = 0;
7777
FILE *fd;
7878

79-
/* AND: Several filepaths are hardcoded here, these must be made
80-
configurable */
81-
/* AND: P4A uses env vars...not sure what's best */
82-
LOGP("Initialize Python for Android");
79+
setenv("P4A_BOOTSTRAP", "SDL2", 1); // env var to identify p4a to applications
80+
81+
LOGP("Initializing Python for Android");
8382
env_argument = getenv("ANDROID_ARGUMENT");
8483
setenv("ANDROID_APP_PATH", env_argument, 1);
8584
env_entrypoint = getenv("ANDROID_ENTRYPOINT");
@@ -109,32 +108,47 @@ int main(int argc, char *argv[]) {
109108

110109
LOGP("Preparing to initialize python");
111110

111+
// Set up the python path
112+
char paths[256];
113+
112114
char crystax_python_dir[256];
113115
snprintf(crystax_python_dir, 256,
114116
"%s/crystax_python", getenv("ANDROID_UNPACK"));
115-
if (dir_exists(crystax_python_dir)) {
116-
LOGP("crystax_python exists");
117-
char paths[256];
118-
snprintf(paths, 256,
119-
"%s/stdlib.zip:%s/modules",
120-
crystax_python_dir, crystax_python_dir);
121-
/* snprintf(paths, 256, "%s/stdlib.zip:%s/modules", env_argument,
122-
* env_argument); */
123-
LOGP("calculated paths to be...");
124-
LOGP(paths);
117+
char python_bundle_dir[256];
118+
snprintf(python_bundle_dir, 256,
119+
"%s/_python_bundle", getenv("ANDROID_UNPACK"));
120+
if (dir_exists(crystax_python_dir) || dir_exists(python_bundle_dir)) {
121+
if (dir_exists(crystax_python_dir)) {
122+
LOGP("crystax_python exists");
123+
snprintf(paths, 256,
124+
"%s/stdlib.zip:%s/modules",
125+
crystax_python_dir, crystax_python_dir);
126+
LOGP("calculated paths to be...");
127+
LOGP(paths);
128+
}
129+
130+
if (dir_exists(python_bundle_dir)) {
131+
LOGP("_python_bundle dir exists");
132+
snprintf(paths, 256,
133+
"%s/stdlib.zip:%s/modules",
134+
python_bundle_dir, python_bundle_dir);
135+
LOGP("calculated paths to be...");
136+
LOGP(paths);
137+
}
125138

126-
#if PY_MAJOR_VERSION >= 3
127-
wchar_t *wchar_paths = Py_DecodeLocale(paths, NULL);
128-
Py_SetPath(wchar_paths);
129-
#else
130-
char *wchar_paths = paths;
131-
LOGP("Can't Py_SetPath in python2, so crystax python2 doesn't work yet");
132-
exit(1);
133-
#endif
134139

135-
LOGP("set wchar paths...");
140+
#if PY_MAJOR_VERSION >= 3
141+
wchar_t *wchar_paths = Py_DecodeLocale(paths, NULL);
142+
Py_SetPath(wchar_paths);
143+
#else
144+
char *wchar_paths = paths;
145+
LOGP("Can't Py_SetPath in python2, so crystax python2 doesn't work yet");
146+
exit(1);
147+
#endif
148+
149+
LOGP("set wchar paths...");
136150
} else {
137-
LOGP("crystax_python does not exist");
151+
LOGP("crystax_python does not exist");
138152
}
139153

140154
Py_Initialize();
@@ -174,8 +188,8 @@ int main(int argc, char *argv[]) {
174188
" argument ]\n");
175189
}
176190

191+
char add_site_packages_dir[256];
177192
if (dir_exists(crystax_python_dir)) {
178-
char add_site_packages_dir[256];
179193
snprintf(add_site_packages_dir, 256,
180194
"sys.path.append('%s/site-packages')",
181195
crystax_python_dir);
@@ -188,6 +202,19 @@ int main(int argc, char *argv[]) {
188202
PyRun_SimpleString("sys.path = ['.'] + sys.path");
189203
}
190204

205+
if (dir_exists(python_bundle_dir)) {
206+
snprintf(add_site_packages_dir, 256,
207+
"sys.path.append('%s/site-packages')",
208+
python_bundle_dir);
209+
210+
PyRun_SimpleString("import sys\n"
211+
"sys.argv = ['notaninterpreterreally']\n"
212+
"from os.path import realpath, join, dirname");
213+
PyRun_SimpleString(add_site_packages_dir);
214+
/* "sys.path.append(join(dirname(realpath(__file__)), 'site-packages'))") */
215+
PyRun_SimpleString("sys.path = ['.'] + sys.path");
216+
}
217+
191218
PyRun_SimpleString(
192219
"class LogFile(object):\n"
193220
" def __init__(self):\n"
@@ -317,6 +344,7 @@ JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart(
317344
setenv("PYTHONHOME", python_home, 1);
318345
setenv("PYTHONPATH", python_path, 1);
319346
setenv("PYTHON_SERVICE_ARGUMENT", arg, 1);
347+
setenv("P4A_BOOTSTRAP", "SDL2", 1);
320348

321349
char *argv[] = {"."};
322350
/* ANDROID_ARGUMENT points to service subdir,

testapps/setup_testapp_python3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'requirements': 'sdl2,pyjnius,kivy,python3',
77
'android-api': 19,
88
'ndk-dir': '/home/asandy/android/crystax-ndk-10.3.2',
9-
'dist-name': 'bdisttest_python3',
9+
'dist-name': 'bdisttest_python3_googlendk',
1010
'ndk-version': '10.3.2',
1111
'arch': 'armeabi-v7a',
1212
'permission': 'VIBRATE',
@@ -20,7 +20,7 @@
2020
print('packages are', packages)
2121

2222
setup(
23-
name='testapp_python3',
23+
name='testapp_python3_googlendk',
2424
version='1.1',
2525
description='p4a setup.py test',
2626
author='Alexander Taylor',

0 commit comments

Comments
 (0)