From 99f27543656a8bea44b7ba0ab6ea91f824d45537 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Thu, 14 Feb 2019 21:21:48 -0800 Subject: Restore getpath.c closer to upstream Instead of changing the control flow significantly, use SetPythonHome, -DPYTHONPATH, and a handful of ifdefs. Test: build/soong/python/tests/runtest.sh Test: git diff 18e21883a4..HEAD -- Modules/getpath.c Change-Id: I27ca46f953c160e437b1610085cc756056ba3812 --- Android.bp | 5 ++- Launcher/launcher_main.cpp | 3 ++ Modules/getpath.c | 83 +++++++++++----------------------------------- 3 files changed, 26 insertions(+), 65 deletions(-) diff --git a/Android.bp b/Android.bp index 2a56885aca..5d72eda072 100644 --- a/Android.bp +++ b/Android.bp @@ -219,7 +219,10 @@ cc_binary { "-DVPATH=\"\"", "-DPREFIX=\"\"", "-DEXEC_PREFIX=\"\"", - "-DPYTHONPATH=\"\"", + "-DPYTHONPATH=\"..:\"", + "-DANDROID_SKIP_ZIP_PATH", + "-DANDROID_SKIP_EXEC_PREFIX_PATH", + "-DANDROID_LIB_PYTHON_PATH=\"internal/stdlib\"", "-DDATE=\"Dec 31 1969\"", "-DTIME=\"23:59:59\"", ], diff --git a/Launcher/launcher_main.cpp b/Launcher/launcher_main.cpp index 09a9d3969f..c0b0c49755 100644 --- a/Launcher/launcher_main.cpp +++ b/Launcher/launcher_main.cpp @@ -42,6 +42,9 @@ int main(int argc, char *argv[]) { // android::base::GetExecutablePath() also handles for Darwin/Windows. std::string executable_path = android::base::GetExecutablePath(); + // Set the equivalent of PYTHONHOME internally. + Py_SetPythonHome(strdup(executable_path.c_str())); + int new_argc = argc + 1; char **new_argv = reinterpret_cast(calloc(new_argc, sizeof(*argv))); diff --git a/Modules/getpath.c b/Modules/getpath.c index e9d969bd1f..27f3381d79 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -104,19 +104,15 @@ #define LANDMARK "os.py" #endif -#ifndef INTERNALDIR -#define INTERNALDIR "internal" -#endif - -#ifndef STDLIBDIR -#define STDLIBDIR "stdlib" -#endif - static char prefix[MAXPATHLEN+1]; static char exec_prefix[MAXPATHLEN+1]; static char progpath[MAXPATHLEN+1]; static char *module_search_path = NULL; +#ifdef ANDROID_LIB_PYTHON_PATH +static char lib_python[] = ANDROID_LIB_PYTHON_PATH; +#else static char lib_python[] = "lib/python" VERSION; +#endif static void reduce(char *dir) @@ -263,11 +259,8 @@ search_for_prefix(char *argv0_path, char *home) return 1; } - // GOOGLE(nanzhang): Always set prefix with hermetic executable full path. - strcpy(prefix, argv0_path); - return 1; - /* Check to see if argv[0] is in the build directory */ + strcpy(prefix, argv0_path); joinpath(prefix, "Modules/Setup"); if (isfile(prefix)) { /* Check VPATH to see if argv0_path is in the build directory. */ @@ -325,13 +318,10 @@ search_for_exec_prefix(char *argv0_path, char *home) return 1; } - // GOOGLE(nanzhang): Always set exec_prefix with hermetic executable full path. - strcpy(exec_prefix, argv0_path); - return 1; - /* Check to see if argv[0] is in the build directory. "pybuilddir.txt" is written by setup.py and contains the relative path to the location of shared library modules. */ + strcpy(exec_prefix, argv0_path); joinpath(exec_prefix, "pybuilddir.txt"); if (isfile(exec_prefix)) { FILE *f = fopen(exec_prefix, "r"); @@ -377,62 +367,19 @@ static void calculate_path(void) { extern char *Py_GetProgramName(void); + static char delimiter[2] = {DELIM, '\0'}; static char separator[2] = {SEP, '\0'}; char *pythonpath = PYTHONPATH; + char *rtpypath = Py_GETENV("PYTHONPATH"); char *home = Py_GetPythonHome(); - // We have overrided argv[0] using the full path to the hermetic Python - // launcher itself. And then Py_SetProgramName(argv[0]) was invoked at - // launcher_main.cpp. The launcher_main.cpp has guaranteed that - // strlen(Py_GetProgramName()) must not exceed MAXPATHLEN. + char *path = getenv("PATH"); char *prog = Py_GetProgramName(); char argv0_path[MAXPATHLEN+1]; - char *buf; - size_t bufsz; - - strncpy(progpath, prog, MAXPATHLEN); - progpath[MAXPATHLEN] = '\0' /* In case of no NUL-termination. */; - strncpy(argv0_path, prog, MAXPATHLEN); - argv0_path[MAXPATHLEN] = '\0' /* In case of no NUL-termination. */; - - // We don't reduce the path of prefix, and exec_prefix. - search_for_prefix(argv0_path, home); - search_for_exec_prefix(argv0_path, home); - - // Calculate size of return buffer. - bufsz = strlen(prog) + 1 + sizeof(INTERNALDIR) /* 1 is for SEP */; - bufsz += strlen(prog) + 1 + sizeof(INTERNALDIR) + 1 + sizeof(STDLIBDIR) /* 1 is for SEP */; - - /* This is the only malloc call in this file */ - buf = (char *)PyMem_Malloc(bufsz); - - if (buf == NULL) { - /* We can't exit, so print a warning and limp along */ - fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); - fprintf(stderr, "Using default static PYTHONPATH.\n"); - exit(1); - } else { - buf[0] = '\0'; - strcat(buf, prefix); - strcat(buf, separator); - strcat(buf, INTERNALDIR); - strcat(buf, delimiter); - - strcat(buf, prefix); - strcat(buf, separator); - strcat(buf, INTERNALDIR); - strcat(buf, separator); - strcat(buf, STDLIBDIR); - - module_search_path = buf; - } - // GOOGLE(nanzhang): Don't need all the code below for embedded Python launcher. - return; - - char *path = getenv("PATH"); - char *rtpypath = Py_GETENV("PYTHONPATH"); char zip_path[MAXPATHLEN+1]; int pfound, efound; /* 1 if found; -1 if found build directory */ + char *buf; + size_t bufsz; size_t prefixsz; char *defpath = pythonpath; #ifdef WITH_NEXT_FRAMEWORK @@ -620,8 +567,12 @@ calculate_path(void) defpath = delim + 1; } +#ifndef ANDROID_SKIP_ZIP_PATH bufsz += strlen(zip_path) + 1; +#endif +#ifndef ANDROID_SKIP_EXEC_PREFIX_PATH bufsz += strlen(exec_prefix) + 1; +#endif /* This is the only malloc call in this file */ buf = (char *)PyMem_Malloc(bufsz); @@ -642,8 +593,10 @@ calculate_path(void) buf[0] = '\0'; /* Next is the default zip path */ +#ifndef ANDROID_SKIP_ZIP_PATH strcat(buf, zip_path); strcat(buf, delimiter); +#endif /* Next goes merge of compile-time $PYTHONPATH with * dynamically located prefix. @@ -672,10 +625,12 @@ calculate_path(void) } defpath = delim + 1; } +#ifndef ANDROID_SKIP_EXEC_PREFIX_PATH strcat(buf, delimiter); /* Finally, on goes the directory for dynamic-load modules */ strcat(buf, exec_prefix); +#endif /* And publish the results */ module_search_path = buf; -- cgit v1.2.3