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