Skip to content

Commit cab1ba4

Browse files
committed
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
1 parent a2f589b commit cab1ba4

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

kitty/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
def kitty_exe():
2323
ans = getattr(kitty_exe, 'ans', None)
2424
if ans is None:
25-
rpath = getattr(sys, 'bundle_exe_dir', None)
25+
rpath = sys._xoptions.get('bundle_exe_dir')
2626
if not rpath:
2727
items = os.environ['PATH'].split(os.pathsep)
2828
seen = set()

linux-launcher.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ safe_realpath(const char* src, char *buf, size_t buf_sz) {
3333
return true;
3434
}
3535

36+
static inline void
37+
set_bundle_exe_dir(const wchar_t *exe_dir) {
38+
wchar_t buf[PATH_MAX+1] = {0};
39+
swprintf(buf, PATH_MAX, L"bundle_exe_dir=%ls", exe_dir);
40+
PySys_AddXOption(buf);
41+
}
3642

3743
#ifdef FOR_BUNDLE
3844
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) {
4854
int ret = 1;
4955
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL);
5056
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; }
51-
PyObject *ed = PyUnicode_FromWideChar(exe_dir, -1);
57+
set_bundle_exe_dir(exe_dir);
5258
wchar_t stdlib[PATH_MAX+1] = {0};
5359
#ifdef __APPLE__
5460
const char *python_relpath = "../Resources/Python/lib";
@@ -72,7 +78,6 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
7278
Py_Initialize();
7379
PySys_SetArgvEx(argc - 1, argv + 1, 0);
7480
PySys_SetObject("frozen", Py_True);
75-
if (ed) { PySys_SetObject("bundle_exe_dir", ed); Py_CLEAR(ed); }
7681
PyObject *kitty = PyUnicode_FromWideChar(stdlib, -1);
7782
if (kitty == NULL) { fprintf(stderr, "Failed to allocate python kitty lib object\n"); goto end; }
7883
PyObject *runpy = PyImport_ImportModule("runpy");
@@ -89,8 +94,13 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
8994
}
9095

9196
#else
92-
static int run_embedded(const char* exe_dir, int argc, wchar_t **argv) {
93-
(void)exe_dir;
97+
static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) {
98+
(void)exe_dir_;
99+
#ifdef __APPLE__
100+
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL);
101+
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; }
102+
set_bundle_exe_dir(exe_dir);
103+
#endif
94104
return Py_Main(argc, argv);
95105
}
96106

0 commit comments

Comments
 (0)