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