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