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