Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
| 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 15 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 18 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 19 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
| 23 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 24 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 25 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 27 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 28 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | #define LLTRACE 1 /* Low-level trace feature */ |
| 30 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 33 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 35 | /* Forward declarations */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 36 | static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 37 | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 38 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 40 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 41 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 42 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 43 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 44 | static int call_trace(Py_tracefunc, PyObject *, |
| 45 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 47 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 48 | PyThreadState *, PyFrameObject *, |
| 49 | int, PyObject *); |
| 50 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 51 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 52 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 53 | PyThreadState *, PyFrameObject *, int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 54 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 55 | static void dtrace_function_entry(PyFrameObject *); |
| 56 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 58 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 59 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 60 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 61 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 62 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 63 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 64 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 65 | PyFrameObject *, const _Py_CODEUNIT *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 66 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 67 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 68 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 70 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 72 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | "free variable '%.200s' referenced before assignment" \ |
| 74 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 76 | /* Dynamic execution profile */ |
| 77 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 78 | #ifdef DXPAIRS |
| 79 | static long dxpairs[257][256]; |
| 80 | #define dxp dxpairs[256] |
| 81 | #else |
| 82 | static long dxp[256]; |
| 83 | #endif |
| 84 | #endif |
| 85 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 86 | /* Function call profile */ |
| 87 | #ifdef CALL_PROFILE |
| 88 | #define PCALL_NUM 11 |
| 89 | static int pcall[PCALL_NUM]; |
| 90 | |
| 91 | #define PCALL_ALL 0 |
| 92 | #define PCALL_FUNCTION 1 |
| 93 | #define PCALL_FAST_FUNCTION 2 |
| 94 | #define PCALL_FASTER_FUNCTION 3 |
| 95 | #define PCALL_METHOD 4 |
| 96 | #define PCALL_BOUND_METHOD 5 |
| 97 | #define PCALL_CFUNCTION 6 |
| 98 | #define PCALL_TYPE 7 |
| 99 | #define PCALL_GENERATOR 8 |
| 100 | #define PCALL_OTHER 9 |
| 101 | #define PCALL_POP 10 |
| 102 | |
| 103 | /* Notes about the statistics |
| 104 | |
| 105 | PCALL_FAST stats |
| 106 | |
| 107 | FAST_FUNCTION means no argument tuple needs to be created. |
| 108 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 109 | |
| 110 | If there is a method call where the call can be optimized by changing |
| 111 | the argument tuple and calling the function directly, it gets recorded |
| 112 | twice. |
| 113 | |
| 114 | As a result, the relationship among the statistics appears to be |
| 115 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 116 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 117 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 118 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 119 | */ |
| 120 | |
| 121 | #define PCALL(POS) pcall[POS]++ |
| 122 | |
| 123 | PyObject * |
| 124 | PyEval_GetCallStats(PyObject *self) |
| 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | return Py_BuildValue("iiiiiiiiiii", |
| 127 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 128 | pcall[4], pcall[5], pcall[6], pcall[7], |
| 129 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 130 | } |
| 131 | #else |
| 132 | #define PCALL(O) |
| 133 | |
| 134 | PyObject * |
| 135 | PyEval_GetCallStats(PyObject *self) |
| 136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | Py_INCREF(Py_None); |
| 138 | return Py_None; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 139 | } |
| 140 | #endif |
| 141 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 142 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 143 | #ifdef WITH_THREAD |
| 144 | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
| 145 | #else |
| 146 | #define GIL_REQUEST 0 |
| 147 | #endif |
| 148 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 149 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 150 | 1. We believe this is all right because the eval loop will release |
| 151 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 152 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | _Py_atomic_store_relaxed( \ |
| 154 | &eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 155 | GIL_REQUEST | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
| 157 | pending_async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 158 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 159 | #ifdef WITH_THREAD |
| 160 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 161 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | do { \ |
| 163 | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
| 164 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 165 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 166 | |
| 167 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | do { \ |
| 169 | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
| 170 | COMPUTE_EVAL_BREAKER(); \ |
| 171 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 172 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 175 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 176 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | do { \ |
| 178 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
| 179 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 180 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 181 | |
| 182 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | do { \ |
| 184 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
| 185 | COMPUTE_EVAL_BREAKER(); \ |
| 186 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 187 | |
| 188 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | do { \ |
| 190 | pending_async_exc = 1; \ |
| 191 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 192 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 193 | |
| 194 | #define UNSIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 196 | |
| 197 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 198 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 199 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 200 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 201 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 202 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 203 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 204 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 205 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 206 | static long main_thread = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 207 | /* This single variable consolidates all requests to break out of the fast path |
| 208 | in the eval loop. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 209 | static _Py_atomic_int eval_breaker = {0}; |
| 210 | /* Request for dropping the GIL */ |
| 211 | static _Py_atomic_int gil_drop_request = {0}; |
| 212 | /* Request for running pending calls. */ |
| 213 | static _Py_atomic_int pendingcalls_to_do = {0}; |
| 214 | /* Request for looking at the `async_exc` field of the current thread state. |
| 215 | Guarded by the GIL. */ |
| 216 | static int pending_async_exc = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 217 | |
| 218 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 219 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 220 | int |
| 221 | PyEval_ThreadsInitialized(void) |
| 222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 226 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 227 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | if (gil_created()) |
| 230 | return; |
| 231 | create_gil(); |
| 232 | take_gil(PyThreadState_GET()); |
| 233 | main_thread = PyThread_get_thread_ident(); |
| 234 | if (!pending_lock) |
| 235 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 236 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 238 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 239 | _PyEval_FiniThreads(void) |
| 240 | { |
| 241 | if (!gil_created()) |
| 242 | return; |
| 243 | destroy_gil(); |
| 244 | assert(!gil_created()); |
| 245 | } |
| 246 | |
| 247 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 248 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | PyThreadState *tstate = PyThreadState_GET(); |
| 251 | if (tstate == NULL) |
| 252 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 253 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 257 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | /* This function must succeed when the current thread state is NULL. |
| 260 | We therefore avoid PyThreadState_GET() which dumps a fatal error |
| 261 | in debug mode. |
| 262 | */ |
| 263 | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
| 264 | &_PyThreadState_Current)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 268 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | if (tstate == NULL) |
| 271 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 272 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 273 | assert(gil_created()); |
| 274 | take_gil(tstate); |
| 275 | if (PyThreadState_Swap(tstate) != NULL) |
| 276 | Py_FatalError( |
| 277 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 281 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | if (tstate == NULL) |
| 284 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 285 | if (PyThreadState_Swap(NULL) != tstate) |
| 286 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 287 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 288 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 290 | /* This function is called from PyOS_AfterFork to destroy all threads which are |
| 291 | * not running in the child process, and clear internal locks which might be |
| 292 | * held by those threads. (This could also be done using pthread_atfork |
| 293 | * mechanism, at least for the pthreads implementation.) */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 294 | |
| 295 | void |
| 296 | PyEval_ReInitThreads(void) |
| 297 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 298 | _Py_IDENTIFIER(_after_fork); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyObject *threading, *result; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 300 | PyThreadState *current_tstate = PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | if (!gil_created()) |
| 303 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | recreate_gil(); |
| 305 | pending_lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 306 | take_gil(current_tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | /* Update the threading module with the new state. |
| 310 | */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 311 | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | "threading"); |
| 313 | if (threading == NULL) { |
| 314 | /* threading not imported */ |
| 315 | PyErr_Clear(); |
| 316 | return; |
| 317 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 318 | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | if (result == NULL) |
| 320 | PyErr_WriteUnraisable(threading); |
| 321 | else |
| 322 | Py_DECREF(result); |
| 323 | Py_DECREF(threading); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 324 | |
| 325 | /* Destroy all threads except the current one */ |
| 326 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 327 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 328 | |
| 329 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 330 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 331 | static int pending_async_exc = 0; |
| 332 | #endif /* WITH_THREAD */ |
| 333 | |
| 334 | /* This function is used to signal that async exceptions are waiting to be |
| 335 | raised, therefore it is also useful in non-threaded builds. */ |
| 336 | |
| 337 | void |
| 338 | _PyEval_SignalAsyncExc(void) |
| 339 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 341 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 343 | /* Functions save_thread and restore_thread are always defined so |
| 344 | dynamically loaded modules needn't be compiled separately for use |
| 345 | with and without threads: */ |
| 346 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 347 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 348 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 351 | if (tstate == NULL) |
| 352 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 353 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | if (gil_created()) |
| 355 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 356 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 361 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | if (tstate == NULL) |
| 364 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 365 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (gil_created()) { |
| 367 | int err = errno; |
| 368 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 369 | /* _Py_Finalizing is protected by the GIL */ |
| 370 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 371 | drop_gil(tstate); |
| 372 | PyThread_exit_thread(); |
| 373 | assert(0); /* unreachable */ |
| 374 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | errno = err; |
| 376 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 377 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 382 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 383 | signal handlers or Mac I/O completion routines) can schedule calls |
| 384 | to a function to be called synchronously. |
| 385 | The synchronous function is called with one void* argument. |
| 386 | It should return 0 for success or -1 for failure -- failure should |
| 387 | be accompanied by an exception. |
| 388 | |
| 389 | If registry succeeds, the registry function returns 0; if it fails |
| 390 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 391 | an exception condition). |
| 392 | |
| 393 | Note that because registry may occur from within signal handlers, |
| 394 | or other asynchronous events, calling malloc() is unsafe! |
| 395 | |
| 396 | #ifdef WITH_THREAD |
| 397 | Any thread can schedule pending calls, but only the main thread |
| 398 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 399 | There is no facility to schedule calls to a particular thread, but |
| 400 | that should be easy to change, should that ever be required. In |
| 401 | that case, the static variables here should go into the python |
| 402 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 403 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 404 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 405 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 406 | #ifdef WITH_THREAD |
| 407 | |
| 408 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 409 | scheduling to be made from any thread, and even from an executing |
| 410 | callback. |
| 411 | */ |
| 412 | |
| 413 | #define NPENDINGCALLS 32 |
| 414 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | int (*func)(void *); |
| 416 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 417 | } pendingcalls[NPENDINGCALLS]; |
| 418 | static int pendingfirst = 0; |
| 419 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 420 | |
| 421 | int |
| 422 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | int i, j, result=0; |
| 425 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | /* try a few times for the lock. Since this mechanism is used |
| 428 | * for signal handling (on the main thread), there is a (slim) |
| 429 | * chance that a signal is delivered on the same thread while we |
| 430 | * hold the lock during the Py_MakePendingCalls() function. |
| 431 | * This avoids a deadlock in that case. |
| 432 | * Note that signals can be delivered on any thread. In particular, |
| 433 | * on Windows, a SIGINT is delivered on a system-created worker |
| 434 | * thread. |
| 435 | * We also check for lock being NULL, in the unlikely case that |
| 436 | * this function is called before any bytecode evaluation takes place. |
| 437 | */ |
| 438 | if (lock != NULL) { |
| 439 | for (i = 0; i<100; i++) { |
| 440 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 441 | break; |
| 442 | } |
| 443 | if (i == 100) |
| 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | i = pendinglast; |
| 448 | j = (i + 1) % NPENDINGCALLS; |
| 449 | if (j == pendingfirst) { |
| 450 | result = -1; /* Queue full */ |
| 451 | } else { |
| 452 | pendingcalls[i].func = func; |
| 453 | pendingcalls[i].arg = arg; |
| 454 | pendinglast = j; |
| 455 | } |
| 456 | /* signal main loop */ |
| 457 | SIGNAL_PENDING_CALLS(); |
| 458 | if (lock != NULL) |
| 459 | PyThread_release_lock(lock); |
| 460 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | int |
| 464 | Py_MakePendingCalls(void) |
| 465 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 466 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | int i; |
| 468 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | if (!pending_lock) { |
| 471 | /* initial allocation of the lock */ |
| 472 | pending_lock = PyThread_allocate_lock(); |
| 473 | if (pending_lock == NULL) |
| 474 | return -1; |
| 475 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | /* only service pending calls on main thread */ |
| 478 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 479 | return 0; |
| 480 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 481 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 483 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | /* perform a bounded number of calls, in case of recursion */ |
| 485 | for (i=0; i<NPENDINGCALLS; i++) { |
| 486 | int j; |
| 487 | int (*func)(void *); |
| 488 | void *arg = NULL; |
| 489 | |
| 490 | /* pop one item off the queue while holding the lock */ |
| 491 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 492 | j = pendingfirst; |
| 493 | if (j == pendinglast) { |
| 494 | func = NULL; /* Queue empty */ |
| 495 | } else { |
| 496 | func = pendingcalls[j].func; |
| 497 | arg = pendingcalls[j].arg; |
| 498 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 499 | } |
| 500 | if (pendingfirst != pendinglast) |
| 501 | SIGNAL_PENDING_CALLS(); |
| 502 | else |
| 503 | UNSIGNAL_PENDING_CALLS(); |
| 504 | PyThread_release_lock(pending_lock); |
| 505 | /* having released the lock, perform the callback */ |
| 506 | if (func == NULL) |
| 507 | break; |
| 508 | r = func(arg); |
| 509 | if (r) |
| 510 | break; |
| 511 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 512 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | #else /* if ! defined WITH_THREAD */ |
| 517 | |
| 518 | /* |
| 519 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 520 | This code is used for signal handling in python that isn't built |
| 521 | with WITH_THREAD. |
| 522 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 523 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 525 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 526 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 527 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 529 | (1) is very unlikely because typically signal delivery |
| 530 | is blocked during signal handling. So it should be impossible. |
| 531 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 532 | The current code is safe against (2), but not against (1). |
| 533 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 534 | thread is present, interrupted by signals, and that the critical |
| 535 | section is protected with the "busy" variable. On Windows, which |
| 536 | delivers SIGINT on a system thread, this does not hold and therefore |
| 537 | Windows really shouldn't use this version. |
| 538 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 539 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 541 | #define NPENDINGCALLS 32 |
| 542 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | int (*func)(void *); |
| 544 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 545 | } pendingcalls[NPENDINGCALLS]; |
| 546 | static volatile int pendingfirst = 0; |
| 547 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 548 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 549 | |
| 550 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 551 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | static volatile int busy = 0; |
| 554 | int i, j; |
| 555 | /* XXX Begin critical section */ |
| 556 | if (busy) |
| 557 | return -1; |
| 558 | busy = 1; |
| 559 | i = pendinglast; |
| 560 | j = (i + 1) % NPENDINGCALLS; |
| 561 | if (j == pendingfirst) { |
| 562 | busy = 0; |
| 563 | return -1; /* Queue full */ |
| 564 | } |
| 565 | pendingcalls[i].func = func; |
| 566 | pendingcalls[i].arg = arg; |
| 567 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | SIGNAL_PENDING_CALLS(); |
| 570 | busy = 0; |
| 571 | /* XXX End critical section */ |
| 572 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 575 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 576 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | static int busy = 0; |
| 579 | if (busy) |
| 580 | return 0; |
| 581 | busy = 1; |
| 582 | UNSIGNAL_PENDING_CALLS(); |
| 583 | for (;;) { |
| 584 | int i; |
| 585 | int (*func)(void *); |
| 586 | void *arg; |
| 587 | i = pendingfirst; |
| 588 | if (i == pendinglast) |
| 589 | break; /* Queue empty */ |
| 590 | func = pendingcalls[i].func; |
| 591 | arg = pendingcalls[i].arg; |
| 592 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 593 | if (func(arg) < 0) { |
| 594 | busy = 0; |
| 595 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 596 | return -1; |
| 597 | } |
| 598 | } |
| 599 | busy = 0; |
| 600 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 603 | #endif /* WITH_THREAD */ |
| 604 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 605 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 606 | /* The interpreter's recursion limit */ |
| 607 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 608 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 609 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 610 | #endif |
| 611 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 612 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 613 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 614 | int |
| 615 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 620 | void |
| 621 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | recursion_limit = new_limit; |
| 624 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 627 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 628 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 629 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 630 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 631 | Without USE_STACKCHECK, there is no need for this. */ |
| 632 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 633 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 636 | |
| 637 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | if (PyOS_CheckStack()) { |
| 639 | --tstate->recursion_depth; |
| 640 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 641 | return -1; |
| 642 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 643 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | _Py_CheckRecursionLimit = recursion_limit; |
| 645 | if (tstate->recursion_critical) |
| 646 | /* Somebody asked that we don't check for recursion. */ |
| 647 | return 0; |
| 648 | if (tstate->overflowed) { |
| 649 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 650 | /* Overflowing while handling an overflow. Give up. */ |
| 651 | Py_FatalError("Cannot recover from stack overflow."); |
| 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | if (tstate->recursion_depth > recursion_limit) { |
| 656 | --tstate->recursion_depth; |
| 657 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 658 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | "maximum recursion depth exceeded%s", |
| 660 | where); |
| 661 | return -1; |
| 662 | } |
| 663 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 666 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 667 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 668 | WHY_NOT = 0x0001, /* No error */ |
| 669 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 670 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 671 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 672 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 673 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 674 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 675 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 676 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 677 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 678 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 679 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 680 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 681 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 682 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 683 | /* Records whether tracing is on for any thread. Counts the number of |
| 684 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 685 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 686 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 687 | fast_next_opcode*/ |
| 688 | static int _Py_TracingPossible = 0; |
| 689 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 690 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 691 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 692 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 693 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | return PyEval_EvalCodeEx(co, |
| 696 | globals, locals, |
| 697 | (PyObject **)NULL, 0, |
| 698 | (PyObject **)NULL, 0, |
| 699 | (PyObject **)NULL, 0, |
| 700 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | |
| 704 | /* Interpreter main loop */ |
| 705 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 706 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 707 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | /* This is for backward compatibility with extension modules that |
| 709 | used this API; core interpreter code should call |
| 710 | PyEval_EvalFrameEx() */ |
| 711 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 715 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 716 | { |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 717 | PyThreadState *tstate = PyThreadState_GET(); |
| 718 | return tstate->interp->eval_frame(f, throwflag); |
| 719 | } |
| 720 | |
| 721 | PyObject * |
| 722 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 723 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 724 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 726 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 727 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 728 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 729 | int opcode; /* Current opcode */ |
| 730 | int oparg; /* Current opcode argument, if any */ |
| 731 | enum why_code why; /* Reason for block stack unwind */ |
| 732 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | PyObject *retval = NULL; /* Return value */ |
| 734 | PyThreadState *tstate = PyThreadState_GET(); |
| 735 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | is true when the line being executed has changed. The |
| 742 | initial values are such as to make this false the first |
| 743 | time it is tested. */ |
| 744 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 745 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 746 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | PyObject *names; |
| 748 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 749 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 750 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 751 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 752 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 753 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 754 | /* Computed GOTOs, or |
| 755 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 756 | using gcc's labels-as-values extension |
| 757 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 758 | |
| 759 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 761 | combined with a lookup table of jump addresses. However, since the |
| 762 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 763 | hard time making the right prediction for where to jump next (actually, |
| 764 | it will be always wrong except in the uncommon case of a sequence of |
| 765 | several identical opcodes). |
| 766 | |
| 767 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 768 | indirect jump instruction at the end of each opcode. Since the jump |
| 769 | instruction is at a different address for each opcode, the CPU will make a |
| 770 | separate prediction for each of these instructions, which is equivalent to |
| 771 | predicting the second opcode of each opcode pair. These predictions have |
| 772 | a much better chance to turn out valid, especially in small bytecode loops. |
| 773 | |
| 774 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 776 | and potentially many more instructions (depending on the pipeline width). |
| 777 | A correctly predicted branch, however, is nearly free. |
| 778 | |
| 779 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 780 | faster than the normal "switch" version, depending on the compiler and the |
| 781 | CPU architecture. |
| 782 | |
| 783 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 784 | because it would render the measurements invalid. |
| 785 | |
| 786 | |
| 787 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 788 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 789 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 790 | -fno-crossjumping). |
| 791 | */ |
| 792 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 793 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 794 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 795 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 796 | #endif |
| 797 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 798 | #ifdef HAVE_COMPUTED_GOTOS |
| 799 | #ifndef USE_COMPUTED_GOTOS |
| 800 | #define USE_COMPUTED_GOTOS 1 |
| 801 | #endif |
| 802 | #else |
| 803 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 804 | #error "Computed gotos are not supported on this compiler." |
| 805 | #endif |
| 806 | #undef USE_COMPUTED_GOTOS |
| 807 | #define USE_COMPUTED_GOTOS 0 |
| 808 | #endif |
| 809 | |
| 810 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 811 | /* Import the static jump table */ |
| 812 | #include "opcode_targets.h" |
| 813 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 814 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | TARGET_##op: \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | case op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 818 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | { \ |
| 820 | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
| 821 | FAST_DISPATCH(); \ |
| 822 | } \ |
| 823 | continue; \ |
| 824 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 825 | |
| 826 | #ifdef LLTRACE |
| 827 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 829 | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 831 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 832 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | } \ |
| 834 | goto fast_next_opcode; \ |
| 835 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 836 | #else |
| 837 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 839 | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 841 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 842 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | } \ |
| 844 | goto fast_next_opcode; \ |
| 845 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 846 | #endif |
| 847 | |
| 848 | #else |
| 849 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | case op: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 851 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 852 | #define DISPATCH() continue |
| 853 | #define FAST_DISPATCH() goto fast_next_opcode |
| 854 | #endif |
| 855 | |
| 856 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 857 | /* Tuple access macros */ |
| 858 | |
| 859 | #ifndef Py_DEBUG |
| 860 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 861 | #else |
| 862 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 863 | #endif |
| 864 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 865 | /* Code access macros */ |
| 866 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 867 | /* The integer overflow is checked by an assertion below. */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 868 | #define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 869 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 870 | _Py_CODEUNIT word = *next_instr; \ |
| 871 | opcode = _Py_OPCODE(word); \ |
| 872 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 873 | next_instr++; \ |
| 874 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 875 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 876 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 877 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 878 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | Some opcodes tend to come in pairs thus making it possible to |
| 880 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 881 | COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | Verifying the prediction costs a single high-speed test of a register |
| 884 | variable against a constant. If the pairing was good, then the |
| 885 | processor's own internal branch predication has a high likelihood of |
| 886 | success, resulting in a nearly zero-overhead transition to the |
| 887 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 888 | including its unpredictable switch-case branch. Combined with the |
| 889 | processor's internal branch prediction, a successful PREDICT has the |
| 890 | effect of making the two opcodes run as if they were a single new opcode |
| 891 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 892 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 893 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | predictions turned-on and interpret the results as if some opcodes |
| 895 | had been combined or turn-off predictions so that the opcode frequency |
| 896 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 897 | |
| 898 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | the CPU to record separate branch prediction information for each |
| 900 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 901 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 902 | */ |
| 903 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 904 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 906 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 907 | #define PREDICT(op) \ |
| 908 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 909 | _Py_CODEUNIT word = *next_instr; \ |
| 910 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 911 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 912 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 913 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 914 | goto PRED_##op; \ |
| 915 | } \ |
| 916 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 917 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 918 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 919 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 921 | /* Stack manipulation macros */ |
| 922 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 923 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 924 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 925 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 926 | #define EMPTY() (STACK_LEVEL() == 0) |
| 927 | #define TOP() (stack_pointer[-1]) |
| 928 | #define SECOND() (stack_pointer[-2]) |
| 929 | #define THIRD() (stack_pointer[-3]) |
| 930 | #define FOURTH() (stack_pointer[-4]) |
| 931 | #define PEEK(n) (stack_pointer[-(n)]) |
| 932 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 933 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 934 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 935 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 936 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 937 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 938 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 939 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 941 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 943 | lltrace && prtrace(TOP(), "push")); \ |
| 944 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 946 | BASIC_POP()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 948 | lltrace && prtrace(TOP(), "stackadj")); \ |
| 949 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 950 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 951 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 952 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 953 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 954 | #define PUSH(v) BASIC_PUSH(v) |
| 955 | #define POP() BASIC_POP() |
| 956 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 957 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 958 | #endif |
| 959 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 960 | /* Local variable macros */ |
| 961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 963 | |
| 964 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 965 | then store the new value; it must copy the old value to a temporary |
| 966 | value, then store the new value, and then DECREF the temporary value. |
| 967 | This is because it is possible that during the DECREF the frame is |
| 968 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 969 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 971 | GETLOCAL(i) = value; \ |
| 972 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 973 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 974 | |
| 975 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 977 | PyObject *v = POP(); \ |
| 978 | Py_XDECREF(v); \ |
| 979 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 980 | |
| 981 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 982 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | PyObject *type, *value, *traceback; \ |
| 984 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 985 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 986 | value = POP(); \ |
| 987 | Py_XDECREF(value); \ |
| 988 | } \ |
| 989 | type = tstate->exc_type; \ |
| 990 | value = tstate->exc_value; \ |
| 991 | traceback = tstate->exc_traceback; \ |
| 992 | tstate->exc_type = POP(); \ |
| 993 | tstate->exc_value = POP(); \ |
| 994 | tstate->exc_traceback = POP(); \ |
| 995 | Py_XDECREF(type); \ |
| 996 | Py_XDECREF(value); \ |
| 997 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 998 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 999 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1000 | /* Start of code */ |
| 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | /* push frame */ |
| 1003 | if (Py_EnterRecursiveCall("")) |
| 1004 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | if (tstate->use_tracing) { |
| 1009 | if (tstate->c_tracefunc != NULL) { |
| 1010 | /* tstate->c_tracefunc, if defined, is a |
| 1011 | function that will be called on *every* entry |
| 1012 | to a code block. Its return value, if not |
| 1013 | None, is a function that will be called at |
| 1014 | the start of each executed line of code. |
| 1015 | (Actually, the function must return itself |
| 1016 | in order to continue tracing.) The trace |
| 1017 | functions are called with three arguments: |
| 1018 | a pointer to the current frame, a string |
| 1019 | indicating why the function is called, and |
| 1020 | an argument which depends on the situation. |
| 1021 | The global trace function is also called |
| 1022 | whenever an exception is detected. */ |
| 1023 | if (call_trace_protected(tstate->c_tracefunc, |
| 1024 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1025 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | /* Trace function raised an error */ |
| 1027 | goto exit_eval_frame; |
| 1028 | } |
| 1029 | } |
| 1030 | if (tstate->c_profilefunc != NULL) { |
| 1031 | /* Similar for c_profilefunc, except it needn't |
| 1032 | return itself and isn't called for "line" events */ |
| 1033 | if (call_trace_protected(tstate->c_profilefunc, |
| 1034 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1035 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | /* Profile function raised an error */ |
| 1037 | goto exit_eval_frame; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1041 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1042 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 1043 | dtrace_function_entry(f); |
| 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | co = f->f_code; |
| 1046 | names = co->co_names; |
| 1047 | consts = co->co_consts; |
| 1048 | fastlocals = f->f_localsplus; |
| 1049 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1050 | assert(PyBytes_Check(co->co_code)); |
| 1051 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1052 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 1053 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1054 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1055 | /* |
| 1056 | f->f_lasti refers to the index of the last instruction, |
| 1057 | unless it's -1 in which case next_instr should be first_instr. |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1058 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1059 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1060 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1063 | direct succession without updating f->f_lasti. A successful |
| 1064 | prediction effectively links the two codes together as if they |
| 1065 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1066 | the first code in the pair (for instance, GET_ITER followed by |
| 1067 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1068 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1070 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1071 | next_instr = first_instr; |
| 1072 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1073 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1074 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1075 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | stack_pointer = f->f_stacktop; |
| 1077 | assert(stack_pointer != NULL); |
| 1078 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1079 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1080 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1081 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 1082 | if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | /* We were in an except handler when we left, |
| 1084 | restore the exception state which was put aside |
| 1085 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1086 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1088 | else |
| 1089 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1091 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1092 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1093 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1094 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | why = WHY_NOT; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1097 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1098 | if (throwflag) /* support for generator.throw() */ |
| 1099 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1100 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1101 | #ifdef Py_DEBUG |
| 1102 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
| 1103 | because it may clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1104 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1105 | assert(!PyErr_Occurred()); |
| 1106 | #endif |
| 1107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1110 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1111 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | /* Do periodic things. Doing this every time through |
| 1114 | the loop would add too much overhead, so we do it |
| 1115 | only every Nth instruction. We also do it if |
| 1116 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1117 | event needs attention (e.g. a signal handler or |
| 1118 | async I/O handler); see Py_AddPendingCall() and |
| 1119 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1122 | if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1124 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | goto fast_next_opcode; |
| 1126 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1128 | if (Py_MakePendingCalls() < 0) |
| 1129 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1131 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1132 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | /* Give another thread a chance */ |
| 1134 | if (PyThreadState_Swap(NULL) != tstate) |
| 1135 | Py_FatalError("ceval: tstate mix-up"); |
| 1136 | drop_gil(tstate); |
| 1137 | |
| 1138 | /* Other threads may run now */ |
| 1139 | |
| 1140 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1141 | |
| 1142 | /* Check if we should make a quick exit. */ |
| 1143 | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
| 1144 | drop_gil(tstate); |
| 1145 | PyThread_exit_thread(); |
| 1146 | } |
| 1147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | if (PyThreadState_Swap(tstate) != NULL) |
| 1149 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1151 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | /* Check for asynchronous exceptions. */ |
| 1153 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1154 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | tstate->async_exc = NULL; |
| 1156 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1157 | PyErr_SetNone(exc); |
| 1158 | Py_DECREF(exc); |
| 1159 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | } |
| 1161 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | fast_next_opcode: |
| 1164 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1165 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1166 | if (PyDTrace_LINE_ENABLED()) |
| 1167 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1172 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1173 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | /* see maybe_call_line_trace |
| 1175 | for expository comments */ |
| 1176 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1179 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1180 | tstate, f, |
| 1181 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | /* Reload possibly changed frame fields */ |
| 1183 | JUMPTO(f->f_lasti); |
| 1184 | if (f->f_stacktop != NULL) { |
| 1185 | stack_pointer = f->f_stacktop; |
| 1186 | f->f_stacktop = NULL; |
| 1187 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1188 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1190 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1194 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1195 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1196 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1197 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1198 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | dxpairs[lastopcode][opcode]++; |
| 1200 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1201 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1203 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1204 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1205 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if (lltrace) { |
| 1209 | if (HAS_ARG(opcode)) { |
| 1210 | printf("%d: %d, %d\n", |
| 1211 | f->f_lasti, opcode, oparg); |
| 1212 | } |
| 1213 | else { |
| 1214 | printf("%d: %d\n", |
| 1215 | f->f_lasti, opcode); |
| 1216 | } |
| 1217 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1218 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | /* BEWARE! |
| 1223 | It is essential that any operation that fails sets either |
| 1224 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1225 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | TARGET(NOP) |
| 1228 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1229 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1230 | TARGET(LOAD_FAST) { |
| 1231 | PyObject *value = GETLOCAL(oparg); |
| 1232 | if (value == NULL) { |
| 1233 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1234 | UNBOUNDLOCAL_ERROR_MSG, |
| 1235 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1236 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1238 | Py_INCREF(value); |
| 1239 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1241 | } |
| 1242 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1243 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1244 | TARGET(LOAD_CONST) { |
| 1245 | PyObject *value = GETITEM(consts, oparg); |
| 1246 | Py_INCREF(value); |
| 1247 | PUSH(value); |
| 1248 | FAST_DISPATCH(); |
| 1249 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1250 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1251 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1252 | TARGET(STORE_FAST) { |
| 1253 | PyObject *value = POP(); |
| 1254 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1256 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1257 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1258 | TARGET(POP_TOP) { |
| 1259 | PyObject *value = POP(); |
| 1260 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1262 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1263 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1264 | TARGET(ROT_TWO) { |
| 1265 | PyObject *top = TOP(); |
| 1266 | PyObject *second = SECOND(); |
| 1267 | SET_TOP(second); |
| 1268 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1270 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1271 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1272 | TARGET(ROT_THREE) { |
| 1273 | PyObject *top = TOP(); |
| 1274 | PyObject *second = SECOND(); |
| 1275 | PyObject *third = THIRD(); |
| 1276 | SET_TOP(second); |
| 1277 | SET_SECOND(third); |
| 1278 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1280 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1281 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1282 | TARGET(DUP_TOP) { |
| 1283 | PyObject *top = TOP(); |
| 1284 | Py_INCREF(top); |
| 1285 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1287 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1288 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1289 | TARGET(DUP_TOP_TWO) { |
| 1290 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1291 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1292 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1293 | Py_INCREF(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1294 | STACKADJ(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1295 | SET_TOP(top); |
| 1296 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1297 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1298 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1299 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1300 | TARGET(UNARY_POSITIVE) { |
| 1301 | PyObject *value = TOP(); |
| 1302 | PyObject *res = PyNumber_Positive(value); |
| 1303 | Py_DECREF(value); |
| 1304 | SET_TOP(res); |
| 1305 | if (res == NULL) |
| 1306 | goto error; |
| 1307 | DISPATCH(); |
| 1308 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1309 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1310 | TARGET(UNARY_NEGATIVE) { |
| 1311 | PyObject *value = TOP(); |
| 1312 | PyObject *res = PyNumber_Negative(value); |
| 1313 | Py_DECREF(value); |
| 1314 | SET_TOP(res); |
| 1315 | if (res == NULL) |
| 1316 | goto error; |
| 1317 | DISPATCH(); |
| 1318 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1319 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1320 | TARGET(UNARY_NOT) { |
| 1321 | PyObject *value = TOP(); |
| 1322 | int err = PyObject_IsTrue(value); |
| 1323 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | if (err == 0) { |
| 1325 | Py_INCREF(Py_True); |
| 1326 | SET_TOP(Py_True); |
| 1327 | DISPATCH(); |
| 1328 | } |
| 1329 | else if (err > 0) { |
| 1330 | Py_INCREF(Py_False); |
| 1331 | SET_TOP(Py_False); |
| 1332 | err = 0; |
| 1333 | DISPATCH(); |
| 1334 | } |
| 1335 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1336 | goto error; |
| 1337 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1338 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1339 | TARGET(UNARY_INVERT) { |
| 1340 | PyObject *value = TOP(); |
| 1341 | PyObject *res = PyNumber_Invert(value); |
| 1342 | Py_DECREF(value); |
| 1343 | SET_TOP(res); |
| 1344 | if (res == NULL) |
| 1345 | goto error; |
| 1346 | DISPATCH(); |
| 1347 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1348 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1349 | TARGET(BINARY_POWER) { |
| 1350 | PyObject *exp = POP(); |
| 1351 | PyObject *base = TOP(); |
| 1352 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1353 | Py_DECREF(base); |
| 1354 | Py_DECREF(exp); |
| 1355 | SET_TOP(res); |
| 1356 | if (res == NULL) |
| 1357 | goto error; |
| 1358 | DISPATCH(); |
| 1359 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1360 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1361 | TARGET(BINARY_MULTIPLY) { |
| 1362 | PyObject *right = POP(); |
| 1363 | PyObject *left = TOP(); |
| 1364 | PyObject *res = PyNumber_Multiply(left, right); |
| 1365 | Py_DECREF(left); |
| 1366 | Py_DECREF(right); |
| 1367 | SET_TOP(res); |
| 1368 | if (res == NULL) |
| 1369 | goto error; |
| 1370 | DISPATCH(); |
| 1371 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1372 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1373 | TARGET(BINARY_MATRIX_MULTIPLY) { |
| 1374 | PyObject *right = POP(); |
| 1375 | PyObject *left = TOP(); |
| 1376 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1377 | Py_DECREF(left); |
| 1378 | Py_DECREF(right); |
| 1379 | SET_TOP(res); |
| 1380 | if (res == NULL) |
| 1381 | goto error; |
| 1382 | DISPATCH(); |
| 1383 | } |
| 1384 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1385 | TARGET(BINARY_TRUE_DIVIDE) { |
| 1386 | PyObject *divisor = POP(); |
| 1387 | PyObject *dividend = TOP(); |
| 1388 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1389 | Py_DECREF(dividend); |
| 1390 | Py_DECREF(divisor); |
| 1391 | SET_TOP(quotient); |
| 1392 | if (quotient == NULL) |
| 1393 | goto error; |
| 1394 | DISPATCH(); |
| 1395 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1396 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1397 | TARGET(BINARY_FLOOR_DIVIDE) { |
| 1398 | PyObject *divisor = POP(); |
| 1399 | PyObject *dividend = TOP(); |
| 1400 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1401 | Py_DECREF(dividend); |
| 1402 | Py_DECREF(divisor); |
| 1403 | SET_TOP(quotient); |
| 1404 | if (quotient == NULL) |
| 1405 | goto error; |
| 1406 | DISPATCH(); |
| 1407 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1408 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1409 | TARGET(BINARY_MODULO) { |
| 1410 | PyObject *divisor = POP(); |
| 1411 | PyObject *dividend = TOP(); |
| 1412 | PyObject *res = PyUnicode_CheckExact(dividend) ? |
| 1413 | PyUnicode_Format(dividend, divisor) : |
| 1414 | PyNumber_Remainder(dividend, divisor); |
| 1415 | Py_DECREF(divisor); |
| 1416 | Py_DECREF(dividend); |
| 1417 | SET_TOP(res); |
| 1418 | if (res == NULL) |
| 1419 | goto error; |
| 1420 | DISPATCH(); |
| 1421 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1422 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1423 | TARGET(BINARY_ADD) { |
| 1424 | PyObject *right = POP(); |
| 1425 | PyObject *left = TOP(); |
| 1426 | PyObject *sum; |
| 1427 | if (PyUnicode_CheckExact(left) && |
| 1428 | PyUnicode_CheckExact(right)) { |
| 1429 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1430 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1431 | } |
| 1432 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1433 | sum = PyNumber_Add(left, right); |
| 1434 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1435 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1436 | Py_DECREF(right); |
| 1437 | SET_TOP(sum); |
| 1438 | if (sum == NULL) |
| 1439 | goto error; |
| 1440 | DISPATCH(); |
| 1441 | } |
| 1442 | |
| 1443 | TARGET(BINARY_SUBTRACT) { |
| 1444 | PyObject *right = POP(); |
| 1445 | PyObject *left = TOP(); |
| 1446 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1447 | Py_DECREF(right); |
| 1448 | Py_DECREF(left); |
| 1449 | SET_TOP(diff); |
| 1450 | if (diff == NULL) |
| 1451 | goto error; |
| 1452 | DISPATCH(); |
| 1453 | } |
| 1454 | |
| 1455 | TARGET(BINARY_SUBSCR) { |
| 1456 | PyObject *sub = POP(); |
| 1457 | PyObject *container = TOP(); |
| 1458 | PyObject *res = PyObject_GetItem(container, sub); |
| 1459 | Py_DECREF(container); |
| 1460 | Py_DECREF(sub); |
| 1461 | SET_TOP(res); |
| 1462 | if (res == NULL) |
| 1463 | goto error; |
| 1464 | DISPATCH(); |
| 1465 | } |
| 1466 | |
| 1467 | TARGET(BINARY_LSHIFT) { |
| 1468 | PyObject *right = POP(); |
| 1469 | PyObject *left = TOP(); |
| 1470 | PyObject *res = PyNumber_Lshift(left, right); |
| 1471 | Py_DECREF(left); |
| 1472 | Py_DECREF(right); |
| 1473 | SET_TOP(res); |
| 1474 | if (res == NULL) |
| 1475 | goto error; |
| 1476 | DISPATCH(); |
| 1477 | } |
| 1478 | |
| 1479 | TARGET(BINARY_RSHIFT) { |
| 1480 | PyObject *right = POP(); |
| 1481 | PyObject *left = TOP(); |
| 1482 | PyObject *res = PyNumber_Rshift(left, right); |
| 1483 | Py_DECREF(left); |
| 1484 | Py_DECREF(right); |
| 1485 | SET_TOP(res); |
| 1486 | if (res == NULL) |
| 1487 | goto error; |
| 1488 | DISPATCH(); |
| 1489 | } |
| 1490 | |
| 1491 | TARGET(BINARY_AND) { |
| 1492 | PyObject *right = POP(); |
| 1493 | PyObject *left = TOP(); |
| 1494 | PyObject *res = PyNumber_And(left, right); |
| 1495 | Py_DECREF(left); |
| 1496 | Py_DECREF(right); |
| 1497 | SET_TOP(res); |
| 1498 | if (res == NULL) |
| 1499 | goto error; |
| 1500 | DISPATCH(); |
| 1501 | } |
| 1502 | |
| 1503 | TARGET(BINARY_XOR) { |
| 1504 | PyObject *right = POP(); |
| 1505 | PyObject *left = TOP(); |
| 1506 | PyObject *res = PyNumber_Xor(left, right); |
| 1507 | Py_DECREF(left); |
| 1508 | Py_DECREF(right); |
| 1509 | SET_TOP(res); |
| 1510 | if (res == NULL) |
| 1511 | goto error; |
| 1512 | DISPATCH(); |
| 1513 | } |
| 1514 | |
| 1515 | TARGET(BINARY_OR) { |
| 1516 | PyObject *right = POP(); |
| 1517 | PyObject *left = TOP(); |
| 1518 | PyObject *res = PyNumber_Or(left, right); |
| 1519 | Py_DECREF(left); |
| 1520 | Py_DECREF(right); |
| 1521 | SET_TOP(res); |
| 1522 | if (res == NULL) |
| 1523 | goto error; |
| 1524 | DISPATCH(); |
| 1525 | } |
| 1526 | |
| 1527 | TARGET(LIST_APPEND) { |
| 1528 | PyObject *v = POP(); |
| 1529 | PyObject *list = PEEK(oparg); |
| 1530 | int err; |
| 1531 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1533 | if (err != 0) |
| 1534 | goto error; |
| 1535 | PREDICT(JUMP_ABSOLUTE); |
| 1536 | DISPATCH(); |
| 1537 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1538 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1539 | TARGET(SET_ADD) { |
| 1540 | PyObject *v = POP(); |
| 1541 | PyObject *set = stack_pointer[-oparg]; |
| 1542 | int err; |
| 1543 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1545 | if (err != 0) |
| 1546 | goto error; |
| 1547 | PREDICT(JUMP_ABSOLUTE); |
| 1548 | DISPATCH(); |
| 1549 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1550 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1551 | TARGET(INPLACE_POWER) { |
| 1552 | PyObject *exp = POP(); |
| 1553 | PyObject *base = TOP(); |
| 1554 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1555 | Py_DECREF(base); |
| 1556 | Py_DECREF(exp); |
| 1557 | SET_TOP(res); |
| 1558 | if (res == NULL) |
| 1559 | goto error; |
| 1560 | DISPATCH(); |
| 1561 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1562 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1563 | TARGET(INPLACE_MULTIPLY) { |
| 1564 | PyObject *right = POP(); |
| 1565 | PyObject *left = TOP(); |
| 1566 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1567 | Py_DECREF(left); |
| 1568 | Py_DECREF(right); |
| 1569 | SET_TOP(res); |
| 1570 | if (res == NULL) |
| 1571 | goto error; |
| 1572 | DISPATCH(); |
| 1573 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1574 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1575 | TARGET(INPLACE_MATRIX_MULTIPLY) { |
| 1576 | PyObject *right = POP(); |
| 1577 | PyObject *left = TOP(); |
| 1578 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1579 | Py_DECREF(left); |
| 1580 | Py_DECREF(right); |
| 1581 | SET_TOP(res); |
| 1582 | if (res == NULL) |
| 1583 | goto error; |
| 1584 | DISPATCH(); |
| 1585 | } |
| 1586 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1587 | TARGET(INPLACE_TRUE_DIVIDE) { |
| 1588 | PyObject *divisor = POP(); |
| 1589 | PyObject *dividend = TOP(); |
| 1590 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1591 | Py_DECREF(dividend); |
| 1592 | Py_DECREF(divisor); |
| 1593 | SET_TOP(quotient); |
| 1594 | if (quotient == NULL) |
| 1595 | goto error; |
| 1596 | DISPATCH(); |
| 1597 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1598 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1599 | TARGET(INPLACE_FLOOR_DIVIDE) { |
| 1600 | PyObject *divisor = POP(); |
| 1601 | PyObject *dividend = TOP(); |
| 1602 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1603 | Py_DECREF(dividend); |
| 1604 | Py_DECREF(divisor); |
| 1605 | SET_TOP(quotient); |
| 1606 | if (quotient == NULL) |
| 1607 | goto error; |
| 1608 | DISPATCH(); |
| 1609 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1610 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1611 | TARGET(INPLACE_MODULO) { |
| 1612 | PyObject *right = POP(); |
| 1613 | PyObject *left = TOP(); |
| 1614 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1615 | Py_DECREF(left); |
| 1616 | Py_DECREF(right); |
| 1617 | SET_TOP(mod); |
| 1618 | if (mod == NULL) |
| 1619 | goto error; |
| 1620 | DISPATCH(); |
| 1621 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1622 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1623 | TARGET(INPLACE_ADD) { |
| 1624 | PyObject *right = POP(); |
| 1625 | PyObject *left = TOP(); |
| 1626 | PyObject *sum; |
| 1627 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1628 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1629 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1630 | } |
| 1631 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1632 | sum = PyNumber_InPlaceAdd(left, right); |
| 1633 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1634 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1635 | Py_DECREF(right); |
| 1636 | SET_TOP(sum); |
| 1637 | if (sum == NULL) |
| 1638 | goto error; |
| 1639 | DISPATCH(); |
| 1640 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1641 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1642 | TARGET(INPLACE_SUBTRACT) { |
| 1643 | PyObject *right = POP(); |
| 1644 | PyObject *left = TOP(); |
| 1645 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1646 | Py_DECREF(left); |
| 1647 | Py_DECREF(right); |
| 1648 | SET_TOP(diff); |
| 1649 | if (diff == NULL) |
| 1650 | goto error; |
| 1651 | DISPATCH(); |
| 1652 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1653 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1654 | TARGET(INPLACE_LSHIFT) { |
| 1655 | PyObject *right = POP(); |
| 1656 | PyObject *left = TOP(); |
| 1657 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1658 | Py_DECREF(left); |
| 1659 | Py_DECREF(right); |
| 1660 | SET_TOP(res); |
| 1661 | if (res == NULL) |
| 1662 | goto error; |
| 1663 | DISPATCH(); |
| 1664 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1665 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1666 | TARGET(INPLACE_RSHIFT) { |
| 1667 | PyObject *right = POP(); |
| 1668 | PyObject *left = TOP(); |
| 1669 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1670 | Py_DECREF(left); |
| 1671 | Py_DECREF(right); |
| 1672 | SET_TOP(res); |
| 1673 | if (res == NULL) |
| 1674 | goto error; |
| 1675 | DISPATCH(); |
| 1676 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1677 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1678 | TARGET(INPLACE_AND) { |
| 1679 | PyObject *right = POP(); |
| 1680 | PyObject *left = TOP(); |
| 1681 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1682 | Py_DECREF(left); |
| 1683 | Py_DECREF(right); |
| 1684 | SET_TOP(res); |
| 1685 | if (res == NULL) |
| 1686 | goto error; |
| 1687 | DISPATCH(); |
| 1688 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1689 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1690 | TARGET(INPLACE_XOR) { |
| 1691 | PyObject *right = POP(); |
| 1692 | PyObject *left = TOP(); |
| 1693 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1694 | Py_DECREF(left); |
| 1695 | Py_DECREF(right); |
| 1696 | SET_TOP(res); |
| 1697 | if (res == NULL) |
| 1698 | goto error; |
| 1699 | DISPATCH(); |
| 1700 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1701 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1702 | TARGET(INPLACE_OR) { |
| 1703 | PyObject *right = POP(); |
| 1704 | PyObject *left = TOP(); |
| 1705 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1706 | Py_DECREF(left); |
| 1707 | Py_DECREF(right); |
| 1708 | SET_TOP(res); |
| 1709 | if (res == NULL) |
| 1710 | goto error; |
| 1711 | DISPATCH(); |
| 1712 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1713 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1714 | TARGET(STORE_SUBSCR) { |
| 1715 | PyObject *sub = TOP(); |
| 1716 | PyObject *container = SECOND(); |
| 1717 | PyObject *v = THIRD(); |
| 1718 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | STACKADJ(-3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1720 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1721 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1723 | Py_DECREF(container); |
| 1724 | Py_DECREF(sub); |
| 1725 | if (err != 0) |
| 1726 | goto error; |
| 1727 | DISPATCH(); |
| 1728 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1729 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1730 | TARGET(STORE_ANNOTATION) { |
| 1731 | _Py_IDENTIFIER(__annotations__); |
| 1732 | PyObject *ann_dict; |
| 1733 | PyObject *ann = POP(); |
| 1734 | PyObject *name = GETITEM(names, oparg); |
| 1735 | int err; |
| 1736 | if (f->f_locals == NULL) { |
| 1737 | PyErr_Format(PyExc_SystemError, |
| 1738 | "no locals found when storing annotation"); |
| 1739 | Py_DECREF(ann); |
| 1740 | goto error; |
| 1741 | } |
| 1742 | /* first try to get __annotations__ from locals... */ |
| 1743 | if (PyDict_CheckExact(f->f_locals)) { |
| 1744 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 1745 | &PyId___annotations__); |
| 1746 | if (ann_dict == NULL) { |
| 1747 | PyErr_SetString(PyExc_NameError, |
| 1748 | "__annotations__ not found"); |
| 1749 | Py_DECREF(ann); |
| 1750 | goto error; |
| 1751 | } |
| 1752 | Py_INCREF(ann_dict); |
| 1753 | } |
| 1754 | else { |
| 1755 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 1756 | if (ann_str == NULL) { |
| 1757 | Py_DECREF(ann); |
| 1758 | goto error; |
| 1759 | } |
| 1760 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 1761 | if (ann_dict == NULL) { |
| 1762 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 1763 | PyErr_SetString(PyExc_NameError, |
| 1764 | "__annotations__ not found"); |
| 1765 | } |
| 1766 | Py_DECREF(ann); |
| 1767 | goto error; |
| 1768 | } |
| 1769 | } |
| 1770 | /* ...if succeeded, __annotations__[name] = ann */ |
| 1771 | if (PyDict_CheckExact(ann_dict)) { |
| 1772 | err = PyDict_SetItem(ann_dict, name, ann); |
| 1773 | } |
| 1774 | else { |
| 1775 | err = PyObject_SetItem(ann_dict, name, ann); |
| 1776 | } |
| 1777 | Py_DECREF(ann_dict); |
Yury Selivanov | 50c584f | 2016-09-08 23:38:21 -0700 | [diff] [blame] | 1778 | Py_DECREF(ann); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1779 | if (err != 0) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1780 | goto error; |
| 1781 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1782 | DISPATCH(); |
| 1783 | } |
| 1784 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1785 | TARGET(DELETE_SUBSCR) { |
| 1786 | PyObject *sub = TOP(); |
| 1787 | PyObject *container = SECOND(); |
| 1788 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | STACKADJ(-2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1790 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1791 | err = PyObject_DelItem(container, sub); |
| 1792 | Py_DECREF(container); |
| 1793 | Py_DECREF(sub); |
| 1794 | if (err != 0) |
| 1795 | goto error; |
| 1796 | DISPATCH(); |
| 1797 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1798 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1799 | TARGET(PRINT_EXPR) { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1800 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1801 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1802 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1803 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1804 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | PyErr_SetString(PyExc_RuntimeError, |
| 1806 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1807 | Py_DECREF(value); |
| 1808 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | } |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1810 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1811 | Py_DECREF(value); |
| 1812 | if (res == NULL) |
| 1813 | goto error; |
| 1814 | Py_DECREF(res); |
| 1815 | DISPATCH(); |
| 1816 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1817 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1818 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1820 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1821 | TARGET(RAISE_VARARGS) { |
| 1822 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | switch (oparg) { |
| 1824 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1825 | cause = POP(); /* cause */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1827 | exc = POP(); /* exc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | case 0: /* Fallthrough */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1829 | if (do_raise(exc, cause)) { |
| 1830 | why = WHY_EXCEPTION; |
| 1831 | goto fast_block_end; |
| 1832 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | break; |
| 1834 | default: |
| 1835 | PyErr_SetString(PyExc_SystemError, |
| 1836 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | break; |
| 1838 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1839 | goto error; |
| 1840 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1841 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1842 | TARGET(RETURN_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | retval = POP(); |
| 1844 | why = WHY_RETURN; |
| 1845 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1846 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1847 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1848 | TARGET(GET_AITER) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1849 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1850 | PyObject *iter = NULL; |
| 1851 | PyObject *awaitable = NULL; |
| 1852 | PyObject *obj = TOP(); |
| 1853 | PyTypeObject *type = Py_TYPE(obj); |
| 1854 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1855 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1856 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1857 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1858 | |
| 1859 | if (getter != NULL) { |
| 1860 | iter = (*getter)(obj); |
| 1861 | Py_DECREF(obj); |
| 1862 | if (iter == NULL) { |
| 1863 | SET_TOP(NULL); |
| 1864 | goto error; |
| 1865 | } |
| 1866 | } |
| 1867 | else { |
| 1868 | SET_TOP(NULL); |
| 1869 | PyErr_Format( |
| 1870 | PyExc_TypeError, |
| 1871 | "'async for' requires an object with " |
| 1872 | "__aiter__ method, got %.100s", |
| 1873 | type->tp_name); |
| 1874 | Py_DECREF(obj); |
| 1875 | goto error; |
| 1876 | } |
| 1877 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1878 | if (Py_TYPE(iter)->tp_as_async != NULL && |
| 1879 | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
| 1880 | |
| 1881 | /* Starting with CPython 3.5.2 __aiter__ should return |
| 1882 | asynchronous iterators directly (not awaitables that |
| 1883 | resolve to asynchronous iterators.) |
| 1884 | |
| 1885 | Therefore, we check if the object that was returned |
| 1886 | from __aiter__ has an __anext__ method. If it does, |
| 1887 | we wrap it in an awaitable that resolves to `iter`. |
| 1888 | |
| 1889 | See http://bugs.python.org/issue27243 for more |
| 1890 | details. |
| 1891 | */ |
| 1892 | |
| 1893 | PyObject *wrapper = _PyAIterWrapper_New(iter); |
| 1894 | Py_DECREF(iter); |
| 1895 | SET_TOP(wrapper); |
| 1896 | DISPATCH(); |
| 1897 | } |
| 1898 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1899 | awaitable = _PyCoro_GetAwaitableIter(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1900 | if (awaitable == NULL) { |
| 1901 | SET_TOP(NULL); |
| 1902 | PyErr_Format( |
| 1903 | PyExc_TypeError, |
| 1904 | "'async for' received an invalid object " |
| 1905 | "from __aiter__: %.100s", |
| 1906 | Py_TYPE(iter)->tp_name); |
| 1907 | |
| 1908 | Py_DECREF(iter); |
| 1909 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1910 | } else { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1911 | Py_DECREF(iter); |
| 1912 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1913 | if (PyErr_WarnFormat( |
| 1914 | PyExc_PendingDeprecationWarning, 1, |
| 1915 | "'%.100s' implements legacy __aiter__ protocol; " |
| 1916 | "__aiter__ should return an asynchronous " |
| 1917 | "iterator, not awaitable", |
| 1918 | type->tp_name)) |
| 1919 | { |
| 1920 | /* Warning was converted to an error. */ |
| 1921 | Py_DECREF(awaitable); |
| 1922 | SET_TOP(NULL); |
| 1923 | goto error; |
| 1924 | } |
| 1925 | } |
| 1926 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1927 | SET_TOP(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1928 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1929 | DISPATCH(); |
| 1930 | } |
| 1931 | |
| 1932 | TARGET(GET_ANEXT) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1933 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1934 | PyObject *next_iter = NULL; |
| 1935 | PyObject *awaitable = NULL; |
| 1936 | PyObject *aiter = TOP(); |
| 1937 | PyTypeObject *type = Py_TYPE(aiter); |
| 1938 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1939 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1940 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1941 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1942 | goto error; |
| 1943 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1944 | } else { |
| 1945 | if (type->tp_as_async != NULL){ |
| 1946 | getter = type->tp_as_async->am_anext; |
| 1947 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1948 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1949 | if (getter != NULL) { |
| 1950 | next_iter = (*getter)(aiter); |
| 1951 | if (next_iter == NULL) { |
| 1952 | goto error; |
| 1953 | } |
| 1954 | } |
| 1955 | else { |
| 1956 | PyErr_Format( |
| 1957 | PyExc_TypeError, |
| 1958 | "'async for' requires an iterator with " |
| 1959 | "__anext__ method, got %.100s", |
| 1960 | type->tp_name); |
| 1961 | goto error; |
| 1962 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1963 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1964 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1965 | if (awaitable == NULL) { |
| 1966 | PyErr_Format( |
| 1967 | PyExc_TypeError, |
| 1968 | "'async for' received an invalid object " |
| 1969 | "from __anext__: %.100s", |
| 1970 | Py_TYPE(next_iter)->tp_name); |
| 1971 | |
| 1972 | Py_DECREF(next_iter); |
| 1973 | goto error; |
| 1974 | } else { |
| 1975 | Py_DECREF(next_iter); |
| 1976 | } |
| 1977 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1978 | |
| 1979 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1980 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1981 | DISPATCH(); |
| 1982 | } |
| 1983 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1984 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1985 | TARGET(GET_AWAITABLE) { |
| 1986 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1987 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1988 | |
| 1989 | Py_DECREF(iterable); |
| 1990 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1991 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1992 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1993 | if (yf != NULL) { |
| 1994 | /* `iter` is a coroutine object that is being |
| 1995 | awaited, `yf` is a pointer to the current awaitable |
| 1996 | being awaited on. */ |
| 1997 | Py_DECREF(yf); |
| 1998 | Py_CLEAR(iter); |
| 1999 | PyErr_SetString( |
| 2000 | PyExc_RuntimeError, |
| 2001 | "coroutine is being awaited already"); |
| 2002 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2003 | } |
| 2004 | } |
| 2005 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2006 | SET_TOP(iter); /* Even if it's NULL */ |
| 2007 | |
| 2008 | if (iter == NULL) { |
| 2009 | goto error; |
| 2010 | } |
| 2011 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2012 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2013 | DISPATCH(); |
| 2014 | } |
| 2015 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2016 | TARGET(YIELD_FROM) { |
| 2017 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2018 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2019 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2020 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2021 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2022 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2023 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2024 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2025 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2026 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2027 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2028 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2029 | Py_DECREF(v); |
| 2030 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2031 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2032 | if (tstate->c_tracefunc != NULL |
| 2033 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2034 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2035 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2036 | if (err < 0) |
| 2037 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2038 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2039 | SET_TOP(val); |
| 2040 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2041 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2042 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2043 | f->f_stacktop = stack_pointer; |
| 2044 | why = WHY_YIELD; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2045 | /* and repeat... */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 2046 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2047 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2048 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2049 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2050 | TARGET(YIELD_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2052 | |
| 2053 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2054 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2055 | Py_DECREF(retval); |
| 2056 | if (w == NULL) { |
| 2057 | retval = NULL; |
| 2058 | goto error; |
| 2059 | } |
| 2060 | retval = w; |
| 2061 | } |
| 2062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | f->f_stacktop = stack_pointer; |
| 2064 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2066 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2067 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2068 | TARGET(POP_EXCEPT) { |
| 2069 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2070 | if (b->b_type != EXCEPT_HANDLER) { |
| 2071 | PyErr_SetString(PyExc_SystemError, |
| 2072 | "popped block is not an except handler"); |
| 2073 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2075 | UNWIND_EXCEPT_HANDLER(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2077 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2078 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2079 | PREDICTED(POP_BLOCK); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2080 | TARGET(POP_BLOCK) { |
| 2081 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2082 | UNWIND_BLOCK(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2084 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2087 | TARGET(END_FINALLY) { |
| 2088 | PyObject *status = POP(); |
| 2089 | if (PyLong_Check(status)) { |
| 2090 | why = (enum why_code) PyLong_AS_LONG(status); |
| 2091 | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | if (why == WHY_RETURN || |
| 2093 | why == WHY_CONTINUE) |
| 2094 | retval = POP(); |
| 2095 | if (why == WHY_SILENCED) { |
| 2096 | /* An exception was silenced by 'with', we must |
| 2097 | manually unwind the EXCEPT_HANDLER block which was |
| 2098 | created when the exception was caught, otherwise |
| 2099 | the stack will be in an inconsistent state. */ |
| 2100 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2101 | assert(b->b_type == EXCEPT_HANDLER); |
| 2102 | UNWIND_EXCEPT_HANDLER(b); |
| 2103 | why = WHY_NOT; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2104 | Py_DECREF(status); |
| 2105 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2107 | Py_DECREF(status); |
| 2108 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2110 | else if (PyExceptionClass_Check(status)) { |
| 2111 | PyObject *exc = POP(); |
| 2112 | PyObject *tb = POP(); |
| 2113 | PyErr_Restore(status, exc, tb); |
| 2114 | why = WHY_EXCEPTION; |
| 2115 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2117 | else if (status != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | PyErr_SetString(PyExc_SystemError, |
| 2119 | "'finally' pops bad exception"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2120 | Py_DECREF(status); |
| 2121 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2123 | Py_DECREF(status); |
| 2124 | DISPATCH(); |
| 2125 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2126 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2127 | TARGET(LOAD_BUILD_CLASS) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2128 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2129 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2130 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2131 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2132 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2133 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2134 | PyErr_SetString(PyExc_NameError, |
| 2135 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2136 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2137 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2138 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2139 | } |
| 2140 | else { |
| 2141 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2142 | if (build_class_str == NULL) |
| 2143 | break; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2144 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2145 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2146 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2147 | PyErr_SetString(PyExc_NameError, |
| 2148 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2149 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2150 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2151 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2152 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2153 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2154 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2155 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2156 | TARGET(STORE_NAME) { |
| 2157 | PyObject *name = GETITEM(names, oparg); |
| 2158 | PyObject *v = POP(); |
| 2159 | PyObject *ns = f->f_locals; |
| 2160 | int err; |
| 2161 | if (ns == NULL) { |
| 2162 | PyErr_Format(PyExc_SystemError, |
| 2163 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2165 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2167 | if (PyDict_CheckExact(ns)) |
| 2168 | err = PyDict_SetItem(ns, name, v); |
| 2169 | else |
| 2170 | err = PyObject_SetItem(ns, name, v); |
| 2171 | Py_DECREF(v); |
| 2172 | if (err != 0) |
| 2173 | goto error; |
| 2174 | DISPATCH(); |
| 2175 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2176 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2177 | TARGET(DELETE_NAME) { |
| 2178 | PyObject *name = GETITEM(names, oparg); |
| 2179 | PyObject *ns = f->f_locals; |
| 2180 | int err; |
| 2181 | if (ns == NULL) { |
| 2182 | PyErr_Format(PyExc_SystemError, |
| 2183 | "no locals when deleting %R", name); |
| 2184 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2186 | err = PyObject_DelItem(ns, name); |
| 2187 | if (err != 0) { |
| 2188 | format_exc_check_arg(PyExc_NameError, |
| 2189 | NAME_ERROR_MSG, |
| 2190 | name); |
| 2191 | goto error; |
| 2192 | } |
| 2193 | DISPATCH(); |
| 2194 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2195 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2196 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2197 | TARGET(UNPACK_SEQUENCE) { |
| 2198 | PyObject *seq = POP(), *item, **items; |
| 2199 | if (PyTuple_CheckExact(seq) && |
| 2200 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2201 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2203 | item = items[oparg]; |
| 2204 | Py_INCREF(item); |
| 2205 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2207 | } else if (PyList_CheckExact(seq) && |
| 2208 | PyList_GET_SIZE(seq) == oparg) { |
| 2209 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2211 | item = items[oparg]; |
| 2212 | Py_INCREF(item); |
| 2213 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2215 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | stack_pointer + oparg)) { |
| 2217 | STACKADJ(oparg); |
| 2218 | } else { |
| 2219 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2220 | Py_DECREF(seq); |
| 2221 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2223 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2224 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2226 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2227 | TARGET(UNPACK_EX) { |
| 2228 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2229 | PyObject *seq = POP(); |
| 2230 | |
| 2231 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2232 | stack_pointer + totalargs)) { |
| 2233 | stack_pointer += totalargs; |
| 2234 | } else { |
| 2235 | Py_DECREF(seq); |
| 2236 | goto error; |
| 2237 | } |
| 2238 | Py_DECREF(seq); |
| 2239 | DISPATCH(); |
| 2240 | } |
| 2241 | |
| 2242 | TARGET(STORE_ATTR) { |
| 2243 | PyObject *name = GETITEM(names, oparg); |
| 2244 | PyObject *owner = TOP(); |
| 2245 | PyObject *v = SECOND(); |
| 2246 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | STACKADJ(-2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2248 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2250 | Py_DECREF(owner); |
| 2251 | if (err != 0) |
| 2252 | goto error; |
| 2253 | DISPATCH(); |
| 2254 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2255 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2256 | TARGET(DELETE_ATTR) { |
| 2257 | PyObject *name = GETITEM(names, oparg); |
| 2258 | PyObject *owner = POP(); |
| 2259 | int err; |
| 2260 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2261 | Py_DECREF(owner); |
| 2262 | if (err != 0) |
| 2263 | goto error; |
| 2264 | DISPATCH(); |
| 2265 | } |
| 2266 | |
| 2267 | TARGET(STORE_GLOBAL) { |
| 2268 | PyObject *name = GETITEM(names, oparg); |
| 2269 | PyObject *v = POP(); |
| 2270 | int err; |
| 2271 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | if (err != 0) |
| 2274 | goto error; |
| 2275 | DISPATCH(); |
| 2276 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2277 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2278 | TARGET(DELETE_GLOBAL) { |
| 2279 | PyObject *name = GETITEM(names, oparg); |
| 2280 | int err; |
| 2281 | err = PyDict_DelItem(f->f_globals, name); |
| 2282 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2284 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2285 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2286 | } |
| 2287 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2288 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2289 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2290 | TARGET(LOAD_NAME) { |
| 2291 | PyObject *name = GETITEM(names, oparg); |
| 2292 | PyObject *locals = f->f_locals; |
| 2293 | PyObject *v; |
| 2294 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2296 | "no locals when loading %R", name); |
| 2297 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2299 | if (PyDict_CheckExact(locals)) { |
| 2300 | v = PyDict_GetItem(locals, name); |
| 2301 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | } |
| 2303 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2304 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2305 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2306 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2307 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | PyErr_Clear(); |
| 2309 | } |
| 2310 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2311 | if (v == NULL) { |
| 2312 | v = PyDict_GetItem(f->f_globals, name); |
| 2313 | Py_XINCREF(v); |
| 2314 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2315 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2316 | v = PyDict_GetItem(f->f_builtins, name); |
| 2317 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2318 | format_exc_check_arg( |
| 2319 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2320 | NAME_ERROR_MSG, name); |
| 2321 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2322 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2323 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2324 | } |
| 2325 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2326 | v = PyObject_GetItem(f->f_builtins, name); |
| 2327 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2328 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2329 | format_exc_check_arg( |
| 2330 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2331 | NAME_ERROR_MSG, name); |
| 2332 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2333 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2334 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2337 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2339 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2340 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2341 | TARGET(LOAD_GLOBAL) { |
| 2342 | PyObject *name = GETITEM(names, oparg); |
| 2343 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2344 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2345 | && PyDict_CheckExact(f->f_builtins)) |
| 2346 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2347 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2348 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2349 | name); |
| 2350 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2351 | if (!_PyErr_OCCURRED()) { |
| 2352 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2353 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2354 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2355 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2356 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2357 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2359 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2361 | else { |
| 2362 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2363 | |
| 2364 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2365 | v = PyObject_GetItem(f->f_globals, name); |
| 2366 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2367 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2368 | goto error; |
| 2369 | PyErr_Clear(); |
| 2370 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2371 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2372 | v = PyObject_GetItem(f->f_builtins, name); |
| 2373 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2374 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2375 | format_exc_check_arg( |
| 2376 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2377 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2378 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2379 | } |
| 2380 | } |
| 2381 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2382 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2383 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2384 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2385 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2386 | TARGET(DELETE_FAST) { |
| 2387 | PyObject *v = GETLOCAL(oparg); |
| 2388 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2389 | SETLOCAL(oparg, NULL); |
| 2390 | DISPATCH(); |
| 2391 | } |
| 2392 | format_exc_check_arg( |
| 2393 | PyExc_UnboundLocalError, |
| 2394 | UNBOUNDLOCAL_ERROR_MSG, |
| 2395 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2396 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2397 | goto error; |
| 2398 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2399 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2400 | TARGET(DELETE_DEREF) { |
| 2401 | PyObject *cell = freevars[oparg]; |
| 2402 | if (PyCell_GET(cell) != NULL) { |
| 2403 | PyCell_Set(cell, NULL); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2404 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2405 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2406 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2407 | goto error; |
| 2408 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2409 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2410 | TARGET(LOAD_CLOSURE) { |
| 2411 | PyObject *cell = freevars[oparg]; |
| 2412 | Py_INCREF(cell); |
| 2413 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2415 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2416 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2417 | TARGET(LOAD_CLASSDEREF) { |
| 2418 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2419 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2420 | assert(locals); |
| 2421 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2422 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2423 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2424 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2425 | if (PyDict_CheckExact(locals)) { |
| 2426 | value = PyDict_GetItem(locals, name); |
| 2427 | Py_XINCREF(value); |
| 2428 | } |
| 2429 | else { |
| 2430 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2431 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2432 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2433 | goto error; |
| 2434 | PyErr_Clear(); |
| 2435 | } |
| 2436 | } |
| 2437 | if (!value) { |
| 2438 | PyObject *cell = freevars[oparg]; |
| 2439 | value = PyCell_GET(cell); |
| 2440 | if (value == NULL) { |
| 2441 | format_exc_unbound(co, oparg); |
| 2442 | goto error; |
| 2443 | } |
| 2444 | Py_INCREF(value); |
| 2445 | } |
| 2446 | PUSH(value); |
| 2447 | DISPATCH(); |
| 2448 | } |
| 2449 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2450 | TARGET(LOAD_DEREF) { |
| 2451 | PyObject *cell = freevars[oparg]; |
| 2452 | PyObject *value = PyCell_GET(cell); |
| 2453 | if (value == NULL) { |
| 2454 | format_exc_unbound(co, oparg); |
| 2455 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2457 | Py_INCREF(value); |
| 2458 | PUSH(value); |
| 2459 | DISPATCH(); |
| 2460 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2461 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2462 | TARGET(STORE_DEREF) { |
| 2463 | PyObject *v = POP(); |
| 2464 | PyObject *cell = freevars[oparg]; |
| 2465 | PyCell_Set(cell, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2467 | DISPATCH(); |
| 2468 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2469 | |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2470 | TARGET(BUILD_STRING) { |
| 2471 | PyObject *str; |
| 2472 | PyObject *empty = PyUnicode_New(0, 0); |
| 2473 | if (empty == NULL) { |
| 2474 | goto error; |
| 2475 | } |
| 2476 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2477 | Py_DECREF(empty); |
| 2478 | if (str == NULL) |
| 2479 | goto error; |
| 2480 | while (--oparg >= 0) { |
| 2481 | PyObject *item = POP(); |
| 2482 | Py_DECREF(item); |
| 2483 | } |
| 2484 | PUSH(str); |
| 2485 | DISPATCH(); |
| 2486 | } |
| 2487 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2488 | TARGET(BUILD_TUPLE) { |
| 2489 | PyObject *tup = PyTuple_New(oparg); |
| 2490 | if (tup == NULL) |
| 2491 | goto error; |
| 2492 | while (--oparg >= 0) { |
| 2493 | PyObject *item = POP(); |
| 2494 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2495 | } |
| 2496 | PUSH(tup); |
| 2497 | DISPATCH(); |
| 2498 | } |
| 2499 | |
| 2500 | TARGET(BUILD_LIST) { |
| 2501 | PyObject *list = PyList_New(oparg); |
| 2502 | if (list == NULL) |
| 2503 | goto error; |
| 2504 | while (--oparg >= 0) { |
| 2505 | PyObject *item = POP(); |
| 2506 | PyList_SET_ITEM(list, oparg, item); |
| 2507 | } |
| 2508 | PUSH(list); |
| 2509 | DISPATCH(); |
| 2510 | } |
| 2511 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2512 | TARGET(BUILD_TUPLE_UNPACK) |
| 2513 | TARGET(BUILD_LIST_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2514 | int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2515 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2516 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2517 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2518 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2519 | if (sum == NULL) |
| 2520 | goto error; |
| 2521 | |
| 2522 | for (i = oparg; i > 0; i--) { |
| 2523 | PyObject *none_val; |
| 2524 | |
| 2525 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2526 | if (none_val == NULL) { |
| 2527 | Py_DECREF(sum); |
| 2528 | goto error; |
| 2529 | } |
| 2530 | Py_DECREF(none_val); |
| 2531 | } |
| 2532 | |
| 2533 | if (convert_to_tuple) { |
| 2534 | return_value = PyList_AsTuple(sum); |
| 2535 | Py_DECREF(sum); |
| 2536 | if (return_value == NULL) |
| 2537 | goto error; |
| 2538 | } |
| 2539 | else { |
| 2540 | return_value = sum; |
| 2541 | } |
| 2542 | |
| 2543 | while (oparg--) |
| 2544 | Py_DECREF(POP()); |
| 2545 | PUSH(return_value); |
| 2546 | DISPATCH(); |
| 2547 | } |
| 2548 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2549 | TARGET(BUILD_SET) { |
| 2550 | PyObject *set = PySet_New(NULL); |
| 2551 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2552 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2553 | if (set == NULL) |
| 2554 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2555 | for (i = oparg; i > 0; i--) { |
| 2556 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2557 | if (err == 0) |
| 2558 | err = PySet_Add(set, item); |
| 2559 | Py_DECREF(item); |
| 2560 | } |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2561 | STACKADJ(-oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2562 | if (err != 0) { |
| 2563 | Py_DECREF(set); |
| 2564 | goto error; |
| 2565 | } |
| 2566 | PUSH(set); |
| 2567 | DISPATCH(); |
| 2568 | } |
| 2569 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2570 | TARGET(BUILD_SET_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2571 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2572 | PyObject *sum = PySet_New(NULL); |
| 2573 | if (sum == NULL) |
| 2574 | goto error; |
| 2575 | |
| 2576 | for (i = oparg; i > 0; i--) { |
| 2577 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2578 | Py_DECREF(sum); |
| 2579 | goto error; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | while (oparg--) |
| 2584 | Py_DECREF(POP()); |
| 2585 | PUSH(sum); |
| 2586 | DISPATCH(); |
| 2587 | } |
| 2588 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2589 | TARGET(BUILD_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2590 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2591 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2592 | if (map == NULL) |
| 2593 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2594 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2595 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2596 | PyObject *key = PEEK(2*i); |
| 2597 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2598 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2599 | if (err != 0) { |
| 2600 | Py_DECREF(map); |
| 2601 | goto error; |
| 2602 | } |
| 2603 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2604 | |
| 2605 | while (oparg--) { |
| 2606 | Py_DECREF(POP()); |
| 2607 | Py_DECREF(POP()); |
| 2608 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2609 | PUSH(map); |
| 2610 | DISPATCH(); |
| 2611 | } |
| 2612 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2613 | TARGET(SETUP_ANNOTATIONS) { |
| 2614 | _Py_IDENTIFIER(__annotations__); |
| 2615 | int err; |
| 2616 | PyObject *ann_dict; |
| 2617 | if (f->f_locals == NULL) { |
| 2618 | PyErr_Format(PyExc_SystemError, |
| 2619 | "no locals found when setting up annotations"); |
| 2620 | goto error; |
| 2621 | } |
| 2622 | /* check if __annotations__ in locals()... */ |
| 2623 | if (PyDict_CheckExact(f->f_locals)) { |
| 2624 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2625 | &PyId___annotations__); |
| 2626 | if (ann_dict == NULL) { |
| 2627 | /* ...if not, create a new one */ |
| 2628 | ann_dict = PyDict_New(); |
| 2629 | if (ann_dict == NULL) { |
| 2630 | goto error; |
| 2631 | } |
| 2632 | err = _PyDict_SetItemId(f->f_locals, |
| 2633 | &PyId___annotations__, ann_dict); |
| 2634 | Py_DECREF(ann_dict); |
| 2635 | if (err != 0) { |
| 2636 | goto error; |
| 2637 | } |
| 2638 | } |
| 2639 | } |
| 2640 | else { |
| 2641 | /* do the same if locals() is not a dict */ |
| 2642 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2643 | if (ann_str == NULL) { |
| 2644 | break; |
| 2645 | } |
| 2646 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2647 | if (ann_dict == NULL) { |
| 2648 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2649 | goto error; |
| 2650 | } |
| 2651 | PyErr_Clear(); |
| 2652 | ann_dict = PyDict_New(); |
| 2653 | if (ann_dict == NULL) { |
| 2654 | goto error; |
| 2655 | } |
| 2656 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2657 | Py_DECREF(ann_dict); |
| 2658 | if (err != 0) { |
| 2659 | goto error; |
| 2660 | } |
| 2661 | } |
| 2662 | else { |
| 2663 | Py_DECREF(ann_dict); |
| 2664 | } |
| 2665 | } |
| 2666 | DISPATCH(); |
| 2667 | } |
| 2668 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2669 | TARGET(BUILD_CONST_KEY_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2670 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2671 | PyObject *map; |
| 2672 | PyObject *keys = TOP(); |
| 2673 | if (!PyTuple_CheckExact(keys) || |
| 2674 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2675 | PyErr_SetString(PyExc_SystemError, |
| 2676 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2677 | goto error; |
| 2678 | } |
| 2679 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2680 | if (map == NULL) { |
| 2681 | goto error; |
| 2682 | } |
| 2683 | for (i = oparg; i > 0; i--) { |
| 2684 | int err; |
| 2685 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2686 | PyObject *value = PEEK(i + 1); |
| 2687 | err = PyDict_SetItem(map, key, value); |
| 2688 | if (err != 0) { |
| 2689 | Py_DECREF(map); |
| 2690 | goto error; |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | Py_DECREF(POP()); |
| 2695 | while (oparg--) { |
| 2696 | Py_DECREF(POP()); |
| 2697 | } |
| 2698 | PUSH(map); |
| 2699 | DISPATCH(); |
| 2700 | } |
| 2701 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2702 | TARGET(BUILD_MAP_UNPACK_WITH_CALL) |
| 2703 | TARGET(BUILD_MAP_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2704 | int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2705 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2706 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2707 | if (sum == NULL) |
| 2708 | goto error; |
| 2709 | |
| 2710 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2711 | PyObject *arg = PEEK(i); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2712 | if (with_call && PyDict_Size(sum)) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2713 | PyObject *intersection = _PyDictView_Intersect(sum, arg); |
| 2714 | |
| 2715 | if (intersection == NULL) { |
| 2716 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2717 | PyObject *func = PEEK(2 + oparg); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2718 | PyErr_Format(PyExc_TypeError, |
| 2719 | "%.200s%.200s argument after ** " |
| 2720 | "must be a mapping, not %.200s", |
| 2721 | PyEval_GetFuncName(func), |
| 2722 | PyEval_GetFuncDesc(func), |
| 2723 | arg->ob_type->tp_name); |
| 2724 | } |
| 2725 | Py_DECREF(sum); |
| 2726 | goto error; |
| 2727 | } |
| 2728 | |
| 2729 | if (PySet_GET_SIZE(intersection)) { |
| 2730 | Py_ssize_t idx = 0; |
| 2731 | PyObject *key; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2732 | PyObject *func = PEEK(2 + oparg); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2733 | Py_hash_t hash; |
| 2734 | _PySet_NextEntry(intersection, &idx, &key, &hash); |
| 2735 | if (!PyUnicode_Check(key)) { |
| 2736 | PyErr_Format(PyExc_TypeError, |
| 2737 | "%.200s%.200s keywords must be strings", |
| 2738 | PyEval_GetFuncName(func), |
| 2739 | PyEval_GetFuncDesc(func)); |
| 2740 | } else { |
| 2741 | PyErr_Format(PyExc_TypeError, |
| 2742 | "%.200s%.200s got multiple " |
| 2743 | "values for keyword argument '%U'", |
| 2744 | PyEval_GetFuncName(func), |
| 2745 | PyEval_GetFuncDesc(func), |
| 2746 | key); |
| 2747 | } |
| 2748 | Py_DECREF(intersection); |
| 2749 | Py_DECREF(sum); |
| 2750 | goto error; |
| 2751 | } |
| 2752 | Py_DECREF(intersection); |
| 2753 | } |
| 2754 | |
| 2755 | if (PyDict_Update(sum, arg) < 0) { |
| 2756 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2757 | PyErr_Format(PyExc_TypeError, |
| 2758 | "'%.200s' object is not a mapping", |
| 2759 | arg->ob_type->tp_name); |
| 2760 | } |
| 2761 | Py_DECREF(sum); |
| 2762 | goto error; |
| 2763 | } |
| 2764 | } |
| 2765 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2766 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2767 | Py_DECREF(POP()); |
| 2768 | PUSH(sum); |
| 2769 | DISPATCH(); |
| 2770 | } |
| 2771 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2772 | TARGET(MAP_ADD) { |
| 2773 | PyObject *key = TOP(); |
| 2774 | PyObject *value = SECOND(); |
| 2775 | PyObject *map; |
| 2776 | int err; |
| 2777 | STACKADJ(-2); |
| 2778 | map = stack_pointer[-oparg]; /* dict */ |
| 2779 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2780 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2781 | Py_DECREF(value); |
| 2782 | Py_DECREF(key); |
| 2783 | if (err != 0) |
| 2784 | goto error; |
| 2785 | PREDICT(JUMP_ABSOLUTE); |
| 2786 | DISPATCH(); |
| 2787 | } |
| 2788 | |
| 2789 | TARGET(LOAD_ATTR) { |
| 2790 | PyObject *name = GETITEM(names, oparg); |
| 2791 | PyObject *owner = TOP(); |
| 2792 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2793 | Py_DECREF(owner); |
| 2794 | SET_TOP(res); |
| 2795 | if (res == NULL) |
| 2796 | goto error; |
| 2797 | DISPATCH(); |
| 2798 | } |
| 2799 | |
| 2800 | TARGET(COMPARE_OP) { |
| 2801 | PyObject *right = POP(); |
| 2802 | PyObject *left = TOP(); |
| 2803 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2804 | Py_DECREF(left); |
| 2805 | Py_DECREF(right); |
| 2806 | SET_TOP(res); |
| 2807 | if (res == NULL) |
| 2808 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | PREDICT(POP_JUMP_IF_FALSE); |
| 2810 | PREDICT(POP_JUMP_IF_TRUE); |
| 2811 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2812 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2813 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2814 | TARGET(IMPORT_NAME) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2815 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2816 | PyObject *fromlist = POP(); |
| 2817 | PyObject *level = TOP(); |
| 2818 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2819 | res = import_name(f, name, fromlist, level); |
| 2820 | Py_DECREF(level); |
| 2821 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2822 | SET_TOP(res); |
| 2823 | if (res == NULL) |
| 2824 | goto error; |
| 2825 | DISPATCH(); |
| 2826 | } |
| 2827 | |
| 2828 | TARGET(IMPORT_STAR) { |
| 2829 | PyObject *from = POP(), *locals; |
| 2830 | int err; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2831 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 2832 | goto error; |
| 2833 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2834 | locals = f->f_locals; |
| 2835 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | PyErr_SetString(PyExc_SystemError, |
| 2837 | "no locals found during 'import *'"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2838 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2840 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2842 | Py_DECREF(from); |
| 2843 | if (err != 0) |
| 2844 | goto error; |
| 2845 | DISPATCH(); |
| 2846 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2847 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2848 | TARGET(IMPORT_FROM) { |
| 2849 | PyObject *name = GETITEM(names, oparg); |
| 2850 | PyObject *from = TOP(); |
| 2851 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2852 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2853 | PUSH(res); |
| 2854 | if (res == NULL) |
| 2855 | goto error; |
| 2856 | DISPATCH(); |
| 2857 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2858 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2859 | TARGET(JUMP_FORWARD) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | JUMPBY(oparg); |
| 2861 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2862 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2863 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2864 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2865 | TARGET(POP_JUMP_IF_FALSE) { |
| 2866 | PyObject *cond = POP(); |
| 2867 | int err; |
| 2868 | if (cond == Py_True) { |
| 2869 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | FAST_DISPATCH(); |
| 2871 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2872 | if (cond == Py_False) { |
| 2873 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | JUMPTO(oparg); |
| 2875 | FAST_DISPATCH(); |
| 2876 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2877 | err = PyObject_IsTrue(cond); |
| 2878 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | if (err > 0) |
| 2880 | err = 0; |
| 2881 | else if (err == 0) |
| 2882 | JUMPTO(oparg); |
| 2883 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2884 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2886 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2887 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2888 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2889 | TARGET(POP_JUMP_IF_TRUE) { |
| 2890 | PyObject *cond = POP(); |
| 2891 | int err; |
| 2892 | if (cond == Py_False) { |
| 2893 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | FAST_DISPATCH(); |
| 2895 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2896 | if (cond == Py_True) { |
| 2897 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | JUMPTO(oparg); |
| 2899 | FAST_DISPATCH(); |
| 2900 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2901 | err = PyObject_IsTrue(cond); |
| 2902 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | if (err > 0) { |
| 2904 | err = 0; |
| 2905 | JUMPTO(oparg); |
| 2906 | } |
| 2907 | else if (err == 0) |
| 2908 | ; |
| 2909 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2910 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2912 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2913 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2914 | TARGET(JUMP_IF_FALSE_OR_POP) { |
| 2915 | PyObject *cond = TOP(); |
| 2916 | int err; |
| 2917 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2919 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | FAST_DISPATCH(); |
| 2921 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2922 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | JUMPTO(oparg); |
| 2924 | FAST_DISPATCH(); |
| 2925 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2926 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2927 | if (err > 0) { |
| 2928 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2929 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2930 | err = 0; |
| 2931 | } |
| 2932 | else if (err == 0) |
| 2933 | JUMPTO(oparg); |
| 2934 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2935 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2937 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2938 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2939 | TARGET(JUMP_IF_TRUE_OR_POP) { |
| 2940 | PyObject *cond = TOP(); |
| 2941 | int err; |
| 2942 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2944 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | FAST_DISPATCH(); |
| 2946 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2947 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2948 | JUMPTO(oparg); |
| 2949 | FAST_DISPATCH(); |
| 2950 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2951 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | if (err > 0) { |
| 2953 | err = 0; |
| 2954 | JUMPTO(oparg); |
| 2955 | } |
| 2956 | else if (err == 0) { |
| 2957 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2958 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | } |
| 2960 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2961 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2962 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2963 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2964 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2965 | PREDICTED(JUMP_ABSOLUTE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2966 | TARGET(JUMP_ABSOLUTE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2968 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2970 | the per-loop checks for signals. By default, this should be turned-off |
| 2971 | because it prevents detection of a control-break in tight loops like |
| 2972 | "while 1: pass". Compile with this option turned-on when you need |
| 2973 | the speed-up and do not need break checking inside tight loops (ones |
| 2974 | that contain only instructions ending with FAST_DISPATCH). |
| 2975 | */ |
| 2976 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2977 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2979 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2980 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2981 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2982 | TARGET(GET_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2984 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2985 | PyObject *iter = PyObject_GetIter(iterable); |
| 2986 | Py_DECREF(iterable); |
| 2987 | SET_TOP(iter); |
| 2988 | if (iter == NULL) |
| 2989 | goto error; |
| 2990 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2991 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2992 | DISPATCH(); |
| 2993 | } |
| 2994 | |
| 2995 | TARGET(GET_YIELD_FROM_ITER) { |
| 2996 | /* before: [obj]; after [getiter(obj)] */ |
| 2997 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2998 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2999 | if (PyCoro_CheckExact(iterable)) { |
| 3000 | /* `iterable` is a coroutine */ |
| 3001 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3002 | /* and it is used in a 'yield from' expression of a |
| 3003 | regular generator. */ |
| 3004 | Py_DECREF(iterable); |
| 3005 | SET_TOP(NULL); |
| 3006 | PyErr_SetString(PyExc_TypeError, |
| 3007 | "cannot 'yield from' a coroutine object " |
| 3008 | "in a non-coroutine generator"); |
| 3009 | goto error; |
| 3010 | } |
| 3011 | } |
| 3012 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3013 | /* `iterable` is not a generator. */ |
| 3014 | iter = PyObject_GetIter(iterable); |
| 3015 | Py_DECREF(iterable); |
| 3016 | SET_TOP(iter); |
| 3017 | if (iter == NULL) |
| 3018 | goto error; |
| 3019 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3020 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3021 | DISPATCH(); |
| 3022 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3023 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3024 | PREDICTED(FOR_ITER); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3025 | TARGET(FOR_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3027 | PyObject *iter = TOP(); |
| 3028 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3029 | if (next != NULL) { |
| 3030 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | PREDICT(STORE_FAST); |
| 3032 | PREDICT(UNPACK_SEQUENCE); |
| 3033 | DISPATCH(); |
| 3034 | } |
| 3035 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3036 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3037 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 3038 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3039 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | PyErr_Clear(); |
| 3041 | } |
| 3042 | /* iterator ended normally */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3043 | STACKADJ(-1); |
| 3044 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3046 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3048 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3049 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3050 | TARGET(BREAK_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | why = WHY_BREAK; |
| 3052 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3053 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3054 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3055 | TARGET(CONTINUE_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | retval = PyLong_FromLong(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3057 | if (retval == NULL) |
| 3058 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | why = WHY_CONTINUE; |
| 3060 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3061 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3062 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3063 | TARGET(SETUP_LOOP) |
| 3064 | TARGET(SETUP_EXCEPT) |
| 3065 | TARGET(SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | /* NOTE: If you add any new block-setup opcodes that |
| 3067 | are not try/except/finally handlers, you may need |
| 3068 | to update the PyGen_NeedsFinalizing() function. |
| 3069 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 3072 | STACK_LEVEL()); |
| 3073 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3074 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3075 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3076 | TARGET(BEFORE_ASYNC_WITH) { |
| 3077 | _Py_IDENTIFIER(__aexit__); |
| 3078 | _Py_IDENTIFIER(__aenter__); |
| 3079 | |
| 3080 | PyObject *mgr = TOP(); |
| 3081 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 3082 | *enter; |
| 3083 | PyObject *res; |
| 3084 | if (exit == NULL) |
| 3085 | goto error; |
| 3086 | SET_TOP(exit); |
| 3087 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3088 | Py_DECREF(mgr); |
| 3089 | if (enter == NULL) |
| 3090 | goto error; |
| 3091 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3092 | Py_DECREF(enter); |
| 3093 | if (res == NULL) |
| 3094 | goto error; |
| 3095 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3096 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3097 | DISPATCH(); |
| 3098 | } |
| 3099 | |
| 3100 | TARGET(SETUP_ASYNC_WITH) { |
| 3101 | PyObject *res = POP(); |
| 3102 | /* Setup the finally block before pushing the result |
| 3103 | of __aenter__ on the stack. */ |
| 3104 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3105 | STACK_LEVEL()); |
| 3106 | PUSH(res); |
| 3107 | DISPATCH(); |
| 3108 | } |
| 3109 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3110 | TARGET(SETUP_WITH) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3111 | _Py_IDENTIFIER(__exit__); |
| 3112 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3113 | PyObject *mgr = TOP(); |
| 3114 | PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter; |
| 3115 | PyObject *res; |
| 3116 | if (exit == NULL) |
| 3117 | goto error; |
| 3118 | SET_TOP(exit); |
| 3119 | enter = special_lookup(mgr, &PyId___enter__); |
| 3120 | Py_DECREF(mgr); |
| 3121 | if (enter == NULL) |
| 3122 | goto error; |
| 3123 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3124 | Py_DECREF(enter); |
| 3125 | if (res == NULL) |
| 3126 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | /* Setup the finally block before pushing the result |
| 3128 | of __enter__ on the stack. */ |
| 3129 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3130 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3131 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3132 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | DISPATCH(); |
| 3134 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3135 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3136 | TARGET(WITH_CLEANUP_START) { |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3137 | /* At the top of the stack are 1-6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | how/why we entered the finally clause: |
| 3139 | - TOP = None |
| 3140 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 3141 | - TOP = WHY_*; no retval below it |
| 3142 | - (TOP, SECOND, THIRD) = exc_info() |
| 3143 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3144 | Below them is EXIT, the context.__exit__ bound method. |
| 3145 | In the last case, we must call |
| 3146 | EXIT(TOP, SECOND, THIRD) |
| 3147 | otherwise we must call |
| 3148 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3149 | |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3150 | In the first three cases, we remove EXIT from the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | stack, leaving the rest in the same order. In the |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3152 | fourth case, we shift the bottom 3 values of the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | In addition, if the stack represents an exception, |
| 3156 | *and* the function call returns a 'true' value, we |
| 3157 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 3158 | then not re-raise the exception. (But non-local |
| 3159 | gotos should still be resumed.) |
| 3160 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | PyObject *exit_func; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3163 | PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3164 | if (exc == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | (void)POP(); |
| 3166 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3167 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3169 | else if (PyLong_Check(exc)) { |
| 3170 | STACKADJ(-1); |
| 3171 | switch (PyLong_AsLong(exc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | case WHY_RETURN: |
| 3173 | case WHY_CONTINUE: |
| 3174 | /* Retval in TOP. */ |
| 3175 | exit_func = SECOND(); |
| 3176 | SET_SECOND(TOP()); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3177 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | break; |
| 3179 | default: |
| 3180 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3181 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | break; |
| 3183 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3184 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | } |
| 3186 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3187 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3189 | val = SECOND(); |
| 3190 | tb = THIRD(); |
| 3191 | tp2 = FOURTH(); |
| 3192 | exc2 = PEEK(5); |
| 3193 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3195 | SET_VALUE(7, tb2); |
| 3196 | SET_VALUE(6, exc2); |
| 3197 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3199 | SET_FOURTH(NULL); |
| 3200 | /* We just shifted the stack down, so we have |
| 3201 | to tell the except handler block that the |
| 3202 | values are lower than it expects. */ |
| 3203 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3204 | assert(block->b_type == EXCEPT_HANDLER); |
| 3205 | block->b_level--; |
| 3206 | } |
| 3207 | /* XXX Not the fastest way to call it... */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3208 | res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3210 | if (res == NULL) |
| 3211 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3212 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3213 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3214 | PUSH(exc); |
| 3215 | PUSH(res); |
| 3216 | PREDICT(WITH_CLEANUP_FINISH); |
| 3217 | DISPATCH(); |
| 3218 | } |
| 3219 | |
| 3220 | PREDICTED(WITH_CLEANUP_FINISH); |
| 3221 | TARGET(WITH_CLEANUP_FINISH) { |
| 3222 | PyObject *res = POP(); |
| 3223 | PyObject *exc = POP(); |
| 3224 | int err; |
| 3225 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3226 | if (exc != Py_None) |
| 3227 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | else |
| 3229 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3230 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3231 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3232 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3235 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | else if (err > 0) { |
| 3237 | err = 0; |
| 3238 | /* There was an exception and a True return */ |
| 3239 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 3240 | } |
| 3241 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3242 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3244 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3245 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3246 | TARGET(CALL_FUNCTION) { |
| 3247 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | PCALL(PCALL_ALL); |
| 3249 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3250 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3252 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3253 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3254 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3255 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3256 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3258 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3259 | TARGET(CALL_FUNCTION_KW) { |
| 3260 | PyObject **sp, *res, *names; |
| 3261 | |
| 3262 | names = POP(); |
| 3263 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | PCALL(PCALL_ALL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3266 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3268 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3269 | Py_DECREF(names); |
| 3270 | |
| 3271 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3272 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3273 | } |
| 3274 | DISPATCH(); |
| 3275 | } |
| 3276 | |
| 3277 | TARGET(CALL_FUNCTION_EX) { |
| 3278 | PyObject *func, *callargs, *kwargs = NULL, *result; |
| 3279 | PCALL(PCALL_ALL); |
| 3280 | if (oparg & 0x01) { |
| 3281 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3282 | if (!PyDict_CheckExact(kwargs)) { |
| 3283 | PyObject *d = PyDict_New(); |
| 3284 | if (d == NULL) |
| 3285 | goto error; |
| 3286 | if (PyDict_Update(d, kwargs) != 0) { |
| 3287 | Py_DECREF(d); |
| 3288 | /* PyDict_Update raises attribute |
| 3289 | * error (percolated from an attempt |
| 3290 | * to get 'keys' attribute) instead of |
| 3291 | * a type error if its second argument |
| 3292 | * is not a mapping. |
| 3293 | */ |
| 3294 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3295 | func = SECOND(); |
| 3296 | PyErr_Format(PyExc_TypeError, |
| 3297 | "%.200s%.200s argument after ** " |
| 3298 | "must be a mapping, not %.200s", |
| 3299 | PyEval_GetFuncName(func), |
| 3300 | PyEval_GetFuncDesc(func), |
| 3301 | kwargs->ob_type->tp_name); |
| 3302 | } |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame^] | 3303 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3304 | goto error; |
| 3305 | } |
| 3306 | Py_DECREF(kwargs); |
| 3307 | kwargs = d; |
| 3308 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3309 | assert(PyDict_CheckExact(kwargs)); |
| 3310 | } |
| 3311 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3312 | func = TOP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3313 | if (!PyTuple_Check(callargs)) { |
| 3314 | if (Py_TYPE(callargs)->tp_iter == NULL && |
| 3315 | !PySequence_Check(callargs)) { |
| 3316 | PyErr_Format(PyExc_TypeError, |
| 3317 | "%.200s%.200s argument after * " |
| 3318 | "must be an iterable, not %.200s", |
| 3319 | PyEval_GetFuncName(func), |
| 3320 | PyEval_GetFuncDesc(func), |
| 3321 | callargs->ob_type->tp_name); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame^] | 3322 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3323 | goto error; |
| 3324 | } |
| 3325 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3326 | if (callargs == NULL) { |
| 3327 | goto error; |
| 3328 | } |
| 3329 | } |
| 3330 | assert(PyTuple_Check(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3331 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3332 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3333 | Py_DECREF(func); |
| 3334 | Py_DECREF(callargs); |
| 3335 | Py_XDECREF(kwargs); |
| 3336 | |
| 3337 | SET_TOP(result); |
| 3338 | if (result == NULL) { |
| 3339 | goto error; |
| 3340 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3341 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3343 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3344 | TARGET(MAKE_FUNCTION) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3345 | PyObject *qualname = POP(); |
| 3346 | PyObject *codeobj = POP(); |
| 3347 | PyFunctionObject *func = (PyFunctionObject *) |
| 3348 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3349 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3350 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3351 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3352 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3353 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3354 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3355 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3356 | if (oparg & 0x08) { |
| 3357 | assert(PyTuple_CheckExact(TOP())); |
| 3358 | func ->func_closure = POP(); |
| 3359 | } |
| 3360 | if (oparg & 0x04) { |
| 3361 | assert(PyDict_CheckExact(TOP())); |
| 3362 | func->func_annotations = POP(); |
| 3363 | } |
| 3364 | if (oparg & 0x02) { |
| 3365 | assert(PyDict_CheckExact(TOP())); |
| 3366 | func->func_kwdefaults = POP(); |
| 3367 | } |
| 3368 | if (oparg & 0x01) { |
| 3369 | assert(PyTuple_CheckExact(TOP())); |
| 3370 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3372 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3373 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3374 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3376 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3377 | TARGET(BUILD_SLICE) { |
| 3378 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3380 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3382 | step = NULL; |
| 3383 | stop = POP(); |
| 3384 | start = TOP(); |
| 3385 | slice = PySlice_New(start, stop, step); |
| 3386 | Py_DECREF(start); |
| 3387 | Py_DECREF(stop); |
| 3388 | Py_XDECREF(step); |
| 3389 | SET_TOP(slice); |
| 3390 | if (slice == NULL) |
| 3391 | goto error; |
| 3392 | DISPATCH(); |
| 3393 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3394 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3395 | TARGET(FORMAT_VALUE) { |
| 3396 | /* Handles f-string value formatting. */ |
| 3397 | PyObject *result; |
| 3398 | PyObject *fmt_spec; |
| 3399 | PyObject *value; |
| 3400 | PyObject *(*conv_fn)(PyObject *); |
| 3401 | int which_conversion = oparg & FVC_MASK; |
| 3402 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3403 | |
| 3404 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3405 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3406 | |
| 3407 | /* See if any conversion is specified. */ |
| 3408 | switch (which_conversion) { |
| 3409 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3410 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3411 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3412 | |
| 3413 | /* Must be 0 (meaning no conversion), since only four |
| 3414 | values are allowed by (oparg & FVC_MASK). */ |
| 3415 | default: conv_fn = NULL; break; |
| 3416 | } |
| 3417 | |
| 3418 | /* If there's a conversion function, call it and replace |
| 3419 | value with that result. Otherwise, just use value, |
| 3420 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3421 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3422 | result = conv_fn(value); |
| 3423 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3424 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3425 | Py_XDECREF(fmt_spec); |
| 3426 | goto error; |
| 3427 | } |
| 3428 | value = result; |
| 3429 | } |
| 3430 | |
| 3431 | /* If value is a unicode object, and there's no fmt_spec, |
| 3432 | then we know the result of format(value) is value |
| 3433 | itself. In that case, skip calling format(). I plan to |
| 3434 | move this optimization in to PyObject_Format() |
| 3435 | itself. */ |
| 3436 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3437 | /* Do nothing, just transfer ownership to result. */ |
| 3438 | result = value; |
| 3439 | } else { |
| 3440 | /* Actually call format(). */ |
| 3441 | result = PyObject_Format(value, fmt_spec); |
| 3442 | Py_DECREF(value); |
| 3443 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3444 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3445 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3446 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3447 | } |
| 3448 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3449 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3450 | DISPATCH(); |
| 3451 | } |
| 3452 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3453 | TARGET(EXTENDED_ARG) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3454 | int oldoparg = oparg; |
| 3455 | NEXTOPARG(); |
| 3456 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3457 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3458 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3459 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3460 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3461 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3463 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | default: |
| 3465 | fprintf(stderr, |
| 3466 | "XXX lineno: %d, opcode: %d\n", |
| 3467 | PyFrame_GetLineNumber(f), |
| 3468 | opcode); |
| 3469 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3470 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3471 | |
| 3472 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3474 | #endif |
| 3475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3477 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3478 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3479 | or goto error. */ |
| 3480 | assert(0); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3481 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3482 | error: |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3483 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3484 | assert(why == WHY_NOT); |
| 3485 | why = WHY_EXCEPTION; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3486 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3487 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3488 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3489 | if (!PyErr_Occurred()) |
| 3490 | PyErr_SetString(PyExc_SystemError, |
| 3491 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3492 | #else |
| 3493 | assert(PyErr_Occurred()); |
| 3494 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3495 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3496 | /* Log traceback info. */ |
| 3497 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3498 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3499 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3500 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3501 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3502 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3503 | fast_block_end: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3504 | assert(why != WHY_NOT); |
| 3505 | |
| 3506 | /* Unwind stacks if a (pseudo) exception occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3508 | /* Peek at the current block. */ |
| 3509 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | assert(why != WHY_YIELD); |
| 3512 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3513 | why = WHY_NOT; |
| 3514 | JUMPTO(PyLong_AS_LONG(retval)); |
| 3515 | Py_DECREF(retval); |
| 3516 | break; |
| 3517 | } |
| 3518 | /* Now we have to pop the block. */ |
| 3519 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | if (b->b_type == EXCEPT_HANDLER) { |
| 3522 | UNWIND_EXCEPT_HANDLER(b); |
| 3523 | continue; |
| 3524 | } |
| 3525 | UNWIND_BLOCK(b); |
| 3526 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3527 | why = WHY_NOT; |
| 3528 | JUMPTO(b->b_handler); |
| 3529 | break; |
| 3530 | } |
| 3531 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 3532 | || b->b_type == SETUP_FINALLY)) { |
| 3533 | PyObject *exc, *val, *tb; |
| 3534 | int handler = b->b_handler; |
| 3535 | /* Beware, this invalidates all b->b_* fields */ |
| 3536 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 3537 | PUSH(tstate->exc_traceback); |
| 3538 | PUSH(tstate->exc_value); |
| 3539 | if (tstate->exc_type != NULL) { |
| 3540 | PUSH(tstate->exc_type); |
| 3541 | } |
| 3542 | else { |
| 3543 | Py_INCREF(Py_None); |
| 3544 | PUSH(Py_None); |
| 3545 | } |
| 3546 | PyErr_Fetch(&exc, &val, &tb); |
| 3547 | /* Make the raw exception data |
| 3548 | available to the handler, |
| 3549 | so a program can emulate the |
| 3550 | Python main loop. */ |
| 3551 | PyErr_NormalizeException( |
| 3552 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3553 | if (tb != NULL) |
| 3554 | PyException_SetTraceback(val, tb); |
| 3555 | else |
| 3556 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3557 | Py_INCREF(exc); |
| 3558 | tstate->exc_type = exc; |
| 3559 | Py_INCREF(val); |
| 3560 | tstate->exc_value = val; |
| 3561 | tstate->exc_traceback = tb; |
| 3562 | if (tb == NULL) |
| 3563 | tb = Py_None; |
| 3564 | Py_INCREF(tb); |
| 3565 | PUSH(tb); |
| 3566 | PUSH(val); |
| 3567 | PUSH(exc); |
| 3568 | why = WHY_NOT; |
| 3569 | JUMPTO(handler); |
| 3570 | break; |
| 3571 | } |
| 3572 | if (b->b_type == SETUP_FINALLY) { |
| 3573 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3574 | PUSH(retval); |
| 3575 | PUSH(PyLong_FromLong((long)why)); |
| 3576 | why = WHY_NOT; |
| 3577 | JUMPTO(b->b_handler); |
| 3578 | break; |
| 3579 | } |
| 3580 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3584 | if (why != WHY_NOT) |
| 3585 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3586 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3587 | assert(!PyErr_Occurred()); |
| 3588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3589 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | assert(why != WHY_YIELD); |
| 3592 | /* Pop remaining stack entries. */ |
| 3593 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3594 | PyObject *o = POP(); |
| 3595 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3596 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | if (why != WHY_RETURN) |
| 3599 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3600 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 3601 | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3602 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3603 | fast_yield: |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3604 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 3605 | |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3606 | /* The purpose of this block is to put aside the generator's exception |
| 3607 | state and restore that of the calling frame. If the current |
| 3608 | exception state is from the caller, we clear the exception values |
| 3609 | on the generator frame, so they are not swapped back in latter. The |
| 3610 | origin of the current exception state is determined by checking for |
| 3611 | except handler blocks, which we must be in iff a new exception |
| 3612 | state came into existence in this frame. (An uncaught exception |
| 3613 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 3614 | int i; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3615 | for (i = 0; i < f->f_iblock; i++) { |
| 3616 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3617 | break; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3618 | } |
| 3619 | } |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3620 | if (i == f->f_iblock) |
| 3621 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3622 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3623 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3624 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3625 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 3626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3627 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3628 | if (tstate->c_tracefunc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | if (why == WHY_RETURN || why == WHY_YIELD) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3630 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3631 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3633 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3634 | why = WHY_EXCEPTION; |
| 3635 | } |
| 3636 | } |
| 3637 | else if (why == WHY_EXCEPTION) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3638 | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3639 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | PyTrace_RETURN, NULL); |
| 3641 | } |
| 3642 | } |
| 3643 | if (tstate->c_profilefunc) { |
| 3644 | if (why == WHY_EXCEPTION) |
| 3645 | call_trace_protected(tstate->c_profilefunc, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3646 | tstate->c_profileobj, |
| 3647 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | PyTrace_RETURN, NULL); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3649 | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
| 3650 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3652 | Py_CLEAR(retval); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 3653 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | } |
| 3655 | } |
| 3656 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3659 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3660 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3661 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3663 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3665 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3666 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3669 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3670 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3671 | { |
| 3672 | int err; |
| 3673 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3674 | PyObject *name_str, *comma, *tail, *tmp; |
| 3675 | |
| 3676 | assert(PyList_CheckExact(names)); |
| 3677 | assert(len >= 1); |
| 3678 | /* Deal with the joys of natural language. */ |
| 3679 | switch (len) { |
| 3680 | case 1: |
| 3681 | name_str = PyList_GET_ITEM(names, 0); |
| 3682 | Py_INCREF(name_str); |
| 3683 | break; |
| 3684 | case 2: |
| 3685 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3686 | PyList_GET_ITEM(names, len - 2), |
| 3687 | PyList_GET_ITEM(names, len - 1)); |
| 3688 | break; |
| 3689 | default: |
| 3690 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3691 | PyList_GET_ITEM(names, len - 2), |
| 3692 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3693 | if (tail == NULL) |
| 3694 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3695 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3696 | fail, but we can't be too careful. */ |
| 3697 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3698 | if (err == -1) { |
| 3699 | Py_DECREF(tail); |
| 3700 | return; |
| 3701 | } |
| 3702 | /* Stitch everything up into a nice comma-separated list. */ |
| 3703 | comma = PyUnicode_FromString(", "); |
| 3704 | if (comma == NULL) { |
| 3705 | Py_DECREF(tail); |
| 3706 | return; |
| 3707 | } |
| 3708 | tmp = PyUnicode_Join(comma, names); |
| 3709 | Py_DECREF(comma); |
| 3710 | if (tmp == NULL) { |
| 3711 | Py_DECREF(tail); |
| 3712 | return; |
| 3713 | } |
| 3714 | name_str = PyUnicode_Concat(tmp, tail); |
| 3715 | Py_DECREF(tmp); |
| 3716 | Py_DECREF(tail); |
| 3717 | break; |
| 3718 | } |
| 3719 | if (name_str == NULL) |
| 3720 | return; |
| 3721 | PyErr_Format(PyExc_TypeError, |
| 3722 | "%U() missing %i required %s argument%s: %U", |
| 3723 | co->co_name, |
| 3724 | len, |
| 3725 | kind, |
| 3726 | len == 1 ? "" : "s", |
| 3727 | name_str); |
| 3728 | Py_DECREF(name_str); |
| 3729 | } |
| 3730 | |
| 3731 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3732 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3733 | PyObject **fastlocals) |
| 3734 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3735 | Py_ssize_t i, j = 0; |
| 3736 | Py_ssize_t start, end; |
| 3737 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3738 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3739 | PyObject *missing_names; |
| 3740 | |
| 3741 | /* Compute the names of the arguments that are missing. */ |
| 3742 | missing_names = PyList_New(missing); |
| 3743 | if (missing_names == NULL) |
| 3744 | return; |
| 3745 | if (positional) { |
| 3746 | start = 0; |
| 3747 | end = co->co_argcount - defcount; |
| 3748 | } |
| 3749 | else { |
| 3750 | start = co->co_argcount; |
| 3751 | end = start + co->co_kwonlyargcount; |
| 3752 | } |
| 3753 | for (i = start; i < end; i++) { |
| 3754 | if (GETLOCAL(i) == NULL) { |
| 3755 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3756 | PyObject *name = PyObject_Repr(raw); |
| 3757 | if (name == NULL) { |
| 3758 | Py_DECREF(missing_names); |
| 3759 | return; |
| 3760 | } |
| 3761 | PyList_SET_ITEM(missing_names, j++, name); |
| 3762 | } |
| 3763 | } |
| 3764 | assert(j == missing); |
| 3765 | format_missing(kind, co, missing_names); |
| 3766 | Py_DECREF(missing_names); |
| 3767 | } |
| 3768 | |
| 3769 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3770 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3771 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3772 | { |
| 3773 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3774 | Py_ssize_t kwonly_given = 0; |
| 3775 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3776 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3777 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3778 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3779 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3780 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3781 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3782 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3783 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3784 | } |
| 3785 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3786 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3787 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3788 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3789 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3790 | } |
| 3791 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3792 | plural = (co_argcount != 1); |
| 3793 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3794 | } |
| 3795 | if (sig == NULL) |
| 3796 | return; |
| 3797 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3798 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3799 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3800 | given != 1 ? "s" : "", |
| 3801 | kwonly_given, |
| 3802 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3803 | if (kwonly_sig == NULL) { |
| 3804 | Py_DECREF(sig); |
| 3805 | return; |
| 3806 | } |
| 3807 | } |
| 3808 | else { |
| 3809 | /* This will not fail. */ |
| 3810 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3811 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3812 | } |
| 3813 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3814 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3815 | co->co_name, |
| 3816 | sig, |
| 3817 | plural ? "s" : "", |
| 3818 | given, |
| 3819 | kwonly_sig, |
| 3820 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3821 | Py_DECREF(sig); |
| 3822 | Py_DECREF(kwonly_sig); |
| 3823 | } |
| 3824 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3825 | /* This is gonna seem *real weird*, but if you put some other code between |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 3826 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3827 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3828 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3829 | static PyObject * |
| 3830 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3831 | PyObject **args, Py_ssize_t argcount, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3832 | PyObject **kwnames, PyObject **kwargs, |
| 3833 | Py_ssize_t kwcount, int kwstep, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3834 | PyObject **defs, Py_ssize_t defcount, |
| 3835 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3836 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3837 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3838 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3839 | PyFrameObject *f; |
| 3840 | PyObject *retval = NULL; |
| 3841 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3842 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3843 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3844 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3845 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3846 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3848 | if (globals == NULL) { |
| 3849 | PyErr_SetString(PyExc_SystemError, |
| 3850 | "PyEval_EvalCodeEx: NULL globals"); |
| 3851 | return NULL; |
| 3852 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3853 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3854 | /* Create the frame */ |
| 3855 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3856 | assert(tstate != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | f = PyFrame_New(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3858 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3859 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3860 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | fastlocals = f->f_localsplus; |
| 3862 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3863 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3864 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3865 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3866 | kwdict = PyDict_New(); |
| 3867 | if (kwdict == NULL) |
| 3868 | goto fail; |
| 3869 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3870 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3871 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3872 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3873 | SETLOCAL(i, kwdict); |
| 3874 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3875 | else { |
| 3876 | kwdict = NULL; |
| 3877 | } |
| 3878 | |
| 3879 | /* Copy positional arguments into local variables */ |
| 3880 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3881 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3882 | } |
| 3883 | else { |
| 3884 | n = argcount; |
| 3885 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3886 | for (i = 0; i < n; i++) { |
| 3887 | x = args[i]; |
| 3888 | Py_INCREF(x); |
| 3889 | SETLOCAL(i, x); |
| 3890 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3891 | |
| 3892 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3893 | if (co->co_flags & CO_VARARGS) { |
| 3894 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3895 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3896 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3897 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3898 | SETLOCAL(total_args, u); |
| 3899 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | x = args[i]; |
| 3901 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3902 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3904 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3905 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3906 | /* Handle keyword arguments passed as two strided arrays */ |
| 3907 | kwcount *= kwstep; |
| 3908 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3909 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3910 | PyObject *keyword = kwnames[i]; |
| 3911 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3912 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3913 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3914 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3915 | PyErr_Format(PyExc_TypeError, |
| 3916 | "%U() keywords must be strings", |
| 3917 | co->co_name); |
| 3918 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3919 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3920 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3921 | /* Speed hack: do raw pointer compares. As names are |
| 3922 | normally interned this should almost always hit. */ |
| 3923 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3924 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3925 | PyObject *name = co_varnames[j]; |
| 3926 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3927 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3928 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3929 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3930 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3931 | /* Slow fallback, just in case */ |
| 3932 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3933 | PyObject *name = co_varnames[j]; |
| 3934 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 3935 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3936 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3937 | } |
| 3938 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3940 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3941 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3942 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3943 | if (j >= total_args && kwdict == NULL) { |
| 3944 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3945 | "%U() got an unexpected keyword argument '%S'", |
| 3946 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3947 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3948 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3949 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 3950 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3951 | goto fail; |
| 3952 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3953 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3954 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3955 | kw_found: |
| 3956 | if (GETLOCAL(j) != NULL) { |
| 3957 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3958 | "%U() got multiple values for argument '%S'", |
| 3959 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3960 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3962 | Py_INCREF(value); |
| 3963 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3965 | |
| 3966 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3967 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3968 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | goto fail; |
| 3970 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3971 | |
| 3972 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3973 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3974 | Py_ssize_t m = co->co_argcount - defcount; |
| 3975 | Py_ssize_t missing = 0; |
| 3976 | for (i = argcount; i < m; i++) { |
| 3977 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3978 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3979 | } |
| 3980 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3981 | if (missing) { |
| 3982 | missing_arguments(co, missing, defcount, fastlocals); |
| 3983 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3984 | } |
| 3985 | if (n > m) |
| 3986 | i = n - m; |
| 3987 | else |
| 3988 | i = 0; |
| 3989 | for (; i < defcount; i++) { |
| 3990 | if (GETLOCAL(m+i) == NULL) { |
| 3991 | PyObject *def = defs[i]; |
| 3992 | Py_INCREF(def); |
| 3993 | SETLOCAL(m+i, def); |
| 3994 | } |
| 3995 | } |
| 3996 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3997 | |
| 3998 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3999 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4000 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4001 | for (i = co->co_argcount; i < total_args; i++) { |
| 4002 | PyObject *name; |
| 4003 | if (GETLOCAL(i) != NULL) |
| 4004 | continue; |
| 4005 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4006 | if (kwdefs != NULL) { |
| 4007 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 4008 | if (def) { |
| 4009 | Py_INCREF(def); |
| 4010 | SETLOCAL(i, def); |
| 4011 | continue; |
| 4012 | } |
| 4013 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4014 | missing++; |
| 4015 | } |
| 4016 | if (missing) { |
| 4017 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4018 | goto fail; |
| 4019 | } |
| 4020 | } |
| 4021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4023 | vars into frame. */ |
| 4024 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4025 | PyObject *c; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4026 | int arg; |
| 4027 | /* Possibly account for the cell variable being an argument. */ |
| 4028 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4029 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4030 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4031 | /* Clear the local copy. */ |
| 4032 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4033 | } |
| 4034 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4035 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4036 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4037 | if (c == NULL) |
| 4038 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4039 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4040 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4041 | |
| 4042 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4043 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4044 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4045 | Py_INCREF(o); |
| 4046 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4047 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4048 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4049 | /* Handle generator/coroutine/asynchronous generator */ |
| 4050 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4051 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4052 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4053 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4054 | |
| 4055 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 4056 | assert(coro_wrapper != NULL); |
| 4057 | PyErr_Format(PyExc_RuntimeError, |
| 4058 | "coroutine wrapper %.200R attempted " |
| 4059 | "to recursively wrap %.200R", |
| 4060 | coro_wrapper, |
| 4061 | co); |
| 4062 | goto fail; |
| 4063 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | /* Don't need to keep the reference to f_back, it will be set |
| 4066 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4067 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | /* Create a new generator that owns the ready to run frame |
| 4072 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4073 | if (is_coro) { |
| 4074 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4075 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4076 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4077 | } else { |
| 4078 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4079 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4080 | if (gen == NULL) |
| 4081 | return NULL; |
| 4082 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4083 | if (is_coro && coro_wrapper != NULL) { |
| 4084 | PyObject *wrapped; |
| 4085 | tstate->in_coroutine_wrapper = 1; |
| 4086 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 4087 | tstate->in_coroutine_wrapper = 0; |
| 4088 | return wrapped; |
| 4089 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4090 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4091 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4094 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4095 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4096 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4098 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4099 | which can call back into Python. While we're done with the |
| 4100 | current Python frame (f), the associated C stack is still in use, |
| 4101 | so recursion_depth must be boosted for the duration. |
| 4102 | */ |
| 4103 | assert(tstate != NULL); |
| 4104 | ++tstate->recursion_depth; |
| 4105 | Py_DECREF(f); |
| 4106 | --tstate->recursion_depth; |
| 4107 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4110 | PyObject * |
| 4111 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4112 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 4113 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
| 4114 | { |
| 4115 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4116 | args, argcount, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4117 | kws, kws + 1, kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4118 | defs, defcount, |
| 4119 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4120 | NULL, NULL); |
| 4121 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4122 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4123 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4124 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4127 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4129 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4130 | return NULL; |
| 4131 | } |
| 4132 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4136 | /* These 3 functions deal with the exception state of generators. */ |
| 4137 | |
| 4138 | static void |
| 4139 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4140 | { |
| 4141 | PyObject *type, *value, *traceback; |
| 4142 | Py_XINCREF(tstate->exc_type); |
| 4143 | Py_XINCREF(tstate->exc_value); |
| 4144 | Py_XINCREF(tstate->exc_traceback); |
| 4145 | type = f->f_exc_type; |
| 4146 | value = f->f_exc_value; |
| 4147 | traceback = f->f_exc_traceback; |
| 4148 | f->f_exc_type = tstate->exc_type; |
| 4149 | f->f_exc_value = tstate->exc_value; |
| 4150 | f->f_exc_traceback = tstate->exc_traceback; |
| 4151 | Py_XDECREF(type); |
| 4152 | Py_XDECREF(value); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 4153 | Py_XDECREF(traceback); |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | static void |
| 4157 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4158 | { |
| 4159 | PyObject *tmp; |
| 4160 | tmp = tstate->exc_type; |
| 4161 | tstate->exc_type = f->f_exc_type; |
| 4162 | f->f_exc_type = tmp; |
| 4163 | tmp = tstate->exc_value; |
| 4164 | tstate->exc_value = f->f_exc_value; |
| 4165 | f->f_exc_value = tmp; |
| 4166 | tmp = tstate->exc_traceback; |
| 4167 | tstate->exc_traceback = f->f_exc_traceback; |
| 4168 | f->f_exc_traceback = tmp; |
| 4169 | } |
| 4170 | |
| 4171 | static void |
| 4172 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4173 | { |
| 4174 | PyObject *type, *value, *tb; |
| 4175 | type = tstate->exc_type; |
| 4176 | value = tstate->exc_value; |
| 4177 | tb = tstate->exc_traceback; |
| 4178 | tstate->exc_type = f->f_exc_type; |
| 4179 | tstate->exc_value = f->f_exc_value; |
| 4180 | tstate->exc_traceback = f->f_exc_traceback; |
| 4181 | f->f_exc_type = NULL; |
| 4182 | f->f_exc_value = NULL; |
| 4183 | f->f_exc_traceback = NULL; |
| 4184 | Py_XDECREF(type); |
| 4185 | Py_XDECREF(value); |
| 4186 | Py_XDECREF(tb); |
| 4187 | } |
| 4188 | |
| 4189 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4190 | /* Logic for the raise statement (too complicated for inlining). |
| 4191 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4192 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4193 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4195 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | if (exc == NULL) { |
| 4198 | /* Reraise */ |
| 4199 | PyThreadState *tstate = PyThreadState_GET(); |
| 4200 | PyObject *tb; |
| 4201 | type = tstate->exc_type; |
| 4202 | value = tstate->exc_value; |
| 4203 | tb = tstate->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4204 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | PyErr_SetString(PyExc_RuntimeError, |
| 4206 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4207 | return 0; |
| 4208 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | Py_XINCREF(type); |
| 4210 | Py_XINCREF(value); |
| 4211 | Py_XINCREF(tb); |
| 4212 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4213 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | /* We support the following forms of raise: |
| 4217 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4218 | raise <instance> |
| 4219 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4221 | if (PyExceptionClass_Check(exc)) { |
| 4222 | type = exc; |
| 4223 | value = PyObject_CallObject(exc, NULL); |
| 4224 | if (value == NULL) |
| 4225 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4226 | if (!PyExceptionInstance_Check(value)) { |
| 4227 | PyErr_Format(PyExc_TypeError, |
| 4228 | "calling %R should have returned an instance of " |
| 4229 | "BaseException, not %R", |
| 4230 | type, Py_TYPE(value)); |
| 4231 | goto raise_error; |
| 4232 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | } |
| 4234 | else if (PyExceptionInstance_Check(exc)) { |
| 4235 | value = exc; |
| 4236 | type = PyExceptionInstance_Class(exc); |
| 4237 | Py_INCREF(type); |
| 4238 | } |
| 4239 | else { |
| 4240 | /* Not something you can raise. You get an exception |
| 4241 | anyway, just not what you specified :-) */ |
| 4242 | Py_DECREF(exc); |
| 4243 | PyErr_SetString(PyExc_TypeError, |
| 4244 | "exceptions must derive from BaseException"); |
| 4245 | goto raise_error; |
| 4246 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | if (cause) { |
| 4249 | PyObject *fixed_cause; |
| 4250 | if (PyExceptionClass_Check(cause)) { |
| 4251 | fixed_cause = PyObject_CallObject(cause, NULL); |
| 4252 | if (fixed_cause == NULL) |
| 4253 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4254 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4256 | else if (PyExceptionInstance_Check(cause)) { |
| 4257 | fixed_cause = cause; |
| 4258 | } |
| 4259 | else if (cause == Py_None) { |
| 4260 | Py_DECREF(cause); |
| 4261 | fixed_cause = NULL; |
| 4262 | } |
| 4263 | else { |
| 4264 | PyErr_SetString(PyExc_TypeError, |
| 4265 | "exception causes must derive from " |
| 4266 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | goto raise_error; |
| 4268 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4269 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4270 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | PyErr_SetObject(type, value); |
| 4273 | /* PyErr_SetObject incref's its arguments */ |
| 4274 | Py_XDECREF(value); |
| 4275 | Py_XDECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4276 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4277 | |
| 4278 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | Py_XDECREF(value); |
| 4280 | Py_XDECREF(type); |
| 4281 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4282 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4285 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4286 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4287 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4288 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4289 | with a variable target. |
| 4290 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4291 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4292 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4293 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | int i = 0, j = 0; |
| 4296 | Py_ssize_t ll = 0; |
| 4297 | PyObject *it; /* iter(v) */ |
| 4298 | PyObject *w; |
| 4299 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4301 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4303 | it = PyObject_GetIter(v); |
| 4304 | if (it == NULL) |
| 4305 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | for (; i < argcnt; i++) { |
| 4308 | w = PyIter_Next(it); |
| 4309 | if (w == NULL) { |
| 4310 | /* Iterator done, via error or exhaustion. */ |
| 4311 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4312 | if (argcntafter == -1) { |
| 4313 | PyErr_Format(PyExc_ValueError, |
| 4314 | "not enough values to unpack (expected %d, got %d)", |
| 4315 | argcnt, i); |
| 4316 | } |
| 4317 | else { |
| 4318 | PyErr_Format(PyExc_ValueError, |
| 4319 | "not enough values to unpack " |
| 4320 | "(expected at least %d, got %d)", |
| 4321 | argcnt + argcntafter, i); |
| 4322 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | } |
| 4324 | goto Error; |
| 4325 | } |
| 4326 | *--sp = w; |
| 4327 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4329 | if (argcntafter == -1) { |
| 4330 | /* We better have exhausted the iterator now. */ |
| 4331 | w = PyIter_Next(it); |
| 4332 | if (w == NULL) { |
| 4333 | if (PyErr_Occurred()) |
| 4334 | goto Error; |
| 4335 | Py_DECREF(it); |
| 4336 | return 1; |
| 4337 | } |
| 4338 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4339 | PyErr_Format(PyExc_ValueError, |
| 4340 | "too many values to unpack (expected %d)", |
| 4341 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | goto Error; |
| 4343 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | l = PySequence_List(it); |
| 4346 | if (l == NULL) |
| 4347 | goto Error; |
| 4348 | *--sp = l; |
| 4349 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4351 | ll = PyList_GET_SIZE(l); |
| 4352 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4353 | PyErr_Format(PyExc_ValueError, |
| 4354 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4355 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | goto Error; |
| 4357 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4359 | /* Pop the "after-variable" args off the list. */ |
| 4360 | for (j = argcntafter; j > 0; j--, i++) { |
| 4361 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4362 | } |
| 4363 | /* Resize the list. */ |
| 4364 | Py_SIZE(l) = ll - argcntafter; |
| 4365 | Py_DECREF(it); |
| 4366 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4367 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4368 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | for (; i > 0; i--, sp++) |
| 4370 | Py_DECREF(*sp); |
| 4371 | Py_XDECREF(it); |
| 4372 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4376 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4377 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4378 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | printf("%s ", str); |
| 4381 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4382 | PyErr_Clear(); /* Don't know what else to do */ |
| 4383 | printf("\n"); |
| 4384 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4385 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4386 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4387 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4388 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4389 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4390 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4391 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4392 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4394 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4395 | if (value == NULL) { |
| 4396 | value = Py_None; |
| 4397 | Py_INCREF(value); |
| 4398 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4399 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4400 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4402 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4403 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | return; |
| 4405 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4406 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | Py_DECREF(arg); |
| 4408 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4409 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | else { |
| 4411 | Py_XDECREF(type); |
| 4412 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4413 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4417 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4418 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4419 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | PyObject *type, *value, *traceback; |
| 4423 | int err; |
| 4424 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4425 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | if (err == 0) |
| 4427 | { |
| 4428 | PyErr_Restore(type, value, traceback); |
| 4429 | return 0; |
| 4430 | } |
| 4431 | else { |
| 4432 | Py_XDECREF(type); |
| 4433 | Py_XDECREF(value); |
| 4434 | Py_XDECREF(traceback); |
| 4435 | return -1; |
| 4436 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4439 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4440 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4441 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | int result; |
| 4445 | if (tstate->tracing) |
| 4446 | return 0; |
| 4447 | tstate->tracing++; |
| 4448 | tstate->use_tracing = 0; |
| 4449 | result = func(obj, frame, what, arg); |
| 4450 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4451 | || (tstate->c_profilefunc != NULL)); |
| 4452 | tstate->tracing--; |
| 4453 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4456 | PyObject * |
| 4457 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4458 | { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4459 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | int save_tracing = tstate->tracing; |
| 4461 | int save_use_tracing = tstate->use_tracing; |
| 4462 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | tstate->tracing = 0; |
| 4465 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4466 | || (tstate->c_profilefunc != NULL)); |
| 4467 | result = PyObject_Call(func, args, NULL); |
| 4468 | tstate->tracing = save_tracing; |
| 4469 | tstate->use_tracing = save_use_tracing; |
| 4470 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4473 | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4474 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4475 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4476 | PyThreadState *tstate, PyFrameObject *frame, |
| 4477 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | int result = 0; |
| 4480 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | /* If the last instruction executed isn't in the current |
| 4483 | instruction window, reset the window. |
| 4484 | */ |
| 4485 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4486 | PyAddrPair bounds; |
| 4487 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4488 | &bounds); |
| 4489 | *instr_lb = bounds.ap_lower; |
| 4490 | *instr_ub = bounds.ap_upper; |
| 4491 | } |
| 4492 | /* If the last instruction falls at the start of a line or if |
| 4493 | it represents a jump backwards, update the frame's line |
| 4494 | number and call the trace function. */ |
| 4495 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4496 | frame->f_lineno = line; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4497 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | } |
| 4499 | *instr_prev = frame->f_lasti; |
| 4500 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4503 | void |
| 4504 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | PyThreadState *tstate = PyThreadState_GET(); |
| 4507 | PyObject *temp = tstate->c_profileobj; |
| 4508 | Py_XINCREF(arg); |
| 4509 | tstate->c_profilefunc = NULL; |
| 4510 | tstate->c_profileobj = NULL; |
| 4511 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4512 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4513 | Py_XDECREF(temp); |
| 4514 | tstate->c_profilefunc = func; |
| 4515 | tstate->c_profileobj = arg; |
| 4516 | /* Flag that tracing or profiling is turned on */ |
| 4517 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | void |
| 4521 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | PyThreadState *tstate = PyThreadState_GET(); |
| 4524 | PyObject *temp = tstate->c_traceobj; |
| 4525 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4526 | Py_XINCREF(arg); |
| 4527 | tstate->c_tracefunc = NULL; |
| 4528 | tstate->c_traceobj = NULL; |
| 4529 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4530 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4531 | Py_XDECREF(temp); |
| 4532 | tstate->c_tracefunc = func; |
| 4533 | tstate->c_traceobj = arg; |
| 4534 | /* Flag that tracing or profiling is turned on */ |
| 4535 | tstate->use_tracing = ((func != NULL) |
| 4536 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4539 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4540 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4541 | { |
| 4542 | PyThreadState *tstate = PyThreadState_GET(); |
| 4543 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4544 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4545 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4549 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4550 | { |
| 4551 | PyThreadState *tstate = PyThreadState_GET(); |
| 4552 | return tstate->coroutine_wrapper; |
| 4553 | } |
| 4554 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4555 | void |
| 4556 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4557 | { |
| 4558 | PyThreadState *tstate = PyThreadState_GET(); |
| 4559 | |
| 4560 | Py_XINCREF(firstiter); |
| 4561 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4562 | } |
| 4563 | |
| 4564 | PyObject * |
| 4565 | _PyEval_GetAsyncGenFirstiter(void) |
| 4566 | { |
| 4567 | PyThreadState *tstate = PyThreadState_GET(); |
| 4568 | return tstate->async_gen_firstiter; |
| 4569 | } |
| 4570 | |
| 4571 | void |
| 4572 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4573 | { |
| 4574 | PyThreadState *tstate = PyThreadState_GET(); |
| 4575 | |
| 4576 | Py_XINCREF(finalizer); |
| 4577 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4578 | } |
| 4579 | |
| 4580 | PyObject * |
| 4581 | _PyEval_GetAsyncGenFinalizer(void) |
| 4582 | { |
| 4583 | PyThreadState *tstate = PyThreadState_GET(); |
| 4584 | return tstate->async_gen_finalizer; |
| 4585 | } |
| 4586 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4587 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4588 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4591 | if (current_frame == NULL) |
| 4592 | return PyThreadState_GET()->interp->builtins; |
| 4593 | else |
| 4594 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4597 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4598 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4600 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4601 | if (current_frame == NULL) { |
| 4602 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4607 | return NULL; |
| 4608 | |
| 4609 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4610 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4613 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4614 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4617 | if (current_frame == NULL) |
| 4618 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4619 | |
| 4620 | assert(current_frame->f_globals != NULL); |
| 4621 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4624 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4625 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4626 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | PyThreadState *tstate = PyThreadState_GET(); |
| 4628 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4631 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4632 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4635 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | if (current_frame != NULL) { |
| 4638 | const int codeflags = current_frame->f_code->co_flags; |
| 4639 | const int compilerflags = codeflags & PyCF_MASK; |
| 4640 | if (compilerflags) { |
| 4641 | result = 1; |
| 4642 | cf->cf_flags |= compilerflags; |
| 4643 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4644 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4646 | result = 1; |
| 4647 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4648 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4649 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | } |
| 4651 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4654 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4655 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 4656 | The arg must be a tuple or NULL. The kw must be a dict or NULL. */ |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4657 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4658 | PyObject * |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4659 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4660 | { |
Victor Stinner | 59b356d | 2015-03-16 11:52:32 +0100 | [diff] [blame] | 4661 | #ifdef Py_DEBUG |
| 4662 | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
| 4663 | set. It raises a new exception if parameters are invalid or if |
| 4664 | PyTuple_New() fails, and so the original exception is lost. */ |
| 4665 | assert(!PyErr_Occurred()); |
| 4666 | #endif |
| 4667 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4668 | if (args == NULL) { |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4669 | return _PyObject_FastCallDict(func, NULL, 0, kwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | } |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4671 | |
| 4672 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | PyErr_SetString(PyExc_TypeError, |
| 4674 | "argument list must be a tuple"); |
| 4675 | return NULL; |
| 4676 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4677 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4678 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | PyErr_SetString(PyExc_TypeError, |
| 4680 | "keyword list must be a dictionary"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | return NULL; |
| 4682 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4683 | |
Victor Stinner | 6e2333d | 2016-08-23 00:25:01 +0200 | [diff] [blame] | 4684 | return PyObject_Call(func, args, kwargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4687 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4688 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | if (PyMethod_Check(func)) |
| 4691 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4692 | else if (PyFunction_Check(func)) |
| 4693 | return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name); |
| 4694 | else if (PyCFunction_Check(func)) |
| 4695 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4696 | else |
| 4697 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4700 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4701 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | if (PyMethod_Check(func)) |
| 4704 | return "()"; |
| 4705 | else if (PyFunction_Check(func)) |
| 4706 | return "()"; |
| 4707 | else if (PyCFunction_Check(func)) |
| 4708 | return "()"; |
| 4709 | else |
| 4710 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4713 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4714 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4715 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4716 | tstate, tstate->frame, \ |
| 4717 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | x = NULL; \ |
| 4719 | } \ |
| 4720 | else { \ |
| 4721 | x = call; \ |
| 4722 | if (tstate->c_profilefunc != NULL) { \ |
| 4723 | if (x == NULL) { \ |
| 4724 | call_trace_protected(tstate->c_profilefunc, \ |
| 4725 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4726 | tstate, tstate->frame, \ |
| 4727 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | /* XXX should pass (type, value, tb) */ \ |
| 4729 | } else { \ |
| 4730 | if (call_trace(tstate->c_profilefunc, \ |
| 4731 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4732 | tstate, tstate->frame, \ |
| 4733 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4734 | Py_DECREF(x); \ |
| 4735 | x = NULL; \ |
| 4736 | } \ |
| 4737 | } \ |
| 4738 | } \ |
| 4739 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4740 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | x = call; \ |
| 4742 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4743 | |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4744 | static PyObject * |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4745 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4746 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4747 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4748 | PyObject *func = *pfunc; |
| 4749 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4750 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4751 | Py_ssize_t nargs = oparg - nkwargs; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4752 | PyObject **stack; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4754 | /* Always dispatch PyCFunction first, because these are |
| 4755 | presumed to be the most frequent callable object. |
| 4756 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4757 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4758 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | PCALL(PCALL_CFUNCTION); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4761 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4762 | stack = (*pp_stack) - nargs - nkwargs; |
| 4763 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4764 | } |
| 4765 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4766 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 4767 | /* optimize access to bound methods */ |
| 4768 | PyObject *self = PyMethod_GET_SELF(func); |
| 4769 | PCALL(PCALL_METHOD); |
| 4770 | PCALL(PCALL_BOUND_METHOD); |
| 4771 | Py_INCREF(self); |
| 4772 | func = PyMethod_GET_FUNCTION(func); |
| 4773 | Py_INCREF(func); |
| 4774 | Py_SETREF(*pfunc, self); |
| 4775 | nargs++; |
| 4776 | } |
| 4777 | else { |
| 4778 | Py_INCREF(func); |
| 4779 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4780 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4781 | stack = (*pp_stack) - nargs - nkwargs; |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4782 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4783 | if (PyFunction_Check(func)) { |
| 4784 | x = fast_function(func, stack, nargs, kwnames); |
| 4785 | } |
| 4786 | else { |
| 4787 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4788 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4789 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4790 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4791 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4792 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4793 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | /* Clear the stack of the function object. Also removes |
| 4796 | the arguments in case they weren't consumed already |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4797 | (fast_function() and err_args() leave them on the stack). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4798 | */ |
| 4799 | while ((*pp_stack) > pfunc) { |
| 4800 | w = EXT_POP(*pp_stack); |
| 4801 | Py_DECREF(w); |
| 4802 | PCALL(PCALL_POP); |
| 4803 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4808 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4809 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4810 | For the simplest case -- a function that takes only positional |
| 4811 | arguments and is called with only positional arguments -- it |
| 4812 | inlines the most primitive frame setup code from |
| 4813 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4814 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4815 | */ |
| 4816 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4817 | static PyObject* |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4818 | _PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
| 4819 | PyObject *globals) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4820 | { |
| 4821 | PyFrameObject *f; |
| 4822 | PyThreadState *tstate = PyThreadState_GET(); |
| 4823 | PyObject **fastlocals; |
| 4824 | Py_ssize_t i; |
| 4825 | PyObject *result; |
| 4826 | |
| 4827 | PCALL(PCALL_FASTER_FUNCTION); |
| 4828 | assert(globals != NULL); |
| 4829 | /* XXX Perhaps we should create a specialized |
| 4830 | PyFrame_New() that doesn't take locals, but does |
| 4831 | take builtins without sanity checking them. |
| 4832 | */ |
| 4833 | assert(tstate != NULL); |
| 4834 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4835 | if (f == NULL) { |
| 4836 | return NULL; |
| 4837 | } |
| 4838 | |
| 4839 | fastlocals = f->f_localsplus; |
| 4840 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4841 | for (i = 0; i < nargs; i++) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4842 | Py_INCREF(*args); |
| 4843 | fastlocals[i] = *args++; |
| 4844 | } |
| 4845 | result = PyEval_EvalFrameEx(f,0); |
| 4846 | |
| 4847 | ++tstate->recursion_depth; |
| 4848 | Py_DECREF(f); |
| 4849 | --tstate->recursion_depth; |
| 4850 | |
| 4851 | return result; |
| 4852 | } |
| 4853 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4854 | static PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4855 | fast_function(PyObject *func, PyObject **stack, |
| 4856 | Py_ssize_t nargs, PyObject *kwnames) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4858 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4859 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4860 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4861 | PyObject *kwdefs, *closure, *name, *qualname; |
| 4862 | PyObject **d; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4863 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4864 | Py_ssize_t nd; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4865 | |
| 4866 | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | PCALL(PCALL_FUNCTION); |
| 4869 | PCALL(PCALL_FAST_FUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4870 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4871 | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4872 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4873 | { |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4874 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4875 | return _PyFunction_FastCall(co, stack, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4876 | } |
| 4877 | else if (nargs == 0 && argdefs != NULL |
| 4878 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4879 | /* function called with no arguments, but all parameters have |
| 4880 | a default value: use default values as arguments .*/ |
| 4881 | stack = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4882 | return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4883 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4884 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4885 | |
| 4886 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4887 | closure = PyFunction_GET_CLOSURE(func); |
| 4888 | name = ((PyFunctionObject *)func) -> func_name; |
| 4889 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4891 | if (argdefs != NULL) { |
| 4892 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4893 | nd = Py_SIZE(argdefs); |
| 4894 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4895 | else { |
| 4896 | d = NULL; |
| 4897 | nd = 0; |
| 4898 | } |
| 4899 | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4900 | stack, nargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4901 | nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL, |
| 4902 | stack + nargs, |
| 4903 | nkwargs, 1, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4904 | d, (int)nd, kwdefs, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4905 | closure, name, qualname); |
| 4906 | } |
| 4907 | |
| 4908 | PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4909 | _PyFunction_FastCallKeywords(PyObject *func, PyObject **stack, |
| 4910 | Py_ssize_t nargs, PyObject *kwnames) |
| 4911 | { |
| 4912 | return fast_function(func, stack, nargs, kwnames); |
| 4913 | } |
| 4914 | |
| 4915 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4916 | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4917 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4918 | { |
| 4919 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4920 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4921 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4922 | PyObject *kwdefs, *closure, *name, *qualname; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4923 | PyObject *kwtuple, **k; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4924 | PyObject **d; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4925 | Py_ssize_t nd, nk; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4926 | PyObject *result; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4927 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4928 | assert(func != NULL); |
| 4929 | assert(nargs >= 0); |
| 4930 | assert(nargs == 0 || args != NULL); |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4931 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4932 | |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4933 | PCALL(PCALL_FUNCTION); |
| 4934 | PCALL(PCALL_FAST_FUNCTION); |
| 4935 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4936 | if (co->co_kwonlyargcount == 0 && |
| 4937 | (kwargs == NULL || PyDict_Size(kwargs) == 0) && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4938 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4939 | { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4940 | /* Fast paths */ |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4941 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4942 | return _PyFunction_FastCall(co, args, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4943 | } |
| 4944 | else if (nargs == 0 && argdefs != NULL |
| 4945 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4946 | /* function called with no arguments, but all parameters have |
| 4947 | a default value: use default values as arguments .*/ |
| 4948 | args = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4949 | return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4950 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4951 | } |
| 4952 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4953 | if (kwargs != NULL) { |
| 4954 | Py_ssize_t pos, i; |
| 4955 | nk = PyDict_Size(kwargs); |
| 4956 | |
| 4957 | kwtuple = PyTuple_New(2 * nk); |
| 4958 | if (kwtuple == NULL) { |
| 4959 | return NULL; |
| 4960 | } |
| 4961 | |
| 4962 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 4963 | pos = i = 0; |
| 4964 | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
| 4965 | Py_INCREF(k[i]); |
| 4966 | Py_INCREF(k[i+1]); |
| 4967 | i += 2; |
| 4968 | } |
| 4969 | nk = i / 2; |
| 4970 | } |
| 4971 | else { |
| 4972 | kwtuple = NULL; |
| 4973 | k = NULL; |
| 4974 | nk = 0; |
| 4975 | } |
| 4976 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4977 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4978 | closure = PyFunction_GET_CLOSURE(func); |
| 4979 | name = ((PyFunctionObject *)func) -> func_name; |
| 4980 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4981 | |
| 4982 | if (argdefs != NULL) { |
| 4983 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4984 | nd = Py_SIZE(argdefs); |
| 4985 | } |
| 4986 | else { |
| 4987 | d = NULL; |
| 4988 | nd = 0; |
| 4989 | } |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4990 | |
| 4991 | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
| 4992 | args, nargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4993 | k, k + 1, nk, 2, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4994 | d, nd, kwdefs, |
| 4995 | closure, name, qualname); |
| 4996 | Py_XDECREF(kwtuple); |
| 4997 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5001 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5002 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5003 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | /* At this point, we have to look at the type of func to |
| 5005 | update the call stats properly. Do it here so as to avoid |
| 5006 | exposing the call stats machinery outside ceval.c |
| 5007 | */ |
| 5008 | if (PyFunction_Check(func)) |
| 5009 | PCALL(PCALL_FUNCTION); |
| 5010 | else if (PyMethod_Check(func)) |
| 5011 | PCALL(PCALL_METHOD); |
| 5012 | else if (PyType_Check(func)) |
| 5013 | PCALL(PCALL_TYPE); |
| 5014 | else if (PyCFunction_Check(func)) |
| 5015 | PCALL(PCALL_CFUNCTION); |
| 5016 | else |
| 5017 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5018 | #endif |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | if (PyCFunction_Check(func)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5021 | PyObject *result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5022 | PyThreadState *tstate = PyThreadState_GET(); |
| 5023 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5024 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5025 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5026 | else { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5027 | return PyObject_Call(func, callargs, kwdict); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5028 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5031 | /* Extract a slice index from a PyLong or an object with the |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 5032 | nb_index slot defined, and store in *pi. |
| 5033 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 5034 | and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 5035 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5036 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 5037 | /* Note: If v is NULL, return success without storing into *pi. This |
| 5038 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 5039 | called by the SLICE opcode with v and/or w equal to NULL. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5040 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5041 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5042 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | if (v != NULL) { |
| 5045 | Py_ssize_t x; |
| 5046 | if (PyIndex_Check(v)) { |
| 5047 | x = PyNumber_AsSsize_t(v, NULL); |
| 5048 | if (x == -1 && PyErr_Occurred()) |
| 5049 | return 0; |
| 5050 | } |
| 5051 | else { |
| 5052 | PyErr_SetString(PyExc_TypeError, |
| 5053 | "slice indices must be integers or " |
| 5054 | "None or have an __index__ method"); |
| 5055 | return 0; |
| 5056 | } |
| 5057 | *pi = x; |
| 5058 | } |
| 5059 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5062 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5064 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5065 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 5066 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | int res = 0; |
| 5069 | switch (op) { |
| 5070 | case PyCmp_IS: |
| 5071 | res = (v == w); |
| 5072 | break; |
| 5073 | case PyCmp_IS_NOT: |
| 5074 | res = (v != w); |
| 5075 | break; |
| 5076 | case PyCmp_IN: |
| 5077 | res = PySequence_Contains(w, v); |
| 5078 | if (res < 0) |
| 5079 | return NULL; |
| 5080 | break; |
| 5081 | case PyCmp_NOT_IN: |
| 5082 | res = PySequence_Contains(w, v); |
| 5083 | if (res < 0) |
| 5084 | return NULL; |
| 5085 | res = !res; |
| 5086 | break; |
| 5087 | case PyCmp_EXC_MATCH: |
| 5088 | if (PyTuple_Check(w)) { |
| 5089 | Py_ssize_t i, length; |
| 5090 | length = PyTuple_Size(w); |
| 5091 | for (i = 0; i < length; i += 1) { |
| 5092 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5093 | if (!PyExceptionClass_Check(exc)) { |
| 5094 | PyErr_SetString(PyExc_TypeError, |
| 5095 | CANNOT_CATCH_MSG); |
| 5096 | return NULL; |
| 5097 | } |
| 5098 | } |
| 5099 | } |
| 5100 | else { |
| 5101 | if (!PyExceptionClass_Check(w)) { |
| 5102 | PyErr_SetString(PyExc_TypeError, |
| 5103 | CANNOT_CATCH_MSG); |
| 5104 | return NULL; |
| 5105 | } |
| 5106 | } |
| 5107 | res = PyErr_GivenExceptionMatches(v, w); |
| 5108 | break; |
| 5109 | default: |
| 5110 | return PyObject_RichCompare(v, w, op); |
| 5111 | } |
| 5112 | v = res ? Py_True : Py_False; |
| 5113 | Py_INCREF(v); |
| 5114 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5117 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5118 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 5119 | { |
| 5120 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5121 | PyObject *import_func, *res; |
| 5122 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5123 | |
| 5124 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 5125 | if (import_func == NULL) { |
| 5126 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 5127 | return NULL; |
| 5128 | } |
| 5129 | |
| 5130 | /* Fast path for not overloaded __import__. */ |
| 5131 | if (import_func == PyThreadState_GET()->interp->import_func) { |
| 5132 | int ilevel = _PyLong_AsInt(level); |
| 5133 | if (ilevel == -1 && PyErr_Occurred()) { |
| 5134 | return NULL; |
| 5135 | } |
| 5136 | res = PyImport_ImportModuleLevelObject( |
| 5137 | name, |
| 5138 | f->f_globals, |
| 5139 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5140 | fromlist, |
| 5141 | ilevel); |
| 5142 | return res; |
| 5143 | } |
| 5144 | |
| 5145 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5146 | |
| 5147 | stack[0] = name; |
| 5148 | stack[1] = f->f_globals; |
| 5149 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5150 | stack[3] = fromlist; |
| 5151 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5152 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5153 | Py_DECREF(import_func); |
| 5154 | return res; |
| 5155 | } |
| 5156 | |
| 5157 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5158 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5161 | _Py_IDENTIFIER(__name__); |
| 5162 | PyObject *fullmodname, *pkgname; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | x = PyObject_GetAttr(v, name); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5165 | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5166 | return x; |
| 5167 | /* Issue #17636: in case this failed because of a circular relative |
| 5168 | import, try to fallback on reading the module directly from |
| 5169 | sys.modules. */ |
| 5170 | PyErr_Clear(); |
| 5171 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5172 | if (pkgname == NULL) { |
| 5173 | goto error; |
| 5174 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5175 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
| 5176 | Py_DECREF(pkgname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5177 | if (fullmodname == NULL) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5178 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5179 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5180 | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5181 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5182 | if (x == NULL) { |
| 5183 | goto error; |
| 5184 | } |
| 5185 | Py_INCREF(x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5186 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5187 | error: |
| 5188 | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
| 5189 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5190 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5191 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5192 | static int |
| 5193 | import_all_from(PyObject *locals, PyObject *v) |
| 5194 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5195 | _Py_IDENTIFIER(__all__); |
| 5196 | _Py_IDENTIFIER(__dict__); |
| 5197 | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | PyObject *dict, *name, *value; |
| 5199 | int skip_leading_underscores = 0; |
| 5200 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5202 | if (all == NULL) { |
| 5203 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5204 | return -1; /* Unexpected error */ |
| 5205 | PyErr_Clear(); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5206 | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5207 | if (dict == NULL) { |
| 5208 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5209 | return -1; |
| 5210 | PyErr_SetString(PyExc_ImportError, |
| 5211 | "from-import-* object has no __dict__ and no __all__"); |
| 5212 | return -1; |
| 5213 | } |
| 5214 | all = PyMapping_Keys(dict); |
| 5215 | Py_DECREF(dict); |
| 5216 | if (all == NULL) |
| 5217 | return -1; |
| 5218 | skip_leading_underscores = 1; |
| 5219 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | for (pos = 0, err = 0; ; pos++) { |
| 5222 | name = PySequence_GetItem(all, pos); |
| 5223 | if (name == NULL) { |
| 5224 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 5225 | err = -1; |
| 5226 | else |
| 5227 | PyErr_Clear(); |
| 5228 | break; |
| 5229 | } |
| 5230 | if (skip_leading_underscores && |
| 5231 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5232 | PyUnicode_READY(name) != -1 && |
| 5233 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | { |
| 5235 | Py_DECREF(name); |
| 5236 | continue; |
| 5237 | } |
| 5238 | value = PyObject_GetAttr(v, name); |
| 5239 | if (value == NULL) |
| 5240 | err = -1; |
| 5241 | else if (PyDict_CheckExact(locals)) |
| 5242 | err = PyDict_SetItem(locals, name, value); |
| 5243 | else |
| 5244 | err = PyObject_SetItem(locals, name, value); |
| 5245 | Py_DECREF(name); |
| 5246 | Py_XDECREF(value); |
| 5247 | if (err != 0) |
| 5248 | break; |
| 5249 | } |
| 5250 | Py_DECREF(all); |
| 5251 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5254 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5255 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | if (!obj) |
| 5260 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | obj_str = _PyUnicode_AsString(obj); |
| 5263 | if (!obj_str) |
| 5264 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5267 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5268 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5269 | static void |
| 5270 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5271 | { |
| 5272 | PyObject *name; |
| 5273 | /* Don't stomp existing exception */ |
| 5274 | if (PyErr_Occurred()) |
| 5275 | return; |
| 5276 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5277 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5278 | oparg); |
| 5279 | format_exc_check_arg( |
| 5280 | PyExc_UnboundLocalError, |
| 5281 | UNBOUNDLOCAL_ERROR_MSG, |
| 5282 | name); |
| 5283 | } else { |
| 5284 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5285 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5286 | format_exc_check_arg(PyExc_NameError, |
| 5287 | UNBOUNDFREE_ERROR_MSG, name); |
| 5288 | } |
| 5289 | } |
| 5290 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5291 | static PyObject * |
| 5292 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5293 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5294 | { |
| 5295 | PyObject *res; |
| 5296 | if (Py_REFCNT(v) == 2) { |
| 5297 | /* In the common case, there are 2 references to the value |
| 5298 | * stored in 'variable' when the += is performed: one on the |
| 5299 | * value stack (in 'v') and one still stored in the |
| 5300 | * 'variable'. We try to delete the variable now to reduce |
| 5301 | * the refcnt to 1. |
| 5302 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5303 | int opcode, oparg; |
| 5304 | NEXTOPARG(); |
| 5305 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5306 | case STORE_FAST: |
| 5307 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5308 | PyObject **fastlocals = f->f_localsplus; |
| 5309 | if (GETLOCAL(oparg) == v) |
| 5310 | SETLOCAL(oparg, NULL); |
| 5311 | break; |
| 5312 | } |
| 5313 | case STORE_DEREF: |
| 5314 | { |
| 5315 | PyObject **freevars = (f->f_localsplus + |
| 5316 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5317 | PyObject *c = freevars[oparg]; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5318 | if (PyCell_GET(c) == v) |
| 5319 | PyCell_Set(c, NULL); |
| 5320 | break; |
| 5321 | } |
| 5322 | case STORE_NAME: |
| 5323 | { |
| 5324 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5325 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5326 | PyObject *locals = f->f_locals; |
| 5327 | if (PyDict_CheckExact(locals) && |
| 5328 | PyDict_GetItem(locals, name) == v) { |
| 5329 | if (PyDict_DelItem(locals, name) != 0) { |
| 5330 | PyErr_Clear(); |
| 5331 | } |
| 5332 | } |
| 5333 | break; |
| 5334 | } |
| 5335 | } |
| 5336 | } |
| 5337 | res = v; |
| 5338 | PyUnicode_Append(&res, w); |
| 5339 | return res; |
| 5340 | } |
| 5341 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5342 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5343 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5344 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5345 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5347 | int i; |
| 5348 | PyObject *l = PyList_New(256); |
| 5349 | if (l == NULL) return NULL; |
| 5350 | for (i = 0; i < 256; i++) { |
| 5351 | PyObject *x = PyLong_FromLong(a[i]); |
| 5352 | if (x == NULL) { |
| 5353 | Py_DECREF(l); |
| 5354 | return NULL; |
| 5355 | } |
| 5356 | PyList_SetItem(l, i, x); |
| 5357 | } |
| 5358 | for (i = 0; i < 256; i++) |
| 5359 | a[i] = 0; |
| 5360 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5364 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5365 | { |
| 5366 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5367 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5368 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5369 | int i; |
| 5370 | PyObject *l = PyList_New(257); |
| 5371 | if (l == NULL) return NULL; |
| 5372 | for (i = 0; i < 257; i++) { |
| 5373 | PyObject *x = getarray(dxpairs[i]); |
| 5374 | if (x == NULL) { |
| 5375 | Py_DECREF(l); |
| 5376 | return NULL; |
| 5377 | } |
| 5378 | PyList_SetItem(l, i, x); |
| 5379 | } |
| 5380 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5381 | #endif |
| 5382 | } |
| 5383 | |
| 5384 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5385 | |
| 5386 | Py_ssize_t |
| 5387 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5388 | { |
| 5389 | PyThreadState *tstate = PyThreadState_Get(); |
| 5390 | Py_ssize_t new_index; |
| 5391 | |
| 5392 | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
| 5393 | return -1; |
| 5394 | } |
| 5395 | new_index = tstate->co_extra_user_count++; |
| 5396 | tstate->co_extra_freefuncs[new_index] = free; |
| 5397 | return new_index; |
| 5398 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5399 | |
| 5400 | static void |
| 5401 | dtrace_function_entry(PyFrameObject *f) |
| 5402 | { |
| 5403 | char* filename; |
| 5404 | char* funcname; |
| 5405 | int lineno; |
| 5406 | |
| 5407 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5408 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5409 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5410 | |
| 5411 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5412 | } |
| 5413 | |
| 5414 | static void |
| 5415 | dtrace_function_return(PyFrameObject *f) |
| 5416 | { |
| 5417 | char* filename; |
| 5418 | char* funcname; |
| 5419 | int lineno; |
| 5420 | |
| 5421 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5422 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5423 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5424 | |
| 5425 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5426 | } |
| 5427 | |
| 5428 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5429 | static void |
| 5430 | maybe_dtrace_line(PyFrameObject *frame, |
| 5431 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5432 | { |
| 5433 | int line = frame->f_lineno; |
| 5434 | char *co_filename, *co_name; |
| 5435 | |
| 5436 | /* If the last instruction executed isn't in the current |
| 5437 | instruction window, reset the window. |
| 5438 | */ |
| 5439 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5440 | PyAddrPair bounds; |
| 5441 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5442 | &bounds); |
| 5443 | *instr_lb = bounds.ap_lower; |
| 5444 | *instr_ub = bounds.ap_upper; |
| 5445 | } |
| 5446 | /* If the last instruction falls at the start of a line or if |
| 5447 | it represents a jump backwards, update the frame's line |
| 5448 | number and call the trace function. */ |
| 5449 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5450 | frame->f_lineno = line; |
| 5451 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5452 | if (!co_filename) |
| 5453 | co_filename = "?"; |
| 5454 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5455 | if (!co_name) |
| 5456 | co_name = "?"; |
| 5457 | PyDTrace_LINE(co_filename, co_name, line); |
| 5458 | } |
| 5459 | *instr_prev = frame->f_lasti; |
| 5460 | } |