Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
| 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 15 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 18 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 19 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
| 23 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 24 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 25 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 27 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 28 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | #define LLTRACE 1 /* Low-level trace feature */ |
| 30 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 33 | /* Private API for the LOAD_METHOD opcode. */ |
| 34 | extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **); |
| 35 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 36 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 38 | /* Forward declarations */ |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 39 | Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t, PyObject *); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 40 | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 41 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 43 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 44 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 45 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 46 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 47 | static int call_trace(Py_tracefunc, PyObject *, |
| 48 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 50 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 51 | PyThreadState *, PyFrameObject *, |
| 52 | int, PyObject *); |
| 53 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 54 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 55 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 56 | PyThreadState *, PyFrameObject *, int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 57 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 58 | static void dtrace_function_entry(PyFrameObject *); |
| 59 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 60 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 61 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 62 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 63 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 64 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 65 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 66 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 67 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 68 | PyFrameObject *, const _Py_CODEUNIT *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 69 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 70 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 71 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 73 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 75 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | "free variable '%.200s' referenced before assignment" \ |
| 77 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 79 | /* Dynamic execution profile */ |
| 80 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 81 | #ifdef DXPAIRS |
| 82 | static long dxpairs[257][256]; |
| 83 | #define dxp dxpairs[256] |
| 84 | #else |
| 85 | static long dxp[256]; |
| 86 | #endif |
| 87 | #endif |
| 88 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 89 | #ifdef WITH_THREAD |
| 90 | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
| 91 | #else |
| 92 | #define GIL_REQUEST 0 |
| 93 | #endif |
| 94 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 95 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 96 | 1. We believe this is all right because the eval loop will release |
| 97 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 98 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | _Py_atomic_store_relaxed( \ |
| 100 | &eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 101 | GIL_REQUEST | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
| 103 | pending_async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 104 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 105 | #ifdef WITH_THREAD |
| 106 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 107 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | do { \ |
| 109 | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
| 110 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 111 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 112 | |
| 113 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | do { \ |
| 115 | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
| 116 | COMPUTE_EVAL_BREAKER(); \ |
| 117 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 118 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 119 | #endif |
| 120 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 121 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 122 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | do { \ |
| 124 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
| 125 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 126 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 127 | |
| 128 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | do { \ |
| 130 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
| 131 | COMPUTE_EVAL_BREAKER(); \ |
| 132 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 133 | |
| 134 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | do { \ |
| 136 | pending_async_exc = 1; \ |
| 137 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 138 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 139 | |
| 140 | #define UNSIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 142 | |
| 143 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 144 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 145 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 146 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 147 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 148 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 149 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 151 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 152 | static long main_thread = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 153 | /* This single variable consolidates all requests to break out of the fast path |
| 154 | in the eval loop. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 155 | static _Py_atomic_int eval_breaker = {0}; |
| 156 | /* Request for dropping the GIL */ |
| 157 | static _Py_atomic_int gil_drop_request = {0}; |
| 158 | /* Request for running pending calls. */ |
| 159 | static _Py_atomic_int pendingcalls_to_do = {0}; |
| 160 | /* Request for looking at the `async_exc` field of the current thread state. |
| 161 | Guarded by the GIL. */ |
| 162 | static int pending_async_exc = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 163 | |
| 164 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 165 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 166 | int |
| 167 | PyEval_ThreadsInitialized(void) |
| 168 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 172 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 173 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | if (gil_created()) |
| 176 | return; |
| 177 | create_gil(); |
| 178 | take_gil(PyThreadState_GET()); |
| 179 | main_thread = PyThread_get_thread_ident(); |
| 180 | if (!pending_lock) |
| 181 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 184 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 185 | _PyEval_FiniThreads(void) |
| 186 | { |
| 187 | if (!gil_created()) |
| 188 | return; |
| 189 | destroy_gil(); |
| 190 | assert(!gil_created()); |
| 191 | } |
| 192 | |
| 193 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 194 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | PyThreadState *tstate = PyThreadState_GET(); |
| 197 | if (tstate == NULL) |
| 198 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 199 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 203 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | /* This function must succeed when the current thread state is NULL. |
| 206 | We therefore avoid PyThreadState_GET() which dumps a fatal error |
| 207 | in debug mode. |
| 208 | */ |
| 209 | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
| 210 | &_PyThreadState_Current)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 214 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 215 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (tstate == NULL) |
| 217 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 218 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 219 | assert(gil_created()); |
| 220 | take_gil(tstate); |
| 221 | if (PyThreadState_Swap(tstate) != NULL) |
| 222 | Py_FatalError( |
| 223 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 227 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | if (tstate == NULL) |
| 230 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 231 | if (PyThreadState_Swap(NULL) != tstate) |
| 232 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 233 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 234 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 236 | /* This function is called from PyOS_AfterFork to destroy all threads which are |
| 237 | * not running in the child process, and clear internal locks which might be |
| 238 | * held by those threads. (This could also be done using pthread_atfork |
| 239 | * mechanism, at least for the pthreads implementation.) */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 240 | |
| 241 | void |
| 242 | PyEval_ReInitThreads(void) |
| 243 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 244 | _Py_IDENTIFIER(_after_fork); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | PyObject *threading, *result; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 246 | PyThreadState *current_tstate = PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | if (!gil_created()) |
| 249 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | recreate_gil(); |
| 251 | pending_lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 252 | take_gil(current_tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | /* Update the threading module with the new state. |
| 256 | */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 257 | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | "threading"); |
| 259 | if (threading == NULL) { |
| 260 | /* threading not imported */ |
| 261 | PyErr_Clear(); |
| 262 | return; |
| 263 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 264 | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | if (result == NULL) |
| 266 | PyErr_WriteUnraisable(threading); |
| 267 | else |
| 268 | Py_DECREF(result); |
| 269 | Py_DECREF(threading); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 270 | |
| 271 | /* Destroy all threads except the current one */ |
| 272 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 273 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 274 | |
| 275 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 276 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 277 | static int pending_async_exc = 0; |
| 278 | #endif /* WITH_THREAD */ |
| 279 | |
| 280 | /* This function is used to signal that async exceptions are waiting to be |
| 281 | raised, therefore it is also useful in non-threaded builds. */ |
| 282 | |
| 283 | void |
| 284 | _PyEval_SignalAsyncExc(void) |
| 285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 287 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 289 | /* Functions save_thread and restore_thread are always defined so |
| 290 | dynamically loaded modules needn't be compiled separately for use |
| 291 | with and without threads: */ |
| 292 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 293 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 294 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 297 | if (tstate == NULL) |
| 298 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 299 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | if (gil_created()) |
| 301 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 302 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 307 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | if (tstate == NULL) |
| 310 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 311 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (gil_created()) { |
| 313 | int err = errno; |
| 314 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 315 | /* _Py_Finalizing is protected by the GIL */ |
| 316 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 317 | drop_gil(tstate); |
| 318 | PyThread_exit_thread(); |
| 319 | assert(0); /* unreachable */ |
| 320 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | errno = err; |
| 322 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 323 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 328 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 329 | signal handlers or Mac I/O completion routines) can schedule calls |
| 330 | to a function to be called synchronously. |
| 331 | The synchronous function is called with one void* argument. |
| 332 | It should return 0 for success or -1 for failure -- failure should |
| 333 | be accompanied by an exception. |
| 334 | |
| 335 | If registry succeeds, the registry function returns 0; if it fails |
| 336 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 337 | an exception condition). |
| 338 | |
| 339 | Note that because registry may occur from within signal handlers, |
| 340 | or other asynchronous events, calling malloc() is unsafe! |
| 341 | |
| 342 | #ifdef WITH_THREAD |
| 343 | Any thread can schedule pending calls, but only the main thread |
| 344 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 345 | There is no facility to schedule calls to a particular thread, but |
| 346 | that should be easy to change, should that ever be required. In |
| 347 | that case, the static variables here should go into the python |
| 348 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 349 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 350 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 351 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 352 | #ifdef WITH_THREAD |
| 353 | |
| 354 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 355 | scheduling to be made from any thread, and even from an executing |
| 356 | callback. |
| 357 | */ |
| 358 | |
| 359 | #define NPENDINGCALLS 32 |
| 360 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | int (*func)(void *); |
| 362 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 363 | } pendingcalls[NPENDINGCALLS]; |
| 364 | static int pendingfirst = 0; |
| 365 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 366 | |
| 367 | int |
| 368 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | int i, j, result=0; |
| 371 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | /* try a few times for the lock. Since this mechanism is used |
| 374 | * for signal handling (on the main thread), there is a (slim) |
| 375 | * chance that a signal is delivered on the same thread while we |
| 376 | * hold the lock during the Py_MakePendingCalls() function. |
| 377 | * This avoids a deadlock in that case. |
| 378 | * Note that signals can be delivered on any thread. In particular, |
| 379 | * on Windows, a SIGINT is delivered on a system-created worker |
| 380 | * thread. |
| 381 | * We also check for lock being NULL, in the unlikely case that |
| 382 | * this function is called before any bytecode evaluation takes place. |
| 383 | */ |
| 384 | if (lock != NULL) { |
| 385 | for (i = 0; i<100; i++) { |
| 386 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 387 | break; |
| 388 | } |
| 389 | if (i == 100) |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | i = pendinglast; |
| 394 | j = (i + 1) % NPENDINGCALLS; |
| 395 | if (j == pendingfirst) { |
| 396 | result = -1; /* Queue full */ |
| 397 | } else { |
| 398 | pendingcalls[i].func = func; |
| 399 | pendingcalls[i].arg = arg; |
| 400 | pendinglast = j; |
| 401 | } |
| 402 | /* signal main loop */ |
| 403 | SIGNAL_PENDING_CALLS(); |
| 404 | if (lock != NULL) |
| 405 | PyThread_release_lock(lock); |
| 406 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | int |
| 410 | Py_MakePendingCalls(void) |
| 411 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 412 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | int i; |
| 414 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | if (!pending_lock) { |
| 417 | /* initial allocation of the lock */ |
| 418 | pending_lock = PyThread_allocate_lock(); |
| 419 | if (pending_lock == NULL) |
| 420 | return -1; |
| 421 | } |
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 | /* only service pending calls on main thread */ |
| 424 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 425 | return 0; |
| 426 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 427 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 429 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | /* perform a bounded number of calls, in case of recursion */ |
| 431 | for (i=0; i<NPENDINGCALLS; i++) { |
| 432 | int j; |
| 433 | int (*func)(void *); |
| 434 | void *arg = NULL; |
| 435 | |
| 436 | /* pop one item off the queue while holding the lock */ |
| 437 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 438 | j = pendingfirst; |
| 439 | if (j == pendinglast) { |
| 440 | func = NULL; /* Queue empty */ |
| 441 | } else { |
| 442 | func = pendingcalls[j].func; |
| 443 | arg = pendingcalls[j].arg; |
| 444 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 445 | } |
| 446 | if (pendingfirst != pendinglast) |
| 447 | SIGNAL_PENDING_CALLS(); |
| 448 | else |
| 449 | UNSIGNAL_PENDING_CALLS(); |
| 450 | PyThread_release_lock(pending_lock); |
| 451 | /* having released the lock, perform the callback */ |
| 452 | if (func == NULL) |
| 453 | break; |
| 454 | r = func(arg); |
| 455 | if (r) |
| 456 | break; |
| 457 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 458 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | #else /* if ! defined WITH_THREAD */ |
| 463 | |
| 464 | /* |
| 465 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 466 | This code is used for signal handling in python that isn't built |
| 467 | with WITH_THREAD. |
| 468 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 469 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 471 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 472 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 473 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 475 | (1) is very unlikely because typically signal delivery |
| 476 | is blocked during signal handling. So it should be impossible. |
| 477 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 478 | The current code is safe against (2), but not against (1). |
| 479 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 480 | thread is present, interrupted by signals, and that the critical |
| 481 | section is protected with the "busy" variable. On Windows, which |
| 482 | delivers SIGINT on a system thread, this does not hold and therefore |
| 483 | Windows really shouldn't use this version. |
| 484 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 485 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 486 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 487 | #define NPENDINGCALLS 32 |
| 488 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | int (*func)(void *); |
| 490 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 491 | } pendingcalls[NPENDINGCALLS]; |
| 492 | static volatile int pendingfirst = 0; |
| 493 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 494 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 495 | |
| 496 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 497 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | static volatile int busy = 0; |
| 500 | int i, j; |
| 501 | /* XXX Begin critical section */ |
| 502 | if (busy) |
| 503 | return -1; |
| 504 | busy = 1; |
| 505 | i = pendinglast; |
| 506 | j = (i + 1) % NPENDINGCALLS; |
| 507 | if (j == pendingfirst) { |
| 508 | busy = 0; |
| 509 | return -1; /* Queue full */ |
| 510 | } |
| 511 | pendingcalls[i].func = func; |
| 512 | pendingcalls[i].arg = arg; |
| 513 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | SIGNAL_PENDING_CALLS(); |
| 516 | busy = 0; |
| 517 | /* XXX End critical section */ |
| 518 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 521 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 522 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | static int busy = 0; |
| 525 | if (busy) |
| 526 | return 0; |
| 527 | busy = 1; |
| 528 | UNSIGNAL_PENDING_CALLS(); |
| 529 | for (;;) { |
| 530 | int i; |
| 531 | int (*func)(void *); |
| 532 | void *arg; |
| 533 | i = pendingfirst; |
| 534 | if (i == pendinglast) |
| 535 | break; /* Queue empty */ |
| 536 | func = pendingcalls[i].func; |
| 537 | arg = pendingcalls[i].arg; |
| 538 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 539 | if (func(arg) < 0) { |
| 540 | busy = 0; |
| 541 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 542 | return -1; |
| 543 | } |
| 544 | } |
| 545 | busy = 0; |
| 546 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 549 | #endif /* WITH_THREAD */ |
| 550 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 551 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 552 | /* The interpreter's recursion limit */ |
| 553 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 554 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 555 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 556 | #endif |
| 557 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 558 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 559 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 560 | int |
| 561 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 566 | void |
| 567 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | recursion_limit = new_limit; |
| 570 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 573 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 574 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 575 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 576 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 577 | Without USE_STACKCHECK, there is no need for this. */ |
| 578 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 579 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 582 | |
| 583 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | if (PyOS_CheckStack()) { |
| 585 | --tstate->recursion_depth; |
| 586 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 587 | return -1; |
| 588 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 589 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | _Py_CheckRecursionLimit = recursion_limit; |
| 591 | if (tstate->recursion_critical) |
| 592 | /* Somebody asked that we don't check for recursion. */ |
| 593 | return 0; |
| 594 | if (tstate->overflowed) { |
| 595 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 596 | /* Overflowing while handling an overflow. Give up. */ |
| 597 | Py_FatalError("Cannot recover from stack overflow."); |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | if (tstate->recursion_depth > recursion_limit) { |
| 602 | --tstate->recursion_depth; |
| 603 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 604 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | "maximum recursion depth exceeded%s", |
| 606 | where); |
| 607 | return -1; |
| 608 | } |
| 609 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 612 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 613 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 614 | WHY_NOT = 0x0001, /* No error */ |
| 615 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 616 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 617 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 618 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 619 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 620 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 621 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 622 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 623 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 624 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 625 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 626 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 627 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 628 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 629 | /* Records whether tracing is on for any thread. Counts the number of |
| 630 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 631 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 632 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 633 | fast_next_opcode*/ |
| 634 | static int _Py_TracingPossible = 0; |
| 635 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 637 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 638 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 639 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | return PyEval_EvalCodeEx(co, |
| 642 | globals, locals, |
| 643 | (PyObject **)NULL, 0, |
| 644 | (PyObject **)NULL, 0, |
| 645 | (PyObject **)NULL, 0, |
| 646 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | /* Interpreter main loop */ |
| 651 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 652 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 653 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | /* This is for backward compatibility with extension modules that |
| 655 | used this API; core interpreter code should call |
| 656 | PyEval_EvalFrameEx() */ |
| 657 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 661 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 662 | { |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 663 | PyThreadState *tstate = PyThreadState_GET(); |
| 664 | return tstate->interp->eval_frame(f, throwflag); |
| 665 | } |
| 666 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 667 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 668 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 669 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 670 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 672 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 673 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 674 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 675 | int opcode; /* Current opcode */ |
| 676 | int oparg; /* Current opcode argument, if any */ |
| 677 | enum why_code why; /* Reason for block stack unwind */ |
| 678 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | PyObject *retval = NULL; /* Return value */ |
| 680 | PyThreadState *tstate = PyThreadState_GET(); |
| 681 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | is true when the line being executed has changed. The |
| 688 | initial values are such as to make this false the first |
| 689 | time it is tested. */ |
| 690 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 691 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 692 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | PyObject *names; |
| 694 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 695 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 696 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 697 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 698 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 699 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 700 | /* Computed GOTOs, or |
| 701 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 702 | using gcc's labels-as-values extension |
| 703 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 704 | |
| 705 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 707 | combined with a lookup table of jump addresses. However, since the |
| 708 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 709 | hard time making the right prediction for where to jump next (actually, |
| 710 | it will be always wrong except in the uncommon case of a sequence of |
| 711 | several identical opcodes). |
| 712 | |
| 713 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 714 | indirect jump instruction at the end of each opcode. Since the jump |
| 715 | instruction is at a different address for each opcode, the CPU will make a |
| 716 | separate prediction for each of these instructions, which is equivalent to |
| 717 | predicting the second opcode of each opcode pair. These predictions have |
| 718 | a much better chance to turn out valid, especially in small bytecode loops. |
| 719 | |
| 720 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 722 | and potentially many more instructions (depending on the pipeline width). |
| 723 | A correctly predicted branch, however, is nearly free. |
| 724 | |
| 725 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 726 | faster than the normal "switch" version, depending on the compiler and the |
| 727 | CPU architecture. |
| 728 | |
| 729 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 730 | because it would render the measurements invalid. |
| 731 | |
| 732 | |
| 733 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 734 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 735 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 736 | -fno-crossjumping). |
| 737 | */ |
| 738 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 739 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 740 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 741 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 742 | #endif |
| 743 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 744 | #ifdef HAVE_COMPUTED_GOTOS |
| 745 | #ifndef USE_COMPUTED_GOTOS |
| 746 | #define USE_COMPUTED_GOTOS 1 |
| 747 | #endif |
| 748 | #else |
| 749 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 750 | #error "Computed gotos are not supported on this compiler." |
| 751 | #endif |
| 752 | #undef USE_COMPUTED_GOTOS |
| 753 | #define USE_COMPUTED_GOTOS 0 |
| 754 | #endif |
| 755 | |
| 756 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 757 | /* Import the static jump table */ |
| 758 | #include "opcode_targets.h" |
| 759 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 760 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | TARGET_##op: \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | case op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 764 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | { \ |
| 766 | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
| 767 | FAST_DISPATCH(); \ |
| 768 | } \ |
| 769 | continue; \ |
| 770 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 771 | |
| 772 | #ifdef LLTRACE |
| 773 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 775 | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 777 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 778 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | } \ |
| 780 | goto fast_next_opcode; \ |
| 781 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 782 | #else |
| 783 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 785 | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 787 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 788 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | } \ |
| 790 | goto fast_next_opcode; \ |
| 791 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 792 | #endif |
| 793 | |
| 794 | #else |
| 795 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | case op: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 797 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 798 | #define DISPATCH() continue |
| 799 | #define FAST_DISPATCH() goto fast_next_opcode |
| 800 | #endif |
| 801 | |
| 802 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 803 | /* Tuple access macros */ |
| 804 | |
| 805 | #ifndef Py_DEBUG |
| 806 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 807 | #else |
| 808 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 809 | #endif |
| 810 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 811 | /* Code access macros */ |
| 812 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 813 | /* The integer overflow is checked by an assertion below. */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 814 | #define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 815 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 816 | _Py_CODEUNIT word = *next_instr; \ |
| 817 | opcode = _Py_OPCODE(word); \ |
| 818 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 819 | next_instr++; \ |
| 820 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 821 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 822 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 823 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 824 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | Some opcodes tend to come in pairs thus making it possible to |
| 826 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 827 | 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] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | Verifying the prediction costs a single high-speed test of a register |
| 830 | variable against a constant. If the pairing was good, then the |
| 831 | processor's own internal branch predication has a high likelihood of |
| 832 | success, resulting in a nearly zero-overhead transition to the |
| 833 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 834 | including its unpredictable switch-case branch. Combined with the |
| 835 | processor's internal branch prediction, a successful PREDICT has the |
| 836 | effect of making the two opcodes run as if they were a single new opcode |
| 837 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 838 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 839 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | predictions turned-on and interpret the results as if some opcodes |
| 841 | had been combined or turn-off predictions so that the opcode frequency |
| 842 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 843 | |
| 844 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | the CPU to record separate branch prediction information for each |
| 846 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 847 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 848 | */ |
| 849 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 850 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 852 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 853 | #define PREDICT(op) \ |
| 854 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 855 | _Py_CODEUNIT word = *next_instr; \ |
| 856 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 857 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 858 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 859 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 860 | goto PRED_##op; \ |
| 861 | } \ |
| 862 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 863 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 864 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 865 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 867 | /* Stack manipulation macros */ |
| 868 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 869 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 870 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 871 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 872 | #define EMPTY() (STACK_LEVEL() == 0) |
| 873 | #define TOP() (stack_pointer[-1]) |
| 874 | #define SECOND() (stack_pointer[-2]) |
| 875 | #define THIRD() (stack_pointer[-3]) |
| 876 | #define FOURTH() (stack_pointer[-4]) |
| 877 | #define PEEK(n) (stack_pointer[-(n)]) |
| 878 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 879 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 880 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 881 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 882 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 883 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 884 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 885 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 886 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 887 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 889 | lltrace && prtrace(TOP(), "push")); \ |
| 890 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 892 | BASIC_POP()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 894 | lltrace && prtrace(TOP(), "stackadj")); \ |
| 895 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 896 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 897 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 898 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 899 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 900 | #define PUSH(v) BASIC_PUSH(v) |
| 901 | #define POP() BASIC_POP() |
| 902 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 903 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 904 | #endif |
| 905 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 906 | /* Local variable macros */ |
| 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 909 | |
| 910 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 911 | then store the new value; it must copy the old value to a temporary |
| 912 | value, then store the new value, and then DECREF the temporary value. |
| 913 | This is because it is possible that during the DECREF the frame is |
| 914 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 915 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 917 | GETLOCAL(i) = value; \ |
| 918 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 919 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 920 | |
| 921 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 923 | PyObject *v = POP(); \ |
| 924 | Py_XDECREF(v); \ |
| 925 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 926 | |
| 927 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 928 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | PyObject *type, *value, *traceback; \ |
| 930 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 931 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 932 | value = POP(); \ |
| 933 | Py_XDECREF(value); \ |
| 934 | } \ |
| 935 | type = tstate->exc_type; \ |
| 936 | value = tstate->exc_value; \ |
| 937 | traceback = tstate->exc_traceback; \ |
| 938 | tstate->exc_type = POP(); \ |
| 939 | tstate->exc_value = POP(); \ |
| 940 | tstate->exc_traceback = POP(); \ |
| 941 | Py_XDECREF(type); \ |
| 942 | Py_XDECREF(value); \ |
| 943 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 944 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 945 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 946 | /* Start of code */ |
| 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | /* push frame */ |
| 949 | if (Py_EnterRecursiveCall("")) |
| 950 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | if (tstate->use_tracing) { |
| 955 | if (tstate->c_tracefunc != NULL) { |
| 956 | /* tstate->c_tracefunc, if defined, is a |
| 957 | function that will be called on *every* entry |
| 958 | to a code block. Its return value, if not |
| 959 | None, is a function that will be called at |
| 960 | the start of each executed line of code. |
| 961 | (Actually, the function must return itself |
| 962 | in order to continue tracing.) The trace |
| 963 | functions are called with three arguments: |
| 964 | a pointer to the current frame, a string |
| 965 | indicating why the function is called, and |
| 966 | an argument which depends on the situation. |
| 967 | The global trace function is also called |
| 968 | whenever an exception is detected. */ |
| 969 | if (call_trace_protected(tstate->c_tracefunc, |
| 970 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 971 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | /* Trace function raised an error */ |
| 973 | goto exit_eval_frame; |
| 974 | } |
| 975 | } |
| 976 | if (tstate->c_profilefunc != NULL) { |
| 977 | /* Similar for c_profilefunc, except it needn't |
| 978 | return itself and isn't called for "line" events */ |
| 979 | if (call_trace_protected(tstate->c_profilefunc, |
| 980 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 981 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | /* Profile function raised an error */ |
| 983 | goto exit_eval_frame; |
| 984 | } |
| 985 | } |
| 986 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 987 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 988 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 989 | dtrace_function_entry(f); |
| 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | co = f->f_code; |
| 992 | names = co->co_names; |
| 993 | consts = co->co_consts; |
| 994 | fastlocals = f->f_localsplus; |
| 995 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 996 | assert(PyBytes_Check(co->co_code)); |
| 997 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 998 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 999 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1000 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1001 | /* |
| 1002 | f->f_lasti refers to the index of the last instruction, |
| 1003 | 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] | 1004 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1005 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1006 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1009 | direct succession without updating f->f_lasti. A successful |
| 1010 | prediction effectively links the two codes together as if they |
| 1011 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1012 | the first code in the pair (for instance, GET_ITER followed by |
| 1013 | 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] | 1014 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1016 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1017 | next_instr = first_instr; |
| 1018 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1019 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1020 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1021 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | stack_pointer = f->f_stacktop; |
| 1023 | assert(stack_pointer != NULL); |
| 1024 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1025 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1026 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1027 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 1028 | 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] | 1029 | /* We were in an except handler when we left, |
| 1030 | restore the exception state which was put aside |
| 1031 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1032 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1034 | else |
| 1035 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1037 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1038 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1039 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1040 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | why = WHY_NOT; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1043 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1044 | if (throwflag) /* support for generator.throw() */ |
| 1045 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1046 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1047 | #ifdef Py_DEBUG |
| 1048 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
| 1049 | because it may clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1050 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1051 | assert(!PyErr_Occurred()); |
| 1052 | #endif |
| 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1056 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1057 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | /* Do periodic things. Doing this every time through |
| 1060 | the loop would add too much overhead, so we do it |
| 1061 | only every Nth instruction. We also do it if |
| 1062 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1063 | event needs attention (e.g. a signal handler or |
| 1064 | async I/O handler); see Py_AddPendingCall() and |
| 1065 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1068 | if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1070 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | goto fast_next_opcode; |
| 1072 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1074 | if (Py_MakePendingCalls() < 0) |
| 1075 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1077 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1078 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | /* Give another thread a chance */ |
| 1080 | if (PyThreadState_Swap(NULL) != tstate) |
| 1081 | Py_FatalError("ceval: tstate mix-up"); |
| 1082 | drop_gil(tstate); |
| 1083 | |
| 1084 | /* Other threads may run now */ |
| 1085 | |
| 1086 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1087 | |
| 1088 | /* Check if we should make a quick exit. */ |
| 1089 | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
| 1090 | drop_gil(tstate); |
| 1091 | PyThread_exit_thread(); |
| 1092 | } |
| 1093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | if (PyThreadState_Swap(tstate) != NULL) |
| 1095 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1097 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | /* Check for asynchronous exceptions. */ |
| 1099 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1100 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | tstate->async_exc = NULL; |
| 1102 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1103 | PyErr_SetNone(exc); |
| 1104 | Py_DECREF(exc); |
| 1105 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | fast_next_opcode: |
| 1110 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1111 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1112 | if (PyDTrace_LINE_ENABLED()) |
| 1113 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1118 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1119 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | /* see maybe_call_line_trace |
| 1121 | for expository comments */ |
| 1122 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1125 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1126 | tstate, f, |
| 1127 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | /* Reload possibly changed frame fields */ |
| 1129 | JUMPTO(f->f_lasti); |
| 1130 | if (f->f_stacktop != NULL) { |
| 1131 | stack_pointer = f->f_stacktop; |
| 1132 | f->f_stacktop = NULL; |
| 1133 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1134 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1136 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1140 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1141 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1142 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1143 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1144 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | dxpairs[lastopcode][opcode]++; |
| 1146 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1147 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1149 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1150 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1151 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | if (lltrace) { |
| 1155 | if (HAS_ARG(opcode)) { |
| 1156 | printf("%d: %d, %d\n", |
| 1157 | f->f_lasti, opcode, oparg); |
| 1158 | } |
| 1159 | else { |
| 1160 | printf("%d: %d\n", |
| 1161 | f->f_lasti, opcode); |
| 1162 | } |
| 1163 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1164 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | /* BEWARE! |
| 1169 | It is essential that any operation that fails sets either |
| 1170 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1171 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | TARGET(NOP) |
| 1174 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1175 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1176 | TARGET(LOAD_FAST) { |
| 1177 | PyObject *value = GETLOCAL(oparg); |
| 1178 | if (value == NULL) { |
| 1179 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1180 | UNBOUNDLOCAL_ERROR_MSG, |
| 1181 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1182 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1184 | Py_INCREF(value); |
| 1185 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1189 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1190 | TARGET(LOAD_CONST) { |
| 1191 | PyObject *value = GETITEM(consts, oparg); |
| 1192 | Py_INCREF(value); |
| 1193 | PUSH(value); |
| 1194 | FAST_DISPATCH(); |
| 1195 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1196 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1197 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1198 | TARGET(STORE_FAST) { |
| 1199 | PyObject *value = POP(); |
| 1200 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1202 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1203 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1204 | TARGET(POP_TOP) { |
| 1205 | PyObject *value = POP(); |
| 1206 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1208 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1209 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1210 | TARGET(ROT_TWO) { |
| 1211 | PyObject *top = TOP(); |
| 1212 | PyObject *second = SECOND(); |
| 1213 | SET_TOP(second); |
| 1214 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1216 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1217 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1218 | TARGET(ROT_THREE) { |
| 1219 | PyObject *top = TOP(); |
| 1220 | PyObject *second = SECOND(); |
| 1221 | PyObject *third = THIRD(); |
| 1222 | SET_TOP(second); |
| 1223 | SET_SECOND(third); |
| 1224 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1226 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1227 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1228 | TARGET(DUP_TOP) { |
| 1229 | PyObject *top = TOP(); |
| 1230 | Py_INCREF(top); |
| 1231 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1233 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1234 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1235 | TARGET(DUP_TOP_TWO) { |
| 1236 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1237 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1238 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1239 | Py_INCREF(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1240 | STACKADJ(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1241 | SET_TOP(top); |
| 1242 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1243 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1244 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1245 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1246 | TARGET(UNARY_POSITIVE) { |
| 1247 | PyObject *value = TOP(); |
| 1248 | PyObject *res = PyNumber_Positive(value); |
| 1249 | Py_DECREF(value); |
| 1250 | SET_TOP(res); |
| 1251 | if (res == NULL) |
| 1252 | goto error; |
| 1253 | DISPATCH(); |
| 1254 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1255 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1256 | TARGET(UNARY_NEGATIVE) { |
| 1257 | PyObject *value = TOP(); |
| 1258 | PyObject *res = PyNumber_Negative(value); |
| 1259 | Py_DECREF(value); |
| 1260 | SET_TOP(res); |
| 1261 | if (res == NULL) |
| 1262 | goto error; |
| 1263 | DISPATCH(); |
| 1264 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1265 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1266 | TARGET(UNARY_NOT) { |
| 1267 | PyObject *value = TOP(); |
| 1268 | int err = PyObject_IsTrue(value); |
| 1269 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | if (err == 0) { |
| 1271 | Py_INCREF(Py_True); |
| 1272 | SET_TOP(Py_True); |
| 1273 | DISPATCH(); |
| 1274 | } |
| 1275 | else if (err > 0) { |
| 1276 | Py_INCREF(Py_False); |
| 1277 | SET_TOP(Py_False); |
| 1278 | err = 0; |
| 1279 | DISPATCH(); |
| 1280 | } |
| 1281 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1282 | goto error; |
| 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(UNARY_INVERT) { |
| 1286 | PyObject *value = TOP(); |
| 1287 | PyObject *res = PyNumber_Invert(value); |
| 1288 | Py_DECREF(value); |
| 1289 | SET_TOP(res); |
| 1290 | if (res == NULL) |
| 1291 | goto error; |
| 1292 | DISPATCH(); |
| 1293 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1294 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1295 | TARGET(BINARY_POWER) { |
| 1296 | PyObject *exp = POP(); |
| 1297 | PyObject *base = TOP(); |
| 1298 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1299 | Py_DECREF(base); |
| 1300 | Py_DECREF(exp); |
| 1301 | SET_TOP(res); |
| 1302 | if (res == NULL) |
| 1303 | goto error; |
| 1304 | DISPATCH(); |
| 1305 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1306 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1307 | TARGET(BINARY_MULTIPLY) { |
| 1308 | PyObject *right = POP(); |
| 1309 | PyObject *left = TOP(); |
| 1310 | PyObject *res = PyNumber_Multiply(left, right); |
| 1311 | Py_DECREF(left); |
| 1312 | Py_DECREF(right); |
| 1313 | SET_TOP(res); |
| 1314 | if (res == NULL) |
| 1315 | goto error; |
| 1316 | DISPATCH(); |
| 1317 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1318 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1319 | TARGET(BINARY_MATRIX_MULTIPLY) { |
| 1320 | PyObject *right = POP(); |
| 1321 | PyObject *left = TOP(); |
| 1322 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1323 | Py_DECREF(left); |
| 1324 | Py_DECREF(right); |
| 1325 | SET_TOP(res); |
| 1326 | if (res == NULL) |
| 1327 | goto error; |
| 1328 | DISPATCH(); |
| 1329 | } |
| 1330 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1331 | TARGET(BINARY_TRUE_DIVIDE) { |
| 1332 | PyObject *divisor = POP(); |
| 1333 | PyObject *dividend = TOP(); |
| 1334 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1335 | Py_DECREF(dividend); |
| 1336 | Py_DECREF(divisor); |
| 1337 | SET_TOP(quotient); |
| 1338 | if (quotient == NULL) |
| 1339 | goto error; |
| 1340 | DISPATCH(); |
| 1341 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1342 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1343 | TARGET(BINARY_FLOOR_DIVIDE) { |
| 1344 | PyObject *divisor = POP(); |
| 1345 | PyObject *dividend = TOP(); |
| 1346 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1347 | Py_DECREF(dividend); |
| 1348 | Py_DECREF(divisor); |
| 1349 | SET_TOP(quotient); |
| 1350 | if (quotient == NULL) |
| 1351 | goto error; |
| 1352 | DISPATCH(); |
| 1353 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1354 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1355 | TARGET(BINARY_MODULO) { |
| 1356 | PyObject *divisor = POP(); |
| 1357 | PyObject *dividend = TOP(); |
| 1358 | PyObject *res = PyUnicode_CheckExact(dividend) ? |
| 1359 | PyUnicode_Format(dividend, divisor) : |
| 1360 | PyNumber_Remainder(dividend, divisor); |
| 1361 | Py_DECREF(divisor); |
| 1362 | Py_DECREF(dividend); |
| 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 | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1369 | TARGET(BINARY_ADD) { |
| 1370 | PyObject *right = POP(); |
| 1371 | PyObject *left = TOP(); |
| 1372 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1373 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1374 | CPython using bytecode, it is simply worthless. |
| 1375 | See http://bugs.python.org/issue21955 and |
| 1376 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1377 | no patch shown any impact on a realistic benchmark, only a minor |
| 1378 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1379 | if (PyUnicode_CheckExact(left) && |
| 1380 | PyUnicode_CheckExact(right)) { |
| 1381 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1382 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1383 | } |
| 1384 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1385 | sum = PyNumber_Add(left, right); |
| 1386 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1387 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1388 | Py_DECREF(right); |
| 1389 | SET_TOP(sum); |
| 1390 | if (sum == NULL) |
| 1391 | goto error; |
| 1392 | DISPATCH(); |
| 1393 | } |
| 1394 | |
| 1395 | TARGET(BINARY_SUBTRACT) { |
| 1396 | PyObject *right = POP(); |
| 1397 | PyObject *left = TOP(); |
| 1398 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1399 | Py_DECREF(right); |
| 1400 | Py_DECREF(left); |
| 1401 | SET_TOP(diff); |
| 1402 | if (diff == NULL) |
| 1403 | goto error; |
| 1404 | DISPATCH(); |
| 1405 | } |
| 1406 | |
| 1407 | TARGET(BINARY_SUBSCR) { |
| 1408 | PyObject *sub = POP(); |
| 1409 | PyObject *container = TOP(); |
| 1410 | PyObject *res = PyObject_GetItem(container, sub); |
| 1411 | Py_DECREF(container); |
| 1412 | Py_DECREF(sub); |
| 1413 | SET_TOP(res); |
| 1414 | if (res == NULL) |
| 1415 | goto error; |
| 1416 | DISPATCH(); |
| 1417 | } |
| 1418 | |
| 1419 | TARGET(BINARY_LSHIFT) { |
| 1420 | PyObject *right = POP(); |
| 1421 | PyObject *left = TOP(); |
| 1422 | PyObject *res = PyNumber_Lshift(left, right); |
| 1423 | Py_DECREF(left); |
| 1424 | Py_DECREF(right); |
| 1425 | SET_TOP(res); |
| 1426 | if (res == NULL) |
| 1427 | goto error; |
| 1428 | DISPATCH(); |
| 1429 | } |
| 1430 | |
| 1431 | TARGET(BINARY_RSHIFT) { |
| 1432 | PyObject *right = POP(); |
| 1433 | PyObject *left = TOP(); |
| 1434 | PyObject *res = PyNumber_Rshift(left, right); |
| 1435 | Py_DECREF(left); |
| 1436 | Py_DECREF(right); |
| 1437 | SET_TOP(res); |
| 1438 | if (res == NULL) |
| 1439 | goto error; |
| 1440 | DISPATCH(); |
| 1441 | } |
| 1442 | |
| 1443 | TARGET(BINARY_AND) { |
| 1444 | PyObject *right = POP(); |
| 1445 | PyObject *left = TOP(); |
| 1446 | PyObject *res = PyNumber_And(left, right); |
| 1447 | Py_DECREF(left); |
| 1448 | Py_DECREF(right); |
| 1449 | SET_TOP(res); |
| 1450 | if (res == NULL) |
| 1451 | goto error; |
| 1452 | DISPATCH(); |
| 1453 | } |
| 1454 | |
| 1455 | TARGET(BINARY_XOR) { |
| 1456 | PyObject *right = POP(); |
| 1457 | PyObject *left = TOP(); |
| 1458 | PyObject *res = PyNumber_Xor(left, right); |
| 1459 | Py_DECREF(left); |
| 1460 | Py_DECREF(right); |
| 1461 | SET_TOP(res); |
| 1462 | if (res == NULL) |
| 1463 | goto error; |
| 1464 | DISPATCH(); |
| 1465 | } |
| 1466 | |
| 1467 | TARGET(BINARY_OR) { |
| 1468 | PyObject *right = POP(); |
| 1469 | PyObject *left = TOP(); |
| 1470 | PyObject *res = PyNumber_Or(left, right); |
| 1471 | Py_DECREF(left); |
| 1472 | Py_DECREF(right); |
| 1473 | SET_TOP(res); |
| 1474 | if (res == NULL) |
| 1475 | goto error; |
| 1476 | DISPATCH(); |
| 1477 | } |
| 1478 | |
| 1479 | TARGET(LIST_APPEND) { |
| 1480 | PyObject *v = POP(); |
| 1481 | PyObject *list = PEEK(oparg); |
| 1482 | int err; |
| 1483 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1485 | if (err != 0) |
| 1486 | goto error; |
| 1487 | PREDICT(JUMP_ABSOLUTE); |
| 1488 | DISPATCH(); |
| 1489 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1490 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1491 | TARGET(SET_ADD) { |
| 1492 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1493 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1494 | int err; |
| 1495 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1497 | if (err != 0) |
| 1498 | goto error; |
| 1499 | PREDICT(JUMP_ABSOLUTE); |
| 1500 | DISPATCH(); |
| 1501 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1502 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1503 | TARGET(INPLACE_POWER) { |
| 1504 | PyObject *exp = POP(); |
| 1505 | PyObject *base = TOP(); |
| 1506 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1507 | Py_DECREF(base); |
| 1508 | Py_DECREF(exp); |
| 1509 | SET_TOP(res); |
| 1510 | if (res == NULL) |
| 1511 | goto error; |
| 1512 | DISPATCH(); |
| 1513 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1514 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1515 | TARGET(INPLACE_MULTIPLY) { |
| 1516 | PyObject *right = POP(); |
| 1517 | PyObject *left = TOP(); |
| 1518 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1519 | Py_DECREF(left); |
| 1520 | Py_DECREF(right); |
| 1521 | SET_TOP(res); |
| 1522 | if (res == NULL) |
| 1523 | goto error; |
| 1524 | DISPATCH(); |
| 1525 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1526 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1527 | TARGET(INPLACE_MATRIX_MULTIPLY) { |
| 1528 | PyObject *right = POP(); |
| 1529 | PyObject *left = TOP(); |
| 1530 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1531 | Py_DECREF(left); |
| 1532 | Py_DECREF(right); |
| 1533 | SET_TOP(res); |
| 1534 | if (res == NULL) |
| 1535 | goto error; |
| 1536 | DISPATCH(); |
| 1537 | } |
| 1538 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1539 | TARGET(INPLACE_TRUE_DIVIDE) { |
| 1540 | PyObject *divisor = POP(); |
| 1541 | PyObject *dividend = TOP(); |
| 1542 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1543 | Py_DECREF(dividend); |
| 1544 | Py_DECREF(divisor); |
| 1545 | SET_TOP(quotient); |
| 1546 | if (quotient == NULL) |
| 1547 | goto error; |
| 1548 | DISPATCH(); |
| 1549 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1550 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1551 | TARGET(INPLACE_FLOOR_DIVIDE) { |
| 1552 | PyObject *divisor = POP(); |
| 1553 | PyObject *dividend = TOP(); |
| 1554 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1555 | Py_DECREF(dividend); |
| 1556 | Py_DECREF(divisor); |
| 1557 | SET_TOP(quotient); |
| 1558 | if (quotient == NULL) |
| 1559 | goto error; |
| 1560 | DISPATCH(); |
| 1561 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1562 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1563 | TARGET(INPLACE_MODULO) { |
| 1564 | PyObject *right = POP(); |
| 1565 | PyObject *left = TOP(); |
| 1566 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1567 | Py_DECREF(left); |
| 1568 | Py_DECREF(right); |
| 1569 | SET_TOP(mod); |
| 1570 | if (mod == NULL) |
| 1571 | goto error; |
| 1572 | DISPATCH(); |
| 1573 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1574 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1575 | TARGET(INPLACE_ADD) { |
| 1576 | PyObject *right = POP(); |
| 1577 | PyObject *left = TOP(); |
| 1578 | PyObject *sum; |
| 1579 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1580 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1581 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1582 | } |
| 1583 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1584 | sum = PyNumber_InPlaceAdd(left, right); |
| 1585 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1586 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1587 | Py_DECREF(right); |
| 1588 | SET_TOP(sum); |
| 1589 | if (sum == NULL) |
| 1590 | goto error; |
| 1591 | DISPATCH(); |
| 1592 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1593 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1594 | TARGET(INPLACE_SUBTRACT) { |
| 1595 | PyObject *right = POP(); |
| 1596 | PyObject *left = TOP(); |
| 1597 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1598 | Py_DECREF(left); |
| 1599 | Py_DECREF(right); |
| 1600 | SET_TOP(diff); |
| 1601 | if (diff == NULL) |
| 1602 | goto error; |
| 1603 | DISPATCH(); |
| 1604 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1605 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1606 | TARGET(INPLACE_LSHIFT) { |
| 1607 | PyObject *right = POP(); |
| 1608 | PyObject *left = TOP(); |
| 1609 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1610 | Py_DECREF(left); |
| 1611 | Py_DECREF(right); |
| 1612 | SET_TOP(res); |
| 1613 | if (res == NULL) |
| 1614 | goto error; |
| 1615 | DISPATCH(); |
| 1616 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1617 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1618 | TARGET(INPLACE_RSHIFT) { |
| 1619 | PyObject *right = POP(); |
| 1620 | PyObject *left = TOP(); |
| 1621 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1622 | Py_DECREF(left); |
| 1623 | Py_DECREF(right); |
| 1624 | SET_TOP(res); |
| 1625 | if (res == NULL) |
| 1626 | goto error; |
| 1627 | DISPATCH(); |
| 1628 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1629 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1630 | TARGET(INPLACE_AND) { |
| 1631 | PyObject *right = POP(); |
| 1632 | PyObject *left = TOP(); |
| 1633 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1634 | Py_DECREF(left); |
| 1635 | Py_DECREF(right); |
| 1636 | SET_TOP(res); |
| 1637 | if (res == NULL) |
| 1638 | goto error; |
| 1639 | DISPATCH(); |
| 1640 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1641 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1642 | TARGET(INPLACE_XOR) { |
| 1643 | PyObject *right = POP(); |
| 1644 | PyObject *left = TOP(); |
| 1645 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1646 | Py_DECREF(left); |
| 1647 | Py_DECREF(right); |
| 1648 | SET_TOP(res); |
| 1649 | if (res == NULL) |
| 1650 | goto error; |
| 1651 | DISPATCH(); |
| 1652 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1653 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1654 | TARGET(INPLACE_OR) { |
| 1655 | PyObject *right = POP(); |
| 1656 | PyObject *left = TOP(); |
| 1657 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1658 | Py_DECREF(left); |
| 1659 | Py_DECREF(right); |
| 1660 | SET_TOP(res); |
| 1661 | if (res == NULL) |
| 1662 | goto error; |
| 1663 | DISPATCH(); |
| 1664 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1665 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1666 | TARGET(STORE_SUBSCR) { |
| 1667 | PyObject *sub = TOP(); |
| 1668 | PyObject *container = SECOND(); |
| 1669 | PyObject *v = THIRD(); |
| 1670 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | STACKADJ(-3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1672 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1673 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1675 | Py_DECREF(container); |
| 1676 | Py_DECREF(sub); |
| 1677 | if (err != 0) |
| 1678 | goto error; |
| 1679 | DISPATCH(); |
| 1680 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1681 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1682 | TARGET(STORE_ANNOTATION) { |
| 1683 | _Py_IDENTIFIER(__annotations__); |
| 1684 | PyObject *ann_dict; |
| 1685 | PyObject *ann = POP(); |
| 1686 | PyObject *name = GETITEM(names, oparg); |
| 1687 | int err; |
| 1688 | if (f->f_locals == NULL) { |
| 1689 | PyErr_Format(PyExc_SystemError, |
| 1690 | "no locals found when storing annotation"); |
| 1691 | Py_DECREF(ann); |
| 1692 | goto error; |
| 1693 | } |
| 1694 | /* first try to get __annotations__ from locals... */ |
| 1695 | if (PyDict_CheckExact(f->f_locals)) { |
| 1696 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 1697 | &PyId___annotations__); |
| 1698 | if (ann_dict == NULL) { |
| 1699 | PyErr_SetString(PyExc_NameError, |
| 1700 | "__annotations__ not found"); |
| 1701 | Py_DECREF(ann); |
| 1702 | goto error; |
| 1703 | } |
| 1704 | Py_INCREF(ann_dict); |
| 1705 | } |
| 1706 | else { |
| 1707 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 1708 | if (ann_str == NULL) { |
| 1709 | Py_DECREF(ann); |
| 1710 | goto error; |
| 1711 | } |
| 1712 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 1713 | if (ann_dict == NULL) { |
| 1714 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 1715 | PyErr_SetString(PyExc_NameError, |
| 1716 | "__annotations__ not found"); |
| 1717 | } |
| 1718 | Py_DECREF(ann); |
| 1719 | goto error; |
| 1720 | } |
| 1721 | } |
| 1722 | /* ...if succeeded, __annotations__[name] = ann */ |
| 1723 | if (PyDict_CheckExact(ann_dict)) { |
| 1724 | err = PyDict_SetItem(ann_dict, name, ann); |
| 1725 | } |
| 1726 | else { |
| 1727 | err = PyObject_SetItem(ann_dict, name, ann); |
| 1728 | } |
| 1729 | Py_DECREF(ann_dict); |
Yury Selivanov | 50c584f | 2016-09-08 23:38:21 -0700 | [diff] [blame] | 1730 | Py_DECREF(ann); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1731 | if (err != 0) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1732 | goto error; |
| 1733 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1734 | DISPATCH(); |
| 1735 | } |
| 1736 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1737 | TARGET(DELETE_SUBSCR) { |
| 1738 | PyObject *sub = TOP(); |
| 1739 | PyObject *container = SECOND(); |
| 1740 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | STACKADJ(-2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1742 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1743 | err = PyObject_DelItem(container, sub); |
| 1744 | Py_DECREF(container); |
| 1745 | Py_DECREF(sub); |
| 1746 | if (err != 0) |
| 1747 | goto error; |
| 1748 | DISPATCH(); |
| 1749 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1750 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1751 | TARGET(PRINT_EXPR) { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1752 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1753 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1754 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1755 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1756 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | PyErr_SetString(PyExc_RuntimeError, |
| 1758 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1759 | Py_DECREF(value); |
| 1760 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1762 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1763 | Py_DECREF(value); |
| 1764 | if (res == NULL) |
| 1765 | goto error; |
| 1766 | Py_DECREF(res); |
| 1767 | DISPATCH(); |
| 1768 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1769 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1770 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1772 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1773 | TARGET(RAISE_VARARGS) { |
| 1774 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | switch (oparg) { |
| 1776 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1777 | cause = POP(); /* cause */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1779 | exc = POP(); /* exc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | case 0: /* Fallthrough */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1781 | if (do_raise(exc, cause)) { |
| 1782 | why = WHY_EXCEPTION; |
| 1783 | goto fast_block_end; |
| 1784 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | break; |
| 1786 | default: |
| 1787 | PyErr_SetString(PyExc_SystemError, |
| 1788 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | break; |
| 1790 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1791 | goto error; |
| 1792 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1793 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1794 | TARGET(RETURN_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | retval = POP(); |
| 1796 | why = WHY_RETURN; |
| 1797 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1798 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1799 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1800 | TARGET(GET_AITER) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1801 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1802 | PyObject *iter = NULL; |
| 1803 | PyObject *awaitable = NULL; |
| 1804 | PyObject *obj = TOP(); |
| 1805 | PyTypeObject *type = Py_TYPE(obj); |
| 1806 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1807 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1808 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1809 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1810 | |
| 1811 | if (getter != NULL) { |
| 1812 | iter = (*getter)(obj); |
| 1813 | Py_DECREF(obj); |
| 1814 | if (iter == NULL) { |
| 1815 | SET_TOP(NULL); |
| 1816 | goto error; |
| 1817 | } |
| 1818 | } |
| 1819 | else { |
| 1820 | SET_TOP(NULL); |
| 1821 | PyErr_Format( |
| 1822 | PyExc_TypeError, |
| 1823 | "'async for' requires an object with " |
| 1824 | "__aiter__ method, got %.100s", |
| 1825 | type->tp_name); |
| 1826 | Py_DECREF(obj); |
| 1827 | goto error; |
| 1828 | } |
| 1829 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1830 | if (Py_TYPE(iter)->tp_as_async != NULL && |
| 1831 | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
| 1832 | |
| 1833 | /* Starting with CPython 3.5.2 __aiter__ should return |
| 1834 | asynchronous iterators directly (not awaitables that |
| 1835 | resolve to asynchronous iterators.) |
| 1836 | |
| 1837 | Therefore, we check if the object that was returned |
| 1838 | from __aiter__ has an __anext__ method. If it does, |
| 1839 | we wrap it in an awaitable that resolves to `iter`. |
| 1840 | |
| 1841 | See http://bugs.python.org/issue27243 for more |
| 1842 | details. |
| 1843 | */ |
| 1844 | |
| 1845 | PyObject *wrapper = _PyAIterWrapper_New(iter); |
| 1846 | Py_DECREF(iter); |
| 1847 | SET_TOP(wrapper); |
| 1848 | DISPATCH(); |
| 1849 | } |
| 1850 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1851 | awaitable = _PyCoro_GetAwaitableIter(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1852 | if (awaitable == NULL) { |
| 1853 | SET_TOP(NULL); |
| 1854 | PyErr_Format( |
| 1855 | PyExc_TypeError, |
| 1856 | "'async for' received an invalid object " |
| 1857 | "from __aiter__: %.100s", |
| 1858 | Py_TYPE(iter)->tp_name); |
| 1859 | |
| 1860 | Py_DECREF(iter); |
| 1861 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1862 | } else { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1863 | Py_DECREF(iter); |
| 1864 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1865 | if (PyErr_WarnFormat( |
Yury Selivanov | 2edd8a1 | 2016-11-08 15:13:07 -0500 | [diff] [blame] | 1866 | PyExc_DeprecationWarning, 1, |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1867 | "'%.100s' implements legacy __aiter__ protocol; " |
| 1868 | "__aiter__ should return an asynchronous " |
| 1869 | "iterator, not awaitable", |
| 1870 | type->tp_name)) |
| 1871 | { |
| 1872 | /* Warning was converted to an error. */ |
| 1873 | Py_DECREF(awaitable); |
| 1874 | SET_TOP(NULL); |
| 1875 | goto error; |
| 1876 | } |
| 1877 | } |
| 1878 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1879 | SET_TOP(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1880 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1881 | DISPATCH(); |
| 1882 | } |
| 1883 | |
| 1884 | TARGET(GET_ANEXT) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1885 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1886 | PyObject *next_iter = NULL; |
| 1887 | PyObject *awaitable = NULL; |
| 1888 | PyObject *aiter = TOP(); |
| 1889 | PyTypeObject *type = Py_TYPE(aiter); |
| 1890 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1891 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1892 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1893 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1894 | goto error; |
| 1895 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1896 | } else { |
| 1897 | if (type->tp_as_async != NULL){ |
| 1898 | getter = type->tp_as_async->am_anext; |
| 1899 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1900 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1901 | if (getter != NULL) { |
| 1902 | next_iter = (*getter)(aiter); |
| 1903 | if (next_iter == NULL) { |
| 1904 | goto error; |
| 1905 | } |
| 1906 | } |
| 1907 | else { |
| 1908 | PyErr_Format( |
| 1909 | PyExc_TypeError, |
| 1910 | "'async for' requires an iterator with " |
| 1911 | "__anext__ method, got %.100s", |
| 1912 | type->tp_name); |
| 1913 | goto error; |
| 1914 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1915 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1916 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1917 | if (awaitable == NULL) { |
| 1918 | PyErr_Format( |
| 1919 | PyExc_TypeError, |
| 1920 | "'async for' received an invalid object " |
| 1921 | "from __anext__: %.100s", |
| 1922 | Py_TYPE(next_iter)->tp_name); |
| 1923 | |
| 1924 | Py_DECREF(next_iter); |
| 1925 | goto error; |
| 1926 | } else { |
| 1927 | Py_DECREF(next_iter); |
| 1928 | } |
| 1929 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1930 | |
| 1931 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1932 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1933 | DISPATCH(); |
| 1934 | } |
| 1935 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1936 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1937 | TARGET(GET_AWAITABLE) { |
| 1938 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1939 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1940 | |
| 1941 | Py_DECREF(iterable); |
| 1942 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1943 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1944 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1945 | if (yf != NULL) { |
| 1946 | /* `iter` is a coroutine object that is being |
| 1947 | awaited, `yf` is a pointer to the current awaitable |
| 1948 | being awaited on. */ |
| 1949 | Py_DECREF(yf); |
| 1950 | Py_CLEAR(iter); |
| 1951 | PyErr_SetString( |
| 1952 | PyExc_RuntimeError, |
| 1953 | "coroutine is being awaited already"); |
| 1954 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 1955 | } |
| 1956 | } |
| 1957 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1958 | SET_TOP(iter); /* Even if it's NULL */ |
| 1959 | |
| 1960 | if (iter == NULL) { |
| 1961 | goto error; |
| 1962 | } |
| 1963 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1964 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1965 | DISPATCH(); |
| 1966 | } |
| 1967 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1968 | TARGET(YIELD_FROM) { |
| 1969 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1970 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1971 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1972 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 1973 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1974 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 1975 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1976 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1977 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1978 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1979 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1980 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1981 | Py_DECREF(v); |
| 1982 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1983 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1984 | if (tstate->c_tracefunc != NULL |
| 1985 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1986 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 1987 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1988 | if (err < 0) |
| 1989 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1990 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1991 | SET_TOP(val); |
| 1992 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1993 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1994 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1995 | f->f_stacktop = stack_pointer; |
| 1996 | why = WHY_YIELD; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1997 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 1998 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1999 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2000 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2001 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2002 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2003 | TARGET(YIELD_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2005 | |
| 2006 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2007 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2008 | Py_DECREF(retval); |
| 2009 | if (w == NULL) { |
| 2010 | retval = NULL; |
| 2011 | goto error; |
| 2012 | } |
| 2013 | retval = w; |
| 2014 | } |
| 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | f->f_stacktop = stack_pointer; |
| 2017 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2019 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2020 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2021 | TARGET(POP_EXCEPT) { |
| 2022 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2023 | if (b->b_type != EXCEPT_HANDLER) { |
| 2024 | PyErr_SetString(PyExc_SystemError, |
| 2025 | "popped block is not an except handler"); |
| 2026 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2028 | UNWIND_EXCEPT_HANDLER(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2030 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2031 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2032 | PREDICTED(POP_BLOCK); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2033 | TARGET(POP_BLOCK) { |
| 2034 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2035 | UNWIND_BLOCK(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2037 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2040 | TARGET(END_FINALLY) { |
| 2041 | PyObject *status = POP(); |
| 2042 | if (PyLong_Check(status)) { |
| 2043 | why = (enum why_code) PyLong_AS_LONG(status); |
| 2044 | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | if (why == WHY_RETURN || |
| 2046 | why == WHY_CONTINUE) |
| 2047 | retval = POP(); |
| 2048 | if (why == WHY_SILENCED) { |
| 2049 | /* An exception was silenced by 'with', we must |
| 2050 | manually unwind the EXCEPT_HANDLER block which was |
| 2051 | created when the exception was caught, otherwise |
| 2052 | the stack will be in an inconsistent state. */ |
| 2053 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2054 | assert(b->b_type == EXCEPT_HANDLER); |
| 2055 | UNWIND_EXCEPT_HANDLER(b); |
| 2056 | why = WHY_NOT; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2057 | Py_DECREF(status); |
| 2058 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2060 | Py_DECREF(status); |
| 2061 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2063 | else if (PyExceptionClass_Check(status)) { |
| 2064 | PyObject *exc = POP(); |
| 2065 | PyObject *tb = POP(); |
| 2066 | PyErr_Restore(status, exc, tb); |
| 2067 | why = WHY_EXCEPTION; |
| 2068 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2070 | else if (status != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | PyErr_SetString(PyExc_SystemError, |
| 2072 | "'finally' pops bad exception"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2073 | Py_DECREF(status); |
| 2074 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2076 | Py_DECREF(status); |
| 2077 | DISPATCH(); |
| 2078 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2079 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2080 | TARGET(LOAD_BUILD_CLASS) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2081 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2082 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2083 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2084 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2085 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2086 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2087 | PyErr_SetString(PyExc_NameError, |
| 2088 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2089 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2090 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2091 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2092 | } |
| 2093 | else { |
| 2094 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2095 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2096 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2097 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2098 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2099 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2100 | PyErr_SetString(PyExc_NameError, |
| 2101 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2102 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2103 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2105 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2106 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2107 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2108 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2109 | TARGET(STORE_NAME) { |
| 2110 | PyObject *name = GETITEM(names, oparg); |
| 2111 | PyObject *v = POP(); |
| 2112 | PyObject *ns = f->f_locals; |
| 2113 | int err; |
| 2114 | if (ns == NULL) { |
| 2115 | PyErr_Format(PyExc_SystemError, |
| 2116 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2118 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2120 | if (PyDict_CheckExact(ns)) |
| 2121 | err = PyDict_SetItem(ns, name, v); |
| 2122 | else |
| 2123 | err = PyObject_SetItem(ns, name, v); |
| 2124 | Py_DECREF(v); |
| 2125 | if (err != 0) |
| 2126 | goto error; |
| 2127 | DISPATCH(); |
| 2128 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2129 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2130 | TARGET(DELETE_NAME) { |
| 2131 | PyObject *name = GETITEM(names, oparg); |
| 2132 | PyObject *ns = f->f_locals; |
| 2133 | int err; |
| 2134 | if (ns == NULL) { |
| 2135 | PyErr_Format(PyExc_SystemError, |
| 2136 | "no locals when deleting %R", name); |
| 2137 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2138 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2139 | err = PyObject_DelItem(ns, name); |
| 2140 | if (err != 0) { |
| 2141 | format_exc_check_arg(PyExc_NameError, |
| 2142 | NAME_ERROR_MSG, |
| 2143 | name); |
| 2144 | goto error; |
| 2145 | } |
| 2146 | DISPATCH(); |
| 2147 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2148 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2149 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2150 | TARGET(UNPACK_SEQUENCE) { |
| 2151 | PyObject *seq = POP(), *item, **items; |
| 2152 | if (PyTuple_CheckExact(seq) && |
| 2153 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2154 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2156 | item = items[oparg]; |
| 2157 | Py_INCREF(item); |
| 2158 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2160 | } else if (PyList_CheckExact(seq) && |
| 2161 | PyList_GET_SIZE(seq) == oparg) { |
| 2162 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2163 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2164 | item = items[oparg]; |
| 2165 | Py_INCREF(item); |
| 2166 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2168 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | stack_pointer + oparg)) { |
| 2170 | STACKADJ(oparg); |
| 2171 | } else { |
| 2172 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2173 | Py_DECREF(seq); |
| 2174 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2176 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2177 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2179 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2180 | TARGET(UNPACK_EX) { |
| 2181 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2182 | PyObject *seq = POP(); |
| 2183 | |
| 2184 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2185 | stack_pointer + totalargs)) { |
| 2186 | stack_pointer += totalargs; |
| 2187 | } else { |
| 2188 | Py_DECREF(seq); |
| 2189 | goto error; |
| 2190 | } |
| 2191 | Py_DECREF(seq); |
| 2192 | DISPATCH(); |
| 2193 | } |
| 2194 | |
| 2195 | TARGET(STORE_ATTR) { |
| 2196 | PyObject *name = GETITEM(names, oparg); |
| 2197 | PyObject *owner = TOP(); |
| 2198 | PyObject *v = SECOND(); |
| 2199 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2200 | STACKADJ(-2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2201 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2203 | Py_DECREF(owner); |
| 2204 | if (err != 0) |
| 2205 | goto error; |
| 2206 | DISPATCH(); |
| 2207 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2208 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2209 | TARGET(DELETE_ATTR) { |
| 2210 | PyObject *name = GETITEM(names, oparg); |
| 2211 | PyObject *owner = POP(); |
| 2212 | int err; |
| 2213 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2214 | Py_DECREF(owner); |
| 2215 | if (err != 0) |
| 2216 | goto error; |
| 2217 | DISPATCH(); |
| 2218 | } |
| 2219 | |
| 2220 | TARGET(STORE_GLOBAL) { |
| 2221 | PyObject *name = GETITEM(names, oparg); |
| 2222 | PyObject *v = POP(); |
| 2223 | int err; |
| 2224 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2226 | if (err != 0) |
| 2227 | goto error; |
| 2228 | DISPATCH(); |
| 2229 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2230 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2231 | TARGET(DELETE_GLOBAL) { |
| 2232 | PyObject *name = GETITEM(names, oparg); |
| 2233 | int err; |
| 2234 | err = PyDict_DelItem(f->f_globals, name); |
| 2235 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2237 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2238 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2239 | } |
| 2240 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2241 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2242 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2243 | TARGET(LOAD_NAME) { |
| 2244 | PyObject *name = GETITEM(names, oparg); |
| 2245 | PyObject *locals = f->f_locals; |
| 2246 | PyObject *v; |
| 2247 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2249 | "no locals when loading %R", name); |
| 2250 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2252 | if (PyDict_CheckExact(locals)) { |
| 2253 | v = PyDict_GetItem(locals, name); |
| 2254 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | } |
| 2256 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2257 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2258 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2259 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2260 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | PyErr_Clear(); |
| 2262 | } |
| 2263 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2264 | if (v == NULL) { |
| 2265 | v = PyDict_GetItem(f->f_globals, name); |
| 2266 | Py_XINCREF(v); |
| 2267 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2268 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2269 | v = PyDict_GetItem(f->f_builtins, name); |
| 2270 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2271 | format_exc_check_arg( |
| 2272 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | NAME_ERROR_MSG, name); |
| 2274 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2275 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2276 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2277 | } |
| 2278 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2279 | v = PyObject_GetItem(f->f_builtins, name); |
| 2280 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2281 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2282 | format_exc_check_arg( |
| 2283 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2284 | NAME_ERROR_MSG, name); |
| 2285 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2286 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2287 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2290 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2292 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2293 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2294 | TARGET(LOAD_GLOBAL) { |
| 2295 | PyObject *name = GETITEM(names, oparg); |
| 2296 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2297 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2298 | && PyDict_CheckExact(f->f_builtins)) |
| 2299 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2300 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2301 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2302 | name); |
| 2303 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2304 | if (!_PyErr_OCCURRED()) { |
| 2305 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2306 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2307 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2308 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2309 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2310 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2312 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2314 | else { |
| 2315 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2316 | |
| 2317 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2318 | v = PyObject_GetItem(f->f_globals, name); |
| 2319 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2320 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2321 | goto error; |
| 2322 | PyErr_Clear(); |
| 2323 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2324 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2325 | v = PyObject_GetItem(f->f_builtins, name); |
| 2326 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2327 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2328 | format_exc_check_arg( |
| 2329 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2330 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2331 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2335 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2337 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2338 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2339 | TARGET(DELETE_FAST) { |
| 2340 | PyObject *v = GETLOCAL(oparg); |
| 2341 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | SETLOCAL(oparg, NULL); |
| 2343 | DISPATCH(); |
| 2344 | } |
| 2345 | format_exc_check_arg( |
| 2346 | PyExc_UnboundLocalError, |
| 2347 | UNBOUNDLOCAL_ERROR_MSG, |
| 2348 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2349 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2350 | goto error; |
| 2351 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2352 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2353 | TARGET(DELETE_DEREF) { |
| 2354 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2355 | PyObject *oldobj = PyCell_GET(cell); |
| 2356 | if (oldobj != NULL) { |
| 2357 | PyCell_SET(cell, NULL); |
| 2358 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2359 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2360 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2361 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2362 | goto error; |
| 2363 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2364 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2365 | TARGET(LOAD_CLOSURE) { |
| 2366 | PyObject *cell = freevars[oparg]; |
| 2367 | Py_INCREF(cell); |
| 2368 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2370 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2371 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2372 | TARGET(LOAD_CLASSDEREF) { |
| 2373 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2374 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2375 | assert(locals); |
| 2376 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2377 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2378 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2379 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2380 | if (PyDict_CheckExact(locals)) { |
| 2381 | value = PyDict_GetItem(locals, name); |
| 2382 | Py_XINCREF(value); |
| 2383 | } |
| 2384 | else { |
| 2385 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2386 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2387 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2388 | goto error; |
| 2389 | PyErr_Clear(); |
| 2390 | } |
| 2391 | } |
| 2392 | if (!value) { |
| 2393 | PyObject *cell = freevars[oparg]; |
| 2394 | value = PyCell_GET(cell); |
| 2395 | if (value == NULL) { |
| 2396 | format_exc_unbound(co, oparg); |
| 2397 | goto error; |
| 2398 | } |
| 2399 | Py_INCREF(value); |
| 2400 | } |
| 2401 | PUSH(value); |
| 2402 | DISPATCH(); |
| 2403 | } |
| 2404 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2405 | TARGET(LOAD_DEREF) { |
| 2406 | PyObject *cell = freevars[oparg]; |
| 2407 | PyObject *value = PyCell_GET(cell); |
| 2408 | if (value == NULL) { |
| 2409 | format_exc_unbound(co, oparg); |
| 2410 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2412 | Py_INCREF(value); |
| 2413 | PUSH(value); |
| 2414 | DISPATCH(); |
| 2415 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2416 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2417 | TARGET(STORE_DEREF) { |
| 2418 | PyObject *v = POP(); |
| 2419 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2420 | PyObject *oldobj = PyCell_GET(cell); |
| 2421 | PyCell_SET(cell, v); |
| 2422 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2423 | DISPATCH(); |
| 2424 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2425 | |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2426 | TARGET(BUILD_STRING) { |
| 2427 | PyObject *str; |
| 2428 | PyObject *empty = PyUnicode_New(0, 0); |
| 2429 | if (empty == NULL) { |
| 2430 | goto error; |
| 2431 | } |
| 2432 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2433 | Py_DECREF(empty); |
| 2434 | if (str == NULL) |
| 2435 | goto error; |
| 2436 | while (--oparg >= 0) { |
| 2437 | PyObject *item = POP(); |
| 2438 | Py_DECREF(item); |
| 2439 | } |
| 2440 | PUSH(str); |
| 2441 | DISPATCH(); |
| 2442 | } |
| 2443 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2444 | TARGET(BUILD_TUPLE) { |
| 2445 | PyObject *tup = PyTuple_New(oparg); |
| 2446 | if (tup == NULL) |
| 2447 | goto error; |
| 2448 | while (--oparg >= 0) { |
| 2449 | PyObject *item = POP(); |
| 2450 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2451 | } |
| 2452 | PUSH(tup); |
| 2453 | DISPATCH(); |
| 2454 | } |
| 2455 | |
| 2456 | TARGET(BUILD_LIST) { |
| 2457 | PyObject *list = PyList_New(oparg); |
| 2458 | if (list == NULL) |
| 2459 | goto error; |
| 2460 | while (--oparg >= 0) { |
| 2461 | PyObject *item = POP(); |
| 2462 | PyList_SET_ITEM(list, oparg, item); |
| 2463 | } |
| 2464 | PUSH(list); |
| 2465 | DISPATCH(); |
| 2466 | } |
| 2467 | |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2468 | TARGET(BUILD_TUPLE_UNPACK_WITH_CALL) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2469 | TARGET(BUILD_TUPLE_UNPACK) |
| 2470 | TARGET(BUILD_LIST_UNPACK) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2471 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2472 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2473 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2474 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2475 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2476 | if (sum == NULL) |
| 2477 | goto error; |
| 2478 | |
| 2479 | for (i = oparg; i > 0; i--) { |
| 2480 | PyObject *none_val; |
| 2481 | |
| 2482 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2483 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2484 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
| 2485 | PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 2486 | PyObject *func = PEEK(1 + oparg); |
| 2487 | PyErr_Format(PyExc_TypeError, |
| 2488 | "%.200s%.200s argument after * " |
| 2489 | "must be an iterable, not %.200s", |
| 2490 | PyEval_GetFuncName(func), |
| 2491 | PyEval_GetFuncDesc(func), |
| 2492 | PEEK(i)->ob_type->tp_name); |
| 2493 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2494 | Py_DECREF(sum); |
| 2495 | goto error; |
| 2496 | } |
| 2497 | Py_DECREF(none_val); |
| 2498 | } |
| 2499 | |
| 2500 | if (convert_to_tuple) { |
| 2501 | return_value = PyList_AsTuple(sum); |
| 2502 | Py_DECREF(sum); |
| 2503 | if (return_value == NULL) |
| 2504 | goto error; |
| 2505 | } |
| 2506 | else { |
| 2507 | return_value = sum; |
| 2508 | } |
| 2509 | |
| 2510 | while (oparg--) |
| 2511 | Py_DECREF(POP()); |
| 2512 | PUSH(return_value); |
| 2513 | DISPATCH(); |
| 2514 | } |
| 2515 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2516 | TARGET(BUILD_SET) { |
| 2517 | PyObject *set = PySet_New(NULL); |
| 2518 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2519 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2520 | if (set == NULL) |
| 2521 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2522 | for (i = oparg; i > 0; i--) { |
| 2523 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2524 | if (err == 0) |
| 2525 | err = PySet_Add(set, item); |
| 2526 | Py_DECREF(item); |
| 2527 | } |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2528 | STACKADJ(-oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2529 | if (err != 0) { |
| 2530 | Py_DECREF(set); |
| 2531 | goto error; |
| 2532 | } |
| 2533 | PUSH(set); |
| 2534 | DISPATCH(); |
| 2535 | } |
| 2536 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2537 | TARGET(BUILD_SET_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2538 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2539 | PyObject *sum = PySet_New(NULL); |
| 2540 | if (sum == NULL) |
| 2541 | goto error; |
| 2542 | |
| 2543 | for (i = oparg; i > 0; i--) { |
| 2544 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2545 | Py_DECREF(sum); |
| 2546 | goto error; |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | while (oparg--) |
| 2551 | Py_DECREF(POP()); |
| 2552 | PUSH(sum); |
| 2553 | DISPATCH(); |
| 2554 | } |
| 2555 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2556 | TARGET(BUILD_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2557 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2558 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2559 | if (map == NULL) |
| 2560 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2561 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2562 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2563 | PyObject *key = PEEK(2*i); |
| 2564 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2565 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2566 | if (err != 0) { |
| 2567 | Py_DECREF(map); |
| 2568 | goto error; |
| 2569 | } |
| 2570 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2571 | |
| 2572 | while (oparg--) { |
| 2573 | Py_DECREF(POP()); |
| 2574 | Py_DECREF(POP()); |
| 2575 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2576 | PUSH(map); |
| 2577 | DISPATCH(); |
| 2578 | } |
| 2579 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2580 | TARGET(SETUP_ANNOTATIONS) { |
| 2581 | _Py_IDENTIFIER(__annotations__); |
| 2582 | int err; |
| 2583 | PyObject *ann_dict; |
| 2584 | if (f->f_locals == NULL) { |
| 2585 | PyErr_Format(PyExc_SystemError, |
| 2586 | "no locals found when setting up annotations"); |
| 2587 | goto error; |
| 2588 | } |
| 2589 | /* check if __annotations__ in locals()... */ |
| 2590 | if (PyDict_CheckExact(f->f_locals)) { |
| 2591 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2592 | &PyId___annotations__); |
| 2593 | if (ann_dict == NULL) { |
| 2594 | /* ...if not, create a new one */ |
| 2595 | ann_dict = PyDict_New(); |
| 2596 | if (ann_dict == NULL) { |
| 2597 | goto error; |
| 2598 | } |
| 2599 | err = _PyDict_SetItemId(f->f_locals, |
| 2600 | &PyId___annotations__, ann_dict); |
| 2601 | Py_DECREF(ann_dict); |
| 2602 | if (err != 0) { |
| 2603 | goto error; |
| 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | else { |
| 2608 | /* do the same if locals() is not a dict */ |
| 2609 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2610 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2611 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2612 | } |
| 2613 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2614 | if (ann_dict == NULL) { |
| 2615 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2616 | goto error; |
| 2617 | } |
| 2618 | PyErr_Clear(); |
| 2619 | ann_dict = PyDict_New(); |
| 2620 | if (ann_dict == NULL) { |
| 2621 | goto error; |
| 2622 | } |
| 2623 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2624 | Py_DECREF(ann_dict); |
| 2625 | if (err != 0) { |
| 2626 | goto error; |
| 2627 | } |
| 2628 | } |
| 2629 | else { |
| 2630 | Py_DECREF(ann_dict); |
| 2631 | } |
| 2632 | } |
| 2633 | DISPATCH(); |
| 2634 | } |
| 2635 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2636 | TARGET(BUILD_CONST_KEY_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2637 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2638 | PyObject *map; |
| 2639 | PyObject *keys = TOP(); |
| 2640 | if (!PyTuple_CheckExact(keys) || |
| 2641 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2642 | PyErr_SetString(PyExc_SystemError, |
| 2643 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2644 | goto error; |
| 2645 | } |
| 2646 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2647 | if (map == NULL) { |
| 2648 | goto error; |
| 2649 | } |
| 2650 | for (i = oparg; i > 0; i--) { |
| 2651 | int err; |
| 2652 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2653 | PyObject *value = PEEK(i + 1); |
| 2654 | err = PyDict_SetItem(map, key, value); |
| 2655 | if (err != 0) { |
| 2656 | Py_DECREF(map); |
| 2657 | goto error; |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | Py_DECREF(POP()); |
| 2662 | while (oparg--) { |
| 2663 | Py_DECREF(POP()); |
| 2664 | } |
| 2665 | PUSH(map); |
| 2666 | DISPATCH(); |
| 2667 | } |
| 2668 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2669 | TARGET(BUILD_MAP_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2670 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2671 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2672 | if (sum == NULL) |
| 2673 | goto error; |
| 2674 | |
| 2675 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2676 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2677 | if (PyDict_Update(sum, arg) < 0) { |
| 2678 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2679 | PyErr_Format(PyExc_TypeError, |
Berker Peksag | 8e9045d | 2016-10-02 13:08:25 +0300 | [diff] [blame] | 2680 | "'%.200s' object is not a mapping", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2681 | arg->ob_type->tp_name); |
| 2682 | } |
| 2683 | Py_DECREF(sum); |
| 2684 | goto error; |
| 2685 | } |
| 2686 | } |
| 2687 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2688 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2689 | Py_DECREF(POP()); |
| 2690 | PUSH(sum); |
| 2691 | DISPATCH(); |
| 2692 | } |
| 2693 | |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2694 | TARGET(BUILD_MAP_UNPACK_WITH_CALL) { |
| 2695 | Py_ssize_t i; |
| 2696 | PyObject *sum = PyDict_New(); |
| 2697 | if (sum == NULL) |
| 2698 | goto error; |
| 2699 | |
| 2700 | for (i = oparg; i > 0; i--) { |
| 2701 | PyObject *arg = PEEK(i); |
| 2702 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
| 2703 | PyObject *func = PEEK(2 + oparg); |
| 2704 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2705 | PyErr_Format(PyExc_TypeError, |
| 2706 | "%.200s%.200s argument after ** " |
| 2707 | "must be a mapping, not %.200s", |
| 2708 | PyEval_GetFuncName(func), |
| 2709 | PyEval_GetFuncDesc(func), |
| 2710 | arg->ob_type->tp_name); |
| 2711 | } |
| 2712 | else if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2713 | PyObject *exc, *val, *tb; |
| 2714 | PyErr_Fetch(&exc, &val, &tb); |
| 2715 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 2716 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 2717 | if (!PyUnicode_Check(key)) { |
| 2718 | PyErr_Format(PyExc_TypeError, |
| 2719 | "%.200s%.200s keywords must be strings", |
| 2720 | PyEval_GetFuncName(func), |
| 2721 | PyEval_GetFuncDesc(func)); |
| 2722 | } else { |
| 2723 | PyErr_Format(PyExc_TypeError, |
| 2724 | "%.200s%.200s got multiple " |
| 2725 | "values for keyword argument '%U'", |
| 2726 | PyEval_GetFuncName(func), |
| 2727 | PyEval_GetFuncDesc(func), |
| 2728 | key); |
| 2729 | } |
| 2730 | Py_XDECREF(exc); |
| 2731 | Py_XDECREF(val); |
| 2732 | Py_XDECREF(tb); |
| 2733 | } |
| 2734 | else { |
| 2735 | PyErr_Restore(exc, val, tb); |
| 2736 | } |
| 2737 | } |
| 2738 | Py_DECREF(sum); |
| 2739 | goto error; |
| 2740 | } |
| 2741 | } |
| 2742 | |
| 2743 | while (oparg--) |
| 2744 | Py_DECREF(POP()); |
| 2745 | PUSH(sum); |
| 2746 | DISPATCH(); |
| 2747 | } |
| 2748 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2749 | TARGET(MAP_ADD) { |
| 2750 | PyObject *key = TOP(); |
| 2751 | PyObject *value = SECOND(); |
| 2752 | PyObject *map; |
| 2753 | int err; |
| 2754 | STACKADJ(-2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2755 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2756 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2757 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2758 | Py_DECREF(value); |
| 2759 | Py_DECREF(key); |
| 2760 | if (err != 0) |
| 2761 | goto error; |
| 2762 | PREDICT(JUMP_ABSOLUTE); |
| 2763 | DISPATCH(); |
| 2764 | } |
| 2765 | |
| 2766 | TARGET(LOAD_ATTR) { |
| 2767 | PyObject *name = GETITEM(names, oparg); |
| 2768 | PyObject *owner = TOP(); |
| 2769 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2770 | Py_DECREF(owner); |
| 2771 | SET_TOP(res); |
| 2772 | if (res == NULL) |
| 2773 | goto error; |
| 2774 | DISPATCH(); |
| 2775 | } |
| 2776 | |
| 2777 | TARGET(COMPARE_OP) { |
| 2778 | PyObject *right = POP(); |
| 2779 | PyObject *left = TOP(); |
| 2780 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2781 | Py_DECREF(left); |
| 2782 | Py_DECREF(right); |
| 2783 | SET_TOP(res); |
| 2784 | if (res == NULL) |
| 2785 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | PREDICT(POP_JUMP_IF_FALSE); |
| 2787 | PREDICT(POP_JUMP_IF_TRUE); |
| 2788 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2789 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2790 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2791 | TARGET(IMPORT_NAME) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2792 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2793 | PyObject *fromlist = POP(); |
| 2794 | PyObject *level = TOP(); |
| 2795 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2796 | res = import_name(f, name, fromlist, level); |
| 2797 | Py_DECREF(level); |
| 2798 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2799 | SET_TOP(res); |
| 2800 | if (res == NULL) |
| 2801 | goto error; |
| 2802 | DISPATCH(); |
| 2803 | } |
| 2804 | |
| 2805 | TARGET(IMPORT_STAR) { |
| 2806 | PyObject *from = POP(), *locals; |
| 2807 | int err; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2808 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 2809 | goto error; |
| 2810 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2811 | locals = f->f_locals; |
| 2812 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | PyErr_SetString(PyExc_SystemError, |
| 2814 | "no locals found during 'import *'"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2815 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2817 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2819 | Py_DECREF(from); |
| 2820 | if (err != 0) |
| 2821 | goto error; |
| 2822 | DISPATCH(); |
| 2823 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2824 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2825 | TARGET(IMPORT_FROM) { |
| 2826 | PyObject *name = GETITEM(names, oparg); |
| 2827 | PyObject *from = TOP(); |
| 2828 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2829 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2830 | PUSH(res); |
| 2831 | if (res == NULL) |
| 2832 | goto error; |
| 2833 | DISPATCH(); |
| 2834 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2835 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2836 | TARGET(JUMP_FORWARD) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | JUMPBY(oparg); |
| 2838 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2839 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2840 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2841 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2842 | TARGET(POP_JUMP_IF_FALSE) { |
| 2843 | PyObject *cond = POP(); |
| 2844 | int err; |
| 2845 | if (cond == Py_True) { |
| 2846 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | FAST_DISPATCH(); |
| 2848 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2849 | if (cond == Py_False) { |
| 2850 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | JUMPTO(oparg); |
| 2852 | FAST_DISPATCH(); |
| 2853 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2854 | err = PyObject_IsTrue(cond); |
| 2855 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | if (err > 0) |
| 2857 | err = 0; |
| 2858 | else if (err == 0) |
| 2859 | JUMPTO(oparg); |
| 2860 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2861 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2863 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2864 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2865 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2866 | TARGET(POP_JUMP_IF_TRUE) { |
| 2867 | PyObject *cond = POP(); |
| 2868 | int err; |
| 2869 | if (cond == Py_False) { |
| 2870 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | FAST_DISPATCH(); |
| 2872 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2873 | if (cond == Py_True) { |
| 2874 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | JUMPTO(oparg); |
| 2876 | FAST_DISPATCH(); |
| 2877 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2878 | err = PyObject_IsTrue(cond); |
| 2879 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | if (err > 0) { |
| 2881 | err = 0; |
| 2882 | JUMPTO(oparg); |
| 2883 | } |
| 2884 | else if (err == 0) |
| 2885 | ; |
| 2886 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2887 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2889 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2890 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2891 | TARGET(JUMP_IF_FALSE_OR_POP) { |
| 2892 | PyObject *cond = TOP(); |
| 2893 | int err; |
| 2894 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2896 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | FAST_DISPATCH(); |
| 2898 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2899 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | JUMPTO(oparg); |
| 2901 | FAST_DISPATCH(); |
| 2902 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2903 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | if (err > 0) { |
| 2905 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2906 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | err = 0; |
| 2908 | } |
| 2909 | else if (err == 0) |
| 2910 | JUMPTO(oparg); |
| 2911 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2912 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2914 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2915 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2916 | TARGET(JUMP_IF_TRUE_OR_POP) { |
| 2917 | PyObject *cond = TOP(); |
| 2918 | int err; |
| 2919 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2921 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | FAST_DISPATCH(); |
| 2923 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2924 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | JUMPTO(oparg); |
| 2926 | FAST_DISPATCH(); |
| 2927 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2928 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2929 | if (err > 0) { |
| 2930 | err = 0; |
| 2931 | JUMPTO(oparg); |
| 2932 | } |
| 2933 | else if (err == 0) { |
| 2934 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2935 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | } |
| 2937 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2938 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2940 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2941 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2942 | PREDICTED(JUMP_ABSOLUTE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2943 | TARGET(JUMP_ABSOLUTE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2945 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2947 | the per-loop checks for signals. By default, this should be turned-off |
| 2948 | because it prevents detection of a control-break in tight loops like |
| 2949 | "while 1: pass". Compile with this option turned-on when you need |
| 2950 | the speed-up and do not need break checking inside tight loops (ones |
| 2951 | that contain only instructions ending with FAST_DISPATCH). |
| 2952 | */ |
| 2953 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2954 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2956 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2957 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2958 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2959 | TARGET(GET_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2960 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2961 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2962 | PyObject *iter = PyObject_GetIter(iterable); |
| 2963 | Py_DECREF(iterable); |
| 2964 | SET_TOP(iter); |
| 2965 | if (iter == NULL) |
| 2966 | goto error; |
| 2967 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2968 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2969 | DISPATCH(); |
| 2970 | } |
| 2971 | |
| 2972 | TARGET(GET_YIELD_FROM_ITER) { |
| 2973 | /* before: [obj]; after [getiter(obj)] */ |
| 2974 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2975 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2976 | if (PyCoro_CheckExact(iterable)) { |
| 2977 | /* `iterable` is a coroutine */ |
| 2978 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 2979 | /* and it is used in a 'yield from' expression of a |
| 2980 | regular generator. */ |
| 2981 | Py_DECREF(iterable); |
| 2982 | SET_TOP(NULL); |
| 2983 | PyErr_SetString(PyExc_TypeError, |
| 2984 | "cannot 'yield from' a coroutine object " |
| 2985 | "in a non-coroutine generator"); |
| 2986 | goto error; |
| 2987 | } |
| 2988 | } |
| 2989 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2990 | /* `iterable` is not a generator. */ |
| 2991 | iter = PyObject_GetIter(iterable); |
| 2992 | Py_DECREF(iterable); |
| 2993 | SET_TOP(iter); |
| 2994 | if (iter == NULL) |
| 2995 | goto error; |
| 2996 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2997 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2998 | DISPATCH(); |
| 2999 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3000 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3001 | PREDICTED(FOR_ITER); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3002 | TARGET(FOR_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3003 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3004 | PyObject *iter = TOP(); |
| 3005 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3006 | if (next != NULL) { |
| 3007 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | PREDICT(STORE_FAST); |
| 3009 | PREDICT(UNPACK_SEQUENCE); |
| 3010 | DISPATCH(); |
| 3011 | } |
| 3012 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3013 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3014 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 3015 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3016 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | PyErr_Clear(); |
| 3018 | } |
| 3019 | /* iterator ended normally */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3020 | STACKADJ(-1); |
| 3021 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3023 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3025 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3026 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3027 | TARGET(BREAK_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | why = WHY_BREAK; |
| 3029 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3030 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3031 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3032 | TARGET(CONTINUE_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | retval = PyLong_FromLong(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3034 | if (retval == NULL) |
| 3035 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | why = WHY_CONTINUE; |
| 3037 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3038 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3039 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3040 | TARGET(SETUP_LOOP) |
| 3041 | TARGET(SETUP_EXCEPT) |
| 3042 | TARGET(SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | /* NOTE: If you add any new block-setup opcodes that |
| 3044 | are not try/except/finally handlers, you may need |
| 3045 | to update the PyGen_NeedsFinalizing() function. |
| 3046 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 3049 | STACK_LEVEL()); |
| 3050 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3051 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3052 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3053 | TARGET(BEFORE_ASYNC_WITH) { |
| 3054 | _Py_IDENTIFIER(__aexit__); |
| 3055 | _Py_IDENTIFIER(__aenter__); |
| 3056 | |
| 3057 | PyObject *mgr = TOP(); |
| 3058 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 3059 | *enter; |
| 3060 | PyObject *res; |
| 3061 | if (exit == NULL) |
| 3062 | goto error; |
| 3063 | SET_TOP(exit); |
| 3064 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3065 | Py_DECREF(mgr); |
| 3066 | if (enter == NULL) |
| 3067 | goto error; |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3068 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3069 | Py_DECREF(enter); |
| 3070 | if (res == NULL) |
| 3071 | goto error; |
| 3072 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3073 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3074 | DISPATCH(); |
| 3075 | } |
| 3076 | |
| 3077 | TARGET(SETUP_ASYNC_WITH) { |
| 3078 | PyObject *res = POP(); |
| 3079 | /* Setup the finally block before pushing the result |
| 3080 | of __aenter__ on the stack. */ |
| 3081 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3082 | STACK_LEVEL()); |
| 3083 | PUSH(res); |
| 3084 | DISPATCH(); |
| 3085 | } |
| 3086 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3087 | TARGET(SETUP_WITH) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3088 | _Py_IDENTIFIER(__exit__); |
| 3089 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3090 | PyObject *mgr = TOP(); |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3091 | PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3092 | PyObject *res; |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3093 | if (enter == NULL) |
| 3094 | goto error; |
| 3095 | exit = special_lookup(mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3096 | if (exit == NULL) { |
| 3097 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3098 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3099 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3100 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3101 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3102 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3103 | Py_DECREF(enter); |
| 3104 | if (res == NULL) |
| 3105 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 | /* Setup the finally block before pushing the result |
| 3107 | of __enter__ on the stack. */ |
| 3108 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3109 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3110 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3111 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | DISPATCH(); |
| 3113 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3114 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3115 | TARGET(WITH_CLEANUP_START) { |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3116 | /* At the top of the stack are 1-6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | how/why we entered the finally clause: |
| 3118 | - TOP = None |
| 3119 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 3120 | - TOP = WHY_*; no retval below it |
| 3121 | - (TOP, SECOND, THIRD) = exc_info() |
| 3122 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3123 | Below them is EXIT, the context.__exit__ bound method. |
| 3124 | In the last case, we must call |
| 3125 | EXIT(TOP, SECOND, THIRD) |
| 3126 | otherwise we must call |
| 3127 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3128 | |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3129 | In the first three cases, we remove EXIT from the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3130 | stack, leaving the rest in the same order. In the |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3131 | fourth case, we shift the bottom 3 values of the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | In addition, if the stack represents an exception, |
| 3135 | *and* the function call returns a 'true' value, we |
| 3136 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 3137 | then not re-raise the exception. (But non-local |
| 3138 | gotos should still be resumed.) |
| 3139 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3140 | |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3141 | PyObject* stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3143 | PyObject *exc, *val, *tb, *res; |
| 3144 | |
| 3145 | val = tb = Py_None; |
| 3146 | exc = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3147 | if (exc == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | (void)POP(); |
| 3149 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3150 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3152 | else if (PyLong_Check(exc)) { |
| 3153 | STACKADJ(-1); |
| 3154 | switch (PyLong_AsLong(exc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | case WHY_RETURN: |
| 3156 | case WHY_CONTINUE: |
| 3157 | /* Retval in TOP. */ |
| 3158 | exit_func = SECOND(); |
| 3159 | SET_SECOND(TOP()); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3160 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | break; |
| 3162 | default: |
| 3163 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3164 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | break; |
| 3166 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3167 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | } |
| 3169 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3170 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3172 | val = SECOND(); |
| 3173 | tb = THIRD(); |
| 3174 | tp2 = FOURTH(); |
| 3175 | exc2 = PEEK(5); |
| 3176 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3178 | SET_VALUE(7, tb2); |
| 3179 | SET_VALUE(6, exc2); |
| 3180 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3182 | SET_FOURTH(NULL); |
| 3183 | /* We just shifted the stack down, so we have |
| 3184 | to tell the except handler block that the |
| 3185 | values are lower than it expects. */ |
| 3186 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3187 | assert(block->b_type == EXCEPT_HANDLER); |
| 3188 | block->b_level--; |
| 3189 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3190 | |
| 3191 | stack[0] = exc; |
| 3192 | stack[1] = val; |
| 3193 | stack[2] = tb; |
| 3194 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3196 | if (res == NULL) |
| 3197 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3198 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3199 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3200 | PUSH(exc); |
| 3201 | PUSH(res); |
| 3202 | PREDICT(WITH_CLEANUP_FINISH); |
| 3203 | DISPATCH(); |
| 3204 | } |
| 3205 | |
| 3206 | PREDICTED(WITH_CLEANUP_FINISH); |
| 3207 | TARGET(WITH_CLEANUP_FINISH) { |
| 3208 | PyObject *res = POP(); |
| 3209 | PyObject *exc = POP(); |
| 3210 | int err; |
| 3211 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3212 | if (exc != Py_None) |
| 3213 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | else |
| 3215 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3216 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3217 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3218 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3221 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | else if (err > 0) { |
| 3223 | err = 0; |
| 3224 | /* There was an exception and a True return */ |
| 3225 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 3226 | } |
| 3227 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3228 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3230 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3231 | TARGET(LOAD_METHOD) { |
| 3232 | /* Designed to work in tamdem with CALL_METHOD. */ |
| 3233 | PyObject *name = GETITEM(names, oparg); |
| 3234 | PyObject *obj = TOP(); |
| 3235 | PyObject *meth = NULL; |
| 3236 | |
| 3237 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3238 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3239 | if (meth == NULL) { |
| 3240 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3241 | goto error; |
| 3242 | } |
| 3243 | |
| 3244 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3245 | /* We can bypass temporary bound method object. |
| 3246 | meth is unbound method and obj is self. |
| 3247 | |
| 3248 | meth | self | arg1 | ... | argN |
| 3249 | */ |
| 3250 | SET_TOP(meth); |
| 3251 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3252 | } |
| 3253 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3254 | /* meth is not an unbound method (but a regular attr, or |
| 3255 | something was returned by a descriptor protocol). Set |
| 3256 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3257 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3258 | |
| 3259 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3260 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3261 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3262 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3263 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3264 | } |
| 3265 | DISPATCH(); |
| 3266 | } |
| 3267 | |
| 3268 | TARGET(CALL_METHOD) { |
| 3269 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3270 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3271 | |
| 3272 | sp = stack_pointer; |
| 3273 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3274 | meth = PEEK(oparg + 2); |
| 3275 | if (meth == NULL) { |
| 3276 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3277 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3278 | |
| 3279 | Stack layout: |
| 3280 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3281 | ... | NULL | callable | arg1 | ... | argN |
| 3282 | ^- TOP() |
| 3283 | ^- (-oparg) |
| 3284 | ^- (-oparg-1) |
| 3285 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3286 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3287 | `callable` will be POPed by call_funtion. |
| 3288 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3289 | */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3290 | res = call_function(&sp, oparg, NULL); |
| 3291 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3292 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3293 | } |
| 3294 | else { |
| 3295 | /* This is a method call. Stack layout: |
| 3296 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3297 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3298 | ^- TOP() |
| 3299 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3300 | ^- (-oparg-1) |
| 3301 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3302 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3303 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3304 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame^] | 3305 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3306 | */ |
| 3307 | res = call_function(&sp, oparg + 1, NULL); |
| 3308 | stack_pointer = sp; |
| 3309 | } |
| 3310 | |
| 3311 | PUSH(res); |
| 3312 | if (res == NULL) |
| 3313 | goto error; |
| 3314 | DISPATCH(); |
| 3315 | } |
| 3316 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3317 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3318 | TARGET(CALL_FUNCTION) { |
| 3319 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3321 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3323 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3324 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3325 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3326 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3327 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3329 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3330 | TARGET(CALL_FUNCTION_KW) { |
| 3331 | PyObject **sp, *res, *names; |
| 3332 | |
| 3333 | names = POP(); |
| 3334 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3336 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3338 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3339 | Py_DECREF(names); |
| 3340 | |
| 3341 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3342 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3343 | } |
| 3344 | DISPATCH(); |
| 3345 | } |
| 3346 | |
| 3347 | TARGET(CALL_FUNCTION_EX) { |
| 3348 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3349 | if (oparg & 0x01) { |
| 3350 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3351 | if (!PyDict_CheckExact(kwargs)) { |
| 3352 | PyObject *d = PyDict_New(); |
| 3353 | if (d == NULL) |
| 3354 | goto error; |
| 3355 | if (PyDict_Update(d, kwargs) != 0) { |
| 3356 | Py_DECREF(d); |
| 3357 | /* PyDict_Update raises attribute |
| 3358 | * error (percolated from an attempt |
| 3359 | * to get 'keys' attribute) instead of |
| 3360 | * a type error if its second argument |
| 3361 | * is not a mapping. |
| 3362 | */ |
| 3363 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3364 | func = SECOND(); |
| 3365 | PyErr_Format(PyExc_TypeError, |
| 3366 | "%.200s%.200s argument after ** " |
| 3367 | "must be a mapping, not %.200s", |
| 3368 | PyEval_GetFuncName(func), |
| 3369 | PyEval_GetFuncDesc(func), |
| 3370 | kwargs->ob_type->tp_name); |
| 3371 | } |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3372 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3373 | goto error; |
| 3374 | } |
| 3375 | Py_DECREF(kwargs); |
| 3376 | kwargs = d; |
| 3377 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3378 | assert(PyDict_CheckExact(kwargs)); |
| 3379 | } |
| 3380 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3381 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3382 | if (!PyTuple_CheckExact(callargs)) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3383 | if (Py_TYPE(callargs)->tp_iter == NULL && |
| 3384 | !PySequence_Check(callargs)) { |
| 3385 | PyErr_Format(PyExc_TypeError, |
| 3386 | "%.200s%.200s argument after * " |
| 3387 | "must be an iterable, not %.200s", |
| 3388 | PyEval_GetFuncName(func), |
| 3389 | PyEval_GetFuncDesc(func), |
| 3390 | callargs->ob_type->tp_name); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3391 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3392 | goto error; |
| 3393 | } |
| 3394 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3395 | if (callargs == NULL) { |
| 3396 | goto error; |
| 3397 | } |
| 3398 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3399 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3400 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3401 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3402 | Py_DECREF(func); |
| 3403 | Py_DECREF(callargs); |
| 3404 | Py_XDECREF(kwargs); |
| 3405 | |
| 3406 | SET_TOP(result); |
| 3407 | if (result == NULL) { |
| 3408 | goto error; |
| 3409 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3410 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3411 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3412 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3413 | TARGET(MAKE_FUNCTION) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3414 | PyObject *qualname = POP(); |
| 3415 | PyObject *codeobj = POP(); |
| 3416 | PyFunctionObject *func = (PyFunctionObject *) |
| 3417 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3418 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3419 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3420 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3421 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3422 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3424 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3425 | if (oparg & 0x08) { |
| 3426 | assert(PyTuple_CheckExact(TOP())); |
| 3427 | func ->func_closure = POP(); |
| 3428 | } |
| 3429 | if (oparg & 0x04) { |
| 3430 | assert(PyDict_CheckExact(TOP())); |
| 3431 | func->func_annotations = POP(); |
| 3432 | } |
| 3433 | if (oparg & 0x02) { |
| 3434 | assert(PyDict_CheckExact(TOP())); |
| 3435 | func->func_kwdefaults = POP(); |
| 3436 | } |
| 3437 | if (oparg & 0x01) { |
| 3438 | assert(PyTuple_CheckExact(TOP())); |
| 3439 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3440 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3441 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3442 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3443 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3445 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3446 | TARGET(BUILD_SLICE) { |
| 3447 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3449 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3451 | step = NULL; |
| 3452 | stop = POP(); |
| 3453 | start = TOP(); |
| 3454 | slice = PySlice_New(start, stop, step); |
| 3455 | Py_DECREF(start); |
| 3456 | Py_DECREF(stop); |
| 3457 | Py_XDECREF(step); |
| 3458 | SET_TOP(slice); |
| 3459 | if (slice == NULL) |
| 3460 | goto error; |
| 3461 | DISPATCH(); |
| 3462 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3463 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3464 | TARGET(FORMAT_VALUE) { |
| 3465 | /* Handles f-string value formatting. */ |
| 3466 | PyObject *result; |
| 3467 | PyObject *fmt_spec; |
| 3468 | PyObject *value; |
| 3469 | PyObject *(*conv_fn)(PyObject *); |
| 3470 | int which_conversion = oparg & FVC_MASK; |
| 3471 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3472 | |
| 3473 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3474 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3475 | |
| 3476 | /* See if any conversion is specified. */ |
| 3477 | switch (which_conversion) { |
| 3478 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3479 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3480 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3481 | |
| 3482 | /* Must be 0 (meaning no conversion), since only four |
| 3483 | values are allowed by (oparg & FVC_MASK). */ |
| 3484 | default: conv_fn = NULL; break; |
| 3485 | } |
| 3486 | |
| 3487 | /* If there's a conversion function, call it and replace |
| 3488 | value with that result. Otherwise, just use value, |
| 3489 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3490 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3491 | result = conv_fn(value); |
| 3492 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3493 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3494 | Py_XDECREF(fmt_spec); |
| 3495 | goto error; |
| 3496 | } |
| 3497 | value = result; |
| 3498 | } |
| 3499 | |
| 3500 | /* If value is a unicode object, and there's no fmt_spec, |
| 3501 | then we know the result of format(value) is value |
| 3502 | itself. In that case, skip calling format(). I plan to |
| 3503 | move this optimization in to PyObject_Format() |
| 3504 | itself. */ |
| 3505 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3506 | /* Do nothing, just transfer ownership to result. */ |
| 3507 | result = value; |
| 3508 | } else { |
| 3509 | /* Actually call format(). */ |
| 3510 | result = PyObject_Format(value, fmt_spec); |
| 3511 | Py_DECREF(value); |
| 3512 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3513 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3514 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3515 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3516 | } |
| 3517 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3518 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3519 | DISPATCH(); |
| 3520 | } |
| 3521 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3522 | TARGET(EXTENDED_ARG) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3523 | int oldoparg = oparg; |
| 3524 | NEXTOPARG(); |
| 3525 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3526 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3527 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3528 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3529 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3530 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3532 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | default: |
| 3534 | fprintf(stderr, |
| 3535 | "XXX lineno: %d, opcode: %d\n", |
| 3536 | PyFrame_GetLineNumber(f), |
| 3537 | opcode); |
| 3538 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3539 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3540 | |
| 3541 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3543 | #endif |
| 3544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3546 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3547 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3548 | or goto error. */ |
| 3549 | assert(0); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3550 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3551 | error: |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3552 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3553 | assert(why == WHY_NOT); |
| 3554 | why = WHY_EXCEPTION; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3555 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3556 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3557 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3558 | if (!PyErr_Occurred()) |
| 3559 | PyErr_SetString(PyExc_SystemError, |
| 3560 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3561 | #else |
| 3562 | assert(PyErr_Occurred()); |
| 3563 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3564 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3565 | /* Log traceback info. */ |
| 3566 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3567 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3568 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3569 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3570 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3571 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3572 | fast_block_end: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3573 | assert(why != WHY_NOT); |
| 3574 | |
| 3575 | /* Unwind stacks if a (pseudo) exception occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3577 | /* Peek at the current block. */ |
| 3578 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | assert(why != WHY_YIELD); |
| 3581 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3582 | why = WHY_NOT; |
| 3583 | JUMPTO(PyLong_AS_LONG(retval)); |
| 3584 | Py_DECREF(retval); |
| 3585 | break; |
| 3586 | } |
| 3587 | /* Now we have to pop the block. */ |
| 3588 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 | if (b->b_type == EXCEPT_HANDLER) { |
| 3591 | UNWIND_EXCEPT_HANDLER(b); |
| 3592 | continue; |
| 3593 | } |
| 3594 | UNWIND_BLOCK(b); |
| 3595 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3596 | why = WHY_NOT; |
| 3597 | JUMPTO(b->b_handler); |
| 3598 | break; |
| 3599 | } |
| 3600 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 3601 | || b->b_type == SETUP_FINALLY)) { |
| 3602 | PyObject *exc, *val, *tb; |
| 3603 | int handler = b->b_handler; |
| 3604 | /* Beware, this invalidates all b->b_* fields */ |
| 3605 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 3606 | PUSH(tstate->exc_traceback); |
| 3607 | PUSH(tstate->exc_value); |
| 3608 | if (tstate->exc_type != NULL) { |
| 3609 | PUSH(tstate->exc_type); |
| 3610 | } |
| 3611 | else { |
| 3612 | Py_INCREF(Py_None); |
| 3613 | PUSH(Py_None); |
| 3614 | } |
| 3615 | PyErr_Fetch(&exc, &val, &tb); |
| 3616 | /* Make the raw exception data |
| 3617 | available to the handler, |
| 3618 | so a program can emulate the |
| 3619 | Python main loop. */ |
| 3620 | PyErr_NormalizeException( |
| 3621 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3622 | if (tb != NULL) |
| 3623 | PyException_SetTraceback(val, tb); |
| 3624 | else |
| 3625 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | Py_INCREF(exc); |
| 3627 | tstate->exc_type = exc; |
| 3628 | Py_INCREF(val); |
| 3629 | tstate->exc_value = val; |
| 3630 | tstate->exc_traceback = tb; |
| 3631 | if (tb == NULL) |
| 3632 | tb = Py_None; |
| 3633 | Py_INCREF(tb); |
| 3634 | PUSH(tb); |
| 3635 | PUSH(val); |
| 3636 | PUSH(exc); |
| 3637 | why = WHY_NOT; |
| 3638 | JUMPTO(handler); |
| 3639 | break; |
| 3640 | } |
| 3641 | if (b->b_type == SETUP_FINALLY) { |
| 3642 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3643 | PUSH(retval); |
| 3644 | PUSH(PyLong_FromLong((long)why)); |
| 3645 | why = WHY_NOT; |
| 3646 | JUMPTO(b->b_handler); |
| 3647 | break; |
| 3648 | } |
| 3649 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | if (why != WHY_NOT) |
| 3654 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3655 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3656 | assert(!PyErr_Occurred()); |
| 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3660 | assert(why != WHY_YIELD); |
| 3661 | /* Pop remaining stack entries. */ |
| 3662 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3663 | PyObject *o = POP(); |
| 3664 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3665 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3667 | if (why != WHY_RETURN) |
| 3668 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3669 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 3670 | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3671 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3672 | fast_yield: |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3673 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 3674 | |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3675 | /* The purpose of this block is to put aside the generator's exception |
| 3676 | state and restore that of the calling frame. If the current |
| 3677 | exception state is from the caller, we clear the exception values |
| 3678 | on the generator frame, so they are not swapped back in latter. The |
| 3679 | origin of the current exception state is determined by checking for |
| 3680 | except handler blocks, which we must be in iff a new exception |
| 3681 | state came into existence in this frame. (An uncaught exception |
| 3682 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 3683 | int i; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3684 | for (i = 0; i < f->f_iblock; i++) { |
| 3685 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3686 | break; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3687 | } |
| 3688 | } |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3689 | if (i == f->f_iblock) |
| 3690 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3691 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3692 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3693 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3694 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 3695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3696 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3697 | if (tstate->c_tracefunc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | if (why == WHY_RETURN || why == WHY_YIELD) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3699 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3700 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3701 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3702 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3703 | why = WHY_EXCEPTION; |
| 3704 | } |
| 3705 | } |
| 3706 | else if (why == WHY_EXCEPTION) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3707 | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3708 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3709 | PyTrace_RETURN, NULL); |
| 3710 | } |
| 3711 | } |
| 3712 | if (tstate->c_profilefunc) { |
| 3713 | if (why == WHY_EXCEPTION) |
| 3714 | call_trace_protected(tstate->c_profilefunc, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3715 | tstate->c_profileobj, |
| 3716 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3717 | PyTrace_RETURN, NULL); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3718 | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
| 3719 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3721 | Py_CLEAR(retval); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 3722 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | } |
| 3724 | } |
| 3725 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3727 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3728 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3729 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3730 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3732 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3733 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3734 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3735 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3738 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3739 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3740 | { |
| 3741 | int err; |
| 3742 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3743 | PyObject *name_str, *comma, *tail, *tmp; |
| 3744 | |
| 3745 | assert(PyList_CheckExact(names)); |
| 3746 | assert(len >= 1); |
| 3747 | /* Deal with the joys of natural language. */ |
| 3748 | switch (len) { |
| 3749 | case 1: |
| 3750 | name_str = PyList_GET_ITEM(names, 0); |
| 3751 | Py_INCREF(name_str); |
| 3752 | break; |
| 3753 | case 2: |
| 3754 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3755 | PyList_GET_ITEM(names, len - 2), |
| 3756 | PyList_GET_ITEM(names, len - 1)); |
| 3757 | break; |
| 3758 | default: |
| 3759 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3760 | PyList_GET_ITEM(names, len - 2), |
| 3761 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3762 | if (tail == NULL) |
| 3763 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3764 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3765 | fail, but we can't be too careful. */ |
| 3766 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3767 | if (err == -1) { |
| 3768 | Py_DECREF(tail); |
| 3769 | return; |
| 3770 | } |
| 3771 | /* Stitch everything up into a nice comma-separated list. */ |
| 3772 | comma = PyUnicode_FromString(", "); |
| 3773 | if (comma == NULL) { |
| 3774 | Py_DECREF(tail); |
| 3775 | return; |
| 3776 | } |
| 3777 | tmp = PyUnicode_Join(comma, names); |
| 3778 | Py_DECREF(comma); |
| 3779 | if (tmp == NULL) { |
| 3780 | Py_DECREF(tail); |
| 3781 | return; |
| 3782 | } |
| 3783 | name_str = PyUnicode_Concat(tmp, tail); |
| 3784 | Py_DECREF(tmp); |
| 3785 | Py_DECREF(tail); |
| 3786 | break; |
| 3787 | } |
| 3788 | if (name_str == NULL) |
| 3789 | return; |
| 3790 | PyErr_Format(PyExc_TypeError, |
| 3791 | "%U() missing %i required %s argument%s: %U", |
| 3792 | co->co_name, |
| 3793 | len, |
| 3794 | kind, |
| 3795 | len == 1 ? "" : "s", |
| 3796 | name_str); |
| 3797 | Py_DECREF(name_str); |
| 3798 | } |
| 3799 | |
| 3800 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3801 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3802 | PyObject **fastlocals) |
| 3803 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3804 | Py_ssize_t i, j = 0; |
| 3805 | Py_ssize_t start, end; |
| 3806 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3807 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3808 | PyObject *missing_names; |
| 3809 | |
| 3810 | /* Compute the names of the arguments that are missing. */ |
| 3811 | missing_names = PyList_New(missing); |
| 3812 | if (missing_names == NULL) |
| 3813 | return; |
| 3814 | if (positional) { |
| 3815 | start = 0; |
| 3816 | end = co->co_argcount - defcount; |
| 3817 | } |
| 3818 | else { |
| 3819 | start = co->co_argcount; |
| 3820 | end = start + co->co_kwonlyargcount; |
| 3821 | } |
| 3822 | for (i = start; i < end; i++) { |
| 3823 | if (GETLOCAL(i) == NULL) { |
| 3824 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3825 | PyObject *name = PyObject_Repr(raw); |
| 3826 | if (name == NULL) { |
| 3827 | Py_DECREF(missing_names); |
| 3828 | return; |
| 3829 | } |
| 3830 | PyList_SET_ITEM(missing_names, j++, name); |
| 3831 | } |
| 3832 | } |
| 3833 | assert(j == missing); |
| 3834 | format_missing(kind, co, missing_names); |
| 3835 | Py_DECREF(missing_names); |
| 3836 | } |
| 3837 | |
| 3838 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3839 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3840 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3841 | { |
| 3842 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3843 | Py_ssize_t kwonly_given = 0; |
| 3844 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3845 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3846 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3847 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3848 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3849 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3850 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3851 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3852 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3853 | } |
| 3854 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3855 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3856 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3857 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3858 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3859 | } |
| 3860 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3861 | plural = (co_argcount != 1); |
| 3862 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3863 | } |
| 3864 | if (sig == NULL) |
| 3865 | return; |
| 3866 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3867 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3868 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3869 | given != 1 ? "s" : "", |
| 3870 | kwonly_given, |
| 3871 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3872 | if (kwonly_sig == NULL) { |
| 3873 | Py_DECREF(sig); |
| 3874 | return; |
| 3875 | } |
| 3876 | } |
| 3877 | else { |
| 3878 | /* This will not fail. */ |
| 3879 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3880 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3881 | } |
| 3882 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3883 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3884 | co->co_name, |
| 3885 | sig, |
| 3886 | plural ? "s" : "", |
| 3887 | given, |
| 3888 | kwonly_sig, |
| 3889 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3890 | Py_DECREF(sig); |
| 3891 | Py_DECREF(kwonly_sig); |
| 3892 | } |
| 3893 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3894 | /* 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] | 3895 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3896 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3897 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3898 | static PyObject * |
| 3899 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3900 | PyObject **args, Py_ssize_t argcount, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3901 | PyObject **kwnames, PyObject **kwargs, |
| 3902 | Py_ssize_t kwcount, int kwstep, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3903 | PyObject **defs, Py_ssize_t defcount, |
| 3904 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3905 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3906 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3907 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3908 | PyFrameObject *f; |
| 3909 | PyObject *retval = NULL; |
| 3910 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3911 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3913 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3914 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3915 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3917 | if (globals == NULL) { |
| 3918 | PyErr_SetString(PyExc_SystemError, |
| 3919 | "PyEval_EvalCodeEx: NULL globals"); |
| 3920 | return NULL; |
| 3921 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3922 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3923 | /* Create the frame */ |
| 3924 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 3926 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3927 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3928 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3929 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | fastlocals = f->f_localsplus; |
| 3931 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3932 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3933 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3934 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3935 | kwdict = PyDict_New(); |
| 3936 | if (kwdict == NULL) |
| 3937 | goto fail; |
| 3938 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3939 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3940 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3941 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3942 | SETLOCAL(i, kwdict); |
| 3943 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3944 | else { |
| 3945 | kwdict = NULL; |
| 3946 | } |
| 3947 | |
| 3948 | /* Copy positional arguments into local variables */ |
| 3949 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3950 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3951 | } |
| 3952 | else { |
| 3953 | n = argcount; |
| 3954 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3955 | for (i = 0; i < n; i++) { |
| 3956 | x = args[i]; |
| 3957 | Py_INCREF(x); |
| 3958 | SETLOCAL(i, x); |
| 3959 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3960 | |
| 3961 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3962 | if (co->co_flags & CO_VARARGS) { |
| 3963 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3964 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3965 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3966 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3967 | SETLOCAL(total_args, u); |
| 3968 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | x = args[i]; |
| 3970 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3971 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3973 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3974 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3975 | /* Handle keyword arguments passed as two strided arrays */ |
| 3976 | kwcount *= kwstep; |
| 3977 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3978 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3979 | PyObject *keyword = kwnames[i]; |
| 3980 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3981 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3982 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3983 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3984 | PyErr_Format(PyExc_TypeError, |
| 3985 | "%U() keywords must be strings", |
| 3986 | co->co_name); |
| 3987 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3989 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3990 | /* Speed hack: do raw pointer compares. As names are |
| 3991 | normally interned this should almost always hit. */ |
| 3992 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3993 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3994 | PyObject *name = co_varnames[j]; |
| 3995 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3996 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3997 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3998 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3999 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4000 | /* Slow fallback, just in case */ |
| 4001 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4002 | PyObject *name = co_varnames[j]; |
| 4003 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 4004 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4005 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4006 | } |
| 4007 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4008 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4009 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4010 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4011 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 4012 | assert(j >= total_args); |
| 4013 | if (kwdict == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4014 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4015 | "%U() got an unexpected keyword argument '%S'", |
| 4016 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4017 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4018 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4019 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4020 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4021 | goto fail; |
| 4022 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4023 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4024 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4025 | kw_found: |
| 4026 | if (GETLOCAL(j) != NULL) { |
| 4027 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4028 | "%U() got multiple values for argument '%S'", |
| 4029 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4030 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4031 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4032 | Py_INCREF(value); |
| 4033 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4034 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4035 | |
| 4036 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4037 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4038 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4039 | goto fail; |
| 4040 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4041 | |
| 4042 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4043 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4044 | Py_ssize_t m = co->co_argcount - defcount; |
| 4045 | Py_ssize_t missing = 0; |
| 4046 | for (i = argcount; i < m; i++) { |
| 4047 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4048 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4049 | } |
| 4050 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4051 | if (missing) { |
| 4052 | missing_arguments(co, missing, defcount, fastlocals); |
| 4053 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4054 | } |
| 4055 | if (n > m) |
| 4056 | i = n - m; |
| 4057 | else |
| 4058 | i = 0; |
| 4059 | for (; i < defcount; i++) { |
| 4060 | if (GETLOCAL(m+i) == NULL) { |
| 4061 | PyObject *def = defs[i]; |
| 4062 | Py_INCREF(def); |
| 4063 | SETLOCAL(m+i, def); |
| 4064 | } |
| 4065 | } |
| 4066 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4067 | |
| 4068 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4069 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4070 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4071 | for (i = co->co_argcount; i < total_args; i++) { |
| 4072 | PyObject *name; |
| 4073 | if (GETLOCAL(i) != NULL) |
| 4074 | continue; |
| 4075 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4076 | if (kwdefs != NULL) { |
| 4077 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 4078 | if (def) { |
| 4079 | Py_INCREF(def); |
| 4080 | SETLOCAL(i, def); |
| 4081 | continue; |
| 4082 | } |
| 4083 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4084 | missing++; |
| 4085 | } |
| 4086 | if (missing) { |
| 4087 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4088 | goto fail; |
| 4089 | } |
| 4090 | } |
| 4091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4093 | vars into frame. */ |
| 4094 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 4096 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4097 | /* Possibly account for the cell variable being an argument. */ |
| 4098 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4099 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4100 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4101 | /* Clear the local copy. */ |
| 4102 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4103 | } |
| 4104 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4105 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4106 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4107 | if (c == NULL) |
| 4108 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4109 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4110 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4111 | |
| 4112 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4113 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4114 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4115 | Py_INCREF(o); |
| 4116 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4118 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4119 | /* Handle generator/coroutine/asynchronous generator */ |
| 4120 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4121 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4122 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4123 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4124 | |
| 4125 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 4126 | assert(coro_wrapper != NULL); |
| 4127 | PyErr_Format(PyExc_RuntimeError, |
| 4128 | "coroutine wrapper %.200R attempted " |
| 4129 | "to recursively wrap %.200R", |
| 4130 | coro_wrapper, |
| 4131 | co); |
| 4132 | goto fail; |
| 4133 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4135 | /* Don't need to keep the reference to f_back, it will be set |
| 4136 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4137 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4139 | /* Create a new generator that owns the ready to run frame |
| 4140 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4141 | if (is_coro) { |
| 4142 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4143 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4144 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4145 | } else { |
| 4146 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4147 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4148 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4149 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4150 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 4151 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4152 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4153 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4154 | if (is_coro && coro_wrapper != NULL) { |
| 4155 | PyObject *wrapped; |
| 4156 | tstate->in_coroutine_wrapper = 1; |
| 4157 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 4158 | tstate->in_coroutine_wrapper = 0; |
| 4159 | return wrapped; |
| 4160 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4161 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4162 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4163 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4164 | |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 4165 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4166 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4167 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4169 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4170 | which can call back into Python. While we're done with the |
| 4171 | current Python frame (f), the associated C stack is still in use, |
| 4172 | so recursion_depth must be boosted for the duration. |
| 4173 | */ |
| 4174 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4175 | if (Py_REFCNT(f) > 1) { |
| 4176 | Py_DECREF(f); |
| 4177 | _PyObject_GC_TRACK(f); |
| 4178 | } |
| 4179 | else { |
| 4180 | ++tstate->recursion_depth; |
| 4181 | Py_DECREF(f); |
| 4182 | --tstate->recursion_depth; |
| 4183 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4187 | PyObject * |
| 4188 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4189 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 4190 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
| 4191 | { |
| 4192 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4193 | args, argcount, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4194 | kws, kws + 1, kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4195 | defs, defcount, |
| 4196 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4197 | NULL, NULL); |
| 4198 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4199 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4200 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4201 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4203 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4204 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4206 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4207 | return NULL; |
| 4208 | } |
| 4209 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4213 | /* These 3 functions deal with the exception state of generators. */ |
| 4214 | |
| 4215 | static void |
| 4216 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4217 | { |
| 4218 | PyObject *type, *value, *traceback; |
| 4219 | Py_XINCREF(tstate->exc_type); |
| 4220 | Py_XINCREF(tstate->exc_value); |
| 4221 | Py_XINCREF(tstate->exc_traceback); |
| 4222 | type = f->f_exc_type; |
| 4223 | value = f->f_exc_value; |
| 4224 | traceback = f->f_exc_traceback; |
| 4225 | f->f_exc_type = tstate->exc_type; |
| 4226 | f->f_exc_value = tstate->exc_value; |
| 4227 | f->f_exc_traceback = tstate->exc_traceback; |
| 4228 | Py_XDECREF(type); |
| 4229 | Py_XDECREF(value); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 4230 | Py_XDECREF(traceback); |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | static void |
| 4234 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4235 | { |
| 4236 | PyObject *tmp; |
| 4237 | tmp = tstate->exc_type; |
| 4238 | tstate->exc_type = f->f_exc_type; |
| 4239 | f->f_exc_type = tmp; |
| 4240 | tmp = tstate->exc_value; |
| 4241 | tstate->exc_value = f->f_exc_value; |
| 4242 | f->f_exc_value = tmp; |
| 4243 | tmp = tstate->exc_traceback; |
| 4244 | tstate->exc_traceback = f->f_exc_traceback; |
| 4245 | f->f_exc_traceback = tmp; |
| 4246 | } |
| 4247 | |
| 4248 | static void |
| 4249 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4250 | { |
| 4251 | PyObject *type, *value, *tb; |
| 4252 | type = tstate->exc_type; |
| 4253 | value = tstate->exc_value; |
| 4254 | tb = tstate->exc_traceback; |
| 4255 | tstate->exc_type = f->f_exc_type; |
| 4256 | tstate->exc_value = f->f_exc_value; |
| 4257 | tstate->exc_traceback = f->f_exc_traceback; |
| 4258 | f->f_exc_type = NULL; |
| 4259 | f->f_exc_value = NULL; |
| 4260 | f->f_exc_traceback = NULL; |
| 4261 | Py_XDECREF(type); |
| 4262 | Py_XDECREF(value); |
| 4263 | Py_XDECREF(tb); |
| 4264 | } |
| 4265 | |
| 4266 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4267 | /* Logic for the raise statement (too complicated for inlining). |
| 4268 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4269 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4270 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | if (exc == NULL) { |
| 4275 | /* Reraise */ |
| 4276 | PyThreadState *tstate = PyThreadState_GET(); |
| 4277 | PyObject *tb; |
| 4278 | type = tstate->exc_type; |
| 4279 | value = tstate->exc_value; |
| 4280 | tb = tstate->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4281 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4282 | PyErr_SetString(PyExc_RuntimeError, |
| 4283 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4284 | return 0; |
| 4285 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | Py_XINCREF(type); |
| 4287 | Py_XINCREF(value); |
| 4288 | Py_XINCREF(tb); |
| 4289 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4290 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4291 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | /* We support the following forms of raise: |
| 4294 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4295 | raise <instance> |
| 4296 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | if (PyExceptionClass_Check(exc)) { |
| 4299 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4300 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4301 | if (value == NULL) |
| 4302 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4303 | if (!PyExceptionInstance_Check(value)) { |
| 4304 | PyErr_Format(PyExc_TypeError, |
| 4305 | "calling %R should have returned an instance of " |
| 4306 | "BaseException, not %R", |
| 4307 | type, Py_TYPE(value)); |
| 4308 | goto raise_error; |
| 4309 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | } |
| 4311 | else if (PyExceptionInstance_Check(exc)) { |
| 4312 | value = exc; |
| 4313 | type = PyExceptionInstance_Class(exc); |
| 4314 | Py_INCREF(type); |
| 4315 | } |
| 4316 | else { |
| 4317 | /* Not something you can raise. You get an exception |
| 4318 | anyway, just not what you specified :-) */ |
| 4319 | Py_DECREF(exc); |
| 4320 | PyErr_SetString(PyExc_TypeError, |
| 4321 | "exceptions must derive from BaseException"); |
| 4322 | goto raise_error; |
| 4323 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4324 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4325 | assert(type != NULL); |
| 4326 | assert(value != NULL); |
| 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | if (cause) { |
| 4329 | PyObject *fixed_cause; |
| 4330 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4331 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4332 | if (fixed_cause == NULL) |
| 4333 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4334 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4336 | else if (PyExceptionInstance_Check(cause)) { |
| 4337 | fixed_cause = cause; |
| 4338 | } |
| 4339 | else if (cause == Py_None) { |
| 4340 | Py_DECREF(cause); |
| 4341 | fixed_cause = NULL; |
| 4342 | } |
| 4343 | else { |
| 4344 | PyErr_SetString(PyExc_TypeError, |
| 4345 | "exception causes must derive from " |
| 4346 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4347 | goto raise_error; |
| 4348 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4349 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4350 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | PyErr_SetObject(type, value); |
| 4353 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4354 | Py_DECREF(value); |
| 4355 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4356 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4357 | |
| 4358 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4359 | Py_XDECREF(value); |
| 4360 | Py_XDECREF(type); |
| 4361 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4362 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4365 | /* 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] | 4366 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4367 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4368 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4369 | with a variable target. |
| 4370 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4371 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4372 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4373 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | int i = 0, j = 0; |
| 4376 | Py_ssize_t ll = 0; |
| 4377 | PyObject *it; /* iter(v) */ |
| 4378 | PyObject *w; |
| 4379 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | it = PyObject_GetIter(v); |
| 4384 | if (it == NULL) |
| 4385 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | for (; i < argcnt; i++) { |
| 4388 | w = PyIter_Next(it); |
| 4389 | if (w == NULL) { |
| 4390 | /* Iterator done, via error or exhaustion. */ |
| 4391 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4392 | if (argcntafter == -1) { |
| 4393 | PyErr_Format(PyExc_ValueError, |
| 4394 | "not enough values to unpack (expected %d, got %d)", |
| 4395 | argcnt, i); |
| 4396 | } |
| 4397 | else { |
| 4398 | PyErr_Format(PyExc_ValueError, |
| 4399 | "not enough values to unpack " |
| 4400 | "(expected at least %d, got %d)", |
| 4401 | argcnt + argcntafter, i); |
| 4402 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | } |
| 4404 | goto Error; |
| 4405 | } |
| 4406 | *--sp = w; |
| 4407 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | if (argcntafter == -1) { |
| 4410 | /* We better have exhausted the iterator now. */ |
| 4411 | w = PyIter_Next(it); |
| 4412 | if (w == NULL) { |
| 4413 | if (PyErr_Occurred()) |
| 4414 | goto Error; |
| 4415 | Py_DECREF(it); |
| 4416 | return 1; |
| 4417 | } |
| 4418 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4419 | PyErr_Format(PyExc_ValueError, |
| 4420 | "too many values to unpack (expected %d)", |
| 4421 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | goto Error; |
| 4423 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | l = PySequence_List(it); |
| 4426 | if (l == NULL) |
| 4427 | goto Error; |
| 4428 | *--sp = l; |
| 4429 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | ll = PyList_GET_SIZE(l); |
| 4432 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4433 | PyErr_Format(PyExc_ValueError, |
| 4434 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4435 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | goto Error; |
| 4437 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | /* Pop the "after-variable" args off the list. */ |
| 4440 | for (j = argcntafter; j > 0; j--, i++) { |
| 4441 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4442 | } |
| 4443 | /* Resize the list. */ |
| 4444 | Py_SIZE(l) = ll - argcntafter; |
| 4445 | Py_DECREF(it); |
| 4446 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4447 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4448 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4449 | for (; i > 0; i--, sp++) |
| 4450 | Py_DECREF(*sp); |
| 4451 | Py_XDECREF(it); |
| 4452 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4456 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4457 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4458 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | printf("%s ", str); |
| 4461 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4462 | PyErr_Clear(); /* Don't know what else to do */ |
| 4463 | printf("\n"); |
| 4464 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4465 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4466 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4467 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4468 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4469 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4470 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4471 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4472 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4474 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | if (value == NULL) { |
| 4476 | value = Py_None; |
| 4477 | Py_INCREF(value); |
| 4478 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4479 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4480 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4482 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4483 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | return; |
| 4485 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4486 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | Py_DECREF(arg); |
| 4488 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4489 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4490 | else { |
| 4491 | Py_XDECREF(type); |
| 4492 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4493 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4497 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4498 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4499 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4502 | PyObject *type, *value, *traceback; |
| 4503 | int err; |
| 4504 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4505 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | if (err == 0) |
| 4507 | { |
| 4508 | PyErr_Restore(type, value, traceback); |
| 4509 | return 0; |
| 4510 | } |
| 4511 | else { |
| 4512 | Py_XDECREF(type); |
| 4513 | Py_XDECREF(value); |
| 4514 | Py_XDECREF(traceback); |
| 4515 | return -1; |
| 4516 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4519 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4520 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4521 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | int result; |
| 4525 | if (tstate->tracing) |
| 4526 | return 0; |
| 4527 | tstate->tracing++; |
| 4528 | tstate->use_tracing = 0; |
| 4529 | result = func(obj, frame, what, arg); |
| 4530 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4531 | || (tstate->c_profilefunc != NULL)); |
| 4532 | tstate->tracing--; |
| 4533 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4536 | PyObject * |
| 4537 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4538 | { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4539 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | int save_tracing = tstate->tracing; |
| 4541 | int save_use_tracing = tstate->use_tracing; |
| 4542 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4544 | tstate->tracing = 0; |
| 4545 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4546 | || (tstate->c_profilefunc != NULL)); |
| 4547 | result = PyObject_Call(func, args, NULL); |
| 4548 | tstate->tracing = save_tracing; |
| 4549 | tstate->use_tracing = save_use_tracing; |
| 4550 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4553 | /* 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] | 4554 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4555 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4556 | PyThreadState *tstate, PyFrameObject *frame, |
| 4557 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | int result = 0; |
| 4560 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | /* If the last instruction executed isn't in the current |
| 4563 | instruction window, reset the window. |
| 4564 | */ |
| 4565 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4566 | PyAddrPair bounds; |
| 4567 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4568 | &bounds); |
| 4569 | *instr_lb = bounds.ap_lower; |
| 4570 | *instr_ub = bounds.ap_upper; |
| 4571 | } |
| 4572 | /* If the last instruction falls at the start of a line or if |
| 4573 | it represents a jump backwards, update the frame's line |
| 4574 | number and call the trace function. */ |
| 4575 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4576 | frame->f_lineno = line; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4577 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | } |
| 4579 | *instr_prev = frame->f_lasti; |
| 4580 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4583 | void |
| 4584 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4585 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | PyThreadState *tstate = PyThreadState_GET(); |
| 4587 | PyObject *temp = tstate->c_profileobj; |
| 4588 | Py_XINCREF(arg); |
| 4589 | tstate->c_profilefunc = NULL; |
| 4590 | tstate->c_profileobj = NULL; |
| 4591 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4592 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4593 | Py_XDECREF(temp); |
| 4594 | tstate->c_profilefunc = func; |
| 4595 | tstate->c_profileobj = arg; |
| 4596 | /* Flag that tracing or profiling is turned on */ |
| 4597 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | void |
| 4601 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | PyThreadState *tstate = PyThreadState_GET(); |
| 4604 | PyObject *temp = tstate->c_traceobj; |
| 4605 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4606 | Py_XINCREF(arg); |
| 4607 | tstate->c_tracefunc = NULL; |
| 4608 | tstate->c_traceobj = NULL; |
| 4609 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4610 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4611 | Py_XDECREF(temp); |
| 4612 | tstate->c_tracefunc = func; |
| 4613 | tstate->c_traceobj = arg; |
| 4614 | /* Flag that tracing or profiling is turned on */ |
| 4615 | tstate->use_tracing = ((func != NULL) |
| 4616 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4619 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4620 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4621 | { |
| 4622 | PyThreadState *tstate = PyThreadState_GET(); |
| 4623 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4624 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4625 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4629 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4630 | { |
| 4631 | PyThreadState *tstate = PyThreadState_GET(); |
| 4632 | return tstate->coroutine_wrapper; |
| 4633 | } |
| 4634 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4635 | void |
| 4636 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4637 | { |
| 4638 | PyThreadState *tstate = PyThreadState_GET(); |
| 4639 | |
| 4640 | Py_XINCREF(firstiter); |
| 4641 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4642 | } |
| 4643 | |
| 4644 | PyObject * |
| 4645 | _PyEval_GetAsyncGenFirstiter(void) |
| 4646 | { |
| 4647 | PyThreadState *tstate = PyThreadState_GET(); |
| 4648 | return tstate->async_gen_firstiter; |
| 4649 | } |
| 4650 | |
| 4651 | void |
| 4652 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4653 | { |
| 4654 | PyThreadState *tstate = PyThreadState_GET(); |
| 4655 | |
| 4656 | Py_XINCREF(finalizer); |
| 4657 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4658 | } |
| 4659 | |
| 4660 | PyObject * |
| 4661 | _PyEval_GetAsyncGenFinalizer(void) |
| 4662 | { |
| 4663 | PyThreadState *tstate = PyThreadState_GET(); |
| 4664 | return tstate->async_gen_finalizer; |
| 4665 | } |
| 4666 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4667 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4668 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4671 | if (current_frame == NULL) |
| 4672 | return PyThreadState_GET()->interp->builtins; |
| 4673 | else |
| 4674 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4677 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4678 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4680 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4681 | if (current_frame == NULL) { |
| 4682 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4687 | return NULL; |
| 4688 | |
| 4689 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4693 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4694 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4696 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4697 | if (current_frame == NULL) |
| 4698 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4699 | |
| 4700 | assert(current_frame->f_globals != NULL); |
| 4701 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4704 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4705 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4707 | PyThreadState *tstate = PyThreadState_GET(); |
| 4708 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4711 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4712 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4715 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | if (current_frame != NULL) { |
| 4718 | const int codeflags = current_frame->f_code->co_flags; |
| 4719 | const int compilerflags = codeflags & PyCF_MASK; |
| 4720 | if (compilerflags) { |
| 4721 | result = 1; |
| 4722 | cf->cf_flags |= compilerflags; |
| 4723 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4724 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4725 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4726 | result = 1; |
| 4727 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4728 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4729 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | } |
| 4731 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4734 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4735 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 4736 | 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] | 4737 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4738 | PyObject * |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 4739 | PyEval_CallObjectWithKeywords(PyObject *callable, |
| 4740 | PyObject *args, PyObject *kwargs) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4741 | { |
Victor Stinner | 59b356d | 2015-03-16 11:52:32 +0100 | [diff] [blame] | 4742 | #ifdef Py_DEBUG |
| 4743 | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
| 4744 | set. It raises a new exception if parameters are invalid or if |
| 4745 | PyTuple_New() fails, and so the original exception is lost. */ |
| 4746 | assert(!PyErr_Occurred()); |
| 4747 | #endif |
| 4748 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4749 | if (args == NULL) { |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 4750 | return _PyObject_FastCallDict(callable, NULL, 0, kwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | } |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4752 | |
| 4753 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4754 | PyErr_SetString(PyExc_TypeError, |
| 4755 | "argument list must be a tuple"); |
| 4756 | return NULL; |
| 4757 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4758 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4759 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | PyErr_SetString(PyExc_TypeError, |
| 4761 | "keyword list must be a dictionary"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | return NULL; |
| 4763 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4764 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 4765 | return PyObject_Call(callable, args, kwargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4768 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4769 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | if (PyMethod_Check(func)) |
| 4772 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4773 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4774 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | else if (PyCFunction_Check(func)) |
| 4776 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4777 | else |
| 4778 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4781 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4782 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | if (PyMethod_Check(func)) |
| 4785 | return "()"; |
| 4786 | else if (PyFunction_Check(func)) |
| 4787 | return "()"; |
| 4788 | else if (PyCFunction_Check(func)) |
| 4789 | return "()"; |
| 4790 | else |
| 4791 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4794 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4795 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4796 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4797 | tstate, tstate->frame, \ |
| 4798 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4799 | x = NULL; \ |
| 4800 | } \ |
| 4801 | else { \ |
| 4802 | x = call; \ |
| 4803 | if (tstate->c_profilefunc != NULL) { \ |
| 4804 | if (x == NULL) { \ |
| 4805 | call_trace_protected(tstate->c_profilefunc, \ |
| 4806 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4807 | tstate, tstate->frame, \ |
| 4808 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | /* XXX should pass (type, value, tb) */ \ |
| 4810 | } else { \ |
| 4811 | if (call_trace(tstate->c_profilefunc, \ |
| 4812 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4813 | tstate, tstate->frame, \ |
| 4814 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | Py_DECREF(x); \ |
| 4816 | x = NULL; \ |
| 4817 | } \ |
| 4818 | } \ |
| 4819 | } \ |
| 4820 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4821 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4822 | x = call; \ |
| 4823 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4824 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 4825 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 4826 | to reduce the stack consumption. */ |
| 4827 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4828 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4829 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4830 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4831 | PyObject *func = *pfunc; |
| 4832 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4833 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4834 | Py_ssize_t nargs = oparg - nkwargs; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4835 | PyObject **stack; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4837 | /* Always dispatch PyCFunction first, because these are |
| 4838 | presumed to be the most frequent callable object. |
| 4839 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4840 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4841 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4842 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4843 | stack = (*pp_stack) - nargs - nkwargs; |
| 4844 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4845 | } |
| 4846 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4847 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
Victor Stinner | b69ee8c | 2016-11-28 18:32:31 +0100 | [diff] [blame] | 4848 | /* Optimize access to bound methods. Reuse the Python stack |
| 4849 | to pass 'self' as the first argument, replace 'func' |
| 4850 | with 'self'. It avoids the creation of a new temporary tuple |
| 4851 | for arguments (to replace func with self) when the method uses |
| 4852 | FASTCALL. */ |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4853 | PyObject *self = PyMethod_GET_SELF(func); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4854 | Py_INCREF(self); |
| 4855 | func = PyMethod_GET_FUNCTION(func); |
| 4856 | Py_INCREF(func); |
| 4857 | Py_SETREF(*pfunc, self); |
| 4858 | nargs++; |
| 4859 | } |
| 4860 | else { |
| 4861 | Py_INCREF(func); |
| 4862 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4863 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4864 | stack = (*pp_stack) - nargs - nkwargs; |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4865 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4866 | if (PyFunction_Check(func)) { |
| 4867 | x = fast_function(func, stack, nargs, kwnames); |
| 4868 | } |
| 4869 | else { |
| 4870 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4871 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4872 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4873 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4875 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4876 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | /* Clear the stack of the function object. Also removes |
| 4879 | the arguments in case they weren't consumed already |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4880 | (fast_function() and err_args() leave them on the stack). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4881 | */ |
| 4882 | while ((*pp_stack) > pfunc) { |
| 4883 | w = EXT_POP(*pp_stack); |
| 4884 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4885 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4887 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4890 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4891 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4892 | For the simplest case -- a function that takes only positional |
| 4893 | arguments and is called with only positional arguments -- it |
| 4894 | inlines the most primitive frame setup code from |
| 4895 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4896 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4897 | */ |
| 4898 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 4899 | static PyObject* _Py_HOT_FUNCTION |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4900 | _PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
| 4901 | PyObject *globals) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4902 | { |
| 4903 | PyFrameObject *f; |
| 4904 | PyThreadState *tstate = PyThreadState_GET(); |
| 4905 | PyObject **fastlocals; |
| 4906 | Py_ssize_t i; |
| 4907 | PyObject *result; |
| 4908 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4909 | assert(globals != NULL); |
| 4910 | /* XXX Perhaps we should create a specialized |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4911 | _PyFrame_New_NoTrack() that doesn't take locals, but does |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4912 | take builtins without sanity checking them. |
| 4913 | */ |
| 4914 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4915 | f = _PyFrame_New_NoTrack(tstate, co, globals, NULL); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4916 | if (f == NULL) { |
| 4917 | return NULL; |
| 4918 | } |
| 4919 | |
| 4920 | fastlocals = f->f_localsplus; |
| 4921 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4922 | for (i = 0; i < nargs; i++) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4923 | Py_INCREF(*args); |
| 4924 | fastlocals[i] = *args++; |
| 4925 | } |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 4926 | result = PyEval_EvalFrameEx(f,0); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4927 | |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4928 | if (Py_REFCNT(f) > 1) { |
| 4929 | Py_DECREF(f); |
| 4930 | _PyObject_GC_TRACK(f); |
| 4931 | } |
| 4932 | else { |
| 4933 | ++tstate->recursion_depth; |
| 4934 | Py_DECREF(f); |
| 4935 | --tstate->recursion_depth; |
| 4936 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4937 | return result; |
| 4938 | } |
| 4939 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4940 | static PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4941 | fast_function(PyObject *func, PyObject **stack, |
| 4942 | Py_ssize_t nargs, PyObject *kwnames) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4945 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4946 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4947 | PyObject *kwdefs, *closure, *name, *qualname; |
| 4948 | PyObject **d; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4949 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4950 | Py_ssize_t nd; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4951 | |
Victor Stinner | 57f91ac | 2016-09-12 13:37:07 +0200 | [diff] [blame] | 4952 | assert(PyFunction_Check(func)); |
| 4953 | assert(nargs >= 0); |
| 4954 | assert(kwnames == NULL || PyTuple_CheckExact(kwnames)); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4955 | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
Victor Stinner | 57f91ac | 2016-09-12 13:37:07 +0200 | [diff] [blame] | 4956 | /* kwnames must only contains str strings, no subclass, and all keys must |
| 4957 | be unique */ |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4958 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4959 | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4960 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4961 | { |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4962 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4963 | return _PyFunction_FastCall(co, stack, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4964 | } |
| 4965 | else if (nargs == 0 && argdefs != NULL |
| 4966 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4967 | /* function called with no arguments, but all parameters have |
| 4968 | a default value: use default values as arguments .*/ |
| 4969 | stack = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4970 | return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4971 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4972 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4973 | |
| 4974 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4975 | closure = PyFunction_GET_CLOSURE(func); |
| 4976 | name = ((PyFunctionObject *)func) -> func_name; |
| 4977 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | if (argdefs != NULL) { |
| 4980 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4981 | nd = Py_SIZE(argdefs); |
| 4982 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4983 | else { |
| 4984 | d = NULL; |
| 4985 | nd = 0; |
| 4986 | } |
| 4987 | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4988 | stack, nargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4989 | nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL, |
| 4990 | stack + nargs, |
| 4991 | nkwargs, 1, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4992 | d, (int)nd, kwdefs, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4993 | closure, name, qualname); |
| 4994 | } |
| 4995 | |
| 4996 | PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4997 | _PyFunction_FastCallKeywords(PyObject *func, PyObject **stack, |
| 4998 | Py_ssize_t nargs, PyObject *kwnames) |
| 4999 | { |
| 5000 | return fast_function(func, stack, nargs, kwnames); |
| 5001 | } |
| 5002 | |
| 5003 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5004 | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5005 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5006 | { |
| 5007 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 5008 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 5009 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 5010 | PyObject *kwdefs, *closure, *name, *qualname; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5011 | PyObject *kwtuple, **k; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5012 | PyObject **d; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5013 | Py_ssize_t nd, nk; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5014 | PyObject *result; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5015 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5016 | assert(func != NULL); |
| 5017 | assert(nargs >= 0); |
| 5018 | assert(nargs == 0 || args != NULL); |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5019 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5020 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5021 | if (co->co_kwonlyargcount == 0 && |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5022 | (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5023 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 5024 | { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5025 | /* Fast paths */ |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5026 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 5027 | return _PyFunction_FastCall(co, args, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5028 | } |
| 5029 | else if (nargs == 0 && argdefs != NULL |
| 5030 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 5031 | /* function called with no arguments, but all parameters have |
| 5032 | a default value: use default values as arguments .*/ |
| 5033 | args = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 5034 | return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 5035 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5036 | } |
| 5037 | |
Victor Stinner | 865a0f6 | 2017-01-03 02:01:42 +0100 | [diff] [blame] | 5038 | nk = (kwargs != NULL) ? PyDict_GET_SIZE(kwargs) : 0; |
| 5039 | if (nk != 0) { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5040 | Py_ssize_t pos, i; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5041 | |
| 5042 | kwtuple = PyTuple_New(2 * nk); |
| 5043 | if (kwtuple == NULL) { |
| 5044 | return NULL; |
| 5045 | } |
| 5046 | |
| 5047 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 5048 | pos = i = 0; |
| 5049 | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
Victor Stinner | 865a0f6 | 2017-01-03 02:01:42 +0100 | [diff] [blame] | 5050 | /* We must hold strong references because keyword arguments can be |
| 5051 | indirectly modified while the function is called: |
| 5052 | see issue #2016 and test_extcall */ |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5053 | Py_INCREF(k[i]); |
| 5054 | Py_INCREF(k[i+1]); |
| 5055 | i += 2; |
| 5056 | } |
| 5057 | nk = i / 2; |
| 5058 | } |
| 5059 | else { |
| 5060 | kwtuple = NULL; |
| 5061 | k = NULL; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5062 | } |
| 5063 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 5064 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 5065 | closure = PyFunction_GET_CLOSURE(func); |
| 5066 | name = ((PyFunctionObject *)func) -> func_name; |
| 5067 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 5068 | |
| 5069 | if (argdefs != NULL) { |
| 5070 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 5071 | nd = Py_SIZE(argdefs); |
| 5072 | } |
| 5073 | else { |
| 5074 | d = NULL; |
| 5075 | nd = 0; |
| 5076 | } |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5077 | |
| 5078 | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
| 5079 | args, nargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 5080 | k, k + 1, nk, 2, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 5081 | d, nd, kwdefs, |
| 5082 | closure, name, qualname); |
| 5083 | Py_XDECREF(kwtuple); |
| 5084 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5088 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5089 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5090 | if (PyCFunction_Check(func)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5091 | PyObject *result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | PyThreadState *tstate = PyThreadState_GET(); |
| 5093 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5094 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5095 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5096 | else { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5097 | return PyObject_Call(func, callargs, kwdict); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5098 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5101 | /* 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] | 5102 | nb_index slot defined, and store in *pi. |
| 5103 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 5104 | 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] | 5105 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5106 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 5107 | /* Note: If v is NULL, return success without storing into *pi. This |
| 5108 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 5109 | 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] | 5110 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5111 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5112 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5114 | if (v != NULL) { |
| 5115 | Py_ssize_t x; |
| 5116 | if (PyIndex_Check(v)) { |
| 5117 | x = PyNumber_AsSsize_t(v, NULL); |
| 5118 | if (x == -1 && PyErr_Occurred()) |
| 5119 | return 0; |
| 5120 | } |
| 5121 | else { |
| 5122 | PyErr_SetString(PyExc_TypeError, |
| 5123 | "slice indices must be integers or " |
| 5124 | "None or have an __index__ method"); |
| 5125 | return 0; |
| 5126 | } |
| 5127 | *pi = x; |
| 5128 | } |
| 5129 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5132 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5133 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5134 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5135 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 5136 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5138 | int res = 0; |
| 5139 | switch (op) { |
| 5140 | case PyCmp_IS: |
| 5141 | res = (v == w); |
| 5142 | break; |
| 5143 | case PyCmp_IS_NOT: |
| 5144 | res = (v != w); |
| 5145 | break; |
| 5146 | case PyCmp_IN: |
| 5147 | res = PySequence_Contains(w, v); |
| 5148 | if (res < 0) |
| 5149 | return NULL; |
| 5150 | break; |
| 5151 | case PyCmp_NOT_IN: |
| 5152 | res = PySequence_Contains(w, v); |
| 5153 | if (res < 0) |
| 5154 | return NULL; |
| 5155 | res = !res; |
| 5156 | break; |
| 5157 | case PyCmp_EXC_MATCH: |
| 5158 | if (PyTuple_Check(w)) { |
| 5159 | Py_ssize_t i, length; |
| 5160 | length = PyTuple_Size(w); |
| 5161 | for (i = 0; i < length; i += 1) { |
| 5162 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5163 | if (!PyExceptionClass_Check(exc)) { |
| 5164 | PyErr_SetString(PyExc_TypeError, |
| 5165 | CANNOT_CATCH_MSG); |
| 5166 | return NULL; |
| 5167 | } |
| 5168 | } |
| 5169 | } |
| 5170 | else { |
| 5171 | if (!PyExceptionClass_Check(w)) { |
| 5172 | PyErr_SetString(PyExc_TypeError, |
| 5173 | CANNOT_CATCH_MSG); |
| 5174 | return NULL; |
| 5175 | } |
| 5176 | } |
| 5177 | res = PyErr_GivenExceptionMatches(v, w); |
| 5178 | break; |
| 5179 | default: |
| 5180 | return PyObject_RichCompare(v, w, op); |
| 5181 | } |
| 5182 | v = res ? Py_True : Py_False; |
| 5183 | Py_INCREF(v); |
| 5184 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5187 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5188 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 5189 | { |
| 5190 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5191 | PyObject *import_func, *res; |
| 5192 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5193 | |
| 5194 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 5195 | if (import_func == NULL) { |
| 5196 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 5197 | return NULL; |
| 5198 | } |
| 5199 | |
| 5200 | /* Fast path for not overloaded __import__. */ |
| 5201 | if (import_func == PyThreadState_GET()->interp->import_func) { |
| 5202 | int ilevel = _PyLong_AsInt(level); |
| 5203 | if (ilevel == -1 && PyErr_Occurred()) { |
| 5204 | return NULL; |
| 5205 | } |
| 5206 | res = PyImport_ImportModuleLevelObject( |
| 5207 | name, |
| 5208 | f->f_globals, |
| 5209 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5210 | fromlist, |
| 5211 | ilevel); |
| 5212 | return res; |
| 5213 | } |
| 5214 | |
| 5215 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5216 | |
| 5217 | stack[0] = name; |
| 5218 | stack[1] = f->f_globals; |
| 5219 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5220 | stack[3] = fromlist; |
| 5221 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5222 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5223 | Py_DECREF(import_func); |
| 5224 | return res; |
| 5225 | } |
| 5226 | |
| 5227 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5228 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5231 | _Py_IDENTIFIER(__name__); |
| 5232 | PyObject *fullmodname, *pkgname; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | x = PyObject_GetAttr(v, name); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5235 | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5236 | return x; |
| 5237 | /* Issue #17636: in case this failed because of a circular relative |
| 5238 | import, try to fallback on reading the module directly from |
| 5239 | sys.modules. */ |
| 5240 | PyErr_Clear(); |
| 5241 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5242 | if (pkgname == NULL) { |
| 5243 | goto error; |
| 5244 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5245 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
| 5246 | Py_DECREF(pkgname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5247 | if (fullmodname == NULL) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5248 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5249 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5250 | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5251 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5252 | if (x == NULL) { |
| 5253 | goto error; |
| 5254 | } |
| 5255 | Py_INCREF(x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5256 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5257 | error: |
| 5258 | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
| 5259 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5260 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5261 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5262 | static int |
| 5263 | import_all_from(PyObject *locals, PyObject *v) |
| 5264 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5265 | _Py_IDENTIFIER(__all__); |
| 5266 | _Py_IDENTIFIER(__dict__); |
| 5267 | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | PyObject *dict, *name, *value; |
| 5269 | int skip_leading_underscores = 0; |
| 5270 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | if (all == NULL) { |
| 5273 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5274 | return -1; /* Unexpected error */ |
| 5275 | PyErr_Clear(); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5276 | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5277 | if (dict == NULL) { |
| 5278 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5279 | return -1; |
| 5280 | PyErr_SetString(PyExc_ImportError, |
| 5281 | "from-import-* object has no __dict__ and no __all__"); |
| 5282 | return -1; |
| 5283 | } |
| 5284 | all = PyMapping_Keys(dict); |
| 5285 | Py_DECREF(dict); |
| 5286 | if (all == NULL) |
| 5287 | return -1; |
| 5288 | skip_leading_underscores = 1; |
| 5289 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5291 | for (pos = 0, err = 0; ; pos++) { |
| 5292 | name = PySequence_GetItem(all, pos); |
| 5293 | if (name == NULL) { |
| 5294 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 5295 | err = -1; |
| 5296 | else |
| 5297 | PyErr_Clear(); |
| 5298 | break; |
| 5299 | } |
| 5300 | if (skip_leading_underscores && |
| 5301 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5302 | PyUnicode_READY(name) != -1 && |
| 5303 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | { |
| 5305 | Py_DECREF(name); |
| 5306 | continue; |
| 5307 | } |
| 5308 | value = PyObject_GetAttr(v, name); |
| 5309 | if (value == NULL) |
| 5310 | err = -1; |
| 5311 | else if (PyDict_CheckExact(locals)) |
| 5312 | err = PyDict_SetItem(locals, name, value); |
| 5313 | else |
| 5314 | err = PyObject_SetItem(locals, name, value); |
| 5315 | Py_DECREF(name); |
| 5316 | Py_XDECREF(value); |
| 5317 | if (err != 0) |
| 5318 | break; |
| 5319 | } |
| 5320 | Py_DECREF(all); |
| 5321 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5324 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5325 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5327 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | if (!obj) |
| 5330 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5331 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5332 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5333 | if (!obj_str) |
| 5334 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5336 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5337 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5338 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5339 | static void |
| 5340 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5341 | { |
| 5342 | PyObject *name; |
| 5343 | /* Don't stomp existing exception */ |
| 5344 | if (PyErr_Occurred()) |
| 5345 | return; |
| 5346 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5347 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5348 | oparg); |
| 5349 | format_exc_check_arg( |
| 5350 | PyExc_UnboundLocalError, |
| 5351 | UNBOUNDLOCAL_ERROR_MSG, |
| 5352 | name); |
| 5353 | } else { |
| 5354 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5355 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5356 | format_exc_check_arg(PyExc_NameError, |
| 5357 | UNBOUNDFREE_ERROR_MSG, name); |
| 5358 | } |
| 5359 | } |
| 5360 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5361 | static PyObject * |
| 5362 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5363 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5364 | { |
| 5365 | PyObject *res; |
| 5366 | if (Py_REFCNT(v) == 2) { |
| 5367 | /* In the common case, there are 2 references to the value |
| 5368 | * stored in 'variable' when the += is performed: one on the |
| 5369 | * value stack (in 'v') and one still stored in the |
| 5370 | * 'variable'. We try to delete the variable now to reduce |
| 5371 | * the refcnt to 1. |
| 5372 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5373 | int opcode, oparg; |
| 5374 | NEXTOPARG(); |
| 5375 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5376 | case STORE_FAST: |
| 5377 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5378 | PyObject **fastlocals = f->f_localsplus; |
| 5379 | if (GETLOCAL(oparg) == v) |
| 5380 | SETLOCAL(oparg, NULL); |
| 5381 | break; |
| 5382 | } |
| 5383 | case STORE_DEREF: |
| 5384 | { |
| 5385 | PyObject **freevars = (f->f_localsplus + |
| 5386 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5387 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5388 | if (PyCell_GET(c) == v) { |
| 5389 | PyCell_SET(c, NULL); |
| 5390 | Py_DECREF(v); |
| 5391 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5392 | break; |
| 5393 | } |
| 5394 | case STORE_NAME: |
| 5395 | { |
| 5396 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5397 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5398 | PyObject *locals = f->f_locals; |
| 5399 | if (PyDict_CheckExact(locals) && |
| 5400 | PyDict_GetItem(locals, name) == v) { |
| 5401 | if (PyDict_DelItem(locals, name) != 0) { |
| 5402 | PyErr_Clear(); |
| 5403 | } |
| 5404 | } |
| 5405 | break; |
| 5406 | } |
| 5407 | } |
| 5408 | } |
| 5409 | res = v; |
| 5410 | PyUnicode_Append(&res, w); |
| 5411 | return res; |
| 5412 | } |
| 5413 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5414 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5415 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5416 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5417 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | int i; |
| 5420 | PyObject *l = PyList_New(256); |
| 5421 | if (l == NULL) return NULL; |
| 5422 | for (i = 0; i < 256; i++) { |
| 5423 | PyObject *x = PyLong_FromLong(a[i]); |
| 5424 | if (x == NULL) { |
| 5425 | Py_DECREF(l); |
| 5426 | return NULL; |
| 5427 | } |
| 5428 | PyList_SetItem(l, i, x); |
| 5429 | } |
| 5430 | for (i = 0; i < 256; i++) |
| 5431 | a[i] = 0; |
| 5432 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5436 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5437 | { |
| 5438 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5439 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5440 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | int i; |
| 5442 | PyObject *l = PyList_New(257); |
| 5443 | if (l == NULL) return NULL; |
| 5444 | for (i = 0; i < 257; i++) { |
| 5445 | PyObject *x = getarray(dxpairs[i]); |
| 5446 | if (x == NULL) { |
| 5447 | Py_DECREF(l); |
| 5448 | return NULL; |
| 5449 | } |
| 5450 | PyList_SetItem(l, i, x); |
| 5451 | } |
| 5452 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5453 | #endif |
| 5454 | } |
| 5455 | |
| 5456 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5457 | |
| 5458 | Py_ssize_t |
| 5459 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5460 | { |
| 5461 | PyThreadState *tstate = PyThreadState_Get(); |
| 5462 | Py_ssize_t new_index; |
| 5463 | |
| 5464 | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
| 5465 | return -1; |
| 5466 | } |
| 5467 | new_index = tstate->co_extra_user_count++; |
| 5468 | tstate->co_extra_freefuncs[new_index] = free; |
| 5469 | return new_index; |
| 5470 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5471 | |
| 5472 | static void |
| 5473 | dtrace_function_entry(PyFrameObject *f) |
| 5474 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5475 | const char *filename; |
| 5476 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5477 | int lineno; |
| 5478 | |
| 5479 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5480 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5481 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5482 | |
| 5483 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5484 | } |
| 5485 | |
| 5486 | static void |
| 5487 | dtrace_function_return(PyFrameObject *f) |
| 5488 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5489 | const char *filename; |
| 5490 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5491 | int lineno; |
| 5492 | |
| 5493 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5494 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5495 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5496 | |
| 5497 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5498 | } |
| 5499 | |
| 5500 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5501 | static void |
| 5502 | maybe_dtrace_line(PyFrameObject *frame, |
| 5503 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5504 | { |
| 5505 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5506 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5507 | |
| 5508 | /* If the last instruction executed isn't in the current |
| 5509 | instruction window, reset the window. |
| 5510 | */ |
| 5511 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5512 | PyAddrPair bounds; |
| 5513 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5514 | &bounds); |
| 5515 | *instr_lb = bounds.ap_lower; |
| 5516 | *instr_ub = bounds.ap_upper; |
| 5517 | } |
| 5518 | /* If the last instruction falls at the start of a line or if |
| 5519 | it represents a jump backwards, update the frame's line |
| 5520 | number and call the trace function. */ |
| 5521 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5522 | frame->f_lineno = line; |
| 5523 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5524 | if (!co_filename) |
| 5525 | co_filename = "?"; |
| 5526 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5527 | if (!co_name) |
| 5528 | co_name = "?"; |
| 5529 | PyDTrace_LINE(co_filename, co_name, line); |
| 5530 | } |
| 5531 | *instr_prev = frame->f_lasti; |
| 5532 | } |