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