blob: e117ef29e54dccc547e128970b23d899f1415240 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Victor Stinnerf684d832019-03-01 03:44:13 +01004#include "pycore_coreconfig.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
21#ifdef _MSC_VER
22# include <crtdbg.h> /* STATUS_CONTROL_C_EXIT */
23#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 Stinner6dcb5422019-03-05 02:44:12 +010036static _PyInitError
Victor Stinner5ac27a52019-03-27 13:40:14 +010037pymain_init(const _PyArgv *args)
Victor Stinner95e2cbf2019-03-01 16:25:19 +010038{
39 _PyInitError err;
40
41 err = _PyRuntime_Initialize();
42 if (_Py_INIT_FAILED(err)) {
43 return err;
44 }
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 Stinnerd929f182019-03-27 18:28:46 +010055 _PyPreConfig preconfig = _PyPreConfig_INIT;
56 /* Set to -1 to enable them depending on the LC_CTYPE locale and the
57 environment variables (PYTHONUTF8 and PYTHONCOERCECLOCALE) */
58 preconfig.coerce_c_locale = -1;
59 preconfig.utf8_mode = -1;
Victor Stinner70005ac2019-05-02 15:25:34 -040060 err = _Py_PreInitializeFromPyArgv(&preconfig, args);
Victor Stinner6dcb5422019-03-05 02:44:12 +010061 if (_Py_INIT_FAILED(err)) {
Victor Stinnerf72346c2019-03-25 17:54:58 +010062 return err;
Victor Stinner6dcb5422019-03-05 02:44:12 +010063 }
64
Victor Stinnerd929f182019-03-27 18:28:46 +010065 /* pass NULL as the config: config is read from command line arguments,
66 environment variables, configuration files */
Victor Stinner5ac27a52019-03-27 13:40:14 +010067 if (args->use_bytes_argv) {
Victor Stinnerd929f182019-03-27 18:28:46 +010068 return _Py_InitializeFromArgs(NULL, args->argc, args->bytes_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010069 }
Victor Stinner5ac27a52019-03-27 13:40:14 +010070 else {
Victor Stinnerd929f182019-03-27 18:28:46 +010071 return _Py_InitializeFromWideArgs(NULL, args->argc, args->wchar_argv);
Victor Stinner484f20d2019-03-27 02:04:16 +010072 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +010073}
74
75
76/* --- pymain_run_python() ---------------------------------------- */
77
78/* Non-zero if filename, command (-c) or module (-m) is set
79 on the command line */
80#define RUN_CODE(config) \
81 (config->run_command != NULL || config->run_filename != NULL \
82 || config->run_module != NULL)
83
84/* Return non-zero is stdin is a TTY or if -i command line option is used */
85static int
86stdin_is_interactive(const _PyCoreConfig *config)
87{
88 return (isatty(fileno(stdin)) || config->interactive);
89}
90
91
92static PyObject *
93pymain_get_importer(const wchar_t *filename)
94{
95 PyObject *sys_path0 = NULL, *importer;
96
97 sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename));
98 if (sys_path0 == NULL) {
99 goto error;
100 }
101
102 importer = PyImport_GetImporter(sys_path0);
103 if (importer == NULL) {
104 goto error;
105 }
106
107 if (importer == Py_None) {
108 Py_DECREF(sys_path0);
109 Py_DECREF(importer);
110 return NULL;
111 }
112
113 Py_DECREF(importer);
114 return sys_path0;
115
116error:
117 Py_XDECREF(sys_path0);
118 PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n");
119 PyErr_Print();
120 return NULL;
121}
122
123
124static int
125pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
126{
127 _Py_IDENTIFIER(path);
128 PyObject *sys_path;
129 PyObject *sysdict = interp->sysdict;
130 if (sysdict != NULL) {
131 sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path);
132 if (sys_path == NULL && PyErr_Occurred()) {
133 goto error;
134 }
135 }
136 else {
137 sys_path = NULL;
138 }
139 if (sys_path == NULL) {
140 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
141 goto error;
142 }
143
144 if (PyList_Insert(sys_path, 0, path0)) {
145 goto error;
146 }
147 return 0;
148
149error:
150 PyErr_Print();
151 return -1;
152}
153
154
155static void
156pymain_header(const _PyCoreConfig *config)
157{
158 if (config->quiet) {
159 return;
160 }
161
162 if (!config->verbose && (RUN_CODE(config) || !stdin_is_interactive(config))) {
163 return;
164 }
165
166 fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
167 if (config->site_import) {
168 fprintf(stderr, "%s\n", COPYRIGHT);
169 }
170}
171
172
173static void
174pymain_import_readline(const _PyCoreConfig *config)
175{
Victor Stinner20004952019-03-26 02:31:11 +0100176 if (config->isolated) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100177 return;
178 }
179 if (!config->inspect && RUN_CODE(config)) {
180 return;
181 }
182 if (!isatty(fileno(stdin))) {
183 return;
184 }
185
186 PyObject *mod = PyImport_ImportModule("readline");
187 if (mod == NULL) {
188 PyErr_Clear();
189 }
190 else {
191 Py_DECREF(mod);
192 }
193}
194
195
196static int
197pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
198{
199 PyObject *unicode, *bytes;
200 int ret;
201
202 unicode = PyUnicode_FromWideChar(command, -1);
203 if (unicode == NULL) {
204 goto error;
205 }
206
207 bytes = PyUnicode_AsUTF8String(unicode);
208 Py_DECREF(unicode);
209 if (bytes == NULL) {
210 goto error;
211 }
212
213 ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
214 Py_DECREF(bytes);
215 return (ret != 0);
216
217error:
218 PySys_WriteStderr("Unable to decode the command from the command line:\n");
219 PyErr_Print();
220 return 1;
221}
222
223
224static int
225pymain_run_module(const wchar_t *modname, int set_argv0)
226{
227 PyObject *module, *runpy, *runmodule, *runargs, *result;
228 runpy = PyImport_ImportModule("runpy");
229 if (runpy == NULL) {
230 fprintf(stderr, "Could not import runpy module\n");
231 PyErr_Print();
232 return -1;
233 }
234 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
235 if (runmodule == NULL) {
236 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
237 PyErr_Print();
238 Py_DECREF(runpy);
239 return -1;
240 }
241 module = PyUnicode_FromWideChar(modname, wcslen(modname));
242 if (module == NULL) {
243 fprintf(stderr, "Could not convert module name to unicode\n");
244 PyErr_Print();
245 Py_DECREF(runpy);
246 Py_DECREF(runmodule);
247 return -1;
248 }
249 runargs = Py_BuildValue("(Oi)", module, set_argv0);
250 if (runargs == NULL) {
251 fprintf(stderr,
252 "Could not create arguments for runpy._run_module_as_main\n");
253 PyErr_Print();
254 Py_DECREF(runpy);
255 Py_DECREF(runmodule);
256 Py_DECREF(module);
257 return -1;
258 }
259 result = PyObject_Call(runmodule, runargs, NULL);
260 if (result == NULL) {
261 PyErr_Print();
262 }
263 Py_DECREF(runpy);
264 Py_DECREF(runmodule);
265 Py_DECREF(module);
266 Py_DECREF(runargs);
267 if (result == NULL) {
268 return -1;
269 }
270 Py_DECREF(result);
271 return 0;
272}
273
274
275static int
276pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf)
277{
278 const wchar_t *filename = config->run_filename;
Inada Naoki10654c12019-04-01 18:35:20 +0900279 FILE *fp = _Py_wfopen(filename, L"rb");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100280 if (fp == NULL) {
281 char *cfilename_buffer;
282 const char *cfilename;
283 int err = errno;
284 cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
285 if (cfilename_buffer != NULL)
286 cfilename = cfilename_buffer;
287 else
288 cfilename = "<unprintable file name>";
289 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
290 config->program, cfilename, err, strerror(err));
291 PyMem_RawFree(cfilename_buffer);
292 return 2;
293 }
294
295 if (config->skip_source_first_line) {
296 int ch;
297 /* Push back first newline so line numbers remain the same */
298 while ((ch = getc(fp)) != EOF) {
299 if (ch == '\n') {
300 (void)ungetc(ch, fp);
301 break;
302 }
303 }
304 }
305
306 struct _Py_stat_struct sb;
307 if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
308 fprintf(stderr,
309 "%ls: '%ls' is a directory, cannot continue\n",
310 config->program, filename);
311 fclose(fp);
312 return 1;
313 }
314
315 /* call pending calls like signal handlers (SIGINT) */
316 if (Py_MakePendingCalls() == -1) {
317 PyErr_Print();
318 fclose(fp);
319 return 1;
320 }
321
322 PyObject *unicode, *bytes = NULL;
323 const char *filename_str;
324
325 unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
326 if (unicode != NULL) {
327 bytes = PyUnicode_EncodeFSDefault(unicode);
328 Py_DECREF(unicode);
329 }
330 if (bytes != NULL) {
331 filename_str = PyBytes_AsString(bytes);
332 }
333 else {
334 PyErr_Clear();
335 filename_str = "<filename encoding error>";
336 }
337
338 /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
339 int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
340 Py_XDECREF(bytes);
341 return (run != 0);
342}
343
344
345static void
346pymain_run_startup(_PyCoreConfig *config, PyCompilerFlags *cf)
347{
Victor Stinner20004952019-03-26 02:31:11 +0100348 const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100349 if (startup == NULL) {
350 return;
351 }
352
353 FILE *fp = _Py_fopen(startup, "r");
354 if (fp == NULL) {
355 int save_errno = errno;
356 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
357 errno = save_errno;
358
359 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
360 startup);
361 PyErr_Print();
362 return;
363 }
364
365 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
366 PyErr_Clear();
367 fclose(fp);
368}
369
370
371static void
372pymain_run_interactive_hook(void)
373{
374 PyObject *sys, *hook, *result;
375 sys = PyImport_ImportModule("sys");
376 if (sys == NULL) {
377 goto error;
378 }
379
380 hook = PyObject_GetAttrString(sys, "__interactivehook__");
381 Py_DECREF(sys);
382 if (hook == NULL) {
383 PyErr_Clear();
384 return;
385 }
386
387 result = _PyObject_CallNoArg(hook);
388 Py_DECREF(hook);
389 if (result == NULL) {
390 goto error;
391 }
392 Py_DECREF(result);
393
394 return;
395
396error:
397 PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
398 PyErr_Print();
399}
400
401
402static int
403pymain_run_stdin(_PyCoreConfig *config, PyCompilerFlags *cf)
404{
405 if (stdin_is_interactive(config)) {
406 Py_InspectFlag = 0; /* do exit on SystemExit */
407 config->inspect = 0;
408 pymain_run_startup(config, cf);
409 pymain_run_interactive_hook();
410 }
411
412 /* call pending calls like signal handlers (SIGINT) */
413 if (Py_MakePendingCalls() == -1) {
414 PyErr_Print();
415 return 1;
416 }
417
418 int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
419 return (run != 0);
420}
421
422
423static void
424pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode)
425{
426 /* Check this environment variable at the end, to give programs the
427 opportunity to set it from Python. */
Victor Stinner20004952019-03-26 02:31:11 +0100428 if (!Py_InspectFlag && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100429 Py_InspectFlag = 1;
430 config->inspect = 1;
431 }
432
433 if (!(Py_InspectFlag && stdin_is_interactive(config) && RUN_CODE(config))) {
434 return;
435 }
436
437 Py_InspectFlag = 0;
438 config->inspect = 0;
439 pymain_run_interactive_hook();
440
441 int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
442 *exitcode = (res != 0);
443}
444
445
446static _PyInitError
Victor Stinner5ac27a52019-03-27 13:40:14 +0100447pymain_run_python(int *exitcode)
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100448{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100449 _PyInitError err;
Victor Stinner5ac27a52019-03-27 13:40:14 +0100450
451 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
452 /* pymain_run_stdin() modify the config */
Victor Stinnerd3b19192018-07-25 10:21:03 +0200453 _PyCoreConfig *config = &interp->core_config;
454
455 PyObject *main_importer_path = NULL;
Victor Stinner62be7632019-03-01 13:10:14 +0100456 if (config->run_filename != NULL) {
Victor Stinnerd5dda982017-12-13 17:31:16 +0100457 /* If filename is a package (ex: directory or ZIP file) which contains
458 __main__.py, main_importer_path is set to filename and will be
Victor Stinnerd3b19192018-07-25 10:21:03 +0200459 prepended to sys.path.
460
461 Otherwise, main_importer_path is set to NULL. */
Victor Stinner62be7632019-03-01 13:10:14 +0100462 main_importer_path = pymain_get_importer(config->run_filename);
Victor Stinnerd5dda982017-12-13 17:31:16 +0100463 }
464
Victor Stinnerd3b19192018-07-25 10:21:03 +0200465 if (main_importer_path != NULL) {
466 if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) {
Victor Stinnerdfe88472019-03-01 12:14:41 +0100467 err = _Py_INIT_EXIT(1);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200468 goto done;
469 }
Victor Stinnerd5dda982017-12-13 17:31:16 +0100470 }
Victor Stinner20004952019-03-26 02:31:11 +0100471 else if (!config->isolated) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100472 PyObject *path0 = NULL;
473 if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) {
474 if (path0 == NULL) {
475 err = _Py_INIT_NO_MEMORY();
476 goto done;
477 }
Victor Stinner19760862017-12-20 01:41:59 +0100478
Victor Stinnerdcf61712019-03-19 16:09:27 +0100479 if (pymain_sys_path_add_path0(interp, path0) < 0) {
480 Py_DECREF(path0);
481 err = _Py_INIT_EXIT(1);
482 goto done;
483 }
Victor Stinner19760862017-12-20 01:41:59 +0100484 Py_DECREF(path0);
Victor Stinner19760862017-12-20 01:41:59 +0100485 }
Victor Stinner19760862017-12-20 01:41:59 +0100486 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800487
Guido van Rossum495da292019-03-07 12:38:08 -0800488 PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION};
Victor Stinnera7368ac2017-11-15 18:11:45 -0800489
Victor Stinner62be7632019-03-01 13:10:14 +0100490 pymain_header(config);
491 pymain_import_readline(config);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800492
Victor Stinner62be7632019-03-01 13:10:14 +0100493 if (config->run_command) {
494 *exitcode = pymain_run_command(config->run_command, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800495 }
Victor Stinner62be7632019-03-01 13:10:14 +0100496 else if (config->run_module) {
497 *exitcode = (pymain_run_module(config->run_module, 1) != 0);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800498 }
Victor Stinnerd3b19192018-07-25 10:21:03 +0200499 else if (main_importer_path != NULL) {
500 int sts = pymain_run_module(L"__main__", 0);
Victor Stinnerdfe88472019-03-01 12:14:41 +0100501 *exitcode = (sts != 0);
Victor Stinnerd3b19192018-07-25 10:21:03 +0200502 }
Victor Stinner62be7632019-03-01 13:10:14 +0100503 else if (config->run_filename != NULL) {
504 *exitcode = pymain_run_file(config, &cf);
Victor Stinner72ec3192018-08-02 19:34:20 +0200505 }
Victor Stinnera7368ac2017-11-15 18:11:45 -0800506 else {
Victor Stinner62be7632019-03-01 13:10:14 +0100507 *exitcode = pymain_run_stdin(config, &cf);
Victor Stinnera7368ac2017-11-15 18:11:45 -0800508 }
Victor Stinner9cfc0022017-12-20 19:36:46 +0100509
Victor Stinner62be7632019-03-01 13:10:14 +0100510 pymain_repl(config, &cf, exitcode);
Victor Stinnerdfe88472019-03-01 12:14:41 +0100511 err = _Py_INIT_OK();
Victor Stinnerd3b19192018-07-25 10:21:03 +0200512
513done:
514 Py_XDECREF(main_importer_path);
Victor Stinnerdfe88472019-03-01 12:14:41 +0100515 return err;
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800516}
517
Victor Stinnera7368ac2017-11-15 18:11:45 -0800518
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100519/* --- pymain_main() ---------------------------------------------- */
520
521static void
522pymain_free(void)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800523{
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100524 _PyImport_Fini2();
Victor Stinner99fcc612019-04-29 13:04:07 +0200525
526 /* Free global variables which cannot be freed in Py_Finalize():
527 configuration options set before Py_Initialize() which should
528 remain valid after Py_Finalize(), since
529 Py_Initialize()-Py_Finalize() can be called multiple times. */
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100530 _PyPathConfig_ClearGlobal();
531 _Py_ClearStandardStreamEncoding();
532 _Py_ClearArgcArgv();
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100533 _PyRuntime_Finalize();
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100534}
Victor Stinner94540602017-12-16 04:54:22 +0100535
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200536
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100537static int
538exit_sigint(void)
539{
540 /* bpo-1054041: We need to exit via the
541 * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
542 * If we don't, a calling process such as a shell may not know
543 * about the user's ^C. https://www.cons.org/cracauer/sigint.html */
544#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
545 if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
546 perror("signal"); /* Impossible in normal environments. */
547 } else {
548 kill(getpid(), SIGINT);
Victor Stinner19760862017-12-20 01:41:59 +0100549 }
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100550 /* If setting SIG_DFL failed, or kill failed to terminate us,
551 * there isn't much else we can do aside from an error code. */
552#endif /* HAVE_GETPID && !MS_WINDOWS */
553#ifdef MS_WINDOWS
554 /* cmd.exe detects this, prints ^C, and offers to terminate. */
555 /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
556 return STATUS_CONTROL_C_EXIT;
557#else
558 return SIGINT + 128;
559#endif /* !MS_WINDOWS */
Victor Stinner1dc6e392018-07-25 02:49:17 +0200560}
561
562
Victor Stinner2f549082019-03-29 15:13:46 +0100563static void _Py_NO_RETURN
564pymain_exit_error(_PyInitError err)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200565{
Victor Stinnerdb719752019-05-01 05:35:33 +0200566 if (_Py_INIT_IS_EXIT(err)) {
Victor Stinner4cb525a2019-04-26 13:05:47 +0200567 /* If it's an error rather than a regular exit, leave Python runtime
568 alive: _Py_ExitInitError() uses the current exception and use
569 sys.stdout in this case. */
570 pymain_free();
571 }
Victor Stinner2f549082019-03-29 15:13:46 +0100572 _Py_ExitInitError(err);
573}
Victor Stinner1dc6e392018-07-25 02:49:17 +0200574
Victor Stinnerdfe88472019-03-01 12:14:41 +0100575
Victor Stinner2f549082019-03-29 15:13:46 +0100576int
577_Py_RunMain(void)
578{
Victor Stinnerdfe88472019-03-01 12:14:41 +0100579 int exitcode = 0;
Victor Stinner2f549082019-03-29 15:13:46 +0100580
581 _PyInitError err = pymain_run_python(&exitcode);
Victor Stinnerdfe88472019-03-01 12:14:41 +0100582 if (_Py_INIT_FAILED(err)) {
Victor Stinner2f549082019-03-29 15:13:46 +0100583 pymain_exit_error(err);
Victor Stinnerdfe88472019-03-01 12:14:41 +0100584 }
585
586 if (Py_FinalizeEx() < 0) {
587 /* Value unlikely to be confused with a non-error exit status or
588 other special meaning */
589 exitcode = 120;
Victor Stinner19760862017-12-20 01:41:59 +0100590 }
591
Victor Stinner62be7632019-03-01 13:10:14 +0100592 pymain_free();
Victor Stinner94540602017-12-16 04:54:22 +0100593
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800594 if (_Py_UnhandledKeyboardInterrupt) {
Victor Stinner95e2cbf2019-03-01 16:25:19 +0100595 exitcode = exit_sigint();
Gregory P. Smith38f11cc2019-02-16 12:57:40 -0800596 }
597
Victor Stinnerdfe88472019-03-01 12:14:41 +0100598 return exitcode;
Victor Stinner2f549082019-03-29 15:13:46 +0100599}
Victor Stinnerc1834442019-03-18 22:24:28 +0100600
Victor Stinner2f549082019-03-29 15:13:46 +0100601
602static int
603pymain_main(_PyArgv *args)
604{
605 _PyInitError err = pymain_init(args);
606 if (_Py_INIT_FAILED(err)) {
607 pymain_exit_error(err);
608 }
609
610 return _Py_RunMain();
Victor Stinner94540602017-12-16 04:54:22 +0100611}
612
613
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800614int
615Py_Main(int argc, wchar_t **argv)
616{
Victor Stinner62be7632019-03-01 13:10:14 +0100617 _PyArgv args = {
618 .argc = argc,
619 .use_bytes_argv = 0,
620 .bytes_argv = NULL,
621 .wchar_argv = argv};
622 return pymain_main(&args);
Guido van Rossum667d7041995-08-04 04:20:48 +0000623}
624
Victor Stinner94540602017-12-16 04:54:22 +0100625
626int
627_Py_UnixMain(int argc, char **argv)
628{
Victor Stinner62be7632019-03-01 13:10:14 +0100629 _PyArgv args = {
630 .argc = argc,
631 .use_bytes_argv = 1,
632 .bytes_argv = argv,
633 .wchar_argv = NULL};
634 return pymain_main(&args);
Victor Stinner94540602017-12-16 04:54:22 +0100635}
636
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637#ifdef __cplusplus
638}
639#endif