-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathpyi_python.c
More file actions
396 lines (339 loc) · 14.1 KB
/
Copy pathpyi_python.c
File metadata and controls
396 lines (339 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* ****************************************************************************
* Copyright (c) 2013-2023, PyInstaller Development Team.
*
* Distributed under the terms of the GNU General Public License (version 2
* or later) with exception for distributing the bootloader.
*
* The full license is in the file COPYING.txt, distributed with this software.
*
* SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
* ****************************************************************************
*/
/*
* Functions to load, initialize and launch Python interpreter.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* free() */
/* PyInstaller headers. */
#include "pyi_python.h"
#include "pyi_global.h"
#include "pyi_path.h"
#include "pyi_archive.h"
#include "pyi_main.h"
#include "pyi_utils.h"
#include "pyi_dylib_python.h"
#include "pyi_pyconfig.h"
/*
* Initialize and start python interpreter.
*/
int
pyi_python_start_interpreter(const struct PYI_CONTEXT *pyi_ctx)
{
const struct DYLIB_PYTHON *dylib_python = pyi_ctx->dylib_python;
struct PyiRuntimeOptions *runtime_options = NULL;
PyConfig *config_pep587 = NULL; /* Config structure used in PEP 587 codepath */
PyInitConfig *config_pep741 = NULL; /* Config structure used in PEP 741 codepath */
int ret = -1;
/* Read run-time options */
runtime_options = pyi_runtime_options_read(pyi_ctx);
if (runtime_options == NULL) {
PYI_ERROR("Failed to parse run-time options!\n");
goto end;
}
/* Pre-initialize python. This ensures that PEP 540 UTF-8 mode is enabled
* if necessary. */
PYI_DEBUG("LOADER: pre-initializing embedded python interpreter...\n");
if (pyi_pyconfig_preinit_python(runtime_options, pyi_ctx) < 0) {
PYI_ERROR("Failed to pre-initialize embedded python interpreter!\n");
goto end;
}
/* Set up python configuration. */
if (dylib_python->has_pep741) {
/* PEP 741 codepath */
PYI_DEBUG("LOADER: using PEP-741 API...\n");
/* Allocate the config structure and initialize it using default values
* of the Isolated Configuration. */
PYI_DEBUG("LOADER: creating PyInitConfig structure...\n");
config_pep741 = dylib_python->PyInitConfig_Create();
if (config_pep741 == NULL) {
PYI_ERROR("Failed to allocate PyInitConfig structure!\n");
goto end;
}
/* Set program name */
PYI_DEBUG("LOADER: setting program name...\n");
if (pyi_pyconfig_pep741_set_program_name(config_pep741, pyi_ctx) < 0) {
PYI_ERROR("Failed to set program name!\n");
goto end;
}
/* Set python home */
PYI_DEBUG("LOADER: setting python home path...\n");
if (pyi_pyconfig_pep741_set_python_home(config_pep741, pyi_ctx) < 0) {
PYI_ERROR("Failed to set python home path!\n");
goto end;
}
/* Set module search paths */
PYI_DEBUG("LOADER: setting module search paths...\n");
if (pyi_pyconfig_pep741_set_module_search_paths(config_pep741, pyi_ctx) < 0) {
PYI_ERROR("Failed to set module search paths!\n");
goto end;
}
/* Set arguments (sys.argv) */
PYI_DEBUG("LOADER: setting sys.argv...\n");
if (pyi_pyconfig_pep741_set_argv(config_pep741, pyi_ctx) < 0) {
PYI_ERROR("Failed to set sys.argv!\n");
goto end;
}
/* Apply run-time options */
PYI_DEBUG("LOADER: applying run-time options...\n");
if (pyi_pyconfig_pep741_set_runtime_options(config_pep741, pyi_ctx, runtime_options) < 0) {
PYI_ERROR("Failed to set run-time options!\n");
goto end;
}
} else {
/* PEP 587 codepath */
PYI_DEBUG("LOADER: using PEP-587 API...\n");
/* Allocate the config structure. Since underlying layout is specific to
* python version, this also verifies that python version is supported. */
PYI_DEBUG("LOADER: creating PyConfig structure...\n");
config_pep587 = pyi_pyconfig_pep587_create(pyi_ctx);
if (config_pep587 == NULL) {
PYI_ERROR("Failed to allocate PyConfig structure! Unsupported python version?\n");
goto end;
}
/* Initialize isolated configuration */
PYI_DEBUG("LOADER: initializing interpreter configuration...\n");
dylib_python->PyConfig_InitIsolatedConfig(config_pep587);
/* Set program name */
PYI_DEBUG("LOADER: setting program name...\n");
if (pyi_pyconfig_pep587_set_program_name(config_pep587, pyi_ctx) < 0) {
PYI_ERROR("Failed to set program name!\n");
goto end;
}
/* Set python home */
PYI_DEBUG("LOADER: setting python home path...\n");
if (pyi_pyconfig_pep587_set_python_home(config_pep587, pyi_ctx) < 0) {
PYI_ERROR("Failed to set python home path!\n");
goto end;
}
/* Set module search paths */
PYI_DEBUG("LOADER: setting module search paths...\n");
if (pyi_pyconfig_pep587_set_module_search_paths(config_pep587, pyi_ctx) < 0) {
PYI_ERROR("Failed to set module search paths!\n");
goto end;
}
/* Set arguments (sys.argv) */
PYI_DEBUG("LOADER: setting sys.argv...\n");
if (pyi_pyconfig_pep587_set_argv(config_pep587, pyi_ctx) < 0) {
PYI_ERROR("Failed to set sys.argv!\n");
goto end;
}
/* Apply run-time options */
PYI_DEBUG("LOADER: applying run-time options...\n");
if (pyi_pyconfig_pep587_set_runtime_options(config_pep587, pyi_ctx, runtime_options) < 0) {
PYI_ERROR("Failed to set run-time options!\n");
goto end;
}
}
/* Start the interpreter */
PYI_DEBUG("LOADER: starting embedded python interpreter...\n");
/* In unbuffered mode, flush stdout/stderr before python configuration
* removes the buffer (changing the buffer should probably flush the
* old buffer, but just in case do it manually...) */
if (runtime_options->unbuffered) {
fflush(stdout);
fflush(stderr);
}
if (dylib_python->has_pep741) {
/* PEP 741 codepath */
ret = dylib_python->Py_InitializeFromInitConfig(config_pep741);
if (ret < 0) {
const char *error_message = NULL;
dylib_python->PyInitConfig_GetError(config_pep741, &error_message);
PYI_ERROR("Failed to start embedded python interpreter: %s\n", error_message);
ret = -1;
}
} else {
/* PEP 587 codepath */
PyStatus status;
status = dylib_python->Py_InitializeFromConfig(config_pep587);
if (dylib_python->PyStatus_Exception(status)) {
PYI_ERROR("Failed to start embedded python interpreter!\n");
/* Dump exception information to stderr and exit the process with error code.
*
* Depending on the error type, Py_ExitStatusException() either
* ends up calling exit() or abort(). On Windows, the latter case
* triggers the error reporting service and pops up a dialog with
* "Close program", "Check for a solution", and "Debug" (if Visual
* Studio is installed). Disable this behavior via SetErrorMode() */
#if defined(_WIN32)
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
#endif
dylib_python->Py_ExitStatusException(status);
/* Not reachable; Py_ExitStatusException() either calls exit() or
* abort(). */
} else {
ret = 0; /* Succeeded */
}
}
end:
if (dylib_python->has_pep741) {
/* PEP 741 codepath */
dylib_python->PyInitConfig_Free(config_pep741);
} else {
/* PEP 587 codepath */
pyi_pyconfig_pep587_free(config_pep587, pyi_ctx);
}
pyi_runtime_options_free(runtime_options);
return ret;
}
/*
* Import (bootstrap) modules embedded in the PKG archive.
*/
int
pyi_python_import_modules(const struct PYI_CONTEXT *pyi_ctx)
{
const struct DYLIB_PYTHON *dylib_python = pyi_ctx->dylib_python;
const struct ARCHIVE *archive = pyi_ctx->archive;
const struct TOC_ENTRY *toc_entry;
unsigned char *data;
PyObject *co;
PyObject *mod;
PyObject *meipass_obj;
PYI_DEBUG("LOADER: setting sys._MEIPASS\n");
#ifdef _WIN32
meipass_obj = dylib_python->PyUnicode_Decode(pyi_ctx->application_home_dir, strlen(pyi_ctx->application_home_dir), "utf-8", "strict");
#else
meipass_obj = dylib_python->PyUnicode_DecodeFSDefault(pyi_ctx->application_home_dir);
#endif
if (!meipass_obj) {
PYI_ERROR("Failed to get _MEIPASS as PyObject.\n");
return -1;
}
dylib_python->PySys_SetObject("_MEIPASS", meipass_obj);
PYI_DEBUG("LOADER: importing modules from PKG/CArchive\n");
/* Iterate through toc looking for module entries (type 'm')
* this is normally just bootstrap stuff (archive and iu) */
for (toc_entry = archive->toc; toc_entry < archive->toc_end; toc_entry = pyi_archive_next_toc_entry(archive, toc_entry)) {
if (toc_entry->typecode != ARCHIVE_ITEM_PYMODULE && toc_entry->typecode != ARCHIVE_ITEM_PYPACKAGE) {
continue;
}
data = pyi_archive_extract(archive, toc_entry);
PYI_DEBUG("LOADER: extracted %s\n", toc_entry->name);
/* Unmarshal the stored code object */
co = dylib_python->PyMarshal_ReadObjectFromString((const char *)data, toc_entry->uncompressed_length);
free(data);
if (co == NULL) {
PYI_ERROR("Failed to unmarshal code object for module %s!\n", toc_entry->name);
mod = NULL;
} else {
PYI_DEBUG("LOADER: running unmarshalled code object for module %s...\n", toc_entry->name);
mod = dylib_python->PyImport_ExecCodeModule(toc_entry->name, co);
if (mod == NULL) {
PYI_ERROR("Module object for %s is NULL!\n", toc_entry->name);
}
}
if (dylib_python->PyErr_Occurred()) {
dylib_python->PyErr_Print();
dylib_python->PyErr_Clear();
}
/* Exit on error */
if (mod == NULL) {
return -1;
}
}
return 0;
}
/*
* Store path and offset to PYZ archive into sys._pyinstaller_pyz
* attribute, so that our bootstrap python script can set up PYZ
* archive reader.
*/
int
pyi_python_install_pyz(const struct PYI_CONTEXT *pyi_ctx)
{
const struct DYLIB_PYTHON *dylib_python = pyi_ctx->dylib_python;
const struct ARCHIVE *archive = pyi_ctx->archive;
const struct TOC_ENTRY *toc_entry;
PyObject *archive_filename_obj;
PyObject *pyz_path_obj;
unsigned long long pyz_offset;
int rc;
const char *attr_name = "_pyinstaller_pyz";
PYI_DEBUG("LOADER: looking for PYZ archive TOC entry...\n");
/* Iterate through TOC and look for PYZ entry (type 'z') */
for (toc_entry = archive->toc; toc_entry < archive->toc_end; toc_entry = pyi_archive_next_toc_entry(archive, toc_entry)) {
if (toc_entry->typecode == ARCHIVE_ITEM_PYZ) {
break;
}
}
if (toc_entry >= archive->toc_end) {
PYI_ERROR("PYZ archive entry not found in the TOC!\n");
return -1;
}
/* Store archive filename as Python string. */
#ifdef _WIN32
/* Decode UTF-8 to PyUnicode */
archive_filename_obj = dylib_python->PyUnicode_Decode(pyi_ctx->archive_filename, strlen(pyi_ctx->archive_filename), "utf-8", "strict");
#else
/* Decode locale-encoded filename to PyUnicode object using Python's
* preferred decoding method for filenames. */
archive_filename_obj = dylib_python->PyUnicode_DecodeFSDefault(pyi_ctx->archive_filename);
#endif
/* Format name plus offset; here, we assume that python's %llu format
* matches the platform's definition of "unsigned long long". Of
* which we actually have no guarantee, but thankfully that does
* seem to be the case. */
pyz_offset = pyi_ctx->archive->pkg_offset + toc_entry->offset;
pyz_path_obj = dylib_python->PyUnicode_FromFormat("%U?%llu", archive_filename_obj, pyz_offset);
dylib_python->Py_DecRef(archive_filename_obj);
if (pyz_path_obj == NULL) {
PYI_ERROR("Failed to format PYZ archive path and offset\n");
return -1;
}
/* Store into sys._pyinstaller_pyz */
rc = dylib_python->PySys_SetObject(attr_name, pyz_path_obj);
dylib_python->Py_DecRef(pyz_path_obj);
if (rc != 0) {
PYI_ERROR("Failed to store path to PYZ archive into sys.%s!\n", attr_name);
return -1;
}
PYI_DEBUG("LOADER: path to PYZ archive stored into sys.%s...\n", attr_name);
return 0;
}
void
pyi_python_finalize(const struct PYI_CONTEXT *pyi_ctx)
{
const struct DYLIB_PYTHON *dylib_python = pyi_ctx->dylib_python;
/* Ensure python library was loaded; otherwise PI_* function pointers
* are invalid, and we have nothing to do here. */
if (!dylib_python) {
return;
}
/* Nothing to do if python interpreter was not initialized. Attempting
* to flush streams using PyRun_SimpleStringFlags requires a valid
* interpreter instance. */
if (dylib_python->Py_IsInitialized() == 0) {
return;
}
#ifndef WINDOWED
/* We need to manually flush the buffers because otherwise there can be errors.
* The native python interpreter flushes buffers before calling Py_Finalize,
* so we need to manually do the same. See isse #4908. */
PYI_DEBUG("LOADER: manually flushing stdout and stderr...\n");
/* sys.stdout.flush() */
dylib_python->PyRun_SimpleStringFlags(
"import sys; sys.stdout.flush(); \
(sys.__stdout__.flush if sys.__stdout__ \
is not sys.stdout else (lambda: None))()", NULL);
/* sys.stderr.flush() */
dylib_python->PyRun_SimpleStringFlags(
"import sys; sys.stderr.flush(); \
(sys.__stderr__.flush if sys.__stderr__ \
is not sys.stderr else (lambda: None))()", NULL);
#endif
/* Finalize the interpreter. This calls all of the atexit functions. */
PYI_DEBUG("LOADER: cleaning up Python interpreter...\n");
dylib_python->Py_Finalize();
}