Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Python interpreter top-level routines, including init/exit */ |
| 33 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 35 | |
| 36 | #include "grammar.h" |
| 37 | #include "node.h" |
| 38 | #include "parsetok.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 39 | #include "errcode.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 40 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 41 | #include "eval.h" |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 42 | #include "marshal.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 44 | #ifdef HAVE_UNISTD_H |
| 45 | #include <unistd.h> |
| 46 | #endif |
| 47 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 48 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 49 | #include <signal.h> |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 52 | #ifdef MS_WIN32 |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 53 | #undef BYTE |
| 54 | #include "windows.h" |
| 55 | #endif |
| 56 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 57 | extern char *Py_GetPath(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 59 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 61 | /* Forward */ |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 62 | static void initmain Py_PROTO((void)); |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 63 | static void initsite Py_PROTO((void)); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 64 | static PyObject *run_err_node Py_PROTO((node *n, char *filename, |
| 65 | PyObject *globals, PyObject *locals)); |
| 66 | static PyObject *run_node Py_PROTO((node *n, char *filename, |
| 67 | PyObject *globals, PyObject *locals)); |
| 68 | static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename, |
| 69 | PyObject *globals, PyObject *locals)); |
| 70 | static void err_input Py_PROTO((perrdetail *)); |
| 71 | static void initsigs Py_PROTO((void)); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 72 | static void call_sys_exitfunc Py_PROTO((void)); |
| 73 | static void call_ll_exitfuncs Py_PROTO((void)); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 75 | int Py_DebugFlag; /* Needed by parser.c */ |
| 76 | int Py_VerboseFlag; /* Needed by import.c */ |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 77 | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 78 | int Py_NoSiteFlag; /* Suppress 'import site' */ |
Guido van Rossum | 999e5e9 | 1997-10-03 19:46:02 +0000 | [diff] [blame] | 79 | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c */ |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 80 | int Py_FrozenFlag; /* Needed by getpath.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 82 | static int initialized = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 84 | /* API to access the initialized flag -- useful for eroteric use */ |
| 85 | |
| 86 | int |
| 87 | Py_IsInitialized() |
| 88 | { |
| 89 | return initialized; |
| 90 | } |
| 91 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 92 | /* Global initializations. Can be undone by Py_Finalize(). Don't |
| 93 | call this twice without an intervening Py_Finalize() call. When |
| 94 | initializations fail, a fatal error is issued and the function does |
| 95 | not return. On return, the first thread and interpreter state have |
| 96 | been created. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 98 | Locking: you must hold the interpreter lock while calling this. |
| 99 | (If the lock has not yet been initialized, that's equivalent to |
| 100 | having the lock, but you cannot use multiple threads.) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 102 | */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 103 | |
| 104 | void |
| 105 | Py_Initialize() |
| 106 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 107 | PyInterpreterState *interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 108 | PyThreadState *tstate; |
| 109 | PyObject *bimod, *sysmod; |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 110 | char *p; |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 112 | if (initialized) |
Guido van Rossum | aa61505 | 1997-08-20 22:40:18 +0000 | [diff] [blame] | 113 | return; |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 114 | initialized = 1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 116 | if ((p = getenv("PYTHONDEBUG")) && *p != '\0') |
| 117 | Py_DebugFlag = 1; |
| 118 | if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') |
| 119 | Py_VerboseFlag = 1; |
Guido van Rossum | 562f5b1 | 1998-10-07 14:50:42 +0000 | [diff] [blame] | 120 | if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0') |
| 121 | Py_OptimizeFlag = 1; |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 123 | interp = PyInterpreterState_New(); |
| 124 | if (interp == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 125 | Py_FatalError("Py_Initialize: can't make first interpreter"); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 127 | tstate = PyThreadState_New(interp); |
| 128 | if (tstate == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 129 | Py_FatalError("Py_Initialize: can't make first thread"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 130 | (void) PyThreadState_Swap(tstate); |
| 131 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 132 | interp->modules = PyDict_New(); |
| 133 | if (interp->modules == NULL) |
| 134 | Py_FatalError("Py_Initialize: can't make modules dictionary"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 135 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 136 | bimod = _PyBuiltin_Init_1(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 137 | if (bimod == NULL) |
| 138 | Py_FatalError("Py_Initialize: can't initialize __builtin__"); |
Guido van Rossum | 4a1f39a | 1997-11-04 19:36:18 +0000 | [diff] [blame] | 139 | interp->builtins = PyModule_GetDict(bimod); |
| 140 | Py_INCREF(interp->builtins); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 141 | |
| 142 | sysmod = _PySys_Init(); |
| 143 | if (sysmod == NULL) |
| 144 | Py_FatalError("Py_Initialize: can't initialize sys"); |
| 145 | interp->sysdict = PyModule_GetDict(sysmod); |
| 146 | Py_INCREF(interp->sysdict); |
| 147 | _PyImport_FixupExtension("sys", "sys"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 148 | PySys_SetPath(Py_GetPath()); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 149 | PyDict_SetItemString(interp->sysdict, "modules", |
| 150 | interp->modules); |
| 151 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 152 | /* phase 2 of builtins */ |
Guido van Rossum | 4a1f39a | 1997-11-04 19:36:18 +0000 | [diff] [blame] | 153 | _PyBuiltin_Init_2(interp->builtins); |
Barry Warsaw | 963b871 | 1997-09-18 16:42:02 +0000 | [diff] [blame] | 154 | _PyImport_FixupExtension("__builtin__", "__builtin__"); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 156 | _PyImport_Init(); |
| 157 | |
| 158 | initsigs(); /* Signal handling stuff, including initintr() */ |
| 159 | |
| 160 | initmain(); /* Module __main__ */ |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 161 | if (!Py_NoSiteFlag) |
| 162 | initsite(); /* Module site */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Guido van Rossum | 2edcf0d | 1998-12-15 16:12:00 +0000 | [diff] [blame] | 165 | #ifdef COUNT_ALLOCS |
| 166 | extern void dump_counts Py_PROTO((void)); |
| 167 | #endif |
| 168 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 169 | /* Undo the effect of Py_Initialize(). |
| 170 | |
| 171 | Beware: if multiple interpreter and/or thread states exist, these |
| 172 | are not wiped out; only the current thread and interpreter state |
| 173 | are deleted. But since everything else is deleted, those other |
| 174 | interpreter and thread states should no longer be used. |
| 175 | |
| 176 | (XXX We should do better, e.g. wipe out all interpreters and |
| 177 | threads.) |
| 178 | |
| 179 | Locking: as above. |
| 180 | |
| 181 | */ |
| 182 | |
| 183 | void |
| 184 | Py_Finalize() |
| 185 | { |
| 186 | PyInterpreterState *interp; |
| 187 | PyThreadState *tstate; |
| 188 | |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 189 | if (!initialized) |
Guido van Rossum | aa61505 | 1997-08-20 22:40:18 +0000 | [diff] [blame] | 190 | return; |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 191 | initialized = 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 4cc462e | 1998-01-19 22:00:38 +0000 | [diff] [blame] | 193 | call_sys_exitfunc(); |
| 194 | |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 195 | /* Get current thread state and interpreter pointer */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 196 | tstate = PyThreadState_Get(); |
| 197 | interp = tstate->interp; |
| 198 | |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 199 | /* Disable signal handling */ |
| 200 | PyOS_FiniInterrupts(); |
| 201 | |
| 202 | /* Destroy PyExc_MemoryErrorInst */ |
| 203 | _PyBuiltin_Fini_1(); |
| 204 | |
| 205 | /* Destroy all modules */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 206 | PyImport_Cleanup(); |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 208 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 209 | _PyImport_Fini(); |
| 210 | |
| 211 | /* Debugging stuff */ |
| 212 | #ifdef COUNT_ALLOCS |
| 213 | dump_counts(); |
| 214 | #endif |
| 215 | |
| 216 | #ifdef Py_REF_DEBUG |
| 217 | fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); |
| 218 | #endif |
| 219 | |
| 220 | #ifdef Py_TRACE_REFS |
| 221 | if (_Py_AskYesNo("Print left references?")) { |
| 222 | _Py_PrintReferences(stderr); |
| 223 | } |
| 224 | #endif /* Py_TRACE_REFS */ |
| 225 | |
Guido van Rossum | 4a1f39a | 1997-11-04 19:36:18 +0000 | [diff] [blame] | 226 | /* Delete current thread */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 227 | PyInterpreterState_Clear(interp); |
| 228 | PyThreadState_Swap(NULL); |
| 229 | PyInterpreterState_Delete(interp); |
| 230 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 231 | /* Now we decref the exception classes. After this point nothing |
| 232 | can raise an exception. That's okay, because each Fini() method |
| 233 | below has been checked to make sure no exceptions are ever |
| 234 | raised. |
| 235 | */ |
| 236 | _PyBuiltin_Fini_2(); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 237 | PyMethod_Fini(); |
| 238 | PyFrame_Fini(); |
| 239 | PyCFunction_Fini(); |
| 240 | PyTuple_Fini(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 241 | PyString_Fini(); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 242 | PyInt_Fini(); |
| 243 | PyFloat_Fini(); |
| 244 | |
| 245 | /* XXX Still allocated: |
| 246 | - various static ad-hoc pointers to interned strings |
| 247 | - int and float free list blocks |
| 248 | - whatever various modules and libraries allocate |
| 249 | */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 250 | |
| 251 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 252 | |
| 253 | call_ll_exitfuncs(); |
| 254 | |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 255 | #ifdef Py_TRACE_REFS |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 256 | _Py_ResetReferences(); |
| 257 | #endif /* Py_TRACE_REFS */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* Create and initialize a new interpreter and thread, and return the |
| 261 | new thread. This requires that Py_Initialize() has been called |
| 262 | first. |
| 263 | |
| 264 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 265 | exception information is available even in this case -- the |
| 266 | exception information is held in the thread, and there is no |
| 267 | thread. |
| 268 | |
| 269 | Locking: as above. |
| 270 | |
| 271 | */ |
| 272 | |
| 273 | PyThreadState * |
| 274 | Py_NewInterpreter() |
| 275 | { |
| 276 | PyInterpreterState *interp; |
| 277 | PyThreadState *tstate, *save_tstate; |
| 278 | PyObject *bimod, *sysmod; |
| 279 | |
| 280 | if (!initialized) |
| 281 | Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); |
| 282 | |
| 283 | interp = PyInterpreterState_New(); |
| 284 | if (interp == NULL) |
| 285 | return NULL; |
| 286 | |
| 287 | tstate = PyThreadState_New(interp); |
| 288 | if (tstate == NULL) { |
| 289 | PyInterpreterState_Delete(interp); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | save_tstate = PyThreadState_Swap(tstate); |
| 294 | |
| 295 | /* XXX The following is lax in error checking */ |
| 296 | |
| 297 | interp->modules = PyDict_New(); |
| 298 | |
| 299 | bimod = _PyImport_FindExtension("__builtin__", "__builtin__"); |
| 300 | if (bimod != NULL) { |
Guido van Rossum | 4a1f39a | 1997-11-04 19:36:18 +0000 | [diff] [blame] | 301 | interp->builtins = PyModule_GetDict(bimod); |
| 302 | Py_INCREF(interp->builtins); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 303 | } |
| 304 | sysmod = _PyImport_FindExtension("sys", "sys"); |
| 305 | if (bimod != NULL && sysmod != NULL) { |
| 306 | interp->sysdict = PyModule_GetDict(sysmod); |
| 307 | Py_INCREF(interp->sysdict); |
| 308 | PySys_SetPath(Py_GetPath()); |
| 309 | PyDict_SetItemString(interp->sysdict, "modules", |
| 310 | interp->modules); |
| 311 | initmain(); |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 312 | if (!Py_NoSiteFlag) |
| 313 | initsite(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | if (!PyErr_Occurred()) |
| 317 | return tstate; |
| 318 | |
| 319 | /* Oops, it didn't work. Undo it all. */ |
| 320 | |
| 321 | PyErr_Print(); |
| 322 | PyThreadState_Clear(tstate); |
| 323 | PyThreadState_Swap(save_tstate); |
| 324 | PyThreadState_Delete(tstate); |
| 325 | PyInterpreterState_Delete(interp); |
| 326 | |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | /* Delete an interpreter and its last thread. This requires that the |
| 331 | given thread state is current, that the thread has no remaining |
| 332 | frames, and that it is its interpreter's only remaining thread. |
| 333 | It is a fatal error to violate these constraints. |
| 334 | |
| 335 | (Py_Finalize() doesn't have these constraints -- it zaps |
| 336 | everything, regardless.) |
| 337 | |
| 338 | Locking: as above. |
| 339 | |
| 340 | */ |
| 341 | |
| 342 | void |
| 343 | Py_EndInterpreter(tstate) |
| 344 | PyThreadState *tstate; |
| 345 | { |
| 346 | PyInterpreterState *interp = tstate->interp; |
| 347 | |
| 348 | if (tstate != PyThreadState_Get()) |
| 349 | Py_FatalError("Py_EndInterpreter: thread is not current"); |
| 350 | if (tstate->frame != NULL) |
| 351 | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
| 352 | if (tstate != interp->tstate_head || tstate->next != NULL) |
| 353 | Py_FatalError("Py_EndInterpreter: not the last thread"); |
| 354 | |
| 355 | PyImport_Cleanup(); |
| 356 | PyInterpreterState_Clear(interp); |
| 357 | PyThreadState_Swap(NULL); |
| 358 | PyInterpreterState_Delete(interp); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static char *progname = "python"; |
| 362 | |
| 363 | void |
| 364 | Py_SetProgramName(pn) |
| 365 | char *pn; |
| 366 | { |
| 367 | if (pn && *pn) |
| 368 | progname = pn; |
| 369 | } |
| 370 | |
| 371 | char * |
| 372 | Py_GetProgramName() |
| 373 | { |
| 374 | return progname; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 377 | static char *default_home = NULL; |
| 378 | |
| 379 | void |
| 380 | Py_SetPythonHome(home) |
| 381 | char *home; |
| 382 | { |
| 383 | default_home = home; |
| 384 | } |
| 385 | |
| 386 | char * |
| 387 | Py_GetPythonHome() |
| 388 | { |
| 389 | char *home = default_home; |
| 390 | if (home == NULL) |
| 391 | home = getenv("PYTHONHOME"); |
| 392 | return home; |
| 393 | } |
| 394 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 395 | /* Create __main__ module */ |
| 396 | |
| 397 | static void |
| 398 | initmain() |
| 399 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 400 | PyObject *m, *d; |
| 401 | m = PyImport_AddModule("__main__"); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 402 | if (m == NULL) |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 403 | Py_FatalError("can't create __main__ module"); |
| 404 | d = PyModule_GetDict(m); |
| 405 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
Guido van Rossum | 858cb73 | 1997-11-19 16:15:37 +0000 | [diff] [blame] | 406 | PyObject *bimod = PyImport_ImportModule("__builtin__"); |
| 407 | if (bimod == NULL || |
| 408 | PyDict_SetItemString(d, "__builtins__", bimod) != 0) |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 409 | Py_FatalError("can't add __builtins__ to __main__"); |
Barry Warsaw | 3d05b1a | 1999-01-29 21:30:22 +0000 | [diff] [blame] | 410 | Py_DECREF(bimod); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 411 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 414 | /* Import the site module (not into __main__ though) */ |
| 415 | |
| 416 | static void |
| 417 | initsite() |
| 418 | { |
| 419 | PyObject *m, *f; |
| 420 | m = PyImport_ImportModule("site"); |
| 421 | if (m == NULL) { |
| 422 | f = PySys_GetObject("stderr"); |
| 423 | if (Py_VerboseFlag) { |
| 424 | PyFile_WriteString( |
| 425 | "'import site' failed; traceback:\n", f); |
| 426 | PyErr_Print(); |
| 427 | } |
| 428 | else { |
| 429 | PyFile_WriteString( |
| 430 | "'import site' failed; use -v for traceback\n", f); |
| 431 | PyErr_Clear(); |
| 432 | } |
| 433 | } |
| 434 | else { |
| 435 | Py_DECREF(m); |
| 436 | } |
| 437 | } |
| 438 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 439 | /* Parse input from a file and execute it */ |
| 440 | |
| 441 | int |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 442 | PyRun_AnyFile(fp, filename) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 443 | FILE *fp; |
| 444 | char *filename; |
| 445 | { |
| 446 | if (filename == NULL) |
| 447 | filename = "???"; |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 448 | if (Py_FdIsInteractive(fp, filename)) |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 449 | return PyRun_InteractiveLoop(fp, filename); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 450 | else |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 451 | return PyRun_SimpleFile(fp, filename); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | int |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 455 | PyRun_InteractiveLoop(fp, filename) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 456 | FILE *fp; |
| 457 | char *filename; |
| 458 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 459 | PyObject *v; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 460 | int ret; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 461 | v = PySys_GetObject("ps1"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 462 | if (v == NULL) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 463 | PySys_SetObject("ps1", v = PyString_FromString(">>> ")); |
| 464 | Py_XDECREF(v); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 465 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 466 | v = PySys_GetObject("ps2"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 467 | if (v == NULL) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 468 | PySys_SetObject("ps2", v = PyString_FromString("... ")); |
| 469 | Py_XDECREF(v); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 470 | } |
| 471 | for (;;) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 472 | ret = PyRun_InteractiveOne(fp, filename); |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 473 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 474 | fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 475 | #endif |
| 476 | if (ret == E_EOF) |
| 477 | return 0; |
| 478 | /* |
| 479 | if (ret == E_NOMEM) |
| 480 | return -1; |
| 481 | */ |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | int |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 486 | PyRun_InteractiveOne(fp, filename) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 487 | FILE *fp; |
| 488 | char *filename; |
| 489 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 490 | PyObject *m, *d, *v, *w; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 491 | node *n; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 492 | perrdetail err; |
Guido van Rossum | ddc3fb5 | 1997-11-25 20:58:13 +0000 | [diff] [blame] | 493 | char *ps1 = "", *ps2 = ""; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 494 | v = PySys_GetObject("ps1"); |
Guido van Rossum | ddc3fb5 | 1997-11-25 20:58:13 +0000 | [diff] [blame] | 495 | if (v != NULL) { |
| 496 | v = PyObject_Str(v); |
| 497 | if (v == NULL) |
| 498 | PyErr_Clear(); |
| 499 | else if (PyString_Check(v)) |
| 500 | ps1 = PyString_AsString(v); |
| 501 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 502 | w = PySys_GetObject("ps2"); |
Guido van Rossum | ddc3fb5 | 1997-11-25 20:58:13 +0000 | [diff] [blame] | 503 | if (w != NULL) { |
| 504 | w = PyObject_Str(w); |
| 505 | if (w == NULL) |
| 506 | PyErr_Clear(); |
| 507 | else if (PyString_Check(w)) |
| 508 | ps2 = PyString_AsString(w); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 509 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 510 | n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 511 | Py_single_input, ps1, ps2, &err); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 512 | Py_XDECREF(v); |
| 513 | Py_XDECREF(w); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 514 | if (n == NULL) { |
| 515 | if (err.error == E_EOF) { |
| 516 | if (err.text) |
| 517 | free(err.text); |
| 518 | return E_EOF; |
| 519 | } |
| 520 | err_input(&err); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 521 | PyErr_Print(); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 522 | return err.error; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 523 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 524 | m = PyImport_AddModule("__main__"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 525 | if (m == NULL) |
| 526 | return -1; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 527 | d = PyModule_GetDict(m); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 528 | v = run_node(n, filename, d, d); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 529 | if (v == NULL) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 530 | PyErr_Print(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 531 | return -1; |
| 532 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 533 | Py_DECREF(v); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 534 | if (Py_FlushLine()) |
| 535 | PyErr_Clear(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 540 | PyRun_SimpleFile(fp, filename) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 541 | FILE *fp; |
| 542 | char *filename; |
| 543 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 544 | PyObject *m, *d, *v; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 545 | char *ext; |
| 546 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 547 | m = PyImport_AddModule("__main__"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 548 | if (m == NULL) |
| 549 | return -1; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 550 | d = PyModule_GetDict(m); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 551 | ext = filename + strlen(filename) - 4; |
Guido van Rossum | 2a7f58d | 1997-04-02 05:28:38 +0000 | [diff] [blame] | 552 | if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0 |
Jack Jansen | bd06e96 | 1995-02-13 11:44:56 +0000 | [diff] [blame] | 553 | #ifdef macintosh |
| 554 | /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */ |
Guido van Rossum | 2a7f58d | 1997-04-02 05:28:38 +0000 | [diff] [blame] | 555 | || getfiletype(filename) == 'PYC ' |
| 556 | || getfiletype(filename) == 'APPL' |
Jack Jansen | bd06e96 | 1995-02-13 11:44:56 +0000 | [diff] [blame] | 557 | #endif /* macintosh */ |
Guido van Rossum | 2a7f58d | 1997-04-02 05:28:38 +0000 | [diff] [blame] | 558 | ) { |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 559 | /* Try to run a pyc file. First, re-open in binary */ |
Jack Jansen | e00637b | 1994-12-14 12:58:37 +0000 | [diff] [blame] | 560 | /* Don't close, done in main: fclose(fp); */ |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 561 | if( (fp = fopen(filename, "rb")) == NULL ) { |
| 562 | fprintf(stderr, "python: Can't reopen .pyc file\n"); |
| 563 | return -1; |
| 564 | } |
Guido van Rossum | 2a7f58d | 1997-04-02 05:28:38 +0000 | [diff] [blame] | 565 | /* Turn on optimization if a .pyo file is given */ |
| 566 | if (strcmp(ext, ".pyo") == 0) |
| 567 | Py_OptimizeFlag = 1; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 568 | v = run_pyc_file(fp, filename, d, d); |
| 569 | } else { |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 570 | v = PyRun_File(fp, filename, Py_file_input, d, d); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 571 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 572 | if (v == NULL) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 573 | PyErr_Print(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 574 | return -1; |
| 575 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 576 | Py_DECREF(v); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 577 | if (Py_FlushLine()) |
| 578 | PyErr_Clear(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | int |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 583 | PyRun_SimpleString(command) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 584 | char *command; |
| 585 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 586 | PyObject *m, *d, *v; |
| 587 | m = PyImport_AddModule("__main__"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 588 | if (m == NULL) |
| 589 | return -1; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 590 | d = PyModule_GetDict(m); |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 591 | v = PyRun_String(command, Py_file_input, d, d); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 592 | if (v == NULL) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 593 | PyErr_Print(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 594 | return -1; |
| 595 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 596 | Py_DECREF(v); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 597 | if (Py_FlushLine()) |
| 598 | PyErr_Clear(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 602 | static int |
| 603 | parse_syntax_error(err, message, filename, lineno, offset, text) |
| 604 | PyObject* err; |
| 605 | PyObject** message; |
| 606 | char** filename; |
| 607 | int* lineno; |
| 608 | int* offset; |
| 609 | char** text; |
| 610 | { |
| 611 | long hold; |
| 612 | PyObject *v; |
| 613 | |
| 614 | /* old style errors */ |
| 615 | if (PyTuple_Check(err)) |
| 616 | return PyArg_Parse(err, "(O(ziiz))", message, filename, |
| 617 | lineno, offset, text); |
| 618 | |
| 619 | /* new style errors. `err' is an instance */ |
| 620 | |
| 621 | if (! (v = PyObject_GetAttrString(err, "msg"))) |
| 622 | goto finally; |
| 623 | *message = v; |
| 624 | |
| 625 | if (!(v = PyObject_GetAttrString(err, "filename"))) |
| 626 | goto finally; |
| 627 | if (v == Py_None) |
| 628 | *filename = NULL; |
| 629 | else if (! (*filename = PyString_AsString(v))) |
| 630 | goto finally; |
| 631 | |
| 632 | Py_DECREF(v); |
| 633 | if (!(v = PyObject_GetAttrString(err, "lineno"))) |
| 634 | goto finally; |
| 635 | hold = PyInt_AsLong(v); |
| 636 | Py_DECREF(v); |
| 637 | v = NULL; |
| 638 | if (hold < 0 && PyErr_Occurred()) |
| 639 | goto finally; |
| 640 | *lineno = (int)hold; |
| 641 | |
| 642 | if (!(v = PyObject_GetAttrString(err, "offset"))) |
| 643 | goto finally; |
| 644 | hold = PyInt_AsLong(v); |
| 645 | Py_DECREF(v); |
| 646 | v = NULL; |
| 647 | if (hold < 0 && PyErr_Occurred()) |
| 648 | goto finally; |
| 649 | *offset = (int)hold; |
| 650 | |
| 651 | if (!(v = PyObject_GetAttrString(err, "text"))) |
| 652 | goto finally; |
| 653 | if (v == Py_None) |
| 654 | *text = NULL; |
| 655 | else if (! (*text = PyString_AsString(v))) |
| 656 | goto finally; |
| 657 | Py_DECREF(v); |
| 658 | return 1; |
| 659 | |
| 660 | finally: |
| 661 | Py_XDECREF(v); |
| 662 | return 0; |
| 663 | } |
| 664 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 665 | void |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 666 | PyErr_Print() |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 667 | { |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 668 | PyErr_PrintEx(1); |
| 669 | } |
| 670 | |
| 671 | void |
| 672 | PyErr_PrintEx(set_sys_last_vars) |
| 673 | int set_sys_last_vars; |
| 674 | { |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 675 | int err = 0; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 676 | PyObject *exception, *v, *tb, *f; |
| 677 | PyErr_Fetch(&exception, &v, &tb); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 678 | PyErr_NormalizeException(&exception, &v, &tb); |
| 679 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 680 | if (exception == NULL) |
Guido van Rossum | 296b475 | 1997-05-23 00:19:20 +0000 | [diff] [blame] | 681 | return; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 682 | |
Barry Warsaw | 36b8f94 | 1997-08-26 18:09:48 +0000 | [diff] [blame] | 683 | if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) { |
Guido van Rossum | 0829c75 | 1998-02-28 04:31:39 +0000 | [diff] [blame] | 684 | if (Py_FlushLine()) |
| 685 | PyErr_Clear(); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 686 | fflush(stdout); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 687 | if (v == NULL || v == Py_None) |
| 688 | Py_Exit(0); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 689 | if (PyInstance_Check(v)) { |
| 690 | /* we expect the error code to be store in the |
| 691 | `code' attribute |
| 692 | */ |
| 693 | PyObject *code = PyObject_GetAttrString(v, "code"); |
| 694 | if (code) { |
| 695 | Py_DECREF(v); |
| 696 | v = code; |
Guido van Rossum | aa9606f | 1997-10-03 13:53:28 +0000 | [diff] [blame] | 697 | if (v == Py_None) |
| 698 | Py_Exit(0); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 699 | } |
| 700 | /* if we failed to dig out the "code" attribute, |
| 701 | then just let the else clause below print the |
| 702 | error |
| 703 | */ |
| 704 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 705 | if (PyInt_Check(v)) |
| 706 | Py_Exit((int)PyInt_AsLong(v)); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 707 | else { |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 708 | /* OK to use real stderr here */ |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 709 | PyObject_Print(v, stderr, Py_PRINT_RAW); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 710 | fprintf(stderr, "\n"); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 711 | Py_Exit(1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 714 | if (set_sys_last_vars) { |
| 715 | PySys_SetObject("last_type", exception); |
| 716 | PySys_SetObject("last_value", v); |
| 717 | PySys_SetObject("last_traceback", tb); |
| 718 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 719 | f = PySys_GetObject("stderr"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 720 | if (f == NULL) |
| 721 | fprintf(stderr, "lost sys.stderr\n"); |
| 722 | else { |
Guido van Rossum | 0829c75 | 1998-02-28 04:31:39 +0000 | [diff] [blame] | 723 | if (Py_FlushLine()) |
| 724 | PyErr_Clear(); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 725 | fflush(stdout); |
Guido van Rossum | 0829c75 | 1998-02-28 04:31:39 +0000 | [diff] [blame] | 726 | err = PyTraceBack_Print(tb, f); |
Barry Warsaw | 36b8f94 | 1997-08-26 18:09:48 +0000 | [diff] [blame] | 727 | if (err == 0 && |
| 728 | PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) |
| 729 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 730 | PyObject *message; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 731 | char *filename, *text; |
| 732 | int lineno, offset; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 733 | if (!parse_syntax_error(v, &message, &filename, |
| 734 | &lineno, &offset, &text)) |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 735 | PyErr_Clear(); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 736 | else { |
| 737 | char buf[10]; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 738 | PyFile_WriteString(" File \"", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 739 | if (filename == NULL) |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 740 | PyFile_WriteString("<string>", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 741 | else |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 742 | PyFile_WriteString(filename, f); |
| 743 | PyFile_WriteString("\", line ", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 744 | sprintf(buf, "%d", lineno); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 745 | PyFile_WriteString(buf, f); |
| 746 | PyFile_WriteString("\n", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 747 | if (text != NULL) { |
Guido van Rossum | 798199d | 1994-09-19 08:08:50 +0000 | [diff] [blame] | 748 | char *nl; |
| 749 | if (offset > 0 && |
Guido van Rossum | 478e718 | 1997-05-06 15:24:59 +0000 | [diff] [blame] | 750 | offset == (int)strlen(text)) |
Guido van Rossum | 798199d | 1994-09-19 08:08:50 +0000 | [diff] [blame] | 751 | offset--; |
| 752 | for (;;) { |
| 753 | nl = strchr(text, '\n'); |
| 754 | if (nl == NULL || |
| 755 | nl-text >= offset) |
| 756 | break; |
| 757 | offset -= (nl+1-text); |
| 758 | text = nl+1; |
| 759 | } |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 760 | while (*text == ' ' || *text == '\t') { |
| 761 | text++; |
| 762 | offset--; |
| 763 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 764 | PyFile_WriteString(" ", f); |
| 765 | PyFile_WriteString(text, f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 766 | if (*text == '\0' || |
| 767 | text[strlen(text)-1] != '\n') |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 768 | PyFile_WriteString("\n", f); |
| 769 | PyFile_WriteString(" ", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 770 | offset--; |
| 771 | while (offset > 0) { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 772 | PyFile_WriteString(" ", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 773 | offset--; |
| 774 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 775 | PyFile_WriteString("^\n", f); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 776 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 777 | Py_INCREF(message); |
| 778 | Py_DECREF(v); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 779 | v = message; |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 780 | /* Can't be bothered to check all those |
| 781 | PyFile_WriteString() calls */ |
| 782 | if (PyErr_Occurred()) |
| 783 | err = -1; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 786 | if (err) { |
| 787 | /* Don't do anything else */ |
| 788 | } |
| 789 | else if (PyClass_Check(exception)) { |
Barry Warsaw | 2f5f6a2 | 1997-09-16 21:42:03 +0000 | [diff] [blame] | 790 | PyClassObject* exc = (PyClassObject*)exception; |
| 791 | PyObject* className = exc->cl_name; |
| 792 | PyObject* moduleName = |
| 793 | PyDict_GetItemString(exc->cl_dict, "__module__"); |
| 794 | |
| 795 | if (moduleName == NULL) |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 796 | err = PyFile_WriteString("<unknown>", f); |
Barry Warsaw | 2f5f6a2 | 1997-09-16 21:42:03 +0000 | [diff] [blame] | 797 | else { |
| 798 | char* modstr = PyString_AsString(moduleName); |
| 799 | if (modstr && strcmp(modstr, "exceptions")) |
| 800 | { |
| 801 | err = PyFile_WriteString(modstr, f); |
| 802 | err += PyFile_WriteString(".", f); |
| 803 | } |
| 804 | } |
| 805 | if (err == 0) { |
| 806 | if (className == NULL) |
| 807 | err = PyFile_WriteString("<unknown>", f); |
| 808 | else |
| 809 | err = PyFile_WriteObject(className, f, |
| 810 | Py_PRINT_RAW); |
| 811 | } |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 812 | } |
| 813 | else |
| 814 | err = PyFile_WriteObject(exception, f, Py_PRINT_RAW); |
| 815 | if (err == 0) { |
| 816 | if (v != NULL && v != Py_None) { |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 817 | PyObject *s = PyObject_Str(v); |
| 818 | /* only print colon if the str() of the |
| 819 | object is not the empty string |
| 820 | */ |
Guido van Rossum | d6bf45b | 1997-09-05 19:11:53 +0000 | [diff] [blame] | 821 | if (s == NULL) |
| 822 | err = -1; |
| 823 | else if (!PyString_Check(s) || |
| 824 | PyString_GET_SIZE(s) != 0) |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 825 | err = PyFile_WriteString(": ", f); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 826 | if (err == 0) |
Guido van Rossum | d6bf45b | 1997-09-05 19:11:53 +0000 | [diff] [blame] | 827 | err = PyFile_WriteObject(s, f, Py_PRINT_RAW); |
| 828 | Py_XDECREF(s); |
Guido van Rossum | 262e124 | 1995-02-07 15:30:45 +0000 | [diff] [blame] | 829 | } |
Guido van Rossum | 262e124 | 1995-02-07 15:30:45 +0000 | [diff] [blame] | 830 | } |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 831 | if (err == 0) |
| 832 | err = PyFile_WriteString("\n", f); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 833 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 834 | Py_XDECREF(exception); |
| 835 | Py_XDECREF(v); |
| 836 | Py_XDECREF(tb); |
Guido van Rossum | 78a1ed3 | 1997-05-22 22:35:04 +0000 | [diff] [blame] | 837 | /* If an error happened here, don't show it. |
| 838 | XXX This is wrong, but too many callers rely on this behavior. */ |
| 839 | if (err != 0) |
| 840 | PyErr_Clear(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 843 | PyObject * |
| 844 | PyRun_String(str, start, globals, locals) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 845 | char *str; |
| 846 | int start; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 847 | PyObject *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 848 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 849 | return run_err_node(PyParser_SimpleParseString(str, start), |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 850 | "<string>", globals, locals); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 853 | PyObject * |
| 854 | PyRun_File(fp, filename, start, globals, locals) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 855 | FILE *fp; |
| 856 | char *filename; |
| 857 | int start; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 858 | PyObject *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 859 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 860 | return run_err_node(PyParser_SimpleParseFile(fp, filename, start), |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 861 | filename, globals, locals); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 864 | static PyObject * |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 865 | run_err_node(n, filename, globals, locals) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 866 | node *n; |
| 867 | char *filename; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 868 | PyObject *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 869 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 870 | if (n == NULL) |
| 871 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 872 | return run_node(n, filename, globals, locals); |
| 873 | } |
| 874 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 875 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 876 | run_node(n, filename, globals, locals) |
| 877 | node *n; |
| 878 | char *filename; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 879 | PyObject *globals, *locals; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 880 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 881 | PyCodeObject *co; |
| 882 | PyObject *v; |
| 883 | co = PyNode_Compile(n, filename); |
| 884 | PyNode_Free(n); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 885 | if (co == NULL) |
| 886 | return NULL; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 887 | v = PyEval_EvalCode(co, globals, locals); |
| 888 | Py_DECREF(co); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 889 | return v; |
| 890 | } |
| 891 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 892 | static PyObject * |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 893 | run_pyc_file(fp, filename, globals, locals) |
| 894 | FILE *fp; |
| 895 | char *filename; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 896 | PyObject *globals, *locals; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 897 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 898 | PyCodeObject *co; |
| 899 | PyObject *v; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 900 | long magic; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 901 | long PyImport_GetMagicNumber(); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 903 | magic = PyMarshal_ReadLongFromFile(fp); |
| 904 | if (magic != PyImport_GetMagicNumber()) { |
| 905 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 906 | "Bad magic number in .pyc file"); |
| 907 | return NULL; |
| 908 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 909 | (void) PyMarshal_ReadLongFromFile(fp); |
| 910 | v = PyMarshal_ReadObjectFromFile(fp); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 911 | fclose(fp); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 912 | if (v == NULL || !PyCode_Check(v)) { |
| 913 | Py_XDECREF(v); |
| 914 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 915 | "Bad code object in .pyc file"); |
| 916 | return NULL; |
| 917 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 918 | co = (PyCodeObject *)v; |
| 919 | v = PyEval_EvalCode(co, globals, locals); |
| 920 | Py_DECREF(co); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 921 | return v; |
| 922 | } |
| 923 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 924 | PyObject * |
| 925 | Py_CompileString(str, filename, start) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 926 | char *str; |
| 927 | char *filename; |
| 928 | int start; |
| 929 | { |
| 930 | node *n; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 931 | PyCodeObject *co; |
| 932 | n = PyParser_SimpleParseString(str, start); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 933 | if (n == NULL) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 934 | return NULL; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 935 | co = PyNode_Compile(n, filename); |
| 936 | PyNode_Free(n); |
| 937 | return (PyObject *)co; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 940 | /* Simplified interface to parsefile -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 941 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 942 | node * |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 943 | PyParser_SimpleParseFile(fp, filename, start) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 944 | FILE *fp; |
| 945 | char *filename; |
| 946 | int start; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 947 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 948 | node *n; |
| 949 | perrdetail err; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 950 | n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start, |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 951 | (char *)0, (char *)0, &err); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 952 | if (n == NULL) |
| 953 | err_input(&err); |
| 954 | return n; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 957 | /* Simplified interface to parsestring -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 959 | node * |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 960 | PyParser_SimpleParseString(str, start) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 961 | char *str; |
| 962 | int start; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 963 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 964 | node *n; |
| 965 | perrdetail err; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 966 | n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 967 | if (n == NULL) |
| 968 | err_input(&err); |
| 969 | return n; |
| 970 | } |
| 971 | |
| 972 | /* Set the error appropriate to the given input error code (see errcode.h) */ |
| 973 | |
| 974 | static void |
| 975 | err_input(err) |
| 976 | perrdetail *err; |
| 977 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 978 | PyObject *v, *w; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 979 | char *msg = NULL; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 980 | v = Py_BuildValue("(ziiz)", err->filename, |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 981 | err->lineno, err->offset, err->text); |
| 982 | if (err->text != NULL) { |
| 983 | free(err->text); |
| 984 | err->text = NULL; |
| 985 | } |
| 986 | switch (err->error) { |
| 987 | case E_SYNTAX: |
| 988 | msg = "invalid syntax"; |
| 989 | break; |
| 990 | case E_TOKEN: |
| 991 | msg = "invalid token"; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 992 | break; |
| 993 | case E_INTR: |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 994 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Barry Warsaw | c80baa3 | 1999-01-27 16:39:40 +0000 | [diff] [blame] | 995 | Py_XDECREF(v); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 996 | return; |
| 997 | case E_NOMEM: |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 998 | PyErr_NoMemory(); |
Barry Warsaw | c80baa3 | 1999-01-27 16:39:40 +0000 | [diff] [blame] | 999 | Py_XDECREF(v); |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1000 | return; |
| 1001 | case E_EOF: |
| 1002 | msg = "unexpected EOF while parsing"; |
| 1003 | break; |
Guido van Rossum | 560e8ad | 1998-04-10 19:43:42 +0000 | [diff] [blame] | 1004 | case E_INDENT: |
| 1005 | msg = "inconsistent use of tabs and spaces in indentation"; |
| 1006 | break; |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1007 | default: |
| 1008 | fprintf(stderr, "error=%d\n", err->error); |
| 1009 | msg = "unknown parsing error"; |
| 1010 | break; |
| 1011 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1012 | w = Py_BuildValue("(sO)", msg, v); |
| 1013 | Py_XDECREF(v); |
| 1014 | PyErr_SetObject(PyExc_SyntaxError, w); |
| 1015 | Py_XDECREF(w); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* Print fatal error message and abort */ |
| 1019 | |
| 1020 | void |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1021 | Py_FatalError(msg) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1022 | char *msg; |
| 1023 | { |
Guido van Rossum | 83dd6c3 | 1994-09-29 09:38:33 +0000 | [diff] [blame] | 1024 | fprintf(stderr, "Fatal Python error: %s\n", msg); |
Guido van Rossum | 8ae87c0 | 1995-01-26 00:40:38 +0000 | [diff] [blame] | 1025 | #ifdef macintosh |
| 1026 | for (;;); |
| 1027 | #endif |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 1028 | #ifdef MS_WIN32 |
Guido van Rossum | 23c9446 | 1997-05-22 20:21:30 +0000 | [diff] [blame] | 1029 | OutputDebugString("Fatal Python error: "); |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 1030 | OutputDebugString(msg); |
| 1031 | OutputDebugString("\n"); |
Guido van Rossum | 0ba3536 | 1998-08-13 13:33:16 +0000 | [diff] [blame] | 1032 | #ifdef _DEBUG |
| 1033 | DebugBreak(); |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 1034 | #endif |
Guido van Rossum | 0ba3536 | 1998-08-13 13:33:16 +0000 | [diff] [blame] | 1035 | #endif /* MS_WIN32 */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1036 | abort(); |
| 1037 | } |
| 1038 | |
| 1039 | /* Clean up and exit */ |
| 1040 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1041 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 1042 | #include "pythread.h" |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1043 | int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */ |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 1044 | #endif |
| 1045 | |
Guido van Rossum | 2dcfc96 | 1998-10-01 16:01:57 +0000 | [diff] [blame] | 1046 | #define NEXITFUNCS 32 |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1047 | static void (*exitfuncs[NEXITFUNCS])(); |
| 1048 | static int nexitfuncs = 0; |
| 1049 | |
| 1050 | int Py_AtExit(func) |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1051 | void (*func) Py_PROTO((void)); |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1052 | { |
| 1053 | if (nexitfuncs >= NEXITFUNCS) |
| 1054 | return -1; |
| 1055 | exitfuncs[nexitfuncs++] = func; |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1059 | static void |
| 1060 | call_sys_exitfunc() |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1061 | { |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1062 | PyObject *exitfunc = PySys_GetObject("exitfunc"); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (exitfunc) { |
Guido van Rossum | bf02fb2 | 1998-04-03 21:12:12 +0000 | [diff] [blame] | 1065 | PyObject *res, *f; |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1066 | Py_INCREF(exitfunc); |
| 1067 | PySys_SetObject("exitfunc", (PyObject *)NULL); |
Guido van Rossum | bf02fb2 | 1998-04-03 21:12:12 +0000 | [diff] [blame] | 1068 | f = PySys_GetObject("stderr"); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1069 | res = PyEval_CallObject(exitfunc, (PyObject *)NULL); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1070 | if (res == NULL) { |
Guido van Rossum | bf02fb2 | 1998-04-03 21:12:12 +0000 | [diff] [blame] | 1071 | if (f) |
| 1072 | PyFile_WriteString("Error in sys.exitfunc:\n", f); |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1073 | PyErr_Print(); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1074 | } |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1075 | Py_DECREF(exitfunc); |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Guido van Rossum | 0829c75 | 1998-02-28 04:31:39 +0000 | [diff] [blame] | 1078 | if (Py_FlushLine()) |
| 1079 | PyErr_Clear(); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1080 | } |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1081 | |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1082 | static void |
| 1083 | call_ll_exitfuncs() |
| 1084 | { |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 1085 | while (nexitfuncs > 0) |
| 1086 | (*exitfuncs[--nexitfuncs])(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1087 | |
| 1088 | fflush(stdout); |
| 1089 | fflush(stderr); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | void |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1093 | Py_Exit(sts) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1094 | int sts; |
| 1095 | { |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 1096 | Py_Finalize(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1097 | |
Jack Jansen | 66a8977 | 1995-10-27 13:22:14 +0000 | [diff] [blame] | 1098 | #ifdef macintosh |
| 1099 | PyMac_Exit(sts); |
| 1100 | #else |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1101 | exit(sts); |
Jack Jansen | 66a8977 | 1995-10-27 13:22:14 +0000 | [diff] [blame] | 1102 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 1105 | static void |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1106 | initsigs() |
| 1107 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1108 | #ifdef HAVE_SIGNAL_H |
| 1109 | #ifdef SIGPIPE |
| 1110 | signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1111 | #endif |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1112 | #endif /* HAVE_SIGNAL_H */ |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1113 | PyOS_InitInterrupts(); /* May imply initsignal() */ |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Guido van Rossum | aae0d32 | 1996-05-22 16:35:33 +0000 | [diff] [blame] | 1116 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1117 | /* Ask a yes/no question */ |
| 1118 | |
Guido van Rossum | 59bff39 | 1992-09-03 20:28:00 +0000 | [diff] [blame] | 1119 | int |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1120 | _Py_AskYesNo(prompt) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1121 | char *prompt; |
| 1122 | { |
| 1123 | char buf[256]; |
| 1124 | |
| 1125 | printf("%s [ny] ", prompt); |
| 1126 | if (fgets(buf, sizeof buf, stdin) == NULL) |
| 1127 | return 0; |
| 1128 | return buf[0] == 'y' || buf[0] == 'Y'; |
| 1129 | } |
| 1130 | #endif |
| 1131 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1132 | #ifdef MPW |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1133 | |
| 1134 | /* Check for file descriptor connected to interactive device. |
| 1135 | Pretend that stdin is always interactive, other files never. */ |
| 1136 | |
| 1137 | int |
| 1138 | isatty(fd) |
| 1139 | int fd; |
| 1140 | { |
| 1141 | return fd == fileno(stdin); |
| 1142 | } |
| 1143 | |
| 1144 | #endif |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 1145 | |
| 1146 | /* |
| 1147 | * The file descriptor fd is considered ``interactive'' if either |
| 1148 | * a) isatty(fd) is TRUE, or |
| 1149 | * b) the -i flag was given, and the filename associated with |
| 1150 | * the descriptor is NULL or "<stdin>" or "???". |
| 1151 | */ |
| 1152 | int |
| 1153 | Py_FdIsInteractive(fp, filename) |
| 1154 | FILE *fp; |
| 1155 | char *filename; |
| 1156 | { |
| 1157 | if (isatty((int)fileno(fp))) |
| 1158 | return 1; |
| 1159 | if (!Py_InteractiveFlag) |
| 1160 | return 0; |
| 1161 | return (filename == NULL) || |
| 1162 | (strcmp(filename, "<stdin>") == 0) || |
| 1163 | (strcmp(filename, "???") == 0); |
| 1164 | } |