blob: 08fb0e0417d03823e10ac3bf8e1968d8f3dc70ff [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Victor Stinnerf684d832019-03-01 03:44:13 +01004#include "pycore_coreconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pylifecycle.h"
6#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00008
Victor Stinner6efcb6d2017-12-18 23:42:55 +01009#ifdef __FreeBSD__
Victor Stinner95e2cbf2019-03-01 16:25:19 +010010# include <fenv.h> /* fedisableexcept() */
Martin v. Löwis945362c2007-08-30 14:57:25 +000011#endif
12
Victor Stinner95e2cbf2019-03-01 16:25:19 +010013/* Includes for exit_sigint() */
14#include <stdio.h> /* perror() */
15#ifdef HAVE_SIGNAL_H
16# include <signal.h> /* SIGINT */
Guido van Rossuma075ce11997-12-05 21:56:45 +000017#endif
Victor Stinner95e2cbf2019-03-01 16:25:19 +010018#if defined(HAVE_GETPID) && defined(HAVE_UNISTD_H)
19# include <unistd.h> /* getpid() */
20#endif
Erik Janssens925af1d2019-05-21 12:11:11 +020021#ifdef MS_WINDOWS
22# include <windows.h> /* STATUS_CONTROL_C_EXIT */
Victor Stinner95e2cbf2019-03-01 16:25:19 +010023#endif
24/* End of includes for exit_sigint() */
Guido van Rossuma075ce11997-12-05 21:56:45 +000025
Guido van Rossuma22865e2000-09-05 04:41:18 +000026#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000027 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
28 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000029
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030#ifdef __cplusplus
31extern "C" {
32#endif
33
Victor Stinner95e2cbf2019-03-01 16:25:19 +010034/* --- pymain_init() ---------------------------------------------- */
35
Victor Stinner6dcb5422019-03-05 02:44:12 +010036static _PyInitError
Victor Stinner5ac27a52019-03-27 13:40:14 +010037pymain_init(const _PyArgv *args)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010038{
39 _PyInitError err;
40
41 err = _PyRuntime_Initialize();
42 if (_Py_INIT_FAILED(err)) {
43 return err;
44 }
45
46 /* 754 requires that FP exceptions run in "no stop" mode by default,
47 * and until C vendors implement C99's ways to control FP exceptions,
48 * Python requires non-stop mode. Alas, some platforms enable FP
49 * exceptions by default. Here we disable them.
50 */
51#ifdef __FreeBSD__
52 fedisableexcept(FE_OVERFLOW);
53#endif
54
Victor Stinnercab5d072019-05-17 19:01:14 +020055 _PyPreConfig preconfig;
56 _PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner70005ac2019-05-02 15:25:34 -040057 err = _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinner6dcb5422019-03-05 02:44:12 +010058 if (_Py_INIT_FAILED(err)) {
Victor Stinnerf72346c2019-03-25 17:54:58 +010059 return err;
Victor Stinner6dcb5422019-03-05 02:44:12 +010060 }
61
Victor Stinnercab5d072019-05-17 19:01:14 +020062 _PyCoreConfig config;
63 err = _PyCoreConfig_InitPythonConfig(&config);
64 if (_Py_INIT_FAILED(err)) {
65 return err;
66 }
67
Victor Stinnerd929f182019-03-27 18:28:46 +010068 /* pass NULL as the config: config is read from command line arguments,
69 environment variables, configuration files */
Victor Stinner5ac27a52019-03-27 13:40:14 +010070 if (args->use_bytes_argv) {
Victor Stinnercab5d072019-05-17 19:01:14 +020071 return _Py_InitializeFromArgs(&config,
72 args->argc, args->bytes_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010073 }
Victor Stinner5ac27a52019-03-27 13:40:14 +010074 else {
Victor Stinnercab5d072019-05-17 19:01:14 +020075 return _Py_InitializeFromWideArgs(&config,
76 args->argc, args->wchar_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010077 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +010078}
79
80
81/* --- pymain_run_python() ---------------------------------------- */
82
83/* Non-zero if filename, command (-c) or module (-m) is set
84 on the command line */
85#define RUN_CODE(config) \
86 (config->run_command != NULL || config->run_filename != NULL \
87 || config->run_module != NULL)
88
89/* Return non-zero is stdin is a TTY or if -i command line option is used */
90static int
91stdin_is_interactive(const _PyCoreConfig *config)
92{
93 return (isatty(fileno(stdin)) || config->interactive);
94}
95
96
Victor Stinner12083282019-05-17 23:05:29 +020097/* Display the current Python exception and return an exitcode */
98static int
99pymain_err_print(int *exitcode_p)
100{
101 int exitcode;
102 if (_Py_HandleSystemExit(&exitcode)) {
103 *exitcode_p = exitcode;
104 return 1;
105 }
106
107 PyErr_Print();
108 return 0;
109}
110
111
112static int
113pymain_exit_err_print(void)
114{
115 int exitcode = 1;
116 pymain_err_print(&exitcode);
117 return exitcode;
118}
119
120
121/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
122 Return 0 otherwise. */
123static int
124pymain_get_importer(const wchar_t *filename, PyObject **importer_p, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100125{
126 PyObject *sys_path0 = NULL, *importer;
127
128 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
129 if (sys_path0 == NULL) {
130 goto error;
131 }
132
133 importer = PyImport_GetImporter(sys_path0);
134 if (importer == NULL) {
135 goto error;
136 }
137
138 if (importer == Py_None) {
139 Py_DECREF(sys_path0);
140 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200141 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100142 }
143
144 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200145 *importer_p = sys_path0;
146 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100147
148error:
149 Py_XDECREF(sys_path0);
Victor Stinner12083282019-05-17 23:05:29 +0200150
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100151 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
Victor Stinner12083282019-05-17 23:05:29 +0200152 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100153}
154
155
156static int
157pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
158{
159 _Py_IDENTIFIER(path);
160 PyObject *sys_path;
161 PyObject *sysdict = interp->sysdict;
162 if (sysdict != NULL) {
163 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
164 if (sys_path == NULL && PyErr_Occurred()) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200165 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100166 }
167 }
168 else {
169 sys_path = NULL;
170 }
171 if (sys_path == NULL) {
172 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200173 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100174 }
175
176 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200177 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100178 }
179 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100180}
181
182
183static void
184pymain_header(const _PyCoreConfig *config)
185{
186 if (config->quiet) {
187 return;
188 }
189
190 if (!config->verbose && (RUN_CODE(config) || !stdin_is_interactive(config))) {
191 return;
192 }
193
194 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
195 if (config->site_import) {
196 fprintf(stderr, "%s\n", COPYRIGHT);
197 }
198}
199
200
201static void
202pymain_import_readline(const _PyCoreConfig *config)
203{
Victor Stinner20004952019-03-26 02:31:11 +0100204 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100205 return;
206 }
207 if (!config->inspect && RUN_CODE(config)) {
208 return;
209 }
210 if (!isatty(fileno(stdin))) {
211 return;
212 }
213
214 PyObject *mod = PyImport_ImportModule("readline");
215 if (mod == NULL) {
216 PyErr_Clear();
217 }
218 else {
219 Py_DECREF(mod);
220 }
221}
222
223
224static int
225pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
226{
227 PyObject *unicode, *bytes;
228 int ret;
229
230 unicode = PyUnicode_FromWideChar(command, -1);
231 if (unicode == NULL) {
232 goto error;
233 }
234
235 bytes = PyUnicode_AsUTF8String(unicode);
236 Py_DECREF(unicode);
237 if (bytes == NULL) {
238 goto error;
239 }
240
241 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
242 Py_DECREF(bytes);
243 return (ret != 0);
244
245error:
246 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200247 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100248}
249
250
251static int
252pymain_run_module(const wchar_t *modname, int set_argv0)
253{
254 PyObject *module, *runpy, *runmodule, *runargs, *result;
255 runpy = PyImport_ImportModule("runpy");
256 if (runpy == NULL) {
257 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200258 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100259 }
260 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
261 if (runmodule == NULL) {
262 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100263 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200264 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100265 }
266 module = PyUnicode_FromWideChar(modname, wcslen(modname));
267 if (module == NULL) {
268 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100269 Py_DECREF(runpy);
270 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200271 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100272 }
273 runargs = Py_BuildValue("(Oi)", module, set_argv0);
274 if (runargs == NULL) {
275 fprintf(stderr,
276 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100277 Py_DECREF(runpy);
278 Py_DECREF(runmodule);
279 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200280 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100281 }
282 result = PyObject_Call(runmodule, runargs, NULL);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100283 Py_DECREF(runpy);
284 Py_DECREF(runmodule);
285 Py_DECREF(module);
286 Py_DECREF(runargs);
287 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200288 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100289 }
290 Py_DECREF(result);
291 return 0;
292}
293
294
295static int
296pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf)
297{
298 const wchar_t *filename = config->run_filename;
Inada Naoki10654c12019-04-01 18:35:20 +0900299 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100300 if (fp == NULL) {
301 char *cfilename_buffer;
302 const char *cfilename;
303 int err = errno;
304 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
305 if (cfilename_buffer != NULL)
306 cfilename = cfilename_buffer;
307 else
308 cfilename = "<unprintable file name>";
309 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200310 config->program_name, cfilename, err, strerror(err));
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100311 PyMem_RawFree(cfilename_buffer);
312 return 2;
313 }
314
315 if (config->skip_source_first_line) {
316 int ch;
317 /* Push back first newline so line numbers remain the same */
318 while ((ch = getc(fp)) != EOF) {
319 if (ch == '\n') {
320 (void)ungetc(ch, fp);
321 break;
322 }
323 }
324 }
325
326 struct _Py_stat_struct sb;
327 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
328 fprintf(stderr,
329 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200330 config->program_name, filename);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100331 fclose(fp);
332 return 1;
333 }
334
335 /* call pending calls like signal handlers (SIGINT) */
336 if (Py_MakePendingCalls() == -1) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100337 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200338 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100339 }
340
341 PyObject *unicode, *bytes = NULL;
342 const char *filename_str;
343
344 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
345 if (unicode != NULL) {
346 bytes = PyUnicode_EncodeFSDefault(unicode);
347 Py_DECREF(unicode);
348 }
349 if (bytes != NULL) {
350 filename_str = PyBytes_AsString(bytes);
351 }
352 else {
353 PyErr_Clear();
354 filename_str = "<filename encoding error>";
355 }
356
357 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
358 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
359 Py_XDECREF(bytes);
360 return (run != 0);
361}
362
363
Victor Stinner12083282019-05-17 23:05:29 +0200364static int
365pymain_run_startup(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100366{
Victor Stinner20004952019-03-26 02:31:11 +0100367 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100368 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200369 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100370 }
371
372 FILE *fp = _Py_fopen(startup, "r");
373 if (fp == NULL) {
374 int save_errno = errno;
375 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100376
Victor Stinner12083282019-05-17 23:05:29 +0200377 errno = save_errno;
378 PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
379
380 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100381 }
382
383 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
384 PyErr_Clear();
385 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200386 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100387}
388
389
Victor Stinner12083282019-05-17 23:05:29 +0200390/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
391 Return 0 otherwise. */
392static int
393pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100394{
395 PyObject *sys, *hook, *result;
396 sys = PyImport_ImportModule("sys");
397 if (sys == NULL) {
398 goto error;
399 }
400
401 hook = PyObject_GetAttrString(sys, "__interactivehook__");
402 Py_DECREF(sys);
403 if (hook == NULL) {
404 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200405 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100406 }
407
408 result = _PyObject_CallNoArg(hook);
409 Py_DECREF(hook);
410 if (result == NULL) {
411 goto error;
412 }
413 Py_DECREF(result);
414
Victor Stinner12083282019-05-17 23:05:29 +0200415 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100416
417error:
418 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200419 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100420}
421
422
423static int
424pymain_run_stdin(_PyCoreConfig *config, PyCompilerFlags *cf)
425{
426 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100427 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200428 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200429
430 int exitcode;
431 if (pymain_run_startup(config, cf, &exitcode)) {
432 return exitcode;
433 }
434
435 if (pymain_run_interactive_hook(&exitcode)) {
436 return exitcode;
437 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100438 }
439
440 /* call pending calls like signal handlers (SIGINT) */
441 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200442 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100443 }
444
445 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
446 return (run != 0);
447}
448
449
450static void
451pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
452{
453 /* Check this environment variable at the end, to give programs the
454 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200455 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100456 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200457 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100458 }
459
Victor Stinnerc96be812019-05-14 17:34:56 +0200460 if (!(config->inspect && stdin_is_interactive(config) && RUN_CODE(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100461 return;
462 }
463
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100464 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200465 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200466 if (pymain_run_interactive_hook(exitcode)) {
467 return;
468 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100469
470 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
471 *exitcode = (res != 0);
472}
473
474
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200475static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100476pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100477{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100478 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
479 /* pymain_run_stdin() modify the config */
Victor Stinnerd3b19192018-07-25 10:21:03 +0200480 _PyCoreConfig *config = &interp->core_config;
481
482 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100483 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100484 /* If filename is a package (ex: directory or ZIP file) which contains
485 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200486 prepended to sys.path.
487
Victor Stinner12083282019-05-17 23:05:29 +0200488 Otherwise, main_importer_path is left unchanged. */
489 if (pymain_get_importer(config->run_filename, &main_importer_path,
490 exitcode)) {
491 return;
492 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100493 }
494
Victor Stinnerd3b19192018-07-25 10:21:03 +0200495 if (main_importer_path != NULL) {
496 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200497 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200498 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100499 }
Victor Stinner20004952019-03-26 02:31:11 +0100500 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100501 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200502 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
503 if (res < 0) {
504 goto error;
505 }
Victor Stinner19760862017-12-20 01:41:59 +0100506
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200507 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100508 if (pymain_sys_path_add_path0(interp, path0) < 0) {
509 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200510 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100511 }
Victor Stinner19760862017-12-20 01:41:59 +0100512 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100513 }
Victor Stinner19760862017-12-20 01:41:59 +0100514 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800515
Guido van Rossum495da292019-03-07 12:38:08 -0800516 PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION};
Victor Stinnera7368ac2017-11-15 18:11:45 -0800517
Victor Stinner62be7632019-03-01 13:10:14 +0100518 pymain_header(config);
519 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800520
Victor Stinner62be7632019-03-01 13:10:14 +0100521 if (config->run_command) {
522 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800523 }
Victor Stinner62be7632019-03-01 13:10:14 +0100524 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200525 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800526 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200527 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200528 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200529 }
Victor Stinner62be7632019-03-01 13:10:14 +0100530 else if (config->run_filename != NULL) {
531 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200532 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800533 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100534 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800535 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100536
Victor Stinner62be7632019-03-01 13:10:14 +0100537 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200538 goto done;
539
540error:
Victor Stinner12083282019-05-17 23:05:29 +0200541 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200542
543done:
544 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800545}
546
Victor Stinnera7368ac2017-11-15 18:11:45 -0800547
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100548/* --- pymain_main() ---------------------------------------------- */
549
550static void
551pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800552{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100553 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200554
555 /* Free global variables which cannot be freed in Py_Finalize():
556 configuration options set before Py_Initialize() which should
557 remain valid after Py_Finalize(), since
558 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100559 _PyPathConfig_ClearGlobal();
560 _Py_ClearStandardStreamEncoding();
561 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100562 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100563}
Victor Stinner94540602017-12-16 04:54:22 +0100564
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200565
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100566static int
567exit_sigint(void)
568{
569 /* bpo-1054041: We need to exit via the
570 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
571 * If we don't, a calling process such as a shell may not know
572 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
573#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
574 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
575 perror("signal"); /* Impossible in normal environments. */
576 } else {
577 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100578 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100579 /* If setting SIG_DFL failed, or kill failed to terminate us,
580 * there isn't much else we can do aside from an error code. */
581#endif /* HAVE_GETPID && !MS_WINDOWS */
582#ifdef MS_WINDOWS
583 /* cmd.exe detects this, prints ^C, and offers to terminate. */
584 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
585 return STATUS_CONTROL_C_EXIT;
586#else
587 return SIGINT + 128;
588#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200589}
590
591
Victor Stinner2f549082019-03-29 15:13:46 +0100592static void _Py_NO_RETURN
593pymain_exit_error(_PyInitError err)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200594{
Victor Stinnerdb719752019-05-01 05:35:33 +0200595 if (_Py_INIT_IS_EXIT(err)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200596 /* If it's an error rather than a regular exit, leave Python runtime
597 alive: _Py_ExitInitError() uses the current exception and use
598 sys.stdout in this case. */
599 pymain_free();
600 }
Victor Stinner2f549082019-03-29 15:13:46 +0100601 _Py_ExitInitError(err);
602}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200603
Victor Stinnerdfe88472019-03-01 12:14:41 +0100604
Victor Stinner2f549082019-03-29 15:13:46 +0100605int
606_Py_RunMain(void)
607{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100608 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100609
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200610 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200611
Victor Stinnerdfe88472019-03-01 12:14:41 +0100612 if (Py_FinalizeEx() < 0) {
613 /* Value unlikely to be confused with a non-error exit status or
614 other special meaning */
615 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100616 }
617
Victor Stinner62be7632019-03-01 13:10:14 +0100618 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100619
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800620 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100621 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800622 }
623
Victor Stinnerdfe88472019-03-01 12:14:41 +0100624 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100625}
Victor Stinnerc1834442019-03-18 22:24:28 +0100626
Victor Stinner2f549082019-03-29 15:13:46 +0100627
628static int
629pymain_main(_PyArgv *args)
630{
631 _PyInitError err = pymain_init(args);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200632 if (_Py_INIT_IS_EXIT(err)) {
633 pymain_free();
634 return err.exitcode;
635 }
Victor Stinner2f549082019-03-29 15:13:46 +0100636 if (_Py_INIT_FAILED(err)) {
637 pymain_exit_error(err);
638 }
639
640 return _Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100641}
642
643
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800644int
645Py_Main(int argc, wchar_t **argv)
646{
Victor Stinner62be7632019-03-01 13:10:14 +0100647 _PyArgv args = {
648 .argc = argc,
649 .use_bytes_argv = 0,
650 .bytes_argv = NULL,
651 .wchar_argv = argv};
652 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000653}
654
Victor Stinner94540602017-12-16 04:54:22 +0100655
656int
657_Py_UnixMain(int argc, char **argv)
658{
Victor Stinner62be7632019-03-01 13:10:14 +0100659 _PyArgv args = {
660 .argc = argc,
661 .use_bytes_argv = 1,
662 .bytes_argv = argv,
663 .wchar_argv = NULL};
664 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100665}
666
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667#ifdef __cplusplus
668}
669#endif