blob: 788bc119095c0f235ff94da020af9858b6d226d1 [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;
Victor Stinnerbdace212019-10-01 00:46:42 +020056 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner6e128382019-09-28 04:50:43 +020057
Victor Stinner331a6a52019-05-27 16:39:22 +020058 status = _Py_PreInitializeFromPyArgv(&preconfig, args);
59 if (_PyStatus_EXCEPTION(status)) {
60 return status;
Victor Stinner6dcb5422019-03-05 02:44:12 +010061 }
62
Victor Stinner331a6a52019-05-27 16:39:22 +020063 PyConfig config;
Miss Islington (bot)d49f0962019-10-01 03:26:04 -070064 PyConfig_InitPythonConfig(&config);
Victor Stinnercab5d072019-05-17 19:01:14 +020065
Victor Stinnerd929f182019-03-27 18:28:46 +010066 /* pass NULL as the config: config is read from command line arguments,
67 environment variables, configuration files */
Victor Stinner5ac27a52019-03-27 13:40:14 +010068 if (args->use_bytes_argv) {
Victor Stinner331a6a52019-05-27 16:39:22 +020069 status = PyConfig_SetBytesArgv(&config, args->argc, args->bytes_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010070 }
Victor Stinner5ac27a52019-03-27 13:40:14 +010071 else {
Victor Stinner331a6a52019-05-27 16:39:22 +020072 status = PyConfig_SetArgv(&config, args->argc, args->wchar_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010073 }
Victor Stinner331a6a52019-05-27 16:39:22 +020074 if (_PyStatus_EXCEPTION(status)) {
Miss Islington (bot)4c227e62019-07-01 11:28:55 -070075 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020076 }
77
78 status = Py_InitializeFromConfig(&config);
79 if (_PyStatus_EXCEPTION(status)) {
Miss Islington (bot)4c227e62019-07-01 11:28:55 -070080 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020081 }
Miss Islington (bot)4c227e62019-07-01 11:28:55 -070082 status = _PyStatus_OK();
83
84done:
85 PyConfig_Clear(&config);
86 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010087}
88
89
90/* --- pymain_run_python() ---------------------------------------- */
91
92/* Non-zero if filename, command (-c) or module (-m) is set
93 on the command line */
Victor Stinner331a6a52019-05-27 16:39:22 +020094static inline int config_run_code(const PyConfig *config)
95{
96 return (config->run_command != NULL
97 || config->run_filename != NULL
98 || config->run_module != NULL);
99}
100
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100101
102/* Return non-zero is stdin is a TTY or if -i command line option is used */
103static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200104stdin_is_interactive(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100105{
106 return (isatty(fileno(stdin)) || config->interactive);
107}
108
109
Victor Stinner12083282019-05-17 23:05:29 +0200110/* Display the current Python exception and return an exitcode */
111static int
112pymain_err_print(int *exitcode_p)
113{
114 int exitcode;
115 if (_Py_HandleSystemExit(&exitcode)) {
116 *exitcode_p = exitcode;
117 return 1;
118 }
119
120 PyErr_Print();
121 return 0;
122}
123
124
125static int
126pymain_exit_err_print(void)
127{
128 int exitcode = 1;
129 pymain_err_print(&exitcode);
130 return exitcode;
131}
132
133
134/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
135 Return 0 otherwise. */
136static int
137pymain_get_importer(const wchar_t *filename, PyObject **importer_p, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100138{
139 PyObject *sys_path0 = NULL, *importer;
140
141 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
142 if (sys_path0 == NULL) {
143 goto error;
144 }
145
146 importer = PyImport_GetImporter(sys_path0);
147 if (importer == NULL) {
148 goto error;
149 }
150
151 if (importer == Py_None) {
152 Py_DECREF(sys_path0);
153 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200154 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100155 }
156
157 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200158 *importer_p = sys_path0;
159 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100160
161error:
162 Py_XDECREF(sys_path0);
Victor Stinner12083282019-05-17 23:05:29 +0200163
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100164 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
Victor Stinner12083282019-05-17 23:05:29 +0200165 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100166}
167
168
169static int
170pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
171{
172 _Py_IDENTIFIER(path);
173 PyObject *sys_path;
174 PyObject *sysdict = interp->sysdict;
175 if (sysdict != NULL) {
176 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
177 if (sys_path == NULL && PyErr_Occurred()) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200178 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100179 }
180 }
181 else {
182 sys_path = NULL;
183 }
184 if (sys_path == NULL) {
185 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200186 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100187 }
188
189 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200190 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100191 }
192 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100193}
194
195
196static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200197pymain_header(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100198{
199 if (config->quiet) {
200 return;
201 }
202
Victor Stinner331a6a52019-05-27 16:39:22 +0200203 if (!config->verbose && (config_run_code(config) || !stdin_is_interactive(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100204 return;
205 }
206
207 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
208 if (config->site_import) {
209 fprintf(stderr, "%s\n", COPYRIGHT);
210 }
211}
212
213
214static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200215pymain_import_readline(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100216{
Victor Stinner20004952019-03-26 02:31:11 +0100217 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100218 return;
219 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200220 if (!config->inspect && config_run_code(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100221 return;
222 }
223 if (!isatty(fileno(stdin))) {
224 return;
225 }
226
227 PyObject *mod = PyImport_ImportModule("readline");
228 if (mod == NULL) {
229 PyErr_Clear();
230 }
231 else {
232 Py_DECREF(mod);
233 }
234}
235
236
237static int
238pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
239{
240 PyObject *unicode, *bytes;
241 int ret;
242
243 unicode = PyUnicode_FromWideChar(command, -1);
244 if (unicode == NULL) {
245 goto error;
246 }
247
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700248 if (PySys_Audit("cpython.run_command", "O", unicode) < 0) {
249 return pymain_exit_err_print();
250 }
251
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100252 bytes = PyUnicode_AsUTF8String(unicode);
253 Py_DECREF(unicode);
254 if (bytes == NULL) {
255 goto error;
256 }
257
258 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
259 Py_DECREF(bytes);
260 return (ret != 0);
261
262error:
263 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200264 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100265}
266
267
268static int
269pymain_run_module(const wchar_t *modname, int set_argv0)
270{
271 PyObject *module, *runpy, *runmodule, *runargs, *result;
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700272 if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
273 return pymain_exit_err_print();
274 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100275 runpy = PyImport_ImportModule("runpy");
276 if (runpy == NULL) {
277 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200278 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100279 }
280 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
281 if (runmodule == NULL) {
282 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100283 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200284 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100285 }
286 module = PyUnicode_FromWideChar(modname, wcslen(modname));
287 if (module == NULL) {
288 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100289 Py_DECREF(runpy);
290 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200291 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100292 }
293 runargs = Py_BuildValue("(Oi)", module, set_argv0);
294 if (runargs == NULL) {
295 fprintf(stderr,
296 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100297 Py_DECREF(runpy);
298 Py_DECREF(runmodule);
299 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200300 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100301 }
302 result = PyObject_Call(runmodule, runargs, NULL);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100303 Py_DECREF(runpy);
304 Py_DECREF(runmodule);
305 Py_DECREF(module);
306 Py_DECREF(runargs);
307 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200308 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100309 }
310 Py_DECREF(result);
311 return 0;
312}
313
314
315static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200316pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100317{
318 const wchar_t *filename = config->run_filename;
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700319 if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
320 return pymain_exit_err_print();
321 }
Inada Naoki10654c12019-04-01 18:35:20 +0900322 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100323 if (fp == NULL) {
324 char *cfilename_buffer;
325 const char *cfilename;
326 int err = errno;
327 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
328 if (cfilename_buffer != NULL)
329 cfilename = cfilename_buffer;
330 else
331 cfilename = "<unprintable file name>";
332 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200333 config->program_name, cfilename, err, strerror(err));
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100334 PyMem_RawFree(cfilename_buffer);
335 return 2;
336 }
337
338 if (config->skip_source_first_line) {
339 int ch;
340 /* Push back first newline so line numbers remain the same */
341 while ((ch = getc(fp)) != EOF) {
342 if (ch == '\n') {
343 (void)ungetc(ch, fp);
344 break;
345 }
346 }
347 }
348
349 struct _Py_stat_struct sb;
350 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
351 fprintf(stderr,
352 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200353 config->program_name, filename);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100354 fclose(fp);
355 return 1;
356 }
357
358 /* call pending calls like signal handlers (SIGINT) */
359 if (Py_MakePendingCalls() == -1) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100360 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200361 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100362 }
363
364 PyObject *unicode, *bytes = NULL;
365 const char *filename_str;
366
367 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
368 if (unicode != NULL) {
369 bytes = PyUnicode_EncodeFSDefault(unicode);
370 Py_DECREF(unicode);
371 }
372 if (bytes != NULL) {
373 filename_str = PyBytes_AsString(bytes);
374 }
375 else {
376 PyErr_Clear();
377 filename_str = "<filename encoding error>";
378 }
379
380 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
381 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
382 Py_XDECREF(bytes);
383 return (run != 0);
384}
385
386
Victor Stinner12083282019-05-17 23:05:29 +0200387static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200388pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100389{
Victor Stinner20004952019-03-26 02:31:11 +0100390 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100391 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200392 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100393 }
Miss Islington (bot)1813d312020-06-24 09:45:38 -0700394 PyObject *startup_obj = PyUnicode_DecodeFSDefault(startup);
395 if (startup_obj == NULL) {
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700396 return pymain_err_print(exitcode);
397 }
Miss Islington (bot)1813d312020-06-24 09:45:38 -0700398 if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
399 Py_DECREF(startup_obj);
400 return pymain_err_print(exitcode);
401 }
402 Py_DECREF(startup_obj);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100403
404 FILE *fp = _Py_fopen(startup, "r");
405 if (fp == NULL) {
406 int save_errno = errno;
Miss Islington (bot)1813d312020-06-24 09:45:38 -0700407 PyErr_Clear();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100408 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100409
Victor Stinner12083282019-05-17 23:05:29 +0200410 errno = save_errno;
411 PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
412
413 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100414 }
415
416 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
417 PyErr_Clear();
418 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200419 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100420}
421
422
Victor Stinner12083282019-05-17 23:05:29 +0200423/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
424 Return 0 otherwise. */
425static int
426pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100427{
428 PyObject *sys, *hook, *result;
429 sys = PyImport_ImportModule("sys");
430 if (sys == NULL) {
431 goto error;
432 }
433
434 hook = PyObject_GetAttrString(sys, "__interactivehook__");
435 Py_DECREF(sys);
436 if (hook == NULL) {
437 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200438 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100439 }
440
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700441 if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
442 goto error;
443 }
444
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100445 result = _PyObject_CallNoArg(hook);
446 Py_DECREF(hook);
447 if (result == NULL) {
448 goto error;
449 }
450 Py_DECREF(result);
451
Victor Stinner12083282019-05-17 23:05:29 +0200452 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100453
454error:
455 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200456 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100457}
458
459
460static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200461pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100462{
463 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100464 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200465 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200466
467 int exitcode;
468 if (pymain_run_startup(config, cf, &exitcode)) {
469 return exitcode;
470 }
471
472 if (pymain_run_interactive_hook(&exitcode)) {
473 return exitcode;
474 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100475 }
476
477 /* call pending calls like signal handlers (SIGINT) */
478 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200479 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100480 }
481
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700482 if (PySys_Audit("cpython.run_stdin", NULL) < 0) {
483 return pymain_exit_err_print();
484 }
485
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100486 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
487 return (run != 0);
488}
489
490
491static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200492pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100493{
494 /* Check this environment variable at the end, to give programs the
495 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200496 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100497 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200498 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100499 }
500
Victor Stinner331a6a52019-05-27 16:39:22 +0200501 if (!(config->inspect && stdin_is_interactive(config) && config_run_code(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100502 return;
503 }
504
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100505 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200506 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200507 if (pymain_run_interactive_hook(exitcode)) {
508 return;
509 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100510
511 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
512 *exitcode = (res != 0);
513}
514
515
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200516static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100517pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100518{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100519 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
520 /* pymain_run_stdin() modify the config */
Victor Stinner331a6a52019-05-27 16:39:22 +0200521 PyConfig *config = &interp->config;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200522
523 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100524 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100525 /* If filename is a package (ex: directory or ZIP file) which contains
526 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200527 prepended to sys.path.
528
Victor Stinner12083282019-05-17 23:05:29 +0200529 Otherwise, main_importer_path is left unchanged. */
530 if (pymain_get_importer(config->run_filename, &main_importer_path,
531 exitcode)) {
532 return;
533 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100534 }
535
Victor Stinnerd3b19192018-07-25 10:21:03 +0200536 if (main_importer_path != NULL) {
537 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200538 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200539 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100540 }
Victor Stinner20004952019-03-26 02:31:11 +0100541 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100542 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200543 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
544 if (res < 0) {
545 goto error;
546 }
Victor Stinner19760862017-12-20 01:41:59 +0100547
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200548 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100549 if (pymain_sys_path_add_path0(interp, path0) < 0) {
550 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200551 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100552 }
Victor Stinner19760862017-12-20 01:41:59 +0100553 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100554 }
Victor Stinner19760862017-12-20 01:41:59 +0100555 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800556
Miss Islington (bot)92e836c2019-06-12 17:36:03 -0700557 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800558
Victor Stinner62be7632019-03-01 13:10:14 +0100559 pymain_header(config);
560 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800561
Victor Stinner62be7632019-03-01 13:10:14 +0100562 if (config->run_command) {
563 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800564 }
Victor Stinner62be7632019-03-01 13:10:14 +0100565 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200566 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800567 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200568 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200569 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200570 }
Victor Stinner62be7632019-03-01 13:10:14 +0100571 else if (config->run_filename != NULL) {
572 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200573 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800574 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100575 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800576 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100577
Victor Stinner62be7632019-03-01 13:10:14 +0100578 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200579 goto done;
580
581error:
Victor Stinner12083282019-05-17 23:05:29 +0200582 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200583
584done:
585 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800586}
587
Victor Stinnera7368ac2017-11-15 18:11:45 -0800588
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100589/* --- pymain_main() ---------------------------------------------- */
590
591static void
592pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800593{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100594 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200595
596 /* Free global variables which cannot be freed in Py_Finalize():
597 configuration options set before Py_Initialize() which should
598 remain valid after Py_Finalize(), since
599 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100600 _PyPathConfig_ClearGlobal();
601 _Py_ClearStandardStreamEncoding();
602 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100603 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100604}
Victor Stinner94540602017-12-16 04:54:22 +0100605
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200606
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100607static int
608exit_sigint(void)
609{
610 /* bpo-1054041: We need to exit via the
611 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
612 * If we don't, a calling process such as a shell may not know
613 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
614#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
615 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
616 perror("signal"); /* Impossible in normal environments. */
617 } else {
618 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100619 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100620 /* If setting SIG_DFL failed, or kill failed to terminate us,
621 * there isn't much else we can do aside from an error code. */
622#endif /* HAVE_GETPID && !MS_WINDOWS */
623#ifdef MS_WINDOWS
624 /* cmd.exe detects this, prints ^C, and offers to terminate. */
625 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
626 return STATUS_CONTROL_C_EXIT;
627#else
628 return SIGINT + 128;
629#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200630}
631
632
Victor Stinner2f549082019-03-29 15:13:46 +0100633static void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +0200634pymain_exit_error(PyStatus status)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200635{
Victor Stinner331a6a52019-05-27 16:39:22 +0200636 if (_PyStatus_IS_EXIT(status)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200637 /* If it's an error rather than a regular exit, leave Python runtime
Victor Stinner331a6a52019-05-27 16:39:22 +0200638 alive: Py_ExitStatusException() uses the current exception and use
Victor Stinner4cb525a2019-04-26 13:05:47 +0200639 sys.stdout in this case. */
640 pymain_free();
641 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200642 Py_ExitStatusException(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100643}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200644
Victor Stinnerdfe88472019-03-01 12:14:41 +0100645
Victor Stinner2f549082019-03-29 15:13:46 +0100646int
Victor Stinner331a6a52019-05-27 16:39:22 +0200647Py_RunMain(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100648{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100649 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100650
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200651 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200652
Victor Stinnerdfe88472019-03-01 12:14:41 +0100653 if (Py_FinalizeEx() < 0) {
654 /* Value unlikely to be confused with a non-error exit status or
655 other special meaning */
656 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100657 }
658
Victor Stinner62be7632019-03-01 13:10:14 +0100659 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100660
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800661 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100662 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800663 }
664
Victor Stinnerdfe88472019-03-01 12:14:41 +0100665 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100666}
Victor Stinnerc1834442019-03-18 22:24:28 +0100667
Victor Stinner2f549082019-03-29 15:13:46 +0100668
669static int
670pymain_main(_PyArgv *args)
671{
Victor Stinner331a6a52019-05-27 16:39:22 +0200672 PyStatus status = pymain_init(args);
673 if (_PyStatus_IS_EXIT(status)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200674 pymain_free();
Victor Stinner331a6a52019-05-27 16:39:22 +0200675 return status.exitcode;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200676 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200677 if (_PyStatus_EXCEPTION(status)) {
678 pymain_exit_error(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100679 }
680
Victor Stinner331a6a52019-05-27 16:39:22 +0200681 return Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100682}
683
684
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800685int
686Py_Main(int argc, wchar_t **argv)
687{
Victor Stinner62be7632019-03-01 13:10:14 +0100688 _PyArgv args = {
689 .argc = argc,
690 .use_bytes_argv = 0,
691 .bytes_argv = NULL,
692 .wchar_argv = argv};
693 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000694}
695
Victor Stinner94540602017-12-16 04:54:22 +0100696
697int
Victor Stinner331a6a52019-05-27 16:39:22 +0200698Py_BytesMain(int argc, char **argv)
Victor Stinner94540602017-12-16 04:54:22 +0100699{
Victor Stinner62be7632019-03-01 13:10:14 +0100700 _PyArgv args = {
701 .argc = argc,
702 .use_bytes_argv = 1,
703 .bytes_argv = argv,
704 .wchar_argv = NULL};
705 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100706}
707
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000708#ifdef __cplusplus
709}
710#endif