Skip to content

[WIP] python: update to 3.13 #3180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
100 changes: 61 additions & 39 deletions pythonforandroid/bootstraps/common/build/jni/application/src/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define ENTRYPOINT_MAXLEN 128
#define LOG(n, x) __android_log_write(ANDROID_LOG_INFO, (n), (x))
#define LOGP(x) LOG("python", (x))
#define P4A_MIN_VER 11

static PyObject *androidembed_log(PyObject *self, PyObject *args) {
char *logstr = NULL;
Expand Down Expand Up @@ -154,11 +155,6 @@ int main(int argc, char *argv[]) {
Py_NoSiteFlag=1;
#endif

#if PY_MAJOR_VERSION < 3
Py_SetProgramName("android_python");
#else
Py_SetProgramName(L"android_python");
#endif

#if PY_MAJOR_VERSION >= 3
/* our logging module for android
Expand All @@ -174,40 +170,80 @@ int main(int argc, char *argv[]) {
char python_bundle_dir[256];
snprintf(python_bundle_dir, 256,
"%s/_python_bundle", getenv("ANDROID_UNPACK"));
if (dir_exists(python_bundle_dir)) {
LOGP("_python_bundle dir exists");
snprintf(paths, 256,
"%s/stdlib.zip:%s/modules",
python_bundle_dir, python_bundle_dir);

LOGP("calculated paths to be...");
LOGP(paths);
#if PY_MAJOR_VERSION >= 3

#if PY_MAJOR_VERSION >= 3
wchar_t *wchar_paths = Py_DecodeLocale(paths, NULL);
Py_SetPath(wchar_paths);
#if PY_MINOR_VERSION >= P4A_MIN_VER
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.program_name = L"android_python";
#else
Py_SetProgramName(L"android_python");
#endif

LOGP("set wchar paths...");
#else
Py_SetProgramName("android_python");
#endif

if (dir_exists(python_bundle_dir)) {
LOGP("_python_bundle dir exists");

#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= P4A_MIN_VER

wchar_t wchar_zip_path[256];
wchar_t wchar_modules_path[256];
swprintf(wchar_zip_path, 256, L"%s/stdlib.zip", python_bundle_dir);
swprintf(wchar_modules_path, 256, L"%s/modules", python_bundle_dir);

config.module_search_paths_set = 1;
PyWideStringList_Append(&config.module_search_paths, wchar_zip_path);
PyWideStringList_Append(&config.module_search_paths, wchar_modules_path);
#else
char paths[512];
snprintf(paths, 512, "%s/stdlib.zip:%s/modules", python_bundle_dir, python_bundle_dir);
wchar_t *wchar_paths = Py_DecodeLocale(paths, NULL);
Py_SetPath(wchar_paths);
#endif

#endif

LOGP("set wchar paths...");
} else {
LOGP("_python_bundle does not exist...this not looks good, all python"
" recipes should have this folder, should we expect a crash soon?");
LOGP("_python_bundle does not exist...this not looks good, all python"
" recipes should have this folder, should we expect a crash soon?");
}

Py_Initialize();
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= P4A_MIN_VER
PyStatus status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
LOGP("Python initialization failed:");
LOGP(status.err_msg);
}
#else
Py_Initialize();
LOGP("Python initialized using legacy Py_Initialize().");
#endif

LOGP("Initialized python");

/* ensure threads will work.
*/
LOGP("AND: Init threads");
PyEval_InitThreads();
/* < 3.9 requires explicit GIL initialization
* 3.9+ PyEval_InitThreads() is deprecated and unnecessary
*/
#if PY_VERSION_HEX < 0x03090000
LOGP("Initializing threads (required for Python < 3.9)");
PyEval_InitThreads();
#endif

#if PY_MAJOR_VERSION < 3
initandroidembed();
#endif

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

);

/* inject our bootstrap code to redirect python stdin/stdout
* replace sys.path with our path
Expand Down Expand Up @@ -325,20 +361,6 @@ int main(int argc, char *argv[]) {

LOGP("Python for android ended.");

/* Shut down: since regular shutdown causes issues sometimes
(seems to be an incomplete shutdown breaking next launch)
we'll use sys.exit(ret) to shutdown, since that one works.

Reference discussion:

https://github.com/kivy/kivy/pull/6107#issue-246120816
*/
char terminatecmd[256];
snprintf(
terminatecmd, sizeof(terminatecmd),
"import sys; sys.exit(%d)\n", ret
);
PyRun_SimpleString(terminatecmd);

/* This should never actually be reached, but we'll leave the clean-up
* here just to be safe.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,22 @@ protected static void addLibraryIfExists(ArrayList<String> libsList, String patt
}

protected static ArrayList<String> getLibraries(File libsDir) {
ArrayList<String> libsList = new ArrayList<String>();
addLibraryIfExists(libsList, "sqlite3", libsDir);
addLibraryIfExists(libsList, "ffi", libsDir);
addLibraryIfExists(libsList, "png16", libsDir);
addLibraryIfExists(libsList, "ssl.*", libsDir);
addLibraryIfExists(libsList, "crypto.*", libsDir);
addLibraryIfExists(libsList, "SDL2", libsDir);
addLibraryIfExists(libsList, "SDL2_image", libsDir);
addLibraryIfExists(libsList, "SDL2_mixer", libsDir);
addLibraryIfExists(libsList, "SDL2_ttf", libsDir);
addLibraryIfExists(libsList, "SDL3", libsDir);
addLibraryIfExists(libsList, "SDL3_image", libsDir);
addLibraryIfExists(libsList, "SDL3_mixer", libsDir);
addLibraryIfExists(libsList, "SDL3_ttf", libsDir);
libsList.add("python3.5m");
libsList.add("python3.6m");
libsList.add("python3.7m");
libsList.add("python3.8");
libsList.add("python3.9");
libsList.add("python3.10");
libsList.add("python3.11");
ArrayList<String> libsList = new ArrayList<>();

String[] libNames = {
"sqlite3", "ffi", "png16", "ssl.*", "crypto.*",
"SDL2", "SDL2_image", "SDL2_mixer", "SDL2_ttf",
"SDL3", "SDL3_image", "SDL3_mixer", "SDL3_ttf"
};

for (String name : libNames) {
addLibraryIfExists(libsList, name, libsDir);
}

for (int v = 5; v <= 13; v++) {
libsList.add("python3." + v + (v <= 7 ? "m" : ""));
}

libsList.add("main");
return libsList;
}
Expand All @@ -79,7 +74,7 @@ public static void loadLibraries(File filesDir, File libsDir) {
// load, and it has failed, give a more
// general error
Log.v(TAG, "Library loading error: " + e.getMessage());
if (lib.startsWith("python3.11") && !foundPython) {
if (lib.startsWith("python3.13") && !foundPython) {
throw new RuntimeException("Could not load any libpythonXXX.so");
} else if (lib.startsWith("python")) {
continue;
Expand Down
Loading
Loading