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" |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 13 | #include "pycore_ceval.h" |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 14 | #include "pycore_code.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 15 | #include "pycore_object.h" |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 16 | #include "pycore_pyerrors.h" |
| 17 | #include "pycore_pylifecycle.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 18 | #include "pycore_pystate.h" |
Victor Stinner | ec13b93 | 2018-11-25 23:56:17 +0100 | [diff] [blame] | 19 | #include "pycore_tupleobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 20 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 21 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 22 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 23 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 24 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 25 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 26 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 27 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
| 30 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 31 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 32 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | #define LLTRACE 1 /* Low-level trace feature */ |
| 34 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 37 | #if !defined(Py_BUILD_CORE) |
| 38 | # error "ceval.c must be build with Py_BUILD_CORE define for best performance" |
| 39 | #endif |
| 40 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 41 | /* Private API for the LOAD_METHOD opcode. */ |
| 42 | extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **); |
| 43 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 44 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 46 | /* Forward declarations */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 47 | Py_LOCAL_INLINE(PyObject *) call_function( |
| 48 | PyThreadState *tstate, PyObject ***pp_stack, |
| 49 | Py_ssize_t oparg, PyObject *kwnames); |
| 50 | static PyObject * do_call_core( |
| 51 | PyThreadState *tstate, PyObject *func, |
| 52 | PyObject *callargs, PyObject *kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 54 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 55 | static int lltrace; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 56 | static int prtrace(PyThreadState *, PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 57 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 58 | static int call_trace(Py_tracefunc, PyObject *, |
| 59 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 61 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 62 | PyThreadState *, PyFrameObject *, |
| 63 | int, PyObject *); |
| 64 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 65 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 66 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 67 | PyThreadState *, PyFrameObject *, |
| 68 | int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 69 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 70 | static void dtrace_function_entry(PyFrameObject *); |
| 71 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 72 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 73 | static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *); |
| 74 | static PyObject * import_name(PyThreadState *, PyFrameObject *, |
| 75 | PyObject *, PyObject *, PyObject *); |
| 76 | static PyObject * import_from(PyThreadState *, PyObject *, PyObject *); |
| 77 | static int import_all_from(PyThreadState *, PyObject *, PyObject *); |
| 78 | static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *); |
| 79 | static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg); |
| 80 | static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 81 | PyFrameObject *, const _Py_CODEUNIT *); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 82 | static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *); |
| 83 | static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg); |
| 84 | static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs); |
| 85 | static void format_awaitable_error(PyThreadState *, PyTypeObject *, int); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 86 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 87 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 89 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 91 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | "free variable '%.200s' referenced before assignment" \ |
| 93 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 95 | /* Dynamic execution profile */ |
| 96 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 97 | #ifdef DXPAIRS |
| 98 | static long dxpairs[257][256]; |
| 99 | #define dxp dxpairs[256] |
| 100 | #else |
| 101 | static long dxp[256]; |
| 102 | #endif |
| 103 | #endif |
| 104 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 105 | /* per opcode cache */ |
| 106 | #define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */ |
| 107 | #define OPCACHE_STATS 0 /* Enable stats */ |
| 108 | |
| 109 | #if OPCACHE_STATS |
| 110 | static size_t opcache_code_objects = 0; |
| 111 | static size_t opcache_code_objects_extra_mem = 0; |
| 112 | |
| 113 | static size_t opcache_global_opts = 0; |
| 114 | static size_t opcache_global_hits = 0; |
| 115 | static size_t opcache_global_misses = 0; |
| 116 | #endif |
| 117 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 118 | #define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request) |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 119 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 120 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 121 | 1. We believe this is all right because the eval loop will release |
| 122 | the GIL eventually anyway. */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 123 | #define COMPUTE_EVAL_BREAKER(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | _Py_atomic_store_relaxed( \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 125 | &(ceval)->eval_breaker, \ |
| 126 | GIL_REQUEST | \ |
| 127 | _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \ |
| 128 | _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \ |
| 129 | (ceval)->pending.async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 130 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 131 | #define SET_GIL_DROP_REQUEST(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 133 | _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \ |
| 134 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 136 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 137 | #define RESET_GIL_DROP_REQUEST(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 139 | _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \ |
| 140 | COMPUTE_EVAL_BREAKER(ceval); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 142 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 143 | /* Pending calls are only modified under pending_lock */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 144 | #define SIGNAL_PENDING_CALLS(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 146 | _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \ |
| 147 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 149 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 150 | #define UNSIGNAL_PENDING_CALLS(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 152 | _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \ |
| 153 | COMPUTE_EVAL_BREAKER(ceval); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 155 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 156 | #define SIGNAL_PENDING_SIGNALS(ceval) \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 157 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 158 | _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \ |
| 159 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 160 | } while (0) |
| 161 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 162 | #define UNSIGNAL_PENDING_SIGNALS(ceval) \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 163 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 164 | _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \ |
| 165 | COMPUTE_EVAL_BREAKER(ceval); \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 166 | } while (0) |
| 167 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 168 | #define SIGNAL_ASYNC_EXC(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 170 | (ceval)->pending.async_exc = 1; \ |
| 171 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 173 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 174 | #define UNSIGNAL_ASYNC_EXC(ceval) \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 175 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 176 | (ceval)->pending.async_exc = 0; \ |
| 177 | COMPUTE_EVAL_BREAKER(ceval); \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 178 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 179 | |
| 180 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 181 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 182 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 183 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 184 | #include "pythread.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 185 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 186 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 187 | int |
| 188 | PyEval_ThreadsInitialized(void) |
| 189 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 190 | return gil_created(&_PyRuntime.ceval.gil); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 193 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 194 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 195 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 196 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 197 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 198 | struct _gil_runtime_state *gil = &ceval->gil; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 199 | if (gil_created(gil)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | return; |
Victor Stinner | a712679 | 2019-03-19 14:19:38 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 203 | PyThread_init_thread(); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 204 | create_gil(gil); |
| 205 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 206 | take_gil(ceval, tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 207 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 208 | struct _pending_calls *pending = &ceval->pending; |
| 209 | pending->lock = PyThread_allocate_lock(); |
| 210 | if (pending->lock == NULL) { |
| 211 | Py_FatalError("Can't initialize threads for pending calls"); |
| 212 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 213 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 215 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 216 | _PyEval_FiniThreads(struct _ceval_runtime_state *ceval) |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 217 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 218 | struct _gil_runtime_state *gil = &ceval->gil; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 219 | if (!gil_created(gil)) { |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 220 | return; |
Victor Stinner | a712679 | 2019-03-19 14:19:38 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 223 | destroy_gil(gil); |
| 224 | assert(!gil_created(gil)); |
Victor Stinner | 99fcc61 | 2019-04-29 13:04:07 +0200 | [diff] [blame] | 225 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 226 | struct _pending_calls *pending = &ceval->pending; |
| 227 | if (pending->lock != NULL) { |
| 228 | PyThread_free_lock(pending->lock); |
| 229 | pending->lock = NULL; |
| 230 | } |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 233 | static inline void |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 234 | exit_thread_if_finalizing(PyThreadState *tstate) |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 235 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 236 | _PyRuntimeState *runtime = tstate->interp->runtime; |
| 237 | /* _Py_Finalizing is protected by the GIL */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 238 | if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 239 | drop_gil(&runtime->ceval, tstate); |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 240 | PyThread_exit_thread(); |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 244 | void |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 245 | _PyEval_Fini(void) |
| 246 | { |
| 247 | #if OPCACHE_STATS |
| 248 | fprintf(stderr, "-- Opcode cache number of objects = %zd\n", |
| 249 | opcache_code_objects); |
| 250 | |
| 251 | fprintf(stderr, "-- Opcode cache total extra mem = %zd\n", |
| 252 | opcache_code_objects_extra_mem); |
| 253 | |
| 254 | fprintf(stderr, "\n"); |
| 255 | |
| 256 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n", |
| 257 | opcache_global_hits, |
| 258 | (int) (100.0 * opcache_global_hits / |
| 259 | (opcache_global_hits + opcache_global_misses))); |
| 260 | |
| 261 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n", |
| 262 | opcache_global_misses, |
| 263 | (int) (100.0 * opcache_global_misses / |
| 264 | (opcache_global_hits + opcache_global_misses))); |
| 265 | |
| 266 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n", |
| 267 | opcache_global_opts); |
| 268 | |
| 269 | fprintf(stderr, "\n"); |
| 270 | #endif |
| 271 | } |
| 272 | |
| 273 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 274 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 275 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 276 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 277 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 278 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 279 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 281 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 282 | take_gil(ceval, tstate); |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 283 | exit_thread_if_finalizing(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 287 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 288 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 289 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 290 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 292 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | in debug mode. |
| 294 | */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 295 | drop_gil(&runtime->ceval, tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 299 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 300 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 301 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 303 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 304 | assert(tstate->interp != NULL); |
| 305 | |
| 306 | _PyRuntimeState *runtime = tstate->interp->runtime; |
| 307 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 310 | assert(gil_created(&ceval->gil)); |
| 311 | take_gil(ceval, tstate); |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 312 | exit_thread_if_finalizing(tstate); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 313 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
| 314 | Py_FatalError("PyEval_AcquireThread: non-NULL old thread state"); |
| 315 | } |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 319 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 320 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 321 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 323 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 324 | assert(tstate->interp != NULL); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 325 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 326 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 327 | PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 328 | if (new_tstate != tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 330 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 331 | drop_gil(&runtime->ceval, tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 332 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 334 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
| 335 | * which are not running in the child process, and clear internal locks |
| 336 | * which might be held by those threads. |
| 337 | */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 338 | |
| 339 | void |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 340 | _PyEval_ReInitThreads(_PyRuntimeState *runtime) |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 341 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 342 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 343 | if (!gil_created(&ceval->gil)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | return; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 345 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 346 | recreate_gil(&ceval->gil); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 347 | PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 348 | take_gil(ceval, current_tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 349 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 350 | struct _pending_calls *pending = &ceval->pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 351 | pending->lock = PyThread_allocate_lock(); |
| 352 | if (pending->lock == NULL) { |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 353 | Py_FatalError("Can't initialize threads for pending calls"); |
| 354 | } |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 356 | /* Destroy all threads except the current one */ |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 357 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 358 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 360 | /* This function is used to signal that async exceptions are waiting to be |
Zackery Spytz | eef0596 | 2018-09-29 10:07:11 -0600 | [diff] [blame] | 361 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 362 | |
| 363 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 364 | _PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 365 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 366 | SIGNAL_ASYNC_EXC(ceval); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 367 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 369 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 370 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 371 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 372 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 373 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 374 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 375 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 377 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 378 | assert(gil_created(&ceval->gil)); |
| 379 | drop_gil(ceval, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 384 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 385 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 386 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 388 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 389 | assert(tstate->interp != NULL); |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 390 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 391 | _PyRuntimeState *runtime = tstate->interp->runtime; |
| 392 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 393 | assert(gil_created(&ceval->gil)); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 394 | |
| 395 | int err = errno; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 396 | take_gil(ceval, tstate); |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 397 | exit_thread_if_finalizing(tstate); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 398 | errno = err; |
| 399 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 400 | _PyThreadState_Swap(&runtime->gilstate, tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 404 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 405 | signal handlers or Mac I/O completion routines) can schedule calls |
| 406 | to a function to be called synchronously. |
| 407 | The synchronous function is called with one void* argument. |
| 408 | It should return 0 for success or -1 for failure -- failure should |
| 409 | be accompanied by an exception. |
| 410 | |
| 411 | If registry succeeds, the registry function returns 0; if it fails |
| 412 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 413 | an exception condition). |
| 414 | |
| 415 | Note that because registry may occur from within signal handlers, |
| 416 | or other asynchronous events, calling malloc() is unsafe! |
| 417 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 418 | Any thread can schedule pending calls, but only the main thread |
| 419 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 420 | There is no facility to schedule calls to a particular thread, but |
| 421 | that should be easy to change, should that ever be required. In |
| 422 | that case, the static variables here should go into the python |
| 423 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 424 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 426 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 427 | _PyEval_SignalReceived(struct _ceval_runtime_state *ceval) |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 428 | { |
| 429 | /* bpo-30703: Function called when the C signal handler of Python gets a |
| 430 | signal. We cannot queue a callback using Py_AddPendingCall() since |
| 431 | that function is not async-signal-safe. */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 432 | SIGNAL_PENDING_SIGNALS(ceval); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 435 | /* Push one item onto the queue while holding the lock. */ |
| 436 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 437 | _push_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 438 | int (*func)(void *), void *arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 439 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 440 | int i = pending->last; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 441 | int j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 442 | if (j == pending->first) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 443 | return -1; /* Queue full */ |
| 444 | } |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 445 | pending->calls[i].func = func; |
| 446 | pending->calls[i].arg = arg; |
| 447 | pending->last = j; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* Pop one item off the queue while holding the lock. */ |
| 452 | static void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 453 | _pop_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 454 | int (**func)(void *), void **arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 455 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 456 | int i = pending->first; |
| 457 | if (i == pending->last) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 458 | return; /* Queue empty */ |
| 459 | } |
| 460 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 461 | *func = pending->calls[i].func; |
| 462 | *arg = pending->calls[i].arg; |
| 463 | pending->first = (i + 1) % NPENDINGCALLS; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 466 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 467 | scheduling to be made from any thread, and even from an executing |
| 468 | callback. |
| 469 | */ |
| 470 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 471 | int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 472 | _PyEval_AddPendingCall(PyThreadState *tstate, |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 473 | struct _ceval_runtime_state *ceval, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 474 | int (*func)(void *), void *arg) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 475 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 476 | struct _pending_calls *pending = &ceval->pending; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 477 | |
| 478 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
| 479 | if (pending->finishing) { |
| 480 | PyThread_release_lock(pending->lock); |
| 481 | |
| 482 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 483 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 484 | _PyErr_SetString(tstate, PyExc_SystemError, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 485 | "Py_AddPendingCall: cannot add pending calls " |
| 486 | "(Python shutting down)"); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 487 | _PyErr_Print(tstate); |
| 488 | _PyErr_Restore(tstate, exc, val, tb); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 489 | return -1; |
| 490 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 491 | int result = _push_pending_call(pending, func, arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 492 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 493 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 494 | /* signal main loop */ |
| 495 | SIGNAL_PENDING_CALLS(ceval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 499 | int |
| 500 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 501 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 502 | _PyRuntimeState *runtime = &_PyRuntime; |
| 503 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 504 | return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 507 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 508 | handle_signals(_PyRuntimeState *runtime) |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 509 | { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 510 | /* Only handle signals on main thread. PyEval_InitThreads must |
| 511 | * have been called already. |
| 512 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 513 | if (PyThread_get_thread_ident() != runtime->main_thread) { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 514 | return 0; |
| 515 | } |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 516 | /* |
| 517 | * Ensure that the thread isn't currently running some other |
| 518 | * interpreter. |
| 519 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 520 | PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp; |
| 521 | if (interp != runtime->interpreters.main) { |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 522 | return 0; |
| 523 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 524 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 525 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 526 | UNSIGNAL_PENDING_SIGNALS(ceval); |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 527 | if (_PyErr_CheckSignals() < 0) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 528 | SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 529 | return -1; |
| 530 | } |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 535 | make_pending_calls(_PyRuntimeState *runtime) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 536 | { |
Eric Snow | 6a150bc | 2019-06-01 15:39:46 -0600 | [diff] [blame] | 537 | static int busy = 0; |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 538 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 539 | /* only service pending calls on main thread */ |
| 540 | if (PyThread_get_thread_ident() != runtime->main_thread) { |
| 541 | return 0; |
| 542 | } |
| 543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | /* don't perform recursive pending calls */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 545 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 547 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 548 | busy = 1; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 549 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 550 | /* unsignal before starting to call callbacks, so that any callback |
| 551 | added in-between re-signals */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 552 | UNSIGNAL_PENDING_CALLS(ceval); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 553 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* perform a bounded number of calls, in case of recursion */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 556 | struct _pending_calls *pending = &ceval->pending; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 557 | for (int i=0; i<NPENDINGCALLS; i++) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 558 | int (*func)(void *) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | void *arg = NULL; |
| 560 | |
| 561 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 562 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 563 | _pop_pending_call(pending, &func, &arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 564 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 565 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 566 | /* having released the lock, perform the callback */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 567 | if (func == NULL) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 568 | break; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 569 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 570 | res = func(arg); |
| 571 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 572 | goto error; |
| 573 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 575 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 576 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 577 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 578 | |
| 579 | error: |
| 580 | busy = 0; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 581 | SIGNAL_PENDING_CALLS(ceval); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 582 | return res; |
| 583 | } |
| 584 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 585 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 586 | _Py_FinishPendingCalls(_PyRuntimeState *runtime) |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 587 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 588 | assert(PyGILState_Check()); |
| 589 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 590 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 591 | struct _pending_calls *pending = &runtime->ceval.pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 592 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 593 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
| 594 | pending->finishing = 1; |
| 595 | PyThread_release_lock(pending->lock); |
| 596 | |
| 597 | if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) { |
| 598 | return; |
| 599 | } |
| 600 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 601 | if (make_pending_calls(runtime) < 0) { |
| 602 | PyObject *exc, *val, *tb; |
| 603 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 604 | PyErr_BadInternalCall(); |
| 605 | _PyErr_ChainExceptions(exc, val, tb); |
| 606 | _PyErr_Print(tstate); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 610 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 611 | of backward-compatibility. */ |
| 612 | int |
| 613 | Py_MakePendingCalls(void) |
| 614 | { |
| 615 | assert(PyGILState_Check()); |
| 616 | |
| 617 | /* Python signal handler doesn't really queue a callback: it only signals |
| 618 | that a signal was received, see _PyEval_SignalReceived(). */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 619 | _PyRuntimeState *runtime = &_PyRuntime; |
| 620 | int res = handle_signals(runtime); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 621 | if (res != 0) { |
| 622 | return res; |
| 623 | } |
| 624 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 625 | res = make_pending_calls(runtime); |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 626 | if (res != 0) { |
| 627 | return res; |
| 628 | } |
| 629 | |
| 630 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 633 | /* The interpreter's recursion limit */ |
| 634 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 635 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 636 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 637 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 638 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 639 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 640 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 641 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 642 | _PyEval_Initialize(struct _ceval_runtime_state *state) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 643 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 644 | state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 645 | _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 646 | _gil_initialize(&state->gil); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 647 | } |
| 648 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 649 | int |
| 650 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 651 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 652 | return _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 655 | void |
| 656 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 657 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 658 | struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; |
| 659 | ceval->recursion_limit = new_limit; |
| 660 | _Py_CheckRecursionLimit = ceval->recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 663 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 664 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 665 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 666 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 667 | Without USE_STACKCHECK, there is no need for this. */ |
| 668 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 669 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 670 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 671 | _PyRuntimeState *runtime = &_PyRuntime; |
| 672 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 673 | int recursion_limit = runtime->ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 674 | |
| 675 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 676 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | if (PyOS_CheckStack()) { |
| 678 | --tstate->recursion_depth; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 679 | _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | return -1; |
| 681 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 682 | /* Needed for ABI backwards-compatibility (see bpo-31857) */ |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 683 | _Py_CheckRecursionLimit = recursion_limit; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 684 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | if (tstate->recursion_critical) |
| 686 | /* Somebody asked that we don't check for recursion. */ |
| 687 | return 0; |
| 688 | if (tstate->overflowed) { |
| 689 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 690 | /* Overflowing while handling an overflow. Give up. */ |
| 691 | Py_FatalError("Cannot recover from stack overflow."); |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | if (tstate->recursion_depth > recursion_limit) { |
| 696 | --tstate->recursion_depth; |
| 697 | tstate->overflowed = 1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 698 | _PyErr_Format(tstate, PyExc_RecursionError, |
| 699 | "maximum recursion depth exceeded%s", |
| 700 | where); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | return -1; |
| 702 | } |
| 703 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 706 | static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 707 | static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 708 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 709 | #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 710 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 711 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 712 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 713 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | return PyEval_EvalCodeEx(co, |
| 716 | globals, locals, |
| 717 | (PyObject **)NULL, 0, |
| 718 | (PyObject **)NULL, 0, |
| 719 | (PyObject **)NULL, 0, |
| 720 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
| 724 | /* Interpreter main loop */ |
| 725 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 726 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 727 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | /* This is for backward compatibility with extension modules that |
| 729 | used this API; core interpreter code should call |
| 730 | PyEval_EvalFrameEx() */ |
| 731 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 735 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 736 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 737 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 738 | return interp->eval_frame(f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 741 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 742 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 743 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 744 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 746 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 747 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 748 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 749 | int opcode; /* Current opcode */ |
| 750 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 751 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 753 | _PyRuntimeState * const runtime = &_PyRuntime; |
| 754 | PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 755 | struct _ceval_runtime_state * const ceval = &runtime->ceval; |
| 756 | _Py_atomic_int * const eval_breaker = &ceval->eval_breaker; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | is true when the line being executed has changed. The |
| 764 | initial values are such as to make this false the first |
| 765 | time it is tested. */ |
| 766 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 767 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 768 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | PyObject *names; |
| 770 | PyObject *consts; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 771 | _PyOpcache *co_opcache; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 772 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 773 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 774 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 775 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 776 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 777 | /* Computed GOTOs, or |
| 778 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 779 | using gcc's labels-as-values extension |
| 780 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 781 | |
| 782 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 784 | combined with a lookup table of jump addresses. However, since the |
| 785 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 786 | hard time making the right prediction for where to jump next (actually, |
| 787 | it will be always wrong except in the uncommon case of a sequence of |
| 788 | several identical opcodes). |
| 789 | |
| 790 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 791 | indirect jump instruction at the end of each opcode. Since the jump |
| 792 | instruction is at a different address for each opcode, the CPU will make a |
| 793 | separate prediction for each of these instructions, which is equivalent to |
| 794 | predicting the second opcode of each opcode pair. These predictions have |
| 795 | a much better chance to turn out valid, especially in small bytecode loops. |
| 796 | |
| 797 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 799 | and potentially many more instructions (depending on the pipeline width). |
| 800 | A correctly predicted branch, however, is nearly free. |
| 801 | |
| 802 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 803 | faster than the normal "switch" version, depending on the compiler and the |
| 804 | CPU architecture. |
| 805 | |
| 806 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 807 | because it would render the measurements invalid. |
| 808 | |
| 809 | |
| 810 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 811 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 812 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 813 | -fno-crossjumping). |
| 814 | */ |
| 815 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 816 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 817 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 818 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 819 | #endif |
| 820 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 821 | #ifdef HAVE_COMPUTED_GOTOS |
| 822 | #ifndef USE_COMPUTED_GOTOS |
| 823 | #define USE_COMPUTED_GOTOS 1 |
| 824 | #endif |
| 825 | #else |
| 826 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 827 | #error "Computed gotos are not supported on this compiler." |
| 828 | #endif |
| 829 | #undef USE_COMPUTED_GOTOS |
| 830 | #define USE_COMPUTED_GOTOS 0 |
| 831 | #endif |
| 832 | |
| 833 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 834 | /* Import the static jump table */ |
| 835 | #include "opcode_targets.h" |
| 836 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 837 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 838 | op: \ |
| 839 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 840 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 841 | #ifdef LLTRACE |
| 842 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 844 | if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 846 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 847 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | } \ |
| 849 | goto fast_next_opcode; \ |
| 850 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 851 | #else |
| 852 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 854 | if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 856 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 857 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | } \ |
| 859 | goto fast_next_opcode; \ |
| 860 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 861 | #endif |
| 862 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 863 | #define DISPATCH() \ |
| 864 | { \ |
| 865 | if (!_Py_atomic_load_relaxed(eval_breaker)) { \ |
| 866 | FAST_DISPATCH(); \ |
| 867 | } \ |
| 868 | continue; \ |
| 869 | } |
| 870 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 871 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 872 | #define TARGET(op) op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 873 | #define FAST_DISPATCH() goto fast_next_opcode |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 874 | #define DISPATCH() continue |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 875 | #endif |
| 876 | |
| 877 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 878 | /* Tuple access macros */ |
| 879 | |
| 880 | #ifndef Py_DEBUG |
| 881 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 882 | #else |
| 883 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 884 | #endif |
| 885 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 886 | /* Code access macros */ |
| 887 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 888 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 889 | #define INSTR_OFFSET() \ |
| 890 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 891 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 892 | _Py_CODEUNIT word = *next_instr; \ |
| 893 | opcode = _Py_OPCODE(word); \ |
| 894 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 895 | next_instr++; \ |
| 896 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 897 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 898 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 899 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 900 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | Some opcodes tend to come in pairs thus making it possible to |
| 902 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 903 | 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] | 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | Verifying the prediction costs a single high-speed test of a register |
| 906 | variable against a constant. If the pairing was good, then the |
| 907 | processor's own internal branch predication has a high likelihood of |
| 908 | success, resulting in a nearly zero-overhead transition to the |
| 909 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 910 | including its unpredictable switch-case branch. Combined with the |
| 911 | processor's internal branch prediction, a successful PREDICT has the |
| 912 | effect of making the two opcodes run as if they were a single new opcode |
| 913 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 914 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 915 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | predictions turned-on and interpret the results as if some opcodes |
| 917 | had been combined or turn-off predictions so that the opcode frequency |
| 918 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 919 | |
| 920 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | the CPU to record separate branch prediction information for each |
| 922 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 923 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 924 | */ |
| 925 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 926 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 928 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 929 | #define PREDICT(op) \ |
| 930 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 931 | _Py_CODEUNIT word = *next_instr; \ |
| 932 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 933 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 934 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 935 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 936 | goto PRED_##op; \ |
| 937 | } \ |
| 938 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 939 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 940 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 941 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 942 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 943 | /* Stack manipulation macros */ |
| 944 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 945 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 946 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 947 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 948 | #define EMPTY() (STACK_LEVEL() == 0) |
| 949 | #define TOP() (stack_pointer[-1]) |
| 950 | #define SECOND() (stack_pointer[-2]) |
| 951 | #define THIRD() (stack_pointer[-3]) |
| 952 | #define FOURTH() (stack_pointer[-4]) |
| 953 | #define PEEK(n) (stack_pointer[-(n)]) |
| 954 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 955 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 956 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 957 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 958 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 959 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 960 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 961 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 962 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 963 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 965 | lltrace && prtrace(tstate, TOP(), "push")); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 966 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 967 | #define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 968 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 969 | #define STACK_GROW(n) do { \ |
| 970 | assert(n >= 0); \ |
| 971 | (void)(BASIC_STACKADJ(n), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 972 | lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 973 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 974 | } while (0) |
| 975 | #define STACK_SHRINK(n) do { \ |
| 976 | assert(n >= 0); \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 977 | (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 978 | (void)(BASIC_STACKADJ(-n)); \ |
| 979 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 980 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 981 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 982 | prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 983 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 984 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 985 | #define PUSH(v) BASIC_PUSH(v) |
| 986 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 987 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 988 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 989 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 990 | #endif |
| 991 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 992 | /* Local variable macros */ |
| 993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 995 | |
| 996 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 997 | then store the new value; it must copy the old value to a temporary |
| 998 | value, then store the new value, and then DECREF the temporary value. |
| 999 | This is because it is possible that during the DECREF the frame is |
| 1000 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1001 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1003 | GETLOCAL(i) = value; \ |
| 1004 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1005 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1006 | |
| 1007 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1009 | PyObject *v = POP(); \ |
| 1010 | Py_XDECREF(v); \ |
| 1011 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1012 | |
| 1013 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1014 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1016 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1018 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1019 | value = POP(); \ |
| 1020 | Py_XDECREF(value); \ |
| 1021 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1022 | exc_info = tstate->exc_info; \ |
| 1023 | type = exc_info->exc_type; \ |
| 1024 | value = exc_info->exc_value; \ |
| 1025 | traceback = exc_info->exc_traceback; \ |
| 1026 | exc_info->exc_type = POP(); \ |
| 1027 | exc_info->exc_value = POP(); \ |
| 1028 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | Py_XDECREF(type); \ |
| 1030 | Py_XDECREF(value); \ |
| 1031 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1032 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1033 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1034 | /* macros for opcode cache */ |
| 1035 | #define OPCACHE_CHECK() \ |
| 1036 | do { \ |
| 1037 | co_opcache = NULL; \ |
| 1038 | if (co->co_opcache != NULL) { \ |
| 1039 | unsigned char co_opt_offset = \ |
| 1040 | co->co_opcache_map[next_instr - first_instr]; \ |
| 1041 | if (co_opt_offset > 0) { \ |
| 1042 | assert(co_opt_offset <= co->co_opcache_size); \ |
| 1043 | co_opcache = &co->co_opcache[co_opt_offset - 1]; \ |
| 1044 | assert(co_opcache != NULL); \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1045 | } \ |
| 1046 | } \ |
| 1047 | } while (0) |
| 1048 | |
| 1049 | #if OPCACHE_STATS |
| 1050 | |
| 1051 | #define OPCACHE_STAT_GLOBAL_HIT() \ |
| 1052 | do { \ |
| 1053 | if (co->co_opcache != NULL) opcache_global_hits++; \ |
| 1054 | } while (0) |
| 1055 | |
| 1056 | #define OPCACHE_STAT_GLOBAL_MISS() \ |
| 1057 | do { \ |
| 1058 | if (co->co_opcache != NULL) opcache_global_misses++; \ |
| 1059 | } while (0) |
| 1060 | |
| 1061 | #define OPCACHE_STAT_GLOBAL_OPT() \ |
| 1062 | do { \ |
| 1063 | if (co->co_opcache != NULL) opcache_global_opts++; \ |
| 1064 | } while (0) |
| 1065 | |
| 1066 | #else /* OPCACHE_STATS */ |
| 1067 | |
| 1068 | #define OPCACHE_STAT_GLOBAL_HIT() |
| 1069 | #define OPCACHE_STAT_GLOBAL_MISS() |
| 1070 | #define OPCACHE_STAT_GLOBAL_OPT() |
| 1071 | |
| 1072 | #endif |
| 1073 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1074 | /* Start of code */ |
| 1075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | /* push frame */ |
| 1077 | if (Py_EnterRecursiveCall("")) |
| 1078 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | if (tstate->use_tracing) { |
| 1083 | if (tstate->c_tracefunc != NULL) { |
| 1084 | /* tstate->c_tracefunc, if defined, is a |
| 1085 | function that will be called on *every* entry |
| 1086 | to a code block. Its return value, if not |
| 1087 | None, is a function that will be called at |
| 1088 | the start of each executed line of code. |
| 1089 | (Actually, the function must return itself |
| 1090 | in order to continue tracing.) The trace |
| 1091 | functions are called with three arguments: |
| 1092 | a pointer to the current frame, a string |
| 1093 | indicating why the function is called, and |
| 1094 | an argument which depends on the situation. |
| 1095 | The global trace function is also called |
| 1096 | whenever an exception is detected. */ |
| 1097 | if (call_trace_protected(tstate->c_tracefunc, |
| 1098 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1099 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | /* Trace function raised an error */ |
| 1101 | goto exit_eval_frame; |
| 1102 | } |
| 1103 | } |
| 1104 | if (tstate->c_profilefunc != NULL) { |
| 1105 | /* Similar for c_profilefunc, except it needn't |
| 1106 | return itself and isn't called for "line" events */ |
| 1107 | if (call_trace_protected(tstate->c_profilefunc, |
| 1108 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1109 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | /* Profile function raised an error */ |
| 1111 | goto exit_eval_frame; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1115 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1116 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 1117 | dtrace_function_entry(f); |
| 1118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | co = f->f_code; |
| 1120 | names = co->co_names; |
| 1121 | consts = co->co_consts; |
| 1122 | fastlocals = f->f_localsplus; |
| 1123 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1124 | assert(PyBytes_Check(co->co_code)); |
| 1125 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1126 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 1127 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1128 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1129 | /* |
| 1130 | f->f_lasti refers to the index of the last instruction, |
| 1131 | 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] | 1132 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1133 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1134 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1137 | direct succession without updating f->f_lasti. A successful |
| 1138 | prediction effectively links the two codes together as if they |
| 1139 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1140 | the first code in the pair (for instance, GET_ITER followed by |
| 1141 | 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] | 1142 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1144 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1145 | next_instr = first_instr; |
| 1146 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1147 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1148 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1149 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | stack_pointer = f->f_stacktop; |
| 1151 | assert(stack_pointer != NULL); |
| 1152 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1153 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1154 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1155 | if (co->co_opcache_flag < OPCACHE_MIN_RUNS) { |
| 1156 | co->co_opcache_flag++; |
| 1157 | if (co->co_opcache_flag == OPCACHE_MIN_RUNS) { |
| 1158 | if (_PyCode_InitOpcache(co) < 0) { |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | #if OPCACHE_STATS |
| 1162 | opcache_code_objects_extra_mem += |
| 1163 | PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) + |
| 1164 | sizeof(_PyOpcache) * co->co_opcache_size; |
| 1165 | opcache_code_objects++; |
| 1166 | #endif |
| 1167 | } |
| 1168 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1169 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1170 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1171 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1172 | #endif |
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 | if (throwflag) /* support for generator.throw() */ |
| 1175 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1176 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1177 | #ifdef Py_DEBUG |
| 1178 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 1179 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1180 | caller loses its exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1181 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1182 | #endif |
| 1183 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1184 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1187 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1188 | assert(!_PyErr_Occurred(tstate)); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | /* Do periodic things. Doing this every time through |
| 1191 | the loop would add too much overhead, so we do it |
| 1192 | only every Nth instruction. We also do it if |
| 1193 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1194 | event needs attention (e.g. a signal handler or |
| 1195 | async I/O handler); see Py_AddPendingCall() and |
| 1196 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1197 | |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 1198 | if (_Py_atomic_load_relaxed(eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1199 | opcode = _Py_OPCODE(*next_instr); |
| 1200 | if (opcode == SETUP_FINALLY || |
| 1201 | opcode == SETUP_WITH || |
| 1202 | opcode == BEFORE_ASYNC_WITH || |
| 1203 | opcode == YIELD_FROM) { |
| 1204 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 1205 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1206 | - If we're about to enter the 'with:'. It will prevent |
| 1207 | emitting a resource warning in the common idiom |
| 1208 | 'with open(path) as file:'. |
| 1209 | - If we're about to enter the 'async with:'. |
| 1210 | - If we're about to enter the 'try:' of a try/finally (not |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 1211 | *very* useful, but might help in some cases and it's |
| 1212 | traditional) |
| 1213 | - If we're resuming a chain of nested 'yield from' or |
| 1214 | 'await' calls, then each frame is parked with YIELD_FROM |
| 1215 | as its next opcode. If the user hit control-C we want to |
| 1216 | wait until we've reached the innermost frame before |
| 1217 | running the signal handler and raising KeyboardInterrupt |
| 1218 | (see bpo-30039). |
| 1219 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | goto fast_next_opcode; |
| 1221 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1222 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1223 | if (_Py_atomic_load_relaxed(&ceval->signals_pending)) { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1224 | if (handle_signals(runtime) != 0) { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1225 | goto error; |
| 1226 | } |
| 1227 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1228 | if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) { |
| 1229 | if (make_pending_calls(runtime) != 0) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1230 | goto error; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1231 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1233 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1234 | if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | /* Give another thread a chance */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1236 | if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | Py_FatalError("ceval: tstate mix-up"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1238 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1239 | drop_gil(ceval, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | |
| 1241 | /* Other threads may run now */ |
| 1242 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1243 | take_gil(ceval, tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1244 | |
| 1245 | /* Check if we should make a quick exit. */ |
Eric Snow | 396e0a8 | 2019-05-31 21:16:47 -0600 | [diff] [blame] | 1246 | exit_thread_if_finalizing(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1247 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1248 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | Py_FatalError("ceval: orphan tstate"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1250 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | } |
| 1252 | /* Check for asynchronous exceptions. */ |
| 1253 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1254 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | tstate->async_exc = NULL; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1256 | UNSIGNAL_ASYNC_EXC(ceval); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1257 | _PyErr_SetNone(tstate, exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1258 | Py_DECREF(exc); |
| 1259 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | fast_next_opcode: |
| 1264 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1265 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1266 | if (PyDTrace_LINE_ENABLED()) |
| 1267 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1270 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame^] | 1271 | if (_Py_TracingPossible(ceval) && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1272 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1273 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | /* see maybe_call_line_trace |
| 1275 | for expository comments */ |
| 1276 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1279 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1280 | tstate, f, |
| 1281 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | /* Reload possibly changed frame fields */ |
| 1283 | JUMPTO(f->f_lasti); |
| 1284 | if (f->f_stacktop != NULL) { |
| 1285 | stack_pointer = f->f_stacktop; |
| 1286 | f->f_stacktop = NULL; |
| 1287 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1288 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1290 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1294 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1295 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1296 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1297 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1298 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | dxpairs[lastopcode][opcode]++; |
| 1300 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1301 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1303 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1304 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1305 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | if (lltrace) { |
| 1309 | if (HAS_ARG(opcode)) { |
| 1310 | printf("%d: %d, %d\n", |
| 1311 | f->f_lasti, opcode, oparg); |
| 1312 | } |
| 1313 | else { |
| 1314 | printf("%d: %d\n", |
| 1315 | f->f_lasti, opcode); |
| 1316 | } |
| 1317 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1318 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1323 | It is essential that any operation that fails must goto error |
| 1324 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1325 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1326 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1328 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1329 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1330 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1331 | PyObject *value = GETLOCAL(oparg); |
| 1332 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1333 | format_exc_check_arg(tstate, PyExc_UnboundLocalError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1334 | UNBOUNDLOCAL_ERROR_MSG, |
| 1335 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1336 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1338 | Py_INCREF(value); |
| 1339 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1341 | } |
| 1342 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1343 | case TARGET(LOAD_CONST): { |
| 1344 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1345 | PyObject *value = GETITEM(consts, oparg); |
| 1346 | Py_INCREF(value); |
| 1347 | PUSH(value); |
| 1348 | FAST_DISPATCH(); |
| 1349 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1350 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1351 | case TARGET(STORE_FAST): { |
| 1352 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1353 | PyObject *value = POP(); |
| 1354 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1356 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1357 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1358 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1359 | PyObject *value = POP(); |
| 1360 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1362 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1363 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1364 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1365 | PyObject *top = TOP(); |
| 1366 | PyObject *second = SECOND(); |
| 1367 | SET_TOP(second); |
| 1368 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1370 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1371 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1372 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1373 | PyObject *top = TOP(); |
| 1374 | PyObject *second = SECOND(); |
| 1375 | PyObject *third = THIRD(); |
| 1376 | SET_TOP(second); |
| 1377 | SET_SECOND(third); |
| 1378 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1380 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1381 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1382 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1383 | PyObject *top = TOP(); |
| 1384 | PyObject *second = SECOND(); |
| 1385 | PyObject *third = THIRD(); |
| 1386 | PyObject *fourth = FOURTH(); |
| 1387 | SET_TOP(second); |
| 1388 | SET_SECOND(third); |
| 1389 | SET_THIRD(fourth); |
| 1390 | SET_FOURTH(top); |
| 1391 | FAST_DISPATCH(); |
| 1392 | } |
| 1393 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1394 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1395 | PyObject *top = TOP(); |
| 1396 | Py_INCREF(top); |
| 1397 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1399 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1400 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1401 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1402 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1403 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1404 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1405 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1406 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1407 | SET_TOP(top); |
| 1408 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1409 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1410 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1411 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1412 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1413 | PyObject *value = TOP(); |
| 1414 | PyObject *res = PyNumber_Positive(value); |
| 1415 | Py_DECREF(value); |
| 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 | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1422 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1423 | PyObject *value = TOP(); |
| 1424 | PyObject *res = PyNumber_Negative(value); |
| 1425 | Py_DECREF(value); |
| 1426 | SET_TOP(res); |
| 1427 | if (res == NULL) |
| 1428 | goto error; |
| 1429 | DISPATCH(); |
| 1430 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1431 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1432 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1433 | PyObject *value = TOP(); |
| 1434 | int err = PyObject_IsTrue(value); |
| 1435 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | if (err == 0) { |
| 1437 | Py_INCREF(Py_True); |
| 1438 | SET_TOP(Py_True); |
| 1439 | DISPATCH(); |
| 1440 | } |
| 1441 | else if (err > 0) { |
| 1442 | Py_INCREF(Py_False); |
| 1443 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | DISPATCH(); |
| 1445 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1446 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1447 | goto error; |
| 1448 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1449 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1450 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1451 | PyObject *value = TOP(); |
| 1452 | PyObject *res = PyNumber_Invert(value); |
| 1453 | Py_DECREF(value); |
| 1454 | SET_TOP(res); |
| 1455 | if (res == NULL) |
| 1456 | goto error; |
| 1457 | DISPATCH(); |
| 1458 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1459 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1460 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1461 | PyObject *exp = POP(); |
| 1462 | PyObject *base = TOP(); |
| 1463 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1464 | Py_DECREF(base); |
| 1465 | Py_DECREF(exp); |
| 1466 | SET_TOP(res); |
| 1467 | if (res == NULL) |
| 1468 | goto error; |
| 1469 | DISPATCH(); |
| 1470 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1471 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1472 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1473 | PyObject *right = POP(); |
| 1474 | PyObject *left = TOP(); |
| 1475 | PyObject *res = PyNumber_Multiply(left, right); |
| 1476 | Py_DECREF(left); |
| 1477 | Py_DECREF(right); |
| 1478 | SET_TOP(res); |
| 1479 | if (res == NULL) |
| 1480 | goto error; |
| 1481 | DISPATCH(); |
| 1482 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1483 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1484 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1485 | PyObject *right = POP(); |
| 1486 | PyObject *left = TOP(); |
| 1487 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1488 | Py_DECREF(left); |
| 1489 | Py_DECREF(right); |
| 1490 | SET_TOP(res); |
| 1491 | if (res == NULL) |
| 1492 | goto error; |
| 1493 | DISPATCH(); |
| 1494 | } |
| 1495 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1496 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1497 | PyObject *divisor = POP(); |
| 1498 | PyObject *dividend = TOP(); |
| 1499 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1500 | Py_DECREF(dividend); |
| 1501 | Py_DECREF(divisor); |
| 1502 | SET_TOP(quotient); |
| 1503 | if (quotient == NULL) |
| 1504 | goto error; |
| 1505 | DISPATCH(); |
| 1506 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1507 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1508 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1509 | PyObject *divisor = POP(); |
| 1510 | PyObject *dividend = TOP(); |
| 1511 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1512 | Py_DECREF(dividend); |
| 1513 | Py_DECREF(divisor); |
| 1514 | SET_TOP(quotient); |
| 1515 | if (quotient == NULL) |
| 1516 | goto error; |
| 1517 | DISPATCH(); |
| 1518 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1519 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1520 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1521 | PyObject *divisor = POP(); |
| 1522 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1523 | PyObject *res; |
| 1524 | if (PyUnicode_CheckExact(dividend) && ( |
| 1525 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1526 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1527 | // (see issue28598) |
| 1528 | res = PyUnicode_Format(dividend, divisor); |
| 1529 | } else { |
| 1530 | res = PyNumber_Remainder(dividend, divisor); |
| 1531 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1532 | Py_DECREF(divisor); |
| 1533 | Py_DECREF(dividend); |
| 1534 | SET_TOP(res); |
| 1535 | if (res == NULL) |
| 1536 | goto error; |
| 1537 | DISPATCH(); |
| 1538 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1539 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1540 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1541 | PyObject *right = POP(); |
| 1542 | PyObject *left = TOP(); |
| 1543 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1544 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1545 | CPython using bytecode, it is simply worthless. |
| 1546 | See http://bugs.python.org/issue21955 and |
| 1547 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1548 | no patch shown any impact on a realistic benchmark, only a minor |
| 1549 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1550 | if (PyUnicode_CheckExact(left) && |
| 1551 | PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1552 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1553 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1554 | } |
| 1555 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1556 | sum = PyNumber_Add(left, right); |
| 1557 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1558 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1559 | Py_DECREF(right); |
| 1560 | SET_TOP(sum); |
| 1561 | if (sum == NULL) |
| 1562 | goto error; |
| 1563 | DISPATCH(); |
| 1564 | } |
| 1565 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1566 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1567 | PyObject *right = POP(); |
| 1568 | PyObject *left = TOP(); |
| 1569 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1570 | Py_DECREF(right); |
| 1571 | Py_DECREF(left); |
| 1572 | SET_TOP(diff); |
| 1573 | if (diff == NULL) |
| 1574 | goto error; |
| 1575 | DISPATCH(); |
| 1576 | } |
| 1577 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1578 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1579 | PyObject *sub = POP(); |
| 1580 | PyObject *container = TOP(); |
| 1581 | PyObject *res = PyObject_GetItem(container, sub); |
| 1582 | Py_DECREF(container); |
| 1583 | Py_DECREF(sub); |
| 1584 | SET_TOP(res); |
| 1585 | if (res == NULL) |
| 1586 | goto error; |
| 1587 | DISPATCH(); |
| 1588 | } |
| 1589 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1590 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1591 | PyObject *right = POP(); |
| 1592 | PyObject *left = TOP(); |
| 1593 | PyObject *res = PyNumber_Lshift(left, right); |
| 1594 | Py_DECREF(left); |
| 1595 | Py_DECREF(right); |
| 1596 | SET_TOP(res); |
| 1597 | if (res == NULL) |
| 1598 | goto error; |
| 1599 | DISPATCH(); |
| 1600 | } |
| 1601 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1602 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1603 | PyObject *right = POP(); |
| 1604 | PyObject *left = TOP(); |
| 1605 | PyObject *res = PyNumber_Rshift(left, right); |
| 1606 | Py_DECREF(left); |
| 1607 | Py_DECREF(right); |
| 1608 | SET_TOP(res); |
| 1609 | if (res == NULL) |
| 1610 | goto error; |
| 1611 | DISPATCH(); |
| 1612 | } |
| 1613 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1614 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1615 | PyObject *right = POP(); |
| 1616 | PyObject *left = TOP(); |
| 1617 | PyObject *res = PyNumber_And(left, right); |
| 1618 | Py_DECREF(left); |
| 1619 | Py_DECREF(right); |
| 1620 | SET_TOP(res); |
| 1621 | if (res == NULL) |
| 1622 | goto error; |
| 1623 | DISPATCH(); |
| 1624 | } |
| 1625 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1626 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1627 | PyObject *right = POP(); |
| 1628 | PyObject *left = TOP(); |
| 1629 | PyObject *res = PyNumber_Xor(left, right); |
| 1630 | Py_DECREF(left); |
| 1631 | Py_DECREF(right); |
| 1632 | SET_TOP(res); |
| 1633 | if (res == NULL) |
| 1634 | goto error; |
| 1635 | DISPATCH(); |
| 1636 | } |
| 1637 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1638 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1639 | PyObject *right = POP(); |
| 1640 | PyObject *left = TOP(); |
| 1641 | PyObject *res = PyNumber_Or(left, right); |
| 1642 | Py_DECREF(left); |
| 1643 | Py_DECREF(right); |
| 1644 | SET_TOP(res); |
| 1645 | if (res == NULL) |
| 1646 | goto error; |
| 1647 | DISPATCH(); |
| 1648 | } |
| 1649 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1650 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1651 | PyObject *v = POP(); |
| 1652 | PyObject *list = PEEK(oparg); |
| 1653 | int err; |
| 1654 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1656 | if (err != 0) |
| 1657 | goto error; |
| 1658 | PREDICT(JUMP_ABSOLUTE); |
| 1659 | DISPATCH(); |
| 1660 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1661 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1662 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1663 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1664 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1665 | int err; |
| 1666 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1668 | if (err != 0) |
| 1669 | goto error; |
| 1670 | PREDICT(JUMP_ABSOLUTE); |
| 1671 | DISPATCH(); |
| 1672 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1673 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1674 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1675 | PyObject *exp = POP(); |
| 1676 | PyObject *base = TOP(); |
| 1677 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1678 | Py_DECREF(base); |
| 1679 | Py_DECREF(exp); |
| 1680 | SET_TOP(res); |
| 1681 | if (res == NULL) |
| 1682 | goto error; |
| 1683 | DISPATCH(); |
| 1684 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1685 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1686 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1687 | PyObject *right = POP(); |
| 1688 | PyObject *left = TOP(); |
| 1689 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1690 | Py_DECREF(left); |
| 1691 | Py_DECREF(right); |
| 1692 | SET_TOP(res); |
| 1693 | if (res == NULL) |
| 1694 | goto error; |
| 1695 | DISPATCH(); |
| 1696 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1697 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1698 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1699 | PyObject *right = POP(); |
| 1700 | PyObject *left = TOP(); |
| 1701 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1702 | Py_DECREF(left); |
| 1703 | Py_DECREF(right); |
| 1704 | SET_TOP(res); |
| 1705 | if (res == NULL) |
| 1706 | goto error; |
| 1707 | DISPATCH(); |
| 1708 | } |
| 1709 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1710 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1711 | PyObject *divisor = POP(); |
| 1712 | PyObject *dividend = TOP(); |
| 1713 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1714 | Py_DECREF(dividend); |
| 1715 | Py_DECREF(divisor); |
| 1716 | SET_TOP(quotient); |
| 1717 | if (quotient == NULL) |
| 1718 | goto error; |
| 1719 | DISPATCH(); |
| 1720 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1721 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1722 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1723 | PyObject *divisor = POP(); |
| 1724 | PyObject *dividend = TOP(); |
| 1725 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1726 | Py_DECREF(dividend); |
| 1727 | Py_DECREF(divisor); |
| 1728 | SET_TOP(quotient); |
| 1729 | if (quotient == NULL) |
| 1730 | goto error; |
| 1731 | DISPATCH(); |
| 1732 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1733 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1734 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1735 | PyObject *right = POP(); |
| 1736 | PyObject *left = TOP(); |
| 1737 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1738 | Py_DECREF(left); |
| 1739 | Py_DECREF(right); |
| 1740 | SET_TOP(mod); |
| 1741 | if (mod == NULL) |
| 1742 | goto error; |
| 1743 | DISPATCH(); |
| 1744 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1745 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1746 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1747 | PyObject *right = POP(); |
| 1748 | PyObject *left = TOP(); |
| 1749 | PyObject *sum; |
| 1750 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1751 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1752 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1753 | } |
| 1754 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1755 | sum = PyNumber_InPlaceAdd(left, right); |
| 1756 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1757 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1758 | Py_DECREF(right); |
| 1759 | SET_TOP(sum); |
| 1760 | if (sum == NULL) |
| 1761 | goto error; |
| 1762 | DISPATCH(); |
| 1763 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1764 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1765 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1766 | PyObject *right = POP(); |
| 1767 | PyObject *left = TOP(); |
| 1768 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1769 | Py_DECREF(left); |
| 1770 | Py_DECREF(right); |
| 1771 | SET_TOP(diff); |
| 1772 | if (diff == NULL) |
| 1773 | goto error; |
| 1774 | DISPATCH(); |
| 1775 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1776 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1777 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1778 | PyObject *right = POP(); |
| 1779 | PyObject *left = TOP(); |
| 1780 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1781 | Py_DECREF(left); |
| 1782 | Py_DECREF(right); |
| 1783 | SET_TOP(res); |
| 1784 | if (res == NULL) |
| 1785 | goto error; |
| 1786 | DISPATCH(); |
| 1787 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1788 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1789 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1790 | PyObject *right = POP(); |
| 1791 | PyObject *left = TOP(); |
| 1792 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1793 | Py_DECREF(left); |
| 1794 | Py_DECREF(right); |
| 1795 | SET_TOP(res); |
| 1796 | if (res == NULL) |
| 1797 | goto error; |
| 1798 | DISPATCH(); |
| 1799 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1800 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1801 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1802 | PyObject *right = POP(); |
| 1803 | PyObject *left = TOP(); |
| 1804 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1805 | Py_DECREF(left); |
| 1806 | Py_DECREF(right); |
| 1807 | SET_TOP(res); |
| 1808 | if (res == NULL) |
| 1809 | goto error; |
| 1810 | DISPATCH(); |
| 1811 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1812 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1813 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1814 | PyObject *right = POP(); |
| 1815 | PyObject *left = TOP(); |
| 1816 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1817 | Py_DECREF(left); |
| 1818 | Py_DECREF(right); |
| 1819 | SET_TOP(res); |
| 1820 | if (res == NULL) |
| 1821 | goto error; |
| 1822 | DISPATCH(); |
| 1823 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1824 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1825 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1826 | PyObject *right = POP(); |
| 1827 | PyObject *left = TOP(); |
| 1828 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1829 | Py_DECREF(left); |
| 1830 | Py_DECREF(right); |
| 1831 | SET_TOP(res); |
| 1832 | if (res == NULL) |
| 1833 | goto error; |
| 1834 | DISPATCH(); |
| 1835 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1836 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1837 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1838 | PyObject *sub = TOP(); |
| 1839 | PyObject *container = SECOND(); |
| 1840 | PyObject *v = THIRD(); |
| 1841 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1842 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1843 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1844 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1846 | Py_DECREF(container); |
| 1847 | Py_DECREF(sub); |
| 1848 | if (err != 0) |
| 1849 | goto error; |
| 1850 | DISPATCH(); |
| 1851 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1852 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1853 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1854 | PyObject *sub = TOP(); |
| 1855 | PyObject *container = SECOND(); |
| 1856 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1857 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1858 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1859 | err = PyObject_DelItem(container, sub); |
| 1860 | Py_DECREF(container); |
| 1861 | Py_DECREF(sub); |
| 1862 | if (err != 0) |
| 1863 | goto error; |
| 1864 | DISPATCH(); |
| 1865 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1866 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1867 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1868 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1869 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1870 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1871 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1872 | if (hook == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1873 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 1874 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1875 | Py_DECREF(value); |
| 1876 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1878 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1879 | Py_DECREF(value); |
| 1880 | if (res == NULL) |
| 1881 | goto error; |
| 1882 | Py_DECREF(res); |
| 1883 | DISPATCH(); |
| 1884 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1885 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1886 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1887 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | switch (oparg) { |
| 1889 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1890 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1891 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1893 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1894 | /* fall through */ |
| 1895 | case 0: |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1896 | if (do_raise(tstate, exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1897 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1898 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1901 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 1902 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | break; |
| 1904 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1905 | goto error; |
| 1906 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1907 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1908 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1910 | assert(f->f_iblock == 0); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 1911 | goto exit_returning; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1912 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1913 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1914 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1915 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1916 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1917 | PyObject *obj = TOP(); |
| 1918 | PyTypeObject *type = Py_TYPE(obj); |
| 1919 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1920 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1921 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1922 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1923 | |
| 1924 | if (getter != NULL) { |
| 1925 | iter = (*getter)(obj); |
| 1926 | Py_DECREF(obj); |
| 1927 | if (iter == NULL) { |
| 1928 | SET_TOP(NULL); |
| 1929 | goto error; |
| 1930 | } |
| 1931 | } |
| 1932 | else { |
| 1933 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1934 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1935 | "'async for' requires an object with " |
| 1936 | "__aiter__ method, got %.100s", |
| 1937 | type->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1938 | Py_DECREF(obj); |
| 1939 | goto error; |
| 1940 | } |
| 1941 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1942 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 1943 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1944 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1945 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1946 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1947 | "'async for' received an object from __aiter__ " |
| 1948 | "that does not implement __anext__: %.100s", |
| 1949 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1950 | Py_DECREF(iter); |
| 1951 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1952 | } |
| 1953 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1954 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1955 | DISPATCH(); |
| 1956 | } |
| 1957 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1958 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1959 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1960 | PyObject *next_iter = NULL; |
| 1961 | PyObject *awaitable = NULL; |
| 1962 | PyObject *aiter = TOP(); |
| 1963 | PyTypeObject *type = Py_TYPE(aiter); |
| 1964 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1965 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1966 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1967 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1968 | goto error; |
| 1969 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1970 | } else { |
| 1971 | if (type->tp_as_async != NULL){ |
| 1972 | getter = type->tp_as_async->am_anext; |
| 1973 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1974 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1975 | if (getter != NULL) { |
| 1976 | next_iter = (*getter)(aiter); |
| 1977 | if (next_iter == NULL) { |
| 1978 | goto error; |
| 1979 | } |
| 1980 | } |
| 1981 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1982 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1983 | "'async for' requires an iterator with " |
| 1984 | "__anext__ method, got %.100s", |
| 1985 | type->tp_name); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1986 | goto error; |
| 1987 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1988 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1989 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1990 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1991 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1992 | PyExc_TypeError, |
| 1993 | "'async for' received an invalid object " |
| 1994 | "from __anext__: %.100s", |
| 1995 | Py_TYPE(next_iter)->tp_name); |
| 1996 | |
| 1997 | Py_DECREF(next_iter); |
| 1998 | goto error; |
| 1999 | } else { |
| 2000 | Py_DECREF(next_iter); |
| 2001 | } |
| 2002 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2003 | |
| 2004 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2005 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2006 | DISPATCH(); |
| 2007 | } |
| 2008 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2009 | case TARGET(GET_AWAITABLE): { |
| 2010 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2011 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2012 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2013 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2014 | if (iter == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2015 | format_awaitable_error(tstate, Py_TYPE(iterable), |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2016 | _Py_OPCODE(next_instr[-2])); |
| 2017 | } |
| 2018 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2019 | Py_DECREF(iterable); |
| 2020 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2021 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2022 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2023 | if (yf != NULL) { |
| 2024 | /* `iter` is a coroutine object that is being |
| 2025 | awaited, `yf` is a pointer to the current awaitable |
| 2026 | being awaited on. */ |
| 2027 | Py_DECREF(yf); |
| 2028 | Py_CLEAR(iter); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2029 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2030 | "coroutine is being awaited already"); |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2031 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2032 | } |
| 2033 | } |
| 2034 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2035 | SET_TOP(iter); /* Even if it's NULL */ |
| 2036 | |
| 2037 | if (iter == NULL) { |
| 2038 | goto error; |
| 2039 | } |
| 2040 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2041 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2042 | DISPATCH(); |
| 2043 | } |
| 2044 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2045 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2046 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2047 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2048 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2049 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2050 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2051 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2052 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2053 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2054 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2055 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2056 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2057 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2058 | Py_DECREF(v); |
| 2059 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2060 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2061 | if (tstate->c_tracefunc != NULL |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2062 | && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2063 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2064 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2065 | if (err < 0) |
| 2066 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2067 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2068 | SET_TOP(val); |
| 2069 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2070 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2071 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2072 | f->f_stacktop = stack_pointer; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2073 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 2074 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 2075 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 2076 | goto exit_yielding; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2077 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2078 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2079 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2081 | |
| 2082 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2083 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2084 | Py_DECREF(retval); |
| 2085 | if (w == NULL) { |
| 2086 | retval = NULL; |
| 2087 | goto error; |
| 2088 | } |
| 2089 | retval = w; |
| 2090 | } |
| 2091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | f->f_stacktop = stack_pointer; |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 2093 | goto exit_yielding; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2094 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2095 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2096 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2097 | PyObject *type, *value, *traceback; |
| 2098 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2099 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2100 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2101 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2102 | "popped block is not an except handler"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2103 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2105 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 2106 | STACK_LEVEL() <= (b)->b_level + 4); |
| 2107 | exc_info = tstate->exc_info; |
| 2108 | type = exc_info->exc_type; |
| 2109 | value = exc_info->exc_value; |
| 2110 | traceback = exc_info->exc_traceback; |
| 2111 | exc_info->exc_type = POP(); |
| 2112 | exc_info->exc_value = POP(); |
| 2113 | exc_info->exc_traceback = POP(); |
| 2114 | Py_XDECREF(type); |
| 2115 | Py_XDECREF(value); |
| 2116 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2118 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2119 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2120 | case TARGET(POP_BLOCK): { |
| 2121 | PREDICTED(POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2122 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2124 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2125 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2126 | case TARGET(POP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2127 | /* If oparg is 0 at the top of the stack are 1 or 6 values: |
| 2128 | Either: |
| 2129 | - TOP = NULL or an integer |
| 2130 | or: |
| 2131 | - (TOP, SECOND, THIRD) = exc_info() |
| 2132 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 2133 | |
| 2134 | If oparg is 1 the value for 'return' was additionally pushed |
| 2135 | at the top of the stack. |
| 2136 | */ |
| 2137 | PyObject *res = NULL; |
| 2138 | if (oparg) { |
| 2139 | res = POP(); |
| 2140 | } |
| 2141 | PyObject *exc = POP(); |
| 2142 | if (exc == NULL || PyLong_CheckExact(exc)) { |
| 2143 | Py_XDECREF(exc); |
| 2144 | } |
| 2145 | else { |
| 2146 | Py_DECREF(exc); |
| 2147 | Py_DECREF(POP()); |
| 2148 | Py_DECREF(POP()); |
| 2149 | |
| 2150 | PyObject *type, *value, *traceback; |
| 2151 | _PyErr_StackItem *exc_info; |
| 2152 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2153 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2154 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2155 | "popped block is not an except handler"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2156 | Py_XDECREF(res); |
| 2157 | goto error; |
| 2158 | } |
| 2159 | assert(STACK_LEVEL() == (b)->b_level + 3); |
| 2160 | exc_info = tstate->exc_info; |
| 2161 | type = exc_info->exc_type; |
| 2162 | value = exc_info->exc_value; |
| 2163 | traceback = exc_info->exc_traceback; |
| 2164 | exc_info->exc_type = POP(); |
| 2165 | exc_info->exc_value = POP(); |
| 2166 | exc_info->exc_traceback = POP(); |
| 2167 | Py_XDECREF(type); |
| 2168 | Py_XDECREF(value); |
| 2169 | Py_XDECREF(traceback); |
| 2170 | } |
| 2171 | if (oparg) { |
| 2172 | PUSH(res); |
| 2173 | } |
| 2174 | DISPATCH(); |
| 2175 | } |
| 2176 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2177 | case TARGET(CALL_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2178 | PyObject *ret = PyLong_FromLong(INSTR_OFFSET()); |
| 2179 | if (ret == NULL) { |
| 2180 | goto error; |
| 2181 | } |
| 2182 | PUSH(ret); |
| 2183 | JUMPBY(oparg); |
| 2184 | FAST_DISPATCH(); |
| 2185 | } |
| 2186 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2187 | case TARGET(BEGIN_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2188 | /* Push NULL onto the stack for using it in END_FINALLY, |
| 2189 | POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH. |
| 2190 | */ |
| 2191 | PUSH(NULL); |
| 2192 | FAST_DISPATCH(); |
| 2193 | } |
| 2194 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2195 | case TARGET(END_FINALLY): { |
| 2196 | PREDICTED(END_FINALLY); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2197 | /* At the top of the stack are 1 or 6 values: |
| 2198 | Either: |
| 2199 | - TOP = NULL or an integer |
| 2200 | or: |
| 2201 | - (TOP, SECOND, THIRD) = exc_info() |
| 2202 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 2203 | */ |
| 2204 | PyObject *exc = POP(); |
| 2205 | if (exc == NULL) { |
| 2206 | FAST_DISPATCH(); |
| 2207 | } |
| 2208 | else if (PyLong_CheckExact(exc)) { |
| 2209 | int ret = _PyLong_AsInt(exc); |
| 2210 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2211 | if (ret == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2212 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2214 | JUMPTO(ret); |
| 2215 | FAST_DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2217 | else { |
| 2218 | assert(PyExceptionClass_Check(exc)); |
| 2219 | PyObject *val = POP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2220 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2221 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2222 | goto exception_unwind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2224 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2225 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2226 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2227 | PyObject *exc = POP(); |
| 2228 | assert(PyExceptionClass_Check(exc)); |
| 2229 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2230 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2231 | assert(b->b_type == EXCEPT_HANDLER); |
| 2232 | Py_DECREF(exc); |
| 2233 | UNWIND_EXCEPT_HANDLER(b); |
| 2234 | Py_DECREF(POP()); |
| 2235 | JUMPBY(oparg); |
| 2236 | FAST_DISPATCH(); |
| 2237 | } |
| 2238 | else { |
| 2239 | PyObject *val = POP(); |
| 2240 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2241 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2242 | goto exception_unwind; |
| 2243 | } |
| 2244 | } |
| 2245 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2246 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2247 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2248 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2249 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2250 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2251 | bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2252 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2253 | if (!_PyErr_Occurred(tstate)) { |
| 2254 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2255 | "__build_class__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2256 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2257 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2258 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2259 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2260 | } |
| 2261 | else { |
| 2262 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2263 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2264 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2265 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2266 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2267 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
| 2268 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2269 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2270 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2271 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2274 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2275 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2276 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2277 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2278 | PyObject *name = GETITEM(names, oparg); |
| 2279 | PyObject *v = POP(); |
| 2280 | PyObject *ns = f->f_locals; |
| 2281 | int err; |
| 2282 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2283 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2284 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2286 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2288 | if (PyDict_CheckExact(ns)) |
| 2289 | err = PyDict_SetItem(ns, name, v); |
| 2290 | else |
| 2291 | err = PyObject_SetItem(ns, name, v); |
| 2292 | Py_DECREF(v); |
| 2293 | if (err != 0) |
| 2294 | goto error; |
| 2295 | DISPATCH(); |
| 2296 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2297 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2298 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2299 | PyObject *name = GETITEM(names, oparg); |
| 2300 | PyObject *ns = f->f_locals; |
| 2301 | int err; |
| 2302 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2303 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2304 | "no locals when deleting %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2305 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2307 | err = PyObject_DelItem(ns, name); |
| 2308 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2309 | format_exc_check_arg(tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2310 | NAME_ERROR_MSG, |
| 2311 | name); |
| 2312 | goto error; |
| 2313 | } |
| 2314 | DISPATCH(); |
| 2315 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2316 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2317 | case TARGET(UNPACK_SEQUENCE): { |
| 2318 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2319 | PyObject *seq = POP(), *item, **items; |
| 2320 | if (PyTuple_CheckExact(seq) && |
| 2321 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2322 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2323 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2324 | item = items[oparg]; |
| 2325 | Py_INCREF(item); |
| 2326 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2328 | } else if (PyList_CheckExact(seq) && |
| 2329 | PyList_GET_SIZE(seq) == oparg) { |
| 2330 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2332 | item = items[oparg]; |
| 2333 | Py_INCREF(item); |
| 2334 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2336 | } else if (unpack_iterable(tstate, seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2338 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | } else { |
| 2340 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2341 | Py_DECREF(seq); |
| 2342 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2344 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2345 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2347 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2348 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2349 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2350 | PyObject *seq = POP(); |
| 2351 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2352 | if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2353 | stack_pointer + totalargs)) { |
| 2354 | stack_pointer += totalargs; |
| 2355 | } else { |
| 2356 | Py_DECREF(seq); |
| 2357 | goto error; |
| 2358 | } |
| 2359 | Py_DECREF(seq); |
| 2360 | DISPATCH(); |
| 2361 | } |
| 2362 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2363 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2364 | PyObject *name = GETITEM(names, oparg); |
| 2365 | PyObject *owner = TOP(); |
| 2366 | PyObject *v = SECOND(); |
| 2367 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2368 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2369 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2371 | Py_DECREF(owner); |
| 2372 | if (err != 0) |
| 2373 | goto error; |
| 2374 | DISPATCH(); |
| 2375 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2376 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2377 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2378 | PyObject *name = GETITEM(names, oparg); |
| 2379 | PyObject *owner = POP(); |
| 2380 | int err; |
| 2381 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2382 | Py_DECREF(owner); |
| 2383 | if (err != 0) |
| 2384 | goto error; |
| 2385 | DISPATCH(); |
| 2386 | } |
| 2387 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2388 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2389 | PyObject *name = GETITEM(names, oparg); |
| 2390 | PyObject *v = POP(); |
| 2391 | int err; |
| 2392 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2394 | if (err != 0) |
| 2395 | goto error; |
| 2396 | DISPATCH(); |
| 2397 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2398 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2399 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2400 | PyObject *name = GETITEM(names, oparg); |
| 2401 | int err; |
| 2402 | err = PyDict_DelItem(f->f_globals, name); |
| 2403 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2404 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
| 2405 | format_exc_check_arg(tstate, PyExc_NameError, |
| 2406 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2407 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2408 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2409 | } |
| 2410 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2411 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2412 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2413 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2414 | PyObject *name = GETITEM(names, oparg); |
| 2415 | PyObject *locals = f->f_locals; |
| 2416 | PyObject *v; |
| 2417 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2418 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2419 | "no locals when loading %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2420 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2422 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2423 | v = PyDict_GetItemWithError(locals, name); |
| 2424 | if (v != NULL) { |
| 2425 | Py_INCREF(v); |
| 2426 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2427 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2428 | goto error; |
| 2429 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2430 | } |
| 2431 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2432 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2433 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2434 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2435 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2436 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | } |
| 2438 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2439 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2440 | v = PyDict_GetItemWithError(f->f_globals, name); |
| 2441 | if (v != NULL) { |
| 2442 | Py_INCREF(v); |
| 2443 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2444 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2445 | goto error; |
| 2446 | } |
| 2447 | else { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2448 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2449 | v = PyDict_GetItemWithError(f->f_builtins, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2450 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2451 | if (!_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2452 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2453 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2454 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2455 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2456 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2457 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2458 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2459 | } |
| 2460 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2461 | v = PyObject_GetItem(f->f_builtins, name); |
| 2462 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2463 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2464 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2465 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2466 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2467 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2468 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2469 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2470 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2473 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2475 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2476 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2477 | case TARGET(LOAD_GLOBAL): { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2478 | PyObject *name; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2479 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2480 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2481 | && PyDict_CheckExact(f->f_builtins)) |
| 2482 | { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2483 | OPCACHE_CHECK(); |
| 2484 | if (co_opcache != NULL && co_opcache->optimized > 0) { |
| 2485 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2486 | |
| 2487 | if (lg->globals_ver == |
| 2488 | ((PyDictObject *)f->f_globals)->ma_version_tag |
| 2489 | && lg->builtins_ver == |
| 2490 | ((PyDictObject *)f->f_builtins)->ma_version_tag) |
| 2491 | { |
| 2492 | PyObject *ptr = lg->ptr; |
| 2493 | OPCACHE_STAT_GLOBAL_HIT(); |
| 2494 | assert(ptr != NULL); |
| 2495 | Py_INCREF(ptr); |
| 2496 | PUSH(ptr); |
| 2497 | DISPATCH(); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2502 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2503 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2504 | name); |
| 2505 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2506 | if (!_PyErr_OCCURRED()) { |
| 2507 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2508 | * an exception if the key doesn't exist */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2509 | format_exc_check_arg(tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2510 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2511 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2512 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2513 | } |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2514 | |
| 2515 | if (co_opcache != NULL) { |
| 2516 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2517 | |
| 2518 | if (co_opcache->optimized == 0) { |
| 2519 | /* Wasn't optimized before. */ |
| 2520 | OPCACHE_STAT_GLOBAL_OPT(); |
| 2521 | } else { |
| 2522 | OPCACHE_STAT_GLOBAL_MISS(); |
| 2523 | } |
| 2524 | |
| 2525 | co_opcache->optimized = 1; |
| 2526 | lg->globals_ver = |
| 2527 | ((PyDictObject *)f->f_globals)->ma_version_tag; |
| 2528 | lg->builtins_ver = |
| 2529 | ((PyDictObject *)f->f_builtins)->ma_version_tag; |
| 2530 | lg->ptr = v; /* borrowed */ |
| 2531 | } |
| 2532 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2533 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2534 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2535 | else { |
| 2536 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2537 | |
| 2538 | /* namespace 1: globals */ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2539 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2540 | v = PyObject_GetItem(f->f_globals, name); |
| 2541 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2542 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2543 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2544 | } |
| 2545 | _PyErr_Clear(tstate); |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2546 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2547 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2548 | v = PyObject_GetItem(f->f_builtins, name); |
| 2549 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2550 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2551 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2552 | tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2553 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2554 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2555 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2559 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2561 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2562 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2563 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2564 | PyObject *v = GETLOCAL(oparg); |
| 2565 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | SETLOCAL(oparg, NULL); |
| 2567 | DISPATCH(); |
| 2568 | } |
| 2569 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2570 | tstate, PyExc_UnboundLocalError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2571 | UNBOUNDLOCAL_ERROR_MSG, |
| 2572 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2573 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2574 | goto error; |
| 2575 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2576 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2577 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2578 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2579 | PyObject *oldobj = PyCell_GET(cell); |
| 2580 | if (oldobj != NULL) { |
| 2581 | PyCell_SET(cell, NULL); |
| 2582 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2583 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2584 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2585 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2586 | goto error; |
| 2587 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2588 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2589 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2590 | PyObject *cell = freevars[oparg]; |
| 2591 | Py_INCREF(cell); |
| 2592 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2594 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2595 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2596 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2597 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2598 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2599 | assert(locals); |
| 2600 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2601 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2602 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2603 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2604 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2605 | value = PyDict_GetItemWithError(locals, name); |
| 2606 | if (value != NULL) { |
| 2607 | Py_INCREF(value); |
| 2608 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2609 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2610 | goto error; |
| 2611 | } |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2612 | } |
| 2613 | else { |
| 2614 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2615 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2616 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2617 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2618 | } |
| 2619 | _PyErr_Clear(tstate); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | if (!value) { |
| 2623 | PyObject *cell = freevars[oparg]; |
| 2624 | value = PyCell_GET(cell); |
| 2625 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2626 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2627 | goto error; |
| 2628 | } |
| 2629 | Py_INCREF(value); |
| 2630 | } |
| 2631 | PUSH(value); |
| 2632 | DISPATCH(); |
| 2633 | } |
| 2634 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2635 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2636 | PyObject *cell = freevars[oparg]; |
| 2637 | PyObject *value = PyCell_GET(cell); |
| 2638 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2639 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2640 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2642 | Py_INCREF(value); |
| 2643 | PUSH(value); |
| 2644 | DISPATCH(); |
| 2645 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2646 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2647 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2648 | PyObject *v = POP(); |
| 2649 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2650 | PyObject *oldobj = PyCell_GET(cell); |
| 2651 | PyCell_SET(cell, v); |
| 2652 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2653 | DISPATCH(); |
| 2654 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2655 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2656 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2657 | PyObject *str; |
| 2658 | PyObject *empty = PyUnicode_New(0, 0); |
| 2659 | if (empty == NULL) { |
| 2660 | goto error; |
| 2661 | } |
| 2662 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2663 | Py_DECREF(empty); |
| 2664 | if (str == NULL) |
| 2665 | goto error; |
| 2666 | while (--oparg >= 0) { |
| 2667 | PyObject *item = POP(); |
| 2668 | Py_DECREF(item); |
| 2669 | } |
| 2670 | PUSH(str); |
| 2671 | DISPATCH(); |
| 2672 | } |
| 2673 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2674 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2675 | PyObject *tup = PyTuple_New(oparg); |
| 2676 | if (tup == NULL) |
| 2677 | goto error; |
| 2678 | while (--oparg >= 0) { |
| 2679 | PyObject *item = POP(); |
| 2680 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2681 | } |
| 2682 | PUSH(tup); |
| 2683 | DISPATCH(); |
| 2684 | } |
| 2685 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2686 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2687 | PyObject *list = PyList_New(oparg); |
| 2688 | if (list == NULL) |
| 2689 | goto error; |
| 2690 | while (--oparg >= 0) { |
| 2691 | PyObject *item = POP(); |
| 2692 | PyList_SET_ITEM(list, oparg, item); |
| 2693 | } |
| 2694 | PUSH(list); |
| 2695 | DISPATCH(); |
| 2696 | } |
| 2697 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2698 | case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL): |
| 2699 | case TARGET(BUILD_TUPLE_UNPACK): |
| 2700 | case TARGET(BUILD_LIST_UNPACK): { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2701 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2702 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2703 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2704 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2705 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2706 | if (sum == NULL) |
| 2707 | goto error; |
| 2708 | |
| 2709 | for (i = oparg; i > 0; i--) { |
| 2710 | PyObject *none_val; |
| 2711 | |
| 2712 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2713 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2714 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2715 | _PyErr_ExceptionMatches(tstate, PyExc_TypeError)) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2716 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2717 | check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i)); |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2718 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2719 | Py_DECREF(sum); |
| 2720 | goto error; |
| 2721 | } |
| 2722 | Py_DECREF(none_val); |
| 2723 | } |
| 2724 | |
| 2725 | if (convert_to_tuple) { |
| 2726 | return_value = PyList_AsTuple(sum); |
| 2727 | Py_DECREF(sum); |
| 2728 | if (return_value == NULL) |
| 2729 | goto error; |
| 2730 | } |
| 2731 | else { |
| 2732 | return_value = sum; |
| 2733 | } |
| 2734 | |
| 2735 | while (oparg--) |
| 2736 | Py_DECREF(POP()); |
| 2737 | PUSH(return_value); |
| 2738 | DISPATCH(); |
| 2739 | } |
| 2740 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2741 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2742 | PyObject *set = PySet_New(NULL); |
| 2743 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2744 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2745 | if (set == NULL) |
| 2746 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2747 | for (i = oparg; i > 0; i--) { |
| 2748 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2749 | if (err == 0) |
| 2750 | err = PySet_Add(set, item); |
| 2751 | Py_DECREF(item); |
| 2752 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2753 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2754 | if (err != 0) { |
| 2755 | Py_DECREF(set); |
| 2756 | goto error; |
| 2757 | } |
| 2758 | PUSH(set); |
| 2759 | DISPATCH(); |
| 2760 | } |
| 2761 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2762 | case TARGET(BUILD_SET_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2763 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2764 | PyObject *sum = PySet_New(NULL); |
| 2765 | if (sum == NULL) |
| 2766 | goto error; |
| 2767 | |
| 2768 | for (i = oparg; i > 0; i--) { |
| 2769 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2770 | Py_DECREF(sum); |
| 2771 | goto error; |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | while (oparg--) |
| 2776 | Py_DECREF(POP()); |
| 2777 | PUSH(sum); |
| 2778 | DISPATCH(); |
| 2779 | } |
| 2780 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2781 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2782 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2783 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2784 | if (map == NULL) |
| 2785 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2786 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2787 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2788 | PyObject *key = PEEK(2*i); |
| 2789 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2790 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2791 | if (err != 0) { |
| 2792 | Py_DECREF(map); |
| 2793 | goto error; |
| 2794 | } |
| 2795 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2796 | |
| 2797 | while (oparg--) { |
| 2798 | Py_DECREF(POP()); |
| 2799 | Py_DECREF(POP()); |
| 2800 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2801 | PUSH(map); |
| 2802 | DISPATCH(); |
| 2803 | } |
| 2804 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2805 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2806 | _Py_IDENTIFIER(__annotations__); |
| 2807 | int err; |
| 2808 | PyObject *ann_dict; |
| 2809 | if (f->f_locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2810 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2811 | "no locals found when setting up annotations"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2812 | goto error; |
| 2813 | } |
| 2814 | /* check if __annotations__ in locals()... */ |
| 2815 | if (PyDict_CheckExact(f->f_locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2816 | ann_dict = _PyDict_GetItemIdWithError(f->f_locals, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2817 | &PyId___annotations__); |
| 2818 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2819 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2820 | goto error; |
| 2821 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2822 | /* ...if not, create a new one */ |
| 2823 | ann_dict = PyDict_New(); |
| 2824 | if (ann_dict == NULL) { |
| 2825 | goto error; |
| 2826 | } |
| 2827 | err = _PyDict_SetItemId(f->f_locals, |
| 2828 | &PyId___annotations__, ann_dict); |
| 2829 | Py_DECREF(ann_dict); |
| 2830 | if (err != 0) { |
| 2831 | goto error; |
| 2832 | } |
| 2833 | } |
| 2834 | } |
| 2835 | else { |
| 2836 | /* do the same if locals() is not a dict */ |
| 2837 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2838 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2839 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2840 | } |
| 2841 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2842 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2843 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2844 | goto error; |
| 2845 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2846 | _PyErr_Clear(tstate); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2847 | ann_dict = PyDict_New(); |
| 2848 | if (ann_dict == NULL) { |
| 2849 | goto error; |
| 2850 | } |
| 2851 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2852 | Py_DECREF(ann_dict); |
| 2853 | if (err != 0) { |
| 2854 | goto error; |
| 2855 | } |
| 2856 | } |
| 2857 | else { |
| 2858 | Py_DECREF(ann_dict); |
| 2859 | } |
| 2860 | } |
| 2861 | DISPATCH(); |
| 2862 | } |
| 2863 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2864 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2865 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2866 | PyObject *map; |
| 2867 | PyObject *keys = TOP(); |
| 2868 | if (!PyTuple_CheckExact(keys) || |
| 2869 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2870 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2871 | "bad BUILD_CONST_KEY_MAP keys argument"); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2872 | goto error; |
| 2873 | } |
| 2874 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2875 | if (map == NULL) { |
| 2876 | goto error; |
| 2877 | } |
| 2878 | for (i = oparg; i > 0; i--) { |
| 2879 | int err; |
| 2880 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2881 | PyObject *value = PEEK(i + 1); |
| 2882 | err = PyDict_SetItem(map, key, value); |
| 2883 | if (err != 0) { |
| 2884 | Py_DECREF(map); |
| 2885 | goto error; |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | Py_DECREF(POP()); |
| 2890 | while (oparg--) { |
| 2891 | Py_DECREF(POP()); |
| 2892 | } |
| 2893 | PUSH(map); |
| 2894 | DISPATCH(); |
| 2895 | } |
| 2896 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2897 | case TARGET(BUILD_MAP_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2898 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2899 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2900 | if (sum == NULL) |
| 2901 | goto error; |
| 2902 | |
| 2903 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2904 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2905 | if (PyDict_Update(sum, arg) < 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2906 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 2907 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2908 | "'%.200s' object is not a mapping", |
| 2909 | arg->ob_type->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2910 | } |
| 2911 | Py_DECREF(sum); |
| 2912 | goto error; |
| 2913 | } |
| 2914 | } |
| 2915 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2916 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2917 | Py_DECREF(POP()); |
| 2918 | PUSH(sum); |
| 2919 | DISPATCH(); |
| 2920 | } |
| 2921 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2922 | case TARGET(BUILD_MAP_UNPACK_WITH_CALL): { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2923 | Py_ssize_t i; |
| 2924 | PyObject *sum = PyDict_New(); |
| 2925 | if (sum == NULL) |
| 2926 | goto error; |
| 2927 | |
| 2928 | for (i = oparg; i > 0; i--) { |
| 2929 | PyObject *arg = PEEK(i); |
| 2930 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2931 | Py_DECREF(sum); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2932 | format_kwargs_error(tstate, PEEK(2 + oparg), arg); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2933 | goto error; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | while (oparg--) |
| 2938 | Py_DECREF(POP()); |
| 2939 | PUSH(sum); |
| 2940 | DISPATCH(); |
| 2941 | } |
| 2942 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2943 | case TARGET(MAP_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2944 | PyObject *key = TOP(); |
| 2945 | PyObject *value = SECOND(); |
| 2946 | PyObject *map; |
| 2947 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2948 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2949 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2950 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2951 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2952 | Py_DECREF(value); |
| 2953 | Py_DECREF(key); |
| 2954 | if (err != 0) |
| 2955 | goto error; |
| 2956 | PREDICT(JUMP_ABSOLUTE); |
| 2957 | DISPATCH(); |
| 2958 | } |
| 2959 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2960 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2961 | PyObject *name = GETITEM(names, oparg); |
| 2962 | PyObject *owner = TOP(); |
| 2963 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2964 | Py_DECREF(owner); |
| 2965 | SET_TOP(res); |
| 2966 | if (res == NULL) |
| 2967 | goto error; |
| 2968 | DISPATCH(); |
| 2969 | } |
| 2970 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2971 | case TARGET(COMPARE_OP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2972 | PyObject *right = POP(); |
| 2973 | PyObject *left = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2974 | PyObject *res = cmp_outcome(tstate, oparg, left, right); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2975 | Py_DECREF(left); |
| 2976 | Py_DECREF(right); |
| 2977 | SET_TOP(res); |
| 2978 | if (res == NULL) |
| 2979 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | PREDICT(POP_JUMP_IF_FALSE); |
| 2981 | PREDICT(POP_JUMP_IF_TRUE); |
| 2982 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2983 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2984 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2985 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2986 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2987 | PyObject *fromlist = POP(); |
| 2988 | PyObject *level = TOP(); |
| 2989 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2990 | res = import_name(tstate, f, name, fromlist, level); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2991 | Py_DECREF(level); |
| 2992 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2993 | SET_TOP(res); |
| 2994 | if (res == NULL) |
| 2995 | goto error; |
| 2996 | DISPATCH(); |
| 2997 | } |
| 2998 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2999 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3000 | PyObject *from = POP(), *locals; |
| 3001 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3002 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 3003 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3004 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3005 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3006 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3007 | locals = f->f_locals; |
| 3008 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3009 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3010 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3011 | Py_DECREF(from); |
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 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3014 | err = import_all_from(tstate, locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3016 | Py_DECREF(from); |
| 3017 | if (err != 0) |
| 3018 | goto error; |
| 3019 | DISPATCH(); |
| 3020 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3021 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3022 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3023 | PyObject *name = GETITEM(names, oparg); |
| 3024 | PyObject *from = TOP(); |
| 3025 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3026 | res = import_from(tstate, from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3027 | PUSH(res); |
| 3028 | if (res == NULL) |
| 3029 | goto error; |
| 3030 | DISPATCH(); |
| 3031 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3032 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3033 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | JUMPBY(oparg); |
| 3035 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3036 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3037 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3038 | case TARGET(POP_JUMP_IF_FALSE): { |
| 3039 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3040 | PyObject *cond = POP(); |
| 3041 | int err; |
| 3042 | if (cond == Py_True) { |
| 3043 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3044 | FAST_DISPATCH(); |
| 3045 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3046 | if (cond == Py_False) { |
| 3047 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | JUMPTO(oparg); |
| 3049 | FAST_DISPATCH(); |
| 3050 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3051 | err = PyObject_IsTrue(cond); |
| 3052 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 3054 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 | else if (err == 0) |
| 3056 | JUMPTO(oparg); |
| 3057 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3058 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3060 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3061 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3062 | case TARGET(POP_JUMP_IF_TRUE): { |
| 3063 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3064 | PyObject *cond = POP(); |
| 3065 | int err; |
| 3066 | if (cond == Py_False) { |
| 3067 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | FAST_DISPATCH(); |
| 3069 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3070 | if (cond == Py_True) { |
| 3071 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | JUMPTO(oparg); |
| 3073 | FAST_DISPATCH(); |
| 3074 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3075 | err = PyObject_IsTrue(cond); |
| 3076 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | JUMPTO(oparg); |
| 3079 | } |
| 3080 | else if (err == 0) |
| 3081 | ; |
| 3082 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3083 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3085 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3086 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3087 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3088 | PyObject *cond = TOP(); |
| 3089 | int err; |
| 3090 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3091 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3092 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | FAST_DISPATCH(); |
| 3094 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3095 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | JUMPTO(oparg); |
| 3097 | FAST_DISPATCH(); |
| 3098 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3099 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3101 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3102 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | } |
| 3104 | else if (err == 0) |
| 3105 | JUMPTO(oparg); |
| 3106 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3107 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3109 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3110 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3111 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3112 | PyObject *cond = TOP(); |
| 3113 | int err; |
| 3114 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3115 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3116 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | FAST_DISPATCH(); |
| 3118 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3119 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | JUMPTO(oparg); |
| 3121 | FAST_DISPATCH(); |
| 3122 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3123 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | JUMPTO(oparg); |
| 3126 | } |
| 3127 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3128 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3129 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3130 | } |
| 3131 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3132 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3134 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3135 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3136 | case TARGET(JUMP_ABSOLUTE): { |
| 3137 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3139 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 3141 | the per-loop checks for signals. By default, this should be turned-off |
| 3142 | because it prevents detection of a control-break in tight loops like |
| 3143 | "while 1: pass". Compile with this option turned-on when you need |
| 3144 | the speed-up and do not need break checking inside tight loops (ones |
| 3145 | that contain only instructions ending with FAST_DISPATCH). |
| 3146 | */ |
| 3147 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3148 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3150 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3151 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3152 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3153 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3155 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3156 | PyObject *iter = PyObject_GetIter(iterable); |
| 3157 | Py_DECREF(iterable); |
| 3158 | SET_TOP(iter); |
| 3159 | if (iter == NULL) |
| 3160 | goto error; |
| 3161 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3162 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3163 | DISPATCH(); |
| 3164 | } |
| 3165 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3166 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3167 | /* before: [obj]; after [getiter(obj)] */ |
| 3168 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3169 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3170 | if (PyCoro_CheckExact(iterable)) { |
| 3171 | /* `iterable` is a coroutine */ |
| 3172 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3173 | /* and it is used in a 'yield from' expression of a |
| 3174 | regular generator. */ |
| 3175 | Py_DECREF(iterable); |
| 3176 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3177 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3178 | "cannot 'yield from' a coroutine object " |
| 3179 | "in a non-coroutine generator"); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3180 | goto error; |
| 3181 | } |
| 3182 | } |
| 3183 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3184 | /* `iterable` is not a generator. */ |
| 3185 | iter = PyObject_GetIter(iterable); |
| 3186 | Py_DECREF(iterable); |
| 3187 | SET_TOP(iter); |
| 3188 | if (iter == NULL) |
| 3189 | goto error; |
| 3190 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3191 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3192 | DISPATCH(); |
| 3193 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3194 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3195 | case TARGET(FOR_ITER): { |
| 3196 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3198 | PyObject *iter = TOP(); |
| 3199 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3200 | if (next != NULL) { |
| 3201 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | PREDICT(STORE_FAST); |
| 3203 | PREDICT(UNPACK_SEQUENCE); |
| 3204 | DISPATCH(); |
| 3205 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3206 | if (_PyErr_Occurred(tstate)) { |
| 3207 | if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3208 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3209 | } |
| 3210 | else if (tstate->c_tracefunc != NULL) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3211 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3212 | } |
| 3213 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | } |
| 3215 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3216 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3217 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3219 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3221 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3222 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3223 | case TARGET(SETUP_FINALLY): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | /* NOTE: If you add any new block-setup opcodes that |
| 3225 | are not try/except/finally handlers, you may need |
| 3226 | to update the PyGen_NeedsFinalizing() function. |
| 3227 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3228 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3229 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | STACK_LEVEL()); |
| 3231 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3232 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3233 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3234 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3235 | _Py_IDENTIFIER(__aexit__); |
| 3236 | _Py_IDENTIFIER(__aenter__); |
| 3237 | |
| 3238 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3239 | PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3240 | *enter; |
| 3241 | PyObject *res; |
| 3242 | if (exit == NULL) |
| 3243 | goto error; |
| 3244 | SET_TOP(exit); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3245 | enter = special_lookup(tstate, mgr, &PyId___aenter__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3246 | Py_DECREF(mgr); |
| 3247 | if (enter == NULL) |
| 3248 | goto error; |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3249 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3250 | Py_DECREF(enter); |
| 3251 | if (res == NULL) |
| 3252 | goto error; |
| 3253 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3254 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3255 | DISPATCH(); |
| 3256 | } |
| 3257 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3258 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3259 | PyObject *res = POP(); |
| 3260 | /* Setup the finally block before pushing the result |
| 3261 | of __aenter__ on the stack. */ |
| 3262 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3263 | STACK_LEVEL()); |
| 3264 | PUSH(res); |
| 3265 | DISPATCH(); |
| 3266 | } |
| 3267 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3268 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3269 | _Py_IDENTIFIER(__exit__); |
| 3270 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3271 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3272 | PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3273 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3274 | if (enter == NULL) { |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3275 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3276 | } |
| 3277 | PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3278 | if (exit == NULL) { |
| 3279 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3280 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3281 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3282 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3283 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3284 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3285 | Py_DECREF(enter); |
| 3286 | if (res == NULL) |
| 3287 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | /* Setup the finally block before pushing the result |
| 3289 | of __enter__ on the stack. */ |
| 3290 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3291 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3292 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3293 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | DISPATCH(); |
| 3295 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3296 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3297 | case TARGET(WITH_CLEANUP_START): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3298 | /* At the top of the stack are 1 or 6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3299 | how/why we entered the finally clause: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3300 | - TOP = NULL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | - (TOP, SECOND, THIRD) = exc_info() |
| 3302 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3303 | Below them is EXIT, the context.__exit__ or context.__aexit__ |
| 3304 | bound method. |
| 3305 | In the first case, we must call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | EXIT(None, None, None) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3307 | otherwise we must call |
| 3308 | EXIT(TOP, SECOND, THIRD) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3309 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3310 | In the first case, we remove EXIT from the |
| 3311 | stack, leaving TOP, and push TOP on the stack. |
| 3312 | Otherwise we shift the bottom 3 values of the |
| 3313 | stack down, replace the empty spot with NULL, and push |
| 3314 | None on the stack. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3315 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3316 | Finally we push the result of the call. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3318 | PyObject *stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3320 | PyObject *exc, *val, *tb, *res; |
| 3321 | |
| 3322 | val = tb = Py_None; |
| 3323 | exc = TOP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3324 | if (exc == NULL) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3325 | STACK_SHRINK(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3327 | SET_TOP(exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3328 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | } |
| 3330 | else { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3331 | assert(PyExceptionClass_Check(exc)); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3332 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3334 | val = SECOND(); |
| 3335 | tb = THIRD(); |
| 3336 | tp2 = FOURTH(); |
| 3337 | exc2 = PEEK(5); |
| 3338 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3339 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3340 | SET_VALUE(7, tb2); |
| 3341 | SET_VALUE(6, exc2); |
| 3342 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3344 | SET_FOURTH(NULL); |
| 3345 | /* We just shifted the stack down, so we have |
| 3346 | to tell the except handler block that the |
| 3347 | values are lower than it expects. */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3348 | assert(f->f_iblock > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3350 | assert(block->b_type == EXCEPT_HANDLER); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3351 | assert(block->b_level > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | block->b_level--; |
| 3353 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3354 | |
| 3355 | stack[0] = exc; |
| 3356 | stack[1] = val; |
| 3357 | stack[2] = tb; |
| 3358 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3360 | if (res == NULL) |
| 3361 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3362 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3363 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3364 | PUSH(exc); |
| 3365 | PUSH(res); |
| 3366 | PREDICT(WITH_CLEANUP_FINISH); |
| 3367 | DISPATCH(); |
| 3368 | } |
| 3369 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3370 | case TARGET(WITH_CLEANUP_FINISH): { |
| 3371 | PREDICTED(WITH_CLEANUP_FINISH); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3372 | /* TOP = the result of calling the context.__exit__ bound method |
| 3373 | SECOND = either None or exception type |
| 3374 | |
| 3375 | If SECOND is None below is NULL or the return address, |
| 3376 | otherwise below are 7 values representing an exception. |
| 3377 | */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3378 | PyObject *res = POP(); |
| 3379 | PyObject *exc = POP(); |
| 3380 | int err; |
| 3381 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3382 | if (exc != Py_None) |
| 3383 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3384 | else |
| 3385 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3386 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3387 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3388 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3391 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | else if (err > 0) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3393 | /* There was an exception and a True return. |
| 3394 | * We must manually unwind the EXCEPT_HANDLER block |
| 3395 | * which was created when the exception was caught, |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 3396 | * otherwise the stack will be in an inconsistent state. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3397 | */ |
| 3398 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 3399 | assert(b->b_type == EXCEPT_HANDLER); |
| 3400 | UNWIND_EXCEPT_HANDLER(b); |
| 3401 | PUSH(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | } |
| 3403 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3404 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3406 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3407 | case TARGET(LOAD_METHOD): { |
Andrey | b021ba5 | 2019-04-29 14:33:26 +1000 | [diff] [blame] | 3408 | /* Designed to work in tandem with CALL_METHOD. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3409 | PyObject *name = GETITEM(names, oparg); |
| 3410 | PyObject *obj = TOP(); |
| 3411 | PyObject *meth = NULL; |
| 3412 | |
| 3413 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3414 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3415 | if (meth == NULL) { |
| 3416 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3417 | goto error; |
| 3418 | } |
| 3419 | |
| 3420 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3421 | /* We can bypass temporary bound method object. |
| 3422 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3423 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3424 | meth | self | arg1 | ... | argN |
| 3425 | */ |
| 3426 | SET_TOP(meth); |
| 3427 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3428 | } |
| 3429 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3430 | /* meth is not an unbound method (but a regular attr, or |
| 3431 | something was returned by a descriptor protocol). Set |
| 3432 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3433 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3434 | |
| 3435 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3436 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3437 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3438 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3439 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3440 | } |
| 3441 | DISPATCH(); |
| 3442 | } |
| 3443 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3444 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3445 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3446 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3447 | |
| 3448 | sp = stack_pointer; |
| 3449 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3450 | meth = PEEK(oparg + 2); |
| 3451 | if (meth == NULL) { |
| 3452 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3453 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3454 | |
| 3455 | Stack layout: |
| 3456 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3457 | ... | NULL | callable | arg1 | ... | argN |
| 3458 | ^- TOP() |
| 3459 | ^- (-oparg) |
| 3460 | ^- (-oparg-1) |
| 3461 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3462 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3463 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3464 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3465 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3466 | res = call_function(tstate, &sp, oparg, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3467 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3468 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3469 | } |
| 3470 | else { |
| 3471 | /* This is a method call. Stack layout: |
| 3472 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3473 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3474 | ^- TOP() |
| 3475 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3476 | ^- (-oparg-1) |
| 3477 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3478 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3479 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3480 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3481 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3482 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3483 | res = call_function(tstate, &sp, oparg + 1, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3484 | stack_pointer = sp; |
| 3485 | } |
| 3486 | |
| 3487 | PUSH(res); |
| 3488 | if (res == NULL) |
| 3489 | goto error; |
| 3490 | DISPATCH(); |
| 3491 | } |
| 3492 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3493 | case TARGET(CALL_FUNCTION): { |
| 3494 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3495 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3497 | res = call_function(tstate, &sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3499 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3500 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3501 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3502 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3503 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3505 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3506 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3507 | PyObject **sp, *res, *names; |
| 3508 | |
| 3509 | names = POP(); |
| 3510 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3512 | res = call_function(tstate, &sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3514 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3515 | Py_DECREF(names); |
| 3516 | |
| 3517 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3518 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3519 | } |
| 3520 | DISPATCH(); |
| 3521 | } |
| 3522 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3523 | case TARGET(CALL_FUNCTION_EX): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3524 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3525 | if (oparg & 0x01) { |
| 3526 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3527 | if (!PyDict_CheckExact(kwargs)) { |
| 3528 | PyObject *d = PyDict_New(); |
| 3529 | if (d == NULL) |
| 3530 | goto error; |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 3531 | if (_PyDict_MergeEx(d, kwargs, 2) < 0) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3532 | Py_DECREF(d); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3533 | format_kwargs_error(tstate, SECOND(), kwargs); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3534 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3535 | goto error; |
| 3536 | } |
| 3537 | Py_DECREF(kwargs); |
| 3538 | kwargs = d; |
| 3539 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3540 | assert(PyDict_CheckExact(kwargs)); |
| 3541 | } |
| 3542 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3543 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3544 | if (!PyTuple_CheckExact(callargs)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3545 | if (check_args_iterable(tstate, func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3546 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3547 | goto error; |
| 3548 | } |
| 3549 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3550 | if (callargs == NULL) { |
| 3551 | goto error; |
| 3552 | } |
| 3553 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3554 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3555 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3556 | result = do_call_core(tstate, func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3557 | Py_DECREF(func); |
| 3558 | Py_DECREF(callargs); |
| 3559 | Py_XDECREF(kwargs); |
| 3560 | |
| 3561 | SET_TOP(result); |
| 3562 | if (result == NULL) { |
| 3563 | goto error; |
| 3564 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3565 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3567 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3568 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3569 | PyObject *qualname = POP(); |
| 3570 | PyObject *codeobj = POP(); |
| 3571 | PyFunctionObject *func = (PyFunctionObject *) |
| 3572 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3573 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3574 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3575 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3576 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3577 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3579 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3580 | if (oparg & 0x08) { |
| 3581 | assert(PyTuple_CheckExact(TOP())); |
| 3582 | func ->func_closure = POP(); |
| 3583 | } |
| 3584 | if (oparg & 0x04) { |
| 3585 | assert(PyDict_CheckExact(TOP())); |
| 3586 | func->func_annotations = POP(); |
| 3587 | } |
| 3588 | if (oparg & 0x02) { |
| 3589 | assert(PyDict_CheckExact(TOP())); |
| 3590 | func->func_kwdefaults = POP(); |
| 3591 | } |
| 3592 | if (oparg & 0x01) { |
| 3593 | assert(PyTuple_CheckExact(TOP())); |
| 3594 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3596 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3597 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3598 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3600 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3601 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3602 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3604 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3605 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3606 | step = NULL; |
| 3607 | stop = POP(); |
| 3608 | start = TOP(); |
| 3609 | slice = PySlice_New(start, stop, step); |
| 3610 | Py_DECREF(start); |
| 3611 | Py_DECREF(stop); |
| 3612 | Py_XDECREF(step); |
| 3613 | SET_TOP(slice); |
| 3614 | if (slice == NULL) |
| 3615 | goto error; |
| 3616 | DISPATCH(); |
| 3617 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3618 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3619 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3620 | /* Handles f-string value formatting. */ |
| 3621 | PyObject *result; |
| 3622 | PyObject *fmt_spec; |
| 3623 | PyObject *value; |
| 3624 | PyObject *(*conv_fn)(PyObject *); |
| 3625 | int which_conversion = oparg & FVC_MASK; |
| 3626 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3627 | |
| 3628 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3629 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3630 | |
| 3631 | /* See if any conversion is specified. */ |
| 3632 | switch (which_conversion) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3633 | case FVC_NONE: conv_fn = NULL; break; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3634 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3635 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3636 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3637 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3638 | _PyErr_Format(tstate, PyExc_SystemError, |
| 3639 | "unexpected conversion flag %d", |
| 3640 | which_conversion); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3641 | goto error; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | /* If there's a conversion function, call it and replace |
| 3645 | value with that result. Otherwise, just use value, |
| 3646 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3647 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3648 | result = conv_fn(value); |
| 3649 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3650 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3651 | Py_XDECREF(fmt_spec); |
| 3652 | goto error; |
| 3653 | } |
| 3654 | value = result; |
| 3655 | } |
| 3656 | |
| 3657 | /* If value is a unicode object, and there's no fmt_spec, |
| 3658 | then we know the result of format(value) is value |
| 3659 | itself. In that case, skip calling format(). I plan to |
| 3660 | move this optimization in to PyObject_Format() |
| 3661 | itself. */ |
| 3662 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3663 | /* Do nothing, just transfer ownership to result. */ |
| 3664 | result = value; |
| 3665 | } else { |
| 3666 | /* Actually call format(). */ |
| 3667 | result = PyObject_Format(value, fmt_spec); |
| 3668 | Py_DECREF(value); |
| 3669 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3670 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3671 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3672 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3673 | } |
| 3674 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3675 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3676 | DISPATCH(); |
| 3677 | } |
| 3678 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3679 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3680 | int oldoparg = oparg; |
| 3681 | NEXTOPARG(); |
| 3682 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3684 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3685 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3686 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3687 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3688 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3689 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | default: |
| 3691 | fprintf(stderr, |
| 3692 | "XXX lineno: %d, opcode: %d\n", |
| 3693 | PyFrame_GetLineNumber(f), |
| 3694 | opcode); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3695 | _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3696 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3699 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3700 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3701 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3702 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3703 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3704 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3705 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3706 | #ifdef NDEBUG |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3707 | if (!_PyErr_Occurred(tstate)) { |
| 3708 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3709 | "error return without exception set"); |
| 3710 | } |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3711 | #else |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3712 | assert(_PyErr_Occurred(tstate)); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3713 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3714 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3715 | /* Log traceback info. */ |
| 3716 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3717 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3718 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3719 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3720 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3721 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3722 | exception_unwind: |
| 3723 | /* Unwind stacks if an exception occurred */ |
| 3724 | while (f->f_iblock > 0) { |
| 3725 | /* Pop the current block. */ |
| 3726 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3728 | if (b->b_type == EXCEPT_HANDLER) { |
| 3729 | UNWIND_EXCEPT_HANDLER(b); |
| 3730 | continue; |
| 3731 | } |
| 3732 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3733 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | PyObject *exc, *val, *tb; |
| 3735 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3736 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3737 | /* Beware, this invalidates all b->b_* fields */ |
| 3738 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3739 | PUSH(exc_info->exc_traceback); |
| 3740 | PUSH(exc_info->exc_value); |
| 3741 | if (exc_info->exc_type != NULL) { |
| 3742 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | } |
| 3744 | else { |
| 3745 | Py_INCREF(Py_None); |
| 3746 | PUSH(Py_None); |
| 3747 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3748 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | /* Make the raw exception data |
| 3750 | available to the handler, |
| 3751 | so a program can emulate the |
| 3752 | Python main loop. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3753 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3754 | if (tb != NULL) |
| 3755 | PyException_SetTraceback(val, tb); |
| 3756 | else |
| 3757 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3759 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3761 | exc_info->exc_value = val; |
| 3762 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3763 | if (tb == NULL) |
| 3764 | tb = Py_None; |
| 3765 | Py_INCREF(tb); |
| 3766 | PUSH(tb); |
| 3767 | PUSH(val); |
| 3768 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3769 | JUMPTO(handler); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3770 | /* Resume normal execution */ |
| 3771 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3774 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3775 | /* End the loop as we still have an error */ |
| 3776 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3777 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3778 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3779 | assert(retval == NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3780 | assert(_PyErr_Occurred(tstate)); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3781 | |
| 3782 | exit_returning: |
| 3783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3784 | /* Pop remaining stack entries. */ |
| 3785 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3786 | PyObject *o = POP(); |
| 3787 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3789 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3790 | exit_yielding: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3792 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3793 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3794 | tstate, f, PyTrace_RETURN, retval)) { |
| 3795 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3799 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 3800 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3801 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3802 | } |
| 3803 | } |
| 3804 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3806 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3807 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3808 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3809 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3811 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3812 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3813 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3814 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3817 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3818 | format_missing(PyThreadState *tstate, const char *kind, |
| 3819 | PyCodeObject *co, PyObject *names) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3820 | { |
| 3821 | int err; |
| 3822 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3823 | PyObject *name_str, *comma, *tail, *tmp; |
| 3824 | |
| 3825 | assert(PyList_CheckExact(names)); |
| 3826 | assert(len >= 1); |
| 3827 | /* Deal with the joys of natural language. */ |
| 3828 | switch (len) { |
| 3829 | case 1: |
| 3830 | name_str = PyList_GET_ITEM(names, 0); |
| 3831 | Py_INCREF(name_str); |
| 3832 | break; |
| 3833 | case 2: |
| 3834 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3835 | PyList_GET_ITEM(names, len - 2), |
| 3836 | PyList_GET_ITEM(names, len - 1)); |
| 3837 | break; |
| 3838 | default: |
| 3839 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3840 | PyList_GET_ITEM(names, len - 2), |
| 3841 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3842 | if (tail == NULL) |
| 3843 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3844 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3845 | fail, but we can't be too careful. */ |
| 3846 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3847 | if (err == -1) { |
| 3848 | Py_DECREF(tail); |
| 3849 | return; |
| 3850 | } |
| 3851 | /* Stitch everything up into a nice comma-separated list. */ |
| 3852 | comma = PyUnicode_FromString(", "); |
| 3853 | if (comma == NULL) { |
| 3854 | Py_DECREF(tail); |
| 3855 | return; |
| 3856 | } |
| 3857 | tmp = PyUnicode_Join(comma, names); |
| 3858 | Py_DECREF(comma); |
| 3859 | if (tmp == NULL) { |
| 3860 | Py_DECREF(tail); |
| 3861 | return; |
| 3862 | } |
| 3863 | name_str = PyUnicode_Concat(tmp, tail); |
| 3864 | Py_DECREF(tmp); |
| 3865 | Py_DECREF(tail); |
| 3866 | break; |
| 3867 | } |
| 3868 | if (name_str == NULL) |
| 3869 | return; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3870 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3871 | "%U() missing %i required %s argument%s: %U", |
| 3872 | co->co_name, |
| 3873 | len, |
| 3874 | kind, |
| 3875 | len == 1 ? "" : "s", |
| 3876 | name_str); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3877 | Py_DECREF(name_str); |
| 3878 | } |
| 3879 | |
| 3880 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3881 | missing_arguments(PyThreadState *tstate, PyCodeObject *co, |
| 3882 | Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3883 | PyObject **fastlocals) |
| 3884 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3885 | Py_ssize_t i, j = 0; |
| 3886 | Py_ssize_t start, end; |
| 3887 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3888 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3889 | PyObject *missing_names; |
| 3890 | |
| 3891 | /* Compute the names of the arguments that are missing. */ |
| 3892 | missing_names = PyList_New(missing); |
| 3893 | if (missing_names == NULL) |
| 3894 | return; |
| 3895 | if (positional) { |
| 3896 | start = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3897 | end = co->co_argcount - defcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3898 | } |
| 3899 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3900 | start = co->co_argcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3901 | end = start + co->co_kwonlyargcount; |
| 3902 | } |
| 3903 | for (i = start; i < end; i++) { |
| 3904 | if (GETLOCAL(i) == NULL) { |
| 3905 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3906 | PyObject *name = PyObject_Repr(raw); |
| 3907 | if (name == NULL) { |
| 3908 | Py_DECREF(missing_names); |
| 3909 | return; |
| 3910 | } |
| 3911 | PyList_SET_ITEM(missing_names, j++, name); |
| 3912 | } |
| 3913 | } |
| 3914 | assert(j == missing); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3915 | format_missing(tstate, kind, co, missing_names); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3916 | Py_DECREF(missing_names); |
| 3917 | } |
| 3918 | |
| 3919 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3920 | too_many_positional(PyThreadState *tstate, PyCodeObject *co, |
| 3921 | Py_ssize_t given, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3922 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3923 | { |
| 3924 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3925 | Py_ssize_t kwonly_given = 0; |
| 3926 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3927 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3928 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3929 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3930 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3931 | /* Count missing keyword-only args. */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3932 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3933 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3934 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3935 | } |
| 3936 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3937 | if (defcount) { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3938 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3939 | plural = 1; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3940 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3941 | } |
| 3942 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3943 | plural = (co_argcount != 1); |
| 3944 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3945 | } |
| 3946 | if (sig == NULL) |
| 3947 | return; |
| 3948 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3949 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3950 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3951 | given != 1 ? "s" : "", |
| 3952 | kwonly_given, |
| 3953 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3954 | if (kwonly_sig == NULL) { |
| 3955 | Py_DECREF(sig); |
| 3956 | return; |
| 3957 | } |
| 3958 | } |
| 3959 | else { |
| 3960 | /* This will not fail. */ |
| 3961 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3962 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3963 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3964 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3965 | "%U() takes %U positional argument%s but %zd%U %s given", |
| 3966 | co->co_name, |
| 3967 | sig, |
| 3968 | plural ? "s" : "", |
| 3969 | given, |
| 3970 | kwonly_sig, |
| 3971 | given == 1 && !kwonly_given ? "was" : "were"); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3972 | Py_DECREF(sig); |
| 3973 | Py_DECREF(kwonly_sig); |
| 3974 | } |
| 3975 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 3976 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3977 | positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co, |
| 3978 | Py_ssize_t kwcount, PyObject* const* kwnames) |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 3979 | { |
| 3980 | int posonly_conflicts = 0; |
| 3981 | PyObject* posonly_names = PyList_New(0); |
| 3982 | |
| 3983 | for(int k=0; k < co->co_posonlyargcount; k++){ |
| 3984 | PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k); |
| 3985 | |
| 3986 | for (int k2=0; k2<kwcount; k2++){ |
| 3987 | /* Compare the pointers first and fallback to PyObject_RichCompareBool*/ |
| 3988 | PyObject* kwname = kwnames[k2]; |
| 3989 | if (kwname == posonly_name){ |
| 3990 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 3991 | goto fail; |
| 3992 | } |
| 3993 | posonly_conflicts++; |
| 3994 | continue; |
| 3995 | } |
| 3996 | |
| 3997 | int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ); |
| 3998 | |
| 3999 | if ( cmp > 0) { |
| 4000 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 4001 | goto fail; |
| 4002 | } |
| 4003 | posonly_conflicts++; |
| 4004 | } else if (cmp < 0) { |
| 4005 | goto fail; |
| 4006 | } |
| 4007 | |
| 4008 | } |
| 4009 | } |
| 4010 | if (posonly_conflicts) { |
| 4011 | PyObject* comma = PyUnicode_FromString(", "); |
| 4012 | if (comma == NULL) { |
| 4013 | goto fail; |
| 4014 | } |
| 4015 | PyObject* error_names = PyUnicode_Join(comma, posonly_names); |
| 4016 | Py_DECREF(comma); |
| 4017 | if (error_names == NULL) { |
| 4018 | goto fail; |
| 4019 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4020 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4021 | "%U() got some positional-only arguments passed" |
| 4022 | " as keyword arguments: '%U'", |
| 4023 | co->co_name, error_names); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4024 | Py_DECREF(error_names); |
| 4025 | goto fail; |
| 4026 | } |
| 4027 | |
| 4028 | Py_DECREF(posonly_names); |
| 4029 | return 0; |
| 4030 | |
| 4031 | fail: |
| 4032 | Py_XDECREF(posonly_names); |
| 4033 | return 1; |
| 4034 | |
| 4035 | } |
| 4036 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4037 | /* This is gonna seem *real weird*, but if you put some other code between |
Marcel Plch | 3a9ccee | 2018-04-06 23:22:04 +0200 | [diff] [blame] | 4038 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4039 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 4040 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4041 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4042 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4043 | PyObject *const *args, Py_ssize_t argcount, |
| 4044 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4045 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4046 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4047 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4048 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4049 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4050 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4051 | PyFrameObject *f; |
| 4052 | PyObject *retval = NULL; |
| 4053 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | PyObject *x, *u; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4055 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4056 | Py_ssize_t i, j, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4057 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4058 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4059 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4060 | assert(tstate != NULL); |
| 4061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4062 | if (globals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4063 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 4064 | "PyEval_EvalCodeEx: NULL globals"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | return NULL; |
| 4066 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4067 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4068 | /* Create the frame */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4069 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4070 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4072 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | fastlocals = f->f_localsplus; |
| 4074 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4075 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4076 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4077 | if (co->co_flags & CO_VARKEYWORDS) { |
| 4078 | kwdict = PyDict_New(); |
| 4079 | if (kwdict == NULL) |
| 4080 | goto fail; |
| 4081 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4082 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4083 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4084 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4085 | SETLOCAL(i, kwdict); |
| 4086 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4087 | else { |
| 4088 | kwdict = NULL; |
| 4089 | } |
| 4090 | |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4091 | /* Copy all positional arguments into local variables */ |
| 4092 | if (argcount > co->co_argcount) { |
| 4093 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4094 | } |
| 4095 | else { |
| 4096 | n = argcount; |
| 4097 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4098 | for (j = 0; j < n; j++) { |
| 4099 | x = args[j]; |
| 4100 | Py_INCREF(x); |
| 4101 | SETLOCAL(j, x); |
| 4102 | } |
| 4103 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4104 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4105 | if (co->co_flags & CO_VARARGS) { |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 4106 | u = _PyTuple_FromArray(args + n, argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4107 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4108 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4109 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4110 | SETLOCAL(total_args, u); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4111 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4112 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4113 | /* Handle keyword arguments passed as two strided arrays */ |
| 4114 | kwcount *= kwstep; |
| 4115 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4116 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4117 | PyObject *keyword = kwnames[i]; |
| 4118 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4119 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4120 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4121 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4122 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4123 | "%U() keywords must be strings", |
| 4124 | co->co_name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4125 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4127 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4128 | /* Speed hack: do raw pointer compares. As names are |
| 4129 | normally interned this should almost always hit. */ |
| 4130 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4131 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4132 | PyObject *name = co_varnames[j]; |
| 4133 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4134 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4135 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4136 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4137 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4138 | /* Slow fallback, just in case */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4139 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4140 | PyObject *name = co_varnames[j]; |
| 4141 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 4142 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4143 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4144 | } |
| 4145 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4146 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4147 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4148 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4149 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 4150 | assert(j >= total_args); |
| 4151 | if (kwdict == NULL) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4152 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4153 | if (co->co_posonlyargcount |
| 4154 | && positional_only_passed_as_keyword(tstate, co, |
| 4155 | kwcount, kwnames)) |
| 4156 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4157 | goto fail; |
| 4158 | } |
| 4159 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4160 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4161 | "%U() got an unexpected keyword argument '%S'", |
| 4162 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4163 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4164 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4165 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4166 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4167 | goto fail; |
| 4168 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4169 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4170 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4171 | kw_found: |
| 4172 | if (GETLOCAL(j) != NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4173 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4174 | "%U() got multiple values for argument '%S'", |
| 4175 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4176 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4178 | Py_INCREF(value); |
| 4179 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4181 | |
| 4182 | /* Check the number of positional arguments */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4183 | if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4184 | too_many_positional(tstate, co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | goto fail; |
| 4186 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4187 | |
| 4188 | /* Add missing positional arguments (copy default values from defs) */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4189 | if (argcount < co->co_argcount) { |
| 4190 | Py_ssize_t m = co->co_argcount - defcount; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4191 | Py_ssize_t missing = 0; |
| 4192 | for (i = argcount; i < m; i++) { |
| 4193 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4194 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4195 | } |
| 4196 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4197 | if (missing) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4198 | missing_arguments(tstate, co, missing, defcount, fastlocals); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4199 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4200 | } |
| 4201 | if (n > m) |
| 4202 | i = n - m; |
| 4203 | else |
| 4204 | i = 0; |
| 4205 | for (; i < defcount; i++) { |
| 4206 | if (GETLOCAL(m+i) == NULL) { |
| 4207 | PyObject *def = defs[i]; |
| 4208 | Py_INCREF(def); |
| 4209 | SETLOCAL(m+i, def); |
| 4210 | } |
| 4211 | } |
| 4212 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4213 | |
| 4214 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4215 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4216 | Py_ssize_t missing = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4217 | for (i = co->co_argcount; i < total_args; i++) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4218 | PyObject *name; |
| 4219 | if (GETLOCAL(i) != NULL) |
| 4220 | continue; |
| 4221 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4222 | if (kwdefs != NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4223 | PyObject *def = PyDict_GetItemWithError(kwdefs, name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4224 | if (def) { |
| 4225 | Py_INCREF(def); |
| 4226 | SETLOCAL(i, def); |
| 4227 | continue; |
| 4228 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4229 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4230 | goto fail; |
| 4231 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4232 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4233 | missing++; |
| 4234 | } |
| 4235 | if (missing) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4236 | missing_arguments(tstate, co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4237 | goto fail; |
| 4238 | } |
| 4239 | } |
| 4240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4241 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4242 | vars into frame. */ |
| 4243 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4244 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 4245 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4246 | /* Possibly account for the cell variable being an argument. */ |
| 4247 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4248 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4249 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4250 | /* Clear the local copy. */ |
| 4251 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4252 | } |
| 4253 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4254 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4255 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4256 | if (c == NULL) |
| 4257 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4258 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4259 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4260 | |
| 4261 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4262 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4263 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4264 | Py_INCREF(o); |
| 4265 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4266 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4267 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4268 | /* Handle generator/coroutine/asynchronous generator */ |
| 4269 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4270 | PyObject *gen; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4271 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4273 | /* Don't need to keep the reference to f_back, it will be set |
| 4274 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4275 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4277 | /* Create a new generator that owns the ready to run frame |
| 4278 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4279 | if (is_coro) { |
| 4280 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4281 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4282 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4283 | } else { |
| 4284 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4285 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4286 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4287 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4288 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 4289 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4290 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4291 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4292 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4294 | |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 4295 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4296 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4297 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4300 | which can call back into Python. While we're done with the |
| 4301 | current Python frame (f), the associated C stack is still in use, |
| 4302 | so recursion_depth must be boosted for the duration. |
| 4303 | */ |
| 4304 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4305 | if (Py_REFCNT(f) > 1) { |
| 4306 | Py_DECREF(f); |
| 4307 | _PyObject_GC_TRACK(f); |
| 4308 | } |
| 4309 | else { |
| 4310 | ++tstate->recursion_depth; |
| 4311 | Py_DECREF(f); |
| 4312 | --tstate->recursion_depth; |
| 4313 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4317 | PyObject * |
| 4318 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4319 | PyObject *const *args, int argcount, |
| 4320 | PyObject *const *kws, int kwcount, |
| 4321 | PyObject *const *defs, int defcount, |
| 4322 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4323 | { |
| 4324 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4325 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 4326 | kws, kws != NULL ? kws + 1 : NULL, |
| 4327 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4328 | defs, defcount, |
| 4329 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4330 | NULL, NULL); |
| 4331 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4332 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4333 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4334 | special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4336 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4337 | res = _PyObject_LookupSpecial(o, id); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4338 | if (res == NULL && !_PyErr_Occurred(tstate)) { |
| 4339 | _PyErr_SetObject(tstate, PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | return NULL; |
| 4341 | } |
| 4342 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4346 | /* Logic for the raise statement (too complicated for inlining). |
| 4347 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4348 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4349 | do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4350 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4351 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | if (exc == NULL) { |
| 4354 | /* Reraise */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4355 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4357 | type = exc_info->exc_type; |
| 4358 | value = exc_info->exc_value; |
| 4359 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4360 | if (type == Py_None || type == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4361 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 4362 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4363 | return 0; |
| 4364 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4365 | Py_XINCREF(type); |
| 4366 | Py_XINCREF(value); |
| 4367 | Py_XINCREF(tb); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4368 | _PyErr_Restore(tstate, type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4369 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | /* We support the following forms of raise: |
| 4373 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4374 | raise <instance> |
| 4375 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | if (PyExceptionClass_Check(exc)) { |
| 4378 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4379 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | if (value == NULL) |
| 4381 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4382 | if (!PyExceptionInstance_Check(value)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4383 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4384 | "calling %R should have returned an instance of " |
| 4385 | "BaseException, not %R", |
| 4386 | type, Py_TYPE(value)); |
| 4387 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4388 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | } |
| 4390 | else if (PyExceptionInstance_Check(exc)) { |
| 4391 | value = exc; |
| 4392 | type = PyExceptionInstance_Class(exc); |
| 4393 | Py_INCREF(type); |
| 4394 | } |
| 4395 | else { |
| 4396 | /* Not something you can raise. You get an exception |
| 4397 | anyway, just not what you specified :-) */ |
| 4398 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4399 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4400 | "exceptions must derive from BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | goto raise_error; |
| 4402 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4403 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4404 | assert(type != NULL); |
| 4405 | assert(value != NULL); |
| 4406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | if (cause) { |
| 4408 | PyObject *fixed_cause; |
| 4409 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4410 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | if (fixed_cause == NULL) |
| 4412 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4413 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4415 | else if (PyExceptionInstance_Check(cause)) { |
| 4416 | fixed_cause = cause; |
| 4417 | } |
| 4418 | else if (cause == Py_None) { |
| 4419 | Py_DECREF(cause); |
| 4420 | fixed_cause = NULL; |
| 4421 | } |
| 4422 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4423 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4424 | "exception causes must derive from " |
| 4425 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | goto raise_error; |
| 4427 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4428 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4429 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4430 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4431 | _PyErr_SetObject(tstate, type, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4433 | Py_DECREF(value); |
| 4434 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4435 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4436 | |
| 4437 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | Py_XDECREF(value); |
| 4439 | Py_XDECREF(type); |
| 4440 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4441 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4444 | /* 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] | 4445 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4446 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4447 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4448 | with a variable target. |
| 4449 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4450 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4451 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4452 | unpack_iterable(PyThreadState *tstate, PyObject *v, |
| 4453 | int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4455 | int i = 0, j = 0; |
| 4456 | Py_ssize_t ll = 0; |
| 4457 | PyObject *it; /* iter(v) */ |
| 4458 | PyObject *w; |
| 4459 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4464 | if (it == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4465 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4466 | v->ob_type->tp_iter == NULL && !PySequence_Check(v)) |
| 4467 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4468 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4469 | "cannot unpack non-iterable %.200s object", |
| 4470 | v->ob_type->tp_name); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4471 | } |
| 4472 | return 0; |
| 4473 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | for (; i < argcnt; i++) { |
| 4476 | w = PyIter_Next(it); |
| 4477 | if (w == NULL) { |
| 4478 | /* Iterator done, via error or exhaustion. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4479 | if (!_PyErr_Occurred(tstate)) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4480 | if (argcntafter == -1) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4481 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4482 | "not enough values to unpack " |
| 4483 | "(expected %d, got %d)", |
| 4484 | argcnt, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4485 | } |
| 4486 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4487 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4488 | "not enough values to unpack " |
| 4489 | "(expected at least %d, got %d)", |
| 4490 | argcnt + argcntafter, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4491 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4492 | } |
| 4493 | goto Error; |
| 4494 | } |
| 4495 | *--sp = w; |
| 4496 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | if (argcntafter == -1) { |
| 4499 | /* We better have exhausted the iterator now. */ |
| 4500 | w = PyIter_Next(it); |
| 4501 | if (w == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4502 | if (_PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | goto Error; |
| 4504 | Py_DECREF(it); |
| 4505 | return 1; |
| 4506 | } |
| 4507 | Py_DECREF(w); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4508 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4509 | "too many values to unpack (expected %d)", |
| 4510 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | goto Error; |
| 4512 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | l = PySequence_List(it); |
| 4515 | if (l == NULL) |
| 4516 | goto Error; |
| 4517 | *--sp = l; |
| 4518 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | ll = PyList_GET_SIZE(l); |
| 4521 | if (ll < argcntafter) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4522 | _PyErr_Format(tstate, PyExc_ValueError, |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4523 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4524 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | goto Error; |
| 4526 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | /* Pop the "after-variable" args off the list. */ |
| 4529 | for (j = argcntafter; j > 0; j--, i++) { |
| 4530 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4531 | } |
| 4532 | /* Resize the list. */ |
| 4533 | Py_SIZE(l) = ll - argcntafter; |
| 4534 | Py_DECREF(it); |
| 4535 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4536 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4537 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | for (; i > 0; i--, sp++) |
| 4539 | Py_DECREF(*sp); |
| 4540 | Py_XDECREF(it); |
| 4541 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4545 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4546 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4547 | prtrace(PyThreadState *tstate, PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | printf("%s ", str); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4550 | if (PyObject_Print(v, stdout, 0) != 0) { |
| 4551 | /* Don't know what else to do */ |
| 4552 | _PyErr_Clear(tstate); |
| 4553 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | printf("\n"); |
| 4555 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4556 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4557 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4558 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4559 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4560 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4561 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4562 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4563 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4565 | _PyErr_Fetch(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | if (value == NULL) { |
| 4567 | value = Py_None; |
| 4568 | Py_INCREF(value); |
| 4569 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4570 | _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4571 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4572 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4573 | if (arg == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4574 | _PyErr_Restore(tstate, type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | return; |
| 4576 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4577 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | Py_DECREF(arg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4579 | if (err == 0) { |
| 4580 | _PyErr_Restore(tstate, type, value, orig_traceback); |
| 4581 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | else { |
| 4583 | Py_XDECREF(type); |
| 4584 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4585 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4589 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4590 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4591 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4594 | PyObject *type, *value, *traceback; |
| 4595 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4596 | _PyErr_Fetch(tstate, &type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4597 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | if (err == 0) |
| 4599 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4600 | _PyErr_Restore(tstate, type, value, traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | return 0; |
| 4602 | } |
| 4603 | else { |
| 4604 | Py_XDECREF(type); |
| 4605 | Py_XDECREF(value); |
| 4606 | Py_XDECREF(traceback); |
| 4607 | return -1; |
| 4608 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4611 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4612 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4613 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | int result; |
| 4617 | if (tstate->tracing) |
| 4618 | return 0; |
| 4619 | tstate->tracing++; |
| 4620 | tstate->use_tracing = 0; |
| 4621 | result = func(obj, frame, what, arg); |
| 4622 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4623 | || (tstate->c_profilefunc != NULL)); |
| 4624 | tstate->tracing--; |
| 4625 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4628 | PyObject * |
| 4629 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4630 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4631 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | int save_tracing = tstate->tracing; |
| 4633 | int save_use_tracing = tstate->use_tracing; |
| 4634 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | tstate->tracing = 0; |
| 4637 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4638 | || (tstate->c_profilefunc != NULL)); |
| 4639 | result = PyObject_Call(func, args, NULL); |
| 4640 | tstate->tracing = save_tracing; |
| 4641 | tstate->use_tracing = save_use_tracing; |
| 4642 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4645 | /* 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] | 4646 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4647 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4648 | PyThreadState *tstate, PyFrameObject *frame, |
| 4649 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4651 | int result = 0; |
| 4652 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | /* If the last instruction executed isn't in the current |
| 4655 | instruction window, reset the window. |
| 4656 | */ |
| 4657 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4658 | PyAddrPair bounds; |
| 4659 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4660 | &bounds); |
| 4661 | *instr_lb = bounds.ap_lower; |
| 4662 | *instr_ub = bounds.ap_upper; |
| 4663 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4664 | /* If the last instruction falls at the start of a line or if it |
| 4665 | represents a jump backwards, update the frame's line number and |
| 4666 | then call the trace function if we're tracing source lines. |
| 4667 | */ |
| 4668 | if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | frame->f_lineno = line; |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4670 | if (frame->f_trace_lines) { |
| 4671 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 4672 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 4674 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 4675 | if (frame->f_trace_opcodes) { |
| 4676 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 4677 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | *instr_prev = frame->f_lasti; |
| 4679 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4682 | void |
| 4683 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4684 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4685 | if (PySys_Audit("sys.setprofile", NULL) < 0) { |
| 4686 | return; |
| 4687 | } |
| 4688 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4689 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | PyObject *temp = tstate->c_profileobj; |
| 4691 | Py_XINCREF(arg); |
| 4692 | tstate->c_profilefunc = NULL; |
| 4693 | tstate->c_profileobj = NULL; |
| 4694 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4695 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4696 | Py_XDECREF(temp); |
| 4697 | tstate->c_profilefunc = func; |
| 4698 | tstate->c_profileobj = arg; |
| 4699 | /* Flag that tracing or profiling is turned on */ |
| 4700 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | void |
| 4704 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4705 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4706 | if (PySys_Audit("sys.settrace", NULL) < 0) { |
| 4707 | return; |
| 4708 | } |
| 4709 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4710 | _PyRuntimeState *runtime = &_PyRuntime; |
| 4711 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | PyObject *temp = tstate->c_traceobj; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4713 | runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | Py_XINCREF(arg); |
| 4715 | tstate->c_tracefunc = NULL; |
| 4716 | tstate->c_traceobj = NULL; |
| 4717 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4718 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4719 | Py_XDECREF(temp); |
| 4720 | tstate->c_tracefunc = func; |
| 4721 | tstate->c_traceobj = arg; |
| 4722 | /* Flag that tracing or profiling is turned on */ |
| 4723 | tstate->use_tracing = ((func != NULL) |
| 4724 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4727 | void |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4728 | _PyEval_SetCoroutineOriginTrackingDepth(int new_depth) |
| 4729 | { |
| 4730 | assert(new_depth >= 0); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4731 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4732 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 4733 | } |
| 4734 | |
| 4735 | int |
| 4736 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 4737 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4738 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4739 | return tstate->coroutine_origin_tracking_depth; |
| 4740 | } |
| 4741 | |
| 4742 | void |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4743 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4744 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4745 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4746 | |
| 4747 | if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) { |
| 4748 | return; |
| 4749 | } |
| 4750 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4751 | Py_XINCREF(firstiter); |
| 4752 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4753 | } |
| 4754 | |
| 4755 | PyObject * |
| 4756 | _PyEval_GetAsyncGenFirstiter(void) |
| 4757 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4758 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4759 | return tstate->async_gen_firstiter; |
| 4760 | } |
| 4761 | |
| 4762 | void |
| 4763 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4764 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4765 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4766 | |
| 4767 | if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) { |
| 4768 | return; |
| 4769 | } |
| 4770 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4771 | Py_XINCREF(finalizer); |
| 4772 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4773 | } |
| 4774 | |
| 4775 | PyObject * |
| 4776 | _PyEval_GetAsyncGenFinalizer(void) |
| 4777 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4778 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4779 | return tstate->async_gen_finalizer; |
| 4780 | } |
| 4781 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4782 | static PyFrameObject * |
| 4783 | _PyEval_GetFrame(PyThreadState *tstate) |
| 4784 | { |
| 4785 | return _PyRuntime.gilstate.getframe(tstate); |
| 4786 | } |
| 4787 | |
| 4788 | PyFrameObject * |
| 4789 | PyEval_GetFrame(void) |
| 4790 | { |
| 4791 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4792 | return _PyEval_GetFrame(tstate); |
| 4793 | } |
| 4794 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4795 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4796 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4797 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4798 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4799 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | if (current_frame == NULL) |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4801 | return tstate->interp->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4802 | else |
| 4803 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4806 | /* Convenience function to get a builtin from its name */ |
| 4807 | PyObject * |
| 4808 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 4809 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4810 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4811 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 4812 | if (attr) { |
| 4813 | Py_INCREF(attr); |
| 4814 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4815 | else if (!_PyErr_Occurred(tstate)) { |
| 4816 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name)); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4817 | } |
| 4818 | return attr; |
| 4819 | } |
| 4820 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4821 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4822 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4823 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4824 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4825 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4826 | if (current_frame == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4827 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4831 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4832 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4833 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4834 | |
| 4835 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4839 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4840 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4841 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4842 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4843 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
| 4844 | if (current_frame == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4846 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4847 | |
| 4848 | assert(current_frame->f_globals != NULL); |
| 4849 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4852 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4853 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4854 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4855 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4856 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4859 | if (current_frame != NULL) { |
| 4860 | const int codeflags = current_frame->f_code->co_flags; |
| 4861 | const int compilerflags = codeflags & PyCF_MASK; |
| 4862 | if (compilerflags) { |
| 4863 | result = 1; |
| 4864 | cf->cf_flags |= compilerflags; |
| 4865 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4866 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4867 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4868 | result = 1; |
| 4869 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4870 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4871 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | } |
| 4873 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4876 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4877 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4878 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4880 | if (PyMethod_Check(func)) |
| 4881 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4882 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4883 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4884 | else if (PyCFunction_Check(func)) |
| 4885 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4886 | else |
| 4887 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4890 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4891 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4893 | if (PyMethod_Check(func)) |
| 4894 | return "()"; |
| 4895 | else if (PyFunction_Check(func)) |
| 4896 | return "()"; |
| 4897 | else if (PyCFunction_Check(func)) |
| 4898 | return "()"; |
| 4899 | else |
| 4900 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4903 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4904 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4905 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4906 | tstate, tstate->frame, \ |
| 4907 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | x = NULL; \ |
| 4909 | } \ |
| 4910 | else { \ |
| 4911 | x = call; \ |
| 4912 | if (tstate->c_profilefunc != NULL) { \ |
| 4913 | if (x == NULL) { \ |
| 4914 | call_trace_protected(tstate->c_profilefunc, \ |
| 4915 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4916 | tstate, tstate->frame, \ |
| 4917 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | /* XXX should pass (type, value, tb) */ \ |
| 4919 | } else { \ |
| 4920 | if (call_trace(tstate->c_profilefunc, \ |
| 4921 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4922 | tstate, tstate->frame, \ |
| 4923 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | Py_DECREF(x); \ |
| 4925 | x = NULL; \ |
| 4926 | } \ |
| 4927 | } \ |
| 4928 | } \ |
| 4929 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4930 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | x = call; \ |
| 4932 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4933 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4934 | |
| 4935 | static PyObject * |
| 4936 | trace_call_function(PyThreadState *tstate, |
| 4937 | PyObject *func, |
| 4938 | PyObject **args, Py_ssize_t nargs, |
| 4939 | PyObject *kwnames) |
| 4940 | { |
| 4941 | PyObject *x; |
| 4942 | if (PyCFunction_Check(func)) { |
Jeroen Demeyer | 37788bc | 2019-05-30 15:11:22 +0200 | [diff] [blame] | 4943 | C_TRACE(x, _PyCFunction_Vectorcall(func, args, nargs, kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4944 | return x; |
| 4945 | } |
| 4946 | else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) { |
| 4947 | /* We need to create a temporary bound method as argument |
| 4948 | for profiling. |
| 4949 | |
| 4950 | If nargs == 0, then this cannot work because we have no |
| 4951 | "self". In any case, the call itself would raise |
| 4952 | TypeError (foo needs an argument), so we just skip |
| 4953 | profiling. */ |
| 4954 | PyObject *self = args[0]; |
| 4955 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 4956 | if (func == NULL) { |
| 4957 | return NULL; |
| 4958 | } |
Jeroen Demeyer | 37788bc | 2019-05-30 15:11:22 +0200 | [diff] [blame] | 4959 | C_TRACE(x, _PyCFunction_Vectorcall(func, |
| 4960 | args+1, nargs-1, |
| 4961 | kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4962 | Py_DECREF(func); |
| 4963 | return x; |
| 4964 | } |
| 4965 | return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
| 4966 | } |
| 4967 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 4968 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 4969 | to reduce the stack consumption. */ |
| 4970 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4971 | call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4972 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4973 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4974 | PyObject *func = *pfunc; |
| 4975 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4976 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4977 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4978 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4979 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4980 | if (tstate->use_tracing) { |
| 4981 | x = trace_call_function(tstate, func, stack, nargs, kwnames); |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4982 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4983 | else { |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4984 | x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4986 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4987 | assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4988 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4989 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4990 | while ((*pp_stack) > pfunc) { |
| 4991 | w = EXT_POP(*pp_stack); |
| 4992 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4995 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4996 | } |
| 4997 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4998 | static PyObject * |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4999 | do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5000 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5001 | PyObject *result; |
| 5002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5003 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5005 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5006 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5007 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5008 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 5009 | if (nargs > 0 && tstate->use_tracing) { |
| 5010 | /* We need to create a temporary bound method as argument |
| 5011 | for profiling. |
| 5012 | |
| 5013 | If nargs == 0, then this cannot work because we have no |
| 5014 | "self". In any case, the call itself would raise |
| 5015 | TypeError (foo needs an argument), so we just skip |
| 5016 | profiling. */ |
| 5017 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 5018 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5019 | if (func == NULL) { |
| 5020 | return NULL; |
| 5021 | } |
| 5022 | |
| 5023 | C_TRACE(result, _PyCFunction_FastCallDict(func, |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 5024 | &_PyTuple_ITEMS(callargs)[1], |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5025 | nargs - 1, |
| 5026 | kwdict)); |
| 5027 | Py_DECREF(func); |
| 5028 | return result; |
| 5029 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5030 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5031 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5034 | /* 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] | 5035 | nb_index slot defined, and store in *pi. |
| 5036 | 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] | 5037 | 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] | 5038 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5039 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5040 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5041 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5042 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5043 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5044 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | Py_ssize_t x; |
| 5046 | if (PyIndex_Check(v)) { |
| 5047 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5048 | if (x == -1 && _PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5049 | return 0; |
| 5050 | } |
| 5051 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5052 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5053 | "slice indices must be integers or " |
| 5054 | "None or have an __index__ method"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | return 0; |
| 5056 | } |
| 5057 | *pi = x; |
| 5058 | } |
| 5059 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5062 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5063 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5064 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5065 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5066 | Py_ssize_t x; |
| 5067 | if (PyIndex_Check(v)) { |
| 5068 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5069 | if (x == -1 && _PyErr_Occurred(tstate)) |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5070 | return 0; |
| 5071 | } |
| 5072 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5073 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5074 | "slice indices must be integers or " |
| 5075 | "have an __index__ method"); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5076 | return 0; |
| 5077 | } |
| 5078 | *pi = x; |
| 5079 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5083 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5085 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5086 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5087 | cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5088 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | int res = 0; |
| 5090 | switch (op) { |
| 5091 | case PyCmp_IS: |
| 5092 | res = (v == w); |
| 5093 | break; |
| 5094 | case PyCmp_IS_NOT: |
| 5095 | res = (v != w); |
| 5096 | break; |
| 5097 | case PyCmp_IN: |
| 5098 | res = PySequence_Contains(w, v); |
| 5099 | if (res < 0) |
| 5100 | return NULL; |
| 5101 | break; |
| 5102 | case PyCmp_NOT_IN: |
| 5103 | res = PySequence_Contains(w, v); |
| 5104 | if (res < 0) |
| 5105 | return NULL; |
| 5106 | res = !res; |
| 5107 | break; |
| 5108 | case PyCmp_EXC_MATCH: |
| 5109 | if (PyTuple_Check(w)) { |
| 5110 | Py_ssize_t i, length; |
| 5111 | length = PyTuple_Size(w); |
| 5112 | for (i = 0; i < length; i += 1) { |
| 5113 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5114 | if (!PyExceptionClass_Check(exc)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5115 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5116 | CANNOT_CATCH_MSG); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | return NULL; |
| 5118 | } |
| 5119 | } |
| 5120 | } |
| 5121 | else { |
| 5122 | if (!PyExceptionClass_Check(w)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5123 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5124 | CANNOT_CATCH_MSG); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5125 | return NULL; |
| 5126 | } |
| 5127 | } |
| 5128 | res = PyErr_GivenExceptionMatches(v, w); |
| 5129 | break; |
| 5130 | default: |
| 5131 | return PyObject_RichCompare(v, w, op); |
| 5132 | } |
| 5133 | v = res ? Py_True : Py_False; |
| 5134 | Py_INCREF(v); |
| 5135 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5138 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5139 | import_name(PyThreadState *tstate, PyFrameObject *f, |
| 5140 | PyObject *name, PyObject *fromlist, PyObject *level) |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5141 | { |
| 5142 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5143 | PyObject *import_func, *res; |
| 5144 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5145 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5146 | import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5147 | if (import_func == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5148 | if (!_PyErr_Occurred(tstate)) { |
| 5149 | _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5150 | } |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5151 | return NULL; |
| 5152 | } |
| 5153 | |
| 5154 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5155 | if (import_func == tstate->interp->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5156 | int ilevel = _PyLong_AsInt(level); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5157 | if (ilevel == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5158 | return NULL; |
| 5159 | } |
| 5160 | res = PyImport_ImportModuleLevelObject( |
| 5161 | name, |
| 5162 | f->f_globals, |
| 5163 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5164 | fromlist, |
| 5165 | ilevel); |
| 5166 | return res; |
| 5167 | } |
| 5168 | |
| 5169 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5170 | |
| 5171 | stack[0] = name; |
| 5172 | stack[1] = f->f_globals; |
| 5173 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5174 | stack[3] = fromlist; |
| 5175 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5176 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5177 | Py_DECREF(import_func); |
| 5178 | return res; |
| 5179 | } |
| 5180 | |
| 5181 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5182 | import_from(PyThreadState *tstate, PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5184 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5185 | _Py_IDENTIFIER(__name__); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5186 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5187 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5188 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5189 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5190 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5191 | /* Issue #17636: in case this failed because of a circular relative |
| 5192 | import, try to fallback on reading the module directly from |
| 5193 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5194 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5195 | if (pkgname == NULL) { |
| 5196 | goto error; |
| 5197 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 5198 | if (!PyUnicode_Check(pkgname)) { |
| 5199 | Py_CLEAR(pkgname); |
| 5200 | goto error; |
| 5201 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5202 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5203 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5204 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5205 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5206 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 5207 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5208 | Py_DECREF(fullmodname); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5209 | if (x == NULL && !_PyErr_Occurred(tstate)) { |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5210 | goto error; |
| 5211 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5212 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5213 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5214 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5215 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5216 | if (pkgname == NULL) { |
| 5217 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 5218 | if (pkgname_or_unknown == NULL) { |
| 5219 | Py_XDECREF(pkgpath); |
| 5220 | return NULL; |
| 5221 | } |
| 5222 | } else { |
| 5223 | pkgname_or_unknown = pkgname; |
| 5224 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5225 | |
| 5226 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5227 | _PyErr_Clear(tstate); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5228 | errmsg = PyUnicode_FromFormat( |
| 5229 | "cannot import name %R from %R (unknown location)", |
| 5230 | name, pkgname_or_unknown |
| 5231 | ); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5232 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5233 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 5234 | } |
| 5235 | else { |
| 5236 | errmsg = PyUnicode_FromFormat( |
| 5237 | "cannot import name %R from %R (%S)", |
| 5238 | name, pkgname_or_unknown, pkgpath |
| 5239 | ); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5240 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5241 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5242 | } |
| 5243 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5244 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5245 | Py_XDECREF(pkgname_or_unknown); |
| 5246 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5247 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5248 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5249 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5250 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5251 | import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5252 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5253 | _Py_IDENTIFIER(__all__); |
| 5254 | _Py_IDENTIFIER(__dict__); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5255 | _Py_IDENTIFIER(__name__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5256 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | int skip_leading_underscores = 0; |
| 5258 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5259 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5260 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 5261 | return -1; /* Unexpected error */ |
| 5262 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5263 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5264 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 5265 | return -1; |
| 5266 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | if (dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5268 | _PyErr_SetString(tstate, PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5269 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5270 | return -1; |
| 5271 | } |
| 5272 | all = PyMapping_Keys(dict); |
| 5273 | Py_DECREF(dict); |
| 5274 | if (all == NULL) |
| 5275 | return -1; |
| 5276 | skip_leading_underscores = 1; |
| 5277 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | for (pos = 0, err = 0; ; pos++) { |
| 5280 | name = PySequence_GetItem(all, pos); |
| 5281 | if (name == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5282 | if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | err = -1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5284 | } |
| 5285 | else { |
| 5286 | _PyErr_Clear(tstate); |
| 5287 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | break; |
| 5289 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5290 | if (!PyUnicode_Check(name)) { |
| 5291 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 5292 | if (modname == NULL) { |
| 5293 | Py_DECREF(name); |
| 5294 | err = -1; |
| 5295 | break; |
| 5296 | } |
| 5297 | if (!PyUnicode_Check(modname)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5298 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5299 | "module __name__ must be a string, not %.100s", |
| 5300 | Py_TYPE(modname)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5301 | } |
| 5302 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5303 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5304 | "%s in %U.%s must be str, not %.100s", |
| 5305 | skip_leading_underscores ? "Key" : "Item", |
| 5306 | modname, |
| 5307 | skip_leading_underscores ? "__dict__" : "__all__", |
| 5308 | Py_TYPE(name)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5309 | } |
| 5310 | Py_DECREF(modname); |
| 5311 | Py_DECREF(name); |
| 5312 | err = -1; |
| 5313 | break; |
| 5314 | } |
| 5315 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 5316 | if (PyUnicode_READY(name) == -1) { |
| 5317 | Py_DECREF(name); |
| 5318 | err = -1; |
| 5319 | break; |
| 5320 | } |
| 5321 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 5322 | Py_DECREF(name); |
| 5323 | continue; |
| 5324 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5325 | } |
| 5326 | value = PyObject_GetAttr(v, name); |
| 5327 | if (value == NULL) |
| 5328 | err = -1; |
| 5329 | else if (PyDict_CheckExact(locals)) |
| 5330 | err = PyDict_SetItem(locals, name, value); |
| 5331 | else |
| 5332 | err = PyObject_SetItem(locals, name, value); |
| 5333 | Py_DECREF(name); |
| 5334 | Py_XDECREF(value); |
| 5335 | if (err != 0) |
| 5336 | break; |
| 5337 | } |
| 5338 | Py_DECREF(all); |
| 5339 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5342 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5343 | check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5344 | { |
| 5345 | if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5346 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5347 | "%.200s%.200s argument after * " |
| 5348 | "must be an iterable, not %.200s", |
| 5349 | PyEval_GetFuncName(func), |
| 5350 | PyEval_GetFuncDesc(func), |
| 5351 | args->ob_type->tp_name); |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5352 | return -1; |
| 5353 | } |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5358 | format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5359 | { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5360 | /* _PyDict_MergeEx raises attribute |
| 5361 | * error (percolated from an attempt |
| 5362 | * to get 'keys' attribute) instead of |
| 5363 | * a type error if its second argument |
| 5364 | * is not a mapping. |
| 5365 | */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5366 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 5367 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5368 | "%.200s%.200s argument after ** " |
| 5369 | "must be a mapping, not %.200s", |
| 5370 | PyEval_GetFuncName(func), |
| 5371 | PyEval_GetFuncDesc(func), |
| 5372 | kwargs->ob_type->tp_name); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5373 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5374 | else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5375 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5376 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5377 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 5378 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 5379 | if (!PyUnicode_Check(key)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5380 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5381 | "%.200s%.200s keywords must be strings", |
| 5382 | PyEval_GetFuncName(func), |
| 5383 | PyEval_GetFuncDesc(func)); |
| 5384 | } |
| 5385 | else { |
| 5386 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5387 | "%.200s%.200s got multiple " |
| 5388 | "values for keyword argument '%U'", |
| 5389 | PyEval_GetFuncName(func), |
| 5390 | PyEval_GetFuncDesc(func), |
| 5391 | key); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5392 | } |
| 5393 | Py_XDECREF(exc); |
| 5394 | Py_XDECREF(val); |
| 5395 | Py_XDECREF(tb); |
| 5396 | } |
| 5397 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5398 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5399 | } |
| 5400 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5401 | } |
| 5402 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5403 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5404 | format_exc_check_arg(PyThreadState *tstate, PyObject *exc, |
| 5405 | const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | if (!obj) |
| 5410 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5411 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5412 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | if (!obj_str) |
| 5414 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5415 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5416 | _PyErr_Format(tstate, exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5417 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5418 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5419 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5420 | format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5421 | { |
| 5422 | PyObject *name; |
| 5423 | /* Don't stomp existing exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5424 | if (_PyErr_Occurred(tstate)) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5425 | return; |
| 5426 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5427 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5428 | oparg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5429 | format_exc_check_arg(tstate, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5430 | PyExc_UnboundLocalError, |
| 5431 | UNBOUNDLOCAL_ERROR_MSG, |
| 5432 | name); |
| 5433 | } else { |
| 5434 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5435 | PyTuple_GET_SIZE(co->co_cellvars)); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5436 | format_exc_check_arg(tstate, PyExc_NameError, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5437 | UNBOUNDFREE_ERROR_MSG, name); |
| 5438 | } |
| 5439 | } |
| 5440 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5441 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5442 | format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode) |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5443 | { |
| 5444 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5445 | if (prevopcode == BEFORE_ASYNC_WITH) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5446 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5447 | "'async with' received an object from __aenter__ " |
| 5448 | "that does not implement __await__: %.100s", |
| 5449 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5450 | } |
| 5451 | else if (prevopcode == WITH_CLEANUP_START) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5452 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5453 | "'async with' received an object from __aexit__ " |
| 5454 | "that does not implement __await__: %.100s", |
| 5455 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5460 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5461 | unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5462 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5463 | { |
| 5464 | PyObject *res; |
| 5465 | if (Py_REFCNT(v) == 2) { |
| 5466 | /* In the common case, there are 2 references to the value |
| 5467 | * stored in 'variable' when the += is performed: one on the |
| 5468 | * value stack (in 'v') and one still stored in the |
| 5469 | * 'variable'. We try to delete the variable now to reduce |
| 5470 | * the refcnt to 1. |
| 5471 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5472 | int opcode, oparg; |
| 5473 | NEXTOPARG(); |
| 5474 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5475 | case STORE_FAST: |
| 5476 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5477 | PyObject **fastlocals = f->f_localsplus; |
| 5478 | if (GETLOCAL(oparg) == v) |
| 5479 | SETLOCAL(oparg, NULL); |
| 5480 | break; |
| 5481 | } |
| 5482 | case STORE_DEREF: |
| 5483 | { |
| 5484 | PyObject **freevars = (f->f_localsplus + |
| 5485 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5486 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5487 | if (PyCell_GET(c) == v) { |
| 5488 | PyCell_SET(c, NULL); |
| 5489 | Py_DECREF(v); |
| 5490 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5491 | break; |
| 5492 | } |
| 5493 | case STORE_NAME: |
| 5494 | { |
| 5495 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5496 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5497 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5498 | if (locals && PyDict_CheckExact(locals)) { |
| 5499 | PyObject *w = PyDict_GetItemWithError(locals, name); |
| 5500 | if ((w == v && PyDict_DelItem(locals, name) != 0) || |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5501 | (w == NULL && _PyErr_Occurred(tstate))) |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5502 | { |
| 5503 | Py_DECREF(v); |
| 5504 | return NULL; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5505 | } |
| 5506 | } |
| 5507 | break; |
| 5508 | } |
| 5509 | } |
| 5510 | } |
| 5511 | res = v; |
| 5512 | PyUnicode_Append(&res, w); |
| 5513 | return res; |
| 5514 | } |
| 5515 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5516 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5517 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5518 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5519 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5521 | int i; |
| 5522 | PyObject *l = PyList_New(256); |
| 5523 | if (l == NULL) return NULL; |
| 5524 | for (i = 0; i < 256; i++) { |
| 5525 | PyObject *x = PyLong_FromLong(a[i]); |
| 5526 | if (x == NULL) { |
| 5527 | Py_DECREF(l); |
| 5528 | return NULL; |
| 5529 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5530 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5531 | } |
| 5532 | for (i = 0; i < 256; i++) |
| 5533 | a[i] = 0; |
| 5534 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5538 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5539 | { |
| 5540 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5542 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5543 | int i; |
| 5544 | PyObject *l = PyList_New(257); |
| 5545 | if (l == NULL) return NULL; |
| 5546 | for (i = 0; i < 257; i++) { |
| 5547 | PyObject *x = getarray(dxpairs[i]); |
| 5548 | if (x == NULL) { |
| 5549 | Py_DECREF(l); |
| 5550 | return NULL; |
| 5551 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5552 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | } |
| 5554 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5555 | #endif |
| 5556 | } |
| 5557 | |
| 5558 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5559 | |
| 5560 | Py_ssize_t |
| 5561 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5562 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5563 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5564 | Py_ssize_t new_index; |
| 5565 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5566 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5567 | return -1; |
| 5568 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5569 | new_index = interp->co_extra_user_count++; |
| 5570 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5571 | return new_index; |
| 5572 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5573 | |
| 5574 | static void |
| 5575 | dtrace_function_entry(PyFrameObject *f) |
| 5576 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5577 | const char *filename; |
| 5578 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5579 | int lineno; |
| 5580 | |
| 5581 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5582 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5583 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5584 | |
| 5585 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5586 | } |
| 5587 | |
| 5588 | static void |
| 5589 | dtrace_function_return(PyFrameObject *f) |
| 5590 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5591 | const char *filename; |
| 5592 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5593 | int lineno; |
| 5594 | |
| 5595 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5596 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5597 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5598 | |
| 5599 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5600 | } |
| 5601 | |
| 5602 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5603 | static void |
| 5604 | maybe_dtrace_line(PyFrameObject *frame, |
| 5605 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5606 | { |
| 5607 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5608 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5609 | |
| 5610 | /* If the last instruction executed isn't in the current |
| 5611 | instruction window, reset the window. |
| 5612 | */ |
| 5613 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5614 | PyAddrPair bounds; |
| 5615 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5616 | &bounds); |
| 5617 | *instr_lb = bounds.ap_lower; |
| 5618 | *instr_ub = bounds.ap_upper; |
| 5619 | } |
| 5620 | /* If the last instruction falls at the start of a line or if |
| 5621 | it represents a jump backwards, update the frame's line |
| 5622 | number and call the trace function. */ |
| 5623 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5624 | frame->f_lineno = line; |
| 5625 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5626 | if (!co_filename) |
| 5627 | co_filename = "?"; |
| 5628 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5629 | if (!co_name) |
| 5630 | co_name = "?"; |
| 5631 | PyDTrace_LINE(co_filename, co_name, line); |
| 5632 | } |
| 5633 | *instr_prev = frame->f_lasti; |
| 5634 | } |