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