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