Workaround for Python 3.11 breaking sys._xoptions

Apparently in Python-land its acceptable behavior to break backward
compatibility with documented interfaces on a whim. Bloody joke.
https://github.com/python/cpython/pull/28823

Fixes #5223
This commit is contained in:
Kovid Goyal 2022-06-23 08:44:34 +05:30
parent 5673359be2
commit f023f047ff
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 33 additions and 44 deletions

View File

@ -242,7 +242,7 @@ class Child:
def final_env(self) -> Dict[str, str]: def final_env(self) -> Dict[str, str]:
from kitty.options.utils import DELETE_ENV_VAR from kitty.options.utils import DELETE_ENV_VAR
env = default_env().copy() 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): 'lc_ctype_before_python') and not getattr(default_env, 'lc_ctype_set_by_user', False):
del env['LC_CTYPE'] del env['LC_CTYPE']
env.update(self.env) env.update(self.env)

View File

@ -29,7 +29,7 @@ is_macos: bool = 'darwin' in _plat
is_freebsd: bool = 'freebsd' in _plat is_freebsd: bool = 'freebsd' in _plat
is_running_from_develop: bool = False is_running_from_develop: bool = False
if getattr(sys, 'frozen', 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: def get_frozen_base() -> str:
global is_running_from_develop global is_running_from_develop
@ -61,7 +61,7 @@ else:
@run_once @run_once
def kitty_exe() -> str: def kitty_exe() -> str:
rpath = sys._xoptions.get('bundle_exe_dir') rpath = getattr(sys, 'kitty_run_data').get('bundle_exe_dir')
if not rpath: if not rpath:
items = os.environ.get('PATH', '').split(os.pathsep) + [os.path.join(kitty_base_dir, 'kitty', 'launcher')] items = os.environ.get('PATH', '').split(os.pathsep) + [os.path.join(kitty_base_dir, 'kitty', 'launcher')]
seen: Set[str] = set() seen: Set[str] = set()

View File

@ -159,7 +159,7 @@ def setup_openssl_environment() -> None:
# many systems come with no certificates in a useable form or have various # many systems come with no certificates in a useable form or have various
# locations for the certificates. # locations for the certificates.
d = os.path.dirname 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(): if 'darwin' in sys.platform.lower():
cert_file = os.path.join(d(d(d(ext_dir))), 'cacert.pem') cert_file = os.path.join(d(d(d(ext_dir))), 'cacert.pem')
else: else:

View File

@ -305,11 +305,12 @@ def prepend_if_not_present(path: str, paths_serialized: str) -> str:
def ensure_kitty_in_path() -> None: def ensure_kitty_in_path() -> None:
# Ensure the correct kitty is in PATH # 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: if not rpath:
return return
if rpath: 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') existing = shutil.which('kitty')
if modify_path or not existing: if modify_path or not existing:
env_path = os.environ.get('PATH', '') env_path = os.environ.get('PATH', '')

View File

@ -48,44 +48,35 @@ typedef struct {
const char *exe, *exe_dir, *lc_ctype, *lib_dir; const char *exe, *exe_dir, *lc_ctype, *lib_dir;
char **argv; char **argv;
int argc; int argc;
wchar_t* xoptions[8];
int num_xoptions;
} RunData; } RunData;
static bool static bool
set_xoptions(RunData *run_data, bool from_source) { set_kitty_run_data(RunData *run_data, bool from_source, wchar_t *extensions_dir) {
wchar_t *exe_dir = Py_DecodeLocale(run_data->exe_dir, NULL); PyObject *ans = PyDict_New();
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", run_data->exe_dir); return false; } if (!ans) { PyErr_Print(); return false; }
size_t len = 32 + wcslen(exe_dir); PyObject *exe_dir = PyUnicode_DecodeFSDefaultAndSize(run_data->exe_dir, strlen(run_data->exe_dir));
run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t)); if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", run_data->exe_dir); PyErr_Print(); return false; }
if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for bundle_exe_dir\n"); 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; } }
swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"bundle_exe_dir=%ls", exe_dir); S(bundle_exe_dir, exe_dir);
PyMem_RawFree(exe_dir);
if (from_source) { if (from_source) {
len = 32; PyObject *one = Py_True; Py_INCREF(one);
run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t)); S(from_source, one);
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");
} }
if (run_data->lc_ctype) { if (run_data->lc_ctype) {
len = 32 + 4 * strlen(run_data->lc_ctype); PyObject *ctype = PyUnicode_DecodeLocaleAndSize(run_data->lc_ctype, strlen(run_data->lc_ctype), NULL);
run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t)); S(lc_ctype_before_python, ctype);
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);
} }
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; 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 #ifdef FOR_BUNDLE
#include <bypy-freeze.h> #include <bypy-freeze.h>
@ -174,13 +165,10 @@ run_embedded(RunData *run_data) {
if (!canonicalize_path_wide(python_home_full, python_home, num+1)) { 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; } 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(
bypy_initialize_interpreter_with_xoptions( L"kitty", python_home, L"kitty_main", extensions_dir, run_data->argc, run_data->argv)
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;
run_data->num_xoptions, run_data->xoptions);
free_xoptions(run_data);
set_sys_bool("frozen", true); set_sys_bool("frozen", true);
set_sys_string("kitty_extensions_dir", extensions_dir);
return bypy_run_interpreter(); return bypy_run_interpreter();
} }
@ -210,13 +198,11 @@ run_embedded(RunData *run_data) {
status = PyConfig_SetBytesString(&config, &config.run_filename, run_data->lib_dir); status = PyConfig_SetBytesString(&config, &config.run_filename, run_data->lib_dir);
if (PyStatus_Exception(status)) goto fail; 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); status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) goto fail; if (PyStatus_Exception(status)) goto fail;
PyConfig_Clear(&config); PyConfig_Clear(&config);
if (!set_kitty_run_data(run_data, from_source, NULL)) return 1;
PySys_SetObject("frozen", Py_False);
return Py_RunMain(); return Py_RunMain();
fail: fail:
PyConfig_Clear(&config); PyConfig_Clear(&config);

View File

@ -36,6 +36,8 @@ def main() -> None:
paths = os.environ.get('PATH', '/usr/local/sbin:/usr/local/bin:/usr/bin').split(os.pathsep) 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)) 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') 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}' path = f'{launcher_dir}{os.pathsep}{path}'
with TemporaryDirectory() as tdir, env_vars( with TemporaryDirectory() as tdir, env_vars(
PYTHONWARNINGS='error', HOME=tdir, USERPROFILE=tdir, PATH=path, PYTHONWARNINGS='error', HOME=tdir, USERPROFILE=tdir, PATH=path,