Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1 | /* Python interpreter main program */ |
| 2 | |
| 3 | #include "Python.h" |
Victor Stinner | f684d83 | 2019-03-01 03:44:13 +0100 | [diff] [blame] | 4 | #include "pycore_coreconfig.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 5 | #include "pycore_pylifecycle.h" |
| 6 | #include "pycore_pymem.h" |
| 7 | #include "pycore_pystate.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 8 | |
Victor Stinner | 6efcb6d | 2017-12-18 23:42:55 +0100 | [diff] [blame] | 9 | #ifdef __FreeBSD__ |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 10 | # include <fenv.h> /* fedisableexcept() */ |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 11 | #endif |
| 12 | |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 13 | /* Includes for exit_sigint() */ |
| 14 | #include <stdio.h> /* perror() */ |
| 15 | #ifdef HAVE_SIGNAL_H |
| 16 | # include <signal.h> /* SIGINT */ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 17 | #endif |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 18 | #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 Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 26 | #define COPYRIGHT \ |
Guido van Rossum | 36002d7 | 2001-07-18 16:59:46 +0000 | [diff] [blame] | 27 | "Type \"help\", \"copyright\", \"credits\" or \"license\" " \ |
| 28 | "for more information." |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 29 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 34 | /* --- pymain_init() ---------------------------------------------- */ |
| 35 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 36 | static _PyInitError |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 37 | pymain_init(const _PyArgv *args) |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 38 | { |
| 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 Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 55 | _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 Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 60 | if (args->use_bytes_argv) { |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 61 | err = _Py_PreInitializeFromArgs(&preconfig, |
| 62 | args->argc, args->bytes_argv); |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 63 | } |
| 64 | else { |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 65 | err = _Py_PreInitializeFromWideArgs(&preconfig, |
| 66 | args->argc, args->wchar_argv); |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 67 | } |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 68 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | f72346c | 2019-03-25 17:54:58 +0100 | [diff] [blame] | 69 | return err; |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 72 | /* pass NULL as the config: config is read from command line arguments, |
| 73 | environment variables, configuration files */ |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 74 | if (args->use_bytes_argv) { |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 75 | return _Py_InitializeFromArgs(NULL, args->argc, args->bytes_argv); |
Victor Stinner | 484f20d | 2019-03-27 02:04:16 +0100 | [diff] [blame] | 76 | } |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 77 | else { |
Victor Stinner | d929f18 | 2019-03-27 18:28:46 +0100 | [diff] [blame] | 78 | return _Py_InitializeFromWideArgs(NULL, args->argc, args->wchar_argv); |
Victor Stinner | 484f20d | 2019-03-27 02:04:16 +0100 | [diff] [blame] | 79 | } |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | /* --- pymain_run_python() ---------------------------------------- */ |
| 84 | |
| 85 | /* Non-zero if filename, command (-c) or module (-m) is set |
| 86 | on the command line */ |
| 87 | #define RUN_CODE(config) \ |
| 88 | (config->run_command != NULL || config->run_filename != NULL \ |
| 89 | || config->run_module != NULL) |
| 90 | |
| 91 | /* Return non-zero is stdin is a TTY or if -i command line option is used */ |
| 92 | static int |
| 93 | stdin_is_interactive(const _PyCoreConfig *config) |
| 94 | { |
| 95 | return (isatty(fileno(stdin)) || config->interactive); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static PyObject * |
| 100 | pymain_get_importer(const wchar_t *filename) |
| 101 | { |
| 102 | PyObject *sys_path0 = NULL, *importer; |
| 103 | |
| 104 | sys_path0 = PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 105 | if (sys_path0 == NULL) { |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | importer = PyImport_GetImporter(sys_path0); |
| 110 | if (importer == NULL) { |
| 111 | goto error; |
| 112 | } |
| 113 | |
| 114 | if (importer == Py_None) { |
| 115 | Py_DECREF(sys_path0); |
| 116 | Py_DECREF(importer); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | Py_DECREF(importer); |
| 121 | return sys_path0; |
| 122 | |
| 123 | error: |
| 124 | Py_XDECREF(sys_path0); |
| 125 | PySys_WriteStderr("Failed checking if argv[0] is an import path entry\n"); |
| 126 | PyErr_Print(); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int |
| 132 | pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0) |
| 133 | { |
| 134 | _Py_IDENTIFIER(path); |
| 135 | PyObject *sys_path; |
| 136 | PyObject *sysdict = interp->sysdict; |
| 137 | if (sysdict != NULL) { |
| 138 | sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path); |
| 139 | if (sys_path == NULL && PyErr_Occurred()) { |
| 140 | goto error; |
| 141 | } |
| 142 | } |
| 143 | else { |
| 144 | sys_path = NULL; |
| 145 | } |
| 146 | if (sys_path == NULL) { |
| 147 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path"); |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | if (PyList_Insert(sys_path, 0, path0)) { |
| 152 | goto error; |
| 153 | } |
| 154 | return 0; |
| 155 | |
| 156 | error: |
| 157 | PyErr_Print(); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static void |
| 163 | pymain_header(const _PyCoreConfig *config) |
| 164 | { |
| 165 | if (config->quiet) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (!config->verbose && (RUN_CODE(config) || !stdin_is_interactive(config))) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | fprintf(stderr, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform()); |
| 174 | if (config->site_import) { |
| 175 | fprintf(stderr, "%s\n", COPYRIGHT); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static void |
| 181 | pymain_import_readline(const _PyCoreConfig *config) |
| 182 | { |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 183 | if (config->isolated) { |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | if (!config->inspect && RUN_CODE(config)) { |
| 187 | return; |
| 188 | } |
| 189 | if (!isatty(fileno(stdin))) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | PyObject *mod = PyImport_ImportModule("readline"); |
| 194 | if (mod == NULL) { |
| 195 | PyErr_Clear(); |
| 196 | } |
| 197 | else { |
| 198 | Py_DECREF(mod); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static int |
| 204 | pymain_run_command(wchar_t *command, PyCompilerFlags *cf) |
| 205 | { |
| 206 | PyObject *unicode, *bytes; |
| 207 | int ret; |
| 208 | |
| 209 | unicode = PyUnicode_FromWideChar(command, -1); |
| 210 | if (unicode == NULL) { |
| 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | bytes = PyUnicode_AsUTF8String(unicode); |
| 215 | Py_DECREF(unicode); |
| 216 | if (bytes == NULL) { |
| 217 | goto error; |
| 218 | } |
| 219 | |
| 220 | ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf); |
| 221 | Py_DECREF(bytes); |
| 222 | return (ret != 0); |
| 223 | |
| 224 | error: |
| 225 | PySys_WriteStderr("Unable to decode the command from the command line:\n"); |
| 226 | PyErr_Print(); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | static int |
| 232 | pymain_run_module(const wchar_t *modname, int set_argv0) |
| 233 | { |
| 234 | PyObject *module, *runpy, *runmodule, *runargs, *result; |
| 235 | runpy = PyImport_ImportModule("runpy"); |
| 236 | if (runpy == NULL) { |
| 237 | fprintf(stderr, "Could not import runpy module\n"); |
| 238 | PyErr_Print(); |
| 239 | return -1; |
| 240 | } |
| 241 | runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); |
| 242 | if (runmodule == NULL) { |
| 243 | fprintf(stderr, "Could not access runpy._run_module_as_main\n"); |
| 244 | PyErr_Print(); |
| 245 | Py_DECREF(runpy); |
| 246 | return -1; |
| 247 | } |
| 248 | module = PyUnicode_FromWideChar(modname, wcslen(modname)); |
| 249 | if (module == NULL) { |
| 250 | fprintf(stderr, "Could not convert module name to unicode\n"); |
| 251 | PyErr_Print(); |
| 252 | Py_DECREF(runpy); |
| 253 | Py_DECREF(runmodule); |
| 254 | return -1; |
| 255 | } |
| 256 | runargs = Py_BuildValue("(Oi)", module, set_argv0); |
| 257 | if (runargs == NULL) { |
| 258 | fprintf(stderr, |
| 259 | "Could not create arguments for runpy._run_module_as_main\n"); |
| 260 | PyErr_Print(); |
| 261 | Py_DECREF(runpy); |
| 262 | Py_DECREF(runmodule); |
| 263 | Py_DECREF(module); |
| 264 | return -1; |
| 265 | } |
| 266 | result = PyObject_Call(runmodule, runargs, NULL); |
| 267 | if (result == NULL) { |
| 268 | PyErr_Print(); |
| 269 | } |
| 270 | Py_DECREF(runpy); |
| 271 | Py_DECREF(runmodule); |
| 272 | Py_DECREF(module); |
| 273 | Py_DECREF(runargs); |
| 274 | if (result == NULL) { |
| 275 | return -1; |
| 276 | } |
| 277 | Py_DECREF(result); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | static int |
| 283 | pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf) |
| 284 | { |
| 285 | const wchar_t *filename = config->run_filename; |
Inada Naoki | 10654c1 | 2019-04-01 18:35:20 +0900 | [diff] [blame] | 286 | FILE *fp = _Py_wfopen(filename, L"rb"); |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 287 | if (fp == NULL) { |
| 288 | char *cfilename_buffer; |
| 289 | const char *cfilename; |
| 290 | int err = errno; |
| 291 | cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL); |
| 292 | if (cfilename_buffer != NULL) |
| 293 | cfilename = cfilename_buffer; |
| 294 | else |
| 295 | cfilename = "<unprintable file name>"; |
| 296 | fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", |
| 297 | config->program, cfilename, err, strerror(err)); |
| 298 | PyMem_RawFree(cfilename_buffer); |
| 299 | return 2; |
| 300 | } |
| 301 | |
| 302 | if (config->skip_source_first_line) { |
| 303 | int ch; |
| 304 | /* Push back first newline so line numbers remain the same */ |
| 305 | while ((ch = getc(fp)) != EOF) { |
| 306 | if (ch == '\n') { |
| 307 | (void)ungetc(ch, fp); |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | struct _Py_stat_struct sb; |
| 314 | if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 315 | fprintf(stderr, |
| 316 | "%ls: '%ls' is a directory, cannot continue\n", |
| 317 | config->program, filename); |
| 318 | fclose(fp); |
| 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | /* call pending calls like signal handlers (SIGINT) */ |
| 323 | if (Py_MakePendingCalls() == -1) { |
| 324 | PyErr_Print(); |
| 325 | fclose(fp); |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | PyObject *unicode, *bytes = NULL; |
| 330 | const char *filename_str; |
| 331 | |
| 332 | unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 333 | if (unicode != NULL) { |
| 334 | bytes = PyUnicode_EncodeFSDefault(unicode); |
| 335 | Py_DECREF(unicode); |
| 336 | } |
| 337 | if (bytes != NULL) { |
| 338 | filename_str = PyBytes_AsString(bytes); |
| 339 | } |
| 340 | else { |
| 341 | PyErr_Clear(); |
| 342 | filename_str = "<filename encoding error>"; |
| 343 | } |
| 344 | |
| 345 | /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */ |
| 346 | int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf); |
| 347 | Py_XDECREF(bytes); |
| 348 | return (run != 0); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | static void |
| 353 | pymain_run_startup(_PyCoreConfig *config, PyCompilerFlags *cf) |
| 354 | { |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 355 | const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP"); |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 356 | if (startup == NULL) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | FILE *fp = _Py_fopen(startup, "r"); |
| 361 | if (fp == NULL) { |
| 362 | int save_errno = errno; |
| 363 | PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); |
| 364 | errno = save_errno; |
| 365 | |
| 366 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, |
| 367 | startup); |
| 368 | PyErr_Print(); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); |
| 373 | PyErr_Clear(); |
| 374 | fclose(fp); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | static void |
| 379 | pymain_run_interactive_hook(void) |
| 380 | { |
| 381 | PyObject *sys, *hook, *result; |
| 382 | sys = PyImport_ImportModule("sys"); |
| 383 | if (sys == NULL) { |
| 384 | goto error; |
| 385 | } |
| 386 | |
| 387 | hook = PyObject_GetAttrString(sys, "__interactivehook__"); |
| 388 | Py_DECREF(sys); |
| 389 | if (hook == NULL) { |
| 390 | PyErr_Clear(); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | result = _PyObject_CallNoArg(hook); |
| 395 | Py_DECREF(hook); |
| 396 | if (result == NULL) { |
| 397 | goto error; |
| 398 | } |
| 399 | Py_DECREF(result); |
| 400 | |
| 401 | return; |
| 402 | |
| 403 | error: |
| 404 | PySys_WriteStderr("Failed calling sys.__interactivehook__\n"); |
| 405 | PyErr_Print(); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | static int |
| 410 | pymain_run_stdin(_PyCoreConfig *config, PyCompilerFlags *cf) |
| 411 | { |
| 412 | if (stdin_is_interactive(config)) { |
| 413 | Py_InspectFlag = 0; /* do exit on SystemExit */ |
| 414 | config->inspect = 0; |
| 415 | pymain_run_startup(config, cf); |
| 416 | pymain_run_interactive_hook(); |
| 417 | } |
| 418 | |
| 419 | /* call pending calls like signal handlers (SIGINT) */ |
| 420 | if (Py_MakePendingCalls() == -1) { |
| 421 | PyErr_Print(); |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf); |
| 426 | return (run != 0); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | static void |
| 431 | pymain_repl(_PyCoreConfig *config, PyCompilerFlags *cf, int *exitcode) |
| 432 | { |
| 433 | /* Check this environment variable at the end, to give programs the |
| 434 | opportunity to set it from Python. */ |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 435 | if (!Py_InspectFlag && _Py_GetEnv(config->use_environment, "PYTHONINSPECT")) { |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 436 | Py_InspectFlag = 1; |
| 437 | config->inspect = 1; |
| 438 | } |
| 439 | |
| 440 | if (!(Py_InspectFlag && stdin_is_interactive(config) && RUN_CODE(config))) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | Py_InspectFlag = 0; |
| 445 | config->inspect = 0; |
| 446 | pymain_run_interactive_hook(); |
| 447 | |
| 448 | int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf); |
| 449 | *exitcode = (res != 0); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | static _PyInitError |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 454 | pymain_run_python(int *exitcode) |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 455 | { |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 456 | _PyInitError err; |
Victor Stinner | 5ac27a5 | 2019-03-27 13:40:14 +0100 | [diff] [blame] | 457 | |
| 458 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 459 | /* pymain_run_stdin() modify the config */ |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 460 | _PyCoreConfig *config = &interp->core_config; |
| 461 | |
| 462 | PyObject *main_importer_path = NULL; |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 463 | if (config->run_filename != NULL) { |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 464 | /* If filename is a package (ex: directory or ZIP file) which contains |
| 465 | __main__.py, main_importer_path is set to filename and will be |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 466 | prepended to sys.path. |
| 467 | |
| 468 | Otherwise, main_importer_path is set to NULL. */ |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 469 | main_importer_path = pymain_get_importer(config->run_filename); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 472 | if (main_importer_path != NULL) { |
| 473 | if (pymain_sys_path_add_path0(interp, main_importer_path) < 0) { |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 474 | err = _Py_INIT_EXIT(1); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 475 | goto done; |
| 476 | } |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 477 | } |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 478 | else if (!config->isolated) { |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 479 | PyObject *path0 = NULL; |
| 480 | if (_PyPathConfig_ComputeSysPath0(&config->argv, &path0)) { |
| 481 | if (path0 == NULL) { |
| 482 | err = _Py_INIT_NO_MEMORY(); |
| 483 | goto done; |
| 484 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 485 | |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 486 | if (pymain_sys_path_add_path0(interp, path0) < 0) { |
| 487 | Py_DECREF(path0); |
| 488 | err = _Py_INIT_EXIT(1); |
| 489 | goto done; |
| 490 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 491 | Py_DECREF(path0); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 492 | } |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 493 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 494 | |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 495 | PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}; |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 496 | |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 497 | pymain_header(config); |
| 498 | pymain_import_readline(config); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 499 | |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 500 | if (config->run_command) { |
| 501 | *exitcode = pymain_run_command(config->run_command, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 502 | } |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 503 | else if (config->run_module) { |
| 504 | *exitcode = (pymain_run_module(config->run_module, 1) != 0); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 505 | } |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 506 | else if (main_importer_path != NULL) { |
| 507 | int sts = pymain_run_module(L"__main__", 0); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 508 | *exitcode = (sts != 0); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 509 | } |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 510 | else if (config->run_filename != NULL) { |
| 511 | *exitcode = pymain_run_file(config, &cf); |
Victor Stinner | 72ec319 | 2018-08-02 19:34:20 +0200 | [diff] [blame] | 512 | } |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 513 | else { |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 514 | *exitcode = pymain_run_stdin(config, &cf); |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 515 | } |
Victor Stinner | 9cfc002 | 2017-12-20 19:36:46 +0100 | [diff] [blame] | 516 | |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 517 | pymain_repl(config, &cf, exitcode); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 518 | err = _Py_INIT_OK(); |
Victor Stinner | d3b1919 | 2018-07-25 10:21:03 +0200 | [diff] [blame] | 519 | |
| 520 | done: |
| 521 | Py_XDECREF(main_importer_path); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 522 | return err; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 525 | |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 526 | /* --- pymain_main() ---------------------------------------------- */ |
| 527 | |
| 528 | static void |
| 529 | pymain_free(void) |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 530 | { |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 531 | _PyImport_Fini2(); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 532 | |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 533 | /* Free global variables which cannot be freed in Py_Finalize(): |
| 534 | configuration options set before Py_Initialize() which should |
| 535 | remain valid after Py_Finalize(), since |
| 536 | Py_Initialize()-Py_Finalize() can be called multiple times. */ |
| 537 | _PyPathConfig_ClearGlobal(); |
| 538 | _Py_ClearStandardStreamEncoding(); |
| 539 | _Py_ClearArgcArgv(); |
Victor Stinner | f5f336a | 2019-03-19 14:53:58 +0100 | [diff] [blame] | 540 | _PyRuntime_Finalize(); |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 541 | } |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 542 | |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 543 | |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 544 | static int |
| 545 | exit_sigint(void) |
| 546 | { |
| 547 | /* bpo-1054041: We need to exit via the |
| 548 | * SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled. |
| 549 | * If we don't, a calling process such as a shell may not know |
| 550 | * about the user's ^C. https://www.cons.org/cracauer/sigint.html */ |
| 551 | #if defined(HAVE_GETPID) && !defined(MS_WINDOWS) |
| 552 | if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) { |
| 553 | perror("signal"); /* Impossible in normal environments. */ |
| 554 | } else { |
| 555 | kill(getpid(), SIGINT); |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 556 | } |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 557 | /* If setting SIG_DFL failed, or kill failed to terminate us, |
| 558 | * there isn't much else we can do aside from an error code. */ |
| 559 | #endif /* HAVE_GETPID && !MS_WINDOWS */ |
| 560 | #ifdef MS_WINDOWS |
| 561 | /* cmd.exe detects this, prints ^C, and offers to terminate. */ |
| 562 | /* https://msdn.microsoft.com/en-us/library/cc704588.aspx */ |
| 563 | return STATUS_CONTROL_C_EXIT; |
| 564 | #else |
| 565 | return SIGINT + 128; |
| 566 | #endif /* !MS_WINDOWS */ |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 570 | static void _Py_NO_RETURN |
| 571 | pymain_exit_error(_PyInitError err) |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 572 | { |
Victor Stinner | 4cb525a | 2019-04-26 13:05:47 +0200 | [diff] [blame^] | 573 | if (_Py_INIT_HAS_EXITCODE(err)) { |
| 574 | /* If it's an error rather than a regular exit, leave Python runtime |
| 575 | alive: _Py_ExitInitError() uses the current exception and use |
| 576 | sys.stdout in this case. */ |
| 577 | pymain_free(); |
| 578 | } |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 579 | _Py_ExitInitError(err); |
| 580 | } |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 581 | |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 582 | |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 583 | int |
| 584 | _Py_RunMain(void) |
| 585 | { |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 586 | int exitcode = 0; |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 587 | |
| 588 | _PyInitError err = pymain_run_python(&exitcode); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 589 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 590 | pymain_exit_error(err); |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (Py_FinalizeEx() < 0) { |
| 594 | /* Value unlikely to be confused with a non-error exit status or |
| 595 | other special meaning */ |
| 596 | exitcode = 120; |
Victor Stinner | 1976086 | 2017-12-20 01:41:59 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 599 | pymain_free(); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 600 | |
Gregory P. Smith | 38f11cc | 2019-02-16 12:57:40 -0800 | [diff] [blame] | 601 | if (_Py_UnhandledKeyboardInterrupt) { |
Victor Stinner | 95e2cbf | 2019-03-01 16:25:19 +0100 | [diff] [blame] | 602 | exitcode = exit_sigint(); |
Gregory P. Smith | 38f11cc | 2019-02-16 12:57:40 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Victor Stinner | dfe8847 | 2019-03-01 12:14:41 +0100 | [diff] [blame] | 605 | return exitcode; |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 606 | } |
Victor Stinner | c183444 | 2019-03-18 22:24:28 +0100 | [diff] [blame] | 607 | |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 608 | |
| 609 | static int |
| 610 | pymain_main(_PyArgv *args) |
| 611 | { |
| 612 | _PyInitError err = pymain_init(args); |
| 613 | if (_Py_INIT_FAILED(err)) { |
| 614 | pymain_exit_error(err); |
| 615 | } |
| 616 | |
| 617 | return _Py_RunMain(); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 621 | int |
| 622 | Py_Main(int argc, wchar_t **argv) |
| 623 | { |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 624 | _PyArgv args = { |
| 625 | .argc = argc, |
| 626 | .use_bytes_argv = 0, |
| 627 | .bytes_argv = NULL, |
| 628 | .wchar_argv = argv}; |
| 629 | return pymain_main(&args); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 632 | |
| 633 | int |
| 634 | _Py_UnixMain(int argc, char **argv) |
| 635 | { |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 636 | _PyArgv args = { |
| 637 | .argc = argc, |
| 638 | .use_bytes_argv = 1, |
| 639 | .bytes_argv = argv, |
| 640 | .wchar_argv = NULL}; |
| 641 | return pymain_main(&args); |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 644 | #ifdef __cplusplus |
| 645 | } |
| 646 | #endif |