blob: fdc7e0dabf17bbdd5a0f5e337f07c088ffcab6d0 [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)) {
Victor Stinner67310022019-07-01 19:52:45 +020065 goto done;
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)) {
Victor Stinner67310022019-07-01 19:52:45 +020077 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020078 }
79
80 status = Py_InitializeFromConfig(&config);
81 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner67310022019-07-01 19:52:45 +020082 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020083 }
Victor Stinner67310022019-07-01 19:52:45 +020084 status = _PyStatus_OK();
85
86done:
87 PyConfig_Clear(&config);
88 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010089}
90
91
92/* --- pymain_run_python() ---------------------------------------- */
93
94/* Non-zero if filename, command (-c) or module (-m) is set
95 on the command line */
Victor Stinner331a6a52019-05-27 16:39:22 +020096static inline int config_run_code(const PyConfig *config)
97{
98 return (config->run_command != NULL
99 || config->run_filename != NULL
100 || config->run_module != NULL);
101}
102
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100103
104/* Return non-zero is stdin is a TTY or if -i command line option is used */
105static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200106stdin_is_interactive(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100107{
108 return (isatty(fileno(stdin)) || config->interactive);
109}
110
111
Victor Stinner12083282019-05-17 23:05:29 +0200112/* Display the current Python exception and return an exitcode */
113static int
114pymain_err_print(int *exitcode_p)
115{
116 int exitcode;
117 if (_Py_HandleSystemExit(&exitcode)) {
118 *exitcode_p = exitcode;
119 return 1;
120 }
121
122 PyErr_Print();
123 return 0;
124}
125
126
127static int
128pymain_exit_err_print(void)
129{
130 int exitcode = 1;
131 pymain_err_print(&exitcode);
132 return exitcode;
133}
134
135
136/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
137 Return 0 otherwise. */
138static int
139pymain_get_importer(const wchar_t *filename, PyObject **importer_p, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100140{
141 PyObject *sys_path0 = NULL, *importer;
142
143 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
144 if (sys_path0 == NULL) {
145 goto error;
146 }
147
148 importer = PyImport_GetImporter(sys_path0);
149 if (importer == NULL) {
150 goto error;
151 }
152
153 if (importer == Py_None) {
154 Py_DECREF(sys_path0);
155 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200156 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100157 }
158
159 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200160 *importer_p = sys_path0;
161 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100162
163error:
164 Py_XDECREF(sys_path0);
Victor Stinner12083282019-05-17 23:05:29 +0200165
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100166 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
Victor Stinner12083282019-05-17 23:05:29 +0200167 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100168}
169
170
171static int
172pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
173{
174 _Py_IDENTIFIER(path);
175 PyObject *sys_path;
176 PyObject *sysdict = interp->sysdict;
177 if (sysdict != NULL) {
178 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
179 if (sys_path == NULL && PyErr_Occurred()) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200180 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100181 }
182 }
183 else {
184 sys_path = NULL;
185 }
186 if (sys_path == NULL) {
187 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200188 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100189 }
190
191 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200192 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100193 }
194 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100195}
196
197
198static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200199pymain_header(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100200{
201 if (config->quiet) {
202 return;
203 }
204
Victor Stinner331a6a52019-05-27 16:39:22 +0200205 if (!config->verbose && (config_run_code(config) || !stdin_is_interactive(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100206 return;
207 }
208
209 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
210 if (config->site_import) {
211 fprintf(stderr, "%s\n", COPYRIGHT);
212 }
213}
214
215
216static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200217pymain_import_readline(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100218{
Victor Stinner20004952019-03-26 02:31:11 +0100219 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100220 return;
221 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200222 if (!config->inspect && config_run_code(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100223 return;
224 }
225 if (!isatty(fileno(stdin))) {
226 return;
227 }
228
229 PyObject *mod = PyImport_ImportModule("readline");
230 if (mod == NULL) {
231 PyErr_Clear();
232 }
233 else {
234 Py_DECREF(mod);
235 }
236}
237
238
239static int
240pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
241{
242 PyObject *unicode, *bytes;
243 int ret;
244
245 unicode = PyUnicode_FromWideChar(command, -1);
246 if (unicode == NULL) {
247 goto error;
248 }
249
Steve Dowere226e832019-07-01 16:03:53 -0700250 if (PySys_Audit("cpython.run_command", "O", unicode) < 0) {
251 return pymain_exit_err_print();
252 }
253
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100254 bytes = PyUnicode_AsUTF8String(unicode);
255 Py_DECREF(unicode);
256 if (bytes == NULL) {
257 goto error;
258 }
259
260 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
261 Py_DECREF(bytes);
262 return (ret != 0);
263
264error:
265 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200266 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100267}
268
269
270static int
271pymain_run_module(const wchar_t *modname, int set_argv0)
272{
273 PyObject *module, *runpy, *runmodule, *runargs, *result;
Steve Dowere226e832019-07-01 16:03:53 -0700274 if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
275 return pymain_exit_err_print();
276 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100277 runpy = PyImport_ImportModule("runpy");
278 if (runpy == NULL) {
279 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200280 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100281 }
282 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
283 if (runmodule == NULL) {
284 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100285 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200286 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100287 }
288 module = PyUnicode_FromWideChar(modname, wcslen(modname));
289 if (module == NULL) {
290 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100291 Py_DECREF(runpy);
292 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200293 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100294 }
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300295 runargs = PyTuple_Pack(2, module, set_argv0 ? Py_True : Py_False);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100296 if (runargs == NULL) {
297 fprintf(stderr,
298 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100299 Py_DECREF(runpy);
300 Py_DECREF(runmodule);
301 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200302 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100303 }
304 result = PyObject_Call(runmodule, runargs, NULL);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100305 Py_DECREF(runpy);
306 Py_DECREF(runmodule);
307 Py_DECREF(module);
308 Py_DECREF(runargs);
309 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200310 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100311 }
312 Py_DECREF(result);
313 return 0;
314}
315
316
317static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200318pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100319{
320 const wchar_t *filename = config->run_filename;
Steve Dowere226e832019-07-01 16:03:53 -0700321 if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
322 return pymain_exit_err_print();
323 }
Inada Naoki10654c12019-04-01 18:35:20 +0900324 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100325 if (fp == NULL) {
326 char *cfilename_buffer;
327 const char *cfilename;
328 int err = errno;
329 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
330 if (cfilename_buffer != NULL)
331 cfilename = cfilename_buffer;
332 else
333 cfilename = "<unprintable file name>";
334 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200335 config->program_name, cfilename, err, strerror(err));
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100336 PyMem_RawFree(cfilename_buffer);
337 return 2;
338 }
339
340 if (config->skip_source_first_line) {
341 int ch;
342 /* Push back first newline so line numbers remain the same */
343 while ((ch = getc(fp)) != EOF) {
344 if (ch == '\n') {
345 (void)ungetc(ch, fp);
346 break;
347 }
348 }
349 }
350
351 struct _Py_stat_struct sb;
352 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
353 fprintf(stderr,
354 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200355 config->program_name, filename);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100356 fclose(fp);
357 return 1;
358 }
359
360 /* call pending calls like signal handlers (SIGINT) */
361 if (Py_MakePendingCalls() == -1) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100362 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200363 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100364 }
365
366 PyObject *unicode, *bytes = NULL;
367 const char *filename_str;
368
369 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
370 if (unicode != NULL) {
371 bytes = PyUnicode_EncodeFSDefault(unicode);
372 Py_DECREF(unicode);
373 }
374 if (bytes != NULL) {
375 filename_str = PyBytes_AsString(bytes);
376 }
377 else {
378 PyErr_Clear();
379 filename_str = "<filename encoding error>";
380 }
381
382 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
383 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
384 Py_XDECREF(bytes);
385 return (run != 0);
386}
387
388
Victor Stinner12083282019-05-17 23:05:29 +0200389static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200390pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100391{
Victor Stinner20004952019-03-26 02:31:11 +0100392 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100393 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200394 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100395 }
Steve Dowere226e832019-07-01 16:03:53 -0700396 if (PySys_Audit("cpython.run_startup", "s", startup) < 0) {
397 return pymain_err_print(exitcode);
398 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100399
400 FILE *fp = _Py_fopen(startup, "r");
401 if (fp == NULL) {
402 int save_errno = errno;
403 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100404
Victor Stinner12083282019-05-17 23:05:29 +0200405 errno = save_errno;
406 PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
407
408 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100409 }
410
411 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
412 PyErr_Clear();
413 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200414 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100415}
416
417
Victor Stinner12083282019-05-17 23:05:29 +0200418/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
419 Return 0 otherwise. */
420static int
421pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100422{
423 PyObject *sys, *hook, *result;
424 sys = PyImport_ImportModule("sys");
425 if (sys == NULL) {
426 goto error;
427 }
428
429 hook = PyObject_GetAttrString(sys, "__interactivehook__");
430 Py_DECREF(sys);
431 if (hook == NULL) {
432 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200433 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100434 }
435
Steve Dowere226e832019-07-01 16:03:53 -0700436 if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
437 goto error;
438 }
439
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100440 result = _PyObject_CallNoArg(hook);
441 Py_DECREF(hook);
442 if (result == NULL) {
443 goto error;
444 }
445 Py_DECREF(result);
446
Victor Stinner12083282019-05-17 23:05:29 +0200447 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100448
449error:
450 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200451 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100452}
453
454
455static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200456pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100457{
458 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100459 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200460 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200461
462 int exitcode;
463 if (pymain_run_startup(config, cf, &exitcode)) {
464 return exitcode;
465 }
466
467 if (pymain_run_interactive_hook(&exitcode)) {
468 return exitcode;
469 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100470 }
471
472 /* call pending calls like signal handlers (SIGINT) */
473 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200474 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100475 }
476
Steve Dowere226e832019-07-01 16:03:53 -0700477 if (PySys_Audit("cpython.run_stdin", NULL) < 0) {
478 return pymain_exit_err_print();
479 }
480
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100481 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
482 return (run != 0);
483}
484
485
486static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200487pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100488{
489 /* Check this environment variable at the end, to give programs the
490 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200491 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100492 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200493 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100494 }
495
Victor Stinner331a6a52019-05-27 16:39:22 +0200496 if (!(config->inspect && stdin_is_interactive(config) && config_run_code(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100497 return;
498 }
499
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100500 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200501 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200502 if (pymain_run_interactive_hook(exitcode)) {
503 return;
504 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100505
506 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
507 *exitcode = (res != 0);
508}
509
510
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200511static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100512pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100513{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100514 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
515 /* pymain_run_stdin() modify the config */
Victor Stinner331a6a52019-05-27 16:39:22 +0200516 PyConfig *config = &interp->config;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200517
518 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100519 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100520 /* If filename is a package (ex: directory or ZIP file) which contains
521 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200522 prepended to sys.path.
523
Victor Stinner12083282019-05-17 23:05:29 +0200524 Otherwise, main_importer_path is left unchanged. */
525 if (pymain_get_importer(config->run_filename, &main_importer_path,
526 exitcode)) {
527 return;
528 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100529 }
530
Victor Stinnerd3b19192018-07-25 10:21:03 +0200531 if (main_importer_path != NULL) {
532 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200533 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200534 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100535 }
Victor Stinner20004952019-03-26 02:31:11 +0100536 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100537 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200538 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
539 if (res < 0) {
540 goto error;
541 }
Victor Stinner19760862017-12-20 01:41:59 +0100542
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200543 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100544 if (pymain_sys_path_add_path0(interp, path0) < 0) {
545 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200546 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100547 }
Victor Stinner19760862017-12-20 01:41:59 +0100548 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100549 }
Victor Stinner19760862017-12-20 01:41:59 +0100550 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800551
Victor Stinner37d66d72019-06-13 02:16:41 +0200552 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800553
Victor Stinner62be7632019-03-01 13:10:14 +0100554 pymain_header(config);
555 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800556
Victor Stinner62be7632019-03-01 13:10:14 +0100557 if (config->run_command) {
558 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800559 }
Victor Stinner62be7632019-03-01 13:10:14 +0100560 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200561 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800562 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200563 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200564 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200565 }
Victor Stinner62be7632019-03-01 13:10:14 +0100566 else if (config->run_filename != NULL) {
567 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200568 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800569 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100570 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800571 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100572
Victor Stinner62be7632019-03-01 13:10:14 +0100573 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200574 goto done;
575
576error:
Victor Stinner12083282019-05-17 23:05:29 +0200577 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200578
579done:
580 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800581}
582
Victor Stinnera7368ac2017-11-15 18:11:45 -0800583
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100584/* --- pymain_main() ---------------------------------------------- */
585
586static void
587pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800588{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100589 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200590
591 /* Free global variables which cannot be freed in Py_Finalize():
592 configuration options set before Py_Initialize() which should
593 remain valid after Py_Finalize(), since
594 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100595 _PyPathConfig_ClearGlobal();
596 _Py_ClearStandardStreamEncoding();
597 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100598 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100599}
Victor Stinner94540602017-12-16 04:54:22 +0100600
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200601
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100602static int
603exit_sigint(void)
604{
605 /* bpo-1054041: We need to exit via the
606 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
607 * If we don't, a calling process such as a shell may not know
608 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
609#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
610 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
611 perror("signal"); /* Impossible in normal environments. */
612 } else {
613 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100614 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100615 /* If setting SIG_DFL failed, or kill failed to terminate us,
616 * there isn't much else we can do aside from an error code. */
617#endif /* HAVE_GETPID && !MS_WINDOWS */
618#ifdef MS_WINDOWS
619 /* cmd.exe detects this, prints ^C, and offers to terminate. */
620 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
621 return STATUS_CONTROL_C_EXIT;
622#else
623 return SIGINT + 128;
624#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200625}
626
627
Victor Stinner2f549082019-03-29 15:13:46 +0100628static void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +0200629pymain_exit_error(PyStatus status)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200630{
Victor Stinner331a6a52019-05-27 16:39:22 +0200631 if (_PyStatus_IS_EXIT(status)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200632 /* If it's an error rather than a regular exit, leave Python runtime
Victor Stinner331a6a52019-05-27 16:39:22 +0200633 alive: Py_ExitStatusException() uses the current exception and use
Victor Stinner4cb525a2019-04-26 13:05:47 +0200634 sys.stdout in this case. */
635 pymain_free();
636 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200637 Py_ExitStatusException(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100638}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200639
Victor Stinnerdfe88472019-03-01 12:14:41 +0100640
Victor Stinner2f549082019-03-29 15:13:46 +0100641int
Victor Stinner331a6a52019-05-27 16:39:22 +0200642Py_RunMain(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100643{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100644 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100645
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200646 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200647
Victor Stinnerdfe88472019-03-01 12:14:41 +0100648 if (Py_FinalizeEx() < 0) {
649 /* Value unlikely to be confused with a non-error exit status or
650 other special meaning */
651 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100652 }
653
Victor Stinner62be7632019-03-01 13:10:14 +0100654 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100655
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800656 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100657 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800658 }
659
Victor Stinnerdfe88472019-03-01 12:14:41 +0100660 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100661}
Victor Stinnerc1834442019-03-18 22:24:28 +0100662
Victor Stinner2f549082019-03-29 15:13:46 +0100663
664static int
665pymain_main(_PyArgv *args)
666{
Victor Stinner331a6a52019-05-27 16:39:22 +0200667 PyStatus status = pymain_init(args);
668 if (_PyStatus_IS_EXIT(status)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200669 pymain_free();
Victor Stinner331a6a52019-05-27 16:39:22 +0200670 return status.exitcode;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200671 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200672 if (_PyStatus_EXCEPTION(status)) {
673 pymain_exit_error(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100674 }
675
Victor Stinner331a6a52019-05-27 16:39:22 +0200676 return Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100677}
678
679
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800680int
681Py_Main(int argc, wchar_t **argv)
682{
Victor Stinner62be7632019-03-01 13:10:14 +0100683 _PyArgv args = {
684 .argc = argc,
685 .use_bytes_argv = 0,
686 .bytes_argv = NULL,
687 .wchar_argv = argv};
688 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000689}
690
Victor Stinner94540602017-12-16 04:54:22 +0100691
692int
Victor Stinner331a6a52019-05-27 16:39:22 +0200693Py_BytesMain(int argc, char **argv)
Victor Stinner94540602017-12-16 04:54:22 +0100694{
Victor Stinner62be7632019-03-01 13:10:14 +0100695 _PyArgv args = {
696 .argc = argc,
697 .use_bytes_argv = 1,
698 .bytes_argv = argv,
699 .wchar_argv = NULL};
700 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100701}
702
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703#ifdef __cplusplus
704}
705#endif