Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
| 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 15 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 18 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 19 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
| 23 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 24 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 25 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 27 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 28 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | #define LLTRACE 1 /* Low-level trace feature */ |
| 30 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 33 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 35 | /* Forward declarations */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 36 | static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 37 | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 38 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 40 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 41 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 42 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 43 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 44 | static int call_trace(Py_tracefunc, PyObject *, |
| 45 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 47 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 48 | PyThreadState *, PyFrameObject *, |
| 49 | int, PyObject *); |
| 50 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 51 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 52 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 53 | PyThreadState *, PyFrameObject *, int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 54 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 55 | static void dtrace_function_entry(PyFrameObject *); |
| 56 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 58 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 59 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 60 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 61 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 62 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 63 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 64 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 65 | PyFrameObject *, const _Py_CODEUNIT *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 66 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 67 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 68 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 70 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 72 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | "free variable '%.200s' referenced before assignment" \ |
| 74 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 76 | /* Dynamic execution profile */ |
| 77 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 78 | #ifdef DXPAIRS |
| 79 | static long dxpairs[257][256]; |
| 80 | #define dxp dxpairs[256] |
| 81 | #else |
| 82 | static long dxp[256]; |
| 83 | #endif |
| 84 | #endif |
| 85 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 86 | #ifdef WITH_THREAD |
| 87 | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
| 88 | #else |
| 89 | #define GIL_REQUEST 0 |
| 90 | #endif |
| 91 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 92 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 93 | 1. We believe this is all right because the eval loop will release |
| 94 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 95 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | _Py_atomic_store_relaxed( \ |
| 97 | &eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 98 | GIL_REQUEST | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
| 100 | pending_async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 101 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 102 | #ifdef WITH_THREAD |
| 103 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 104 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | do { \ |
| 106 | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
| 107 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 108 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 109 | |
| 110 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | do { \ |
| 112 | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
| 113 | COMPUTE_EVAL_BREAKER(); \ |
| 114 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 116 | #endif |
| 117 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 118 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 119 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | do { \ |
| 121 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
| 122 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 123 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 124 | |
| 125 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | do { \ |
| 127 | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
| 128 | COMPUTE_EVAL_BREAKER(); \ |
| 129 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 130 | |
| 131 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | do { \ |
| 133 | pending_async_exc = 1; \ |
| 134 | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
| 135 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 136 | |
| 137 | #define UNSIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 139 | |
| 140 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 141 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 142 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 143 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 144 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 145 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 146 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 147 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 148 | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 149 | static long main_thread = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 150 | /* This single variable consolidates all requests to break out of the fast path |
| 151 | in the eval loop. */ |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 152 | static _Py_atomic_int eval_breaker = {0}; |
| 153 | /* Request for dropping the GIL */ |
| 154 | static _Py_atomic_int gil_drop_request = {0}; |
| 155 | /* Request for running pending calls. */ |
| 156 | static _Py_atomic_int pendingcalls_to_do = {0}; |
| 157 | /* Request for looking at the `async_exc` field of the current thread state. |
| 158 | Guarded by the GIL. */ |
| 159 | static int pending_async_exc = 0; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 160 | |
| 161 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 162 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 163 | int |
| 164 | PyEval_ThreadsInitialized(void) |
| 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 169 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 170 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (gil_created()) |
| 173 | return; |
| 174 | create_gil(); |
| 175 | take_gil(PyThreadState_GET()); |
| 176 | main_thread = PyThread_get_thread_ident(); |
| 177 | if (!pending_lock) |
| 178 | pending_lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 179 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 181 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 182 | _PyEval_FiniThreads(void) |
| 183 | { |
| 184 | if (!gil_created()) |
| 185 | return; |
| 186 | destroy_gil(); |
| 187 | assert(!gil_created()); |
| 188 | } |
| 189 | |
| 190 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 191 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | PyThreadState *tstate = PyThreadState_GET(); |
| 194 | if (tstate == NULL) |
| 195 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 196 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 200 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | /* This function must succeed when the current thread state is NULL. |
| 203 | We therefore avoid PyThreadState_GET() which dumps a fatal error |
| 204 | in debug mode. |
| 205 | */ |
| 206 | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
| 207 | &_PyThreadState_Current)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 211 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | if (tstate == NULL) |
| 214 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 215 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 216 | assert(gil_created()); |
| 217 | take_gil(tstate); |
| 218 | if (PyThreadState_Swap(tstate) != NULL) |
| 219 | Py_FatalError( |
| 220 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 224 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | if (tstate == NULL) |
| 227 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 228 | if (PyThreadState_Swap(NULL) != tstate) |
| 229 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 230 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 231 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 233 | /* This function is called from PyOS_AfterFork to destroy all threads which are |
| 234 | * not running in the child process, and clear internal locks which might be |
| 235 | * held by those threads. (This could also be done using pthread_atfork |
| 236 | * mechanism, at least for the pthreads implementation.) */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 237 | |
| 238 | void |
| 239 | PyEval_ReInitThreads(void) |
| 240 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 241 | _Py_IDENTIFIER(_after_fork); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | PyObject *threading, *result; |
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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* Update the threading module with the new state. |
| 253 | */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 254 | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | "threading"); |
| 256 | if (threading == NULL) { |
| 257 | /* threading not imported */ |
| 258 | PyErr_Clear(); |
| 259 | return; |
| 260 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 261 | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | if (result == NULL) |
| 263 | PyErr_WriteUnraisable(threading); |
| 264 | else |
| 265 | Py_DECREF(result); |
| 266 | Py_DECREF(threading); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 267 | |
| 268 | /* Destroy all threads except the current one */ |
| 269 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 270 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 271 | |
| 272 | #else |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 273 | static _Py_atomic_int eval_breaker = {0}; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 274 | static int pending_async_exc = 0; |
| 275 | #endif /* WITH_THREAD */ |
| 276 | |
| 277 | /* This function is used to signal that async exceptions are waiting to be |
| 278 | raised, therefore it is also useful in non-threaded builds. */ |
| 279 | |
| 280 | void |
| 281 | _PyEval_SignalAsyncExc(void) |
| 282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 284 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 286 | /* Functions save_thread and restore_thread are always defined so |
| 287 | dynamically loaded modules needn't be compiled separately for use |
| 288 | with and without threads: */ |
| 289 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 290 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 291 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 294 | if (tstate == NULL) |
| 295 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 296 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | if (gil_created()) |
| 298 | drop_gil(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 299 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 304 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | if (tstate == NULL) |
| 307 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 308 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | if (gil_created()) { |
| 310 | int err = errno; |
| 311 | take_gil(tstate); |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 312 | /* _Py_Finalizing is protected by the GIL */ |
| 313 | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
| 314 | drop_gil(tstate); |
| 315 | PyThread_exit_thread(); |
| 316 | assert(0); /* unreachable */ |
| 317 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | errno = err; |
| 319 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 320 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 325 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 326 | signal handlers or Mac I/O completion routines) can schedule calls |
| 327 | to a function to be called synchronously. |
| 328 | The synchronous function is called with one void* argument. |
| 329 | It should return 0 for success or -1 for failure -- failure should |
| 330 | be accompanied by an exception. |
| 331 | |
| 332 | If registry succeeds, the registry function returns 0; if it fails |
| 333 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 334 | an exception condition). |
| 335 | |
| 336 | Note that because registry may occur from within signal handlers, |
| 337 | or other asynchronous events, calling malloc() is unsafe! |
| 338 | |
| 339 | #ifdef WITH_THREAD |
| 340 | Any thread can schedule pending calls, but only the main thread |
| 341 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 342 | There is no facility to schedule calls to a particular thread, but |
| 343 | that should be easy to change, should that ever be required. In |
| 344 | that case, the static variables here should go into the python |
| 345 | threadstate. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 346 | #endif |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 347 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 348 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 349 | #ifdef WITH_THREAD |
| 350 | |
| 351 | /* The WITH_THREAD implementation is thread-safe. It allows |
| 352 | scheduling to be made from any thread, and even from an executing |
| 353 | callback. |
| 354 | */ |
| 355 | |
| 356 | #define NPENDINGCALLS 32 |
| 357 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | int (*func)(void *); |
| 359 | void *arg; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 360 | } pendingcalls[NPENDINGCALLS]; |
| 361 | static int pendingfirst = 0; |
| 362 | static int pendinglast = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 363 | |
| 364 | int |
| 365 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | int i, j, result=0; |
| 368 | PyThread_type_lock lock = pending_lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | /* try a few times for the lock. Since this mechanism is used |
| 371 | * for signal handling (on the main thread), there is a (slim) |
| 372 | * chance that a signal is delivered on the same thread while we |
| 373 | * hold the lock during the Py_MakePendingCalls() function. |
| 374 | * This avoids a deadlock in that case. |
| 375 | * Note that signals can be delivered on any thread. In particular, |
| 376 | * on Windows, a SIGINT is delivered on a system-created worker |
| 377 | * thread. |
| 378 | * We also check for lock being NULL, in the unlikely case that |
| 379 | * this function is called before any bytecode evaluation takes place. |
| 380 | */ |
| 381 | if (lock != NULL) { |
| 382 | for (i = 0; i<100; i++) { |
| 383 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 384 | break; |
| 385 | } |
| 386 | if (i == 100) |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | i = pendinglast; |
| 391 | j = (i + 1) % NPENDINGCALLS; |
| 392 | if (j == pendingfirst) { |
| 393 | result = -1; /* Queue full */ |
| 394 | } else { |
| 395 | pendingcalls[i].func = func; |
| 396 | pendingcalls[i].arg = arg; |
| 397 | pendinglast = j; |
| 398 | } |
| 399 | /* signal main loop */ |
| 400 | SIGNAL_PENDING_CALLS(); |
| 401 | if (lock != NULL) |
| 402 | PyThread_release_lock(lock); |
| 403 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int |
| 407 | Py_MakePendingCalls(void) |
| 408 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 409 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | int i; |
| 411 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | if (!pending_lock) { |
| 414 | /* initial allocation of the lock */ |
| 415 | pending_lock = PyThread_allocate_lock(); |
| 416 | if (pending_lock == NULL) |
| 417 | return -1; |
| 418 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | /* only service pending calls on main thread */ |
| 421 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
| 422 | return 0; |
| 423 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 424 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 426 | busy = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | /* perform a bounded number of calls, in case of recursion */ |
| 428 | for (i=0; i<NPENDINGCALLS; i++) { |
| 429 | int j; |
| 430 | int (*func)(void *); |
| 431 | void *arg = NULL; |
| 432 | |
| 433 | /* pop one item off the queue while holding the lock */ |
| 434 | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
| 435 | j = pendingfirst; |
| 436 | if (j == pendinglast) { |
| 437 | func = NULL; /* Queue empty */ |
| 438 | } else { |
| 439 | func = pendingcalls[j].func; |
| 440 | arg = pendingcalls[j].arg; |
| 441 | pendingfirst = (j + 1) % NPENDINGCALLS; |
| 442 | } |
| 443 | if (pendingfirst != pendinglast) |
| 444 | SIGNAL_PENDING_CALLS(); |
| 445 | else |
| 446 | UNSIGNAL_PENDING_CALLS(); |
| 447 | PyThread_release_lock(pending_lock); |
| 448 | /* having released the lock, perform the callback */ |
| 449 | if (func == NULL) |
| 450 | break; |
| 451 | r = func(arg); |
| 452 | if (r) |
| 453 | break; |
| 454 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 455 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | return r; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | #else /* if ! defined WITH_THREAD */ |
| 460 | |
| 461 | /* |
| 462 | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 463 | This code is used for signal handling in python that isn't built |
| 464 | with WITH_THREAD. |
| 465 | Don't use this implementation when Py_AddPendingCalls() can happen |
| 466 | on a different thread! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 468 | There are two possible race conditions: |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 469 | (1) nested asynchronous calls to Py_AddPendingCall() |
| 470 | (2) AddPendingCall() calls made while pending calls are being processed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 472 | (1) is very unlikely because typically signal delivery |
| 473 | is blocked during signal handling. So it should be impossible. |
| 474 | (2) is a real possibility. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 475 | The current code is safe against (2), but not against (1). |
| 476 | The safety against (2) is derived from the fact that only one |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 477 | thread is present, interrupted by signals, and that the critical |
| 478 | section is protected with the "busy" variable. On Windows, which |
| 479 | delivers SIGINT on a system thread, this does not hold and therefore |
| 480 | Windows really shouldn't use this version. |
| 481 | The two threads could theoretically wiggle around the "busy" variable. |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 482 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 484 | #define NPENDINGCALLS 32 |
| 485 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | int (*func)(void *); |
| 487 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 488 | } pendingcalls[NPENDINGCALLS]; |
| 489 | static volatile int pendingfirst = 0; |
| 490 | static volatile int pendinglast = 0; |
Benjamin Peterson | 08ec84c | 2010-05-30 14:49:32 +0000 | [diff] [blame] | 491 | static _Py_atomic_int pendingcalls_to_do = {0}; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 492 | |
| 493 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 494 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | static volatile int busy = 0; |
| 497 | int i, j; |
| 498 | /* XXX Begin critical section */ |
| 499 | if (busy) |
| 500 | return -1; |
| 501 | busy = 1; |
| 502 | i = pendinglast; |
| 503 | j = (i + 1) % NPENDINGCALLS; |
| 504 | if (j == pendingfirst) { |
| 505 | busy = 0; |
| 506 | return -1; /* Queue full */ |
| 507 | } |
| 508 | pendingcalls[i].func = func; |
| 509 | pendingcalls[i].arg = arg; |
| 510 | pendinglast = j; |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | SIGNAL_PENDING_CALLS(); |
| 513 | busy = 0; |
| 514 | /* XXX End critical section */ |
| 515 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 518 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 519 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | static int busy = 0; |
| 522 | if (busy) |
| 523 | return 0; |
| 524 | busy = 1; |
| 525 | UNSIGNAL_PENDING_CALLS(); |
| 526 | for (;;) { |
| 527 | int i; |
| 528 | int (*func)(void *); |
| 529 | void *arg; |
| 530 | i = pendingfirst; |
| 531 | if (i == pendinglast) |
| 532 | break; /* Queue empty */ |
| 533 | func = pendingcalls[i].func; |
| 534 | arg = pendingcalls[i].arg; |
| 535 | pendingfirst = (i + 1) % NPENDINGCALLS; |
| 536 | if (func(arg) < 0) { |
| 537 | busy = 0; |
| 538 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 539 | return -1; |
| 540 | } |
| 541 | } |
| 542 | busy = 0; |
| 543 | return 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 546 | #endif /* WITH_THREAD */ |
| 547 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 548 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 549 | /* The interpreter's recursion limit */ |
| 550 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 551 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 552 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 553 | #endif |
| 554 | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 555 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 556 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 557 | int |
| 558 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | return recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 563 | void |
| 564 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | recursion_limit = new_limit; |
| 567 | _Py_CheckRecursionLimit = recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 570 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 571 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 572 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 573 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 574 | Without USE_STACKCHECK, there is no need for this. */ |
| 575 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 576 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | PyThreadState *tstate = PyThreadState_GET(); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 579 | |
| 580 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | if (PyOS_CheckStack()) { |
| 582 | --tstate->recursion_depth; |
| 583 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 584 | return -1; |
| 585 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 586 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | _Py_CheckRecursionLimit = recursion_limit; |
| 588 | if (tstate->recursion_critical) |
| 589 | /* Somebody asked that we don't check for recursion. */ |
| 590 | return 0; |
| 591 | if (tstate->overflowed) { |
| 592 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 593 | /* Overflowing while handling an overflow. Give up. */ |
| 594 | Py_FatalError("Cannot recover from stack overflow."); |
| 595 | } |
| 596 | return 0; |
| 597 | } |
| 598 | if (tstate->recursion_depth > recursion_limit) { |
| 599 | --tstate->recursion_depth; |
| 600 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 601 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | "maximum recursion depth exceeded%s", |
| 603 | where); |
| 604 | return -1; |
| 605 | } |
| 606 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 609 | /* Status code for main loop (reason for stack unwind) */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 610 | enum why_code { |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 611 | WHY_NOT = 0x0001, /* No error */ |
| 612 | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 613 | WHY_RETURN = 0x0008, /* 'return' statement */ |
| 614 | WHY_BREAK = 0x0010, /* 'break' statement */ |
| 615 | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
| 616 | WHY_YIELD = 0x0040, /* 'yield' operator */ |
| 617 | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
Raymond Hettinger | 7c95865 | 2004-04-06 10:11:10 +0000 | [diff] [blame] | 618 | }; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 619 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 620 | static void save_exc_state(PyThreadState *, PyFrameObject *); |
| 621 | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
| 622 | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 623 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 624 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 625 | |
Jeffrey Yasskin | 008d8ef | 2008-12-06 17:09:27 +0000 | [diff] [blame] | 626 | /* Records whether tracing is on for any thread. Counts the number of |
| 627 | threads for which tstate->c_tracefunc is non-NULL, so if the value |
| 628 | is 0, we know we don't have to check this thread's c_tracefunc. |
| 629 | This speeds up the if statement in PyEval_EvalFrameEx() after |
| 630 | fast_next_opcode*/ |
| 631 | static int _Py_TracingPossible = 0; |
| 632 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 634 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 635 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 636 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | return PyEval_EvalCodeEx(co, |
| 639 | globals, locals, |
| 640 | (PyObject **)NULL, 0, |
| 641 | (PyObject **)NULL, 0, |
| 642 | (PyObject **)NULL, 0, |
| 643 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
| 647 | /* Interpreter main loop */ |
| 648 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 649 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 650 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | /* This is for backward compatibility with extension modules that |
| 652 | used this API; core interpreter code should call |
| 653 | PyEval_EvalFrameEx() */ |
| 654 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 658 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 659 | { |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 660 | PyThreadState *tstate = PyThreadState_GET(); |
| 661 | return tstate->interp->eval_frame(f, throwflag); |
| 662 | } |
| 663 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 664 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 665 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 666 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 667 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 669 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 670 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 671 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 672 | int opcode; /* Current opcode */ |
| 673 | int oparg; /* Current opcode argument, if any */ |
| 674 | enum why_code why; /* Reason for block stack unwind */ |
| 675 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | PyObject *retval = NULL; /* Return value */ |
| 677 | PyThreadState *tstate = PyThreadState_GET(); |
| 678 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | is true when the line being executed has changed. The |
| 685 | initial values are such as to make this false the first |
| 686 | time it is tested. */ |
| 687 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 688 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 689 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | PyObject *names; |
| 691 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 692 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 693 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 694 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 695 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 696 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 697 | /* Computed GOTOs, or |
| 698 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 699 | using gcc's labels-as-values extension |
| 700 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 701 | |
| 702 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 704 | combined with a lookup table of jump addresses. However, since the |
| 705 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 706 | hard time making the right prediction for where to jump next (actually, |
| 707 | it will be always wrong except in the uncommon case of a sequence of |
| 708 | several identical opcodes). |
| 709 | |
| 710 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 711 | indirect jump instruction at the end of each opcode. Since the jump |
| 712 | instruction is at a different address for each opcode, the CPU will make a |
| 713 | separate prediction for each of these instructions, which is equivalent to |
| 714 | predicting the second opcode of each opcode pair. These predictions have |
| 715 | a much better chance to turn out valid, especially in small bytecode loops. |
| 716 | |
| 717 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 719 | and potentially many more instructions (depending on the pipeline width). |
| 720 | A correctly predicted branch, however, is nearly free. |
| 721 | |
| 722 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 723 | faster than the normal "switch" version, depending on the compiler and the |
| 724 | CPU architecture. |
| 725 | |
| 726 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 727 | because it would render the measurements invalid. |
| 728 | |
| 729 | |
| 730 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 731 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 732 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 733 | -fno-crossjumping). |
| 734 | */ |
| 735 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 736 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 737 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 738 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 739 | #endif |
| 740 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 741 | #ifdef HAVE_COMPUTED_GOTOS |
| 742 | #ifndef USE_COMPUTED_GOTOS |
| 743 | #define USE_COMPUTED_GOTOS 1 |
| 744 | #endif |
| 745 | #else |
| 746 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 747 | #error "Computed gotos are not supported on this compiler." |
| 748 | #endif |
| 749 | #undef USE_COMPUTED_GOTOS |
| 750 | #define USE_COMPUTED_GOTOS 0 |
| 751 | #endif |
| 752 | |
| 753 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 754 | /* Import the static jump table */ |
| 755 | #include "opcode_targets.h" |
| 756 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 757 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | TARGET_##op: \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | case op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 761 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | { \ |
| 763 | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
| 764 | FAST_DISPATCH(); \ |
| 765 | } \ |
| 766 | continue; \ |
| 767 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 768 | |
| 769 | #ifdef LLTRACE |
| 770 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 772 | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 774 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 775 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | } \ |
| 777 | goto fast_next_opcode; \ |
| 778 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 779 | #else |
| 780 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 782 | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 784 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 785 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | } \ |
| 787 | goto fast_next_opcode; \ |
| 788 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 789 | #endif |
| 790 | |
| 791 | #else |
| 792 | #define TARGET(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | case op: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 794 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 795 | #define DISPATCH() continue |
| 796 | #define FAST_DISPATCH() goto fast_next_opcode |
| 797 | #endif |
| 798 | |
| 799 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 800 | /* Tuple access macros */ |
| 801 | |
| 802 | #ifndef Py_DEBUG |
| 803 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 804 | #else |
| 805 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 806 | #endif |
| 807 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 808 | /* Code access macros */ |
| 809 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 810 | /* The integer overflow is checked by an assertion below. */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 811 | #define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 812 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 813 | _Py_CODEUNIT word = *next_instr; \ |
| 814 | opcode = _Py_OPCODE(word); \ |
| 815 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 816 | next_instr++; \ |
| 817 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 818 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 819 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 820 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 821 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | Some opcodes tend to come in pairs thus making it possible to |
| 823 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 824 | 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] | 825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | Verifying the prediction costs a single high-speed test of a register |
| 827 | variable against a constant. If the pairing was good, then the |
| 828 | processor's own internal branch predication has a high likelihood of |
| 829 | success, resulting in a nearly zero-overhead transition to the |
| 830 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 831 | including its unpredictable switch-case branch. Combined with the |
| 832 | processor's internal branch prediction, a successful PREDICT has the |
| 833 | effect of making the two opcodes run as if they were a single new opcode |
| 834 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 835 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 836 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | predictions turned-on and interpret the results as if some opcodes |
| 838 | had been combined or turn-off predictions so that the opcode frequency |
| 839 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 840 | |
| 841 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | the CPU to record separate branch prediction information for each |
| 843 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 844 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 845 | */ |
| 846 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 847 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 849 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 850 | #define PREDICT(op) \ |
| 851 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 852 | _Py_CODEUNIT word = *next_instr; \ |
| 853 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 854 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 855 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 856 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 857 | goto PRED_##op; \ |
| 858 | } \ |
| 859 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 860 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 861 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 862 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 863 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 864 | /* Stack manipulation macros */ |
| 865 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 866 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 867 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 868 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 869 | #define EMPTY() (STACK_LEVEL() == 0) |
| 870 | #define TOP() (stack_pointer[-1]) |
| 871 | #define SECOND() (stack_pointer[-2]) |
| 872 | #define THIRD() (stack_pointer[-3]) |
| 873 | #define FOURTH() (stack_pointer[-4]) |
| 874 | #define PEEK(n) (stack_pointer[-(n)]) |
| 875 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 876 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 877 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 878 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 879 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 880 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 881 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 882 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 883 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 884 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 886 | lltrace && prtrace(TOP(), "push")); \ |
| 887 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 889 | BASIC_POP()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 891 | lltrace && prtrace(TOP(), "stackadj")); \ |
| 892 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 893 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 894 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 895 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 896 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 897 | #define PUSH(v) BASIC_PUSH(v) |
| 898 | #define POP() BASIC_POP() |
| 899 | #define STACKADJ(n) BASIC_STACKADJ(n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 900 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 901 | #endif |
| 902 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 903 | /* Local variable macros */ |
| 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 906 | |
| 907 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 908 | then store the new value; it must copy the old value to a temporary |
| 909 | value, then store the new value, and then DECREF the temporary value. |
| 910 | This is because it is possible that during the DECREF the frame is |
| 911 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 912 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 914 | GETLOCAL(i) = value; \ |
| 915 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 916 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 917 | |
| 918 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 920 | PyObject *v = POP(); \ |
| 921 | Py_XDECREF(v); \ |
| 922 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 923 | |
| 924 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 925 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | PyObject *type, *value, *traceback; \ |
| 927 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 928 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 929 | value = POP(); \ |
| 930 | Py_XDECREF(value); \ |
| 931 | } \ |
| 932 | type = tstate->exc_type; \ |
| 933 | value = tstate->exc_value; \ |
| 934 | traceback = tstate->exc_traceback; \ |
| 935 | tstate->exc_type = POP(); \ |
| 936 | tstate->exc_value = POP(); \ |
| 937 | tstate->exc_traceback = POP(); \ |
| 938 | Py_XDECREF(type); \ |
| 939 | Py_XDECREF(value); \ |
| 940 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 941 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 942 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 943 | /* Start of code */ |
| 944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | /* push frame */ |
| 946 | if (Py_EnterRecursiveCall("")) |
| 947 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | if (tstate->use_tracing) { |
| 952 | if (tstate->c_tracefunc != NULL) { |
| 953 | /* tstate->c_tracefunc, if defined, is a |
| 954 | function that will be called on *every* entry |
| 955 | to a code block. Its return value, if not |
| 956 | None, is a function that will be called at |
| 957 | the start of each executed line of code. |
| 958 | (Actually, the function must return itself |
| 959 | in order to continue tracing.) The trace |
| 960 | functions are called with three arguments: |
| 961 | a pointer to the current frame, a string |
| 962 | indicating why the function is called, and |
| 963 | an argument which depends on the situation. |
| 964 | The global trace function is also called |
| 965 | whenever an exception is detected. */ |
| 966 | if (call_trace_protected(tstate->c_tracefunc, |
| 967 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 968 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | /* Trace function raised an error */ |
| 970 | goto exit_eval_frame; |
| 971 | } |
| 972 | } |
| 973 | if (tstate->c_profilefunc != NULL) { |
| 974 | /* Similar for c_profilefunc, except it needn't |
| 975 | return itself and isn't called for "line" events */ |
| 976 | if (call_trace_protected(tstate->c_profilefunc, |
| 977 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 978 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | /* Profile function raised an error */ |
| 980 | goto exit_eval_frame; |
| 981 | } |
| 982 | } |
| 983 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 984 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 985 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 986 | dtrace_function_entry(f); |
| 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | co = f->f_code; |
| 989 | names = co->co_names; |
| 990 | consts = co->co_consts; |
| 991 | fastlocals = f->f_localsplus; |
| 992 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 993 | assert(PyBytes_Check(co->co_code)); |
| 994 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 995 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 996 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 997 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 998 | /* |
| 999 | f->f_lasti refers to the index of the last instruction, |
| 1000 | 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] | 1001 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1002 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1003 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1006 | direct succession without updating f->f_lasti. A successful |
| 1007 | prediction effectively links the two codes together as if they |
| 1008 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1009 | the first code in the pair (for instance, GET_ITER followed by |
| 1010 | 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] | 1011 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1013 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1014 | next_instr = first_instr; |
| 1015 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1016 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1017 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1018 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | stack_pointer = f->f_stacktop; |
| 1020 | assert(stack_pointer != NULL); |
| 1021 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1022 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1023 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1024 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 1025 | 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] | 1026 | /* We were in an except handler when we left, |
| 1027 | restore the exception state which was put aside |
| 1028 | (see YIELD_VALUE). */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1029 | swap_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | } |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 1031 | else |
| 1032 | save_exc_state(tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1034 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1035 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1036 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1037 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | why = WHY_NOT; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1040 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1041 | if (throwflag) /* support for generator.throw() */ |
| 1042 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1043 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1044 | #ifdef Py_DEBUG |
| 1045 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
| 1046 | because it may clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1047 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1048 | assert(!PyErr_Occurred()); |
| 1049 | #endif |
| 1050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1053 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1054 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | /* Do periodic things. Doing this every time through |
| 1057 | the loop would add too much overhead, so we do it |
| 1058 | only every Nth instruction. We also do it if |
| 1059 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1060 | event needs attention (e.g. a signal handler or |
| 1061 | async I/O handler); see Py_AddPendingCall() and |
| 1062 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1065 | if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | /* Make the last opcode before |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1067 | a try: finally: block uninterruptible. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | goto fast_next_opcode; |
| 1069 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1071 | if (Py_MakePendingCalls() < 0) |
| 1072 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | } |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 1074 | #ifdef WITH_THREAD |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1075 | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | /* Give another thread a chance */ |
| 1077 | if (PyThreadState_Swap(NULL) != tstate) |
| 1078 | Py_FatalError("ceval: tstate mix-up"); |
| 1079 | drop_gil(tstate); |
| 1080 | |
| 1081 | /* Other threads may run now */ |
| 1082 | |
| 1083 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1084 | |
| 1085 | /* Check if we should make a quick exit. */ |
| 1086 | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
| 1087 | drop_gil(tstate); |
| 1088 | PyThread_exit_thread(); |
| 1089 | } |
| 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | if (PyThreadState_Swap(tstate) != NULL) |
| 1092 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | } |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 1094 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | /* Check for asynchronous exceptions. */ |
| 1096 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1097 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | tstate->async_exc = NULL; |
| 1099 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1100 | PyErr_SetNone(exc); |
| 1101 | Py_DECREF(exc); |
| 1102 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | fast_next_opcode: |
| 1107 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1108 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1109 | if (PyDTrace_LINE_ENABLED()) |
| 1110 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1115 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1116 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | /* see maybe_call_line_trace |
| 1118 | for expository comments */ |
| 1119 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1122 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1123 | tstate, f, |
| 1124 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | /* Reload possibly changed frame fields */ |
| 1126 | JUMPTO(f->f_lasti); |
| 1127 | if (f->f_stacktop != NULL) { |
| 1128 | stack_pointer = f->f_stacktop; |
| 1129 | f->f_stacktop = NULL; |
| 1130 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1131 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1133 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1137 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1138 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1139 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1140 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1141 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | dxpairs[lastopcode][opcode]++; |
| 1143 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1144 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1146 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1147 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1148 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | if (lltrace) { |
| 1152 | if (HAS_ARG(opcode)) { |
| 1153 | printf("%d: %d, %d\n", |
| 1154 | f->f_lasti, opcode, oparg); |
| 1155 | } |
| 1156 | else { |
| 1157 | printf("%d: %d\n", |
| 1158 | f->f_lasti, opcode); |
| 1159 | } |
| 1160 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1161 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | switch (opcode) { |
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 | /* BEWARE! |
| 1166 | It is essential that any operation that fails sets either |
| 1167 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 1168 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | TARGET(NOP) |
| 1171 | FAST_DISPATCH(); |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1172 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1173 | TARGET(LOAD_FAST) { |
| 1174 | PyObject *value = GETLOCAL(oparg); |
| 1175 | if (value == NULL) { |
| 1176 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1177 | UNBOUNDLOCAL_ERROR_MSG, |
| 1178 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1179 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1181 | Py_INCREF(value); |
| 1182 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1186 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1187 | TARGET(LOAD_CONST) { |
| 1188 | PyObject *value = GETITEM(consts, oparg); |
| 1189 | Py_INCREF(value); |
| 1190 | PUSH(value); |
| 1191 | FAST_DISPATCH(); |
| 1192 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1193 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1194 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1195 | TARGET(STORE_FAST) { |
| 1196 | PyObject *value = POP(); |
| 1197 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1199 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1200 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1201 | TARGET(POP_TOP) { |
| 1202 | PyObject *value = POP(); |
| 1203 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1205 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1206 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1207 | TARGET(ROT_TWO) { |
| 1208 | PyObject *top = TOP(); |
| 1209 | PyObject *second = SECOND(); |
| 1210 | SET_TOP(second); |
| 1211 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1213 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1214 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1215 | TARGET(ROT_THREE) { |
| 1216 | PyObject *top = TOP(); |
| 1217 | PyObject *second = SECOND(); |
| 1218 | PyObject *third = THIRD(); |
| 1219 | SET_TOP(second); |
| 1220 | SET_SECOND(third); |
| 1221 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1223 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1224 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1225 | TARGET(DUP_TOP) { |
| 1226 | PyObject *top = TOP(); |
| 1227 | Py_INCREF(top); |
| 1228 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1230 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1231 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1232 | TARGET(DUP_TOP_TWO) { |
| 1233 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1234 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1235 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1236 | Py_INCREF(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1237 | STACKADJ(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1238 | SET_TOP(top); |
| 1239 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1240 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1241 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1242 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1243 | TARGET(UNARY_POSITIVE) { |
| 1244 | PyObject *value = TOP(); |
| 1245 | PyObject *res = PyNumber_Positive(value); |
| 1246 | Py_DECREF(value); |
| 1247 | SET_TOP(res); |
| 1248 | if (res == NULL) |
| 1249 | goto error; |
| 1250 | DISPATCH(); |
| 1251 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1252 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1253 | TARGET(UNARY_NEGATIVE) { |
| 1254 | PyObject *value = TOP(); |
| 1255 | PyObject *res = PyNumber_Negative(value); |
| 1256 | Py_DECREF(value); |
| 1257 | SET_TOP(res); |
| 1258 | if (res == NULL) |
| 1259 | goto error; |
| 1260 | DISPATCH(); |
| 1261 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1262 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1263 | TARGET(UNARY_NOT) { |
| 1264 | PyObject *value = TOP(); |
| 1265 | int err = PyObject_IsTrue(value); |
| 1266 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | if (err == 0) { |
| 1268 | Py_INCREF(Py_True); |
| 1269 | SET_TOP(Py_True); |
| 1270 | DISPATCH(); |
| 1271 | } |
| 1272 | else if (err > 0) { |
| 1273 | Py_INCREF(Py_False); |
| 1274 | SET_TOP(Py_False); |
| 1275 | err = 0; |
| 1276 | DISPATCH(); |
| 1277 | } |
| 1278 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1279 | goto error; |
| 1280 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1281 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1282 | TARGET(UNARY_INVERT) { |
| 1283 | PyObject *value = TOP(); |
| 1284 | PyObject *res = PyNumber_Invert(value); |
| 1285 | Py_DECREF(value); |
| 1286 | SET_TOP(res); |
| 1287 | if (res == NULL) |
| 1288 | goto error; |
| 1289 | DISPATCH(); |
| 1290 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1291 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1292 | TARGET(BINARY_POWER) { |
| 1293 | PyObject *exp = POP(); |
| 1294 | PyObject *base = TOP(); |
| 1295 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1296 | Py_DECREF(base); |
| 1297 | Py_DECREF(exp); |
| 1298 | SET_TOP(res); |
| 1299 | if (res == NULL) |
| 1300 | goto error; |
| 1301 | DISPATCH(); |
| 1302 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1303 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1304 | TARGET(BINARY_MULTIPLY) { |
| 1305 | PyObject *right = POP(); |
| 1306 | PyObject *left = TOP(); |
| 1307 | PyObject *res = PyNumber_Multiply(left, right); |
| 1308 | Py_DECREF(left); |
| 1309 | Py_DECREF(right); |
| 1310 | SET_TOP(res); |
| 1311 | if (res == NULL) |
| 1312 | goto error; |
| 1313 | DISPATCH(); |
| 1314 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1315 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1316 | TARGET(BINARY_MATRIX_MULTIPLY) { |
| 1317 | PyObject *right = POP(); |
| 1318 | PyObject *left = TOP(); |
| 1319 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1320 | Py_DECREF(left); |
| 1321 | Py_DECREF(right); |
| 1322 | SET_TOP(res); |
| 1323 | if (res == NULL) |
| 1324 | goto error; |
| 1325 | DISPATCH(); |
| 1326 | } |
| 1327 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1328 | TARGET(BINARY_TRUE_DIVIDE) { |
| 1329 | PyObject *divisor = POP(); |
| 1330 | PyObject *dividend = TOP(); |
| 1331 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1332 | Py_DECREF(dividend); |
| 1333 | Py_DECREF(divisor); |
| 1334 | SET_TOP(quotient); |
| 1335 | if (quotient == NULL) |
| 1336 | goto error; |
| 1337 | DISPATCH(); |
| 1338 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1339 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1340 | TARGET(BINARY_FLOOR_DIVIDE) { |
| 1341 | PyObject *divisor = POP(); |
| 1342 | PyObject *dividend = TOP(); |
| 1343 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1344 | Py_DECREF(dividend); |
| 1345 | Py_DECREF(divisor); |
| 1346 | SET_TOP(quotient); |
| 1347 | if (quotient == NULL) |
| 1348 | goto error; |
| 1349 | DISPATCH(); |
| 1350 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1351 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1352 | TARGET(BINARY_MODULO) { |
| 1353 | PyObject *divisor = POP(); |
| 1354 | PyObject *dividend = TOP(); |
| 1355 | PyObject *res = PyUnicode_CheckExact(dividend) ? |
| 1356 | PyUnicode_Format(dividend, divisor) : |
| 1357 | PyNumber_Remainder(dividend, divisor); |
| 1358 | Py_DECREF(divisor); |
| 1359 | Py_DECREF(dividend); |
| 1360 | SET_TOP(res); |
| 1361 | if (res == NULL) |
| 1362 | goto error; |
| 1363 | DISPATCH(); |
| 1364 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1365 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1366 | TARGET(BINARY_ADD) { |
| 1367 | PyObject *right = POP(); |
| 1368 | PyObject *left = TOP(); |
| 1369 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1370 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1371 | CPython using bytecode, it is simply worthless. |
| 1372 | See http://bugs.python.org/issue21955 and |
| 1373 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1374 | no patch shown any impact on a realistic benchmark, only a minor |
| 1375 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1376 | if (PyUnicode_CheckExact(left) && |
| 1377 | PyUnicode_CheckExact(right)) { |
| 1378 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1379 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1380 | } |
| 1381 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1382 | sum = PyNumber_Add(left, right); |
| 1383 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1384 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1385 | Py_DECREF(right); |
| 1386 | SET_TOP(sum); |
| 1387 | if (sum == NULL) |
| 1388 | goto error; |
| 1389 | DISPATCH(); |
| 1390 | } |
| 1391 | |
| 1392 | TARGET(BINARY_SUBTRACT) { |
| 1393 | PyObject *right = POP(); |
| 1394 | PyObject *left = TOP(); |
| 1395 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1396 | Py_DECREF(right); |
| 1397 | Py_DECREF(left); |
| 1398 | SET_TOP(diff); |
| 1399 | if (diff == NULL) |
| 1400 | goto error; |
| 1401 | DISPATCH(); |
| 1402 | } |
| 1403 | |
| 1404 | TARGET(BINARY_SUBSCR) { |
| 1405 | PyObject *sub = POP(); |
| 1406 | PyObject *container = TOP(); |
| 1407 | PyObject *res = PyObject_GetItem(container, sub); |
| 1408 | Py_DECREF(container); |
| 1409 | Py_DECREF(sub); |
| 1410 | SET_TOP(res); |
| 1411 | if (res == NULL) |
| 1412 | goto error; |
| 1413 | DISPATCH(); |
| 1414 | } |
| 1415 | |
| 1416 | TARGET(BINARY_LSHIFT) { |
| 1417 | PyObject *right = POP(); |
| 1418 | PyObject *left = TOP(); |
| 1419 | PyObject *res = PyNumber_Lshift(left, right); |
| 1420 | Py_DECREF(left); |
| 1421 | Py_DECREF(right); |
| 1422 | SET_TOP(res); |
| 1423 | if (res == NULL) |
| 1424 | goto error; |
| 1425 | DISPATCH(); |
| 1426 | } |
| 1427 | |
| 1428 | TARGET(BINARY_RSHIFT) { |
| 1429 | PyObject *right = POP(); |
| 1430 | PyObject *left = TOP(); |
| 1431 | PyObject *res = PyNumber_Rshift(left, right); |
| 1432 | Py_DECREF(left); |
| 1433 | Py_DECREF(right); |
| 1434 | SET_TOP(res); |
| 1435 | if (res == NULL) |
| 1436 | goto error; |
| 1437 | DISPATCH(); |
| 1438 | } |
| 1439 | |
| 1440 | TARGET(BINARY_AND) { |
| 1441 | PyObject *right = POP(); |
| 1442 | PyObject *left = TOP(); |
| 1443 | PyObject *res = PyNumber_And(left, right); |
| 1444 | Py_DECREF(left); |
| 1445 | Py_DECREF(right); |
| 1446 | SET_TOP(res); |
| 1447 | if (res == NULL) |
| 1448 | goto error; |
| 1449 | DISPATCH(); |
| 1450 | } |
| 1451 | |
| 1452 | TARGET(BINARY_XOR) { |
| 1453 | PyObject *right = POP(); |
| 1454 | PyObject *left = TOP(); |
| 1455 | PyObject *res = PyNumber_Xor(left, right); |
| 1456 | Py_DECREF(left); |
| 1457 | Py_DECREF(right); |
| 1458 | SET_TOP(res); |
| 1459 | if (res == NULL) |
| 1460 | goto error; |
| 1461 | DISPATCH(); |
| 1462 | } |
| 1463 | |
| 1464 | TARGET(BINARY_OR) { |
| 1465 | PyObject *right = POP(); |
| 1466 | PyObject *left = TOP(); |
| 1467 | PyObject *res = PyNumber_Or(left, right); |
| 1468 | Py_DECREF(left); |
| 1469 | Py_DECREF(right); |
| 1470 | SET_TOP(res); |
| 1471 | if (res == NULL) |
| 1472 | goto error; |
| 1473 | DISPATCH(); |
| 1474 | } |
| 1475 | |
| 1476 | TARGET(LIST_APPEND) { |
| 1477 | PyObject *v = POP(); |
| 1478 | PyObject *list = PEEK(oparg); |
| 1479 | int err; |
| 1480 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1482 | if (err != 0) |
| 1483 | goto error; |
| 1484 | PREDICT(JUMP_ABSOLUTE); |
| 1485 | DISPATCH(); |
| 1486 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1487 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1488 | TARGET(SET_ADD) { |
| 1489 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1490 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1491 | int err; |
| 1492 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1494 | if (err != 0) |
| 1495 | goto error; |
| 1496 | PREDICT(JUMP_ABSOLUTE); |
| 1497 | DISPATCH(); |
| 1498 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1499 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1500 | TARGET(INPLACE_POWER) { |
| 1501 | PyObject *exp = POP(); |
| 1502 | PyObject *base = TOP(); |
| 1503 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1504 | Py_DECREF(base); |
| 1505 | Py_DECREF(exp); |
| 1506 | SET_TOP(res); |
| 1507 | if (res == NULL) |
| 1508 | goto error; |
| 1509 | DISPATCH(); |
| 1510 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1511 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1512 | TARGET(INPLACE_MULTIPLY) { |
| 1513 | PyObject *right = POP(); |
| 1514 | PyObject *left = TOP(); |
| 1515 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1516 | Py_DECREF(left); |
| 1517 | Py_DECREF(right); |
| 1518 | SET_TOP(res); |
| 1519 | if (res == NULL) |
| 1520 | goto error; |
| 1521 | DISPATCH(); |
| 1522 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1523 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1524 | TARGET(INPLACE_MATRIX_MULTIPLY) { |
| 1525 | PyObject *right = POP(); |
| 1526 | PyObject *left = TOP(); |
| 1527 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1528 | Py_DECREF(left); |
| 1529 | Py_DECREF(right); |
| 1530 | SET_TOP(res); |
| 1531 | if (res == NULL) |
| 1532 | goto error; |
| 1533 | DISPATCH(); |
| 1534 | } |
| 1535 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1536 | TARGET(INPLACE_TRUE_DIVIDE) { |
| 1537 | PyObject *divisor = POP(); |
| 1538 | PyObject *dividend = TOP(); |
| 1539 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1540 | Py_DECREF(dividend); |
| 1541 | Py_DECREF(divisor); |
| 1542 | SET_TOP(quotient); |
| 1543 | if (quotient == NULL) |
| 1544 | goto error; |
| 1545 | DISPATCH(); |
| 1546 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1547 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1548 | TARGET(INPLACE_FLOOR_DIVIDE) { |
| 1549 | PyObject *divisor = POP(); |
| 1550 | PyObject *dividend = TOP(); |
| 1551 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1552 | Py_DECREF(dividend); |
| 1553 | Py_DECREF(divisor); |
| 1554 | SET_TOP(quotient); |
| 1555 | if (quotient == NULL) |
| 1556 | goto error; |
| 1557 | DISPATCH(); |
| 1558 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1559 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1560 | TARGET(INPLACE_MODULO) { |
| 1561 | PyObject *right = POP(); |
| 1562 | PyObject *left = TOP(); |
| 1563 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1564 | Py_DECREF(left); |
| 1565 | Py_DECREF(right); |
| 1566 | SET_TOP(mod); |
| 1567 | if (mod == NULL) |
| 1568 | goto error; |
| 1569 | DISPATCH(); |
| 1570 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1571 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1572 | TARGET(INPLACE_ADD) { |
| 1573 | PyObject *right = POP(); |
| 1574 | PyObject *left = TOP(); |
| 1575 | PyObject *sum; |
| 1576 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1577 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1578 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1579 | } |
| 1580 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1581 | sum = PyNumber_InPlaceAdd(left, right); |
| 1582 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1583 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1584 | Py_DECREF(right); |
| 1585 | SET_TOP(sum); |
| 1586 | if (sum == NULL) |
| 1587 | goto error; |
| 1588 | DISPATCH(); |
| 1589 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1590 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1591 | TARGET(INPLACE_SUBTRACT) { |
| 1592 | PyObject *right = POP(); |
| 1593 | PyObject *left = TOP(); |
| 1594 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1595 | Py_DECREF(left); |
| 1596 | Py_DECREF(right); |
| 1597 | SET_TOP(diff); |
| 1598 | if (diff == NULL) |
| 1599 | goto error; |
| 1600 | DISPATCH(); |
| 1601 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1602 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1603 | TARGET(INPLACE_LSHIFT) { |
| 1604 | PyObject *right = POP(); |
| 1605 | PyObject *left = TOP(); |
| 1606 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1607 | Py_DECREF(left); |
| 1608 | Py_DECREF(right); |
| 1609 | SET_TOP(res); |
| 1610 | if (res == NULL) |
| 1611 | goto error; |
| 1612 | DISPATCH(); |
| 1613 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1614 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1615 | TARGET(INPLACE_RSHIFT) { |
| 1616 | PyObject *right = POP(); |
| 1617 | PyObject *left = TOP(); |
| 1618 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1619 | Py_DECREF(left); |
| 1620 | Py_DECREF(right); |
| 1621 | SET_TOP(res); |
| 1622 | if (res == NULL) |
| 1623 | goto error; |
| 1624 | DISPATCH(); |
| 1625 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1626 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1627 | TARGET(INPLACE_AND) { |
| 1628 | PyObject *right = POP(); |
| 1629 | PyObject *left = TOP(); |
| 1630 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1631 | Py_DECREF(left); |
| 1632 | Py_DECREF(right); |
| 1633 | SET_TOP(res); |
| 1634 | if (res == NULL) |
| 1635 | goto error; |
| 1636 | DISPATCH(); |
| 1637 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1638 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1639 | TARGET(INPLACE_XOR) { |
| 1640 | PyObject *right = POP(); |
| 1641 | PyObject *left = TOP(); |
| 1642 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1643 | Py_DECREF(left); |
| 1644 | Py_DECREF(right); |
| 1645 | SET_TOP(res); |
| 1646 | if (res == NULL) |
| 1647 | goto error; |
| 1648 | DISPATCH(); |
| 1649 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1650 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1651 | TARGET(INPLACE_OR) { |
| 1652 | PyObject *right = POP(); |
| 1653 | PyObject *left = TOP(); |
| 1654 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1655 | Py_DECREF(left); |
| 1656 | Py_DECREF(right); |
| 1657 | SET_TOP(res); |
| 1658 | if (res == NULL) |
| 1659 | goto error; |
| 1660 | DISPATCH(); |
| 1661 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1662 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1663 | TARGET(STORE_SUBSCR) { |
| 1664 | PyObject *sub = TOP(); |
| 1665 | PyObject *container = SECOND(); |
| 1666 | PyObject *v = THIRD(); |
| 1667 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | STACKADJ(-3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1669 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1670 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1672 | Py_DECREF(container); |
| 1673 | Py_DECREF(sub); |
| 1674 | if (err != 0) |
| 1675 | goto error; |
| 1676 | DISPATCH(); |
| 1677 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1678 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1679 | TARGET(STORE_ANNOTATION) { |
| 1680 | _Py_IDENTIFIER(__annotations__); |
| 1681 | PyObject *ann_dict; |
| 1682 | PyObject *ann = POP(); |
| 1683 | PyObject *name = GETITEM(names, oparg); |
| 1684 | int err; |
| 1685 | if (f->f_locals == NULL) { |
| 1686 | PyErr_Format(PyExc_SystemError, |
| 1687 | "no locals found when storing annotation"); |
| 1688 | Py_DECREF(ann); |
| 1689 | goto error; |
| 1690 | } |
| 1691 | /* first try to get __annotations__ from locals... */ |
| 1692 | if (PyDict_CheckExact(f->f_locals)) { |
| 1693 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 1694 | &PyId___annotations__); |
| 1695 | if (ann_dict == NULL) { |
| 1696 | PyErr_SetString(PyExc_NameError, |
| 1697 | "__annotations__ not found"); |
| 1698 | Py_DECREF(ann); |
| 1699 | goto error; |
| 1700 | } |
| 1701 | Py_INCREF(ann_dict); |
| 1702 | } |
| 1703 | else { |
| 1704 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 1705 | if (ann_str == NULL) { |
| 1706 | Py_DECREF(ann); |
| 1707 | goto error; |
| 1708 | } |
| 1709 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 1710 | if (ann_dict == NULL) { |
| 1711 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 1712 | PyErr_SetString(PyExc_NameError, |
| 1713 | "__annotations__ not found"); |
| 1714 | } |
| 1715 | Py_DECREF(ann); |
| 1716 | goto error; |
| 1717 | } |
| 1718 | } |
| 1719 | /* ...if succeeded, __annotations__[name] = ann */ |
| 1720 | if (PyDict_CheckExact(ann_dict)) { |
| 1721 | err = PyDict_SetItem(ann_dict, name, ann); |
| 1722 | } |
| 1723 | else { |
| 1724 | err = PyObject_SetItem(ann_dict, name, ann); |
| 1725 | } |
| 1726 | Py_DECREF(ann_dict); |
Yury Selivanov | 50c584f | 2016-09-08 23:38:21 -0700 | [diff] [blame] | 1727 | Py_DECREF(ann); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1728 | if (err != 0) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1729 | goto error; |
| 1730 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1731 | DISPATCH(); |
| 1732 | } |
| 1733 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1734 | TARGET(DELETE_SUBSCR) { |
| 1735 | PyObject *sub = TOP(); |
| 1736 | PyObject *container = SECOND(); |
| 1737 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | STACKADJ(-2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1739 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1740 | err = PyObject_DelItem(container, sub); |
| 1741 | Py_DECREF(container); |
| 1742 | Py_DECREF(sub); |
| 1743 | if (err != 0) |
| 1744 | goto error; |
| 1745 | DISPATCH(); |
| 1746 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1747 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1748 | TARGET(PRINT_EXPR) { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1749 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1750 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1751 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1752 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1753 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | PyErr_SetString(PyExc_RuntimeError, |
| 1755 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1756 | Py_DECREF(value); |
| 1757 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1759 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1760 | Py_DECREF(value); |
| 1761 | if (res == NULL) |
| 1762 | goto error; |
| 1763 | Py_DECREF(res); |
| 1764 | DISPATCH(); |
| 1765 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1766 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1767 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | default: switch (opcode) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1769 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1770 | TARGET(RAISE_VARARGS) { |
| 1771 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | switch (oparg) { |
| 1773 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1774 | cause = POP(); /* cause */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1776 | exc = POP(); /* exc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | case 0: /* Fallthrough */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1778 | if (do_raise(exc, cause)) { |
| 1779 | why = WHY_EXCEPTION; |
| 1780 | goto fast_block_end; |
| 1781 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | break; |
| 1783 | default: |
| 1784 | PyErr_SetString(PyExc_SystemError, |
| 1785 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | break; |
| 1787 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1788 | goto error; |
| 1789 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1790 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1791 | TARGET(RETURN_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | retval = POP(); |
| 1793 | why = WHY_RETURN; |
| 1794 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1795 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1796 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1797 | TARGET(GET_AITER) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1798 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1799 | PyObject *iter = NULL; |
| 1800 | PyObject *awaitable = NULL; |
| 1801 | PyObject *obj = TOP(); |
| 1802 | PyTypeObject *type = Py_TYPE(obj); |
| 1803 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1804 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1805 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1806 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1807 | |
| 1808 | if (getter != NULL) { |
| 1809 | iter = (*getter)(obj); |
| 1810 | Py_DECREF(obj); |
| 1811 | if (iter == NULL) { |
| 1812 | SET_TOP(NULL); |
| 1813 | goto error; |
| 1814 | } |
| 1815 | } |
| 1816 | else { |
| 1817 | SET_TOP(NULL); |
| 1818 | PyErr_Format( |
| 1819 | PyExc_TypeError, |
| 1820 | "'async for' requires an object with " |
| 1821 | "__aiter__ method, got %.100s", |
| 1822 | type->tp_name); |
| 1823 | Py_DECREF(obj); |
| 1824 | goto error; |
| 1825 | } |
| 1826 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1827 | if (Py_TYPE(iter)->tp_as_async != NULL && |
| 1828 | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
| 1829 | |
| 1830 | /* Starting with CPython 3.5.2 __aiter__ should return |
| 1831 | asynchronous iterators directly (not awaitables that |
| 1832 | resolve to asynchronous iterators.) |
| 1833 | |
| 1834 | Therefore, we check if the object that was returned |
| 1835 | from __aiter__ has an __anext__ method. If it does, |
| 1836 | we wrap it in an awaitable that resolves to `iter`. |
| 1837 | |
| 1838 | See http://bugs.python.org/issue27243 for more |
| 1839 | details. |
| 1840 | */ |
| 1841 | |
| 1842 | PyObject *wrapper = _PyAIterWrapper_New(iter); |
| 1843 | Py_DECREF(iter); |
| 1844 | SET_TOP(wrapper); |
| 1845 | DISPATCH(); |
| 1846 | } |
| 1847 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1848 | awaitable = _PyCoro_GetAwaitableIter(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1849 | if (awaitable == NULL) { |
| 1850 | SET_TOP(NULL); |
| 1851 | PyErr_Format( |
| 1852 | PyExc_TypeError, |
| 1853 | "'async for' received an invalid object " |
| 1854 | "from __aiter__: %.100s", |
| 1855 | Py_TYPE(iter)->tp_name); |
| 1856 | |
| 1857 | Py_DECREF(iter); |
| 1858 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1859 | } else { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1860 | Py_DECREF(iter); |
| 1861 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1862 | if (PyErr_WarnFormat( |
Yury Selivanov | 2edd8a1 | 2016-11-08 15:13:07 -0500 | [diff] [blame] | 1863 | PyExc_DeprecationWarning, 1, |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1864 | "'%.100s' implements legacy __aiter__ protocol; " |
| 1865 | "__aiter__ should return an asynchronous " |
| 1866 | "iterator, not awaitable", |
| 1867 | type->tp_name)) |
| 1868 | { |
| 1869 | /* Warning was converted to an error. */ |
| 1870 | Py_DECREF(awaitable); |
| 1871 | SET_TOP(NULL); |
| 1872 | goto error; |
| 1873 | } |
| 1874 | } |
| 1875 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1876 | SET_TOP(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1877 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1878 | DISPATCH(); |
| 1879 | } |
| 1880 | |
| 1881 | TARGET(GET_ANEXT) { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1882 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1883 | PyObject *next_iter = NULL; |
| 1884 | PyObject *awaitable = NULL; |
| 1885 | PyObject *aiter = TOP(); |
| 1886 | PyTypeObject *type = Py_TYPE(aiter); |
| 1887 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1888 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1889 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1890 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1891 | goto error; |
| 1892 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1893 | } else { |
| 1894 | if (type->tp_as_async != NULL){ |
| 1895 | getter = type->tp_as_async->am_anext; |
| 1896 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1897 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1898 | if (getter != NULL) { |
| 1899 | next_iter = (*getter)(aiter); |
| 1900 | if (next_iter == NULL) { |
| 1901 | goto error; |
| 1902 | } |
| 1903 | } |
| 1904 | else { |
| 1905 | PyErr_Format( |
| 1906 | PyExc_TypeError, |
| 1907 | "'async for' requires an iterator with " |
| 1908 | "__anext__ method, got %.100s", |
| 1909 | type->tp_name); |
| 1910 | goto error; |
| 1911 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1912 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1913 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1914 | if (awaitable == NULL) { |
| 1915 | PyErr_Format( |
| 1916 | PyExc_TypeError, |
| 1917 | "'async for' received an invalid object " |
| 1918 | "from __anext__: %.100s", |
| 1919 | Py_TYPE(next_iter)->tp_name); |
| 1920 | |
| 1921 | Py_DECREF(next_iter); |
| 1922 | goto error; |
| 1923 | } else { |
| 1924 | Py_DECREF(next_iter); |
| 1925 | } |
| 1926 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1927 | |
| 1928 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1929 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1930 | DISPATCH(); |
| 1931 | } |
| 1932 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1933 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1934 | TARGET(GET_AWAITABLE) { |
| 1935 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1936 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1937 | |
| 1938 | Py_DECREF(iterable); |
| 1939 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1940 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1941 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1942 | if (yf != NULL) { |
| 1943 | /* `iter` is a coroutine object that is being |
| 1944 | awaited, `yf` is a pointer to the current awaitable |
| 1945 | being awaited on. */ |
| 1946 | Py_DECREF(yf); |
| 1947 | Py_CLEAR(iter); |
| 1948 | PyErr_SetString( |
| 1949 | PyExc_RuntimeError, |
| 1950 | "coroutine is being awaited already"); |
| 1951 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 1952 | } |
| 1953 | } |
| 1954 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1955 | SET_TOP(iter); /* Even if it's NULL */ |
| 1956 | |
| 1957 | if (iter == NULL) { |
| 1958 | goto error; |
| 1959 | } |
| 1960 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1961 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1962 | DISPATCH(); |
| 1963 | } |
| 1964 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1965 | TARGET(YIELD_FROM) { |
| 1966 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1967 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1968 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1969 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 1970 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1971 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 1972 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1973 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1974 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1975 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1976 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1977 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1978 | Py_DECREF(v); |
| 1979 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1980 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1981 | if (tstate->c_tracefunc != NULL |
| 1982 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1983 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 1984 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1985 | if (err < 0) |
| 1986 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1987 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1988 | SET_TOP(val); |
| 1989 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1990 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1991 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1992 | f->f_stacktop = stack_pointer; |
| 1993 | why = WHY_YIELD; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1994 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 1995 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1996 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1997 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1998 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1999 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2000 | TARGET(YIELD_VALUE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2002 | |
| 2003 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2004 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2005 | Py_DECREF(retval); |
| 2006 | if (w == NULL) { |
| 2007 | retval = NULL; |
| 2008 | goto error; |
| 2009 | } |
| 2010 | retval = w; |
| 2011 | } |
| 2012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | f->f_stacktop = stack_pointer; |
| 2014 | why = WHY_YIELD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | goto fast_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2016 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2017 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2018 | TARGET(POP_EXCEPT) { |
| 2019 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2020 | if (b->b_type != EXCEPT_HANDLER) { |
| 2021 | PyErr_SetString(PyExc_SystemError, |
| 2022 | "popped block is not an except handler"); |
| 2023 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2025 | UNWIND_EXCEPT_HANDLER(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2027 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2028 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2029 | PREDICTED(POP_BLOCK); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2030 | TARGET(POP_BLOCK) { |
| 2031 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2032 | UNWIND_BLOCK(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2034 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | PREDICTED(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2037 | TARGET(END_FINALLY) { |
| 2038 | PyObject *status = POP(); |
| 2039 | if (PyLong_Check(status)) { |
| 2040 | why = (enum why_code) PyLong_AS_LONG(status); |
| 2041 | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | if (why == WHY_RETURN || |
| 2043 | why == WHY_CONTINUE) |
| 2044 | retval = POP(); |
| 2045 | if (why == WHY_SILENCED) { |
| 2046 | /* An exception was silenced by 'with', we must |
| 2047 | manually unwind the EXCEPT_HANDLER block which was |
| 2048 | created when the exception was caught, otherwise |
| 2049 | the stack will be in an inconsistent state. */ |
| 2050 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2051 | assert(b->b_type == EXCEPT_HANDLER); |
| 2052 | UNWIND_EXCEPT_HANDLER(b); |
| 2053 | why = WHY_NOT; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2054 | Py_DECREF(status); |
| 2055 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2057 | Py_DECREF(status); |
| 2058 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2060 | else if (PyExceptionClass_Check(status)) { |
| 2061 | PyObject *exc = POP(); |
| 2062 | PyObject *tb = POP(); |
| 2063 | PyErr_Restore(status, exc, tb); |
| 2064 | why = WHY_EXCEPTION; |
| 2065 | goto fast_block_end; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2067 | else if (status != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | PyErr_SetString(PyExc_SystemError, |
| 2069 | "'finally' pops bad exception"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2070 | Py_DECREF(status); |
| 2071 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2073 | Py_DECREF(status); |
| 2074 | DISPATCH(); |
| 2075 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2076 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2077 | TARGET(LOAD_BUILD_CLASS) { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2078 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2079 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2080 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2081 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2082 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2083 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2084 | PyErr_SetString(PyExc_NameError, |
| 2085 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2086 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2087 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2088 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2089 | } |
| 2090 | else { |
| 2091 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2092 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2093 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2094 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2095 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2096 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2097 | PyErr_SetString(PyExc_NameError, |
| 2098 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2099 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2100 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2102 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2103 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2104 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2105 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2106 | TARGET(STORE_NAME) { |
| 2107 | PyObject *name = GETITEM(names, oparg); |
| 2108 | PyObject *v = POP(); |
| 2109 | PyObject *ns = f->f_locals; |
| 2110 | int err; |
| 2111 | if (ns == NULL) { |
| 2112 | PyErr_Format(PyExc_SystemError, |
| 2113 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2115 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2117 | if (PyDict_CheckExact(ns)) |
| 2118 | err = PyDict_SetItem(ns, name, v); |
| 2119 | else |
| 2120 | err = PyObject_SetItem(ns, name, v); |
| 2121 | Py_DECREF(v); |
| 2122 | if (err != 0) |
| 2123 | goto error; |
| 2124 | DISPATCH(); |
| 2125 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2126 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2127 | TARGET(DELETE_NAME) { |
| 2128 | PyObject *name = GETITEM(names, oparg); |
| 2129 | PyObject *ns = f->f_locals; |
| 2130 | int err; |
| 2131 | if (ns == NULL) { |
| 2132 | PyErr_Format(PyExc_SystemError, |
| 2133 | "no locals when deleting %R", name); |
| 2134 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2136 | err = PyObject_DelItem(ns, name); |
| 2137 | if (err != 0) { |
| 2138 | format_exc_check_arg(PyExc_NameError, |
| 2139 | NAME_ERROR_MSG, |
| 2140 | name); |
| 2141 | goto error; |
| 2142 | } |
| 2143 | DISPATCH(); |
| 2144 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2145 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2146 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2147 | TARGET(UNPACK_SEQUENCE) { |
| 2148 | PyObject *seq = POP(), *item, **items; |
| 2149 | if (PyTuple_CheckExact(seq) && |
| 2150 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2151 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2153 | item = items[oparg]; |
| 2154 | Py_INCREF(item); |
| 2155 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2157 | } else if (PyList_CheckExact(seq) && |
| 2158 | PyList_GET_SIZE(seq) == oparg) { |
| 2159 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2161 | item = items[oparg]; |
| 2162 | Py_INCREF(item); |
| 2163 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2165 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | stack_pointer + oparg)) { |
| 2167 | STACKADJ(oparg); |
| 2168 | } else { |
| 2169 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2170 | Py_DECREF(seq); |
| 2171 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2173 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2174 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2176 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2177 | TARGET(UNPACK_EX) { |
| 2178 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2179 | PyObject *seq = POP(); |
| 2180 | |
| 2181 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2182 | stack_pointer + totalargs)) { |
| 2183 | stack_pointer += totalargs; |
| 2184 | } else { |
| 2185 | Py_DECREF(seq); |
| 2186 | goto error; |
| 2187 | } |
| 2188 | Py_DECREF(seq); |
| 2189 | DISPATCH(); |
| 2190 | } |
| 2191 | |
| 2192 | TARGET(STORE_ATTR) { |
| 2193 | PyObject *name = GETITEM(names, oparg); |
| 2194 | PyObject *owner = TOP(); |
| 2195 | PyObject *v = SECOND(); |
| 2196 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | STACKADJ(-2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2198 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2200 | Py_DECREF(owner); |
| 2201 | if (err != 0) |
| 2202 | goto error; |
| 2203 | DISPATCH(); |
| 2204 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2205 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2206 | TARGET(DELETE_ATTR) { |
| 2207 | PyObject *name = GETITEM(names, oparg); |
| 2208 | PyObject *owner = POP(); |
| 2209 | int err; |
| 2210 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2211 | Py_DECREF(owner); |
| 2212 | if (err != 0) |
| 2213 | goto error; |
| 2214 | DISPATCH(); |
| 2215 | } |
| 2216 | |
| 2217 | TARGET(STORE_GLOBAL) { |
| 2218 | PyObject *name = GETITEM(names, oparg); |
| 2219 | PyObject *v = POP(); |
| 2220 | int err; |
| 2221 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2223 | if (err != 0) |
| 2224 | goto error; |
| 2225 | DISPATCH(); |
| 2226 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2227 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2228 | TARGET(DELETE_GLOBAL) { |
| 2229 | PyObject *name = GETITEM(names, oparg); |
| 2230 | int err; |
| 2231 | err = PyDict_DelItem(f->f_globals, name); |
| 2232 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2234 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2235 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2236 | } |
| 2237 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2238 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2239 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2240 | TARGET(LOAD_NAME) { |
| 2241 | PyObject *name = GETITEM(names, oparg); |
| 2242 | PyObject *locals = f->f_locals; |
| 2243 | PyObject *v; |
| 2244 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2246 | "no locals when loading %R", name); |
| 2247 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2249 | if (PyDict_CheckExact(locals)) { |
| 2250 | v = PyDict_GetItem(locals, name); |
| 2251 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | } |
| 2253 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2254 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2255 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2256 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2257 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | PyErr_Clear(); |
| 2259 | } |
| 2260 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2261 | if (v == NULL) { |
| 2262 | v = PyDict_GetItem(f->f_globals, name); |
| 2263 | Py_XINCREF(v); |
| 2264 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2265 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2266 | v = PyDict_GetItem(f->f_builtins, name); |
| 2267 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2268 | format_exc_check_arg( |
| 2269 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2270 | NAME_ERROR_MSG, name); |
| 2271 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2272 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2274 | } |
| 2275 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2276 | v = PyObject_GetItem(f->f_builtins, name); |
| 2277 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2278 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2279 | format_exc_check_arg( |
| 2280 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2281 | NAME_ERROR_MSG, name); |
| 2282 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2283 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2284 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2287 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2289 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2290 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2291 | TARGET(LOAD_GLOBAL) { |
| 2292 | PyObject *name = GETITEM(names, oparg); |
| 2293 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2294 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2295 | && PyDict_CheckExact(f->f_builtins)) |
| 2296 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2297 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2298 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2299 | name); |
| 2300 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2301 | if (!_PyErr_OCCURRED()) { |
| 2302 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2303 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2304 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2305 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2306 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2307 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2309 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2311 | else { |
| 2312 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2313 | |
| 2314 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2315 | v = PyObject_GetItem(f->f_globals, name); |
| 2316 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2317 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2318 | goto error; |
| 2319 | PyErr_Clear(); |
| 2320 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2321 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2322 | v = PyObject_GetItem(f->f_builtins, name); |
| 2323 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2324 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2325 | format_exc_check_arg( |
| 2326 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2327 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2328 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2332 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2334 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2335 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2336 | TARGET(DELETE_FAST) { |
| 2337 | PyObject *v = GETLOCAL(oparg); |
| 2338 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | SETLOCAL(oparg, NULL); |
| 2340 | DISPATCH(); |
| 2341 | } |
| 2342 | format_exc_check_arg( |
| 2343 | PyExc_UnboundLocalError, |
| 2344 | UNBOUNDLOCAL_ERROR_MSG, |
| 2345 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2346 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2347 | goto error; |
| 2348 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2349 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2350 | TARGET(DELETE_DEREF) { |
| 2351 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2352 | PyObject *oldobj = PyCell_GET(cell); |
| 2353 | if (oldobj != NULL) { |
| 2354 | PyCell_SET(cell, NULL); |
| 2355 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2356 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2357 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2358 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2359 | goto error; |
| 2360 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2361 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2362 | TARGET(LOAD_CLOSURE) { |
| 2363 | PyObject *cell = freevars[oparg]; |
| 2364 | Py_INCREF(cell); |
| 2365 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2367 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2368 | |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2369 | TARGET(LOAD_CLASSDEREF) { |
| 2370 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2371 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2372 | assert(locals); |
| 2373 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2374 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2375 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2376 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2377 | if (PyDict_CheckExact(locals)) { |
| 2378 | value = PyDict_GetItem(locals, name); |
| 2379 | Py_XINCREF(value); |
| 2380 | } |
| 2381 | else { |
| 2382 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2383 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2384 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2385 | goto error; |
| 2386 | PyErr_Clear(); |
| 2387 | } |
| 2388 | } |
| 2389 | if (!value) { |
| 2390 | PyObject *cell = freevars[oparg]; |
| 2391 | value = PyCell_GET(cell); |
| 2392 | if (value == NULL) { |
| 2393 | format_exc_unbound(co, oparg); |
| 2394 | goto error; |
| 2395 | } |
| 2396 | Py_INCREF(value); |
| 2397 | } |
| 2398 | PUSH(value); |
| 2399 | DISPATCH(); |
| 2400 | } |
| 2401 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2402 | TARGET(LOAD_DEREF) { |
| 2403 | PyObject *cell = freevars[oparg]; |
| 2404 | PyObject *value = PyCell_GET(cell); |
| 2405 | if (value == NULL) { |
| 2406 | format_exc_unbound(co, oparg); |
| 2407 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2409 | Py_INCREF(value); |
| 2410 | PUSH(value); |
| 2411 | DISPATCH(); |
| 2412 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2413 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2414 | TARGET(STORE_DEREF) { |
| 2415 | PyObject *v = POP(); |
| 2416 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2417 | PyObject *oldobj = PyCell_GET(cell); |
| 2418 | PyCell_SET(cell, v); |
| 2419 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2420 | DISPATCH(); |
| 2421 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2422 | |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2423 | TARGET(BUILD_STRING) { |
| 2424 | PyObject *str; |
| 2425 | PyObject *empty = PyUnicode_New(0, 0); |
| 2426 | if (empty == NULL) { |
| 2427 | goto error; |
| 2428 | } |
| 2429 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2430 | Py_DECREF(empty); |
| 2431 | if (str == NULL) |
| 2432 | goto error; |
| 2433 | while (--oparg >= 0) { |
| 2434 | PyObject *item = POP(); |
| 2435 | Py_DECREF(item); |
| 2436 | } |
| 2437 | PUSH(str); |
| 2438 | DISPATCH(); |
| 2439 | } |
| 2440 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2441 | TARGET(BUILD_TUPLE) { |
| 2442 | PyObject *tup = PyTuple_New(oparg); |
| 2443 | if (tup == NULL) |
| 2444 | goto error; |
| 2445 | while (--oparg >= 0) { |
| 2446 | PyObject *item = POP(); |
| 2447 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2448 | } |
| 2449 | PUSH(tup); |
| 2450 | DISPATCH(); |
| 2451 | } |
| 2452 | |
| 2453 | TARGET(BUILD_LIST) { |
| 2454 | PyObject *list = PyList_New(oparg); |
| 2455 | if (list == NULL) |
| 2456 | goto error; |
| 2457 | while (--oparg >= 0) { |
| 2458 | PyObject *item = POP(); |
| 2459 | PyList_SET_ITEM(list, oparg, item); |
| 2460 | } |
| 2461 | PUSH(list); |
| 2462 | DISPATCH(); |
| 2463 | } |
| 2464 | |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2465 | TARGET(BUILD_TUPLE_UNPACK_WITH_CALL) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2466 | TARGET(BUILD_TUPLE_UNPACK) |
| 2467 | TARGET(BUILD_LIST_UNPACK) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2468 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2469 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2470 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2471 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2472 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2473 | if (sum == NULL) |
| 2474 | goto error; |
| 2475 | |
| 2476 | for (i = oparg; i > 0; i--) { |
| 2477 | PyObject *none_val; |
| 2478 | |
| 2479 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2480 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2481 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
| 2482 | PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 2483 | PyObject *func = PEEK(1 + oparg); |
| 2484 | PyErr_Format(PyExc_TypeError, |
| 2485 | "%.200s%.200s argument after * " |
| 2486 | "must be an iterable, not %.200s", |
| 2487 | PyEval_GetFuncName(func), |
| 2488 | PyEval_GetFuncDesc(func), |
| 2489 | PEEK(i)->ob_type->tp_name); |
| 2490 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2491 | Py_DECREF(sum); |
| 2492 | goto error; |
| 2493 | } |
| 2494 | Py_DECREF(none_val); |
| 2495 | } |
| 2496 | |
| 2497 | if (convert_to_tuple) { |
| 2498 | return_value = PyList_AsTuple(sum); |
| 2499 | Py_DECREF(sum); |
| 2500 | if (return_value == NULL) |
| 2501 | goto error; |
| 2502 | } |
| 2503 | else { |
| 2504 | return_value = sum; |
| 2505 | } |
| 2506 | |
| 2507 | while (oparg--) |
| 2508 | Py_DECREF(POP()); |
| 2509 | PUSH(return_value); |
| 2510 | DISPATCH(); |
| 2511 | } |
| 2512 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2513 | TARGET(BUILD_SET) { |
| 2514 | PyObject *set = PySet_New(NULL); |
| 2515 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2516 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2517 | if (set == NULL) |
| 2518 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2519 | for (i = oparg; i > 0; i--) { |
| 2520 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2521 | if (err == 0) |
| 2522 | err = PySet_Add(set, item); |
| 2523 | Py_DECREF(item); |
| 2524 | } |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2525 | STACKADJ(-oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2526 | if (err != 0) { |
| 2527 | Py_DECREF(set); |
| 2528 | goto error; |
| 2529 | } |
| 2530 | PUSH(set); |
| 2531 | DISPATCH(); |
| 2532 | } |
| 2533 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2534 | TARGET(BUILD_SET_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2535 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2536 | PyObject *sum = PySet_New(NULL); |
| 2537 | if (sum == NULL) |
| 2538 | goto error; |
| 2539 | |
| 2540 | for (i = oparg; i > 0; i--) { |
| 2541 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2542 | Py_DECREF(sum); |
| 2543 | goto error; |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | while (oparg--) |
| 2548 | Py_DECREF(POP()); |
| 2549 | PUSH(sum); |
| 2550 | DISPATCH(); |
| 2551 | } |
| 2552 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2553 | TARGET(BUILD_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2554 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2555 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2556 | if (map == NULL) |
| 2557 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2558 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2559 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2560 | PyObject *key = PEEK(2*i); |
| 2561 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2562 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2563 | if (err != 0) { |
| 2564 | Py_DECREF(map); |
| 2565 | goto error; |
| 2566 | } |
| 2567 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2568 | |
| 2569 | while (oparg--) { |
| 2570 | Py_DECREF(POP()); |
| 2571 | Py_DECREF(POP()); |
| 2572 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2573 | PUSH(map); |
| 2574 | DISPATCH(); |
| 2575 | } |
| 2576 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2577 | TARGET(SETUP_ANNOTATIONS) { |
| 2578 | _Py_IDENTIFIER(__annotations__); |
| 2579 | int err; |
| 2580 | PyObject *ann_dict; |
| 2581 | if (f->f_locals == NULL) { |
| 2582 | PyErr_Format(PyExc_SystemError, |
| 2583 | "no locals found when setting up annotations"); |
| 2584 | goto error; |
| 2585 | } |
| 2586 | /* check if __annotations__ in locals()... */ |
| 2587 | if (PyDict_CheckExact(f->f_locals)) { |
| 2588 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2589 | &PyId___annotations__); |
| 2590 | if (ann_dict == NULL) { |
| 2591 | /* ...if not, create a new one */ |
| 2592 | ann_dict = PyDict_New(); |
| 2593 | if (ann_dict == NULL) { |
| 2594 | goto error; |
| 2595 | } |
| 2596 | err = _PyDict_SetItemId(f->f_locals, |
| 2597 | &PyId___annotations__, ann_dict); |
| 2598 | Py_DECREF(ann_dict); |
| 2599 | if (err != 0) { |
| 2600 | goto error; |
| 2601 | } |
| 2602 | } |
| 2603 | } |
| 2604 | else { |
| 2605 | /* do the same if locals() is not a dict */ |
| 2606 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2607 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2608 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2609 | } |
| 2610 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2611 | if (ann_dict == NULL) { |
| 2612 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2613 | goto error; |
| 2614 | } |
| 2615 | PyErr_Clear(); |
| 2616 | ann_dict = PyDict_New(); |
| 2617 | if (ann_dict == NULL) { |
| 2618 | goto error; |
| 2619 | } |
| 2620 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2621 | Py_DECREF(ann_dict); |
| 2622 | if (err != 0) { |
| 2623 | goto error; |
| 2624 | } |
| 2625 | } |
| 2626 | else { |
| 2627 | Py_DECREF(ann_dict); |
| 2628 | } |
| 2629 | } |
| 2630 | DISPATCH(); |
| 2631 | } |
| 2632 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2633 | TARGET(BUILD_CONST_KEY_MAP) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2634 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2635 | PyObject *map; |
| 2636 | PyObject *keys = TOP(); |
| 2637 | if (!PyTuple_CheckExact(keys) || |
| 2638 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2639 | PyErr_SetString(PyExc_SystemError, |
| 2640 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2641 | goto error; |
| 2642 | } |
| 2643 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2644 | if (map == NULL) { |
| 2645 | goto error; |
| 2646 | } |
| 2647 | for (i = oparg; i > 0; i--) { |
| 2648 | int err; |
| 2649 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2650 | PyObject *value = PEEK(i + 1); |
| 2651 | err = PyDict_SetItem(map, key, value); |
| 2652 | if (err != 0) { |
| 2653 | Py_DECREF(map); |
| 2654 | goto error; |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | Py_DECREF(POP()); |
| 2659 | while (oparg--) { |
| 2660 | Py_DECREF(POP()); |
| 2661 | } |
| 2662 | PUSH(map); |
| 2663 | DISPATCH(); |
| 2664 | } |
| 2665 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2666 | TARGET(BUILD_MAP_UNPACK) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2667 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2668 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2669 | if (sum == NULL) |
| 2670 | goto error; |
| 2671 | |
| 2672 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2673 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2674 | if (PyDict_Update(sum, arg) < 0) { |
| 2675 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2676 | PyErr_Format(PyExc_TypeError, |
Berker Peksag | 8e9045d | 2016-10-02 13:08:25 +0300 | [diff] [blame] | 2677 | "'%.200s' object is not a mapping", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2678 | arg->ob_type->tp_name); |
| 2679 | } |
| 2680 | Py_DECREF(sum); |
| 2681 | goto error; |
| 2682 | } |
| 2683 | } |
| 2684 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2685 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2686 | Py_DECREF(POP()); |
| 2687 | PUSH(sum); |
| 2688 | DISPATCH(); |
| 2689 | } |
| 2690 | |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2691 | TARGET(BUILD_MAP_UNPACK_WITH_CALL) { |
| 2692 | Py_ssize_t i; |
| 2693 | PyObject *sum = PyDict_New(); |
| 2694 | if (sum == NULL) |
| 2695 | goto error; |
| 2696 | |
| 2697 | for (i = oparg; i > 0; i--) { |
| 2698 | PyObject *arg = PEEK(i); |
| 2699 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
| 2700 | PyObject *func = PEEK(2 + oparg); |
| 2701 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2702 | PyErr_Format(PyExc_TypeError, |
| 2703 | "%.200s%.200s argument after ** " |
| 2704 | "must be a mapping, not %.200s", |
| 2705 | PyEval_GetFuncName(func), |
| 2706 | PyEval_GetFuncDesc(func), |
| 2707 | arg->ob_type->tp_name); |
| 2708 | } |
| 2709 | else if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2710 | PyObject *exc, *val, *tb; |
| 2711 | PyErr_Fetch(&exc, &val, &tb); |
| 2712 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 2713 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 2714 | if (!PyUnicode_Check(key)) { |
| 2715 | PyErr_Format(PyExc_TypeError, |
| 2716 | "%.200s%.200s keywords must be strings", |
| 2717 | PyEval_GetFuncName(func), |
| 2718 | PyEval_GetFuncDesc(func)); |
| 2719 | } else { |
| 2720 | PyErr_Format(PyExc_TypeError, |
| 2721 | "%.200s%.200s got multiple " |
| 2722 | "values for keyword argument '%U'", |
| 2723 | PyEval_GetFuncName(func), |
| 2724 | PyEval_GetFuncDesc(func), |
| 2725 | key); |
| 2726 | } |
| 2727 | Py_XDECREF(exc); |
| 2728 | Py_XDECREF(val); |
| 2729 | Py_XDECREF(tb); |
| 2730 | } |
| 2731 | else { |
| 2732 | PyErr_Restore(exc, val, tb); |
| 2733 | } |
| 2734 | } |
| 2735 | Py_DECREF(sum); |
| 2736 | goto error; |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | while (oparg--) |
| 2741 | Py_DECREF(POP()); |
| 2742 | PUSH(sum); |
| 2743 | DISPATCH(); |
| 2744 | } |
| 2745 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2746 | TARGET(MAP_ADD) { |
| 2747 | PyObject *key = TOP(); |
| 2748 | PyObject *value = SECOND(); |
| 2749 | PyObject *map; |
| 2750 | int err; |
| 2751 | STACKADJ(-2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2752 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2753 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2754 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2755 | Py_DECREF(value); |
| 2756 | Py_DECREF(key); |
| 2757 | if (err != 0) |
| 2758 | goto error; |
| 2759 | PREDICT(JUMP_ABSOLUTE); |
| 2760 | DISPATCH(); |
| 2761 | } |
| 2762 | |
| 2763 | TARGET(LOAD_ATTR) { |
| 2764 | PyObject *name = GETITEM(names, oparg); |
| 2765 | PyObject *owner = TOP(); |
| 2766 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2767 | Py_DECREF(owner); |
| 2768 | SET_TOP(res); |
| 2769 | if (res == NULL) |
| 2770 | goto error; |
| 2771 | DISPATCH(); |
| 2772 | } |
| 2773 | |
| 2774 | TARGET(COMPARE_OP) { |
| 2775 | PyObject *right = POP(); |
| 2776 | PyObject *left = TOP(); |
| 2777 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2778 | Py_DECREF(left); |
| 2779 | Py_DECREF(right); |
| 2780 | SET_TOP(res); |
| 2781 | if (res == NULL) |
| 2782 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | PREDICT(POP_JUMP_IF_FALSE); |
| 2784 | PREDICT(POP_JUMP_IF_TRUE); |
| 2785 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2786 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2787 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2788 | TARGET(IMPORT_NAME) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2789 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2790 | PyObject *fromlist = POP(); |
| 2791 | PyObject *level = TOP(); |
| 2792 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2793 | res = import_name(f, name, fromlist, level); |
| 2794 | Py_DECREF(level); |
| 2795 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2796 | SET_TOP(res); |
| 2797 | if (res == NULL) |
| 2798 | goto error; |
| 2799 | DISPATCH(); |
| 2800 | } |
| 2801 | |
| 2802 | TARGET(IMPORT_STAR) { |
| 2803 | PyObject *from = POP(), *locals; |
| 2804 | int err; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2805 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 2806 | goto error; |
| 2807 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2808 | locals = f->f_locals; |
| 2809 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | PyErr_SetString(PyExc_SystemError, |
| 2811 | "no locals found during 'import *'"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2812 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2814 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2816 | Py_DECREF(from); |
| 2817 | if (err != 0) |
| 2818 | goto error; |
| 2819 | DISPATCH(); |
| 2820 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2821 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2822 | TARGET(IMPORT_FROM) { |
| 2823 | PyObject *name = GETITEM(names, oparg); |
| 2824 | PyObject *from = TOP(); |
| 2825 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2826 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2827 | PUSH(res); |
| 2828 | if (res == NULL) |
| 2829 | goto error; |
| 2830 | DISPATCH(); |
| 2831 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2832 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2833 | TARGET(JUMP_FORWARD) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | JUMPBY(oparg); |
| 2835 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2836 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2837 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2838 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2839 | TARGET(POP_JUMP_IF_FALSE) { |
| 2840 | PyObject *cond = POP(); |
| 2841 | int err; |
| 2842 | if (cond == Py_True) { |
| 2843 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | FAST_DISPATCH(); |
| 2845 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2846 | if (cond == Py_False) { |
| 2847 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | JUMPTO(oparg); |
| 2849 | FAST_DISPATCH(); |
| 2850 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2851 | err = PyObject_IsTrue(cond); |
| 2852 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | if (err > 0) |
| 2854 | err = 0; |
| 2855 | else if (err == 0) |
| 2856 | JUMPTO(oparg); |
| 2857 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2858 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2860 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2861 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2862 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2863 | TARGET(POP_JUMP_IF_TRUE) { |
| 2864 | PyObject *cond = POP(); |
| 2865 | int err; |
| 2866 | if (cond == Py_False) { |
| 2867 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | FAST_DISPATCH(); |
| 2869 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2870 | if (cond == Py_True) { |
| 2871 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | JUMPTO(oparg); |
| 2873 | FAST_DISPATCH(); |
| 2874 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2875 | err = PyObject_IsTrue(cond); |
| 2876 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | if (err > 0) { |
| 2878 | err = 0; |
| 2879 | JUMPTO(oparg); |
| 2880 | } |
| 2881 | else if (err == 0) |
| 2882 | ; |
| 2883 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2884 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2886 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2887 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2888 | TARGET(JUMP_IF_FALSE_OR_POP) { |
| 2889 | PyObject *cond = TOP(); |
| 2890 | int err; |
| 2891 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2893 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | FAST_DISPATCH(); |
| 2895 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2896 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | JUMPTO(oparg); |
| 2898 | FAST_DISPATCH(); |
| 2899 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2900 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | if (err > 0) { |
| 2902 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2903 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | err = 0; |
| 2905 | } |
| 2906 | else if (err == 0) |
| 2907 | JUMPTO(oparg); |
| 2908 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2909 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2911 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2912 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2913 | TARGET(JUMP_IF_TRUE_OR_POP) { |
| 2914 | PyObject *cond = TOP(); |
| 2915 | int err; |
| 2916 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2918 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2919 | FAST_DISPATCH(); |
| 2920 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2921 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | JUMPTO(oparg); |
| 2923 | FAST_DISPATCH(); |
| 2924 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2925 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | if (err > 0) { |
| 2927 | err = 0; |
| 2928 | JUMPTO(oparg); |
| 2929 | } |
| 2930 | else if (err == 0) { |
| 2931 | STACKADJ(-1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2932 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2933 | } |
| 2934 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2935 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2937 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2938 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2939 | PREDICTED(JUMP_ABSOLUTE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2940 | TARGET(JUMP_ABSOLUTE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2941 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2942 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2944 | the per-loop checks for signals. By default, this should be turned-off |
| 2945 | because it prevents detection of a control-break in tight loops like |
| 2946 | "while 1: pass". Compile with this option turned-on when you need |
| 2947 | the speed-up and do not need break checking inside tight loops (ones |
| 2948 | that contain only instructions ending with FAST_DISPATCH). |
| 2949 | */ |
| 2950 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2951 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2953 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2954 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2955 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2956 | TARGET(GET_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2958 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2959 | PyObject *iter = PyObject_GetIter(iterable); |
| 2960 | Py_DECREF(iterable); |
| 2961 | SET_TOP(iter); |
| 2962 | if (iter == NULL) |
| 2963 | goto error; |
| 2964 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2965 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2966 | DISPATCH(); |
| 2967 | } |
| 2968 | |
| 2969 | TARGET(GET_YIELD_FROM_ITER) { |
| 2970 | /* before: [obj]; after [getiter(obj)] */ |
| 2971 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2972 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2973 | if (PyCoro_CheckExact(iterable)) { |
| 2974 | /* `iterable` is a coroutine */ |
| 2975 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 2976 | /* and it is used in a 'yield from' expression of a |
| 2977 | regular generator. */ |
| 2978 | Py_DECREF(iterable); |
| 2979 | SET_TOP(NULL); |
| 2980 | PyErr_SetString(PyExc_TypeError, |
| 2981 | "cannot 'yield from' a coroutine object " |
| 2982 | "in a non-coroutine generator"); |
| 2983 | goto error; |
| 2984 | } |
| 2985 | } |
| 2986 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2987 | /* `iterable` is not a generator. */ |
| 2988 | iter = PyObject_GetIter(iterable); |
| 2989 | Py_DECREF(iterable); |
| 2990 | SET_TOP(iter); |
| 2991 | if (iter == NULL) |
| 2992 | goto error; |
| 2993 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2994 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2995 | DISPATCH(); |
| 2996 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2997 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 2998 | PREDICTED(FOR_ITER); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2999 | TARGET(FOR_ITER) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3000 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3001 | PyObject *iter = TOP(); |
| 3002 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3003 | if (next != NULL) { |
| 3004 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | PREDICT(STORE_FAST); |
| 3006 | PREDICT(UNPACK_SEQUENCE); |
| 3007 | DISPATCH(); |
| 3008 | } |
| 3009 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3010 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3011 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 3012 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3013 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3014 | PyErr_Clear(); |
| 3015 | } |
| 3016 | /* iterator ended normally */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3017 | STACKADJ(-1); |
| 3018 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3020 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3022 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3023 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3024 | TARGET(BREAK_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | why = WHY_BREAK; |
| 3026 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3027 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3028 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3029 | TARGET(CONTINUE_LOOP) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | retval = PyLong_FromLong(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3031 | if (retval == NULL) |
| 3032 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | why = WHY_CONTINUE; |
| 3034 | goto fast_block_end; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3035 | } |
Raymond Hettinger | 2d783e9 | 2004-03-12 09:12:22 +0000 | [diff] [blame] | 3036 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3037 | TARGET(SETUP_LOOP) |
| 3038 | TARGET(SETUP_EXCEPT) |
| 3039 | TARGET(SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | /* NOTE: If you add any new block-setup opcodes that |
| 3041 | are not try/except/finally handlers, you may need |
| 3042 | to update the PyGen_NeedsFinalizing() function. |
| 3043 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
| 3046 | STACK_LEVEL()); |
| 3047 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3048 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3049 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3050 | TARGET(BEFORE_ASYNC_WITH) { |
| 3051 | _Py_IDENTIFIER(__aexit__); |
| 3052 | _Py_IDENTIFIER(__aenter__); |
| 3053 | |
| 3054 | PyObject *mgr = TOP(); |
| 3055 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 3056 | *enter; |
| 3057 | PyObject *res; |
| 3058 | if (exit == NULL) |
| 3059 | goto error; |
| 3060 | SET_TOP(exit); |
| 3061 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3062 | Py_DECREF(mgr); |
| 3063 | if (enter == NULL) |
| 3064 | goto error; |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 3065 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3066 | Py_DECREF(enter); |
| 3067 | if (res == NULL) |
| 3068 | goto error; |
| 3069 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3070 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3071 | DISPATCH(); |
| 3072 | } |
| 3073 | |
| 3074 | TARGET(SETUP_ASYNC_WITH) { |
| 3075 | PyObject *res = POP(); |
| 3076 | /* Setup the finally block before pushing the result |
| 3077 | of __aenter__ on the stack. */ |
| 3078 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3079 | STACK_LEVEL()); |
| 3080 | PUSH(res); |
| 3081 | DISPATCH(); |
| 3082 | } |
| 3083 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3084 | TARGET(SETUP_WITH) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3085 | _Py_IDENTIFIER(__exit__); |
| 3086 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3087 | PyObject *mgr = TOP(); |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3088 | PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3089 | PyObject *res; |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3090 | if (enter == NULL) |
| 3091 | goto error; |
| 3092 | exit = special_lookup(mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3093 | if (exit == NULL) { |
| 3094 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3095 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3096 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3097 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3098 | Py_DECREF(mgr); |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 3099 | res = PyObject_CallFunctionObjArgs(enter, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3100 | Py_DECREF(enter); |
| 3101 | if (res == NULL) |
| 3102 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | /* Setup the finally block before pushing the result |
| 3104 | of __enter__ on the stack. */ |
| 3105 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3106 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3107 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3108 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | DISPATCH(); |
| 3110 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3111 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3112 | TARGET(WITH_CLEANUP_START) { |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3113 | /* At the top of the stack are 1-6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | how/why we entered the finally clause: |
| 3115 | - TOP = None |
| 3116 | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
| 3117 | - TOP = WHY_*; no retval below it |
| 3118 | - (TOP, SECOND, THIRD) = exc_info() |
| 3119 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3120 | Below them is EXIT, the context.__exit__ bound method. |
| 3121 | In the last case, we must call |
| 3122 | EXIT(TOP, SECOND, THIRD) |
| 3123 | otherwise we must call |
| 3124 | EXIT(None, None, None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3125 | |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3126 | In the first three cases, we remove EXIT from the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | stack, leaving the rest in the same order. In the |
Benjamin Peterson | 8f16948 | 2013-10-29 22:25:06 -0400 | [diff] [blame] | 3128 | fourth case, we shift the bottom 3 values of the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | stack down, and replace the empty spot with NULL. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | In addition, if the stack represents an exception, |
| 3132 | *and* the function call returns a 'true' value, we |
| 3133 | push WHY_SILENCED onto the stack. END_FINALLY will |
| 3134 | then not re-raise the exception. (But non-local |
| 3135 | gotos should still be resumed.) |
| 3136 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3137 | |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3138 | PyObject* stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3140 | PyObject *exc, *val, *tb, *res; |
| 3141 | |
| 3142 | val = tb = Py_None; |
| 3143 | exc = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3144 | if (exc == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | (void)POP(); |
| 3146 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3147 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3149 | else if (PyLong_Check(exc)) { |
| 3150 | STACKADJ(-1); |
| 3151 | switch (PyLong_AsLong(exc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | case WHY_RETURN: |
| 3153 | case WHY_CONTINUE: |
| 3154 | /* Retval in TOP. */ |
| 3155 | exit_func = SECOND(); |
| 3156 | SET_SECOND(TOP()); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3157 | SET_TOP(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | break; |
| 3159 | default: |
| 3160 | exit_func = 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 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3164 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | } |
| 3166 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3167 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3169 | val = SECOND(); |
| 3170 | tb = THIRD(); |
| 3171 | tp2 = FOURTH(); |
| 3172 | exc2 = PEEK(5); |
| 3173 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3175 | SET_VALUE(7, tb2); |
| 3176 | SET_VALUE(6, exc2); |
| 3177 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3179 | SET_FOURTH(NULL); |
| 3180 | /* We just shifted the stack down, so we have |
| 3181 | to tell the except handler block that the |
| 3182 | values are lower than it expects. */ |
| 3183 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3184 | assert(block->b_type == EXCEPT_HANDLER); |
| 3185 | block->b_level--; |
| 3186 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3187 | |
| 3188 | stack[0] = exc; |
| 3189 | stack[1] = val; |
| 3190 | stack[2] = tb; |
| 3191 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3193 | if (res == NULL) |
| 3194 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3195 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3196 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3197 | PUSH(exc); |
| 3198 | PUSH(res); |
| 3199 | PREDICT(WITH_CLEANUP_FINISH); |
| 3200 | DISPATCH(); |
| 3201 | } |
| 3202 | |
| 3203 | PREDICTED(WITH_CLEANUP_FINISH); |
| 3204 | TARGET(WITH_CLEANUP_FINISH) { |
| 3205 | PyObject *res = POP(); |
| 3206 | PyObject *exc = POP(); |
| 3207 | int err; |
| 3208 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3209 | if (exc != Py_None) |
| 3210 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | else |
| 3212 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3213 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3214 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3215 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3218 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | else if (err > 0) { |
| 3220 | err = 0; |
| 3221 | /* There was an exception and a True return */ |
| 3222 | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
| 3223 | } |
| 3224 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3225 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3227 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3228 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3229 | TARGET(CALL_FUNCTION) { |
| 3230 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3232 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3234 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3235 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3236 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3237 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3238 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3240 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3241 | TARGET(CALL_FUNCTION_KW) { |
| 3242 | PyObject **sp, *res, *names; |
| 3243 | |
| 3244 | names = POP(); |
| 3245 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3247 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3249 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3250 | Py_DECREF(names); |
| 3251 | |
| 3252 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3253 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3254 | } |
| 3255 | DISPATCH(); |
| 3256 | } |
| 3257 | |
| 3258 | TARGET(CALL_FUNCTION_EX) { |
| 3259 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3260 | if (oparg & 0x01) { |
| 3261 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3262 | if (!PyDict_CheckExact(kwargs)) { |
| 3263 | PyObject *d = PyDict_New(); |
| 3264 | if (d == NULL) |
| 3265 | goto error; |
| 3266 | if (PyDict_Update(d, kwargs) != 0) { |
| 3267 | Py_DECREF(d); |
| 3268 | /* PyDict_Update raises attribute |
| 3269 | * error (percolated from an attempt |
| 3270 | * to get 'keys' attribute) instead of |
| 3271 | * a type error if its second argument |
| 3272 | * is not a mapping. |
| 3273 | */ |
| 3274 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3275 | func = SECOND(); |
| 3276 | PyErr_Format(PyExc_TypeError, |
| 3277 | "%.200s%.200s argument after ** " |
| 3278 | "must be a mapping, not %.200s", |
| 3279 | PyEval_GetFuncName(func), |
| 3280 | PyEval_GetFuncDesc(func), |
| 3281 | kwargs->ob_type->tp_name); |
| 3282 | } |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3283 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3284 | goto error; |
| 3285 | } |
| 3286 | Py_DECREF(kwargs); |
| 3287 | kwargs = d; |
| 3288 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3289 | assert(PyDict_CheckExact(kwargs)); |
| 3290 | } |
| 3291 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3292 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3293 | if (!PyTuple_CheckExact(callargs)) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3294 | if (Py_TYPE(callargs)->tp_iter == NULL && |
| 3295 | !PySequence_Check(callargs)) { |
| 3296 | PyErr_Format(PyExc_TypeError, |
| 3297 | "%.200s%.200s argument after * " |
| 3298 | "must be an iterable, not %.200s", |
| 3299 | PyEval_GetFuncName(func), |
| 3300 | PyEval_GetFuncDesc(func), |
| 3301 | callargs->ob_type->tp_name); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3302 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3303 | goto error; |
| 3304 | } |
| 3305 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3306 | if (callargs == NULL) { |
| 3307 | goto error; |
| 3308 | } |
| 3309 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3310 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3311 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3312 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3313 | Py_DECREF(func); |
| 3314 | Py_DECREF(callargs); |
| 3315 | Py_XDECREF(kwargs); |
| 3316 | |
| 3317 | SET_TOP(result); |
| 3318 | if (result == NULL) { |
| 3319 | goto error; |
| 3320 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3321 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3323 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 3324 | TARGET(MAKE_FUNCTION) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3325 | PyObject *qualname = POP(); |
| 3326 | PyObject *codeobj = POP(); |
| 3327 | PyFunctionObject *func = (PyFunctionObject *) |
| 3328 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3329 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3330 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3331 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3332 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3333 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3335 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3336 | if (oparg & 0x08) { |
| 3337 | assert(PyTuple_CheckExact(TOP())); |
| 3338 | func ->func_closure = POP(); |
| 3339 | } |
| 3340 | if (oparg & 0x04) { |
| 3341 | assert(PyDict_CheckExact(TOP())); |
| 3342 | func->func_annotations = POP(); |
| 3343 | } |
| 3344 | if (oparg & 0x02) { |
| 3345 | assert(PyDict_CheckExact(TOP())); |
| 3346 | func->func_kwdefaults = POP(); |
| 3347 | } |
| 3348 | if (oparg & 0x01) { |
| 3349 | assert(PyTuple_CheckExact(TOP())); |
| 3350 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3352 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3353 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3354 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3356 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3357 | TARGET(BUILD_SLICE) { |
| 3358 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3360 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3362 | step = NULL; |
| 3363 | stop = POP(); |
| 3364 | start = TOP(); |
| 3365 | slice = PySlice_New(start, stop, step); |
| 3366 | Py_DECREF(start); |
| 3367 | Py_DECREF(stop); |
| 3368 | Py_XDECREF(step); |
| 3369 | SET_TOP(slice); |
| 3370 | if (slice == NULL) |
| 3371 | goto error; |
| 3372 | DISPATCH(); |
| 3373 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3374 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3375 | TARGET(FORMAT_VALUE) { |
| 3376 | /* Handles f-string value formatting. */ |
| 3377 | PyObject *result; |
| 3378 | PyObject *fmt_spec; |
| 3379 | PyObject *value; |
| 3380 | PyObject *(*conv_fn)(PyObject *); |
| 3381 | int which_conversion = oparg & FVC_MASK; |
| 3382 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3383 | |
| 3384 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3385 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3386 | |
| 3387 | /* See if any conversion is specified. */ |
| 3388 | switch (which_conversion) { |
| 3389 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3390 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3391 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3392 | |
| 3393 | /* Must be 0 (meaning no conversion), since only four |
| 3394 | values are allowed by (oparg & FVC_MASK). */ |
| 3395 | default: conv_fn = NULL; break; |
| 3396 | } |
| 3397 | |
| 3398 | /* If there's a conversion function, call it and replace |
| 3399 | value with that result. Otherwise, just use value, |
| 3400 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3401 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3402 | result = conv_fn(value); |
| 3403 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3404 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3405 | Py_XDECREF(fmt_spec); |
| 3406 | goto error; |
| 3407 | } |
| 3408 | value = result; |
| 3409 | } |
| 3410 | |
| 3411 | /* If value is a unicode object, and there's no fmt_spec, |
| 3412 | then we know the result of format(value) is value |
| 3413 | itself. In that case, skip calling format(). I plan to |
| 3414 | move this optimization in to PyObject_Format() |
| 3415 | itself. */ |
| 3416 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3417 | /* Do nothing, just transfer ownership to result. */ |
| 3418 | result = value; |
| 3419 | } else { |
| 3420 | /* Actually call format(). */ |
| 3421 | result = PyObject_Format(value, fmt_spec); |
| 3422 | Py_DECREF(value); |
| 3423 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3424 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3425 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3426 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3427 | } |
| 3428 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3429 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3430 | DISPATCH(); |
| 3431 | } |
| 3432 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3433 | TARGET(EXTENDED_ARG) { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3434 | int oldoparg = oparg; |
| 3435 | NEXTOPARG(); |
| 3436 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3438 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3439 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3440 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3441 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3443 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | default: |
| 3445 | fprintf(stderr, |
| 3446 | "XXX lineno: %d, opcode: %d\n", |
| 3447 | PyFrame_GetLineNumber(f), |
| 3448 | opcode); |
| 3449 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3450 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3451 | |
| 3452 | #ifdef CASE_TOO_BIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3454 | #endif |
| 3455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3456 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3457 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3458 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3459 | or goto error. */ |
| 3460 | assert(0); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3461 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3462 | error: |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 3463 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3464 | assert(why == WHY_NOT); |
| 3465 | why = WHY_EXCEPTION; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3466 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3467 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3468 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3469 | if (!PyErr_Occurred()) |
| 3470 | PyErr_SetString(PyExc_SystemError, |
| 3471 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3472 | #else |
| 3473 | assert(PyErr_Occurred()); |
| 3474 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3475 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3476 | /* Log traceback info. */ |
| 3477 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3478 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3479 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3480 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3481 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3482 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3483 | fast_block_end: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3484 | assert(why != WHY_NOT); |
| 3485 | |
| 3486 | /* Unwind stacks if a (pseudo) exception occurred */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 3488 | /* Peek at the current block. */ |
| 3489 | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | assert(why != WHY_YIELD); |
| 3492 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 3493 | why = WHY_NOT; |
| 3494 | JUMPTO(PyLong_AS_LONG(retval)); |
| 3495 | Py_DECREF(retval); |
| 3496 | break; |
| 3497 | } |
| 3498 | /* Now we have to pop the block. */ |
| 3499 | f->f_iblock--; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | if (b->b_type == EXCEPT_HANDLER) { |
| 3502 | UNWIND_EXCEPT_HANDLER(b); |
| 3503 | continue; |
| 3504 | } |
| 3505 | UNWIND_BLOCK(b); |
| 3506 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 3507 | why = WHY_NOT; |
| 3508 | JUMPTO(b->b_handler); |
| 3509 | break; |
| 3510 | } |
| 3511 | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
| 3512 | || b->b_type == SETUP_FINALLY)) { |
| 3513 | PyObject *exc, *val, *tb; |
| 3514 | int handler = b->b_handler; |
| 3515 | /* Beware, this invalidates all b->b_* fields */ |
| 3516 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
| 3517 | PUSH(tstate->exc_traceback); |
| 3518 | PUSH(tstate->exc_value); |
| 3519 | if (tstate->exc_type != NULL) { |
| 3520 | PUSH(tstate->exc_type); |
| 3521 | } |
| 3522 | else { |
| 3523 | Py_INCREF(Py_None); |
| 3524 | PUSH(Py_None); |
| 3525 | } |
| 3526 | PyErr_Fetch(&exc, &val, &tb); |
| 3527 | /* Make the raw exception data |
| 3528 | available to the handler, |
| 3529 | so a program can emulate the |
| 3530 | Python main loop. */ |
| 3531 | PyErr_NormalizeException( |
| 3532 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3533 | if (tb != NULL) |
| 3534 | PyException_SetTraceback(val, tb); |
| 3535 | else |
| 3536 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | Py_INCREF(exc); |
| 3538 | tstate->exc_type = exc; |
| 3539 | Py_INCREF(val); |
| 3540 | tstate->exc_value = val; |
| 3541 | tstate->exc_traceback = tb; |
| 3542 | if (tb == NULL) |
| 3543 | tb = Py_None; |
| 3544 | Py_INCREF(tb); |
| 3545 | PUSH(tb); |
| 3546 | PUSH(val); |
| 3547 | PUSH(exc); |
| 3548 | why = WHY_NOT; |
| 3549 | JUMPTO(handler); |
| 3550 | break; |
| 3551 | } |
| 3552 | if (b->b_type == SETUP_FINALLY) { |
| 3553 | if (why & (WHY_RETURN | WHY_CONTINUE)) |
| 3554 | PUSH(retval); |
| 3555 | PUSH(PyLong_FromLong((long)why)); |
| 3556 | why = WHY_NOT; |
| 3557 | JUMPTO(b->b_handler); |
| 3558 | break; |
| 3559 | } |
| 3560 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | if (why != WHY_NOT) |
| 3565 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3566 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3567 | assert(!PyErr_Occurred()); |
| 3568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | assert(why != WHY_YIELD); |
| 3572 | /* Pop remaining stack entries. */ |
| 3573 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3574 | PyObject *o = POP(); |
| 3575 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | if (why != WHY_RETURN) |
| 3579 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3580 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 3581 | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 3582 | |
Raymond Hettinger | 1dd8309 | 2004-02-06 18:32:33 +0000 | [diff] [blame] | 3583 | fast_yield: |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3584 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Victor Stinner | 26f7b8a | 2015-01-31 10:29:47 +0100 | [diff] [blame] | 3585 | |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3586 | /* The purpose of this block is to put aside the generator's exception |
| 3587 | state and restore that of the calling frame. If the current |
| 3588 | exception state is from the caller, we clear the exception values |
| 3589 | on the generator frame, so they are not swapped back in latter. The |
| 3590 | origin of the current exception state is determined by checking for |
| 3591 | except handler blocks, which we must be in iff a new exception |
| 3592 | state came into existence in this frame. (An uncaught exception |
| 3593 | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
| 3594 | int i; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3595 | for (i = 0; i < f->f_iblock; i++) { |
| 3596 | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3597 | break; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3598 | } |
| 3599 | } |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3600 | if (i == f->f_iblock) |
| 3601 | /* We did not create this exception. */ |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3602 | restore_and_clear_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3603 | else |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 3604 | swap_exc_state(tstate, f); |
Benjamin Peterson | ac91341 | 2011-07-03 16:25:11 -0500 | [diff] [blame] | 3605 | } |
Benjamin Peterson | 83195c3 | 2011-07-03 13:44:00 -0500 | [diff] [blame] | 3606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3607 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3608 | if (tstate->c_tracefunc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | if (why == WHY_RETURN || why == WHY_YIELD) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3610 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3611 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3612 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3613 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3614 | why = WHY_EXCEPTION; |
| 3615 | } |
| 3616 | } |
| 3617 | else if (why == WHY_EXCEPTION) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3618 | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3619 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3620 | PyTrace_RETURN, NULL); |
| 3621 | } |
| 3622 | } |
| 3623 | if (tstate->c_profilefunc) { |
| 3624 | if (why == WHY_EXCEPTION) |
| 3625 | call_trace_protected(tstate->c_profilefunc, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3626 | tstate->c_profileobj, |
| 3627 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | PyTrace_RETURN, NULL); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3629 | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
| 3630 | tstate, f, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3632 | Py_CLEAR(retval); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 3633 | /* why = WHY_EXCEPTION; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3634 | } |
| 3635 | } |
| 3636 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3639 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3640 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3641 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3643 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3645 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3646 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3649 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3650 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3651 | { |
| 3652 | int err; |
| 3653 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3654 | PyObject *name_str, *comma, *tail, *tmp; |
| 3655 | |
| 3656 | assert(PyList_CheckExact(names)); |
| 3657 | assert(len >= 1); |
| 3658 | /* Deal with the joys of natural language. */ |
| 3659 | switch (len) { |
| 3660 | case 1: |
| 3661 | name_str = PyList_GET_ITEM(names, 0); |
| 3662 | Py_INCREF(name_str); |
| 3663 | break; |
| 3664 | case 2: |
| 3665 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3666 | PyList_GET_ITEM(names, len - 2), |
| 3667 | PyList_GET_ITEM(names, len - 1)); |
| 3668 | break; |
| 3669 | default: |
| 3670 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3671 | PyList_GET_ITEM(names, len - 2), |
| 3672 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3673 | if (tail == NULL) |
| 3674 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3675 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3676 | fail, but we can't be too careful. */ |
| 3677 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3678 | if (err == -1) { |
| 3679 | Py_DECREF(tail); |
| 3680 | return; |
| 3681 | } |
| 3682 | /* Stitch everything up into a nice comma-separated list. */ |
| 3683 | comma = PyUnicode_FromString(", "); |
| 3684 | if (comma == NULL) { |
| 3685 | Py_DECREF(tail); |
| 3686 | return; |
| 3687 | } |
| 3688 | tmp = PyUnicode_Join(comma, names); |
| 3689 | Py_DECREF(comma); |
| 3690 | if (tmp == NULL) { |
| 3691 | Py_DECREF(tail); |
| 3692 | return; |
| 3693 | } |
| 3694 | name_str = PyUnicode_Concat(tmp, tail); |
| 3695 | Py_DECREF(tmp); |
| 3696 | Py_DECREF(tail); |
| 3697 | break; |
| 3698 | } |
| 3699 | if (name_str == NULL) |
| 3700 | return; |
| 3701 | PyErr_Format(PyExc_TypeError, |
| 3702 | "%U() missing %i required %s argument%s: %U", |
| 3703 | co->co_name, |
| 3704 | len, |
| 3705 | kind, |
| 3706 | len == 1 ? "" : "s", |
| 3707 | name_str); |
| 3708 | Py_DECREF(name_str); |
| 3709 | } |
| 3710 | |
| 3711 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3712 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3713 | PyObject **fastlocals) |
| 3714 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3715 | Py_ssize_t i, j = 0; |
| 3716 | Py_ssize_t start, end; |
| 3717 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3718 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3719 | PyObject *missing_names; |
| 3720 | |
| 3721 | /* Compute the names of the arguments that are missing. */ |
| 3722 | missing_names = PyList_New(missing); |
| 3723 | if (missing_names == NULL) |
| 3724 | return; |
| 3725 | if (positional) { |
| 3726 | start = 0; |
| 3727 | end = co->co_argcount - defcount; |
| 3728 | } |
| 3729 | else { |
| 3730 | start = co->co_argcount; |
| 3731 | end = start + co->co_kwonlyargcount; |
| 3732 | } |
| 3733 | for (i = start; i < end; i++) { |
| 3734 | if (GETLOCAL(i) == NULL) { |
| 3735 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3736 | PyObject *name = PyObject_Repr(raw); |
| 3737 | if (name == NULL) { |
| 3738 | Py_DECREF(missing_names); |
| 3739 | return; |
| 3740 | } |
| 3741 | PyList_SET_ITEM(missing_names, j++, name); |
| 3742 | } |
| 3743 | } |
| 3744 | assert(j == missing); |
| 3745 | format_missing(kind, co, missing_names); |
| 3746 | Py_DECREF(missing_names); |
| 3747 | } |
| 3748 | |
| 3749 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3750 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3751 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3752 | { |
| 3753 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3754 | Py_ssize_t kwonly_given = 0; |
| 3755 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3756 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3757 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3758 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3759 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3760 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3761 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3762 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3763 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3764 | } |
| 3765 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3766 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3767 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3768 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3769 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3770 | } |
| 3771 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3772 | plural = (co_argcount != 1); |
| 3773 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3774 | } |
| 3775 | if (sig == NULL) |
| 3776 | return; |
| 3777 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3778 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3779 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3780 | given != 1 ? "s" : "", |
| 3781 | kwonly_given, |
| 3782 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3783 | if (kwonly_sig == NULL) { |
| 3784 | Py_DECREF(sig); |
| 3785 | return; |
| 3786 | } |
| 3787 | } |
| 3788 | else { |
| 3789 | /* This will not fail. */ |
| 3790 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3791 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3792 | } |
| 3793 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3794 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3795 | co->co_name, |
| 3796 | sig, |
| 3797 | plural ? "s" : "", |
| 3798 | given, |
| 3799 | kwonly_sig, |
| 3800 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3801 | Py_DECREF(sig); |
| 3802 | Py_DECREF(kwonly_sig); |
| 3803 | } |
| 3804 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3805 | /* 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] | 3806 | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3807 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3808 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3809 | static PyObject * |
| 3810 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3811 | PyObject **args, Py_ssize_t argcount, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3812 | PyObject **kwnames, PyObject **kwargs, |
| 3813 | Py_ssize_t kwcount, int kwstep, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3814 | PyObject **defs, Py_ssize_t defcount, |
| 3815 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3816 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3817 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3818 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3819 | PyFrameObject *f; |
| 3820 | PyObject *retval = NULL; |
| 3821 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3822 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3824 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3825 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3826 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3828 | if (globals == NULL) { |
| 3829 | PyErr_SetString(PyExc_SystemError, |
| 3830 | "PyEval_EvalCodeEx: NULL globals"); |
| 3831 | return NULL; |
| 3832 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3833 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3834 | /* Create the frame */ |
| 3835 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3836 | assert(tstate != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | f = PyFrame_New(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3838 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3840 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3841 | fastlocals = f->f_localsplus; |
| 3842 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3843 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3844 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3845 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3846 | kwdict = PyDict_New(); |
| 3847 | if (kwdict == NULL) |
| 3848 | goto fail; |
| 3849 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3850 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3851 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3852 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3853 | SETLOCAL(i, kwdict); |
| 3854 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3855 | else { |
| 3856 | kwdict = NULL; |
| 3857 | } |
| 3858 | |
| 3859 | /* Copy positional arguments into local variables */ |
| 3860 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3861 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3862 | } |
| 3863 | else { |
| 3864 | n = argcount; |
| 3865 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3866 | for (i = 0; i < n; i++) { |
| 3867 | x = args[i]; |
| 3868 | Py_INCREF(x); |
| 3869 | SETLOCAL(i, x); |
| 3870 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3871 | |
| 3872 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3873 | if (co->co_flags & CO_VARARGS) { |
| 3874 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3875 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3876 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3877 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3878 | SETLOCAL(total_args, u); |
| 3879 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | x = args[i]; |
| 3881 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3882 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3884 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3885 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3886 | /* Handle keyword arguments passed as two strided arrays */ |
| 3887 | kwcount *= kwstep; |
| 3888 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3889 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3890 | PyObject *keyword = kwnames[i]; |
| 3891 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3892 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3893 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3894 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3895 | PyErr_Format(PyExc_TypeError, |
| 3896 | "%U() keywords must be strings", |
| 3897 | co->co_name); |
| 3898 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3900 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3901 | /* Speed hack: do raw pointer compares. As names are |
| 3902 | normally interned this should almost always hit. */ |
| 3903 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3904 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3905 | PyObject *name = co_varnames[j]; |
| 3906 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3907 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3908 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3909 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3910 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3911 | /* Slow fallback, just in case */ |
| 3912 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3913 | PyObject *name = co_varnames[j]; |
| 3914 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 3915 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3916 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3917 | } |
| 3918 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3919 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3920 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3921 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3922 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3923 | if (j >= total_args && kwdict == NULL) { |
| 3924 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3925 | "%U() got an unexpected keyword argument '%S'", |
| 3926 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3927 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3928 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3929 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 3930 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3931 | goto fail; |
| 3932 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3933 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3934 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3935 | kw_found: |
| 3936 | if (GETLOCAL(j) != NULL) { |
| 3937 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3938 | "%U() got multiple values for argument '%S'", |
| 3939 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3940 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3941 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3942 | Py_INCREF(value); |
| 3943 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3944 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3945 | |
| 3946 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3947 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3948 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3949 | goto fail; |
| 3950 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3951 | |
| 3952 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3953 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3954 | Py_ssize_t m = co->co_argcount - defcount; |
| 3955 | Py_ssize_t missing = 0; |
| 3956 | for (i = argcount; i < m; i++) { |
| 3957 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3958 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3959 | } |
| 3960 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3961 | if (missing) { |
| 3962 | missing_arguments(co, missing, defcount, fastlocals); |
| 3963 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3964 | } |
| 3965 | if (n > m) |
| 3966 | i = n - m; |
| 3967 | else |
| 3968 | i = 0; |
| 3969 | for (; i < defcount; i++) { |
| 3970 | if (GETLOCAL(m+i) == NULL) { |
| 3971 | PyObject *def = defs[i]; |
| 3972 | Py_INCREF(def); |
| 3973 | SETLOCAL(m+i, def); |
| 3974 | } |
| 3975 | } |
| 3976 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3977 | |
| 3978 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3979 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3980 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3981 | for (i = co->co_argcount; i < total_args; i++) { |
| 3982 | PyObject *name; |
| 3983 | if (GETLOCAL(i) != NULL) |
| 3984 | continue; |
| 3985 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3986 | if (kwdefs != NULL) { |
| 3987 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 3988 | if (def) { |
| 3989 | Py_INCREF(def); |
| 3990 | SETLOCAL(i, def); |
| 3991 | continue; |
| 3992 | } |
| 3993 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3994 | missing++; |
| 3995 | } |
| 3996 | if (missing) { |
| 3997 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3998 | goto fail; |
| 3999 | } |
| 4000 | } |
| 4001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4002 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4003 | vars into frame. */ |
| 4004 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | PyObject *c; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4006 | int arg; |
| 4007 | /* Possibly account for the cell variable being an argument. */ |
| 4008 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4009 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4010 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4011 | /* Clear the local copy. */ |
| 4012 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4013 | } |
| 4014 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4015 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4016 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4017 | if (c == NULL) |
| 4018 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4019 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4020 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4021 | |
| 4022 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4023 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4024 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4025 | Py_INCREF(o); |
| 4026 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4027 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4028 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4029 | /* Handle generator/coroutine/asynchronous generator */ |
| 4030 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4031 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4032 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4033 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4034 | |
| 4035 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 4036 | assert(coro_wrapper != NULL); |
| 4037 | PyErr_Format(PyExc_RuntimeError, |
| 4038 | "coroutine wrapper %.200R attempted " |
| 4039 | "to recursively wrap %.200R", |
| 4040 | coro_wrapper, |
| 4041 | co); |
| 4042 | goto fail; |
| 4043 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4045 | /* Don't need to keep the reference to f_back, it will be set |
| 4046 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4047 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | /* Create a new generator that owns the ready to run frame |
| 4050 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4051 | if (is_coro) { |
| 4052 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4053 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4054 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4055 | } else { |
| 4056 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4057 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4058 | if (gen == NULL) |
| 4059 | return NULL; |
| 4060 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4061 | if (is_coro && coro_wrapper != NULL) { |
| 4062 | PyObject *wrapped; |
| 4063 | tstate->in_coroutine_wrapper = 1; |
| 4064 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 4065 | tstate->in_coroutine_wrapper = 0; |
| 4066 | return wrapped; |
| 4067 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4068 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4069 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4070 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4072 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4073 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4074 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4076 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4077 | which can call back into Python. While we're done with the |
| 4078 | current Python frame (f), the associated C stack is still in use, |
| 4079 | so recursion_depth must be boosted for the duration. |
| 4080 | */ |
| 4081 | assert(tstate != NULL); |
| 4082 | ++tstate->recursion_depth; |
| 4083 | Py_DECREF(f); |
| 4084 | --tstate->recursion_depth; |
| 4085 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4088 | PyObject * |
| 4089 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4090 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 4091 | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
| 4092 | { |
| 4093 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4094 | args, argcount, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4095 | kws, kws + 1, kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4096 | defs, defcount, |
| 4097 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4098 | NULL, NULL); |
| 4099 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4100 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4101 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4102 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4105 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4106 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4107 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 | return NULL; |
| 4109 | } |
| 4110 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4114 | /* These 3 functions deal with the exception state of generators. */ |
| 4115 | |
| 4116 | static void |
| 4117 | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4118 | { |
| 4119 | PyObject *type, *value, *traceback; |
| 4120 | Py_XINCREF(tstate->exc_type); |
| 4121 | Py_XINCREF(tstate->exc_value); |
| 4122 | Py_XINCREF(tstate->exc_traceback); |
| 4123 | type = f->f_exc_type; |
| 4124 | value = f->f_exc_value; |
| 4125 | traceback = f->f_exc_traceback; |
| 4126 | f->f_exc_type = tstate->exc_type; |
| 4127 | f->f_exc_value = tstate->exc_value; |
| 4128 | f->f_exc_traceback = tstate->exc_traceback; |
| 4129 | Py_XDECREF(type); |
| 4130 | Py_XDECREF(value); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 4131 | Py_XDECREF(traceback); |
Benjamin Peterson | 8788024 | 2011-07-03 16:48:31 -0500 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | static void |
| 4135 | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4136 | { |
| 4137 | PyObject *tmp; |
| 4138 | tmp = tstate->exc_type; |
| 4139 | tstate->exc_type = f->f_exc_type; |
| 4140 | f->f_exc_type = tmp; |
| 4141 | tmp = tstate->exc_value; |
| 4142 | tstate->exc_value = f->f_exc_value; |
| 4143 | f->f_exc_value = tmp; |
| 4144 | tmp = tstate->exc_traceback; |
| 4145 | tstate->exc_traceback = f->f_exc_traceback; |
| 4146 | f->f_exc_traceback = tmp; |
| 4147 | } |
| 4148 | |
| 4149 | static void |
| 4150 | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
| 4151 | { |
| 4152 | PyObject *type, *value, *tb; |
| 4153 | type = tstate->exc_type; |
| 4154 | value = tstate->exc_value; |
| 4155 | tb = tstate->exc_traceback; |
| 4156 | tstate->exc_type = f->f_exc_type; |
| 4157 | tstate->exc_value = f->f_exc_value; |
| 4158 | tstate->exc_traceback = f->f_exc_traceback; |
| 4159 | f->f_exc_type = NULL; |
| 4160 | f->f_exc_value = NULL; |
| 4161 | f->f_exc_traceback = NULL; |
| 4162 | Py_XDECREF(type); |
| 4163 | Py_XDECREF(value); |
| 4164 | Py_XDECREF(tb); |
| 4165 | } |
| 4166 | |
| 4167 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4168 | /* Logic for the raise statement (too complicated for inlining). |
| 4169 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4170 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4171 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4173 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 | if (exc == NULL) { |
| 4176 | /* Reraise */ |
| 4177 | PyThreadState *tstate = PyThreadState_GET(); |
| 4178 | PyObject *tb; |
| 4179 | type = tstate->exc_type; |
| 4180 | value = tstate->exc_value; |
| 4181 | tb = tstate->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4182 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4183 | PyErr_SetString(PyExc_RuntimeError, |
| 4184 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4185 | return 0; |
| 4186 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | Py_XINCREF(type); |
| 4188 | Py_XINCREF(value); |
| 4189 | Py_XINCREF(tb); |
| 4190 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4191 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4192 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4194 | /* We support the following forms of raise: |
| 4195 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4196 | raise <instance> |
| 4197 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | if (PyExceptionClass_Check(exc)) { |
| 4200 | type = exc; |
| 4201 | value = PyObject_CallObject(exc, NULL); |
| 4202 | if (value == NULL) |
| 4203 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4204 | if (!PyExceptionInstance_Check(value)) { |
| 4205 | PyErr_Format(PyExc_TypeError, |
| 4206 | "calling %R should have returned an instance of " |
| 4207 | "BaseException, not %R", |
| 4208 | type, Py_TYPE(value)); |
| 4209 | goto raise_error; |
| 4210 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | } |
| 4212 | else if (PyExceptionInstance_Check(exc)) { |
| 4213 | value = exc; |
| 4214 | type = PyExceptionInstance_Class(exc); |
| 4215 | Py_INCREF(type); |
| 4216 | } |
| 4217 | else { |
| 4218 | /* Not something you can raise. You get an exception |
| 4219 | anyway, just not what you specified :-) */ |
| 4220 | Py_DECREF(exc); |
| 4221 | PyErr_SetString(PyExc_TypeError, |
| 4222 | "exceptions must derive from BaseException"); |
| 4223 | goto raise_error; |
| 4224 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4225 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4226 | assert(type != NULL); |
| 4227 | assert(value != NULL); |
| 4228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4229 | if (cause) { |
| 4230 | PyObject *fixed_cause; |
| 4231 | if (PyExceptionClass_Check(cause)) { |
| 4232 | fixed_cause = PyObject_CallObject(cause, NULL); |
| 4233 | if (fixed_cause == NULL) |
| 4234 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4235 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4237 | else if (PyExceptionInstance_Check(cause)) { |
| 4238 | fixed_cause = cause; |
| 4239 | } |
| 4240 | else if (cause == Py_None) { |
| 4241 | Py_DECREF(cause); |
| 4242 | fixed_cause = NULL; |
| 4243 | } |
| 4244 | else { |
| 4245 | PyErr_SetString(PyExc_TypeError, |
| 4246 | "exception causes must derive from " |
| 4247 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | goto raise_error; |
| 4249 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4250 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4253 | PyErr_SetObject(type, value); |
| 4254 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4255 | Py_DECREF(value); |
| 4256 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4257 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4258 | |
| 4259 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | Py_XDECREF(value); |
| 4261 | Py_XDECREF(type); |
| 4262 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4263 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4266 | /* 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] | 4267 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4268 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4269 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4270 | with a variable target. |
| 4271 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4272 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4273 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4274 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4276 | int i = 0, j = 0; |
| 4277 | Py_ssize_t ll = 0; |
| 4278 | PyObject *it; /* iter(v) */ |
| 4279 | PyObject *w; |
| 4280 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4282 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4284 | it = PyObject_GetIter(v); |
| 4285 | if (it == NULL) |
| 4286 | goto Error; |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4288 | for (; i < argcnt; i++) { |
| 4289 | w = PyIter_Next(it); |
| 4290 | if (w == NULL) { |
| 4291 | /* Iterator done, via error or exhaustion. */ |
| 4292 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4293 | if (argcntafter == -1) { |
| 4294 | PyErr_Format(PyExc_ValueError, |
| 4295 | "not enough values to unpack (expected %d, got %d)", |
| 4296 | argcnt, i); |
| 4297 | } |
| 4298 | else { |
| 4299 | PyErr_Format(PyExc_ValueError, |
| 4300 | "not enough values to unpack " |
| 4301 | "(expected at least %d, got %d)", |
| 4302 | argcnt + argcntafter, i); |
| 4303 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4304 | } |
| 4305 | goto Error; |
| 4306 | } |
| 4307 | *--sp = w; |
| 4308 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | if (argcntafter == -1) { |
| 4311 | /* We better have exhausted the iterator now. */ |
| 4312 | w = PyIter_Next(it); |
| 4313 | if (w == NULL) { |
| 4314 | if (PyErr_Occurred()) |
| 4315 | goto Error; |
| 4316 | Py_DECREF(it); |
| 4317 | return 1; |
| 4318 | } |
| 4319 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4320 | PyErr_Format(PyExc_ValueError, |
| 4321 | "too many values to unpack (expected %d)", |
| 4322 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | goto Error; |
| 4324 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4326 | l = PySequence_List(it); |
| 4327 | if (l == NULL) |
| 4328 | goto Error; |
| 4329 | *--sp = l; |
| 4330 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4332 | ll = PyList_GET_SIZE(l); |
| 4333 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4334 | PyErr_Format(PyExc_ValueError, |
| 4335 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4336 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4337 | goto Error; |
| 4338 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | /* Pop the "after-variable" args off the list. */ |
| 4341 | for (j = argcntafter; j > 0; j--, i++) { |
| 4342 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4343 | } |
| 4344 | /* Resize the list. */ |
| 4345 | Py_SIZE(l) = ll - argcntafter; |
| 4346 | Py_DECREF(it); |
| 4347 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4348 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4349 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4350 | for (; i > 0; i--, sp++) |
| 4351 | Py_DECREF(*sp); |
| 4352 | Py_XDECREF(it); |
| 4353 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4357 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4358 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4359 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | printf("%s ", str); |
| 4362 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4363 | PyErr_Clear(); /* Don't know what else to do */ |
| 4364 | printf("\n"); |
| 4365 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4366 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4367 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4368 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4369 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4370 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4371 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4372 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4373 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4375 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | if (value == NULL) { |
| 4377 | value = Py_None; |
| 4378 | Py_INCREF(value); |
| 4379 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4380 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4381 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4383 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4384 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | return; |
| 4386 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4387 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | Py_DECREF(arg); |
| 4389 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4390 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | else { |
| 4392 | Py_XDECREF(type); |
| 4393 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4394 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4395 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4398 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4399 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4400 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | PyObject *type, *value, *traceback; |
| 4404 | int err; |
| 4405 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4406 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | if (err == 0) |
| 4408 | { |
| 4409 | PyErr_Restore(type, value, traceback); |
| 4410 | return 0; |
| 4411 | } |
| 4412 | else { |
| 4413 | Py_XDECREF(type); |
| 4414 | Py_XDECREF(value); |
| 4415 | Py_XDECREF(traceback); |
| 4416 | return -1; |
| 4417 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4420 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4421 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4422 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | int result; |
| 4426 | if (tstate->tracing) |
| 4427 | return 0; |
| 4428 | tstate->tracing++; |
| 4429 | tstate->use_tracing = 0; |
| 4430 | result = func(obj, frame, what, arg); |
| 4431 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4432 | || (tstate->c_profilefunc != NULL)); |
| 4433 | tstate->tracing--; |
| 4434 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4437 | PyObject * |
| 4438 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4439 | { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4440 | PyThreadState *tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | int save_tracing = tstate->tracing; |
| 4442 | int save_use_tracing = tstate->use_tracing; |
| 4443 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4445 | tstate->tracing = 0; |
| 4446 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4447 | || (tstate->c_profilefunc != NULL)); |
| 4448 | result = PyObject_Call(func, args, NULL); |
| 4449 | tstate->tracing = save_tracing; |
| 4450 | tstate->use_tracing = save_use_tracing; |
| 4451 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4454 | /* 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] | 4455 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4456 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4457 | PyThreadState *tstate, PyFrameObject *frame, |
| 4458 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | int result = 0; |
| 4461 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | /* If the last instruction executed isn't in the current |
| 4464 | instruction window, reset the window. |
| 4465 | */ |
| 4466 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4467 | PyAddrPair bounds; |
| 4468 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4469 | &bounds); |
| 4470 | *instr_lb = bounds.ap_lower; |
| 4471 | *instr_ub = bounds.ap_upper; |
| 4472 | } |
| 4473 | /* If the last instruction falls at the start of a line or if |
| 4474 | it represents a jump backwards, update the frame's line |
| 4475 | number and call the trace function. */ |
| 4476 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 4477 | frame->f_lineno = line; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4478 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | } |
| 4480 | *instr_prev = frame->f_lasti; |
| 4481 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4484 | void |
| 4485 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | PyThreadState *tstate = PyThreadState_GET(); |
| 4488 | PyObject *temp = tstate->c_profileobj; |
| 4489 | Py_XINCREF(arg); |
| 4490 | tstate->c_profilefunc = NULL; |
| 4491 | tstate->c_profileobj = NULL; |
| 4492 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4493 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4494 | Py_XDECREF(temp); |
| 4495 | tstate->c_profilefunc = func; |
| 4496 | tstate->c_profileobj = arg; |
| 4497 | /* Flag that tracing or profiling is turned on */ |
| 4498 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | void |
| 4502 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | PyThreadState *tstate = PyThreadState_GET(); |
| 4505 | PyObject *temp = tstate->c_traceobj; |
| 4506 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4507 | Py_XINCREF(arg); |
| 4508 | tstate->c_tracefunc = NULL; |
| 4509 | tstate->c_traceobj = NULL; |
| 4510 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4511 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4512 | Py_XDECREF(temp); |
| 4513 | tstate->c_tracefunc = func; |
| 4514 | tstate->c_traceobj = arg; |
| 4515 | /* Flag that tracing or profiling is turned on */ |
| 4516 | tstate->use_tracing = ((func != NULL) |
| 4517 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4520 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4521 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4522 | { |
| 4523 | PyThreadState *tstate = PyThreadState_GET(); |
| 4524 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4525 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4526 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4530 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4531 | { |
| 4532 | PyThreadState *tstate = PyThreadState_GET(); |
| 4533 | return tstate->coroutine_wrapper; |
| 4534 | } |
| 4535 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4536 | void |
| 4537 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4538 | { |
| 4539 | PyThreadState *tstate = PyThreadState_GET(); |
| 4540 | |
| 4541 | Py_XINCREF(firstiter); |
| 4542 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4543 | } |
| 4544 | |
| 4545 | PyObject * |
| 4546 | _PyEval_GetAsyncGenFirstiter(void) |
| 4547 | { |
| 4548 | PyThreadState *tstate = PyThreadState_GET(); |
| 4549 | return tstate->async_gen_firstiter; |
| 4550 | } |
| 4551 | |
| 4552 | void |
| 4553 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4554 | { |
| 4555 | PyThreadState *tstate = PyThreadState_GET(); |
| 4556 | |
| 4557 | Py_XINCREF(finalizer); |
| 4558 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4559 | } |
| 4560 | |
| 4561 | PyObject * |
| 4562 | _PyEval_GetAsyncGenFinalizer(void) |
| 4563 | { |
| 4564 | PyThreadState *tstate = PyThreadState_GET(); |
| 4565 | return tstate->async_gen_finalizer; |
| 4566 | } |
| 4567 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4568 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4569 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4571 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4572 | if (current_frame == NULL) |
| 4573 | return PyThreadState_GET()->interp->builtins; |
| 4574 | else |
| 4575 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4578 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4579 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4582 | if (current_frame == NULL) { |
| 4583 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4584 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4588 | return NULL; |
| 4589 | |
| 4590 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4594 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4595 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4598 | if (current_frame == NULL) |
| 4599 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4600 | |
| 4601 | assert(current_frame->f_globals != NULL); |
| 4602 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4605 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4606 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4608 | PyThreadState *tstate = PyThreadState_GET(); |
| 4609 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4612 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4613 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4615 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4616 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4618 | if (current_frame != NULL) { |
| 4619 | const int codeflags = current_frame->f_code->co_flags; |
| 4620 | const int compilerflags = codeflags & PyCF_MASK; |
| 4621 | if (compilerflags) { |
| 4622 | result = 1; |
| 4623 | cf->cf_flags |= compilerflags; |
| 4624 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4625 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4627 | result = 1; |
| 4628 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4629 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4630 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4631 | } |
| 4632 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4635 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4636 | /* External interface to call any callable object. |
Antoine Pitrou | 8689a10 | 2010-04-01 16:53:15 +0000 | [diff] [blame] | 4637 | The arg must be a tuple or NULL. The kw must be a dict or NULL. */ |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4638 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4639 | PyObject * |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4640 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4641 | { |
Victor Stinner | 59b356d | 2015-03-16 11:52:32 +0100 | [diff] [blame] | 4642 | #ifdef Py_DEBUG |
| 4643 | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
| 4644 | set. It raises a new exception if parameters are invalid or if |
| 4645 | PyTuple_New() fails, and so the original exception is lost. */ |
| 4646 | assert(!PyErr_Occurred()); |
| 4647 | #endif |
| 4648 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4649 | if (args == NULL) { |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4650 | return _PyObject_FastCallDict(func, NULL, 0, kwargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4651 | } |
Victor Stinner | 155ea65 | 2016-08-22 23:26:00 +0200 | [diff] [blame] | 4652 | |
| 4653 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | PyErr_SetString(PyExc_TypeError, |
| 4655 | "argument list must be a tuple"); |
| 4656 | return NULL; |
| 4657 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4658 | |
Victor Stinner | 8a31c82 | 2016-08-19 17:12:23 +0200 | [diff] [blame] | 4659 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | PyErr_SetString(PyExc_TypeError, |
| 4661 | "keyword list must be a dictionary"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4662 | return NULL; |
| 4663 | } |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 4664 | |
Victor Stinner | 6e2333d | 2016-08-23 00:25:01 +0200 | [diff] [blame] | 4665 | return PyObject_Call(func, args, kwargs); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4668 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4669 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | if (PyMethod_Check(func)) |
| 4672 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4673 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4674 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4675 | else if (PyCFunction_Check(func)) |
| 4676 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4677 | else |
| 4678 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4681 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4682 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4684 | if (PyMethod_Check(func)) |
| 4685 | return "()"; |
| 4686 | else if (PyFunction_Check(func)) |
| 4687 | return "()"; |
| 4688 | else if (PyCFunction_Check(func)) |
| 4689 | return "()"; |
| 4690 | else |
| 4691 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4694 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4695 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4696 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4697 | tstate, tstate->frame, \ |
| 4698 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | x = NULL; \ |
| 4700 | } \ |
| 4701 | else { \ |
| 4702 | x = call; \ |
| 4703 | if (tstate->c_profilefunc != NULL) { \ |
| 4704 | if (x == NULL) { \ |
| 4705 | call_trace_protected(tstate->c_profilefunc, \ |
| 4706 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4707 | tstate, tstate->frame, \ |
| 4708 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | /* XXX should pass (type, value, tb) */ \ |
| 4710 | } else { \ |
| 4711 | if (call_trace(tstate->c_profilefunc, \ |
| 4712 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4713 | tstate, tstate->frame, \ |
| 4714 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4715 | Py_DECREF(x); \ |
| 4716 | x = NULL; \ |
| 4717 | } \ |
| 4718 | } \ |
| 4719 | } \ |
| 4720 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4721 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4722 | x = call; \ |
| 4723 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4724 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 4725 | static PyObject* _Py_HOT_FUNCTION |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4726 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4727 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4728 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | PyObject *func = *pfunc; |
| 4730 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4731 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4732 | Py_ssize_t nargs = oparg - nkwargs; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4733 | PyObject **stack; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | /* Always dispatch PyCFunction first, because these are |
| 4736 | presumed to be the most frequent callable object. |
| 4737 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4738 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | PyThreadState *tstate = PyThreadState_GET(); |
Raymond Hettinger | a7f56bc | 2004-06-26 04:34:33 +0000 | [diff] [blame] | 4740 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4741 | stack = (*pp_stack) - nargs - nkwargs; |
| 4742 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4743 | } |
| 4744 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4745 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
Victor Stinner | b69ee8c | 2016-11-28 18:32:31 +0100 | [diff] [blame] | 4746 | /* Optimize access to bound methods. Reuse the Python stack |
| 4747 | to pass 'self' as the first argument, replace 'func' |
| 4748 | with 'self'. It avoids the creation of a new temporary tuple |
| 4749 | for arguments (to replace func with self) when the method uses |
| 4750 | FASTCALL. */ |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4751 | PyObject *self = PyMethod_GET_SELF(func); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4752 | Py_INCREF(self); |
| 4753 | func = PyMethod_GET_FUNCTION(func); |
| 4754 | Py_INCREF(func); |
| 4755 | Py_SETREF(*pfunc, self); |
| 4756 | nargs++; |
| 4757 | } |
| 4758 | else { |
| 4759 | Py_INCREF(func); |
| 4760 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4761 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4762 | stack = (*pp_stack) - nargs - nkwargs; |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4763 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4764 | if (PyFunction_Check(func)) { |
| 4765 | x = fast_function(func, stack, nargs, kwnames); |
| 4766 | } |
| 4767 | else { |
| 4768 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4769 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4770 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4771 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4772 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4773 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4774 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | /* Clear the stack of the function object. Also removes |
| 4777 | the arguments in case they weren't consumed already |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4778 | (fast_function() and err_args() leave them on the stack). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | */ |
| 4780 | while ((*pp_stack) > pfunc) { |
| 4781 | w = EXT_POP(*pp_stack); |
| 4782 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4783 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4788 | /* The fast_function() function optimize calls for which no argument |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4789 | tuple is necessary; the objects are passed directly from the stack. |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 4790 | For the simplest case -- a function that takes only positional |
| 4791 | arguments and is called with only positional arguments -- it |
| 4792 | inlines the most primitive frame setup code from |
| 4793 | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
| 4794 | done before evaluating the frame. |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4795 | */ |
| 4796 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 4797 | static PyObject* _Py_HOT_FUNCTION |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4798 | _PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
| 4799 | PyObject *globals) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4800 | { |
| 4801 | PyFrameObject *f; |
| 4802 | PyThreadState *tstate = PyThreadState_GET(); |
| 4803 | PyObject **fastlocals; |
| 4804 | Py_ssize_t i; |
| 4805 | PyObject *result; |
| 4806 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4807 | assert(globals != NULL); |
| 4808 | /* XXX Perhaps we should create a specialized |
| 4809 | PyFrame_New() that doesn't take locals, but does |
| 4810 | take builtins without sanity checking them. |
| 4811 | */ |
| 4812 | assert(tstate != NULL); |
| 4813 | f = PyFrame_New(tstate, co, globals, NULL); |
| 4814 | if (f == NULL) { |
| 4815 | return NULL; |
| 4816 | } |
| 4817 | |
| 4818 | fastlocals = f->f_localsplus; |
| 4819 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4820 | for (i = 0; i < nargs; i++) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4821 | Py_INCREF(*args); |
| 4822 | fastlocals[i] = *args++; |
| 4823 | } |
| 4824 | result = PyEval_EvalFrameEx(f,0); |
| 4825 | |
| 4826 | ++tstate->recursion_depth; |
| 4827 | Py_DECREF(f); |
| 4828 | --tstate->recursion_depth; |
| 4829 | |
| 4830 | return result; |
| 4831 | } |
| 4832 | |
Victor Stinner | e90bdb1 | 2016-08-25 23:26:50 +0200 | [diff] [blame] | 4833 | static PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4834 | fast_function(PyObject *func, PyObject **stack, |
| 4835 | Py_ssize_t nargs, PyObject *kwnames) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4837 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4838 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4839 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4840 | PyObject *kwdefs, *closure, *name, *qualname; |
| 4841 | PyObject **d; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4842 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4843 | Py_ssize_t nd; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4844 | |
Victor Stinner | 57f91ac | 2016-09-12 13:37:07 +0200 | [diff] [blame] | 4845 | assert(PyFunction_Check(func)); |
| 4846 | assert(nargs >= 0); |
| 4847 | assert(kwnames == NULL || PyTuple_CheckExact(kwnames)); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4848 | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
Victor Stinner | 57f91ac | 2016-09-12 13:37:07 +0200 | [diff] [blame] | 4849 | /* kwnames must only contains str strings, no subclass, and all keys must |
| 4850 | be unique */ |
Victor Stinner | 577e1f8 | 2016-08-25 00:29:32 +0200 | [diff] [blame] | 4851 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4852 | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4853 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4854 | { |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4855 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4856 | return _PyFunction_FastCall(co, stack, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4857 | } |
| 4858 | else if (nargs == 0 && argdefs != NULL |
| 4859 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4860 | /* function called with no arguments, but all parameters have |
| 4861 | a default value: use default values as arguments .*/ |
| 4862 | stack = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4863 | return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4864 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4865 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4866 | |
| 4867 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4868 | closure = PyFunction_GET_CLOSURE(func); |
| 4869 | name = ((PyFunctionObject *)func) -> func_name; |
| 4870 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | if (argdefs != NULL) { |
| 4873 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4874 | nd = Py_SIZE(argdefs); |
| 4875 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4876 | else { |
| 4877 | d = NULL; |
| 4878 | nd = 0; |
| 4879 | } |
| 4880 | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4881 | stack, nargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4882 | nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL, |
| 4883 | stack + nargs, |
| 4884 | nkwargs, 1, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4885 | d, (int)nd, kwdefs, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4886 | closure, name, qualname); |
| 4887 | } |
| 4888 | |
| 4889 | PyObject * |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4890 | _PyFunction_FastCallKeywords(PyObject *func, PyObject **stack, |
| 4891 | Py_ssize_t nargs, PyObject *kwnames) |
| 4892 | { |
| 4893 | return fast_function(func, stack, nargs, kwnames); |
| 4894 | } |
| 4895 | |
| 4896 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4897 | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4898 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4899 | { |
| 4900 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
| 4901 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 4902 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
| 4903 | PyObject *kwdefs, *closure, *name, *qualname; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4904 | PyObject *kwtuple, **k; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4905 | PyObject **d; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4906 | Py_ssize_t nd, nk; |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4907 | PyObject *result; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4908 | |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4909 | assert(func != NULL); |
| 4910 | assert(nargs >= 0); |
| 4911 | assert(nargs == 0 || args != NULL); |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4912 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4913 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4914 | if (co->co_kwonlyargcount == 0 && |
| 4915 | (kwargs == NULL || PyDict_Size(kwargs) == 0) && |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4916 | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
| 4917 | { |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4918 | /* Fast paths */ |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4919 | if (argdefs == NULL && co->co_argcount == nargs) { |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4920 | return _PyFunction_FastCall(co, args, nargs, globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4921 | } |
| 4922 | else if (nargs == 0 && argdefs != NULL |
| 4923 | && co->co_argcount == Py_SIZE(argdefs)) { |
| 4924 | /* function called with no arguments, but all parameters have |
| 4925 | a default value: use default values as arguments .*/ |
| 4926 | args = &PyTuple_GET_ITEM(argdefs, 0); |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4927 | return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals); |
Victor Stinner | 2eedc11 | 2016-08-22 12:29:42 +0200 | [diff] [blame] | 4928 | } |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4929 | } |
| 4930 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4931 | if (kwargs != NULL) { |
| 4932 | Py_ssize_t pos, i; |
| 4933 | nk = PyDict_Size(kwargs); |
| 4934 | |
| 4935 | kwtuple = PyTuple_New(2 * nk); |
| 4936 | if (kwtuple == NULL) { |
| 4937 | return NULL; |
| 4938 | } |
| 4939 | |
| 4940 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 4941 | pos = i = 0; |
| 4942 | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
| 4943 | Py_INCREF(k[i]); |
| 4944 | Py_INCREF(k[i+1]); |
| 4945 | i += 2; |
| 4946 | } |
| 4947 | nk = i / 2; |
| 4948 | } |
| 4949 | else { |
| 4950 | kwtuple = NULL; |
| 4951 | k = NULL; |
| 4952 | nk = 0; |
| 4953 | } |
| 4954 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4955 | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
| 4956 | closure = PyFunction_GET_CLOSURE(func); |
| 4957 | name = ((PyFunctionObject *)func) -> func_name; |
| 4958 | qualname = ((PyFunctionObject *)func) -> func_qualname; |
| 4959 | |
| 4960 | if (argdefs != NULL) { |
| 4961 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 4962 | nd = Py_SIZE(argdefs); |
| 4963 | } |
| 4964 | else { |
| 4965 | d = NULL; |
| 4966 | nd = 0; |
| 4967 | } |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4968 | |
| 4969 | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
| 4970 | args, nargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4971 | k, k + 1, nk, 2, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 4972 | d, nd, kwdefs, |
| 4973 | closure, name, qualname); |
| 4974 | Py_XDECREF(kwtuple); |
| 4975 | return result; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4979 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | if (PyCFunction_Check(func)) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4982 | PyObject *result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | PyThreadState *tstate = PyThreadState_GET(); |
| 4984 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4985 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4986 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4987 | else { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4988 | return PyObject_Call(func, callargs, kwdict); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4989 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 4992 | /* 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] | 4993 | nb_index slot defined, and store in *pi. |
| 4994 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
| 4995 | and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4996 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4997 | */ |
Tim Peters | b519638 | 2001-12-16 19:44:20 +0000 | [diff] [blame] | 4998 | /* Note: If v is NULL, return success without storing into *pi. This |
| 4999 | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
| 5000 | called by the SLICE opcode with v and/or w equal to NULL. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5001 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5002 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5003 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5005 | if (v != NULL) { |
| 5006 | Py_ssize_t x; |
| 5007 | if (PyIndex_Check(v)) { |
| 5008 | x = PyNumber_AsSsize_t(v, NULL); |
| 5009 | if (x == -1 && PyErr_Occurred()) |
| 5010 | return 0; |
| 5011 | } |
| 5012 | else { |
| 5013 | PyErr_SetString(PyExc_TypeError, |
| 5014 | "slice indices must be integers or " |
| 5015 | "None or have an __index__ method"); |
| 5016 | return 0; |
| 5017 | } |
| 5018 | *pi = x; |
| 5019 | } |
| 5020 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5023 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5025 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5026 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 5027 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5028 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5029 | int res = 0; |
| 5030 | switch (op) { |
| 5031 | case PyCmp_IS: |
| 5032 | res = (v == w); |
| 5033 | break; |
| 5034 | case PyCmp_IS_NOT: |
| 5035 | res = (v != w); |
| 5036 | break; |
| 5037 | case PyCmp_IN: |
| 5038 | res = PySequence_Contains(w, v); |
| 5039 | if (res < 0) |
| 5040 | return NULL; |
| 5041 | break; |
| 5042 | case PyCmp_NOT_IN: |
| 5043 | res = PySequence_Contains(w, v); |
| 5044 | if (res < 0) |
| 5045 | return NULL; |
| 5046 | res = !res; |
| 5047 | break; |
| 5048 | case PyCmp_EXC_MATCH: |
| 5049 | if (PyTuple_Check(w)) { |
| 5050 | Py_ssize_t i, length; |
| 5051 | length = PyTuple_Size(w); |
| 5052 | for (i = 0; i < length; i += 1) { |
| 5053 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5054 | if (!PyExceptionClass_Check(exc)) { |
| 5055 | PyErr_SetString(PyExc_TypeError, |
| 5056 | CANNOT_CATCH_MSG); |
| 5057 | return NULL; |
| 5058 | } |
| 5059 | } |
| 5060 | } |
| 5061 | else { |
| 5062 | if (!PyExceptionClass_Check(w)) { |
| 5063 | PyErr_SetString(PyExc_TypeError, |
| 5064 | CANNOT_CATCH_MSG); |
| 5065 | return NULL; |
| 5066 | } |
| 5067 | } |
| 5068 | res = PyErr_GivenExceptionMatches(v, w); |
| 5069 | break; |
| 5070 | default: |
| 5071 | return PyObject_RichCompare(v, w, op); |
| 5072 | } |
| 5073 | v = res ? Py_True : Py_False; |
| 5074 | Py_INCREF(v); |
| 5075 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5078 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5079 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 5080 | { |
| 5081 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5082 | PyObject *import_func, *res; |
| 5083 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5084 | |
| 5085 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 5086 | if (import_func == NULL) { |
| 5087 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | |
| 5091 | /* Fast path for not overloaded __import__. */ |
| 5092 | if (import_func == PyThreadState_GET()->interp->import_func) { |
| 5093 | int ilevel = _PyLong_AsInt(level); |
| 5094 | if (ilevel == -1 && PyErr_Occurred()) { |
| 5095 | return NULL; |
| 5096 | } |
| 5097 | res = PyImport_ImportModuleLevelObject( |
| 5098 | name, |
| 5099 | f->f_globals, |
| 5100 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5101 | fromlist, |
| 5102 | ilevel); |
| 5103 | return res; |
| 5104 | } |
| 5105 | |
| 5106 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5107 | |
| 5108 | stack[0] = name; |
| 5109 | stack[1] = f->f_globals; |
| 5110 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5111 | stack[3] = fromlist; |
| 5112 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5113 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5114 | Py_DECREF(import_func); |
| 5115 | return res; |
| 5116 | } |
| 5117 | |
| 5118 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5119 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5122 | _Py_IDENTIFIER(__name__); |
| 5123 | PyObject *fullmodname, *pkgname; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5125 | x = PyObject_GetAttr(v, name); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5126 | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5127 | return x; |
| 5128 | /* Issue #17636: in case this failed because of a circular relative |
| 5129 | import, try to fallback on reading the module directly from |
| 5130 | sys.modules. */ |
| 5131 | PyErr_Clear(); |
| 5132 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5133 | if (pkgname == NULL) { |
| 5134 | goto error; |
| 5135 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5136 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
| 5137 | Py_DECREF(pkgname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5138 | if (fullmodname == NULL) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5139 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5140 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5141 | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5142 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5143 | if (x == NULL) { |
| 5144 | goto error; |
| 5145 | } |
| 5146 | Py_INCREF(x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5147 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5148 | error: |
| 5149 | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
| 5150 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5151 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5152 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5153 | static int |
| 5154 | import_all_from(PyObject *locals, PyObject *v) |
| 5155 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5156 | _Py_IDENTIFIER(__all__); |
| 5157 | _Py_IDENTIFIER(__dict__); |
| 5158 | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5159 | PyObject *dict, *name, *value; |
| 5160 | int skip_leading_underscores = 0; |
| 5161 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5163 | if (all == NULL) { |
| 5164 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5165 | return -1; /* Unexpected error */ |
| 5166 | PyErr_Clear(); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5167 | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | if (dict == NULL) { |
| 5169 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5170 | return -1; |
| 5171 | PyErr_SetString(PyExc_ImportError, |
| 5172 | "from-import-* object has no __dict__ and no __all__"); |
| 5173 | return -1; |
| 5174 | } |
| 5175 | all = PyMapping_Keys(dict); |
| 5176 | Py_DECREF(dict); |
| 5177 | if (all == NULL) |
| 5178 | return -1; |
| 5179 | skip_leading_underscores = 1; |
| 5180 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5182 | for (pos = 0, err = 0; ; pos++) { |
| 5183 | name = PySequence_GetItem(all, pos); |
| 5184 | if (name == NULL) { |
| 5185 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 5186 | err = -1; |
| 5187 | else |
| 5188 | PyErr_Clear(); |
| 5189 | break; |
| 5190 | } |
| 5191 | if (skip_leading_underscores && |
| 5192 | PyUnicode_Check(name) && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5193 | PyUnicode_READY(name) != -1 && |
| 5194 | PyUnicode_READ_CHAR(name, 0) == '_') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5195 | { |
| 5196 | Py_DECREF(name); |
| 5197 | continue; |
| 5198 | } |
| 5199 | value = PyObject_GetAttr(v, name); |
| 5200 | if (value == NULL) |
| 5201 | err = -1; |
| 5202 | else if (PyDict_CheckExact(locals)) |
| 5203 | err = PyDict_SetItem(locals, name, value); |
| 5204 | else |
| 5205 | err = PyObject_SetItem(locals, name, value); |
| 5206 | Py_DECREF(name); |
| 5207 | Py_XDECREF(value); |
| 5208 | if (err != 0) |
| 5209 | break; |
| 5210 | } |
| 5211 | Py_DECREF(all); |
| 5212 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5215 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5216 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | if (!obj) |
| 5221 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5222 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5223 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5224 | if (!obj_str) |
| 5225 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5227 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5228 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5229 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5230 | static void |
| 5231 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5232 | { |
| 5233 | PyObject *name; |
| 5234 | /* Don't stomp existing exception */ |
| 5235 | if (PyErr_Occurred()) |
| 5236 | return; |
| 5237 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5238 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5239 | oparg); |
| 5240 | format_exc_check_arg( |
| 5241 | PyExc_UnboundLocalError, |
| 5242 | UNBOUNDLOCAL_ERROR_MSG, |
| 5243 | name); |
| 5244 | } else { |
| 5245 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5246 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5247 | format_exc_check_arg(PyExc_NameError, |
| 5248 | UNBOUNDFREE_ERROR_MSG, name); |
| 5249 | } |
| 5250 | } |
| 5251 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5252 | static PyObject * |
| 5253 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5254 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5255 | { |
| 5256 | PyObject *res; |
| 5257 | if (Py_REFCNT(v) == 2) { |
| 5258 | /* In the common case, there are 2 references to the value |
| 5259 | * stored in 'variable' when the += is performed: one on the |
| 5260 | * value stack (in 'v') and one still stored in the |
| 5261 | * 'variable'. We try to delete the variable now to reduce |
| 5262 | * the refcnt to 1. |
| 5263 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5264 | int opcode, oparg; |
| 5265 | NEXTOPARG(); |
| 5266 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5267 | case STORE_FAST: |
| 5268 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5269 | PyObject **fastlocals = f->f_localsplus; |
| 5270 | if (GETLOCAL(oparg) == v) |
| 5271 | SETLOCAL(oparg, NULL); |
| 5272 | break; |
| 5273 | } |
| 5274 | case STORE_DEREF: |
| 5275 | { |
| 5276 | PyObject **freevars = (f->f_localsplus + |
| 5277 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5278 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5279 | if (PyCell_GET(c) == v) { |
| 5280 | PyCell_SET(c, NULL); |
| 5281 | Py_DECREF(v); |
| 5282 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5283 | break; |
| 5284 | } |
| 5285 | case STORE_NAME: |
| 5286 | { |
| 5287 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5288 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5289 | PyObject *locals = f->f_locals; |
| 5290 | if (PyDict_CheckExact(locals) && |
| 5291 | PyDict_GetItem(locals, name) == v) { |
| 5292 | if (PyDict_DelItem(locals, name) != 0) { |
| 5293 | PyErr_Clear(); |
| 5294 | } |
| 5295 | } |
| 5296 | break; |
| 5297 | } |
| 5298 | } |
| 5299 | } |
| 5300 | res = v; |
| 5301 | PyUnicode_Append(&res, w); |
| 5302 | return res; |
| 5303 | } |
| 5304 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5305 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5306 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5307 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5308 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | int i; |
| 5311 | PyObject *l = PyList_New(256); |
| 5312 | if (l == NULL) return NULL; |
| 5313 | for (i = 0; i < 256; i++) { |
| 5314 | PyObject *x = PyLong_FromLong(a[i]); |
| 5315 | if (x == NULL) { |
| 5316 | Py_DECREF(l); |
| 5317 | return NULL; |
| 5318 | } |
| 5319 | PyList_SetItem(l, i, x); |
| 5320 | } |
| 5321 | for (i = 0; i < 256; i++) |
| 5322 | a[i] = 0; |
| 5323 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5327 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5328 | { |
| 5329 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5331 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5332 | int i; |
| 5333 | PyObject *l = PyList_New(257); |
| 5334 | if (l == NULL) return NULL; |
| 5335 | for (i = 0; i < 257; i++) { |
| 5336 | PyObject *x = getarray(dxpairs[i]); |
| 5337 | if (x == NULL) { |
| 5338 | Py_DECREF(l); |
| 5339 | return NULL; |
| 5340 | } |
| 5341 | PyList_SetItem(l, i, x); |
| 5342 | } |
| 5343 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5344 | #endif |
| 5345 | } |
| 5346 | |
| 5347 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5348 | |
| 5349 | Py_ssize_t |
| 5350 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5351 | { |
| 5352 | PyThreadState *tstate = PyThreadState_Get(); |
| 5353 | Py_ssize_t new_index; |
| 5354 | |
| 5355 | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
| 5356 | return -1; |
| 5357 | } |
| 5358 | new_index = tstate->co_extra_user_count++; |
| 5359 | tstate->co_extra_freefuncs[new_index] = free; |
| 5360 | return new_index; |
| 5361 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5362 | |
| 5363 | static void |
| 5364 | dtrace_function_entry(PyFrameObject *f) |
| 5365 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5366 | const char *filename; |
| 5367 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5368 | int lineno; |
| 5369 | |
| 5370 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5371 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5372 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5373 | |
| 5374 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5375 | } |
| 5376 | |
| 5377 | static void |
| 5378 | dtrace_function_return(PyFrameObject *f) |
| 5379 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5380 | const char *filename; |
| 5381 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5382 | int lineno; |
| 5383 | |
| 5384 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5385 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5386 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5387 | |
| 5388 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5389 | } |
| 5390 | |
| 5391 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5392 | static void |
| 5393 | maybe_dtrace_line(PyFrameObject *frame, |
| 5394 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5395 | { |
| 5396 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5397 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5398 | |
| 5399 | /* If the last instruction executed isn't in the current |
| 5400 | instruction window, reset the window. |
| 5401 | */ |
| 5402 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5403 | PyAddrPair bounds; |
| 5404 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5405 | &bounds); |
| 5406 | *instr_lb = bounds.ap_lower; |
| 5407 | *instr_ub = bounds.ap_upper; |
| 5408 | } |
| 5409 | /* If the last instruction falls at the start of a line or if |
| 5410 | it represents a jump backwards, update the frame's line |
| 5411 | number and call the trace function. */ |
| 5412 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5413 | frame->f_lineno = line; |
| 5414 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5415 | if (!co_filename) |
| 5416 | co_filename = "?"; |
| 5417 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5418 | if (!co_name) |
| 5419 | co_name = "?"; |
| 5420 | PyDTrace_LINE(co_filename, co_name, line); |
| 5421 | } |
| 5422 | *instr_prev = frame->f_lasti; |
| 5423 | } |