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