blob: b126f4554d41b11f6a72510b21a7349667bdbba6 [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
250 bytes = PyUnicode_AsUTF8String(unicode);
251 Py_DECREF(unicode);
252 if (bytes == NULL) {
253 goto error;
254 }
255
256 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
257 Py_DECREF(bytes);
258 return (ret != 0);
259
260error:
261 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200262 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100263}
264
265
266static int
267pymain_run_module(const wchar_t *modname, int set_argv0)
268{
269 PyObject *module, *runpy, *runmodule, *runargs, *result;
270 runpy = PyImport_ImportModule("runpy");
271 if (runpy == NULL) {
272 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200273 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100274 }
275 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
276 if (runmodule == NULL) {
277 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100278 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200279 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100280 }
281 module = PyUnicode_FromWideChar(modname, wcslen(modname));
282 if (module == NULL) {
283 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100284 Py_DECREF(runpy);
285 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200286 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100287 }
288 runargs = Py_BuildValue("(Oi)", module, set_argv0);
289 if (runargs == NULL) {
290 fprintf(stderr,
291 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100292 Py_DECREF(runpy);
293 Py_DECREF(runmodule);
294 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200295 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100296 }
297 result = PyObject_Call(runmodule, runargs, NULL);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100298 Py_DECREF(runpy);
299 Py_DECREF(runmodule);
300 Py_DECREF(module);
301 Py_DECREF(runargs);
302 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200303 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100304 }
305 Py_DECREF(result);
306 return 0;
307}
308
309
310static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200311pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100312{
313 const wchar_t *filename = config->run_filename;
Inada Naoki10654c12019-04-01 18:35:20 +0900314 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100315 if (fp == NULL) {
316 char *cfilename_buffer;
317 const char *cfilename;
318 int err = errno;
319 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
320 if (cfilename_buffer != NULL)
321 cfilename = cfilename_buffer;
322 else
323 cfilename = "<unprintable file name>";
324 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200325 config->program_name, cfilename, err, strerror(err));
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100326 PyMem_RawFree(cfilename_buffer);
327 return 2;
328 }
329
330 if (config->skip_source_first_line) {
331 int ch;
332 /* Push back first newline so line numbers remain the same */
333 while ((ch = getc(fp)) != EOF) {
334 if (ch == '\n') {
335 (void)ungetc(ch, fp);
336 break;
337 }
338 }
339 }
340
341 struct _Py_stat_struct sb;
342 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
343 fprintf(stderr,
344 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200345 config->program_name, filename);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100346 fclose(fp);
347 return 1;
348 }
349
350 /* call pending calls like signal handlers (SIGINT) */
351 if (Py_MakePendingCalls() == -1) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100352 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200353 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100354 }
355
356 PyObject *unicode, *bytes = NULL;
357 const char *filename_str;
358
359 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
360 if (unicode != NULL) {
361 bytes = PyUnicode_EncodeFSDefault(unicode);
362 Py_DECREF(unicode);
363 }
364 if (bytes != NULL) {
365 filename_str = PyBytes_AsString(bytes);
366 }
367 else {
368 PyErr_Clear();
369 filename_str = "<filename encoding error>";
370 }
371
372 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
373 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
374 Py_XDECREF(bytes);
375 return (run != 0);
376}
377
378
Victor Stinner12083282019-05-17 23:05:29 +0200379static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200380pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100381{
Victor Stinner20004952019-03-26 02:31:11 +0100382 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100383 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200384 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100385 }
386
387 FILE *fp = _Py_fopen(startup, "r");
388 if (fp == NULL) {
389 int save_errno = errno;
390 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100391
Victor Stinner12083282019-05-17 23:05:29 +0200392 errno = save_errno;
393 PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
394
395 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100396 }
397
398 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
399 PyErr_Clear();
400 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200401 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100402}
403
404
Victor Stinner12083282019-05-17 23:05:29 +0200405/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
406 Return 0 otherwise. */
407static int
408pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100409{
410 PyObject *sys, *hook, *result;
411 sys = PyImport_ImportModule("sys");
412 if (sys == NULL) {
413 goto error;
414 }
415
416 hook = PyObject_GetAttrString(sys, "__interactivehook__");
417 Py_DECREF(sys);
418 if (hook == NULL) {
419 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200420 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100421 }
422
423 result = _PyObject_CallNoArg(hook);
424 Py_DECREF(hook);
425 if (result == NULL) {
426 goto error;
427 }
428 Py_DECREF(result);
429
Victor Stinner12083282019-05-17 23:05:29 +0200430 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100431
432error:
433 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200434 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100435}
436
437
438static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200439pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100440{
441 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100442 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200443 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200444
445 int exitcode;
446 if (pymain_run_startup(config, cf, &exitcode)) {
447 return exitcode;
448 }
449
450 if (pymain_run_interactive_hook(&exitcode)) {
451 return exitcode;
452 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100453 }
454
455 /* call pending calls like signal handlers (SIGINT) */
456 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200457 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100458 }
459
460 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
461 return (run != 0);
462}
463
464
465static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200466pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100467{
468 /* Check this environment variable at the end, to give programs the
469 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200470 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100471 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200472 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100473 }
474
Victor Stinner331a6a52019-05-27 16:39:22 +0200475 if (!(config->inspect && stdin_is_interactive(config) && config_run_code(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100476 return;
477 }
478
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100479 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200480 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200481 if (pymain_run_interactive_hook(exitcode)) {
482 return;
483 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100484
485 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
486 *exitcode = (res != 0);
487}
488
489
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200490static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100491pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100492{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100493 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
494 /* pymain_run_stdin() modify the config */
Victor Stinner331a6a52019-05-27 16:39:22 +0200495 PyConfig *config = &interp->config;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200496
497 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100498 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100499 /* If filename is a package (ex: directory or ZIP file) which contains
500 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200501 prepended to sys.path.
502
Victor Stinner12083282019-05-17 23:05:29 +0200503 Otherwise, main_importer_path is left unchanged. */
504 if (pymain_get_importer(config->run_filename, &main_importer_path,
505 exitcode)) {
506 return;
507 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100508 }
509
Victor Stinnerd3b19192018-07-25 10:21:03 +0200510 if (main_importer_path != NULL) {
511 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200512 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200513 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100514 }
Victor Stinner20004952019-03-26 02:31:11 +0100515 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100516 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200517 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
518 if (res < 0) {
519 goto error;
520 }
Victor Stinner19760862017-12-20 01:41:59 +0100521
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200522 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100523 if (pymain_sys_path_add_path0(interp, path0) < 0) {
524 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200525 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100526 }
Victor Stinner19760862017-12-20 01:41:59 +0100527 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100528 }
Victor Stinner19760862017-12-20 01:41:59 +0100529 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800530
Victor Stinner37d66d72019-06-13 02:16:41 +0200531 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800532
Victor Stinner62be7632019-03-01 13:10:14 +0100533 pymain_header(config);
534 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800535
Victor Stinner62be7632019-03-01 13:10:14 +0100536 if (config->run_command) {
537 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800538 }
Victor Stinner62be7632019-03-01 13:10:14 +0100539 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200540 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800541 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200542 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200543 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200544 }
Victor Stinner62be7632019-03-01 13:10:14 +0100545 else if (config->run_filename != NULL) {
546 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200547 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800548 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100549 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800550 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100551
Victor Stinner62be7632019-03-01 13:10:14 +0100552 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200553 goto done;
554
555error:
Victor Stinner12083282019-05-17 23:05:29 +0200556 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200557
558done:
559 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800560}
561
Victor Stinnera7368ac2017-11-15 18:11:45 -0800562
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100563/* --- pymain_main() ---------------------------------------------- */
564
565static void
566pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800567{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100568 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200569
570 /* Free global variables which cannot be freed in Py_Finalize():
571 configuration options set before Py_Initialize() which should
572 remain valid after Py_Finalize(), since
573 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100574 _PyPathConfig_ClearGlobal();
575 _Py_ClearStandardStreamEncoding();
576 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100577 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100578}
Victor Stinner94540602017-12-16 04:54:22 +0100579
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200580
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100581static int
582exit_sigint(void)
583{
584 /* bpo-1054041: We need to exit via the
585 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
586 * If we don't, a calling process such as a shell may not know
587 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
588#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
589 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
590 perror("signal"); /* Impossible in normal environments. */
591 } else {
592 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100593 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100594 /* If setting SIG_DFL failed, or kill failed to terminate us,
595 * there isn't much else we can do aside from an error code. */
596#endif /* HAVE_GETPID && !MS_WINDOWS */
597#ifdef MS_WINDOWS
598 /* cmd.exe detects this, prints ^C, and offers to terminate. */
599 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
600 return STATUS_CONTROL_C_EXIT;
601#else
602 return SIGINT + 128;
603#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200604}
605
606
Victor Stinner2f549082019-03-29 15:13:46 +0100607static void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +0200608pymain_exit_error(PyStatus status)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200609{
Victor Stinner331a6a52019-05-27 16:39:22 +0200610 if (_PyStatus_IS_EXIT(status)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200611 /* If it's an error rather than a regular exit, leave Python runtime
Victor Stinner331a6a52019-05-27 16:39:22 +0200612 alive: Py_ExitStatusException() uses the current exception and use
Victor Stinner4cb525a2019-04-26 13:05:47 +0200613 sys.stdout in this case. */
614 pymain_free();
615 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200616 Py_ExitStatusException(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100617}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200618
Victor Stinnerdfe88472019-03-01 12:14:41 +0100619
Victor Stinner2f549082019-03-29 15:13:46 +0100620int
Victor Stinner331a6a52019-05-27 16:39:22 +0200621Py_RunMain(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100622{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100623 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100624
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200625 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200626
Victor Stinnerdfe88472019-03-01 12:14:41 +0100627 if (Py_FinalizeEx() < 0) {
628 /* Value unlikely to be confused with a non-error exit status or
629 other special meaning */
630 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100631 }
632
Victor Stinner62be7632019-03-01 13:10:14 +0100633 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100634
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800635 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100636 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800637 }
638
Victor Stinnerdfe88472019-03-01 12:14:41 +0100639 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100640}
Victor Stinnerc1834442019-03-18 22:24:28 +0100641
Victor Stinner2f549082019-03-29 15:13:46 +0100642
643static int
644pymain_main(_PyArgv *args)
645{
Victor Stinner331a6a52019-05-27 16:39:22 +0200646 PyStatus status = pymain_init(args);
647 if (_PyStatus_IS_EXIT(status)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200648 pymain_free();
Victor Stinner331a6a52019-05-27 16:39:22 +0200649 return status.exitcode;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200650 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200651 if (_PyStatus_EXCEPTION(status)) {
652 pymain_exit_error(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100653 }
654
Victor Stinner331a6a52019-05-27 16:39:22 +0200655 return Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100656}
657
658
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800659int
660Py_Main(int argc, wchar_t **argv)
661{
Victor Stinner62be7632019-03-01 13:10:14 +0100662 _PyArgv args = {
663 .argc = argc,
664 .use_bytes_argv = 0,
665 .bytes_argv = NULL,
666 .wchar_argv = argv};
667 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000668}
669
Victor Stinner94540602017-12-16 04:54:22 +0100670
671int
Victor Stinner331a6a52019-05-27 16:39:22 +0200672Py_BytesMain(int argc, char **argv)
Victor Stinner94540602017-12-16 04:54:22 +0100673{
Victor Stinner62be7632019-03-01 13:10:14 +0100674 _PyArgv args = {
675 .argc = argc,
676 .use_bytes_argv = 1,
677 .bytes_argv = argv,
678 .wchar_argv = NULL};
679 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100680}
681
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000682#ifdef __cplusplus
683}
684#endif