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