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