@@ -48,44 +48,35 @@ typedef struct {
48
48
const char * exe , * exe_dir , * lc_ctype , * lib_dir ;
49
49
char * * argv ;
50
50
int argc ;
51
- wchar_t * xoptions [8 ];
52
- int num_xoptions ;
53
51
} RunData ;
54
52
55
-
56
53
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 );
65
61
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 );
70
64
}
71
65
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 );
76
72
}
73
+ #undef S
74
+ int ret = PySys_SetObject ("kitty_run_data" , ans );
75
+ Py_CLEAR (ans );
76
+ if (ret != 0 ) { PyErr_Print (); return false; }
77
77
return true;
78
78
}
79
79
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
- }
89
80
90
81
#ifdef FOR_BUNDLE
91
82
#include <bypy-freeze.h>
@@ -174,13 +165,10 @@ run_embedded(RunData *run_data) {
174
165
if (!canonicalize_path_wide (python_home_full , python_home , num + 1 )) {
175
166
fprintf (stderr , "Failed to canonicalize the path: %s\n" , python_home_full ); return 1 ; }
176
167
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 ;
182
171
set_sys_bool ("frozen" , true);
183
- set_sys_string ("kitty_extensions_dir" , extensions_dir );
184
172
return bypy_run_interpreter ();
185
173
}
186
174
@@ -210,13 +198,11 @@ run_embedded(RunData *run_data) {
210
198
status = PyConfig_SetBytesString (& config , & config .run_filename , run_data -> lib_dir );
211
199
if (PyStatus_Exception (status )) goto fail ;
212
200
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 ;
217
201
status = Py_InitializeFromConfig (& config );
218
202
if (PyStatus_Exception (status )) goto fail ;
219
203
PyConfig_Clear (& config );
204
+ if (!set_kitty_run_data (run_data , from_source , NULL )) return 1 ;
205
+ PySys_SetObject ("frozen" , Py_False );
220
206
return Py_RunMain ();
221
207
fail :
222
208
PyConfig_Clear (& config );
0 commit comments