blob: 929417fe775f28cc2d0ad857d4643c07da71d6aa [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 Stinner95e2cbf2019-03-01 16:25:19 +01009/* Includes for exit_sigint() */
10#include <stdio.h> /* perror() */
11#ifdef HAVE_SIGNAL_H
12# include <signal.h> /* SIGINT */
Guido van Rossuma075ce11997-12-05 21:56:45 +000013#endif
Victor Stinner95e2cbf2019-03-01 16:25:19 +010014#if defined(HAVE_GETPID) && defined(HAVE_UNISTD_H)
15# include <unistd.h> /* getpid() */
16#endif
Erik Janssens925af1d2019-05-21 12:11:11 +020017#ifdef MS_WINDOWS
18# include <windows.h> /* STATUS_CONTROL_C_EXIT */
Victor Stinner95e2cbf2019-03-01 16:25:19 +010019#endif
20/* End of includes for exit_sigint() */
Guido van Rossuma075ce11997-12-05 21:56:45 +000021
Guido van Rossuma22865e2000-09-05 04:41:18 +000022#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000023 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
24 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000025
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026#ifdef __cplusplus
27extern "C" {
28#endif
29
Victor Stinner95e2cbf2019-03-01 16:25:19 +010030/* --- pymain_init() ---------------------------------------------- */
31
Victor Stinner331a6a52019-05-27 16:39:22 +020032static PyStatus
Victor Stinner5ac27a52019-03-27 13:40:14 +010033pymain_init(const _PyArgv *args)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010034{
Victor Stinner331a6a52019-05-27 16:39:22 +020035 PyStatus status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010036
Victor Stinner331a6a52019-05-27 16:39:22 +020037 status = _PyRuntime_Initialize();
38 if (_PyStatus_EXCEPTION(status)) {
39 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010040 }
41
Victor Stinner331a6a52019-05-27 16:39:22 +020042 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +020043 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner441b10c2019-09-28 04:28:35 +020044
Victor Stinner331a6a52019-05-27 16:39:22 +020045 status = _Py_PreInitializeFromPyArgv(&preconfig, args);
46 if (_PyStatus_EXCEPTION(status)) {
47 return status;
Victor Stinner6dcb5422019-03-05 02:44:12 +010048 }
49
Victor Stinner331a6a52019-05-27 16:39:22 +020050 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +020051 PyConfig_InitPythonConfig(&config);
Victor Stinnercab5d072019-05-17 19:01:14 +020052
Victor Stinnerd929f182019-03-27 18:28:46 +010053 /* pass NULL as the config: config is read from command line arguments,
54 environment variables, configuration files */
Victor Stinner5ac27a52019-03-27 13:40:14 +010055 if (args->use_bytes_argv) {
Victor Stinner331a6a52019-05-27 16:39:22 +020056 status = PyConfig_SetBytesArgv(&config, args->argc, args->bytes_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010057 }
Victor Stinner5ac27a52019-03-27 13:40:14 +010058 else {
Victor Stinner331a6a52019-05-27 16:39:22 +020059 status = PyConfig_SetArgv(&config, args->argc, args->wchar_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010060 }
Victor Stinner331a6a52019-05-27 16:39:22 +020061 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner67310022019-07-01 19:52:45 +020062 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020063 }
64
65 status = Py_InitializeFromConfig(&config);
66 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner67310022019-07-01 19:52:45 +020067 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020068 }
Victor Stinner67310022019-07-01 19:52:45 +020069 status = _PyStatus_OK();
70
71done:
72 PyConfig_Clear(&config);
73 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010074}
75
76
77/* --- pymain_run_python() ---------------------------------------- */
78
79/* Non-zero if filename, command (-c) or module (-m) is set
80 on the command line */
Victor Stinner331a6a52019-05-27 16:39:22 +020081static inline int config_run_code(const PyConfig *config)
82{
83 return (config->run_command != NULL
84 || config->run_filename != NULL
85 || config->run_module != NULL);
86}
87
Victor Stinner95e2cbf2019-03-01 16:25:19 +010088
89/* Return non-zero is stdin is a TTY or if -i command line option is used */
90static int
Victor Stinner331a6a52019-05-27 16:39:22 +020091stdin_is_interactive(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010092{
93 return (isatty(fileno(stdin)) || config->interactive);
94}
95
96
Victor Stinner12083282019-05-17 23:05:29 +020097/* Display the current Python exception and return an exitcode */
98static int
99pymain_err_print(int *exitcode_p)
100{
101 int exitcode;
102 if (_Py_HandleSystemExit(&exitcode)) {
103 *exitcode_p = exitcode;
104 return 1;
105 }
106
107 PyErr_Print();
108 return 0;
109}
110
111
112static int
113pymain_exit_err_print(void)
114{
115 int exitcode = 1;
116 pymain_err_print(&exitcode);
117 return exitcode;
118}
119
120
121/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
122 Return 0 otherwise. */
123static int
124pymain_get_importer(const wchar_t *filename, PyObject **importer_p, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100125{
126 PyObject *sys_path0 = NULL, *importer;
127
128 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
129 if (sys_path0 == NULL) {
130 goto error;
131 }
132
133 importer = PyImport_GetImporter(sys_path0);
134 if (importer == NULL) {
135 goto error;
136 }
137
138 if (importer == Py_None) {
139 Py_DECREF(sys_path0);
140 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200141 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100142 }
143
144 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200145 *importer_p = sys_path0;
146 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100147
148error:
149 Py_XDECREF(sys_path0);
Victor Stinner12083282019-05-17 23:05:29 +0200150
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100151 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
Victor Stinner12083282019-05-17 23:05:29 +0200152 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100153}
154
155
156static int
157pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
158{
159 _Py_IDENTIFIER(path);
160 PyObject *sys_path;
161 PyObject *sysdict = interp->sysdict;
162 if (sysdict != NULL) {
163 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
164 if (sys_path == NULL && PyErr_Occurred()) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200165 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100166 }
167 }
168 else {
169 sys_path = NULL;
170 }
171 if (sys_path == NULL) {
172 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200173 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100174 }
175
176 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200177 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100178 }
179 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100180}
181
182
183static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200184pymain_header(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100185{
186 if (config->quiet) {
187 return;
188 }
189
Victor Stinner331a6a52019-05-27 16:39:22 +0200190 if (!config->verbose && (config_run_code(config) || !stdin_is_interactive(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100191 return;
192 }
193
194 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
195 if (config->site_import) {
196 fprintf(stderr, "%s\n", COPYRIGHT);
197 }
198}
199
200
201static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200202pymain_import_readline(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100203{
Victor Stinner20004952019-03-26 02:31:11 +0100204 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100205 return;
206 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200207 if (!config->inspect && config_run_code(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100208 return;
209 }
210 if (!isatty(fileno(stdin))) {
211 return;
212 }
213
214 PyObject *mod = PyImport_ImportModule("readline");
215 if (mod == NULL) {
216 PyErr_Clear();
217 }
218 else {
219 Py_DECREF(mod);
220 }
221}
222
223
224static int
225pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
226{
227 PyObject *unicode, *bytes;
228 int ret;
229
230 unicode = PyUnicode_FromWideChar(command, -1);
231 if (unicode == NULL) {
232 goto error;
233 }
234
Steve Dowere226e832019-07-01 16:03:53 -0700235 if (PySys_Audit("cpython.run_command", "O", unicode) < 0) {
236 return pymain_exit_err_print();
237 }
238
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100239 bytes = PyUnicode_AsUTF8String(unicode);
240 Py_DECREF(unicode);
241 if (bytes == NULL) {
242 goto error;
243 }
244
245 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
246 Py_DECREF(bytes);
247 return (ret != 0);
248
249error:
250 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200251 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100252}
253
254
255static int
256pymain_run_module(const wchar_t *modname, int set_argv0)
257{
258 PyObject *module, *runpy, *runmodule, *runargs, *result;
Steve Dowere226e832019-07-01 16:03:53 -0700259 if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
260 return pymain_exit_err_print();
261 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100262 runpy = PyImport_ImportModule("runpy");
263 if (runpy == NULL) {
264 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200265 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100266 }
267 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
268 if (runmodule == NULL) {
269 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100270 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200271 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100272 }
273 module = PyUnicode_FromWideChar(modname, wcslen(modname));
274 if (module == NULL) {
275 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100276 Py_DECREF(runpy);
277 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200278 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100279 }
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300280 runargs = PyTuple_Pack(2, module, set_argv0 ? Py_True : Py_False);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100281 if (runargs == NULL) {
282 fprintf(stderr,
283 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100284 Py_DECREF(runpy);
285 Py_DECREF(runmodule);
286 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200287 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100288 }
289 result = PyObject_Call(runmodule, runargs, NULL);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100290 Py_DECREF(runpy);
291 Py_DECREF(runmodule);
292 Py_DECREF(module);
293 Py_DECREF(runargs);
294 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200295 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100296 }
297 Py_DECREF(result);
298 return 0;
299}
300
301
302static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200303pymain_run_file(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100304{
305 const wchar_t *filename = config->run_filename;
Steve Dowere226e832019-07-01 16:03:53 -0700306 if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
307 return pymain_exit_err_print();
308 }
Inada Naoki10654c12019-04-01 18:35:20 +0900309 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100310 if (fp == NULL) {
311 char *cfilename_buffer;
312 const char *cfilename;
313 int err = errno;
314 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
315 if (cfilename_buffer != NULL)
316 cfilename = cfilename_buffer;
317 else
318 cfilename = "<unprintable file name>";
319 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200320 config->program_name, cfilename, err, strerror(err));
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100321 PyMem_RawFree(cfilename_buffer);
322 return 2;
323 }
324
325 if (config->skip_source_first_line) {
326 int ch;
327 /* Push back first newline so line numbers remain the same */
328 while ((ch = getc(fp)) != EOF) {
329 if (ch == '\n') {
330 (void)ungetc(ch, fp);
331 break;
332 }
333 }
334 }
335
336 struct _Py_stat_struct sb;
337 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
338 fprintf(stderr,
339 "%ls: '%ls' is a directory, cannot continue\n",
Victor Stinnerfed02e12019-05-17 11:12:09 +0200340 config->program_name, filename);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100341 fclose(fp);
342 return 1;
343 }
344
345 /* call pending calls like signal handlers (SIGINT) */
346 if (Py_MakePendingCalls() == -1) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100347 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200348 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100349 }
350
351 PyObject *unicode, *bytes = NULL;
352 const char *filename_str;
353
354 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
355 if (unicode != NULL) {
356 bytes = PyUnicode_EncodeFSDefault(unicode);
357 Py_DECREF(unicode);
358 }
359 if (bytes != NULL) {
360 filename_str = PyBytes_AsString(bytes);
361 }
362 else {
363 PyErr_Clear();
364 filename_str = "<filename encoding error>";
365 }
366
367 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
368 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
369 Py_XDECREF(bytes);
370 return (run != 0);
371}
372
373
Victor Stinner12083282019-05-17 23:05:29 +0200374static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200375pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100376{
Victor Stinner20004952019-03-26 02:31:11 +0100377 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100378 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200379 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100380 }
Steve Dowere226e832019-07-01 16:03:53 -0700381 if (PySys_Audit("cpython.run_startup", "s", startup) < 0) {
382 return pymain_err_print(exitcode);
383 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100384
385 FILE *fp = _Py_fopen(startup, "r");
386 if (fp == NULL) {
387 int save_errno = errno;
388 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100389
Victor Stinner12083282019-05-17 23:05:29 +0200390 errno = save_errno;
391 PyErr_SetFromErrnoWithFilename(PyExc_OSError, startup);
392
393 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100394 }
395
396 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
397 PyErr_Clear();
398 fclose(fp);
Victor Stinner12083282019-05-17 23:05:29 +0200399 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100400}
401
402
Victor Stinner12083282019-05-17 23:05:29 +0200403/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
404 Return 0 otherwise. */
405static int
406pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100407{
408 PyObject *sys, *hook, *result;
409 sys = PyImport_ImportModule("sys");
410 if (sys == NULL) {
411 goto error;
412 }
413
414 hook = PyObject_GetAttrString(sys, "__interactivehook__");
415 Py_DECREF(sys);
416 if (hook == NULL) {
417 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200418 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100419 }
420
Steve Dowere226e832019-07-01 16:03:53 -0700421 if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
422 goto error;
423 }
424
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100425 result = _PyObject_CallNoArg(hook);
426 Py_DECREF(hook);
427 if (result == NULL) {
428 goto error;
429 }
430 Py_DECREF(result);
431
Victor Stinner12083282019-05-17 23:05:29 +0200432 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100433
434error:
435 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200436 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100437}
438
439
440static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200441pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100442{
443 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100444 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200445 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200446
447 int exitcode;
448 if (pymain_run_startup(config, cf, &exitcode)) {
449 return exitcode;
450 }
451
452 if (pymain_run_interactive_hook(&exitcode)) {
453 return exitcode;
454 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100455 }
456
457 /* call pending calls like signal handlers (SIGINT) */
458 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200459 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100460 }
461
Steve Dowere226e832019-07-01 16:03:53 -0700462 if (PySys_Audit("cpython.run_stdin", NULL) < 0) {
463 return pymain_exit_err_print();
464 }
465
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100466 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
467 return (run != 0);
468}
469
470
471static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200472pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100473{
474 /* Check this environment variable at the end, to give programs the
475 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200476 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100477 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200478 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100479 }
480
Victor Stinner331a6a52019-05-27 16:39:22 +0200481 if (!(config->inspect && stdin_is_interactive(config) && config_run_code(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100482 return;
483 }
484
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100485 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200486 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200487 if (pymain_run_interactive_hook(exitcode)) {
488 return;
489 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100490
491 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
492 *exitcode = (res != 0);
493}
494
495
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200496static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100497pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100498{
Victor Stinner5ac27a52019-03-27 13:40:14 +0100499 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
500 /* pymain_run_stdin() modify the config */
Victor Stinner331a6a52019-05-27 16:39:22 +0200501 PyConfig *config = &interp->config;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200502
503 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100504 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100505 /* If filename is a package (ex: directory or ZIP file) which contains
506 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200507 prepended to sys.path.
508
Victor Stinner12083282019-05-17 23:05:29 +0200509 Otherwise, main_importer_path is left unchanged. */
510 if (pymain_get_importer(config->run_filename, &main_importer_path,
511 exitcode)) {
512 return;
513 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100514 }
515
Victor Stinnerd3b19192018-07-25 10:21:03 +0200516 if (main_importer_path != NULL) {
517 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200518 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200519 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100520 }
Victor Stinner20004952019-03-26 02:31:11 +0100521 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100522 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200523 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
524 if (res < 0) {
525 goto error;
526 }
Victor Stinner19760862017-12-20 01:41:59 +0100527
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200528 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100529 if (pymain_sys_path_add_path0(interp, path0) < 0) {
530 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200531 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100532 }
Victor Stinner19760862017-12-20 01:41:59 +0100533 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100534 }
Victor Stinner19760862017-12-20 01:41:59 +0100535 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800536
Victor Stinner37d66d72019-06-13 02:16:41 +0200537 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800538
Victor Stinner62be7632019-03-01 13:10:14 +0100539 pymain_header(config);
540 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800541
Victor Stinner62be7632019-03-01 13:10:14 +0100542 if (config->run_command) {
543 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800544 }
Victor Stinner62be7632019-03-01 13:10:14 +0100545 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200546 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800547 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200548 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200549 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200550 }
Victor Stinner62be7632019-03-01 13:10:14 +0100551 else if (config->run_filename != NULL) {
552 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200553 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800554 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100555 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800556 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100557
Victor Stinner62be7632019-03-01 13:10:14 +0100558 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200559 goto done;
560
561error:
Victor Stinner12083282019-05-17 23:05:29 +0200562 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200563
564done:
565 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800566}
567
Victor Stinnera7368ac2017-11-15 18:11:45 -0800568
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100569/* --- pymain_main() ---------------------------------------------- */
570
571static void
572pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800573{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100574 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200575
576 /* Free global variables which cannot be freed in Py_Finalize():
577 configuration options set before Py_Initialize() which should
578 remain valid after Py_Finalize(), since
579 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100580 _PyPathConfig_ClearGlobal();
581 _Py_ClearStandardStreamEncoding();
582 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100583 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100584}
Victor Stinner94540602017-12-16 04:54:22 +0100585
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200586
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100587static int
588exit_sigint(void)
589{
590 /* bpo-1054041: We need to exit via the
591 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
592 * If we don't, a calling process such as a shell may not know
593 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
594#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
595 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
596 perror("signal"); /* Impossible in normal environments. */
597 } else {
598 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100599 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100600 /* If setting SIG_DFL failed, or kill failed to terminate us,
601 * there isn't much else we can do aside from an error code. */
602#endif /* HAVE_GETPID && !MS_WINDOWS */
603#ifdef MS_WINDOWS
604 /* cmd.exe detects this, prints ^C, and offers to terminate. */
605 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
606 return STATUS_CONTROL_C_EXIT;
607#else
608 return SIGINT + 128;
609#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200610}
611
612
Victor Stinner2f549082019-03-29 15:13:46 +0100613static void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +0200614pymain_exit_error(PyStatus status)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200615{
Victor Stinner331a6a52019-05-27 16:39:22 +0200616 if (_PyStatus_IS_EXIT(status)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200617 /* If it's an error rather than a regular exit, leave Python runtime
Victor Stinner331a6a52019-05-27 16:39:22 +0200618 alive: Py_ExitStatusException() uses the current exception and use
Victor Stinner4cb525a2019-04-26 13:05:47 +0200619 sys.stdout in this case. */
620 pymain_free();
621 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200622 Py_ExitStatusException(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100623}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200624
Victor Stinnerdfe88472019-03-01 12:14:41 +0100625
Victor Stinner2f549082019-03-29 15:13:46 +0100626int
Victor Stinner331a6a52019-05-27 16:39:22 +0200627Py_RunMain(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100628{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100629 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100630
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200631 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200632
Victor Stinnerdfe88472019-03-01 12:14:41 +0100633 if (Py_FinalizeEx() < 0) {
634 /* Value unlikely to be confused with a non-error exit status or
635 other special meaning */
636 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100637 }
638
Victor Stinner62be7632019-03-01 13:10:14 +0100639 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100640
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800641 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100642 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800643 }
644
Victor Stinnerdfe88472019-03-01 12:14:41 +0100645 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100646}
Victor Stinnerc1834442019-03-18 22:24:28 +0100647
Victor Stinner2f549082019-03-29 15:13:46 +0100648
649static int
650pymain_main(_PyArgv *args)
651{
Victor Stinner331a6a52019-05-27 16:39:22 +0200652 PyStatus status = pymain_init(args);
653 if (_PyStatus_IS_EXIT(status)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200654 pymain_free();
Victor Stinner331a6a52019-05-27 16:39:22 +0200655 return status.exitcode;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200656 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200657 if (_PyStatus_EXCEPTION(status)) {
658 pymain_exit_error(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100659 }
660
Victor Stinner331a6a52019-05-27 16:39:22 +0200661 return Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100662}
663
664
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800665int
666Py_Main(int argc, wchar_t **argv)
667{
Victor Stinner62be7632019-03-01 13:10:14 +0100668 _PyArgv args = {
669 .argc = argc,
670 .use_bytes_argv = 0,
671 .bytes_argv = NULL,
672 .wchar_argv = argv};
673 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000674}
675
Victor Stinner94540602017-12-16 04:54:22 +0100676
677int
Victor Stinner331a6a52019-05-27 16:39:22 +0200678Py_BytesMain(int argc, char **argv)
Victor Stinner94540602017-12-16 04:54:22 +0100679{
Victor Stinner62be7632019-03-01 13:10:14 +0100680 _PyArgv args = {
681 .argc = argc,
682 .use_bytes_argv = 1,
683 .bytes_argv = argv,
684 .wchar_argv = NULL};
685 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100686}
687
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688#ifdef __cplusplus
689}
690#endif