blob: 2cc891f61aadd1b3ecf99efd35e8fc0979b3bd57 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Victor Stinner4f98f462020-04-15 04:01:58 +02004#include "pycore_initconfig.h" // _PyArgv
5#include "pycore_interp.h" // _PyInterpreterState.sysdict
6#include "pycore_pathconfig.h" // _PyPathConfig_ComputeSysPath0()
7#include "pycore_pylifecycle.h" // _Py_PreInitializeFromPyArgv()
8#include "pycore_pystate.h" // _PyInterpreterState_GET()
Guido van Rossum667d7041995-08-04 04:20:48 +00009
Victor Stinner95e2cbf2019-03-01 16:25:19 +010010/* Includes for exit_sigint() */
Victor Stinner4f98f462020-04-15 04:01:58 +020011#include <stdio.h> // perror()
Victor Stinner95e2cbf2019-03-01 16:25:19 +010012#ifdef HAVE_SIGNAL_H
Victor Stinner4f98f462020-04-15 04:01:58 +020013# include <signal.h> // SIGINT
Guido van Rossuma075ce11997-12-05 21:56:45 +000014#endif
Victor Stinner95e2cbf2019-03-01 16:25:19 +010015#if defined(HAVE_GETPID) && defined(HAVE_UNISTD_H)
Victor Stinner4f98f462020-04-15 04:01:58 +020016# include <unistd.h> // getpid()
Victor Stinner95e2cbf2019-03-01 16:25:19 +010017#endif
Erik Janssens925af1d2019-05-21 12:11:11 +020018#ifdef MS_WINDOWS
Victor Stinner4f98f462020-04-15 04:01:58 +020019# include <windows.h> // STATUS_CONTROL_C_EXIT
Victor Stinner95e2cbf2019-03-01 16:25:19 +010020#endif
21/* End of includes for exit_sigint() */
Guido van Rossuma075ce11997-12-05 21:56:45 +000022
Guido van Rossuma22865e2000-09-05 04:41:18 +000023#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000024 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
25 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000026
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#ifdef __cplusplus
28extern "C" {
29#endif
30
Victor Stinner95e2cbf2019-03-01 16:25:19 +010031/* --- pymain_init() ---------------------------------------------- */
32
Victor Stinner331a6a52019-05-27 16:39:22 +020033static PyStatus
Victor Stinner5ac27a52019-03-27 13:40:14 +010034pymain_init(const _PyArgv *args)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010035{
Victor Stinner331a6a52019-05-27 16:39:22 +020036 PyStatus status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010037
Victor Stinner331a6a52019-05-27 16:39:22 +020038 status = _PyRuntime_Initialize();
39 if (_PyStatus_EXCEPTION(status)) {
40 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010041 }
42
Victor Stinner331a6a52019-05-27 16:39:22 +020043 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +020044 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner441b10c2019-09-28 04:28:35 +020045
Victor Stinner331a6a52019-05-27 16:39:22 +020046 status = _Py_PreInitializeFromPyArgv(&preconfig, args);
47 if (_PyStatus_EXCEPTION(status)) {
48 return status;
Victor Stinner6dcb5422019-03-05 02:44:12 +010049 }
50
Victor Stinner331a6a52019-05-27 16:39:22 +020051 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +020052 PyConfig_InitPythonConfig(&config);
Victor Stinnercab5d072019-05-17 19:01:14 +020053
Victor Stinnerd929f182019-03-27 18:28:46 +010054 /* pass NULL as the config: config is read from command line arguments,
55 environment variables, configuration files */
Victor Stinner5ac27a52019-03-27 13:40:14 +010056 if (args->use_bytes_argv) {
Victor Stinner331a6a52019-05-27 16:39:22 +020057 status = PyConfig_SetBytesArgv(&config, args->argc, args->bytes_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010058 }
Victor Stinner5ac27a52019-03-27 13:40:14 +010059 else {
Victor Stinner331a6a52019-05-27 16:39:22 +020060 status = PyConfig_SetArgv(&config, args->argc, args->wchar_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010061 }
Victor Stinner331a6a52019-05-27 16:39:22 +020062 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner67310022019-07-01 19:52:45 +020063 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020064 }
65
66 status = Py_InitializeFromConfig(&config);
67 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner67310022019-07-01 19:52:45 +020068 goto done;
Victor Stinner331a6a52019-05-27 16:39:22 +020069 }
Victor Stinner67310022019-07-01 19:52:45 +020070 status = _PyStatus_OK();
71
72done:
73 PyConfig_Clear(&config);
74 return status;
Victor Stinner95e2cbf2019-03-01 16:25:19 +010075}
76
77
78/* --- pymain_run_python() ---------------------------------------- */
79
80/* Non-zero if filename, command (-c) or module (-m) is set
81 on the command line */
Victor Stinner331a6a52019-05-27 16:39:22 +020082static inline int config_run_code(const PyConfig *config)
83{
84 return (config->run_command != NULL
85 || config->run_filename != NULL
86 || config->run_module != NULL);
87}
88
Victor Stinner95e2cbf2019-03-01 16:25:19 +010089
90/* Return non-zero is stdin is a TTY or if -i command line option is used */
91static int
Victor Stinner331a6a52019-05-27 16:39:22 +020092stdin_is_interactive(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010093{
94 return (isatty(fileno(stdin)) || config->interactive);
95}
96
97
Victor Stinner12083282019-05-17 23:05:29 +020098/* Display the current Python exception and return an exitcode */
99static int
100pymain_err_print(int *exitcode_p)
101{
102 int exitcode;
103 if (_Py_HandleSystemExit(&exitcode)) {
104 *exitcode_p = exitcode;
105 return 1;
106 }
107
108 PyErr_Print();
109 return 0;
110}
111
112
113static int
114pymain_exit_err_print(void)
115{
116 int exitcode = 1;
117 pymain_err_print(&exitcode);
118 return exitcode;
119}
120
121
122/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
123 Return 0 otherwise. */
124static int
125pymain_get_importer(const wchar_t *filename, PyObject **importer_p, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100126{
127 PyObject *sys_path0 = NULL, *importer;
128
129 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
130 if (sys_path0 == NULL) {
131 goto error;
132 }
133
134 importer = PyImport_GetImporter(sys_path0);
135 if (importer == NULL) {
136 goto error;
137 }
138
139 if (importer == Py_None) {
140 Py_DECREF(sys_path0);
141 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200142 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100143 }
144
145 Py_DECREF(importer);
Victor Stinner12083282019-05-17 23:05:29 +0200146 *importer_p = sys_path0;
147 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100148
149error:
150 Py_XDECREF(sys_path0);
Victor Stinner12083282019-05-17 23:05:29 +0200151
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100152 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
Victor Stinner12083282019-05-17 23:05:29 +0200153 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100154}
155
156
157static int
158pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
159{
160 _Py_IDENTIFIER(path);
161 PyObject *sys_path;
162 PyObject *sysdict = interp->sysdict;
163 if (sysdict != NULL) {
164 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
165 if (sys_path == NULL && PyErr_Occurred()) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200166 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100167 }
168 }
169 else {
170 sys_path = NULL;
171 }
172 if (sys_path == NULL) {
173 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200174 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100175 }
176
177 if (PyList_Insert(sys_path, 0, path0)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200178 return -1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100179 }
180 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100181}
182
183
184static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200185pymain_header(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100186{
187 if (config->quiet) {
188 return;
189 }
190
Victor Stinner331a6a52019-05-27 16:39:22 +0200191 if (!config->verbose && (config_run_code(config) || !stdin_is_interactive(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100192 return;
193 }
194
195 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
196 if (config->site_import) {
197 fprintf(stderr, "%s\n", COPYRIGHT);
198 }
199}
200
201
202static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200203pymain_import_readline(const PyConfig *config)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100204{
Victor Stinner20004952019-03-26 02:31:11 +0100205 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100206 return;
207 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200208 if (!config->inspect && config_run_code(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100209 return;
210 }
211 if (!isatty(fileno(stdin))) {
212 return;
213 }
214
215 PyObject *mod = PyImport_ImportModule("readline");
216 if (mod == NULL) {
217 PyErr_Clear();
218 }
219 else {
220 Py_DECREF(mod);
221 }
222}
223
224
225static int
226pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
227{
228 PyObject *unicode, *bytes;
229 int ret;
230
231 unicode = PyUnicode_FromWideChar(command, -1);
232 if (unicode == NULL) {
233 goto error;
234 }
235
Steve Dowere226e832019-07-01 16:03:53 -0700236 if (PySys_Audit("cpython.run_command", "O", unicode) < 0) {
237 return pymain_exit_err_print();
238 }
239
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100240 bytes = PyUnicode_AsUTF8String(unicode);
241 Py_DECREF(unicode);
242 if (bytes == NULL) {
243 goto error;
244 }
245
246 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
247 Py_DECREF(bytes);
248 return (ret != 0);
249
250error:
251 PySys_WriteStderr("Unable to decode the command from the command line:\n");
Victor Stinner12083282019-05-17 23:05:29 +0200252 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100253}
254
255
256static int
257pymain_run_module(const wchar_t *modname, int set_argv0)
258{
259 PyObject *module, *runpy, *runmodule, *runargs, *result;
Steve Dowere226e832019-07-01 16:03:53 -0700260 if (PySys_Audit("cpython.run_module", "u", modname) < 0) {
261 return pymain_exit_err_print();
262 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100263 runpy = PyImport_ImportModule("runpy");
264 if (runpy == NULL) {
265 fprintf(stderr, "Could not import runpy module\n");
Victor Stinner12083282019-05-17 23:05:29 +0200266 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100267 }
268 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
269 if (runmodule == NULL) {
270 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100271 Py_DECREF(runpy);
Victor Stinner12083282019-05-17 23:05:29 +0200272 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100273 }
274 module = PyUnicode_FromWideChar(modname, wcslen(modname));
275 if (module == NULL) {
276 fprintf(stderr, "Could not convert module name to unicode\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100277 Py_DECREF(runpy);
278 Py_DECREF(runmodule);
Victor Stinner12083282019-05-17 23:05:29 +0200279 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100280 }
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300281 runargs = PyTuple_Pack(2, module, set_argv0 ? Py_True : Py_False);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100282 if (runargs == NULL) {
283 fprintf(stderr,
284 "Could not create arguments for runpy._run_module_as_main\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100285 Py_DECREF(runpy);
286 Py_DECREF(runmodule);
287 Py_DECREF(module);
Victor Stinner12083282019-05-17 23:05:29 +0200288 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100289 }
Thomas Graingera68a2ad2020-09-22 16:53:03 +0100290 _Py_UnhandledKeyboardInterrupt = 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100291 result = PyObject_Call(runmodule, runargs, NULL);
Thomas Graingera68a2ad2020-09-22 16:53:03 +0100292 if (!result && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
293 _Py_UnhandledKeyboardInterrupt = 1;
294 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100295 Py_DECREF(runpy);
296 Py_DECREF(runmodule);
297 Py_DECREF(module);
298 Py_DECREF(runargs);
299 if (result == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200300 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100301 }
302 Py_DECREF(result);
303 return 0;
304}
305
306
307static int
Victor Stinnerda7933e2020-04-13 03:04:28 +0200308pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100309{
310 const wchar_t *filename = config->run_filename;
Steve Dowere226e832019-07-01 16:03:53 -0700311 if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
312 return pymain_exit_err_print();
313 }
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{
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300382 int ret;
383 PyObject *startup_obj = NULL;
384 if (!config->use_environment) {
385 return 0;
386 }
387#ifdef MS_WINDOWS
388 const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP");
389 if (wstartup == NULL || wstartup[0] == L'\0') {
390 return 0;
391 }
392 PyObject *startup_bytes = NULL;
393 startup_obj = PyUnicode_FromWideChar(wstartup, wcslen(wstartup));
394 if (startup_obj == NULL) {
395 goto error;
396 }
397 startup_bytes = PyUnicode_EncodeFSDefault(startup_obj);
398 if (startup_bytes == NULL) {
399 goto error;
400 }
401 const char *startup = PyBytes_AS_STRING(startup_bytes);
402#else
Victor Stinner20004952019-03-26 02:31:11 +0100403 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100404 if (startup == NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200405 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100406 }
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300407 startup_obj = PyUnicode_DecodeFSDefault(startup);
Serhiy Storchaka6c6810d2020-06-24 08:46:05 +0300408 if (startup_obj == NULL) {
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300409 goto error;
Steve Dowere226e832019-07-01 16:03:53 -0700410 }
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300411#endif
Serhiy Storchaka6c6810d2020-06-24 08:46:05 +0300412 if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300413 goto error;
Serhiy Storchaka6c6810d2020-06-24 08:46:05 +0300414 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100415
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300416#ifdef MS_WINDOWS
417 FILE *fp = _Py_wfopen(wstartup, L"r");
418#else
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100419 FILE *fp = _Py_fopen(startup, "r");
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300420#endif
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100421 if (fp == NULL) {
422 int save_errno = errno;
Serhiy Storchaka6c6810d2020-06-24 08:46:05 +0300423 PyErr_Clear();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100424 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100425
Victor Stinner12083282019-05-17 23:05:29 +0200426 errno = save_errno;
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300427 PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL);
428 goto error;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100429 }
430
431 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
432 PyErr_Clear();
433 fclose(fp);
Serhiy Storchakaa7dc7142020-06-24 19:46:30 +0300434 ret = 0;
435
436done:
437#ifdef MS_WINDOWS
438 Py_XDECREF(startup_bytes);
439#endif
440 Py_XDECREF(startup_obj);
441 return ret;
442
443error:
444 ret = pymain_err_print(exitcode);
445 goto done;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100446}
447
448
Victor Stinner12083282019-05-17 23:05:29 +0200449/* Write an exitcode into *exitcode and return 1 if we have to exit Python.
450 Return 0 otherwise. */
451static int
452pymain_run_interactive_hook(int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100453{
454 PyObject *sys, *hook, *result;
455 sys = PyImport_ImportModule("sys");
456 if (sys == NULL) {
457 goto error;
458 }
459
460 hook = PyObject_GetAttrString(sys, "__interactivehook__");
461 Py_DECREF(sys);
462 if (hook == NULL) {
463 PyErr_Clear();
Victor Stinner12083282019-05-17 23:05:29 +0200464 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100465 }
466
Steve Dowere226e832019-07-01 16:03:53 -0700467 if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) {
468 goto error;
469 }
470
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100471 result = _PyObject_CallNoArg(hook);
472 Py_DECREF(hook);
473 if (result == NULL) {
474 goto error;
475 }
476 Py_DECREF(result);
477
Victor Stinner12083282019-05-17 23:05:29 +0200478 return 0;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100479
480error:
481 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
Victor Stinner12083282019-05-17 23:05:29 +0200482 return pymain_err_print(exitcode);
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100483}
484
485
486static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200487pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100488{
489 if (stdin_is_interactive(config)) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100490 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200491 Py_InspectFlag = 0; /* do exit on SystemExit */
Victor Stinner12083282019-05-17 23:05:29 +0200492
493 int exitcode;
494 if (pymain_run_startup(config, cf, &exitcode)) {
495 return exitcode;
496 }
497
498 if (pymain_run_interactive_hook(&exitcode)) {
499 return exitcode;
500 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100501 }
502
503 /* call pending calls like signal handlers (SIGINT) */
504 if (Py_MakePendingCalls() == -1) {
Victor Stinner12083282019-05-17 23:05:29 +0200505 return pymain_exit_err_print();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100506 }
507
Steve Dowere226e832019-07-01 16:03:53 -0700508 if (PySys_Audit("cpython.run_stdin", NULL) < 0) {
509 return pymain_exit_err_print();
510 }
511
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100512 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
513 return (run != 0);
514}
515
516
517static void
Victor Stinner331a6a52019-05-27 16:39:22 +0200518pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100519{
520 /* Check this environment variable at the end, to give programs the
521 opportunity to set it from Python. */
Victor Stinnerc96be812019-05-14 17:34:56 +0200522 if (!config->inspect && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100523 config->inspect = 1;
Victor Stinnerc96be812019-05-14 17:34:56 +0200524 Py_InspectFlag = 1;
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100525 }
526
Victor Stinner331a6a52019-05-27 16:39:22 +0200527 if (!(config->inspect && stdin_is_interactive(config) && config_run_code(config))) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100528 return;
529 }
530
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100531 config->inspect = 0;
Victor Stinnerc96be812019-05-14 17:34:56 +0200532 Py_InspectFlag = 0;
Victor Stinner12083282019-05-17 23:05:29 +0200533 if (pymain_run_interactive_hook(exitcode)) {
534 return;
535 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100536
537 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
538 *exitcode = (res != 0);
539}
540
541
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200542static void
Victor Stinner5ac27a52019-03-27 13:40:14 +0100543pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100544{
Victor Stinner81a7be32020-04-14 15:14:01 +0200545 PyInterpreterState *interp = _PyInterpreterState_GET();
Victor Stinner5ac27a52019-03-27 13:40:14 +0100546 /* pymain_run_stdin() modify the config */
Victor Stinnerda7933e2020-04-13 03:04:28 +0200547 PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200548
549 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100550 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100551 /* If filename is a package (ex: directory or ZIP file) which contains
552 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200553 prepended to sys.path.
554
Victor Stinner12083282019-05-17 23:05:29 +0200555 Otherwise, main_importer_path is left unchanged. */
556 if (pymain_get_importer(config->run_filename, &main_importer_path,
557 exitcode)) {
558 return;
559 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100560 }
561
Victor Stinnerd3b19192018-07-25 10:21:03 +0200562 if (main_importer_path != NULL) {
563 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200564 goto error;
Victor Stinnerd3b19192018-07-25 10:21:03 +0200565 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100566 }
Victor Stinner20004952019-03-26 02:31:11 +0100567 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100568 PyObject *path0 = NULL;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200569 int res = _PyPathConfig_ComputeSysPath0(&config->argv, &path0);
570 if (res < 0) {
571 goto error;
572 }
Victor Stinner19760862017-12-20 01:41:59 +0100573
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200574 if (res > 0) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100575 if (pymain_sys_path_add_path0(interp, path0) < 0) {
576 Py_DECREF(path0);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200577 goto error;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100578 }
Victor Stinner19760862017-12-20 01:41:59 +0100579 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100580 }
Victor Stinner19760862017-12-20 01:41:59 +0100581 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800582
Victor Stinner37d66d72019-06-13 02:16:41 +0200583 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Victor Stinnera7368ac2017-11-15 18:11:45 -0800584
Victor Stinner62be7632019-03-01 13:10:14 +0100585 pymain_header(config);
586 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800587
Victor Stinner62be7632019-03-01 13:10:14 +0100588 if (config->run_command) {
589 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800590 }
Victor Stinner62be7632019-03-01 13:10:14 +0100591 else if (config->run_module) {
Victor Stinner12083282019-05-17 23:05:29 +0200592 *exitcode = pymain_run_module(config->run_module, 1);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800593 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200594 else if (main_importer_path != NULL) {
Victor Stinner12083282019-05-17 23:05:29 +0200595 *exitcode = pymain_run_module(L"__main__", 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200596 }
Victor Stinner62be7632019-03-01 13:10:14 +0100597 else if (config->run_filename != NULL) {
598 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200599 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800600 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100601 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800602 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100603
Victor Stinner62be7632019-03-01 13:10:14 +0100604 pymain_repl(config, &cf, exitcode);
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200605 goto done;
606
607error:
Victor Stinner12083282019-05-17 23:05:29 +0200608 *exitcode = pymain_exit_err_print();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200609
610done:
611 Py_XDECREF(main_importer_path);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800612}
613
Victor Stinnera7368ac2017-11-15 18:11:45 -0800614
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100615/* --- pymain_main() ---------------------------------------------- */
616
617static void
618pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800619{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100620 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200621
622 /* Free global variables which cannot be freed in Py_Finalize():
623 configuration options set before Py_Initialize() which should
624 remain valid after Py_Finalize(), since
625 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100626 _PyPathConfig_ClearGlobal();
627 _Py_ClearStandardStreamEncoding();
628 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100629 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100630}
Victor Stinner94540602017-12-16 04:54:22 +0100631
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200632
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100633static int
634exit_sigint(void)
635{
636 /* bpo-1054041: We need to exit via the
637 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
638 * If we don't, a calling process such as a shell may not know
639 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
640#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
641 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
642 perror("signal"); /* Impossible in normal environments. */
643 } else {
644 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100645 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100646 /* If setting SIG_DFL failed, or kill failed to terminate us,
647 * there isn't much else we can do aside from an error code. */
648#endif /* HAVE_GETPID && !MS_WINDOWS */
649#ifdef MS_WINDOWS
650 /* cmd.exe detects this, prints ^C, and offers to terminate. */
651 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
652 return STATUS_CONTROL_C_EXIT;
653#else
654 return SIGINT + 128;
655#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200656}
657
658
Victor Stinner2f549082019-03-29 15:13:46 +0100659static void _Py_NO_RETURN
Victor Stinner331a6a52019-05-27 16:39:22 +0200660pymain_exit_error(PyStatus status)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200661{
Victor Stinner331a6a52019-05-27 16:39:22 +0200662 if (_PyStatus_IS_EXIT(status)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200663 /* If it's an error rather than a regular exit, leave Python runtime
Victor Stinner331a6a52019-05-27 16:39:22 +0200664 alive: Py_ExitStatusException() uses the current exception and use
Victor Stinner4cb525a2019-04-26 13:05:47 +0200665 sys.stdout in this case. */
666 pymain_free();
667 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200668 Py_ExitStatusException(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100669}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200670
Victor Stinnerdfe88472019-03-01 12:14:41 +0100671
Victor Stinner2f549082019-03-29 15:13:46 +0100672int
Victor Stinner331a6a52019-05-27 16:39:22 +0200673Py_RunMain(void)
Victor Stinner2f549082019-03-29 15:13:46 +0100674{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100675 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100676
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200677 pymain_run_python(&exitcode);
Victor Stinnerae239f62019-05-16 17:02:56 +0200678
Victor Stinnerdfe88472019-03-01 12:14:41 +0100679 if (Py_FinalizeEx() < 0) {
680 /* Value unlikely to be confused with a non-error exit status or
681 other special meaning */
682 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100683 }
684
Victor Stinner62be7632019-03-01 13:10:14 +0100685 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100686
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800687 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100688 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800689 }
690
Victor Stinnerdfe88472019-03-01 12:14:41 +0100691 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100692}
Victor Stinnerc1834442019-03-18 22:24:28 +0100693
Victor Stinner2f549082019-03-29 15:13:46 +0100694
695static int
696pymain_main(_PyArgv *args)
697{
Victor Stinner331a6a52019-05-27 16:39:22 +0200698 PyStatus status = pymain_init(args);
699 if (_PyStatus_IS_EXIT(status)) {
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200700 pymain_free();
Victor Stinner331a6a52019-05-27 16:39:22 +0200701 return status.exitcode;
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200702 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200703 if (_PyStatus_EXCEPTION(status)) {
704 pymain_exit_error(status);
Victor Stinner2f549082019-03-29 15:13:46 +0100705 }
706
Victor Stinner331a6a52019-05-27 16:39:22 +0200707 return Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100708}
709
710
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800711int
712Py_Main(int argc, wchar_t **argv)
713{
Victor Stinner62be7632019-03-01 13:10:14 +0100714 _PyArgv args = {
715 .argc = argc,
716 .use_bytes_argv = 0,
717 .bytes_argv = NULL,
718 .wchar_argv = argv};
719 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000720}
721
Victor Stinner94540602017-12-16 04:54:22 +0100722
723int
Victor Stinner331a6a52019-05-27 16:39:22 +0200724Py_BytesMain(int argc, char **argv)
Victor Stinner94540602017-12-16 04:54:22 +0100725{
Victor Stinner62be7632019-03-01 13:10:14 +0100726 _PyArgv args = {
727 .argc = argc,
728 .use_bytes_argv = 1,
729 .bytes_argv = argv,
730 .wchar_argv = NULL};
731 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100732}
733
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734#ifdef __cplusplus
735}
736#endif