Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
5 changes: 1 addition & 4 deletions pythonforandroid/archs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ def get_env(self, with_flags_in_cc=True):
env['CPPFLAGS'] = ' '.join(self.common_cppflags).format(
ctx=self.ctx,
command_prefix=self.command_prefix,
python_includes=join(
self.ctx.get_python_install_dir(self.arch),
'include/python{}'.format(self.ctx.python_recipe.version[0:3]),
),
python_includes=join(self.ctx.python_recipe.get_build_dir(self.arch), 'Include')
)

# LDFLAGS: Link the extra global link paths first before anything else
Expand Down
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?");
}

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
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