blob: 6b9406f786667260f5e2875f11235297f71aee14 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02004#include "pycore_initconfig.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 Stinner331a6a52019-05-27 16:39:22 +020036static PyStatus
Victor Stinner5ac27a52019-03-27 13:40:14 +010037pymain_init(const _PyArgv *args)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010038{
Victor Stinner331a6a52019-05-27 16:39:22 +020039 PyStatus status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010040
Victor Stinner331a6a52019-05-27 16:39:22 +020041 status = _PyRuntime_Initialize();
42 if (_PyStatus_EXCEPTION(status)) {
43 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010044 }
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 Stinner331a6a52019-05-27 16:39:22 +020055 PyPreConfig preconfig;
56 PyPreConfig_InitPythonConfig(&preconfig);
57 status = _Py_PreInitializeFromPyArgv(&preconfig, args);
58 if (_PyStatus_EXCEPTION(status)) {
59 return status;
Victor Stinner6dcb5422019-03-05 02:44:12 +010060 }
61
Victor Stinner331a6a52019-05-27 16:39:22 +020062 PyConfig config;
63 status = PyConfig_InitPythonConfig(&config);
64 if (_PyStatus_EXCEPTION(status)) {
65 return status;
Victor Stinnercab5d072019-05-17 19:01:14 +020066 }
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 Stinner331a6a52019-05-27 16:39:22 +020071 status = PyConfig_SetBytesArgv(&config, args->argc, args->bytes_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010072 }
Victor Stinner5ac27a52019-03-27 13:40:14 +010073 else {
Victor Stinner331a6a52019-05-27 16:39:22 +020074 status = PyConfig_SetArgv(&config, args->argc, args->wchar_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010075 }
Victor Stinner331a6a52019-05-27 16:39:22 +020076 if (_PyStatus_EXCEPTION(status)) {
77 return status;
78 }
79
80 status = Py_InitializeFromConfig(&config);
81 if (_PyStatus_EXCEPTION(status)) {
82 return status;
83 }
84 return _PyStatus_OK();
Victor Stinner95e2cbf2019-03-01 16:25:19 +010085}
86
87
88/* --- pymain_run_python() ---------------------------------------- */
89
90/* Non-zero if filename, command (-c) or module (-m) is set
91 on the command line */
Victor Stinner331a6a52019-05-27 16:39:22 +020092static inline int config_run_code(const PyConfig *config)
93{
94 return (config->run_command != NULL
95 || config->run_filename != NULL
96 || config->run_module != NULL);
97}
98
Victor Stinner95e2cbf2019-03-01 16:25:19 +010099
100/* Return non-zero is stdin is a TTY or if -i command line option is used */
101static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200102stdin_is_interactive(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100103{
104 return (isatty(fileno(stdin)) || config->interactive);
105}
106
107
Victor Stinner12083282019-05-17 23:05:29 +0200108/* Display the current Python exception and return an exitcode */
109static int
110pymain_err_print(int *exitcode_p)
111{
112 int exitcode;
113 if (_Py_HandleSystemExit(&exitcode)) {
114 *exitcode_p = exitcode;
115 return 1;
116 }
117
118 PyErr_Print();
119 return 0;
120}
121
122
123static int
124pymain_exit_err_print(void)
125{
126 int exitcode = 1;
127 pymain_err_print(&exitcode);
128 return exitcode;
129}
130
131
132/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
133 Return 0 otherwise. */
134static int
135pymain_get_importer(const wchar_t *filename, PyObject **importer_p, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100136{
137 PyObject *sys_path0 = NULL, *importer;
138
139 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
140 if (sys_path0 == NULL) {
141 goto error;
142 }
143
144 importer = PyImport_GetImporter(sys_path0);
145 if (importer == NULL) {
146 goto error;
147 }
148
149 if (importer == Py_None) {
150 Py_DECREF(sys_path0);
151 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200152 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100153 }
154
155 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200156 *importer_p = sys_path0;
157 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100158
159error:
160 Py_XDECREF(sys_path0);
Victor Stinner12083282019-05-17 23:05:29 +0200161
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100162 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
Victor Stinner12083282019-05-17 23:05:29 +0200163 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100164}
165
166
167static int
168pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
169{
170 _Py_IDENTIFIER(path);
171 PyObject *sys_path;
172 PyObject *sysdict = interp->sysdict;
173 if (sysdict != NULL) {
174 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
175 if (sys_path == NULL && PyErr_Occurred()) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200176 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100177 }
178 }
179 else {
180 sys_path = NULL;
181 }
182 if (sys_path == NULL) {
183 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200184 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100185 }
186
187 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200188 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100189 }
190 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100191}
192
193
194static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200195pymain_header(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100196{
197 if (config->quiet) {
198 return;
199 }
200
Victor Stinner331a6a52019-05-27 16:39:22 +0200201 if (!config->verbose && (config_run_code(config) || !stdin_is_interactive(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100202 return;
203 }
204
205 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
206 if (config->site_import) {
207 fprintf(stderr, "%s\n", COPYRIGHT);
208 }
209}
210
211
212static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200213pymain_import_readline(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100214{
Victor Stinner20004952019-03-26 02:31:11 +0100215 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100216 return;
217 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200218 if (!config->inspect && config_run_code(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100219 return;
220 }
221 if (!isatty(fileno(stdin))) {
222 return;
223 }
224
225 PyObject *mod = PyImport_ImportModule("readline");
226 if (mod == NULL) {
227 PyErr_Clear();
228 }
229 else {
230 Py_DECREF(mod);
231 }
232}
233
234
235static int
236pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
237{
238 PyObject *unicode, *bytes;
239 int ret;
240
241 unicode = PyUnicode_FromWideChar(command, -1);
242 if (unicode == NULL) {
243 goto error;
244 }
245
246 bytes = PyUnicode_AsUTF8String(unicode);
247 Py_DECREF(unicode);
248 if (bytes == NULL) {
249 goto error;
250 }
251
252 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
253 Py_DECREF(bytes);
254 return (ret != 0);
255
256error:
257 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200258 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100259}
260
261
262static int
263pymain_run_module(const wchar_t *modname, int set_argv0)
264{
265 PyObject *module, *runpy, *runmodule, *runargs, *result;
266 runpy = PyImport_ImportModule("runpy");
267 if (runpy == NULL) {
268 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200269 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100270 }
271 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
272 if (runmodule == NULL) {
273 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100274 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200275 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100276 }
277 module = PyUnicode_FromWideChar(modname, wcslen(modname));
278 if (module == NULL) {
279 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100280 Py_DECREF(runpy);
281 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200282 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100283 }
284 runargs = Py_BuildValue("(Oi)", module, set_argv0);
285 if (runargs == NULL) {
286 fprintf(stderr,
287 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100288 Py_DECREF(runpy);
289 Py_DECREF(runmodule);
290 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200291 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100292 }
293 result = PyObject_Call(runmodule, runargs, NULL);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100294 Py_DECREF(runpy);
295 Py_DECREF(runmodule);
296 Py_DECREF(module);
297 Py_DECREF(runargs);
298 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200299 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100300 }
301 Py_DECREF(result);
302 return 0;
303}
304
305
306static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200307pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100308{
309 const wchar_t *filename = config->run_filename;
Inada Naoki10654c12019-04-01 18:35:20 +0900310 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100311 if (fp == NULL) {
312 char *cfilename_buffer;
313 const char *cfilename;
314 int err = errno;
315 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
316 if (cfilename_buffer != NULL)
317 cfilename = cfilename_buffer;
318 else
319 cfilename = "<unprintable file name>";
320 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200321 config->program_name, cfilename, err, strerror(err));
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100322 PyMem_RawFree(cfilename_buffer);
323 return 2;
324 }
325
326 if (config->skip_source_first_line) {
327 int ch;
328 /* Push back first newline so line numbers remain the same */
329 while ((ch = getc(fp)) != EOF) {
330 if (ch == '\n') {
331 (void)ungetc(ch, fp);
332 break;
333 }
334 }
335 }
336
337 struct _Py_stat_struct sb;
338 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
339 fprintf(stderr,
340 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200341 config->program_name, filename);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100342 fclose(fp);
343 return 1;
344 }
345
346 /* call pending calls like signal handlers (SIGINT) */
347 if (Py_MakePendingCalls() == -1) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100348 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200349 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100350 }
351
352 PyObject *unicode, *bytes = NULL;
353 const char *filename_str;
354
355 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
356 if (unicode != NULL) {
357 bytes = PyUnicode_EncodeFSDefault(unicode);
358 Py_DECREF(unicode);
359 }
360 if (bytes != NULL) {
361 filename_str = PyBytes_AsString(bytes);
362 }
363 else {
364 PyErr_Clear();
365 filename_str = "<filename encoding error>";
366 }
367
368 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
369 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
370 Py_XDECREF(bytes);
371 return (run != 0);
372}
373
374
Victor Stinner12083282019-05-17 23:05:29 +0200375static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200376pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100377{
Victor Stinner20004952019-03-26 02:31:11 +0100378 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100379 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200380 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100381 }
382
383 FILE *fp = _Py_fopen(startup, "r");
384 if (fp == NULL) {
385 int save_errno = errno;
386 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100387
Victor Stinner12083282019-05-17 23:05:29 +0200388 errno = save_errno;
389 PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
390
391 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100392 }
393
394 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
395 PyErr_Clear();
396 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200397 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100398}
399
400
Victor Stinner12083282019-05-17 23:05:29 +0200401/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
402 Return 0 otherwise. */
403static int
404pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100405{
406 PyObject *sys, *hook, *result;
407 sys = PyImport_ImportModule("sys");
408 if (sys == NULL) {
409 goto error;
410 }
411
412 hook = PyObject_GetAttrString(sys, "__interactivehook__");
413 Py_DECREF(sys);
414 if (hook == NULL) {
415 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200416 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100417 }
418
419 result = _PyObject_CallNoArg(hook);
420 Py_DECREF(hook);
421 if (result == NULL) {
422 goto error;
423 }
424 Py_DECREF(result);
425
Victor Stinner12083282019-05-17 23:05:29 +0200426 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100427
428error:
429 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200430 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100431}
432
433
434static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200435pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100436{
437 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100438 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200439 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200440
441 int exitcode;
442 if (pymain_run_startup(config, cf, &exitcode)) {
443 return exitcode;
444 }
445
446 if (pymain_run_interactive_hook(&exitcode)) {
447 return exitcode;
448 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100449 }
450
451 /* call pending calls like signal handlers (SIGINT) */
452 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200453 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100454 }
455
456 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
457 return (run != 0);
458}
459
460
461static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200462pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100463{
464 /* Check this environment variable at the end, to give programs the
465 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200466 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100467 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200468 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100469 }
470
Victor Stinner331a6a52019-05-27 16:39:22 +0200471 if (!(config->inspect && stdin_is_interactive(config) && config_run_code(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100472 return;
473 }
474
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100475 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200476 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200477 if (pymain_run_interactive_hook(exitcode)) {
478 return;
479 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100480
481 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
482 *exitcode = (res != 0);
483}
484
485
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200486static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100487pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100488{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100489 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
490 /* pymain_run_stdin() modify the config */
Victor Stinner331a6a52019-05-27 16:39:22 +0200491 PyConfig *config = &interp->config;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200492
493 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100494 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100495 /* If filename is a package (ex: directory or ZIP file) which contains
496 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200497 prepended to sys.path.
498
Victor Stinner12083282019-05-17 23:05:29 +0200499 Otherwise, main_importer_path is left unchanged. */
500 if (pymain_get_importer(config->run_filename, &main_importer_path,
501 exitcode)) {
502 return;
503 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100504 }
505
Victor Stinnerd3b19192018-07-25 10:21:03 +0200506 if (main_importer_path != NULL) {
507 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200508 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200509 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100510 }
Victor Stinner20004952019-03-26 02:31:11 +0100511 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100512 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200513 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
514 if (res < 0) {
515 goto error;
516 }
Victor Stinner19760862017-12-20 01:41:59 +0100517
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200518 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100519 if (pymain_sys_path_add_path0(interp, path0) < 0) {
520 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200521 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100522 }
Victor Stinner19760862017-12-20 01:41:59 +0100523 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100524 }
Victor Stinner19760862017-12-20 01:41:59 +0100525 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800526
Guido van Rossum495da292019-03-07 12:38:08 -0800527 PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION};
Victor Stinnera7368ac2017-11-15 18:11:45 -0800528
Victor Stinner62be7632019-03-01 13:10:14 +0100529 pymain_header(config);
530 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800531
Victor Stinner62be7632019-03-01 13:10:14 +0100532 if (config->run_command) {
533 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800534 }
Victor Stinner62be7632019-03-01 13:10:14 +0100535 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200536 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800537 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200538 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200539 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200540 }
Victor Stinner62be7632019-03-01 13:10:14 +0100541 else if (config->run_filename != NULL) {
542 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200543 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800544 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100545 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800546 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100547
Victor Stinner62be7632019-03-01 13:10:14 +0100548 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200549 goto done;
550
551error:
Victor Stinner12083282019-05-17 23:05:29 +0200552 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200553
554done:
555 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800556}
557
Victor Stinnera7368ac2017-11-15 18:11:45 -0800558
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100559/* --- pymain_main() ---------------------------------------------- */
560
561static void
562pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800563{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100564 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200565
566 /* Free global variables which cannot be freed in Py_Finalize():
567 configuration options set before Py_Initialize() which should
568 remain valid after Py_Finalize(), since
569 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100570 _PyPathConfig_ClearGlobal();
571 _Py_ClearStandardStreamEncoding();
572 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100573 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100574}
Victor Stinner94540602017-12-16 04:54:22 +0100575
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200576
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100577static int
578exit_sigint(void)
579{
580 /* bpo-1054041: We need to exit via the
581 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
582 * If we don't, a calling process such as a shell may not know
583 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
584#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
585 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
586 perror("signal"); /* Impossible in normal environments. */
587 } else {
588 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100589 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100590 /* If setting SIG_DFL failed, or kill failed to terminate us,
591 * there isn't much else we can do aside from an error code. */
592#endif /* HAVE_GETPID && !MS_WINDOWS */
593#ifdef MS_WINDOWS
594 /* cmd.exe detects this, prints ^C, and offers to terminate. */
595 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
596 return STATUS_CONTROL_C_EXIT;
597#else
598 return SIGINT + 128;
599#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200600}
601
602
Victor Stinner2f549082019-03-29 15:13:46 +0100603static void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +0200604pymain_exit_error(PyStatus status)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200605{
Victor Stinner331a6a52019-05-27 16:39:22 +0200606 if (_PyStatus_IS_EXIT(status)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200607 /* If it's an error rather than a regular exit, leave Python runtime
Victor Stinner331a6a52019-05-27 16:39:22 +0200608 alive: Py_ExitStatusException() uses the current exception and use
Victor Stinner4cb525a2019-04-26 13:05:47 +0200609 sys.stdout in this case. */
610 pymain_free();
611 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200612 Py_ExitStatusException(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100613}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200614
Victor Stinnerdfe88472019-03-01 12:14:41 +0100615
Victor Stinner2f549082019-03-29 15:13:46 +0100616int
Victor Stinner331a6a52019-05-27 16:39:22 +0200617Py_RunMain(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100618{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100619 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100620
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200621 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200622
Victor Stinnerdfe88472019-03-01 12:14:41 +0100623 if (Py_FinalizeEx() < 0) {
624 /* Value unlikely to be confused with a non-error exit status or
625 other special meaning */
626 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100627 }
628
Victor Stinner62be7632019-03-01 13:10:14 +0100629 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100630
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800631 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100632 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800633 }
634
Victor Stinnerdfe88472019-03-01 12:14:41 +0100635 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100636}
Victor Stinnerc1834442019-03-18 22:24:28 +0100637
Victor Stinner2f549082019-03-29 15:13:46 +0100638
639static int
640pymain_main(_PyArgv *args)
641{
Victor Stinner331a6a52019-05-27 16:39:22 +0200642 PyStatus status = pymain_init(args);
643 if (_PyStatus_IS_EXIT(status)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200644 pymain_free();
Victor Stinner331a6a52019-05-27 16:39:22 +0200645 return status.exitcode;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200646 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200647 if (_PyStatus_EXCEPTION(status)) {
648 pymain_exit_error(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100649 }
650
Victor Stinner331a6a52019-05-27 16:39:22 +0200651 return Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100652}
653
654
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800655int
656Py_Main(int argc, wchar_t **argv)
657{
Victor Stinner62be7632019-03-01 13:10:14 +0100658 _PyArgv args = {
659 .argc = argc,
660 .use_bytes_argv = 0,
661 .bytes_argv = NULL,
662 .wchar_argv = argv};
663 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000664}
665
Victor Stinner94540602017-12-16 04:54:22 +0100666
667int
Victor Stinner331a6a52019-05-27 16:39:22 +0200668Py_BytesMain(int argc, char **argv)
Victor Stinner94540602017-12-16 04:54:22 +0100669{
Victor Stinner62be7632019-03-01 13:10:14 +0100670 _PyArgv args = {
671 .argc = argc,
672 .use_bytes_argv = 1,
673 .bytes_argv = argv,
674 .wchar_argv = NULL};
675 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100676}
677
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678#ifdef __cplusplus
679}
680#endif