diff --git a/kitty/child.py b/kitty/child.py index 2ebd24e87..4911dd9e1 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -242,7 +242,7 @@ class Child: def final_env(self) -> Dict[str, str]: from kitty.options.utils import DELETE_ENV_VAR env = default_env().copy() - if is_macos and env.get('LC_CTYPE') == 'UTF-8' and not sys._xoptions.get( + if is_macos and env.get('LC_CTYPE') == 'UTF-8' and not getattr(sys, 'kitty_run_data').get( 'lc_ctype_before_python') and not getattr(default_env, 'lc_ctype_set_by_user', False): del env['LC_CTYPE'] env.update(self.env) diff --git a/kitty/constants.py b/kitty/constants.py index 030b4e452..c4c25b8bc 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -29,7 +29,7 @@ is_macos: bool = 'darwin' in _plat is_freebsd: bool = 'freebsd' in _plat is_running_from_develop: bool = False if getattr(sys, 'frozen', False): - extensions_dir: str = getattr(sys, 'kitty_extensions_dir') + extensions_dir: str = getattr(sys, 'kitty_run_data')['extensions_dir'] def get_frozen_base() -> str: global is_running_from_develop @@ -61,7 +61,7 @@ else: @run_once def kitty_exe() -> str: - rpath = sys._xoptions.get('bundle_exe_dir') + rpath = getattr(sys, 'kitty_run_data').get('bundle_exe_dir') if not rpath: items = os.environ.get('PATH', '').split(os.pathsep) + [os.path.join(kitty_base_dir, 'kitty', 'launcher')] seen: Set[str] = set() diff --git a/kitty/entry_points.py b/kitty/entry_points.py index 6351f177f..438e5d6b5 100644 --- a/kitty/entry_points.py +++ b/kitty/entry_points.py @@ -159,7 +159,7 @@ def setup_openssl_environment() -> None: # many systems come with no certificates in a useable form or have various # locations for the certificates. d = os.path.dirname - ext_dir: str = getattr(sys, 'kitty_extensions_dir') + ext_dir: str = getattr(sys, 'kitty_run_data')['extensions_dir'] if 'darwin' in sys.platform.lower(): cert_file = os.path.join(d(d(d(ext_dir))), 'cacert.pem') else: diff --git a/kitty/main.py b/kitty/main.py index 50afb0d47..d84b58f00 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -305,11 +305,12 @@ def prepend_if_not_present(path: str, paths_serialized: str) -> str: def ensure_kitty_in_path() -> None: # Ensure the correct kitty is in PATH - rpath = sys._xoptions.get('bundle_exe_dir') + krd = getattr(sys, 'kitty_run_data') + rpath = krd.get('bundle_exe_dir') if not rpath: return if rpath: - modify_path = is_macos or getattr(sys, 'frozen', False) or sys._xoptions.get('kitty_from_source') == '1' + modify_path = is_macos or getattr(sys, 'frozen', False) or krd.get('from_source') existing = shutil.which('kitty') if modify_path or not existing: env_path = os.environ.get('PATH', '') diff --git a/launcher.c b/launcher.c index ef395e127..48999c66c 100644 --- a/launcher.c +++ b/launcher.c @@ -48,44 +48,35 @@ typedef struct { const char *exe, *exe_dir, *lc_ctype, *lib_dir; char **argv; int argc; - wchar_t* xoptions[8]; - int num_xoptions; } RunData; - static bool -set_xoptions(RunData *run_data, bool from_source) { - wchar_t *exe_dir = Py_DecodeLocale(run_data->exe_dir, NULL); - if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", run_data->exe_dir); return false; } - size_t len = 32 + wcslen(exe_dir); - run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t)); - if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for bundle_exe_dir\n"); return false; } - swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"bundle_exe_dir=%ls", exe_dir); - PyMem_RawFree(exe_dir); +set_kitty_run_data(RunData *run_data, bool from_source, wchar_t *extensions_dir) { + PyObject *ans = PyDict_New(); + if (!ans) { PyErr_Print(); return false; } + PyObject *exe_dir = PyUnicode_DecodeFSDefaultAndSize(run_data->exe_dir, strlen(run_data->exe_dir)); + if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", run_data->exe_dir); PyErr_Print(); return false; } +#define S(key, val) { if (!val) { PyErr_Print(); return false; } int ret = PyDict_SetItemString(ans, #key, val); Py_CLEAR(val); if (ret != 0) { PyErr_Print(); return false; } } + S(bundle_exe_dir, exe_dir); if (from_source) { - len = 32; - run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t)); - if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for from_source\n"); return false; } - swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"kitty_from_source=1"); + PyObject *one = Py_True; Py_INCREF(one); + S(from_source, one); } if (run_data->lc_ctype) { - len = 32 + 4 * strlen(run_data->lc_ctype); - run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t)); - if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for lc_ctype\n"); return false; } - swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"lc_ctype_before_python=%s", run_data->lc_ctype); + PyObject *ctype = PyUnicode_DecodeLocaleAndSize(run_data->lc_ctype, strlen(run_data->lc_ctype), NULL); + S(lc_ctype_before_python, ctype); } + if (extensions_dir) { + PyObject *ed = PyUnicode_FromWideChar(extensions_dir, -1); + S(extensions_dir, ed); + } +#undef S + int ret = PySys_SetObject("kitty_run_data", ans); + Py_CLEAR(ans); + if (ret != 0) { PyErr_Print(); return false; } return true; } -static void -free_xoptions(RunData *run_data) { - if (run_data->num_xoptions > 0) { - while (run_data->num_xoptions--) { - free(run_data->xoptions[run_data->num_xoptions]); - run_data->xoptions[run_data->num_xoptions] = 0; - } - } -} #ifdef FOR_BUNDLE #include @@ -174,13 +165,10 @@ run_embedded(RunData *run_data) { if (!canonicalize_path_wide(python_home_full, python_home, num+1)) { fprintf(stderr, "Failed to canonicalize the path: %s\n", python_home_full); return 1; } - if (!set_xoptions(run_data, false)) return 1; - bypy_initialize_interpreter_with_xoptions( - L"kitty", python_home, L"kitty_main", extensions_dir, run_data->argc, run_data->argv, - run_data->num_xoptions, run_data->xoptions); - free_xoptions(run_data); + bypy_initialize_interpreter( + L"kitty", python_home, L"kitty_main", extensions_dir, run_data->argc, run_data->argv) + if (!set_kitty_run_data(run_data, false, extensions_dir)) return 1; set_sys_bool("frozen", true); - set_sys_string("kitty_extensions_dir", extensions_dir); return bypy_run_interpreter(); } @@ -210,13 +198,11 @@ run_embedded(RunData *run_data) { status = PyConfig_SetBytesString(&config, &config.run_filename, run_data->lib_dir); if (PyStatus_Exception(status)) goto fail; - if (!set_xoptions(run_data, from_source)) return 1; - status = PyConfig_SetWideStringList(&config, &config.xoptions, run_data->num_xoptions, run_data->xoptions); - free_xoptions(run_data); - if (PyStatus_Exception(status)) goto fail; status = Py_InitializeFromConfig(&config); if (PyStatus_Exception(status)) goto fail; PyConfig_Clear(&config); + if (!set_kitty_run_data(run_data, from_source, NULL)) return 1; + PySys_SetObject("frozen", Py_False); return Py_RunMain(); fail: PyConfig_Clear(&config); diff --git a/test.py b/test.py index 42e69a264..889ce07f3 100755 --- a/test.py +++ b/test.py @@ -36,6 +36,8 @@ def main() -> None: paths = os.environ.get('PATH', '/usr/local/sbin:/usr/local/bin:/usr/bin').split(os.pathsep) path = os.pathsep.join(x for x in paths if not x.startswith(current_home)) launcher_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kitty', 'launcher') + if not hasattr(sys, 'kitty_run_data'): + setattr(sys, 'kitty_run_data', {'bundle_exe_dir': launcher_dir, 'from_source': True}) path = f'{launcher_dir}{os.pathsep}{path}' with TemporaryDirectory() as tdir, env_vars( PYTHONWARNINGS='error', HOME=tdir, USERPROFILE=tdir, PATH=path,