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