Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | |
| 2 | /* Python interpreter top-level routines, including init/exit */ |
| 3 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 5 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "Python-ast.h" |
Kristján Valur Jónsson | 67387fb | 2007-04-25 00:17:39 +0000 | [diff] [blame] | 7 | #undef Yield /* undefine macro conflicting with winbase.h */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 8 | #include "grammar.h" |
| 9 | #include "node.h" |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 10 | #include "token.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 11 | #include "parsetok.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 12 | #include "errcode.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | #include "code.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 14 | #include "compile.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 15 | #include "symtable.h" |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 16 | #include "pyarena.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 17 | #include "ast.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 18 | #include "eval.h" |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 19 | #include "marshal.h" |
Antoine Pitrou | efb60c0 | 2009-10-20 21:29:37 +0000 | [diff] [blame] | 20 | #include "abstract.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 21 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 22 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 23 | #include <signal.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 24 | #endif |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 25 | |
Benjamin Peterson | 796798b | 2009-01-02 20:47:27 +0000 | [diff] [blame] | 26 | #ifdef MS_WINDOWS |
Martin v. Löwis | 5344c99 | 2009-01-02 20:32:55 +0000 | [diff] [blame] | 27 | #include "malloc.h" /* for alloca */ |
Benjamin Peterson | 796798b | 2009-01-02 20:47:27 +0000 | [diff] [blame] | 28 | #endif |
Martin v. Löwis | 5344c99 | 2009-01-02 20:32:55 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 30 | #ifdef HAVE_LANGINFO_H |
| 31 | #include <locale.h> |
| 32 | #include <langinfo.h> |
| 33 | #endif |
| 34 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 35 | #ifdef MS_WINDOWS |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 36 | #undef BYTE |
| 37 | #include "windows.h" |
| 38 | #endif |
| 39 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 44 | extern char *Py_GetPath(void); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 46 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 48 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 49 | static void initmain(void); |
| 50 | static void initsite(void); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 51 | static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 52 | PyCompilerFlags *, PyArena *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 53 | static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 54 | PyCompilerFlags *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 55 | static void err_input(perrdetail *); |
| 56 | static void initsigs(void); |
Antoine Pitrou | efb60c0 | 2009-10-20 21:29:37 +0000 | [diff] [blame] | 57 | static void wait_for_thread_shutdown(void); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 58 | static void call_sys_exitfunc(void); |
| 59 | static void call_ll_exitfuncs(void); |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 60 | extern void _PyUnicode_Init(void); |
| 61 | extern void _PyUnicode_Fini(void); |
Guido van Rossum | c94044c | 2000-03-10 23:03:54 +0000 | [diff] [blame] | 62 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 63 | #ifdef WITH_THREAD |
| 64 | extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); |
| 65 | extern void _PyGILState_Fini(void); |
| 66 | #endif /* WITH_THREAD */ |
| 67 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 68 | int Py_DebugFlag; /* Needed by parser.c */ |
| 69 | int Py_VerboseFlag; /* Needed by import.c */ |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 70 | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ |
Georg Brandl | 11041f0 | 2011-05-15 08:50:32 +0200 | [diff] [blame] | 71 | int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */ |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 72 | int Py_NoSiteFlag; /* Suppress 'import site' */ |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 73 | int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 74 | int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */ |
Barry Warsaw | 3ce0964 | 2000-05-02 19:18:59 +0000 | [diff] [blame] | 75 | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */ |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 76 | int Py_FrozenFlag; /* Needed by getpath.c */ |
Guido van Rossum | b16d197 | 2000-05-01 17:55:15 +0000 | [diff] [blame] | 77 | int Py_UnicodeFlag = 0; /* Needed by compile.c */ |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 78 | int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 79 | /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed, |
| 80 | on the command line, and is used in 2.2 by ceval.c to make all "/" divisions |
| 81 | true divisions (which they will be in 2.3). */ |
| 82 | int _Py_QnewFlag = 0; |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 83 | int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 84 | int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 85 | |
Christian Heimes | 51c4d72 | 2013-10-22 10:22:29 +0200 | [diff] [blame] | 86 | |
| 87 | /* Hack to force loading of object files */ |
| 88 | int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \ |
| 89 | PyOS_mystrnicmp; /* Python/pystrcmp.o */ |
| 90 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 91 | /* PyModule_GetWarningsModule is no longer necessary as of 2.6 |
| 92 | since _warnings is builtin. This API should not be used. */ |
| 93 | PyObject * |
| 94 | PyModule_GetWarningsModule(void) |
Mark Hammond | edd0773 | 2003-07-15 23:03:55 +0000 | [diff] [blame] | 95 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 96 | return PyImport_ImportModule("warnings"); |
Mark Hammond | edd0773 | 2003-07-15 23:03:55 +0000 | [diff] [blame] | 97 | } |
Mark Hammond | a43fd0c | 2003-02-19 00:33:33 +0000 | [diff] [blame] | 98 | |
Victor Stinner | 3c082a7 | 2017-10-17 01:35:19 -0700 | [diff] [blame] | 99 | static void |
| 100 | _PyDebug_PrintTotalRefs(void) |
| 101 | { |
| 102 | #ifdef Py_REF_DEBUG |
| 103 | Py_ssize_t total; |
| 104 | |
| 105 | if (!Py_GETENV("PYTHONSHOWREFCOUNT")) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | total = _Py_GetRefTotal(); |
| 110 | fprintf(stderr, "[%" PY_FORMAT_SIZE_T "d refs]\n", total); |
| 111 | #endif |
| 112 | } |
| 113 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 114 | static int initialized = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 115 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 116 | /* API to access the initialized flag -- useful for esoteric use */ |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 117 | |
| 118 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 119 | Py_IsInitialized(void) |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 120 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 121 | return initialized; |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 124 | /* Global initializations. Can be undone by Py_Finalize(). Don't |
| 125 | call this twice without an intervening Py_Finalize() call. When |
| 126 | initializations fail, a fatal error is issued and the function does |
| 127 | not return. On return, the first thread and interpreter state have |
| 128 | been created. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 130 | Locking: you must hold the interpreter lock while calling this. |
| 131 | (If the lock has not yet been initialized, that's equivalent to |
| 132 | having the lock, but you cannot use multiple threads.) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 134 | */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 9abaf4d | 2001-10-12 22:17:56 +0000 | [diff] [blame] | 136 | static int |
| 137 | add_flag(int flag, const char *envs) |
| 138 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 139 | int env = atoi(envs); |
| 140 | if (flag < env) |
| 141 | flag = env; |
| 142 | if (flag < 1) |
| 143 | flag = 1; |
| 144 | return flag; |
Guido van Rossum | 9abaf4d | 2001-10-12 22:17:56 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Serhiy Storchaka | 5127ed7 | 2015-05-30 17:45:12 +0300 | [diff] [blame] | 147 | static int |
| 148 | isatty_no_error(PyObject *sys_stream) |
| 149 | { |
| 150 | PyObject *sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); |
| 151 | if (sys_isatty) { |
| 152 | int isatty = PyObject_IsTrue(sys_isatty); |
| 153 | Py_DECREF(sys_isatty); |
| 154 | if (isatty >= 0) |
| 155 | return isatty; |
| 156 | } |
| 157 | PyErr_Clear(); |
| 158 | return 0; |
| 159 | } |
| 160 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 161 | void |
Martin v. Löwis | 336e85f | 2004-08-19 11:31:58 +0000 | [diff] [blame] | 162 | Py_InitializeEx(int install_sigs) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 163 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 164 | PyInterpreterState *interp; |
| 165 | PyThreadState *tstate; |
| 166 | PyObject *bimod, *sysmod; |
| 167 | char *p; |
| 168 | char *icodeset = NULL; /* On Windows, input codeset may theoretically |
| 169 | differ from output codeset. */ |
| 170 | char *codeset = NULL; |
| 171 | char *errors = NULL; |
| 172 | int free_codeset = 0; |
| 173 | int overridden = 0; |
Serhiy Storchaka | 5127ed7 | 2015-05-30 17:45:12 +0300 | [diff] [blame] | 174 | PyObject *sys_stream; |
Martin v. Löwis | a2c17c5 | 2003-08-09 09:47:11 +0000 | [diff] [blame] | 175 | #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 176 | char *saved_locale, *loc_codeset; |
Martin v. Löwis | a2c17c5 | 2003-08-09 09:47:11 +0000 | [diff] [blame] | 177 | #endif |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 178 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 179 | char ibuf[128]; |
| 180 | char buf[128]; |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 181 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 182 | extern void _Py_ReadyTypes(void); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 184 | if (initialized) |
| 185 | return; |
| 186 | initialized = 1; |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 188 | if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') |
| 189 | Py_DebugFlag = add_flag(Py_DebugFlag, p); |
| 190 | if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') |
| 191 | Py_VerboseFlag = add_flag(Py_VerboseFlag, p); |
| 192 | if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') |
| 193 | Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); |
| 194 | if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') |
| 195 | Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 196 | /* The variable is only tested for existence here; _PyRandom_Init will |
| 197 | check its value further. */ |
| 198 | if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0') |
| 199 | Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p); |
| 200 | |
| 201 | _PyRandom_Init(); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 203 | interp = PyInterpreterState_New(); |
| 204 | if (interp == NULL) |
| 205 | Py_FatalError("Py_Initialize: can't make first interpreter"); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 207 | tstate = PyThreadState_New(interp); |
| 208 | if (tstate == NULL) |
| 209 | Py_FatalError("Py_Initialize: can't make first thread"); |
| 210 | (void) PyThreadState_Swap(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 212 | _Py_ReadyTypes(); |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 214 | if (!_PyFrame_Init()) |
| 215 | Py_FatalError("Py_Initialize: can't init frames"); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 216 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 217 | if (!_PyInt_Init()) |
| 218 | Py_FatalError("Py_Initialize: can't init ints"); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 220 | if (!_PyLong_Init()) |
| 221 | Py_FatalError("Py_Initialize: can't init longs"); |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 223 | if (!PyByteArray_Init()) |
| 224 | Py_FatalError("Py_Initialize: can't init bytearray"); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 226 | _PyFloat_Init(); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 228 | interp->modules = PyDict_New(); |
| 229 | if (interp->modules == NULL) |
| 230 | Py_FatalError("Py_Initialize: can't make modules dictionary"); |
| 231 | interp->modules_reloading = PyDict_New(); |
| 232 | if (interp->modules_reloading == NULL) |
| 233 | Py_FatalError("Py_Initialize: can't make modules_reloading dictionary"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 234 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 235 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 236 | /* Init Unicode implementation; relies on the codec registry */ |
| 237 | _PyUnicode_Init(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 238 | #endif |
Guido van Rossum | c94044c | 2000-03-10 23:03:54 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 240 | bimod = _PyBuiltin_Init(); |
| 241 | if (bimod == NULL) |
| 242 | Py_FatalError("Py_Initialize: can't initialize __builtin__"); |
| 243 | interp->builtins = PyModule_GetDict(bimod); |
| 244 | if (interp->builtins == NULL) |
| 245 | Py_FatalError("Py_Initialize: can't initialize builtins dict"); |
| 246 | Py_INCREF(interp->builtins); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 248 | sysmod = _PySys_Init(); |
| 249 | if (sysmod == NULL) |
| 250 | Py_FatalError("Py_Initialize: can't initialize sys"); |
| 251 | interp->sysdict = PyModule_GetDict(sysmod); |
| 252 | if (interp->sysdict == NULL) |
| 253 | Py_FatalError("Py_Initialize: can't initialize sys dict"); |
| 254 | Py_INCREF(interp->sysdict); |
| 255 | _PyImport_FixupExtension("sys", "sys"); |
| 256 | PySys_SetPath(Py_GetPath()); |
| 257 | PyDict_SetItemString(interp->sysdict, "modules", |
| 258 | interp->modules); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 260 | _PyImport_Init(); |
Guido van Rossum | 7c85ab8 | 1999-07-08 17:26:56 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 262 | /* initialize builtin exceptions */ |
| 263 | _PyExc_Init(); |
| 264 | _PyImport_FixupExtension("exceptions", "exceptions"); |
Barry Warsaw | f242aa0 | 2000-05-25 23:09:49 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 266 | /* phase 2 of builtins */ |
| 267 | _PyImport_FixupExtension("__builtin__", "__builtin__"); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 269 | _PyImportHooks_Init(); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 270 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 271 | if (install_sigs) |
| 272 | initsigs(); /* Signal handling stuff, including initintr() */ |
Brett Cannon | c33e82d | 2010-05-05 20:38:52 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 274 | /* Initialize warnings. */ |
| 275 | _PyWarnings_Init(); |
| 276 | if (PySys_HasWarnOptions()) { |
| 277 | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
| 278 | if (!warnings_module) |
| 279 | PyErr_Clear(); |
| 280 | Py_XDECREF(warnings_module); |
| 281 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 283 | initmain(); /* Module __main__ */ |
Just van Rossum | 5bfba3a | 2003-02-25 20:25:12 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 285 | /* auto-thread-state API, if available */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 286 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 287 | _PyGILState_Init(interp, tstate); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 288 | #endif /* WITH_THREAD */ |
| 289 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | if (!Py_NoSiteFlag) |
| 291 | initsite(); /* Module site */ |
Victor Stinner | 6664426 | 2010-03-10 22:30:19 +0000 | [diff] [blame] | 292 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 293 | if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') { |
| 294 | p = icodeset = codeset = strdup(p); |
| 295 | free_codeset = 1; |
| 296 | errors = strchr(p, ':'); |
| 297 | if (errors) { |
| 298 | *errors = '\0'; |
| 299 | errors++; |
| 300 | } |
| 301 | overridden = 1; |
| 302 | } |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 303 | |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 304 | #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 305 | /* On Unix, set the file system encoding according to the |
| 306 | user's preference, if the CODESET names a well-known |
| 307 | Python codec, and Py_FileSystemDefaultEncoding isn't |
| 308 | initialized by other means. Also set the encoding of |
| 309 | stdin and stdout if these are terminals, unless overridden. */ |
Martin v. Löwis | a2c17c5 | 2003-08-09 09:47:11 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 311 | if (!overridden || !Py_FileSystemDefaultEncoding) { |
| 312 | saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 313 | setlocale(LC_CTYPE, ""); |
| 314 | loc_codeset = nl_langinfo(CODESET); |
| 315 | if (loc_codeset && *loc_codeset) { |
| 316 | PyObject *enc = PyCodec_Encoder(loc_codeset); |
| 317 | if (enc) { |
| 318 | loc_codeset = strdup(loc_codeset); |
| 319 | Py_DECREF(enc); |
| 320 | } else { |
| 321 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 322 | PyErr_Clear(); |
| 323 | loc_codeset = NULL; |
| 324 | } else { |
| 325 | PyErr_Print(); |
| 326 | exit(1); |
| 327 | } |
| 328 | } |
| 329 | } else |
| 330 | loc_codeset = NULL; |
| 331 | setlocale(LC_CTYPE, saved_locale); |
| 332 | free(saved_locale); |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | if (!overridden) { |
| 335 | codeset = icodeset = loc_codeset; |
| 336 | free_codeset = 1; |
| 337 | } |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 339 | /* Initialize Py_FileSystemDefaultEncoding from |
| 340 | locale even if PYTHONIOENCODING is set. */ |
| 341 | if (!Py_FileSystemDefaultEncoding) { |
| 342 | Py_FileSystemDefaultEncoding = loc_codeset; |
| 343 | if (!overridden) |
| 344 | free_codeset = 0; |
| 345 | } |
| 346 | } |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 347 | #endif |
| 348 | |
| 349 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 350 | if (!overridden) { |
| 351 | icodeset = ibuf; |
| 352 | codeset = buf; |
| 353 | sprintf(ibuf, "cp%d", GetConsoleCP()); |
| 354 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 355 | } |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 356 | #endif |
Martin v. Löwis | a2c17c5 | 2003-08-09 09:47:11 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 358 | if (codeset) { |
| 359 | sys_stream = PySys_GetObject("stdin"); |
Serhiy Storchaka | 5127ed7 | 2015-05-30 17:45:12 +0300 | [diff] [blame] | 360 | if ((overridden || isatty_no_error(sys_stream)) && |
| 361 | PyFile_Check(sys_stream)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 362 | if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors)) |
| 363 | Py_FatalError("Cannot set codeset of stdin"); |
| 364 | } |
Martin v. Löwis | a2c17c5 | 2003-08-09 09:47:11 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 366 | sys_stream = PySys_GetObject("stdout"); |
Serhiy Storchaka | 5127ed7 | 2015-05-30 17:45:12 +0300 | [diff] [blame] | 367 | if ((overridden || isatty_no_error(sys_stream)) && |
| 368 | PyFile_Check(sys_stream)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 369 | if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) |
| 370 | Py_FatalError("Cannot set codeset of stdout"); |
| 371 | } |
Martin v. Löwis | a2c17c5 | 2003-08-09 09:47:11 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 373 | sys_stream = PySys_GetObject("stderr"); |
Serhiy Storchaka | 5127ed7 | 2015-05-30 17:45:12 +0300 | [diff] [blame] | 374 | if ((overridden || isatty_no_error(sys_stream)) && |
| 375 | PyFile_Check(sys_stream)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 376 | if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) |
| 377 | Py_FatalError("Cannot set codeset of stderr"); |
| 378 | } |
Martin v. Löwis | ea62d25 | 2006-04-03 10:56:49 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 380 | if (free_codeset) |
| 381 | free(codeset); |
| 382 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Martin v. Löwis | 336e85f | 2004-08-19 11:31:58 +0000 | [diff] [blame] | 385 | void |
| 386 | Py_Initialize(void) |
| 387 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 388 | Py_InitializeEx(1); |
Martin v. Löwis | 336e85f | 2004-08-19 11:31:58 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Guido van Rossum | 2edcf0d | 1998-12-15 16:12:00 +0000 | [diff] [blame] | 392 | #ifdef COUNT_ALLOCS |
Martin v. Löwis | 45294a9 | 2006-04-18 06:24:08 +0000 | [diff] [blame] | 393 | extern void dump_counts(FILE*); |
Guido van Rossum | 2edcf0d | 1998-12-15 16:12:00 +0000 | [diff] [blame] | 394 | #endif |
| 395 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 396 | /* Undo the effect of Py_Initialize(). |
| 397 | |
| 398 | Beware: if multiple interpreter and/or thread states exist, these |
| 399 | are not wiped out; only the current thread and interpreter state |
| 400 | are deleted. But since everything else is deleted, those other |
| 401 | interpreter and thread states should no longer be used. |
| 402 | |
| 403 | (XXX We should do better, e.g. wipe out all interpreters and |
| 404 | threads.) |
| 405 | |
| 406 | Locking: as above. |
| 407 | |
| 408 | */ |
| 409 | |
| 410 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 411 | Py_Finalize(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 412 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 413 | PyInterpreterState *interp; |
| 414 | PyThreadState *tstate; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 415 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 416 | if (!initialized) |
| 417 | return; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 419 | wait_for_thread_shutdown(); |
Antoine Pitrou | efb60c0 | 2009-10-20 21:29:37 +0000 | [diff] [blame] | 420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 421 | /* The interpreter is still entirely intact at this point, and the |
| 422 | * exit funcs may be relying on that. In particular, if some thread |
| 423 | * or exit func is still waiting to do an import, the import machinery |
| 424 | * expects Py_IsInitialized() to return true. So don't say the |
| 425 | * interpreter is uninitialized until after the exit funcs have run. |
| 426 | * Note that Threading.py uses an exit func to do a join on all the |
| 427 | * threads created thru it, so this also protects pending imports in |
| 428 | * the threads created via Threading. |
| 429 | */ |
| 430 | call_sys_exitfunc(); |
Antoine Pitrou | b9a4501 | 2014-11-21 02:04:21 +0100 | [diff] [blame] | 431 | initialized = 0; |
Guido van Rossum | 4cc462e | 1998-01-19 22:00:38 +0000 | [diff] [blame] | 432 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 433 | /* Get current thread state and interpreter pointer */ |
| 434 | tstate = PyThreadState_GET(); |
| 435 | interp = tstate->interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 437 | /* Disable signal handling */ |
| 438 | PyOS_FiniInterrupts(); |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 440 | /* Clear type lookup cache */ |
| 441 | PyType_ClearCache(); |
Christian Heimes | 908caac | 2008-01-27 23:34:59 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 443 | /* Collect garbage. This may call finalizers; it's nice to call these |
| 444 | * before all modules are destroyed. |
| 445 | * XXX If a __del__ or weakref callback is triggered here, and tries to |
| 446 | * XXX import a module, bad things can happen, because Python no |
| 447 | * XXX longer believes it's initialized. |
| 448 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
| 449 | * XXX is easy to provoke that way. I've also seen, e.g., |
| 450 | * XXX Exception exceptions.ImportError: 'No module named sha' |
| 451 | * XXX in <function callback at 0x008F5718> ignored |
| 452 | * XXX but I'm unclear on exactly how that one happens. In any case, |
| 453 | * XXX I haven't seen a real-life report of either of these. |
| 454 | */ |
| 455 | PyGC_Collect(); |
Martin v. Löwis | 45294a9 | 2006-04-18 06:24:08 +0000 | [diff] [blame] | 456 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 457 | /* With COUNT_ALLOCS, it helps to run GC multiple times: |
| 458 | each collection might release some types from the type |
| 459 | list, so they become garbage. */ |
| 460 | while (PyGC_Collect() > 0) |
| 461 | /* nothing */; |
Martin v. Löwis | 45294a9 | 2006-04-18 06:24:08 +0000 | [diff] [blame] | 462 | #endif |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 464 | /* Destroy all modules */ |
| 465 | PyImport_Cleanup(); |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 466 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 467 | /* Collect final garbage. This disposes of cycles created by |
| 468 | * new-style class definitions, for example. |
| 469 | * XXX This is disabled because it caused too many problems. If |
| 470 | * XXX a __del__ or weakref callback triggers here, Python code has |
| 471 | * XXX a hard time running, because even the sys module has been |
| 472 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
| 473 | * XXX One symptom is a sequence of information-free messages |
| 474 | * XXX coming from threads (if a __del__ or callback is invoked, |
| 475 | * XXX other threads can execute too, and any exception they encounter |
| 476 | * XXX triggers a comedy of errors as subsystem after subsystem |
| 477 | * XXX fails to find what it *expects* to find in sys to help report |
| 478 | * XXX the exception and consequent unexpected failures). I've also |
| 479 | * XXX seen segfaults then, after adding print statements to the |
| 480 | * XXX Python code getting called. |
| 481 | */ |
Tim Peters | 1d7323e | 2003-12-01 21:35:27 +0000 | [diff] [blame] | 482 | #if 0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 483 | PyGC_Collect(); |
Tim Peters | 1d7323e | 2003-12-01 21:35:27 +0000 | [diff] [blame] | 484 | #endif |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 485 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 486 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 487 | _PyImport_Fini(); |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 488 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 489 | /* Debugging stuff */ |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 490 | #ifdef COUNT_ALLOCS |
Victor Stinner | 7b4ba62 | 2017-10-17 02:25:23 -0700 | [diff] [blame] | 491 | if (Py_GETENV("PYTHONSHOWALLOCCOUNT")) { |
| 492 | dump_counts(stderr); |
| 493 | } |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 494 | #endif |
| 495 | |
Victor Stinner | 3c082a7 | 2017-10-17 01:35:19 -0700 | [diff] [blame] | 496 | _PyDebug_PrintTotalRefs(); |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 497 | |
Tim Peters | 9cf25ce | 2003-04-17 15:21:01 +0000 | [diff] [blame] | 498 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 499 | /* Display all objects still alive -- this can invoke arbitrary |
| 500 | * __repr__ overrides, so requires a mostly-intact interpreter. |
| 501 | * Alas, a lot of stuff may still be alive now that will be cleaned |
| 502 | * up later. |
| 503 | */ |
| 504 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 505 | _Py_PrintReferences(stderr); |
Tim Peters | 9cf25ce | 2003-04-17 15:21:01 +0000 | [diff] [blame] | 506 | #endif /* Py_TRACE_REFS */ |
| 507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 508 | /* Clear interpreter state */ |
| 509 | PyInterpreterState_Clear(interp); |
Guido van Rossum | d922fa4 | 2003-04-15 14:10:09 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 511 | /* Now we decref the exception classes. After this point nothing |
| 512 | can raise an exception. That's okay, because each Fini() method |
| 513 | below has been checked to make sure no exceptions are ever |
| 514 | raised. |
| 515 | */ |
Anthony Baxter | 12b6f6c | 2005-03-29 13:36:16 +0000 | [diff] [blame] | 516 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 517 | _PyExc_Fini(); |
Anthony Baxter | 12b6f6c | 2005-03-29 13:36:16 +0000 | [diff] [blame] | 518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 519 | /* Cleanup auto-thread-state */ |
Amaury Forgeot d'Arc | 025c347 | 2007-11-29 23:35:25 +0000 | [diff] [blame] | 520 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | _PyGILState_Fini(); |
Amaury Forgeot d'Arc | 025c347 | 2007-11-29 23:35:25 +0000 | [diff] [blame] | 522 | #endif /* WITH_THREAD */ |
| 523 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 524 | /* Delete current thread */ |
| 525 | PyThreadState_Swap(NULL); |
| 526 | PyInterpreterState_Delete(interp); |
Barry Warsaw | f242aa0 | 2000-05-25 23:09:49 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 528 | /* Sundry finalizers */ |
| 529 | PyMethod_Fini(); |
| 530 | PyFrame_Fini(); |
| 531 | PyCFunction_Fini(); |
| 532 | PyTuple_Fini(); |
| 533 | PyList_Fini(); |
| 534 | PySet_Fini(); |
| 535 | PyString_Fini(); |
| 536 | PyByteArray_Fini(); |
| 537 | PyInt_Fini(); |
| 538 | PyFloat_Fini(); |
| 539 | PyDict_Fini(); |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 540 | _PyRandom_Fini(); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 541 | |
Marc-André Lemburg | 95de5c1 | 2002-04-08 08:19:36 +0000 | [diff] [blame] | 542 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 543 | /* Cleanup Unicode implementation */ |
| 544 | _PyUnicode_Fini(); |
Marc-André Lemburg | 95de5c1 | 2002-04-08 08:19:36 +0000 | [diff] [blame] | 545 | #endif |
| 546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 547 | /* XXX Still allocated: |
| 548 | - various static ad-hoc pointers to interned strings |
| 549 | - int and float free list blocks |
| 550 | - whatever various modules and libraries allocate |
| 551 | */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 553 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 554 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 555 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 556 | /* Display addresses (& refcnts) of all objects still alive. |
| 557 | * An address can be used to find the repr of the object, printed |
| 558 | * above by _Py_PrintReferences. |
| 559 | */ |
| 560 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 561 | _Py_PrintReferenceAddresses(stderr); |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 562 | #endif /* Py_TRACE_REFS */ |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 563 | #ifdef PYMALLOC_DEBUG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 564 | if (Py_GETENV("PYTHONMALLOCSTATS")) |
| 565 | _PyObject_DebugMallocStats(); |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 566 | #endif |
| 567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 568 | call_ll_exitfuncs(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* Create and initialize a new interpreter and thread, and return the |
| 572 | new thread. This requires that Py_Initialize() has been called |
| 573 | first. |
| 574 | |
| 575 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 576 | exception information is available even in this case -- the |
| 577 | exception information is held in the thread, and there is no |
| 578 | thread. |
| 579 | |
| 580 | Locking: as above. |
| 581 | |
| 582 | */ |
| 583 | |
| 584 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 585 | Py_NewInterpreter(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 586 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 587 | PyInterpreterState *interp; |
| 588 | PyThreadState *tstate, *save_tstate; |
| 589 | PyObject *bimod, *sysmod; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 590 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | if (!initialized) |
| 592 | Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 594 | interp = PyInterpreterState_New(); |
| 595 | if (interp == NULL) |
| 596 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 598 | tstate = PyThreadState_New(interp); |
| 599 | if (tstate == NULL) { |
| 600 | PyInterpreterState_Delete(interp); |
| 601 | return NULL; |
| 602 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 604 | save_tstate = PyThreadState_Swap(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 606 | /* XXX The following is lax in error checking */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 608 | interp->modules = PyDict_New(); |
| 609 | interp->modules_reloading = PyDict_New(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 611 | bimod = _PyImport_FindExtension("__builtin__", "__builtin__"); |
| 612 | if (bimod != NULL) { |
| 613 | interp->builtins = PyModule_GetDict(bimod); |
| 614 | if (interp->builtins == NULL) |
| 615 | goto handle_error; |
| 616 | Py_INCREF(interp->builtins); |
| 617 | } |
| 618 | sysmod = _PyImport_FindExtension("sys", "sys"); |
| 619 | if (bimod != NULL && sysmod != NULL) { |
| 620 | interp->sysdict = PyModule_GetDict(sysmod); |
| 621 | if (interp->sysdict == NULL) |
| 622 | goto handle_error; |
| 623 | Py_INCREF(interp->sysdict); |
| 624 | PySys_SetPath(Py_GetPath()); |
| 625 | PyDict_SetItemString(interp->sysdict, "modules", |
| 626 | interp->modules); |
| 627 | _PyImportHooks_Init(); |
| 628 | initmain(); |
| 629 | if (!Py_NoSiteFlag) |
| 630 | initsite(); |
| 631 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | if (!PyErr_Occurred()) |
| 634 | return tstate; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 635 | |
Neal Norwitz | 0c6ae5b | 2006-08-21 20:16:24 +0000 | [diff] [blame] | 636 | handle_error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 637 | /* Oops, it didn't work. Undo it all. */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 639 | PyErr_Print(); |
| 640 | PyThreadState_Clear(tstate); |
| 641 | PyThreadState_Swap(save_tstate); |
| 642 | PyThreadState_Delete(tstate); |
| 643 | PyInterpreterState_Delete(interp); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 645 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Delete an interpreter and its last thread. This requires that the |
| 649 | given thread state is current, that the thread has no remaining |
| 650 | frames, and that it is its interpreter's only remaining thread. |
| 651 | It is a fatal error to violate these constraints. |
| 652 | |
| 653 | (Py_Finalize() doesn't have these constraints -- it zaps |
| 654 | everything, regardless.) |
| 655 | |
| 656 | Locking: as above. |
| 657 | |
| 658 | */ |
| 659 | |
| 660 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 661 | Py_EndInterpreter(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 662 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | PyInterpreterState *interp = tstate->interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 665 | if (tstate != PyThreadState_GET()) |
| 666 | Py_FatalError("Py_EndInterpreter: thread is not current"); |
| 667 | if (tstate->frame != NULL) |
| 668 | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
| 669 | if (tstate != interp->tstate_head || tstate->next != NULL) |
| 670 | Py_FatalError("Py_EndInterpreter: not the last thread"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 672 | PyImport_Cleanup(); |
| 673 | PyInterpreterState_Clear(interp); |
| 674 | PyThreadState_Swap(NULL); |
| 675 | PyInterpreterState_Delete(interp); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static char *progname = "python"; |
| 679 | |
| 680 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 681 | Py_SetProgramName(char *pn) |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 682 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 683 | if (pn && *pn) |
| 684 | progname = pn; |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 688 | Py_GetProgramName(void) |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 689 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 690 | return progname; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 693 | static char *default_home = NULL; |
| 694 | |
| 695 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 696 | Py_SetPythonHome(char *home) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 697 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | default_home = home; |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 702 | Py_GetPythonHome(void) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 703 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 704 | char *home = default_home; |
| 705 | if (home == NULL && !Py_IgnoreEnvironmentFlag) |
| 706 | home = Py_GETENV("PYTHONHOME"); |
| 707 | return home; |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 710 | /* Create __main__ module */ |
| 711 | |
| 712 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 713 | initmain(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 714 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 715 | PyObject *m, *d; |
| 716 | m = PyImport_AddModule("__main__"); |
| 717 | if (m == NULL) |
| 718 | Py_FatalError("can't create __main__ module"); |
| 719 | d = PyModule_GetDict(m); |
| 720 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 721 | PyObject *bimod = PyImport_ImportModule("__builtin__"); |
| 722 | if (bimod == NULL || |
| 723 | PyDict_SetItemString(d, "__builtins__", bimod) != 0) |
| 724 | Py_FatalError("can't add __builtins__ to __main__"); |
| 725 | Py_XDECREF(bimod); |
| 726 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 729 | /* Import the site module (not into __main__ though) */ |
| 730 | |
| 731 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 732 | initsite(void) |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 733 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 734 | PyObject *m; |
| 735 | m = PyImport_ImportModule("site"); |
| 736 | if (m == NULL) { |
| 737 | PyErr_Print(); |
| 738 | Py_Finalize(); |
| 739 | exit(1); |
| 740 | } |
| 741 | else { |
| 742 | Py_DECREF(m); |
| 743 | } |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 746 | /* Parse input from a file and execute it */ |
| 747 | |
| 748 | int |
Tim Peters | 5e9d6cf | 2006-05-28 10:41:29 +0000 | [diff] [blame] | 749 | PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 750 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 751 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 752 | if (filename == NULL) |
| 753 | filename = "???"; |
| 754 | if (Py_FdIsInteractive(fp, filename)) { |
| 755 | int err = PyRun_InteractiveLoopFlags(fp, filename, flags); |
| 756 | if (closeit) |
| 757 | fclose(fp); |
| 758 | return err; |
| 759 | } |
| 760 | else |
| 761 | return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 765 | PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 766 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 767 | PyObject *v; |
| 768 | int ret; |
| 769 | PyCompilerFlags local_flags; |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 771 | if (flags == NULL) { |
| 772 | flags = &local_flags; |
| 773 | local_flags.cf_flags = 0; |
| 774 | } |
| 775 | v = PySys_GetObject("ps1"); |
| 776 | if (v == NULL) { |
| 777 | PySys_SetObject("ps1", v = PyString_FromString(">>> ")); |
| 778 | Py_XDECREF(v); |
| 779 | } |
| 780 | v = PySys_GetObject("ps2"); |
| 781 | if (v == NULL) { |
| 782 | PySys_SetObject("ps2", v = PyString_FromString("... ")); |
| 783 | Py_XDECREF(v); |
| 784 | } |
| 785 | for (;;) { |
| 786 | ret = PyRun_InteractiveOneFlags(fp, filename, flags); |
Victor Stinner | 3c082a7 | 2017-10-17 01:35:19 -0700 | [diff] [blame] | 787 | _PyDebug_PrintTotalRefs(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | if (ret == E_EOF) |
| 789 | return 0; |
| 790 | /* |
| 791 | if (ret == E_NOMEM) |
| 792 | return -1; |
| 793 | */ |
| 794 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 797 | #if 0 |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 798 | /* compute parser flags based on compiler flags */ |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 799 | #define PARSER_FLAGS(flags) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 800 | ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ |
| 801 | PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0) |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 802 | #endif |
| 803 | #if 1 |
Neal Norwitz | ca460d9 | 2006-09-06 06:28:06 +0000 | [diff] [blame] | 804 | /* Keep an example of flags with future keyword support. */ |
| 805 | #define PARSER_FLAGS(flags) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 806 | ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ |
| 807 | PyPARSE_DONT_IMPLY_DEDENT : 0) \ |
| 808 | | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \ |
| 809 | PyPARSE_PRINT_IS_FUNCTION : 0) \ |
| 810 | | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \ |
| 811 | PyPARSE_UNICODE_LITERALS : 0) \ |
| 812 | ) : 0) |
Neal Norwitz | ca460d9 | 2006-09-06 06:28:06 +0000 | [diff] [blame] | 813 | #endif |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 814 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 815 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 816 | PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 817 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 818 | PyObject *m, *d, *v, *w; |
| 819 | mod_ty mod; |
| 820 | PyArena *arena; |
| 821 | char *ps1 = "", *ps2 = ""; |
| 822 | int errcode = 0; |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 824 | v = PySys_GetObject("ps1"); |
| 825 | if (v != NULL) { |
| 826 | v = PyObject_Str(v); |
| 827 | if (v == NULL) |
| 828 | PyErr_Clear(); |
| 829 | else if (PyString_Check(v)) |
| 830 | ps1 = PyString_AsString(v); |
| 831 | } |
| 832 | w = PySys_GetObject("ps2"); |
| 833 | if (w != NULL) { |
| 834 | w = PyObject_Str(w); |
| 835 | if (w == NULL) |
| 836 | PyErr_Clear(); |
| 837 | else if (PyString_Check(w)) |
| 838 | ps2 = PyString_AsString(w); |
| 839 | } |
| 840 | arena = PyArena_New(); |
| 841 | if (arena == NULL) { |
| 842 | Py_XDECREF(v); |
| 843 | Py_XDECREF(w); |
| 844 | return -1; |
| 845 | } |
| 846 | mod = PyParser_ASTFromFile(fp, filename, |
| 847 | Py_single_input, ps1, ps2, |
| 848 | flags, &errcode, arena); |
| 849 | Py_XDECREF(v); |
| 850 | Py_XDECREF(w); |
| 851 | if (mod == NULL) { |
| 852 | PyArena_Free(arena); |
| 853 | if (errcode == E_EOF) { |
| 854 | PyErr_Clear(); |
| 855 | return E_EOF; |
| 856 | } |
| 857 | PyErr_Print(); |
| 858 | return -1; |
| 859 | } |
| 860 | m = PyImport_AddModule("__main__"); |
| 861 | if (m == NULL) { |
| 862 | PyArena_Free(arena); |
| 863 | return -1; |
| 864 | } |
| 865 | d = PyModule_GetDict(m); |
| 866 | v = run_mod(mod, filename, d, d, flags, arena); |
| 867 | PyArena_Free(arena); |
| 868 | if (v == NULL) { |
| 869 | PyErr_Print(); |
| 870 | return -1; |
| 871 | } |
| 872 | Py_DECREF(v); |
| 873 | if (Py_FlushLine()) |
| 874 | PyErr_Clear(); |
| 875 | return 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 878 | /* Check whether a file maybe a pyc file: Look at the extension, |
| 879 | the file type, and, if we may close it, at the first few bytes. */ |
| 880 | |
| 881 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 882 | maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit) |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 883 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 884 | if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0) |
| 885 | return 1; |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 887 | /* Only look into the file if we are allowed to close it, since |
| 888 | it then should also be seekable. */ |
| 889 | if (closeit) { |
| 890 | /* Read only two bytes of the magic. If the file was opened in |
| 891 | text mode, the bytes 3 and 4 of the magic (\r\n) might not |
| 892 | be read as they are on disk. */ |
| 893 | unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF; |
| 894 | unsigned char buf[2]; |
| 895 | /* Mess: In case of -x, the stream is NOT at its start now, |
| 896 | and ungetc() was used to push back the first newline, |
| 897 | which makes the current stream position formally undefined, |
| 898 | and a x-platform nightmare. |
| 899 | Unfortunately, we have no direct way to know whether -x |
| 900 | was specified. So we use a terrible hack: if the current |
| 901 | stream position is not 0, we assume -x was specified, and |
| 902 | give up. Bug 132850 on SourceForge spells out the |
| 903 | hopelessness of trying anything else (fseek and ftell |
| 904 | don't work predictably x-platform for text-mode files). |
| 905 | */ |
| 906 | int ispyc = 0; |
| 907 | if (ftell(fp) == 0) { |
| 908 | if (fread(buf, 1, 2, fp) == 2 && |
| 909 | ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) |
| 910 | ispyc = 1; |
| 911 | rewind(fp); |
| 912 | } |
| 913 | return ispyc; |
| 914 | } |
| 915 | return 0; |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 916 | } |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 917 | |
Guido van Rossum | 0df002c | 2000-08-27 19:21:52 +0000 | [diff] [blame] | 918 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 919 | PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 920 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 921 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 922 | PyObject *m, *d, *v; |
| 923 | const char *ext; |
Hynek Schlawack | b271b3e | 2012-11-07 09:41:28 +0100 | [diff] [blame] | 924 | int set_file_name = 0, len, ret = -1; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 926 | m = PyImport_AddModule("__main__"); |
| 927 | if (m == NULL) |
| 928 | return -1; |
Hynek Schlawack | b271b3e | 2012-11-07 09:41:28 +0100 | [diff] [blame] | 929 | Py_INCREF(m); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 930 | d = PyModule_GetDict(m); |
| 931 | if (PyDict_GetItemString(d, "__file__") == NULL) { |
| 932 | PyObject *f = PyString_FromString(filename); |
| 933 | if (f == NULL) |
Hynek Schlawack | b271b3e | 2012-11-07 09:41:28 +0100 | [diff] [blame] | 934 | goto done; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 935 | if (PyDict_SetItemString(d, "__file__", f) < 0) { |
| 936 | Py_DECREF(f); |
Hynek Schlawack | b271b3e | 2012-11-07 09:41:28 +0100 | [diff] [blame] | 937 | goto done; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 938 | } |
| 939 | set_file_name = 1; |
| 940 | Py_DECREF(f); |
| 941 | } |
| 942 | len = strlen(filename); |
| 943 | ext = filename + len - (len > 4 ? 4 : 0); |
| 944 | if (maybe_pyc_file(fp, filename, ext, closeit)) { |
| 945 | /* Try to run a pyc file. First, re-open in binary */ |
| 946 | if (closeit) |
| 947 | fclose(fp); |
| 948 | if ((fp = fopen(filename, "rb")) == NULL) { |
| 949 | fprintf(stderr, "python: Can't reopen .pyc file\n"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 950 | goto done; |
| 951 | } |
| 952 | /* Turn on optimization if a .pyo file is given */ |
| 953 | if (strcmp(ext, ".pyo") == 0) |
| 954 | Py_OptimizeFlag = 1; |
| 955 | v = run_pyc_file(fp, filename, d, d, flags); |
| 956 | } else { |
| 957 | v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, |
| 958 | closeit, flags); |
| 959 | } |
| 960 | if (v == NULL) { |
| 961 | PyErr_Print(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 962 | goto done; |
| 963 | } |
| 964 | Py_DECREF(v); |
| 965 | if (Py_FlushLine()) |
| 966 | PyErr_Clear(); |
| 967 | ret = 0; |
Georg Brandl | aa2321b | 2007-03-07 00:40:28 +0000 | [diff] [blame] | 968 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 969 | if (set_file_name && PyDict_DelItemString(d, "__file__")) |
| 970 | PyErr_Clear(); |
Hynek Schlawack | b271b3e | 2012-11-07 09:41:28 +0100 | [diff] [blame] | 971 | Py_DECREF(m); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 972 | return ret; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 976 | PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 977 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 978 | PyObject *m, *d, *v; |
| 979 | m = PyImport_AddModule("__main__"); |
| 980 | if (m == NULL) |
| 981 | return -1; |
| 982 | d = PyModule_GetDict(m); |
| 983 | v = PyRun_StringFlags(command, Py_file_input, d, d, flags); |
| 984 | if (v == NULL) { |
| 985 | PyErr_Print(); |
| 986 | return -1; |
| 987 | } |
| 988 | Py_DECREF(v); |
| 989 | if (Py_FlushLine()) |
| 990 | PyErr_Clear(); |
| 991 | return 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 994 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 995 | parse_syntax_error(PyObject *err, PyObject **message, const char **filename, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 996 | int *lineno, int *offset, const char **text) |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 997 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 998 | long hold; |
| 999 | PyObject *v; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1001 | /* old style errors */ |
| 1002 | if (PyTuple_Check(err)) |
| 1003 | return PyArg_ParseTuple(err, "O(ziiz)", message, filename, |
| 1004 | lineno, offset, text); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1005 | |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1006 | *message = NULL; |
| 1007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | /* new style errors. `err' is an instance */ |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1009 | *message = PyObject_GetAttrString(err, "msg"); |
| 1010 | if (!*message) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1011 | goto finally; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1012 | |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1013 | v = PyObject_GetAttrString(err, "filename"); |
| 1014 | if (!v) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1015 | goto finally; |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1016 | if (v == Py_None) { |
| 1017 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1018 | *filename = NULL; |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1019 | } |
| 1020 | else { |
| 1021 | *filename = PyString_AsString(v); |
| 1022 | Py_DECREF(v); |
| 1023 | if (!*filename) |
| 1024 | goto finally; |
| 1025 | } |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1026 | |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1027 | v = PyObject_GetAttrString(err, "lineno"); |
| 1028 | if (!v) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1029 | goto finally; |
| 1030 | hold = PyInt_AsLong(v); |
| 1031 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1032 | if (hold < 0 && PyErr_Occurred()) |
| 1033 | goto finally; |
| 1034 | *lineno = (int)hold; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1035 | |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1036 | v = PyObject_GetAttrString(err, "offset"); |
| 1037 | if (!v) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1038 | goto finally; |
| 1039 | if (v == Py_None) { |
| 1040 | *offset = -1; |
| 1041 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1042 | } else { |
| 1043 | hold = PyInt_AsLong(v); |
| 1044 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1045 | if (hold < 0 && PyErr_Occurred()) |
| 1046 | goto finally; |
| 1047 | *offset = (int)hold; |
| 1048 | } |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1049 | |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1050 | v = PyObject_GetAttrString(err, "text"); |
| 1051 | if (!v) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1052 | goto finally; |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1053 | if (v == Py_None) { |
| 1054 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1055 | *text = NULL; |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1056 | } |
| 1057 | else { |
| 1058 | *text = PyString_AsString(v); |
| 1059 | Py_DECREF(v); |
| 1060 | if (!*text) |
| 1061 | goto finally; |
| 1062 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1063 | return 1; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1064 | |
| 1065 | finally: |
Benjamin Peterson | b9348e7 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 1066 | Py_XDECREF(*message); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1067 | return 0; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1070 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1071 | PyErr_Print(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1072 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1073 | PyErr_PrintEx(1); |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 1076 | static void |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1077 | print_error_text(PyObject *f, int offset, const char *text) |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 1078 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1079 | char *nl; |
| 1080 | if (offset >= 0) { |
Benjamin Peterson | ec9f9f5 | 2010-10-29 03:45:34 +0000 | [diff] [blame] | 1081 | if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n') |
| 1082 | offset--; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1083 | for (;;) { |
| 1084 | nl = strchr(text, '\n'); |
| 1085 | if (nl == NULL || nl-text >= offset) |
| 1086 | break; |
| 1087 | offset -= (int)(nl+1-text); |
| 1088 | text = nl+1; |
| 1089 | } |
| 1090 | while (*text == ' ' || *text == '\t') { |
| 1091 | text++; |
| 1092 | offset--; |
| 1093 | } |
| 1094 | } |
| 1095 | PyFile_WriteString(" ", f); |
| 1096 | PyFile_WriteString(text, f); |
| 1097 | if (*text == '\0' || text[strlen(text)-1] != '\n') |
| 1098 | PyFile_WriteString("\n", f); |
| 1099 | if (offset == -1) |
| 1100 | return; |
| 1101 | PyFile_WriteString(" ", f); |
| 1102 | offset--; |
| 1103 | while (offset > 0) { |
| 1104 | PyFile_WriteString(" ", f); |
| 1105 | offset--; |
| 1106 | } |
| 1107 | PyFile_WriteString("^\n", f); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Guido van Rossum | 66e8e86 | 2001-03-23 17:54:43 +0000 | [diff] [blame] | 1110 | static void |
| 1111 | handle_system_exit(void) |
Ka-Ping Yee | 26fabb0 | 2001-03-23 15:36:41 +0000 | [diff] [blame] | 1112 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1113 | PyObject *exception, *value, *tb; |
| 1114 | int exitcode = 0; |
Tim Peters | cf615b5 | 2003-04-19 18:47:02 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1116 | if (Py_InspectFlag) |
| 1117 | /* Don't exit if -i flag was given. This flag is set to 0 |
| 1118 | * when entering interactive mode for inspecting. */ |
| 1119 | return; |
Georg Brandl | 49aafc9 | 2007-03-07 00:34:46 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1121 | PyErr_Fetch(&exception, &value, &tb); |
| 1122 | if (Py_FlushLine()) |
| 1123 | PyErr_Clear(); |
| 1124 | fflush(stdout); |
| 1125 | if (value == NULL || value == Py_None) |
| 1126 | goto done; |
| 1127 | if (PyExceptionInstance_Check(value)) { |
| 1128 | /* The error code should be in the `code' attribute. */ |
| 1129 | PyObject *code = PyObject_GetAttrString(value, "code"); |
| 1130 | if (code) { |
| 1131 | Py_DECREF(value); |
| 1132 | value = code; |
| 1133 | if (value == Py_None) |
| 1134 | goto done; |
| 1135 | } |
| 1136 | /* If we failed to dig out the 'code' attribute, |
| 1137 | just let the else clause below print the error. */ |
| 1138 | } |
Mark Dickinson | ea82972 | 2017-02-02 19:31:53 +0000 | [diff] [blame] | 1139 | if (PyInt_Check(value) || PyLong_Check(value)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1140 | exitcode = (int)PyInt_AsLong(value); |
| 1141 | else { |
Victor Stinner | c49dfcc | 2010-05-25 22:30:32 +0000 | [diff] [blame] | 1142 | PyObject *sys_stderr = PySys_GetObject("stderr"); |
| 1143 | if (sys_stderr != NULL && sys_stderr != Py_None) { |
| 1144 | PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW); |
| 1145 | } else { |
| 1146 | PyObject_Print(value, stderr, Py_PRINT_RAW); |
| 1147 | fflush(stderr); |
| 1148 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1149 | PySys_WriteStderr("\n"); |
| 1150 | exitcode = 1; |
| 1151 | } |
Tim Peters | cf615b5 | 2003-04-19 18:47:02 +0000 | [diff] [blame] | 1152 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1153 | /* Restore and clear the exception info, in order to properly decref |
| 1154 | * the exception, value, and traceback. If we just exit instead, |
| 1155 | * these leak, which confuses PYTHONDUMPREFS output, and may prevent |
| 1156 | * some finalizers from running. |
| 1157 | */ |
| 1158 | PyErr_Restore(exception, value, tb); |
| 1159 | PyErr_Clear(); |
| 1160 | Py_Exit(exitcode); |
| 1161 | /* NOTREACHED */ |
Ka-Ping Yee | 26fabb0 | 2001-03-23 15:36:41 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1165 | PyErr_PrintEx(int set_sys_last_vars) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 1166 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1167 | PyObject *exception, *v, *tb, *hook; |
Guido van Rossum | 66e8e86 | 2001-03-23 17:54:43 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1169 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 1170 | handle_system_exit(); |
| 1171 | } |
| 1172 | PyErr_Fetch(&exception, &v, &tb); |
| 1173 | if (exception == NULL) |
| 1174 | return; |
| 1175 | PyErr_NormalizeException(&exception, &v, &tb); |
| 1176 | if (exception == NULL) |
| 1177 | return; |
| 1178 | /* Now we know v != NULL too */ |
| 1179 | if (set_sys_last_vars) { |
| 1180 | PySys_SetObject("last_type", exception); |
| 1181 | PySys_SetObject("last_value", v); |
| 1182 | PySys_SetObject("last_traceback", tb); |
| 1183 | } |
| 1184 | hook = PySys_GetObject("excepthook"); |
Antoine Pitrou | a03ff6d | 2010-08-08 21:37:51 +0000 | [diff] [blame] | 1185 | if (hook && hook != Py_None) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1186 | PyObject *args = PyTuple_Pack(3, |
| 1187 | exception, v, tb ? tb : Py_None); |
| 1188 | PyObject *result = PyEval_CallObject(hook, args); |
| 1189 | if (result == NULL) { |
| 1190 | PyObject *exception2, *v2, *tb2; |
| 1191 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 1192 | handle_system_exit(); |
| 1193 | } |
| 1194 | PyErr_Fetch(&exception2, &v2, &tb2); |
| 1195 | PyErr_NormalizeException(&exception2, &v2, &tb2); |
| 1196 | /* It should not be possible for exception2 or v2 |
| 1197 | to be NULL. However PyErr_Display() can't |
| 1198 | tolerate NULLs, so just be safe. */ |
| 1199 | if (exception2 == NULL) { |
| 1200 | exception2 = Py_None; |
| 1201 | Py_INCREF(exception2); |
| 1202 | } |
| 1203 | if (v2 == NULL) { |
| 1204 | v2 = Py_None; |
| 1205 | Py_INCREF(v2); |
| 1206 | } |
| 1207 | if (Py_FlushLine()) |
| 1208 | PyErr_Clear(); |
| 1209 | fflush(stdout); |
| 1210 | PySys_WriteStderr("Error in sys.excepthook:\n"); |
| 1211 | PyErr_Display(exception2, v2, tb2); |
| 1212 | PySys_WriteStderr("\nOriginal exception was:\n"); |
| 1213 | PyErr_Display(exception, v, tb); |
| 1214 | Py_DECREF(exception2); |
| 1215 | Py_DECREF(v2); |
| 1216 | Py_XDECREF(tb2); |
| 1217 | } |
| 1218 | Py_XDECREF(result); |
| 1219 | Py_XDECREF(args); |
| 1220 | } else { |
| 1221 | PySys_WriteStderr("sys.excepthook is missing\n"); |
| 1222 | PyErr_Display(exception, v, tb); |
| 1223 | } |
| 1224 | Py_XDECREF(exception); |
| 1225 | Py_XDECREF(v); |
| 1226 | Py_XDECREF(tb); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1229 | void |
| 1230 | PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1231 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1232 | int err = 0; |
| 1233 | PyObject *f = PySys_GetObject("stderr"); |
| 1234 | Py_INCREF(value); |
Antoine Pitrou | a03ff6d | 2010-08-08 21:37:51 +0000 | [diff] [blame] | 1235 | if (f == NULL || f == Py_None) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1236 | fprintf(stderr, "lost sys.stderr\n"); |
| 1237 | else { |
| 1238 | if (Py_FlushLine()) |
| 1239 | PyErr_Clear(); |
| 1240 | fflush(stdout); |
| 1241 | if (tb && tb != Py_None) |
| 1242 | err = PyTraceBack_Print(tb, f); |
| 1243 | if (err == 0 && |
| 1244 | PyObject_HasAttrString(value, "print_file_and_line")) |
| 1245 | { |
| 1246 | PyObject *message; |
| 1247 | const char *filename, *text; |
| 1248 | int lineno, offset; |
| 1249 | if (!parse_syntax_error(value, &message, &filename, |
| 1250 | &lineno, &offset, &text)) |
| 1251 | PyErr_Clear(); |
| 1252 | else { |
| 1253 | char buf[10]; |
| 1254 | PyFile_WriteString(" File \"", f); |
| 1255 | if (filename == NULL) |
| 1256 | PyFile_WriteString("<string>", f); |
| 1257 | else |
| 1258 | PyFile_WriteString(filename, f); |
| 1259 | PyFile_WriteString("\", line ", f); |
| 1260 | PyOS_snprintf(buf, sizeof(buf), "%d", lineno); |
| 1261 | PyFile_WriteString(buf, f); |
| 1262 | PyFile_WriteString("\n", f); |
| 1263 | if (text != NULL) |
| 1264 | print_error_text(f, offset, text); |
| 1265 | Py_DECREF(value); |
| 1266 | value = message; |
| 1267 | /* Can't be bothered to check all those |
| 1268 | PyFile_WriteString() calls */ |
| 1269 | if (PyErr_Occurred()) |
| 1270 | err = -1; |
| 1271 | } |
| 1272 | } |
| 1273 | if (err) { |
| 1274 | /* Don't do anything else */ |
| 1275 | } |
| 1276 | else if (PyExceptionClass_Check(exception)) { |
| 1277 | PyObject* moduleName; |
| 1278 | char* className = PyExceptionClass_Name(exception); |
| 1279 | if (className != NULL) { |
| 1280 | char *dot = strrchr(className, '.'); |
| 1281 | if (dot != NULL) |
| 1282 | className = dot+1; |
| 1283 | } |
Barry Warsaw | 2f5f6a2 | 1997-09-16 21:42:03 +0000 | [diff] [blame] | 1284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1285 | moduleName = PyObject_GetAttrString(exception, "__module__"); |
| 1286 | if (moduleName == NULL) |
| 1287 | err = PyFile_WriteString("<unknown>", f); |
| 1288 | else { |
| 1289 | char* modstr = PyString_AsString(moduleName); |
| 1290 | if (modstr && strcmp(modstr, "exceptions")) |
| 1291 | { |
| 1292 | err = PyFile_WriteString(modstr, f); |
| 1293 | err += PyFile_WriteString(".", f); |
| 1294 | } |
| 1295 | Py_DECREF(moduleName); |
| 1296 | } |
| 1297 | if (err == 0) { |
| 1298 | if (className == NULL) |
| 1299 | err = PyFile_WriteString("<unknown>", f); |
| 1300 | else |
| 1301 | err = PyFile_WriteString(className, f); |
| 1302 | } |
| 1303 | } |
| 1304 | else |
| 1305 | err = PyFile_WriteObject(exception, f, Py_PRINT_RAW); |
| 1306 | if (err == 0 && (value != Py_None)) { |
| 1307 | PyObject *s = PyObject_Str(value); |
| 1308 | /* only print colon if the str() of the |
| 1309 | object is not the empty string |
| 1310 | */ |
Martin Panter | ef85a1a | 2016-02-28 00:18:43 +0000 | [diff] [blame] | 1311 | if (s == NULL) { |
| 1312 | PyErr_Clear(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1313 | err = -1; |
Martin Panter | ef85a1a | 2016-02-28 00:18:43 +0000 | [diff] [blame] | 1314 | PyFile_WriteString(": <exception str() failed>", f); |
| 1315 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1316 | else if (!PyString_Check(s) || |
| 1317 | PyString_GET_SIZE(s) != 0) |
| 1318 | err = PyFile_WriteString(": ", f); |
| 1319 | if (err == 0) |
| 1320 | err = PyFile_WriteObject(s, f, Py_PRINT_RAW); |
| 1321 | Py_XDECREF(s); |
| 1322 | } |
| 1323 | /* try to write a newline in any case */ |
Martin Panter | ef85a1a | 2016-02-28 00:18:43 +0000 | [diff] [blame] | 1324 | if (err < 0) { |
| 1325 | PyErr_Clear(); |
| 1326 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1327 | err += PyFile_WriteString("\n", f); |
| 1328 | } |
| 1329 | Py_DECREF(value); |
| 1330 | /* If an error happened here, don't show it. |
| 1331 | XXX This is wrong, but too many callers rely on this behavior. */ |
| 1332 | if (err != 0) |
| 1333 | PyErr_Clear(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1336 | PyObject * |
Tim Peters | 5e9d6cf | 2006-05-28 10:41:29 +0000 | [diff] [blame] | 1337 | PyRun_StringFlags(const char *str, int start, PyObject *globals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1338 | PyObject *locals, PyCompilerFlags *flags) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1339 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1340 | PyObject *ret = NULL; |
| 1341 | mod_ty mod; |
| 1342 | PyArena *arena = PyArena_New(); |
| 1343 | if (arena == NULL) |
| 1344 | return NULL; |
Brett Cannon | c33e82d | 2010-05-05 20:38:52 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1346 | mod = PyParser_ASTFromString(str, "<string>", start, flags, arena); |
| 1347 | if (mod != NULL) |
| 1348 | ret = run_mod(mod, "<string>", globals, locals, flags, arena); |
| 1349 | PyArena_Free(arena); |
| 1350 | return ret; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | PyObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1354 | PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1355 | PyObject *locals, int closeit, PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1356 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1357 | PyObject *ret; |
| 1358 | mod_ty mod; |
| 1359 | PyArena *arena = PyArena_New(); |
| 1360 | if (arena == NULL) |
| 1361 | return NULL; |
Brett Cannon | c33e82d | 2010-05-05 20:38:52 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1363 | mod = PyParser_ASTFromFile(fp, filename, start, 0, 0, |
| 1364 | flags, NULL, arena); |
| 1365 | if (closeit) |
| 1366 | fclose(fp); |
| 1367 | if (mod == NULL) { |
| 1368 | PyArena_Free(arena); |
| 1369 | return NULL; |
| 1370 | } |
| 1371 | ret = run_mod(mod, filename, globals, locals, flags, arena); |
| 1372 | PyArena_Free(arena); |
| 1373 | return ret; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1376 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1377 | run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1378 | PyCompilerFlags *flags, PyArena *arena) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1379 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1380 | PyCodeObject *co; |
| 1381 | PyObject *v; |
| 1382 | co = PyAST_Compile(mod, filename, flags, arena); |
| 1383 | if (co == NULL) |
| 1384 | return NULL; |
| 1385 | v = PyEval_EvalCode(co, globals, locals); |
| 1386 | Py_DECREF(co); |
| 1387 | return v; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1390 | static PyObject * |
Tim Peters | 5e9d6cf | 2006-05-28 10:41:29 +0000 | [diff] [blame] | 1391 | run_pyc_file(FILE *fp, const char *filename, PyObject *globals, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1392 | PyObject *locals, PyCompilerFlags *flags) |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1393 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1394 | PyCodeObject *co; |
| 1395 | PyObject *v; |
| 1396 | long magic; |
| 1397 | long PyImport_GetMagicNumber(void); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1399 | magic = PyMarshal_ReadLongFromFile(fp); |
| 1400 | if (magic != PyImport_GetMagicNumber()) { |
| 1401 | PyErr_SetString(PyExc_RuntimeError, |
| 1402 | "Bad magic number in .pyc file"); |
| 1403 | return NULL; |
| 1404 | } |
| 1405 | (void) PyMarshal_ReadLongFromFile(fp); |
| 1406 | v = PyMarshal_ReadLastObjectFromFile(fp); |
| 1407 | fclose(fp); |
| 1408 | if (v == NULL || !PyCode_Check(v)) { |
| 1409 | Py_XDECREF(v); |
| 1410 | PyErr_SetString(PyExc_RuntimeError, |
| 1411 | "Bad code object in .pyc file"); |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | co = (PyCodeObject *)v; |
| 1415 | v = PyEval_EvalCode(co, globals, locals); |
| 1416 | if (v && flags) |
| 1417 | flags->cf_flags |= (co->co_flags & PyCF_MASK); |
| 1418 | Py_DECREF(co); |
| 1419 | return v; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1422 | PyObject * |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 1423 | Py_CompileStringFlags(const char *str, const char *filename, int start, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1424 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1425 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1426 | PyCodeObject *co; |
| 1427 | mod_ty mod; |
| 1428 | PyArena *arena = PyArena_New(); |
| 1429 | if (arena == NULL) |
| 1430 | return NULL; |
Neal Norwitz | b59d08c | 2006-07-22 16:20:49 +0000 | [diff] [blame] | 1431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1432 | mod = PyParser_ASTFromString(str, filename, start, flags, arena); |
| 1433 | if (mod == NULL) { |
| 1434 | PyArena_Free(arena); |
| 1435 | return NULL; |
| 1436 | } |
| 1437 | if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { |
| 1438 | PyObject *result = PyAST_mod2obj(mod); |
| 1439 | PyArena_Free(arena); |
| 1440 | return result; |
| 1441 | } |
| 1442 | co = PyAST_Compile(mod, filename, flags, arena); |
| 1443 | PyArena_Free(arena); |
| 1444 | return (PyObject *)co; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1447 | struct symtable * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1448 | Py_SymtableString(const char *str, const char *filename, int start) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1449 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1450 | struct symtable *st; |
| 1451 | mod_ty mod; |
| 1452 | PyCompilerFlags flags; |
| 1453 | PyArena *arena = PyArena_New(); |
| 1454 | if (arena == NULL) |
| 1455 | return NULL; |
Neal Norwitz | b59d08c | 2006-07-22 16:20:49 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1457 | flags.cf_flags = 0; |
Christian Heimes | 7f23d86 | 2008-03-26 22:51:58 +0000 | [diff] [blame] | 1458 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1459 | mod = PyParser_ASTFromString(str, filename, start, &flags, arena); |
| 1460 | if (mod == NULL) { |
| 1461 | PyArena_Free(arena); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | st = PySymtable_Build(mod, filename, 0); |
| 1465 | PyArena_Free(arena); |
| 1466 | return st; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1469 | /* Preferred access to parser is through AST. */ |
| 1470 | mod_ty |
Tim Peters | 5e9d6cf | 2006-05-28 10:41:29 +0000 | [diff] [blame] | 1471 | PyParser_ASTFromString(const char *s, const char *filename, int start, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1472 | PyCompilerFlags *flags, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1473 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1474 | mod_ty mod; |
| 1475 | PyCompilerFlags localflags; |
| 1476 | perrdetail err; |
| 1477 | int iflags = PARSER_FLAGS(flags); |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 1478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1479 | node *n = PyParser_ParseStringFlagsFilenameEx(s, filename, |
| 1480 | &_PyParser_Grammar, start, &err, |
| 1481 | &iflags); |
| 1482 | if (flags == NULL) { |
| 1483 | localflags.cf_flags = 0; |
| 1484 | flags = &localflags; |
| 1485 | } |
| 1486 | if (n) { |
| 1487 | flags->cf_flags |= iflags & PyCF_MASK; |
| 1488 | mod = PyAST_FromNode(n, flags, filename, arena); |
| 1489 | PyNode_Free(n); |
| 1490 | return mod; |
| 1491 | } |
| 1492 | else { |
| 1493 | err_input(&err); |
| 1494 | return NULL; |
| 1495 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | mod_ty |
Tim Peters | 5e9d6cf | 2006-05-28 10:41:29 +0000 | [diff] [blame] | 1499 | PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1500 | char *ps2, PyCompilerFlags *flags, int *errcode, |
| 1501 | PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1502 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1503 | mod_ty mod; |
| 1504 | PyCompilerFlags localflags; |
| 1505 | perrdetail err; |
| 1506 | int iflags = PARSER_FLAGS(flags); |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1508 | node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar, |
| 1509 | start, ps1, ps2, &err, &iflags); |
| 1510 | if (flags == NULL) { |
| 1511 | localflags.cf_flags = 0; |
| 1512 | flags = &localflags; |
| 1513 | } |
| 1514 | if (n) { |
| 1515 | flags->cf_flags |= iflags & PyCF_MASK; |
| 1516 | mod = PyAST_FromNode(n, flags, filename, arena); |
| 1517 | PyNode_Free(n); |
| 1518 | return mod; |
| 1519 | } |
| 1520 | else { |
| 1521 | err_input(&err); |
| 1522 | if (errcode) |
| 1523 | *errcode = err.error; |
| 1524 | return NULL; |
| 1525 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1528 | /* Simplified interface to parsefile -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1529 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1530 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1531 | PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1532 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1533 | perrdetail err; |
| 1534 | node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, |
| 1535 | start, NULL, NULL, &err, flags); |
| 1536 | if (n == NULL) |
| 1537 | err_input(&err); |
Tim Peters | 5e9d6cf | 2006-05-28 10:41:29 +0000 | [diff] [blame] | 1538 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1539 | return n; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1542 | /* Simplified interface to parsestring -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1543 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1544 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1545 | PyParser_SimpleParseStringFlags(const char *str, int start, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 1546 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1547 | perrdetail err; |
| 1548 | node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, |
| 1549 | start, &err, flags); |
| 1550 | if (n == NULL) |
| 1551 | err_input(&err); |
| 1552 | return n; |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1556 | PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1557 | int start, int flags) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1558 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1559 | perrdetail err; |
| 1560 | node *n = PyParser_ParseStringFlagsFilename(str, filename, |
| 1561 | &_PyParser_Grammar, start, &err, flags); |
| 1562 | if (n == NULL) |
| 1563 | err_input(&err); |
| 1564 | return n; |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1568 | PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1569 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1570 | return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0); |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 1573 | /* May want to move a more generalized form of this to parsetok.c or |
| 1574 | even parser modules. */ |
| 1575 | |
| 1576 | void |
| 1577 | PyParser_SetError(perrdetail *err) |
| 1578 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1579 | err_input(err); |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1582 | /* Set the error appropriate to the given input error code (see errcode.h) */ |
| 1583 | |
| 1584 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1585 | err_input(perrdetail *err) |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1586 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1587 | PyObject *v, *w, *errtype; |
| 1588 | PyObject* u = NULL; |
| 1589 | char *msg = NULL; |
| 1590 | errtype = PyExc_SyntaxError; |
| 1591 | switch (err->error) { |
| 1592 | case E_ERROR: |
| 1593 | return; |
| 1594 | case E_SYNTAX: |
| 1595 | errtype = PyExc_IndentationError; |
| 1596 | if (err->expected == INDENT) |
| 1597 | msg = "expected an indented block"; |
| 1598 | else if (err->token == INDENT) |
| 1599 | msg = "unexpected indent"; |
| 1600 | else if (err->token == DEDENT) |
| 1601 | msg = "unexpected unindent"; |
| 1602 | else { |
| 1603 | errtype = PyExc_SyntaxError; |
| 1604 | msg = "invalid syntax"; |
| 1605 | } |
| 1606 | break; |
| 1607 | case E_TOKEN: |
| 1608 | msg = "invalid token"; |
| 1609 | break; |
| 1610 | case E_EOFS: |
| 1611 | msg = "EOF while scanning triple-quoted string literal"; |
| 1612 | break; |
| 1613 | case E_EOLS: |
| 1614 | msg = "EOL while scanning string literal"; |
| 1615 | break; |
| 1616 | case E_INTR: |
| 1617 | if (!PyErr_Occurred()) |
| 1618 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 1619 | goto cleanup; |
| 1620 | case E_NOMEM: |
| 1621 | PyErr_NoMemory(); |
| 1622 | goto cleanup; |
| 1623 | case E_EOF: |
| 1624 | msg = "unexpected EOF while parsing"; |
| 1625 | break; |
| 1626 | case E_TABSPACE: |
| 1627 | errtype = PyExc_TabError; |
| 1628 | msg = "inconsistent use of tabs and spaces in indentation"; |
| 1629 | break; |
| 1630 | case E_OVERFLOW: |
| 1631 | msg = "expression too long"; |
| 1632 | break; |
| 1633 | case E_DEDENT: |
| 1634 | errtype = PyExc_IndentationError; |
| 1635 | msg = "unindent does not match any outer indentation level"; |
| 1636 | break; |
| 1637 | case E_TOODEEP: |
| 1638 | errtype = PyExc_IndentationError; |
| 1639 | msg = "too many levels of indentation"; |
| 1640 | break; |
| 1641 | case E_DECODE: { |
| 1642 | PyObject *type, *value, *tb; |
| 1643 | PyErr_Fetch(&type, &value, &tb); |
| 1644 | if (value != NULL) { |
| 1645 | u = PyObject_Str(value); |
| 1646 | if (u != NULL) { |
| 1647 | msg = PyString_AsString(u); |
| 1648 | } |
| 1649 | } |
| 1650 | if (msg == NULL) |
| 1651 | msg = "unknown decode error"; |
| 1652 | Py_XDECREF(type); |
| 1653 | Py_XDECREF(value); |
| 1654 | Py_XDECREF(tb); |
| 1655 | break; |
| 1656 | } |
| 1657 | case E_LINECONT: |
| 1658 | msg = "unexpected character after line continuation character"; |
| 1659 | break; |
| 1660 | default: |
| 1661 | fprintf(stderr, "error=%d\n", err->error); |
| 1662 | msg = "unknown parsing error"; |
| 1663 | break; |
| 1664 | } |
| 1665 | v = Py_BuildValue("(ziiz)", err->filename, |
| 1666 | err->lineno, err->offset, err->text); |
| 1667 | w = NULL; |
| 1668 | if (v != NULL) |
| 1669 | w = Py_BuildValue("(sO)", msg, v); |
| 1670 | Py_XDECREF(u); |
| 1671 | Py_XDECREF(v); |
| 1672 | PyErr_SetObject(errtype, w); |
| 1673 | Py_XDECREF(w); |
Georg Brandl | 1ad108d | 2008-07-19 10:08:55 +0000 | [diff] [blame] | 1674 | cleanup: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1675 | if (err->text != NULL) { |
| 1676 | PyObject_FREE(err->text); |
| 1677 | err->text = NULL; |
| 1678 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* Print fatal error message and abort */ |
| 1682 | |
| 1683 | void |
Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 1684 | Py_FatalError(const char *msg) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1685 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1686 | fprintf(stderr, "Fatal Python error: %s\n", msg); |
| 1687 | fflush(stderr); /* it helps in Windows debug build */ |
Jesse Noller | 42f9b4e | 2009-03-31 22:20:35 +0000 | [diff] [blame] | 1688 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1689 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1690 | { |
| 1691 | size_t len = strlen(msg); |
| 1692 | WCHAR* buffer; |
| 1693 | size_t i; |
Martin v. Löwis | 5344c99 | 2009-01-02 20:32:55 +0000 | [diff] [blame] | 1694 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1695 | /* Convert the message to wchar_t. This uses a simple one-to-one |
| 1696 | conversion, assuming that the this error message actually uses ASCII |
| 1697 | only. If this ceases to be true, we will have to convert. */ |
| 1698 | buffer = alloca( (len+1) * (sizeof *buffer)); |
| 1699 | for( i=0; i<=len; ++i) |
| 1700 | buffer[i] = msg[i]; |
| 1701 | OutputDebugStringW(L"Fatal Python error: "); |
| 1702 | OutputDebugStringW(buffer); |
| 1703 | OutputDebugStringW(L"\n"); |
| 1704 | } |
Guido van Rossum | 0ba3536 | 1998-08-13 13:33:16 +0000 | [diff] [blame] | 1705 | #ifdef _DEBUG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1706 | DebugBreak(); |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 1707 | #endif |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1708 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1709 | abort(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | /* Clean up and exit */ |
| 1713 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1714 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 1715 | #include "pythread.h" |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 1716 | #endif |
| 1717 | |
Antoine Pitrou | efb60c0 | 2009-10-20 21:29:37 +0000 | [diff] [blame] | 1718 | /* Wait until threading._shutdown completes, provided |
| 1719 | the threading module was imported in the first place. |
| 1720 | The shutdown routine will wait until all non-daemon |
| 1721 | "threading" threads have completed. */ |
| 1722 | static void |
| 1723 | wait_for_thread_shutdown(void) |
| 1724 | { |
| 1725 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1726 | PyObject *result; |
| 1727 | PyThreadState *tstate = PyThreadState_GET(); |
| 1728 | PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, |
| 1729 | "threading"); |
| 1730 | if (threading == NULL) { |
| 1731 | /* threading not imported */ |
| 1732 | PyErr_Clear(); |
| 1733 | return; |
| 1734 | } |
| 1735 | result = PyObject_CallMethod(threading, "_shutdown", ""); |
| 1736 | if (result == NULL) |
| 1737 | PyErr_WriteUnraisable(threading); |
| 1738 | else |
| 1739 | Py_DECREF(result); |
| 1740 | Py_DECREF(threading); |
Antoine Pitrou | efb60c0 | 2009-10-20 21:29:37 +0000 | [diff] [blame] | 1741 | #endif |
| 1742 | } |
| 1743 | |
Guido van Rossum | 2dcfc96 | 1998-10-01 16:01:57 +0000 | [diff] [blame] | 1744 | #define NEXITFUNCS 32 |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1745 | static void (*exitfuncs[NEXITFUNCS])(void); |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1746 | static int nexitfuncs = 0; |
| 1747 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1748 | int Py_AtExit(void (*func)(void)) |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1749 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1750 | if (nexitfuncs >= NEXITFUNCS) |
| 1751 | return -1; |
| 1752 | exitfuncs[nexitfuncs++] = func; |
| 1753 | return 0; |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1756 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1757 | call_sys_exitfunc(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1758 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1759 | PyObject *exitfunc = PySys_GetObject("exitfunc"); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1760 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1761 | if (exitfunc) { |
| 1762 | PyObject *res; |
| 1763 | Py_INCREF(exitfunc); |
| 1764 | PySys_SetObject("exitfunc", (PyObject *)NULL); |
| 1765 | res = PyEval_CallObject(exitfunc, (PyObject *)NULL); |
| 1766 | if (res == NULL) { |
| 1767 | if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 1768 | PySys_WriteStderr("Error in sys.exitfunc:\n"); |
| 1769 | } |
| 1770 | PyErr_Print(); |
| 1771 | } |
| 1772 | Py_DECREF(exitfunc); |
| 1773 | } |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1775 | if (Py_FlushLine()) |
| 1776 | PyErr_Clear(); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1777 | } |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1778 | |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1779 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1780 | call_ll_exitfuncs(void) |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1781 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1782 | while (nexitfuncs > 0) |
| 1783 | (*exitfuncs[--nexitfuncs])(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1785 | fflush(stdout); |
| 1786 | fflush(stderr); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1790 | Py_Exit(int sts) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1791 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1792 | Py_Finalize(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1794 | exit(sts); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 1797 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1798 | initsigs(void) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1799 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1800 | #ifdef SIGPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1801 | PyOS_setsig(SIGPIPE, SIG_IGN); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1802 | #endif |
Guido van Rossum | 70d893a | 2001-08-16 08:21:42 +0000 | [diff] [blame] | 1803 | #ifdef SIGXFZ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1804 | PyOS_setsig(SIGXFZ, SIG_IGN); |
Guido van Rossum | 70d893a | 2001-08-16 08:21:42 +0000 | [diff] [blame] | 1805 | #endif |
Jeremy Hylton | 1b0bf9b | 2002-04-23 20:31:01 +0000 | [diff] [blame] | 1806 | #ifdef SIGXFSZ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1807 | PyOS_setsig(SIGXFSZ, SIG_IGN); |
Jeremy Hylton | 1b0bf9b | 2002-04-23 20:31:01 +0000 | [diff] [blame] | 1808 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1809 | PyOS_InitInterrupts(); /* May imply initsignal() */ |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 1812 | |
| 1813 | /* |
| 1814 | * The file descriptor fd is considered ``interactive'' if either |
| 1815 | * a) isatty(fd) is TRUE, or |
| 1816 | * b) the -i flag was given, and the filename associated with |
| 1817 | * the descriptor is NULL or "<stdin>" or "???". |
| 1818 | */ |
| 1819 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1820 | Py_FdIsInteractive(FILE *fp, const char *filename) |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 1821 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1822 | if (isatty((int)fileno(fp))) |
| 1823 | return 1; |
| 1824 | if (!Py_InteractiveFlag) |
| 1825 | return 0; |
| 1826 | return (filename == NULL) || |
| 1827 | (strcmp(filename, "<stdin>") == 0) || |
| 1828 | (strcmp(filename, "???") == 0); |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 1829 | } |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 1830 | |
| 1831 | |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 1832 | #if defined(USE_STACKCHECK) |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 1833 | #if defined(WIN32) && defined(_MSC_VER) |
| 1834 | |
| 1835 | /* Stack checking for Microsoft C */ |
| 1836 | |
| 1837 | #include <malloc.h> |
| 1838 | #include <excpt.h> |
| 1839 | |
Fred Drake | e8de31c | 2000-08-31 05:38:39 +0000 | [diff] [blame] | 1840 | /* |
| 1841 | * Return non-zero when we run out of memory on the stack; zero otherwise. |
| 1842 | */ |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 1843 | int |
Fred Drake | 399739f | 2000-08-31 05:52:44 +0000 | [diff] [blame] | 1844 | PyOS_CheckStack(void) |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 1845 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1846 | __try { |
| 1847 | /* alloca throws a stack overflow exception if there's |
| 1848 | not enough space left on the stack */ |
| 1849 | alloca(PYOS_STACK_MARGIN * sizeof(void*)); |
| 1850 | return 0; |
| 1851 | } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ? |
| 1852 | EXCEPTION_EXECUTE_HANDLER : |
| 1853 | EXCEPTION_CONTINUE_SEARCH) { |
| 1854 | int errcode = _resetstkoflw(); |
| 1855 | if (errcode == 0) |
| 1856 | { |
| 1857 | Py_FatalError("Could not reset the stack!"); |
| 1858 | } |
| 1859 | } |
| 1860 | return 1; |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | #endif /* WIN32 && _MSC_VER */ |
| 1864 | |
| 1865 | /* Alternate implementations can be added here... */ |
| 1866 | |
| 1867 | #endif /* USE_STACKCHECK */ |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 1868 | |
| 1869 | |
| 1870 | /* Wrappers around sigaction() or signal(). */ |
| 1871 | |
| 1872 | PyOS_sighandler_t |
| 1873 | PyOS_getsig(int sig) |
| 1874 | { |
| 1875 | #ifdef HAVE_SIGACTION |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1876 | struct sigaction context; |
| 1877 | if (sigaction(sig, NULL, &context) == -1) |
| 1878 | return SIG_ERR; |
| 1879 | return context.sa_handler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 1880 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1881 | PyOS_sighandler_t handler; |
Martin v. Löwis | b45b315 | 2005-11-28 17:34:23 +0000 | [diff] [blame] | 1882 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
| 1883 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1884 | switch (sig) { |
| 1885 | /* Only these signals are valid */ |
| 1886 | case SIGINT: |
| 1887 | case SIGILL: |
| 1888 | case SIGFPE: |
| 1889 | case SIGSEGV: |
| 1890 | case SIGTERM: |
| 1891 | case SIGBREAK: |
| 1892 | case SIGABRT: |
| 1893 | break; |
| 1894 | /* Don't call signal() with other values or it will assert */ |
| 1895 | default: |
| 1896 | return SIG_ERR; |
| 1897 | } |
Martin v. Löwis | b45b315 | 2005-11-28 17:34:23 +0000 | [diff] [blame] | 1898 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1899 | handler = signal(sig, SIG_IGN); |
| 1900 | if (handler != SIG_ERR) |
| 1901 | signal(sig, handler); |
| 1902 | return handler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 1903 | #endif |
| 1904 | } |
| 1905 | |
| 1906 | PyOS_sighandler_t |
| 1907 | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
| 1908 | { |
| 1909 | #ifdef HAVE_SIGACTION |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1910 | /* Some code in Modules/signalmodule.c depends on sigaction() being |
| 1911 | * used here if HAVE_SIGACTION is defined. Fix that if this code |
| 1912 | * changes to invalidate that assumption. |
| 1913 | */ |
| 1914 | struct sigaction context, ocontext; |
| 1915 | context.sa_handler = handler; |
| 1916 | sigemptyset(&context.sa_mask); |
| 1917 | context.sa_flags = 0; |
| 1918 | if (sigaction(sig, &context, &ocontext) == -1) |
| 1919 | return SIG_ERR; |
| 1920 | return ocontext.sa_handler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 1921 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1922 | PyOS_sighandler_t oldhandler; |
| 1923 | oldhandler = signal(sig, handler); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 1924 | #ifdef HAVE_SIGINTERRUPT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1925 | siginterrupt(sig, 1); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 1926 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1927 | return oldhandler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 1928 | #endif |
| 1929 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | |
Martin Panter | b1d867f | 2016-05-26 05:28:50 +0000 | [diff] [blame] | 1931 | /* Deprecated C API functions still provided for binary compatibility */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1932 | |
| 1933 | #undef PyParser_SimpleParseFile |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1934 | PyAPI_FUNC(node *) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1935 | PyParser_SimpleParseFile(FILE *fp, const char *filename, int start) |
| 1936 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1937 | return PyParser_SimpleParseFileFlags(fp, filename, start, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1940 | #undef PyParser_SimpleParseString |
| 1941 | PyAPI_FUNC(node *) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1942 | PyParser_SimpleParseString(const char *str, int start) |
| 1943 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1944 | return PyParser_SimpleParseStringFlags(str, start, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1945 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 1946 | |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1947 | #undef PyRun_AnyFile |
| 1948 | PyAPI_FUNC(int) |
| 1949 | PyRun_AnyFile(FILE *fp, const char *name) |
| 1950 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1951 | return PyRun_AnyFileExFlags(fp, name, 0, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | #undef PyRun_AnyFileEx |
| 1955 | PyAPI_FUNC(int) |
| 1956 | PyRun_AnyFileEx(FILE *fp, const char *name, int closeit) |
| 1957 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1958 | return PyRun_AnyFileExFlags(fp, name, closeit, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | #undef PyRun_AnyFileFlags |
| 1962 | PyAPI_FUNC(int) |
| 1963 | PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags) |
| 1964 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1965 | return PyRun_AnyFileExFlags(fp, name, 0, flags); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | #undef PyRun_File |
| 1969 | PyAPI_FUNC(PyObject *) |
| 1970 | PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l) |
| 1971 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1972 | return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | #undef PyRun_FileEx |
| 1976 | PyAPI_FUNC(PyObject *) |
| 1977 | PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c) |
| 1978 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1979 | return PyRun_FileExFlags(fp, p, s, g, l, c, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | #undef PyRun_FileFlags |
| 1983 | PyAPI_FUNC(PyObject *) |
| 1984 | PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1985 | PyCompilerFlags *flags) |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1986 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1987 | return PyRun_FileExFlags(fp, p, s, g, l, 0, flags); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | #undef PyRun_SimpleFile |
| 1991 | PyAPI_FUNC(int) |
| 1992 | PyRun_SimpleFile(FILE *f, const char *p) |
| 1993 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1994 | return PyRun_SimpleFileExFlags(f, p, 0, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | #undef PyRun_SimpleFileEx |
| 1998 | PyAPI_FUNC(int) |
| 1999 | PyRun_SimpleFileEx(FILE *f, const char *p, int c) |
| 2000 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2001 | return PyRun_SimpleFileExFlags(f, p, c, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | |
| 2005 | #undef PyRun_String |
| 2006 | PyAPI_FUNC(PyObject *) |
| 2007 | PyRun_String(const char *str, int s, PyObject *g, PyObject *l) |
| 2008 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2009 | return PyRun_StringFlags(str, s, g, l, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | #undef PyRun_SimpleString |
| 2013 | PyAPI_FUNC(int) |
| 2014 | PyRun_SimpleString(const char *s) |
| 2015 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2016 | return PyRun_SimpleStringFlags(s, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | #undef Py_CompileString |
| 2020 | PyAPI_FUNC(PyObject *) |
| 2021 | Py_CompileString(const char *str, const char *p, int s) |
| 2022 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2023 | return Py_CompileStringFlags(str, p, s, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | #undef PyRun_InteractiveOne |
| 2027 | PyAPI_FUNC(int) |
| 2028 | PyRun_InteractiveOne(FILE *f, const char *p) |
| 2029 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2030 | return PyRun_InteractiveOneFlags(f, p, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | #undef PyRun_InteractiveLoop |
| 2034 | PyAPI_FUNC(int) |
| 2035 | PyRun_InteractiveLoop(FILE *f, const char *p) |
| 2036 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2037 | return PyRun_InteractiveLoopFlags(f, p, NULL); |
Thomas Heller | 1b04664 | 2006-04-18 18:51:06 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 2040 | #ifdef __cplusplus |
| 2041 | } |
| 2042 | #endif |
| 2043 | |