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