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" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 18 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 19 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 23 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 24 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 26 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 27 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | #define LLTRACE 1 /* Low-level trace feature */ |
| 29 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 32 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 34 | /* Forward declarations */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 35 | static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 36 | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 37 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 39 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 40 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 41 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 42 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 43 | static int call_trace(Py_tracefunc, PyObject *, |
| 44 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 46 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 47 | PyThreadState *, PyFrameObject *, |
| 48 | int, PyObject *); |
| 49 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 50 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 51 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 52 | PyThreadState *, PyFrameObject *, int *, int *, int *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 54 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 55 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 56 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 57 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 58 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 59 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 60 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 61 | PyFrameObject *, const unsigned short *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 62 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 63 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 64 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 66 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 68 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | "free variable '%.200s' referenced before assignment" \ |
| 70 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 72 | /* Dynamic execution profile */ |
| 73 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 74 | #ifdef DXPAIRS |
| 75 | static long dxpairs[257][256]; |
| 76 | #define dxp dxpairs[256] |
| 77 | #else |
| 78 | static long dxp[256]; |
| 79 | #endif |
| 80 | #endif |
| 81 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 82 | /* Function call profile */ |
| 83 | #ifdef CALL_PROFILE |
| 84 | #define PCALL_NUM 11 |
| 85 | static int pcall[PCALL_NUM]; |
| 86 | |
| 87 | #define PCALL_ALL 0 |
| 88 | #define PCALL_FUNCTION 1 |
| 89 | #define PCALL_FAST_FUNCTION 2 |
| 90 | #define PCALL_FASTER_FUNCTION 3 |
| 91 | #define PCALL_METHOD 4 |
| 92 | #define PCALL_BOUND_METHOD 5 |
| 93 | #define PCALL_CFUNCTION 6 |
| 94 | #define PCALL_TYPE 7 |
| 95 | #define PCALL_GENERATOR 8 |
| 96 | #define PCALL_OTHER 9 |
| 97 | #define PCALL_POP 10 |
| 98 | |
| 99 | /* Notes about the statistics |
| 100 | |
| 101 | PCALL_FAST stats |
| 102 | |
| 103 | FAST_FUNCTION means no argument tuple needs to be created. |
| 104 | FASTER_FUNCTION means that the fast-path frame setup code is used. |
| 105 | |
| 106 | If there is a method call where the call can be optimized by changing |
| 107 | the argument tuple and calling the function directly, it gets recorded |
| 108 | twice. |
| 109 | |
| 110 | As a result, the relationship among the statistics appears to be |
| 111 | PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + |
| 112 | PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER |
| 113 | PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION |
| 114 | PCALL_METHOD > PCALL_BOUND_METHOD |
| 115 | */ |
| 116 | |
| 117 | #define PCALL(POS) pcall[POS]++ |
| 118 | |
| 119 | PyObject * |
| 120 | PyEval_GetCallStats(PyObject *self) |
| 121 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | return Py_BuildValue("iiiiiiiiiii", |
| 123 | pcall[0], pcall[1], pcall[2], pcall[3], |
| 124 | pcall[4], pcall[5], pcall[6], pcall[7], |
| 125 | pcall[8], pcall[9], pcall[10]); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 126 | } |
| 127 | #else |
| 128 | #define PCALL(O) |
| 129 | |
| 130 | PyObject * |
| 131 | PyEval_GetCallStats(PyObject *self) |
| 132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | Py_INCREF(Py_None); |
| 134 | return Py_None; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 138 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 139 | #ifdef WITH_THREAD |
| 140 | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
| 141 | #else |
| 142 | #define GIL_REQUEST 0 |
| 143 | #endif |
| 144 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 145 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 146 | 1. We believe this is all right because the eval loop will release |
| 147 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 148 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | _Py_atomic_store_relaxed( \ |
| 150 | &eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 151 | GIL_REQUEST | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
| 153 | pending_async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 154 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 155 | #ifdef WITH_THREAD |
| 156 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 157 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | do { \ |
| 159 | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
| 160 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 161 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 162 | |
| 163 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | do { \ |
| 165 | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
| 166 | COMPUTE_EVAL_BREAKER(); \ |
| 167 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 171 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 172 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | do { \ |
| 174 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
| 175 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 176 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 177 | |
| 178 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | do { \ |
| 180 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
| 181 | COMPUTE_EVAL_BREAKER(); \ |
| 182 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 183 | |
| 184 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | do { \ |
| 186 | pending_async_exc = 1; \ |
| 187 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 188 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 189 | |
| 190 | #define UNSIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 192 | |
| 193 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 194 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 195 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 196 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 197 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 198 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 199 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 200 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 201 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 202 | static long main_thread = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 203 | /* This single variable consolidates all requests to break out of the fast path |
| 204 | in the eval loop. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 205 | static _Py_atomic_int eval_breaker = {0}; |
| 206 | /* Request for dropping the GIL */ |
| 207 | static _Py_atomic_int gil_drop_request = {0}; |
| 208 | /* Request for running pending calls. */ |
| 209 | static _Py_atomic_int pendingcalls_to_do = {0}; |
| 210 | /* Request for looking at the `async_exc` field of the current thread state. |
| 211 | Guarded by the GIL. */ |
| 212 | static int pending_async_exc = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 213 | |
| 214 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 215 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 216 | int |
| 217 | PyEval_ThreadsInitialized(void) |
| 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 222 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 223 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | if (gil_created()) |
| 226 | return; |
| 227 | create_gil(); |
| 228 | take_gil(PyThreadState_GET()); |
| 229 | main_thread = PyThread_get_thread_ident(); |
| 230 | if (!pending_lock) |
| 231 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 232 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 234 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 235 | _PyEval_FiniThreads(void) |
| 236 | { |
| 237 | if (!gil_created()) |
| 238 | return; |
| 239 | destroy_gil(); |
| 240 | assert(!gil_created()); |
| 241 | } |
| 242 | |
| 243 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 244 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | PyThreadState *tstate = PyThreadState_GET(); |
| 247 | if (tstate == NULL) |
| 248 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 249 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 253 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 254 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | /* This function must succeed when the current thread state is NULL. |
| 256 | We therefore avoid PyThreadState_GET() which dumps a fatal error |
| 257 | in debug mode. |
| 258 | */ |
| 259 | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
| 260 | &_PyThreadState_Current)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 264 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | if (tstate == NULL) |
| 267 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 268 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 269 | assert(gil_created()); |
| 270 | take_gil(tstate); |
| 271 | if (PyThreadState_Swap(tstate) != NULL) |
| 272 | Py_FatalError( |
| 273 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 277 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 278 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | if (tstate == NULL) |
| 280 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 281 | if (PyThreadState_Swap(NULL) != tstate) |
| 282 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 283 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 284 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 286 | /* This function is called from PyOS_AfterFork to destroy all threads which are |
| 287 | * not running in the child process, and clear internal locks which might be |
| 288 | * held by those threads. (This could also be done using pthread_atfork |
| 289 | * mechanism, at least for the pthreads implementation.) */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 290 | |
| 291 | void |
| 292 | PyEval_ReInitThreads(void) |
| 293 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 294 | _Py_IDENTIFIER(_after_fork); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | PyObject *threading, *result; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 296 | PyThreadState *current_tstate = PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | if (!gil_created()) |
| 299 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | recreate_gil(); |
| 301 | pending_lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 302 | take_gil(current_tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | /* Update the threading module with the new state. |
| 306 | */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 307 | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | "threading"); |
| 309 | if (threading == NULL) { |
| 310 | /* threading not imported */ |
| 311 | PyErr_Clear(); |
| 312 | return; |
| 313 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 314 | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | if (result == NULL) |
| 316 | PyErr_WriteUnraisable(threading); |
| 317 | else |
| 318 | Py_DECREF(result); |
| 319 | Py_DECREF(threading); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 320 | |
| 321 | /* Destroy all threads except the current one */ |
| 322 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 323 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 324 | |
| 325 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 326 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 327 | static int pending_async_exc = 0; |
| 328 | #endif /* WITH_THREAD */ |
| 329 | |
| 330 | /* This function is used to signal that async exceptions are waiting to be |
| 331 | raised, therefore it is also useful in non-threaded builds. */ |
| 332 | |
| 333 | void |
| 334 | _PyEval_SignalAsyncExc(void) |
| 335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 337 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 339 | /* Functions save_thread and restore_thread are always defined so |
| 340 | dynamically loaded modules needn't be compiled separately for use |
| 341 | with and without threads: */ |
| 342 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 343 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 344 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 345 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 347 | if (tstate == NULL) |
| 348 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 349 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | if (gil_created()) |
| 351 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 352 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 357 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | if (tstate == NULL) |
| 360 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 361 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (gil_created()) { |
| 363 | int err = errno; |
| 364 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 365 | /* _Py_Finalizing is protected by the GIL */ |
| 366 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 367 | drop_gil(tstate); |
| 368 | PyThread_exit_thread(); |
| 369 | assert(0); /* unreachable */ |
| 370 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | errno = err; |
| 372 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 373 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 378 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 379 | signal handlers or Mac I/O completion routines) can schedule calls |
| 380 | to a function to be called synchronously. |
| 381 | The synchronous function is called with one void* argument. |
| 382 | It should return 0 for success or -1 for failure -- failure should |
| 383 | be accompanied by an exception. |
| 384 | |
| 385 | If registry succeeds, the registry function returns 0; if it fails |
| 386 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 387 | an exception condition). |
| 388 | |
| 389 | Note that because registry may occur from within signal handlers, |
| 390 | or other asynchronous events, calling malloc() is unsafe! |
| 391 | |
| 392 | #ifdef WITH_THREAD |
| 393 | Any thread can schedule pending calls, but only the main thread |
| 394 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 395 | There is no facility to schedule calls to a particular thread, but |
| 396 | that should be easy to change, should that ever be required. In |
| 397 | that case, the static variables here should go into the python |
| 398 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 399 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 400 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 401 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 402 | #ifdef WITH_THREAD |
| 403 | |
| 404 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 405 | scheduling to be made from any thread, and even from an executing |
| 406 | callback. |
| 407 | */ |
| 408 | |
| 409 | #define NPENDINGCALLS 32 |
| 410 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | int (*func)(void *); |
| 412 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 413 | } pendingcalls[NPENDINGCALLS]; |
| 414 | static int pendingfirst = 0; |
| 415 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 416 | |
| 417 | int |
| 418 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | int i, j, result=0; |
| 421 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | /* try a few times for the lock. Since this mechanism is used |
| 424 | * for signal handling (on the main thread), there is a (slim) |
| 425 | * chance that a signal is delivered on the same thread while we |
| 426 | * hold the lock during the Py_MakePendingCalls() function. |
| 427 | * This avoids a deadlock in that case. |
| 428 | * Note that signals can be delivered on any thread. In particular, |
| 429 | * on Windows, a SIGINT is delivered on a system-created worker |
| 430 | * thread. |
| 431 | * We also check for lock being NULL, in the unlikely case that |
| 432 | * this function is called before any bytecode evaluation takes place. |
| 433 | */ |
| 434 | if (lock != NULL) { |
| 435 | for (i = 0; i<100; i++) { |
| 436 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 437 | break; |
| 438 | } |
| 439 | if (i == 100) |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | i = pendinglast; |
| 444 | j = (i + 1) % NPENDINGCALLS; |
| 445 | if (j == pendingfirst) { |
| 446 | result = -1; /* Queue full */ |
| 447 | } else { |
| 448 | pendingcalls[i].func = func; |
| 449 | pendingcalls[i].arg = arg; |
| 450 | pendinglast = j; |
| 451 | } |
| 452 | /* signal main loop */ |
| 453 | SIGNAL_PENDING_CALLS(); |
| 454 | if (lock != NULL) |
| 455 | PyThread_release_lock(lock); |
| 456 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | int |
| 460 | Py_MakePendingCalls(void) |
| 461 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 462 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | int i; |
| 464 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | if (!pending_lock) { |
| 467 | /* initial allocation of the lock */ |
| 468 | pending_lock = PyThread_allocate_lock(); |
| 469 | if (pending_lock == NULL) |
| 470 | return -1; |
| 471 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | /* only service pending calls on main thread */ |
| 474 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 475 | return 0; |
| 476 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 477 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 479 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | /* perform a bounded number of calls, in case of recursion */ |
| 481 | for (i=0; i<NPENDINGCALLS; i++) { |
| 482 | int j; |
| 483 | int (*func)(void *); |
| 484 | void *arg = NULL; |
| 485 | |
| 486 | /* pop one item off the queue while holding the lock */ |
| 487 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 488 | j = pendingfirst; |
| 489 | if (j == pendinglast) { |
| 490 | func = NULL; /* Queue empty */ |
| 491 | } else { |
| 492 | func = pendingcalls[j].func; |
| 493 | arg = pendingcalls[j].arg; |
| 494 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 495 | } |
| 496 | if (pendingfirst != pendinglast) |
| 497 | SIGNAL_PENDING_CALLS(); |
| 498 | else |
| 499 | UNSIGNAL_PENDING_CALLS(); |
| 500 | PyThread_release_lock(pending_lock); |
| 501 | /* having released the lock, perform the callback */ |
| 502 | if (func == NULL) |
| 503 | break; |
| 504 | r = func(arg); |
| 505 | if (r) |
| 506 | break; |
| 507 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 508 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | #else /* if ! defined WITH_THREAD */ |
| 513 | |
| 514 | /* |
| 515 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 516 | This code is used for signal handling in python that isn't built |
| 517 | with WITH_THREAD. |
| 518 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 519 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 521 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 522 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 523 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 525 | (1) is very unlikely because typically signal delivery |
| 526 | is blocked during signal handling. So it should be impossible. |
| 527 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 528 | The current code is safe against (2), but not against (1). |
| 529 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 530 | thread is present, interrupted by signals, and that the critical |
| 531 | section is protected with the "busy" variable. On Windows, which |
| 532 | delivers SIGINT on a system thread, this does not hold and therefore |
| 533 | Windows really shouldn't use this version. |
| 534 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 535 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 536 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 537 | #define NPENDINGCALLS 32 |
| 538 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | int (*func)(void *); |
| 540 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 541 | } pendingcalls[NPENDINGCALLS]; |
| 542 | static volatile int pendingfirst = 0; |
| 543 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 544 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 545 | |
| 546 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 547 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | static volatile int busy = 0; |
| 550 | int i, j; |
| 551 | /* XXX Begin critical section */ |
| 552 | if (busy) |
| 553 | return -1; |
| 554 | busy = 1; |
| 555 | i = pendinglast; |
| 556 | j = (i + 1) % NPENDINGCALLS; |
| 557 | if (j == pendingfirst) { |
| 558 | busy = 0; |
| 559 | return -1; /* Queue full */ |
| 560 | } |
| 561 | pendingcalls[i].func = func; |
| 562 | pendingcalls[i].arg = arg; |
| 563 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | SIGNAL_PENDING_CALLS(); |
| 566 | busy = 0; |
| 567 | /* XXX End critical section */ |
| 568 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 571 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 572 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | static int busy = 0; |
| 575 | if (busy) |
| 576 | return 0; |
| 577 | busy = 1; |
| 578 | UNSIGNAL_PENDING_CALLS(); |
| 579 | for (;;) { |
| 580 | int i; |
| 581 | int (*func)(void *); |
| 582 | void *arg; |
| 583 | i = pendingfirst; |
| 584 | if (i == pendinglast) |
| 585 | break; /* Queue empty */ |
| 586 | func = pendingcalls[i].func; |
| 587 | arg = pendingcalls[i].arg; |
| 588 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 589 | if (func(arg) < 0) { |
| 590 | busy = 0; |
| 591 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 592 | return -1; |
| 593 | } |
| 594 | } |
| 595 | busy = 0; |
| 596 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 599 | #endif /* WITH_THREAD */ |
| 600 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 601 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 602 | /* The interpreter's recursion limit */ |
| 603 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 604 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 605 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 606 | #endif |
| 607 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 608 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 609 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 610 | int |
| 611 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 616 | void |
| 617 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | recursion_limit = new_limit; |
| 620 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 623 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 624 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 625 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 626 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 627 | Without USE_STACKCHECK, there is no need for this. */ |
| 628 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 629 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 632 | |
| 633 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | if (PyOS_CheckStack()) { |
| 635 | --tstate->recursion_depth; |
| 636 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 637 | return -1; |
| 638 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 639 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | _Py_CheckRecursionLimit = recursion_limit; |
| 641 | if (tstate->recursion_critical) |
| 642 | /* Somebody asked that we don't check for recursion. */ |
| 643 | return 0; |
| 644 | if (tstate->overflowed) { |
| 645 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 646 | /* Overflowing while handling an overflow. Give up. */ |
| 647 | Py_FatalError("Cannot recover from stack overflow."); |
| 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | if (tstate->recursion_depth > recursion_limit) { |
| 652 | --tstate->recursion_depth; |
| 653 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 654 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | "maximum recursion depth exceeded%s", |
| 656 | where); |
| 657 | return -1; |
| 658 | } |
| 659 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 662 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 663 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 664 | WHY_NOT = 0x0001, /* No error */ |
| 665 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 666 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 667 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 668 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 669 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 670 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 671 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 672 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 673 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 674 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 675 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 676 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 677 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 678 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 679 | /* Records whether tracing is on for any thread. Counts the number of |
| 680 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 681 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 682 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 683 | fast_next_opcode*/ |
| 684 | static int _Py_TracingPossible = 0; |
| 685 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 686 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 687 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 688 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 689 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | return PyEval_EvalCodeEx(co, |
| 692 | globals, locals, |
| 693 | (PyObject **)NULL, 0, |
| 694 | (PyObject **)NULL, 0, |
| 695 | (PyObject **)NULL, 0, |
| 696 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | |
| 700 | /* Interpreter main loop */ |
| 701 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 702 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 703 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | /* This is for backward compatibility with extension modules that |
| 705 | used this API; core interpreter code should call |
| 706 | PyEval_EvalFrameEx() */ |
| 707 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 711 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 712 | { |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 713 | PyThreadState *tstate = PyThreadState_GET(); |
| 714 | return tstate->interp->eval_frame(f, throwflag); |
| 715 | } |
| 716 | |
| 717 | PyObject * |
| 718 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 719 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 720 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 722 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 723 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 724 | const unsigned short *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 725 | int opcode; /* Current opcode */ |
| 726 | int oparg; /* Current opcode argument, if any */ |
| 727 | enum why_code why; /* Reason for block stack unwind */ |
| 728 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | PyObject *retval = NULL; /* Return value */ |
| 730 | PyThreadState *tstate = PyThreadState_GET(); |
| 731 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | not (instr_lb <= current_bytecode_offset < instr_ub) |
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 | is true when the line being executed has changed. The |
| 738 | initial values are such as to make this false the first |
| 739 | time it is tested. */ |
| 740 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 741 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 742 | const unsigned short *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | PyObject *names; |
| 744 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 745 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 746 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 747 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 748 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 749 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 750 | /* Computed GOTOs, or |
| 751 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 752 | using gcc's labels-as-values extension |
| 753 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 754 | |
| 755 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 757 | combined with a lookup table of jump addresses. However, since the |
| 758 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 759 | hard time making the right prediction for where to jump next (actually, |
| 760 | it will be always wrong except in the uncommon case of a sequence of |
| 761 | several identical opcodes). |
| 762 | |
| 763 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 764 | indirect jump instruction at the end of each opcode. Since the jump |
| 765 | instruction is at a different address for each opcode, the CPU will make a |
| 766 | separate prediction for each of these instructions, which is equivalent to |
| 767 | predicting the second opcode of each opcode pair. These predictions have |
| 768 | a much better chance to turn out valid, especially in small bytecode loops. |
| 769 | |
| 770 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 772 | and potentially many more instructions (depending on the pipeline width). |
| 773 | A correctly predicted branch, however, is nearly free. |
| 774 | |
| 775 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 776 | faster than the normal "switch" version, depending on the compiler and the |
| 777 | CPU architecture. |
| 778 | |
| 779 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 780 | because it would render the measurements invalid. |
| 781 | |
| 782 | |
| 783 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 784 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 785 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 786 | -fno-crossjumping). |
| 787 | */ |
| 788 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 789 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 790 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 791 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 792 | #endif |
| 793 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 794 | #ifdef HAVE_COMPUTED_GOTOS |
| 795 | #ifndef USE_COMPUTED_GOTOS |
| 796 | #define USE_COMPUTED_GOTOS 1 |
| 797 | #endif |
| 798 | #else |
| 799 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 800 | #error "Computed gotos are not supported on this compiler." |
| 801 | #endif |
| 802 | #undef USE_COMPUTED_GOTOS |
| 803 | #define USE_COMPUTED_GOTOS 0 |
| 804 | #endif |
| 805 | |
| 806 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 807 | /* Import the static jump table */ |
| 808 | #include "opcode_targets.h" |
| 809 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 810 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | TARGET_##op: \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | case op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 813 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 814 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | { \ |
| 816 | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
| 817 | FAST_DISPATCH(); \ |
| 818 | } \ |
| 819 | continue; \ |
| 820 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 821 | |
| 822 | #ifdef LLTRACE |
| 823 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | { \ |
| 825 | if (!lltrace && !_Py_TracingPossible) { \ |
| 826 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 827 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 828 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | } \ |
| 830 | goto fast_next_opcode; \ |
| 831 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 832 | #else |
| 833 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | { \ |
| 835 | if (!_Py_TracingPossible) { \ |
| 836 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 837 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 838 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | } \ |
| 840 | goto fast_next_opcode; \ |
| 841 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 842 | #endif |
| 843 | |
| 844 | #else |
| 845 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | case op: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 847 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 848 | #define DISPATCH() continue |
| 849 | #define FAST_DISPATCH() goto fast_next_opcode |
| 850 | #endif |
| 851 | |
| 852 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 853 | /* Tuple access macros */ |
| 854 | |
| 855 | #ifndef Py_DEBUG |
| 856 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 857 | #else |
| 858 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 859 | #endif |
| 860 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 861 | /* Code access macros */ |
| 862 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 863 | #ifdef WORDS_BIGENDIAN |
| 864 | #define OPCODE(word) ((word) >> 8) |
| 865 | #define OPARG(word) ((word) & 255) |
| 866 | #else |
| 867 | #define OPCODE(word) ((word) & 255) |
| 868 | #define OPARG(word) ((word) >> 8) |
| 869 | #endif |
| 870 | /* The integer overflow is checked by an assertion below. */ |
| 871 | #define INSTR_OFFSET() (2*(int)(next_instr - first_instr)) |
| 872 | #define NEXTOPARG() do { \ |
| 873 | unsigned short word = *next_instr; \ |
| 874 | opcode = OPCODE(word); \ |
| 875 | oparg = OPARG(word); \ |
| 876 | next_instr++; \ |
| 877 | } while (0) |
| 878 | #define JUMPTO(x) (next_instr = first_instr + (x)/2) |
| 879 | #define JUMPBY(x) (next_instr += (x)/2) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 880 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 881 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | Some opcodes tend to come in pairs thus making it possible to |
| 883 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 884 | 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] | 885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | Verifying the prediction costs a single high-speed test of a register |
| 887 | variable against a constant. If the pairing was good, then the |
| 888 | processor's own internal branch predication has a high likelihood of |
| 889 | success, resulting in a nearly zero-overhead transition to the |
| 890 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 891 | including its unpredictable switch-case branch. Combined with the |
| 892 | processor's internal branch prediction, a successful PREDICT has the |
| 893 | effect of making the two opcodes run as if they were a single new opcode |
| 894 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 895 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 896 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | predictions turned-on and interpret the results as if some opcodes |
| 898 | had been combined or turn-off predictions so that the opcode frequency |
| 899 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 900 | |
| 901 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | the CPU to record separate branch prediction information for each |
| 903 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 904 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 905 | */ |
| 906 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 907 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 909 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 910 | #define PREDICT(op) \ |
| 911 | do{ \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 912 | unsigned short word = *next_instr; \ |
| 913 | opcode = OPCODE(word); \ |
| 914 | if (opcode == op){ \ |
| 915 | oparg = OPARG(word); \ |
| 916 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 917 | goto PRED_##op; \ |
| 918 | } \ |
| 919 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 920 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 921 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 922 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 923 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 924 | /* Stack manipulation macros */ |
| 925 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 926 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 927 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 928 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 929 | #define EMPTY() (STACK_LEVEL() == 0) |
| 930 | #define TOP() (stack_pointer[-1]) |
| 931 | #define SECOND() (stack_pointer[-2]) |
| 932 | #define THIRD() (stack_pointer[-3]) |
| 933 | #define FOURTH() (stack_pointer[-4]) |
| 934 | #define PEEK(n) (stack_pointer[-(n)]) |
| 935 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 936 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 937 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 938 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 939 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 940 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 941 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 942 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 944 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 946 | lltrace && prtrace(TOP(), "push")); \ |
| 947 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 949 | BASIC_POP()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 951 | lltrace && prtrace(TOP(), "stackadj")); \ |
| 952 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 953 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 954 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 955 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 956 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 957 | #define PUSH(v) BASIC_PUSH(v) |
| 958 | #define POP() BASIC_POP() |
| 959 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 960 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 961 | #endif |
| 962 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 963 | /* Local variable macros */ |
| 964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 966 | |
| 967 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 968 | then store the new value; it must copy the old value to a temporary |
| 969 | value, then store the new value, and then DECREF the temporary value. |
| 970 | This is because it is possible that during the DECREF the frame is |
| 971 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 972 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 974 | GETLOCAL(i) = value; \ |
| 975 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 976 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 977 | |
| 978 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 980 | PyObject *v = POP(); \ |
| 981 | Py_XDECREF(v); \ |
| 982 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 983 | |
| 984 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 985 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | PyObject *type, *value, *traceback; \ |
| 987 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 988 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 989 | value = POP(); \ |
| 990 | Py_XDECREF(value); \ |
| 991 | } \ |
| 992 | type = tstate->exc_type; \ |
| 993 | value = tstate->exc_value; \ |
| 994 | traceback = tstate->exc_traceback; \ |
| 995 | tstate->exc_type = POP(); \ |
| 996 | tstate->exc_value = POP(); \ |
| 997 | tstate->exc_traceback = POP(); \ |
| 998 | Py_XDECREF(type); \ |
| 999 | Py_XDECREF(value); \ |
| 1000 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1001 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1002 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1003 | /* Start of code */ |
| 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | /* push frame */ |
| 1006 | if (Py_EnterRecursiveCall("")) |
| 1007 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | if (tstate->use_tracing) { |
| 1012 | if (tstate->c_tracefunc != NULL) { |
| 1013 | /* tstate->c_tracefunc, if defined, is a |
| 1014 | function that will be called on *every* entry |
| 1015 | to a code block. Its return value, if not |
| 1016 | None, is a function that will be called at |
| 1017 | the start of each executed line of code. |
| 1018 | (Actually, the function must return itself |
| 1019 | in order to continue tracing.) The trace |
| 1020 | functions are called with three arguments: |
| 1021 | a pointer to the current frame, a string |
| 1022 | indicating why the function is called, and |
| 1023 | an argument which depends on the situation. |
| 1024 | The global trace function is also called |
| 1025 | whenever an exception is detected. */ |
| 1026 | if (call_trace_protected(tstate->c_tracefunc, |
| 1027 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1028 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | /* Trace function raised an error */ |
| 1030 | goto exit_eval_frame; |
| 1031 | } |
| 1032 | } |
| 1033 | if (tstate->c_profilefunc != NULL) { |
| 1034 | /* Similar for c_profilefunc, except it needn't |
| 1035 | return itself and isn't called for "line" events */ |
| 1036 | if (call_trace_protected(tstate->c_profilefunc, |
| 1037 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1038 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | /* Profile function raised an error */ |
| 1040 | goto exit_eval_frame; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 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); |
| 1052 | assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0); |
Serhiy Storchaka | 74f2fe6 | 2016-05-25 20:35:44 +0300 | [diff] [blame] | 1053 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2)); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1054 | first_instr = (unsigned short*) 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 | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1070 | next_instr = first_instr; |
| 1071 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1072 | assert(f->f_lasti % 2 == 0); |
| 1073 | next_instr += f->f_lasti/2 + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1074 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | stack_pointer = f->f_stacktop; |
| 1076 | assert(stack_pointer != NULL); |
| 1077 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1078 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1079 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1080 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 1081 | 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] | 1082 | /* We were in an except handler when we left, |
| 1083 | restore the exception state which was put aside |
| 1084 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1085 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1087 | else |
| 1088 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1090 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1091 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1092 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1093 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | why = WHY_NOT; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1096 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1097 | if (throwflag) /* support for generator.throw() */ |
| 1098 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1099 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1100 | #ifdef Py_DEBUG |
| 1101 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
| 1102 | because it may clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1103 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1104 | assert(!PyErr_Occurred()); |
| 1105 | #endif |
| 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1109 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1110 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | /* Do periodic things. Doing this every time through |
| 1113 | the loop would add too much overhead, so we do it |
| 1114 | only every Nth instruction. We also do it if |
| 1115 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1116 | event needs attention (e.g. a signal handler or |
| 1117 | async I/O handler); see Py_AddPendingCall() and |
| 1118 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1121 | if (OPCODE(*next_instr) == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1123 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | goto fast_next_opcode; |
| 1125 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1127 | if (Py_MakePendingCalls() < 0) |
| 1128 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1130 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1131 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | /* Give another thread a chance */ |
| 1133 | if (PyThreadState_Swap(NULL) != tstate) |
| 1134 | Py_FatalError("ceval: tstate mix-up"); |
| 1135 | drop_gil(tstate); |
| 1136 | |
| 1137 | /* Other threads may run now */ |
| 1138 | |
| 1139 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1140 | |
| 1141 | /* Check if we should make a quick exit. */ |
| 1142 | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
| 1143 | drop_gil(tstate); |
| 1144 | PyThread_exit_thread(); |
| 1145 | } |
| 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (PyThreadState_Swap(tstate) != NULL) |
| 1148 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1150 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | /* Check for asynchronous exceptions. */ |
| 1152 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1153 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | tstate->async_exc = NULL; |
| 1155 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1156 | PyErr_SetNone(exc); |
| 1157 | Py_DECREF(exc); |
| 1158 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | fast_next_opcode: |
| 1163 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1168 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1169 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | /* see maybe_call_line_trace |
| 1171 | for expository comments */ |
| 1172 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1175 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1176 | tstate, f, |
| 1177 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | /* Reload possibly changed frame fields */ |
| 1179 | JUMPTO(f->f_lasti); |
| 1180 | if (f->f_stacktop != NULL) { |
| 1181 | stack_pointer = f->f_stacktop; |
| 1182 | f->f_stacktop = NULL; |
| 1183 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1184 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1186 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1190 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1191 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1192 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1193 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1194 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | dxpairs[lastopcode][opcode]++; |
| 1196 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1197 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1199 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1200 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1201 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | if (lltrace) { |
| 1205 | if (HAS_ARG(opcode)) { |
| 1206 | printf("%d: %d, %d\n", |
| 1207 | f->f_lasti, opcode, oparg); |
| 1208 | } |
| 1209 | else { |
| 1210 | printf("%d: %d\n", |
| 1211 | f->f_lasti, opcode); |
| 1212 | } |
| 1213 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1214 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | /* BEWARE! |
| 1219 | It is essential that any operation that fails sets either |
| 1220 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1221 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | TARGET(NOP) |
| 1224 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1225 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1226 | TARGET(LOAD_FAST) { |
| 1227 | PyObject *value = GETLOCAL(oparg); |
| 1228 | if (value == NULL) { |
| 1229 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1230 | UNBOUNDLOCAL_ERROR_MSG, |
| 1231 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1232 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1234 | Py_INCREF(value); |
| 1235 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1237 | } |
| 1238 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1239 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1240 | TARGET(LOAD_CONST) { |
| 1241 | PyObject *value = GETITEM(consts, oparg); |
| 1242 | Py_INCREF(value); |
| 1243 | PUSH(value); |
| 1244 | FAST_DISPATCH(); |
| 1245 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1246 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1247 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1248 | TARGET(STORE_FAST) { |
| 1249 | PyObject *value = POP(); |
| 1250 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1252 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1253 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1254 | TARGET(POP_TOP) { |
| 1255 | PyObject *value = POP(); |
| 1256 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1258 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1259 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1260 | TARGET(ROT_TWO) { |
| 1261 | PyObject *top = TOP(); |
| 1262 | PyObject *second = SECOND(); |
| 1263 | SET_TOP(second); |
| 1264 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1266 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1267 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1268 | TARGET(ROT_THREE) { |
| 1269 | PyObject *top = TOP(); |
| 1270 | PyObject *second = SECOND(); |
| 1271 | PyObject *third = THIRD(); |
| 1272 | SET_TOP(second); |
| 1273 | SET_SECOND(third); |
| 1274 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1276 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1277 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1278 | TARGET(DUP_TOP) { |
| 1279 | PyObject *top = TOP(); |
| 1280 | Py_INCREF(top); |
| 1281 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1283 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1284 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1285 | TARGET(DUP_TOP_TWO) { |
| 1286 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1287 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1288 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1289 | Py_INCREF(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1290 | STACKADJ(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1291 | SET_TOP(top); |
| 1292 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1293 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1294 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1295 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1296 | TARGET(UNARY_POSITIVE) { |
| 1297 | PyObject *value = TOP(); |
| 1298 | PyObject *res = PyNumber_Positive(value); |
| 1299 | Py_DECREF(value); |
| 1300 | SET_TOP(res); |
| 1301 | if (res == NULL) |
| 1302 | goto error; |
| 1303 | DISPATCH(); |
| 1304 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1305 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1306 | TARGET(UNARY_NEGATIVE) { |
| 1307 | PyObject *value = TOP(); |
| 1308 | PyObject *res = PyNumber_Negative(value); |
| 1309 | Py_DECREF(value); |
| 1310 | SET_TOP(res); |
| 1311 | if (res == NULL) |
| 1312 | goto error; |
| 1313 | DISPATCH(); |
| 1314 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1315 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1316 | TARGET(UNARY_NOT) { |
| 1317 | PyObject *value = TOP(); |
| 1318 | int err = PyObject_IsTrue(value); |
| 1319 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | if (err == 0) { |
| 1321 | Py_INCREF(Py_True); |
| 1322 | SET_TOP(Py_True); |
| 1323 | DISPATCH(); |
| 1324 | } |
| 1325 | else if (err > 0) { |
| 1326 | Py_INCREF(Py_False); |
| 1327 | SET_TOP(Py_False); |
| 1328 | err = 0; |
| 1329 | DISPATCH(); |
| 1330 | } |
| 1331 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1332 | goto error; |
| 1333 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1334 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1335 | TARGET(UNARY_INVERT) { |
| 1336 | PyObject *value = TOP(); |
| 1337 | PyObject *res = PyNumber_Invert(value); |
| 1338 | Py_DECREF(value); |
| 1339 | SET_TOP(res); |
| 1340 | if (res == NULL) |
| 1341 | goto error; |
| 1342 | DISPATCH(); |
| 1343 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1344 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1345 | TARGET(BINARY_POWER) { |
| 1346 | PyObject *exp = POP(); |
| 1347 | PyObject *base = TOP(); |
| 1348 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1349 | Py_DECREF(base); |
| 1350 | Py_DECREF(exp); |
| 1351 | SET_TOP(res); |
| 1352 | if (res == NULL) |
| 1353 | goto error; |
| 1354 | DISPATCH(); |
| 1355 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1356 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1357 | TARGET(BINARY_MULTIPLY) { |
| 1358 | PyObject *right = POP(); |
| 1359 | PyObject *left = TOP(); |
| 1360 | PyObject *res = PyNumber_Multiply(left, right); |
| 1361 | Py_DECREF(left); |
| 1362 | Py_DECREF(right); |
| 1363 | SET_TOP(res); |
| 1364 | if (res == NULL) |
| 1365 | goto error; |
| 1366 | DISPATCH(); |
| 1367 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1368 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1369 | TARGET(BINARY_MATRIX_MULTIPLY) { |
| 1370 | PyObject *right = POP(); |
| 1371 | PyObject *left = TOP(); |
| 1372 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1373 | Py_DECREF(left); |
| 1374 | Py_DECREF(right); |
| 1375 | SET_TOP(res); |
| 1376 | if (res == NULL) |
| 1377 | goto error; |
| 1378 | DISPATCH(); |
| 1379 | } |
| 1380 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1381 | TARGET(BINARY_TRUE_DIVIDE) { |
| 1382 | PyObject *divisor = POP(); |
| 1383 | PyObject *dividend = TOP(); |
| 1384 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1385 | Py_DECREF(dividend); |
| 1386 | Py_DECREF(divisor); |
| 1387 | SET_TOP(quotient); |
| 1388 | if (quotient == NULL) |
| 1389 | goto error; |
| 1390 | DISPATCH(); |
| 1391 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1392 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1393 | TARGET(BINARY_FLOOR_DIVIDE) { |
| 1394 | PyObject *divisor = POP(); |
| 1395 | PyObject *dividend = TOP(); |
| 1396 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1397 | Py_DECREF(dividend); |
| 1398 | Py_DECREF(divisor); |
| 1399 | SET_TOP(quotient); |
| 1400 | if (quotient == NULL) |
| 1401 | goto error; |
| 1402 | DISPATCH(); |
| 1403 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1404 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1405 | TARGET(BINARY_MODULO) { |
| 1406 | PyObject *divisor = POP(); |
| 1407 | PyObject *dividend = TOP(); |
| 1408 | PyObject *res = PyUnicode_CheckExact(dividend) ? |
| 1409 | PyUnicode_Format(dividend, divisor) : |
| 1410 | PyNumber_Remainder(dividend, divisor); |
| 1411 | Py_DECREF(divisor); |
| 1412 | Py_DECREF(dividend); |
| 1413 | SET_TOP(res); |
| 1414 | if (res == NULL) |
| 1415 | goto error; |
| 1416 | DISPATCH(); |
| 1417 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1418 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1419 | TARGET(BINARY_ADD) { |
| 1420 | PyObject *right = POP(); |
| 1421 | PyObject *left = TOP(); |
| 1422 | PyObject *sum; |
| 1423 | if (PyUnicode_CheckExact(left) && |
| 1424 | PyUnicode_CheckExact(right)) { |
| 1425 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1426 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1427 | } |
| 1428 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1429 | sum = PyNumber_Add(left, right); |
| 1430 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1431 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1432 | Py_DECREF(right); |
| 1433 | SET_TOP(sum); |
| 1434 | if (sum == NULL) |
| 1435 | goto error; |
| 1436 | DISPATCH(); |
| 1437 | } |
| 1438 | |
| 1439 | TARGET(BINARY_SUBTRACT) { |
| 1440 | PyObject *right = POP(); |
| 1441 | PyObject *left = TOP(); |
| 1442 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1443 | Py_DECREF(right); |
| 1444 | Py_DECREF(left); |
| 1445 | SET_TOP(diff); |
| 1446 | if (diff == NULL) |
| 1447 | goto error; |
| 1448 | DISPATCH(); |
| 1449 | } |
| 1450 | |
| 1451 | TARGET(BINARY_SUBSCR) { |
| 1452 | PyObject *sub = POP(); |
| 1453 | PyObject *container = TOP(); |
| 1454 | PyObject *res = PyObject_GetItem(container, sub); |
| 1455 | Py_DECREF(container); |
| 1456 | Py_DECREF(sub); |
| 1457 | SET_TOP(res); |
| 1458 | if (res == NULL) |
| 1459 | goto error; |
| 1460 | DISPATCH(); |
| 1461 | } |
| 1462 | |
| 1463 | TARGET(BINARY_LSHIFT) { |
| 1464 | PyObject *right = POP(); |
| 1465 | PyObject *left = TOP(); |
| 1466 | PyObject *res = PyNumber_Lshift(left, right); |
| 1467 | Py_DECREF(left); |
| 1468 | Py_DECREF(right); |
| 1469 | SET_TOP(res); |
| 1470 | if (res == NULL) |
| 1471 | goto error; |
| 1472 | DISPATCH(); |
| 1473 | } |
| 1474 | |
| 1475 | TARGET(BINARY_RSHIFT) { |
| 1476 | PyObject *right = POP(); |
| 1477 | PyObject *left = TOP(); |
| 1478 | PyObject *res = PyNumber_Rshift(left, right); |
| 1479 | Py_DECREF(left); |
| 1480 | Py_DECREF(right); |
| 1481 | SET_TOP(res); |
| 1482 | if (res == NULL) |
| 1483 | goto error; |
| 1484 | DISPATCH(); |
| 1485 | } |
| 1486 | |
| 1487 | TARGET(BINARY_AND) { |
| 1488 | PyObject *right = POP(); |
| 1489 | PyObject *left = TOP(); |
| 1490 | PyObject *res = PyNumber_And(left, right); |
| 1491 | Py_DECREF(left); |
| 1492 | Py_DECREF(right); |
| 1493 | SET_TOP(res); |
| 1494 | if (res == NULL) |
| 1495 | goto error; |
| 1496 | DISPATCH(); |
| 1497 | } |
| 1498 | |
| 1499 | TARGET(BINARY_XOR) { |
| 1500 | PyObject *right = POP(); |
| 1501 | PyObject *left = TOP(); |
| 1502 | PyObject *res = PyNumber_Xor(left, right); |
| 1503 | Py_DECREF(left); |
| 1504 | Py_DECREF(right); |
| 1505 | SET_TOP(res); |
| 1506 | if (res == NULL) |
| 1507 | goto error; |
| 1508 | DISPATCH(); |
| 1509 | } |
| 1510 | |
| 1511 | TARGET(BINARY_OR) { |
| 1512 | PyObject *right = POP(); |
| 1513 | PyObject *left = TOP(); |
| 1514 | PyObject *res = PyNumber_Or(left, right); |
| 1515 | Py_DECREF(left); |
| 1516 | Py_DECREF(right); |
| 1517 | SET_TOP(res); |
| 1518 | if (res == NULL) |
| 1519 | goto error; |
| 1520 | DISPATCH(); |
| 1521 | } |
| 1522 | |
| 1523 | TARGET(LIST_APPEND) { |
| 1524 | PyObject *v = POP(); |
| 1525 | PyObject *list = PEEK(oparg); |
| 1526 | int err; |
| 1527 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1529 | if (err != 0) |
| 1530 | goto error; |
| 1531 | PREDICT(JUMP_ABSOLUTE); |
| 1532 | DISPATCH(); |
| 1533 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1534 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1535 | TARGET(SET_ADD) { |
| 1536 | PyObject *v = POP(); |
| 1537 | PyObject *set = stack_pointer[-oparg]; |
| 1538 | int err; |
| 1539 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1541 | if (err != 0) |
| 1542 | goto error; |
| 1543 | PREDICT(JUMP_ABSOLUTE); |
| 1544 | DISPATCH(); |
| 1545 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1546 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1547 | TARGET(INPLACE_POWER) { |
| 1548 | PyObject *exp = POP(); |
| 1549 | PyObject *base = TOP(); |
| 1550 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1551 | Py_DECREF(base); |
| 1552 | Py_DECREF(exp); |
| 1553 | SET_TOP(res); |
| 1554 | if (res == NULL) |
| 1555 | goto error; |
| 1556 | DISPATCH(); |
| 1557 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1558 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1559 | TARGET(INPLACE_MULTIPLY) { |
| 1560 | PyObject *right = POP(); |
| 1561 | PyObject *left = TOP(); |
| 1562 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1563 | Py_DECREF(left); |
| 1564 | Py_DECREF(right); |
| 1565 | SET_TOP(res); |
| 1566 | if (res == NULL) |
| 1567 | goto error; |
| 1568 | DISPATCH(); |
| 1569 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1570 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1571 | TARGET(INPLACE_MATRIX_MULTIPLY) { |
| 1572 | PyObject *right = POP(); |
| 1573 | PyObject *left = TOP(); |
| 1574 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1575 | Py_DECREF(left); |
| 1576 | Py_DECREF(right); |
| 1577 | SET_TOP(res); |
| 1578 | if (res == NULL) |
| 1579 | goto error; |
| 1580 | DISPATCH(); |
| 1581 | } |
| 1582 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1583 | TARGET(INPLACE_TRUE_DIVIDE) { |
| 1584 | PyObject *divisor = POP(); |
| 1585 | PyObject *dividend = TOP(); |
| 1586 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1587 | Py_DECREF(dividend); |
| 1588 | Py_DECREF(divisor); |
| 1589 | SET_TOP(quotient); |
| 1590 | if (quotient == NULL) |
| 1591 | goto error; |
| 1592 | DISPATCH(); |
| 1593 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1594 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1595 | TARGET(INPLACE_FLOOR_DIVIDE) { |
| 1596 | PyObject *divisor = POP(); |
| 1597 | PyObject *dividend = TOP(); |
| 1598 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1599 | Py_DECREF(dividend); |
| 1600 | Py_DECREF(divisor); |
| 1601 | SET_TOP(quotient); |
| 1602 | if (quotient == NULL) |
| 1603 | goto error; |
| 1604 | DISPATCH(); |
| 1605 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1606 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1607 | TARGET(INPLACE_MODULO) { |
| 1608 | PyObject *right = POP(); |
| 1609 | PyObject *left = TOP(); |
| 1610 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1611 | Py_DECREF(left); |
| 1612 | Py_DECREF(right); |
| 1613 | SET_TOP(mod); |
| 1614 | if (mod == NULL) |
| 1615 | goto error; |
| 1616 | DISPATCH(); |
| 1617 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1618 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1619 | TARGET(INPLACE_ADD) { |
| 1620 | PyObject *right = POP(); |
| 1621 | PyObject *left = TOP(); |
| 1622 | PyObject *sum; |
| 1623 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1624 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1625 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1626 | } |
| 1627 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1628 | sum = PyNumber_InPlaceAdd(left, right); |
| 1629 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1630 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1631 | Py_DECREF(right); |
| 1632 | SET_TOP(sum); |
| 1633 | if (sum == NULL) |
| 1634 | goto error; |
| 1635 | DISPATCH(); |
| 1636 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1637 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1638 | TARGET(INPLACE_SUBTRACT) { |
| 1639 | PyObject *right = POP(); |
| 1640 | PyObject *left = TOP(); |
| 1641 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1642 | Py_DECREF(left); |
| 1643 | Py_DECREF(right); |
| 1644 | SET_TOP(diff); |
| 1645 | if (diff == NULL) |
| 1646 | goto error; |
| 1647 | DISPATCH(); |
| 1648 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1649 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1650 | TARGET(INPLACE_LSHIFT) { |
| 1651 | PyObject *right = POP(); |
| 1652 | PyObject *left = TOP(); |
| 1653 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1654 | Py_DECREF(left); |
| 1655 | Py_DECREF(right); |
| 1656 | SET_TOP(res); |
| 1657 | if (res == NULL) |
| 1658 | goto error; |
| 1659 | DISPATCH(); |
| 1660 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1661 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1662 | TARGET(INPLACE_RSHIFT) { |
| 1663 | PyObject *right = POP(); |
| 1664 | PyObject *left = TOP(); |
| 1665 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1666 | Py_DECREF(left); |
| 1667 | Py_DECREF(right); |
| 1668 | SET_TOP(res); |
| 1669 | if (res == NULL) |
| 1670 | goto error; |
| 1671 | DISPATCH(); |
| 1672 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1673 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1674 | TARGET(INPLACE_AND) { |
| 1675 | PyObject *right = POP(); |
| 1676 | PyObject *left = TOP(); |
| 1677 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1678 | Py_DECREF(left); |
| 1679 | Py_DECREF(right); |
| 1680 | SET_TOP(res); |
| 1681 | if (res == NULL) |
| 1682 | goto error; |
| 1683 | DISPATCH(); |
| 1684 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1685 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1686 | TARGET(INPLACE_XOR) { |
| 1687 | PyObject *right = POP(); |
| 1688 | PyObject *left = TOP(); |
| 1689 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1690 | Py_DECREF(left); |
| 1691 | Py_DECREF(right); |
| 1692 | SET_TOP(res); |
| 1693 | if (res == NULL) |
| 1694 | goto error; |
| 1695 | DISPATCH(); |
| 1696 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1697 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1698 | TARGET(INPLACE_OR) { |
| 1699 | PyObject *right = POP(); |
| 1700 | PyObject *left = TOP(); |
| 1701 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1702 | Py_DECREF(left); |
| 1703 | Py_DECREF(right); |
| 1704 | SET_TOP(res); |
| 1705 | if (res == NULL) |
| 1706 | goto error; |
| 1707 | DISPATCH(); |
| 1708 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1709 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1710 | TARGET(STORE_SUBSCR) { |
| 1711 | PyObject *sub = TOP(); |
| 1712 | PyObject *container = SECOND(); |
| 1713 | PyObject *v = THIRD(); |
| 1714 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | STACKADJ(-3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1716 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1717 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1719 | Py_DECREF(container); |
| 1720 | Py_DECREF(sub); |
| 1721 | if (err != 0) |
| 1722 | goto error; |
| 1723 | DISPATCH(); |
| 1724 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1725 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1726 | TARGET(STORE_ANNOTATION) { |
| 1727 | _Py_IDENTIFIER(__annotations__); |
| 1728 | PyObject *ann_dict; |
| 1729 | PyObject *ann = POP(); |
| 1730 | PyObject *name = GETITEM(names, oparg); |
| 1731 | int err; |
| 1732 | if (f->f_locals == NULL) { |
| 1733 | PyErr_Format(PyExc_SystemError, |
| 1734 | "no locals found when storing annotation"); |
| 1735 | Py_DECREF(ann); |
| 1736 | goto error; |
| 1737 | } |
| 1738 | /* first try to get __annotations__ from locals... */ |
| 1739 | if (PyDict_CheckExact(f->f_locals)) { |
| 1740 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 1741 | &PyId___annotations__); |
| 1742 | if (ann_dict == NULL) { |
| 1743 | PyErr_SetString(PyExc_NameError, |
| 1744 | "__annotations__ not found"); |
| 1745 | Py_DECREF(ann); |
| 1746 | goto error; |
| 1747 | } |
| 1748 | Py_INCREF(ann_dict); |
| 1749 | } |
| 1750 | else { |
| 1751 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 1752 | if (ann_str == NULL) { |
| 1753 | Py_DECREF(ann); |
| 1754 | goto error; |
| 1755 | } |
| 1756 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 1757 | if (ann_dict == NULL) { |
| 1758 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 1759 | PyErr_SetString(PyExc_NameError, |
| 1760 | "__annotations__ not found"); |
| 1761 | } |
| 1762 | Py_DECREF(ann); |
| 1763 | goto error; |
| 1764 | } |
| 1765 | } |
| 1766 | /* ...if succeeded, __annotations__[name] = ann */ |
| 1767 | if (PyDict_CheckExact(ann_dict)) { |
| 1768 | err = PyDict_SetItem(ann_dict, name, ann); |
| 1769 | } |
| 1770 | else { |
| 1771 | err = PyObject_SetItem(ann_dict, name, ann); |
| 1772 | } |
| 1773 | Py_DECREF(ann_dict); |
Yury Selivanov | 50c584f | 2016-09-08 23:38:21 -0700 | [diff] [blame] | 1774 | Py_DECREF(ann); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1775 | if (err != 0) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1776 | goto error; |
| 1777 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1778 | DISPATCH(); |
| 1779 | } |
| 1780 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1781 | TARGET(DELETE_SUBSCR) { |
| 1782 | PyObject *sub = TOP(); |
| 1783 | PyObject *container = SECOND(); |
| 1784 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | STACKADJ(-2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1786 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1787 | err = PyObject_DelItem(container, sub); |
| 1788 | Py_DECREF(container); |
| 1789 | Py_DECREF(sub); |
| 1790 | if (err != 0) |
| 1791 | goto error; |
| 1792 | DISPATCH(); |
| 1793 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1794 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1795 | TARGET(PRINT_EXPR) { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1796 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1797 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1798 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1799 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1800 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | PyErr_SetString(PyExc_RuntimeError, |
| 1802 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1803 | Py_DECREF(value); |
| 1804 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | } |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1806 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1807 | Py_DECREF(value); |
| 1808 | if (res == NULL) |
| 1809 | goto error; |
| 1810 | Py_DECREF(res); |
| 1811 | DISPATCH(); |
| 1812 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1813 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1814 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1816 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1817 | TARGET(RAISE_VARARGS) { |
| 1818 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | switch (oparg) { |
| 1820 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1821 | cause = POP(); /* cause */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1823 | exc = POP(); /* exc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | case 0: /* Fallthrough */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1825 | if (do_raise(exc, cause)) { |
| 1826 | why = WHY_EXCEPTION; |
| 1827 | goto fast_block_end; |
| 1828 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | break; |
| 1830 | default: |
| 1831 | PyErr_SetString(PyExc_SystemError, |
| 1832 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | break; |
| 1834 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1835 | goto error; |
| 1836 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1837 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1838 | TARGET(RETURN_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | retval = POP(); |
| 1840 | why = WHY_RETURN; |
| 1841 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1842 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1843 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1844 | TARGET(GET_AITER) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1845 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1846 | PyObject *iter = NULL; |
| 1847 | PyObject *awaitable = NULL; |
| 1848 | PyObject *obj = TOP(); |
| 1849 | PyTypeObject *type = Py_TYPE(obj); |
| 1850 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1851 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1852 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1853 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1854 | |
| 1855 | if (getter != NULL) { |
| 1856 | iter = (*getter)(obj); |
| 1857 | Py_DECREF(obj); |
| 1858 | if (iter == NULL) { |
| 1859 | SET_TOP(NULL); |
| 1860 | goto error; |
| 1861 | } |
| 1862 | } |
| 1863 | else { |
| 1864 | SET_TOP(NULL); |
| 1865 | PyErr_Format( |
| 1866 | PyExc_TypeError, |
| 1867 | "'async for' requires an object with " |
| 1868 | "__aiter__ method, got %.100s", |
| 1869 | type->tp_name); |
| 1870 | Py_DECREF(obj); |
| 1871 | goto error; |
| 1872 | } |
| 1873 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1874 | if (Py_TYPE(iter)->tp_as_async != NULL && |
| 1875 | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
| 1876 | |
| 1877 | /* Starting with CPython 3.5.2 __aiter__ should return |
| 1878 | asynchronous iterators directly (not awaitables that |
| 1879 | resolve to asynchronous iterators.) |
| 1880 | |
| 1881 | Therefore, we check if the object that was returned |
| 1882 | from __aiter__ has an __anext__ method. If it does, |
| 1883 | we wrap it in an awaitable that resolves to `iter`. |
| 1884 | |
| 1885 | See http://bugs.python.org/issue27243 for more |
| 1886 | details. |
| 1887 | */ |
| 1888 | |
| 1889 | PyObject *wrapper = _PyAIterWrapper_New(iter); |
| 1890 | Py_DECREF(iter); |
| 1891 | SET_TOP(wrapper); |
| 1892 | DISPATCH(); |
| 1893 | } |
| 1894 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1895 | awaitable = _PyCoro_GetAwaitableIter(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1896 | if (awaitable == NULL) { |
| 1897 | SET_TOP(NULL); |
| 1898 | PyErr_Format( |
| 1899 | PyExc_TypeError, |
| 1900 | "'async for' received an invalid object " |
| 1901 | "from __aiter__: %.100s", |
| 1902 | Py_TYPE(iter)->tp_name); |
| 1903 | |
| 1904 | Py_DECREF(iter); |
| 1905 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1906 | } else { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1907 | Py_DECREF(iter); |
| 1908 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1909 | if (PyErr_WarnFormat( |
| 1910 | PyExc_PendingDeprecationWarning, 1, |
| 1911 | "'%.100s' implements legacy __aiter__ protocol; " |
| 1912 | "__aiter__ should return an asynchronous " |
| 1913 | "iterator, not awaitable", |
| 1914 | type->tp_name)) |
| 1915 | { |
| 1916 | /* Warning was converted to an error. */ |
| 1917 | Py_DECREF(awaitable); |
| 1918 | SET_TOP(NULL); |
| 1919 | goto error; |
| 1920 | } |
| 1921 | } |
| 1922 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1923 | SET_TOP(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1924 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1925 | DISPATCH(); |
| 1926 | } |
| 1927 | |
| 1928 | TARGET(GET_ANEXT) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1929 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1930 | PyObject *next_iter = NULL; |
| 1931 | PyObject *awaitable = NULL; |
| 1932 | PyObject *aiter = TOP(); |
| 1933 | PyTypeObject *type = Py_TYPE(aiter); |
| 1934 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1935 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1936 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1937 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1938 | goto error; |
| 1939 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1940 | } else { |
| 1941 | if (type->tp_as_async != NULL){ |
| 1942 | getter = type->tp_as_async->am_anext; |
| 1943 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1944 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1945 | if (getter != NULL) { |
| 1946 | next_iter = (*getter)(aiter); |
| 1947 | if (next_iter == NULL) { |
| 1948 | goto error; |
| 1949 | } |
| 1950 | } |
| 1951 | else { |
| 1952 | PyErr_Format( |
| 1953 | PyExc_TypeError, |
| 1954 | "'async for' requires an iterator with " |
| 1955 | "__anext__ method, got %.100s", |
| 1956 | type->tp_name); |
| 1957 | goto error; |
| 1958 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1959 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1960 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1961 | if (awaitable == NULL) { |
| 1962 | PyErr_Format( |
| 1963 | PyExc_TypeError, |
| 1964 | "'async for' received an invalid object " |
| 1965 | "from __anext__: %.100s", |
| 1966 | Py_TYPE(next_iter)->tp_name); |
| 1967 | |
| 1968 | Py_DECREF(next_iter); |
| 1969 | goto error; |
| 1970 | } else { |
| 1971 | Py_DECREF(next_iter); |
| 1972 | } |
| 1973 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1974 | |
| 1975 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1976 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1977 | DISPATCH(); |
| 1978 | } |
| 1979 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1980 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1981 | TARGET(GET_AWAITABLE) { |
| 1982 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1983 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1984 | |
| 1985 | Py_DECREF(iterable); |
| 1986 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1987 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1988 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1989 | if (yf != NULL) { |
| 1990 | /* `iter` is a coroutine object that is being |
| 1991 | awaited, `yf` is a pointer to the current awaitable |
| 1992 | being awaited on. */ |
| 1993 | Py_DECREF(yf); |
| 1994 | Py_CLEAR(iter); |
| 1995 | PyErr_SetString( |
| 1996 | PyExc_RuntimeError, |
| 1997 | "coroutine is being awaited already"); |
| 1998 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 1999 | } |
| 2000 | } |
| 2001 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2002 | SET_TOP(iter); /* Even if it's NULL */ |
| 2003 | |
| 2004 | if (iter == NULL) { |
| 2005 | goto error; |
| 2006 | } |
| 2007 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2008 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2009 | DISPATCH(); |
| 2010 | } |
| 2011 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2012 | TARGET(YIELD_FROM) { |
| 2013 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2014 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2015 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2016 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2017 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2018 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2019 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2020 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2021 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2022 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2023 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2024 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2025 | Py_DECREF(v); |
| 2026 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2027 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2028 | if (tstate->c_tracefunc != NULL |
| 2029 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2030 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2031 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2032 | if (err < 0) |
| 2033 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2034 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2035 | SET_TOP(val); |
| 2036 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2037 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2038 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2039 | f->f_stacktop = stack_pointer; |
| 2040 | why = WHY_YIELD; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2041 | /* and repeat... */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2042 | f->f_lasti -= 2; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2043 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2044 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2045 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2046 | TARGET(YIELD_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2048 | |
| 2049 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2050 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2051 | Py_DECREF(retval); |
| 2052 | if (w == NULL) { |
| 2053 | retval = NULL; |
| 2054 | goto error; |
| 2055 | } |
| 2056 | retval = w; |
| 2057 | } |
| 2058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | f->f_stacktop = stack_pointer; |
| 2060 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2062 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2063 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2064 | TARGET(POP_EXCEPT) { |
| 2065 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2066 | if (b->b_type != EXCEPT_HANDLER) { |
| 2067 | PyErr_SetString(PyExc_SystemError, |
| 2068 | "popped block is not an except handler"); |
| 2069 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2071 | UNWIND_EXCEPT_HANDLER(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2073 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2074 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2075 | PREDICTED(POP_BLOCK); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2076 | TARGET(POP_BLOCK) { |
| 2077 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2078 | UNWIND_BLOCK(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2079 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2080 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2083 | TARGET(END_FINALLY) { |
| 2084 | PyObject *status = POP(); |
| 2085 | if (PyLong_Check(status)) { |
| 2086 | why = (enum why_code) PyLong_AS_LONG(status); |
| 2087 | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | if (why == WHY_RETURN || |
| 2089 | why == WHY_CONTINUE) |
| 2090 | retval = POP(); |
| 2091 | if (why == WHY_SILENCED) { |
| 2092 | /* An exception was silenced by 'with', we must |
| 2093 | manually unwind the EXCEPT_HANDLER block which was |
| 2094 | created when the exception was caught, otherwise |
| 2095 | the stack will be in an inconsistent state. */ |
| 2096 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2097 | assert(b->b_type == EXCEPT_HANDLER); |
| 2098 | UNWIND_EXCEPT_HANDLER(b); |
| 2099 | why = WHY_NOT; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2100 | Py_DECREF(status); |
| 2101 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2103 | Py_DECREF(status); |
| 2104 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2106 | else if (PyExceptionClass_Check(status)) { |
| 2107 | PyObject *exc = POP(); |
| 2108 | PyObject *tb = POP(); |
| 2109 | PyErr_Restore(status, exc, tb); |
| 2110 | why = WHY_EXCEPTION; |
| 2111 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2113 | else if (status != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | PyErr_SetString(PyExc_SystemError, |
| 2115 | "'finally' pops bad exception"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2116 | Py_DECREF(status); |
| 2117 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2119 | Py_DECREF(status); |
| 2120 | DISPATCH(); |
| 2121 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2122 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2123 | TARGET(LOAD_BUILD_CLASS) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2124 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2125 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2126 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2127 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2128 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2129 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2130 | PyErr_SetString(PyExc_NameError, |
| 2131 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2132 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2133 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2134 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2135 | } |
| 2136 | else { |
| 2137 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2138 | if (build_class_str == NULL) |
| 2139 | break; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2140 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2141 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2142 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2143 | PyErr_SetString(PyExc_NameError, |
| 2144 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2145 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2146 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2148 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2149 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2150 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2151 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2152 | TARGET(STORE_NAME) { |
| 2153 | PyObject *name = GETITEM(names, oparg); |
| 2154 | PyObject *v = POP(); |
| 2155 | PyObject *ns = f->f_locals; |
| 2156 | int err; |
| 2157 | if (ns == NULL) { |
| 2158 | PyErr_Format(PyExc_SystemError, |
| 2159 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2161 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2163 | if (PyDict_CheckExact(ns)) |
| 2164 | err = PyDict_SetItem(ns, name, v); |
| 2165 | else |
| 2166 | err = PyObject_SetItem(ns, name, v); |
| 2167 | Py_DECREF(v); |
| 2168 | if (err != 0) |
| 2169 | goto error; |
| 2170 | DISPATCH(); |
| 2171 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2172 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2173 | TARGET(DELETE_NAME) { |
| 2174 | PyObject *name = GETITEM(names, oparg); |
| 2175 | PyObject *ns = f->f_locals; |
| 2176 | int err; |
| 2177 | if (ns == NULL) { |
| 2178 | PyErr_Format(PyExc_SystemError, |
| 2179 | "no locals when deleting %R", name); |
| 2180 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2182 | err = PyObject_DelItem(ns, name); |
| 2183 | if (err != 0) { |
| 2184 | format_exc_check_arg(PyExc_NameError, |
| 2185 | NAME_ERROR_MSG, |
| 2186 | name); |
| 2187 | goto error; |
| 2188 | } |
| 2189 | DISPATCH(); |
| 2190 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2191 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2192 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2193 | TARGET(UNPACK_SEQUENCE) { |
| 2194 | PyObject *seq = POP(), *item, **items; |
| 2195 | if (PyTuple_CheckExact(seq) && |
| 2196 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2197 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2199 | item = items[oparg]; |
| 2200 | Py_INCREF(item); |
| 2201 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2203 | } else if (PyList_CheckExact(seq) && |
| 2204 | PyList_GET_SIZE(seq) == oparg) { |
| 2205 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2207 | item = items[oparg]; |
| 2208 | Py_INCREF(item); |
| 2209 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2211 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | stack_pointer + oparg)) { |
| 2213 | STACKADJ(oparg); |
| 2214 | } else { |
| 2215 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2216 | Py_DECREF(seq); |
| 2217 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2219 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2220 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2222 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2223 | TARGET(UNPACK_EX) { |
| 2224 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2225 | PyObject *seq = POP(); |
| 2226 | |
| 2227 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2228 | stack_pointer + totalargs)) { |
| 2229 | stack_pointer += totalargs; |
| 2230 | } else { |
| 2231 | Py_DECREF(seq); |
| 2232 | goto error; |
| 2233 | } |
| 2234 | Py_DECREF(seq); |
| 2235 | DISPATCH(); |
| 2236 | } |
| 2237 | |
| 2238 | TARGET(STORE_ATTR) { |
| 2239 | PyObject *name = GETITEM(names, oparg); |
| 2240 | PyObject *owner = TOP(); |
| 2241 | PyObject *v = SECOND(); |
| 2242 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | STACKADJ(-2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2244 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2246 | Py_DECREF(owner); |
| 2247 | if (err != 0) |
| 2248 | goto error; |
| 2249 | DISPATCH(); |
| 2250 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2251 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2252 | TARGET(DELETE_ATTR) { |
| 2253 | PyObject *name = GETITEM(names, oparg); |
| 2254 | PyObject *owner = POP(); |
| 2255 | int err; |
| 2256 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2257 | Py_DECREF(owner); |
| 2258 | if (err != 0) |
| 2259 | goto error; |
| 2260 | DISPATCH(); |
| 2261 | } |
| 2262 | |
| 2263 | TARGET(STORE_GLOBAL) { |
| 2264 | PyObject *name = GETITEM(names, oparg); |
| 2265 | PyObject *v = POP(); |
| 2266 | int err; |
| 2267 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2269 | if (err != 0) |
| 2270 | goto error; |
| 2271 | DISPATCH(); |
| 2272 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2273 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2274 | TARGET(DELETE_GLOBAL) { |
| 2275 | PyObject *name = GETITEM(names, oparg); |
| 2276 | int err; |
| 2277 | err = PyDict_DelItem(f->f_globals, name); |
| 2278 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2279 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2280 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2281 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2282 | } |
| 2283 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2284 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2285 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2286 | TARGET(LOAD_NAME) { |
| 2287 | PyObject *name = GETITEM(names, oparg); |
| 2288 | PyObject *locals = f->f_locals; |
| 2289 | PyObject *v; |
| 2290 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2292 | "no locals when loading %R", name); |
| 2293 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2295 | if (PyDict_CheckExact(locals)) { |
| 2296 | v = PyDict_GetItem(locals, name); |
| 2297 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | } |
| 2299 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2300 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2301 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2302 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2303 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | PyErr_Clear(); |
| 2305 | } |
| 2306 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2307 | if (v == NULL) { |
| 2308 | v = PyDict_GetItem(f->f_globals, name); |
| 2309 | Py_XINCREF(v); |
| 2310 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2311 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2312 | v = PyDict_GetItem(f->f_builtins, name); |
| 2313 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2314 | format_exc_check_arg( |
| 2315 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2316 | NAME_ERROR_MSG, name); |
| 2317 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2318 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2319 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2320 | } |
| 2321 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2322 | v = PyObject_GetItem(f->f_builtins, name); |
| 2323 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2324 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2325 | format_exc_check_arg( |
| 2326 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2327 | NAME_ERROR_MSG, name); |
| 2328 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2329 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2330 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2333 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2335 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2336 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2337 | TARGET(LOAD_GLOBAL) { |
| 2338 | PyObject *name = GETITEM(names, oparg); |
| 2339 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2340 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2341 | && PyDict_CheckExact(f->f_builtins)) |
| 2342 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2343 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2344 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2345 | name); |
| 2346 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2347 | if (!_PyErr_OCCURRED()) { |
| 2348 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2349 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2350 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2351 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2352 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2353 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2355 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2357 | else { |
| 2358 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2359 | |
| 2360 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2361 | v = PyObject_GetItem(f->f_globals, name); |
| 2362 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2363 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2364 | goto error; |
| 2365 | PyErr_Clear(); |
| 2366 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2367 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2368 | v = PyObject_GetItem(f->f_builtins, name); |
| 2369 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2370 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2371 | format_exc_check_arg( |
| 2372 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2373 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2374 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2378 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2380 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2381 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2382 | TARGET(DELETE_FAST) { |
| 2383 | PyObject *v = GETLOCAL(oparg); |
| 2384 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | SETLOCAL(oparg, NULL); |
| 2386 | DISPATCH(); |
| 2387 | } |
| 2388 | format_exc_check_arg( |
| 2389 | PyExc_UnboundLocalError, |
| 2390 | UNBOUNDLOCAL_ERROR_MSG, |
| 2391 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2392 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2393 | goto error; |
| 2394 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2395 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2396 | TARGET(DELETE_DEREF) { |
| 2397 | PyObject *cell = freevars[oparg]; |
| 2398 | if (PyCell_GET(cell) != NULL) { |
| 2399 | PyCell_Set(cell, NULL); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2400 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2401 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2402 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2403 | goto error; |
| 2404 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2405 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2406 | TARGET(LOAD_CLOSURE) { |
| 2407 | PyObject *cell = freevars[oparg]; |
| 2408 | Py_INCREF(cell); |
| 2409 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2411 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2412 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2413 | TARGET(LOAD_CLASSDEREF) { |
| 2414 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2415 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2416 | assert(locals); |
| 2417 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2418 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2419 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2420 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2421 | if (PyDict_CheckExact(locals)) { |
| 2422 | value = PyDict_GetItem(locals, name); |
| 2423 | Py_XINCREF(value); |
| 2424 | } |
| 2425 | else { |
| 2426 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2427 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2428 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2429 | goto error; |
| 2430 | PyErr_Clear(); |
| 2431 | } |
| 2432 | } |
| 2433 | if (!value) { |
| 2434 | PyObject *cell = freevars[oparg]; |
| 2435 | value = PyCell_GET(cell); |
| 2436 | if (value == NULL) { |
| 2437 | format_exc_unbound(co, oparg); |
| 2438 | goto error; |
| 2439 | } |
| 2440 | Py_INCREF(value); |
| 2441 | } |
| 2442 | PUSH(value); |
| 2443 | DISPATCH(); |
| 2444 | } |
| 2445 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2446 | TARGET(LOAD_DEREF) { |
| 2447 | PyObject *cell = freevars[oparg]; |
| 2448 | PyObject *value = PyCell_GET(cell); |
| 2449 | if (value == NULL) { |
| 2450 | format_exc_unbound(co, oparg); |
| 2451 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2453 | Py_INCREF(value); |
| 2454 | PUSH(value); |
| 2455 | DISPATCH(); |
| 2456 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2457 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2458 | TARGET(STORE_DEREF) { |
| 2459 | PyObject *v = POP(); |
| 2460 | PyObject *cell = freevars[oparg]; |
| 2461 | PyCell_Set(cell, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2463 | DISPATCH(); |
| 2464 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2465 | |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2466 | TARGET(BUILD_STRING) { |
| 2467 | PyObject *str; |
| 2468 | PyObject *empty = PyUnicode_New(0, 0); |
| 2469 | if (empty == NULL) { |
| 2470 | goto error; |
| 2471 | } |
| 2472 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2473 | Py_DECREF(empty); |
| 2474 | if (str == NULL) |
| 2475 | goto error; |
| 2476 | while (--oparg >= 0) { |
| 2477 | PyObject *item = POP(); |
| 2478 | Py_DECREF(item); |
| 2479 | } |
| 2480 | PUSH(str); |
| 2481 | DISPATCH(); |
| 2482 | } |
| 2483 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2484 | TARGET(BUILD_TUPLE) { |
| 2485 | PyObject *tup = PyTuple_New(oparg); |
| 2486 | if (tup == NULL) |
| 2487 | goto error; |
| 2488 | while (--oparg >= 0) { |
| 2489 | PyObject *item = POP(); |
| 2490 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2491 | } |
| 2492 | PUSH(tup); |
| 2493 | DISPATCH(); |
| 2494 | } |
| 2495 | |
| 2496 | TARGET(BUILD_LIST) { |
| 2497 | PyObject *list = PyList_New(oparg); |
| 2498 | if (list == NULL) |
| 2499 | goto error; |
| 2500 | while (--oparg >= 0) { |
| 2501 | PyObject *item = POP(); |
| 2502 | PyList_SET_ITEM(list, oparg, item); |
| 2503 | } |
| 2504 | PUSH(list); |
| 2505 | DISPATCH(); |
| 2506 | } |
| 2507 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2508 | TARGET(BUILD_TUPLE_UNPACK) |
| 2509 | TARGET(BUILD_LIST_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2510 | int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2511 | Py_ssize_t i; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2512 | PyObject *sum; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2513 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2514 | |
| 2515 | if (convert_to_tuple && oparg == 1 && PyTuple_CheckExact(TOP())) { |
| 2516 | DISPATCH(); |
| 2517 | } |
| 2518 | |
| 2519 | sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2520 | if (sum == NULL) |
| 2521 | goto error; |
| 2522 | |
| 2523 | for (i = oparg; i > 0; i--) { |
| 2524 | PyObject *none_val; |
| 2525 | |
| 2526 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2527 | if (none_val == NULL) { |
| 2528 | Py_DECREF(sum); |
| 2529 | goto error; |
| 2530 | } |
| 2531 | Py_DECREF(none_val); |
| 2532 | } |
| 2533 | |
| 2534 | if (convert_to_tuple) { |
| 2535 | return_value = PyList_AsTuple(sum); |
| 2536 | Py_DECREF(sum); |
| 2537 | if (return_value == NULL) |
| 2538 | goto error; |
| 2539 | } |
| 2540 | else { |
| 2541 | return_value = sum; |
| 2542 | } |
| 2543 | |
| 2544 | while (oparg--) |
| 2545 | Py_DECREF(POP()); |
| 2546 | PUSH(return_value); |
| 2547 | DISPATCH(); |
| 2548 | } |
| 2549 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2550 | TARGET(BUILD_SET) { |
| 2551 | PyObject *set = PySet_New(NULL); |
| 2552 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2553 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2554 | if (set == NULL) |
| 2555 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2556 | for (i = oparg; i > 0; i--) { |
| 2557 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2558 | if (err == 0) |
| 2559 | err = PySet_Add(set, item); |
| 2560 | Py_DECREF(item); |
| 2561 | } |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2562 | STACKADJ(-oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2563 | if (err != 0) { |
| 2564 | Py_DECREF(set); |
| 2565 | goto error; |
| 2566 | } |
| 2567 | PUSH(set); |
| 2568 | DISPATCH(); |
| 2569 | } |
| 2570 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2571 | TARGET(BUILD_SET_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2572 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2573 | PyObject *sum = PySet_New(NULL); |
| 2574 | if (sum == NULL) |
| 2575 | goto error; |
| 2576 | |
| 2577 | for (i = oparg; i > 0; i--) { |
| 2578 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2579 | Py_DECREF(sum); |
| 2580 | goto error; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | while (oparg--) |
| 2585 | Py_DECREF(POP()); |
| 2586 | PUSH(sum); |
| 2587 | DISPATCH(); |
| 2588 | } |
| 2589 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2590 | TARGET(BUILD_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2591 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2592 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2593 | if (map == NULL) |
| 2594 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2595 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2596 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2597 | PyObject *key = PEEK(2*i); |
| 2598 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2599 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2600 | if (err != 0) { |
| 2601 | Py_DECREF(map); |
| 2602 | goto error; |
| 2603 | } |
| 2604 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2605 | |
| 2606 | while (oparg--) { |
| 2607 | Py_DECREF(POP()); |
| 2608 | Py_DECREF(POP()); |
| 2609 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2610 | PUSH(map); |
| 2611 | DISPATCH(); |
| 2612 | } |
| 2613 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2614 | TARGET(SETUP_ANNOTATIONS) { |
| 2615 | _Py_IDENTIFIER(__annotations__); |
| 2616 | int err; |
| 2617 | PyObject *ann_dict; |
| 2618 | if (f->f_locals == NULL) { |
| 2619 | PyErr_Format(PyExc_SystemError, |
| 2620 | "no locals found when setting up annotations"); |
| 2621 | goto error; |
| 2622 | } |
| 2623 | /* check if __annotations__ in locals()... */ |
| 2624 | if (PyDict_CheckExact(f->f_locals)) { |
| 2625 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2626 | &PyId___annotations__); |
| 2627 | if (ann_dict == NULL) { |
| 2628 | /* ...if not, create a new one */ |
| 2629 | ann_dict = PyDict_New(); |
| 2630 | if (ann_dict == NULL) { |
| 2631 | goto error; |
| 2632 | } |
| 2633 | err = _PyDict_SetItemId(f->f_locals, |
| 2634 | &PyId___annotations__, ann_dict); |
| 2635 | Py_DECREF(ann_dict); |
| 2636 | if (err != 0) { |
| 2637 | goto error; |
| 2638 | } |
| 2639 | } |
| 2640 | } |
| 2641 | else { |
| 2642 | /* do the same if locals() is not a dict */ |
| 2643 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2644 | if (ann_str == NULL) { |
| 2645 | break; |
| 2646 | } |
| 2647 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2648 | if (ann_dict == NULL) { |
| 2649 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2650 | goto error; |
| 2651 | } |
| 2652 | PyErr_Clear(); |
| 2653 | ann_dict = PyDict_New(); |
| 2654 | if (ann_dict == NULL) { |
| 2655 | goto error; |
| 2656 | } |
| 2657 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2658 | Py_DECREF(ann_dict); |
| 2659 | if (err != 0) { |
| 2660 | goto error; |
| 2661 | } |
| 2662 | } |
| 2663 | else { |
| 2664 | Py_DECREF(ann_dict); |
| 2665 | } |
| 2666 | } |
| 2667 | DISPATCH(); |
| 2668 | } |
| 2669 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2670 | TARGET(BUILD_CONST_KEY_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2671 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2672 | PyObject *map; |
| 2673 | PyObject *keys = TOP(); |
| 2674 | if (!PyTuple_CheckExact(keys) || |
| 2675 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2676 | PyErr_SetString(PyExc_SystemError, |
| 2677 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2678 | goto error; |
| 2679 | } |
| 2680 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2681 | if (map == NULL) { |
| 2682 | goto error; |
| 2683 | } |
| 2684 | for (i = oparg; i > 0; i--) { |
| 2685 | int err; |
| 2686 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2687 | PyObject *value = PEEK(i + 1); |
| 2688 | err = PyDict_SetItem(map, key, value); |
| 2689 | if (err != 0) { |
| 2690 | Py_DECREF(map); |
| 2691 | goto error; |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | Py_DECREF(POP()); |
| 2696 | while (oparg--) { |
| 2697 | Py_DECREF(POP()); |
| 2698 | } |
| 2699 | PUSH(map); |
| 2700 | DISPATCH(); |
| 2701 | } |
| 2702 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2703 | TARGET(BUILD_MAP_UNPACK_WITH_CALL) |
| 2704 | TARGET(BUILD_MAP_UNPACK) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2705 | int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2706 | Py_ssize_t i; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2707 | PyObject *sum; |
| 2708 | |
| 2709 | if (with_call && oparg == 1 && PyDict_CheckExact(TOP())) { |
| 2710 | DISPATCH(); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2711 | } |
| 2712 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2713 | sum = PyDict_New(); |
| 2714 | if (sum == NULL) |
| 2715 | goto error; |
| 2716 | |
| 2717 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2718 | PyObject *arg = PEEK(i); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2719 | if (with_call && PyDict_Size(sum)) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2720 | PyObject *intersection = _PyDictView_Intersect(sum, arg); |
| 2721 | |
| 2722 | if (intersection == NULL) { |
| 2723 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2724 | PyObject *func = PEEK(2 + oparg); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2725 | PyErr_Format(PyExc_TypeError, |
| 2726 | "%.200s%.200s argument after ** " |
| 2727 | "must be a mapping, not %.200s", |
| 2728 | PyEval_GetFuncName(func), |
| 2729 | PyEval_GetFuncDesc(func), |
| 2730 | arg->ob_type->tp_name); |
| 2731 | } |
| 2732 | Py_DECREF(sum); |
| 2733 | goto error; |
| 2734 | } |
| 2735 | |
| 2736 | if (PySet_GET_SIZE(intersection)) { |
| 2737 | Py_ssize_t idx = 0; |
| 2738 | PyObject *key; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2739 | PyObject *func = PEEK(2 + oparg); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2740 | Py_hash_t hash; |
| 2741 | _PySet_NextEntry(intersection, &idx, &key, &hash); |
| 2742 | if (!PyUnicode_Check(key)) { |
| 2743 | PyErr_Format(PyExc_TypeError, |
| 2744 | "%.200s%.200s keywords must be strings", |
| 2745 | PyEval_GetFuncName(func), |
| 2746 | PyEval_GetFuncDesc(func)); |
| 2747 | } else { |
| 2748 | PyErr_Format(PyExc_TypeError, |
| 2749 | "%.200s%.200s got multiple " |
| 2750 | "values for keyword argument '%U'", |
| 2751 | PyEval_GetFuncName(func), |
| 2752 | PyEval_GetFuncDesc(func), |
| 2753 | key); |
| 2754 | } |
| 2755 | Py_DECREF(intersection); |
| 2756 | Py_DECREF(sum); |
| 2757 | goto error; |
| 2758 | } |
| 2759 | Py_DECREF(intersection); |
| 2760 | } |
| 2761 | |
| 2762 | if (PyDict_Update(sum, arg) < 0) { |
| 2763 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2764 | PyErr_Format(PyExc_TypeError, |
| 2765 | "'%.200s' object is not a mapping", |
| 2766 | arg->ob_type->tp_name); |
| 2767 | } |
| 2768 | Py_DECREF(sum); |
| 2769 | goto error; |
| 2770 | } |
| 2771 | } |
| 2772 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2773 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2774 | Py_DECREF(POP()); |
| 2775 | PUSH(sum); |
| 2776 | DISPATCH(); |
| 2777 | } |
| 2778 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2779 | TARGET(MAP_ADD) { |
| 2780 | PyObject *key = TOP(); |
| 2781 | PyObject *value = SECOND(); |
| 2782 | PyObject *map; |
| 2783 | int err; |
| 2784 | STACKADJ(-2); |
| 2785 | map = stack_pointer[-oparg]; /* dict */ |
| 2786 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2787 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2788 | Py_DECREF(value); |
| 2789 | Py_DECREF(key); |
| 2790 | if (err != 0) |
| 2791 | goto error; |
| 2792 | PREDICT(JUMP_ABSOLUTE); |
| 2793 | DISPATCH(); |
| 2794 | } |
| 2795 | |
| 2796 | TARGET(LOAD_ATTR) { |
| 2797 | PyObject *name = GETITEM(names, oparg); |
| 2798 | PyObject *owner = TOP(); |
| 2799 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2800 | Py_DECREF(owner); |
| 2801 | SET_TOP(res); |
| 2802 | if (res == NULL) |
| 2803 | goto error; |
| 2804 | DISPATCH(); |
| 2805 | } |
| 2806 | |
| 2807 | TARGET(COMPARE_OP) { |
| 2808 | PyObject *right = POP(); |
| 2809 | PyObject *left = TOP(); |
| 2810 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2811 | Py_DECREF(left); |
| 2812 | Py_DECREF(right); |
| 2813 | SET_TOP(res); |
| 2814 | if (res == NULL) |
| 2815 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | PREDICT(POP_JUMP_IF_FALSE); |
| 2817 | PREDICT(POP_JUMP_IF_TRUE); |
| 2818 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2819 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2820 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2821 | TARGET(IMPORT_NAME) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2822 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2823 | PyObject *fromlist = POP(); |
| 2824 | PyObject *level = TOP(); |
| 2825 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2826 | res = import_name(f, name, fromlist, level); |
| 2827 | Py_DECREF(level); |
| 2828 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2829 | SET_TOP(res); |
| 2830 | if (res == NULL) |
| 2831 | goto error; |
| 2832 | DISPATCH(); |
| 2833 | } |
| 2834 | |
| 2835 | TARGET(IMPORT_STAR) { |
| 2836 | PyObject *from = POP(), *locals; |
| 2837 | int err; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2838 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 2839 | goto error; |
| 2840 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2841 | locals = f->f_locals; |
| 2842 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | PyErr_SetString(PyExc_SystemError, |
| 2844 | "no locals found during 'import *'"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2845 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2847 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2849 | Py_DECREF(from); |
| 2850 | if (err != 0) |
| 2851 | goto error; |
| 2852 | DISPATCH(); |
| 2853 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2854 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2855 | TARGET(IMPORT_FROM) { |
| 2856 | PyObject *name = GETITEM(names, oparg); |
| 2857 | PyObject *from = TOP(); |
| 2858 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2859 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2860 | PUSH(res); |
| 2861 | if (res == NULL) |
| 2862 | goto error; |
| 2863 | DISPATCH(); |
| 2864 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2865 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2866 | TARGET(JUMP_FORWARD) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | JUMPBY(oparg); |
| 2868 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2869 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2870 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2871 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2872 | TARGET(POP_JUMP_IF_FALSE) { |
| 2873 | PyObject *cond = POP(); |
| 2874 | int err; |
| 2875 | if (cond == Py_True) { |
| 2876 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | FAST_DISPATCH(); |
| 2878 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2879 | if (cond == Py_False) { |
| 2880 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2881 | JUMPTO(oparg); |
| 2882 | FAST_DISPATCH(); |
| 2883 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2884 | err = PyObject_IsTrue(cond); |
| 2885 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | if (err > 0) |
| 2887 | err = 0; |
| 2888 | else if (err == 0) |
| 2889 | JUMPTO(oparg); |
| 2890 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2891 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2893 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2894 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2895 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2896 | TARGET(POP_JUMP_IF_TRUE) { |
| 2897 | PyObject *cond = POP(); |
| 2898 | int err; |
| 2899 | if (cond == Py_False) { |
| 2900 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | FAST_DISPATCH(); |
| 2902 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2903 | if (cond == Py_True) { |
| 2904 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 | JUMPTO(oparg); |
| 2906 | FAST_DISPATCH(); |
| 2907 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2908 | err = PyObject_IsTrue(cond); |
| 2909 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 | if (err > 0) { |
| 2911 | err = 0; |
| 2912 | JUMPTO(oparg); |
| 2913 | } |
| 2914 | else if (err == 0) |
| 2915 | ; |
| 2916 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2917 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2919 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2920 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2921 | TARGET(JUMP_IF_FALSE_OR_POP) { |
| 2922 | PyObject *cond = TOP(); |
| 2923 | int err; |
| 2924 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2926 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2927 | FAST_DISPATCH(); |
| 2928 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2929 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2930 | JUMPTO(oparg); |
| 2931 | FAST_DISPATCH(); |
| 2932 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2933 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | if (err > 0) { |
| 2935 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2936 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | err = 0; |
| 2938 | } |
| 2939 | else if (err == 0) |
| 2940 | JUMPTO(oparg); |
| 2941 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2942 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2944 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2945 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2946 | TARGET(JUMP_IF_TRUE_OR_POP) { |
| 2947 | PyObject *cond = TOP(); |
| 2948 | int err; |
| 2949 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2950 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2951 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | FAST_DISPATCH(); |
| 2953 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2954 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | JUMPTO(oparg); |
| 2956 | FAST_DISPATCH(); |
| 2957 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2958 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | if (err > 0) { |
| 2960 | err = 0; |
| 2961 | JUMPTO(oparg); |
| 2962 | } |
| 2963 | else if (err == 0) { |
| 2964 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2965 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | } |
| 2967 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2968 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2970 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2971 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2972 | PREDICTED(JUMP_ABSOLUTE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2973 | TARGET(JUMP_ABSOLUTE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2975 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2977 | the per-loop checks for signals. By default, this should be turned-off |
| 2978 | because it prevents detection of a control-break in tight loops like |
| 2979 | "while 1: pass". Compile with this option turned-on when you need |
| 2980 | the speed-up and do not need break checking inside tight loops (ones |
| 2981 | that contain only instructions ending with FAST_DISPATCH). |
| 2982 | */ |
| 2983 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2984 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2985 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2986 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2987 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2988 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2989 | TARGET(GET_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2991 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2992 | PyObject *iter = PyObject_GetIter(iterable); |
| 2993 | Py_DECREF(iterable); |
| 2994 | SET_TOP(iter); |
| 2995 | if (iter == NULL) |
| 2996 | goto error; |
| 2997 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2998 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2999 | DISPATCH(); |
| 3000 | } |
| 3001 | |
| 3002 | TARGET(GET_YIELD_FROM_ITER) { |
| 3003 | /* before: [obj]; after [getiter(obj)] */ |
| 3004 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3005 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3006 | if (PyCoro_CheckExact(iterable)) { |
| 3007 | /* `iterable` is a coroutine */ |
| 3008 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3009 | /* and it is used in a 'yield from' expression of a |
| 3010 | regular generator. */ |
| 3011 | Py_DECREF(iterable); |
| 3012 | SET_TOP(NULL); |
| 3013 | PyErr_SetString(PyExc_TypeError, |
| 3014 | "cannot 'yield from' a coroutine object " |
| 3015 | "in a non-coroutine generator"); |
| 3016 | goto error; |
| 3017 | } |
| 3018 | } |
| 3019 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3020 | /* `iterable` is not a generator. */ |
| 3021 | iter = PyObject_GetIter(iterable); |
| 3022 | Py_DECREF(iterable); |
| 3023 | SET_TOP(iter); |
| 3024 | if (iter == NULL) |
| 3025 | goto error; |
| 3026 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3027 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3028 | DISPATCH(); |
| 3029 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3030 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3031 | PREDICTED(FOR_ITER); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3032 | TARGET(FOR_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3034 | PyObject *iter = TOP(); |
| 3035 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3036 | if (next != NULL) { |
| 3037 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | PREDICT(STORE_FAST); |
| 3039 | PREDICT(UNPACK_SEQUENCE); |
| 3040 | DISPATCH(); |
| 3041 | } |
| 3042 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3043 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3044 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 3045 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3046 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | PyErr_Clear(); |
| 3048 | } |
| 3049 | /* iterator ended normally */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3050 | STACKADJ(-1); |
| 3051 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3053 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3055 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3056 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3057 | TARGET(BREAK_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | why = WHY_BREAK; |
| 3059 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3060 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3061 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3062 | TARGET(CONTINUE_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | retval = PyLong_FromLong(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3064 | if (retval == NULL) |
| 3065 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | why = WHY_CONTINUE; |
| 3067 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3068 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3069 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3070 | TARGET(SETUP_LOOP) |
| 3071 | TARGET(SETUP_EXCEPT) |
| 3072 | TARGET(SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | /* NOTE: If you add any new block-setup opcodes that |
| 3074 | are not try/except/finally handlers, you may need |
| 3075 | to update the PyGen_NeedsFinalizing() function. |
| 3076 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 3079 | STACK_LEVEL()); |
| 3080 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3081 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3082 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3083 | TARGET(BEFORE_ASYNC_WITH) { |
| 3084 | _Py_IDENTIFIER(__aexit__); |
| 3085 | _Py_IDENTIFIER(__aenter__); |
| 3086 | |
| 3087 | PyObject *mgr = TOP(); |
| 3088 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 3089 | *enter; |
| 3090 | PyObject *res; |
| 3091 | if (exit == NULL) |
| 3092 | goto error; |
| 3093 | SET_TOP(exit); |
| 3094 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3095 | Py_DECREF(mgr); |
| 3096 | if (enter == NULL) |
| 3097 | goto error; |
| 3098 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3099 | Py_DECREF(enter); |
| 3100 | if (res == NULL) |
| 3101 | goto error; |
| 3102 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3103 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3104 | DISPATCH(); |
| 3105 | } |
| 3106 | |
| 3107 | TARGET(SETUP_ASYNC_WITH) { |
| 3108 | PyObject *res = POP(); |
| 3109 | /* Setup the finally block before pushing the result |
| 3110 | of __aenter__ on the stack. */ |
| 3111 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3112 | STACK_LEVEL()); |
| 3113 | PUSH(res); |
| 3114 | DISPATCH(); |
| 3115 | } |
| 3116 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3117 | TARGET(SETUP_WITH) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3118 | _Py_IDENTIFIER(__exit__); |
| 3119 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3120 | PyObject *mgr = TOP(); |
| 3121 | PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter; |
| 3122 | PyObject *res; |
| 3123 | if (exit == NULL) |
| 3124 | goto error; |
| 3125 | SET_TOP(exit); |
| 3126 | enter = special_lookup(mgr, &PyId___enter__); |
| 3127 | Py_DECREF(mgr); |
| 3128 | if (enter == NULL) |
| 3129 | goto error; |
| 3130 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
| 3131 | Py_DECREF(enter); |
| 3132 | if (res == NULL) |
| 3133 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | /* Setup the finally block before pushing the result |
| 3135 | of __enter__ on the stack. */ |
| 3136 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3137 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3138 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3139 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | DISPATCH(); |
| 3141 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3142 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3143 | TARGET(WITH_CLEANUP_START) { |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3144 | /* At the top of the stack are 1-6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | how/why we entered the finally clause: |
| 3146 | - TOP = None |
| 3147 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 3148 | - TOP = WHY_*; no retval below it |
| 3149 | - (TOP, SECOND, THIRD) = exc_info() |
| 3150 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3151 | Below them is EXIT, the context.__exit__ bound method. |
| 3152 | In the last case, we must call |
| 3153 | EXIT(TOP, SECOND, THIRD) |
| 3154 | otherwise we must call |
| 3155 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3156 | |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3157 | In the first three cases, we remove EXIT from the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | stack, leaving the rest in the same order. In the |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3159 | fourth case, we shift the bottom 3 values of the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | In addition, if the stack represents an exception, |
| 3163 | *and* the function call returns a 'true' value, we |
| 3164 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 3165 | then not re-raise the exception. (But non-local |
| 3166 | gotos should still be resumed.) |
| 3167 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | PyObject *exit_func; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3170 | PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3171 | if (exc == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | (void)POP(); |
| 3173 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3174 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3176 | else if (PyLong_Check(exc)) { |
| 3177 | STACKADJ(-1); |
| 3178 | switch (PyLong_AsLong(exc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3179 | case WHY_RETURN: |
| 3180 | case WHY_CONTINUE: |
| 3181 | /* Retval in TOP. */ |
| 3182 | exit_func = SECOND(); |
| 3183 | SET_SECOND(TOP()); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3184 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | break; |
| 3186 | default: |
| 3187 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3188 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | break; |
| 3190 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3191 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | } |
| 3193 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3194 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3196 | val = SECOND(); |
| 3197 | tb = THIRD(); |
| 3198 | tp2 = FOURTH(); |
| 3199 | exc2 = PEEK(5); |
| 3200 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3201 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3202 | SET_VALUE(7, tb2); |
| 3203 | SET_VALUE(6, exc2); |
| 3204 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3206 | SET_FOURTH(NULL); |
| 3207 | /* We just shifted the stack down, so we have |
| 3208 | to tell the except handler block that the |
| 3209 | values are lower than it expects. */ |
| 3210 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3211 | assert(block->b_type == EXCEPT_HANDLER); |
| 3212 | block->b_level--; |
| 3213 | } |
| 3214 | /* XXX Not the fastest way to call it... */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3215 | res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3217 | if (res == NULL) |
| 3218 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3219 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3220 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3221 | PUSH(exc); |
| 3222 | PUSH(res); |
| 3223 | PREDICT(WITH_CLEANUP_FINISH); |
| 3224 | DISPATCH(); |
| 3225 | } |
| 3226 | |
| 3227 | PREDICTED(WITH_CLEANUP_FINISH); |
| 3228 | TARGET(WITH_CLEANUP_FINISH) { |
| 3229 | PyObject *res = POP(); |
| 3230 | PyObject *exc = POP(); |
| 3231 | int err; |
| 3232 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3233 | if (exc != Py_None) |
| 3234 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | else |
| 3236 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3237 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3238 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3239 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3241 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3242 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | else if (err > 0) { |
| 3244 | err = 0; |
| 3245 | /* There was an exception and a True return */ |
| 3246 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 3247 | } |
| 3248 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3249 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3251 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3252 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3253 | TARGET(CALL_FUNCTION) { |
| 3254 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | PCALL(PCALL_ALL); |
| 3256 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3257 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3258 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3259 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3260 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3261 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3262 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3263 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3265 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3266 | TARGET(CALL_FUNCTION_KW) { |
| 3267 | PyObject **sp, *res, *names; |
| 3268 | |
| 3269 | names = POP(); |
| 3270 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | PCALL(PCALL_ALL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3272 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3273 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3275 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3276 | Py_DECREF(names); |
| 3277 | |
| 3278 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3279 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3280 | } |
| 3281 | DISPATCH(); |
| 3282 | } |
| 3283 | |
| 3284 | TARGET(CALL_FUNCTION_EX) { |
| 3285 | PyObject *func, *callargs, *kwargs = NULL, *result; |
| 3286 | PCALL(PCALL_ALL); |
| 3287 | if (oparg & 0x01) { |
| 3288 | kwargs = POP(); |
| 3289 | assert(PyDict_CheckExact(kwargs)); |
| 3290 | } |
| 3291 | callargs = POP(); |
| 3292 | assert(PyTuple_CheckExact(callargs)); |
| 3293 | func = TOP(); |
| 3294 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3295 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3296 | Py_DECREF(func); |
| 3297 | Py_DECREF(callargs); |
| 3298 | Py_XDECREF(kwargs); |
| 3299 | |
| 3300 | SET_TOP(result); |
| 3301 | if (result == NULL) { |
| 3302 | goto error; |
| 3303 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3304 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3306 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3307 | TARGET(MAKE_FUNCTION) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3308 | PyObject *qualname = POP(); |
| 3309 | PyObject *codeobj = POP(); |
| 3310 | PyFunctionObject *func = (PyFunctionObject *) |
| 3311 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3312 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3313 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3314 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3315 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3316 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3318 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3319 | if (oparg & 0x08) { |
| 3320 | assert(PyTuple_CheckExact(TOP())); |
| 3321 | func ->func_closure = POP(); |
| 3322 | } |
| 3323 | if (oparg & 0x04) { |
| 3324 | assert(PyDict_CheckExact(TOP())); |
| 3325 | func->func_annotations = POP(); |
| 3326 | } |
| 3327 | if (oparg & 0x02) { |
| 3328 | assert(PyDict_CheckExact(TOP())); |
| 3329 | func->func_kwdefaults = POP(); |
| 3330 | } |
| 3331 | if (oparg & 0x01) { |
| 3332 | assert(PyTuple_CheckExact(TOP())); |
| 3333 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3335 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3336 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3337 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3338 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3339 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3340 | TARGET(BUILD_SLICE) { |
| 3341 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3343 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3345 | step = NULL; |
| 3346 | stop = POP(); |
| 3347 | start = TOP(); |
| 3348 | slice = PySlice_New(start, stop, step); |
| 3349 | Py_DECREF(start); |
| 3350 | Py_DECREF(stop); |
| 3351 | Py_XDECREF(step); |
| 3352 | SET_TOP(slice); |
| 3353 | if (slice == NULL) |
| 3354 | goto error; |
| 3355 | DISPATCH(); |
| 3356 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3357 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3358 | TARGET(FORMAT_VALUE) { |
| 3359 | /* Handles f-string value formatting. */ |
| 3360 | PyObject *result; |
| 3361 | PyObject *fmt_spec; |
| 3362 | PyObject *value; |
| 3363 | PyObject *(*conv_fn)(PyObject *); |
| 3364 | int which_conversion = oparg & FVC_MASK; |
| 3365 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3366 | |
| 3367 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3368 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3369 | |
| 3370 | /* See if any conversion is specified. */ |
| 3371 | switch (which_conversion) { |
| 3372 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3373 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3374 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3375 | |
| 3376 | /* Must be 0 (meaning no conversion), since only four |
| 3377 | values are allowed by (oparg & FVC_MASK). */ |
| 3378 | default: conv_fn = NULL; break; |
| 3379 | } |
| 3380 | |
| 3381 | /* If there's a conversion function, call it and replace |
| 3382 | value with that result. Otherwise, just use value, |
| 3383 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3384 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3385 | result = conv_fn(value); |
| 3386 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3387 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3388 | Py_XDECREF(fmt_spec); |
| 3389 | goto error; |
| 3390 | } |
| 3391 | value = result; |
| 3392 | } |
| 3393 | |
| 3394 | /* If value is a unicode object, and there's no fmt_spec, |
| 3395 | then we know the result of format(value) is value |
| 3396 | itself. In that case, skip calling format(). I plan to |
| 3397 | move this optimization in to PyObject_Format() |
| 3398 | itself. */ |
| 3399 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3400 | /* Do nothing, just transfer ownership to result. */ |
| 3401 | result = value; |
| 3402 | } else { |
| 3403 | /* Actually call format(). */ |
| 3404 | result = PyObject_Format(value, fmt_spec); |
| 3405 | Py_DECREF(value); |
| 3406 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3407 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3408 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3409 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3410 | } |
| 3411 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3412 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3413 | DISPATCH(); |
| 3414 | } |
| 3415 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3416 | TARGET(EXTENDED_ARG) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3417 | int oldoparg = oparg; |
| 3418 | NEXTOPARG(); |
| 3419 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3421 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3422 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3423 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3424 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3425 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3426 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | default: |
| 3428 | fprintf(stderr, |
| 3429 | "XXX lineno: %d, opcode: %d\n", |
| 3430 | PyFrame_GetLineNumber(f), |
| 3431 | opcode); |
| 3432 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3433 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3434 | |
| 3435 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3437 | #endif |
| 3438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3440 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3441 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3442 | or goto error. */ |
| 3443 | assert(0); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3444 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3445 | error: |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3446 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3447 | assert(why == WHY_NOT); |
| 3448 | why = WHY_EXCEPTION; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3449 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3450 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3451 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3452 | if (!PyErr_Occurred()) |
| 3453 | PyErr_SetString(PyExc_SystemError, |
| 3454 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3455 | #else |
| 3456 | assert(PyErr_Occurred()); |
| 3457 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3458 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3459 | /* Log traceback info. */ |
| 3460 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3461 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3462 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3463 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3464 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3465 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3466 | fast_block_end: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3467 | assert(why != WHY_NOT); |
| 3468 | |
| 3469 | /* Unwind stacks if a (pseudo) exception occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3471 | /* Peek at the current block. */ |
| 3472 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3474 | assert(why != WHY_YIELD); |
| 3475 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3476 | why = WHY_NOT; |
| 3477 | JUMPTO(PyLong_AS_LONG(retval)); |
| 3478 | Py_DECREF(retval); |
| 3479 | break; |
| 3480 | } |
| 3481 | /* Now we have to pop the block. */ |
| 3482 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | if (b->b_type == EXCEPT_HANDLER) { |
| 3485 | UNWIND_EXCEPT_HANDLER(b); |
| 3486 | continue; |
| 3487 | } |
| 3488 | UNWIND_BLOCK(b); |
| 3489 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3490 | why = WHY_NOT; |
| 3491 | JUMPTO(b->b_handler); |
| 3492 | break; |
| 3493 | } |
| 3494 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 3495 | || b->b_type == SETUP_FINALLY)) { |
| 3496 | PyObject *exc, *val, *tb; |
| 3497 | int handler = b->b_handler; |
| 3498 | /* Beware, this invalidates all b->b_* fields */ |
| 3499 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 3500 | PUSH(tstate->exc_traceback); |
| 3501 | PUSH(tstate->exc_value); |
| 3502 | if (tstate->exc_type != NULL) { |
| 3503 | PUSH(tstate->exc_type); |
| 3504 | } |
| 3505 | else { |
| 3506 | Py_INCREF(Py_None); |
| 3507 | PUSH(Py_None); |
| 3508 | } |
| 3509 | PyErr_Fetch(&exc, &val, &tb); |
| 3510 | /* Make the raw exception data |
| 3511 | available to the handler, |
| 3512 | so a program can emulate the |
| 3513 | Python main loop. */ |
| 3514 | PyErr_NormalizeException( |
| 3515 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3516 | if (tb != NULL) |
| 3517 | PyException_SetTraceback(val, tb); |
| 3518 | else |
| 3519 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3520 | Py_INCREF(exc); |
| 3521 | tstate->exc_type = exc; |
| 3522 | Py_INCREF(val); |
| 3523 | tstate->exc_value = val; |
| 3524 | tstate->exc_traceback = tb; |
| 3525 | if (tb == NULL) |
| 3526 | tb = Py_None; |
| 3527 | Py_INCREF(tb); |
| 3528 | PUSH(tb); |
| 3529 | PUSH(val); |
| 3530 | PUSH(exc); |
| 3531 | why = WHY_NOT; |
| 3532 | JUMPTO(handler); |
| 3533 | break; |
| 3534 | } |
| 3535 | if (b->b_type == SETUP_FINALLY) { |
| 3536 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3537 | PUSH(retval); |
| 3538 | PUSH(PyLong_FromLong((long)why)); |
| 3539 | why = WHY_NOT; |
| 3540 | JUMPTO(b->b_handler); |
| 3541 | break; |
| 3542 | } |
| 3543 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | if (why != WHY_NOT) |
| 3548 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3549 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3550 | assert(!PyErr_Occurred()); |
| 3551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | assert(why != WHY_YIELD); |
| 3555 | /* Pop remaining stack entries. */ |
| 3556 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3557 | PyObject *o = POP(); |
| 3558 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3559 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | if (why != WHY_RETURN) |
| 3562 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3563 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 3564 | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3565 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3566 | fast_yield: |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3567 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 3568 | |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3569 | /* The purpose of this block is to put aside the generator's exception |
| 3570 | state and restore that of the calling frame. If the current |
| 3571 | exception state is from the caller, we clear the exception values |
| 3572 | on the generator frame, so they are not swapped back in latter. The |
| 3573 | origin of the current exception state is determined by checking for |
| 3574 | except handler blocks, which we must be in iff a new exception |
| 3575 | state came into existence in this frame. (An uncaught exception |
| 3576 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 3577 | int i; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3578 | for (i = 0; i < f->f_iblock; i++) { |
| 3579 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3580 | break; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3581 | } |
| 3582 | } |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3583 | if (i == f->f_iblock) |
| 3584 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3585 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3586 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3587 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3588 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 3589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3591 | if (tstate->c_tracefunc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | if (why == WHY_RETURN || why == WHY_YIELD) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3593 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3594 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3596 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3597 | why = WHY_EXCEPTION; |
| 3598 | } |
| 3599 | } |
| 3600 | else if (why == WHY_EXCEPTION) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3601 | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3602 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | PyTrace_RETURN, NULL); |
| 3604 | } |
| 3605 | } |
| 3606 | if (tstate->c_profilefunc) { |
| 3607 | if (why == WHY_EXCEPTION) |
| 3608 | call_trace_protected(tstate->c_profilefunc, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3609 | tstate->c_profileobj, |
| 3610 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3611 | PyTrace_RETURN, NULL); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3612 | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
| 3613 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3614 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3615 | Py_CLEAR(retval); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 3616 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3622 | exit_eval_frame: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3623 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3624 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3626 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3627 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3628 | } |
| 3629 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3630 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3631 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3632 | { |
| 3633 | int err; |
| 3634 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3635 | PyObject *name_str, *comma, *tail, *tmp; |
| 3636 | |
| 3637 | assert(PyList_CheckExact(names)); |
| 3638 | assert(len >= 1); |
| 3639 | /* Deal with the joys of natural language. */ |
| 3640 | switch (len) { |
| 3641 | case 1: |
| 3642 | name_str = PyList_GET_ITEM(names, 0); |
| 3643 | Py_INCREF(name_str); |
| 3644 | break; |
| 3645 | case 2: |
| 3646 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3647 | PyList_GET_ITEM(names, len - 2), |
| 3648 | PyList_GET_ITEM(names, len - 1)); |
| 3649 | break; |
| 3650 | default: |
| 3651 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3652 | PyList_GET_ITEM(names, len - 2), |
| 3653 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3654 | if (tail == NULL) |
| 3655 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3656 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3657 | fail, but we can't be too careful. */ |
| 3658 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3659 | if (err == -1) { |
| 3660 | Py_DECREF(tail); |
| 3661 | return; |
| 3662 | } |
| 3663 | /* Stitch everything up into a nice comma-separated list. */ |
| 3664 | comma = PyUnicode_FromString(", "); |
| 3665 | if (comma == NULL) { |
| 3666 | Py_DECREF(tail); |
| 3667 | return; |
| 3668 | } |
| 3669 | tmp = PyUnicode_Join(comma, names); |
| 3670 | Py_DECREF(comma); |
| 3671 | if (tmp == NULL) { |
| 3672 | Py_DECREF(tail); |
| 3673 | return; |
| 3674 | } |
| 3675 | name_str = PyUnicode_Concat(tmp, tail); |
| 3676 | Py_DECREF(tmp); |
| 3677 | Py_DECREF(tail); |
| 3678 | break; |
| 3679 | } |
| 3680 | if (name_str == NULL) |
| 3681 | return; |
| 3682 | PyErr_Format(PyExc_TypeError, |
| 3683 | "%U() missing %i required %s argument%s: %U", |
| 3684 | co->co_name, |
| 3685 | len, |
| 3686 | kind, |
| 3687 | len == 1 ? "" : "s", |
| 3688 | name_str); |
| 3689 | Py_DECREF(name_str); |
| 3690 | } |
| 3691 | |
| 3692 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3693 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3694 | PyObject **fastlocals) |
| 3695 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3696 | Py_ssize_t i, j = 0; |
| 3697 | Py_ssize_t start, end; |
| 3698 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3699 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3700 | PyObject *missing_names; |
| 3701 | |
| 3702 | /* Compute the names of the arguments that are missing. */ |
| 3703 | missing_names = PyList_New(missing); |
| 3704 | if (missing_names == NULL) |
| 3705 | return; |
| 3706 | if (positional) { |
| 3707 | start = 0; |
| 3708 | end = co->co_argcount - defcount; |
| 3709 | } |
| 3710 | else { |
| 3711 | start = co->co_argcount; |
| 3712 | end = start + co->co_kwonlyargcount; |
| 3713 | } |
| 3714 | for (i = start; i < end; i++) { |
| 3715 | if (GETLOCAL(i) == NULL) { |
| 3716 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3717 | PyObject *name = PyObject_Repr(raw); |
| 3718 | if (name == NULL) { |
| 3719 | Py_DECREF(missing_names); |
| 3720 | return; |
| 3721 | } |
| 3722 | PyList_SET_ITEM(missing_names, j++, name); |
| 3723 | } |
| 3724 | } |
| 3725 | assert(j == missing); |
| 3726 | format_missing(kind, co, missing_names); |
| 3727 | Py_DECREF(missing_names); |
| 3728 | } |
| 3729 | |
| 3730 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3731 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3732 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3733 | { |
| 3734 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3735 | Py_ssize_t kwonly_given = 0; |
| 3736 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3737 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3738 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3739 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3740 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3741 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3742 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3743 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3744 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3745 | } |
| 3746 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3747 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3748 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3749 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3750 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3751 | } |
| 3752 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3753 | plural = (co_argcount != 1); |
| 3754 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3755 | } |
| 3756 | if (sig == NULL) |
| 3757 | return; |
| 3758 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3759 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3760 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3761 | given != 1 ? "s" : "", |
| 3762 | kwonly_given, |
| 3763 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3764 | if (kwonly_sig == NULL) { |
| 3765 | Py_DECREF(sig); |
| 3766 | return; |
| 3767 | } |
| 3768 | } |
| 3769 | else { |
| 3770 | /* This will not fail. */ |
| 3771 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3772 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3773 | } |
| 3774 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3775 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3776 | co->co_name, |
| 3777 | sig, |
| 3778 | plural ? "s" : "", |
| 3779 | given, |
| 3780 | kwonly_sig, |
| 3781 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3782 | Py_DECREF(sig); |
| 3783 | Py_DECREF(kwonly_sig); |
| 3784 | } |
| 3785 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3786 | /* 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] | 3787 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3788 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3789 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3790 | static PyObject * |
| 3791 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3792 | PyObject **args, Py_ssize_t argcount, |
| 3793 | PyObject **kws, Py_ssize_t kwcount, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3794 | PyObject *kwnames, PyObject **kwstack, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3795 | PyObject **defs, Py_ssize_t defcount, |
| 3796 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3797 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3798 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3799 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3800 | PyFrameObject *f; |
| 3801 | PyObject *retval = NULL; |
| 3802 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3803 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3804 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3805 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3806 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3807 | PyObject *kwdict; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3808 | Py_ssize_t kwcount2 = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3809 | |
| 3810 | assert((kwcount == 0) || (kws != NULL)); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3812 | if (globals == NULL) { |
| 3813 | PyErr_SetString(PyExc_SystemError, |
| 3814 | "PyEval_EvalCodeEx: NULL globals"); |
| 3815 | return NULL; |
| 3816 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3817 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3818 | /* Create the frame */ |
| 3819 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | assert(tstate != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3821 | f = PyFrame_New(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3822 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3824 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | fastlocals = f->f_localsplus; |
| 3826 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3827 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3828 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3829 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3830 | kwdict = PyDict_New(); |
| 3831 | if (kwdict == NULL) |
| 3832 | goto fail; |
| 3833 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3834 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3835 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3836 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3837 | SETLOCAL(i, kwdict); |
| 3838 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3839 | else { |
| 3840 | kwdict = NULL; |
| 3841 | } |
| 3842 | |
| 3843 | /* Copy positional arguments into local variables */ |
| 3844 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3845 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3846 | } |
| 3847 | else { |
| 3848 | n = argcount; |
| 3849 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3850 | for (i = 0; i < n; i++) { |
| 3851 | x = args[i]; |
| 3852 | Py_INCREF(x); |
| 3853 | SETLOCAL(i, x); |
| 3854 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3855 | |
| 3856 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3857 | if (co->co_flags & CO_VARARGS) { |
| 3858 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3859 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3860 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3861 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3862 | SETLOCAL(total_args, u); |
| 3863 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3864 | x = args[i]; |
| 3865 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3866 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3868 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3869 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3870 | /* Handle keyword arguments passed as an array of (key, value) pairs */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3871 | for (i = 0; i < kwcount; i++) { |
| 3872 | PyObject **co_varnames; |
| 3873 | PyObject *keyword = kws[2*i]; |
| 3874 | PyObject *value = kws[2*i + 1]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3875 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3876 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3877 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3878 | PyErr_Format(PyExc_TypeError, |
| 3879 | "%U() keywords must be strings", |
| 3880 | co->co_name); |
| 3881 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3883 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3884 | /* Speed hack: do raw pointer compares. As names are |
| 3885 | normally interned this should almost always hit. */ |
| 3886 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3887 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3888 | PyObject *name = co_varnames[j]; |
| 3889 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3890 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3891 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3892 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3893 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3894 | /* Slow fallback, just in case */ |
| 3895 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3896 | PyObject *name = co_varnames[j]; |
| 3897 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 3898 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3899 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3900 | } |
| 3901 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3902 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [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 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3906 | if (j >= total_args && kwdict == NULL) { |
| 3907 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3908 | "%U() got an unexpected keyword argument '%S'", |
| 3909 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3910 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3911 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3912 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 3913 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3914 | goto fail; |
| 3915 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3916 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3917 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3918 | kw_found: |
| 3919 | if (GETLOCAL(j) != NULL) { |
| 3920 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3921 | "%U() got multiple values for argument '%S'", |
| 3922 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3923 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3924 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3925 | Py_INCREF(value); |
| 3926 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3927 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3928 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3929 | /* Handle keyword arguments passed as keys tuple + values array */ |
| 3930 | for (i = 0; i < kwcount2; i++) { |
| 3931 | PyObject **co_varnames; |
| 3932 | PyObject *keyword = PyTuple_GET_ITEM(kwnames, i); |
| 3933 | PyObject *value = kwstack[i]; |
| 3934 | int j; |
| 3935 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3936 | PyErr_Format(PyExc_TypeError, |
| 3937 | "%U() keywords must be strings", |
| 3938 | co->co_name); |
| 3939 | goto fail; |
| 3940 | } |
| 3941 | /* Speed hack: do raw pointer compares. As names are |
| 3942 | normally interned this should almost always hit. */ |
| 3943 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3944 | for (j = 0; j < total_args; j++) { |
| 3945 | PyObject *nm = co_varnames[j]; |
| 3946 | if (nm == keyword) |
| 3947 | goto kw_found2; |
| 3948 | } |
| 3949 | /* Slow fallback, just in case */ |
| 3950 | for (j = 0; j < total_args; j++) { |
| 3951 | PyObject *nm = co_varnames[j]; |
| 3952 | int cmp = PyObject_RichCompareBool( |
| 3953 | keyword, nm, Py_EQ); |
| 3954 | if (cmp > 0) |
| 3955 | goto kw_found2; |
| 3956 | else if (cmp < 0) |
| 3957 | goto fail; |
| 3958 | } |
| 3959 | if (j >= total_args && kwdict == NULL) { |
| 3960 | PyErr_Format(PyExc_TypeError, |
| 3961 | "%U() got an unexpected " |
| 3962 | "keyword argument '%S'", |
| 3963 | co->co_name, |
| 3964 | keyword); |
| 3965 | goto fail; |
| 3966 | } |
| 3967 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3968 | goto fail; |
| 3969 | } |
| 3970 | continue; |
| 3971 | kw_found2: |
| 3972 | if (GETLOCAL(j) != NULL) { |
| 3973 | PyErr_Format(PyExc_TypeError, |
| 3974 | "%U() got multiple " |
| 3975 | "values for argument '%S'", |
| 3976 | co->co_name, |
| 3977 | keyword); |
| 3978 | goto fail; |
| 3979 | } |
| 3980 | Py_INCREF(value); |
| 3981 | SETLOCAL(j, value); |
| 3982 | } |
| 3983 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3984 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3985 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3986 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3987 | goto fail; |
| 3988 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3989 | |
| 3990 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3991 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3992 | Py_ssize_t m = co->co_argcount - defcount; |
| 3993 | Py_ssize_t missing = 0; |
| 3994 | for (i = argcount; i < m; i++) { |
| 3995 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3996 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3997 | } |
| 3998 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3999 | if (missing) { |
| 4000 | missing_arguments(co, missing, defcount, fastlocals); |
| 4001 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4002 | } |
| 4003 | if (n > m) |
| 4004 | i = n - m; |
| 4005 | else |
| 4006 | i = 0; |
| 4007 | for (; i < defcount; i++) { |
| 4008 | if (GETLOCAL(m+i) == NULL) { |
| 4009 | PyObject *def = defs[i]; |
| 4010 | Py_INCREF(def); |
| 4011 | SETLOCAL(m+i, def); |
| 4012 | } |
| 4013 | } |
| 4014 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4015 | |
| 4016 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4017 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4018 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4019 | for (i = co->co_argcount; i < total_args; i++) { |
| 4020 | PyObject *name; |
| 4021 | if (GETLOCAL(i) != NULL) |
| 4022 | continue; |
| 4023 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4024 | if (kwdefs != NULL) { |
| 4025 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 4026 | if (def) { |
| 4027 | Py_INCREF(def); |
| 4028 | SETLOCAL(i, def); |
| 4029 | continue; |
| 4030 | } |
| 4031 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4032 | missing++; |
| 4033 | } |
| 4034 | if (missing) { |
| 4035 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4036 | goto fail; |
| 4037 | } |
| 4038 | } |
| 4039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4040 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4041 | vars into frame. */ |
| 4042 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | PyObject *c; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4044 | int arg; |
| 4045 | /* Possibly account for the cell variable being an argument. */ |
| 4046 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4047 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4048 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4049 | /* Clear the local copy. */ |
| 4050 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4051 | } |
| 4052 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4053 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4054 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4055 | if (c == NULL) |
| 4056 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4057 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4059 | |
| 4060 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4061 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4062 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4063 | Py_INCREF(o); |
| 4064 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4066 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4067 | /* Handle generator/coroutine/asynchronous generator */ |
| 4068 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4069 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4070 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4071 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4072 | |
| 4073 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 4074 | assert(coro_wrapper != NULL); |
| 4075 | PyErr_Format(PyExc_RuntimeError, |
| 4076 | "coroutine wrapper %.200R attempted " |
| 4077 | "to recursively wrap %.200R", |
| 4078 | coro_wrapper, |
| 4079 | co); |
| 4080 | goto fail; |
| 4081 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4083 | /* Don't need to keep the reference to f_back, it will be set |
| 4084 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4085 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | PCALL(PCALL_GENERATOR); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | /* Create a new generator that owns the ready to run frame |
| 4090 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4091 | if (is_coro) { |
| 4092 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4093 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4094 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4095 | } else { |
| 4096 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4097 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4098 | if (gen == NULL) |
| 4099 | return NULL; |
| 4100 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4101 | if (is_coro && coro_wrapper != NULL) { |
| 4102 | PyObject *wrapped; |
| 4103 | tstate->in_coroutine_wrapper = 1; |
| 4104 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 4105 | tstate->in_coroutine_wrapper = 0; |
| 4106 | return wrapped; |
| 4107 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4108 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4109 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4110 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4112 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4113 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4114 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4117 | which can call back into Python. While we're done with the |
| 4118 | current Python frame (f), the associated C stack is still in use, |
| 4119 | so recursion_depth must be boosted for the duration. |
| 4120 | */ |
| 4121 | assert(tstate != NULL); |
| 4122 | ++tstate->recursion_depth; |
| 4123 | Py_DECREF(f); |
| 4124 | --tstate->recursion_depth; |
| 4125 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4128 | PyObject * |
| 4129 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4130 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 4131 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
| 4132 | { |
| 4133 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4134 | args, argcount, |
| 4135 | kws, kwcount, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4136 | NULL, NULL, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4137 | defs, defcount, |
| 4138 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4139 | NULL, NULL); |
| 4140 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4141 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4142 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4143 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4145 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4146 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4148 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4149 | return NULL; |
| 4150 | } |
| 4151 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4155 | /* These 3 functions deal with the exception state of generators. */ |
| 4156 | |
| 4157 | static void |
| 4158 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4159 | { |
| 4160 | PyObject *type, *value, *traceback; |
| 4161 | Py_XINCREF(tstate->exc_type); |
| 4162 | Py_XINCREF(tstate->exc_value); |
| 4163 | Py_XINCREF(tstate->exc_traceback); |
| 4164 | type = f->f_exc_type; |
| 4165 | value = f->f_exc_value; |
| 4166 | traceback = f->f_exc_traceback; |
| 4167 | f->f_exc_type = tstate->exc_type; |
| 4168 | f->f_exc_value = tstate->exc_value; |
| 4169 | f->f_exc_traceback = tstate->exc_traceback; |
| 4170 | Py_XDECREF(type); |
| 4171 | Py_XDECREF(value); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 4172 | Py_XDECREF(traceback); |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | static void |
| 4176 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4177 | { |
| 4178 | PyObject *tmp; |
| 4179 | tmp = tstate->exc_type; |
| 4180 | tstate->exc_type = f->f_exc_type; |
| 4181 | f->f_exc_type = tmp; |
| 4182 | tmp = tstate->exc_value; |
| 4183 | tstate->exc_value = f->f_exc_value; |
| 4184 | f->f_exc_value = tmp; |
| 4185 | tmp = tstate->exc_traceback; |
| 4186 | tstate->exc_traceback = f->f_exc_traceback; |
| 4187 | f->f_exc_traceback = tmp; |
| 4188 | } |
| 4189 | |
| 4190 | static void |
| 4191 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4192 | { |
| 4193 | PyObject *type, *value, *tb; |
| 4194 | type = tstate->exc_type; |
| 4195 | value = tstate->exc_value; |
| 4196 | tb = tstate->exc_traceback; |
| 4197 | tstate->exc_type = f->f_exc_type; |
| 4198 | tstate->exc_value = f->f_exc_value; |
| 4199 | tstate->exc_traceback = f->f_exc_traceback; |
| 4200 | f->f_exc_type = NULL; |
| 4201 | f->f_exc_value = NULL; |
| 4202 | f->f_exc_traceback = NULL; |
| 4203 | Py_XDECREF(type); |
| 4204 | Py_XDECREF(value); |
| 4205 | Py_XDECREF(tb); |
| 4206 | } |
| 4207 | |
| 4208 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4209 | /* Logic for the raise statement (too complicated for inlining). |
| 4210 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4211 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4212 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | if (exc == NULL) { |
| 4217 | /* Reraise */ |
| 4218 | PyThreadState *tstate = PyThreadState_GET(); |
| 4219 | PyObject *tb; |
| 4220 | type = tstate->exc_type; |
| 4221 | value = tstate->exc_value; |
| 4222 | tb = tstate->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4223 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | PyErr_SetString(PyExc_RuntimeError, |
| 4225 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4226 | return 0; |
| 4227 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4228 | Py_XINCREF(type); |
| 4229 | Py_XINCREF(value); |
| 4230 | Py_XINCREF(tb); |
| 4231 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4232 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | /* We support the following forms of raise: |
| 4236 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4237 | raise <instance> |
| 4238 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4240 | if (PyExceptionClass_Check(exc)) { |
| 4241 | type = exc; |
| 4242 | value = PyObject_CallObject(exc, NULL); |
| 4243 | if (value == NULL) |
| 4244 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4245 | if (!PyExceptionInstance_Check(value)) { |
| 4246 | PyErr_Format(PyExc_TypeError, |
| 4247 | "calling %R should have returned an instance of " |
| 4248 | "BaseException, not %R", |
| 4249 | type, Py_TYPE(value)); |
| 4250 | goto raise_error; |
| 4251 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | } |
| 4253 | else if (PyExceptionInstance_Check(exc)) { |
| 4254 | value = exc; |
| 4255 | type = PyExceptionInstance_Class(exc); |
| 4256 | Py_INCREF(type); |
| 4257 | } |
| 4258 | else { |
| 4259 | /* Not something you can raise. You get an exception |
| 4260 | anyway, just not what you specified :-) */ |
| 4261 | Py_DECREF(exc); |
| 4262 | PyErr_SetString(PyExc_TypeError, |
| 4263 | "exceptions must derive from BaseException"); |
| 4264 | goto raise_error; |
| 4265 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | if (cause) { |
| 4268 | PyObject *fixed_cause; |
| 4269 | if (PyExceptionClass_Check(cause)) { |
| 4270 | fixed_cause = PyObject_CallObject(cause, NULL); |
| 4271 | if (fixed_cause == NULL) |
| 4272 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4273 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4275 | else if (PyExceptionInstance_Check(cause)) { |
| 4276 | fixed_cause = cause; |
| 4277 | } |
| 4278 | else if (cause == Py_None) { |
| 4279 | Py_DECREF(cause); |
| 4280 | fixed_cause = NULL; |
| 4281 | } |
| 4282 | else { |
| 4283 | PyErr_SetString(PyExc_TypeError, |
| 4284 | "exception causes must derive from " |
| 4285 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | goto raise_error; |
| 4287 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4288 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4291 | PyErr_SetObject(type, value); |
| 4292 | /* PyErr_SetObject incref's its arguments */ |
| 4293 | Py_XDECREF(value); |
| 4294 | Py_XDECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4295 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4296 | |
| 4297 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | Py_XDECREF(value); |
| 4299 | Py_XDECREF(type); |
| 4300 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4301 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4304 | /* 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] | 4305 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4306 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4307 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4308 | with a variable target. |
| 4309 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4310 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4311 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4312 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4313 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | int i = 0, j = 0; |
| 4315 | Py_ssize_t ll = 0; |
| 4316 | PyObject *it; /* iter(v) */ |
| 4317 | PyObject *w; |
| 4318 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | it = PyObject_GetIter(v); |
| 4323 | if (it == NULL) |
| 4324 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4326 | for (; i < argcnt; i++) { |
| 4327 | w = PyIter_Next(it); |
| 4328 | if (w == NULL) { |
| 4329 | /* Iterator done, via error or exhaustion. */ |
| 4330 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4331 | if (argcntafter == -1) { |
| 4332 | PyErr_Format(PyExc_ValueError, |
| 4333 | "not enough values to unpack (expected %d, got %d)", |
| 4334 | argcnt, i); |
| 4335 | } |
| 4336 | else { |
| 4337 | PyErr_Format(PyExc_ValueError, |
| 4338 | "not enough values to unpack " |
| 4339 | "(expected at least %d, got %d)", |
| 4340 | argcnt + argcntafter, i); |
| 4341 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | } |
| 4343 | goto Error; |
| 4344 | } |
| 4345 | *--sp = w; |
| 4346 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4348 | if (argcntafter == -1) { |
| 4349 | /* We better have exhausted the iterator now. */ |
| 4350 | w = PyIter_Next(it); |
| 4351 | if (w == NULL) { |
| 4352 | if (PyErr_Occurred()) |
| 4353 | goto Error; |
| 4354 | Py_DECREF(it); |
| 4355 | return 1; |
| 4356 | } |
| 4357 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4358 | PyErr_Format(PyExc_ValueError, |
| 4359 | "too many values to unpack (expected %d)", |
| 4360 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | goto Error; |
| 4362 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | l = PySequence_List(it); |
| 4365 | if (l == NULL) |
| 4366 | goto Error; |
| 4367 | *--sp = l; |
| 4368 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | ll = PyList_GET_SIZE(l); |
| 4371 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4372 | PyErr_Format(PyExc_ValueError, |
| 4373 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4374 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | goto Error; |
| 4376 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | /* Pop the "after-variable" args off the list. */ |
| 4379 | for (j = argcntafter; j > 0; j--, i++) { |
| 4380 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4381 | } |
| 4382 | /* Resize the list. */ |
| 4383 | Py_SIZE(l) = ll - argcntafter; |
| 4384 | Py_DECREF(it); |
| 4385 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4386 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4387 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | for (; i > 0; i--, sp++) |
| 4389 | Py_DECREF(*sp); |
| 4390 | Py_XDECREF(it); |
| 4391 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4395 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4396 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4397 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4399 | printf("%s ", str); |
| 4400 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4401 | PyErr_Clear(); /* Don't know what else to do */ |
| 4402 | printf("\n"); |
| 4403 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4404 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4405 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4406 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4407 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4408 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4409 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4410 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4411 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4413 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | if (value == NULL) { |
| 4415 | value = Py_None; |
| 4416 | Py_INCREF(value); |
| 4417 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4418 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4419 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4421 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4422 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | return; |
| 4424 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4425 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | Py_DECREF(arg); |
| 4427 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4428 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4429 | else { |
| 4430 | Py_XDECREF(type); |
| 4431 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4432 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4436 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4437 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4438 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | PyObject *type, *value, *traceback; |
| 4442 | int err; |
| 4443 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4444 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4445 | if (err == 0) |
| 4446 | { |
| 4447 | PyErr_Restore(type, value, traceback); |
| 4448 | return 0; |
| 4449 | } |
| 4450 | else { |
| 4451 | Py_XDECREF(type); |
| 4452 | Py_XDECREF(value); |
| 4453 | Py_XDECREF(traceback); |
| 4454 | return -1; |
| 4455 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4458 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4459 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4460 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | int result; |
| 4464 | if (tstate->tracing) |
| 4465 | return 0; |
| 4466 | tstate->tracing++; |
| 4467 | tstate->use_tracing = 0; |
| 4468 | result = func(obj, frame, what, arg); |
| 4469 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4470 | || (tstate->c_profilefunc != NULL)); |
| 4471 | tstate->tracing--; |
| 4472 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4475 | PyObject * |
| 4476 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4477 | { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4478 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | int save_tracing = tstate->tracing; |
| 4480 | int save_use_tracing = tstate->use_tracing; |
| 4481 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4483 | tstate->tracing = 0; |
| 4484 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4485 | || (tstate->c_profilefunc != NULL)); |
| 4486 | result = PyObject_Call(func, args, NULL); |
| 4487 | tstate->tracing = save_tracing; |
| 4488 | tstate->use_tracing = save_use_tracing; |
| 4489 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4492 | /* 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] | 4493 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4494 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4495 | PyThreadState *tstate, PyFrameObject *frame, |
| 4496 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | int result = 0; |
| 4499 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | /* If the last instruction executed isn't in the current |
| 4502 | instruction window, reset the window. |
| 4503 | */ |
| 4504 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4505 | PyAddrPair bounds; |
| 4506 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4507 | &bounds); |
| 4508 | *instr_lb = bounds.ap_lower; |
| 4509 | *instr_ub = bounds.ap_upper; |
| 4510 | } |
| 4511 | /* If the last instruction falls at the start of a line or if |
| 4512 | it represents a jump backwards, update the frame's line |
| 4513 | number and call the trace function. */ |
| 4514 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4515 | frame->f_lineno = line; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4516 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | } |
| 4518 | *instr_prev = frame->f_lasti; |
| 4519 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4522 | void |
| 4523 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4524 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | PyThreadState *tstate = PyThreadState_GET(); |
| 4526 | PyObject *temp = tstate->c_profileobj; |
| 4527 | Py_XINCREF(arg); |
| 4528 | tstate->c_profilefunc = NULL; |
| 4529 | tstate->c_profileobj = NULL; |
| 4530 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4531 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4532 | Py_XDECREF(temp); |
| 4533 | tstate->c_profilefunc = func; |
| 4534 | tstate->c_profileobj = arg; |
| 4535 | /* Flag that tracing or profiling is turned on */ |
| 4536 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | void |
| 4540 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4541 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | PyThreadState *tstate = PyThreadState_GET(); |
| 4543 | PyObject *temp = tstate->c_traceobj; |
| 4544 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4545 | Py_XINCREF(arg); |
| 4546 | tstate->c_tracefunc = NULL; |
| 4547 | tstate->c_traceobj = NULL; |
| 4548 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4549 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4550 | Py_XDECREF(temp); |
| 4551 | tstate->c_tracefunc = func; |
| 4552 | tstate->c_traceobj = arg; |
| 4553 | /* Flag that tracing or profiling is turned on */ |
| 4554 | tstate->use_tracing = ((func != NULL) |
| 4555 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4558 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4559 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4560 | { |
| 4561 | PyThreadState *tstate = PyThreadState_GET(); |
| 4562 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4563 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4564 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4568 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4569 | { |
| 4570 | PyThreadState *tstate = PyThreadState_GET(); |
| 4571 | return tstate->coroutine_wrapper; |
| 4572 | } |
| 4573 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4574 | void |
| 4575 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4576 | { |
| 4577 | PyThreadState *tstate = PyThreadState_GET(); |
| 4578 | |
| 4579 | Py_XINCREF(firstiter); |
| 4580 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4581 | } |
| 4582 | |
| 4583 | PyObject * |
| 4584 | _PyEval_GetAsyncGenFirstiter(void) |
| 4585 | { |
| 4586 | PyThreadState *tstate = PyThreadState_GET(); |
| 4587 | return tstate->async_gen_firstiter; |
| 4588 | } |
| 4589 | |
| 4590 | void |
| 4591 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4592 | { |
| 4593 | PyThreadState *tstate = PyThreadState_GET(); |
| 4594 | |
| 4595 | Py_XINCREF(finalizer); |
| 4596 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4597 | } |
| 4598 | |
| 4599 | PyObject * |
| 4600 | _PyEval_GetAsyncGenFinalizer(void) |
| 4601 | { |
| 4602 | PyThreadState *tstate = PyThreadState_GET(); |
| 4603 | return tstate->async_gen_finalizer; |
| 4604 | } |
| 4605 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4606 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4607 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4609 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4610 | if (current_frame == NULL) |
| 4611 | return PyThreadState_GET()->interp->builtins; |
| 4612 | else |
| 4613 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4616 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4617 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4620 | if (current_frame == NULL) { |
| 4621 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4626 | return NULL; |
| 4627 | |
| 4628 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4632 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4633 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4636 | if (current_frame == NULL) |
| 4637 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4638 | |
| 4639 | assert(current_frame->f_globals != NULL); |
| 4640 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4643 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4644 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4645 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | PyThreadState *tstate = PyThreadState_GET(); |
| 4647 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4650 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4651 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4654 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4656 | if (current_frame != NULL) { |
| 4657 | const int codeflags = current_frame->f_code->co_flags; |
| 4658 | const int compilerflags = codeflags & PyCF_MASK; |
| 4659 | if (compilerflags) { |
| 4660 | result = 1; |
| 4661 | cf->cf_flags |= compilerflags; |
| 4662 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4663 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4665 | result = 1; |
| 4666 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4667 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4668 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | } |
| 4670 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4673 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4674 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 4675 | 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] | 4676 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4677 | PyObject * |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4678 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4679 | { |
Victor Stinner | 59b356d | 2015-03-16 11:52:32 +0100 | [diff] [blame] | 4680 | #ifdef Py_DEBUG |
| 4681 | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
| 4682 | set. It raises a new exception if parameters are invalid or if |
| 4683 | PyTuple_New() fails, and so the original exception is lost. */ |
| 4684 | assert(!PyErr_Occurred()); |
| 4685 | #endif |
| 4686 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4687 | if (args == NULL) { |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4688 | return _PyObject_FastCallDict(func, NULL, 0, kwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4689 | } |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4690 | |
| 4691 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | PyErr_SetString(PyExc_TypeError, |
| 4693 | "argument list must be a tuple"); |
| 4694 | return NULL; |
| 4695 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4696 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4697 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4698 | PyErr_SetString(PyExc_TypeError, |
| 4699 | "keyword list must be a dictionary"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4700 | return NULL; |
| 4701 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4702 | |
Victor Stinner | 6e2333d | 2016-08-23 00:25:01 +0200 | [diff] [blame] | 4703 | return PyObject_Call(func, args, kwargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4706 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4707 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | if (PyMethod_Check(func)) |
| 4710 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4711 | else if (PyFunction_Check(func)) |
| 4712 | return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name); |
| 4713 | else if (PyCFunction_Check(func)) |
| 4714 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4715 | else |
| 4716 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4719 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4720 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4722 | if (PyMethod_Check(func)) |
| 4723 | return "()"; |
| 4724 | else if (PyFunction_Check(func)) |
| 4725 | return "()"; |
| 4726 | else if (PyCFunction_Check(func)) |
| 4727 | return "()"; |
| 4728 | else |
| 4729 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4732 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4733 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4734 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4735 | tstate, tstate->frame, \ |
| 4736 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 | x = NULL; \ |
| 4738 | } \ |
| 4739 | else { \ |
| 4740 | x = call; \ |
| 4741 | if (tstate->c_profilefunc != NULL) { \ |
| 4742 | if (x == NULL) { \ |
| 4743 | call_trace_protected(tstate->c_profilefunc, \ |
| 4744 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4745 | tstate, tstate->frame, \ |
| 4746 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | /* XXX should pass (type, value, tb) */ \ |
| 4748 | } else { \ |
| 4749 | if (call_trace(tstate->c_profilefunc, \ |
| 4750 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4751 | tstate, tstate->frame, \ |
| 4752 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4753 | Py_DECREF(x); \ |
| 4754 | x = NULL; \ |
| 4755 | } \ |
| 4756 | } \ |
| 4757 | } \ |
| 4758 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4759 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | x = call; \ |
| 4761 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4762 | |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4763 | static PyObject * |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4764 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4765 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4766 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | PyObject *func = *pfunc; |
| 4768 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4769 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4770 | Py_ssize_t nargs = oparg - nkwargs; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4771 | PyObject **stack; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | /* Always dispatch PyCFunction first, because these are |
| 4774 | presumed to be the most frequent callable object. |
| 4775 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4776 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4777 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | PCALL(PCALL_CFUNCTION); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4780 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4781 | stack = (*pp_stack) - nargs - nkwargs; |
| 4782 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4783 | } |
| 4784 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4785 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
| 4786 | /* optimize access to bound methods */ |
| 4787 | PyObject *self = PyMethod_GET_SELF(func); |
| 4788 | PCALL(PCALL_METHOD); |
| 4789 | PCALL(PCALL_BOUND_METHOD); |
| 4790 | Py_INCREF(self); |
| 4791 | func = PyMethod_GET_FUNCTION(func); |
| 4792 | Py_INCREF(func); |
| 4793 | Py_SETREF(*pfunc, self); |
| 4794 | nargs++; |
| 4795 | } |
| 4796 | else { |
| 4797 | Py_INCREF(func); |
| 4798 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4799 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4800 | stack = (*pp_stack) - nargs - nkwargs; |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4801 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4802 | if (PyFunction_Check(func)) { |
| 4803 | x = fast_function(func, stack, nargs, kwnames); |
| 4804 | } |
| 4805 | else { |
| 4806 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4807 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4808 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4809 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4810 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4811 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4812 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4814 | /* Clear the stack of the function object. Also removes |
| 4815 | the arguments in case they weren't consumed already |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4816 | (fast_function() and err_args() leave them on the stack). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4817 | */ |
| 4818 | while ((*pp_stack) > pfunc) { |
| 4819 | w = EXT_POP(*pp_stack); |
| 4820 | Py_DECREF(w); |
| 4821 | PCALL(PCALL_POP); |
| 4822 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4824 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4827 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4828 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4829 | For the simplest case -- a function that takes only positional |
| 4830 | arguments and is called with only positional arguments -- it |
| 4831 | inlines the most primitive frame setup code from |
| 4832 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4833 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4834 | */ |
| 4835 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4836 | static PyObject* |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4837 | _PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
| 4838 | PyObject *globals) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4839 | { |
| 4840 | PyFrameObject *f; |
| 4841 | PyThreadState *tstate = PyThreadState_GET(); |
| 4842 | PyObject **fastlocals; |
| 4843 | Py_ssize_t i; |
| 4844 | PyObject *result; |
| 4845 | |
| 4846 | PCALL(PCALL_FASTER_FUNCTION); |
| 4847 | assert(globals != NULL); |
| 4848 | /* XXX Perhaps we should create a specialized |
| 4849 | PyFrame_New() that doesn't take locals, but does |
| 4850 | take builtins without sanity checking them. |
| 4851 | */ |
| 4852 | assert(tstate != NULL); |
| 4853 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4854 | if (f == NULL) { |
| 4855 | return NULL; |
| 4856 | } |
| 4857 | |
| 4858 | fastlocals = f->f_localsplus; |
| 4859 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4860 | for (i = 0; i < nargs; i++) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4861 | Py_INCREF(*args); |
| 4862 | fastlocals[i] = *args++; |
| 4863 | } |
| 4864 | result = PyEval_EvalFrameEx(f,0); |
| 4865 | |
| 4866 | ++tstate->recursion_depth; |
| 4867 | Py_DECREF(f); |
| 4868 | --tstate->recursion_depth; |
| 4869 | |
| 4870 | return result; |
| 4871 | } |
| 4872 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4873 | static PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4874 | fast_function(PyObject *func, PyObject **stack, |
| 4875 | Py_ssize_t nargs, PyObject *kwnames) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4877 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4878 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4879 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4880 | PyObject *kwdefs, *closure, *name, *qualname; |
| 4881 | PyObject **d; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4882 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4883 | Py_ssize_t nd; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4884 | |
| 4885 | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4887 | PCALL(PCALL_FUNCTION); |
| 4888 | PCALL(PCALL_FAST_FUNCTION); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4889 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4890 | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4891 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4892 | { |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4893 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4894 | return _PyFunction_FastCall(co, stack, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4895 | } |
| 4896 | else if (nargs == 0 && argdefs != NULL |
| 4897 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4898 | /* function called with no arguments, but all parameters have |
| 4899 | a default value: use default values as arguments .*/ |
| 4900 | stack = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4901 | return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4902 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4903 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4904 | |
| 4905 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4906 | closure = PyFunction_GET_CLOSURE(func); |
| 4907 | name = ((PyFunctionObject *)func) -> func_name; |
| 4908 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | if (argdefs != NULL) { |
| 4911 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4912 | nd = Py_SIZE(argdefs); |
| 4913 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4914 | else { |
| 4915 | d = NULL; |
| 4916 | nd = 0; |
| 4917 | } |
| 4918 | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4919 | stack, nargs, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4920 | NULL, 0, |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4921 | kwnames, stack + nargs, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4922 | d, (int)nd, kwdefs, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4923 | closure, name, qualname); |
| 4924 | } |
| 4925 | |
| 4926 | PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4927 | _PyFunction_FastCallKeywords(PyObject *func, PyObject **stack, |
| 4928 | Py_ssize_t nargs, PyObject *kwnames) |
| 4929 | { |
| 4930 | return fast_function(func, stack, nargs, kwnames); |
| 4931 | } |
| 4932 | |
| 4933 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4934 | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4935 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4936 | { |
| 4937 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4938 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4939 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4940 | PyObject *kwdefs, *closure, *name, *qualname; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4941 | PyObject *kwtuple, **k; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4942 | PyObject **d; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4943 | Py_ssize_t nd, nk; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4944 | PyObject *result; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4945 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4946 | assert(func != NULL); |
| 4947 | assert(nargs >= 0); |
| 4948 | assert(nargs == 0 || args != NULL); |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4949 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4950 | |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4951 | PCALL(PCALL_FUNCTION); |
| 4952 | PCALL(PCALL_FAST_FUNCTION); |
| 4953 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4954 | if (co->co_kwonlyargcount == 0 && |
| 4955 | (kwargs == NULL || PyDict_Size(kwargs) == 0) && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4956 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4957 | { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4958 | /* Fast paths */ |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4959 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4960 | return _PyFunction_FastCall(co, args, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4961 | } |
| 4962 | else if (nargs == 0 && argdefs != NULL |
| 4963 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4964 | /* function called with no arguments, but all parameters have |
| 4965 | a default value: use default values as arguments .*/ |
| 4966 | args = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4967 | return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4968 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4969 | } |
| 4970 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4971 | if (kwargs != NULL) { |
| 4972 | Py_ssize_t pos, i; |
| 4973 | nk = PyDict_Size(kwargs); |
| 4974 | |
| 4975 | kwtuple = PyTuple_New(2 * nk); |
| 4976 | if (kwtuple == NULL) { |
| 4977 | return NULL; |
| 4978 | } |
| 4979 | |
| 4980 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 4981 | pos = i = 0; |
| 4982 | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
| 4983 | Py_INCREF(k[i]); |
| 4984 | Py_INCREF(k[i+1]); |
| 4985 | i += 2; |
| 4986 | } |
| 4987 | nk = i / 2; |
| 4988 | } |
| 4989 | else { |
| 4990 | kwtuple = NULL; |
| 4991 | k = NULL; |
| 4992 | nk = 0; |
| 4993 | } |
| 4994 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4995 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4996 | closure = PyFunction_GET_CLOSURE(func); |
| 4997 | name = ((PyFunctionObject *)func) -> func_name; |
| 4998 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4999 | |
| 5000 | if (argdefs != NULL) { |
| 5001 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 5002 | nd = Py_SIZE(argdefs); |
| 5003 | } |
| 5004 | else { |
| 5005 | d = NULL; |
| 5006 | nd = 0; |
| 5007 | } |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5008 | |
| 5009 | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
| 5010 | args, nargs, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5011 | k, nk, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5012 | NULL, NULL, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5013 | d, nd, kwdefs, |
| 5014 | closure, name, qualname); |
| 5015 | Py_XDECREF(kwtuple); |
| 5016 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5020 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5021 | { |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5022 | #ifdef CALL_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | /* At this point, we have to look at the type of func to |
| 5024 | update the call stats properly. Do it here so as to avoid |
| 5025 | exposing the call stats machinery outside ceval.c |
| 5026 | */ |
| 5027 | if (PyFunction_Check(func)) |
| 5028 | PCALL(PCALL_FUNCTION); |
| 5029 | else if (PyMethod_Check(func)) |
| 5030 | PCALL(PCALL_METHOD); |
| 5031 | else if (PyType_Check(func)) |
| 5032 | PCALL(PCALL_TYPE); |
| 5033 | else if (PyCFunction_Check(func)) |
| 5034 | PCALL(PCALL_CFUNCTION); |
| 5035 | else |
| 5036 | PCALL(PCALL_OTHER); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 5037 | #endif |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | if (PyCFunction_Check(func)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5040 | PyObject *result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | PyThreadState *tstate = PyThreadState_GET(); |
| 5042 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5043 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5045 | else { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5046 | return PyObject_Call(func, callargs, kwdict); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5047 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5050 | /* 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] | 5051 | nb_index slot defined, and store in *pi. |
| 5052 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 5053 | 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] | 5054 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5055 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 5056 | /* Note: If v is NULL, return success without storing into *pi. This |
| 5057 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 5058 | 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] | 5059 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5060 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5061 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | if (v != NULL) { |
| 5064 | Py_ssize_t x; |
| 5065 | if (PyIndex_Check(v)) { |
| 5066 | x = PyNumber_AsSsize_t(v, NULL); |
| 5067 | if (x == -1 && PyErr_Occurred()) |
| 5068 | return 0; |
| 5069 | } |
| 5070 | else { |
| 5071 | PyErr_SetString(PyExc_TypeError, |
| 5072 | "slice indices must be integers or " |
| 5073 | "None or have an __index__ method"); |
| 5074 | return 0; |
| 5075 | } |
| 5076 | *pi = x; |
| 5077 | } |
| 5078 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5081 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5083 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5084 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 5085 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | int res = 0; |
| 5088 | switch (op) { |
| 5089 | case PyCmp_IS: |
| 5090 | res = (v == w); |
| 5091 | break; |
| 5092 | case PyCmp_IS_NOT: |
| 5093 | res = (v != w); |
| 5094 | break; |
| 5095 | case PyCmp_IN: |
| 5096 | res = PySequence_Contains(w, v); |
| 5097 | if (res < 0) |
| 5098 | return NULL; |
| 5099 | break; |
| 5100 | case PyCmp_NOT_IN: |
| 5101 | res = PySequence_Contains(w, v); |
| 5102 | if (res < 0) |
| 5103 | return NULL; |
| 5104 | res = !res; |
| 5105 | break; |
| 5106 | case PyCmp_EXC_MATCH: |
| 5107 | if (PyTuple_Check(w)) { |
| 5108 | Py_ssize_t i, length; |
| 5109 | length = PyTuple_Size(w); |
| 5110 | for (i = 0; i < length; i += 1) { |
| 5111 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5112 | if (!PyExceptionClass_Check(exc)) { |
| 5113 | PyErr_SetString(PyExc_TypeError, |
| 5114 | CANNOT_CATCH_MSG); |
| 5115 | return NULL; |
| 5116 | } |
| 5117 | } |
| 5118 | } |
| 5119 | else { |
| 5120 | if (!PyExceptionClass_Check(w)) { |
| 5121 | PyErr_SetString(PyExc_TypeError, |
| 5122 | CANNOT_CATCH_MSG); |
| 5123 | return NULL; |
| 5124 | } |
| 5125 | } |
| 5126 | res = PyErr_GivenExceptionMatches(v, w); |
| 5127 | break; |
| 5128 | default: |
| 5129 | return PyObject_RichCompare(v, w, op); |
| 5130 | } |
| 5131 | v = res ? Py_True : Py_False; |
| 5132 | Py_INCREF(v); |
| 5133 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5136 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5137 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 5138 | { |
| 5139 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5140 | PyObject *import_func, *res; |
| 5141 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5142 | |
| 5143 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 5144 | if (import_func == NULL) { |
| 5145 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 5146 | return NULL; |
| 5147 | } |
| 5148 | |
| 5149 | /* Fast path for not overloaded __import__. */ |
| 5150 | if (import_func == PyThreadState_GET()->interp->import_func) { |
| 5151 | int ilevel = _PyLong_AsInt(level); |
| 5152 | if (ilevel == -1 && PyErr_Occurred()) { |
| 5153 | return NULL; |
| 5154 | } |
| 5155 | res = PyImport_ImportModuleLevelObject( |
| 5156 | name, |
| 5157 | f->f_globals, |
| 5158 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5159 | fromlist, |
| 5160 | ilevel); |
| 5161 | return res; |
| 5162 | } |
| 5163 | |
| 5164 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5165 | |
| 5166 | stack[0] = name; |
| 5167 | stack[1] = f->f_globals; |
| 5168 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5169 | stack[3] = fromlist; |
| 5170 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5171 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5172 | Py_DECREF(import_func); |
| 5173 | return res; |
| 5174 | } |
| 5175 | |
| 5176 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5177 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5180 | _Py_IDENTIFIER(__name__); |
| 5181 | PyObject *fullmodname, *pkgname; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5183 | x = PyObject_GetAttr(v, name); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5184 | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5185 | return x; |
| 5186 | /* Issue #17636: in case this failed because of a circular relative |
| 5187 | import, try to fallback on reading the module directly from |
| 5188 | sys.modules. */ |
| 5189 | PyErr_Clear(); |
| 5190 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5191 | if (pkgname == NULL) { |
| 5192 | goto error; |
| 5193 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5194 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
| 5195 | Py_DECREF(pkgname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5196 | if (fullmodname == NULL) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5197 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5198 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5199 | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5200 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5201 | if (x == NULL) { |
| 5202 | goto error; |
| 5203 | } |
| 5204 | Py_INCREF(x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5205 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5206 | error: |
| 5207 | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
| 5208 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5209 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5210 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5211 | static int |
| 5212 | import_all_from(PyObject *locals, PyObject *v) |
| 5213 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5214 | _Py_IDENTIFIER(__all__); |
| 5215 | _Py_IDENTIFIER(__dict__); |
| 5216 | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | PyObject *dict, *name, *value; |
| 5218 | int skip_leading_underscores = 0; |
| 5219 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | if (all == NULL) { |
| 5222 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5223 | return -1; /* Unexpected error */ |
| 5224 | PyErr_Clear(); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5225 | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5226 | if (dict == NULL) { |
| 5227 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5228 | return -1; |
| 5229 | PyErr_SetString(PyExc_ImportError, |
| 5230 | "from-import-* object has no __dict__ and no __all__"); |
| 5231 | return -1; |
| 5232 | } |
| 5233 | all = PyMapping_Keys(dict); |
| 5234 | Py_DECREF(dict); |
| 5235 | if (all == NULL) |
| 5236 | return -1; |
| 5237 | skip_leading_underscores = 1; |
| 5238 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5240 | for (pos = 0, err = 0; ; pos++) { |
| 5241 | name = PySequence_GetItem(all, pos); |
| 5242 | if (name == NULL) { |
| 5243 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 5244 | err = -1; |
| 5245 | else |
| 5246 | PyErr_Clear(); |
| 5247 | break; |
| 5248 | } |
| 5249 | if (skip_leading_underscores && |
| 5250 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5251 | PyUnicode_READY(name) != -1 && |
| 5252 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | { |
| 5254 | Py_DECREF(name); |
| 5255 | continue; |
| 5256 | } |
| 5257 | value = PyObject_GetAttr(v, name); |
| 5258 | if (value == NULL) |
| 5259 | err = -1; |
| 5260 | else if (PyDict_CheckExact(locals)) |
| 5261 | err = PyDict_SetItem(locals, name, value); |
| 5262 | else |
| 5263 | err = PyObject_SetItem(locals, name, value); |
| 5264 | Py_DECREF(name); |
| 5265 | Py_XDECREF(value); |
| 5266 | if (err != 0) |
| 5267 | break; |
| 5268 | } |
| 5269 | Py_DECREF(all); |
| 5270 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5273 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5274 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5276 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | if (!obj) |
| 5279 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | obj_str = _PyUnicode_AsString(obj); |
| 5282 | if (!obj_str) |
| 5283 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5286 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5287 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5288 | static void |
| 5289 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5290 | { |
| 5291 | PyObject *name; |
| 5292 | /* Don't stomp existing exception */ |
| 5293 | if (PyErr_Occurred()) |
| 5294 | return; |
| 5295 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5296 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5297 | oparg); |
| 5298 | format_exc_check_arg( |
| 5299 | PyExc_UnboundLocalError, |
| 5300 | UNBOUNDLOCAL_ERROR_MSG, |
| 5301 | name); |
| 5302 | } else { |
| 5303 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5304 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5305 | format_exc_check_arg(PyExc_NameError, |
| 5306 | UNBOUNDFREE_ERROR_MSG, name); |
| 5307 | } |
| 5308 | } |
| 5309 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5310 | static PyObject * |
| 5311 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5312 | PyFrameObject *f, const unsigned short *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5313 | { |
| 5314 | PyObject *res; |
| 5315 | if (Py_REFCNT(v) == 2) { |
| 5316 | /* In the common case, there are 2 references to the value |
| 5317 | * stored in 'variable' when the += is performed: one on the |
| 5318 | * value stack (in 'v') and one still stored in the |
| 5319 | * 'variable'. We try to delete the variable now to reduce |
| 5320 | * the refcnt to 1. |
| 5321 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5322 | int opcode, oparg; |
| 5323 | NEXTOPARG(); |
| 5324 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5325 | case STORE_FAST: |
| 5326 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5327 | PyObject **fastlocals = f->f_localsplus; |
| 5328 | if (GETLOCAL(oparg) == v) |
| 5329 | SETLOCAL(oparg, NULL); |
| 5330 | break; |
| 5331 | } |
| 5332 | case STORE_DEREF: |
| 5333 | { |
| 5334 | PyObject **freevars = (f->f_localsplus + |
| 5335 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5336 | PyObject *c = freevars[oparg]; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5337 | if (PyCell_GET(c) == v) |
| 5338 | PyCell_Set(c, NULL); |
| 5339 | break; |
| 5340 | } |
| 5341 | case STORE_NAME: |
| 5342 | { |
| 5343 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5344 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5345 | PyObject *locals = f->f_locals; |
| 5346 | if (PyDict_CheckExact(locals) && |
| 5347 | PyDict_GetItem(locals, name) == v) { |
| 5348 | if (PyDict_DelItem(locals, name) != 0) { |
| 5349 | PyErr_Clear(); |
| 5350 | } |
| 5351 | } |
| 5352 | break; |
| 5353 | } |
| 5354 | } |
| 5355 | } |
| 5356 | res = v; |
| 5357 | PyUnicode_Append(&res, w); |
| 5358 | return res; |
| 5359 | } |
| 5360 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5361 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5362 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5363 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5364 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5366 | int i; |
| 5367 | PyObject *l = PyList_New(256); |
| 5368 | if (l == NULL) return NULL; |
| 5369 | for (i = 0; i < 256; i++) { |
| 5370 | PyObject *x = PyLong_FromLong(a[i]); |
| 5371 | if (x == NULL) { |
| 5372 | Py_DECREF(l); |
| 5373 | return NULL; |
| 5374 | } |
| 5375 | PyList_SetItem(l, i, x); |
| 5376 | } |
| 5377 | for (i = 0; i < 256; i++) |
| 5378 | a[i] = 0; |
| 5379 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5383 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5384 | { |
| 5385 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5386 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5387 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5388 | int i; |
| 5389 | PyObject *l = PyList_New(257); |
| 5390 | if (l == NULL) return NULL; |
| 5391 | for (i = 0; i < 257; i++) { |
| 5392 | PyObject *x = getarray(dxpairs[i]); |
| 5393 | if (x == NULL) { |
| 5394 | Py_DECREF(l); |
| 5395 | return NULL; |
| 5396 | } |
| 5397 | PyList_SetItem(l, i, x); |
| 5398 | } |
| 5399 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5400 | #endif |
| 5401 | } |
| 5402 | |
| 5403 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5404 | |
| 5405 | Py_ssize_t |
| 5406 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5407 | { |
| 5408 | PyThreadState *tstate = PyThreadState_Get(); |
| 5409 | Py_ssize_t new_index; |
| 5410 | |
| 5411 | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
| 5412 | return -1; |
| 5413 | } |
| 5414 | new_index = tstate->co_extra_user_count++; |
| 5415 | tstate->co_extra_freefuncs[new_index] = free; |
| 5416 | return new_index; |
| 5417 | } |