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