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