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 */ |
Inada Naoki | eddef86 | 2019-06-04 07:38:10 +0900 | [diff] [blame] | 106 | #ifdef Py_DEBUG |
| 107 | // --with-pydebug is used to find memory leak. opcache makes it harder. |
| 108 | // So we disable opcache when Py_DEBUG is defined. |
| 109 | // See bpo-37146 |
| 110 | #define OPCACHE_MIN_RUNS 0 /* disable opcache */ |
| 111 | #else |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 112 | #define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */ |
Inada Naoki | eddef86 | 2019-06-04 07:38:10 +0900 | [diff] [blame] | 113 | #endif |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 114 | #define OPCACHE_STATS 0 /* Enable stats */ |
| 115 | |
| 116 | #if OPCACHE_STATS |
| 117 | static size_t opcache_code_objects = 0; |
| 118 | static size_t opcache_code_objects_extra_mem = 0; |
| 119 | |
| 120 | static size_t opcache_global_opts = 0; |
| 121 | static size_t opcache_global_hits = 0; |
| 122 | static size_t opcache_global_misses = 0; |
| 123 | #endif |
| 124 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 125 | #define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request) |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 126 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 127 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 128 | 1. We believe this is all right because the eval loop will release |
| 129 | the GIL eventually anyway. */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 130 | #define COMPUTE_EVAL_BREAKER(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | _Py_atomic_store_relaxed( \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 132 | &(ceval)->eval_breaker, \ |
| 133 | GIL_REQUEST | \ |
| 134 | _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \ |
| 135 | _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \ |
| 136 | (ceval)->pending.async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 137 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 138 | #define SET_GIL_DROP_REQUEST(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 140 | _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \ |
| 141 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 143 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 144 | #define RESET_GIL_DROP_REQUEST(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)->gil_drop_request, 0); \ |
| 147 | COMPUTE_EVAL_BREAKER(ceval); \ |
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 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 150 | /* Pending calls are only modified under pending_lock */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 151 | #define SIGNAL_PENDING_CALLS(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 153 | _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \ |
| 154 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 156 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 157 | #define UNSIGNAL_PENDING_CALLS(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 159 | _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \ |
| 160 | COMPUTE_EVAL_BREAKER(ceval); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 162 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 163 | #define SIGNAL_PENDING_SIGNALS(ceval) \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 164 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 165 | _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \ |
| 166 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 167 | } while (0) |
| 168 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 169 | #define UNSIGNAL_PENDING_SIGNALS(ceval) \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 170 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 171 | _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \ |
| 172 | COMPUTE_EVAL_BREAKER(ceval); \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 173 | } while (0) |
| 174 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 175 | #define SIGNAL_ASYNC_EXC(ceval) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 177 | (ceval)->pending.async_exc = 1; \ |
| 178 | _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 180 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 181 | #define UNSIGNAL_ASYNC_EXC(ceval) \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 182 | do { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 183 | (ceval)->pending.async_exc = 0; \ |
| 184 | COMPUTE_EVAL_BREAKER(ceval); \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 185 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 186 | |
| 187 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 188 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 189 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 190 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 191 | #include "pythread.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 192 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 193 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 194 | int |
| 195 | PyEval_ThreadsInitialized(void) |
| 196 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 197 | return gil_created(&_PyRuntime.ceval.gil); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 200 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 201 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 202 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 203 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 204 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 205 | struct _gil_runtime_state *gil = &ceval->gil; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 206 | if (gil_created(gil)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | return; |
Victor Stinner | a712679 | 2019-03-19 14:19:38 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 210 | PyThread_init_thread(); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 211 | create_gil(gil); |
| 212 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 213 | take_gil(ceval, tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 214 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 215 | struct _pending_calls *pending = &ceval->pending; |
| 216 | pending->lock = PyThread_allocate_lock(); |
| 217 | if (pending->lock == NULL) { |
| 218 | Py_FatalError("Can't initialize threads for pending calls"); |
| 219 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 220 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 222 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 223 | _PyEval_FiniThreads(struct _ceval_runtime_state *ceval) |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 224 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 225 | struct _gil_runtime_state *gil = &ceval->gil; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 226 | if (!gil_created(gil)) { |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 227 | return; |
Victor Stinner | a712679 | 2019-03-19 14:19:38 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 230 | destroy_gil(gil); |
| 231 | assert(!gil_created(gil)); |
Victor Stinner | 99fcc61 | 2019-04-29 13:04:07 +0200 | [diff] [blame] | 232 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 233 | struct _pending_calls *pending = &ceval->pending; |
| 234 | if (pending->lock != NULL) { |
| 235 | PyThread_free_lock(pending->lock); |
| 236 | pending->lock = NULL; |
| 237 | } |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 240 | static inline void |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 241 | exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate) |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 242 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 243 | /* _Py_Finalizing is protected by the GIL */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 244 | if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 245 | drop_gil(&runtime->ceval, tstate); |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 246 | PyThread_exit_thread(); |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 250 | void |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 251 | _PyEval_Fini(void) |
| 252 | { |
| 253 | #if OPCACHE_STATS |
| 254 | fprintf(stderr, "-- Opcode cache number of objects = %zd\n", |
| 255 | opcache_code_objects); |
| 256 | |
| 257 | fprintf(stderr, "-- Opcode cache total extra mem = %zd\n", |
| 258 | opcache_code_objects_extra_mem); |
| 259 | |
| 260 | fprintf(stderr, "\n"); |
| 261 | |
| 262 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n", |
| 263 | opcache_global_hits, |
| 264 | (int) (100.0 * opcache_global_hits / |
| 265 | (opcache_global_hits + opcache_global_misses))); |
| 266 | |
| 267 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n", |
| 268 | opcache_global_misses, |
| 269 | (int) (100.0 * opcache_global_misses / |
| 270 | (opcache_global_hits + opcache_global_misses))); |
| 271 | |
| 272 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n", |
| 273 | opcache_global_opts); |
| 274 | |
| 275 | fprintf(stderr, "\n"); |
| 276 | #endif |
| 277 | } |
| 278 | |
| 279 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 280 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 281 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 282 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 283 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 284 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 285 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 287 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 288 | take_gil(ceval, tstate); |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 289 | exit_thread_if_finalizing(runtime, tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 293 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 294 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 295 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 296 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 298 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | in debug mode. |
| 300 | */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 301 | drop_gil(&runtime->ceval, tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 305 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 306 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 307 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 309 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 310 | |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 311 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 312 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 315 | assert(gil_created(&ceval->gil)); |
| 316 | take_gil(ceval, tstate); |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 317 | exit_thread_if_finalizing(runtime, tstate); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 318 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
| 319 | Py_FatalError("PyEval_AcquireThread: non-NULL old thread state"); |
| 320 | } |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 324 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 325 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 326 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 330 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 331 | PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 332 | if (new_tstate != tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 334 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 335 | drop_gil(&runtime->ceval, tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 336 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 338 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
| 339 | * which are not running in the child process, and clear internal locks |
| 340 | * which might be held by those threads. |
| 341 | */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 342 | |
| 343 | void |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 344 | _PyEval_ReInitThreads(_PyRuntimeState *runtime) |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 345 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 346 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 347 | if (!gil_created(&ceval->gil)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | return; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 349 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 350 | recreate_gil(&ceval->gil); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 351 | PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 352 | take_gil(ceval, current_tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 353 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 354 | struct _pending_calls *pending = &ceval->pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 355 | pending->lock = PyThread_allocate_lock(); |
| 356 | if (pending->lock == NULL) { |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 357 | Py_FatalError("Can't initialize threads for pending calls"); |
| 358 | } |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 360 | /* Destroy all threads except the current one */ |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 361 | _PyThreadState_DeleteExcept(runtime, current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 362 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 364 | /* 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] | 365 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 366 | |
| 367 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 368 | _PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 369 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 370 | SIGNAL_ASYNC_EXC(ceval); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 371 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 373 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 374 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 375 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 376 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 377 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 378 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 379 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 381 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 382 | assert(gil_created(&ceval->gil)); |
| 383 | drop_gil(ceval, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 388 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 389 | { |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 390 | _PyRuntimeState *runtime = &_PyRuntime; |
| 391 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 392 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 393 | if (tstate == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 395 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 396 | assert(gil_created(&ceval->gil)); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 397 | |
| 398 | int err = errno; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 399 | take_gil(ceval, tstate); |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 400 | exit_thread_if_finalizing(runtime, tstate); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 401 | errno = err; |
| 402 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 403 | _PyThreadState_Swap(&runtime->gilstate, tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 407 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 408 | signal handlers or Mac I/O completion routines) can schedule calls |
| 409 | to a function to be called synchronously. |
| 410 | The synchronous function is called with one void* argument. |
| 411 | It should return 0 for success or -1 for failure -- failure should |
| 412 | be accompanied by an exception. |
| 413 | |
| 414 | If registry succeeds, the registry function returns 0; if it fails |
| 415 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 416 | an exception condition). |
| 417 | |
| 418 | Note that because registry may occur from within signal handlers, |
| 419 | or other asynchronous events, calling malloc() is unsafe! |
| 420 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 421 | Any thread can schedule pending calls, but only the main thread |
| 422 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 423 | There is no facility to schedule calls to a particular thread, but |
| 424 | that should be easy to change, should that ever be required. In |
| 425 | that case, the static variables here should go into the python |
| 426 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 427 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 429 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 430 | _PyEval_SignalReceived(struct _ceval_runtime_state *ceval) |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 431 | { |
| 432 | /* bpo-30703: Function called when the C signal handler of Python gets a |
| 433 | signal. We cannot queue a callback using Py_AddPendingCall() since |
| 434 | that function is not async-signal-safe. */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 435 | SIGNAL_PENDING_SIGNALS(ceval); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 438 | /* Push one item onto the queue while holding the lock. */ |
| 439 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 440 | _push_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 441 | int (*func)(void *), void *arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 442 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 443 | int i = pending->last; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 444 | int j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 445 | if (j == pending->first) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 446 | return -1; /* Queue full */ |
| 447 | } |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 448 | pending->calls[i].func = func; |
| 449 | pending->calls[i].arg = arg; |
| 450 | pending->last = j; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* Pop one item off the queue while holding the lock. */ |
| 455 | static void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 456 | _pop_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 457 | int (**func)(void *), void **arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 458 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 459 | int i = pending->first; |
| 460 | if (i == pending->last) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 461 | return; /* Queue empty */ |
| 462 | } |
| 463 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 464 | *func = pending->calls[i].func; |
| 465 | *arg = pending->calls[i].arg; |
| 466 | pending->first = (i + 1) % NPENDINGCALLS; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 469 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 470 | scheduling to be made from any thread, and even from an executing |
| 471 | callback. |
| 472 | */ |
| 473 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 474 | int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 475 | _PyEval_AddPendingCall(PyThreadState *tstate, |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 476 | struct _ceval_runtime_state *ceval, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 477 | int (*func)(void *), void *arg) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 478 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 479 | struct _pending_calls *pending = &ceval->pending; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 480 | |
| 481 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
| 482 | if (pending->finishing) { |
| 483 | PyThread_release_lock(pending->lock); |
| 484 | |
| 485 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 486 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 487 | _PyErr_SetString(tstate, PyExc_SystemError, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 488 | "Py_AddPendingCall: cannot add pending calls " |
| 489 | "(Python shutting down)"); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 490 | _PyErr_Print(tstate); |
| 491 | _PyErr_Restore(tstate, exc, val, tb); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 492 | return -1; |
| 493 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 494 | int result = _push_pending_call(pending, func, arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 495 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 496 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 497 | /* signal main loop */ |
| 498 | SIGNAL_PENDING_CALLS(ceval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 502 | int |
| 503 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 504 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 505 | _PyRuntimeState *runtime = &_PyRuntime; |
| 506 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 507 | return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 510 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 511 | handle_signals(_PyRuntimeState *runtime) |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 512 | { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 513 | /* Only handle signals on main thread. PyEval_InitThreads must |
| 514 | * have been called already. |
| 515 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 516 | if (PyThread_get_thread_ident() != runtime->main_thread) { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 517 | return 0; |
| 518 | } |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 519 | /* |
| 520 | * Ensure that the thread isn't currently running some other |
| 521 | * interpreter. |
| 522 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 523 | PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp; |
| 524 | if (interp != runtime->interpreters.main) { |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 527 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 528 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 529 | UNSIGNAL_PENDING_SIGNALS(ceval); |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 530 | if (_PyErr_CheckSignals() < 0) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 531 | SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 532 | return -1; |
| 533 | } |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 538 | make_pending_calls(_PyRuntimeState *runtime) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 539 | { |
Eric Snow | 6a150bc | 2019-06-01 15:39:46 -0600 | [diff] [blame] | 540 | static int busy = 0; |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 541 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 542 | /* only service pending calls on main thread */ |
| 543 | if (PyThread_get_thread_ident() != runtime->main_thread) { |
| 544 | return 0; |
| 545 | } |
| 546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | /* don't perform recursive pending calls */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 548 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 550 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 551 | busy = 1; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 552 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 553 | /* unsignal before starting to call callbacks, so that any callback |
| 554 | added in-between re-signals */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 555 | UNSIGNAL_PENDING_CALLS(ceval); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 556 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | /* perform a bounded number of calls, in case of recursion */ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 559 | struct _pending_calls *pending = &ceval->pending; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 560 | for (int i=0; i<NPENDINGCALLS; i++) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 561 | int (*func)(void *) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | void *arg = NULL; |
| 563 | |
| 564 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 565 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 566 | _pop_pending_call(pending, &func, &arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 567 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 568 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 569 | /* having released the lock, perform the callback */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 570 | if (func == NULL) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 571 | break; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 572 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 573 | res = func(arg); |
| 574 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 575 | goto error; |
| 576 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 578 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 579 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 580 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 581 | |
| 582 | error: |
| 583 | busy = 0; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 584 | SIGNAL_PENDING_CALLS(ceval); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 585 | return res; |
| 586 | } |
| 587 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 588 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 589 | _Py_FinishPendingCalls(_PyRuntimeState *runtime) |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 590 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 591 | assert(PyGILState_Check()); |
| 592 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 593 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 594 | struct _pending_calls *pending = &runtime->ceval.pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 595 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 596 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
| 597 | pending->finishing = 1; |
| 598 | PyThread_release_lock(pending->lock); |
| 599 | |
| 600 | if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) { |
| 601 | return; |
| 602 | } |
| 603 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 604 | if (make_pending_calls(runtime) < 0) { |
| 605 | PyObject *exc, *val, *tb; |
| 606 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 607 | PyErr_BadInternalCall(); |
| 608 | _PyErr_ChainExceptions(exc, val, tb); |
| 609 | _PyErr_Print(tstate); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 613 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 614 | of backward-compatibility. */ |
| 615 | int |
| 616 | Py_MakePendingCalls(void) |
| 617 | { |
| 618 | assert(PyGILState_Check()); |
| 619 | |
| 620 | /* Python signal handler doesn't really queue a callback: it only signals |
| 621 | that a signal was received, see _PyEval_SignalReceived(). */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 622 | _PyRuntimeState *runtime = &_PyRuntime; |
| 623 | int res = handle_signals(runtime); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 624 | if (res != 0) { |
| 625 | return res; |
| 626 | } |
| 627 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 628 | res = make_pending_calls(runtime); |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 629 | if (res != 0) { |
| 630 | return res; |
| 631 | } |
| 632 | |
| 633 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 636 | /* The interpreter's recursion limit */ |
| 637 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 638 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 639 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 640 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 641 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 642 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 643 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 644 | void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 645 | _PyEval_Initialize(struct _ceval_runtime_state *state) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 646 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 647 | state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 648 | _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 649 | _gil_initialize(&state->gil); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 650 | } |
| 651 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 652 | int |
| 653 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 654 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 655 | return _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 658 | void |
| 659 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 660 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 661 | struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; |
| 662 | ceval->recursion_limit = new_limit; |
| 663 | _Py_CheckRecursionLimit = ceval->recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 666 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 667 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 668 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 669 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 670 | Without USE_STACKCHECK, there is no need for this. */ |
| 671 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 672 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 673 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 674 | _PyRuntimeState *runtime = &_PyRuntime; |
| 675 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 676 | int recursion_limit = runtime->ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 677 | |
| 678 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 679 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | if (PyOS_CheckStack()) { |
| 681 | --tstate->recursion_depth; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 682 | _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | return -1; |
| 684 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 685 | /* Needed for ABI backwards-compatibility (see bpo-31857) */ |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 686 | _Py_CheckRecursionLimit = recursion_limit; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 687 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | if (tstate->recursion_critical) |
| 689 | /* Somebody asked that we don't check for recursion. */ |
| 690 | return 0; |
| 691 | if (tstate->overflowed) { |
| 692 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 693 | /* Overflowing while handling an overflow. Give up. */ |
| 694 | Py_FatalError("Cannot recover from stack overflow."); |
| 695 | } |
| 696 | return 0; |
| 697 | } |
| 698 | if (tstate->recursion_depth > recursion_limit) { |
| 699 | --tstate->recursion_depth; |
| 700 | tstate->overflowed = 1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 701 | _PyErr_Format(tstate, PyExc_RecursionError, |
| 702 | "maximum recursion depth exceeded%s", |
| 703 | where); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | return -1; |
| 705 | } |
| 706 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 709 | static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 710 | static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 711 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 712 | #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 714 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 715 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 716 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | return PyEval_EvalCodeEx(co, |
| 719 | globals, locals, |
| 720 | (PyObject **)NULL, 0, |
| 721 | (PyObject **)NULL, 0, |
| 722 | (PyObject **)NULL, 0, |
| 723 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
| 727 | /* Interpreter main loop */ |
| 728 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 729 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 730 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | /* This is for backward compatibility with extension modules that |
| 732 | used this API; core interpreter code should call |
| 733 | PyEval_EvalFrameEx() */ |
| 734 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 738 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 739 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 740 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 741 | return interp->eval_frame(f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 744 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 745 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 746 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 747 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 749 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 750 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 751 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 752 | int opcode; /* Current opcode */ |
| 753 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 754 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 756 | _PyRuntimeState * const runtime = &_PyRuntime; |
| 757 | PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 758 | struct _ceval_runtime_state * const ceval = &runtime->ceval; |
| 759 | _Py_atomic_int * const eval_breaker = &ceval->eval_breaker; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | is true when the line being executed has changed. The |
| 767 | initial values are such as to make this false the first |
| 768 | time it is tested. */ |
| 769 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 770 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 771 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | PyObject *names; |
| 773 | PyObject *consts; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 774 | _PyOpcache *co_opcache; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 775 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 776 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 777 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 778 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 779 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 780 | /* Computed GOTOs, or |
| 781 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 782 | using gcc's labels-as-values extension |
| 783 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 784 | |
| 785 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 787 | combined with a lookup table of jump addresses. However, since the |
| 788 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 789 | hard time making the right prediction for where to jump next (actually, |
| 790 | it will be always wrong except in the uncommon case of a sequence of |
| 791 | several identical opcodes). |
| 792 | |
| 793 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 794 | indirect jump instruction at the end of each opcode. Since the jump |
| 795 | instruction is at a different address for each opcode, the CPU will make a |
| 796 | separate prediction for each of these instructions, which is equivalent to |
| 797 | predicting the second opcode of each opcode pair. These predictions have |
| 798 | a much better chance to turn out valid, especially in small bytecode loops. |
| 799 | |
| 800 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 802 | and potentially many more instructions (depending on the pipeline width). |
| 803 | A correctly predicted branch, however, is nearly free. |
| 804 | |
| 805 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 806 | faster than the normal "switch" version, depending on the compiler and the |
| 807 | CPU architecture. |
| 808 | |
| 809 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 810 | because it would render the measurements invalid. |
| 811 | |
| 812 | |
| 813 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 814 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 815 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 816 | -fno-crossjumping). |
| 817 | */ |
| 818 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 819 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 820 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 821 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 822 | #endif |
| 823 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 824 | #ifdef HAVE_COMPUTED_GOTOS |
| 825 | #ifndef USE_COMPUTED_GOTOS |
| 826 | #define USE_COMPUTED_GOTOS 1 |
| 827 | #endif |
| 828 | #else |
| 829 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 830 | #error "Computed gotos are not supported on this compiler." |
| 831 | #endif |
| 832 | #undef USE_COMPUTED_GOTOS |
| 833 | #define USE_COMPUTED_GOTOS 0 |
| 834 | #endif |
| 835 | |
| 836 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 837 | /* Import the static jump table */ |
| 838 | #include "opcode_targets.h" |
| 839 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 840 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 841 | op: \ |
| 842 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 844 | #ifdef LLTRACE |
| 845 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 847 | if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 849 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 850 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | } \ |
| 852 | goto fast_next_opcode; \ |
| 853 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 854 | #else |
| 855 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | { \ |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 857 | if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 859 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 860 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | } \ |
| 862 | goto fast_next_opcode; \ |
| 863 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 864 | #endif |
| 865 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 866 | #define DISPATCH() \ |
| 867 | { \ |
| 868 | if (!_Py_atomic_load_relaxed(eval_breaker)) { \ |
| 869 | FAST_DISPATCH(); \ |
| 870 | } \ |
| 871 | continue; \ |
| 872 | } |
| 873 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 874 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 875 | #define TARGET(op) op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 876 | #define FAST_DISPATCH() goto fast_next_opcode |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 877 | #define DISPATCH() continue |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 878 | #endif |
| 879 | |
| 880 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 881 | /* Tuple access macros */ |
| 882 | |
| 883 | #ifndef Py_DEBUG |
| 884 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 885 | #else |
| 886 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 887 | #endif |
| 888 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 889 | /* Code access macros */ |
| 890 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 891 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 892 | #define INSTR_OFFSET() \ |
| 893 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 894 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 895 | _Py_CODEUNIT word = *next_instr; \ |
| 896 | opcode = _Py_OPCODE(word); \ |
| 897 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 898 | next_instr++; \ |
| 899 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 900 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 901 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 902 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 903 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | Some opcodes tend to come in pairs thus making it possible to |
| 905 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 906 | 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] | 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | Verifying the prediction costs a single high-speed test of a register |
| 909 | variable against a constant. If the pairing was good, then the |
| 910 | processor's own internal branch predication has a high likelihood of |
| 911 | success, resulting in a nearly zero-overhead transition to the |
| 912 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 913 | including its unpredictable switch-case branch. Combined with the |
| 914 | processor's internal branch prediction, a successful PREDICT has the |
| 915 | effect of making the two opcodes run as if they were a single new opcode |
| 916 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 917 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 918 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | predictions turned-on and interpret the results as if some opcodes |
| 920 | had been combined or turn-off predictions so that the opcode frequency |
| 921 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 922 | |
| 923 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | the CPU to record separate branch prediction information for each |
| 925 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 926 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 927 | */ |
| 928 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 929 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 931 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 932 | #define PREDICT(op) \ |
| 933 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 934 | _Py_CODEUNIT word = *next_instr; \ |
| 935 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 936 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 937 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 938 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 939 | goto PRED_##op; \ |
| 940 | } \ |
| 941 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 942 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 943 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 944 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 945 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 946 | /* Stack manipulation macros */ |
| 947 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 948 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 949 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 950 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 951 | #define EMPTY() (STACK_LEVEL() == 0) |
| 952 | #define TOP() (stack_pointer[-1]) |
| 953 | #define SECOND() (stack_pointer[-2]) |
| 954 | #define THIRD() (stack_pointer[-3]) |
| 955 | #define FOURTH() (stack_pointer[-4]) |
| 956 | #define PEEK(n) (stack_pointer[-(n)]) |
| 957 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 958 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 959 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 960 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 961 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 962 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 963 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 964 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 965 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 966 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 968 | lltrace && prtrace(tstate, TOP(), "push")); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 969 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 970 | #define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 971 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 972 | #define STACK_GROW(n) do { \ |
| 973 | assert(n >= 0); \ |
| 974 | (void)(BASIC_STACKADJ(n), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 975 | lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 976 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 977 | } while (0) |
| 978 | #define STACK_SHRINK(n) do { \ |
| 979 | assert(n >= 0); \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 980 | (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 981 | (void)(BASIC_STACKADJ(-n)); \ |
| 982 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 983 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 984 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 985 | prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 986 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 987 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 988 | #define PUSH(v) BASIC_PUSH(v) |
| 989 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 990 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 991 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 992 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 993 | #endif |
| 994 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 995 | /* Local variable macros */ |
| 996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 998 | |
| 999 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 1000 | then store the new value; it must copy the old value to a temporary |
| 1001 | value, then store the new value, and then DECREF the temporary value. |
| 1002 | This is because it is possible that during the DECREF the frame is |
| 1003 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1004 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1006 | GETLOCAL(i) = value; \ |
| 1007 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1008 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1009 | |
| 1010 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1012 | PyObject *v = POP(); \ |
| 1013 | Py_XDECREF(v); \ |
| 1014 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1015 | |
| 1016 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1017 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1019 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1021 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1022 | value = POP(); \ |
| 1023 | Py_XDECREF(value); \ |
| 1024 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1025 | exc_info = tstate->exc_info; \ |
| 1026 | type = exc_info->exc_type; \ |
| 1027 | value = exc_info->exc_value; \ |
| 1028 | traceback = exc_info->exc_traceback; \ |
| 1029 | exc_info->exc_type = POP(); \ |
| 1030 | exc_info->exc_value = POP(); \ |
| 1031 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | Py_XDECREF(type); \ |
| 1033 | Py_XDECREF(value); \ |
| 1034 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1035 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1036 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1037 | /* macros for opcode cache */ |
| 1038 | #define OPCACHE_CHECK() \ |
| 1039 | do { \ |
| 1040 | co_opcache = NULL; \ |
| 1041 | if (co->co_opcache != NULL) { \ |
| 1042 | unsigned char co_opt_offset = \ |
| 1043 | co->co_opcache_map[next_instr - first_instr]; \ |
| 1044 | if (co_opt_offset > 0) { \ |
| 1045 | assert(co_opt_offset <= co->co_opcache_size); \ |
| 1046 | co_opcache = &co->co_opcache[co_opt_offset - 1]; \ |
| 1047 | assert(co_opcache != NULL); \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1048 | } \ |
| 1049 | } \ |
| 1050 | } while (0) |
| 1051 | |
| 1052 | #if OPCACHE_STATS |
| 1053 | |
| 1054 | #define OPCACHE_STAT_GLOBAL_HIT() \ |
| 1055 | do { \ |
| 1056 | if (co->co_opcache != NULL) opcache_global_hits++; \ |
| 1057 | } while (0) |
| 1058 | |
| 1059 | #define OPCACHE_STAT_GLOBAL_MISS() \ |
| 1060 | do { \ |
| 1061 | if (co->co_opcache != NULL) opcache_global_misses++; \ |
| 1062 | } while (0) |
| 1063 | |
| 1064 | #define OPCACHE_STAT_GLOBAL_OPT() \ |
| 1065 | do { \ |
| 1066 | if (co->co_opcache != NULL) opcache_global_opts++; \ |
| 1067 | } while (0) |
| 1068 | |
| 1069 | #else /* OPCACHE_STATS */ |
| 1070 | |
| 1071 | #define OPCACHE_STAT_GLOBAL_HIT() |
| 1072 | #define OPCACHE_STAT_GLOBAL_MISS() |
| 1073 | #define OPCACHE_STAT_GLOBAL_OPT() |
| 1074 | |
| 1075 | #endif |
| 1076 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1077 | /* Start of code */ |
| 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | /* push frame */ |
| 1080 | if (Py_EnterRecursiveCall("")) |
| 1081 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | if (tstate->use_tracing) { |
| 1086 | if (tstate->c_tracefunc != NULL) { |
| 1087 | /* tstate->c_tracefunc, if defined, is a |
| 1088 | function that will be called on *every* entry |
| 1089 | to a code block. Its return value, if not |
| 1090 | None, is a function that will be called at |
| 1091 | the start of each executed line of code. |
| 1092 | (Actually, the function must return itself |
| 1093 | in order to continue tracing.) The trace |
| 1094 | functions are called with three arguments: |
| 1095 | a pointer to the current frame, a string |
| 1096 | indicating why the function is called, and |
| 1097 | an argument which depends on the situation. |
| 1098 | The global trace function is also called |
| 1099 | whenever an exception is detected. */ |
| 1100 | if (call_trace_protected(tstate->c_tracefunc, |
| 1101 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1102 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | /* Trace function raised an error */ |
| 1104 | goto exit_eval_frame; |
| 1105 | } |
| 1106 | } |
| 1107 | if (tstate->c_profilefunc != NULL) { |
| 1108 | /* Similar for c_profilefunc, except it needn't |
| 1109 | return itself and isn't called for "line" events */ |
| 1110 | if (call_trace_protected(tstate->c_profilefunc, |
| 1111 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1112 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | /* Profile function raised an error */ |
| 1114 | goto exit_eval_frame; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1118 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1119 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 1120 | dtrace_function_entry(f); |
| 1121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | co = f->f_code; |
| 1123 | names = co->co_names; |
| 1124 | consts = co->co_consts; |
| 1125 | fastlocals = f->f_localsplus; |
| 1126 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1127 | assert(PyBytes_Check(co->co_code)); |
| 1128 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1129 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 1130 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1131 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1132 | /* |
| 1133 | f->f_lasti refers to the index of the last instruction, |
| 1134 | 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] | 1135 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1136 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1137 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1140 | direct succession without updating f->f_lasti. A successful |
| 1141 | prediction effectively links the two codes together as if they |
| 1142 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1143 | the first code in the pair (for instance, GET_ITER followed by |
| 1144 | 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] | 1145 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1147 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1148 | next_instr = first_instr; |
| 1149 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1150 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1151 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1152 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | stack_pointer = f->f_stacktop; |
| 1154 | assert(stack_pointer != NULL); |
| 1155 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1156 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1157 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1158 | if (co->co_opcache_flag < OPCACHE_MIN_RUNS) { |
| 1159 | co->co_opcache_flag++; |
| 1160 | if (co->co_opcache_flag == OPCACHE_MIN_RUNS) { |
| 1161 | if (_PyCode_InitOpcache(co) < 0) { |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | #if OPCACHE_STATS |
| 1165 | opcache_code_objects_extra_mem += |
| 1166 | PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) + |
| 1167 | sizeof(_PyOpcache) * co->co_opcache_size; |
| 1168 | opcache_code_objects++; |
| 1169 | #endif |
| 1170 | } |
| 1171 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1172 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1173 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1174 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1175 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1176 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1177 | if (throwflag) /* support for generator.throw() */ |
| 1178 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1179 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1180 | #ifdef Py_DEBUG |
| 1181 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 1182 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1183 | caller loses its exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1184 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1185 | #endif |
| 1186 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1187 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1190 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1191 | assert(!_PyErr_Occurred(tstate)); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | /* Do periodic things. Doing this every time through |
| 1194 | the loop would add too much overhead, so we do it |
| 1195 | only every Nth instruction. We also do it if |
| 1196 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1197 | event needs attention (e.g. a signal handler or |
| 1198 | async I/O handler); see Py_AddPendingCall() and |
| 1199 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1200 | |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 1201 | if (_Py_atomic_load_relaxed(eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1202 | opcode = _Py_OPCODE(*next_instr); |
| 1203 | if (opcode == SETUP_FINALLY || |
| 1204 | opcode == SETUP_WITH || |
| 1205 | opcode == BEFORE_ASYNC_WITH || |
| 1206 | opcode == YIELD_FROM) { |
| 1207 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 1208 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1209 | - If we're about to enter the 'with:'. It will prevent |
| 1210 | emitting a resource warning in the common idiom |
| 1211 | 'with open(path) as file:'. |
| 1212 | - If we're about to enter the 'async with:'. |
| 1213 | - 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] | 1214 | *very* useful, but might help in some cases and it's |
| 1215 | traditional) |
| 1216 | - If we're resuming a chain of nested 'yield from' or |
| 1217 | 'await' calls, then each frame is parked with YIELD_FROM |
| 1218 | as its next opcode. If the user hit control-C we want to |
| 1219 | wait until we've reached the innermost frame before |
| 1220 | running the signal handler and raising KeyboardInterrupt |
| 1221 | (see bpo-30039). |
| 1222 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | goto fast_next_opcode; |
| 1224 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1225 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1226 | if (_Py_atomic_load_relaxed(&ceval->signals_pending)) { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1227 | if (handle_signals(runtime) != 0) { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1228 | goto error; |
| 1229 | } |
| 1230 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1231 | if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) { |
| 1232 | if (make_pending_calls(runtime) != 0) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1233 | goto error; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1234 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1236 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1237 | if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | /* Give another thread a chance */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1239 | if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | Py_FatalError("ceval: tstate mix-up"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1241 | } |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1242 | drop_gil(ceval, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* Other threads may run now */ |
| 1245 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1246 | take_gil(ceval, tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1247 | |
| 1248 | /* Check if we should make a quick exit. */ |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 1249 | exit_thread_if_finalizing(runtime, tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1250 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1251 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | Py_FatalError("ceval: orphan tstate"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1253 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | } |
| 1255 | /* Check for asynchronous exceptions. */ |
| 1256 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1257 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | tstate->async_exc = NULL; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1259 | UNSIGNAL_ASYNC_EXC(ceval); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1260 | _PyErr_SetNone(tstate, exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1261 | Py_DECREF(exc); |
| 1262 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | fast_next_opcode: |
| 1267 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1268 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1269 | if (PyDTrace_LINE_ENABLED()) |
| 1270 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1273 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1274 | if (_Py_TracingPossible(ceval) && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1275 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1276 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | /* see maybe_call_line_trace |
| 1278 | for expository comments */ |
| 1279 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1282 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1283 | tstate, f, |
| 1284 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | /* Reload possibly changed frame fields */ |
| 1286 | JUMPTO(f->f_lasti); |
| 1287 | if (f->f_stacktop != NULL) { |
| 1288 | stack_pointer = f->f_stacktop; |
| 1289 | f->f_stacktop = NULL; |
| 1290 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1291 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1293 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1297 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1298 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1299 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1300 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1301 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | dxpairs[lastopcode][opcode]++; |
| 1303 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1304 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1306 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1308 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | if (lltrace) { |
| 1312 | if (HAS_ARG(opcode)) { |
| 1313 | printf("%d: %d, %d\n", |
| 1314 | f->f_lasti, opcode, oparg); |
| 1315 | } |
| 1316 | else { |
| 1317 | printf("%d: %d\n", |
| 1318 | f->f_lasti, opcode); |
| 1319 | } |
| 1320 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1321 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1326 | It is essential that any operation that fails must goto error |
| 1327 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1328 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1329 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1331 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1332 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1333 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1334 | PyObject *value = GETLOCAL(oparg); |
| 1335 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1336 | format_exc_check_arg(tstate, PyExc_UnboundLocalError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1337 | UNBOUNDLOCAL_ERROR_MSG, |
| 1338 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1339 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1341 | Py_INCREF(value); |
| 1342 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1344 | } |
| 1345 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1346 | case TARGET(LOAD_CONST): { |
| 1347 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1348 | PyObject *value = GETITEM(consts, oparg); |
| 1349 | Py_INCREF(value); |
| 1350 | PUSH(value); |
| 1351 | FAST_DISPATCH(); |
| 1352 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1353 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1354 | case TARGET(STORE_FAST): { |
| 1355 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1356 | PyObject *value = POP(); |
| 1357 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1359 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1360 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1361 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1362 | PyObject *value = POP(); |
| 1363 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1365 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1366 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1367 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1368 | PyObject *top = TOP(); |
| 1369 | PyObject *second = SECOND(); |
| 1370 | SET_TOP(second); |
| 1371 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1373 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1374 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1375 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1376 | PyObject *top = TOP(); |
| 1377 | PyObject *second = SECOND(); |
| 1378 | PyObject *third = THIRD(); |
| 1379 | SET_TOP(second); |
| 1380 | SET_SECOND(third); |
| 1381 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1383 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1384 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1385 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1386 | PyObject *top = TOP(); |
| 1387 | PyObject *second = SECOND(); |
| 1388 | PyObject *third = THIRD(); |
| 1389 | PyObject *fourth = FOURTH(); |
| 1390 | SET_TOP(second); |
| 1391 | SET_SECOND(third); |
| 1392 | SET_THIRD(fourth); |
| 1393 | SET_FOURTH(top); |
| 1394 | FAST_DISPATCH(); |
| 1395 | } |
| 1396 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1397 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1398 | PyObject *top = TOP(); |
| 1399 | Py_INCREF(top); |
| 1400 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1402 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1403 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1404 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1405 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1406 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1407 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1408 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1409 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1410 | SET_TOP(top); |
| 1411 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1412 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1413 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1414 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1415 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1416 | PyObject *value = TOP(); |
| 1417 | PyObject *res = PyNumber_Positive(value); |
| 1418 | Py_DECREF(value); |
| 1419 | SET_TOP(res); |
| 1420 | if (res == NULL) |
| 1421 | goto error; |
| 1422 | DISPATCH(); |
| 1423 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1424 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1425 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1426 | PyObject *value = TOP(); |
| 1427 | PyObject *res = PyNumber_Negative(value); |
| 1428 | Py_DECREF(value); |
| 1429 | SET_TOP(res); |
| 1430 | if (res == NULL) |
| 1431 | goto error; |
| 1432 | DISPATCH(); |
| 1433 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1434 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1435 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1436 | PyObject *value = TOP(); |
| 1437 | int err = PyObject_IsTrue(value); |
| 1438 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | if (err == 0) { |
| 1440 | Py_INCREF(Py_True); |
| 1441 | SET_TOP(Py_True); |
| 1442 | DISPATCH(); |
| 1443 | } |
| 1444 | else if (err > 0) { |
| 1445 | Py_INCREF(Py_False); |
| 1446 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | DISPATCH(); |
| 1448 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1449 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1450 | goto error; |
| 1451 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1452 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1453 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1454 | PyObject *value = TOP(); |
| 1455 | PyObject *res = PyNumber_Invert(value); |
| 1456 | Py_DECREF(value); |
| 1457 | SET_TOP(res); |
| 1458 | if (res == NULL) |
| 1459 | goto error; |
| 1460 | DISPATCH(); |
| 1461 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1462 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1463 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1464 | PyObject *exp = POP(); |
| 1465 | PyObject *base = TOP(); |
| 1466 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1467 | Py_DECREF(base); |
| 1468 | Py_DECREF(exp); |
| 1469 | SET_TOP(res); |
| 1470 | if (res == NULL) |
| 1471 | goto error; |
| 1472 | DISPATCH(); |
| 1473 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1474 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1475 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1476 | PyObject *right = POP(); |
| 1477 | PyObject *left = TOP(); |
| 1478 | PyObject *res = PyNumber_Multiply(left, right); |
| 1479 | Py_DECREF(left); |
| 1480 | Py_DECREF(right); |
| 1481 | SET_TOP(res); |
| 1482 | if (res == NULL) |
| 1483 | goto error; |
| 1484 | DISPATCH(); |
| 1485 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1486 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1487 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1488 | PyObject *right = POP(); |
| 1489 | PyObject *left = TOP(); |
| 1490 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1491 | Py_DECREF(left); |
| 1492 | Py_DECREF(right); |
| 1493 | SET_TOP(res); |
| 1494 | if (res == NULL) |
| 1495 | goto error; |
| 1496 | DISPATCH(); |
| 1497 | } |
| 1498 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1499 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1500 | PyObject *divisor = POP(); |
| 1501 | PyObject *dividend = TOP(); |
| 1502 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1503 | Py_DECREF(dividend); |
| 1504 | Py_DECREF(divisor); |
| 1505 | SET_TOP(quotient); |
| 1506 | if (quotient == NULL) |
| 1507 | goto error; |
| 1508 | DISPATCH(); |
| 1509 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1510 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1511 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1512 | PyObject *divisor = POP(); |
| 1513 | PyObject *dividend = TOP(); |
| 1514 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1515 | Py_DECREF(dividend); |
| 1516 | Py_DECREF(divisor); |
| 1517 | SET_TOP(quotient); |
| 1518 | if (quotient == NULL) |
| 1519 | goto error; |
| 1520 | DISPATCH(); |
| 1521 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1522 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1523 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1524 | PyObject *divisor = POP(); |
| 1525 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1526 | PyObject *res; |
| 1527 | if (PyUnicode_CheckExact(dividend) && ( |
| 1528 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1529 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1530 | // (see issue28598) |
| 1531 | res = PyUnicode_Format(dividend, divisor); |
| 1532 | } else { |
| 1533 | res = PyNumber_Remainder(dividend, divisor); |
| 1534 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1535 | Py_DECREF(divisor); |
| 1536 | Py_DECREF(dividend); |
| 1537 | SET_TOP(res); |
| 1538 | if (res == NULL) |
| 1539 | goto error; |
| 1540 | DISPATCH(); |
| 1541 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1542 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1543 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1544 | PyObject *right = POP(); |
| 1545 | PyObject *left = TOP(); |
| 1546 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1547 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1548 | CPython using bytecode, it is simply worthless. |
| 1549 | See http://bugs.python.org/issue21955 and |
| 1550 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1551 | no patch shown any impact on a realistic benchmark, only a minor |
| 1552 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1553 | if (PyUnicode_CheckExact(left) && |
| 1554 | PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1555 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1556 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1557 | } |
| 1558 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1559 | sum = PyNumber_Add(left, right); |
| 1560 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1561 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1562 | Py_DECREF(right); |
| 1563 | SET_TOP(sum); |
| 1564 | if (sum == NULL) |
| 1565 | goto error; |
| 1566 | DISPATCH(); |
| 1567 | } |
| 1568 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1569 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1570 | PyObject *right = POP(); |
| 1571 | PyObject *left = TOP(); |
| 1572 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1573 | Py_DECREF(right); |
| 1574 | Py_DECREF(left); |
| 1575 | SET_TOP(diff); |
| 1576 | if (diff == NULL) |
| 1577 | goto error; |
| 1578 | DISPATCH(); |
| 1579 | } |
| 1580 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1581 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1582 | PyObject *sub = POP(); |
| 1583 | PyObject *container = TOP(); |
| 1584 | PyObject *res = PyObject_GetItem(container, sub); |
| 1585 | Py_DECREF(container); |
| 1586 | Py_DECREF(sub); |
| 1587 | SET_TOP(res); |
| 1588 | if (res == NULL) |
| 1589 | goto error; |
| 1590 | DISPATCH(); |
| 1591 | } |
| 1592 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1593 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1594 | PyObject *right = POP(); |
| 1595 | PyObject *left = TOP(); |
| 1596 | PyObject *res = PyNumber_Lshift(left, right); |
| 1597 | Py_DECREF(left); |
| 1598 | Py_DECREF(right); |
| 1599 | SET_TOP(res); |
| 1600 | if (res == NULL) |
| 1601 | goto error; |
| 1602 | DISPATCH(); |
| 1603 | } |
| 1604 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1605 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1606 | PyObject *right = POP(); |
| 1607 | PyObject *left = TOP(); |
| 1608 | PyObject *res = PyNumber_Rshift(left, right); |
| 1609 | Py_DECREF(left); |
| 1610 | Py_DECREF(right); |
| 1611 | SET_TOP(res); |
| 1612 | if (res == NULL) |
| 1613 | goto error; |
| 1614 | DISPATCH(); |
| 1615 | } |
| 1616 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1617 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1618 | PyObject *right = POP(); |
| 1619 | PyObject *left = TOP(); |
| 1620 | PyObject *res = PyNumber_And(left, right); |
| 1621 | Py_DECREF(left); |
| 1622 | Py_DECREF(right); |
| 1623 | SET_TOP(res); |
| 1624 | if (res == NULL) |
| 1625 | goto error; |
| 1626 | DISPATCH(); |
| 1627 | } |
| 1628 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1629 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1630 | PyObject *right = POP(); |
| 1631 | PyObject *left = TOP(); |
| 1632 | PyObject *res = PyNumber_Xor(left, right); |
| 1633 | Py_DECREF(left); |
| 1634 | Py_DECREF(right); |
| 1635 | SET_TOP(res); |
| 1636 | if (res == NULL) |
| 1637 | goto error; |
| 1638 | DISPATCH(); |
| 1639 | } |
| 1640 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1641 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1642 | PyObject *right = POP(); |
| 1643 | PyObject *left = TOP(); |
| 1644 | PyObject *res = PyNumber_Or(left, right); |
| 1645 | Py_DECREF(left); |
| 1646 | Py_DECREF(right); |
| 1647 | SET_TOP(res); |
| 1648 | if (res == NULL) |
| 1649 | goto error; |
| 1650 | DISPATCH(); |
| 1651 | } |
| 1652 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1653 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1654 | PyObject *v = POP(); |
| 1655 | PyObject *list = PEEK(oparg); |
| 1656 | int err; |
| 1657 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1659 | if (err != 0) |
| 1660 | goto error; |
| 1661 | PREDICT(JUMP_ABSOLUTE); |
| 1662 | DISPATCH(); |
| 1663 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1664 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1665 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1666 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1667 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1668 | int err; |
| 1669 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1671 | if (err != 0) |
| 1672 | goto error; |
| 1673 | PREDICT(JUMP_ABSOLUTE); |
| 1674 | DISPATCH(); |
| 1675 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1676 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1677 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1678 | PyObject *exp = POP(); |
| 1679 | PyObject *base = TOP(); |
| 1680 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1681 | Py_DECREF(base); |
| 1682 | Py_DECREF(exp); |
| 1683 | SET_TOP(res); |
| 1684 | if (res == NULL) |
| 1685 | goto error; |
| 1686 | DISPATCH(); |
| 1687 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1688 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1689 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1690 | PyObject *right = POP(); |
| 1691 | PyObject *left = TOP(); |
| 1692 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1693 | Py_DECREF(left); |
| 1694 | Py_DECREF(right); |
| 1695 | SET_TOP(res); |
| 1696 | if (res == NULL) |
| 1697 | goto error; |
| 1698 | DISPATCH(); |
| 1699 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1700 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1701 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1702 | PyObject *right = POP(); |
| 1703 | PyObject *left = TOP(); |
| 1704 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1705 | Py_DECREF(left); |
| 1706 | Py_DECREF(right); |
| 1707 | SET_TOP(res); |
| 1708 | if (res == NULL) |
| 1709 | goto error; |
| 1710 | DISPATCH(); |
| 1711 | } |
| 1712 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1713 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1714 | PyObject *divisor = POP(); |
| 1715 | PyObject *dividend = TOP(); |
| 1716 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1717 | Py_DECREF(dividend); |
| 1718 | Py_DECREF(divisor); |
| 1719 | SET_TOP(quotient); |
| 1720 | if (quotient == NULL) |
| 1721 | goto error; |
| 1722 | DISPATCH(); |
| 1723 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1724 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1725 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1726 | PyObject *divisor = POP(); |
| 1727 | PyObject *dividend = TOP(); |
| 1728 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1729 | Py_DECREF(dividend); |
| 1730 | Py_DECREF(divisor); |
| 1731 | SET_TOP(quotient); |
| 1732 | if (quotient == NULL) |
| 1733 | goto error; |
| 1734 | DISPATCH(); |
| 1735 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1736 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1737 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1738 | PyObject *right = POP(); |
| 1739 | PyObject *left = TOP(); |
| 1740 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1741 | Py_DECREF(left); |
| 1742 | Py_DECREF(right); |
| 1743 | SET_TOP(mod); |
| 1744 | if (mod == NULL) |
| 1745 | goto error; |
| 1746 | DISPATCH(); |
| 1747 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1748 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1749 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1750 | PyObject *right = POP(); |
| 1751 | PyObject *left = TOP(); |
| 1752 | PyObject *sum; |
| 1753 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1754 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1755 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1756 | } |
| 1757 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1758 | sum = PyNumber_InPlaceAdd(left, right); |
| 1759 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1760 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1761 | Py_DECREF(right); |
| 1762 | SET_TOP(sum); |
| 1763 | if (sum == NULL) |
| 1764 | goto error; |
| 1765 | DISPATCH(); |
| 1766 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1767 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1768 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1769 | PyObject *right = POP(); |
| 1770 | PyObject *left = TOP(); |
| 1771 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1772 | Py_DECREF(left); |
| 1773 | Py_DECREF(right); |
| 1774 | SET_TOP(diff); |
| 1775 | if (diff == NULL) |
| 1776 | goto error; |
| 1777 | DISPATCH(); |
| 1778 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1779 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1780 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1781 | PyObject *right = POP(); |
| 1782 | PyObject *left = TOP(); |
| 1783 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1784 | Py_DECREF(left); |
| 1785 | Py_DECREF(right); |
| 1786 | SET_TOP(res); |
| 1787 | if (res == NULL) |
| 1788 | goto error; |
| 1789 | DISPATCH(); |
| 1790 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1791 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1792 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1793 | PyObject *right = POP(); |
| 1794 | PyObject *left = TOP(); |
| 1795 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1796 | Py_DECREF(left); |
| 1797 | Py_DECREF(right); |
| 1798 | SET_TOP(res); |
| 1799 | if (res == NULL) |
| 1800 | goto error; |
| 1801 | DISPATCH(); |
| 1802 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1803 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1804 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1805 | PyObject *right = POP(); |
| 1806 | PyObject *left = TOP(); |
| 1807 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1808 | Py_DECREF(left); |
| 1809 | Py_DECREF(right); |
| 1810 | SET_TOP(res); |
| 1811 | if (res == NULL) |
| 1812 | goto error; |
| 1813 | DISPATCH(); |
| 1814 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1815 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1816 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1817 | PyObject *right = POP(); |
| 1818 | PyObject *left = TOP(); |
| 1819 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1820 | Py_DECREF(left); |
| 1821 | Py_DECREF(right); |
| 1822 | SET_TOP(res); |
| 1823 | if (res == NULL) |
| 1824 | goto error; |
| 1825 | DISPATCH(); |
| 1826 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1827 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1828 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1829 | PyObject *right = POP(); |
| 1830 | PyObject *left = TOP(); |
| 1831 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1832 | Py_DECREF(left); |
| 1833 | Py_DECREF(right); |
| 1834 | SET_TOP(res); |
| 1835 | if (res == NULL) |
| 1836 | goto error; |
| 1837 | DISPATCH(); |
| 1838 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1839 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1840 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1841 | PyObject *sub = TOP(); |
| 1842 | PyObject *container = SECOND(); |
| 1843 | PyObject *v = THIRD(); |
| 1844 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1845 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1846 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1847 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1849 | Py_DECREF(container); |
| 1850 | Py_DECREF(sub); |
| 1851 | if (err != 0) |
| 1852 | goto error; |
| 1853 | DISPATCH(); |
| 1854 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1855 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1856 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1857 | PyObject *sub = TOP(); |
| 1858 | PyObject *container = SECOND(); |
| 1859 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1860 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1861 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1862 | err = PyObject_DelItem(container, sub); |
| 1863 | Py_DECREF(container); |
| 1864 | Py_DECREF(sub); |
| 1865 | if (err != 0) |
| 1866 | goto error; |
| 1867 | DISPATCH(); |
| 1868 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1869 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1870 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1871 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1872 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1873 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1874 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1875 | if (hook == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1876 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 1877 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1878 | Py_DECREF(value); |
| 1879 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1881 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1882 | Py_DECREF(value); |
| 1883 | if (res == NULL) |
| 1884 | goto error; |
| 1885 | Py_DECREF(res); |
| 1886 | DISPATCH(); |
| 1887 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1888 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1889 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1890 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | switch (oparg) { |
| 1892 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1893 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1894 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1896 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1897 | /* fall through */ |
| 1898 | case 0: |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1899 | if (do_raise(tstate, exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1900 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1901 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | break; |
| 1903 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1904 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 1905 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1908 | goto error; |
| 1909 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1910 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1911 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1913 | assert(f->f_iblock == 0); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 1914 | goto exit_returning; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1915 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1916 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1917 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1918 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1919 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1920 | PyObject *obj = TOP(); |
| 1921 | PyTypeObject *type = Py_TYPE(obj); |
| 1922 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1923 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1924 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1925 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1926 | |
| 1927 | if (getter != NULL) { |
| 1928 | iter = (*getter)(obj); |
| 1929 | Py_DECREF(obj); |
| 1930 | if (iter == NULL) { |
| 1931 | SET_TOP(NULL); |
| 1932 | goto error; |
| 1933 | } |
| 1934 | } |
| 1935 | else { |
| 1936 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1937 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1938 | "'async for' requires an object with " |
| 1939 | "__aiter__ method, got %.100s", |
| 1940 | type->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1941 | Py_DECREF(obj); |
| 1942 | goto error; |
| 1943 | } |
| 1944 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1945 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 1946 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1947 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1948 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1949 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1950 | "'async for' received an object from __aiter__ " |
| 1951 | "that does not implement __anext__: %.100s", |
| 1952 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1953 | Py_DECREF(iter); |
| 1954 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1955 | } |
| 1956 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1957 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1958 | DISPATCH(); |
| 1959 | } |
| 1960 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1961 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1962 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1963 | PyObject *next_iter = NULL; |
| 1964 | PyObject *awaitable = NULL; |
| 1965 | PyObject *aiter = TOP(); |
| 1966 | PyTypeObject *type = Py_TYPE(aiter); |
| 1967 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1968 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1969 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1970 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1971 | goto error; |
| 1972 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1973 | } else { |
| 1974 | if (type->tp_as_async != NULL){ |
| 1975 | getter = type->tp_as_async->am_anext; |
| 1976 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1977 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1978 | if (getter != NULL) { |
| 1979 | next_iter = (*getter)(aiter); |
| 1980 | if (next_iter == NULL) { |
| 1981 | goto error; |
| 1982 | } |
| 1983 | } |
| 1984 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1985 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1986 | "'async for' requires an iterator with " |
| 1987 | "__anext__ method, got %.100s", |
| 1988 | type->tp_name); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1989 | goto error; |
| 1990 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1991 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1992 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1993 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1994 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1995 | PyExc_TypeError, |
| 1996 | "'async for' received an invalid object " |
| 1997 | "from __anext__: %.100s", |
| 1998 | Py_TYPE(next_iter)->tp_name); |
| 1999 | |
| 2000 | Py_DECREF(next_iter); |
| 2001 | goto error; |
| 2002 | } else { |
| 2003 | Py_DECREF(next_iter); |
| 2004 | } |
| 2005 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2006 | |
| 2007 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2008 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2009 | DISPATCH(); |
| 2010 | } |
| 2011 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2012 | case TARGET(GET_AWAITABLE): { |
| 2013 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2014 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2015 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2016 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2017 | if (iter == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2018 | format_awaitable_error(tstate, Py_TYPE(iterable), |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2019 | _Py_OPCODE(next_instr[-2])); |
| 2020 | } |
| 2021 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2022 | Py_DECREF(iterable); |
| 2023 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2024 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2025 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2026 | if (yf != NULL) { |
| 2027 | /* `iter` is a coroutine object that is being |
| 2028 | awaited, `yf` is a pointer to the current awaitable |
| 2029 | being awaited on. */ |
| 2030 | Py_DECREF(yf); |
| 2031 | Py_CLEAR(iter); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2032 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2033 | "coroutine is being awaited already"); |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2034 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2035 | } |
| 2036 | } |
| 2037 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2038 | SET_TOP(iter); /* Even if it's NULL */ |
| 2039 | |
| 2040 | if (iter == NULL) { |
| 2041 | goto error; |
| 2042 | } |
| 2043 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2044 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2045 | DISPATCH(); |
| 2046 | } |
| 2047 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2048 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2049 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2050 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2051 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2052 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2053 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2054 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2055 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2056 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2057 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2058 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2059 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2060 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2061 | Py_DECREF(v); |
| 2062 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2063 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2064 | if (tstate->c_tracefunc != NULL |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2065 | && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2066 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2067 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2068 | if (err < 0) |
| 2069 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2070 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2071 | SET_TOP(val); |
| 2072 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2073 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2074 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2075 | f->f_stacktop = stack_pointer; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2076 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 2077 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 2078 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 2079 | goto exit_yielding; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2080 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2081 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2082 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2084 | |
| 2085 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2086 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2087 | Py_DECREF(retval); |
| 2088 | if (w == NULL) { |
| 2089 | retval = NULL; |
| 2090 | goto error; |
| 2091 | } |
| 2092 | retval = w; |
| 2093 | } |
| 2094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | f->f_stacktop = stack_pointer; |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 2096 | goto exit_yielding; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2097 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2098 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2099 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2100 | PyObject *type, *value, *traceback; |
| 2101 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2102 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2103 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2104 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2105 | "popped block is not an except handler"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2106 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2108 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 2109 | STACK_LEVEL() <= (b)->b_level + 4); |
| 2110 | exc_info = tstate->exc_info; |
| 2111 | type = exc_info->exc_type; |
| 2112 | value = exc_info->exc_value; |
| 2113 | traceback = exc_info->exc_traceback; |
| 2114 | exc_info->exc_type = POP(); |
| 2115 | exc_info->exc_value = POP(); |
| 2116 | exc_info->exc_traceback = POP(); |
| 2117 | Py_XDECREF(type); |
| 2118 | Py_XDECREF(value); |
| 2119 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2121 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2122 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2123 | case TARGET(POP_BLOCK): { |
| 2124 | PREDICTED(POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2125 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2127 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2128 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2129 | case TARGET(POP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2130 | /* If oparg is 0 at the top of the stack are 1 or 6 values: |
| 2131 | Either: |
| 2132 | - TOP = NULL or an integer |
| 2133 | or: |
| 2134 | - (TOP, SECOND, THIRD) = exc_info() |
| 2135 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 2136 | |
| 2137 | If oparg is 1 the value for 'return' was additionally pushed |
| 2138 | at the top of the stack. |
| 2139 | */ |
| 2140 | PyObject *res = NULL; |
| 2141 | if (oparg) { |
| 2142 | res = POP(); |
| 2143 | } |
| 2144 | PyObject *exc = POP(); |
| 2145 | if (exc == NULL || PyLong_CheckExact(exc)) { |
| 2146 | Py_XDECREF(exc); |
| 2147 | } |
| 2148 | else { |
| 2149 | Py_DECREF(exc); |
| 2150 | Py_DECREF(POP()); |
| 2151 | Py_DECREF(POP()); |
| 2152 | |
| 2153 | PyObject *type, *value, *traceback; |
| 2154 | _PyErr_StackItem *exc_info; |
| 2155 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2156 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2157 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2158 | "popped block is not an except handler"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2159 | Py_XDECREF(res); |
| 2160 | goto error; |
| 2161 | } |
| 2162 | assert(STACK_LEVEL() == (b)->b_level + 3); |
| 2163 | exc_info = tstate->exc_info; |
| 2164 | type = exc_info->exc_type; |
| 2165 | value = exc_info->exc_value; |
| 2166 | traceback = exc_info->exc_traceback; |
| 2167 | exc_info->exc_type = POP(); |
| 2168 | exc_info->exc_value = POP(); |
| 2169 | exc_info->exc_traceback = POP(); |
| 2170 | Py_XDECREF(type); |
| 2171 | Py_XDECREF(value); |
| 2172 | Py_XDECREF(traceback); |
| 2173 | } |
| 2174 | if (oparg) { |
| 2175 | PUSH(res); |
| 2176 | } |
| 2177 | DISPATCH(); |
| 2178 | } |
| 2179 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2180 | case TARGET(CALL_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2181 | PyObject *ret = PyLong_FromLong(INSTR_OFFSET()); |
| 2182 | if (ret == NULL) { |
| 2183 | goto error; |
| 2184 | } |
| 2185 | PUSH(ret); |
| 2186 | JUMPBY(oparg); |
| 2187 | FAST_DISPATCH(); |
| 2188 | } |
| 2189 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2190 | case TARGET(BEGIN_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2191 | /* Push NULL onto the stack for using it in END_FINALLY, |
| 2192 | POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH. |
| 2193 | */ |
| 2194 | PUSH(NULL); |
| 2195 | FAST_DISPATCH(); |
| 2196 | } |
| 2197 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2198 | case TARGET(END_FINALLY): { |
| 2199 | PREDICTED(END_FINALLY); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2200 | /* At the top of the stack are 1 or 6 values: |
| 2201 | Either: |
| 2202 | - TOP = NULL or an integer |
| 2203 | or: |
| 2204 | - (TOP, SECOND, THIRD) = exc_info() |
| 2205 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 2206 | */ |
| 2207 | PyObject *exc = POP(); |
| 2208 | if (exc == NULL) { |
| 2209 | FAST_DISPATCH(); |
| 2210 | } |
| 2211 | else if (PyLong_CheckExact(exc)) { |
| 2212 | int ret = _PyLong_AsInt(exc); |
| 2213 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2214 | if (ret == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2215 | goto error; |
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 | JUMPTO(ret); |
| 2218 | FAST_DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2220 | else { |
| 2221 | assert(PyExceptionClass_Check(exc)); |
| 2222 | PyObject *val = POP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2223 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2224 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2225 | goto exception_unwind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2227 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2228 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2229 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2230 | PyObject *exc = POP(); |
| 2231 | assert(PyExceptionClass_Check(exc)); |
| 2232 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2233 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2234 | assert(b->b_type == EXCEPT_HANDLER); |
| 2235 | Py_DECREF(exc); |
| 2236 | UNWIND_EXCEPT_HANDLER(b); |
| 2237 | Py_DECREF(POP()); |
| 2238 | JUMPBY(oparg); |
| 2239 | FAST_DISPATCH(); |
| 2240 | } |
| 2241 | else { |
| 2242 | PyObject *val = POP(); |
| 2243 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2244 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2245 | goto exception_unwind; |
| 2246 | } |
| 2247 | } |
| 2248 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2249 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2250 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2251 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2252 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2253 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2254 | bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2255 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2256 | if (!_PyErr_Occurred(tstate)) { |
| 2257 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2258 | "__build_class__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2259 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2260 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2261 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2262 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2263 | } |
| 2264 | else { |
| 2265 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2266 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2267 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2268 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2269 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2270 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
| 2271 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2272 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2274 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2276 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2277 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2278 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2279 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2280 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2281 | PyObject *name = GETITEM(names, oparg); |
| 2282 | PyObject *v = POP(); |
| 2283 | PyObject *ns = f->f_locals; |
| 2284 | int err; |
| 2285 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2286 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2287 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2289 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2291 | if (PyDict_CheckExact(ns)) |
| 2292 | err = PyDict_SetItem(ns, name, v); |
| 2293 | else |
| 2294 | err = PyObject_SetItem(ns, name, v); |
| 2295 | Py_DECREF(v); |
| 2296 | if (err != 0) |
| 2297 | goto error; |
| 2298 | DISPATCH(); |
| 2299 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2300 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2301 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2302 | PyObject *name = GETITEM(names, oparg); |
| 2303 | PyObject *ns = f->f_locals; |
| 2304 | int err; |
| 2305 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2306 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2307 | "no locals when deleting %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2308 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2310 | err = PyObject_DelItem(ns, name); |
| 2311 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2312 | format_exc_check_arg(tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2313 | NAME_ERROR_MSG, |
| 2314 | name); |
| 2315 | goto error; |
| 2316 | } |
| 2317 | DISPATCH(); |
| 2318 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2319 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2320 | case TARGET(UNPACK_SEQUENCE): { |
| 2321 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2322 | PyObject *seq = POP(), *item, **items; |
| 2323 | if (PyTuple_CheckExact(seq) && |
| 2324 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2325 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2327 | item = items[oparg]; |
| 2328 | Py_INCREF(item); |
| 2329 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2331 | } else if (PyList_CheckExact(seq) && |
| 2332 | PyList_GET_SIZE(seq) == oparg) { |
| 2333 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2335 | item = items[oparg]; |
| 2336 | Py_INCREF(item); |
| 2337 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2339 | } else if (unpack_iterable(tstate, seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2341 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | } else { |
| 2343 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2344 | Py_DECREF(seq); |
| 2345 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2347 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2348 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2350 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2351 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2352 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2353 | PyObject *seq = POP(); |
| 2354 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2355 | if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2356 | stack_pointer + totalargs)) { |
| 2357 | stack_pointer += totalargs; |
| 2358 | } else { |
| 2359 | Py_DECREF(seq); |
| 2360 | goto error; |
| 2361 | } |
| 2362 | Py_DECREF(seq); |
| 2363 | DISPATCH(); |
| 2364 | } |
| 2365 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2366 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2367 | PyObject *name = GETITEM(names, oparg); |
| 2368 | PyObject *owner = TOP(); |
| 2369 | PyObject *v = SECOND(); |
| 2370 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2371 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2372 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2374 | Py_DECREF(owner); |
| 2375 | if (err != 0) |
| 2376 | goto error; |
| 2377 | DISPATCH(); |
| 2378 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2379 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2380 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2381 | PyObject *name = GETITEM(names, oparg); |
| 2382 | PyObject *owner = POP(); |
| 2383 | int err; |
| 2384 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2385 | Py_DECREF(owner); |
| 2386 | if (err != 0) |
| 2387 | goto error; |
| 2388 | DISPATCH(); |
| 2389 | } |
| 2390 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2391 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2392 | PyObject *name = GETITEM(names, oparg); |
| 2393 | PyObject *v = POP(); |
| 2394 | int err; |
| 2395 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2397 | if (err != 0) |
| 2398 | goto error; |
| 2399 | DISPATCH(); |
| 2400 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2401 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2402 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2403 | PyObject *name = GETITEM(names, oparg); |
| 2404 | int err; |
| 2405 | err = PyDict_DelItem(f->f_globals, name); |
| 2406 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2407 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
| 2408 | format_exc_check_arg(tstate, PyExc_NameError, |
| 2409 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2410 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2411 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2412 | } |
| 2413 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2414 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2415 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2416 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2417 | PyObject *name = GETITEM(names, oparg); |
| 2418 | PyObject *locals = f->f_locals; |
| 2419 | PyObject *v; |
| 2420 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2421 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2422 | "no locals when loading %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2423 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2425 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2426 | v = PyDict_GetItemWithError(locals, name); |
| 2427 | if (v != NULL) { |
| 2428 | Py_INCREF(v); |
| 2429 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2430 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2431 | goto error; |
| 2432 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2433 | } |
| 2434 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2435 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2436 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2437 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2438 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2439 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2442 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2443 | v = PyDict_GetItemWithError(f->f_globals, name); |
| 2444 | if (v != NULL) { |
| 2445 | Py_INCREF(v); |
| 2446 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2447 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2448 | goto error; |
| 2449 | } |
| 2450 | else { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2451 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2452 | v = PyDict_GetItemWithError(f->f_builtins, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2453 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2454 | if (!_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2455 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2456 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2457 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2458 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2459 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2460 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2461 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2462 | } |
| 2463 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2464 | v = PyObject_GetItem(f->f_builtins, name); |
| 2465 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2466 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2467 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2468 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2469 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2470 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2471 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2472 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2473 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2476 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2478 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2479 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2480 | case TARGET(LOAD_GLOBAL): { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2481 | PyObject *name; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2482 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2483 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2484 | && PyDict_CheckExact(f->f_builtins)) |
| 2485 | { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2486 | OPCACHE_CHECK(); |
| 2487 | if (co_opcache != NULL && co_opcache->optimized > 0) { |
| 2488 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2489 | |
| 2490 | if (lg->globals_ver == |
| 2491 | ((PyDictObject *)f->f_globals)->ma_version_tag |
| 2492 | && lg->builtins_ver == |
| 2493 | ((PyDictObject *)f->f_builtins)->ma_version_tag) |
| 2494 | { |
| 2495 | PyObject *ptr = lg->ptr; |
| 2496 | OPCACHE_STAT_GLOBAL_HIT(); |
| 2497 | assert(ptr != NULL); |
| 2498 | Py_INCREF(ptr); |
| 2499 | PUSH(ptr); |
| 2500 | DISPATCH(); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2505 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2506 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2507 | name); |
| 2508 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2509 | if (!_PyErr_OCCURRED()) { |
| 2510 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2511 | * an exception if the key doesn't exist */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2512 | format_exc_check_arg(tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2513 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2514 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2515 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | } |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2517 | |
| 2518 | if (co_opcache != NULL) { |
| 2519 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2520 | |
| 2521 | if (co_opcache->optimized == 0) { |
| 2522 | /* Wasn't optimized before. */ |
| 2523 | OPCACHE_STAT_GLOBAL_OPT(); |
| 2524 | } else { |
| 2525 | OPCACHE_STAT_GLOBAL_MISS(); |
| 2526 | } |
| 2527 | |
| 2528 | co_opcache->optimized = 1; |
| 2529 | lg->globals_ver = |
| 2530 | ((PyDictObject *)f->f_globals)->ma_version_tag; |
| 2531 | lg->builtins_ver = |
| 2532 | ((PyDictObject *)f->f_builtins)->ma_version_tag; |
| 2533 | lg->ptr = v; /* borrowed */ |
| 2534 | } |
| 2535 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2536 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2537 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2538 | else { |
| 2539 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2540 | |
| 2541 | /* namespace 1: globals */ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2542 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2543 | v = PyObject_GetItem(f->f_globals, name); |
| 2544 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2545 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2546 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2547 | } |
| 2548 | _PyErr_Clear(tstate); |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2549 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2550 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2551 | v = PyObject_GetItem(f->f_builtins, name); |
| 2552 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2553 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2554 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2555 | tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2556 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2557 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2558 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2562 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2564 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2565 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2566 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2567 | PyObject *v = GETLOCAL(oparg); |
| 2568 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | SETLOCAL(oparg, NULL); |
| 2570 | DISPATCH(); |
| 2571 | } |
| 2572 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2573 | tstate, PyExc_UnboundLocalError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | UNBOUNDLOCAL_ERROR_MSG, |
| 2575 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2576 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2577 | goto error; |
| 2578 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2579 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2580 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2581 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2582 | PyObject *oldobj = PyCell_GET(cell); |
| 2583 | if (oldobj != NULL) { |
| 2584 | PyCell_SET(cell, NULL); |
| 2585 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2586 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2587 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2588 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2589 | goto error; |
| 2590 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2591 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2592 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2593 | PyObject *cell = freevars[oparg]; |
| 2594 | Py_INCREF(cell); |
| 2595 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2597 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2598 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2599 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2600 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2601 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2602 | assert(locals); |
| 2603 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2604 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2605 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2606 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2607 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2608 | value = PyDict_GetItemWithError(locals, name); |
| 2609 | if (value != NULL) { |
| 2610 | Py_INCREF(value); |
| 2611 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2612 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2613 | goto error; |
| 2614 | } |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2615 | } |
| 2616 | else { |
| 2617 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2618 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2619 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2620 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2621 | } |
| 2622 | _PyErr_Clear(tstate); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2623 | } |
| 2624 | } |
| 2625 | if (!value) { |
| 2626 | PyObject *cell = freevars[oparg]; |
| 2627 | value = PyCell_GET(cell); |
| 2628 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2629 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2630 | goto error; |
| 2631 | } |
| 2632 | Py_INCREF(value); |
| 2633 | } |
| 2634 | PUSH(value); |
| 2635 | DISPATCH(); |
| 2636 | } |
| 2637 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2638 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2639 | PyObject *cell = freevars[oparg]; |
| 2640 | PyObject *value = PyCell_GET(cell); |
| 2641 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2642 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2643 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2645 | Py_INCREF(value); |
| 2646 | PUSH(value); |
| 2647 | DISPATCH(); |
| 2648 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2649 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2650 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2651 | PyObject *v = POP(); |
| 2652 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2653 | PyObject *oldobj = PyCell_GET(cell); |
| 2654 | PyCell_SET(cell, v); |
| 2655 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2656 | DISPATCH(); |
| 2657 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2658 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2659 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2660 | PyObject *str; |
| 2661 | PyObject *empty = PyUnicode_New(0, 0); |
| 2662 | if (empty == NULL) { |
| 2663 | goto error; |
| 2664 | } |
| 2665 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2666 | Py_DECREF(empty); |
| 2667 | if (str == NULL) |
| 2668 | goto error; |
| 2669 | while (--oparg >= 0) { |
| 2670 | PyObject *item = POP(); |
| 2671 | Py_DECREF(item); |
| 2672 | } |
| 2673 | PUSH(str); |
| 2674 | DISPATCH(); |
| 2675 | } |
| 2676 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2677 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2678 | PyObject *tup = PyTuple_New(oparg); |
| 2679 | if (tup == NULL) |
| 2680 | goto error; |
| 2681 | while (--oparg >= 0) { |
| 2682 | PyObject *item = POP(); |
| 2683 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2684 | } |
| 2685 | PUSH(tup); |
| 2686 | DISPATCH(); |
| 2687 | } |
| 2688 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2689 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2690 | PyObject *list = PyList_New(oparg); |
| 2691 | if (list == NULL) |
| 2692 | goto error; |
| 2693 | while (--oparg >= 0) { |
| 2694 | PyObject *item = POP(); |
| 2695 | PyList_SET_ITEM(list, oparg, item); |
| 2696 | } |
| 2697 | PUSH(list); |
| 2698 | DISPATCH(); |
| 2699 | } |
| 2700 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2701 | case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL): |
| 2702 | case TARGET(BUILD_TUPLE_UNPACK): |
| 2703 | case TARGET(BUILD_LIST_UNPACK): { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2704 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2705 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2706 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2707 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2708 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2709 | if (sum == NULL) |
| 2710 | goto error; |
| 2711 | |
| 2712 | for (i = oparg; i > 0; i--) { |
| 2713 | PyObject *none_val; |
| 2714 | |
| 2715 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2716 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2717 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2718 | _PyErr_ExceptionMatches(tstate, PyExc_TypeError)) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2719 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2720 | check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i)); |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2721 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2722 | Py_DECREF(sum); |
| 2723 | goto error; |
| 2724 | } |
| 2725 | Py_DECREF(none_val); |
| 2726 | } |
| 2727 | |
| 2728 | if (convert_to_tuple) { |
| 2729 | return_value = PyList_AsTuple(sum); |
| 2730 | Py_DECREF(sum); |
| 2731 | if (return_value == NULL) |
| 2732 | goto error; |
| 2733 | } |
| 2734 | else { |
| 2735 | return_value = sum; |
| 2736 | } |
| 2737 | |
| 2738 | while (oparg--) |
| 2739 | Py_DECREF(POP()); |
| 2740 | PUSH(return_value); |
| 2741 | DISPATCH(); |
| 2742 | } |
| 2743 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2744 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2745 | PyObject *set = PySet_New(NULL); |
| 2746 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2747 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2748 | if (set == NULL) |
| 2749 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2750 | for (i = oparg; i > 0; i--) { |
| 2751 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2752 | if (err == 0) |
| 2753 | err = PySet_Add(set, item); |
| 2754 | Py_DECREF(item); |
| 2755 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2756 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2757 | if (err != 0) { |
| 2758 | Py_DECREF(set); |
| 2759 | goto error; |
| 2760 | } |
| 2761 | PUSH(set); |
| 2762 | DISPATCH(); |
| 2763 | } |
| 2764 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2765 | case TARGET(BUILD_SET_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2766 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2767 | PyObject *sum = PySet_New(NULL); |
| 2768 | if (sum == NULL) |
| 2769 | goto error; |
| 2770 | |
| 2771 | for (i = oparg; i > 0; i--) { |
| 2772 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2773 | Py_DECREF(sum); |
| 2774 | goto error; |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | while (oparg--) |
| 2779 | Py_DECREF(POP()); |
| 2780 | PUSH(sum); |
| 2781 | DISPATCH(); |
| 2782 | } |
| 2783 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2784 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2785 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2786 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2787 | if (map == NULL) |
| 2788 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2789 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2790 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2791 | PyObject *key = PEEK(2*i); |
| 2792 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2793 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2794 | if (err != 0) { |
| 2795 | Py_DECREF(map); |
| 2796 | goto error; |
| 2797 | } |
| 2798 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2799 | |
| 2800 | while (oparg--) { |
| 2801 | Py_DECREF(POP()); |
| 2802 | Py_DECREF(POP()); |
| 2803 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2804 | PUSH(map); |
| 2805 | DISPATCH(); |
| 2806 | } |
| 2807 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2808 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2809 | _Py_IDENTIFIER(__annotations__); |
| 2810 | int err; |
| 2811 | PyObject *ann_dict; |
| 2812 | if (f->f_locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2813 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2814 | "no locals found when setting up annotations"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2815 | goto error; |
| 2816 | } |
| 2817 | /* check if __annotations__ in locals()... */ |
| 2818 | if (PyDict_CheckExact(f->f_locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2819 | ann_dict = _PyDict_GetItemIdWithError(f->f_locals, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2820 | &PyId___annotations__); |
| 2821 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2822 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2823 | goto error; |
| 2824 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2825 | /* ...if not, create a new one */ |
| 2826 | ann_dict = PyDict_New(); |
| 2827 | if (ann_dict == NULL) { |
| 2828 | goto error; |
| 2829 | } |
| 2830 | err = _PyDict_SetItemId(f->f_locals, |
| 2831 | &PyId___annotations__, ann_dict); |
| 2832 | Py_DECREF(ann_dict); |
| 2833 | if (err != 0) { |
| 2834 | goto error; |
| 2835 | } |
| 2836 | } |
| 2837 | } |
| 2838 | else { |
| 2839 | /* do the same if locals() is not a dict */ |
| 2840 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2841 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2842 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2843 | } |
| 2844 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2845 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2846 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2847 | goto error; |
| 2848 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2849 | _PyErr_Clear(tstate); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2850 | ann_dict = PyDict_New(); |
| 2851 | if (ann_dict == NULL) { |
| 2852 | goto error; |
| 2853 | } |
| 2854 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2855 | Py_DECREF(ann_dict); |
| 2856 | if (err != 0) { |
| 2857 | goto error; |
| 2858 | } |
| 2859 | } |
| 2860 | else { |
| 2861 | Py_DECREF(ann_dict); |
| 2862 | } |
| 2863 | } |
| 2864 | DISPATCH(); |
| 2865 | } |
| 2866 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2867 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2868 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2869 | PyObject *map; |
| 2870 | PyObject *keys = TOP(); |
| 2871 | if (!PyTuple_CheckExact(keys) || |
| 2872 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2873 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2874 | "bad BUILD_CONST_KEY_MAP keys argument"); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2875 | goto error; |
| 2876 | } |
| 2877 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2878 | if (map == NULL) { |
| 2879 | goto error; |
| 2880 | } |
| 2881 | for (i = oparg; i > 0; i--) { |
| 2882 | int err; |
| 2883 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2884 | PyObject *value = PEEK(i + 1); |
| 2885 | err = PyDict_SetItem(map, key, value); |
| 2886 | if (err != 0) { |
| 2887 | Py_DECREF(map); |
| 2888 | goto error; |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | Py_DECREF(POP()); |
| 2893 | while (oparg--) { |
| 2894 | Py_DECREF(POP()); |
| 2895 | } |
| 2896 | PUSH(map); |
| 2897 | DISPATCH(); |
| 2898 | } |
| 2899 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2900 | case TARGET(BUILD_MAP_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2901 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2902 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2903 | if (sum == NULL) |
| 2904 | goto error; |
| 2905 | |
| 2906 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2907 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2908 | if (PyDict_Update(sum, arg) < 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2909 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 2910 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2911 | "'%.200s' object is not a mapping", |
| 2912 | arg->ob_type->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2913 | } |
| 2914 | Py_DECREF(sum); |
| 2915 | goto error; |
| 2916 | } |
| 2917 | } |
| 2918 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2919 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2920 | Py_DECREF(POP()); |
| 2921 | PUSH(sum); |
| 2922 | DISPATCH(); |
| 2923 | } |
| 2924 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2925 | case TARGET(BUILD_MAP_UNPACK_WITH_CALL): { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2926 | Py_ssize_t i; |
| 2927 | PyObject *sum = PyDict_New(); |
| 2928 | if (sum == NULL) |
| 2929 | goto error; |
| 2930 | |
| 2931 | for (i = oparg; i > 0; i--) { |
| 2932 | PyObject *arg = PEEK(i); |
| 2933 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2934 | Py_DECREF(sum); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2935 | format_kwargs_error(tstate, PEEK(2 + oparg), arg); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2936 | goto error; |
| 2937 | } |
| 2938 | } |
| 2939 | |
| 2940 | while (oparg--) |
| 2941 | Py_DECREF(POP()); |
| 2942 | PUSH(sum); |
| 2943 | DISPATCH(); |
| 2944 | } |
| 2945 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2946 | case TARGET(MAP_ADD): { |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 2947 | PyObject *value = TOP(); |
| 2948 | PyObject *key = SECOND(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2949 | PyObject *map; |
| 2950 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2951 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2952 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2953 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2954 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2955 | Py_DECREF(value); |
| 2956 | Py_DECREF(key); |
| 2957 | if (err != 0) |
| 2958 | goto error; |
| 2959 | PREDICT(JUMP_ABSOLUTE); |
| 2960 | DISPATCH(); |
| 2961 | } |
| 2962 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2963 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2964 | PyObject *name = GETITEM(names, oparg); |
| 2965 | PyObject *owner = TOP(); |
| 2966 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2967 | Py_DECREF(owner); |
| 2968 | SET_TOP(res); |
| 2969 | if (res == NULL) |
| 2970 | goto error; |
| 2971 | DISPATCH(); |
| 2972 | } |
| 2973 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2974 | case TARGET(COMPARE_OP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2975 | PyObject *right = POP(); |
| 2976 | PyObject *left = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2977 | PyObject *res = cmp_outcome(tstate, oparg, left, right); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2978 | Py_DECREF(left); |
| 2979 | Py_DECREF(right); |
| 2980 | SET_TOP(res); |
| 2981 | if (res == NULL) |
| 2982 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | PREDICT(POP_JUMP_IF_FALSE); |
| 2984 | PREDICT(POP_JUMP_IF_TRUE); |
| 2985 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2986 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2987 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2988 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2989 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2990 | PyObject *fromlist = POP(); |
| 2991 | PyObject *level = TOP(); |
| 2992 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2993 | res = import_name(tstate, f, name, fromlist, level); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2994 | Py_DECREF(level); |
| 2995 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2996 | SET_TOP(res); |
| 2997 | if (res == NULL) |
| 2998 | goto error; |
| 2999 | DISPATCH(); |
| 3000 | } |
| 3001 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3002 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3003 | PyObject *from = POP(), *locals; |
| 3004 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3005 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 3006 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3007 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3008 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3009 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3010 | locals = f->f_locals; |
| 3011 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3012 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3013 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3014 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3015 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3017 | err = import_all_from(tstate, locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3019 | Py_DECREF(from); |
| 3020 | if (err != 0) |
| 3021 | goto error; |
| 3022 | DISPATCH(); |
| 3023 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3024 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3025 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3026 | PyObject *name = GETITEM(names, oparg); |
| 3027 | PyObject *from = TOP(); |
| 3028 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3029 | res = import_from(tstate, from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3030 | PUSH(res); |
| 3031 | if (res == NULL) |
| 3032 | goto error; |
| 3033 | DISPATCH(); |
| 3034 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3035 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3036 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | JUMPBY(oparg); |
| 3038 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3039 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3040 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3041 | case TARGET(POP_JUMP_IF_FALSE): { |
| 3042 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3043 | PyObject *cond = POP(); |
| 3044 | int err; |
| 3045 | if (cond == Py_True) { |
| 3046 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | FAST_DISPATCH(); |
| 3048 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3049 | if (cond == Py_False) { |
| 3050 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | JUMPTO(oparg); |
| 3052 | FAST_DISPATCH(); |
| 3053 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3054 | err = PyObject_IsTrue(cond); |
| 3055 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 3057 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | else if (err == 0) |
| 3059 | JUMPTO(oparg); |
| 3060 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3061 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3063 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3064 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3065 | case TARGET(POP_JUMP_IF_TRUE): { |
| 3066 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3067 | PyObject *cond = POP(); |
| 3068 | int err; |
| 3069 | if (cond == Py_False) { |
| 3070 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | FAST_DISPATCH(); |
| 3072 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3073 | if (cond == Py_True) { |
| 3074 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | JUMPTO(oparg); |
| 3076 | FAST_DISPATCH(); |
| 3077 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3078 | err = PyObject_IsTrue(cond); |
| 3079 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | JUMPTO(oparg); |
| 3082 | } |
| 3083 | else if (err == 0) |
| 3084 | ; |
| 3085 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3086 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3088 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3089 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3090 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3091 | PyObject *cond = TOP(); |
| 3092 | int err; |
| 3093 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3094 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3095 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | FAST_DISPATCH(); |
| 3097 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3098 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | JUMPTO(oparg); |
| 3100 | FAST_DISPATCH(); |
| 3101 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3102 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3104 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3105 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 | } |
| 3107 | else if (err == 0) |
| 3108 | JUMPTO(oparg); |
| 3109 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3110 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3112 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3113 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3114 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3115 | PyObject *cond = TOP(); |
| 3116 | int err; |
| 3117 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3118 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3119 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | FAST_DISPATCH(); |
| 3121 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3122 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3123 | JUMPTO(oparg); |
| 3124 | FAST_DISPATCH(); |
| 3125 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3126 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | JUMPTO(oparg); |
| 3129 | } |
| 3130 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3131 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3132 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | } |
| 3134 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3135 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3137 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3138 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3139 | case TARGET(JUMP_ABSOLUTE): { |
| 3140 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3142 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 3144 | the per-loop checks for signals. By default, this should be turned-off |
| 3145 | because it prevents detection of a control-break in tight loops like |
| 3146 | "while 1: pass". Compile with this option turned-on when you need |
| 3147 | the speed-up and do not need break checking inside tight loops (ones |
| 3148 | that contain only instructions ending with FAST_DISPATCH). |
| 3149 | */ |
| 3150 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3151 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3153 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3154 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3155 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3156 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3158 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3159 | PyObject *iter = PyObject_GetIter(iterable); |
| 3160 | Py_DECREF(iterable); |
| 3161 | SET_TOP(iter); |
| 3162 | if (iter == NULL) |
| 3163 | goto error; |
| 3164 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3165 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3166 | DISPATCH(); |
| 3167 | } |
| 3168 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3169 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3170 | /* before: [obj]; after [getiter(obj)] */ |
| 3171 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3172 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3173 | if (PyCoro_CheckExact(iterable)) { |
| 3174 | /* `iterable` is a coroutine */ |
| 3175 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3176 | /* and it is used in a 'yield from' expression of a |
| 3177 | regular generator. */ |
| 3178 | Py_DECREF(iterable); |
| 3179 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3180 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3181 | "cannot 'yield from' a coroutine object " |
| 3182 | "in a non-coroutine generator"); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3183 | goto error; |
| 3184 | } |
| 3185 | } |
| 3186 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3187 | /* `iterable` is not a generator. */ |
| 3188 | iter = PyObject_GetIter(iterable); |
| 3189 | Py_DECREF(iterable); |
| 3190 | SET_TOP(iter); |
| 3191 | if (iter == NULL) |
| 3192 | goto error; |
| 3193 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3194 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3195 | DISPATCH(); |
| 3196 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3197 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3198 | case TARGET(FOR_ITER): { |
| 3199 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3201 | PyObject *iter = TOP(); |
| 3202 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 3203 | if (next != NULL) { |
| 3204 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | PREDICT(STORE_FAST); |
| 3206 | PREDICT(UNPACK_SEQUENCE); |
| 3207 | DISPATCH(); |
| 3208 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3209 | if (_PyErr_Occurred(tstate)) { |
| 3210 | if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3211 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3212 | } |
| 3213 | else if (tstate->c_tracefunc != NULL) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3214 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3215 | } |
| 3216 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | } |
| 3218 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3219 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3220 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3222 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3224 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3225 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3226 | case TARGET(SETUP_FINALLY): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | /* NOTE: If you add any new block-setup opcodes that |
| 3228 | are not try/except/finally handlers, you may need |
| 3229 | to update the PyGen_NeedsFinalizing() function. |
| 3230 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3231 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3232 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | STACK_LEVEL()); |
| 3234 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3235 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3236 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3237 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3238 | _Py_IDENTIFIER(__aexit__); |
| 3239 | _Py_IDENTIFIER(__aenter__); |
| 3240 | |
| 3241 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3242 | PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3243 | *enter; |
| 3244 | PyObject *res; |
| 3245 | if (exit == NULL) |
| 3246 | goto error; |
| 3247 | SET_TOP(exit); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3248 | enter = special_lookup(tstate, mgr, &PyId___aenter__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3249 | Py_DECREF(mgr); |
| 3250 | if (enter == NULL) |
| 3251 | goto error; |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3252 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3253 | Py_DECREF(enter); |
| 3254 | if (res == NULL) |
| 3255 | goto error; |
| 3256 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3257 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3258 | DISPATCH(); |
| 3259 | } |
| 3260 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3261 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3262 | PyObject *res = POP(); |
| 3263 | /* Setup the finally block before pushing the result |
| 3264 | of __aenter__ on the stack. */ |
| 3265 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3266 | STACK_LEVEL()); |
| 3267 | PUSH(res); |
| 3268 | DISPATCH(); |
| 3269 | } |
| 3270 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3271 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3272 | _Py_IDENTIFIER(__exit__); |
| 3273 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3274 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3275 | PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3276 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3277 | if (enter == NULL) { |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3278 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3279 | } |
| 3280 | PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3281 | if (exit == NULL) { |
| 3282 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3283 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3284 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3285 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3286 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3287 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3288 | Py_DECREF(enter); |
| 3289 | if (res == NULL) |
| 3290 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | /* Setup the finally block before pushing the result |
| 3292 | of __enter__ on the stack. */ |
| 3293 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3294 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3295 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3296 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | DISPATCH(); |
| 3298 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3299 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3300 | case TARGET(WITH_CLEANUP_START): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3301 | /* At the top of the stack are 1 or 6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3302 | how/why we entered the finally clause: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3303 | - TOP = NULL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | - (TOP, SECOND, THIRD) = exc_info() |
| 3305 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3306 | Below them is EXIT, the context.__exit__ or context.__aexit__ |
| 3307 | bound method. |
| 3308 | In the first case, we must call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | EXIT(None, None, None) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3310 | otherwise we must call |
| 3311 | EXIT(TOP, SECOND, THIRD) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3312 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3313 | In the first case, we remove EXIT from the |
| 3314 | stack, leaving TOP, and push TOP on the stack. |
| 3315 | Otherwise we shift the bottom 3 values of the |
| 3316 | stack down, replace the empty spot with NULL, and push |
| 3317 | None on the stack. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3318 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3319 | Finally we push the result of the call. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3321 | PyObject *stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3323 | PyObject *exc, *val, *tb, *res; |
| 3324 | |
| 3325 | val = tb = Py_None; |
| 3326 | exc = TOP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3327 | if (exc == NULL) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3328 | STACK_SHRINK(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3330 | SET_TOP(exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3331 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | } |
| 3333 | else { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3334 | assert(PyExceptionClass_Check(exc)); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3335 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3337 | val = SECOND(); |
| 3338 | tb = THIRD(); |
| 3339 | tp2 = FOURTH(); |
| 3340 | exc2 = PEEK(5); |
| 3341 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3343 | SET_VALUE(7, tb2); |
| 3344 | SET_VALUE(6, exc2); |
| 3345 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3347 | SET_FOURTH(NULL); |
| 3348 | /* We just shifted the stack down, so we have |
| 3349 | to tell the except handler block that the |
| 3350 | values are lower than it expects. */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3351 | assert(f->f_iblock > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3353 | assert(block->b_type == EXCEPT_HANDLER); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3354 | assert(block->b_level > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | block->b_level--; |
| 3356 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3357 | |
| 3358 | stack[0] = exc; |
| 3359 | stack[1] = val; |
| 3360 | stack[2] = tb; |
| 3361 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3363 | if (res == NULL) |
| 3364 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3365 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3366 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3367 | PUSH(exc); |
| 3368 | PUSH(res); |
| 3369 | PREDICT(WITH_CLEANUP_FINISH); |
| 3370 | DISPATCH(); |
| 3371 | } |
| 3372 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3373 | case TARGET(WITH_CLEANUP_FINISH): { |
| 3374 | PREDICTED(WITH_CLEANUP_FINISH); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3375 | /* TOP = the result of calling the context.__exit__ bound method |
| 3376 | SECOND = either None or exception type |
| 3377 | |
| 3378 | If SECOND is None below is NULL or the return address, |
| 3379 | otherwise below are 7 values representing an exception. |
| 3380 | */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3381 | PyObject *res = POP(); |
| 3382 | PyObject *exc = POP(); |
| 3383 | int err; |
| 3384 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3385 | if (exc != Py_None) |
| 3386 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | else |
| 3388 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3389 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3390 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3391 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3394 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | else if (err > 0) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3396 | /* There was an exception and a True return. |
| 3397 | * We must manually unwind the EXCEPT_HANDLER block |
| 3398 | * which was created when the exception was caught, |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 3399 | * otherwise the stack will be in an inconsistent state. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3400 | */ |
| 3401 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 3402 | assert(b->b_type == EXCEPT_HANDLER); |
| 3403 | UNWIND_EXCEPT_HANDLER(b); |
| 3404 | PUSH(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | } |
| 3406 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3407 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3409 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3410 | case TARGET(LOAD_METHOD): { |
Andrey | b021ba5 | 2019-04-29 14:33:26 +1000 | [diff] [blame] | 3411 | /* Designed to work in tandem with CALL_METHOD. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3412 | PyObject *name = GETITEM(names, oparg); |
| 3413 | PyObject *obj = TOP(); |
| 3414 | PyObject *meth = NULL; |
| 3415 | |
| 3416 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3417 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3418 | if (meth == NULL) { |
| 3419 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3420 | goto error; |
| 3421 | } |
| 3422 | |
| 3423 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3424 | /* We can bypass temporary bound method object. |
| 3425 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3426 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3427 | meth | self | arg1 | ... | argN |
| 3428 | */ |
| 3429 | SET_TOP(meth); |
| 3430 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3431 | } |
| 3432 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3433 | /* meth is not an unbound method (but a regular attr, or |
| 3434 | something was returned by a descriptor protocol). Set |
| 3435 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3436 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3437 | |
| 3438 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3439 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3440 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3441 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3442 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3443 | } |
| 3444 | DISPATCH(); |
| 3445 | } |
| 3446 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3447 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3448 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3449 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3450 | |
| 3451 | sp = stack_pointer; |
| 3452 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3453 | meth = PEEK(oparg + 2); |
| 3454 | if (meth == NULL) { |
| 3455 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3456 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3457 | |
| 3458 | Stack layout: |
| 3459 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3460 | ... | NULL | callable | arg1 | ... | argN |
| 3461 | ^- TOP() |
| 3462 | ^- (-oparg) |
| 3463 | ^- (-oparg-1) |
| 3464 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3465 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3466 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3467 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3468 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3469 | res = call_function(tstate, &sp, oparg, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3470 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3471 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3472 | } |
| 3473 | else { |
| 3474 | /* This is a method call. Stack layout: |
| 3475 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3476 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3477 | ^- TOP() |
| 3478 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3479 | ^- (-oparg-1) |
| 3480 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3481 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3482 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3483 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3484 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3485 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3486 | res = call_function(tstate, &sp, oparg + 1, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3487 | stack_pointer = sp; |
| 3488 | } |
| 3489 | |
| 3490 | PUSH(res); |
| 3491 | if (res == NULL) |
| 3492 | goto error; |
| 3493 | DISPATCH(); |
| 3494 | } |
| 3495 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3496 | case TARGET(CALL_FUNCTION): { |
| 3497 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3498 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3500 | res = call_function(tstate, &sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3502 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3503 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3504 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3505 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3506 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3508 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3509 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3510 | PyObject **sp, *res, *names; |
| 3511 | |
| 3512 | names = POP(); |
| 3513 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3515 | res = call_function(tstate, &sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3517 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3518 | Py_DECREF(names); |
| 3519 | |
| 3520 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3521 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3522 | } |
| 3523 | DISPATCH(); |
| 3524 | } |
| 3525 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3526 | case TARGET(CALL_FUNCTION_EX): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3527 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3528 | if (oparg & 0x01) { |
| 3529 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3530 | if (!PyDict_CheckExact(kwargs)) { |
| 3531 | PyObject *d = PyDict_New(); |
| 3532 | if (d == NULL) |
| 3533 | goto error; |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 3534 | if (_PyDict_MergeEx(d, kwargs, 2) < 0) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3535 | Py_DECREF(d); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3536 | format_kwargs_error(tstate, SECOND(), kwargs); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3537 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3538 | goto error; |
| 3539 | } |
| 3540 | Py_DECREF(kwargs); |
| 3541 | kwargs = d; |
| 3542 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3543 | assert(PyDict_CheckExact(kwargs)); |
| 3544 | } |
| 3545 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3546 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3547 | if (!PyTuple_CheckExact(callargs)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3548 | if (check_args_iterable(tstate, func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3549 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3550 | goto error; |
| 3551 | } |
| 3552 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3553 | if (callargs == NULL) { |
| 3554 | goto error; |
| 3555 | } |
| 3556 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3557 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3558 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3559 | result = do_call_core(tstate, func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3560 | Py_DECREF(func); |
| 3561 | Py_DECREF(callargs); |
| 3562 | Py_XDECREF(kwargs); |
| 3563 | |
| 3564 | SET_TOP(result); |
| 3565 | if (result == NULL) { |
| 3566 | goto error; |
| 3567 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3568 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3570 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3571 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3572 | PyObject *qualname = POP(); |
| 3573 | PyObject *codeobj = POP(); |
| 3574 | PyFunctionObject *func = (PyFunctionObject *) |
| 3575 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3576 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3577 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3578 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3579 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3580 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3582 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3583 | if (oparg & 0x08) { |
| 3584 | assert(PyTuple_CheckExact(TOP())); |
| 3585 | func ->func_closure = POP(); |
| 3586 | } |
| 3587 | if (oparg & 0x04) { |
| 3588 | assert(PyDict_CheckExact(TOP())); |
| 3589 | func->func_annotations = POP(); |
| 3590 | } |
| 3591 | if (oparg & 0x02) { |
| 3592 | assert(PyDict_CheckExact(TOP())); |
| 3593 | func->func_kwdefaults = POP(); |
| 3594 | } |
| 3595 | if (oparg & 0x01) { |
| 3596 | assert(PyTuple_CheckExact(TOP())); |
| 3597 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3599 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3600 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3601 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3603 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3604 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3605 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3607 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3609 | step = NULL; |
| 3610 | stop = POP(); |
| 3611 | start = TOP(); |
| 3612 | slice = PySlice_New(start, stop, step); |
| 3613 | Py_DECREF(start); |
| 3614 | Py_DECREF(stop); |
| 3615 | Py_XDECREF(step); |
| 3616 | SET_TOP(slice); |
| 3617 | if (slice == NULL) |
| 3618 | goto error; |
| 3619 | DISPATCH(); |
| 3620 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3621 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3622 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3623 | /* Handles f-string value formatting. */ |
| 3624 | PyObject *result; |
| 3625 | PyObject *fmt_spec; |
| 3626 | PyObject *value; |
| 3627 | PyObject *(*conv_fn)(PyObject *); |
| 3628 | int which_conversion = oparg & FVC_MASK; |
| 3629 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3630 | |
| 3631 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3632 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3633 | |
| 3634 | /* See if any conversion is specified. */ |
| 3635 | switch (which_conversion) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3636 | case FVC_NONE: conv_fn = NULL; break; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3637 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3638 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3639 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3640 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3641 | _PyErr_Format(tstate, PyExc_SystemError, |
| 3642 | "unexpected conversion flag %d", |
| 3643 | which_conversion); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3644 | goto error; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | /* If there's a conversion function, call it and replace |
| 3648 | value with that result. Otherwise, just use value, |
| 3649 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3650 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3651 | result = conv_fn(value); |
| 3652 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3653 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3654 | Py_XDECREF(fmt_spec); |
| 3655 | goto error; |
| 3656 | } |
| 3657 | value = result; |
| 3658 | } |
| 3659 | |
| 3660 | /* If value is a unicode object, and there's no fmt_spec, |
| 3661 | then we know the result of format(value) is value |
| 3662 | itself. In that case, skip calling format(). I plan to |
| 3663 | move this optimization in to PyObject_Format() |
| 3664 | itself. */ |
| 3665 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3666 | /* Do nothing, just transfer ownership to result. */ |
| 3667 | result = value; |
| 3668 | } else { |
| 3669 | /* Actually call format(). */ |
| 3670 | result = PyObject_Format(value, fmt_spec); |
| 3671 | Py_DECREF(value); |
| 3672 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3673 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3674 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3675 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3676 | } |
| 3677 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3678 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3679 | DISPATCH(); |
| 3680 | } |
| 3681 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3682 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3683 | int oldoparg = oparg; |
| 3684 | NEXTOPARG(); |
| 3685 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3686 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3687 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3688 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3689 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3690 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3691 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3692 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3693 | default: |
| 3694 | fprintf(stderr, |
| 3695 | "XXX lineno: %d, opcode: %d\n", |
| 3696 | PyFrame_GetLineNumber(f), |
| 3697 | opcode); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3698 | _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3699 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3701 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3702 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3703 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3704 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3705 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3706 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3707 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3708 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3709 | #ifdef NDEBUG |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3710 | if (!_PyErr_Occurred(tstate)) { |
| 3711 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3712 | "error return without exception set"); |
| 3713 | } |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3714 | #else |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3715 | assert(_PyErr_Occurred(tstate)); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3716 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3717 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3718 | /* Log traceback info. */ |
| 3719 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3720 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3721 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3722 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3723 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3724 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3725 | exception_unwind: |
| 3726 | /* Unwind stacks if an exception occurred */ |
| 3727 | while (f->f_iblock > 0) { |
| 3728 | /* Pop the current block. */ |
| 3729 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | if (b->b_type == EXCEPT_HANDLER) { |
| 3732 | UNWIND_EXCEPT_HANDLER(b); |
| 3733 | continue; |
| 3734 | } |
| 3735 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3736 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3737 | PyObject *exc, *val, *tb; |
| 3738 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3739 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3740 | /* Beware, this invalidates all b->b_* fields */ |
| 3741 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3742 | PUSH(exc_info->exc_traceback); |
| 3743 | PUSH(exc_info->exc_value); |
| 3744 | if (exc_info->exc_type != NULL) { |
| 3745 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3746 | } |
| 3747 | else { |
| 3748 | Py_INCREF(Py_None); |
| 3749 | PUSH(Py_None); |
| 3750 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3751 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | /* Make the raw exception data |
| 3753 | available to the handler, |
| 3754 | so a program can emulate the |
| 3755 | Python main loop. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3756 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3757 | if (tb != NULL) |
| 3758 | PyException_SetTraceback(val, tb); |
| 3759 | else |
| 3760 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3761 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3762 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3763 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3764 | exc_info->exc_value = val; |
| 3765 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | if (tb == NULL) |
| 3767 | tb = Py_None; |
| 3768 | Py_INCREF(tb); |
| 3769 | PUSH(tb); |
| 3770 | PUSH(val); |
| 3771 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | JUMPTO(handler); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3773 | /* Resume normal execution */ |
| 3774 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | } |
| 3776 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3777 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3778 | /* End the loop as we still have an error */ |
| 3779 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3781 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3782 | assert(retval == NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3783 | assert(_PyErr_Occurred(tstate)); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3784 | |
| 3785 | exit_returning: |
| 3786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3787 | /* Pop remaining stack entries. */ |
| 3788 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3789 | PyObject *o = POP(); |
| 3790 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3792 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3793 | exit_yielding: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3795 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3796 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3797 | tstate, f, PyTrace_RETURN, retval)) { |
| 3798 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3799 | } |
| 3800 | } |
| 3801 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3802 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 3803 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3804 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3809 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3810 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3811 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3812 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3814 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3815 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3816 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3817 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3820 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3821 | format_missing(PyThreadState *tstate, const char *kind, |
| 3822 | PyCodeObject *co, PyObject *names) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3823 | { |
| 3824 | int err; |
| 3825 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3826 | PyObject *name_str, *comma, *tail, *tmp; |
| 3827 | |
| 3828 | assert(PyList_CheckExact(names)); |
| 3829 | assert(len >= 1); |
| 3830 | /* Deal with the joys of natural language. */ |
| 3831 | switch (len) { |
| 3832 | case 1: |
| 3833 | name_str = PyList_GET_ITEM(names, 0); |
| 3834 | Py_INCREF(name_str); |
| 3835 | break; |
| 3836 | case 2: |
| 3837 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3838 | PyList_GET_ITEM(names, len - 2), |
| 3839 | PyList_GET_ITEM(names, len - 1)); |
| 3840 | break; |
| 3841 | default: |
| 3842 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3843 | PyList_GET_ITEM(names, len - 2), |
| 3844 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3845 | if (tail == NULL) |
| 3846 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3847 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3848 | fail, but we can't be too careful. */ |
| 3849 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3850 | if (err == -1) { |
| 3851 | Py_DECREF(tail); |
| 3852 | return; |
| 3853 | } |
| 3854 | /* Stitch everything up into a nice comma-separated list. */ |
| 3855 | comma = PyUnicode_FromString(", "); |
| 3856 | if (comma == NULL) { |
| 3857 | Py_DECREF(tail); |
| 3858 | return; |
| 3859 | } |
| 3860 | tmp = PyUnicode_Join(comma, names); |
| 3861 | Py_DECREF(comma); |
| 3862 | if (tmp == NULL) { |
| 3863 | Py_DECREF(tail); |
| 3864 | return; |
| 3865 | } |
| 3866 | name_str = PyUnicode_Concat(tmp, tail); |
| 3867 | Py_DECREF(tmp); |
| 3868 | Py_DECREF(tail); |
| 3869 | break; |
| 3870 | } |
| 3871 | if (name_str == NULL) |
| 3872 | return; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3873 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3874 | "%U() missing %i required %s argument%s: %U", |
| 3875 | co->co_name, |
| 3876 | len, |
| 3877 | kind, |
| 3878 | len == 1 ? "" : "s", |
| 3879 | name_str); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3880 | Py_DECREF(name_str); |
| 3881 | } |
| 3882 | |
| 3883 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3884 | missing_arguments(PyThreadState *tstate, PyCodeObject *co, |
| 3885 | Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3886 | PyObject **fastlocals) |
| 3887 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3888 | Py_ssize_t i, j = 0; |
| 3889 | Py_ssize_t start, end; |
| 3890 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3891 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3892 | PyObject *missing_names; |
| 3893 | |
| 3894 | /* Compute the names of the arguments that are missing. */ |
| 3895 | missing_names = PyList_New(missing); |
| 3896 | if (missing_names == NULL) |
| 3897 | return; |
| 3898 | if (positional) { |
| 3899 | start = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3900 | end = co->co_argcount - defcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3901 | } |
| 3902 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3903 | start = co->co_argcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3904 | end = start + co->co_kwonlyargcount; |
| 3905 | } |
| 3906 | for (i = start; i < end; i++) { |
| 3907 | if (GETLOCAL(i) == NULL) { |
| 3908 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3909 | PyObject *name = PyObject_Repr(raw); |
| 3910 | if (name == NULL) { |
| 3911 | Py_DECREF(missing_names); |
| 3912 | return; |
| 3913 | } |
| 3914 | PyList_SET_ITEM(missing_names, j++, name); |
| 3915 | } |
| 3916 | } |
| 3917 | assert(j == missing); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3918 | format_missing(tstate, kind, co, missing_names); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3919 | Py_DECREF(missing_names); |
| 3920 | } |
| 3921 | |
| 3922 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3923 | too_many_positional(PyThreadState *tstate, PyCodeObject *co, |
| 3924 | Py_ssize_t given, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3925 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3926 | { |
| 3927 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3928 | Py_ssize_t kwonly_given = 0; |
| 3929 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3930 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3931 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3932 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3933 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3934 | /* Count missing keyword-only args. */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3935 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3936 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3937 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3938 | } |
| 3939 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3940 | if (defcount) { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3941 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3942 | plural = 1; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3943 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3944 | } |
| 3945 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3946 | plural = (co_argcount != 1); |
| 3947 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3948 | } |
| 3949 | if (sig == NULL) |
| 3950 | return; |
| 3951 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3952 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3953 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3954 | given != 1 ? "s" : "", |
| 3955 | kwonly_given, |
| 3956 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3957 | if (kwonly_sig == NULL) { |
| 3958 | Py_DECREF(sig); |
| 3959 | return; |
| 3960 | } |
| 3961 | } |
| 3962 | else { |
| 3963 | /* This will not fail. */ |
| 3964 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3965 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3966 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3967 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3968 | "%U() takes %U positional argument%s but %zd%U %s given", |
| 3969 | co->co_name, |
| 3970 | sig, |
| 3971 | plural ? "s" : "", |
| 3972 | given, |
| 3973 | kwonly_sig, |
| 3974 | given == 1 && !kwonly_given ? "was" : "were"); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3975 | Py_DECREF(sig); |
| 3976 | Py_DECREF(kwonly_sig); |
| 3977 | } |
| 3978 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 3979 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3980 | positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co, |
| 3981 | Py_ssize_t kwcount, PyObject* const* kwnames) |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 3982 | { |
| 3983 | int posonly_conflicts = 0; |
| 3984 | PyObject* posonly_names = PyList_New(0); |
| 3985 | |
| 3986 | for(int k=0; k < co->co_posonlyargcount; k++){ |
| 3987 | PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k); |
| 3988 | |
| 3989 | for (int k2=0; k2<kwcount; k2++){ |
| 3990 | /* Compare the pointers first and fallback to PyObject_RichCompareBool*/ |
| 3991 | PyObject* kwname = kwnames[k2]; |
| 3992 | if (kwname == posonly_name){ |
| 3993 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 3994 | goto fail; |
| 3995 | } |
| 3996 | posonly_conflicts++; |
| 3997 | continue; |
| 3998 | } |
| 3999 | |
| 4000 | int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ); |
| 4001 | |
| 4002 | if ( cmp > 0) { |
| 4003 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 4004 | goto fail; |
| 4005 | } |
| 4006 | posonly_conflicts++; |
| 4007 | } else if (cmp < 0) { |
| 4008 | goto fail; |
| 4009 | } |
| 4010 | |
| 4011 | } |
| 4012 | } |
| 4013 | if (posonly_conflicts) { |
| 4014 | PyObject* comma = PyUnicode_FromString(", "); |
| 4015 | if (comma == NULL) { |
| 4016 | goto fail; |
| 4017 | } |
| 4018 | PyObject* error_names = PyUnicode_Join(comma, posonly_names); |
| 4019 | Py_DECREF(comma); |
| 4020 | if (error_names == NULL) { |
| 4021 | goto fail; |
| 4022 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4023 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4024 | "%U() got some positional-only arguments passed" |
| 4025 | " as keyword arguments: '%U'", |
| 4026 | co->co_name, error_names); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4027 | Py_DECREF(error_names); |
| 4028 | goto fail; |
| 4029 | } |
| 4030 | |
| 4031 | Py_DECREF(posonly_names); |
| 4032 | return 0; |
| 4033 | |
| 4034 | fail: |
| 4035 | Py_XDECREF(posonly_names); |
| 4036 | return 1; |
| 4037 | |
| 4038 | } |
| 4039 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4040 | /* 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] | 4041 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4042 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 4043 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4044 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4045 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4046 | PyObject *const *args, Py_ssize_t argcount, |
| 4047 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4048 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4049 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4050 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4051 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4052 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4053 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4054 | PyFrameObject *f; |
| 4055 | PyObject *retval = NULL; |
| 4056 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4057 | PyObject *x, *u; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4058 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4059 | Py_ssize_t i, j, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4060 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4061 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4062 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4063 | assert(tstate != NULL); |
| 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | if (globals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4066 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 4067 | "PyEval_EvalCodeEx: NULL globals"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | return NULL; |
| 4069 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4070 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4071 | /* Create the frame */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4072 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4073 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4074 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4075 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4076 | fastlocals = f->f_localsplus; |
| 4077 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4078 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4079 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4080 | if (co->co_flags & CO_VARKEYWORDS) { |
| 4081 | kwdict = PyDict_New(); |
| 4082 | if (kwdict == NULL) |
| 4083 | goto fail; |
| 4084 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4085 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4086 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4087 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4088 | SETLOCAL(i, kwdict); |
| 4089 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4090 | else { |
| 4091 | kwdict = NULL; |
| 4092 | } |
| 4093 | |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4094 | /* Copy all positional arguments into local variables */ |
| 4095 | if (argcount > co->co_argcount) { |
| 4096 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4097 | } |
| 4098 | else { |
| 4099 | n = argcount; |
| 4100 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4101 | for (j = 0; j < n; j++) { |
| 4102 | x = args[j]; |
| 4103 | Py_INCREF(x); |
| 4104 | SETLOCAL(j, x); |
| 4105 | } |
| 4106 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4107 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4108 | if (co->co_flags & CO_VARARGS) { |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 4109 | u = _PyTuple_FromArray(args + n, argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4110 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4111 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4112 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4113 | SETLOCAL(total_args, u); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4114 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4115 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4116 | /* Handle keyword arguments passed as two strided arrays */ |
| 4117 | kwcount *= kwstep; |
| 4118 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4119 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4120 | PyObject *keyword = kwnames[i]; |
| 4121 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4122 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4123 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4124 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4125 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4126 | "%U() keywords must be strings", |
| 4127 | co->co_name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4128 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4129 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4130 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4131 | /* Speed hack: do raw pointer compares. As names are |
| 4132 | normally interned this should almost always hit. */ |
| 4133 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4134 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4135 | PyObject *name = co_varnames[j]; |
| 4136 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4137 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4138 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4139 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4140 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4141 | /* Slow fallback, just in case */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4142 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4143 | PyObject *name = co_varnames[j]; |
| 4144 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 4145 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4146 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4147 | } |
| 4148 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4149 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4150 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4151 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4152 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 4153 | assert(j >= total_args); |
| 4154 | if (kwdict == NULL) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4155 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4156 | if (co->co_posonlyargcount |
| 4157 | && positional_only_passed_as_keyword(tstate, co, |
| 4158 | kwcount, kwnames)) |
| 4159 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4160 | goto fail; |
| 4161 | } |
| 4162 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4163 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4164 | "%U() got an unexpected keyword argument '%S'", |
| 4165 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4166 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4167 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4168 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4169 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4170 | goto fail; |
| 4171 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4172 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4173 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4174 | kw_found: |
| 4175 | if (GETLOCAL(j) != NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4176 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4177 | "%U() got multiple values for argument '%S'", |
| 4178 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4179 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4181 | Py_INCREF(value); |
| 4182 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4183 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4184 | |
| 4185 | /* Check the number of positional arguments */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4186 | if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4187 | too_many_positional(tstate, co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 | goto fail; |
| 4189 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4190 | |
| 4191 | /* Add missing positional arguments (copy default values from defs) */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4192 | if (argcount < co->co_argcount) { |
| 4193 | Py_ssize_t m = co->co_argcount - defcount; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4194 | Py_ssize_t missing = 0; |
| 4195 | for (i = argcount; i < m; i++) { |
| 4196 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4197 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4198 | } |
| 4199 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4200 | if (missing) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4201 | missing_arguments(tstate, co, missing, defcount, fastlocals); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4202 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4203 | } |
| 4204 | if (n > m) |
| 4205 | i = n - m; |
| 4206 | else |
| 4207 | i = 0; |
| 4208 | for (; i < defcount; i++) { |
| 4209 | if (GETLOCAL(m+i) == NULL) { |
| 4210 | PyObject *def = defs[i]; |
| 4211 | Py_INCREF(def); |
| 4212 | SETLOCAL(m+i, def); |
| 4213 | } |
| 4214 | } |
| 4215 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4216 | |
| 4217 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4218 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4219 | Py_ssize_t missing = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4220 | for (i = co->co_argcount; i < total_args; i++) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4221 | PyObject *name; |
| 4222 | if (GETLOCAL(i) != NULL) |
| 4223 | continue; |
| 4224 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4225 | if (kwdefs != NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4226 | PyObject *def = PyDict_GetItemWithError(kwdefs, name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4227 | if (def) { |
| 4228 | Py_INCREF(def); |
| 4229 | SETLOCAL(i, def); |
| 4230 | continue; |
| 4231 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4232 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4233 | goto fail; |
| 4234 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4235 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4236 | missing++; |
| 4237 | } |
| 4238 | if (missing) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4239 | missing_arguments(tstate, co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4240 | goto fail; |
| 4241 | } |
| 4242 | } |
| 4243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4244 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4245 | vars into frame. */ |
| 4246 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 4248 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4249 | /* Possibly account for the cell variable being an argument. */ |
| 4250 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4251 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4252 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4253 | /* Clear the local copy. */ |
| 4254 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4255 | } |
| 4256 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4257 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4258 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4259 | if (c == NULL) |
| 4260 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4261 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4262 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4263 | |
| 4264 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4265 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4266 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4267 | Py_INCREF(o); |
| 4268 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4269 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4270 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4271 | /* Handle generator/coroutine/asynchronous generator */ |
| 4272 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4273 | PyObject *gen; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4274 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4276 | /* Don't need to keep the reference to f_back, it will be set |
| 4277 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4278 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4280 | /* Create a new generator that owns the ready to run frame |
| 4281 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4282 | if (is_coro) { |
| 4283 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4284 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4285 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4286 | } else { |
| 4287 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4288 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4289 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4290 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4291 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 4292 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4293 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4294 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4295 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4296 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4297 | |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 4298 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4299 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4300 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4303 | which can call back into Python. While we're done with the |
| 4304 | current Python frame (f), the associated C stack is still in use, |
| 4305 | so recursion_depth must be boosted for the duration. |
| 4306 | */ |
| 4307 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4308 | if (Py_REFCNT(f) > 1) { |
| 4309 | Py_DECREF(f); |
| 4310 | _PyObject_GC_TRACK(f); |
| 4311 | } |
| 4312 | else { |
| 4313 | ++tstate->recursion_depth; |
| 4314 | Py_DECREF(f); |
| 4315 | --tstate->recursion_depth; |
| 4316 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4320 | PyObject * |
| 4321 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4322 | PyObject *const *args, int argcount, |
| 4323 | PyObject *const *kws, int kwcount, |
| 4324 | PyObject *const *defs, int defcount, |
| 4325 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4326 | { |
| 4327 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4328 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 4329 | kws, kws != NULL ? kws + 1 : NULL, |
| 4330 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4331 | defs, defcount, |
| 4332 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4333 | NULL, NULL); |
| 4334 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4335 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4336 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4337 | special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4340 | res = _PyObject_LookupSpecial(o, id); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4341 | if (res == NULL && !_PyErr_Occurred(tstate)) { |
| 4342 | _PyErr_SetObject(tstate, PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4343 | return NULL; |
| 4344 | } |
| 4345 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4349 | /* Logic for the raise statement (too complicated for inlining). |
| 4350 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4351 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4352 | do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | if (exc == NULL) { |
| 4357 | /* Reraise */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4358 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4359 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4360 | type = exc_info->exc_type; |
| 4361 | value = exc_info->exc_value; |
| 4362 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4363 | if (type == Py_None || type == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4364 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 4365 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4366 | return 0; |
| 4367 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | Py_XINCREF(type); |
| 4369 | Py_XINCREF(value); |
| 4370 | Py_XINCREF(tb); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4371 | _PyErr_Restore(tstate, type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4372 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4373 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | /* We support the following forms of raise: |
| 4376 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4377 | raise <instance> |
| 4378 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | if (PyExceptionClass_Check(exc)) { |
| 4381 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4382 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | if (value == NULL) |
| 4384 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4385 | if (!PyExceptionInstance_Check(value)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4386 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4387 | "calling %R should have returned an instance of " |
| 4388 | "BaseException, not %R", |
| 4389 | type, Py_TYPE(value)); |
| 4390 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4391 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4392 | } |
| 4393 | else if (PyExceptionInstance_Check(exc)) { |
| 4394 | value = exc; |
| 4395 | type = PyExceptionInstance_Class(exc); |
| 4396 | Py_INCREF(type); |
| 4397 | } |
| 4398 | else { |
| 4399 | /* Not something you can raise. You get an exception |
| 4400 | anyway, just not what you specified :-) */ |
| 4401 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4402 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4403 | "exceptions must derive from BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | goto raise_error; |
| 4405 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4406 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4407 | assert(type != NULL); |
| 4408 | assert(value != NULL); |
| 4409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | if (cause) { |
| 4411 | PyObject *fixed_cause; |
| 4412 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4413 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | if (fixed_cause == NULL) |
| 4415 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4416 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4417 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4418 | else if (PyExceptionInstance_Check(cause)) { |
| 4419 | fixed_cause = cause; |
| 4420 | } |
| 4421 | else if (cause == Py_None) { |
| 4422 | Py_DECREF(cause); |
| 4423 | fixed_cause = NULL; |
| 4424 | } |
| 4425 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4426 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4427 | "exception causes must derive from " |
| 4428 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4429 | goto raise_error; |
| 4430 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4431 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4433 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4434 | _PyErr_SetObject(tstate, type, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4436 | Py_DECREF(value); |
| 4437 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4438 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4439 | |
| 4440 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | Py_XDECREF(value); |
| 4442 | Py_XDECREF(type); |
| 4443 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4444 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4447 | /* 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] | 4448 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4449 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4450 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4451 | with a variable target. |
| 4452 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4453 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4454 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4455 | unpack_iterable(PyThreadState *tstate, PyObject *v, |
| 4456 | int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4458 | int i = 0, j = 0; |
| 4459 | Py_ssize_t ll = 0; |
| 4460 | PyObject *it; /* iter(v) */ |
| 4461 | PyObject *w; |
| 4462 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4466 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4467 | if (it == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4468 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4469 | v->ob_type->tp_iter == NULL && !PySequence_Check(v)) |
| 4470 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4471 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4472 | "cannot unpack non-iterable %.200s object", |
| 4473 | v->ob_type->tp_name); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4474 | } |
| 4475 | return 0; |
| 4476 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | for (; i < argcnt; i++) { |
| 4479 | w = PyIter_Next(it); |
| 4480 | if (w == NULL) { |
| 4481 | /* Iterator done, via error or exhaustion. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4482 | if (!_PyErr_Occurred(tstate)) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4483 | if (argcntafter == -1) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4484 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4485 | "not enough values to unpack " |
| 4486 | "(expected %d, got %d)", |
| 4487 | argcnt, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4488 | } |
| 4489 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4490 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4491 | "not enough values to unpack " |
| 4492 | "(expected at least %d, got %d)", |
| 4493 | argcnt + argcntafter, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4494 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4495 | } |
| 4496 | goto Error; |
| 4497 | } |
| 4498 | *--sp = w; |
| 4499 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | if (argcntafter == -1) { |
| 4502 | /* We better have exhausted the iterator now. */ |
| 4503 | w = PyIter_Next(it); |
| 4504 | if (w == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4505 | if (_PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | goto Error; |
| 4507 | Py_DECREF(it); |
| 4508 | return 1; |
| 4509 | } |
| 4510 | Py_DECREF(w); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4511 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4512 | "too many values to unpack (expected %d)", |
| 4513 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | goto Error; |
| 4515 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | l = PySequence_List(it); |
| 4518 | if (l == NULL) |
| 4519 | goto Error; |
| 4520 | *--sp = l; |
| 4521 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | ll = PyList_GET_SIZE(l); |
| 4524 | if (ll < argcntafter) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4525 | _PyErr_Format(tstate, PyExc_ValueError, |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4526 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4527 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | goto Error; |
| 4529 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4531 | /* Pop the "after-variable" args off the list. */ |
| 4532 | for (j = argcntafter; j > 0; j--, i++) { |
| 4533 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4534 | } |
| 4535 | /* Resize the list. */ |
| 4536 | Py_SIZE(l) = ll - argcntafter; |
| 4537 | Py_DECREF(it); |
| 4538 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4539 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4540 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | for (; i > 0; i--, sp++) |
| 4542 | Py_DECREF(*sp); |
| 4543 | Py_XDECREF(it); |
| 4544 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4548 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4549 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4550 | prtrace(PyThreadState *tstate, PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4551 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | printf("%s ", str); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4553 | if (PyObject_Print(v, stdout, 0) != 0) { |
| 4554 | /* Don't know what else to do */ |
| 4555 | _PyErr_Clear(tstate); |
| 4556 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | printf("\n"); |
| 4558 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4559 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4560 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4561 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4562 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4563 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4564 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4565 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4566 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4568 | _PyErr_Fetch(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4569 | if (value == NULL) { |
| 4570 | value = Py_None; |
| 4571 | Py_INCREF(value); |
| 4572 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4573 | _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4574 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4576 | if (arg == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4577 | _PyErr_Restore(tstate, type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | return; |
| 4579 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4580 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | Py_DECREF(arg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4582 | if (err == 0) { |
| 4583 | _PyErr_Restore(tstate, type, value, orig_traceback); |
| 4584 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | else { |
| 4586 | Py_XDECREF(type); |
| 4587 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4588 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4592 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4593 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4594 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4595 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | PyObject *type, *value, *traceback; |
| 4598 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4599 | _PyErr_Fetch(tstate, &type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4600 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | if (err == 0) |
| 4602 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4603 | _PyErr_Restore(tstate, type, value, traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | return 0; |
| 4605 | } |
| 4606 | else { |
| 4607 | Py_XDECREF(type); |
| 4608 | Py_XDECREF(value); |
| 4609 | Py_XDECREF(traceback); |
| 4610 | return -1; |
| 4611 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4614 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4615 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4616 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | int result; |
| 4620 | if (tstate->tracing) |
| 4621 | return 0; |
| 4622 | tstate->tracing++; |
| 4623 | tstate->use_tracing = 0; |
| 4624 | result = func(obj, frame, what, arg); |
| 4625 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4626 | || (tstate->c_profilefunc != NULL)); |
| 4627 | tstate->tracing--; |
| 4628 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4631 | PyObject * |
| 4632 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4633 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4634 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | int save_tracing = tstate->tracing; |
| 4636 | int save_use_tracing = tstate->use_tracing; |
| 4637 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4639 | tstate->tracing = 0; |
| 4640 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4641 | || (tstate->c_profilefunc != NULL)); |
| 4642 | result = PyObject_Call(func, args, NULL); |
| 4643 | tstate->tracing = save_tracing; |
| 4644 | tstate->use_tracing = save_use_tracing; |
| 4645 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4648 | /* 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] | 4649 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4650 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4651 | PyThreadState *tstate, PyFrameObject *frame, |
| 4652 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | int result = 0; |
| 4655 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4657 | /* If the last instruction executed isn't in the current |
| 4658 | instruction window, reset the window. |
| 4659 | */ |
| 4660 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4661 | PyAddrPair bounds; |
| 4662 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4663 | &bounds); |
| 4664 | *instr_lb = bounds.ap_lower; |
| 4665 | *instr_ub = bounds.ap_upper; |
| 4666 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4667 | /* If the last instruction falls at the start of a line or if it |
| 4668 | represents a jump backwards, update the frame's line number and |
| 4669 | then call the trace function if we're tracing source lines. |
| 4670 | */ |
| 4671 | if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | frame->f_lineno = line; |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4673 | if (frame->f_trace_lines) { |
| 4674 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 4675 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 4677 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 4678 | if (frame->f_trace_opcodes) { |
| 4679 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 4680 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | *instr_prev = frame->f_lasti; |
| 4682 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4685 | void |
| 4686 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4687 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4688 | if (PySys_Audit("sys.setprofile", NULL) < 0) { |
| 4689 | return; |
| 4690 | } |
| 4691 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4692 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | PyObject *temp = tstate->c_profileobj; |
| 4694 | Py_XINCREF(arg); |
| 4695 | tstate->c_profilefunc = NULL; |
| 4696 | tstate->c_profileobj = NULL; |
| 4697 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4698 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4699 | Py_XDECREF(temp); |
| 4700 | tstate->c_profilefunc = func; |
| 4701 | tstate->c_profileobj = arg; |
| 4702 | /* Flag that tracing or profiling is turned on */ |
| 4703 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | void |
| 4707 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4708 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4709 | if (PySys_Audit("sys.settrace", NULL) < 0) { |
| 4710 | return; |
| 4711 | } |
| 4712 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4713 | _PyRuntimeState *runtime = &_PyRuntime; |
| 4714 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4715 | PyObject *temp = tstate->c_traceobj; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4716 | runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | Py_XINCREF(arg); |
| 4718 | tstate->c_tracefunc = NULL; |
| 4719 | tstate->c_traceobj = NULL; |
| 4720 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4721 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4722 | Py_XDECREF(temp); |
| 4723 | tstate->c_tracefunc = func; |
| 4724 | tstate->c_traceobj = arg; |
| 4725 | /* Flag that tracing or profiling is turned on */ |
| 4726 | tstate->use_tracing = ((func != NULL) |
| 4727 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4730 | void |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4731 | _PyEval_SetCoroutineOriginTrackingDepth(int new_depth) |
| 4732 | { |
| 4733 | assert(new_depth >= 0); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4734 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4735 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 4736 | } |
| 4737 | |
| 4738 | int |
| 4739 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 4740 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4741 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4742 | return tstate->coroutine_origin_tracking_depth; |
| 4743 | } |
| 4744 | |
| 4745 | void |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4746 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4747 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4748 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4749 | |
| 4750 | if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) { |
| 4751 | return; |
| 4752 | } |
| 4753 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4754 | Py_XINCREF(firstiter); |
| 4755 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4756 | } |
| 4757 | |
| 4758 | PyObject * |
| 4759 | _PyEval_GetAsyncGenFirstiter(void) |
| 4760 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4761 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4762 | return tstate->async_gen_firstiter; |
| 4763 | } |
| 4764 | |
| 4765 | void |
| 4766 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4767 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4768 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4769 | |
| 4770 | if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) { |
| 4771 | return; |
| 4772 | } |
| 4773 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4774 | Py_XINCREF(finalizer); |
| 4775 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4776 | } |
| 4777 | |
| 4778 | PyObject * |
| 4779 | _PyEval_GetAsyncGenFinalizer(void) |
| 4780 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4781 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4782 | return tstate->async_gen_finalizer; |
| 4783 | } |
| 4784 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4785 | static PyFrameObject * |
| 4786 | _PyEval_GetFrame(PyThreadState *tstate) |
| 4787 | { |
| 4788 | return _PyRuntime.gilstate.getframe(tstate); |
| 4789 | } |
| 4790 | |
| 4791 | PyFrameObject * |
| 4792 | PyEval_GetFrame(void) |
| 4793 | { |
| 4794 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4795 | return _PyEval_GetFrame(tstate); |
| 4796 | } |
| 4797 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4798 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4799 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4800 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4801 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4802 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | if (current_frame == NULL) |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4804 | return tstate->interp->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | else |
| 4806 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4809 | /* Convenience function to get a builtin from its name */ |
| 4810 | PyObject * |
| 4811 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 4812 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4813 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4814 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 4815 | if (attr) { |
| 4816 | Py_INCREF(attr); |
| 4817 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4818 | else if (!_PyErr_Occurred(tstate)) { |
| 4819 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name)); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4820 | } |
| 4821 | return attr; |
| 4822 | } |
| 4823 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4824 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4825 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4826 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4827 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4828 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4829 | if (current_frame == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4830 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4831 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4832 | } |
| 4833 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4834 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4835 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4836 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4837 | |
| 4838 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4842 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4843 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4844 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4845 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4846 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
| 4847 | if (current_frame == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4848 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4849 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4850 | |
| 4851 | assert(current_frame->f_globals != NULL); |
| 4852 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4855 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4856 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4857 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4858 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4859 | PyFrameObject *current_frame = _PyEval_GetFrame(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4860 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4862 | if (current_frame != NULL) { |
| 4863 | const int codeflags = current_frame->f_code->co_flags; |
| 4864 | const int compilerflags = codeflags & PyCF_MASK; |
| 4865 | if (compilerflags) { |
| 4866 | result = 1; |
| 4867 | cf->cf_flags |= compilerflags; |
| 4868 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4869 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4871 | result = 1; |
| 4872 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4873 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4874 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4875 | } |
| 4876 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4879 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4880 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4881 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4883 | if (PyMethod_Check(func)) |
| 4884 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4885 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4886 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4887 | else if (PyCFunction_Check(func)) |
| 4888 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4889 | else |
| 4890 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4893 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4894 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4895 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4896 | if (PyMethod_Check(func)) |
| 4897 | return "()"; |
| 4898 | else if (PyFunction_Check(func)) |
| 4899 | return "()"; |
| 4900 | else if (PyCFunction_Check(func)) |
| 4901 | return "()"; |
| 4902 | else |
| 4903 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4906 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4907 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4908 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4909 | tstate, tstate->frame, \ |
| 4910 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4911 | x = NULL; \ |
| 4912 | } \ |
| 4913 | else { \ |
| 4914 | x = call; \ |
| 4915 | if (tstate->c_profilefunc != NULL) { \ |
| 4916 | if (x == NULL) { \ |
| 4917 | call_trace_protected(tstate->c_profilefunc, \ |
| 4918 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4919 | tstate, tstate->frame, \ |
| 4920 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4921 | /* XXX should pass (type, value, tb) */ \ |
| 4922 | } else { \ |
| 4923 | if (call_trace(tstate->c_profilefunc, \ |
| 4924 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4925 | tstate, tstate->frame, \ |
| 4926 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4927 | Py_DECREF(x); \ |
| 4928 | x = NULL; \ |
| 4929 | } \ |
| 4930 | } \ |
| 4931 | } \ |
| 4932 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4933 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4934 | x = call; \ |
| 4935 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4936 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4937 | |
| 4938 | static PyObject * |
| 4939 | trace_call_function(PyThreadState *tstate, |
| 4940 | PyObject *func, |
| 4941 | PyObject **args, Py_ssize_t nargs, |
| 4942 | PyObject *kwnames) |
| 4943 | { |
| 4944 | PyObject *x; |
| 4945 | if (PyCFunction_Check(func)) { |
Jeroen Demeyer | bf8e82f | 2019-07-23 12:39:51 +0200 | [diff] [blame] | 4946 | C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4947 | return x; |
| 4948 | } |
| 4949 | else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) { |
| 4950 | /* We need to create a temporary bound method as argument |
| 4951 | for profiling. |
| 4952 | |
| 4953 | If nargs == 0, then this cannot work because we have no |
| 4954 | "self". In any case, the call itself would raise |
| 4955 | TypeError (foo needs an argument), so we just skip |
| 4956 | profiling. */ |
| 4957 | PyObject *self = args[0]; |
| 4958 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 4959 | if (func == NULL) { |
| 4960 | return NULL; |
| 4961 | } |
Jeroen Demeyer | bf8e82f | 2019-07-23 12:39:51 +0200 | [diff] [blame] | 4962 | C_TRACE(x, _PyObject_Vectorcall(func, |
| 4963 | args+1, nargs-1, |
| 4964 | kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4965 | Py_DECREF(func); |
| 4966 | return x; |
| 4967 | } |
| 4968 | return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
| 4969 | } |
| 4970 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 4971 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 4972 | to reduce the stack consumption. */ |
| 4973 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4974 | 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] | 4975 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4976 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | PyObject *func = *pfunc; |
| 4978 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4979 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4980 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4981 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4982 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4983 | if (tstate->use_tracing) { |
| 4984 | x = trace_call_function(tstate, func, stack, nargs, kwnames); |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4985 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4986 | else { |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4987 | x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4989 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4990 | assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4991 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4992 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | while ((*pp_stack) > pfunc) { |
| 4994 | w = EXT_POP(*pp_stack); |
| 4995 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5001 | static PyObject * |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 5002 | do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5003 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5004 | PyObject *result; |
| 5005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5006 | if (PyCFunction_Check(func)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5007 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5008 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5010 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5011 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 5012 | if (nargs > 0 && tstate->use_tracing) { |
| 5013 | /* We need to create a temporary bound method as argument |
| 5014 | for profiling. |
| 5015 | |
| 5016 | If nargs == 0, then this cannot work because we have no |
| 5017 | "self". In any case, the call itself would raise |
| 5018 | TypeError (foo needs an argument), so we just skip |
| 5019 | profiling. */ |
| 5020 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 5021 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5022 | if (func == NULL) { |
| 5023 | return NULL; |
| 5024 | } |
| 5025 | |
Jeroen Demeyer | bf8e82f | 2019-07-23 12:39:51 +0200 | [diff] [blame] | 5026 | C_TRACE(result, _PyObject_FastCallDict(func, |
| 5027 | &_PyTuple_ITEMS(callargs)[1], |
| 5028 | nargs - 1, |
| 5029 | kwdict)); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5030 | Py_DECREF(func); |
| 5031 | return result; |
| 5032 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5033 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5034 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5037 | /* 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] | 5038 | nb_index slot defined, and store in *pi. |
| 5039 | 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] | 5040 | 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] | 5041 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5042 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5043 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5044 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5045 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5046 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5047 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | Py_ssize_t x; |
| 5049 | if (PyIndex_Check(v)) { |
| 5050 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5051 | if (x == -1 && _PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5052 | return 0; |
| 5053 | } |
| 5054 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5055 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5056 | "slice indices must be integers or " |
| 5057 | "None or have an __index__ method"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5058 | return 0; |
| 5059 | } |
| 5060 | *pi = x; |
| 5061 | } |
| 5062 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5065 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5066 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5067 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5068 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5069 | Py_ssize_t x; |
| 5070 | if (PyIndex_Check(v)) { |
| 5071 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5072 | if (x == -1 && _PyErr_Occurred(tstate)) |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5073 | return 0; |
| 5074 | } |
| 5075 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5076 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5077 | "slice indices must be integers or " |
| 5078 | "have an __index__ method"); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5079 | return 0; |
| 5080 | } |
| 5081 | *pi = x; |
| 5082 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 5086 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 5088 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5089 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5090 | cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5091 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | int res = 0; |
| 5093 | switch (op) { |
| 5094 | case PyCmp_IS: |
| 5095 | res = (v == w); |
| 5096 | break; |
| 5097 | case PyCmp_IS_NOT: |
| 5098 | res = (v != w); |
| 5099 | break; |
| 5100 | case PyCmp_IN: |
| 5101 | res = PySequence_Contains(w, v); |
| 5102 | if (res < 0) |
| 5103 | return NULL; |
| 5104 | break; |
| 5105 | case PyCmp_NOT_IN: |
| 5106 | res = PySequence_Contains(w, v); |
| 5107 | if (res < 0) |
| 5108 | return NULL; |
| 5109 | res = !res; |
| 5110 | break; |
| 5111 | case PyCmp_EXC_MATCH: |
| 5112 | if (PyTuple_Check(w)) { |
| 5113 | Py_ssize_t i, length; |
| 5114 | length = PyTuple_Size(w); |
| 5115 | for (i = 0; i < length; i += 1) { |
| 5116 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 5117 | if (!PyExceptionClass_Check(exc)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5118 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5119 | CANNOT_CATCH_MSG); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5120 | return NULL; |
| 5121 | } |
| 5122 | } |
| 5123 | } |
| 5124 | else { |
| 5125 | if (!PyExceptionClass_Check(w)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5126 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5127 | CANNOT_CATCH_MSG); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5128 | return NULL; |
| 5129 | } |
| 5130 | } |
| 5131 | res = PyErr_GivenExceptionMatches(v, w); |
| 5132 | break; |
| 5133 | default: |
| 5134 | return PyObject_RichCompare(v, w, op); |
| 5135 | } |
| 5136 | v = res ? Py_True : Py_False; |
| 5137 | Py_INCREF(v); |
| 5138 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5141 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5142 | import_name(PyThreadState *tstate, PyFrameObject *f, |
| 5143 | PyObject *name, PyObject *fromlist, PyObject *level) |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5144 | { |
| 5145 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5146 | PyObject *import_func, *res; |
| 5147 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5148 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5149 | import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5150 | if (import_func == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5151 | if (!_PyErr_Occurred(tstate)) { |
| 5152 | _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5153 | } |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5154 | return NULL; |
| 5155 | } |
| 5156 | |
| 5157 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5158 | if (import_func == tstate->interp->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5159 | int ilevel = _PyLong_AsInt(level); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5160 | if (ilevel == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5161 | return NULL; |
| 5162 | } |
| 5163 | res = PyImport_ImportModuleLevelObject( |
| 5164 | name, |
| 5165 | f->f_globals, |
| 5166 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5167 | fromlist, |
| 5168 | ilevel); |
| 5169 | return res; |
| 5170 | } |
| 5171 | |
| 5172 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5173 | |
| 5174 | stack[0] = name; |
| 5175 | stack[1] = f->f_globals; |
| 5176 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5177 | stack[3] = fromlist; |
| 5178 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5179 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5180 | Py_DECREF(import_func); |
| 5181 | return res; |
| 5182 | } |
| 5183 | |
| 5184 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5185 | import_from(PyThreadState *tstate, PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5188 | _Py_IDENTIFIER(__name__); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5189 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5190 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5191 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5192 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5193 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5194 | /* Issue #17636: in case this failed because of a circular relative |
| 5195 | import, try to fallback on reading the module directly from |
| 5196 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5197 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5198 | if (pkgname == NULL) { |
| 5199 | goto error; |
| 5200 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 5201 | if (!PyUnicode_Check(pkgname)) { |
| 5202 | Py_CLEAR(pkgname); |
| 5203 | goto error; |
| 5204 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5205 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5206 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5207 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5208 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5209 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 5210 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5211 | Py_DECREF(fullmodname); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5212 | if (x == NULL && !_PyErr_Occurred(tstate)) { |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5213 | goto error; |
| 5214 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5215 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5216 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5217 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5218 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5219 | if (pkgname == NULL) { |
| 5220 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 5221 | if (pkgname_or_unknown == NULL) { |
| 5222 | Py_XDECREF(pkgpath); |
| 5223 | return NULL; |
| 5224 | } |
| 5225 | } else { |
| 5226 | pkgname_or_unknown = pkgname; |
| 5227 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5228 | |
| 5229 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5230 | _PyErr_Clear(tstate); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5231 | errmsg = PyUnicode_FromFormat( |
| 5232 | "cannot import name %R from %R (unknown location)", |
| 5233 | name, pkgname_or_unknown |
| 5234 | ); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5235 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5236 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 5237 | } |
| 5238 | else { |
Steve Dower | 2d5594f | 2019-09-09 09:45:18 -0700 | [diff] [blame] | 5239 | _Py_IDENTIFIER(__spec__); |
| 5240 | PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__); |
Steve Dower | 2d5594f | 2019-09-09 09:45:18 -0700 | [diff] [blame] | 5241 | const char *fmt = |
| 5242 | _PyModuleSpec_IsInitializing(spec) ? |
| 5243 | "cannot import name %R from partially initialized module %R " |
| 5244 | "(most likely due to a circular import) (%S)" : |
| 5245 | "cannot import name %R from %R (%S)"; |
| 5246 | Py_XDECREF(spec); |
| 5247 | |
| 5248 | errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5249 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5250 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5251 | } |
| 5252 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5253 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5254 | Py_XDECREF(pkgname_or_unknown); |
| 5255 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5256 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5257 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5258 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5259 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5260 | import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5261 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5262 | _Py_IDENTIFIER(__all__); |
| 5263 | _Py_IDENTIFIER(__dict__); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5264 | _Py_IDENTIFIER(__name__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5265 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | int skip_leading_underscores = 0; |
| 5267 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5268 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5269 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 5270 | return -1; /* Unexpected error */ |
| 5271 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5273 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 5274 | return -1; |
| 5275 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5276 | if (dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5277 | _PyErr_SetString(tstate, PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5278 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | return -1; |
| 5280 | } |
| 5281 | all = PyMapping_Keys(dict); |
| 5282 | Py_DECREF(dict); |
| 5283 | if (all == NULL) |
| 5284 | return -1; |
| 5285 | skip_leading_underscores = 1; |
| 5286 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | for (pos = 0, err = 0; ; pos++) { |
| 5289 | name = PySequence_GetItem(all, pos); |
| 5290 | if (name == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5291 | if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5292 | err = -1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5293 | } |
| 5294 | else { |
| 5295 | _PyErr_Clear(tstate); |
| 5296 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5297 | break; |
| 5298 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5299 | if (!PyUnicode_Check(name)) { |
| 5300 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 5301 | if (modname == NULL) { |
| 5302 | Py_DECREF(name); |
| 5303 | err = -1; |
| 5304 | break; |
| 5305 | } |
| 5306 | if (!PyUnicode_Check(modname)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5307 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5308 | "module __name__ must be a string, not %.100s", |
| 5309 | Py_TYPE(modname)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5310 | } |
| 5311 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5312 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5313 | "%s in %U.%s must be str, not %.100s", |
| 5314 | skip_leading_underscores ? "Key" : "Item", |
| 5315 | modname, |
| 5316 | skip_leading_underscores ? "__dict__" : "__all__", |
| 5317 | Py_TYPE(name)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5318 | } |
| 5319 | Py_DECREF(modname); |
| 5320 | Py_DECREF(name); |
| 5321 | err = -1; |
| 5322 | break; |
| 5323 | } |
| 5324 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 5325 | if (PyUnicode_READY(name) == -1) { |
| 5326 | Py_DECREF(name); |
| 5327 | err = -1; |
| 5328 | break; |
| 5329 | } |
| 5330 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 5331 | Py_DECREF(name); |
| 5332 | continue; |
| 5333 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5334 | } |
| 5335 | value = PyObject_GetAttr(v, name); |
| 5336 | if (value == NULL) |
| 5337 | err = -1; |
| 5338 | else if (PyDict_CheckExact(locals)) |
| 5339 | err = PyDict_SetItem(locals, name, value); |
| 5340 | else |
| 5341 | err = PyObject_SetItem(locals, name, value); |
| 5342 | Py_DECREF(name); |
| 5343 | Py_XDECREF(value); |
| 5344 | if (err != 0) |
| 5345 | break; |
| 5346 | } |
| 5347 | Py_DECREF(all); |
| 5348 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5351 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5352 | check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5353 | { |
| 5354 | if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5355 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5356 | "%.200s%.200s argument after * " |
| 5357 | "must be an iterable, not %.200s", |
| 5358 | PyEval_GetFuncName(func), |
| 5359 | PyEval_GetFuncDesc(func), |
| 5360 | args->ob_type->tp_name); |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5361 | return -1; |
| 5362 | } |
| 5363 | return 0; |
| 5364 | } |
| 5365 | |
| 5366 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5367 | format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5368 | { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5369 | /* _PyDict_MergeEx raises attribute |
| 5370 | * error (percolated from an attempt |
| 5371 | * to get 'keys' attribute) instead of |
| 5372 | * a type error if its second argument |
| 5373 | * is not a mapping. |
| 5374 | */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5375 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 5376 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5377 | "%.200s%.200s argument after ** " |
| 5378 | "must be a mapping, not %.200s", |
| 5379 | PyEval_GetFuncName(func), |
| 5380 | PyEval_GetFuncDesc(func), |
| 5381 | kwargs->ob_type->tp_name); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5382 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5383 | else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5384 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5385 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5386 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 5387 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 5388 | if (!PyUnicode_Check(key)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5389 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5390 | "%.200s%.200s keywords must be strings", |
| 5391 | PyEval_GetFuncName(func), |
| 5392 | PyEval_GetFuncDesc(func)); |
| 5393 | } |
| 5394 | else { |
| 5395 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5396 | "%.200s%.200s got multiple " |
| 5397 | "values for keyword argument '%U'", |
| 5398 | PyEval_GetFuncName(func), |
| 5399 | PyEval_GetFuncDesc(func), |
| 5400 | key); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5401 | } |
| 5402 | Py_XDECREF(exc); |
| 5403 | Py_XDECREF(val); |
| 5404 | Py_XDECREF(tb); |
| 5405 | } |
| 5406 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5407 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5408 | } |
| 5409 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5410 | } |
| 5411 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5412 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5413 | format_exc_check_arg(PyThreadState *tstate, PyObject *exc, |
| 5414 | const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5416 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | if (!obj) |
| 5419 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5420 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5421 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 | if (!obj_str) |
| 5423 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5424 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5425 | _PyErr_Format(tstate, exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5426 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5427 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5428 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5429 | format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5430 | { |
| 5431 | PyObject *name; |
| 5432 | /* Don't stomp existing exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5433 | if (_PyErr_Occurred(tstate)) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5434 | return; |
| 5435 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5436 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5437 | oparg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5438 | format_exc_check_arg(tstate, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5439 | PyExc_UnboundLocalError, |
| 5440 | UNBOUNDLOCAL_ERROR_MSG, |
| 5441 | name); |
| 5442 | } else { |
| 5443 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5444 | PyTuple_GET_SIZE(co->co_cellvars)); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5445 | format_exc_check_arg(tstate, PyExc_NameError, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5446 | UNBOUNDFREE_ERROR_MSG, name); |
| 5447 | } |
| 5448 | } |
| 5449 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5450 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5451 | format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode) |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5452 | { |
| 5453 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5454 | if (prevopcode == BEFORE_ASYNC_WITH) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5455 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5456 | "'async with' received an object from __aenter__ " |
| 5457 | "that does not implement __await__: %.100s", |
| 5458 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5459 | } |
| 5460 | else if (prevopcode == WITH_CLEANUP_START) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5461 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5462 | "'async with' received an object from __aexit__ " |
| 5463 | "that does not implement __await__: %.100s", |
| 5464 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | } |
| 5468 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5469 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5470 | unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5471 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5472 | { |
| 5473 | PyObject *res; |
| 5474 | if (Py_REFCNT(v) == 2) { |
| 5475 | /* In the common case, there are 2 references to the value |
| 5476 | * stored in 'variable' when the += is performed: one on the |
| 5477 | * value stack (in 'v') and one still stored in the |
| 5478 | * 'variable'. We try to delete the variable now to reduce |
| 5479 | * the refcnt to 1. |
| 5480 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5481 | int opcode, oparg; |
| 5482 | NEXTOPARG(); |
| 5483 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5484 | case STORE_FAST: |
| 5485 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5486 | PyObject **fastlocals = f->f_localsplus; |
| 5487 | if (GETLOCAL(oparg) == v) |
| 5488 | SETLOCAL(oparg, NULL); |
| 5489 | break; |
| 5490 | } |
| 5491 | case STORE_DEREF: |
| 5492 | { |
| 5493 | PyObject **freevars = (f->f_localsplus + |
| 5494 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5495 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5496 | if (PyCell_GET(c) == v) { |
| 5497 | PyCell_SET(c, NULL); |
| 5498 | Py_DECREF(v); |
| 5499 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5500 | break; |
| 5501 | } |
| 5502 | case STORE_NAME: |
| 5503 | { |
| 5504 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5505 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5506 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5507 | if (locals && PyDict_CheckExact(locals)) { |
| 5508 | PyObject *w = PyDict_GetItemWithError(locals, name); |
| 5509 | if ((w == v && PyDict_DelItem(locals, name) != 0) || |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5510 | (w == NULL && _PyErr_Occurred(tstate))) |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5511 | { |
| 5512 | Py_DECREF(v); |
| 5513 | return NULL; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5514 | } |
| 5515 | } |
| 5516 | break; |
| 5517 | } |
| 5518 | } |
| 5519 | } |
| 5520 | res = v; |
| 5521 | PyUnicode_Append(&res, w); |
| 5522 | return res; |
| 5523 | } |
| 5524 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5525 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5526 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5527 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5528 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5530 | int i; |
| 5531 | PyObject *l = PyList_New(256); |
| 5532 | if (l == NULL) return NULL; |
| 5533 | for (i = 0; i < 256; i++) { |
| 5534 | PyObject *x = PyLong_FromLong(a[i]); |
| 5535 | if (x == NULL) { |
| 5536 | Py_DECREF(l); |
| 5537 | return NULL; |
| 5538 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5539 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5540 | } |
| 5541 | for (i = 0; i < 256; i++) |
| 5542 | a[i] = 0; |
| 5543 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5547 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5548 | { |
| 5549 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5551 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5552 | int i; |
| 5553 | PyObject *l = PyList_New(257); |
| 5554 | if (l == NULL) return NULL; |
| 5555 | for (i = 0; i < 257; i++) { |
| 5556 | PyObject *x = getarray(dxpairs[i]); |
| 5557 | if (x == NULL) { |
| 5558 | Py_DECREF(l); |
| 5559 | return NULL; |
| 5560 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5561 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | } |
| 5563 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5564 | #endif |
| 5565 | } |
| 5566 | |
| 5567 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5568 | |
| 5569 | Py_ssize_t |
| 5570 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5571 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5572 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5573 | Py_ssize_t new_index; |
| 5574 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5575 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5576 | return -1; |
| 5577 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5578 | new_index = interp->co_extra_user_count++; |
| 5579 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5580 | return new_index; |
| 5581 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5582 | |
| 5583 | static void |
| 5584 | dtrace_function_entry(PyFrameObject *f) |
| 5585 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5586 | const char *filename; |
| 5587 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5588 | int lineno; |
| 5589 | |
| 5590 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5591 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5592 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5593 | |
| 5594 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5595 | } |
| 5596 | |
| 5597 | static void |
| 5598 | dtrace_function_return(PyFrameObject *f) |
| 5599 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5600 | const char *filename; |
| 5601 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5602 | int lineno; |
| 5603 | |
| 5604 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5605 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5606 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5607 | |
| 5608 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5609 | } |
| 5610 | |
| 5611 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5612 | static void |
| 5613 | maybe_dtrace_line(PyFrameObject *frame, |
| 5614 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5615 | { |
| 5616 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5617 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5618 | |
| 5619 | /* If the last instruction executed isn't in the current |
| 5620 | instruction window, reset the window. |
| 5621 | */ |
| 5622 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5623 | PyAddrPair bounds; |
| 5624 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5625 | &bounds); |
| 5626 | *instr_lb = bounds.ap_lower; |
| 5627 | *instr_ub = bounds.ap_upper; |
| 5628 | } |
| 5629 | /* If the last instruction falls at the start of a line or if |
| 5630 | it represents a jump backwards, update the frame's line |
| 5631 | number and call the trace function. */ |
| 5632 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5633 | frame->f_lineno = line; |
| 5634 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5635 | if (!co_filename) |
| 5636 | co_filename = "?"; |
| 5637 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5638 | if (!co_name) |
| 5639 | co_name = "?"; |
| 5640 | PyDTrace_LINE(co_filename, co_name, line); |
| 5641 | } |
| 5642 | *instr_prev = frame->f_lasti; |
| 5643 | } |