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