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