Use sys._xoptions to pass bundle_exe_dir

This allows it to be used with the make app kitty bundle as well.
This makes it robust against launching in environments where
python3 is not on PATH. See #1280
This commit is contained in:
Kovid Goyal 2019-05-01 08:45:01 +05:30
parent a2f589be00
commit cab1ba4e50
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 5 deletions

View File

@ -22,7 +22,7 @@ WindowGeometry = namedtuple('WindowGeometry', 'left top right bottom xnum ynum')
def kitty_exe():
ans = getattr(kitty_exe, 'ans', None)
if ans is None:
rpath = getattr(sys, 'bundle_exe_dir', None)
rpath = sys._xoptions.get('bundle_exe_dir')
if not rpath:
items = os.environ['PATH'].split(os.pathsep)
seen = set()

View File

@ -33,6 +33,12 @@ safe_realpath(const char* src, char *buf, size_t buf_sz) {
return true;
}
static inline void
set_bundle_exe_dir(const wchar_t *exe_dir) {
wchar_t buf[PATH_MAX+1] = {0};
swprintf(buf, PATH_MAX, L"bundle_exe_dir=%ls", exe_dir);
PySys_AddXOption(buf);
}
#ifdef FOR_BUNDLE
static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
@ -48,7 +54,7 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
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; }
PyObject *ed = PyUnicode_FromWideChar(exe_dir, -1);
set_bundle_exe_dir(exe_dir);
wchar_t stdlib[PATH_MAX+1] = {0};
#ifdef __APPLE__
const char *python_relpath = "../Resources/Python/lib";
@ -72,7 +78,6 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
Py_Initialize();
PySys_SetArgvEx(argc - 1, argv + 1, 0);
PySys_SetObject("frozen", Py_True);
if (ed) { PySys_SetObject("bundle_exe_dir", ed); Py_CLEAR(ed); }
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");
@ -89,8 +94,13 @@ end:
}
#else
static int run_embedded(const char* exe_dir, int argc, wchar_t **argv) {
(void)exe_dir;
static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
(void)exe_dir_;
#ifdef __APPLE__
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL);
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; }
set_bundle_exe_dir(exe_dir);
#endif
return Py_Main(argc, argv);
}