Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 40 additions & 88 deletions pythonforandroid/bootstraps/common/build/jni/application/src/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ int main(int argc, char *argv[]) {
int ret = 0;
FILE *fd;

char *workSpace = NULL;
char *pyScript = NULL;

LOGP("Initializing Python for Android");

// Set a couple of built-in environment vars:
Expand All @@ -97,6 +100,9 @@ int main(int argc, char *argv[]) {
setenv("PYTHON_NAME", "python", 1);
}

workSpace = getenv("WORKSPACE");
pyScript = getenv("PYSCRIPT");

// Set additional file-provided environment vars:
LOGP("Setting additional env vars from p4a_env_vars.txt");
char env_file_path[256];
Expand Down Expand Up @@ -140,25 +146,15 @@ int main(int argc, char *argv[]) {
LOGP("Warning: no p4a_env_vars.txt found / failed to open!");
}

LOGP("Changing directory to the one provided by ANDROID_ARGUMENT");
LOGP(env_argument);
chdir(env_argument);

#if PY_MAJOR_VERSION < 3
Py_NoSiteFlag=1;
#endif

#if PY_MAJOR_VERSION < 3
Py_SetProgramName("android_python");
#else
Py_SetProgramName(L"android_python");
#endif
LOGP("Changing directory to the one provided by ANDROID_ARGUMENT");
LOGP(env_argument);
// chdir(env_argument);
chdir(workSpace);

#if PY_MAJOR_VERSION >= 3
/* our logging module for android
Py_SetProgramName(L"android_python");
/* our logging module for android
*/
PyImport_AppendInittab("androidembed", initandroidembed);
#endif

LOGP("Preparing to initialize python");

Expand Down Expand Up @@ -196,10 +192,6 @@ int main(int argc, char *argv[]) {
LOGP("AND: Init threads");
PyEval_InitThreads();

#if PY_MAJOR_VERSION < 3
initandroidembed();
#endif

PyRun_SimpleString("import androidembed\nandroidembed.log('testing python "
"print redirection')");

Expand All @@ -209,16 +201,32 @@ int main(int argc, char *argv[]) {
PyRun_SimpleString("import sys, posix\n");

char add_site_packages_dir[256];
char add_prefix[256];
char add_lib_path[256];
char add_current_workspace_path[256];

if (dir_exists(python_bundle_dir)) {
snprintf(add_site_packages_dir, 256,
"sys.path.append('%s/site-packages')",
python_bundle_dir);
snprintf(add_lib_path, 256,
"sys.path.append('%s/lib/python3.8/site-packages')",
getenv("ANDROID_UNPACK"));

snprintf(add_current_workspace_path, 256,
"sys.path.append('%s')", workSpace);

snprintf(add_prefix, 256,
"sys.prefix = '%s'",
getenv("ANDROID_UNPACK"));

PyRun_SimpleString("import sys\n"
"sys.argv = ['notaninterpreterreally']\n"
"from os.path import realpath, join, dirname");
PyRun_SimpleString(add_prefix);
PyRun_SimpleString(add_site_packages_dir);
PyRun_SimpleString(add_lib_path);
PyRun_SimpleString(add_current_workspace_path);
/* "sys.path.append(join(dirname(realpath(__file__)), 'site-packages'))") */
PyRun_SimpleString("sys.path = ['.'] + sys.path");
}
Expand All @@ -241,10 +249,6 @@ int main(int argc, char *argv[]) {
"print('os.environ is', os.environ)\n"
"print('Android kivy bootstrap done. __name__ is', __name__)");

#if PY_MAJOR_VERSION < 3
PyRun_SimpleString("import site; print site.getsitepackages()\n");
#endif

LOGP("AND: Ran string");

/* run it !
Expand All @@ -253,74 +257,26 @@ int main(int argc, char *argv[]) {

/* Get the entrypoint, search the .pyo then .py
*/
char *dot = strrchr(env_entrypoint, '.');
#if PY_MAJOR_VERSION > 2
char *ext = ".pyc";
#else
char *ext = ".pyo";
#endif
if (dot <= 0) {
LOGP("Invalid entrypoint, abort.");
return -1;
}
if (strlen(env_entrypoint) > ENTRYPOINT_MAXLEN - 2) {
LOGP("Entrypoint path is too long, try increasing ENTRYPOINT_MAXLEN.");
return -1;
}
if (!strcmp(dot, ext)) {
if (!file_exists(env_entrypoint)) {
/* fallback on .py */
strcpy(entrypoint, env_entrypoint);
entrypoint[strlen(env_entrypoint) - 1] = '\0';
LOGP(entrypoint);
if (!file_exists(entrypoint)) {
LOGP("Entrypoint not found (.pyc, fallback on .py), abort");
return -1;
}
} else {
strcpy(entrypoint, env_entrypoint);
}
} else if (!strcmp(dot, ".py")) {
/* if .py is passed, check the pyo version first */
strcpy(entrypoint, env_entrypoint);
entrypoint[strlen(env_entrypoint) + 1] = '\0';
#if PY_MAJOR_VERSION > 2
entrypoint[strlen(env_entrypoint)] = 'c';
#else
entrypoint[strlen(env_entrypoint)] = 'o';
#endif
if (!file_exists(entrypoint)) {
/* fallback on pure python version */
if (!file_exists(env_entrypoint)) {
LOGP("Entrypoint not found (.py), abort.");
return -1;
}
strcpy(entrypoint, env_entrypoint);
}
} else {
LOGP("Entrypoint have an invalid extension (must be .py or .pyc), abort.");
return -1;
}
// LOGP("Entrypoint is:");
// LOGP(entrypoint);
fd = fopen(entrypoint, "r");
fd = fopen(pyScript, "r");
if (fd == NULL) {
LOGP("Open the entrypoint failed");
LOGP(entrypoint);
LOGP(pyScript);
return -1;
}

/* run python !
*/
ret = PyRun_SimpleFile(fd, entrypoint);
ret = PyRun_SimpleFile(fd, pyScript);
fclose(fd);

if (PyErr_Occurred() != NULL) {
ret = 1;
PyErr_Print(); /* This exits with the right code if SystemExit. */
PyObject *f = PySys_GetObject("stdout");
if (PyFile_WriteString("\n", f))
PyErr_Clear();
PyErr_Clear();
// PyObject *f = PySys_GetObject("stdout");
// if (PyFile_WriteString("\n", f))
// PyErr_Clear();
}

LOGP("Python for android ended.");
Expand All @@ -333,25 +289,21 @@ int main(int argc, char *argv[]) {

https://github.com/kivy/kivy/pull/6107#issue-246120816
*/
char terminatecmd[256];

/* char terminatecmd[256];
snprintf(
terminatecmd, sizeof(terminatecmd),
"import sys; sys.exit(%d)\n", ret
);
PyRun_SimpleString(terminatecmd);
PyRun_SimpleString(terminatecmd);*/

/* This should never actually be reached, but we'll leave the clean-up
* here just to be safe.
*/
#if PY_MAJOR_VERSION < 3
Py_Finalize();
LOGP("Unexpectedly reached Py_FinalizeEx(), but was successful.");
#else
if (Py_FinalizeEx() != 0) // properly check success on Python 3
LOGP("Unexpectedly reached Py_FinalizeEx(), and got error!");
LOGP("Unexpectedly reached Py_FinalizeEx(), and got error!");
else
LOGP("Unexpectedly reached Py_FinalizeEx(), but was successful.");
#endif
LOGP("Unexpectedly reached Py_FinalizeEx(), but was successful.");

return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/recipes/pygame/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Pygame2Recipe(CompiledComponentsPythonRecipe):
not part of the build. It's usable, but not complete.
"""

version = '2.0.0-dev7'
url = 'https://github.com/pygame/pygame/archive/android-{version}.tar.gz'
version = '2.0.1'
url = 'https://github.com/pygame/pygame/archive/{version}.tar.gz'

site_packages_name = 'pygame'
name = 'pygame'
Expand Down