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]:
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)

View File

@ -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()

View File

@ -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:

View File

@ -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', '')

View File

@ -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 <bypy-freeze.h>
@ -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);

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)
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,