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