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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "Python-ast.h" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 7 | #undef Yield /* undefine macro conflicting with winbase.h */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 8 | #include "grammar.h" |
| 9 | #include "node.h" |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 10 | #include "token.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 11 | #include "parsetok.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 12 | #include "errcode.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 14 | #include "symtable.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 | #include "ast.h" |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 16 | #include "marshal.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 17 | #include "osdefs.h" |
Victor Stinner | 518e610 | 2014-03-18 02:06:38 +0100 | [diff] [blame] | 18 | #include <locale.h> |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 21 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | #endif |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 23 | |
Benjamin Peterson | 80a50ac | 2009-01-02 21:24:04 +0000 | [diff] [blame] | 24 | #ifdef MS_WINDOWS |
Martin v. Löwis | 5c88d81 | 2009-01-02 20:47:48 +0000 | [diff] [blame] | 25 | #include "malloc.h" /* for alloca */ |
Benjamin Peterson | 80a50ac | 2009-01-02 21:24:04 +0000 | [diff] [blame] | 26 | #endif |
Martin v. Löwis | 5c88d81 | 2009-01-02 20:47:48 +0000 | [diff] [blame] | 27 | |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 28 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 29 | #include <langinfo.h> |
| 30 | #endif |
| 31 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 32 | #ifdef MS_WINDOWS |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 33 | #undef BYTE |
| 34 | #include "windows.h" |
| 35 | #endif |
| 36 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 37 | _Py_IDENTIFIER(builtins); |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 38 | _Py_IDENTIFIER(excepthook); |
Victor Stinner | 3f36a57 | 2013-11-12 21:39:02 +0100 | [diff] [blame] | 39 | _Py_IDENTIFIER(flush); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 40 | _Py_IDENTIFIER(last_traceback); |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 41 | _Py_IDENTIFIER(last_type); |
| 42 | _Py_IDENTIFIER(last_value); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 43 | _Py_IDENTIFIER(ps1); |
| 44 | _Py_IDENTIFIER(ps2); |
| 45 | _Py_IDENTIFIER(stdin); |
| 46 | _Py_IDENTIFIER(stdout); |
| 47 | _Py_IDENTIFIER(stderr); |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 48 | _Py_static_string(PyId_string, "<string>"); |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 49 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
Neal Norwitz | 4281cef | 2006-03-04 19:58:13 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 54 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 56 | /* Forward */ |
Amaury Forgeot d'Arc | 7fedbe5 | 2008-04-10 21:03:09 +0000 | [diff] [blame] | 57 | static void flush_io(void); |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 58 | static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | PyCompilerFlags *, PyArena *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 60 | static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | PyCompilerFlags *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 62 | static void err_input(perrdetail *); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 63 | static void err_free(perrdetail *); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 65 | /* Parse input from a file and execute it */ |
| 66 | |
| 67 | int |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 68 | PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 70 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | if (filename == NULL) |
| 72 | filename = "???"; |
| 73 | if (Py_FdIsInteractive(fp, filename)) { |
| 74 | int err = PyRun_InteractiveLoopFlags(fp, filename, flags); |
| 75 | if (closeit) |
| 76 | fclose(fp); |
| 77 | return err; |
| 78 | } |
| 79 | else |
| 80 | return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 84 | PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 85 | { |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 86 | PyObject *filename, *v; |
| 87 | int ret, err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | PyCompilerFlags local_flags; |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 89 | |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 90 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 91 | if (filename == NULL) { |
| 92 | PyErr_Print(); |
| 93 | return -1; |
| 94 | } |
| 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (flags == NULL) { |
| 97 | flags = &local_flags; |
| 98 | local_flags.cf_flags = 0; |
| 99 | } |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 100 | v = _PySys_GetObjectId(&PyId_ps1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | if (v == NULL) { |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 102 | _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> ")); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | Py_XDECREF(v); |
| 104 | } |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 105 | v = _PySys_GetObjectId(&PyId_ps2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | if (v == NULL) { |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 107 | _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... ")); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | Py_XDECREF(v); |
| 109 | } |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 110 | err = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | for (;;) { |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 112 | ret = PyRun_InteractiveOneObject(fp, filename, flags); |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 113 | _PY_DEBUG_PRINT_TOTAL_REFS(); |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 114 | if (ret == E_EOF) { |
| 115 | err = 0; |
| 116 | break; |
| 117 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | /* |
| 119 | if (ret == E_NOMEM) |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 120 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | */ |
| 122 | } |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 123 | Py_DECREF(filename); |
| 124 | return err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 127 | /* compute parser flags based on compiler flags */ |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 128 | static int PARSER_FLAGS(PyCompilerFlags *flags) |
| 129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | int parser_flags = 0; |
| 131 | if (!flags) |
| 132 | return 0; |
| 133 | if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT) |
| 134 | parser_flags |= PyPARSE_DONT_IMPLY_DEDENT; |
| 135 | if (flags->cf_flags & PyCF_IGNORE_COOKIE) |
| 136 | parser_flags |= PyPARSE_IGNORE_COOKIE; |
| 137 | if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL) |
| 138 | parser_flags |= PyPARSE_BARRY_AS_BDFL; |
| 139 | return parser_flags; |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 140 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 141 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | #if 0 |
| 143 | /* Keep an example of flags with future keyword support. */ |
| 144 | #define PARSER_FLAGS(flags) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ |
| 146 | PyPARSE_DONT_IMPLY_DEDENT : 0) \ |
| 147 | | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ |
| 148 | PyPARSE_WITH_IS_KEYWORD : 0)) : 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 151 | int |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 152 | PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 153 | { |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 154 | PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | mod_ty mod; |
| 156 | PyArena *arena; |
| 157 | char *ps1 = "", *ps2 = "", *enc = NULL; |
| 158 | int errcode = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 159 | _Py_IDENTIFIER(encoding); |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 160 | _Py_IDENTIFIER(__main__); |
| 161 | |
| 162 | mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */ |
| 163 | if (mod_name == NULL) { |
| 164 | PyErr_Print(); |
| 165 | return -1; |
| 166 | } |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | if (fp == stdin) { |
Benjamin Peterson | fe1b22a | 2013-04-29 10:23:08 -0400 | [diff] [blame] | 169 | /* Fetch encoding from sys.stdin if possible. */ |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 170 | v = _PySys_GetObjectId(&PyId_stdin); |
Benjamin Peterson | fe1b22a | 2013-04-29 10:23:08 -0400 | [diff] [blame] | 171 | if (v && v != Py_None) { |
| 172 | oenc = _PyObject_GetAttrId(v, &PyId_encoding); |
| 173 | if (oenc) |
| 174 | enc = _PyUnicode_AsString(oenc); |
| 175 | if (!enc) |
| 176 | PyErr_Clear(); |
| 177 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | } |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 179 | v = _PySys_GetObjectId(&PyId_ps1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | if (v != NULL) { |
| 181 | v = PyObject_Str(v); |
| 182 | if (v == NULL) |
| 183 | PyErr_Clear(); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 184 | else if (PyUnicode_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | ps1 = _PyUnicode_AsString(v); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 186 | if (ps1 == NULL) { |
| 187 | PyErr_Clear(); |
| 188 | ps1 = ""; |
| 189 | } |
| 190 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | } |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 192 | w = _PySys_GetObjectId(&PyId_ps2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | if (w != NULL) { |
| 194 | w = PyObject_Str(w); |
| 195 | if (w == NULL) |
| 196 | PyErr_Clear(); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 197 | else if (PyUnicode_Check(w)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | ps2 = _PyUnicode_AsString(w); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 199 | if (ps2 == NULL) { |
| 200 | PyErr_Clear(); |
| 201 | ps2 = ""; |
| 202 | } |
| 203 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | } |
| 205 | arena = PyArena_New(); |
| 206 | if (arena == NULL) { |
| 207 | Py_XDECREF(v); |
| 208 | Py_XDECREF(w); |
| 209 | Py_XDECREF(oenc); |
| 210 | return -1; |
| 211 | } |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 212 | mod = PyParser_ASTFromFileObject(fp, filename, enc, |
| 213 | Py_single_input, ps1, ps2, |
| 214 | flags, &errcode, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | Py_XDECREF(v); |
| 216 | Py_XDECREF(w); |
| 217 | Py_XDECREF(oenc); |
| 218 | if (mod == NULL) { |
| 219 | PyArena_Free(arena); |
| 220 | if (errcode == E_EOF) { |
| 221 | PyErr_Clear(); |
| 222 | return E_EOF; |
| 223 | } |
| 224 | PyErr_Print(); |
| 225 | return -1; |
| 226 | } |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 227 | m = PyImport_AddModuleObject(mod_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | if (m == NULL) { |
| 229 | PyArena_Free(arena); |
| 230 | return -1; |
| 231 | } |
| 232 | d = PyModule_GetDict(m); |
| 233 | v = run_mod(mod, filename, d, d, flags, arena); |
| 234 | PyArena_Free(arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | if (v == NULL) { |
| 236 | PyErr_Print(); |
Antoine Pitrou | 9845c7e | 2014-05-11 13:42:17 +0200 | [diff] [blame] | 237 | flush_io(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | return -1; |
| 239 | } |
| 240 | Py_DECREF(v); |
Antoine Pitrou | 9845c7e | 2014-05-11 13:42:17 +0200 | [diff] [blame] | 241 | flush_io(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | return 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 245 | int |
| 246 | PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags) |
| 247 | { |
| 248 | PyObject *filename; |
| 249 | int res; |
| 250 | |
| 251 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 252 | if (filename == NULL) { |
| 253 | PyErr_Print(); |
| 254 | return -1; |
| 255 | } |
| 256 | res = PyRun_InteractiveOneObject(fp, filename, flags); |
| 257 | Py_DECREF(filename); |
| 258 | return res; |
| 259 | } |
| 260 | |
| 261 | |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 262 | /* Check whether a file maybe a pyc file: Look at the extension, |
| 263 | the file type, and, if we may close it, at the first few bytes. */ |
| 264 | |
| 265 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 266 | 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] | 267 | { |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame^] | 268 | if (strcmp(ext, ".pyc") == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | return 1; |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | /* Only look into the file if we are allowed to close it, since |
| 272 | it then should also be seekable. */ |
| 273 | if (closeit) { |
| 274 | /* Read only two bytes of the magic. If the file was opened in |
| 275 | text mode, the bytes 3 and 4 of the magic (\r\n) might not |
| 276 | be read as they are on disk. */ |
| 277 | unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF; |
| 278 | unsigned char buf[2]; |
| 279 | /* Mess: In case of -x, the stream is NOT at its start now, |
| 280 | and ungetc() was used to push back the first newline, |
| 281 | which makes the current stream position formally undefined, |
| 282 | and a x-platform nightmare. |
| 283 | Unfortunately, we have no direct way to know whether -x |
| 284 | was specified. So we use a terrible hack: if the current |
| 285 | stream position is not 0, we assume -x was specified, and |
| 286 | give up. Bug 132850 on SourceForge spells out the |
| 287 | hopelessness of trying anything else (fseek and ftell |
| 288 | don't work predictably x-platform for text-mode files). |
| 289 | */ |
| 290 | int ispyc = 0; |
| 291 | if (ftell(fp) == 0) { |
| 292 | if (fread(buf, 1, 2, fp) == 2 && |
| 293 | ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) |
| 294 | ispyc = 1; |
| 295 | rewind(fp); |
| 296 | } |
| 297 | return ispyc; |
| 298 | } |
| 299 | return 0; |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 300 | } |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | 32d483c | 2013-07-30 21:01:23 +0200 | [diff] [blame] | 302 | static int |
| 303 | set_main_loader(PyObject *d, const char *filename, const char *loader_name) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 304 | { |
| 305 | PyInterpreterState *interp; |
| 306 | PyThreadState *tstate; |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 307 | PyObject *filename_obj, *loader_type, *loader; |
Nick Coghlan | b7a5894 | 2012-07-15 23:21:08 +1000 | [diff] [blame] | 308 | int result = 0; |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 309 | |
| 310 | filename_obj = PyUnicode_DecodeFSDefault(filename); |
| 311 | if (filename_obj == NULL) |
| 312 | return -1; |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 313 | /* Get current thread state and interpreter pointer */ |
| 314 | tstate = PyThreadState_GET(); |
| 315 | interp = tstate->interp; |
Nick Coghlan | 3f94cbf | 2012-07-15 19:10:39 +1000 | [diff] [blame] | 316 | loader_type = PyObject_GetAttrString(interp->importlib, loader_name); |
| 317 | if (loader_type == NULL) { |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 318 | Py_DECREF(filename_obj); |
Nick Coghlan | 3f94cbf | 2012-07-15 19:10:39 +1000 | [diff] [blame] | 319 | return -1; |
| 320 | } |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 321 | loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj); |
Nick Coghlan | b7a5894 | 2012-07-15 23:21:08 +1000 | [diff] [blame] | 322 | Py_DECREF(loader_type); |
| 323 | if (loader == NULL) { |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 324 | return -1; |
| 325 | } |
Nick Coghlan | b7a5894 | 2012-07-15 23:21:08 +1000 | [diff] [blame] | 326 | if (PyDict_SetItemString(d, "__loader__", loader) < 0) { |
| 327 | result = -1; |
| 328 | } |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 329 | Py_DECREF(loader); |
Nick Coghlan | b7a5894 | 2012-07-15 23:21:08 +1000 | [diff] [blame] | 330 | return result; |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 334 | PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | PyObject *m, *d, *v; |
| 338 | const char *ext; |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 339 | int set_file_name = 0, ret = -1; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 340 | size_t len; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | m = PyImport_AddModule("__main__"); |
| 343 | if (m == NULL) |
| 344 | return -1; |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 345 | Py_INCREF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | d = PyModule_GetDict(m); |
| 347 | if (PyDict_GetItemString(d, "__file__") == NULL) { |
| 348 | PyObject *f; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 349 | f = PyUnicode_DecodeFSDefault(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | if (f == NULL) |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 351 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (PyDict_SetItemString(d, "__file__", f) < 0) { |
| 353 | Py_DECREF(f); |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 354 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | } |
Barry Warsaw | 916048d | 2011-09-20 14:45:44 -0400 | [diff] [blame] | 356 | if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) { |
| 357 | Py_DECREF(f); |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 358 | goto done; |
Barry Warsaw | 916048d | 2011-09-20 14:45:44 -0400 | [diff] [blame] | 359 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | set_file_name = 1; |
| 361 | Py_DECREF(f); |
| 362 | } |
| 363 | len = strlen(filename); |
| 364 | ext = filename + len - (len > 4 ? 4 : 0); |
| 365 | if (maybe_pyc_file(fp, filename, ext, closeit)) { |
Christian Heimes | 04ac4c1 | 2012-09-11 15:47:28 +0200 | [diff] [blame] | 366 | FILE *pyc_fp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | /* Try to run a pyc file. First, re-open in binary */ |
| 368 | if (closeit) |
| 369 | fclose(fp); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 370 | if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | fprintf(stderr, "python: Can't reopen .pyc file\n"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | goto done; |
| 373 | } |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 374 | |
| 375 | if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) { |
| 376 | fprintf(stderr, "python: failed to set __main__.__loader__\n"); |
| 377 | ret = -1; |
Christian Heimes | 04ac4c1 | 2012-09-11 15:47:28 +0200 | [diff] [blame] | 378 | fclose(pyc_fp); |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 379 | goto done; |
| 380 | } |
Christian Heimes | 04ac4c1 | 2012-09-11 15:47:28 +0200 | [diff] [blame] | 381 | v = run_pyc_file(pyc_fp, filename, d, d, flags); |
| 382 | fclose(pyc_fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | } else { |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 384 | /* When running from stdin, leave __main__.__loader__ alone */ |
| 385 | if (strcmp(filename, "<stdin>") != 0 && |
| 386 | set_main_loader(d, filename, "SourceFileLoader") < 0) { |
| 387 | fprintf(stderr, "python: failed to set __main__.__loader__\n"); |
| 388 | ret = -1; |
| 389 | goto done; |
| 390 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, |
| 392 | closeit, flags); |
| 393 | } |
| 394 | flush_io(); |
| 395 | if (v == NULL) { |
| 396 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | goto done; |
| 398 | } |
| 399 | Py_DECREF(v); |
| 400 | ret = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 401 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (set_file_name && PyDict_DelItemString(d, "__file__")) |
| 403 | PyErr_Clear(); |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 404 | Py_DECREF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | return ret; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 409 | PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | PyObject *m, *d, *v; |
| 412 | m = PyImport_AddModule("__main__"); |
| 413 | if (m == NULL) |
| 414 | return -1; |
| 415 | d = PyModule_GetDict(m); |
| 416 | v = PyRun_StringFlags(command, Py_file_input, d, d, flags); |
| 417 | if (v == NULL) { |
| 418 | PyErr_Print(); |
| 419 | return -1; |
| 420 | } |
| 421 | Py_DECREF(v); |
| 422 | return 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 425 | static int |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 426 | parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename, |
| 427 | int *lineno, int *offset, PyObject **text) |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | long hold; |
| 430 | PyObject *v; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 431 | _Py_IDENTIFIER(msg); |
| 432 | _Py_IDENTIFIER(filename); |
| 433 | _Py_IDENTIFIER(lineno); |
| 434 | _Py_IDENTIFIER(offset); |
| 435 | _Py_IDENTIFIER(text); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 436 | |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 437 | *message = NULL; |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 438 | *filename = NULL; |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | /* new style errors. `err' is an instance */ |
Benjamin Peterson | 0a9a636 | 2012-04-03 00:35:36 -0400 | [diff] [blame] | 441 | *message = _PyObject_GetAttrId(err, &PyId_msg); |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 442 | if (!*message) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | goto finally; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 444 | |
Benjamin Peterson | 0a9a636 | 2012-04-03 00:35:36 -0400 | [diff] [blame] | 445 | v = _PyObject_GetAttrId(err, &PyId_filename); |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 446 | if (!v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | goto finally; |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 448 | if (v == Py_None) { |
| 449 | Py_DECREF(v); |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 450 | *filename = _PyUnicode_FromId(&PyId_string); |
| 451 | if (*filename == NULL) |
| 452 | goto finally; |
| 453 | Py_INCREF(*filename); |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 454 | } |
| 455 | else { |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 456 | *filename = v; |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 457 | } |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 458 | |
Benjamin Peterson | 0a9a636 | 2012-04-03 00:35:36 -0400 | [diff] [blame] | 459 | v = _PyObject_GetAttrId(err, &PyId_lineno); |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 460 | if (!v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | goto finally; |
| 462 | hold = PyLong_AsLong(v); |
| 463 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | if (hold < 0 && PyErr_Occurred()) |
| 465 | goto finally; |
| 466 | *lineno = (int)hold; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 467 | |
Benjamin Peterson | 0a9a636 | 2012-04-03 00:35:36 -0400 | [diff] [blame] | 468 | v = _PyObject_GetAttrId(err, &PyId_offset); |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 469 | if (!v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | goto finally; |
| 471 | if (v == Py_None) { |
| 472 | *offset = -1; |
| 473 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | } else { |
| 475 | hold = PyLong_AsLong(v); |
| 476 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | if (hold < 0 && PyErr_Occurred()) |
| 478 | goto finally; |
| 479 | *offset = (int)hold; |
| 480 | } |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 481 | |
Benjamin Peterson | 0a9a636 | 2012-04-03 00:35:36 -0400 | [diff] [blame] | 482 | v = _PyObject_GetAttrId(err, &PyId_text); |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 483 | if (!v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | goto finally; |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 485 | if (v == Py_None) { |
| 486 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | *text = NULL; |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 488 | } |
| 489 | else { |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 490 | *text = v; |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 491 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | return 1; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 493 | |
| 494 | finally: |
Benjamin Peterson | 80d5042 | 2012-04-03 00:30:38 -0400 | [diff] [blame] | 495 | Py_XDECREF(*message); |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 496 | Py_XDECREF(*filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | return 0; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 500 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 501 | PyErr_Print(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | PyErr_PrintEx(1); |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 506 | static void |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 507 | print_error_text(PyObject *f, int offset, PyObject *text_obj) |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 508 | { |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 509 | char *text; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | char *nl; |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 511 | |
| 512 | text = _PyUnicode_AsString(text_obj); |
| 513 | if (text == NULL) |
| 514 | return; |
| 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | if (offset >= 0) { |
Victor Stinner | 98ea54c | 2014-08-15 23:30:40 +0200 | [diff] [blame] | 517 | if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n') |
Benjamin Peterson | a95e977 | 2010-10-29 03:28:14 +0000 | [diff] [blame] | 518 | offset--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | for (;;) { |
| 520 | nl = strchr(text, '\n'); |
| 521 | if (nl == NULL || nl-text >= offset) |
| 522 | break; |
| 523 | offset -= (int)(nl+1-text); |
| 524 | text = nl+1; |
| 525 | } |
| 526 | while (*text == ' ' || *text == '\t') { |
| 527 | text++; |
| 528 | offset--; |
| 529 | } |
| 530 | } |
| 531 | PyFile_WriteString(" ", f); |
| 532 | PyFile_WriteString(text, f); |
| 533 | if (*text == '\0' || text[strlen(text)-1] != '\n') |
| 534 | PyFile_WriteString("\n", f); |
| 535 | if (offset == -1) |
| 536 | return; |
| 537 | PyFile_WriteString(" ", f); |
Benjamin Peterson | a95e977 | 2010-10-29 03:28:14 +0000 | [diff] [blame] | 538 | while (--offset > 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | PyFile_WriteString(" ", f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | PyFile_WriteString("^\n", f); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Guido van Rossum | 66e8e86 | 2001-03-23 17:54:43 +0000 | [diff] [blame] | 543 | static void |
| 544 | handle_system_exit(void) |
Ka-Ping Yee | 26fabb0 | 2001-03-23 15:36:41 +0000 | [diff] [blame] | 545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | PyObject *exception, *value, *tb; |
| 547 | int exitcode = 0; |
Tim Peters | cf615b5 | 2003-04-19 18:47:02 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | if (Py_InspectFlag) |
| 550 | /* Don't exit if -i flag was given. This flag is set to 0 |
| 551 | * when entering interactive mode for inspecting. */ |
| 552 | return; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | PyErr_Fetch(&exception, &value, &tb); |
| 555 | fflush(stdout); |
| 556 | if (value == NULL || value == Py_None) |
| 557 | goto done; |
| 558 | if (PyExceptionInstance_Check(value)) { |
| 559 | /* The error code should be in the `code' attribute. */ |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 560 | _Py_IDENTIFIER(code); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 561 | PyObject *code = _PyObject_GetAttrId(value, &PyId_code); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | if (code) { |
| 563 | Py_DECREF(value); |
| 564 | value = code; |
| 565 | if (value == Py_None) |
| 566 | goto done; |
| 567 | } |
| 568 | /* If we failed to dig out the 'code' attribute, |
| 569 | just let the else clause below print the error. */ |
| 570 | } |
| 571 | if (PyLong_Check(value)) |
| 572 | exitcode = (int)PyLong_AsLong(value); |
| 573 | else { |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 574 | PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr); |
Nick Coghlan | d979e43 | 2014-02-09 10:43:21 +1000 | [diff] [blame] | 575 | /* We clear the exception here to avoid triggering the assertion |
| 576 | * in PyObject_Str that ensures it won't silently lose exception |
| 577 | * details. |
| 578 | */ |
| 579 | PyErr_Clear(); |
Victor Stinner | 7126dbc | 2010-05-21 23:45:42 +0000 | [diff] [blame] | 580 | if (sys_stderr != NULL && sys_stderr != Py_None) { |
| 581 | PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW); |
| 582 | } else { |
| 583 | PyObject_Print(value, stderr, Py_PRINT_RAW); |
| 584 | fflush(stderr); |
| 585 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | PySys_WriteStderr("\n"); |
| 587 | exitcode = 1; |
| 588 | } |
Tim Peters | cf615b5 | 2003-04-19 18:47:02 +0000 | [diff] [blame] | 589 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | /* Restore and clear the exception info, in order to properly decref |
| 591 | * the exception, value, and traceback. If we just exit instead, |
| 592 | * these leak, which confuses PYTHONDUMPREFS output, and may prevent |
| 593 | * some finalizers from running. |
| 594 | */ |
| 595 | PyErr_Restore(exception, value, tb); |
| 596 | PyErr_Clear(); |
| 597 | Py_Exit(exitcode); |
| 598 | /* NOTREACHED */ |
Ka-Ping Yee | 26fabb0 | 2001-03-23 15:36:41 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 602 | PyErr_PrintEx(int set_sys_last_vars) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | PyObject *exception, *v, *tb, *hook; |
Guido van Rossum | 66e8e86 | 2001-03-23 17:54:43 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 607 | handle_system_exit(); |
| 608 | } |
| 609 | PyErr_Fetch(&exception, &v, &tb); |
| 610 | if (exception == NULL) |
| 611 | return; |
| 612 | PyErr_NormalizeException(&exception, &v, &tb); |
| 613 | if (tb == NULL) { |
| 614 | tb = Py_None; |
| 615 | Py_INCREF(tb); |
| 616 | } |
| 617 | PyException_SetTraceback(v, tb); |
| 618 | if (exception == NULL) |
| 619 | return; |
| 620 | /* Now we know v != NULL too */ |
| 621 | if (set_sys_last_vars) { |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 622 | _PySys_SetObjectId(&PyId_last_type, exception); |
| 623 | _PySys_SetObjectId(&PyId_last_value, v); |
| 624 | _PySys_SetObjectId(&PyId_last_traceback, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | } |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 626 | hook = _PySys_GetObjectId(&PyId_excepthook); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | if (hook) { |
| 628 | PyObject *args = PyTuple_Pack(3, exception, v, tb); |
| 629 | PyObject *result = PyEval_CallObject(hook, args); |
| 630 | if (result == NULL) { |
| 631 | PyObject *exception2, *v2, *tb2; |
| 632 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 633 | handle_system_exit(); |
| 634 | } |
| 635 | PyErr_Fetch(&exception2, &v2, &tb2); |
| 636 | PyErr_NormalizeException(&exception2, &v2, &tb2); |
| 637 | /* It should not be possible for exception2 or v2 |
| 638 | to be NULL. However PyErr_Display() can't |
| 639 | tolerate NULLs, so just be safe. */ |
| 640 | if (exception2 == NULL) { |
| 641 | exception2 = Py_None; |
| 642 | Py_INCREF(exception2); |
| 643 | } |
| 644 | if (v2 == NULL) { |
| 645 | v2 = Py_None; |
| 646 | Py_INCREF(v2); |
| 647 | } |
| 648 | fflush(stdout); |
| 649 | PySys_WriteStderr("Error in sys.excepthook:\n"); |
| 650 | PyErr_Display(exception2, v2, tb2); |
| 651 | PySys_WriteStderr("\nOriginal exception was:\n"); |
| 652 | PyErr_Display(exception, v, tb); |
| 653 | Py_DECREF(exception2); |
| 654 | Py_DECREF(v2); |
| 655 | Py_XDECREF(tb2); |
| 656 | } |
| 657 | Py_XDECREF(result); |
| 658 | Py_XDECREF(args); |
| 659 | } else { |
| 660 | PySys_WriteStderr("sys.excepthook is missing\n"); |
| 661 | PyErr_Display(exception, v, tb); |
| 662 | } |
| 663 | Py_XDECREF(exception); |
| 664 | Py_XDECREF(v); |
| 665 | Py_XDECREF(tb); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 668 | static void |
| 669 | print_exception(PyObject *f, PyObject *value) |
| 670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | int err = 0; |
| 672 | PyObject *type, *tb; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 673 | _Py_IDENTIFIER(print_file_and_line); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | if (!PyExceptionInstance_Check(value)) { |
Victor Stinner | 52ce3b0 | 2013-12-09 02:10:08 +0100 | [diff] [blame] | 676 | err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f); |
| 677 | err += PyFile_WriteString(Py_TYPE(value)->tp_name, f); |
| 678 | err += PyFile_WriteString(" found\n", f); |
| 679 | if (err) |
| 680 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | return; |
| 682 | } |
Benjamin Peterson | 2658260 | 2008-08-23 20:08:07 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | Py_INCREF(value); |
| 685 | fflush(stdout); |
| 686 | type = (PyObject *) Py_TYPE(value); |
| 687 | tb = PyException_GetTraceback(value); |
| 688 | if (tb && tb != Py_None) |
| 689 | err = PyTraceBack_Print(tb, f); |
| 690 | if (err == 0 && |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 691 | _PyObject_HasAttrId(value, &PyId_print_file_and_line)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | { |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 693 | PyObject *message, *filename, *text; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | int lineno, offset; |
| 695 | if (!parse_syntax_error(value, &message, &filename, |
| 696 | &lineno, &offset, &text)) |
| 697 | PyErr_Clear(); |
| 698 | else { |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 699 | PyObject *line; |
| 700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | Py_DECREF(value); |
| 702 | value = message; |
Victor Stinner | efa7a0e | 2013-11-07 12:37:56 +0100 | [diff] [blame] | 703 | |
| 704 | line = PyUnicode_FromFormat(" File \"%U\", line %d\n", |
| 705 | filename, lineno); |
| 706 | Py_DECREF(filename); |
| 707 | if (line != NULL) { |
| 708 | PyFile_WriteObject(line, f, Py_PRINT_RAW); |
| 709 | Py_DECREF(line); |
| 710 | } |
| 711 | |
| 712 | if (text != NULL) { |
| 713 | print_error_text(f, offset, text); |
| 714 | Py_DECREF(text); |
| 715 | } |
| 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | /* Can't be bothered to check all those |
| 718 | PyFile_WriteString() calls */ |
| 719 | if (PyErr_Occurred()) |
| 720 | err = -1; |
| 721 | } |
| 722 | } |
| 723 | if (err) { |
| 724 | /* Don't do anything else */ |
| 725 | } |
| 726 | else { |
| 727 | PyObject* moduleName; |
| 728 | char* className; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 729 | _Py_IDENTIFIER(__module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | assert(PyExceptionClass_Check(type)); |
| 731 | className = PyExceptionClass_Name(type); |
| 732 | if (className != NULL) { |
| 733 | char *dot = strrchr(className, '.'); |
| 734 | if (dot != NULL) |
| 735 | className = dot+1; |
| 736 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 737 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 738 | moduleName = _PyObject_GetAttrId(type, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | if (moduleName == NULL || !PyUnicode_Check(moduleName)) |
| 740 | { |
Victor Stinner | 13b21bd | 2011-05-26 14:25:13 +0200 | [diff] [blame] | 741 | Py_XDECREF(moduleName); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | err = PyFile_WriteString("<unknown>", f); |
| 743 | } |
| 744 | else { |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 745 | if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | { |
Victor Stinner | 937114f | 2013-11-07 00:12:30 +0100 | [diff] [blame] | 747 | err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | err += PyFile_WriteString(".", f); |
| 749 | } |
| 750 | Py_DECREF(moduleName); |
| 751 | } |
| 752 | if (err == 0) { |
| 753 | if (className == NULL) |
| 754 | err = PyFile_WriteString("<unknown>", f); |
| 755 | else |
| 756 | err = PyFile_WriteString(className, f); |
| 757 | } |
| 758 | } |
| 759 | if (err == 0 && (value != Py_None)) { |
| 760 | PyObject *s = PyObject_Str(value); |
| 761 | /* only print colon if the str() of the |
| 762 | object is not the empty string |
| 763 | */ |
| 764 | if (s == NULL) |
| 765 | err = -1; |
| 766 | else if (!PyUnicode_Check(s) || |
Victor Stinner | e251d6d | 2011-11-20 19:20:00 +0100 | [diff] [blame] | 767 | PyUnicode_GetLength(s) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | err = PyFile_WriteString(": ", f); |
| 769 | if (err == 0) |
| 770 | err = PyFile_WriteObject(s, f, Py_PRINT_RAW); |
| 771 | Py_XDECREF(s); |
| 772 | } |
| 773 | /* try to write a newline in any case */ |
| 774 | err += PyFile_WriteString("\n", f); |
| 775 | Py_XDECREF(tb); |
| 776 | Py_DECREF(value); |
| 777 | /* If an error happened here, don't show it. |
| 778 | XXX This is wrong, but too many callers rely on this behavior. */ |
| 779 | if (err != 0) |
| 780 | PyErr_Clear(); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static const char *cause_message = |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | "\nThe above exception was the direct cause " |
| 785 | "of the following exception:\n\n"; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 786 | |
| 787 | static const char *context_message = |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | "\nDuring handling of the above exception, " |
| 789 | "another exception occurred:\n\n"; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 790 | |
| 791 | static void |
| 792 | print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) |
| 793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | int err = 0, res; |
| 795 | PyObject *cause, *context; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | if (seen != NULL) { |
| 798 | /* Exception chaining */ |
| 799 | if (PySet_Add(seen, value) == -1) |
| 800 | PyErr_Clear(); |
| 801 | else if (PyExceptionInstance_Check(value)) { |
| 802 | cause = PyException_GetCause(value); |
| 803 | context = PyException_GetContext(value); |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 804 | if (cause) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | res = PySet_Contains(seen, cause); |
| 806 | if (res == -1) |
| 807 | PyErr_Clear(); |
| 808 | if (res == 0) { |
| 809 | print_exception_recursive( |
| 810 | f, cause, seen); |
| 811 | err |= PyFile_WriteString( |
| 812 | cause_message, f); |
| 813 | } |
| 814 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 815 | else if (context && |
| 816 | !((PyBaseExceptionObject *)value)->suppress_context) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | res = PySet_Contains(seen, context); |
| 818 | if (res == -1) |
| 819 | PyErr_Clear(); |
| 820 | if (res == 0) { |
| 821 | print_exception_recursive( |
| 822 | f, context, seen); |
| 823 | err |= PyFile_WriteString( |
| 824 | context_message, f); |
| 825 | } |
| 826 | } |
| 827 | Py_XDECREF(context); |
| 828 | Py_XDECREF(cause); |
| 829 | } |
| 830 | } |
| 831 | print_exception(f, value); |
| 832 | if (err != 0) |
| 833 | PyErr_Clear(); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 836 | void |
| 837 | PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | PyObject *seen; |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 840 | PyObject *f = _PySys_GetObjectId(&PyId_stderr); |
Antoine Pitrou | 24201d4 | 2013-10-13 21:53:13 +0200 | [diff] [blame] | 841 | if (PyExceptionInstance_Check(value) |
| 842 | && tb != NULL && PyTraceBack_Check(tb)) { |
| 843 | /* Put the traceback on the exception, otherwise it won't get |
| 844 | displayed. See issue #18776. */ |
| 845 | PyObject *cur_tb = PyException_GetTraceback(value); |
| 846 | if (cur_tb == NULL) |
| 847 | PyException_SetTraceback(value, tb); |
| 848 | else |
| 849 | Py_DECREF(cur_tb); |
| 850 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | if (f == Py_None) { |
| 852 | /* pass */ |
| 853 | } |
| 854 | else if (f == NULL) { |
| 855 | _PyObject_Dump(value); |
| 856 | fprintf(stderr, "lost sys.stderr\n"); |
| 857 | } |
| 858 | else { |
| 859 | /* We choose to ignore seen being possibly NULL, and report |
| 860 | at least the main exception (it could be a MemoryError). |
| 861 | */ |
| 862 | seen = PySet_New(NULL); |
| 863 | if (seen == NULL) |
| 864 | PyErr_Clear(); |
| 865 | print_exception_recursive(f, value, seen); |
| 866 | Py_XDECREF(seen); |
| 867 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 870 | PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 871 | PyRun_StringFlags(const char *str, int start, PyObject *globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | PyObject *locals, PyCompilerFlags *flags) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 873 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | PyObject *ret = NULL; |
| 875 | mod_ty mod; |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 876 | PyArena *arena; |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 877 | PyObject *filename; |
| 878 | |
| 879 | filename = _PyUnicode_FromId(&PyId_string); /* borrowed */ |
| 880 | if (filename == NULL) |
| 881 | return NULL; |
| 882 | |
| 883 | arena = PyArena_New(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | if (arena == NULL) |
| 885 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 886 | |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 887 | mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | if (mod != NULL) |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 889 | ret = run_mod(mod, filename, globals, locals, flags, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | PyArena_Free(arena); |
| 891 | return ret; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | PyObject * |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 895 | PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | PyObject *locals, int closeit, PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 897 | { |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 898 | PyObject *ret = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | mod_ty mod; |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 900 | PyArena *arena = NULL; |
| 901 | PyObject *filename; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 902 | |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 903 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 904 | if (filename == NULL) |
| 905 | goto exit; |
| 906 | |
| 907 | arena = PyArena_New(); |
| 908 | if (arena == NULL) |
| 909 | goto exit; |
| 910 | |
| 911 | mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0, |
| 912 | flags, NULL, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | if (closeit) |
| 914 | fclose(fp); |
| 915 | if (mod == NULL) { |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 916 | goto exit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | } |
| 918 | ret = run_mod(mod, filename, globals, locals, flags, arena); |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 919 | |
| 920 | exit: |
| 921 | Py_XDECREF(filename); |
| 922 | if (arena != NULL) |
| 923 | PyArena_Free(arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | return ret; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Guido van Rossum | 6c193fa | 2007-12-05 05:14:58 +0000 | [diff] [blame] | 927 | static void |
| 928 | flush_io(void) |
| 929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | PyObject *f, *r; |
| 931 | PyObject *type, *value, *traceback; |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | /* Save the current exception */ |
| 934 | PyErr_Fetch(&type, &value, &traceback); |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 935 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 936 | f = _PySys_GetObjectId(&PyId_stderr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | if (f != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 938 | r = _PyObject_CallMethodId(f, &PyId_flush, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | if (r) |
| 940 | Py_DECREF(r); |
| 941 | else |
| 942 | PyErr_Clear(); |
| 943 | } |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 944 | f = _PySys_GetObjectId(&PyId_stdout); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | if (f != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 946 | r = _PyObject_CallMethodId(f, &PyId_flush, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | if (r) |
| 948 | Py_DECREF(r); |
| 949 | else |
| 950 | PyErr_Clear(); |
| 951 | } |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 6c193fa | 2007-12-05 05:14:58 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 956 | static PyObject * |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 957 | run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals, |
| 958 | PyCompilerFlags *flags, PyArena *arena) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | PyCodeObject *co; |
| 961 | PyObject *v; |
Victor Stinner | 95701bd | 2013-11-06 18:41:07 +0100 | [diff] [blame] | 962 | co = PyAST_CompileObject(mod, filename, flags, -1, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | if (co == NULL) |
| 964 | return NULL; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 965 | v = PyEval_EvalCode((PyObject*)co, globals, locals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | Py_DECREF(co); |
| 967 | return v; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 970 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 971 | run_pyc_file(FILE *fp, const char *filename, PyObject *globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | PyObject *locals, PyCompilerFlags *flags) |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | PyCodeObject *co; |
| 975 | PyObject *v; |
| 976 | long magic; |
| 977 | long PyImport_GetMagicNumber(void); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | magic = PyMarshal_ReadLongFromFile(fp); |
| 980 | if (magic != PyImport_GetMagicNumber()) { |
Victor Stinner | 5200f55 | 2015-03-18 13:56:25 +0100 | [diff] [blame] | 981 | if (!PyErr_Occurred()) |
| 982 | PyErr_SetString(PyExc_RuntimeError, |
| 983 | "Bad magic number in .pyc file"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | return NULL; |
| 985 | } |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 986 | /* Skip mtime and size */ |
| 987 | (void) PyMarshal_ReadLongFromFile(fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | (void) PyMarshal_ReadLongFromFile(fp); |
Victor Stinner | 5200f55 | 2015-03-18 13:56:25 +0100 | [diff] [blame] | 989 | if (PyErr_Occurred()) |
| 990 | return NULL; |
| 991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | v = PyMarshal_ReadLastObjectFromFile(fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | if (v == NULL || !PyCode_Check(v)) { |
| 994 | Py_XDECREF(v); |
| 995 | PyErr_SetString(PyExc_RuntimeError, |
| 996 | "Bad code object in .pyc file"); |
| 997 | return NULL; |
| 998 | } |
| 999 | co = (PyCodeObject *)v; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1000 | v = PyEval_EvalCode((PyObject*)co, globals, locals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | if (v && flags) |
| 1002 | flags->cf_flags |= (co->co_flags & PyCF_MASK); |
| 1003 | Py_DECREF(co); |
| 1004 | return v; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1007 | PyObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1008 | Py_CompileStringObject(const char *str, PyObject *filename, int start, |
| 1009 | PyCompilerFlags *flags, int optimize) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | PyCodeObject *co; |
| 1012 | mod_ty mod; |
| 1013 | PyArena *arena = PyArena_New(); |
| 1014 | if (arena == NULL) |
| 1015 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1016 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1017 | mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | if (mod == NULL) { |
| 1019 | PyArena_Free(arena); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { |
| 1023 | PyObject *result = PyAST_mod2obj(mod); |
| 1024 | PyArena_Free(arena); |
| 1025 | return result; |
| 1026 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1027 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | PyArena_Free(arena); |
| 1029 | return (PyObject *)co; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1032 | PyObject * |
| 1033 | Py_CompileStringExFlags(const char *str, const char *filename_str, int start, |
| 1034 | PyCompilerFlags *flags, int optimize) |
| 1035 | { |
| 1036 | PyObject *filename, *co; |
| 1037 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 1038 | if (filename == NULL) |
| 1039 | return NULL; |
| 1040 | co = Py_CompileStringObject(str, filename, start, flags, optimize); |
| 1041 | Py_DECREF(filename); |
| 1042 | return co; |
| 1043 | } |
| 1044 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1045 | /* For use in Py_LIMITED_API */ |
| 1046 | #undef Py_CompileString |
| 1047 | PyObject * |
| 1048 | PyCompileString(const char *str, const char *filename, int start) |
| 1049 | { |
| 1050 | return Py_CompileStringFlags(str, filename, start, NULL); |
| 1051 | } |
| 1052 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1053 | struct symtable * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1054 | Py_SymtableStringObject(const char *str, PyObject *filename, int start) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | struct symtable *st; |
| 1057 | mod_ty mod; |
| 1058 | PyCompilerFlags flags; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1059 | PyArena *arena; |
| 1060 | |
| 1061 | arena = PyArena_New(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | if (arena == NULL) |
| 1063 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | flags.cf_flags = 0; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1066 | mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | if (mod == NULL) { |
| 1068 | PyArena_Free(arena); |
| 1069 | return NULL; |
| 1070 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1071 | st = PySymtable_BuildObject(mod, filename, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | PyArena_Free(arena); |
| 1073 | return st; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1076 | struct symtable * |
| 1077 | Py_SymtableString(const char *str, const char *filename_str, int start) |
| 1078 | { |
| 1079 | PyObject *filename; |
| 1080 | struct symtable *st; |
| 1081 | |
| 1082 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 1083 | if (filename == NULL) |
| 1084 | return NULL; |
| 1085 | st = Py_SymtableStringObject(str, filename, start); |
| 1086 | Py_DECREF(filename); |
| 1087 | return st; |
| 1088 | } |
| 1089 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1090 | /* Preferred access to parser is through AST. */ |
| 1091 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1092 | PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start, |
| 1093 | PyCompilerFlags *flags, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | mod_ty mod; |
| 1096 | PyCompilerFlags localflags; |
| 1097 | perrdetail err; |
| 1098 | int iflags = PARSER_FLAGS(flags); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 1099 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1100 | node *n = PyParser_ParseStringObject(s, filename, |
| 1101 | &_PyParser_Grammar, start, &err, |
| 1102 | &iflags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | if (flags == NULL) { |
| 1104 | localflags.cf_flags = 0; |
| 1105 | flags = &localflags; |
| 1106 | } |
| 1107 | if (n) { |
| 1108 | flags->cf_flags |= iflags & PyCF_MASK; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1109 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | PyNode_Free(n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | } |
| 1112 | else { |
| 1113 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1114 | mod = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1116 | err_free(&err); |
| 1117 | return mod; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | mod_ty |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1121 | PyParser_ASTFromString(const char *s, const char *filename_str, int start, |
| 1122 | PyCompilerFlags *flags, PyArena *arena) |
| 1123 | { |
| 1124 | PyObject *filename; |
| 1125 | mod_ty mod; |
| 1126 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 1127 | if (filename == NULL) |
| 1128 | return NULL; |
| 1129 | mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena); |
| 1130 | Py_DECREF(filename); |
| 1131 | return mod; |
| 1132 | } |
| 1133 | |
| 1134 | mod_ty |
| 1135 | PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc, |
| 1136 | int start, char *ps1, |
| 1137 | char *ps2, PyCompilerFlags *flags, int *errcode, |
| 1138 | PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | mod_ty mod; |
| 1141 | PyCompilerFlags localflags; |
| 1142 | perrdetail err; |
| 1143 | int iflags = PARSER_FLAGS(flags); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 1144 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1145 | node *n = PyParser_ParseFileObject(fp, filename, enc, |
| 1146 | &_PyParser_Grammar, |
| 1147 | start, ps1, ps2, &err, &iflags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | if (flags == NULL) { |
| 1149 | localflags.cf_flags = 0; |
| 1150 | flags = &localflags; |
| 1151 | } |
| 1152 | if (n) { |
| 1153 | flags->cf_flags |= iflags & PyCF_MASK; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1154 | mod = PyAST_FromNodeObject(n, flags, filename, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | PyNode_Free(n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | } |
| 1157 | else { |
| 1158 | err_input(&err); |
| 1159 | if (errcode) |
| 1160 | *errcode = err.error; |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1161 | mod = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1163 | err_free(&err); |
| 1164 | return mod; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1167 | mod_ty |
| 1168 | PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc, |
| 1169 | int start, char *ps1, |
| 1170 | char *ps2, PyCompilerFlags *flags, int *errcode, |
| 1171 | PyArena *arena) |
| 1172 | { |
| 1173 | mod_ty mod; |
| 1174 | PyObject *filename; |
| 1175 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 1176 | if (filename == NULL) |
| 1177 | return NULL; |
| 1178 | mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2, |
| 1179 | flags, errcode, arena); |
| 1180 | Py_DECREF(filename); |
| 1181 | return mod; |
| 1182 | } |
| 1183 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1184 | /* Simplified interface to parsefile -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1185 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1186 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1187 | PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | perrdetail err; |
| 1190 | node *n = PyParser_ParseFileFlags(fp, filename, NULL, |
| 1191 | &_PyParser_Grammar, |
| 1192 | start, NULL, NULL, &err, flags); |
| 1193 | if (n == NULL) |
| 1194 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1195 | err_free(&err); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | return n; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1200 | /* Simplified interface to parsestring -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1201 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1202 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1203 | PyParser_SimpleParseStringFlags(const char *str, int start, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 1204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | perrdetail err; |
| 1206 | node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, |
| 1207 | start, &err, flags); |
| 1208 | if (n == NULL) |
| 1209 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1210 | err_free(&err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | return n; |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1215 | PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | int start, int flags) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | perrdetail err; |
| 1219 | node *n = PyParser_ParseStringFlagsFilename(str, filename, |
| 1220 | &_PyParser_Grammar, start, &err, flags); |
| 1221 | if (n == NULL) |
| 1222 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1223 | err_free(&err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | return n; |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1228 | PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0); |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 1233 | /* May want to move a more generalized form of this to parsetok.c or |
| 1234 | even parser modules. */ |
| 1235 | |
| 1236 | void |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1237 | PyParser_ClearError(perrdetail *err) |
| 1238 | { |
| 1239 | err_free(err); |
| 1240 | } |
| 1241 | |
| 1242 | void |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 1243 | PyParser_SetError(perrdetail *err) |
| 1244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | err_input(err); |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1248 | static void |
| 1249 | err_free(perrdetail *err) |
| 1250 | { |
| 1251 | Py_CLEAR(err->filename); |
| 1252 | } |
| 1253 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1254 | /* Set the error appropriate to the given input error code (see errcode.h) */ |
| 1255 | |
| 1256 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1257 | err_input(perrdetail *err) |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | PyObject *v, *w, *errtype, *errtext; |
| 1260 | PyObject *msg_obj = NULL; |
| 1261 | char *msg = NULL; |
Serhiy Storchaka | 65fd059 | 2014-01-21 22:26:52 +0200 | [diff] [blame] | 1262 | int offset = err->offset; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 1263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | errtype = PyExc_SyntaxError; |
| 1265 | switch (err->error) { |
| 1266 | case E_ERROR: |
| 1267 | return; |
| 1268 | case E_SYNTAX: |
| 1269 | errtype = PyExc_IndentationError; |
| 1270 | if (err->expected == INDENT) |
| 1271 | msg = "expected an indented block"; |
| 1272 | else if (err->token == INDENT) |
| 1273 | msg = "unexpected indent"; |
| 1274 | else if (err->token == DEDENT) |
| 1275 | msg = "unexpected unindent"; |
| 1276 | else { |
| 1277 | errtype = PyExc_SyntaxError; |
| 1278 | msg = "invalid syntax"; |
| 1279 | } |
| 1280 | break; |
| 1281 | case E_TOKEN: |
| 1282 | msg = "invalid token"; |
| 1283 | break; |
| 1284 | case E_EOFS: |
| 1285 | msg = "EOF while scanning triple-quoted string literal"; |
| 1286 | break; |
| 1287 | case E_EOLS: |
| 1288 | msg = "EOL while scanning string literal"; |
| 1289 | break; |
| 1290 | case E_INTR: |
| 1291 | if (!PyErr_Occurred()) |
| 1292 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 1293 | goto cleanup; |
| 1294 | case E_NOMEM: |
| 1295 | PyErr_NoMemory(); |
| 1296 | goto cleanup; |
| 1297 | case E_EOF: |
| 1298 | msg = "unexpected EOF while parsing"; |
| 1299 | break; |
| 1300 | case E_TABSPACE: |
| 1301 | errtype = PyExc_TabError; |
| 1302 | msg = "inconsistent use of tabs and spaces in indentation"; |
| 1303 | break; |
| 1304 | case E_OVERFLOW: |
| 1305 | msg = "expression too long"; |
| 1306 | break; |
| 1307 | case E_DEDENT: |
| 1308 | errtype = PyExc_IndentationError; |
| 1309 | msg = "unindent does not match any outer indentation level"; |
| 1310 | break; |
| 1311 | case E_TOODEEP: |
| 1312 | errtype = PyExc_IndentationError; |
| 1313 | msg = "too many levels of indentation"; |
| 1314 | break; |
| 1315 | case E_DECODE: { |
| 1316 | PyObject *type, *value, *tb; |
| 1317 | PyErr_Fetch(&type, &value, &tb); |
| 1318 | msg = "unknown decode error"; |
| 1319 | if (value != NULL) |
| 1320 | msg_obj = PyObject_Str(value); |
| 1321 | Py_XDECREF(type); |
| 1322 | Py_XDECREF(value); |
| 1323 | Py_XDECREF(tb); |
| 1324 | break; |
| 1325 | } |
| 1326 | case E_LINECONT: |
| 1327 | msg = "unexpected character after line continuation character"; |
| 1328 | break; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | case E_IDENTIFIER: |
| 1331 | msg = "invalid character in identifier"; |
| 1332 | break; |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 1333 | case E_BADSINGLE: |
| 1334 | msg = "multiple statements found while compiling a single statement"; |
| 1335 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | default: |
| 1337 | fprintf(stderr, "error=%d\n", err->error); |
| 1338 | msg = "unknown parsing error"; |
| 1339 | break; |
| 1340 | } |
| 1341 | /* err->text may not be UTF-8 in case of decoding errors. |
| 1342 | Explicitly convert to an object. */ |
| 1343 | if (!err->text) { |
| 1344 | errtext = Py_None; |
| 1345 | Py_INCREF(Py_None); |
| 1346 | } else { |
Serhiy Storchaka | 65fd059 | 2014-01-21 22:26:52 +0200 | [diff] [blame] | 1347 | errtext = PyUnicode_DecodeUTF8(err->text, err->offset, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | "replace"); |
Serhiy Storchaka | 65fd059 | 2014-01-21 22:26:52 +0200 | [diff] [blame] | 1349 | if (errtext != NULL) { |
| 1350 | Py_ssize_t len = strlen(err->text); |
| 1351 | offset = (int)PyUnicode_GET_LENGTH(errtext); |
| 1352 | if (len != err->offset) { |
| 1353 | Py_DECREF(errtext); |
| 1354 | errtext = PyUnicode_DecodeUTF8(err->text, len, |
| 1355 | "replace"); |
| 1356 | } |
| 1357 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1359 | v = Py_BuildValue("(OiiN)", err->filename, |
Serhiy Storchaka | 65fd059 | 2014-01-21 22:26:52 +0200 | [diff] [blame] | 1360 | err->lineno, offset, errtext); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | if (v != NULL) { |
| 1362 | if (msg_obj) |
| 1363 | w = Py_BuildValue("(OO)", msg_obj, v); |
| 1364 | else |
| 1365 | w = Py_BuildValue("(sO)", msg, v); |
| 1366 | } else |
| 1367 | w = NULL; |
| 1368 | Py_XDECREF(v); |
| 1369 | PyErr_SetObject(errtype, w); |
| 1370 | Py_XDECREF(w); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1371 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | Py_XDECREF(msg_obj); |
| 1373 | if (err->text != NULL) { |
| 1374 | PyObject_FREE(err->text); |
| 1375 | err->text = NULL; |
| 1376 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 1379 | |
Zachary Ware | c4821d6 | 2014-11-21 23:35:12 -0600 | [diff] [blame] | 1380 | #if defined(USE_STACKCHECK) |
| 1381 | #if defined(WIN32) && defined(_MSC_VER) |
| 1382 | |
| 1383 | /* Stack checking for Microsoft C */ |
| 1384 | |
| 1385 | #include <malloc.h> |
| 1386 | #include <excpt.h> |
| 1387 | |
| 1388 | /* |
| 1389 | * Return non-zero when we run out of memory on the stack; zero otherwise. |
| 1390 | */ |
| 1391 | int |
| 1392 | PyOS_CheckStack(void) |
| 1393 | { |
| 1394 | __try { |
| 1395 | /* alloca throws a stack overflow exception if there's |
| 1396 | not enough space left on the stack */ |
| 1397 | alloca(PYOS_STACK_MARGIN * sizeof(void*)); |
| 1398 | return 0; |
| 1399 | } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ? |
| 1400 | EXCEPTION_EXECUTE_HANDLER : |
| 1401 | EXCEPTION_CONTINUE_SEARCH) { |
| 1402 | int errcode = _resetstkoflw(); |
| 1403 | if (errcode == 0) |
| 1404 | { |
| 1405 | Py_FatalError("Could not reset the stack!"); |
| 1406 | } |
| 1407 | } |
| 1408 | return 1; |
| 1409 | } |
| 1410 | |
| 1411 | #endif /* WIN32 && _MSC_VER */ |
| 1412 | |
| 1413 | /* Alternate implementations can be added here... */ |
| 1414 | |
| 1415 | #endif /* USE_STACKCHECK */ |
| 1416 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1417 | /* Deprecated C API functions still provided for binary compatiblity */ |
| 1418 | |
| 1419 | #undef PyParser_SimpleParseFile |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1420 | PyAPI_FUNC(node *) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1421 | PyParser_SimpleParseFile(FILE *fp, const char *filename, int start) |
| 1422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | return PyParser_SimpleParseFileFlags(fp, filename, start, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1426 | #undef PyParser_SimpleParseString |
| 1427 | PyAPI_FUNC(node *) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | PyParser_SimpleParseString(const char *str, int start) |
| 1429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | return PyParser_SimpleParseStringFlags(str, start, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1431 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1432 | |
| 1433 | #undef PyRun_AnyFile |
| 1434 | PyAPI_FUNC(int) |
| 1435 | PyRun_AnyFile(FILE *fp, const char *name) |
| 1436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | return PyRun_AnyFileExFlags(fp, name, 0, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | #undef PyRun_AnyFileEx |
| 1441 | PyAPI_FUNC(int) |
| 1442 | PyRun_AnyFileEx(FILE *fp, const char *name, int closeit) |
| 1443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | return PyRun_AnyFileExFlags(fp, name, closeit, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | #undef PyRun_AnyFileFlags |
| 1448 | PyAPI_FUNC(int) |
| 1449 | PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags) |
| 1450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | return PyRun_AnyFileExFlags(fp, name, 0, flags); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | #undef PyRun_File |
| 1455 | PyAPI_FUNC(PyObject *) |
| 1456 | PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l) |
| 1457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | #undef PyRun_FileEx |
| 1462 | PyAPI_FUNC(PyObject *) |
| 1463 | PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c) |
| 1464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | return PyRun_FileExFlags(fp, p, s, g, l, c, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | #undef PyRun_FileFlags |
| 1469 | PyAPI_FUNC(PyObject *) |
| 1470 | PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | PyCompilerFlags *flags) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | return PyRun_FileExFlags(fp, p, s, g, l, 0, flags); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | #undef PyRun_SimpleFile |
| 1477 | PyAPI_FUNC(int) |
| 1478 | PyRun_SimpleFile(FILE *f, const char *p) |
| 1479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | return PyRun_SimpleFileExFlags(f, p, 0, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | #undef PyRun_SimpleFileEx |
| 1484 | PyAPI_FUNC(int) |
| 1485 | PyRun_SimpleFileEx(FILE *f, const char *p, int c) |
| 1486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | return PyRun_SimpleFileExFlags(f, p, c, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | #undef PyRun_String |
| 1492 | PyAPI_FUNC(PyObject *) |
| 1493 | PyRun_String(const char *str, int s, PyObject *g, PyObject *l) |
| 1494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | return PyRun_StringFlags(str, s, g, l, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | #undef PyRun_SimpleString |
| 1499 | PyAPI_FUNC(int) |
| 1500 | PyRun_SimpleString(const char *s) |
| 1501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | return PyRun_SimpleStringFlags(s, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | #undef Py_CompileString |
| 1506 | PyAPI_FUNC(PyObject *) |
| 1507 | Py_CompileString(const char *str, const char *p, int s) |
| 1508 | { |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1509 | return Py_CompileStringExFlags(str, p, s, NULL, -1); |
| 1510 | } |
| 1511 | |
| 1512 | #undef Py_CompileStringFlags |
| 1513 | PyAPI_FUNC(PyObject *) |
| 1514 | Py_CompileStringFlags(const char *str, const char *p, int s, |
| 1515 | PyCompilerFlags *flags) |
| 1516 | { |
| 1517 | return Py_CompileStringExFlags(str, p, s, flags, -1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | #undef PyRun_InteractiveOne |
| 1521 | PyAPI_FUNC(int) |
| 1522 | PyRun_InteractiveOne(FILE *f, const char *p) |
| 1523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | return PyRun_InteractiveOneFlags(f, p, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | #undef PyRun_InteractiveLoop |
| 1528 | PyAPI_FUNC(int) |
| 1529 | PyRun_InteractiveLoop(FILE *f, const char *p) |
| 1530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | return PyRun_InteractiveLoopFlags(f, p, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | #ifdef __cplusplus |
| 1535 | } |
| 1536 | #endif |