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