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