Skip to content

Commit f023f04

Browse files
committed
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. python/cpython#28823 Fixes #5223
1 parent 5673359 commit f023f04

File tree

6 files changed

+33
-44
lines changed

6 files changed

+33
-44
lines changed

kitty/child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def __init__(
242242
def final_env(self) -> Dict[str, str]:
243243
from kitty.options.utils import DELETE_ENV_VAR
244244
env = default_env().copy()
245-
if is_macos and env.get('LC_CTYPE') == 'UTF-8' and not sys._xoptions.get(
245+
if is_macos and env.get('LC_CTYPE') == 'UTF-8' and not getattr(sys, 'kitty_run_data').get(
246246
'lc_ctype_before_python') and not getattr(default_env, 'lc_ctype_set_by_user', False):
247247
del env['LC_CTYPE']
248248
env.update(self.env)

kitty/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Version(NamedTuple):
2929
is_freebsd: bool = 'freebsd' in _plat
3030
is_running_from_develop: bool = False
3131
if getattr(sys, 'frozen', False):
32-
extensions_dir: str = getattr(sys, 'kitty_extensions_dir')
32+
extensions_dir: str = getattr(sys, 'kitty_run_data')['extensions_dir']
3333

3434
def get_frozen_base() -> str:
3535
global is_running_from_develop
@@ -61,7 +61,7 @@ def get_frozen_base() -> str:
6161

6262
@run_once
6363
def kitty_exe() -> str:
64-
rpath = sys._xoptions.get('bundle_exe_dir')
64+
rpath = getattr(sys, 'kitty_run_data').get('bundle_exe_dir')
6565
if not rpath:
6666
items = os.environ.get('PATH', '').split(os.pathsep) + [os.path.join(kitty_base_dir, 'kitty', 'launcher')]
6767
seen: Set[str] = set()

kitty/entry_points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def setup_openssl_environment() -> None:
159159
# many systems come with no certificates in a useable form or have various
160160
# locations for the certificates.
161161
d = os.path.dirname
162-
ext_dir: str = getattr(sys, 'kitty_extensions_dir')
162+
ext_dir: str = getattr(sys, 'kitty_run_data')['extensions_dir']
163163
if 'darwin' in sys.platform.lower():
164164
cert_file = os.path.join(d(d(d(ext_dir))), 'cacert.pem')
165165
else:

kitty/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,12 @@ def prepend_if_not_present(path: str, paths_serialized: str) -> str:
305305

306306
def ensure_kitty_in_path() -> None:
307307
# Ensure the correct kitty is in PATH
308-
rpath = sys._xoptions.get('bundle_exe_dir')
308+
krd = getattr(sys, 'kitty_run_data')
309+
rpath = krd.get('bundle_exe_dir')
309310
if not rpath:
310311
return
311312
if rpath:
312-
modify_path = is_macos or getattr(sys, 'frozen', False) or sys._xoptions.get('kitty_from_source') == '1'
313+
modify_path = is_macos or getattr(sys, 'frozen', False) or krd.get('from_source')
313314
existing = shutil.which('kitty')
314315
if modify_path or not existing:
315316
env_path = os.environ.get('PATH', '')

launcher.c

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,35 @@ typedef struct {
4848
const char *exe, *exe_dir, *lc_ctype, *lib_dir;
4949
char **argv;
5050
int argc;
51-
wchar_t* xoptions[8];
52-
int num_xoptions;
5351
} RunData;
5452

55-
5653
static bool
57-
set_xoptions(RunData *run_data, bool from_source) {
58-
wchar_t *exe_dir = Py_DecodeLocale(run_data->exe_dir, NULL);
59-
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", run_data->exe_dir); return false; }
60-
size_t len = 32 + wcslen(exe_dir);
61-
run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t));
62-
if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for bundle_exe_dir\n"); return false; }
63-
swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"bundle_exe_dir=%ls", exe_dir);
64-
PyMem_RawFree(exe_dir);
54+
set_kitty_run_data(RunData *run_data, bool from_source, wchar_t *extensions_dir) {
55+
PyObject *ans = PyDict_New();
56+
if (!ans) { PyErr_Print(); return false; }
57+
PyObject *exe_dir = PyUnicode_DecodeFSDefaultAndSize(run_data->exe_dir, strlen(run_data->exe_dir));
58+
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", run_data->exe_dir); PyErr_Print(); return false; }
59+
#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; } }
60+
S(bundle_exe_dir, exe_dir);
6561
if (from_source) {
66-
len = 32;
67-
run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t));
68-
if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for from_source\n"); return false; }
69-
swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"kitty_from_source=1");
62+
PyObject *one = Py_True; Py_INCREF(one);
63+
S(from_source, one);
7064
}
7165
if (run_data->lc_ctype) {
72-
len = 32 + 4 * strlen(run_data->lc_ctype);
73-
run_data->xoptions[run_data->num_xoptions] = calloc(len, sizeof(wchar_t));
74-
if (!run_data->xoptions[run_data->num_xoptions]) { fprintf(stderr, "Out of memory allocating for lc_ctype\n"); return false; }
75-
swprintf(run_data->xoptions[run_data->num_xoptions++], len, L"lc_ctype_before_python=%s", run_data->lc_ctype);
66+
PyObject *ctype = PyUnicode_DecodeLocaleAndSize(run_data->lc_ctype, strlen(run_data->lc_ctype), NULL);
67+
S(lc_ctype_before_python, ctype);
68+
}
69+
if (extensions_dir) {
70+
PyObject *ed = PyUnicode_FromWideChar(extensions_dir, -1);
71+
S(extensions_dir, ed);
7672
}
73+
#undef S
74+
int ret = PySys_SetObject("kitty_run_data", ans);
75+
Py_CLEAR(ans);
76+
if (ret != 0) { PyErr_Print(); return false; }
7777
return true;
7878
}
7979

