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