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 | e560f90 | 2020-04-14 18:30:41 +0200 | [diff] [blame] | 13 | #include "pycore_abstract.h" // _PyIndex_Check() |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 14 | #include "pycore_call.h" // _PyObject_FastCallDictTstate() |
| 15 | #include "pycore_ceval.h" // _PyEval_SignalAsyncExc() |
| 16 | #include "pycore_code.h" // _PyCode_InitOpcache() |
| 17 | #include "pycore_initconfig.h" // _PyStatus_OK() |
| 18 | #include "pycore_object.h" // _PyObject_GC_TRACK() |
| 19 | #include "pycore_pyerrors.h" // _PyErr_Fetch() |
| 20 | #include "pycore_pylifecycle.h" // _PyErr_Print() |
Victor Stinner | e560f90 | 2020-04-14 18:30:41 +0200 | [diff] [blame] | 21 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
| 22 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 23 | #include "pycore_sysmodule.h" // _PySys_Audit() |
| 24 | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 25 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 26 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 27 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 29 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 30 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 31 | #include "setobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 33 | #include <ctype.h> |
| 34 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 35 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 36 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | #define LLTRACE 1 /* Low-level trace feature */ |
| 38 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 41 | #if !defined(Py_BUILD_CORE) |
| 42 | # error "ceval.c must be build with Py_BUILD_CORE define for best performance" |
| 43 | #endif |
| 44 | |
Hai Shi | 46874c2 | 2020-01-30 17:20:25 -0600 | [diff] [blame] | 45 | _Py_IDENTIFIER(__name__); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 47 | /* Forward declarations */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 48 | Py_LOCAL_INLINE(PyObject *) call_function( |
| 49 | PyThreadState *tstate, PyObject ***pp_stack, |
| 50 | Py_ssize_t oparg, PyObject *kwnames); |
| 51 | static PyObject * do_call_core( |
| 52 | PyThreadState *tstate, PyObject *func, |
| 53 | PyObject *callargs, PyObject *kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 55 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 56 | static int lltrace; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 57 | static int prtrace(PyThreadState *, PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 58 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 59 | static int call_trace(Py_tracefunc, PyObject *, |
| 60 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 62 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 63 | PyThreadState *, PyFrameObject *, |
| 64 | int, PyObject *); |
| 65 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 66 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 67 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 68 | PyThreadState *, PyFrameObject *, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 69 | PyCodeAddressRange *, int *); |
| 70 | static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 71 | static void dtrace_function_entry(PyFrameObject *); |
| 72 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 73 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 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); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 85 | static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, 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 |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 114 | #define OPCODE_CACHE_MAX_TRIES 20 |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 115 | #define OPCACHE_STATS 0 /* Enable stats */ |
| 116 | |
| 117 | #if OPCACHE_STATS |
| 118 | static size_t opcache_code_objects = 0; |
| 119 | static size_t opcache_code_objects_extra_mem = 0; |
| 120 | |
| 121 | static size_t opcache_global_opts = 0; |
| 122 | static size_t opcache_global_hits = 0; |
| 123 | static size_t opcache_global_misses = 0; |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 124 | |
| 125 | static size_t opcache_attr_opts = 0; |
| 126 | static size_t opcache_attr_hits = 0; |
| 127 | static size_t opcache_attr_misses = 0; |
| 128 | static size_t opcache_attr_deopts = 0; |
| 129 | static size_t opcache_attr_total = 0; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 130 | #endif |
| 131 | |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 132 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 133 | #ifndef NDEBUG |
| 134 | /* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and |
| 135 | PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen |
| 136 | when a thread continues to run after Python finalization, especially |
| 137 | daemon threads. */ |
| 138 | static int |
| 139 | is_tstate_valid(PyThreadState *tstate) |
| 140 | { |
| 141 | assert(!_PyMem_IsPtrFreed(tstate)); |
| 142 | assert(!_PyMem_IsPtrFreed(tstate->interp)); |
| 143 | return 1; |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 148 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 149 | 1. We believe this is all right because the eval loop will release |
| 150 | the GIL eventually anyway. */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 151 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 152 | COMPUTE_EVAL_BREAKER(PyInterpreterState *interp, |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 153 | struct _ceval_runtime_state *ceval, |
| 154 | struct _ceval_state *ceval2) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 155 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 156 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, |
| 157 | _Py_atomic_load_relaxed(&ceval2->gil_drop_request) |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 158 | | (_Py_atomic_load_relaxed(&ceval->signals_pending) |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 159 | && _Py_ThreadCanHandleSignals(interp)) |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 160 | | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do) |
Victor Stinner | d831688 | 2020-03-20 14:50:35 +0100 | [diff] [blame] | 161 | && _Py_ThreadCanHandlePendingCalls()) |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 162 | | ceval2->pending.async_exc); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 163 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 165 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 166 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 167 | SET_GIL_DROP_REQUEST(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 168 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 169 | struct _ceval_state *ceval2 = &interp->ceval; |
| 170 | _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1); |
| 171 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 172 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 174 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 175 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 176 | RESET_GIL_DROP_REQUEST(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 177 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 178 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 179 | struct _ceval_state *ceval2 = &interp->ceval; |
| 180 | _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0); |
| 181 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 182 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 183 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 184 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 185 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 186 | SIGNAL_PENDING_CALLS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 187 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 188 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 189 | struct _ceval_state *ceval2 = &interp->ceval; |
| 190 | _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1); |
| 191 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 192 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 193 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 194 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 195 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 196 | UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 197 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 198 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 199 | struct _ceval_state *ceval2 = &interp->ceval; |
| 200 | _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0); |
| 201 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | static inline void |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 206 | SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 207 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 208 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 209 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 210 | _Py_atomic_store_relaxed(&ceval->signals_pending, 1); |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 211 | if (force) { |
| 212 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
| 213 | } |
| 214 | else { |
| 215 | /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */ |
| 216 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
| 217 | } |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 222 | UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 223 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 224 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 225 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 226 | _Py_atomic_store_relaxed(&ceval->signals_pending, 0); |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 227 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 232 | SIGNAL_ASYNC_EXC(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 233 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 234 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 235 | ceval2->pending.async_exc = 1; |
| 236 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 241 | UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 242 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 243 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 244 | struct _ceval_state *ceval2 = &interp->ceval; |
| 245 | ceval2->pending.async_exc = 0; |
| 246 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 247 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 248 | |
| 249 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 250 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 251 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 252 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 253 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 254 | |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 255 | void _Py_NO_RETURN |
| 256 | _Py_FatalError_TstateNULL(const char *func) |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 257 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 258 | _Py_FatalErrorFunc(func, |
| 259 | "the function must be called with the GIL held, " |
| 260 | "but the GIL is released " |
| 261 | "(the current Python thread state is NULL)"); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 264 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 265 | int |
| 266 | _PyEval_ThreadsInitialized(PyInterpreterState *interp) |
| 267 | { |
| 268 | return gil_created(&interp->ceval.gil); |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | PyEval_ThreadsInitialized(void) |
| 273 | { |
| 274 | // Fatal error if there is no current interpreter |
| 275 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 276 | return _PyEval_ThreadsInitialized(interp); |
| 277 | } |
| 278 | #else |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 279 | int |
Victor Stinner | 175a704 | 2020-03-10 00:37:48 +0100 | [diff] [blame] | 280 | _PyEval_ThreadsInitialized(_PyRuntimeState *runtime) |
| 281 | { |
| 282 | return gil_created(&runtime->ceval.gil); |
| 283 | } |
| 284 | |
| 285 | int |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 286 | PyEval_ThreadsInitialized(void) |
| 287 | { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 288 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 175a704 | 2020-03-10 00:37:48 +0100 | [diff] [blame] | 289 | return _PyEval_ThreadsInitialized(runtime); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 290 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 291 | #endif |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 292 | |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 293 | PyStatus |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 294 | _PyEval_InitGIL(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 295 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 296 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 297 | if (!_Py_IsMainInterpreter(tstate)) { |
| 298 | /* Currently, the GIL is shared by all interpreters, |
| 299 | and only the main interpreter is responsible to create |
| 300 | and destroy it. */ |
| 301 | return _PyStatus_OK(); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 302 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 303 | #endif |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 304 | |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 305 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 306 | struct _gil_runtime_state *gil = &tstate->interp->ceval.gil; |
| 307 | #else |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 308 | struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 309 | #endif |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 310 | assert(!gil_created(gil)); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 311 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 312 | PyThread_init_thread(); |
| 313 | create_gil(gil); |
| 314 | |
| 315 | take_gil(tstate); |
| 316 | |
| 317 | assert(gil_created(gil)); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 318 | return _PyStatus_OK(); |
| 319 | } |
| 320 | |
| 321 | void |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 322 | _PyEval_FiniGIL(PyThreadState *tstate) |
| 323 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 324 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 325 | if (!_Py_IsMainInterpreter(tstate)) { |
| 326 | /* Currently, the GIL is shared by all interpreters, |
| 327 | and only the main interpreter is responsible to create |
| 328 | and destroy it. */ |
| 329 | return; |
| 330 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 331 | #endif |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 332 | |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 333 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 334 | struct _gil_runtime_state *gil = &tstate->interp->ceval.gil; |
| 335 | #else |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 336 | struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 337 | #endif |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 338 | if (!gil_created(gil)) { |
| 339 | /* First Py_InitializeFromConfig() call: the GIL doesn't exist |
| 340 | yet: do nothing. */ |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | destroy_gil(gil); |
| 345 | assert(!gil_created(gil)); |
| 346 | } |
| 347 | |
| 348 | void |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 349 | PyEval_InitThreads(void) |
| 350 | { |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 351 | /* Do nothing: kept for backward compatibility */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 352 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 353 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 354 | void |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 355 | _PyEval_Fini(void) |
| 356 | { |
| 357 | #if OPCACHE_STATS |
| 358 | fprintf(stderr, "-- Opcode cache number of objects = %zd\n", |
| 359 | opcache_code_objects); |
| 360 | |
| 361 | fprintf(stderr, "-- Opcode cache total extra mem = %zd\n", |
| 362 | opcache_code_objects_extra_mem); |
| 363 | |
| 364 | fprintf(stderr, "\n"); |
| 365 | |
| 366 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n", |
| 367 | opcache_global_hits, |
| 368 | (int) (100.0 * opcache_global_hits / |
| 369 | (opcache_global_hits + opcache_global_misses))); |
| 370 | |
| 371 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n", |
| 372 | opcache_global_misses, |
| 373 | (int) (100.0 * opcache_global_misses / |
| 374 | (opcache_global_hits + opcache_global_misses))); |
| 375 | |
| 376 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n", |
| 377 | opcache_global_opts); |
| 378 | |
| 379 | fprintf(stderr, "\n"); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 380 | |
| 381 | fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n", |
| 382 | opcache_attr_hits, |
| 383 | (int) (100.0 * opcache_attr_hits / |
| 384 | opcache_attr_total)); |
| 385 | |
| 386 | fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n", |
| 387 | opcache_attr_misses, |
| 388 | (int) (100.0 * opcache_attr_misses / |
| 389 | opcache_attr_total)); |
| 390 | |
| 391 | fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n", |
| 392 | opcache_attr_opts); |
| 393 | |
| 394 | fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n", |
| 395 | opcache_attr_deopts); |
| 396 | |
| 397 | fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n", |
| 398 | opcache_attr_total); |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 399 | #endif |
| 400 | } |
| 401 | |
| 402 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 403 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 404 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 405 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 406 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 407 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 408 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 409 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 413 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 414 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 415 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 416 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 418 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 419 | in debug mode. */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 420 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 421 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 422 | drop_gil(ceval, ceval2, tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 426 | _PyEval_ReleaseLock(PyThreadState *tstate) |
| 427 | { |
| 428 | struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 429 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 430 | drop_gil(ceval, ceval2, tstate); |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 434 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 435 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 436 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 437 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 438 | take_gil(tstate); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 439 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 440 | struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 441 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 442 | (void)_PyThreadState_Swap(gilstate, tstate); |
| 443 | #else |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 444 | if (_PyThreadState_Swap(gilstate, tstate) != NULL) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 445 | Py_FatalError("non-NULL old thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 446 | } |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 447 | #endif |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 451 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 452 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 453 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 454 | |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 455 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 456 | PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 457 | if (new_tstate != tstate) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 458 | Py_FatalError("wrong thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 459 | } |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 460 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 461 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 462 | drop_gil(ceval, ceval2, tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 463 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 464 | |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 465 | #ifdef HAVE_FORK |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 466 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 467 | which are not running in the child process, and clear internal locks |
| 468 | which might be held by those threads. */ |
| 469 | PyStatus |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 470 | _PyEval_ReInitThreads(PyThreadState *tstate) |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 471 | { |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 472 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 473 | |
| 474 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 475 | struct _gil_runtime_state *gil = &tstate->interp->ceval.gil; |
| 476 | #else |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 477 | struct _gil_runtime_state *gil = &runtime->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 478 | #endif |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 479 | if (!gil_created(gil)) { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 480 | return _PyStatus_OK(); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 481 | } |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 482 | recreate_gil(gil); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 483 | |
| 484 | take_gil(tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 485 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 486 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 487 | if (_PyThread_at_fork_reinit(&pending->lock) < 0) { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 488 | return _PyStatus_ERR("Can't reinitialize pending calls lock"); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 489 | } |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 491 | /* Destroy all threads except the current one */ |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 492 | _PyThreadState_DeleteExcept(runtime, tstate); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 493 | return _PyStatus_OK(); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 494 | } |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 495 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 497 | /* 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] | 498 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 499 | |
| 500 | void |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 501 | _PyEval_SignalAsyncExc(PyThreadState *tstate) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 502 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 503 | assert(is_tstate_valid(tstate)); |
| 504 | SIGNAL_ASYNC_EXC(tstate->interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 505 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 507 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 508 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 509 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 510 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 511 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 512 | PyThreadState *old_tstate = _PyThreadState_GET(); |
| 513 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate); |
| 514 | #else |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 515 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 516 | #endif |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 517 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 518 | |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 519 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 520 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 521 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 522 | assert(gil_created(&ceval2->gil)); |
| 523 | #else |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 524 | assert(gil_created(&ceval->gil)); |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 525 | #endif |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 526 | drop_gil(ceval, ceval2, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 531 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 532 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 533 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 534 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 535 | take_gil(tstate); |
Victor Stinner | 17c68b8 | 2020-01-30 12:20:48 +0100 | [diff] [blame] | 536 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 537 | struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; |
| 538 | _PyThreadState_Swap(gilstate, tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 542 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 543 | signal handlers or Mac I/O completion routines) can schedule calls |
| 544 | to a function to be called synchronously. |
| 545 | The synchronous function is called with one void* argument. |
| 546 | It should return 0 for success or -1 for failure -- failure should |
| 547 | be accompanied by an exception. |
| 548 | |
| 549 | If registry succeeds, the registry function returns 0; if it fails |
| 550 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 551 | an exception condition). |
| 552 | |
| 553 | Note that because registry may occur from within signal handlers, |
| 554 | or other asynchronous events, calling malloc() is unsafe! |
| 555 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 556 | Any thread can schedule pending calls, but only the main thread |
| 557 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 558 | There is no facility to schedule calls to a particular thread, but |
| 559 | that should be easy to change, should that ever be required. In |
| 560 | that case, the static variables here should go into the python |
| 561 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 562 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 564 | void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 565 | _PyEval_SignalReceived(PyInterpreterState *interp) |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 566 | { |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 567 | #ifdef MS_WINDOWS |
| 568 | // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal |
| 569 | // handler which can run in a thread different than the Python thread, in |
| 570 | // which case _Py_ThreadCanHandleSignals() is wrong. Ignore |
| 571 | // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1. |
| 572 | // |
| 573 | // The next eval_frame_handle_pending() call will call |
| 574 | // _Py_ThreadCanHandleSignals() to recompute eval_breaker. |
| 575 | int force = 1; |
| 576 | #else |
| 577 | int force = 0; |
| 578 | #endif |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 579 | /* bpo-30703: Function called when the C signal handler of Python gets a |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 580 | signal. We cannot queue a callback using _PyEval_AddPendingCall() since |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 581 | that function is not async-signal-safe. */ |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 582 | SIGNAL_PENDING_SIGNALS(interp, force); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 585 | /* Push one item onto the queue while holding the lock. */ |
| 586 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 587 | _push_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 588 | int (*func)(void *), void *arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 589 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 590 | int i = pending->last; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 591 | int j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 592 | if (j == pending->first) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 593 | return -1; /* Queue full */ |
| 594 | } |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 595 | pending->calls[i].func = func; |
| 596 | pending->calls[i].arg = arg; |
| 597 | pending->last = j; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* Pop one item off the queue while holding the lock. */ |
| 602 | static void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 603 | _pop_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 604 | int (**func)(void *), void **arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 605 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 606 | int i = pending->first; |
| 607 | if (i == pending->last) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 608 | return; /* Queue empty */ |
| 609 | } |
| 610 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 611 | *func = pending->calls[i].func; |
| 612 | *arg = pending->calls[i].arg; |
| 613 | pending->first = (i + 1) % NPENDINGCALLS; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 616 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 617 | scheduling to be made from any thread, and even from an executing |
| 618 | callback. |
| 619 | */ |
| 620 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 621 | int |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 622 | _PyEval_AddPendingCall(PyInterpreterState *interp, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 623 | int (*func)(void *), void *arg) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 624 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 625 | struct _pending_calls *pending = &interp->ceval.pending; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 626 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 627 | /* Ensure that _PyEval_InitPendingCalls() was called |
| 628 | and that _PyEval_FiniPendingCalls() is not called yet. */ |
| 629 | assert(pending->lock != NULL); |
| 630 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 631 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 632 | int result = _push_pending_call(pending, func, arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 633 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 634 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 635 | /* signal main loop */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 636 | SIGNAL_PENDING_CALLS(interp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 640 | int |
| 641 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 642 | { |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 643 | /* Best-effort to support subinterpreters and calls with the GIL released. |
| 644 | |
| 645 | First attempt _PyThreadState_GET() since it supports subinterpreters. |
| 646 | |
| 647 | If the GIL is released, _PyThreadState_GET() returns NULL . In this |
| 648 | case, use PyGILState_GetThisThreadState() which works even if the GIL |
| 649 | is released. |
| 650 | |
| 651 | Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters: |
| 652 | see bpo-10915 and bpo-15751. |
| 653 | |
Victor Stinner | 8849e59 | 2020-03-18 19:28:53 +0100 | [diff] [blame] | 654 | Py_AddPendingCall() doesn't require the caller to hold the GIL. */ |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 655 | PyThreadState *tstate = _PyThreadState_GET(); |
| 656 | if (tstate == NULL) { |
| 657 | tstate = PyGILState_GetThisThreadState(); |
| 658 | } |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 659 | |
| 660 | PyInterpreterState *interp; |
| 661 | if (tstate != NULL) { |
| 662 | interp = tstate->interp; |
| 663 | } |
| 664 | else { |
| 665 | /* Last resort: use the main interpreter */ |
| 666 | interp = _PyRuntime.interpreters.main; |
| 667 | } |
| 668 | return _PyEval_AddPendingCall(interp, func, arg); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 669 | } |
| 670 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 671 | static int |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 672 | handle_signals(PyThreadState *tstate) |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 673 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 674 | assert(is_tstate_valid(tstate)); |
| 675 | if (!_Py_ThreadCanHandleSignals(tstate->interp)) { |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 676 | return 0; |
| 677 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 678 | |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 679 | UNSIGNAL_PENDING_SIGNALS(tstate->interp); |
Victor Stinner | 7281898 | 2020-03-26 22:28:11 +0100 | [diff] [blame] | 680 | if (_PyErr_CheckSignalsTstate(tstate) < 0) { |
| 681 | /* On failure, re-schedule a call to handle_signals(). */ |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 682 | SIGNAL_PENDING_SIGNALS(tstate->interp, 0); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 683 | return -1; |
| 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static int |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 689 | make_pending_calls(PyThreadState *tstate) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 690 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 691 | assert(is_tstate_valid(tstate)); |
| 692 | |
Victor Stinner | d831688 | 2020-03-20 14:50:35 +0100 | [diff] [blame] | 693 | /* only execute pending calls on main thread */ |
| 694 | if (!_Py_ThreadCanHandlePendingCalls()) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | /* don't perform recursive pending calls */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 699 | static int busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 700 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 702 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 703 | busy = 1; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 704 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 705 | /* unsignal before starting to call callbacks, so that any callback |
| 706 | added in-between re-signals */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 707 | UNSIGNAL_PENDING_CALLS(tstate->interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 708 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | /* perform a bounded number of calls, in case of recursion */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 711 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 712 | for (int i=0; i<NPENDINGCALLS; i++) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 713 | int (*func)(void *) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | void *arg = NULL; |
| 715 | |
| 716 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 717 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 718 | _pop_pending_call(pending, &func, &arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 719 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 720 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 721 | /* having released the lock, perform the callback */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 722 | if (func == NULL) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 723 | break; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 724 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 725 | res = func(arg); |
| 726 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 727 | goto error; |
| 728 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 730 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 731 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 732 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 733 | |
| 734 | error: |
| 735 | busy = 0; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 736 | SIGNAL_PENDING_CALLS(tstate->interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 737 | return res; |
| 738 | } |
| 739 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 740 | void |
Victor Stinner | 2b1df45 | 2020-01-13 18:46:59 +0100 | [diff] [blame] | 741 | _Py_FinishPendingCalls(PyThreadState *tstate) |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 742 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 743 | assert(PyGILState_Check()); |
| 744 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 745 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 746 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 747 | if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) { |
| 748 | return; |
| 749 | } |
| 750 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 751 | if (make_pending_calls(tstate) < 0) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 752 | PyObject *exc, *val, *tb; |
| 753 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 754 | PyErr_BadInternalCall(); |
| 755 | _PyErr_ChainExceptions(exc, val, tb); |
| 756 | _PyErr_Print(tstate); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 760 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 761 | of backward-compatibility. */ |
| 762 | int |
| 763 | Py_MakePendingCalls(void) |
| 764 | { |
| 765 | assert(PyGILState_Check()); |
| 766 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 767 | PyThreadState *tstate = _PyThreadState_GET(); |
| 768 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 769 | /* Python signal handler doesn't really queue a callback: it only signals |
| 770 | that a signal was received, see _PyEval_SignalReceived(). */ |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 771 | int res = handle_signals(tstate); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 772 | if (res != 0) { |
| 773 | return res; |
| 774 | } |
| 775 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 776 | res = make_pending_calls(tstate); |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 777 | if (res != 0) { |
| 778 | return res; |
| 779 | } |
| 780 | |
| 781 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 784 | /* The interpreter's recursion limit */ |
| 785 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 786 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
Victor Stinner | 19c3ac9 | 2020-09-23 14:04:57 +0200 | [diff] [blame] | 787 | # define Py_DEFAULT_RECURSION_LIMIT 1000 |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 788 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 789 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 790 | void |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 791 | _PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 792 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 793 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 794 | _gil_initialize(&ceval->gil); |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 795 | #endif |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 798 | int |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 799 | _PyEval_InitState(struct _ceval_state *ceval) |
| 800 | { |
Victor Stinner | 4e30ed3 | 2020-05-05 16:52:52 +0200 | [diff] [blame] | 801 | ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 802 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 803 | struct _pending_calls *pending = &ceval->pending; |
| 804 | assert(pending->lock == NULL); |
| 805 | |
| 806 | pending->lock = PyThread_allocate_lock(); |
| 807 | if (pending->lock == NULL) { |
| 808 | return -1; |
| 809 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 810 | |
| 811 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 812 | _gil_initialize(&ceval->gil); |
| 813 | #endif |
| 814 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | void |
| 819 | _PyEval_FiniState(struct _ceval_state *ceval) |
| 820 | { |
| 821 | struct _pending_calls *pending = &ceval->pending; |
| 822 | if (pending->lock != NULL) { |
| 823 | PyThread_free_lock(pending->lock); |
| 824 | pending->lock = NULL; |
| 825 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 826 | } |
| 827 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 828 | int |
| 829 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 830 | { |
Victor Stinner | 1bcc32f | 2020-06-10 20:08:26 +0200 | [diff] [blame] | 831 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 832 | return interp->ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 835 | void |
| 836 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 837 | { |
Victor Stinner | 4e30ed3 | 2020-05-05 16:52:52 +0200 | [diff] [blame] | 838 | PyThreadState *tstate = _PyThreadState_GET(); |
| 839 | tstate->interp->ceval.recursion_limit = new_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 842 | /* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
Victor Stinner | 19c3ac9 | 2020-09-23 14:04:57 +0200 | [diff] [blame] | 843 | if the recursion_depth reaches recursion_limit. |
| 844 | If USE_STACKCHECK, the macro decrements recursion_limit |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 845 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 846 | Without USE_STACKCHECK, there is no need for this. */ |
| 847 | int |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 848 | _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 849 | { |
Victor Stinner | 4e30ed3 | 2020-05-05 16:52:52 +0200 | [diff] [blame] | 850 | int recursion_limit = tstate->interp->ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 851 | |
| 852 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 853 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | if (PyOS_CheckStack()) { |
| 855 | --tstate->recursion_depth; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 856 | _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | return -1; |
| 858 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 859 | #endif |
Mark Shannon | 4e7a69b | 2020-12-02 13:30:55 +0000 | [diff] [blame] | 860 | if (tstate->recursion_headroom) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 862 | /* Overflowing while handling an overflow. Give up. */ |
| 863 | Py_FatalError("Cannot recover from stack overflow."); |
| 864 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | } |
Mark Shannon | 4e7a69b | 2020-12-02 13:30:55 +0000 | [diff] [blame] | 866 | else { |
| 867 | if (tstate->recursion_depth > recursion_limit) { |
| 868 | tstate->recursion_headroom++; |
| 869 | _PyErr_Format(tstate, PyExc_RecursionError, |
| 870 | "maximum recursion depth exceeded%s", |
| 871 | where); |
| 872 | tstate->recursion_headroom--; |
| 873 | --tstate->recursion_depth; |
| 874 | return -1; |
| 875 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | } |
| 877 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 880 | static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 881 | static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 882 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 883 | #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 884 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 885 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 886 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 887 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | return PyEval_EvalCodeEx(co, |
| 890 | globals, locals, |
| 891 | (PyObject **)NULL, 0, |
| 892 | (PyObject **)NULL, 0, |
| 893 | (PyObject **)NULL, 0, |
| 894 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | |
| 898 | /* Interpreter main loop */ |
| 899 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 900 | PyObject * |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 901 | PyEval_EvalFrame(PyFrameObject *f) |
| 902 | { |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 903 | /* Function kept for backward compatibility */ |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 904 | PyThreadState *tstate = _PyThreadState_GET(); |
| 905 | return _PyEval_EvalFrame(tstate, f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 909 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 910 | { |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 911 | PyThreadState *tstate = _PyThreadState_GET(); |
| 912 | return _PyEval_EvalFrame(tstate, f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 915 | |
| 916 | /* Handle signals, pending calls, GIL drop request |
| 917 | and asynchronous exception */ |
| 918 | static int |
| 919 | eval_frame_handle_pending(PyThreadState *tstate) |
| 920 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 921 | _PyRuntimeState * const runtime = &_PyRuntime; |
| 922 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 923 | |
| 924 | /* Pending signals */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 925 | if (_Py_atomic_load_relaxed(&ceval->signals_pending)) { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 926 | if (handle_signals(tstate) != 0) { |
| 927 | return -1; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | /* Pending calls */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 932 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 933 | if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) { |
| 934 | if (make_pending_calls(tstate) != 0) { |
| 935 | return -1; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* GIL drop request */ |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 940 | if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 941 | /* Give another thread a chance */ |
| 942 | if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { |
| 943 | Py_FatalError("tstate mix-up"); |
| 944 | } |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 945 | drop_gil(ceval, ceval2, tstate); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 946 | |
| 947 | /* Other threads may run now */ |
| 948 | |
| 949 | take_gil(tstate); |
| 950 | |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 951 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 952 | (void)_PyThreadState_Swap(&runtime->gilstate, tstate); |
| 953 | #else |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 954 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
| 955 | Py_FatalError("orphan tstate"); |
| 956 | } |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 957 | #endif |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* Check for asynchronous exception. */ |
| 961 | if (tstate->async_exc != NULL) { |
| 962 | PyObject *exc = tstate->async_exc; |
| 963 | tstate->async_exc = NULL; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 964 | UNSIGNAL_ASYNC_EXC(tstate->interp); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 965 | _PyErr_SetNone(tstate, exc); |
| 966 | Py_DECREF(exc); |
| 967 | return -1; |
| 968 | } |
| 969 | |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 970 | #ifdef MS_WINDOWS |
| 971 | // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a |
| 972 | // different thread than the Python thread, in which case |
| 973 | // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the |
| 974 | // current Python thread with the correct _Py_ThreadCanHandleSignals() |
| 975 | // value. It prevents to interrupt the eval loop at every instruction if |
| 976 | // the current Python thread cannot handle signals (if |
| 977 | // _Py_ThreadCanHandleSignals() is false). |
| 978 | COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2); |
| 979 | #endif |
| 980 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 981 | return 0; |
| 982 | } |
| 983 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 984 | PyObject* _Py_HOT_FUNCTION |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 985 | _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag) |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 986 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 987 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 988 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 989 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 991 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 992 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 993 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 994 | int opcode; /* Current opcode */ |
| 995 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 996 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 998 | struct _ceval_state * const ceval2 = &tstate->interp->ceval; |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 999 | _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | is true when the line being executed has changed. The |
| 1007 | initial values are such as to make this false the first |
| 1008 | time it is tested. */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1009 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1010 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | PyObject *names; |
| 1012 | PyObject *consts; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1013 | _PyOpcache *co_opcache; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1014 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 1015 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1016 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 1017 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1018 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1019 | /* Computed GOTOs, or |
| 1020 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 1021 | using gcc's labels-as-values extension |
| 1022 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 1023 | |
| 1024 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1026 | combined with a lookup table of jump addresses. However, since the |
| 1027 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 1028 | hard time making the right prediction for where to jump next (actually, |
| 1029 | it will be always wrong except in the uncommon case of a sequence of |
| 1030 | several identical opcodes). |
| 1031 | |
| 1032 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 1033 | indirect jump instruction at the end of each opcode. Since the jump |
| 1034 | instruction is at a different address for each opcode, the CPU will make a |
| 1035 | separate prediction for each of these instructions, which is equivalent to |
| 1036 | predicting the second opcode of each opcode pair. These predictions have |
| 1037 | a much better chance to turn out valid, especially in small bytecode loops. |
| 1038 | |
| 1039 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1041 | and potentially many more instructions (depending on the pipeline width). |
| 1042 | A correctly predicted branch, however, is nearly free. |
| 1043 | |
| 1044 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 1045 | faster than the normal "switch" version, depending on the compiler and the |
| 1046 | CPU architecture. |
| 1047 | |
| 1048 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 1049 | because it would render the measurements invalid. |
| 1050 | |
| 1051 | |
| 1052 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 1053 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 1054 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 1055 | -fno-crossjumping). |
| 1056 | */ |
| 1057 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1058 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1059 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1060 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1061 | #endif |
| 1062 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1063 | #ifdef HAVE_COMPUTED_GOTOS |
| 1064 | #ifndef USE_COMPUTED_GOTOS |
| 1065 | #define USE_COMPUTED_GOTOS 1 |
| 1066 | #endif |
| 1067 | #else |
| 1068 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 1069 | #error "Computed gotos are not supported on this compiler." |
| 1070 | #endif |
| 1071 | #undef USE_COMPUTED_GOTOS |
| 1072 | #define USE_COMPUTED_GOTOS 0 |
| 1073 | #endif |
| 1074 | |
| 1075 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1076 | /* Import the static jump table */ |
| 1077 | #include "opcode_targets.h" |
| 1078 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1079 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1080 | op: \ |
| 1081 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1083 | #ifdef LLTRACE |
| 1084 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | { \ |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 1086 | if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1088 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1089 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | } \ |
| 1091 | goto fast_next_opcode; \ |
| 1092 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1093 | #else |
| 1094 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | { \ |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 1096 | if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1098 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1099 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | } \ |
| 1101 | goto fast_next_opcode; \ |
| 1102 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1103 | #endif |
| 1104 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1105 | #define DISPATCH() \ |
| 1106 | { \ |
| 1107 | if (!_Py_atomic_load_relaxed(eval_breaker)) { \ |
| 1108 | FAST_DISPATCH(); \ |
| 1109 | } \ |
| 1110 | continue; \ |
| 1111 | } |
| 1112 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1113 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1114 | #define TARGET(op) op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1115 | #define FAST_DISPATCH() goto fast_next_opcode |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1116 | #define DISPATCH() continue |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1117 | #endif |
| 1118 | |
| 1119 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 1120 | /* Tuple access macros */ |
| 1121 | |
| 1122 | #ifndef Py_DEBUG |
| 1123 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 1124 | #else |
| 1125 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 1126 | #endif |
| 1127 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1128 | /* Code access macros */ |
| 1129 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1130 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1131 | #define INSTR_OFFSET() \ |
| 1132 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1133 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1134 | _Py_CODEUNIT word = *next_instr; \ |
| 1135 | opcode = _Py_OPCODE(word); \ |
| 1136 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1137 | next_instr++; \ |
| 1138 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1139 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 1140 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1141 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1142 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | Some opcodes tend to come in pairs thus making it possible to |
| 1144 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1145 | 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] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | Verifying the prediction costs a single high-speed test of a register |
| 1148 | variable against a constant. If the pairing was good, then the |
| 1149 | processor's own internal branch predication has a high likelihood of |
| 1150 | success, resulting in a nearly zero-overhead transition to the |
| 1151 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1152 | including its unpredictable switch-case branch. Combined with the |
| 1153 | processor's internal branch prediction, a successful PREDICT has the |
| 1154 | effect of making the two opcodes run as if they were a single new opcode |
| 1155 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1156 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 1157 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | predictions turned-on and interpret the results as if some opcodes |
| 1159 | had been combined or turn-off predictions so that the opcode frequency |
| 1160 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1161 | |
| 1162 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | the CPU to record separate branch prediction information for each |
| 1164 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1165 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1166 | */ |
| 1167 | |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1168 | #define PREDICT_ID(op) PRED_##op |
| 1169 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1170 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1171 | #define PREDICT(op) if (0) goto PREDICT_ID(op) |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 1172 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1173 | #define PREDICT(op) \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1174 | do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1175 | _Py_CODEUNIT word = *next_instr; \ |
| 1176 | opcode = _Py_OPCODE(word); \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1177 | if (opcode == op) { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1178 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1179 | next_instr++; \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1180 | goto PREDICT_ID(op); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1181 | } \ |
| 1182 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1183 | #endif |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1184 | #define PREDICTED(op) PREDICT_ID(op): |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1185 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1186 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1187 | /* Stack manipulation macros */ |
| 1188 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1189 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 1190 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1191 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 1192 | #define EMPTY() (STACK_LEVEL() == 0) |
| 1193 | #define TOP() (stack_pointer[-1]) |
| 1194 | #define SECOND() (stack_pointer[-2]) |
| 1195 | #define THIRD() (stack_pointer[-3]) |
| 1196 | #define FOURTH() (stack_pointer[-4]) |
| 1197 | #define PEEK(n) (stack_pointer[-(n)]) |
| 1198 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 1199 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 1200 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 1201 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1202 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 1203 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 1204 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1205 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1206 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1208 | lltrace && prtrace(tstate, TOP(), "push")); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1209 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1210 | #define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1211 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1212 | #define STACK_GROW(n) do { \ |
| 1213 | assert(n >= 0); \ |
| 1214 | (void)(BASIC_STACKADJ(n), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1215 | lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1216 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 1217 | } while (0) |
| 1218 | #define STACK_SHRINK(n) do { \ |
| 1219 | assert(n >= 0); \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1220 | (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1221 | (void)(BASIC_STACKADJ(-n)); \ |
| 1222 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 1223 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 1224 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1225 | prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1226 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1227 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1228 | #define PUSH(v) BASIC_PUSH(v) |
| 1229 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1230 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 1231 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1232 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1233 | #endif |
| 1234 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1235 | /* Local variable macros */ |
| 1236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 1238 | |
| 1239 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 1240 | then store the new value; it must copy the old value to a temporary |
| 1241 | value, then store the new value, and then DECREF the temporary value. |
| 1242 | This is because it is possible that during the DECREF the frame is |
| 1243 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1244 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1246 | GETLOCAL(i) = value; \ |
| 1247 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1248 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1249 | |
| 1250 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1252 | PyObject *v = POP(); \ |
| 1253 | Py_XDECREF(v); \ |
| 1254 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1255 | |
| 1256 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1257 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1259 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1261 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1262 | value = POP(); \ |
| 1263 | Py_XDECREF(value); \ |
| 1264 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1265 | exc_info = tstate->exc_info; \ |
| 1266 | type = exc_info->exc_type; \ |
| 1267 | value = exc_info->exc_value; \ |
| 1268 | traceback = exc_info->exc_traceback; \ |
| 1269 | exc_info->exc_type = POP(); \ |
| 1270 | exc_info->exc_value = POP(); \ |
| 1271 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | Py_XDECREF(type); \ |
| 1273 | Py_XDECREF(value); \ |
| 1274 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1275 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1276 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1277 | /* macros for opcode cache */ |
| 1278 | #define OPCACHE_CHECK() \ |
| 1279 | do { \ |
| 1280 | co_opcache = NULL; \ |
| 1281 | if (co->co_opcache != NULL) { \ |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1282 | unsigned char co_opcache_offset = \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1283 | co->co_opcache_map[next_instr - first_instr]; \ |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1284 | if (co_opcache_offset > 0) { \ |
| 1285 | assert(co_opcache_offset <= co->co_opcache_size); \ |
| 1286 | co_opcache = &co->co_opcache[co_opcache_offset - 1]; \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1287 | assert(co_opcache != NULL); \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1288 | } \ |
| 1289 | } \ |
| 1290 | } while (0) |
| 1291 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1292 | #define OPCACHE_DEOPT() \ |
| 1293 | do { \ |
| 1294 | if (co_opcache != NULL) { \ |
| 1295 | co_opcache->optimized = -1; \ |
| 1296 | unsigned char co_opcache_offset = \ |
| 1297 | co->co_opcache_map[next_instr - first_instr]; \ |
| 1298 | assert(co_opcache_offset <= co->co_opcache_size); \ |
| 1299 | co->co_opcache_map[co_opcache_offset] = 0; \ |
| 1300 | co_opcache = NULL; \ |
| 1301 | } \ |
| 1302 | } while (0) |
| 1303 | |
| 1304 | #define OPCACHE_DEOPT_LOAD_ATTR() \ |
| 1305 | do { \ |
| 1306 | if (co_opcache != NULL) { \ |
| 1307 | OPCACHE_STAT_ATTR_DEOPT(); \ |
| 1308 | OPCACHE_DEOPT(); \ |
| 1309 | } \ |
| 1310 | } while (0) |
| 1311 | |
| 1312 | #define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \ |
| 1313 | do { \ |
| 1314 | if (co_opcache != NULL && --co_opcache->optimized <= 0) { \ |
| 1315 | OPCACHE_DEOPT_LOAD_ATTR(); \ |
| 1316 | } \ |
| 1317 | } while (0) |
| 1318 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1319 | #if OPCACHE_STATS |
| 1320 | |
| 1321 | #define OPCACHE_STAT_GLOBAL_HIT() \ |
| 1322 | do { \ |
| 1323 | if (co->co_opcache != NULL) opcache_global_hits++; \ |
| 1324 | } while (0) |
| 1325 | |
| 1326 | #define OPCACHE_STAT_GLOBAL_MISS() \ |
| 1327 | do { \ |
| 1328 | if (co->co_opcache != NULL) opcache_global_misses++; \ |
| 1329 | } while (0) |
| 1330 | |
| 1331 | #define OPCACHE_STAT_GLOBAL_OPT() \ |
| 1332 | do { \ |
| 1333 | if (co->co_opcache != NULL) opcache_global_opts++; \ |
| 1334 | } while (0) |
| 1335 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1336 | #define OPCACHE_STAT_ATTR_HIT() \ |
| 1337 | do { \ |
| 1338 | if (co->co_opcache != NULL) opcache_attr_hits++; \ |
| 1339 | } while (0) |
| 1340 | |
| 1341 | #define OPCACHE_STAT_ATTR_MISS() \ |
| 1342 | do { \ |
| 1343 | if (co->co_opcache != NULL) opcache_attr_misses++; \ |
| 1344 | } while (0) |
| 1345 | |
| 1346 | #define OPCACHE_STAT_ATTR_OPT() \ |
| 1347 | do { \ |
| 1348 | if (co->co_opcache!= NULL) opcache_attr_opts++; \ |
| 1349 | } while (0) |
| 1350 | |
| 1351 | #define OPCACHE_STAT_ATTR_DEOPT() \ |
| 1352 | do { \ |
| 1353 | if (co->co_opcache != NULL) opcache_attr_deopts++; \ |
| 1354 | } while (0) |
| 1355 | |
| 1356 | #define OPCACHE_STAT_ATTR_TOTAL() \ |
| 1357 | do { \ |
| 1358 | if (co->co_opcache != NULL) opcache_attr_total++; \ |
| 1359 | } while (0) |
| 1360 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1361 | #else /* OPCACHE_STATS */ |
| 1362 | |
| 1363 | #define OPCACHE_STAT_GLOBAL_HIT() |
| 1364 | #define OPCACHE_STAT_GLOBAL_MISS() |
| 1365 | #define OPCACHE_STAT_GLOBAL_OPT() |
| 1366 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1367 | #define OPCACHE_STAT_ATTR_HIT() |
| 1368 | #define OPCACHE_STAT_ATTR_MISS() |
| 1369 | #define OPCACHE_STAT_ATTR_OPT() |
| 1370 | #define OPCACHE_STAT_ATTR_DEOPT() |
| 1371 | #define OPCACHE_STAT_ATTR_TOTAL() |
| 1372 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1373 | #endif |
| 1374 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1375 | /* Start of code */ |
| 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | /* push frame */ |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 1378 | if (_Py_EnterRecursiveCall(tstate, "")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | return NULL; |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 1380 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | if (tstate->use_tracing) { |
| 1385 | if (tstate->c_tracefunc != NULL) { |
| 1386 | /* tstate->c_tracefunc, if defined, is a |
| 1387 | function that will be called on *every* entry |
| 1388 | to a code block. Its return value, if not |
| 1389 | None, is a function that will be called at |
| 1390 | the start of each executed line of code. |
| 1391 | (Actually, the function must return itself |
| 1392 | in order to continue tracing.) The trace |
| 1393 | functions are called with three arguments: |
| 1394 | a pointer to the current frame, a string |
| 1395 | indicating why the function is called, and |
| 1396 | an argument which depends on the situation. |
| 1397 | The global trace function is also called |
| 1398 | whenever an exception is detected. */ |
| 1399 | if (call_trace_protected(tstate->c_tracefunc, |
| 1400 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1401 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | /* Trace function raised an error */ |
| 1403 | goto exit_eval_frame; |
| 1404 | } |
| 1405 | } |
| 1406 | if (tstate->c_profilefunc != NULL) { |
| 1407 | /* Similar for c_profilefunc, except it needn't |
| 1408 | return itself and isn't called for "line" events */ |
| 1409 | if (call_trace_protected(tstate->c_profilefunc, |
| 1410 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1411 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | /* Profile function raised an error */ |
| 1413 | goto exit_eval_frame; |
| 1414 | } |
| 1415 | } |
| 1416 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1417 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1418 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 1419 | dtrace_function_entry(f); |
| 1420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | co = f->f_code; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1422 | PyCodeAddressRange bounds; |
| 1423 | _PyCode_InitAddressRange(co, &bounds); |
| 1424 | int instr_prev = -1; |
| 1425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | names = co->co_names; |
| 1427 | consts = co->co_consts; |
| 1428 | fastlocals = f->f_localsplus; |
| 1429 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1430 | assert(PyBytes_Check(co->co_code)); |
| 1431 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1432 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 1433 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1434 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1435 | /* |
| 1436 | f->f_lasti refers to the index of the last instruction, |
| 1437 | 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] | 1438 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1439 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1440 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1443 | direct succession without updating f->f_lasti. A successful |
| 1444 | prediction effectively links the two codes together as if they |
| 1445 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1446 | the first code in the pair (for instance, GET_ITER followed by |
| 1447 | 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] | 1448 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1449 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1450 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1451 | next_instr = first_instr; |
| 1452 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1453 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1454 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1455 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 1456 | stack_pointer = f->f_valuestack + f->f_stackdepth; |
| 1457 | /* Set f->f_stackdepth to -1. |
| 1458 | * Update when returning or calling trace function. |
| 1459 | Having f_stackdepth <= 0 ensures that invalid |
| 1460 | values are not visible to the cycle GC. |
| 1461 | We choose -1 rather than 0 to assist debugging. |
| 1462 | */ |
| 1463 | f->f_stackdepth = -1; |
| 1464 | f->f_state = FRAME_EXECUTING; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1465 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1466 | if (co->co_opcache_flag < OPCACHE_MIN_RUNS) { |
| 1467 | co->co_opcache_flag++; |
| 1468 | if (co->co_opcache_flag == OPCACHE_MIN_RUNS) { |
| 1469 | if (_PyCode_InitOpcache(co) < 0) { |
Victor Stinner | 2510494 | 2020-04-24 02:43:18 +0200 | [diff] [blame] | 1470 | goto exit_eval_frame; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1471 | } |
| 1472 | #if OPCACHE_STATS |
| 1473 | opcache_code_objects_extra_mem += |
| 1474 | PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) + |
| 1475 | sizeof(_PyOpcache) * co->co_opcache_size; |
| 1476 | opcache_code_objects++; |
| 1477 | #endif |
| 1478 | } |
| 1479 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1480 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1481 | #ifdef LLTRACE |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1482 | { |
| 1483 | int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__); |
| 1484 | if (r < 0) { |
| 1485 | goto exit_eval_frame; |
| 1486 | } |
| 1487 | lltrace = r; |
| 1488 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1489 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1490 | |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1491 | if (throwflag) { /* support for generator.throw() */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1492 | goto error; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1493 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1494 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1495 | #ifdef Py_DEBUG |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 1496 | /* _PyEval_EvalFrameDefault() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 1497 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1498 | caller loses its exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1499 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1500 | #endif |
| 1501 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1502 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1505 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1506 | assert(!_PyErr_Occurred(tstate)); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | /* Do periodic things. Doing this every time through |
| 1509 | the loop would add too much overhead, so we do it |
| 1510 | only every Nth instruction. We also do it if |
Chris Jerdonek | 4a12d12 | 2020-05-14 19:25:45 -0700 | [diff] [blame] | 1511 | ``pending.calls_to_do'' is set, i.e. when an asynchronous |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | event needs attention (e.g. a signal handler or |
| 1513 | async I/O handler); see Py_AddPendingCall() and |
| 1514 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1515 | |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 1516 | if (_Py_atomic_load_relaxed(eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1517 | opcode = _Py_OPCODE(*next_instr); |
| 1518 | if (opcode == SETUP_FINALLY || |
| 1519 | opcode == SETUP_WITH || |
| 1520 | opcode == BEFORE_ASYNC_WITH || |
| 1521 | opcode == YIELD_FROM) { |
| 1522 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 1523 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1524 | - If we're about to enter the 'with:'. It will prevent |
| 1525 | emitting a resource warning in the common idiom |
| 1526 | 'with open(path) as file:'. |
| 1527 | - If we're about to enter the 'async with:'. |
| 1528 | - 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] | 1529 | *very* useful, but might help in some cases and it's |
| 1530 | traditional) |
| 1531 | - If we're resuming a chain of nested 'yield from' or |
| 1532 | 'await' calls, then each frame is parked with YIELD_FROM |
| 1533 | as its next opcode. If the user hit control-C we want to |
| 1534 | wait until we've reached the innermost frame before |
| 1535 | running the signal handler and raising KeyboardInterrupt |
| 1536 | (see bpo-30039). |
| 1537 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | goto fast_next_opcode; |
| 1539 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1540 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1541 | if (eval_frame_handle_pending(tstate) != 0) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1542 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | fast_next_opcode: |
| 1547 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1548 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1549 | if (PyDTrace_LINE_ENABLED()) |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1550 | maybe_dtrace_line(f, &bounds, &instr_prev); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1553 | |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 1554 | if (_Py_TracingPossible(ceval2) && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1555 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1556 | int err; |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 1557 | /* see maybe_call_line_trace() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | for expository comments */ |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 1559 | f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1562 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1563 | tstate, f, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1564 | &bounds, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | /* Reload possibly changed frame fields */ |
| 1566 | JUMPTO(f->f_lasti); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 1567 | stack_pointer = f->f_valuestack+f->f_stackdepth; |
| 1568 | f->f_stackdepth = -1; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1569 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1571 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1575 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1576 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1577 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1578 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1579 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | dxpairs[lastopcode][opcode]++; |
| 1581 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1582 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1584 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1585 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1586 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | if (lltrace) { |
| 1590 | if (HAS_ARG(opcode)) { |
| 1591 | printf("%d: %d, %d\n", |
| 1592 | f->f_lasti, opcode, oparg); |
| 1593 | } |
| 1594 | else { |
| 1595 | printf("%d: %d\n", |
| 1596 | f->f_lasti, opcode); |
| 1597 | } |
| 1598 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1599 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1604 | It is essential that any operation that fails must goto error |
| 1605 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1606 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1607 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1609 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1610 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1611 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1612 | PyObject *value = GETLOCAL(oparg); |
| 1613 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1614 | format_exc_check_arg(tstate, PyExc_UnboundLocalError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1615 | UNBOUNDLOCAL_ERROR_MSG, |
| 1616 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1617 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1619 | Py_INCREF(value); |
| 1620 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1622 | } |
| 1623 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1624 | case TARGET(LOAD_CONST): { |
| 1625 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1626 | PyObject *value = GETITEM(consts, oparg); |
| 1627 | Py_INCREF(value); |
| 1628 | PUSH(value); |
| 1629 | FAST_DISPATCH(); |
| 1630 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1631 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1632 | case TARGET(STORE_FAST): { |
| 1633 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1634 | PyObject *value = POP(); |
| 1635 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1637 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1638 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1639 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1640 | PyObject *value = POP(); |
| 1641 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1643 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1644 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1645 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1646 | PyObject *top = TOP(); |
| 1647 | PyObject *second = SECOND(); |
| 1648 | SET_TOP(second); |
| 1649 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1651 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1652 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1653 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1654 | PyObject *top = TOP(); |
| 1655 | PyObject *second = SECOND(); |
| 1656 | PyObject *third = THIRD(); |
| 1657 | SET_TOP(second); |
| 1658 | SET_SECOND(third); |
| 1659 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1661 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1662 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1663 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1664 | PyObject *top = TOP(); |
| 1665 | PyObject *second = SECOND(); |
| 1666 | PyObject *third = THIRD(); |
| 1667 | PyObject *fourth = FOURTH(); |
| 1668 | SET_TOP(second); |
| 1669 | SET_SECOND(third); |
| 1670 | SET_THIRD(fourth); |
| 1671 | SET_FOURTH(top); |
| 1672 | FAST_DISPATCH(); |
| 1673 | } |
| 1674 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1675 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1676 | PyObject *top = TOP(); |
| 1677 | Py_INCREF(top); |
| 1678 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1680 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1681 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1682 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1683 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1684 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1685 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1686 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1687 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1688 | SET_TOP(top); |
| 1689 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1690 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1691 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1692 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1693 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1694 | PyObject *value = TOP(); |
| 1695 | PyObject *res = PyNumber_Positive(value); |
| 1696 | Py_DECREF(value); |
| 1697 | SET_TOP(res); |
| 1698 | if (res == NULL) |
| 1699 | goto error; |
| 1700 | DISPATCH(); |
| 1701 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1702 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1703 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1704 | PyObject *value = TOP(); |
| 1705 | PyObject *res = PyNumber_Negative(value); |
| 1706 | Py_DECREF(value); |
| 1707 | SET_TOP(res); |
| 1708 | if (res == NULL) |
| 1709 | goto error; |
| 1710 | DISPATCH(); |
| 1711 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1712 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1713 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1714 | PyObject *value = TOP(); |
| 1715 | int err = PyObject_IsTrue(value); |
| 1716 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | if (err == 0) { |
| 1718 | Py_INCREF(Py_True); |
| 1719 | SET_TOP(Py_True); |
| 1720 | DISPATCH(); |
| 1721 | } |
| 1722 | else if (err > 0) { |
| 1723 | Py_INCREF(Py_False); |
| 1724 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | DISPATCH(); |
| 1726 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1727 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1728 | goto error; |
| 1729 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1730 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1731 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1732 | PyObject *value = TOP(); |
| 1733 | PyObject *res = PyNumber_Invert(value); |
| 1734 | Py_DECREF(value); |
| 1735 | SET_TOP(res); |
| 1736 | if (res == NULL) |
| 1737 | goto error; |
| 1738 | DISPATCH(); |
| 1739 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1740 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1741 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1742 | PyObject *exp = POP(); |
| 1743 | PyObject *base = TOP(); |
| 1744 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1745 | Py_DECREF(base); |
| 1746 | Py_DECREF(exp); |
| 1747 | SET_TOP(res); |
| 1748 | if (res == NULL) |
| 1749 | goto error; |
| 1750 | DISPATCH(); |
| 1751 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1752 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1753 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1754 | PyObject *right = POP(); |
| 1755 | PyObject *left = TOP(); |
| 1756 | PyObject *res = PyNumber_Multiply(left, right); |
| 1757 | Py_DECREF(left); |
| 1758 | Py_DECREF(right); |
| 1759 | SET_TOP(res); |
| 1760 | if (res == NULL) |
| 1761 | goto error; |
| 1762 | DISPATCH(); |
| 1763 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1764 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1765 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1766 | PyObject *right = POP(); |
| 1767 | PyObject *left = TOP(); |
| 1768 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1769 | Py_DECREF(left); |
| 1770 | Py_DECREF(right); |
| 1771 | SET_TOP(res); |
| 1772 | if (res == NULL) |
| 1773 | goto error; |
| 1774 | DISPATCH(); |
| 1775 | } |
| 1776 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1777 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1778 | PyObject *divisor = POP(); |
| 1779 | PyObject *dividend = TOP(); |
| 1780 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1781 | Py_DECREF(dividend); |
| 1782 | Py_DECREF(divisor); |
| 1783 | SET_TOP(quotient); |
| 1784 | if (quotient == NULL) |
| 1785 | goto error; |
| 1786 | DISPATCH(); |
| 1787 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1788 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1789 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1790 | PyObject *divisor = POP(); |
| 1791 | PyObject *dividend = TOP(); |
| 1792 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1793 | Py_DECREF(dividend); |
| 1794 | Py_DECREF(divisor); |
| 1795 | SET_TOP(quotient); |
| 1796 | if (quotient == NULL) |
| 1797 | goto error; |
| 1798 | DISPATCH(); |
| 1799 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1800 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1801 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1802 | PyObject *divisor = POP(); |
| 1803 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1804 | PyObject *res; |
| 1805 | if (PyUnicode_CheckExact(dividend) && ( |
| 1806 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1807 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1808 | // (see issue28598) |
| 1809 | res = PyUnicode_Format(dividend, divisor); |
| 1810 | } else { |
| 1811 | res = PyNumber_Remainder(dividend, divisor); |
| 1812 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1813 | Py_DECREF(divisor); |
| 1814 | Py_DECREF(dividend); |
| 1815 | SET_TOP(res); |
| 1816 | if (res == NULL) |
| 1817 | goto error; |
| 1818 | DISPATCH(); |
| 1819 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1820 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1821 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1822 | PyObject *right = POP(); |
| 1823 | PyObject *left = TOP(); |
| 1824 | PyObject *sum; |
Victor Stinner | bd0a08e | 2020-10-01 18:57:37 +0200 | [diff] [blame] | 1825 | /* NOTE(vstinner): Please don't try to micro-optimize int+int on |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1826 | CPython using bytecode, it is simply worthless. |
| 1827 | See http://bugs.python.org/issue21955 and |
| 1828 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1829 | no patch shown any impact on a realistic benchmark, only a minor |
| 1830 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1831 | if (PyUnicode_CheckExact(left) && |
| 1832 | PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1833 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1834 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1835 | } |
| 1836 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1837 | sum = PyNumber_Add(left, right); |
| 1838 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1839 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1840 | Py_DECREF(right); |
| 1841 | SET_TOP(sum); |
| 1842 | if (sum == NULL) |
| 1843 | goto error; |
| 1844 | DISPATCH(); |
| 1845 | } |
| 1846 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1847 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1848 | PyObject *right = POP(); |
| 1849 | PyObject *left = TOP(); |
| 1850 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1851 | Py_DECREF(right); |
| 1852 | Py_DECREF(left); |
| 1853 | SET_TOP(diff); |
| 1854 | if (diff == NULL) |
| 1855 | goto error; |
| 1856 | DISPATCH(); |
| 1857 | } |
| 1858 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1859 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1860 | PyObject *sub = POP(); |
| 1861 | PyObject *container = TOP(); |
| 1862 | PyObject *res = PyObject_GetItem(container, sub); |
| 1863 | Py_DECREF(container); |
| 1864 | Py_DECREF(sub); |
| 1865 | SET_TOP(res); |
| 1866 | if (res == NULL) |
| 1867 | goto error; |
| 1868 | DISPATCH(); |
| 1869 | } |
| 1870 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1871 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1872 | PyObject *right = POP(); |
| 1873 | PyObject *left = TOP(); |
| 1874 | PyObject *res = PyNumber_Lshift(left, right); |
| 1875 | Py_DECREF(left); |
| 1876 | Py_DECREF(right); |
| 1877 | SET_TOP(res); |
| 1878 | if (res == NULL) |
| 1879 | goto error; |
| 1880 | DISPATCH(); |
| 1881 | } |
| 1882 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1883 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1884 | PyObject *right = POP(); |
| 1885 | PyObject *left = TOP(); |
| 1886 | PyObject *res = PyNumber_Rshift(left, right); |
| 1887 | Py_DECREF(left); |
| 1888 | Py_DECREF(right); |
| 1889 | SET_TOP(res); |
| 1890 | if (res == NULL) |
| 1891 | goto error; |
| 1892 | DISPATCH(); |
| 1893 | } |
| 1894 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1895 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1896 | PyObject *right = POP(); |
| 1897 | PyObject *left = TOP(); |
| 1898 | PyObject *res = PyNumber_And(left, right); |
| 1899 | Py_DECREF(left); |
| 1900 | Py_DECREF(right); |
| 1901 | SET_TOP(res); |
| 1902 | if (res == NULL) |
| 1903 | goto error; |
| 1904 | DISPATCH(); |
| 1905 | } |
| 1906 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1907 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1908 | PyObject *right = POP(); |
| 1909 | PyObject *left = TOP(); |
| 1910 | PyObject *res = PyNumber_Xor(left, right); |
| 1911 | Py_DECREF(left); |
| 1912 | Py_DECREF(right); |
| 1913 | SET_TOP(res); |
| 1914 | if (res == NULL) |
| 1915 | goto error; |
| 1916 | DISPATCH(); |
| 1917 | } |
| 1918 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1919 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1920 | PyObject *right = POP(); |
| 1921 | PyObject *left = TOP(); |
| 1922 | PyObject *res = PyNumber_Or(left, right); |
| 1923 | Py_DECREF(left); |
| 1924 | Py_DECREF(right); |
| 1925 | SET_TOP(res); |
| 1926 | if (res == NULL) |
| 1927 | goto error; |
| 1928 | DISPATCH(); |
| 1929 | } |
| 1930 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1931 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1932 | PyObject *v = POP(); |
| 1933 | PyObject *list = PEEK(oparg); |
| 1934 | int err; |
| 1935 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1937 | if (err != 0) |
| 1938 | goto error; |
| 1939 | PREDICT(JUMP_ABSOLUTE); |
| 1940 | DISPATCH(); |
| 1941 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1942 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1943 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1944 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1945 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1946 | int err; |
| 1947 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1949 | if (err != 0) |
| 1950 | goto error; |
| 1951 | PREDICT(JUMP_ABSOLUTE); |
| 1952 | DISPATCH(); |
| 1953 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1954 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1955 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1956 | PyObject *exp = POP(); |
| 1957 | PyObject *base = TOP(); |
| 1958 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1959 | Py_DECREF(base); |
| 1960 | Py_DECREF(exp); |
| 1961 | SET_TOP(res); |
| 1962 | if (res == NULL) |
| 1963 | goto error; |
| 1964 | DISPATCH(); |
| 1965 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1966 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1967 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1968 | PyObject *right = POP(); |
| 1969 | PyObject *left = TOP(); |
| 1970 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1971 | Py_DECREF(left); |
| 1972 | Py_DECREF(right); |
| 1973 | SET_TOP(res); |
| 1974 | if (res == NULL) |
| 1975 | goto error; |
| 1976 | DISPATCH(); |
| 1977 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1978 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1979 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1980 | PyObject *right = POP(); |
| 1981 | PyObject *left = TOP(); |
| 1982 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1983 | Py_DECREF(left); |
| 1984 | Py_DECREF(right); |
| 1985 | SET_TOP(res); |
| 1986 | if (res == NULL) |
| 1987 | goto error; |
| 1988 | DISPATCH(); |
| 1989 | } |
| 1990 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1991 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1992 | PyObject *divisor = POP(); |
| 1993 | PyObject *dividend = TOP(); |
| 1994 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1995 | Py_DECREF(dividend); |
| 1996 | Py_DECREF(divisor); |
| 1997 | SET_TOP(quotient); |
| 1998 | if (quotient == NULL) |
| 1999 | goto error; |
| 2000 | DISPATCH(); |
| 2001 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2002 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2003 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2004 | PyObject *divisor = POP(); |
| 2005 | PyObject *dividend = TOP(); |
| 2006 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 2007 | Py_DECREF(dividend); |
| 2008 | Py_DECREF(divisor); |
| 2009 | SET_TOP(quotient); |
| 2010 | if (quotient == NULL) |
| 2011 | goto error; |
| 2012 | DISPATCH(); |
| 2013 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2014 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2015 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2016 | PyObject *right = POP(); |
| 2017 | PyObject *left = TOP(); |
| 2018 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 2019 | Py_DECREF(left); |
| 2020 | Py_DECREF(right); |
| 2021 | SET_TOP(mod); |
| 2022 | if (mod == NULL) |
| 2023 | goto error; |
| 2024 | DISPATCH(); |
| 2025 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2026 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2027 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2028 | PyObject *right = POP(); |
| 2029 | PyObject *left = TOP(); |
| 2030 | PyObject *sum; |
| 2031 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2032 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2033 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 2034 | } |
| 2035 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2036 | sum = PyNumber_InPlaceAdd(left, right); |
| 2037 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 2038 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2039 | Py_DECREF(right); |
| 2040 | SET_TOP(sum); |
| 2041 | if (sum == NULL) |
| 2042 | goto error; |
| 2043 | DISPATCH(); |
| 2044 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2045 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2046 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2047 | PyObject *right = POP(); |
| 2048 | PyObject *left = TOP(); |
| 2049 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 2050 | Py_DECREF(left); |
| 2051 | Py_DECREF(right); |
| 2052 | SET_TOP(diff); |
| 2053 | if (diff == NULL) |
| 2054 | goto error; |
| 2055 | DISPATCH(); |
| 2056 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2057 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2058 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2059 | PyObject *right = POP(); |
| 2060 | PyObject *left = TOP(); |
| 2061 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 2062 | Py_DECREF(left); |
| 2063 | Py_DECREF(right); |
| 2064 | SET_TOP(res); |
| 2065 | if (res == NULL) |
| 2066 | goto error; |
| 2067 | DISPATCH(); |
| 2068 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2069 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2070 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2071 | PyObject *right = POP(); |
| 2072 | PyObject *left = TOP(); |
| 2073 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 2074 | Py_DECREF(left); |
| 2075 | Py_DECREF(right); |
| 2076 | SET_TOP(res); |
| 2077 | if (res == NULL) |
| 2078 | goto error; |
| 2079 | DISPATCH(); |
| 2080 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2081 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2082 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2083 | PyObject *right = POP(); |
| 2084 | PyObject *left = TOP(); |
| 2085 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 2086 | Py_DECREF(left); |
| 2087 | Py_DECREF(right); |
| 2088 | SET_TOP(res); |
| 2089 | if (res == NULL) |
| 2090 | goto error; |
| 2091 | DISPATCH(); |
| 2092 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2093 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2094 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2095 | PyObject *right = POP(); |
| 2096 | PyObject *left = TOP(); |
| 2097 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 2098 | Py_DECREF(left); |
| 2099 | Py_DECREF(right); |
| 2100 | SET_TOP(res); |
| 2101 | if (res == NULL) |
| 2102 | goto error; |
| 2103 | DISPATCH(); |
| 2104 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2105 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2106 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2107 | PyObject *right = POP(); |
| 2108 | PyObject *left = TOP(); |
| 2109 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 2110 | Py_DECREF(left); |
| 2111 | Py_DECREF(right); |
| 2112 | SET_TOP(res); |
| 2113 | if (res == NULL) |
| 2114 | goto error; |
| 2115 | DISPATCH(); |
| 2116 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2117 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2118 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2119 | PyObject *sub = TOP(); |
| 2120 | PyObject *container = SECOND(); |
| 2121 | PyObject *v = THIRD(); |
| 2122 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2123 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2124 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2125 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2127 | Py_DECREF(container); |
| 2128 | Py_DECREF(sub); |
| 2129 | if (err != 0) |
| 2130 | goto error; |
| 2131 | DISPATCH(); |
| 2132 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2133 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2134 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2135 | PyObject *sub = TOP(); |
| 2136 | PyObject *container = SECOND(); |
| 2137 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2138 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2139 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2140 | err = PyObject_DelItem(container, sub); |
| 2141 | Py_DECREF(container); |
| 2142 | Py_DECREF(sub); |
| 2143 | if (err != 0) |
| 2144 | goto error; |
| 2145 | DISPATCH(); |
| 2146 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 2147 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2148 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 2149 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2150 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 2151 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 2152 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2153 | if (hook == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2154 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2155 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2156 | Py_DECREF(value); |
| 2157 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2158 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 2159 | res = PyObject_CallOneArg(hook, value); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2160 | Py_DECREF(value); |
| 2161 | if (res == NULL) |
| 2162 | goto error; |
| 2163 | Py_DECREF(res); |
| 2164 | DISPATCH(); |
| 2165 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 2166 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2167 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2168 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | switch (oparg) { |
| 2170 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2171 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2172 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2174 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2175 | /* fall through */ |
| 2176 | case 0: |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 2177 | if (do_raise(tstate, exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2178 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2179 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2182 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2183 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2186 | goto error; |
| 2187 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2188 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2189 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2191 | assert(f->f_iblock == 0); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2192 | assert(EMPTY()); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 2193 | f->f_state = FRAME_RETURNED; |
| 2194 | f->f_stackdepth = 0; |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2195 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2196 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2197 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2198 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2199 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2200 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2201 | PyObject *obj = TOP(); |
| 2202 | PyTypeObject *type = Py_TYPE(obj); |
| 2203 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2204 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2205 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2206 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2207 | |
| 2208 | if (getter != NULL) { |
| 2209 | iter = (*getter)(obj); |
| 2210 | Py_DECREF(obj); |
| 2211 | if (iter == NULL) { |
| 2212 | SET_TOP(NULL); |
| 2213 | goto error; |
| 2214 | } |
| 2215 | } |
| 2216 | else { |
| 2217 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2218 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2219 | "'async for' requires an object with " |
| 2220 | "__aiter__ method, got %.100s", |
| 2221 | type->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2222 | Py_DECREF(obj); |
| 2223 | goto error; |
| 2224 | } |
| 2225 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 2226 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 2227 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2228 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 2229 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2230 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2231 | "'async for' received an object from __aiter__ " |
| 2232 | "that does not implement __anext__: %.100s", |
| 2233 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2234 | Py_DECREF(iter); |
| 2235 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2236 | } |
| 2237 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 2238 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2239 | DISPATCH(); |
| 2240 | } |
| 2241 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2242 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2243 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2244 | PyObject *next_iter = NULL; |
| 2245 | PyObject *awaitable = NULL; |
| 2246 | PyObject *aiter = TOP(); |
| 2247 | PyTypeObject *type = Py_TYPE(aiter); |
| 2248 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2249 | if (PyAsyncGen_CheckExact(aiter)) { |
| 2250 | awaitable = type->tp_as_async->am_anext(aiter); |
| 2251 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2252 | goto error; |
| 2253 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2254 | } else { |
| 2255 | if (type->tp_as_async != NULL){ |
| 2256 | getter = type->tp_as_async->am_anext; |
| 2257 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2258 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2259 | if (getter != NULL) { |
| 2260 | next_iter = (*getter)(aiter); |
| 2261 | if (next_iter == NULL) { |
| 2262 | goto error; |
| 2263 | } |
| 2264 | } |
| 2265 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2266 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2267 | "'async for' requires an iterator with " |
| 2268 | "__anext__ method, got %.100s", |
| 2269 | type->tp_name); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2270 | goto error; |
| 2271 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2272 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2273 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 2274 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 2275 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2276 | PyExc_TypeError, |
| 2277 | "'async for' received an invalid object " |
| 2278 | "from __anext__: %.100s", |
| 2279 | Py_TYPE(next_iter)->tp_name); |
| 2280 | |
| 2281 | Py_DECREF(next_iter); |
| 2282 | goto error; |
| 2283 | } else { |
| 2284 | Py_DECREF(next_iter); |
| 2285 | } |
| 2286 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2287 | |
| 2288 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2289 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2290 | DISPATCH(); |
| 2291 | } |
| 2292 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2293 | case TARGET(GET_AWAITABLE): { |
| 2294 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2295 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2296 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2297 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2298 | if (iter == NULL) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2299 | int opcode_at_minus_3 = 0; |
| 2300 | if ((next_instr - first_instr) > 2) { |
| 2301 | opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]); |
| 2302 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2303 | format_awaitable_error(tstate, Py_TYPE(iterable), |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2304 | opcode_at_minus_3, |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2305 | _Py_OPCODE(next_instr[-2])); |
| 2306 | } |
| 2307 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2308 | Py_DECREF(iterable); |
| 2309 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2310 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2311 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2312 | if (yf != NULL) { |
| 2313 | /* `iter` is a coroutine object that is being |
| 2314 | awaited, `yf` is a pointer to the current awaitable |
| 2315 | being awaited on. */ |
| 2316 | Py_DECREF(yf); |
| 2317 | Py_CLEAR(iter); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2318 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2319 | "coroutine is being awaited already"); |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2320 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2321 | } |
| 2322 | } |
| 2323 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2324 | SET_TOP(iter); /* Even if it's NULL */ |
| 2325 | |
| 2326 | if (iter == NULL) { |
| 2327 | goto error; |
| 2328 | } |
| 2329 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2330 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2331 | DISPATCH(); |
| 2332 | } |
| 2333 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2334 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2335 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2336 | PyObject *receiver = TOP(); |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 2337 | PySendResult gen_status; |
| 2338 | if (tstate->c_tracefunc == NULL) { |
| 2339 | gen_status = PyIter_Send(receiver, v, &retval); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2340 | } else { |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 2341 | _Py_IDENTIFIER(send); |
| 2342 | if (v == Py_None && PyIter_Check(receiver)) { |
| 2343 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2344 | } |
| 2345 | else { |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 2346 | retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2347 | } |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2348 | if (retval == NULL) { |
| 2349 | if (tstate->c_tracefunc != NULL |
| 2350 | && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) |
| 2351 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
| 2352 | if (_PyGen_FetchStopIterationValue(&retval) == 0) { |
| 2353 | gen_status = PYGEN_RETURN; |
| 2354 | } |
| 2355 | else { |
| 2356 | gen_status = PYGEN_ERROR; |
| 2357 | } |
| 2358 | } |
| 2359 | else { |
| 2360 | gen_status = PYGEN_NEXT; |
| 2361 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2362 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2363 | Py_DECREF(v); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2364 | if (gen_status == PYGEN_ERROR) { |
| 2365 | assert (retval == NULL); |
| 2366 | goto error; |
| 2367 | } |
| 2368 | if (gen_status == PYGEN_RETURN) { |
| 2369 | assert (retval != NULL); |
| 2370 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2371 | Py_DECREF(receiver); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2372 | SET_TOP(retval); |
| 2373 | retval = NULL; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2374 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2375 | } |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2376 | assert (gen_status == PYGEN_NEXT); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2377 | /* receiver remains on stack, retval is value to be yielded */ |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2378 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 2379 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 2380 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 2381 | f->f_state = FRAME_SUSPENDED; |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 2382 | f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2383 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2384 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2385 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2386 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2388 | |
| 2389 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2390 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2391 | Py_DECREF(retval); |
| 2392 | if (w == NULL) { |
| 2393 | retval = NULL; |
| 2394 | goto error; |
| 2395 | } |
| 2396 | retval = w; |
| 2397 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 2398 | f->f_state = FRAME_SUSPENDED; |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 2399 | f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2400 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2401 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2402 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2403 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2404 | PyObject *type, *value, *traceback; |
| 2405 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2406 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2407 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2408 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2409 | "popped block is not an except handler"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2410 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2412 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 2413 | STACK_LEVEL() <= (b)->b_level + 4); |
| 2414 | exc_info = tstate->exc_info; |
| 2415 | type = exc_info->exc_type; |
| 2416 | value = exc_info->exc_value; |
| 2417 | traceback = exc_info->exc_traceback; |
| 2418 | exc_info->exc_type = POP(); |
| 2419 | exc_info->exc_value = POP(); |
| 2420 | exc_info->exc_traceback = POP(); |
| 2421 | Py_XDECREF(type); |
| 2422 | Py_XDECREF(value); |
| 2423 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2425 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2426 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2427 | case TARGET(POP_BLOCK): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2428 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2430 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2431 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2432 | case TARGET(RERAISE): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2433 | PyObject *exc = POP(); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2434 | PyObject *val = POP(); |
| 2435 | PyObject *tb = POP(); |
| 2436 | assert(PyExceptionClass_Check(exc)); |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 2437 | _PyErr_Restore(tstate, exc, val, tb); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2438 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2439 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2440 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2441 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2442 | PyObject *exc = POP(); |
| 2443 | assert(PyExceptionClass_Check(exc)); |
| 2444 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2445 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2446 | assert(b->b_type == EXCEPT_HANDLER); |
| 2447 | Py_DECREF(exc); |
| 2448 | UNWIND_EXCEPT_HANDLER(b); |
| 2449 | Py_DECREF(POP()); |
| 2450 | JUMPBY(oparg); |
| 2451 | FAST_DISPATCH(); |
| 2452 | } |
| 2453 | else { |
| 2454 | PyObject *val = POP(); |
| 2455 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2456 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2457 | goto exception_unwind; |
| 2458 | } |
| 2459 | } |
| 2460 | |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 2461 | case TARGET(LOAD_ASSERTION_ERROR): { |
| 2462 | PyObject *value = PyExc_AssertionError; |
| 2463 | Py_INCREF(value); |
| 2464 | PUSH(value); |
| 2465 | FAST_DISPATCH(); |
| 2466 | } |
| 2467 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2468 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2469 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2470 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2471 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2472 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2473 | bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2474 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2475 | if (!_PyErr_Occurred(tstate)) { |
| 2476 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2477 | "__build_class__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2478 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2479 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2480 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2481 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2482 | } |
| 2483 | else { |
| 2484 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2485 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2486 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2487 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2488 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2489 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
| 2490 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2491 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2492 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2493 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2494 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2495 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2496 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2497 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2498 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2499 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2500 | PyObject *name = GETITEM(names, oparg); |
| 2501 | PyObject *v = POP(); |
| 2502 | PyObject *ns = f->f_locals; |
| 2503 | int err; |
| 2504 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2505 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2506 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2508 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2510 | if (PyDict_CheckExact(ns)) |
| 2511 | err = PyDict_SetItem(ns, name, v); |
| 2512 | else |
| 2513 | err = PyObject_SetItem(ns, name, v); |
| 2514 | Py_DECREF(v); |
| 2515 | if (err != 0) |
| 2516 | goto error; |
| 2517 | DISPATCH(); |
| 2518 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2519 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2520 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2521 | PyObject *name = GETITEM(names, oparg); |
| 2522 | PyObject *ns = f->f_locals; |
| 2523 | int err; |
| 2524 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2525 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2526 | "no locals when deleting %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2527 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2529 | err = PyObject_DelItem(ns, name); |
| 2530 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2531 | format_exc_check_arg(tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2532 | NAME_ERROR_MSG, |
| 2533 | name); |
| 2534 | goto error; |
| 2535 | } |
| 2536 | DISPATCH(); |
| 2537 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2538 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2539 | case TARGET(UNPACK_SEQUENCE): { |
| 2540 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2541 | PyObject *seq = POP(), *item, **items; |
| 2542 | if (PyTuple_CheckExact(seq) && |
| 2543 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2544 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2546 | item = items[oparg]; |
| 2547 | Py_INCREF(item); |
| 2548 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2550 | } else if (PyList_CheckExact(seq) && |
| 2551 | PyList_GET_SIZE(seq) == oparg) { |
| 2552 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2554 | item = items[oparg]; |
| 2555 | Py_INCREF(item); |
| 2556 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2558 | } else if (unpack_iterable(tstate, seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2560 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | } else { |
| 2562 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2563 | Py_DECREF(seq); |
| 2564 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2566 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2567 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2568 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2569 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2570 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2571 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2572 | PyObject *seq = POP(); |
| 2573 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2574 | if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2575 | stack_pointer + totalargs)) { |
| 2576 | stack_pointer += totalargs; |
| 2577 | } else { |
| 2578 | Py_DECREF(seq); |
| 2579 | goto error; |
| 2580 | } |
| 2581 | Py_DECREF(seq); |
| 2582 | DISPATCH(); |
| 2583 | } |
| 2584 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2585 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2586 | PyObject *name = GETITEM(names, oparg); |
| 2587 | PyObject *owner = TOP(); |
| 2588 | PyObject *v = SECOND(); |
| 2589 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2590 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2591 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2593 | Py_DECREF(owner); |
| 2594 | if (err != 0) |
| 2595 | goto error; |
| 2596 | DISPATCH(); |
| 2597 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2598 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2599 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2600 | PyObject *name = GETITEM(names, oparg); |
| 2601 | PyObject *owner = POP(); |
| 2602 | int err; |
| 2603 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2604 | Py_DECREF(owner); |
| 2605 | if (err != 0) |
| 2606 | goto error; |
| 2607 | DISPATCH(); |
| 2608 | } |
| 2609 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2610 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2611 | PyObject *name = GETITEM(names, oparg); |
| 2612 | PyObject *v = POP(); |
| 2613 | int err; |
| 2614 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2615 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2616 | if (err != 0) |
| 2617 | goto error; |
| 2618 | DISPATCH(); |
| 2619 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2620 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2621 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2622 | PyObject *name = GETITEM(names, oparg); |
| 2623 | int err; |
| 2624 | err = PyDict_DelItem(f->f_globals, name); |
| 2625 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2626 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
| 2627 | format_exc_check_arg(tstate, PyExc_NameError, |
| 2628 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2629 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2630 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2631 | } |
| 2632 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2633 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2634 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2635 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2636 | PyObject *name = GETITEM(names, oparg); |
| 2637 | PyObject *locals = f->f_locals; |
| 2638 | PyObject *v; |
| 2639 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2640 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2641 | "no locals when loading %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2642 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2644 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2645 | v = PyDict_GetItemWithError(locals, name); |
| 2646 | if (v != NULL) { |
| 2647 | Py_INCREF(v); |
| 2648 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2649 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2650 | goto error; |
| 2651 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | } |
| 2653 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2654 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2655 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2656 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2657 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2658 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | } |
| 2660 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2661 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2662 | v = PyDict_GetItemWithError(f->f_globals, name); |
| 2663 | if (v != NULL) { |
| 2664 | Py_INCREF(v); |
| 2665 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2666 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2667 | goto error; |
| 2668 | } |
| 2669 | else { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2670 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2671 | v = PyDict_GetItemWithError(f->f_builtins, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2672 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2673 | if (!_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2674 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2675 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2676 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2677 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2678 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2679 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2680 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2681 | } |
| 2682 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2683 | v = PyObject_GetItem(f->f_builtins, name); |
| 2684 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2685 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2686 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2687 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2688 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2689 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2690 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2691 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2692 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2695 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2697 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2698 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2699 | case TARGET(LOAD_GLOBAL): { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2700 | PyObject *name; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2701 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2702 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2703 | && PyDict_CheckExact(f->f_builtins)) |
| 2704 | { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2705 | OPCACHE_CHECK(); |
| 2706 | if (co_opcache != NULL && co_opcache->optimized > 0) { |
| 2707 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2708 | |
| 2709 | if (lg->globals_ver == |
| 2710 | ((PyDictObject *)f->f_globals)->ma_version_tag |
| 2711 | && lg->builtins_ver == |
| 2712 | ((PyDictObject *)f->f_builtins)->ma_version_tag) |
| 2713 | { |
| 2714 | PyObject *ptr = lg->ptr; |
| 2715 | OPCACHE_STAT_GLOBAL_HIT(); |
| 2716 | assert(ptr != NULL); |
| 2717 | Py_INCREF(ptr); |
| 2718 | PUSH(ptr); |
| 2719 | DISPATCH(); |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2724 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2725 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2726 | name); |
| 2727 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2728 | if (!_PyErr_OCCURRED()) { |
| 2729 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2730 | * an exception if the key doesn't exist */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2731 | format_exc_check_arg(tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2732 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2733 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2734 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | } |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2736 | |
| 2737 | if (co_opcache != NULL) { |
| 2738 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2739 | |
| 2740 | if (co_opcache->optimized == 0) { |
| 2741 | /* Wasn't optimized before. */ |
| 2742 | OPCACHE_STAT_GLOBAL_OPT(); |
| 2743 | } else { |
| 2744 | OPCACHE_STAT_GLOBAL_MISS(); |
| 2745 | } |
| 2746 | |
| 2747 | co_opcache->optimized = 1; |
| 2748 | lg->globals_ver = |
| 2749 | ((PyDictObject *)f->f_globals)->ma_version_tag; |
| 2750 | lg->builtins_ver = |
| 2751 | ((PyDictObject *)f->f_builtins)->ma_version_tag; |
| 2752 | lg->ptr = v; /* borrowed */ |
| 2753 | } |
| 2754 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2755 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2757 | else { |
| 2758 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2759 | |
| 2760 | /* namespace 1: globals */ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2761 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2762 | v = PyObject_GetItem(f->f_globals, name); |
| 2763 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2764 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2765 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2766 | } |
| 2767 | _PyErr_Clear(tstate); |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2768 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2769 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2770 | v = PyObject_GetItem(f->f_builtins, name); |
| 2771 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2772 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2773 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2774 | tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2775 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2776 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2777 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2781 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2783 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2784 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2785 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2786 | PyObject *v = GETLOCAL(oparg); |
| 2787 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | SETLOCAL(oparg, NULL); |
| 2789 | DISPATCH(); |
| 2790 | } |
| 2791 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2792 | tstate, PyExc_UnboundLocalError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | UNBOUNDLOCAL_ERROR_MSG, |
| 2794 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2795 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2796 | goto error; |
| 2797 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2798 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2799 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2800 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2801 | PyObject *oldobj = PyCell_GET(cell); |
| 2802 | if (oldobj != NULL) { |
| 2803 | PyCell_SET(cell, NULL); |
| 2804 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2805 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2806 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2807 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2808 | goto error; |
| 2809 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2810 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2811 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2812 | PyObject *cell = freevars[oparg]; |
| 2813 | Py_INCREF(cell); |
| 2814 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2816 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2817 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2818 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2819 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2820 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2821 | assert(locals); |
| 2822 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2823 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2824 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2825 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2826 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2827 | value = PyDict_GetItemWithError(locals, name); |
| 2828 | if (value != NULL) { |
| 2829 | Py_INCREF(value); |
| 2830 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2831 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2832 | goto error; |
| 2833 | } |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2834 | } |
| 2835 | else { |
| 2836 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2837 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2838 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2839 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2840 | } |
| 2841 | _PyErr_Clear(tstate); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | if (!value) { |
| 2845 | PyObject *cell = freevars[oparg]; |
| 2846 | value = PyCell_GET(cell); |
| 2847 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2848 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2849 | goto error; |
| 2850 | } |
| 2851 | Py_INCREF(value); |
| 2852 | } |
| 2853 | PUSH(value); |
| 2854 | DISPATCH(); |
| 2855 | } |
| 2856 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2857 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2858 | PyObject *cell = freevars[oparg]; |
| 2859 | PyObject *value = PyCell_GET(cell); |
| 2860 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2861 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2862 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2864 | Py_INCREF(value); |
| 2865 | PUSH(value); |
| 2866 | DISPATCH(); |
| 2867 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2868 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2869 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2870 | PyObject *v = POP(); |
| 2871 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2872 | PyObject *oldobj = PyCell_GET(cell); |
| 2873 | PyCell_SET(cell, v); |
| 2874 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2875 | DISPATCH(); |
| 2876 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2877 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2878 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2879 | PyObject *str; |
| 2880 | PyObject *empty = PyUnicode_New(0, 0); |
| 2881 | if (empty == NULL) { |
| 2882 | goto error; |
| 2883 | } |
| 2884 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2885 | Py_DECREF(empty); |
| 2886 | if (str == NULL) |
| 2887 | goto error; |
| 2888 | while (--oparg >= 0) { |
| 2889 | PyObject *item = POP(); |
| 2890 | Py_DECREF(item); |
| 2891 | } |
| 2892 | PUSH(str); |
| 2893 | DISPATCH(); |
| 2894 | } |
| 2895 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2896 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2897 | PyObject *tup = PyTuple_New(oparg); |
| 2898 | if (tup == NULL) |
| 2899 | goto error; |
| 2900 | while (--oparg >= 0) { |
| 2901 | PyObject *item = POP(); |
| 2902 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2903 | } |
| 2904 | PUSH(tup); |
| 2905 | DISPATCH(); |
| 2906 | } |
| 2907 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2908 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2909 | PyObject *list = PyList_New(oparg); |
| 2910 | if (list == NULL) |
| 2911 | goto error; |
| 2912 | while (--oparg >= 0) { |
| 2913 | PyObject *item = POP(); |
| 2914 | PyList_SET_ITEM(list, oparg, item); |
| 2915 | } |
| 2916 | PUSH(list); |
| 2917 | DISPATCH(); |
| 2918 | } |
| 2919 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2920 | case TARGET(LIST_TO_TUPLE): { |
| 2921 | PyObject *list = POP(); |
| 2922 | PyObject *tuple = PyList_AsTuple(list); |
| 2923 | Py_DECREF(list); |
| 2924 | if (tuple == NULL) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2925 | goto error; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2926 | } |
| 2927 | PUSH(tuple); |
| 2928 | DISPATCH(); |
| 2929 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2930 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2931 | case TARGET(LIST_EXTEND): { |
| 2932 | PyObject *iterable = POP(); |
| 2933 | PyObject *list = PEEK(oparg); |
| 2934 | PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable); |
| 2935 | if (none_val == NULL) { |
| 2936 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 2937 | (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable))) |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2938 | { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 2939 | _PyErr_Clear(tstate); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2940 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2941 | "Value after * must be an iterable, not %.200s", |
| 2942 | Py_TYPE(iterable)->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2943 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2944 | Py_DECREF(iterable); |
| 2945 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2946 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2947 | Py_DECREF(none_val); |
| 2948 | Py_DECREF(iterable); |
| 2949 | DISPATCH(); |
| 2950 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2951 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2952 | case TARGET(SET_UPDATE): { |
| 2953 | PyObject *iterable = POP(); |
| 2954 | PyObject *set = PEEK(oparg); |
| 2955 | int err = _PySet_Update(set, iterable); |
| 2956 | Py_DECREF(iterable); |
| 2957 | if (err < 0) { |
| 2958 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2959 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2960 | DISPATCH(); |
| 2961 | } |
| 2962 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2963 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2964 | PyObject *set = PySet_New(NULL); |
| 2965 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2966 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2967 | if (set == NULL) |
| 2968 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2969 | for (i = oparg; i > 0; i--) { |
| 2970 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2971 | if (err == 0) |
| 2972 | err = PySet_Add(set, item); |
| 2973 | Py_DECREF(item); |
| 2974 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2975 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2976 | if (err != 0) { |
| 2977 | Py_DECREF(set); |
| 2978 | goto error; |
| 2979 | } |
| 2980 | PUSH(set); |
| 2981 | DISPATCH(); |
| 2982 | } |
| 2983 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2984 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2985 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2986 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2987 | if (map == NULL) |
| 2988 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2989 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2990 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2991 | PyObject *key = PEEK(2*i); |
| 2992 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2993 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2994 | if (err != 0) { |
| 2995 | Py_DECREF(map); |
| 2996 | goto error; |
| 2997 | } |
| 2998 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2999 | |
| 3000 | while (oparg--) { |
| 3001 | Py_DECREF(POP()); |
| 3002 | Py_DECREF(POP()); |
| 3003 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3004 | PUSH(map); |
| 3005 | DISPATCH(); |
| 3006 | } |
| 3007 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3008 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3009 | _Py_IDENTIFIER(__annotations__); |
| 3010 | int err; |
| 3011 | PyObject *ann_dict; |
| 3012 | if (f->f_locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3013 | _PyErr_Format(tstate, PyExc_SystemError, |
| 3014 | "no locals found when setting up annotations"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3015 | goto error; |
| 3016 | } |
| 3017 | /* check if __annotations__ in locals()... */ |
| 3018 | if (PyDict_CheckExact(f->f_locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3019 | ann_dict = _PyDict_GetItemIdWithError(f->f_locals, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3020 | &PyId___annotations__); |
| 3021 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3022 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3023 | goto error; |
| 3024 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3025 | /* ...if not, create a new one */ |
| 3026 | ann_dict = PyDict_New(); |
| 3027 | if (ann_dict == NULL) { |
| 3028 | goto error; |
| 3029 | } |
| 3030 | err = _PyDict_SetItemId(f->f_locals, |
| 3031 | &PyId___annotations__, ann_dict); |
| 3032 | Py_DECREF(ann_dict); |
| 3033 | if (err != 0) { |
| 3034 | goto error; |
| 3035 | } |
| 3036 | } |
| 3037 | } |
| 3038 | else { |
| 3039 | /* do the same if locals() is not a dict */ |
| 3040 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 3041 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 3042 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3043 | } |
| 3044 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 3045 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3046 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3047 | goto error; |
| 3048 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3049 | _PyErr_Clear(tstate); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3050 | ann_dict = PyDict_New(); |
| 3051 | if (ann_dict == NULL) { |
| 3052 | goto error; |
| 3053 | } |
| 3054 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 3055 | Py_DECREF(ann_dict); |
| 3056 | if (err != 0) { |
| 3057 | goto error; |
| 3058 | } |
| 3059 | } |
| 3060 | else { |
| 3061 | Py_DECREF(ann_dict); |
| 3062 | } |
| 3063 | } |
| 3064 | DISPATCH(); |
| 3065 | } |
| 3066 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3067 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3068 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3069 | PyObject *map; |
| 3070 | PyObject *keys = TOP(); |
| 3071 | if (!PyTuple_CheckExact(keys) || |
| 3072 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3073 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3074 | "bad BUILD_CONST_KEY_MAP keys argument"); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3075 | goto error; |
| 3076 | } |
| 3077 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 3078 | if (map == NULL) { |
| 3079 | goto error; |
| 3080 | } |
| 3081 | for (i = oparg; i > 0; i--) { |
| 3082 | int err; |
| 3083 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 3084 | PyObject *value = PEEK(i + 1); |
| 3085 | err = PyDict_SetItem(map, key, value); |
| 3086 | if (err != 0) { |
| 3087 | Py_DECREF(map); |
| 3088 | goto error; |
| 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | Py_DECREF(POP()); |
| 3093 | while (oparg--) { |
| 3094 | Py_DECREF(POP()); |
| 3095 | } |
| 3096 | PUSH(map); |
| 3097 | DISPATCH(); |
| 3098 | } |
| 3099 | |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3100 | case TARGET(DICT_UPDATE): { |
| 3101 | PyObject *update = POP(); |
| 3102 | PyObject *dict = PEEK(oparg); |
| 3103 | if (PyDict_Update(dict, update) < 0) { |
| 3104 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 3105 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3106 | "'%.200s' object is not a mapping", |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3107 | Py_TYPE(update)->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3108 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3109 | Py_DECREF(update); |
| 3110 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3111 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3112 | Py_DECREF(update); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3113 | DISPATCH(); |
| 3114 | } |
| 3115 | |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3116 | case TARGET(DICT_MERGE): { |
| 3117 | PyObject *update = POP(); |
| 3118 | PyObject *dict = PEEK(oparg); |
| 3119 | |
| 3120 | if (_PyDict_MergeEx(dict, update, 2) < 0) { |
| 3121 | format_kwargs_error(tstate, PEEK(2 + oparg), update); |
| 3122 | Py_DECREF(update); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 3123 | goto error; |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 3124 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3125 | Py_DECREF(update); |
Brandt Bucher | f185a73 | 2019-09-28 17:12:49 -0700 | [diff] [blame] | 3126 | PREDICT(CALL_FUNCTION_EX); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 3127 | DISPATCH(); |
| 3128 | } |
| 3129 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3130 | case TARGET(MAP_ADD): { |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 3131 | PyObject *value = TOP(); |
| 3132 | PyObject *key = SECOND(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3133 | PyObject *map; |
| 3134 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3135 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 3136 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3137 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 3138 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3139 | Py_DECREF(value); |
| 3140 | Py_DECREF(key); |
| 3141 | if (err != 0) |
| 3142 | goto error; |
| 3143 | PREDICT(JUMP_ABSOLUTE); |
| 3144 | DISPATCH(); |
| 3145 | } |
| 3146 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3147 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3148 | PyObject *name = GETITEM(names, oparg); |
| 3149 | PyObject *owner = TOP(); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3150 | |
| 3151 | PyTypeObject *type = Py_TYPE(owner); |
| 3152 | PyObject *res; |
| 3153 | PyObject **dictptr; |
| 3154 | PyObject *dict; |
| 3155 | _PyOpCodeOpt_LoadAttr *la; |
| 3156 | |
| 3157 | OPCACHE_STAT_ATTR_TOTAL(); |
| 3158 | |
| 3159 | OPCACHE_CHECK(); |
| 3160 | if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 3161 | { |
| 3162 | if (co_opcache->optimized > 0) { |
| 3163 | /* Fast path -- cache hit makes LOAD_ATTR ~30% faster */ |
| 3164 | la = &co_opcache->u.la; |
| 3165 | if (la->type == type && la->tp_version_tag == type->tp_version_tag) |
| 3166 | { |
| 3167 | assert(type->tp_dict != NULL); |
| 3168 | assert(type->tp_dictoffset > 0); |
| 3169 | |
| 3170 | dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset); |
| 3171 | dict = *dictptr; |
| 3172 | if (dict != NULL && PyDict_CheckExact(dict)) { |
| 3173 | Py_ssize_t hint = la->hint; |
| 3174 | Py_INCREF(dict); |
| 3175 | res = NULL; |
| 3176 | la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res); |
| 3177 | |
| 3178 | if (res != NULL) { |
| 3179 | if (la->hint == hint && hint >= 0) { |
| 3180 | /* Our hint has helped -- cache hit. */ |
| 3181 | OPCACHE_STAT_ATTR_HIT(); |
| 3182 | } else { |
| 3183 | /* The hint we provided didn't work. |
| 3184 | Maybe next time? */ |
| 3185 | OPCACHE_MAYBE_DEOPT_LOAD_ATTR(); |
| 3186 | } |
| 3187 | |
| 3188 | Py_INCREF(res); |
| 3189 | SET_TOP(res); |
| 3190 | Py_DECREF(owner); |
| 3191 | Py_DECREF(dict); |
| 3192 | DISPATCH(); |
| 3193 | } else { |
| 3194 | // This attribute can be missing sometimes -- we |
| 3195 | // don't want to optimize this lookup. |
| 3196 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3197 | Py_DECREF(dict); |
| 3198 | } |
| 3199 | } else { |
| 3200 | // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact |
| 3201 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3202 | } |
| 3203 | } else { |
| 3204 | // The type of the object has either been updated, |
| 3205 | // or is different. Maybe it will stabilize? |
| 3206 | OPCACHE_MAYBE_DEOPT_LOAD_ATTR(); |
| 3207 | } |
| 3208 | |
| 3209 | OPCACHE_STAT_ATTR_MISS(); |
| 3210 | } |
| 3211 | |
| 3212 | if (co_opcache != NULL && /* co_opcache can be NULL after a DEOPT() call. */ |
| 3213 | type->tp_getattro == PyObject_GenericGetAttr) |
| 3214 | { |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3215 | Py_ssize_t ret; |
| 3216 | |
| 3217 | if (type->tp_dictoffset > 0) { |
| 3218 | if (type->tp_dict == NULL) { |
| 3219 | if (PyType_Ready(type) < 0) { |
| 3220 | Py_DECREF(owner); |
| 3221 | SET_TOP(NULL); |
| 3222 | goto error; |
| 3223 | } |
| 3224 | } |
Pablo Galindo | 80449f2 | 2020-11-05 09:23:15 +0000 | [diff] [blame] | 3225 | if (_PyType_Lookup(type, name) == NULL) { |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3226 | dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset); |
| 3227 | dict = *dictptr; |
| 3228 | |
| 3229 | if (dict != NULL && PyDict_CheckExact(dict)) { |
| 3230 | Py_INCREF(dict); |
| 3231 | res = NULL; |
| 3232 | ret = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res); |
| 3233 | if (res != NULL) { |
| 3234 | Py_INCREF(res); |
| 3235 | Py_DECREF(dict); |
| 3236 | Py_DECREF(owner); |
| 3237 | SET_TOP(res); |
| 3238 | |
| 3239 | if (co_opcache->optimized == 0) { |
| 3240 | // First time we optimize this opcode. */ |
| 3241 | OPCACHE_STAT_ATTR_OPT(); |
| 3242 | co_opcache->optimized = OPCODE_CACHE_MAX_TRIES; |
| 3243 | } |
| 3244 | |
| 3245 | la = &co_opcache->u.la; |
| 3246 | la->type = type; |
| 3247 | la->tp_version_tag = type->tp_version_tag; |
| 3248 | la->hint = ret; |
| 3249 | |
| 3250 | DISPATCH(); |
| 3251 | } |
| 3252 | Py_DECREF(dict); |
| 3253 | } else { |
| 3254 | // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact |
| 3255 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3256 | } |
| 3257 | } else { |
| 3258 | // We failed to find an attribute without a data-like descriptor |
| 3259 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3260 | } |
| 3261 | } else { |
| 3262 | // The object's class does not have a tp_dictoffset we can use |
| 3263 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3264 | } |
| 3265 | } else if (type->tp_getattro != PyObject_GenericGetAttr) { |
| 3266 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | /* slow path */ |
| 3271 | res = PyObject_GetAttr(owner, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3272 | Py_DECREF(owner); |
| 3273 | SET_TOP(res); |
| 3274 | if (res == NULL) |
| 3275 | goto error; |
| 3276 | DISPATCH(); |
| 3277 | } |
| 3278 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3279 | case TARGET(COMPARE_OP): { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3280 | assert(oparg <= Py_GE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3281 | PyObject *right = POP(); |
| 3282 | PyObject *left = TOP(); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3283 | PyObject *res = PyObject_RichCompare(left, right, oparg); |
| 3284 | SET_TOP(res); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3285 | Py_DECREF(left); |
| 3286 | Py_DECREF(right); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3287 | if (res == NULL) |
| 3288 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | PREDICT(POP_JUMP_IF_FALSE); |
| 3290 | PREDICT(POP_JUMP_IF_TRUE); |
| 3291 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3292 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3293 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3294 | case TARGET(IS_OP): { |
| 3295 | PyObject *right = POP(); |
| 3296 | PyObject *left = TOP(); |
| 3297 | int res = (left == right)^oparg; |
| 3298 | PyObject *b = res ? Py_True : Py_False; |
| 3299 | Py_INCREF(b); |
| 3300 | SET_TOP(b); |
| 3301 | Py_DECREF(left); |
| 3302 | Py_DECREF(right); |
| 3303 | PREDICT(POP_JUMP_IF_FALSE); |
| 3304 | PREDICT(POP_JUMP_IF_TRUE); |
| 3305 | FAST_DISPATCH(); |
| 3306 | } |
| 3307 | |
| 3308 | case TARGET(CONTAINS_OP): { |
| 3309 | PyObject *right = POP(); |
| 3310 | PyObject *left = POP(); |
| 3311 | int res = PySequence_Contains(right, left); |
| 3312 | Py_DECREF(left); |
| 3313 | Py_DECREF(right); |
| 3314 | if (res < 0) { |
| 3315 | goto error; |
| 3316 | } |
| 3317 | PyObject *b = (res^oparg) ? Py_True : Py_False; |
| 3318 | Py_INCREF(b); |
| 3319 | PUSH(b); |
| 3320 | PREDICT(POP_JUMP_IF_FALSE); |
| 3321 | PREDICT(POP_JUMP_IF_TRUE); |
| 3322 | FAST_DISPATCH(); |
| 3323 | } |
| 3324 | |
| 3325 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
| 3326 | "BaseException is not allowed" |
| 3327 | |
| 3328 | case TARGET(JUMP_IF_NOT_EXC_MATCH): { |
| 3329 | PyObject *right = POP(); |
| 3330 | PyObject *left = POP(); |
| 3331 | if (PyTuple_Check(right)) { |
| 3332 | Py_ssize_t i, length; |
| 3333 | length = PyTuple_GET_SIZE(right); |
| 3334 | for (i = 0; i < length; i++) { |
| 3335 | PyObject *exc = PyTuple_GET_ITEM(right, i); |
| 3336 | if (!PyExceptionClass_Check(exc)) { |
| 3337 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3338 | CANNOT_CATCH_MSG); |
| 3339 | Py_DECREF(left); |
| 3340 | Py_DECREF(right); |
| 3341 | goto error; |
| 3342 | } |
| 3343 | } |
| 3344 | } |
| 3345 | else { |
| 3346 | if (!PyExceptionClass_Check(right)) { |
| 3347 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3348 | CANNOT_CATCH_MSG); |
| 3349 | Py_DECREF(left); |
| 3350 | Py_DECREF(right); |
| 3351 | goto error; |
| 3352 | } |
| 3353 | } |
| 3354 | int res = PyErr_GivenExceptionMatches(left, right); |
| 3355 | Py_DECREF(left); |
| 3356 | Py_DECREF(right); |
| 3357 | if (res > 0) { |
| 3358 | /* Exception matches -- Do nothing */; |
| 3359 | } |
| 3360 | else if (res == 0) { |
| 3361 | JUMPTO(oparg); |
| 3362 | } |
| 3363 | else { |
| 3364 | goto error; |
| 3365 | } |
| 3366 | DISPATCH(); |
| 3367 | } |
| 3368 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3369 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3370 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 3371 | PyObject *fromlist = POP(); |
| 3372 | PyObject *level = TOP(); |
| 3373 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3374 | res = import_name(tstate, f, name, fromlist, level); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 3375 | Py_DECREF(level); |
| 3376 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3377 | SET_TOP(res); |
| 3378 | if (res == NULL) |
| 3379 | goto error; |
| 3380 | DISPATCH(); |
| 3381 | } |
| 3382 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3383 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3384 | PyObject *from = POP(), *locals; |
| 3385 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3386 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 3387 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3388 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3389 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3390 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3391 | locals = f->f_locals; |
| 3392 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3393 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3394 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3395 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3396 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3398 | err = import_all_from(tstate, locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3400 | Py_DECREF(from); |
| 3401 | if (err != 0) |
| 3402 | goto error; |
| 3403 | DISPATCH(); |
| 3404 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3405 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3406 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3407 | PyObject *name = GETITEM(names, oparg); |
| 3408 | PyObject *from = TOP(); |
| 3409 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3410 | res = import_from(tstate, from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3411 | PUSH(res); |
| 3412 | if (res == NULL) |
| 3413 | goto error; |
| 3414 | DISPATCH(); |
| 3415 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3416 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3417 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | JUMPBY(oparg); |
| 3419 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3420 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3421 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3422 | case TARGET(POP_JUMP_IF_FALSE): { |
| 3423 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3424 | PyObject *cond = POP(); |
| 3425 | int err; |
| 3426 | if (cond == Py_True) { |
| 3427 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | FAST_DISPATCH(); |
| 3429 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3430 | if (cond == Py_False) { |
| 3431 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | JUMPTO(oparg); |
| 3433 | FAST_DISPATCH(); |
| 3434 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3435 | err = PyObject_IsTrue(cond); |
| 3436 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 3438 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | else if (err == 0) |
| 3440 | JUMPTO(oparg); |
| 3441 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3442 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3443 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3444 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3445 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3446 | case TARGET(POP_JUMP_IF_TRUE): { |
| 3447 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3448 | PyObject *cond = POP(); |
| 3449 | int err; |
| 3450 | if (cond == Py_False) { |
| 3451 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | FAST_DISPATCH(); |
| 3453 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3454 | if (cond == Py_True) { |
| 3455 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3456 | JUMPTO(oparg); |
| 3457 | FAST_DISPATCH(); |
| 3458 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3459 | err = PyObject_IsTrue(cond); |
| 3460 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | JUMPTO(oparg); |
| 3463 | } |
| 3464 | else if (err == 0) |
| 3465 | ; |
| 3466 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3467 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3469 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3470 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3471 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3472 | PyObject *cond = TOP(); |
| 3473 | int err; |
| 3474 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3475 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3476 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3477 | FAST_DISPATCH(); |
| 3478 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3479 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | JUMPTO(oparg); |
| 3481 | FAST_DISPATCH(); |
| 3482 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3483 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3485 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3486 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | } |
| 3488 | else if (err == 0) |
| 3489 | JUMPTO(oparg); |
| 3490 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3491 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3493 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3494 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3495 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3496 | PyObject *cond = TOP(); |
| 3497 | int err; |
| 3498 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3499 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3500 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | FAST_DISPATCH(); |
| 3502 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3503 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | JUMPTO(oparg); |
| 3505 | FAST_DISPATCH(); |
| 3506 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3507 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | JUMPTO(oparg); |
| 3510 | } |
| 3511 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3512 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3513 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | } |
| 3515 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3516 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3518 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3519 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3520 | case TARGET(JUMP_ABSOLUTE): { |
| 3521 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3523 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 3525 | the per-loop checks for signals. By default, this should be turned-off |
| 3526 | because it prevents detection of a control-break in tight loops like |
| 3527 | "while 1: pass". Compile with this option turned-on when you need |
| 3528 | the speed-up and do not need break checking inside tight loops (ones |
| 3529 | that contain only instructions ending with FAST_DISPATCH). |
| 3530 | */ |
| 3531 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3532 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3534 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3535 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3536 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3537 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3539 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3540 | PyObject *iter = PyObject_GetIter(iterable); |
| 3541 | Py_DECREF(iterable); |
| 3542 | SET_TOP(iter); |
| 3543 | if (iter == NULL) |
| 3544 | goto error; |
| 3545 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3546 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3547 | DISPATCH(); |
| 3548 | } |
| 3549 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3550 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3551 | /* before: [obj]; after [getiter(obj)] */ |
| 3552 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3553 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3554 | if (PyCoro_CheckExact(iterable)) { |
| 3555 | /* `iterable` is a coroutine */ |
| 3556 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3557 | /* and it is used in a 'yield from' expression of a |
| 3558 | regular generator. */ |
| 3559 | Py_DECREF(iterable); |
| 3560 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3561 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3562 | "cannot 'yield from' a coroutine object " |
| 3563 | "in a non-coroutine generator"); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3564 | goto error; |
| 3565 | } |
| 3566 | } |
| 3567 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3568 | /* `iterable` is not a generator. */ |
| 3569 | iter = PyObject_GetIter(iterable); |
| 3570 | Py_DECREF(iterable); |
| 3571 | SET_TOP(iter); |
| 3572 | if (iter == NULL) |
| 3573 | goto error; |
| 3574 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3575 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3576 | DISPATCH(); |
| 3577 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3578 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3579 | case TARGET(FOR_ITER): { |
| 3580 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3582 | PyObject *iter = TOP(); |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3583 | PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3584 | if (next != NULL) { |
| 3585 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3586 | PREDICT(STORE_FAST); |
| 3587 | PREDICT(UNPACK_SEQUENCE); |
| 3588 | DISPATCH(); |
| 3589 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3590 | if (_PyErr_Occurred(tstate)) { |
| 3591 | if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3592 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3593 | } |
| 3594 | else if (tstate->c_tracefunc != NULL) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3595 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3596 | } |
| 3597 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | } |
| 3599 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3600 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3601 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | JUMPBY(oparg); |
| 3603 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3604 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3605 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3606 | case TARGET(SETUP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3607 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | STACK_LEVEL()); |
| 3609 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3610 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3611 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3612 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3613 | _Py_IDENTIFIER(__aenter__); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3614 | _Py_IDENTIFIER(__aexit__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3615 | PyObject *mgr = TOP(); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3616 | PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3617 | PyObject *res; |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3618 | if (enter == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3619 | goto error; |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3620 | } |
| 3621 | PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__); |
| 3622 | if (exit == NULL) { |
| 3623 | Py_DECREF(enter); |
| 3624 | goto error; |
| 3625 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3626 | SET_TOP(exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3627 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3628 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3629 | Py_DECREF(enter); |
| 3630 | if (res == NULL) |
| 3631 | goto error; |
| 3632 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3633 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3634 | DISPATCH(); |
| 3635 | } |
| 3636 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3637 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3638 | PyObject *res = POP(); |
| 3639 | /* Setup the finally block before pushing the result |
| 3640 | of __aenter__ on the stack. */ |
| 3641 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3642 | STACK_LEVEL()); |
| 3643 | PUSH(res); |
| 3644 | DISPATCH(); |
| 3645 | } |
| 3646 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3647 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3648 | _Py_IDENTIFIER(__enter__); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3649 | _Py_IDENTIFIER(__exit__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3650 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3651 | PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3652 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3653 | if (enter == NULL) { |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3654 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3655 | } |
| 3656 | PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3657 | if (exit == NULL) { |
| 3658 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3659 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3660 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3661 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3662 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3663 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3664 | Py_DECREF(enter); |
| 3665 | if (res == NULL) |
| 3666 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3667 | /* Setup the finally block before pushing the result |
| 3668 | of __enter__ on the stack. */ |
| 3669 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3670 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3671 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3672 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3673 | DISPATCH(); |
| 3674 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3675 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3676 | case TARGET(WITH_EXCEPT_START): { |
| 3677 | /* At the top of the stack are 7 values: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | - (TOP, SECOND, THIRD) = exc_info() |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3679 | - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3680 | - SEVENTH: the context.__exit__ bound method |
| 3681 | We call SEVENTH(TOP, SECOND, THIRD). |
| 3682 | Then we push again the TOP exception and the __exit__ |
| 3683 | return value. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3684 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3685 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3686 | PyObject *exc, *val, *tb, *res; |
| 3687 | |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3688 | exc = TOP(); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3689 | val = SECOND(); |
| 3690 | tb = THIRD(); |
| 3691 | assert(exc != Py_None); |
| 3692 | assert(!PyLong_Check(exc)); |
| 3693 | exit_func = PEEK(7); |
Jeroen Demeyer | 469d1a7 | 2019-07-03 12:52:21 +0200 | [diff] [blame] | 3694 | PyObject *stack[4] = {NULL, exc, val, tb}; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 3695 | res = PyObject_Vectorcall(exit_func, stack + 1, |
Jeroen Demeyer | 469d1a7 | 2019-07-03 12:52:21 +0200 | [diff] [blame] | 3696 | 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3697 | if (res == NULL) |
| 3698 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3699 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3700 | PUSH(res); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3701 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3702 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3703 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3704 | case TARGET(LOAD_METHOD): { |
Andrey | b021ba5 | 2019-04-29 14:33:26 +1000 | [diff] [blame] | 3705 | /* Designed to work in tandem with CALL_METHOD. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3706 | PyObject *name = GETITEM(names, oparg); |
| 3707 | PyObject *obj = TOP(); |
| 3708 | PyObject *meth = NULL; |
| 3709 | |
| 3710 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3711 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3712 | if (meth == NULL) { |
| 3713 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3714 | goto error; |
| 3715 | } |
| 3716 | |
| 3717 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3718 | /* We can bypass temporary bound method object. |
| 3719 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3720 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3721 | meth | self | arg1 | ... | argN |
| 3722 | */ |
| 3723 | SET_TOP(meth); |
| 3724 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3725 | } |
| 3726 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3727 | /* meth is not an unbound method (but a regular attr, or |
| 3728 | something was returned by a descriptor protocol). Set |
| 3729 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3730 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3731 | |
| 3732 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3733 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3734 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3735 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3736 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3737 | } |
| 3738 | DISPATCH(); |
| 3739 | } |
| 3740 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3741 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3742 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3743 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3744 | |
| 3745 | sp = stack_pointer; |
| 3746 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3747 | meth = PEEK(oparg + 2); |
| 3748 | if (meth == NULL) { |
| 3749 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3750 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3751 | |
| 3752 | Stack layout: |
| 3753 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3754 | ... | NULL | callable | arg1 | ... | argN |
| 3755 | ^- TOP() |
| 3756 | ^- (-oparg) |
| 3757 | ^- (-oparg-1) |
| 3758 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3759 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3760 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3761 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3762 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3763 | res = call_function(tstate, &sp, oparg, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3764 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3765 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3766 | } |
| 3767 | else { |
| 3768 | /* This is a method call. Stack layout: |
| 3769 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3770 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3771 | ^- TOP() |
| 3772 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3773 | ^- (-oparg-1) |
| 3774 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3775 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3776 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3777 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3778 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3779 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3780 | res = call_function(tstate, &sp, oparg + 1, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3781 | stack_pointer = sp; |
| 3782 | } |
| 3783 | |
| 3784 | PUSH(res); |
| 3785 | if (res == NULL) |
| 3786 | goto error; |
| 3787 | DISPATCH(); |
| 3788 | } |
| 3789 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3790 | case TARGET(CALL_FUNCTION): { |
| 3791 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3792 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3793 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3794 | res = call_function(tstate, &sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3795 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3796 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3797 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3798 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3799 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3800 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3801 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3802 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3803 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3804 | PyObject **sp, *res, *names; |
| 3805 | |
| 3806 | names = POP(); |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 3807 | assert(PyTuple_Check(names)); |
| 3808 | assert(PyTuple_GET_SIZE(names) <= oparg); |
| 3809 | /* We assume without checking that names contains only strings */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3811 | res = call_function(tstate, &sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3812 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3813 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3814 | Py_DECREF(names); |
| 3815 | |
| 3816 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3817 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3818 | } |
| 3819 | DISPATCH(); |
| 3820 | } |
| 3821 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3822 | case TARGET(CALL_FUNCTION_EX): { |
Brandt Bucher | f185a73 | 2019-09-28 17:12:49 -0700 | [diff] [blame] | 3823 | PREDICTED(CALL_FUNCTION_EX); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3824 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3825 | if (oparg & 0x01) { |
| 3826 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3827 | if (!PyDict_CheckExact(kwargs)) { |
| 3828 | PyObject *d = PyDict_New(); |
| 3829 | if (d == NULL) |
| 3830 | goto error; |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 3831 | if (_PyDict_MergeEx(d, kwargs, 2) < 0) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3832 | Py_DECREF(d); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3833 | format_kwargs_error(tstate, SECOND(), kwargs); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3834 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3835 | goto error; |
| 3836 | } |
| 3837 | Py_DECREF(kwargs); |
| 3838 | kwargs = d; |
| 3839 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3840 | assert(PyDict_CheckExact(kwargs)); |
| 3841 | } |
| 3842 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3843 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3844 | if (!PyTuple_CheckExact(callargs)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3845 | if (check_args_iterable(tstate, func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3846 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3847 | goto error; |
| 3848 | } |
| 3849 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3850 | if (callargs == NULL) { |
| 3851 | goto error; |
| 3852 | } |
| 3853 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3854 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3855 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3856 | result = do_call_core(tstate, func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3857 | Py_DECREF(func); |
| 3858 | Py_DECREF(callargs); |
| 3859 | Py_XDECREF(kwargs); |
| 3860 | |
| 3861 | SET_TOP(result); |
| 3862 | if (result == NULL) { |
| 3863 | goto error; |
| 3864 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3865 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3867 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3868 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3869 | PyObject *qualname = POP(); |
| 3870 | PyObject *codeobj = POP(); |
| 3871 | PyFunctionObject *func = (PyFunctionObject *) |
| 3872 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3873 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3874 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3875 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3876 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3877 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3879 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3880 | if (oparg & 0x08) { |
| 3881 | assert(PyTuple_CheckExact(TOP())); |
| 3882 | func ->func_closure = POP(); |
| 3883 | } |
| 3884 | if (oparg & 0x04) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 3885 | assert(PyTuple_CheckExact(TOP())); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3886 | func->func_annotations = POP(); |
| 3887 | } |
| 3888 | if (oparg & 0x02) { |
| 3889 | assert(PyDict_CheckExact(TOP())); |
| 3890 | func->func_kwdefaults = POP(); |
| 3891 | } |
| 3892 | if (oparg & 0x01) { |
| 3893 | assert(PyTuple_CheckExact(TOP())); |
| 3894 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3895 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3896 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3897 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3898 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3900 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3901 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3902 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3904 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3905 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3906 | step = NULL; |
| 3907 | stop = POP(); |
| 3908 | start = TOP(); |
| 3909 | slice = PySlice_New(start, stop, step); |
| 3910 | Py_DECREF(start); |
| 3911 | Py_DECREF(stop); |
| 3912 | Py_XDECREF(step); |
| 3913 | SET_TOP(slice); |
| 3914 | if (slice == NULL) |
| 3915 | goto error; |
| 3916 | DISPATCH(); |
| 3917 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3918 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3919 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3920 | /* Handles f-string value formatting. */ |
| 3921 | PyObject *result; |
| 3922 | PyObject *fmt_spec; |
| 3923 | PyObject *value; |
| 3924 | PyObject *(*conv_fn)(PyObject *); |
| 3925 | int which_conversion = oparg & FVC_MASK; |
| 3926 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3927 | |
| 3928 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3929 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3930 | |
| 3931 | /* See if any conversion is specified. */ |
| 3932 | switch (which_conversion) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3933 | case FVC_NONE: conv_fn = NULL; break; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3934 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3935 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3936 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3937 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3938 | _PyErr_Format(tstate, PyExc_SystemError, |
| 3939 | "unexpected conversion flag %d", |
| 3940 | which_conversion); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3941 | goto error; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | /* If there's a conversion function, call it and replace |
| 3945 | value with that result. Otherwise, just use value, |
| 3946 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3947 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3948 | result = conv_fn(value); |
| 3949 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3950 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3951 | Py_XDECREF(fmt_spec); |
| 3952 | goto error; |
| 3953 | } |
| 3954 | value = result; |
| 3955 | } |
| 3956 | |
| 3957 | /* If value is a unicode object, and there's no fmt_spec, |
| 3958 | then we know the result of format(value) is value |
| 3959 | itself. In that case, skip calling format(). I plan to |
| 3960 | move this optimization in to PyObject_Format() |
| 3961 | itself. */ |
| 3962 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3963 | /* Do nothing, just transfer ownership to result. */ |
| 3964 | result = value; |
| 3965 | } else { |
| 3966 | /* Actually call format(). */ |
| 3967 | result = PyObject_Format(value, fmt_spec); |
| 3968 | Py_DECREF(value); |
| 3969 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3970 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3971 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3972 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3973 | } |
| 3974 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3975 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3976 | DISPATCH(); |
| 3977 | } |
| 3978 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3979 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3980 | int oldoparg = oparg; |
| 3981 | NEXTOPARG(); |
| 3982 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3984 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3985 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3986 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3987 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3989 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3990 | default: |
| 3991 | fprintf(stderr, |
| 3992 | "XXX lineno: %d, opcode: %d\n", |
| 3993 | PyFrame_GetLineNumber(f), |
| 3994 | opcode); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3995 | _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3996 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3998 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3999 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4000 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 4001 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 4002 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4003 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4004 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4005 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 4006 | #ifdef NDEBUG |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4007 | if (!_PyErr_Occurred(tstate)) { |
| 4008 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 4009 | "error return without exception set"); |
| 4010 | } |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 4011 | #else |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4012 | assert(_PyErr_Occurred(tstate)); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 4013 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4014 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4015 | /* Log traceback info. */ |
| 4016 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4017 | |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4018 | if (tstate->c_tracefunc != NULL) { |
| 4019 | /* Make sure state is set to FRAME_EXECUTING for tracing */ |
| 4020 | assert(f->f_state == FRAME_EXECUTING); |
| 4021 | f->f_state = FRAME_UNWINDING; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4022 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 4023 | tstate, f); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4024 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4025 | exception_unwind: |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4026 | f->f_state = FRAME_UNWINDING; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4027 | /* Unwind stacks if an exception occurred */ |
| 4028 | while (f->f_iblock > 0) { |
| 4029 | /* Pop the current block. */ |
| 4030 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 4031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | if (b->b_type == EXCEPT_HANDLER) { |
| 4033 | UNWIND_EXCEPT_HANDLER(b); |
| 4034 | continue; |
| 4035 | } |
| 4036 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4037 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4038 | PyObject *exc, *val, *tb; |
| 4039 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4040 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | /* Beware, this invalidates all b->b_* fields */ |
| 4042 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4043 | PUSH(exc_info->exc_traceback); |
| 4044 | PUSH(exc_info->exc_value); |
| 4045 | if (exc_info->exc_type != NULL) { |
| 4046 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4047 | } |
| 4048 | else { |
| 4049 | Py_INCREF(Py_None); |
| 4050 | PUSH(Py_None); |
| 4051 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4052 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | /* Make the raw exception data |
| 4054 | available to the handler, |
| 4055 | so a program can emulate the |
| 4056 | Python main loop. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4057 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 4058 | if (tb != NULL) |
| 4059 | PyException_SetTraceback(val, tb); |
| 4060 | else |
| 4061 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4062 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4063 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4065 | exc_info->exc_value = val; |
| 4066 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | if (tb == NULL) |
| 4068 | tb = Py_None; |
| 4069 | Py_INCREF(tb); |
| 4070 | PUSH(tb); |
| 4071 | PUSH(val); |
| 4072 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | JUMPTO(handler); |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 4074 | if (_Py_TracingPossible(ceval2)) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 4075 | instr_prev = INT_MAX; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4076 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4077 | /* Resume normal execution */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4078 | f->f_state = FRAME_EXECUTING; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4079 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4080 | } |
| 4081 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4082 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4083 | /* End the loop as we still have an error */ |
| 4084 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4085 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4086 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 4087 | assert(retval == NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4088 | assert(_PyErr_Occurred(tstate)); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 4089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4090 | /* Pop remaining stack entries. */ |
| 4091 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4092 | PyObject *o = POP(); |
| 4093 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4094 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4095 | f->f_stackdepth = 0; |
| 4096 | f->f_state = FRAME_RAISED; |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 4097 | exiting: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4098 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 4099 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4100 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 4101 | tstate, f, PyTrace_RETURN, retval)) { |
| 4102 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4106 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 4107 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4108 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4109 | } |
| 4110 | } |
| 4111 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 4112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4113 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4114 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 4115 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 4116 | dtrace_function_return(f); |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 4117 | _Py_LeaveRecursiveCall(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4118 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4119 | |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 4120 | return _Py_CheckFunctionResult(tstate, NULL, retval, __func__); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4123 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4124 | format_missing(PyThreadState *tstate, const char *kind, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4125 | PyCodeObject *co, PyObject *names, PyObject *qualname) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4126 | { |
| 4127 | int err; |
| 4128 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 4129 | PyObject *name_str, *comma, *tail, *tmp; |
| 4130 | |
| 4131 | assert(PyList_CheckExact(names)); |
| 4132 | assert(len >= 1); |
| 4133 | /* Deal with the joys of natural language. */ |
| 4134 | switch (len) { |
| 4135 | case 1: |
| 4136 | name_str = PyList_GET_ITEM(names, 0); |
| 4137 | Py_INCREF(name_str); |
| 4138 | break; |
| 4139 | case 2: |
| 4140 | name_str = PyUnicode_FromFormat("%U and %U", |
| 4141 | PyList_GET_ITEM(names, len - 2), |
| 4142 | PyList_GET_ITEM(names, len - 1)); |
| 4143 | break; |
| 4144 | default: |
| 4145 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 4146 | PyList_GET_ITEM(names, len - 2), |
| 4147 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 4148 | if (tail == NULL) |
| 4149 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4150 | /* Chop off the last two objects in the list. This shouldn't actually |
| 4151 | fail, but we can't be too careful. */ |
| 4152 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 4153 | if (err == -1) { |
| 4154 | Py_DECREF(tail); |
| 4155 | return; |
| 4156 | } |
| 4157 | /* Stitch everything up into a nice comma-separated list. */ |
| 4158 | comma = PyUnicode_FromString(", "); |
| 4159 | if (comma == NULL) { |
| 4160 | Py_DECREF(tail); |
| 4161 | return; |
| 4162 | } |
| 4163 | tmp = PyUnicode_Join(comma, names); |
| 4164 | Py_DECREF(comma); |
| 4165 | if (tmp == NULL) { |
| 4166 | Py_DECREF(tail); |
| 4167 | return; |
| 4168 | } |
| 4169 | name_str = PyUnicode_Concat(tmp, tail); |
| 4170 | Py_DECREF(tmp); |
| 4171 | Py_DECREF(tail); |
| 4172 | break; |
| 4173 | } |
| 4174 | if (name_str == NULL) |
| 4175 | return; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4176 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4177 | "%U() missing %i required %s argument%s: %U", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4178 | qualname, |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4179 | len, |
| 4180 | kind, |
| 4181 | len == 1 ? "" : "s", |
| 4182 | name_str); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4183 | Py_DECREF(name_str); |
| 4184 | } |
| 4185 | |
| 4186 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4187 | missing_arguments(PyThreadState *tstate, PyCodeObject *co, |
| 4188 | Py_ssize_t missing, Py_ssize_t defcount, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4189 | PyObject **fastlocals, PyObject *qualname) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4190 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4191 | Py_ssize_t i, j = 0; |
| 4192 | Py_ssize_t start, end; |
| 4193 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4194 | const char *kind = positional ? "positional" : "keyword-only"; |
| 4195 | PyObject *missing_names; |
| 4196 | |
| 4197 | /* Compute the names of the arguments that are missing. */ |
| 4198 | missing_names = PyList_New(missing); |
| 4199 | if (missing_names == NULL) |
| 4200 | return; |
| 4201 | if (positional) { |
| 4202 | start = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4203 | end = co->co_argcount - defcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4204 | } |
| 4205 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4206 | start = co->co_argcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4207 | end = start + co->co_kwonlyargcount; |
| 4208 | } |
| 4209 | for (i = start; i < end; i++) { |
| 4210 | if (GETLOCAL(i) == NULL) { |
| 4211 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4212 | PyObject *name = PyObject_Repr(raw); |
| 4213 | if (name == NULL) { |
| 4214 | Py_DECREF(missing_names); |
| 4215 | return; |
| 4216 | } |
| 4217 | PyList_SET_ITEM(missing_names, j++, name); |
| 4218 | } |
| 4219 | } |
| 4220 | assert(j == missing); |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4221 | format_missing(tstate, kind, co, missing_names, qualname); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4222 | Py_DECREF(missing_names); |
| 4223 | } |
| 4224 | |
| 4225 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4226 | too_many_positional(PyThreadState *tstate, PyCodeObject *co, |
| 4227 | Py_ssize_t given, Py_ssize_t defcount, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4228 | PyObject **fastlocals, PyObject *qualname) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4229 | { |
| 4230 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4231 | Py_ssize_t kwonly_given = 0; |
| 4232 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4233 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4234 | Py_ssize_t co_argcount = co->co_argcount; |
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 | assert((co->co_flags & CO_VARARGS) == 0); |
| 4237 | /* Count missing keyword-only args. */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4238 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4239 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4240 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4241 | } |
| 4242 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4243 | if (defcount) { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4244 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4245 | plural = 1; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4246 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4247 | } |
| 4248 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4249 | plural = (co_argcount != 1); |
| 4250 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4251 | } |
| 4252 | if (sig == NULL) |
| 4253 | return; |
| 4254 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4255 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 4256 | kwonly_sig = PyUnicode_FromFormat(format, |
| 4257 | given != 1 ? "s" : "", |
| 4258 | kwonly_given, |
| 4259 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4260 | if (kwonly_sig == NULL) { |
| 4261 | Py_DECREF(sig); |
| 4262 | return; |
| 4263 | } |
| 4264 | } |
| 4265 | else { |
| 4266 | /* This will not fail. */ |
| 4267 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4268 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4269 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4270 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4271 | "%U() takes %U positional argument%s but %zd%U %s given", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4272 | qualname, |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4273 | sig, |
| 4274 | plural ? "s" : "", |
| 4275 | given, |
| 4276 | kwonly_sig, |
| 4277 | given == 1 && !kwonly_given ? "was" : "were"); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4278 | Py_DECREF(sig); |
| 4279 | Py_DECREF(kwonly_sig); |
| 4280 | } |
| 4281 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4282 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4283 | positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4284 | Py_ssize_t kwcount, PyObject* const* kwnames, |
| 4285 | PyObject *qualname) |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4286 | { |
| 4287 | int posonly_conflicts = 0; |
| 4288 | PyObject* posonly_names = PyList_New(0); |
| 4289 | |
| 4290 | for(int k=0; k < co->co_posonlyargcount; k++){ |
| 4291 | PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k); |
| 4292 | |
| 4293 | for (int k2=0; k2<kwcount; k2++){ |
| 4294 | /* Compare the pointers first and fallback to PyObject_RichCompareBool*/ |
| 4295 | PyObject* kwname = kwnames[k2]; |
| 4296 | if (kwname == posonly_name){ |
| 4297 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 4298 | goto fail; |
| 4299 | } |
| 4300 | posonly_conflicts++; |
| 4301 | continue; |
| 4302 | } |
| 4303 | |
| 4304 | int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ); |
| 4305 | |
| 4306 | if ( cmp > 0) { |
| 4307 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 4308 | goto fail; |
| 4309 | } |
| 4310 | posonly_conflicts++; |
| 4311 | } else if (cmp < 0) { |
| 4312 | goto fail; |
| 4313 | } |
| 4314 | |
| 4315 | } |
| 4316 | } |
| 4317 | if (posonly_conflicts) { |
| 4318 | PyObject* comma = PyUnicode_FromString(", "); |
| 4319 | if (comma == NULL) { |
| 4320 | goto fail; |
| 4321 | } |
| 4322 | PyObject* error_names = PyUnicode_Join(comma, posonly_names); |
| 4323 | Py_DECREF(comma); |
| 4324 | if (error_names == NULL) { |
| 4325 | goto fail; |
| 4326 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4327 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4328 | "%U() got some positional-only arguments passed" |
| 4329 | " as keyword arguments: '%U'", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4330 | qualname, error_names); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4331 | Py_DECREF(error_names); |
| 4332 | goto fail; |
| 4333 | } |
| 4334 | |
| 4335 | Py_DECREF(posonly_names); |
| 4336 | return 0; |
| 4337 | |
| 4338 | fail: |
| 4339 | Py_XDECREF(posonly_names); |
| 4340 | return 1; |
| 4341 | |
| 4342 | } |
| 4343 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4344 | /* 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] | 4345 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4346 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 4347 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4348 | PyObject * |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4349 | _PyEval_EvalCode(PyThreadState *tstate, |
| 4350 | PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4351 | PyObject *const *args, Py_ssize_t argcount, |
| 4352 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4353 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4354 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4355 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4356 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4357 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4358 | assert(is_tstate_valid(tstate)); |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4359 | |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4360 | PyCodeObject *co = (PyCodeObject*)_co; |
| 4361 | |
| 4362 | if (!name) { |
| 4363 | name = co->co_name; |
| 4364 | } |
| 4365 | assert(name != NULL); |
| 4366 | assert(PyUnicode_Check(name)); |
| 4367 | |
| 4368 | if (!qualname) { |
| 4369 | qualname = name; |
| 4370 | } |
| 4371 | assert(qualname != NULL); |
| 4372 | assert(PyUnicode_Check(qualname)); |
| 4373 | |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4374 | PyObject *retval = NULL; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4375 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | if (globals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4378 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 4379 | "PyEval_EvalCodeEx: NULL globals"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | return NULL; |
| 4381 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4382 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4383 | /* Create the frame */ |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4384 | PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4385 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4386 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4387 | } |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4388 | PyObject **fastlocals = f->f_localsplus; |
| 4389 | PyObject **freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4390 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4391 | /* Create a dictionary for keyword parameters (**kwags) */ |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4392 | PyObject *kwdict; |
| 4393 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4394 | if (co->co_flags & CO_VARKEYWORDS) { |
| 4395 | kwdict = PyDict_New(); |
| 4396 | if (kwdict == NULL) |
| 4397 | goto fail; |
| 4398 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4399 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4400 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4401 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4402 | SETLOCAL(i, kwdict); |
| 4403 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4404 | else { |
| 4405 | kwdict = NULL; |
| 4406 | } |
| 4407 | |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4408 | /* Copy all positional arguments into local variables */ |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4409 | Py_ssize_t j, n; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4410 | if (argcount > co->co_argcount) { |
| 4411 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4412 | } |
| 4413 | else { |
| 4414 | n = argcount; |
| 4415 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4416 | for (j = 0; j < n; j++) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4417 | PyObject *x = args[j]; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4418 | Py_INCREF(x); |
| 4419 | SETLOCAL(j, x); |
| 4420 | } |
| 4421 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4422 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4423 | if (co->co_flags & CO_VARARGS) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4424 | PyObject *u = _PyTuple_FromArray(args + n, argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4425 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4426 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4427 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4428 | SETLOCAL(total_args, u); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4429 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4430 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4431 | /* Handle keyword arguments passed as two strided arrays */ |
| 4432 | kwcount *= kwstep; |
| 4433 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4434 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4435 | PyObject *keyword = kwnames[i]; |
| 4436 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4437 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4438 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4439 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4440 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4441 | "%U() keywords must be strings", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4442 | qualname); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4443 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4445 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4446 | /* Speed hack: do raw pointer compares. As names are |
| 4447 | normally interned this should almost always hit. */ |
| 4448 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4449 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4450 | PyObject *varname = co_varnames[j]; |
| 4451 | if (varname == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4452 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4453 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4454 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4455 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4456 | /* Slow fallback, just in case */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4457 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4458 | PyObject *varname = co_varnames[j]; |
| 4459 | int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ); |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4460 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4461 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4462 | } |
| 4463 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4465 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4466 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4467 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 4468 | assert(j >= total_args); |
| 4469 | if (kwdict == NULL) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4470 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4471 | if (co->co_posonlyargcount |
| 4472 | && positional_only_passed_as_keyword(tstate, co, |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4473 | kwcount, kwnames, |
| 4474 | qualname)) |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4475 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4476 | goto fail; |
| 4477 | } |
| 4478 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4479 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4480 | "%U() got an unexpected keyword argument '%S'", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4481 | qualname, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4482 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4483 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4484 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4485 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4486 | goto fail; |
| 4487 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4488 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4489 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4490 | kw_found: |
| 4491 | if (GETLOCAL(j) != NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4492 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4493 | "%U() got multiple values for argument '%S'", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4494 | qualname, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4495 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4497 | Py_INCREF(value); |
| 4498 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4500 | |
| 4501 | /* Check the number of positional arguments */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4502 | if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4503 | too_many_positional(tstate, co, argcount, defcount, fastlocals, |
| 4504 | qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | goto fail; |
| 4506 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4507 | |
| 4508 | /* Add missing positional arguments (copy default values from defs) */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4509 | if (argcount < co->co_argcount) { |
| 4510 | Py_ssize_t m = co->co_argcount - defcount; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4511 | Py_ssize_t missing = 0; |
| 4512 | for (i = argcount; i < m; i++) { |
| 4513 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4514 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4515 | } |
| 4516 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4517 | if (missing) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4518 | missing_arguments(tstate, co, missing, defcount, fastlocals, |
| 4519 | qualname); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4520 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4521 | } |
| 4522 | if (n > m) |
| 4523 | i = n - m; |
| 4524 | else |
| 4525 | i = 0; |
| 4526 | for (; i < defcount; i++) { |
| 4527 | if (GETLOCAL(m+i) == NULL) { |
| 4528 | PyObject *def = defs[i]; |
| 4529 | Py_INCREF(def); |
| 4530 | SETLOCAL(m+i, def); |
| 4531 | } |
| 4532 | } |
| 4533 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4534 | |
| 4535 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4536 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4537 | Py_ssize_t missing = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4538 | for (i = co->co_argcount; i < total_args; i++) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4539 | if (GETLOCAL(i) != NULL) |
| 4540 | continue; |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4541 | PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4542 | if (kwdefs != NULL) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4543 | PyObject *def = PyDict_GetItemWithError(kwdefs, varname); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4544 | if (def) { |
| 4545 | Py_INCREF(def); |
| 4546 | SETLOCAL(i, def); |
| 4547 | continue; |
| 4548 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4549 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4550 | goto fail; |
| 4551 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4552 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4553 | missing++; |
| 4554 | } |
| 4555 | if (missing) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4556 | missing_arguments(tstate, co, missing, -1, fastlocals, |
| 4557 | qualname); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4558 | goto fail; |
| 4559 | } |
| 4560 | } |
| 4561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4563 | vars into frame. */ |
| 4564 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4565 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 4566 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4567 | /* Possibly account for the cell variable being an argument. */ |
| 4568 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4569 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4570 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4571 | /* Clear the local copy. */ |
| 4572 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4573 | } |
| 4574 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4575 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4576 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4577 | if (c == NULL) |
| 4578 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4579 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4581 | |
| 4582 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4583 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4584 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4585 | Py_INCREF(o); |
| 4586 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4588 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4589 | /* Handle generator/coroutine/asynchronous generator */ |
| 4590 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4591 | PyObject *gen; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4592 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4594 | /* Don't need to keep the reference to f_back, it will be set |
| 4595 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4596 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | /* Create a new generator that owns the ready to run frame |
| 4599 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4600 | if (is_coro) { |
| 4601 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4602 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4603 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4604 | } else { |
| 4605 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4606 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4607 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4608 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4609 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 4610 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4611 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4612 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4613 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4615 | |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 4616 | retval = _PyEval_EvalFrame(tstate, f, 0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4617 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4618 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4620 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4621 | which can call back into Python. While we're done with the |
| 4622 | current Python frame (f), the associated C stack is still in use, |
| 4623 | so recursion_depth must be boosted for the duration. |
| 4624 | */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4625 | if (Py_REFCNT(f) > 1) { |
| 4626 | Py_DECREF(f); |
| 4627 | _PyObject_GC_TRACK(f); |
| 4628 | } |
| 4629 | else { |
| 4630 | ++tstate->recursion_depth; |
| 4631 | Py_DECREF(f); |
| 4632 | --tstate->recursion_depth; |
| 4633 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4637 | |
| 4638 | PyObject * |
| 4639 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4640 | PyObject *const *args, Py_ssize_t argcount, |
| 4641 | PyObject *const *kwnames, PyObject *const *kwargs, |
| 4642 | Py_ssize_t kwcount, int kwstep, |
| 4643 | PyObject *const *defs, Py_ssize_t defcount, |
| 4644 | PyObject *kwdefs, PyObject *closure, |
| 4645 | PyObject *name, PyObject *qualname) |
| 4646 | { |
| 4647 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4648 | return _PyEval_EvalCode(tstate, _co, globals, locals, |
| 4649 | args, argcount, |
| 4650 | kwnames, kwargs, |
| 4651 | kwcount, kwstep, |
| 4652 | defs, defcount, |
| 4653 | kwdefs, closure, |
| 4654 | name, qualname); |
| 4655 | } |
| 4656 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4657 | PyObject * |
| 4658 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4659 | PyObject *const *args, int argcount, |
| 4660 | PyObject *const *kws, int kwcount, |
| 4661 | PyObject *const *defs, int defcount, |
| 4662 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4663 | { |
| 4664 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4665 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 4666 | kws, kws != NULL ? kws + 1 : NULL, |
| 4667 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4668 | defs, defcount, |
| 4669 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4670 | NULL, NULL); |
| 4671 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4672 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4673 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4674 | special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4677 | res = _PyObject_LookupSpecial(o, id); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4678 | if (res == NULL && !_PyErr_Occurred(tstate)) { |
Victor Stinner | 4804b5b | 2020-05-12 01:43:38 +0200 | [diff] [blame] | 4679 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4680 | return NULL; |
| 4681 | } |
| 4682 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4686 | /* Logic for the raise statement (too complicated for inlining). |
| 4687 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4688 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4689 | do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | if (exc == NULL) { |
| 4694 | /* Reraise */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4695 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4696 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4697 | type = exc_info->exc_type; |
| 4698 | value = exc_info->exc_value; |
| 4699 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4700 | if (type == Py_None || type == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4701 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 4702 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4703 | return 0; |
| 4704 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | Py_XINCREF(type); |
| 4706 | Py_XINCREF(value); |
| 4707 | Py_XINCREF(tb); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4708 | _PyErr_Restore(tstate, type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4709 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4710 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | /* We support the following forms of raise: |
| 4713 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4714 | raise <instance> |
| 4715 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | if (PyExceptionClass_Check(exc)) { |
| 4718 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4719 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4720 | if (value == NULL) |
| 4721 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4722 | if (!PyExceptionInstance_Check(value)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4723 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4724 | "calling %R should have returned an instance of " |
| 4725 | "BaseException, not %R", |
| 4726 | type, Py_TYPE(value)); |
| 4727 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4728 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | } |
| 4730 | else if (PyExceptionInstance_Check(exc)) { |
| 4731 | value = exc; |
| 4732 | type = PyExceptionInstance_Class(exc); |
| 4733 | Py_INCREF(type); |
| 4734 | } |
| 4735 | else { |
| 4736 | /* Not something you can raise. You get an exception |
| 4737 | anyway, just not what you specified :-) */ |
| 4738 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4739 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4740 | "exceptions must derive from BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | goto raise_error; |
| 4742 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4743 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4744 | assert(type != NULL); |
| 4745 | assert(value != NULL); |
| 4746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | if (cause) { |
| 4748 | PyObject *fixed_cause; |
| 4749 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4750 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | if (fixed_cause == NULL) |
| 4752 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4753 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4754 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4755 | else if (PyExceptionInstance_Check(cause)) { |
| 4756 | fixed_cause = cause; |
| 4757 | } |
| 4758 | else if (cause == Py_None) { |
| 4759 | Py_DECREF(cause); |
| 4760 | fixed_cause = NULL; |
| 4761 | } |
| 4762 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4763 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4764 | "exception causes must derive from " |
| 4765 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | goto raise_error; |
| 4767 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4768 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4769 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4770 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4771 | _PyErr_SetObject(tstate, type, value); |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 4772 | /* _PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4773 | Py_DECREF(value); |
| 4774 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4775 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4776 | |
| 4777 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | Py_XDECREF(value); |
| 4779 | Py_XDECREF(type); |
| 4780 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4781 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4784 | /* 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] | 4785 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4786 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4787 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4788 | with a variable target. |
| 4789 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4790 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4791 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4792 | unpack_iterable(PyThreadState *tstate, PyObject *v, |
| 4793 | int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | int i = 0, j = 0; |
| 4796 | Py_ssize_t ll = 0; |
| 4797 | PyObject *it; /* iter(v) */ |
| 4798 | PyObject *w; |
| 4799 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4801 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4804 | if (it == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4805 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4806 | Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v)) |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4807 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4808 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4809 | "cannot unpack non-iterable %.200s object", |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4810 | Py_TYPE(v)->tp_name); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4811 | } |
| 4812 | return 0; |
| 4813 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | for (; i < argcnt; i++) { |
| 4816 | w = PyIter_Next(it); |
| 4817 | if (w == NULL) { |
| 4818 | /* Iterator done, via error or exhaustion. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4819 | if (!_PyErr_Occurred(tstate)) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4820 | if (argcntafter == -1) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4821 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4822 | "not enough values to unpack " |
| 4823 | "(expected %d, got %d)", |
| 4824 | argcnt, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4825 | } |
| 4826 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4827 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4828 | "not enough values to unpack " |
| 4829 | "(expected at least %d, got %d)", |
| 4830 | argcnt + argcntafter, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4831 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4832 | } |
| 4833 | goto Error; |
| 4834 | } |
| 4835 | *--sp = w; |
| 4836 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | if (argcntafter == -1) { |
| 4839 | /* We better have exhausted the iterator now. */ |
| 4840 | w = PyIter_Next(it); |
| 4841 | if (w == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4842 | if (_PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | goto Error; |
| 4844 | Py_DECREF(it); |
| 4845 | return 1; |
| 4846 | } |
| 4847 | Py_DECREF(w); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4848 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4849 | "too many values to unpack (expected %d)", |
| 4850 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | goto Error; |
| 4852 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4854 | l = PySequence_List(it); |
| 4855 | if (l == NULL) |
| 4856 | goto Error; |
| 4857 | *--sp = l; |
| 4858 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4860 | ll = PyList_GET_SIZE(l); |
| 4861 | if (ll < argcntafter) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4862 | _PyErr_Format(tstate, PyExc_ValueError, |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4863 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4864 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4865 | goto Error; |
| 4866 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | /* Pop the "after-variable" args off the list. */ |
| 4869 | for (j = argcntafter; j > 0; j--, i++) { |
| 4870 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4871 | } |
| 4872 | /* Resize the list. */ |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4873 | Py_SET_SIZE(l, ll - argcntafter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | Py_DECREF(it); |
| 4875 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4876 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4877 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | for (; i > 0; i--, sp++) |
| 4879 | Py_DECREF(*sp); |
| 4880 | Py_XDECREF(it); |
| 4881 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4885 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4886 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4887 | prtrace(PyThreadState *tstate, PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | printf("%s ", str); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4890 | if (PyObject_Print(v, stdout, 0) != 0) { |
| 4891 | /* Don't know what else to do */ |
| 4892 | _PyErr_Clear(tstate); |
| 4893 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | printf("\n"); |
| 4895 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4896 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4897 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4898 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4899 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4900 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4901 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4902 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4903 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4904 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4905 | _PyErr_Fetch(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | if (value == NULL) { |
| 4907 | value = Py_None; |
| 4908 | Py_INCREF(value); |
| 4909 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4910 | _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4911 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4912 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4913 | if (arg == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4914 | _PyErr_Restore(tstate, type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | return; |
| 4916 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4917 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | Py_DECREF(arg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4919 | if (err == 0) { |
| 4920 | _PyErr_Restore(tstate, type, value, orig_traceback); |
| 4921 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4922 | else { |
| 4923 | Py_XDECREF(type); |
| 4924 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4925 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4926 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4929 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4930 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4931 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4932 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4934 | PyObject *type, *value, *traceback; |
| 4935 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4936 | _PyErr_Fetch(tstate, &type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4937 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4938 | if (err == 0) |
| 4939 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4940 | _PyErr_Restore(tstate, type, value, traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | return 0; |
| 4942 | } |
| 4943 | else { |
| 4944 | Py_XDECREF(type); |
| 4945 | Py_XDECREF(value); |
| 4946 | Py_XDECREF(traceback); |
| 4947 | return -1; |
| 4948 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4951 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4952 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4953 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | int result; |
| 4957 | if (tstate->tracing) |
| 4958 | return 0; |
| 4959 | tstate->tracing++; |
| 4960 | tstate->use_tracing = 0; |
| 4961 | result = func(obj, frame, what, arg); |
| 4962 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4963 | || (tstate->c_profilefunc != NULL)); |
| 4964 | tstate->tracing--; |
| 4965 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4968 | PyObject * |
| 4969 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4970 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4971 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4972 | int save_tracing = tstate->tracing; |
| 4973 | int save_use_tracing = tstate->use_tracing; |
| 4974 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4976 | tstate->tracing = 0; |
| 4977 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4978 | || (tstate->c_profilefunc != NULL)); |
| 4979 | result = PyObject_Call(func, args, NULL); |
| 4980 | tstate->tracing = save_tracing; |
| 4981 | tstate->use_tracing = save_use_tracing; |
| 4982 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4985 | /* 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] | 4986 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4987 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4988 | PyThreadState *tstate, PyFrameObject *frame, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 4989 | PyCodeAddressRange *bounds, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4990 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4991 | int result = 0; |
| 4992 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4994 | /* If the last instruction executed isn't in the current |
| 4995 | instruction window, reset the window. |
| 4996 | */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 4997 | line = _PyCode_CheckLineNumber(frame->f_lasti, bounds); |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4998 | /* If the last instruction falls at the start of a line or if it |
| 4999 | represents a jump backwards, update the frame's line number and |
| 5000 | then call the trace function if we're tracing source lines. |
| 5001 | */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5002 | if ((line != frame->f_lineno || frame->f_lasti < *instr_prev)) { |
| 5003 | if (line != -1) { |
| 5004 | frame->f_lineno = line; |
| 5005 | if (frame->f_trace_lines) { |
| 5006 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 5007 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 5008 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 5010 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 5011 | if (frame->f_trace_opcodes) { |
| 5012 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 5013 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | *instr_prev = frame->f_lasti; |
| 5015 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5018 | int |
| 5019 | _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) |
| 5020 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5021 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5022 | /* The caller must hold the GIL */ |
| 5023 | assert(PyGILState_Check()); |
| 5024 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5025 | /* Call _PySys_Audit() in the context of the current thread state, |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5026 | even if tstate is not the current thread state. */ |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5027 | PyThreadState *current_tstate = _PyThreadState_GET(); |
| 5028 | if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5029 | return -1; |
| 5030 | } |
| 5031 | |
| 5032 | PyObject *profileobj = tstate->c_profileobj; |
| 5033 | |
| 5034 | tstate->c_profilefunc = NULL; |
| 5035 | tstate->c_profileobj = NULL; |
| 5036 | /* Must make sure that tracing is not ignored if 'profileobj' is freed */ |
| 5037 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 5038 | Py_XDECREF(profileobj); |
| 5039 | |
| 5040 | Py_XINCREF(arg); |
| 5041 | tstate->c_profileobj = arg; |
| 5042 | tstate->c_profilefunc = func; |
| 5043 | |
| 5044 | /* Flag that tracing or profiling is turned on */ |
| 5045 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 5049 | void |
| 5050 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 5051 | { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5052 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5053 | if (_PyEval_SetProfile(tstate, func, arg) < 0) { |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5054 | /* Log _PySys_Audit() error */ |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5055 | _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL); |
| 5056 | } |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | int |
| 5060 | _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) |
| 5061 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5062 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5063 | /* The caller must hold the GIL */ |
| 5064 | assert(PyGILState_Check()); |
| 5065 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5066 | /* Call _PySys_Audit() in the context of the current thread state, |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5067 | even if tstate is not the current thread state. */ |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5068 | PyThreadState *current_tstate = _PyThreadState_GET(); |
| 5069 | if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5070 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5071 | } |
| 5072 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5073 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5074 | PyObject *traceobj = tstate->c_traceobj; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5075 | ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5076 | |
| 5077 | tstate->c_tracefunc = NULL; |
| 5078 | tstate->c_traceobj = NULL; |
| 5079 | /* Must make sure that profiling is not ignored if 'traceobj' is freed */ |
| 5080 | tstate->use_tracing = (tstate->c_profilefunc != NULL); |
| 5081 | Py_XDECREF(traceobj); |
| 5082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | Py_XINCREF(arg); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5084 | tstate->c_traceobj = arg; |
| 5085 | tstate->c_tracefunc = func; |
| 5086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | /* Flag that tracing or profiling is turned on */ |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5088 | tstate->use_tracing = ((func != NULL) |
| 5089 | || (tstate->c_profilefunc != NULL)); |
| 5090 | |
| 5091 | return 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | void |
| 5095 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 5096 | { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5097 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5098 | if (_PyEval_SetTrace(tstate, func, arg) < 0) { |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5099 | /* Log _PySys_Audit() error */ |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5100 | _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL); |
| 5101 | } |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5104 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5105 | void |
Victor Stinner | 838f264 | 2019-06-13 22:41:23 +0200 | [diff] [blame] | 5106 | _PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth) |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 5107 | { |
| 5108 | assert(new_depth >= 0); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 5109 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 5110 | } |
| 5111 | |
| 5112 | int |
| 5113 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 5114 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5115 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 5116 | return tstate->coroutine_origin_tracking_depth; |
| 5117 | } |
| 5118 | |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5119 | int |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5120 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 5121 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5122 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5123 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5124 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) { |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5125 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5126 | } |
| 5127 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5128 | Py_XINCREF(firstiter); |
| 5129 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5130 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | PyObject * |
| 5134 | _PyEval_GetAsyncGenFirstiter(void) |
| 5135 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5136 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5137 | return tstate->async_gen_firstiter; |
| 5138 | } |
| 5139 | |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5140 | int |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5141 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 5142 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5143 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5144 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5145 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) { |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5146 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5147 | } |
| 5148 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5149 | Py_XINCREF(finalizer); |
| 5150 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5151 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | PyObject * |
| 5155 | _PyEval_GetAsyncGenFinalizer(void) |
| 5156 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5157 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5158 | return tstate->async_gen_finalizer; |
| 5159 | } |
| 5160 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5161 | PyFrameObject * |
| 5162 | PyEval_GetFrame(void) |
| 5163 | { |
| 5164 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5165 | return tstate->frame; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5166 | } |
| 5167 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5168 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5169 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 5170 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5171 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5172 | PyFrameObject *current_frame = tstate->frame; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5173 | if (current_frame == NULL) |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5174 | return tstate->interp->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | else |
| 5176 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 5179 | /* Convenience function to get a builtin from its name */ |
| 5180 | PyObject * |
| 5181 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 5182 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5183 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 5184 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 5185 | if (attr) { |
| 5186 | Py_INCREF(attr); |
| 5187 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5188 | else if (!_PyErr_Occurred(tstate)) { |
| 5189 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name)); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 5190 | } |
| 5191 | return attr; |
| 5192 | } |
| 5193 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5194 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5195 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 5196 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5197 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5198 | PyFrameObject *current_frame = tstate->frame; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5199 | if (current_frame == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5200 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5201 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5202 | } |
| 5203 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5204 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5205 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5206 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5207 | |
| 5208 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5212 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5213 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5214 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5215 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5216 | PyFrameObject *current_frame = tstate->frame; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5217 | if (current_frame == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5219 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5220 | |
| 5221 | assert(current_frame->f_globals != NULL); |
| 5222 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 5225 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 5226 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 5227 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5228 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5229 | PyFrameObject *current_frame = tstate->frame; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | if (current_frame != NULL) { |
| 5233 | const int codeflags = current_frame->f_code->co_flags; |
| 5234 | const int compilerflags = codeflags & PyCF_MASK; |
| 5235 | if (compilerflags) { |
| 5236 | result = 1; |
| 5237 | cf->cf_flags |= compilerflags; |
| 5238 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 5239 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5240 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 5241 | result = 1; |
| 5242 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 5243 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 5244 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5245 | } |
| 5246 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5249 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 5250 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5251 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | if (PyMethod_Check(func)) |
| 5254 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 5255 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5256 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | else if (PyCFunction_Check(func)) |
| 5258 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 5259 | else |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 5260 | return Py_TYPE(func)->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 5263 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5264 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | if (PyMethod_Check(func)) |
| 5267 | return "()"; |
| 5268 | else if (PyFunction_Check(func)) |
| 5269 | return "()"; |
| 5270 | else if (PyCFunction_Check(func)) |
| 5271 | return "()"; |
| 5272 | else |
| 5273 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 5276 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 5277 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5278 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 5279 | tstate, tstate->frame, \ |
| 5280 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | x = NULL; \ |
| 5282 | } \ |
| 5283 | else { \ |
| 5284 | x = call; \ |
| 5285 | if (tstate->c_profilefunc != NULL) { \ |
| 5286 | if (x == NULL) { \ |
| 5287 | call_trace_protected(tstate->c_profilefunc, \ |
| 5288 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5289 | tstate, tstate->frame, \ |
| 5290 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5291 | /* XXX should pass (type, value, tb) */ \ |
| 5292 | } else { \ |
| 5293 | if (call_trace(tstate->c_profilefunc, \ |
| 5294 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5295 | tstate, tstate->frame, \ |
| 5296 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5297 | Py_DECREF(x); \ |
| 5298 | x = NULL; \ |
| 5299 | } \ |
| 5300 | } \ |
| 5301 | } \ |
| 5302 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 5303 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | x = call; \ |
| 5305 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 5306 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5307 | |
| 5308 | static PyObject * |
| 5309 | trace_call_function(PyThreadState *tstate, |
| 5310 | PyObject *func, |
| 5311 | PyObject **args, Py_ssize_t nargs, |
| 5312 | PyObject *kwnames) |
| 5313 | { |
| 5314 | PyObject *x; |
scoder | 4c9ea09 | 2020-05-12 16:12:41 +0200 | [diff] [blame] | 5315 | if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5316 | C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5317 | return x; |
| 5318 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 5319 | else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) { |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5320 | /* We need to create a temporary bound method as argument |
| 5321 | for profiling. |
| 5322 | |
| 5323 | If nargs == 0, then this cannot work because we have no |
| 5324 | "self". In any case, the call itself would raise |
| 5325 | TypeError (foo needs an argument), so we just skip |
| 5326 | profiling. */ |
| 5327 | PyObject *self = args[0]; |
| 5328 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5329 | if (func == NULL) { |
| 5330 | return NULL; |
| 5331 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5332 | C_TRACE(x, PyObject_Vectorcall(func, |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 5333 | args+1, nargs-1, |
| 5334 | kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5335 | Py_DECREF(func); |
| 5336 | return x; |
| 5337 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5338 | return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5339 | } |
| 5340 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 5341 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 5342 | to reduce the stack consumption. */ |
| 5343 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 5344 | 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] | 5345 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5346 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5347 | PyObject *func = *pfunc; |
| 5348 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 5349 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 5350 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 5351 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5352 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5353 | if (tstate->use_tracing) { |
| 5354 | x = trace_call_function(tstate, func, stack, nargs, kwnames); |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 5355 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 5356 | else { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5357 | x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5358 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 5359 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5360 | assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5361 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 5362 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | while ((*pp_stack) > pfunc) { |
| 5364 | w = EXT_POP(*pp_stack); |
| 5365 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5366 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 5367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5368 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5371 | static PyObject * |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 5372 | do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5373 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5374 | PyObject *result; |
| 5375 | |
scoder | 4c9ea09 | 2020-05-12 16:12:41 +0200 | [diff] [blame] | 5376 | if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) { |
Jeroen Demeyer | 7a6873c | 2019-09-11 13:01:01 +0200 | [diff] [blame] | 5377 | C_TRACE(result, PyObject_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5378 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5379 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 5380 | else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5381 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 5382 | if (nargs > 0 && tstate->use_tracing) { |
| 5383 | /* We need to create a temporary bound method as argument |
| 5384 | for profiling. |
| 5385 | |
| 5386 | If nargs == 0, then this cannot work because we have no |
| 5387 | "self". In any case, the call itself would raise |
| 5388 | TypeError (foo needs an argument), so we just skip |
| 5389 | profiling. */ |
| 5390 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 5391 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5392 | if (func == NULL) { |
| 5393 | return NULL; |
| 5394 | } |
| 5395 | |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 5396 | C_TRACE(result, _PyObject_FastCallDictTstate( |
| 5397 | tstate, func, |
| 5398 | &_PyTuple_ITEMS(callargs)[1], |
| 5399 | nargs - 1, |
| 5400 | kwdict)); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5401 | Py_DECREF(func); |
| 5402 | return result; |
| 5403 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5404 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5405 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5408 | /* 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] | 5409 | nb_index slot defined, and store in *pi. |
| 5410 | 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] | 5411 | 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] | 5412 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5413 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5414 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5415 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5416 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5417 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5418 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | Py_ssize_t x; |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 5420 | if (_PyIndex_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5421 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5422 | if (x == -1 && _PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5423 | return 0; |
| 5424 | } |
| 5425 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5426 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5427 | "slice indices must be integers or " |
| 5428 | "None or have an __index__ method"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5429 | return 0; |
| 5430 | } |
| 5431 | *pi = x; |
| 5432 | } |
| 5433 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5436 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5437 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5438 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5439 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5440 | Py_ssize_t x; |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 5441 | if (_PyIndex_Check(v)) { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5442 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5443 | if (x == -1 && _PyErr_Occurred(tstate)) |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5444 | return 0; |
| 5445 | } |
| 5446 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5447 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5448 | "slice indices must be integers or " |
| 5449 | "have an __index__ method"); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5450 | return 0; |
| 5451 | } |
| 5452 | *pi = x; |
| 5453 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5454 | } |
| 5455 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5456 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5457 | import_name(PyThreadState *tstate, PyFrameObject *f, |
| 5458 | PyObject *name, PyObject *fromlist, PyObject *level) |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5459 | { |
| 5460 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5461 | PyObject *import_func, *res; |
| 5462 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5463 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5464 | import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5465 | if (import_func == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5466 | if (!_PyErr_Occurred(tstate)) { |
| 5467 | _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5468 | } |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5469 | return NULL; |
| 5470 | } |
| 5471 | |
| 5472 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5473 | if (import_func == tstate->interp->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5474 | int ilevel = _PyLong_AsInt(level); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5475 | if (ilevel == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5476 | return NULL; |
| 5477 | } |
| 5478 | res = PyImport_ImportModuleLevelObject( |
| 5479 | name, |
| 5480 | f->f_globals, |
| 5481 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5482 | fromlist, |
| 5483 | ilevel); |
| 5484 | return res; |
| 5485 | } |
| 5486 | |
| 5487 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5488 | |
| 5489 | stack[0] = name; |
| 5490 | stack[1] = f->f_globals; |
| 5491 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5492 | stack[3] = fromlist; |
| 5493 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5494 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5495 | Py_DECREF(import_func); |
| 5496 | return res; |
| 5497 | } |
| 5498 | |
| 5499 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5500 | import_from(PyThreadState *tstate, PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5502 | PyObject *x; |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5503 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5504 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5505 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5506 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5507 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5508 | /* Issue #17636: in case this failed because of a circular relative |
| 5509 | import, try to fallback on reading the module directly from |
| 5510 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5511 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5512 | if (pkgname == NULL) { |
| 5513 | goto error; |
| 5514 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 5515 | if (!PyUnicode_Check(pkgname)) { |
| 5516 | Py_CLEAR(pkgname); |
| 5517 | goto error; |
| 5518 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5519 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5520 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5521 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5522 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5523 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 5524 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5525 | Py_DECREF(fullmodname); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5526 | if (x == NULL && !_PyErr_Occurred(tstate)) { |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5527 | goto error; |
| 5528 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5529 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5530 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5531 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5532 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5533 | if (pkgname == NULL) { |
| 5534 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 5535 | if (pkgname_or_unknown == NULL) { |
| 5536 | Py_XDECREF(pkgpath); |
| 5537 | return NULL; |
| 5538 | } |
| 5539 | } else { |
| 5540 | pkgname_or_unknown = pkgname; |
| 5541 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5542 | |
| 5543 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5544 | _PyErr_Clear(tstate); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5545 | errmsg = PyUnicode_FromFormat( |
| 5546 | "cannot import name %R from %R (unknown location)", |
| 5547 | name, pkgname_or_unknown |
| 5548 | ); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5549 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5550 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 5551 | } |
| 5552 | else { |
Anthony Sottile | 65366bc | 2019-09-09 08:17:50 -0700 | [diff] [blame] | 5553 | _Py_IDENTIFIER(__spec__); |
| 5554 | PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__); |
Anthony Sottile | 65366bc | 2019-09-09 08:17:50 -0700 | [diff] [blame] | 5555 | const char *fmt = |
| 5556 | _PyModuleSpec_IsInitializing(spec) ? |
| 5557 | "cannot import name %R from partially initialized module %R " |
| 5558 | "(most likely due to a circular import) (%S)" : |
| 5559 | "cannot import name %R from %R (%S)"; |
| 5560 | Py_XDECREF(spec); |
| 5561 | |
| 5562 | errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5563 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5564 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5565 | } |
| 5566 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5567 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5568 | Py_XDECREF(pkgname_or_unknown); |
| 5569 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5570 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5571 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5572 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5573 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5574 | import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5575 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5576 | _Py_IDENTIFIER(__all__); |
| 5577 | _Py_IDENTIFIER(__dict__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5578 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5579 | int skip_leading_underscores = 0; |
| 5580 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5581 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5582 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 5583 | return -1; /* Unexpected error */ |
| 5584 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5586 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 5587 | return -1; |
| 5588 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5589 | if (dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5590 | _PyErr_SetString(tstate, PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5591 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | return -1; |
| 5593 | } |
| 5594 | all = PyMapping_Keys(dict); |
| 5595 | Py_DECREF(dict); |
| 5596 | if (all == NULL) |
| 5597 | return -1; |
| 5598 | skip_leading_underscores = 1; |
| 5599 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5601 | for (pos = 0, err = 0; ; pos++) { |
| 5602 | name = PySequence_GetItem(all, pos); |
| 5603 | if (name == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5604 | if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5605 | err = -1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5606 | } |
| 5607 | else { |
| 5608 | _PyErr_Clear(tstate); |
| 5609 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5610 | break; |
| 5611 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5612 | if (!PyUnicode_Check(name)) { |
| 5613 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 5614 | if (modname == NULL) { |
| 5615 | Py_DECREF(name); |
| 5616 | err = -1; |
| 5617 | break; |
| 5618 | } |
| 5619 | if (!PyUnicode_Check(modname)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5620 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5621 | "module __name__ must be a string, not %.100s", |
| 5622 | Py_TYPE(modname)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5623 | } |
| 5624 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5625 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5626 | "%s in %U.%s must be str, not %.100s", |
| 5627 | skip_leading_underscores ? "Key" : "Item", |
| 5628 | modname, |
| 5629 | skip_leading_underscores ? "__dict__" : "__all__", |
| 5630 | Py_TYPE(name)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5631 | } |
| 5632 | Py_DECREF(modname); |
| 5633 | Py_DECREF(name); |
| 5634 | err = -1; |
| 5635 | break; |
| 5636 | } |
| 5637 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 5638 | if (PyUnicode_READY(name) == -1) { |
| 5639 | Py_DECREF(name); |
| 5640 | err = -1; |
| 5641 | break; |
| 5642 | } |
| 5643 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 5644 | Py_DECREF(name); |
| 5645 | continue; |
| 5646 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5647 | } |
| 5648 | value = PyObject_GetAttr(v, name); |
| 5649 | if (value == NULL) |
| 5650 | err = -1; |
| 5651 | else if (PyDict_CheckExact(locals)) |
| 5652 | err = PyDict_SetItem(locals, name, value); |
| 5653 | else |
| 5654 | err = PyObject_SetItem(locals, name, value); |
| 5655 | Py_DECREF(name); |
| 5656 | Py_XDECREF(value); |
| 5657 | if (err != 0) |
| 5658 | break; |
| 5659 | } |
| 5660 | Py_DECREF(all); |
| 5661 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5664 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5665 | check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5666 | { |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 5667 | if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) { |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5668 | /* check_args_iterable() may be called with a live exception: |
| 5669 | * clear it to prevent calling _PyObject_FunctionStr() with an |
| 5670 | * exception set. */ |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5671 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5672 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 5673 | if (funcstr != NULL) { |
| 5674 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5675 | "%U argument after * must be an iterable, not %.200s", |
| 5676 | funcstr, Py_TYPE(args)->tp_name); |
| 5677 | Py_DECREF(funcstr); |
| 5678 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5679 | return -1; |
| 5680 | } |
| 5681 | return 0; |
| 5682 | } |
| 5683 | |
| 5684 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5685 | format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5686 | { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5687 | /* _PyDict_MergeEx raises attribute |
| 5688 | * error (percolated from an attempt |
| 5689 | * to get 'keys' attribute) instead of |
| 5690 | * a type error if its second argument |
| 5691 | * is not a mapping. |
| 5692 | */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5693 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5694 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5695 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 5696 | if (funcstr != NULL) { |
| 5697 | _PyErr_Format( |
| 5698 | tstate, PyExc_TypeError, |
| 5699 | "%U argument after ** must be a mapping, not %.200s", |
| 5700 | funcstr, Py_TYPE(kwargs)->tp_name); |
| 5701 | Py_DECREF(funcstr); |
| 5702 | } |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5703 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5704 | else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5705 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5706 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5707 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5708 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5709 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 5710 | if (funcstr != NULL) { |
| 5711 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 5712 | _PyErr_Format( |
| 5713 | tstate, PyExc_TypeError, |
| 5714 | "%U got multiple values for keyword argument '%S'", |
| 5715 | funcstr, key); |
| 5716 | Py_DECREF(funcstr); |
| 5717 | } |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5718 | Py_XDECREF(exc); |
| 5719 | Py_XDECREF(val); |
| 5720 | Py_XDECREF(tb); |
| 5721 | } |
| 5722 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5723 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5724 | } |
| 5725 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5726 | } |
| 5727 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5728 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5729 | format_exc_check_arg(PyThreadState *tstate, PyObject *exc, |
| 5730 | const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5732 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5734 | if (!obj) |
| 5735 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5736 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5737 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5738 | if (!obj_str) |
| 5739 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5740 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5741 | _PyErr_Format(tstate, exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5742 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5743 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5744 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5745 | format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5746 | { |
| 5747 | PyObject *name; |
| 5748 | /* Don't stomp existing exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5749 | if (_PyErr_Occurred(tstate)) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5750 | return; |
| 5751 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5752 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5753 | oparg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5754 | format_exc_check_arg(tstate, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5755 | PyExc_UnboundLocalError, |
| 5756 | UNBOUNDLOCAL_ERROR_MSG, |
| 5757 | name); |
| 5758 | } else { |
| 5759 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5760 | PyTuple_GET_SIZE(co->co_cellvars)); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5761 | format_exc_check_arg(tstate, PyExc_NameError, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5762 | UNBOUNDFREE_ERROR_MSG, name); |
| 5763 | } |
| 5764 | } |
| 5765 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5766 | static void |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5767 | format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode) |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5768 | { |
| 5769 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5770 | if (prevopcode == BEFORE_ASYNC_WITH) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5771 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5772 | "'async with' received an object from __aenter__ " |
| 5773 | "that does not implement __await__: %.100s", |
| 5774 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5775 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5776 | else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5777 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5778 | "'async with' received an object from __aexit__ " |
| 5779 | "that does not implement __await__: %.100s", |
| 5780 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5781 | } |
| 5782 | } |
| 5783 | } |
| 5784 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5785 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5786 | unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5787 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5788 | { |
| 5789 | PyObject *res; |
| 5790 | if (Py_REFCNT(v) == 2) { |
| 5791 | /* In the common case, there are 2 references to the value |
| 5792 | * stored in 'variable' when the += is performed: one on the |
| 5793 | * value stack (in 'v') and one still stored in the |
| 5794 | * 'variable'. We try to delete the variable now to reduce |
| 5795 | * the refcnt to 1. |
| 5796 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5797 | int opcode, oparg; |
| 5798 | NEXTOPARG(); |
| 5799 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5800 | case STORE_FAST: |
| 5801 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5802 | PyObject **fastlocals = f->f_localsplus; |
| 5803 | if (GETLOCAL(oparg) == v) |
| 5804 | SETLOCAL(oparg, NULL); |
| 5805 | break; |
| 5806 | } |
| 5807 | case STORE_DEREF: |
| 5808 | { |
| 5809 | PyObject **freevars = (f->f_localsplus + |
| 5810 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5811 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5812 | if (PyCell_GET(c) == v) { |
| 5813 | PyCell_SET(c, NULL); |
| 5814 | Py_DECREF(v); |
| 5815 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5816 | break; |
| 5817 | } |
| 5818 | case STORE_NAME: |
| 5819 | { |
| 5820 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5821 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5822 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5823 | if (locals && PyDict_CheckExact(locals)) { |
| 5824 | PyObject *w = PyDict_GetItemWithError(locals, name); |
| 5825 | if ((w == v && PyDict_DelItem(locals, name) != 0) || |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5826 | (w == NULL && _PyErr_Occurred(tstate))) |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5827 | { |
| 5828 | Py_DECREF(v); |
| 5829 | return NULL; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5830 | } |
| 5831 | } |
| 5832 | break; |
| 5833 | } |
| 5834 | } |
| 5835 | } |
| 5836 | res = v; |
| 5837 | PyUnicode_Append(&res, w); |
| 5838 | return res; |
| 5839 | } |
| 5840 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5841 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5842 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5843 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5844 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | int i; |
| 5847 | PyObject *l = PyList_New(256); |
| 5848 | if (l == NULL) return NULL; |
| 5849 | for (i = 0; i < 256; i++) { |
| 5850 | PyObject *x = PyLong_FromLong(a[i]); |
| 5851 | if (x == NULL) { |
| 5852 | Py_DECREF(l); |
| 5853 | return NULL; |
| 5854 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5855 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5856 | } |
| 5857 | for (i = 0; i < 256; i++) |
| 5858 | a[i] = 0; |
| 5859 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
| 5862 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5863 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5864 | { |
| 5865 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5866 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5867 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5868 | int i; |
| 5869 | PyObject *l = PyList_New(257); |
| 5870 | if (l == NULL) return NULL; |
| 5871 | for (i = 0; i < 257; i++) { |
| 5872 | PyObject *x = getarray(dxpairs[i]); |
| 5873 | if (x == NULL) { |
| 5874 | Py_DECREF(l); |
| 5875 | return NULL; |
| 5876 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5877 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5878 | } |
| 5879 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5880 | #endif |
| 5881 | } |
| 5882 | |
| 5883 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5884 | |
| 5885 | Py_ssize_t |
| 5886 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5887 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 5888 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5889 | Py_ssize_t new_index; |
| 5890 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5891 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5892 | return -1; |
| 5893 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5894 | new_index = interp->co_extra_user_count++; |
| 5895 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5896 | return new_index; |
| 5897 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5898 | |
| 5899 | static void |
| 5900 | dtrace_function_entry(PyFrameObject *f) |
| 5901 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5902 | const char *filename; |
| 5903 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5904 | int lineno; |
| 5905 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 5906 | PyCodeObject *code = f->f_code; |
| 5907 | filename = PyUnicode_AsUTF8(code->co_filename); |
| 5908 | funcname = PyUnicode_AsUTF8(code->co_name); |
| 5909 | lineno = PyCode_Addr2Line(code, f->f_lasti); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5910 | |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 5911 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | static void |
| 5915 | dtrace_function_return(PyFrameObject *f) |
| 5916 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5917 | const char *filename; |
| 5918 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5919 | int lineno; |
| 5920 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 5921 | PyCodeObject *code = f->f_code; |
| 5922 | filename = PyUnicode_AsUTF8(code->co_filename); |
| 5923 | funcname = PyUnicode_AsUTF8(code->co_name); |
| 5924 | lineno = PyCode_Addr2Line(code, f->f_lasti); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5925 | |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 5926 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5930 | static void |
| 5931 | maybe_dtrace_line(PyFrameObject *frame, |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5932 | PyCodeAddressRange *bounds, int *instr_prev) |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5933 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5934 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5935 | |
| 5936 | /* If the last instruction executed isn't in the current |
| 5937 | instruction window, reset the window. |
| 5938 | */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5939 | int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5940 | /* If the last instruction falls at the start of a line or if |
| 5941 | it represents a jump backwards, update the frame's line |
| 5942 | number and call the trace function. */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5943 | if (line != frame->f_lineno || frame->f_lasti < *instr_prev) { |
| 5944 | if (line != -1) { |
| 5945 | frame->f_lineno = line; |
| 5946 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5947 | if (!co_filename) |
| 5948 | co_filename = "?"; |
| 5949 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5950 | if (!co_name) |
| 5951 | co_name = "?"; |
| 5952 | PyDTrace_LINE(co_filename, co_name, line); |
| 5953 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5954 | } |
| 5955 | *instr_prev = frame->f_lasti; |
| 5956 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 5957 | |
| 5958 | |
| 5959 | /* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions |
| 5960 | for the limited API. */ |
| 5961 | |
| 5962 | #undef Py_EnterRecursiveCall |
| 5963 | |
| 5964 | int Py_EnterRecursiveCall(const char *where) |
| 5965 | { |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 5966 | return _Py_EnterRecursiveCall_inline(where); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 5967 | } |
| 5968 | |
| 5969 | #undef Py_LeaveRecursiveCall |
| 5970 | |
| 5971 | void Py_LeaveRecursiveCall(void) |
| 5972 | { |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 5973 | _Py_LeaveRecursiveCall_inline(); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 5974 | } |