80-
static void
81-
free_xoptions(RunData *run_data) {
82-
if (run_data->num_xoptions > 0) {
83-
while (run_data->num_xoptions--) {
84-
free(run_data->xoptions[run_data->num_xoptions]);
85-
run_data->xoptions[run_data->num_xoptions] = 0;
86-
}
87-
}
88-
}
8980

9081
#ifdef FOR_BUNDLE
9182
#include <bypy-freeze.h>
@@ -174,13 +165,10 @@ run_embedded(RunData *run_data) {
174165
if (!canonicalize_path_wide(python_home_full, python_home, num+1)) {
175166
fprintf(stderr, "Failed to canonicalize the path: %s\n", python_home_full); return 1; }
176167

177-
if (!set_xoptions(run_data, false)) return 1;
178-
bypy_initialize_interpreter_with_xoptions(
179-
L"kitty", python_home, L"kitty_main", extensions_dir, run_data->argc, run_data->argv,
180-
run_data->num_xoptions, run_data->xoptions);
181-
free_xoptions(run_data);
168+
bypy_initialize_interpreter(
169+
L"kitty", python_home, L"kitty_main", extensions_dir, run_data->argc, run_data->argv)
170+
if (!set_kitty_run_data(run_data, false, extensions_dir)) return 1;
182171
set_sys_bool("frozen", true);
183-
set_sys_string("kitty_extensions_dir", extensions_dir);
184172
return bypy_run_interpreter();
185173
}
186174

@@ -210,13 +198,11 @@ run_embedded(RunData *run_data) {
210198
status = PyConfig_SetBytesString(&config, &config.run_filename, run_data->lib_dir);
211199
if (PyStatus_Exception(status)) goto fail;
212200

213-
if (!set_xoptions(run_data, from_source)) return 1;
214-
status = PyConfig_SetWideStringList(&config, &config.xoptions, run_data->num_xoptions, run_data->xoptions);
215-
free_xoptions(run_data);
216-
if (PyStatus_Exception(status)) goto fail;
217201
status = Py_InitializeFromConfig(&config);
218202
if (PyStatus_Exception(status)) goto fail;
219203
PyConfig_Clear(&config);
204+
if (!set_kitty_run_data(run_data, from_source, NULL)) return 1;
205+
PySys_SetObject("frozen", Py_False);
220206
return Py_RunMain();
221207
fail:
222208
PyConfig_Clear(&config);

test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def main() -> None:
3636
paths = os.environ.get('PATH', '/usr/local/sbin:/usr/local/bin:/usr/bin').split(os.pathsep)
3737
path = os.pathsep.join(x for x in paths if not x.startswith(current_home))
3838
launcher_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kitty', 'launcher')
39+
if not hasattr(sys, 'kitty_run_data'):
40+
setattr(sys, 'kitty_run_data', {'bundle_exe_dir': launcher_dir, 'from_source': True})
3941
path = f'{launcher_dir}{os.pathsep}{path}'
4042
with TemporaryDirectory() as tdir, env_vars(
4143
PYTHONWARNINGS='error', HOME=tdir, USERPROFILE=tdir, PATH=path,

0 commit comments

Comments
 (0)