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