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