More robust in bundle launcher

Ignores environment, user settings. Should minimize the chances of
conflict with any other python installations on the system.
This commit is contained in:
Kovid Goyal 2017-02-02 14:26:26 +05:30
parent 543af0dd89
commit af1f3cc7c7
2 changed files with 52 additions and 12 deletions

View File

@ -18,16 +18,56 @@
#include <limits.h> #include <limits.h>
#endif #endif
#include <Python.h> #include <Python.h>
#include <wchar.h>
#define MIN(x, y) ((x) < (y)) ? (x) : (y) #define MIN(x, y) ((x) < (y)) ? (x) : (y)
#define MAX_ARGC 1024 #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 main(int argc, char *argv[]) {
int num, num_args, i, ret=0;
char exe[PATH_MAX+1] = {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__ #ifdef __APPLE__
uint32_t size = PATH_MAX; uint32_t size = PATH_MAX;
@ -39,12 +79,11 @@ int main(int argc, char *argv[]) {
#endif #endif
char *exe_dir = dirname(exe); char *exe_dir = dirname(exe);
int num, num_args, i, ret=0;
#ifdef FOR_BUNDLE char lib[PATH_MAX+1] = {0};
num = snprintf(lib, PATH_MAX, "%s%s", exe_dir, "/../Frameworks/kitty"); char *final_argv[MAX_ARGC + 1] = {0};
#else wchar_t *argvw[MAX_ARGC + 1] = {0};
num = snprintf(lib, PATH_MAX, "%s%s", exe_dir, "/../lib/kitty"); 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; } if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to /../lib/kitty\n"); return 1; }
final_argv[0] = exe; final_argv[0] = exe;
@ -57,10 +96,10 @@ int main(int argc, char *argv[]) {
argvw[i] = Py_DecodeLocale(final_argv[i], NULL); argvw[i] = Py_DecodeLocale(final_argv[i], NULL);
if (argvw[i] == NULL) { if (argvw[i] == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[%d]\n", i); 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: end:
for (i = 0; i < num_args; i++) { if(argvw[i]) PyMem_RawFree(argvw[i]); } for (i = 0; i < num_args; i++) { if(argvw[i]) PyMem_RawFree(argvw[i]); }
return ret; return ret;

View File

@ -239,9 +239,10 @@ def package(args, for_bundle=False): # {{{
os.chmod(path, 0o755 if f.endswith('.so') else 0o644) os.chmod(path, 0o755 if f.endswith('.so') else 0o644)
launcher_dir = os.path.join(ddir, 'bin') launcher_dir = os.path.join(ddir, 'bin')
safe_makedirs(launcher_dir) safe_makedirs(launcher_dir)
cflags = '-O3'.split() cflags = '-O3 -Wall -Werror'.split()
if for_bundle: if for_bundle:
cflags.append('-DFOR_BUNDLE') cflags.append('-DFOR_BUNDLE')
cflags.append('-DPYVER="{}"'.format(sysconfig.get_python_version()))
pylib = get_python_flags(cflags) pylib = get_python_flags(cflags)
cmd = [cc] + cflags + ['linux-launcher.c', '-o', os.path.join(launcher_dir, 'kitty')] + pylib cmd = [cc] + cflags + ['linux-launcher.c', '-o', os.path.join(launcher_dir, 'kitty')] + pylib
run_tool(cmd) run_tool(cmd)