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