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