From af1f3cc7c7815ab5ee53d16b7f625ea732a0f692 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Feb 2017 14:26:26 +0530 Subject: [PATCH] More robust in bundle launcher Ignores environment, user settings. Should minimize the chances of conflict with any other python installations on the system. --- linux-launcher.c | 61 +++++++++++++++++++++++++++++++++++++++--------- setup.py | 3 ++- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/linux-launcher.c b/linux-launcher.c index 69c994d7f..c3a216e37 100644 --- a/linux-launcher.c +++ b/linux-launcher.c @@ -18,16 +18,56 @@ #include #endif #include +#include #define MIN(x, y) ((x) < (y)) ? (x) : (y) #define MAX_ARGC 1024 +#ifdef FOR_BUNDLE +static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) { + int num; + Py_NoSiteFlag = 1; + Py_FrozenFlag = 1; + Py_IgnoreEnvironmentFlag = 1; + Py_DontWriteBytecodeFlag = 1; + Py_NoUserSiteDirectory = 1; + Py_IsolatedFlag = 1; + Py_SetProgramName(L"kitty"); + + int ret = 1; + wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL); + if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; } + wchar_t stdlib[PATH_MAX+1] = {0}; + num = swprintf(stdlib, PATH_MAX, L"%ls/../Resources/Python/lib/python%s:%ls/../Resources/Python/lib/python%s/lib-dynload", exe_dir, PYVER, exe_dir, PYVER); + if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to python stdlib\n"); return 1; } + Py_SetPath(stdlib); + num = swprintf(stdlib, PATH_MAX, L"%ls/../Frameworks/kitty", exe_dir); + PyMem_RawFree(exe_dir); + if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to kitty lib\n"); return 1; } + Py_Initialize(); + PySys_SetArgvEx(argc - 1, argv + 1, 0); + PyObject *kitty = PyUnicode_FromWideChar(stdlib, -1); + if (kitty == NULL) { fprintf(stderr, "Failed to allocate python kitty lib object\n"); goto end; } + PyObject *runpy = PyImport_ImportModule("runpy"); + if (runpy == NULL) { PyErr_Print(); fprintf(stderr, "Unable to import runpy\n"); Py_CLEAR(kitty); goto end; } + PyObject *res = PyObject_CallMethod(runpy, "run_path", "O", kitty); + Py_CLEAR(runpy); Py_CLEAR(kitty); + if (res == NULL) PyErr_Print(); + else { ret = 0; Py_CLEAR(res); } +end: + if (Py_FinalizeEx() < 0) ret = 120; + return ret; +} + +#else +static int run_embedded(const char* exe_dir, int argc, wchar_t **argv) { + return Py_Main(argc, argv); +} + +#endif + int main(int argc, char *argv[]) { - int num, num_args, i, ret=0; char exe[PATH_MAX+1] = {0}; - char lib[PATH_MAX+1] = {0}; - char *final_argv[MAX_ARGC + 1] = {0}; - wchar_t *argvw[MAX_ARGC + 1] = {0}; #ifdef __APPLE__ uint32_t size = PATH_MAX; @@ -39,12 +79,11 @@ int main(int argc, char *argv[]) { #endif char *exe_dir = dirname(exe); - -#ifdef FOR_BUNDLE - num = snprintf(lib, PATH_MAX, "%s%s", exe_dir, "/../Frameworks/kitty"); -#else + int num, num_args, i, ret=0; + char lib[PATH_MAX+1] = {0}; + char *final_argv[MAX_ARGC + 1] = {0}; + wchar_t *argvw[MAX_ARGC + 1] = {0}; num = snprintf(lib, PATH_MAX, "%s%s", exe_dir, "/../lib/kitty"); -#endif if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to /../lib/kitty\n"); return 1; } final_argv[0] = exe; @@ -57,10 +96,10 @@ int main(int argc, char *argv[]) { argvw[i] = Py_DecodeLocale(final_argv[i], NULL); if (argvw[i] == NULL) { fprintf(stderr, "Fatal error: cannot decode argv[%d]\n", i); - goto end; + ret = 1; goto end; } } - ret = Py_Main(num_args, argvw); + ret = run_embedded(exe_dir, num_args, argvw); end: for (i = 0; i < num_args; i++) { if(argvw[i]) PyMem_RawFree(argvw[i]); } return ret; diff --git a/setup.py b/setup.py index 4d332c0e8..37eed4a56 100755 --- a/setup.py +++ b/setup.py @@ -239,9 +239,10 @@ def package(args, for_bundle=False): # {{{ os.chmod(path, 0o755 if f.endswith('.so') else 0o644) launcher_dir = os.path.join(ddir, 'bin') safe_makedirs(launcher_dir) - cflags = '-O3'.split() + cflags = '-O3 -Wall -Werror'.split() if for_bundle: cflags.append('-DFOR_BUNDLE') + cflags.append('-DPYVER="{}"'.format(sysconfig.get_python_version())) pylib = get_python_flags(cflags) cmd = [cc] + cflags + ['linux-launcher.c', '-o', os.path.join(launcher_dir, 'kitty')] + pylib run_tool(cmd)