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