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 | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 14 | #include "pycore_call.h" |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 15 | #include "pycore_ceval.h" |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 16 | #include "pycore_code.h" |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 17 | #include "pycore_initconfig.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 18 | #include "pycore_object.h" |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 19 | #include "pycore_pyerrors.h" |
| 20 | #include "pycore_pylifecycle.h" |
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 | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 23 | #include "pycore_sysmodule.h" |
Victor Stinner | ec13b93 | 2018-11-25 23:56:17 +0100 | [diff] [blame] | 24 | #include "pycore_tupleobject.h" |
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 *, |
| 69 | int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 70 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 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 |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 114 | #define OPCACHE_STATS 0 /* Enable stats */ |
| 115 | |
| 116 | #if OPCACHE_STATS |
| 117 | static size_t opcache_code_objects = 0; |
| 118 | static size_t opcache_code_objects_extra_mem = 0; |
| 119 | |
| 120 | static size_t opcache_global_opts = 0; |
| 121 | static size_t opcache_global_hits = 0; |
| 122 | static size_t opcache_global_misses = 0; |
| 123 | #endif |
| 124 | |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 125 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 126 | #ifndef NDEBUG |
| 127 | /* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and |
| 128 | PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen |
| 129 | when a thread continues to run after Python finalization, especially |
| 130 | daemon threads. */ |
| 131 | static int |
| 132 | is_tstate_valid(PyThreadState *tstate) |
| 133 | { |
| 134 | assert(!_PyMem_IsPtrFreed(tstate)); |
| 135 | assert(!_PyMem_IsPtrFreed(tstate->interp)); |
| 136 | return 1; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 141 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 142 | 1. We believe this is all right because the eval loop will release |
| 143 | the GIL eventually anyway. */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 144 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 145 | COMPUTE_EVAL_BREAKER(PyInterpreterState *interp, |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 146 | struct _ceval_runtime_state *ceval, |
| 147 | struct _ceval_state *ceval2) |
| 148 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 149 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, |
| 150 | _Py_atomic_load_relaxed(&ceval->gil_drop_request) |
| 151 | | (_Py_atomic_load_relaxed(&ceval->signals_pending) |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 152 | && _Py_ThreadCanHandleSignals(interp)) |
Victor Stinner | d831688 | 2020-03-20 14:50:35 +0100 | [diff] [blame] | 153 | | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do) |
| 154 | && _Py_ThreadCanHandlePendingCalls()) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 155 | | ceval2->pending.async_exc); |
| 156 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 158 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 159 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 160 | SET_GIL_DROP_REQUEST(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 161 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 162 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 163 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 164 | _Py_atomic_store_relaxed(&ceval->gil_drop_request, 1); |
| 165 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
| 166 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 168 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 169 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 170 | RESET_GIL_DROP_REQUEST(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 171 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 172 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 173 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 174 | _Py_atomic_store_relaxed(&ceval->gil_drop_request, 0); |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 175 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 176 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 177 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 178 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 179 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 180 | SIGNAL_PENDING_CALLS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 181 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 182 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 183 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 184 | _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1); |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 185 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 186 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 187 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 188 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 189 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 190 | UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 191 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 192 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 193 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 194 | _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0); |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 195 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 200 | SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 201 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 202 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 203 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 204 | _Py_atomic_store_relaxed(&ceval->signals_pending, 1); |
| 205 | /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 206 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
| 210 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 211 | UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 212 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 213 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 214 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 215 | _Py_atomic_store_relaxed(&ceval->signals_pending, 0); |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 216 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 221 | SIGNAL_ASYNC_EXC(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 222 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 223 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 224 | ceval2->pending.async_exc = 1; |
| 225 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 230 | UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 231 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 232 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 233 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 234 | ceval2->pending.async_exc = 0; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 235 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 236 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 237 | |
| 238 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 239 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 240 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 241 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 242 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 243 | |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 244 | static void |
| 245 | ensure_tstate_not_null(const char *func, PyThreadState *tstate) |
| 246 | { |
| 247 | if (tstate == NULL) { |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 248 | _Py_FatalErrorFunc(func, |
| 249 | "current thread state is NULL (released GIL?)"); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 254 | int |
Victor Stinner | 175a704 | 2020-03-10 00:37:48 +0100 | [diff] [blame] | 255 | _PyEval_ThreadsInitialized(_PyRuntimeState *runtime) |
| 256 | { |
| 257 | return gil_created(&runtime->ceval.gil); |
| 258 | } |
| 259 | |
| 260 | int |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 261 | PyEval_ThreadsInitialized(void) |
| 262 | { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 263 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 175a704 | 2020-03-10 00:37:48 +0100 | [diff] [blame] | 264 | return _PyEval_ThreadsInitialized(runtime); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 267 | PyStatus |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 268 | _PyEval_InitGIL(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 269 | { |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 270 | if (!_Py_IsMainInterpreter(tstate)) { |
| 271 | /* Currently, the GIL is shared by all interpreters, |
| 272 | and only the main interpreter is responsible to create |
| 273 | and destroy it. */ |
| 274 | return _PyStatus_OK(); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 277 | struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; |
| 278 | assert(!gil_created(gil)); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 279 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 280 | PyThread_init_thread(); |
| 281 | create_gil(gil); |
| 282 | |
| 283 | take_gil(tstate); |
| 284 | |
| 285 | assert(gil_created(gil)); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 286 | return _PyStatus_OK(); |
| 287 | } |
| 288 | |
| 289 | void |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 290 | _PyEval_FiniGIL(PyThreadState *tstate) |
| 291 | { |
| 292 | if (!_Py_IsMainInterpreter(tstate)) { |
| 293 | /* Currently, the GIL is shared by all interpreters, |
| 294 | and only the main interpreter is responsible to create |
| 295 | and destroy it. */ |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; |
| 300 | if (!gil_created(gil)) { |
| 301 | /* First Py_InitializeFromConfig() call: the GIL doesn't exist |
| 302 | yet: do nothing. */ |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | destroy_gil(gil); |
| 307 | assert(!gil_created(gil)); |
| 308 | } |
| 309 | |
| 310 | void |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 311 | PyEval_InitThreads(void) |
| 312 | { |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 313 | /* Do nothing: kept for backward compatibility */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 314 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 316 | void |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 317 | _PyEval_Fini(void) |
| 318 | { |
| 319 | #if OPCACHE_STATS |
| 320 | fprintf(stderr, "-- Opcode cache number of objects = %zd\n", |
| 321 | opcache_code_objects); |
| 322 | |
| 323 | fprintf(stderr, "-- Opcode cache total extra mem = %zd\n", |
| 324 | opcache_code_objects_extra_mem); |
| 325 | |
| 326 | fprintf(stderr, "\n"); |
| 327 | |
| 328 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n", |
| 329 | opcache_global_hits, |
| 330 | (int) (100.0 * opcache_global_hits / |
| 331 | (opcache_global_hits + opcache_global_misses))); |
| 332 | |
| 333 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n", |
| 334 | opcache_global_misses, |
| 335 | (int) (100.0 * opcache_global_misses / |
| 336 | (opcache_global_hits + opcache_global_misses))); |
| 337 | |
| 338 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n", |
| 339 | opcache_global_opts); |
| 340 | |
| 341 | fprintf(stderr, "\n"); |
| 342 | #endif |
| 343 | } |
| 344 | |
| 345 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 346 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 347 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 348 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 349 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 350 | ensure_tstate_not_null(__func__, tstate); |
| 351 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 352 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 356 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 357 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 358 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 359 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 361 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 362 | in debug mode. */ |
| 363 | drop_gil(&runtime->ceval, tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 367 | _PyEval_ReleaseLock(PyThreadState *tstate) |
| 368 | { |
| 369 | struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 370 | drop_gil(ceval, tstate); |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 374 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 375 | { |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 376 | ensure_tstate_not_null(__func__, tstate); |
| 377 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 378 | take_gil(tstate); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 379 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 380 | struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; |
| 381 | if (_PyThreadState_Swap(gilstate, tstate) != NULL) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 382 | Py_FatalError("non-NULL old thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 383 | } |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 387 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 388 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 389 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 390 | |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 391 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 392 | PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 393 | if (new_tstate != tstate) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 394 | Py_FatalError("wrong thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 395 | } |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 396 | drop_gil(&runtime->ceval, tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 397 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 398 | |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 399 | #ifdef HAVE_FORK |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 400 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
| 401 | * which are not running in the child process, and clear internal locks |
| 402 | * which might be held by those threads. |
| 403 | */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 404 | |
| 405 | void |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 406 | _PyEval_ReInitThreads(_PyRuntimeState *runtime) |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 407 | { |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 408 | struct _gil_runtime_state *gil = &runtime->ceval.gil; |
| 409 | if (!gil_created(gil)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | return; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 411 | } |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 412 | recreate_gil(gil); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 413 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
| 414 | ensure_tstate_not_null(__func__, tstate); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 415 | |
| 416 | take_gil(tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 417 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 418 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 419 | if (_PyThread_at_fork_reinit(&pending->lock) < 0) { |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 420 | Py_FatalError("Can't initialize threads for pending calls"); |
| 421 | } |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 423 | /* Destroy all threads except the current one */ |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 424 | _PyThreadState_DeleteExcept(runtime, tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 425 | } |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 426 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 427 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 428 | /* 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] | 429 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 430 | |
| 431 | void |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 432 | _PyEval_SignalAsyncExc(PyThreadState *tstate) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 433 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 434 | assert(is_tstate_valid(tstate)); |
| 435 | SIGNAL_ASYNC_EXC(tstate->interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 436 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 438 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 439 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 440 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 441 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 442 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 443 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 444 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 445 | ensure_tstate_not_null(__func__, tstate); |
| 446 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 447 | assert(gil_created(&ceval->gil)); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 448 | drop_gil(ceval, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 453 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 454 | { |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 455 | ensure_tstate_not_null(__func__, tstate); |
| 456 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 457 | take_gil(tstate); |
Victor Stinner | 17c68b8 | 2020-01-30 12:20:48 +0100 | [diff] [blame] | 458 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 459 | struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; |
| 460 | _PyThreadState_Swap(gilstate, tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 464 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 465 | signal handlers or Mac I/O completion routines) can schedule calls |
| 466 | to a function to be called synchronously. |
| 467 | The synchronous function is called with one void* argument. |
| 468 | It should return 0 for success or -1 for failure -- failure should |
| 469 | be accompanied by an exception. |
| 470 | |
| 471 | If registry succeeds, the registry function returns 0; if it fails |
| 472 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 473 | an exception condition). |
| 474 | |
| 475 | Note that because registry may occur from within signal handlers, |
| 476 | or other asynchronous events, calling malloc() is unsafe! |
| 477 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 478 | Any thread can schedule pending calls, but only the main thread |
| 479 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 480 | There is no facility to schedule calls to a particular thread, but |
| 481 | that should be easy to change, should that ever be required. In |
| 482 | that case, the static variables here should go into the python |
| 483 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 484 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 485 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 486 | void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 487 | _PyEval_SignalReceived(PyInterpreterState *interp) |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 488 | { |
| 489 | /* 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] | 490 | signal. We cannot queue a callback using _PyEval_AddPendingCall() since |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 491 | that function is not async-signal-safe. */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 492 | SIGNAL_PENDING_SIGNALS(interp); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 495 | /* Push one item onto the queue while holding the lock. */ |
| 496 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 497 | _push_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 498 | int (*func)(void *), void *arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 499 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 500 | int i = pending->last; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 501 | int j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 502 | if (j == pending->first) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 503 | return -1; /* Queue full */ |
| 504 | } |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 505 | pending->calls[i].func = func; |
| 506 | pending->calls[i].arg = arg; |
| 507 | pending->last = j; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /* Pop one item off the queue while holding the lock. */ |
| 512 | static void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 513 | _pop_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 514 | int (**func)(void *), void **arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 515 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 516 | int i = pending->first; |
| 517 | if (i == pending->last) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 518 | return; /* Queue empty */ |
| 519 | } |
| 520 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 521 | *func = pending->calls[i].func; |
| 522 | *arg = pending->calls[i].arg; |
| 523 | pending->first = (i + 1) % NPENDINGCALLS; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 526 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 527 | scheduling to be made from any thread, and even from an executing |
| 528 | callback. |
| 529 | */ |
| 530 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 531 | int |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 532 | _PyEval_AddPendingCall(PyInterpreterState *interp, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 533 | int (*func)(void *), void *arg) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 534 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 535 | struct _pending_calls *pending = &interp->ceval.pending; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 536 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 537 | /* Ensure that _PyEval_InitPendingCalls() was called |
| 538 | and that _PyEval_FiniPendingCalls() is not called yet. */ |
| 539 | assert(pending->lock != NULL); |
| 540 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 541 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 542 | int result = _push_pending_call(pending, func, arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 543 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 544 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 545 | /* signal main loop */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 546 | SIGNAL_PENDING_CALLS(interp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 550 | int |
| 551 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 552 | { |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 553 | /* Best-effort to support subinterpreters and calls with the GIL released. |
| 554 | |
| 555 | First attempt _PyThreadState_GET() since it supports subinterpreters. |
| 556 | |
| 557 | If the GIL is released, _PyThreadState_GET() returns NULL . In this |
| 558 | case, use PyGILState_GetThisThreadState() which works even if the GIL |
| 559 | is released. |
| 560 | |
| 561 | Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters: |
| 562 | see bpo-10915 and bpo-15751. |
| 563 | |
Victor Stinner | 8849e59 | 2020-03-18 19:28:53 +0100 | [diff] [blame] | 564 | Py_AddPendingCall() doesn't require the caller to hold the GIL. */ |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 565 | PyThreadState *tstate = _PyThreadState_GET(); |
| 566 | if (tstate == NULL) { |
| 567 | tstate = PyGILState_GetThisThreadState(); |
| 568 | } |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 569 | |
| 570 | PyInterpreterState *interp; |
| 571 | if (tstate != NULL) { |
| 572 | interp = tstate->interp; |
| 573 | } |
| 574 | else { |
| 575 | /* Last resort: use the main interpreter */ |
| 576 | interp = _PyRuntime.interpreters.main; |
| 577 | } |
| 578 | return _PyEval_AddPendingCall(interp, func, arg); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 581 | static int |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 582 | handle_signals(PyThreadState *tstate) |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 583 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 584 | assert(is_tstate_valid(tstate)); |
| 585 | if (!_Py_ThreadCanHandleSignals(tstate->interp)) { |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 586 | return 0; |
| 587 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 588 | |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 589 | UNSIGNAL_PENDING_SIGNALS(tstate->interp); |
Victor Stinner | 7281898 | 2020-03-26 22:28:11 +0100 | [diff] [blame] | 590 | if (_PyErr_CheckSignalsTstate(tstate) < 0) { |
| 591 | /* On failure, re-schedule a call to handle_signals(). */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 592 | SIGNAL_PENDING_SIGNALS(tstate->interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 593 | return -1; |
| 594 | } |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 599 | make_pending_calls(PyThreadState *tstate) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 600 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 601 | assert(is_tstate_valid(tstate)); |
| 602 | |
Victor Stinner | d831688 | 2020-03-20 14:50:35 +0100 | [diff] [blame] | 603 | /* only execute pending calls on main thread */ |
| 604 | if (!_Py_ThreadCanHandlePendingCalls()) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | /* don't perform recursive pending calls */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 609 | static int busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 610 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 612 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 613 | busy = 1; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 614 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 615 | /* unsignal before starting to call callbacks, so that any callback |
| 616 | added in-between re-signals */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 617 | UNSIGNAL_PENDING_CALLS(tstate->interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 618 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | /* perform a bounded number of calls, in case of recursion */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 621 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 622 | for (int i=0; i<NPENDINGCALLS; i++) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 623 | int (*func)(void *) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | void *arg = NULL; |
| 625 | |
| 626 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 627 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 628 | _pop_pending_call(pending, &func, &arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 629 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 630 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 631 | /* having released the lock, perform the callback */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 632 | if (func == NULL) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 633 | break; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 634 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 635 | res = func(arg); |
| 636 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 637 | goto error; |
| 638 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 640 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 641 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 642 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 643 | |
| 644 | error: |
| 645 | busy = 0; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 646 | SIGNAL_PENDING_CALLS(tstate->interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 647 | return res; |
| 648 | } |
| 649 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 650 | void |
Victor Stinner | 2b1df45 | 2020-01-13 18:46:59 +0100 | [diff] [blame] | 651 | _Py_FinishPendingCalls(PyThreadState *tstate) |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 652 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 653 | assert(PyGILState_Check()); |
| 654 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 655 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 656 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 657 | if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) { |
| 658 | return; |
| 659 | } |
| 660 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 661 | if (make_pending_calls(tstate) < 0) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 662 | PyObject *exc, *val, *tb; |
| 663 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 664 | PyErr_BadInternalCall(); |
| 665 | _PyErr_ChainExceptions(exc, val, tb); |
| 666 | _PyErr_Print(tstate); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 670 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 671 | of backward-compatibility. */ |
| 672 | int |
| 673 | Py_MakePendingCalls(void) |
| 674 | { |
| 675 | assert(PyGILState_Check()); |
| 676 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 677 | PyThreadState *tstate = _PyThreadState_GET(); |
| 678 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 679 | /* Python signal handler doesn't really queue a callback: it only signals |
| 680 | that a signal was received, see _PyEval_SignalReceived(). */ |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 681 | int res = handle_signals(tstate); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 682 | if (res != 0) { |
| 683 | return res; |
| 684 | } |
| 685 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 686 | res = make_pending_calls(tstate); |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 687 | if (res != 0) { |
| 688 | return res; |
| 689 | } |
| 690 | |
| 691 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 694 | /* The interpreter's recursion limit */ |
| 695 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 696 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 697 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 698 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 699 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 700 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 701 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 702 | void |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 703 | _PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 704 | { |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 705 | ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 706 | _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 707 | _gil_initialize(&ceval->gil); |
| 708 | } |
| 709 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 710 | int |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 711 | _PyEval_InitState(struct _ceval_state *ceval) |
| 712 | { |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 713 | struct _pending_calls *pending = &ceval->pending; |
| 714 | assert(pending->lock == NULL); |
| 715 | |
| 716 | pending->lock = PyThread_allocate_lock(); |
| 717 | if (pending->lock == NULL) { |
| 718 | return -1; |
| 719 | } |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | void |
| 724 | _PyEval_FiniState(struct _ceval_state *ceval) |
| 725 | { |
| 726 | struct _pending_calls *pending = &ceval->pending; |
| 727 | if (pending->lock != NULL) { |
| 728 | PyThread_free_lock(pending->lock); |
| 729 | pending->lock = NULL; |
| 730 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 731 | } |
| 732 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 733 | int |
| 734 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 735 | { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 736 | struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; |
| 737 | return ceval->recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 740 | void |
| 741 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 742 | { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 743 | struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; |
| 744 | ceval->recursion_limit = new_limit; |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 745 | _Py_CheckRecursionLimit = new_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 748 | /* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 749 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 750 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 751 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 752 | Without USE_STACKCHECK, there is no need for this. */ |
| 753 | int |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 754 | _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 755 | { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 756 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 757 | int recursion_limit = runtime->ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 758 | |
| 759 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 760 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | if (PyOS_CheckStack()) { |
| 762 | --tstate->recursion_depth; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 763 | _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | return -1; |
| 765 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 766 | /* Needed for ABI backwards-compatibility (see bpo-31857) */ |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 767 | _Py_CheckRecursionLimit = recursion_limit; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 768 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | if (tstate->recursion_critical) |
| 770 | /* Somebody asked that we don't check for recursion. */ |
| 771 | return 0; |
| 772 | if (tstate->overflowed) { |
| 773 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 774 | /* Overflowing while handling an overflow. Give up. */ |
| 775 | Py_FatalError("Cannot recover from stack overflow."); |
| 776 | } |
| 777 | return 0; |
| 778 | } |
| 779 | if (tstate->recursion_depth > recursion_limit) { |
| 780 | --tstate->recursion_depth; |
| 781 | tstate->overflowed = 1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 782 | _PyErr_Format(tstate, PyExc_RecursionError, |
| 783 | "maximum recursion depth exceeded%s", |
| 784 | where); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | return -1; |
| 786 | } |
| 787 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 790 | static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 791 | static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 792 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 793 | #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 794 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 795 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 796 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 797 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | return PyEval_EvalCodeEx(co, |
| 800 | globals, locals, |
| 801 | (PyObject **)NULL, 0, |
| 802 | (PyObject **)NULL, 0, |
| 803 | (PyObject **)NULL, 0, |
| 804 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | |
| 808 | /* Interpreter main loop */ |
| 809 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 810 | PyObject * |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 811 | PyEval_EvalFrame(PyFrameObject *f) |
| 812 | { |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 813 | /* Function kept for backward compatibility */ |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 814 | PyThreadState *tstate = _PyThreadState_GET(); |
| 815 | return _PyEval_EvalFrame(tstate, f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 819 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 820 | { |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 821 | PyThreadState *tstate = _PyThreadState_GET(); |
| 822 | return _PyEval_EvalFrame(tstate, f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 825 | |
| 826 | /* Handle signals, pending calls, GIL drop request |
| 827 | and asynchronous exception */ |
| 828 | static int |
| 829 | eval_frame_handle_pending(PyThreadState *tstate) |
| 830 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 831 | _PyRuntimeState * const runtime = &_PyRuntime; |
| 832 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 833 | |
| 834 | /* Pending signals */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 835 | if (_Py_atomic_load_relaxed(&ceval->signals_pending)) { |
| 836 | if (handle_signals(tstate) != 0) { |
| 837 | return -1; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /* Pending calls */ |
| 842 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 843 | if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) { |
| 844 | if (make_pending_calls(tstate) != 0) { |
| 845 | return -1; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /* GIL drop request */ |
| 850 | if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) { |
| 851 | /* Give another thread a chance */ |
| 852 | if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { |
| 853 | Py_FatalError("tstate mix-up"); |
| 854 | } |
| 855 | drop_gil(ceval, tstate); |
| 856 | |
| 857 | /* Other threads may run now */ |
| 858 | |
| 859 | take_gil(tstate); |
| 860 | |
| 861 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
| 862 | Py_FatalError("orphan tstate"); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /* Check for asynchronous exception. */ |
| 867 | if (tstate->async_exc != NULL) { |
| 868 | PyObject *exc = tstate->async_exc; |
| 869 | tstate->async_exc = NULL; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 870 | UNSIGNAL_ASYNC_EXC(tstate->interp); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 871 | _PyErr_SetNone(tstate, exc); |
| 872 | Py_DECREF(exc); |
| 873 | return -1; |
| 874 | } |
| 875 | |
| 876 | return 0; |
| 877 | } |
| 878 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 879 | PyObject* _Py_HOT_FUNCTION |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 880 | _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag) |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 881 | { |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 882 | ensure_tstate_not_null(__func__, tstate); |
| 883 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 884 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 886 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 887 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 888 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 889 | int opcode; /* Current opcode */ |
| 890 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 891 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 893 | struct _ceval_state * const ceval2 = &tstate->interp->ceval; |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 894 | _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | is true when the line being executed has changed. The |
| 902 | initial values are such as to make this false the first |
| 903 | time it is tested. */ |
| 904 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 905 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 906 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | PyObject *names; |
| 908 | PyObject *consts; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 909 | _PyOpcache *co_opcache; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 910 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 911 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 912 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 913 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 914 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 915 | /* Computed GOTOs, or |
| 916 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 917 | using gcc's labels-as-values extension |
| 918 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 919 | |
| 920 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 922 | combined with a lookup table of jump addresses. However, since the |
| 923 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 924 | hard time making the right prediction for where to jump next (actually, |
| 925 | it will be always wrong except in the uncommon case of a sequence of |
| 926 | several identical opcodes). |
| 927 | |
| 928 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 929 | indirect jump instruction at the end of each opcode. Since the jump |
| 930 | instruction is at a different address for each opcode, the CPU will make a |
| 931 | separate prediction for each of these instructions, which is equivalent to |
| 932 | predicting the second opcode of each opcode pair. These predictions have |
| 933 | a much better chance to turn out valid, especially in small bytecode loops. |
| 934 | |
| 935 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 937 | and potentially many more instructions (depending on the pipeline width). |
| 938 | A correctly predicted branch, however, is nearly free. |
| 939 | |
| 940 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 941 | faster than the normal "switch" version, depending on the compiler and the |
| 942 | CPU architecture. |
| 943 | |
| 944 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 945 | because it would render the measurements invalid. |
| 946 | |
| 947 | |
| 948 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 949 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 950 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 951 | -fno-crossjumping). |
| 952 | */ |
| 953 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 954 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 955 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 956 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 957 | #endif |
| 958 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 959 | #ifdef HAVE_COMPUTED_GOTOS |
| 960 | #ifndef USE_COMPUTED_GOTOS |
| 961 | #define USE_COMPUTED_GOTOS 1 |
| 962 | #endif |
| 963 | #else |
| 964 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 965 | #error "Computed gotos are not supported on this compiler." |
| 966 | #endif |
| 967 | #undef USE_COMPUTED_GOTOS |
| 968 | #define USE_COMPUTED_GOTOS 0 |
| 969 | #endif |
| 970 | |
| 971 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 972 | /* Import the static jump table */ |
| 973 | #include "opcode_targets.h" |
| 974 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 975 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 976 | op: \ |
| 977 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 979 | #ifdef LLTRACE |
| 980 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | { \ |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 982 | if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 984 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 985 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | } \ |
| 987 | goto fast_next_opcode; \ |
| 988 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 989 | #else |
| 990 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | { \ |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 992 | if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 994 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 995 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | } \ |
| 997 | goto fast_next_opcode; \ |
| 998 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 999 | #endif |
| 1000 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1001 | #define DISPATCH() \ |
| 1002 | { \ |
| 1003 | if (!_Py_atomic_load_relaxed(eval_breaker)) { \ |
| 1004 | FAST_DISPATCH(); \ |
| 1005 | } \ |
| 1006 | continue; \ |
| 1007 | } |
| 1008 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1009 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1010 | #define TARGET(op) op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1011 | #define FAST_DISPATCH() goto fast_next_opcode |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1012 | #define DISPATCH() continue |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1013 | #endif |
| 1014 | |
| 1015 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 1016 | /* Tuple access macros */ |
| 1017 | |
| 1018 | #ifndef Py_DEBUG |
| 1019 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 1020 | #else |
| 1021 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 1022 | #endif |
| 1023 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1024 | /* Code access macros */ |
| 1025 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1026 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1027 | #define INSTR_OFFSET() \ |
| 1028 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1029 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1030 | _Py_CODEUNIT word = *next_instr; \ |
| 1031 | opcode = _Py_OPCODE(word); \ |
| 1032 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1033 | next_instr++; \ |
| 1034 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1035 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 1036 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1037 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1038 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | Some opcodes tend to come in pairs thus making it possible to |
| 1040 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1041 | 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] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | Verifying the prediction costs a single high-speed test of a register |
| 1044 | variable against a constant. If the pairing was good, then the |
| 1045 | processor's own internal branch predication has a high likelihood of |
| 1046 | success, resulting in a nearly zero-overhead transition to the |
| 1047 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1048 | including its unpredictable switch-case branch. Combined with the |
| 1049 | processor's internal branch prediction, a successful PREDICT has the |
| 1050 | effect of making the two opcodes run as if they were a single new opcode |
| 1051 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1052 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 1053 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | predictions turned-on and interpret the results as if some opcodes |
| 1055 | had been combined or turn-off predictions so that the opcode frequency |
| 1056 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1057 | |
| 1058 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | the CPU to record separate branch prediction information for each |
| 1060 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1061 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1062 | */ |
| 1063 | |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1064 | #define PREDICT_ID(op) PRED_##op |
| 1065 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1066 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1067 | #define PREDICT(op) if (0) goto PREDICT_ID(op) |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 1068 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1069 | #define PREDICT(op) \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1070 | do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1071 | _Py_CODEUNIT word = *next_instr; \ |
| 1072 | opcode = _Py_OPCODE(word); \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1073 | if (opcode == op) { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1074 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1075 | next_instr++; \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1076 | goto PREDICT_ID(op); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1077 | } \ |
| 1078 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1079 | #endif |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1080 | #define PREDICTED(op) PREDICT_ID(op): |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1081 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1082 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1083 | /* Stack manipulation macros */ |
| 1084 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1085 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 1086 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1087 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 1088 | #define EMPTY() (STACK_LEVEL() == 0) |
| 1089 | #define TOP() (stack_pointer[-1]) |
| 1090 | #define SECOND() (stack_pointer[-2]) |
| 1091 | #define THIRD() (stack_pointer[-3]) |
| 1092 | #define FOURTH() (stack_pointer[-4]) |
| 1093 | #define PEEK(n) (stack_pointer[-(n)]) |
| 1094 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 1095 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 1096 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 1097 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 1098 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 1099 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 1100 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 1101 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1103 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1105 | lltrace && prtrace(tstate, TOP(), "push")); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1106 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1107 | #define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1108 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1109 | #define STACK_GROW(n) do { \ |
| 1110 | assert(n >= 0); \ |
| 1111 | (void)(BASIC_STACKADJ(n), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1112 | lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1113 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 1114 | } while (0) |
| 1115 | #define STACK_SHRINK(n) do { \ |
| 1116 | assert(n >= 0); \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1117 | (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1118 | (void)(BASIC_STACKADJ(-n)); \ |
| 1119 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 1120 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 1121 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1122 | prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1123 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1124 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1125 | #define PUSH(v) BASIC_PUSH(v) |
| 1126 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1127 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 1128 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1129 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1130 | #endif |
| 1131 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1132 | /* Local variable macros */ |
| 1133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 1137 | then store the new value; it must copy the old value to a temporary |
| 1138 | value, then store the new value, and then DECREF the temporary value. |
| 1139 | This is because it is possible that during the DECREF the frame is |
| 1140 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1141 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1143 | GETLOCAL(i) = value; \ |
| 1144 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1145 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1146 | |
| 1147 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1149 | PyObject *v = POP(); \ |
| 1150 | Py_XDECREF(v); \ |
| 1151 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1152 | |
| 1153 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1154 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1156 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1158 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1159 | value = POP(); \ |
| 1160 | Py_XDECREF(value); \ |
| 1161 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1162 | exc_info = tstate->exc_info; \ |
| 1163 | type = exc_info->exc_type; \ |
| 1164 | value = exc_info->exc_value; \ |
| 1165 | traceback = exc_info->exc_traceback; \ |
| 1166 | exc_info->exc_type = POP(); \ |
| 1167 | exc_info->exc_value = POP(); \ |
| 1168 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | Py_XDECREF(type); \ |
| 1170 | Py_XDECREF(value); \ |
| 1171 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1172 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1173 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1174 | /* macros for opcode cache */ |
| 1175 | #define OPCACHE_CHECK() \ |
| 1176 | do { \ |
| 1177 | co_opcache = NULL; \ |
| 1178 | if (co->co_opcache != NULL) { \ |
| 1179 | unsigned char co_opt_offset = \ |
| 1180 | co->co_opcache_map[next_instr - first_instr]; \ |
| 1181 | if (co_opt_offset > 0) { \ |
| 1182 | assert(co_opt_offset <= co->co_opcache_size); \ |
| 1183 | co_opcache = &co->co_opcache[co_opt_offset - 1]; \ |
| 1184 | assert(co_opcache != NULL); \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1185 | } \ |
| 1186 | } \ |
| 1187 | } while (0) |
| 1188 | |
| 1189 | #if OPCACHE_STATS |
| 1190 | |
| 1191 | #define OPCACHE_STAT_GLOBAL_HIT() \ |
| 1192 | do { \ |
| 1193 | if (co->co_opcache != NULL) opcache_global_hits++; \ |
| 1194 | } while (0) |
| 1195 | |
| 1196 | #define OPCACHE_STAT_GLOBAL_MISS() \ |
| 1197 | do { \ |
| 1198 | if (co->co_opcache != NULL) opcache_global_misses++; \ |
| 1199 | } while (0) |
| 1200 | |
| 1201 | #define OPCACHE_STAT_GLOBAL_OPT() \ |
| 1202 | do { \ |
| 1203 | if (co->co_opcache != NULL) opcache_global_opts++; \ |
| 1204 | } while (0) |
| 1205 | |
| 1206 | #else /* OPCACHE_STATS */ |
| 1207 | |
| 1208 | #define OPCACHE_STAT_GLOBAL_HIT() |
| 1209 | #define OPCACHE_STAT_GLOBAL_MISS() |
| 1210 | #define OPCACHE_STAT_GLOBAL_OPT() |
| 1211 | |
| 1212 | #endif |
| 1213 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1214 | /* Start of code */ |
| 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | /* push frame */ |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 1217 | if (_Py_EnterRecursiveCall(tstate, "")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | return NULL; |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 1219 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | if (tstate->use_tracing) { |
| 1224 | if (tstate->c_tracefunc != NULL) { |
| 1225 | /* tstate->c_tracefunc, if defined, is a |
| 1226 | function that will be called on *every* entry |
| 1227 | to a code block. Its return value, if not |
| 1228 | None, is a function that will be called at |
| 1229 | the start of each executed line of code. |
| 1230 | (Actually, the function must return itself |
| 1231 | in order to continue tracing.) The trace |
| 1232 | functions are called with three arguments: |
| 1233 | a pointer to the current frame, a string |
| 1234 | indicating why the function is called, and |
| 1235 | an argument which depends on the situation. |
| 1236 | The global trace function is also called |
| 1237 | whenever an exception is detected. */ |
| 1238 | if (call_trace_protected(tstate->c_tracefunc, |
| 1239 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1240 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | /* Trace function raised an error */ |
| 1242 | goto exit_eval_frame; |
| 1243 | } |
| 1244 | } |
| 1245 | if (tstate->c_profilefunc != NULL) { |
| 1246 | /* Similar for c_profilefunc, except it needn't |
| 1247 | return itself and isn't called for "line" events */ |
| 1248 | if (call_trace_protected(tstate->c_profilefunc, |
| 1249 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1250 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | /* Profile function raised an error */ |
| 1252 | goto exit_eval_frame; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1256 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1257 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 1258 | dtrace_function_entry(f); |
| 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | co = f->f_code; |
| 1261 | names = co->co_names; |
| 1262 | consts = co->co_consts; |
| 1263 | fastlocals = f->f_localsplus; |
| 1264 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1265 | assert(PyBytes_Check(co->co_code)); |
| 1266 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1267 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 1268 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1269 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1270 | /* |
| 1271 | f->f_lasti refers to the index of the last instruction, |
| 1272 | 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] | 1273 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1274 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1275 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1278 | direct succession without updating f->f_lasti. A successful |
| 1279 | prediction effectively links the two codes together as if they |
| 1280 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1281 | the first code in the pair (for instance, GET_ITER followed by |
| 1282 | 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] | 1283 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1285 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1286 | next_instr = first_instr; |
| 1287 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1288 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 1289 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | stack_pointer = f->f_stacktop; |
| 1292 | assert(stack_pointer != NULL); |
| 1293 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 1294 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1295 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1296 | if (co->co_opcache_flag < OPCACHE_MIN_RUNS) { |
| 1297 | co->co_opcache_flag++; |
| 1298 | if (co->co_opcache_flag == OPCACHE_MIN_RUNS) { |
| 1299 | if (_PyCode_InitOpcache(co) < 0) { |
Victor Stinner | 2510494 | 2020-04-24 02:43:18 +0200 | [diff] [blame] | 1300 | goto exit_eval_frame; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1301 | } |
| 1302 | #if OPCACHE_STATS |
| 1303 | opcache_code_objects_extra_mem += |
| 1304 | PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) + |
| 1305 | sizeof(_PyOpcache) * co->co_opcache_size; |
| 1306 | opcache_code_objects++; |
| 1307 | #endif |
| 1308 | } |
| 1309 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1310 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1311 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1312 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1313 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1314 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1315 | if (throwflag) /* support for generator.throw() */ |
| 1316 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1317 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1318 | #ifdef Py_DEBUG |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 1319 | /* _PyEval_EvalFrameDefault() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 1320 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1321 | caller loses its exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1322 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1323 | #endif |
| 1324 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1325 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1328 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1329 | assert(!_PyErr_Occurred(tstate)); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | /* Do periodic things. Doing this every time through |
| 1332 | the loop would add too much overhead, so we do it |
| 1333 | only every Nth instruction. We also do it if |
| 1334 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 1335 | event needs attention (e.g. a signal handler or |
| 1336 | async I/O handler); see Py_AddPendingCall() and |
| 1337 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1338 | |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 1339 | if (_Py_atomic_load_relaxed(eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1340 | opcode = _Py_OPCODE(*next_instr); |
| 1341 | if (opcode == SETUP_FINALLY || |
| 1342 | opcode == SETUP_WITH || |
| 1343 | opcode == BEFORE_ASYNC_WITH || |
| 1344 | opcode == YIELD_FROM) { |
| 1345 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 1346 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1347 | - If we're about to enter the 'with:'. It will prevent |
| 1348 | emitting a resource warning in the common idiom |
| 1349 | 'with open(path) as file:'. |
| 1350 | - If we're about to enter the 'async with:'. |
| 1351 | - 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] | 1352 | *very* useful, but might help in some cases and it's |
| 1353 | traditional) |
| 1354 | - If we're resuming a chain of nested 'yield from' or |
| 1355 | 'await' calls, then each frame is parked with YIELD_FROM |
| 1356 | as its next opcode. If the user hit control-C we want to |
| 1357 | wait until we've reached the innermost frame before |
| 1358 | running the signal handler and raising KeyboardInterrupt |
| 1359 | (see bpo-30039). |
| 1360 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | goto fast_next_opcode; |
| 1362 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1363 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1364 | if (eval_frame_handle_pending(tstate) != 0) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1365 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | fast_next_opcode: |
| 1370 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1371 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1372 | if (PyDTrace_LINE_ENABLED()) |
| 1373 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1376 | |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 1377 | if (_Py_TracingPossible(ceval2) && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1378 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1379 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | /* see maybe_call_line_trace |
| 1381 | for expository comments */ |
| 1382 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1385 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1386 | tstate, f, |
| 1387 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | /* Reload possibly changed frame fields */ |
| 1389 | JUMPTO(f->f_lasti); |
| 1390 | if (f->f_stacktop != NULL) { |
| 1391 | stack_pointer = f->f_stacktop; |
| 1392 | f->f_stacktop = NULL; |
| 1393 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1394 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1396 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1400 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1401 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1402 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1403 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1404 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | dxpairs[lastopcode][opcode]++; |
| 1406 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1407 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1409 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1410 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1411 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | if (lltrace) { |
| 1415 | if (HAS_ARG(opcode)) { |
| 1416 | printf("%d: %d, %d\n", |
| 1417 | f->f_lasti, opcode, oparg); |
| 1418 | } |
| 1419 | else { |
| 1420 | printf("%d: %d\n", |
| 1421 | f->f_lasti, opcode); |
| 1422 | } |
| 1423 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1424 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1429 | It is essential that any operation that fails must goto error |
| 1430 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1431 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1432 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1434 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1435 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1436 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1437 | PyObject *value = GETLOCAL(oparg); |
| 1438 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1439 | format_exc_check_arg(tstate, PyExc_UnboundLocalError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1440 | UNBOUNDLOCAL_ERROR_MSG, |
| 1441 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1442 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1444 | Py_INCREF(value); |
| 1445 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1447 | } |
| 1448 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1449 | case TARGET(LOAD_CONST): { |
| 1450 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1451 | PyObject *value = GETITEM(consts, oparg); |
| 1452 | Py_INCREF(value); |
| 1453 | PUSH(value); |
| 1454 | FAST_DISPATCH(); |
| 1455 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1456 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1457 | case TARGET(STORE_FAST): { |
| 1458 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1459 | PyObject *value = POP(); |
| 1460 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1462 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1463 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1464 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1465 | PyObject *value = POP(); |
| 1466 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1468 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1469 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1470 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1471 | PyObject *top = TOP(); |
| 1472 | PyObject *second = SECOND(); |
| 1473 | SET_TOP(second); |
| 1474 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1476 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1477 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1478 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1479 | PyObject *top = TOP(); |
| 1480 | PyObject *second = SECOND(); |
| 1481 | PyObject *third = THIRD(); |
| 1482 | SET_TOP(second); |
| 1483 | SET_SECOND(third); |
| 1484 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1485 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1486 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1487 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1488 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1489 | PyObject *top = TOP(); |
| 1490 | PyObject *second = SECOND(); |
| 1491 | PyObject *third = THIRD(); |
| 1492 | PyObject *fourth = FOURTH(); |
| 1493 | SET_TOP(second); |
| 1494 | SET_SECOND(third); |
| 1495 | SET_THIRD(fourth); |
| 1496 | SET_FOURTH(top); |
| 1497 | FAST_DISPATCH(); |
| 1498 | } |
| 1499 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1500 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1501 | PyObject *top = TOP(); |
| 1502 | Py_INCREF(top); |
| 1503 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1505 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1506 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1507 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1508 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1509 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1510 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1511 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1512 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1513 | SET_TOP(top); |
| 1514 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1515 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1516 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1517 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1518 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1519 | PyObject *value = TOP(); |
| 1520 | PyObject *res = PyNumber_Positive(value); |
| 1521 | Py_DECREF(value); |
| 1522 | SET_TOP(res); |
| 1523 | if (res == NULL) |
| 1524 | goto error; |
| 1525 | DISPATCH(); |
| 1526 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1527 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1528 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1529 | PyObject *value = TOP(); |
| 1530 | PyObject *res = PyNumber_Negative(value); |
| 1531 | Py_DECREF(value); |
| 1532 | SET_TOP(res); |
| 1533 | if (res == NULL) |
| 1534 | goto error; |
| 1535 | DISPATCH(); |
| 1536 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1537 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1538 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1539 | PyObject *value = TOP(); |
| 1540 | int err = PyObject_IsTrue(value); |
| 1541 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | if (err == 0) { |
| 1543 | Py_INCREF(Py_True); |
| 1544 | SET_TOP(Py_True); |
| 1545 | DISPATCH(); |
| 1546 | } |
| 1547 | else if (err > 0) { |
| 1548 | Py_INCREF(Py_False); |
| 1549 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | DISPATCH(); |
| 1551 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1552 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1553 | goto error; |
| 1554 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1555 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1556 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1557 | PyObject *value = TOP(); |
| 1558 | PyObject *res = PyNumber_Invert(value); |
| 1559 | Py_DECREF(value); |
| 1560 | SET_TOP(res); |
| 1561 | if (res == NULL) |
| 1562 | goto error; |
| 1563 | DISPATCH(); |
| 1564 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1565 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1566 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1567 | PyObject *exp = POP(); |
| 1568 | PyObject *base = TOP(); |
| 1569 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1570 | Py_DECREF(base); |
| 1571 | Py_DECREF(exp); |
| 1572 | SET_TOP(res); |
| 1573 | if (res == NULL) |
| 1574 | goto error; |
| 1575 | DISPATCH(); |
| 1576 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1577 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1578 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1579 | PyObject *right = POP(); |
| 1580 | PyObject *left = TOP(); |
| 1581 | PyObject *res = PyNumber_Multiply(left, right); |
| 1582 | Py_DECREF(left); |
| 1583 | Py_DECREF(right); |
| 1584 | SET_TOP(res); |
| 1585 | if (res == NULL) |
| 1586 | goto error; |
| 1587 | DISPATCH(); |
| 1588 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1589 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1590 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1591 | PyObject *right = POP(); |
| 1592 | PyObject *left = TOP(); |
| 1593 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1594 | Py_DECREF(left); |
| 1595 | Py_DECREF(right); |
| 1596 | SET_TOP(res); |
| 1597 | if (res == NULL) |
| 1598 | goto error; |
| 1599 | DISPATCH(); |
| 1600 | } |
| 1601 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1602 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1603 | PyObject *divisor = POP(); |
| 1604 | PyObject *dividend = TOP(); |
| 1605 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1606 | Py_DECREF(dividend); |
| 1607 | Py_DECREF(divisor); |
| 1608 | SET_TOP(quotient); |
| 1609 | if (quotient == NULL) |
| 1610 | goto error; |
| 1611 | DISPATCH(); |
| 1612 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1613 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1614 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1615 | PyObject *divisor = POP(); |
| 1616 | PyObject *dividend = TOP(); |
| 1617 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1618 | Py_DECREF(dividend); |
| 1619 | Py_DECREF(divisor); |
| 1620 | SET_TOP(quotient); |
| 1621 | if (quotient == NULL) |
| 1622 | goto error; |
| 1623 | DISPATCH(); |
| 1624 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1625 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1626 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1627 | PyObject *divisor = POP(); |
| 1628 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1629 | PyObject *res; |
| 1630 | if (PyUnicode_CheckExact(dividend) && ( |
| 1631 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1632 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1633 | // (see issue28598) |
| 1634 | res = PyUnicode_Format(dividend, divisor); |
| 1635 | } else { |
| 1636 | res = PyNumber_Remainder(dividend, divisor); |
| 1637 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1638 | Py_DECREF(divisor); |
| 1639 | Py_DECREF(dividend); |
| 1640 | SET_TOP(res); |
| 1641 | if (res == NULL) |
| 1642 | goto error; |
| 1643 | DISPATCH(); |
| 1644 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1645 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1646 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1647 | PyObject *right = POP(); |
| 1648 | PyObject *left = TOP(); |
| 1649 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1650 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1651 | CPython using bytecode, it is simply worthless. |
| 1652 | See http://bugs.python.org/issue21955 and |
| 1653 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1654 | no patch shown any impact on a realistic benchmark, only a minor |
| 1655 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1656 | if (PyUnicode_CheckExact(left) && |
| 1657 | PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1658 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1659 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1660 | } |
| 1661 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1662 | sum = PyNumber_Add(left, right); |
| 1663 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1664 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1665 | Py_DECREF(right); |
| 1666 | SET_TOP(sum); |
| 1667 | if (sum == NULL) |
| 1668 | goto error; |
| 1669 | DISPATCH(); |
| 1670 | } |
| 1671 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1672 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1673 | PyObject *right = POP(); |
| 1674 | PyObject *left = TOP(); |
| 1675 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1676 | Py_DECREF(right); |
| 1677 | Py_DECREF(left); |
| 1678 | SET_TOP(diff); |
| 1679 | if (diff == NULL) |
| 1680 | goto error; |
| 1681 | DISPATCH(); |
| 1682 | } |
| 1683 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1684 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1685 | PyObject *sub = POP(); |
| 1686 | PyObject *container = TOP(); |
| 1687 | PyObject *res = PyObject_GetItem(container, sub); |
| 1688 | Py_DECREF(container); |
| 1689 | Py_DECREF(sub); |
| 1690 | SET_TOP(res); |
| 1691 | if (res == NULL) |
| 1692 | goto error; |
| 1693 | DISPATCH(); |
| 1694 | } |
| 1695 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1696 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1697 | PyObject *right = POP(); |
| 1698 | PyObject *left = TOP(); |
| 1699 | PyObject *res = PyNumber_Lshift(left, right); |
| 1700 | Py_DECREF(left); |
| 1701 | Py_DECREF(right); |
| 1702 | SET_TOP(res); |
| 1703 | if (res == NULL) |
| 1704 | goto error; |
| 1705 | DISPATCH(); |
| 1706 | } |
| 1707 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1708 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1709 | PyObject *right = POP(); |
| 1710 | PyObject *left = TOP(); |
| 1711 | PyObject *res = PyNumber_Rshift(left, right); |
| 1712 | Py_DECREF(left); |
| 1713 | Py_DECREF(right); |
| 1714 | SET_TOP(res); |
| 1715 | if (res == NULL) |
| 1716 | goto error; |
| 1717 | DISPATCH(); |
| 1718 | } |
| 1719 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1720 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1721 | PyObject *right = POP(); |
| 1722 | PyObject *left = TOP(); |
| 1723 | PyObject *res = PyNumber_And(left, right); |
| 1724 | Py_DECREF(left); |
| 1725 | Py_DECREF(right); |
| 1726 | SET_TOP(res); |
| 1727 | if (res == NULL) |
| 1728 | goto error; |
| 1729 | DISPATCH(); |
| 1730 | } |
| 1731 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1732 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1733 | PyObject *right = POP(); |
| 1734 | PyObject *left = TOP(); |
| 1735 | PyObject *res = PyNumber_Xor(left, right); |
| 1736 | Py_DECREF(left); |
| 1737 | Py_DECREF(right); |
| 1738 | SET_TOP(res); |
| 1739 | if (res == NULL) |
| 1740 | goto error; |
| 1741 | DISPATCH(); |
| 1742 | } |
| 1743 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1744 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1745 | PyObject *right = POP(); |
| 1746 | PyObject *left = TOP(); |
| 1747 | PyObject *res = PyNumber_Or(left, right); |
| 1748 | Py_DECREF(left); |
| 1749 | Py_DECREF(right); |
| 1750 | SET_TOP(res); |
| 1751 | if (res == NULL) |
| 1752 | goto error; |
| 1753 | DISPATCH(); |
| 1754 | } |
| 1755 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1756 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1757 | PyObject *v = POP(); |
| 1758 | PyObject *list = PEEK(oparg); |
| 1759 | int err; |
| 1760 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1762 | if (err != 0) |
| 1763 | goto error; |
| 1764 | PREDICT(JUMP_ABSOLUTE); |
| 1765 | DISPATCH(); |
| 1766 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1767 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1768 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1769 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1770 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1771 | int err; |
| 1772 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1774 | if (err != 0) |
| 1775 | goto error; |
| 1776 | PREDICT(JUMP_ABSOLUTE); |
| 1777 | DISPATCH(); |
| 1778 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1779 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1780 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1781 | PyObject *exp = POP(); |
| 1782 | PyObject *base = TOP(); |
| 1783 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1784 | Py_DECREF(base); |
| 1785 | Py_DECREF(exp); |
| 1786 | SET_TOP(res); |
| 1787 | if (res == NULL) |
| 1788 | goto error; |
| 1789 | DISPATCH(); |
| 1790 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1791 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1792 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1793 | PyObject *right = POP(); |
| 1794 | PyObject *left = TOP(); |
| 1795 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1796 | Py_DECREF(left); |
| 1797 | Py_DECREF(right); |
| 1798 | SET_TOP(res); |
| 1799 | if (res == NULL) |
| 1800 | goto error; |
| 1801 | DISPATCH(); |
| 1802 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1803 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1804 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1805 | PyObject *right = POP(); |
| 1806 | PyObject *left = TOP(); |
| 1807 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1808 | Py_DECREF(left); |
| 1809 | Py_DECREF(right); |
| 1810 | SET_TOP(res); |
| 1811 | if (res == NULL) |
| 1812 | goto error; |
| 1813 | DISPATCH(); |
| 1814 | } |
| 1815 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1816 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1817 | PyObject *divisor = POP(); |
| 1818 | PyObject *dividend = TOP(); |
| 1819 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1820 | Py_DECREF(dividend); |
| 1821 | Py_DECREF(divisor); |
| 1822 | SET_TOP(quotient); |
| 1823 | if (quotient == NULL) |
| 1824 | goto error; |
| 1825 | DISPATCH(); |
| 1826 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1827 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1828 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1829 | PyObject *divisor = POP(); |
| 1830 | PyObject *dividend = TOP(); |
| 1831 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1832 | Py_DECREF(dividend); |
| 1833 | Py_DECREF(divisor); |
| 1834 | SET_TOP(quotient); |
| 1835 | if (quotient == NULL) |
| 1836 | goto error; |
| 1837 | DISPATCH(); |
| 1838 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1839 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1840 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1841 | PyObject *right = POP(); |
| 1842 | PyObject *left = TOP(); |
| 1843 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1844 | Py_DECREF(left); |
| 1845 | Py_DECREF(right); |
| 1846 | SET_TOP(mod); |
| 1847 | if (mod == NULL) |
| 1848 | goto error; |
| 1849 | DISPATCH(); |
| 1850 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1851 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1852 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1853 | PyObject *right = POP(); |
| 1854 | PyObject *left = TOP(); |
| 1855 | PyObject *sum; |
| 1856 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1857 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1858 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1859 | } |
| 1860 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1861 | sum = PyNumber_InPlaceAdd(left, right); |
| 1862 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1863 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1864 | Py_DECREF(right); |
| 1865 | SET_TOP(sum); |
| 1866 | if (sum == NULL) |
| 1867 | goto error; |
| 1868 | DISPATCH(); |
| 1869 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1870 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1871 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1872 | PyObject *right = POP(); |
| 1873 | PyObject *left = TOP(); |
| 1874 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1875 | Py_DECREF(left); |
| 1876 | Py_DECREF(right); |
| 1877 | SET_TOP(diff); |
| 1878 | if (diff == NULL) |
| 1879 | goto error; |
| 1880 | DISPATCH(); |
| 1881 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1882 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1883 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1884 | PyObject *right = POP(); |
| 1885 | PyObject *left = TOP(); |
| 1886 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1887 | Py_DECREF(left); |
| 1888 | Py_DECREF(right); |
| 1889 | SET_TOP(res); |
| 1890 | if (res == NULL) |
| 1891 | goto error; |
| 1892 | DISPATCH(); |
| 1893 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1894 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1895 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1896 | PyObject *right = POP(); |
| 1897 | PyObject *left = TOP(); |
| 1898 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1899 | Py_DECREF(left); |
| 1900 | Py_DECREF(right); |
| 1901 | SET_TOP(res); |
| 1902 | if (res == NULL) |
| 1903 | goto error; |
| 1904 | DISPATCH(); |
| 1905 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1906 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1907 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1908 | PyObject *right = POP(); |
| 1909 | PyObject *left = TOP(); |
| 1910 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1911 | Py_DECREF(left); |
| 1912 | Py_DECREF(right); |
| 1913 | SET_TOP(res); |
| 1914 | if (res == NULL) |
| 1915 | goto error; |
| 1916 | DISPATCH(); |
| 1917 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1918 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1919 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1920 | PyObject *right = POP(); |
| 1921 | PyObject *left = TOP(); |
| 1922 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1923 | Py_DECREF(left); |
| 1924 | Py_DECREF(right); |
| 1925 | SET_TOP(res); |
| 1926 | if (res == NULL) |
| 1927 | goto error; |
| 1928 | DISPATCH(); |
| 1929 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1930 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1931 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1932 | PyObject *right = POP(); |
| 1933 | PyObject *left = TOP(); |
| 1934 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1935 | Py_DECREF(left); |
| 1936 | Py_DECREF(right); |
| 1937 | SET_TOP(res); |
| 1938 | if (res == NULL) |
| 1939 | goto error; |
| 1940 | DISPATCH(); |
| 1941 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1942 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1943 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1944 | PyObject *sub = TOP(); |
| 1945 | PyObject *container = SECOND(); |
| 1946 | PyObject *v = THIRD(); |
| 1947 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1948 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1949 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1950 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1952 | Py_DECREF(container); |
| 1953 | Py_DECREF(sub); |
| 1954 | if (err != 0) |
| 1955 | goto error; |
| 1956 | DISPATCH(); |
| 1957 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1958 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1959 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1960 | PyObject *sub = TOP(); |
| 1961 | PyObject *container = SECOND(); |
| 1962 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1963 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1964 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1965 | err = PyObject_DelItem(container, sub); |
| 1966 | Py_DECREF(container); |
| 1967 | Py_DECREF(sub); |
| 1968 | if (err != 0) |
| 1969 | goto error; |
| 1970 | DISPATCH(); |
| 1971 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1972 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1973 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1974 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1975 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1976 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1977 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1978 | if (hook == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1979 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 1980 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1981 | Py_DECREF(value); |
| 1982 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1984 | res = PyObject_CallOneArg(hook, value); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1985 | Py_DECREF(value); |
| 1986 | if (res == NULL) |
| 1987 | goto error; |
| 1988 | Py_DECREF(res); |
| 1989 | DISPATCH(); |
| 1990 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1991 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1992 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1993 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | switch (oparg) { |
| 1995 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1996 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1997 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1999 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2000 | /* fall through */ |
| 2001 | case 0: |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 2002 | if (do_raise(tstate, exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2003 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2004 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | break; |
| 2006 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2007 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2008 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | break; |
| 2010 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2011 | goto error; |
| 2012 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2013 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2014 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2016 | assert(f->f_iblock == 0); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2017 | assert(EMPTY()); |
| 2018 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2019 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2020 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2021 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2022 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2023 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2024 | PyObject *obj = TOP(); |
| 2025 | PyTypeObject *type = Py_TYPE(obj); |
| 2026 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2027 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2028 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2029 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2030 | |
| 2031 | if (getter != NULL) { |
| 2032 | iter = (*getter)(obj); |
| 2033 | Py_DECREF(obj); |
| 2034 | if (iter == NULL) { |
| 2035 | SET_TOP(NULL); |
| 2036 | goto error; |
| 2037 | } |
| 2038 | } |
| 2039 | else { |
| 2040 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2041 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2042 | "'async for' requires an object with " |
| 2043 | "__aiter__ method, got %.100s", |
| 2044 | type->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2045 | Py_DECREF(obj); |
| 2046 | goto error; |
| 2047 | } |
| 2048 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 2049 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 2050 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2051 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 2052 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2053 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2054 | "'async for' received an object from __aiter__ " |
| 2055 | "that does not implement __anext__: %.100s", |
| 2056 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2057 | Py_DECREF(iter); |
| 2058 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2059 | } |
| 2060 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 2061 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2062 | DISPATCH(); |
| 2063 | } |
| 2064 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2065 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2066 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2067 | PyObject *next_iter = NULL; |
| 2068 | PyObject *awaitable = NULL; |
| 2069 | PyObject *aiter = TOP(); |
| 2070 | PyTypeObject *type = Py_TYPE(aiter); |
| 2071 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2072 | if (PyAsyncGen_CheckExact(aiter)) { |
| 2073 | awaitable = type->tp_as_async->am_anext(aiter); |
| 2074 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2075 | goto error; |
| 2076 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2077 | } else { |
| 2078 | if (type->tp_as_async != NULL){ |
| 2079 | getter = type->tp_as_async->am_anext; |
| 2080 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2081 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2082 | if (getter != NULL) { |
| 2083 | next_iter = (*getter)(aiter); |
| 2084 | if (next_iter == NULL) { |
| 2085 | goto error; |
| 2086 | } |
| 2087 | } |
| 2088 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2089 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2090 | "'async for' requires an iterator with " |
| 2091 | "__anext__ method, got %.100s", |
| 2092 | type->tp_name); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2093 | goto error; |
| 2094 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2095 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2096 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 2097 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 2098 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2099 | PyExc_TypeError, |
| 2100 | "'async for' received an invalid object " |
| 2101 | "from __anext__: %.100s", |
| 2102 | Py_TYPE(next_iter)->tp_name); |
| 2103 | |
| 2104 | Py_DECREF(next_iter); |
| 2105 | goto error; |
| 2106 | } else { |
| 2107 | Py_DECREF(next_iter); |
| 2108 | } |
| 2109 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2110 | |
| 2111 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2112 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2113 | DISPATCH(); |
| 2114 | } |
| 2115 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2116 | case TARGET(GET_AWAITABLE): { |
| 2117 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2118 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2119 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2120 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2121 | if (iter == NULL) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2122 | int opcode_at_minus_3 = 0; |
| 2123 | if ((next_instr - first_instr) > 2) { |
| 2124 | opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]); |
| 2125 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2126 | format_awaitable_error(tstate, Py_TYPE(iterable), |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2127 | opcode_at_minus_3, |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2128 | _Py_OPCODE(next_instr[-2])); |
| 2129 | } |
| 2130 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2131 | Py_DECREF(iterable); |
| 2132 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2133 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2134 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2135 | if (yf != NULL) { |
| 2136 | /* `iter` is a coroutine object that is being |
| 2137 | awaited, `yf` is a pointer to the current awaitable |
| 2138 | being awaited on. */ |
| 2139 | Py_DECREF(yf); |
| 2140 | Py_CLEAR(iter); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2141 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2142 | "coroutine is being awaited already"); |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2143 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2144 | } |
| 2145 | } |
| 2146 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2147 | SET_TOP(iter); /* Even if it's NULL */ |
| 2148 | |
| 2149 | if (iter == NULL) { |
| 2150 | goto error; |
| 2151 | } |
| 2152 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2153 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2154 | DISPATCH(); |
| 2155 | } |
| 2156 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2157 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2158 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2159 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2160 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2161 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 2162 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2163 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 2164 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2165 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2166 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2167 | else |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 2168 | retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2169 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2170 | Py_DECREF(v); |
| 2171 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2172 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2173 | if (tstate->c_tracefunc != NULL |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2174 | && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2175 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 2176 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2177 | if (err < 0) |
| 2178 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2179 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2180 | SET_TOP(val); |
| 2181 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2182 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2183 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2184 | f->f_stacktop = stack_pointer; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2185 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 2186 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 2187 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2188 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2189 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2190 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2191 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2193 | |
| 2194 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2195 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2196 | Py_DECREF(retval); |
| 2197 | if (w == NULL) { |
| 2198 | retval = NULL; |
| 2199 | goto error; |
| 2200 | } |
| 2201 | retval = w; |
| 2202 | } |
| 2203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | f->f_stacktop = stack_pointer; |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2205 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2206 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2207 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2208 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2209 | PyObject *type, *value, *traceback; |
| 2210 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2211 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2212 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2213 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2214 | "popped block is not an except handler"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2215 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2217 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 2218 | STACK_LEVEL() <= (b)->b_level + 4); |
| 2219 | exc_info = tstate->exc_info; |
| 2220 | type = exc_info->exc_type; |
| 2221 | value = exc_info->exc_value; |
| 2222 | traceback = exc_info->exc_traceback; |
| 2223 | exc_info->exc_type = POP(); |
| 2224 | exc_info->exc_value = POP(); |
| 2225 | exc_info->exc_traceback = POP(); |
| 2226 | Py_XDECREF(type); |
| 2227 | Py_XDECREF(value); |
| 2228 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2230 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2231 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2232 | case TARGET(POP_BLOCK): { |
| 2233 | PREDICTED(POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2234 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2236 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2237 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2238 | case TARGET(RERAISE): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2239 | PyObject *exc = POP(); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2240 | PyObject *val = POP(); |
| 2241 | PyObject *tb = POP(); |
| 2242 | assert(PyExceptionClass_Check(exc)); |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 2243 | _PyErr_Restore(tstate, exc, val, tb); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2244 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2245 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2246 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2247 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2248 | PyObject *exc = POP(); |
| 2249 | assert(PyExceptionClass_Check(exc)); |
| 2250 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2251 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2252 | assert(b->b_type == EXCEPT_HANDLER); |
| 2253 | Py_DECREF(exc); |
| 2254 | UNWIND_EXCEPT_HANDLER(b); |
| 2255 | Py_DECREF(POP()); |
| 2256 | JUMPBY(oparg); |
| 2257 | FAST_DISPATCH(); |
| 2258 | } |
| 2259 | else { |
| 2260 | PyObject *val = POP(); |
| 2261 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2262 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2263 | goto exception_unwind; |
| 2264 | } |
| 2265 | } |
| 2266 | |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 2267 | case TARGET(LOAD_ASSERTION_ERROR): { |
| 2268 | PyObject *value = PyExc_AssertionError; |
| 2269 | Py_INCREF(value); |
| 2270 | PUSH(value); |
| 2271 | FAST_DISPATCH(); |
| 2272 | } |
| 2273 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2274 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2275 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2276 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2277 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2278 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2279 | bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2280 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2281 | if (!_PyErr_Occurred(tstate)) { |
| 2282 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2283 | "__build_class__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2284 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2285 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2286 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2287 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2288 | } |
| 2289 | else { |
| 2290 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2291 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2292 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2293 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2294 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2295 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
| 2296 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2297 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2298 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2299 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2301 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2302 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2303 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2304 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2305 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2306 | PyObject *name = GETITEM(names, oparg); |
| 2307 | PyObject *v = POP(); |
| 2308 | PyObject *ns = f->f_locals; |
| 2309 | int err; |
| 2310 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2311 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2312 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2314 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2316 | if (PyDict_CheckExact(ns)) |
| 2317 | err = PyDict_SetItem(ns, name, v); |
| 2318 | else |
| 2319 | err = PyObject_SetItem(ns, name, v); |
| 2320 | Py_DECREF(v); |
| 2321 | if (err != 0) |
| 2322 | goto error; |
| 2323 | DISPATCH(); |
| 2324 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2325 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2326 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2327 | PyObject *name = GETITEM(names, oparg); |
| 2328 | PyObject *ns = f->f_locals; |
| 2329 | int err; |
| 2330 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2331 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2332 | "no locals when deleting %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2333 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2335 | err = PyObject_DelItem(ns, name); |
| 2336 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2337 | format_exc_check_arg(tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2338 | NAME_ERROR_MSG, |
| 2339 | name); |
| 2340 | goto error; |
| 2341 | } |
| 2342 | DISPATCH(); |
| 2343 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2344 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2345 | case TARGET(UNPACK_SEQUENCE): { |
| 2346 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2347 | PyObject *seq = POP(), *item, **items; |
| 2348 | if (PyTuple_CheckExact(seq) && |
| 2349 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2350 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2352 | item = items[oparg]; |
| 2353 | Py_INCREF(item); |
| 2354 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2356 | } else if (PyList_CheckExact(seq) && |
| 2357 | PyList_GET_SIZE(seq) == oparg) { |
| 2358 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2360 | item = items[oparg]; |
| 2361 | Py_INCREF(item); |
| 2362 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2364 | } else if (unpack_iterable(tstate, seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2366 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | } else { |
| 2368 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2369 | Py_DECREF(seq); |
| 2370 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2372 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2373 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2375 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2376 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2377 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2378 | PyObject *seq = POP(); |
| 2379 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2380 | if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2381 | stack_pointer + totalargs)) { |
| 2382 | stack_pointer += totalargs; |
| 2383 | } else { |
| 2384 | Py_DECREF(seq); |
| 2385 | goto error; |
| 2386 | } |
| 2387 | Py_DECREF(seq); |
| 2388 | DISPATCH(); |
| 2389 | } |
| 2390 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2391 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2392 | PyObject *name = GETITEM(names, oparg); |
| 2393 | PyObject *owner = TOP(); |
| 2394 | PyObject *v = SECOND(); |
| 2395 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2396 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2397 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2399 | Py_DECREF(owner); |
| 2400 | if (err != 0) |
| 2401 | goto error; |
| 2402 | DISPATCH(); |
| 2403 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2404 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2405 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2406 | PyObject *name = GETITEM(names, oparg); |
| 2407 | PyObject *owner = POP(); |
| 2408 | int err; |
| 2409 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2410 | Py_DECREF(owner); |
| 2411 | if (err != 0) |
| 2412 | goto error; |
| 2413 | DISPATCH(); |
| 2414 | } |
| 2415 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2416 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2417 | PyObject *name = GETITEM(names, oparg); |
| 2418 | PyObject *v = POP(); |
| 2419 | int err; |
| 2420 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2422 | if (err != 0) |
| 2423 | goto error; |
| 2424 | DISPATCH(); |
| 2425 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2426 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2427 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2428 | PyObject *name = GETITEM(names, oparg); |
| 2429 | int err; |
| 2430 | err = PyDict_DelItem(f->f_globals, name); |
| 2431 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2432 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
| 2433 | format_exc_check_arg(tstate, PyExc_NameError, |
| 2434 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2435 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2436 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2437 | } |
| 2438 | DISPATCH(); |
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(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2442 | PyObject *name = GETITEM(names, oparg); |
| 2443 | PyObject *locals = f->f_locals; |
| 2444 | PyObject *v; |
| 2445 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2446 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2447 | "no locals when loading %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2448 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2450 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2451 | v = PyDict_GetItemWithError(locals, name); |
| 2452 | if (v != NULL) { |
| 2453 | Py_INCREF(v); |
| 2454 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2455 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2456 | goto error; |
| 2457 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | } |
| 2459 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2460 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2461 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2462 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2463 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2464 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | } |
| 2466 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2467 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2468 | v = PyDict_GetItemWithError(f->f_globals, name); |
| 2469 | if (v != NULL) { |
| 2470 | Py_INCREF(v); |
| 2471 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2472 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2473 | goto error; |
| 2474 | } |
| 2475 | else { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2476 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2477 | v = PyDict_GetItemWithError(f->f_builtins, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2478 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2479 | if (!_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2480 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2481 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2482 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2483 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2484 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2485 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2486 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2487 | } |
| 2488 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2489 | v = PyObject_GetItem(f->f_builtins, name); |
| 2490 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2491 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2492 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2493 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2494 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2495 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2496 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2497 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2498 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2499 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2500 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2501 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2503 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2504 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2505 | case TARGET(LOAD_GLOBAL): { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2506 | PyObject *name; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2507 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2508 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2509 | && PyDict_CheckExact(f->f_builtins)) |
| 2510 | { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2511 | OPCACHE_CHECK(); |
| 2512 | if (co_opcache != NULL && co_opcache->optimized > 0) { |
| 2513 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2514 | |
| 2515 | if (lg->globals_ver == |
| 2516 | ((PyDictObject *)f->f_globals)->ma_version_tag |
| 2517 | && lg->builtins_ver == |
| 2518 | ((PyDictObject *)f->f_builtins)->ma_version_tag) |
| 2519 | { |
| 2520 | PyObject *ptr = lg->ptr; |
| 2521 | OPCACHE_STAT_GLOBAL_HIT(); |
| 2522 | assert(ptr != NULL); |
| 2523 | Py_INCREF(ptr); |
| 2524 | PUSH(ptr); |
| 2525 | DISPATCH(); |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2530 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2531 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2532 | name); |
| 2533 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2534 | if (!_PyErr_OCCURRED()) { |
| 2535 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2536 | * an exception if the key doesn't exist */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2537 | format_exc_check_arg(tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2538 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2539 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2540 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | } |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2542 | |
| 2543 | if (co_opcache != NULL) { |
| 2544 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2545 | |
| 2546 | if (co_opcache->optimized == 0) { |
| 2547 | /* Wasn't optimized before. */ |
| 2548 | OPCACHE_STAT_GLOBAL_OPT(); |
| 2549 | } else { |
| 2550 | OPCACHE_STAT_GLOBAL_MISS(); |
| 2551 | } |
| 2552 | |
| 2553 | co_opcache->optimized = 1; |
| 2554 | lg->globals_ver = |
| 2555 | ((PyDictObject *)f->f_globals)->ma_version_tag; |
| 2556 | lg->builtins_ver = |
| 2557 | ((PyDictObject *)f->f_builtins)->ma_version_tag; |
| 2558 | lg->ptr = v; /* borrowed */ |
| 2559 | } |
| 2560 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2561 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2563 | else { |
| 2564 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2565 | |
| 2566 | /* namespace 1: globals */ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2567 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2568 | v = PyObject_GetItem(f->f_globals, name); |
| 2569 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2570 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2571 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2572 | } |
| 2573 | _PyErr_Clear(tstate); |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2574 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2575 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2576 | v = PyObject_GetItem(f->f_builtins, name); |
| 2577 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2578 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2579 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2580 | tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2581 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2582 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2583 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2587 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2588 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2589 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2590 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2591 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2592 | PyObject *v = GETLOCAL(oparg); |
| 2593 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | SETLOCAL(oparg, NULL); |
| 2595 | DISPATCH(); |
| 2596 | } |
| 2597 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2598 | tstate, PyExc_UnboundLocalError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2599 | UNBOUNDLOCAL_ERROR_MSG, |
| 2600 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2601 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2602 | goto error; |
| 2603 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2604 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2605 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2606 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2607 | PyObject *oldobj = PyCell_GET(cell); |
| 2608 | if (oldobj != NULL) { |
| 2609 | PyCell_SET(cell, NULL); |
| 2610 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2611 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2612 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2613 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2614 | goto error; |
| 2615 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2616 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2617 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2618 | PyObject *cell = freevars[oparg]; |
| 2619 | Py_INCREF(cell); |
| 2620 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2622 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2623 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2624 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2625 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2626 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2627 | assert(locals); |
| 2628 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2629 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2630 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2631 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2632 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2633 | value = PyDict_GetItemWithError(locals, name); |
| 2634 | if (value != NULL) { |
| 2635 | Py_INCREF(value); |
| 2636 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2637 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2638 | goto error; |
| 2639 | } |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2640 | } |
| 2641 | else { |
| 2642 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2643 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2644 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2645 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2646 | } |
| 2647 | _PyErr_Clear(tstate); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | if (!value) { |
| 2651 | PyObject *cell = freevars[oparg]; |
| 2652 | value = PyCell_GET(cell); |
| 2653 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2654 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2655 | goto error; |
| 2656 | } |
| 2657 | Py_INCREF(value); |
| 2658 | } |
| 2659 | PUSH(value); |
| 2660 | DISPATCH(); |
| 2661 | } |
| 2662 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2663 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2664 | PyObject *cell = freevars[oparg]; |
| 2665 | PyObject *value = PyCell_GET(cell); |
| 2666 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2667 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2668 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2670 | Py_INCREF(value); |
| 2671 | PUSH(value); |
| 2672 | DISPATCH(); |
| 2673 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2674 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2675 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2676 | PyObject *v = POP(); |
| 2677 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2678 | PyObject *oldobj = PyCell_GET(cell); |
| 2679 | PyCell_SET(cell, v); |
| 2680 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2681 | DISPATCH(); |
| 2682 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2683 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2684 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2685 | PyObject *str; |
| 2686 | PyObject *empty = PyUnicode_New(0, 0); |
| 2687 | if (empty == NULL) { |
| 2688 | goto error; |
| 2689 | } |
| 2690 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2691 | Py_DECREF(empty); |
| 2692 | if (str == NULL) |
| 2693 | goto error; |
| 2694 | while (--oparg >= 0) { |
| 2695 | PyObject *item = POP(); |
| 2696 | Py_DECREF(item); |
| 2697 | } |
| 2698 | PUSH(str); |
| 2699 | DISPATCH(); |
| 2700 | } |
| 2701 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2702 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2703 | PyObject *tup = PyTuple_New(oparg); |
| 2704 | if (tup == NULL) |
| 2705 | goto error; |
| 2706 | while (--oparg >= 0) { |
| 2707 | PyObject *item = POP(); |
| 2708 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2709 | } |
| 2710 | PUSH(tup); |
| 2711 | DISPATCH(); |
| 2712 | } |
| 2713 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2714 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2715 | PyObject *list = PyList_New(oparg); |
| 2716 | if (list == NULL) |
| 2717 | goto error; |
| 2718 | while (--oparg >= 0) { |
| 2719 | PyObject *item = POP(); |
| 2720 | PyList_SET_ITEM(list, oparg, item); |
| 2721 | } |
| 2722 | PUSH(list); |
| 2723 | DISPATCH(); |
| 2724 | } |
| 2725 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2726 | case TARGET(LIST_TO_TUPLE): { |
| 2727 | PyObject *list = POP(); |
| 2728 | PyObject *tuple = PyList_AsTuple(list); |
| 2729 | Py_DECREF(list); |
| 2730 | if (tuple == NULL) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2731 | goto error; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2732 | } |
| 2733 | PUSH(tuple); |
| 2734 | DISPATCH(); |
| 2735 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2736 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2737 | case TARGET(LIST_EXTEND): { |
| 2738 | PyObject *iterable = POP(); |
| 2739 | PyObject *list = PEEK(oparg); |
| 2740 | PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable); |
| 2741 | if (none_val == NULL) { |
| 2742 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 2743 | (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable))) |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2744 | { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 2745 | _PyErr_Clear(tstate); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2746 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2747 | "Value after * must be an iterable, not %.200s", |
| 2748 | Py_TYPE(iterable)->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2749 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2750 | Py_DECREF(iterable); |
| 2751 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2752 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2753 | Py_DECREF(none_val); |
| 2754 | Py_DECREF(iterable); |
| 2755 | DISPATCH(); |
| 2756 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2757 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 2758 | case TARGET(SET_UPDATE): { |
| 2759 | PyObject *iterable = POP(); |
| 2760 | PyObject *set = PEEK(oparg); |
| 2761 | int err = _PySet_Update(set, iterable); |
| 2762 | Py_DECREF(iterable); |
| 2763 | if (err < 0) { |
| 2764 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2765 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2766 | DISPATCH(); |
| 2767 | } |
| 2768 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2769 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2770 | PyObject *set = PySet_New(NULL); |
| 2771 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2772 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2773 | if (set == NULL) |
| 2774 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2775 | for (i = oparg; i > 0; i--) { |
| 2776 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2777 | if (err == 0) |
| 2778 | err = PySet_Add(set, item); |
| 2779 | Py_DECREF(item); |
| 2780 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2781 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2782 | if (err != 0) { |
| 2783 | Py_DECREF(set); |
| 2784 | goto error; |
| 2785 | } |
| 2786 | PUSH(set); |
| 2787 | DISPATCH(); |
| 2788 | } |
| 2789 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2790 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2791 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2792 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2793 | if (map == NULL) |
| 2794 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2795 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2796 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2797 | PyObject *key = PEEK(2*i); |
| 2798 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2799 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2800 | if (err != 0) { |
| 2801 | Py_DECREF(map); |
| 2802 | goto error; |
| 2803 | } |
| 2804 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2805 | |
| 2806 | while (oparg--) { |
| 2807 | Py_DECREF(POP()); |
| 2808 | Py_DECREF(POP()); |
| 2809 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2810 | PUSH(map); |
| 2811 | DISPATCH(); |
| 2812 | } |
| 2813 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2814 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2815 | _Py_IDENTIFIER(__annotations__); |
| 2816 | int err; |
| 2817 | PyObject *ann_dict; |
| 2818 | if (f->f_locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2819 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2820 | "no locals found when setting up annotations"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2821 | goto error; |
| 2822 | } |
| 2823 | /* check if __annotations__ in locals()... */ |
| 2824 | if (PyDict_CheckExact(f->f_locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2825 | ann_dict = _PyDict_GetItemIdWithError(f->f_locals, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2826 | &PyId___annotations__); |
| 2827 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2828 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2829 | goto error; |
| 2830 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2831 | /* ...if not, create a new one */ |
| 2832 | ann_dict = PyDict_New(); |
| 2833 | if (ann_dict == NULL) { |
| 2834 | goto error; |
| 2835 | } |
| 2836 | err = _PyDict_SetItemId(f->f_locals, |
| 2837 | &PyId___annotations__, ann_dict); |
| 2838 | Py_DECREF(ann_dict); |
| 2839 | if (err != 0) { |
| 2840 | goto error; |
| 2841 | } |
| 2842 | } |
| 2843 | } |
| 2844 | else { |
| 2845 | /* do the same if locals() is not a dict */ |
| 2846 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2847 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2848 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2849 | } |
| 2850 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2851 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2852 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2853 | goto error; |
| 2854 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2855 | _PyErr_Clear(tstate); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2856 | ann_dict = PyDict_New(); |
| 2857 | if (ann_dict == NULL) { |
| 2858 | goto error; |
| 2859 | } |
| 2860 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2861 | Py_DECREF(ann_dict); |
| 2862 | if (err != 0) { |
| 2863 | goto error; |
| 2864 | } |
| 2865 | } |
| 2866 | else { |
| 2867 | Py_DECREF(ann_dict); |
| 2868 | } |
| 2869 | } |
| 2870 | DISPATCH(); |
| 2871 | } |
| 2872 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2873 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2874 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2875 | PyObject *map; |
| 2876 | PyObject *keys = TOP(); |
| 2877 | if (!PyTuple_CheckExact(keys) || |
| 2878 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2879 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2880 | "bad BUILD_CONST_KEY_MAP keys argument"); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2881 | goto error; |
| 2882 | } |
| 2883 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2884 | if (map == NULL) { |
| 2885 | goto error; |
| 2886 | } |
| 2887 | for (i = oparg; i > 0; i--) { |
| 2888 | int err; |
| 2889 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2890 | PyObject *value = PEEK(i + 1); |
| 2891 | err = PyDict_SetItem(map, key, value); |
| 2892 | if (err != 0) { |
| 2893 | Py_DECREF(map); |
| 2894 | goto error; |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | Py_DECREF(POP()); |
| 2899 | while (oparg--) { |
| 2900 | Py_DECREF(POP()); |
| 2901 | } |
| 2902 | PUSH(map); |
| 2903 | DISPATCH(); |
| 2904 | } |
| 2905 | |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 2906 | case TARGET(DICT_UPDATE): { |
| 2907 | PyObject *update = POP(); |
| 2908 | PyObject *dict = PEEK(oparg); |
| 2909 | if (PyDict_Update(dict, update) < 0) { |
| 2910 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 2911 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2912 | "'%.200s' object is not a mapping", |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 2913 | Py_TYPE(update)->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2914 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 2915 | Py_DECREF(update); |
| 2916 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2917 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 2918 | Py_DECREF(update); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2919 | DISPATCH(); |
| 2920 | } |
| 2921 | |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 2922 | case TARGET(DICT_MERGE): { |
| 2923 | PyObject *update = POP(); |
| 2924 | PyObject *dict = PEEK(oparg); |
| 2925 | |
| 2926 | if (_PyDict_MergeEx(dict, update, 2) < 0) { |
| 2927 | format_kwargs_error(tstate, PEEK(2 + oparg), update); |
| 2928 | Py_DECREF(update); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2929 | goto error; |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2930 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 2931 | Py_DECREF(update); |
Brandt Bucher | f185a73 | 2019-09-28 17:12:49 -0700 | [diff] [blame] | 2932 | PREDICT(CALL_FUNCTION_EX); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2933 | DISPATCH(); |
| 2934 | } |
| 2935 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2936 | case TARGET(MAP_ADD): { |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 2937 | PyObject *value = TOP(); |
| 2938 | PyObject *key = SECOND(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2939 | PyObject *map; |
| 2940 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2941 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2942 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2943 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2944 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2945 | Py_DECREF(value); |
| 2946 | Py_DECREF(key); |
| 2947 | if (err != 0) |
| 2948 | goto error; |
| 2949 | PREDICT(JUMP_ABSOLUTE); |
| 2950 | DISPATCH(); |
| 2951 | } |
| 2952 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2953 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2954 | PyObject *name = GETITEM(names, oparg); |
| 2955 | PyObject *owner = TOP(); |
| 2956 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2957 | Py_DECREF(owner); |
| 2958 | SET_TOP(res); |
| 2959 | if (res == NULL) |
| 2960 | goto error; |
| 2961 | DISPATCH(); |
| 2962 | } |
| 2963 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2964 | case TARGET(COMPARE_OP): { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2965 | assert(oparg <= Py_GE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2966 | PyObject *right = POP(); |
| 2967 | PyObject *left = TOP(); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2968 | PyObject *res = PyObject_RichCompare(left, right, oparg); |
| 2969 | SET_TOP(res); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2970 | Py_DECREF(left); |
| 2971 | Py_DECREF(right); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2972 | if (res == NULL) |
| 2973 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | PREDICT(POP_JUMP_IF_FALSE); |
| 2975 | PREDICT(POP_JUMP_IF_TRUE); |
| 2976 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2977 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2978 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2979 | case TARGET(IS_OP): { |
| 2980 | PyObject *right = POP(); |
| 2981 | PyObject *left = TOP(); |
| 2982 | int res = (left == right)^oparg; |
| 2983 | PyObject *b = res ? Py_True : Py_False; |
| 2984 | Py_INCREF(b); |
| 2985 | SET_TOP(b); |
| 2986 | Py_DECREF(left); |
| 2987 | Py_DECREF(right); |
| 2988 | PREDICT(POP_JUMP_IF_FALSE); |
| 2989 | PREDICT(POP_JUMP_IF_TRUE); |
| 2990 | FAST_DISPATCH(); |
| 2991 | } |
| 2992 | |
| 2993 | case TARGET(CONTAINS_OP): { |
| 2994 | PyObject *right = POP(); |
| 2995 | PyObject *left = POP(); |
| 2996 | int res = PySequence_Contains(right, left); |
| 2997 | Py_DECREF(left); |
| 2998 | Py_DECREF(right); |
| 2999 | if (res < 0) { |
| 3000 | goto error; |
| 3001 | } |
| 3002 | PyObject *b = (res^oparg) ? Py_True : Py_False; |
| 3003 | Py_INCREF(b); |
| 3004 | PUSH(b); |
| 3005 | PREDICT(POP_JUMP_IF_FALSE); |
| 3006 | PREDICT(POP_JUMP_IF_TRUE); |
| 3007 | FAST_DISPATCH(); |
| 3008 | } |
| 3009 | |
| 3010 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
| 3011 | "BaseException is not allowed" |
| 3012 | |
| 3013 | case TARGET(JUMP_IF_NOT_EXC_MATCH): { |
| 3014 | PyObject *right = POP(); |
| 3015 | PyObject *left = POP(); |
| 3016 | if (PyTuple_Check(right)) { |
| 3017 | Py_ssize_t i, length; |
| 3018 | length = PyTuple_GET_SIZE(right); |
| 3019 | for (i = 0; i < length; i++) { |
| 3020 | PyObject *exc = PyTuple_GET_ITEM(right, i); |
| 3021 | if (!PyExceptionClass_Check(exc)) { |
| 3022 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3023 | CANNOT_CATCH_MSG); |
| 3024 | Py_DECREF(left); |
| 3025 | Py_DECREF(right); |
| 3026 | goto error; |
| 3027 | } |
| 3028 | } |
| 3029 | } |
| 3030 | else { |
| 3031 | if (!PyExceptionClass_Check(right)) { |
| 3032 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3033 | CANNOT_CATCH_MSG); |
| 3034 | Py_DECREF(left); |
| 3035 | Py_DECREF(right); |
| 3036 | goto error; |
| 3037 | } |
| 3038 | } |
| 3039 | int res = PyErr_GivenExceptionMatches(left, right); |
| 3040 | Py_DECREF(left); |
| 3041 | Py_DECREF(right); |
| 3042 | if (res > 0) { |
| 3043 | /* Exception matches -- Do nothing */; |
| 3044 | } |
| 3045 | else if (res == 0) { |
| 3046 | JUMPTO(oparg); |
| 3047 | } |
| 3048 | else { |
| 3049 | goto error; |
| 3050 | } |
| 3051 | DISPATCH(); |
| 3052 | } |
| 3053 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3054 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3055 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 3056 | PyObject *fromlist = POP(); |
| 3057 | PyObject *level = TOP(); |
| 3058 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3059 | res = import_name(tstate, f, name, fromlist, level); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 3060 | Py_DECREF(level); |
| 3061 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3062 | SET_TOP(res); |
| 3063 | if (res == NULL) |
| 3064 | goto error; |
| 3065 | DISPATCH(); |
| 3066 | } |
| 3067 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3068 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3069 | PyObject *from = POP(), *locals; |
| 3070 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3071 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 3072 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3073 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3074 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3075 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3076 | locals = f->f_locals; |
| 3077 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3078 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3079 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3080 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3081 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3083 | err = import_all_from(tstate, locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3085 | Py_DECREF(from); |
| 3086 | if (err != 0) |
| 3087 | goto error; |
| 3088 | DISPATCH(); |
| 3089 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3090 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3091 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3092 | PyObject *name = GETITEM(names, oparg); |
| 3093 | PyObject *from = TOP(); |
| 3094 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3095 | res = import_from(tstate, from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3096 | PUSH(res); |
| 3097 | if (res == NULL) |
| 3098 | goto error; |
| 3099 | DISPATCH(); |
| 3100 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3101 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3102 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | JUMPBY(oparg); |
| 3104 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3105 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3106 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3107 | case TARGET(POP_JUMP_IF_FALSE): { |
| 3108 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3109 | PyObject *cond = POP(); |
| 3110 | int err; |
| 3111 | if (cond == Py_True) { |
| 3112 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | FAST_DISPATCH(); |
| 3114 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3115 | if (cond == Py_False) { |
| 3116 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | JUMPTO(oparg); |
| 3118 | FAST_DISPATCH(); |
| 3119 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3120 | err = PyObject_IsTrue(cond); |
| 3121 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 3123 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | else if (err == 0) |
| 3125 | JUMPTO(oparg); |
| 3126 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3127 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3129 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3130 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3131 | case TARGET(POP_JUMP_IF_TRUE): { |
| 3132 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3133 | PyObject *cond = POP(); |
| 3134 | int err; |
| 3135 | if (cond == Py_False) { |
| 3136 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | FAST_DISPATCH(); |
| 3138 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3139 | if (cond == Py_True) { |
| 3140 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | JUMPTO(oparg); |
| 3142 | FAST_DISPATCH(); |
| 3143 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3144 | err = PyObject_IsTrue(cond); |
| 3145 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | JUMPTO(oparg); |
| 3148 | } |
| 3149 | else if (err == 0) |
| 3150 | ; |
| 3151 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3152 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3154 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3155 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3156 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3157 | PyObject *cond = TOP(); |
| 3158 | int err; |
| 3159 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3160 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3161 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | FAST_DISPATCH(); |
| 3163 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3164 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | JUMPTO(oparg); |
| 3166 | FAST_DISPATCH(); |
| 3167 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3168 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3170 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3171 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | } |
| 3173 | else if (err == 0) |
| 3174 | JUMPTO(oparg); |
| 3175 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3176 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3178 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3179 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3180 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3181 | PyObject *cond = TOP(); |
| 3182 | int err; |
| 3183 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3184 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3185 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | FAST_DISPATCH(); |
| 3187 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3188 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | JUMPTO(oparg); |
| 3190 | FAST_DISPATCH(); |
| 3191 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3192 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | JUMPTO(oparg); |
| 3195 | } |
| 3196 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3197 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3198 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | } |
| 3200 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3201 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3203 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3204 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3205 | case TARGET(JUMP_ABSOLUTE): { |
| 3206 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3208 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 3210 | the per-loop checks for signals. By default, this should be turned-off |
| 3211 | because it prevents detection of a control-break in tight loops like |
| 3212 | "while 1: pass". Compile with this option turned-on when you need |
| 3213 | the speed-up and do not need break checking inside tight loops (ones |
| 3214 | that contain only instructions ending with FAST_DISPATCH). |
| 3215 | */ |
| 3216 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3217 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3219 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3220 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3221 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3222 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3224 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3225 | PyObject *iter = PyObject_GetIter(iterable); |
| 3226 | Py_DECREF(iterable); |
| 3227 | SET_TOP(iter); |
| 3228 | if (iter == NULL) |
| 3229 | goto error; |
| 3230 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3231 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3232 | DISPATCH(); |
| 3233 | } |
| 3234 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3235 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3236 | /* before: [obj]; after [getiter(obj)] */ |
| 3237 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3238 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3239 | if (PyCoro_CheckExact(iterable)) { |
| 3240 | /* `iterable` is a coroutine */ |
| 3241 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 3242 | /* and it is used in a 'yield from' expression of a |
| 3243 | regular generator. */ |
| 3244 | Py_DECREF(iterable); |
| 3245 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3246 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3247 | "cannot 'yield from' a coroutine object " |
| 3248 | "in a non-coroutine generator"); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3249 | goto error; |
| 3250 | } |
| 3251 | } |
| 3252 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3253 | /* `iterable` is not a generator. */ |
| 3254 | iter = PyObject_GetIter(iterable); |
| 3255 | Py_DECREF(iterable); |
| 3256 | SET_TOP(iter); |
| 3257 | if (iter == NULL) |
| 3258 | goto error; |
| 3259 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3260 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3261 | DISPATCH(); |
| 3262 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3263 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3264 | case TARGET(FOR_ITER): { |
| 3265 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3267 | PyObject *iter = TOP(); |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3268 | PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3269 | if (next != NULL) { |
| 3270 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | PREDICT(STORE_FAST); |
| 3272 | PREDICT(UNPACK_SEQUENCE); |
| 3273 | DISPATCH(); |
| 3274 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3275 | if (_PyErr_Occurred(tstate)) { |
| 3276 | if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3277 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3278 | } |
| 3279 | else if (tstate->c_tracefunc != NULL) { |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3280 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3281 | } |
| 3282 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | } |
| 3284 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3285 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3286 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3288 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3290 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3291 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3292 | case TARGET(SETUP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3293 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | STACK_LEVEL()); |
| 3295 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3296 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3297 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3298 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3299 | _Py_IDENTIFIER(__aenter__); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3300 | _Py_IDENTIFIER(__aexit__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3301 | PyObject *mgr = TOP(); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3302 | PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3303 | PyObject *res; |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3304 | if (enter == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3305 | goto error; |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3306 | } |
| 3307 | PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__); |
| 3308 | if (exit == NULL) { |
| 3309 | Py_DECREF(enter); |
| 3310 | goto error; |
| 3311 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3312 | SET_TOP(exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3313 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3314 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3315 | Py_DECREF(enter); |
| 3316 | if (res == NULL) |
| 3317 | goto error; |
| 3318 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3319 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3320 | DISPATCH(); |
| 3321 | } |
| 3322 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3323 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3324 | PyObject *res = POP(); |
| 3325 | /* Setup the finally block before pushing the result |
| 3326 | of __aenter__ on the stack. */ |
| 3327 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3328 | STACK_LEVEL()); |
| 3329 | PUSH(res); |
| 3330 | DISPATCH(); |
| 3331 | } |
| 3332 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3333 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3334 | _Py_IDENTIFIER(__enter__); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 3335 | _Py_IDENTIFIER(__exit__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3336 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3337 | PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3338 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3339 | if (enter == NULL) { |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3340 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3341 | } |
| 3342 | PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3343 | if (exit == NULL) { |
| 3344 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3345 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3346 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3347 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3348 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3349 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3350 | Py_DECREF(enter); |
| 3351 | if (res == NULL) |
| 3352 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3353 | /* Setup the finally block before pushing the result |
| 3354 | of __enter__ on the stack. */ |
| 3355 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3356 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3357 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3358 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | DISPATCH(); |
| 3360 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3361 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3362 | case TARGET(WITH_EXCEPT_START): { |
| 3363 | /* At the top of the stack are 7 values: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3364 | - (TOP, SECOND, THIRD) = exc_info() |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3365 | - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 3366 | - SEVENTH: the context.__exit__ bound method |
| 3367 | We call SEVENTH(TOP, SECOND, THIRD). |
| 3368 | Then we push again the TOP exception and the __exit__ |
| 3369 | return value. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3370 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3372 | PyObject *exc, *val, *tb, *res; |
| 3373 | |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3374 | exc = TOP(); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3375 | val = SECOND(); |
| 3376 | tb = THIRD(); |
| 3377 | assert(exc != Py_None); |
| 3378 | assert(!PyLong_Check(exc)); |
| 3379 | exit_func = PEEK(7); |
Jeroen Demeyer | 469d1a7 | 2019-07-03 12:52:21 +0200 | [diff] [blame] | 3380 | PyObject *stack[4] = {NULL, exc, val, tb}; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 3381 | res = PyObject_Vectorcall(exit_func, stack + 1, |
Jeroen Demeyer | 469d1a7 | 2019-07-03 12:52:21 +0200 | [diff] [blame] | 3382 | 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3383 | if (res == NULL) |
| 3384 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3385 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3386 | PUSH(res); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3387 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3389 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3390 | case TARGET(LOAD_METHOD): { |
Andrey | b021ba5 | 2019-04-29 14:33:26 +1000 | [diff] [blame] | 3391 | /* Designed to work in tandem with CALL_METHOD. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3392 | PyObject *name = GETITEM(names, oparg); |
| 3393 | PyObject *obj = TOP(); |
| 3394 | PyObject *meth = NULL; |
| 3395 | |
| 3396 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3397 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3398 | if (meth == NULL) { |
| 3399 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3400 | goto error; |
| 3401 | } |
| 3402 | |
| 3403 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3404 | /* We can bypass temporary bound method object. |
| 3405 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3406 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3407 | meth | self | arg1 | ... | argN |
| 3408 | */ |
| 3409 | SET_TOP(meth); |
| 3410 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3411 | } |
| 3412 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3413 | /* meth is not an unbound method (but a regular attr, or |
| 3414 | something was returned by a descriptor protocol). Set |
| 3415 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3416 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3417 | |
| 3418 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3419 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3420 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3421 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3422 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3423 | } |
| 3424 | DISPATCH(); |
| 3425 | } |
| 3426 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3427 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3428 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3429 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3430 | |
| 3431 | sp = stack_pointer; |
| 3432 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3433 | meth = PEEK(oparg + 2); |
| 3434 | if (meth == NULL) { |
| 3435 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3436 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3437 | |
| 3438 | Stack layout: |
| 3439 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3440 | ... | NULL | callable | arg1 | ... | argN |
| 3441 | ^- TOP() |
| 3442 | ^- (-oparg) |
| 3443 | ^- (-oparg-1) |
| 3444 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3445 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3446 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3447 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3448 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3449 | res = call_function(tstate, &sp, oparg, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3450 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3451 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3452 | } |
| 3453 | else { |
| 3454 | /* This is a method call. Stack layout: |
| 3455 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3456 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3457 | ^- TOP() |
| 3458 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3459 | ^- (-oparg-1) |
| 3460 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3461 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3462 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3463 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3464 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3465 | */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3466 | res = call_function(tstate, &sp, oparg + 1, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3467 | stack_pointer = sp; |
| 3468 | } |
| 3469 | |
| 3470 | PUSH(res); |
| 3471 | if (res == NULL) |
| 3472 | goto error; |
| 3473 | DISPATCH(); |
| 3474 | } |
| 3475 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3476 | case TARGET(CALL_FUNCTION): { |
| 3477 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3478 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3479 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3480 | res = call_function(tstate, &sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3482 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3483 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3484 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3485 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3486 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3488 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3489 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3490 | PyObject **sp, *res, *names; |
| 3491 | |
| 3492 | names = POP(); |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 3493 | assert(PyTuple_Check(names)); |
| 3494 | assert(PyTuple_GET_SIZE(names) <= oparg); |
| 3495 | /* We assume without checking that names contains only strings */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | sp = stack_pointer; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3497 | res = call_function(tstate, &sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3499 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3500 | Py_DECREF(names); |
| 3501 | |
| 3502 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3503 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3504 | } |
| 3505 | DISPATCH(); |
| 3506 | } |
| 3507 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3508 | case TARGET(CALL_FUNCTION_EX): { |
Brandt Bucher | f185a73 | 2019-09-28 17:12:49 -0700 | [diff] [blame] | 3509 | PREDICTED(CALL_FUNCTION_EX); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3510 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3511 | if (oparg & 0x01) { |
| 3512 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3513 | if (!PyDict_CheckExact(kwargs)) { |
| 3514 | PyObject *d = PyDict_New(); |
| 3515 | if (d == NULL) |
| 3516 | goto error; |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 3517 | if (_PyDict_MergeEx(d, kwargs, 2) < 0) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3518 | Py_DECREF(d); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3519 | format_kwargs_error(tstate, SECOND(), kwargs); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3520 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3521 | goto error; |
| 3522 | } |
| 3523 | Py_DECREF(kwargs); |
| 3524 | kwargs = d; |
| 3525 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3526 | assert(PyDict_CheckExact(kwargs)); |
| 3527 | } |
| 3528 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3529 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3530 | if (!PyTuple_CheckExact(callargs)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3531 | if (check_args_iterable(tstate, func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3532 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3533 | goto error; |
| 3534 | } |
| 3535 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3536 | if (callargs == NULL) { |
| 3537 | goto error; |
| 3538 | } |
| 3539 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3540 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3541 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 3542 | result = do_call_core(tstate, func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3543 | Py_DECREF(func); |
| 3544 | Py_DECREF(callargs); |
| 3545 | Py_XDECREF(kwargs); |
| 3546 | |
| 3547 | SET_TOP(result); |
| 3548 | if (result == NULL) { |
| 3549 | goto error; |
| 3550 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3551 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3553 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3554 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3555 | PyObject *qualname = POP(); |
| 3556 | PyObject *codeobj = POP(); |
| 3557 | PyFunctionObject *func = (PyFunctionObject *) |
| 3558 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3559 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3560 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3561 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3562 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3563 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3565 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3566 | if (oparg & 0x08) { |
| 3567 | assert(PyTuple_CheckExact(TOP())); |
| 3568 | func ->func_closure = POP(); |
| 3569 | } |
| 3570 | if (oparg & 0x04) { |
| 3571 | assert(PyDict_CheckExact(TOP())); |
| 3572 | func->func_annotations = POP(); |
| 3573 | } |
| 3574 | if (oparg & 0x02) { |
| 3575 | assert(PyDict_CheckExact(TOP())); |
| 3576 | func->func_kwdefaults = POP(); |
| 3577 | } |
| 3578 | if (oparg & 0x01) { |
| 3579 | assert(PyTuple_CheckExact(TOP())); |
| 3580 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3582 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3583 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3584 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3586 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3587 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3588 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3589 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3590 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3592 | step = NULL; |
| 3593 | stop = POP(); |
| 3594 | start = TOP(); |
| 3595 | slice = PySlice_New(start, stop, step); |
| 3596 | Py_DECREF(start); |
| 3597 | Py_DECREF(stop); |
| 3598 | Py_XDECREF(step); |
| 3599 | SET_TOP(slice); |
| 3600 | if (slice == NULL) |
| 3601 | goto error; |
| 3602 | DISPATCH(); |
| 3603 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3604 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3605 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3606 | /* Handles f-string value formatting. */ |
| 3607 | PyObject *result; |
| 3608 | PyObject *fmt_spec; |
| 3609 | PyObject *value; |
| 3610 | PyObject *(*conv_fn)(PyObject *); |
| 3611 | int which_conversion = oparg & FVC_MASK; |
| 3612 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3613 | |
| 3614 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3615 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3616 | |
| 3617 | /* See if any conversion is specified. */ |
| 3618 | switch (which_conversion) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3619 | case FVC_NONE: conv_fn = NULL; break; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3620 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3621 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3622 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3623 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3624 | _PyErr_Format(tstate, PyExc_SystemError, |
| 3625 | "unexpected conversion flag %d", |
| 3626 | which_conversion); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3627 | goto error; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | /* If there's a conversion function, call it and replace |
| 3631 | value with that result. Otherwise, just use value, |
| 3632 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3633 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3634 | result = conv_fn(value); |
| 3635 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3636 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3637 | Py_XDECREF(fmt_spec); |
| 3638 | goto error; |
| 3639 | } |
| 3640 | value = result; |
| 3641 | } |
| 3642 | |
| 3643 | /* If value is a unicode object, and there's no fmt_spec, |
| 3644 | then we know the result of format(value) is value |
| 3645 | itself. In that case, skip calling format(). I plan to |
| 3646 | move this optimization in to PyObject_Format() |
| 3647 | itself. */ |
| 3648 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3649 | /* Do nothing, just transfer ownership to result. */ |
| 3650 | result = value; |
| 3651 | } else { |
| 3652 | /* Actually call format(). */ |
| 3653 | result = PyObject_Format(value, fmt_spec); |
| 3654 | Py_DECREF(value); |
| 3655 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3656 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3657 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3658 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3659 | } |
| 3660 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3661 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3662 | DISPATCH(); |
| 3663 | } |
| 3664 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3665 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3666 | int oldoparg = oparg; |
| 3667 | NEXTOPARG(); |
| 3668 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3670 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3671 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3672 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3673 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3675 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | default: |
| 3677 | fprintf(stderr, |
| 3678 | "XXX lineno: %d, opcode: %d\n", |
| 3679 | PyFrame_GetLineNumber(f), |
| 3680 | opcode); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3681 | _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3682 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3684 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3685 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3686 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3687 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3688 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3689 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3690 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3691 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3692 | #ifdef NDEBUG |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3693 | if (!_PyErr_Occurred(tstate)) { |
| 3694 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3695 | "error return without exception set"); |
| 3696 | } |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3697 | #else |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3698 | assert(_PyErr_Occurred(tstate)); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3699 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3700 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3701 | /* Log traceback info. */ |
| 3702 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3703 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3704 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3705 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3706 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3707 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3708 | exception_unwind: |
| 3709 | /* Unwind stacks if an exception occurred */ |
| 3710 | while (f->f_iblock > 0) { |
| 3711 | /* Pop the current block. */ |
| 3712 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | if (b->b_type == EXCEPT_HANDLER) { |
| 3715 | UNWIND_EXCEPT_HANDLER(b); |
| 3716 | continue; |
| 3717 | } |
| 3718 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3719 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | PyObject *exc, *val, *tb; |
| 3721 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3722 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | /* Beware, this invalidates all b->b_* fields */ |
| 3724 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3725 | PUSH(exc_info->exc_traceback); |
| 3726 | PUSH(exc_info->exc_value); |
| 3727 | if (exc_info->exc_type != NULL) { |
| 3728 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | } |
| 3730 | else { |
| 3731 | Py_INCREF(Py_None); |
| 3732 | PUSH(Py_None); |
| 3733 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3734 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3735 | /* Make the raw exception data |
| 3736 | available to the handler, |
| 3737 | so a program can emulate the |
| 3738 | Python main loop. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3739 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3740 | if (tb != NULL) |
| 3741 | PyException_SetTraceback(val, tb); |
| 3742 | else |
| 3743 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3745 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3746 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3747 | exc_info->exc_value = val; |
| 3748 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | if (tb == NULL) |
| 3750 | tb = Py_None; |
| 3751 | Py_INCREF(tb); |
| 3752 | PUSH(tb); |
| 3753 | PUSH(val); |
| 3754 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | JUMPTO(handler); |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 3756 | if (_Py_TracingPossible(ceval2)) { |
Pablo Galindo | 4c53e63 | 2020-01-10 09:24:22 +0000 | [diff] [blame] | 3757 | int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub); |
| 3758 | int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev); |
| 3759 | /* Make sure that we trace line after exception if we are in a new execution |
| 3760 | * window or we don't need a line update and we are not in the first instruction |
| 3761 | * of the line. */ |
| 3762 | if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) { |
| 3763 | instr_prev = INT_MAX; |
| 3764 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3765 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3766 | /* Resume normal execution */ |
| 3767 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3768 | } |
| 3769 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3770 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3771 | /* End the loop as we still have an error */ |
| 3772 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3773 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3774 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3775 | assert(retval == NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3776 | assert(_PyErr_Occurred(tstate)); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 3777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3778 | /* Pop remaining stack entries. */ |
| 3779 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3780 | PyObject *o = POP(); |
| 3781 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3783 | |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 3784 | exiting: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3785 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3786 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3787 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3788 | tstate, f, PyTrace_RETURN, retval)) { |
| 3789 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3793 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 3794 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3795 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3800 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3801 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3802 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3803 | dtrace_function_return(f); |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 3804 | _Py_LeaveRecursiveCall(tstate); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3805 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3806 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3807 | |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 3808 | return _Py_CheckFunctionResult(tstate, NULL, retval, __func__); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3811 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3812 | format_missing(PyThreadState *tstate, const char *kind, |
| 3813 | PyCodeObject *co, PyObject *names) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3814 | { |
| 3815 | int err; |
| 3816 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3817 | PyObject *name_str, *comma, *tail, *tmp; |
| 3818 | |
| 3819 | assert(PyList_CheckExact(names)); |
| 3820 | assert(len >= 1); |
| 3821 | /* Deal with the joys of natural language. */ |
| 3822 | switch (len) { |
| 3823 | case 1: |
| 3824 | name_str = PyList_GET_ITEM(names, 0); |
| 3825 | Py_INCREF(name_str); |
| 3826 | break; |
| 3827 | case 2: |
| 3828 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3829 | PyList_GET_ITEM(names, len - 2), |
| 3830 | PyList_GET_ITEM(names, len - 1)); |
| 3831 | break; |
| 3832 | default: |
| 3833 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3834 | PyList_GET_ITEM(names, len - 2), |
| 3835 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3836 | if (tail == NULL) |
| 3837 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3838 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3839 | fail, but we can't be too careful. */ |
| 3840 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3841 | if (err == -1) { |
| 3842 | Py_DECREF(tail); |
| 3843 | return; |
| 3844 | } |
| 3845 | /* Stitch everything up into a nice comma-separated list. */ |
| 3846 | comma = PyUnicode_FromString(", "); |
| 3847 | if (comma == NULL) { |
| 3848 | Py_DECREF(tail); |
| 3849 | return; |
| 3850 | } |
| 3851 | tmp = PyUnicode_Join(comma, names); |
| 3852 | Py_DECREF(comma); |
| 3853 | if (tmp == NULL) { |
| 3854 | Py_DECREF(tail); |
| 3855 | return; |
| 3856 | } |
| 3857 | name_str = PyUnicode_Concat(tmp, tail); |
| 3858 | Py_DECREF(tmp); |
| 3859 | Py_DECREF(tail); |
| 3860 | break; |
| 3861 | } |
| 3862 | if (name_str == NULL) |
| 3863 | return; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3864 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3865 | "%U() missing %i required %s argument%s: %U", |
| 3866 | co->co_name, |
| 3867 | len, |
| 3868 | kind, |
| 3869 | len == 1 ? "" : "s", |
| 3870 | name_str); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3871 | Py_DECREF(name_str); |
| 3872 | } |
| 3873 | |
| 3874 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3875 | missing_arguments(PyThreadState *tstate, PyCodeObject *co, |
| 3876 | Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3877 | PyObject **fastlocals) |
| 3878 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3879 | Py_ssize_t i, j = 0; |
| 3880 | Py_ssize_t start, end; |
| 3881 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3882 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3883 | PyObject *missing_names; |
| 3884 | |
| 3885 | /* Compute the names of the arguments that are missing. */ |
| 3886 | missing_names = PyList_New(missing); |
| 3887 | if (missing_names == NULL) |
| 3888 | return; |
| 3889 | if (positional) { |
| 3890 | start = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3891 | end = co->co_argcount - defcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3892 | } |
| 3893 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3894 | start = co->co_argcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3895 | end = start + co->co_kwonlyargcount; |
| 3896 | } |
| 3897 | for (i = start; i < end; i++) { |
| 3898 | if (GETLOCAL(i) == NULL) { |
| 3899 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3900 | PyObject *name = PyObject_Repr(raw); |
| 3901 | if (name == NULL) { |
| 3902 | Py_DECREF(missing_names); |
| 3903 | return; |
| 3904 | } |
| 3905 | PyList_SET_ITEM(missing_names, j++, name); |
| 3906 | } |
| 3907 | } |
| 3908 | assert(j == missing); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3909 | format_missing(tstate, kind, co, missing_names); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3910 | Py_DECREF(missing_names); |
| 3911 | } |
| 3912 | |
| 3913 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3914 | too_many_positional(PyThreadState *tstate, PyCodeObject *co, |
| 3915 | Py_ssize_t given, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3916 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3917 | { |
| 3918 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3919 | Py_ssize_t kwonly_given = 0; |
| 3920 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3921 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3922 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3923 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3924 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3925 | /* Count missing keyword-only args. */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3926 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3927 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3928 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3929 | } |
| 3930 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3931 | if (defcount) { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3932 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3933 | plural = 1; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3934 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3935 | } |
| 3936 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 3937 | plural = (co_argcount != 1); |
| 3938 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3939 | } |
| 3940 | if (sig == NULL) |
| 3941 | return; |
| 3942 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3943 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3944 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3945 | given != 1 ? "s" : "", |
| 3946 | kwonly_given, |
| 3947 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3948 | if (kwonly_sig == NULL) { |
| 3949 | Py_DECREF(sig); |
| 3950 | return; |
| 3951 | } |
| 3952 | } |
| 3953 | else { |
| 3954 | /* This will not fail. */ |
| 3955 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3956 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3957 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3958 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3959 | "%U() takes %U positional argument%s but %zd%U %s given", |
| 3960 | co->co_name, |
| 3961 | sig, |
| 3962 | plural ? "s" : "", |
| 3963 | given, |
| 3964 | kwonly_sig, |
| 3965 | given == 1 && !kwonly_given ? "was" : "were"); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3966 | Py_DECREF(sig); |
| 3967 | Py_DECREF(kwonly_sig); |
| 3968 | } |
| 3969 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 3970 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3971 | positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co, |
| 3972 | Py_ssize_t kwcount, PyObject* const* kwnames) |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 3973 | { |
| 3974 | int posonly_conflicts = 0; |
| 3975 | PyObject* posonly_names = PyList_New(0); |
| 3976 | |
| 3977 | for(int k=0; k < co->co_posonlyargcount; k++){ |
| 3978 | PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k); |
| 3979 | |
| 3980 | for (int k2=0; k2<kwcount; k2++){ |
| 3981 | /* Compare the pointers first and fallback to PyObject_RichCompareBool*/ |
| 3982 | PyObject* kwname = kwnames[k2]; |
| 3983 | if (kwname == posonly_name){ |
| 3984 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 3985 | goto fail; |
| 3986 | } |
| 3987 | posonly_conflicts++; |
| 3988 | continue; |
| 3989 | } |
| 3990 | |
| 3991 | int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ); |
| 3992 | |
| 3993 | if ( cmp > 0) { |
| 3994 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 3995 | goto fail; |
| 3996 | } |
| 3997 | posonly_conflicts++; |
| 3998 | } else if (cmp < 0) { |
| 3999 | goto fail; |
| 4000 | } |
| 4001 | |
| 4002 | } |
| 4003 | } |
| 4004 | if (posonly_conflicts) { |
| 4005 | PyObject* comma = PyUnicode_FromString(", "); |
| 4006 | if (comma == NULL) { |
| 4007 | goto fail; |
| 4008 | } |
| 4009 | PyObject* error_names = PyUnicode_Join(comma, posonly_names); |
| 4010 | Py_DECREF(comma); |
| 4011 | if (error_names == NULL) { |
| 4012 | goto fail; |
| 4013 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4014 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4015 | "%U() got some positional-only arguments passed" |
| 4016 | " as keyword arguments: '%U'", |
| 4017 | co->co_name, error_names); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4018 | Py_DECREF(error_names); |
| 4019 | goto fail; |
| 4020 | } |
| 4021 | |
| 4022 | Py_DECREF(posonly_names); |
| 4023 | return 0; |
| 4024 | |
| 4025 | fail: |
| 4026 | Py_XDECREF(posonly_names); |
| 4027 | return 1; |
| 4028 | |
| 4029 | } |
| 4030 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4031 | /* 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] | 4032 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4033 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 4034 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4035 | PyObject * |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4036 | _PyEval_EvalCode(PyThreadState *tstate, |
| 4037 | PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4038 | PyObject *const *args, Py_ssize_t argcount, |
| 4039 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4040 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4041 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4042 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4043 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4044 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4045 | assert(is_tstate_valid(tstate)); |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4046 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4047 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4048 | PyFrameObject *f; |
| 4049 | PyObject *retval = NULL; |
| 4050 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4051 | PyObject *x, *u; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4052 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4053 | Py_ssize_t i, j, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4054 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | if (globals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4057 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 4058 | "PyEval_EvalCodeEx: NULL globals"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4059 | return NULL; |
| 4060 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4061 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4062 | /* Create the frame */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4063 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4064 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4066 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | fastlocals = f->f_localsplus; |
| 4068 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4069 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4070 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4071 | if (co->co_flags & CO_VARKEYWORDS) { |
| 4072 | kwdict = PyDict_New(); |
| 4073 | if (kwdict == NULL) |
| 4074 | goto fail; |
| 4075 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4076 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4077 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4078 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4079 | SETLOCAL(i, kwdict); |
| 4080 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4081 | else { |
| 4082 | kwdict = NULL; |
| 4083 | } |
| 4084 | |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4085 | /* Copy all positional arguments into local variables */ |
| 4086 | if (argcount > co->co_argcount) { |
| 4087 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4088 | } |
| 4089 | else { |
| 4090 | n = argcount; |
| 4091 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4092 | for (j = 0; j < n; j++) { |
| 4093 | x = args[j]; |
| 4094 | Py_INCREF(x); |
| 4095 | SETLOCAL(j, x); |
| 4096 | } |
| 4097 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4098 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4099 | if (co->co_flags & CO_VARARGS) { |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 4100 | u = _PyTuple_FromArray(args + n, argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4101 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4102 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4103 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4104 | SETLOCAL(total_args, u); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4105 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4106 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4107 | /* Handle keyword arguments passed as two strided arrays */ |
| 4108 | kwcount *= kwstep; |
| 4109 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4110 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4111 | PyObject *keyword = kwnames[i]; |
| 4112 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4113 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4114 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4115 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4116 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4117 | "%U() keywords must be strings", |
| 4118 | co->co_name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4119 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4121 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4122 | /* Speed hack: do raw pointer compares. As names are |
| 4123 | normally interned this should almost always hit. */ |
| 4124 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4125 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4126 | PyObject *name = co_varnames[j]; |
| 4127 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4128 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4129 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4130 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4131 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4132 | /* Slow fallback, just in case */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4133 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4134 | PyObject *name = co_varnames[j]; |
| 4135 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 4136 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4137 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4138 | } |
| 4139 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4140 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4141 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4142 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4143 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 4144 | assert(j >= total_args); |
| 4145 | if (kwdict == NULL) { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4146 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4147 | if (co->co_posonlyargcount |
| 4148 | && positional_only_passed_as_keyword(tstate, co, |
| 4149 | kwcount, kwnames)) |
| 4150 | { |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4151 | goto fail; |
| 4152 | } |
| 4153 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4154 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4155 | "%U() got an unexpected keyword argument '%S'", |
| 4156 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4157 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4159 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 4160 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4161 | goto fail; |
| 4162 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4163 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4164 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4165 | kw_found: |
| 4166 | if (GETLOCAL(j) != NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4167 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4168 | "%U() got multiple values for argument '%S'", |
| 4169 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4170 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4172 | Py_INCREF(value); |
| 4173 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4174 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4175 | |
| 4176 | /* Check the number of positional arguments */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4177 | if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4178 | too_many_positional(tstate, co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 | goto fail; |
| 4180 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4181 | |
| 4182 | /* Add missing positional arguments (copy default values from defs) */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4183 | if (argcount < co->co_argcount) { |
| 4184 | Py_ssize_t m = co->co_argcount - defcount; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4185 | Py_ssize_t missing = 0; |
| 4186 | for (i = argcount; i < m; i++) { |
| 4187 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4188 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4189 | } |
| 4190 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4191 | if (missing) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4192 | missing_arguments(tstate, co, missing, defcount, fastlocals); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4193 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4194 | } |
| 4195 | if (n > m) |
| 4196 | i = n - m; |
| 4197 | else |
| 4198 | i = 0; |
| 4199 | for (; i < defcount; i++) { |
| 4200 | if (GETLOCAL(m+i) == NULL) { |
| 4201 | PyObject *def = defs[i]; |
| 4202 | Py_INCREF(def); |
| 4203 | SETLOCAL(m+i, def); |
| 4204 | } |
| 4205 | } |
| 4206 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4207 | |
| 4208 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4209 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4210 | Py_ssize_t missing = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4211 | for (i = co->co_argcount; i < total_args; i++) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4212 | PyObject *name; |
| 4213 | if (GETLOCAL(i) != NULL) |
| 4214 | continue; |
| 4215 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4216 | if (kwdefs != NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4217 | PyObject *def = PyDict_GetItemWithError(kwdefs, name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4218 | if (def) { |
| 4219 | Py_INCREF(def); |
| 4220 | SETLOCAL(i, def); |
| 4221 | continue; |
| 4222 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4223 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4224 | goto fail; |
| 4225 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4226 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4227 | missing++; |
| 4228 | } |
| 4229 | if (missing) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4230 | missing_arguments(tstate, co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4231 | goto fail; |
| 4232 | } |
| 4233 | } |
| 4234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4236 | vars into frame. */ |
| 4237 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 4239 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4240 | /* Possibly account for the cell variable being an argument. */ |
| 4241 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4242 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4243 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4244 | /* Clear the local copy. */ |
| 4245 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4246 | } |
| 4247 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4248 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 4249 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 4250 | if (c == NULL) |
| 4251 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4252 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4253 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4254 | |
| 4255 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 4256 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 4257 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 4258 | Py_INCREF(o); |
| 4259 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4261 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4262 | /* Handle generator/coroutine/asynchronous generator */ |
| 4263 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4264 | PyObject *gen; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4265 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | /* Don't need to keep the reference to f_back, it will be set |
| 4268 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4269 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 4270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4271 | /* Create a new generator that owns the ready to run frame |
| 4272 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4273 | if (is_coro) { |
| 4274 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4275 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 4276 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4277 | } else { |
| 4278 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 4279 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4280 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4281 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4282 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 4283 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 4284 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4285 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4286 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4287 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4288 | |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 4289 | retval = _PyEval_EvalFrame(tstate, f, 0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4290 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4291 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4294 | which can call back into Python. While we're done with the |
| 4295 | current Python frame (f), the associated C stack is still in use, |
| 4296 | so recursion_depth must be boosted for the duration. |
| 4297 | */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4298 | if (Py_REFCNT(f) > 1) { |
| 4299 | Py_DECREF(f); |
| 4300 | _PyObject_GC_TRACK(f); |
| 4301 | } |
| 4302 | else { |
| 4303 | ++tstate->recursion_depth; |
| 4304 | Py_DECREF(f); |
| 4305 | --tstate->recursion_depth; |
| 4306 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4310 | |
| 4311 | PyObject * |
| 4312 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
| 4313 | PyObject *const *args, Py_ssize_t argcount, |
| 4314 | PyObject *const *kwnames, PyObject *const *kwargs, |
| 4315 | Py_ssize_t kwcount, int kwstep, |
| 4316 | PyObject *const *defs, Py_ssize_t defcount, |
| 4317 | PyObject *kwdefs, PyObject *closure, |
| 4318 | PyObject *name, PyObject *qualname) |
| 4319 | { |
| 4320 | PyThreadState *tstate = _PyThreadState_GET(); |
| 4321 | return _PyEval_EvalCode(tstate, _co, globals, locals, |
| 4322 | args, argcount, |
| 4323 | kwnames, kwargs, |
| 4324 | kwcount, kwstep, |
| 4325 | defs, defcount, |
| 4326 | kwdefs, closure, |
| 4327 | name, qualname); |
| 4328 | } |
| 4329 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4330 | PyObject * |
| 4331 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4332 | PyObject *const *args, int argcount, |
| 4333 | PyObject *const *kws, int kwcount, |
| 4334 | PyObject *const *defs, int defcount, |
| 4335 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4336 | { |
| 4337 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4338 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 4339 | kws, kws != NULL ? kws + 1 : NULL, |
| 4340 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4341 | defs, defcount, |
| 4342 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4343 | NULL, NULL); |
| 4344 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4345 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4346 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4347 | special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4350 | res = _PyObject_LookupSpecial(o, id); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4351 | if (res == NULL && !_PyErr_Occurred(tstate)) { |
| 4352 | _PyErr_SetObject(tstate, PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | return NULL; |
| 4354 | } |
| 4355 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4359 | /* Logic for the raise statement (too complicated for inlining). |
| 4360 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4361 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 4362 | do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | if (exc == NULL) { |
| 4367 | /* Reraise */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4368 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4370 | type = exc_info->exc_type; |
| 4371 | value = exc_info->exc_value; |
| 4372 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4373 | if (type == Py_None || type == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4374 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 4375 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4376 | return 0; |
| 4377 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | Py_XINCREF(type); |
| 4379 | Py_XINCREF(value); |
| 4380 | Py_XINCREF(tb); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4381 | _PyErr_Restore(tstate, type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4382 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | /* We support the following forms of raise: |
| 4386 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4387 | raise <instance> |
| 4388 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 | if (PyExceptionClass_Check(exc)) { |
| 4391 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4392 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | if (value == NULL) |
| 4394 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4395 | if (!PyExceptionInstance_Check(value)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4396 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4397 | "calling %R should have returned an instance of " |
| 4398 | "BaseException, not %R", |
| 4399 | type, Py_TYPE(value)); |
| 4400 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4401 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | } |
| 4403 | else if (PyExceptionInstance_Check(exc)) { |
| 4404 | value = exc; |
| 4405 | type = PyExceptionInstance_Class(exc); |
| 4406 | Py_INCREF(type); |
| 4407 | } |
| 4408 | else { |
| 4409 | /* Not something you can raise. You get an exception |
| 4410 | anyway, just not what you specified :-) */ |
| 4411 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4412 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4413 | "exceptions must derive from BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | goto raise_error; |
| 4415 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4416 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4417 | assert(type != NULL); |
| 4418 | assert(value != NULL); |
| 4419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | if (cause) { |
| 4421 | PyObject *fixed_cause; |
| 4422 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4423 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4424 | if (fixed_cause == NULL) |
| 4425 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4426 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4428 | else if (PyExceptionInstance_Check(cause)) { |
| 4429 | fixed_cause = cause; |
| 4430 | } |
| 4431 | else if (cause == Py_None) { |
| 4432 | Py_DECREF(cause); |
| 4433 | fixed_cause = NULL; |
| 4434 | } |
| 4435 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4436 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4437 | "exception causes must derive from " |
| 4438 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | goto raise_error; |
| 4440 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4441 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4443 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4444 | _PyErr_SetObject(tstate, type, value); |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 4445 | /* _PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4446 | Py_DECREF(value); |
| 4447 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4448 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4449 | |
| 4450 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | Py_XDECREF(value); |
| 4452 | Py_XDECREF(type); |
| 4453 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4454 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4457 | /* 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] | 4458 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4459 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4460 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4461 | with a variable target. |
| 4462 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4463 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4464 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4465 | unpack_iterable(PyThreadState *tstate, PyObject *v, |
| 4466 | int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | int i = 0, j = 0; |
| 4469 | Py_ssize_t ll = 0; |
| 4470 | PyObject *it; /* iter(v) */ |
| 4471 | PyObject *w; |
| 4472 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4477 | if (it == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4478 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4479 | Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v)) |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4480 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4481 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4482 | "cannot unpack non-iterable %.200s object", |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4483 | Py_TYPE(v)->tp_name); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4484 | } |
| 4485 | return 0; |
| 4486 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4488 | for (; i < argcnt; i++) { |
| 4489 | w = PyIter_Next(it); |
| 4490 | if (w == NULL) { |
| 4491 | /* Iterator done, via error or exhaustion. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4492 | if (!_PyErr_Occurred(tstate)) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4493 | if (argcntafter == -1) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4494 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4495 | "not enough values to unpack " |
| 4496 | "(expected %d, got %d)", |
| 4497 | argcnt, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4498 | } |
| 4499 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4500 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4501 | "not enough values to unpack " |
| 4502 | "(expected at least %d, got %d)", |
| 4503 | argcnt + argcntafter, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4504 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | } |
| 4506 | goto Error; |
| 4507 | } |
| 4508 | *--sp = w; |
| 4509 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | if (argcntafter == -1) { |
| 4512 | /* We better have exhausted the iterator now. */ |
| 4513 | w = PyIter_Next(it); |
| 4514 | if (w == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4515 | if (_PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | goto Error; |
| 4517 | Py_DECREF(it); |
| 4518 | return 1; |
| 4519 | } |
| 4520 | Py_DECREF(w); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4521 | _PyErr_Format(tstate, PyExc_ValueError, |
| 4522 | "too many values to unpack (expected %d)", |
| 4523 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | goto Error; |
| 4525 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4527 | l = PySequence_List(it); |
| 4528 | if (l == NULL) |
| 4529 | goto Error; |
| 4530 | *--sp = l; |
| 4531 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | ll = PyList_GET_SIZE(l); |
| 4534 | if (ll < argcntafter) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4535 | _PyErr_Format(tstate, PyExc_ValueError, |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4536 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4537 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | goto Error; |
| 4539 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | /* Pop the "after-variable" args off the list. */ |
| 4542 | for (j = argcntafter; j > 0; j--, i++) { |
| 4543 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4544 | } |
| 4545 | /* Resize the list. */ |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 4546 | Py_SET_SIZE(l, ll - argcntafter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | Py_DECREF(it); |
| 4548 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4549 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4550 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | for (; i > 0; i--, sp++) |
| 4552 | Py_DECREF(*sp); |
| 4553 | Py_XDECREF(it); |
| 4554 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4558 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4559 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4560 | prtrace(PyThreadState *tstate, PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | printf("%s ", str); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4563 | if (PyObject_Print(v, stdout, 0) != 0) { |
| 4564 | /* Don't know what else to do */ |
| 4565 | _PyErr_Clear(tstate); |
| 4566 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | printf("\n"); |
| 4568 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4569 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4570 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4571 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4572 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4573 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4574 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4575 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4576 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4578 | _PyErr_Fetch(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4579 | if (value == NULL) { |
| 4580 | value = Py_None; |
| 4581 | Py_INCREF(value); |
| 4582 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4583 | _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4584 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4586 | if (arg == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4587 | _PyErr_Restore(tstate, type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | return; |
| 4589 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4590 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | Py_DECREF(arg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4592 | if (err == 0) { |
| 4593 | _PyErr_Restore(tstate, type, value, orig_traceback); |
| 4594 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4595 | else { |
| 4596 | Py_XDECREF(type); |
| 4597 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4598 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4599 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4602 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4603 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4604 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4605 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4607 | PyObject *type, *value, *traceback; |
| 4608 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4609 | _PyErr_Fetch(tstate, &type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4610 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | if (err == 0) |
| 4612 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4613 | _PyErr_Restore(tstate, type, value, traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | return 0; |
| 4615 | } |
| 4616 | else { |
| 4617 | Py_XDECREF(type); |
| 4618 | Py_XDECREF(value); |
| 4619 | Py_XDECREF(traceback); |
| 4620 | return -1; |
| 4621 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4624 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4625 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4626 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | int result; |
| 4630 | if (tstate->tracing) |
| 4631 | return 0; |
| 4632 | tstate->tracing++; |
| 4633 | tstate->use_tracing = 0; |
| 4634 | result = func(obj, frame, what, arg); |
| 4635 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4636 | || (tstate->c_profilefunc != NULL)); |
| 4637 | tstate->tracing--; |
| 4638 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4641 | PyObject * |
| 4642 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4643 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4644 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | int save_tracing = tstate->tracing; |
| 4646 | int save_use_tracing = tstate->use_tracing; |
| 4647 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4649 | tstate->tracing = 0; |
| 4650 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4651 | || (tstate->c_profilefunc != NULL)); |
| 4652 | result = PyObject_Call(func, args, NULL); |
| 4653 | tstate->tracing = save_tracing; |
| 4654 | tstate->use_tracing = save_use_tracing; |
| 4655 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4658 | /* 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] | 4659 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4660 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4661 | PyThreadState *tstate, PyFrameObject *frame, |
| 4662 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | int result = 0; |
| 4665 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4667 | /* If the last instruction executed isn't in the current |
| 4668 | instruction window, reset the window. |
| 4669 | */ |
| 4670 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4671 | PyAddrPair bounds; |
| 4672 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4673 | &bounds); |
| 4674 | *instr_lb = bounds.ap_lower; |
| 4675 | *instr_ub = bounds.ap_upper; |
| 4676 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4677 | /* If the last instruction falls at the start of a line or if it |
| 4678 | represents a jump backwards, update the frame's line number and |
| 4679 | then call the trace function if we're tracing source lines. |
| 4680 | */ |
| 4681 | if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | frame->f_lineno = line; |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4683 | if (frame->f_trace_lines) { |
| 4684 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 4685 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4686 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 4687 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 4688 | if (frame->f_trace_opcodes) { |
| 4689 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 4690 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | *instr_prev = frame->f_lasti; |
| 4692 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4695 | int |
| 4696 | _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) |
| 4697 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4698 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4699 | /* The caller must hold the GIL */ |
| 4700 | assert(PyGILState_Check()); |
| 4701 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4702 | /* Call _PySys_Audit() in the context of the current thread state, |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4703 | even if tstate is not the current thread state. */ |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4704 | PyThreadState *current_tstate = _PyThreadState_GET(); |
| 4705 | if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4706 | return -1; |
| 4707 | } |
| 4708 | |
| 4709 | PyObject *profileobj = tstate->c_profileobj; |
| 4710 | |
| 4711 | tstate->c_profilefunc = NULL; |
| 4712 | tstate->c_profileobj = NULL; |
| 4713 | /* Must make sure that tracing is not ignored if 'profileobj' is freed */ |
| 4714 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4715 | Py_XDECREF(profileobj); |
| 4716 | |
| 4717 | Py_XINCREF(arg); |
| 4718 | tstate->c_profileobj = arg; |
| 4719 | tstate->c_profilefunc = func; |
| 4720 | |
| 4721 | /* Flag that tracing or profiling is turned on */ |
| 4722 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
| 4723 | return 0; |
| 4724 | } |
| 4725 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4726 | void |
| 4727 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4728 | { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4729 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 4730 | if (_PyEval_SetProfile(tstate, func, arg) < 0) { |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4731 | /* Log _PySys_Audit() error */ |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 4732 | _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL); |
| 4733 | } |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | int |
| 4737 | _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) |
| 4738 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4739 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4740 | /* The caller must hold the GIL */ |
| 4741 | assert(PyGILState_Check()); |
| 4742 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4743 | /* Call _PySys_Audit() in the context of the current thread state, |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4744 | even if tstate is not the current thread state. */ |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4745 | PyThreadState *current_tstate = _PyThreadState_GET(); |
| 4746 | if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4747 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4748 | } |
| 4749 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4750 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4751 | PyObject *traceobj = tstate->c_traceobj; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4752 | ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4753 | |
| 4754 | tstate->c_tracefunc = NULL; |
| 4755 | tstate->c_traceobj = NULL; |
| 4756 | /* Must make sure that profiling is not ignored if 'traceobj' is freed */ |
| 4757 | tstate->use_tracing = (tstate->c_profilefunc != NULL); |
| 4758 | Py_XDECREF(traceobj); |
| 4759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | Py_XINCREF(arg); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4761 | tstate->c_traceobj = arg; |
| 4762 | tstate->c_tracefunc = func; |
| 4763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | /* Flag that tracing or profiling is turned on */ |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4765 | tstate->use_tracing = ((func != NULL) |
| 4766 | || (tstate->c_profilefunc != NULL)); |
| 4767 | |
| 4768 | return 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | void |
| 4772 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4773 | { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4774 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 4775 | if (_PyEval_SetTrace(tstate, func, arg) < 0) { |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4776 | /* Log _PySys_Audit() error */ |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 4777 | _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL); |
| 4778 | } |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 4781 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4782 | void |
Victor Stinner | 838f264 | 2019-06-13 22:41:23 +0200 | [diff] [blame] | 4783 | _PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth) |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4784 | { |
| 4785 | assert(new_depth >= 0); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4786 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 4787 | } |
| 4788 | |
| 4789 | int |
| 4790 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 4791 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4792 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4793 | return tstate->coroutine_origin_tracking_depth; |
| 4794 | } |
| 4795 | |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 4796 | int |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4797 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4798 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4799 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4800 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4801 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) { |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 4802 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4803 | } |
| 4804 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4805 | Py_XINCREF(firstiter); |
| 4806 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 4807 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | PyObject * |
| 4811 | _PyEval_GetAsyncGenFirstiter(void) |
| 4812 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4813 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4814 | return tstate->async_gen_firstiter; |
| 4815 | } |
| 4816 | |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 4817 | int |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4818 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4819 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4820 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4821 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 4822 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) { |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 4823 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4826 | Py_XINCREF(finalizer); |
| 4827 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 4828 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | PyObject * |
| 4832 | _PyEval_GetAsyncGenFinalizer(void) |
| 4833 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4834 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4835 | return tstate->async_gen_finalizer; |
| 4836 | } |
| 4837 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4838 | PyFrameObject * |
| 4839 | PyEval_GetFrame(void) |
| 4840 | { |
| 4841 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 4842 | return tstate->frame; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4843 | } |
| 4844 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4845 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4846 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4847 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4848 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 4849 | PyFrameObject *current_frame = tstate->frame; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4850 | if (current_frame == NULL) |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4851 | return tstate->interp->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4852 | else |
| 4853 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4856 | /* Convenience function to get a builtin from its name */ |
| 4857 | PyObject * |
| 4858 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 4859 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4860 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4861 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 4862 | if (attr) { |
| 4863 | Py_INCREF(attr); |
| 4864 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4865 | else if (!_PyErr_Occurred(tstate)) { |
| 4866 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name)); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4867 | } |
| 4868 | return attr; |
| 4869 | } |
| 4870 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4871 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4872 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4873 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4874 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 4875 | PyFrameObject *current_frame = tstate->frame; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4876 | if (current_frame == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4877 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4881 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4882 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4883 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4884 | |
| 4885 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4886 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4889 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4890 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4891 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4892 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 4893 | PyFrameObject *current_frame = tstate->frame; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4894 | if (current_frame == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4895 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4896 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4897 | |
| 4898 | assert(current_frame->f_globals != NULL); |
| 4899 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4902 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4903 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4904 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4905 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 4906 | PyFrameObject *current_frame = tstate->frame; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4909 | if (current_frame != NULL) { |
| 4910 | const int codeflags = current_frame->f_code->co_flags; |
| 4911 | const int compilerflags = codeflags & PyCF_MASK; |
| 4912 | if (compilerflags) { |
| 4913 | result = 1; |
| 4914 | cf->cf_flags |= compilerflags; |
| 4915 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4916 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4917 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4918 | result = 1; |
| 4919 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4920 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4921 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4922 | } |
| 4923 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4926 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4927 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4928 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4930 | if (PyMethod_Check(func)) |
| 4931 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4932 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4933 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4934 | else if (PyCFunction_Check(func)) |
| 4935 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4936 | else |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4937 | return Py_TYPE(func)->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4940 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4941 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4942 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | if (PyMethod_Check(func)) |
| 4944 | return "()"; |
| 4945 | else if (PyFunction_Check(func)) |
| 4946 | return "()"; |
| 4947 | else if (PyCFunction_Check(func)) |
| 4948 | return "()"; |
| 4949 | else |
| 4950 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4953 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4954 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4955 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4956 | tstate, tstate->frame, \ |
| 4957 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | x = NULL; \ |
| 4959 | } \ |
| 4960 | else { \ |
| 4961 | x = call; \ |
| 4962 | if (tstate->c_profilefunc != NULL) { \ |
| 4963 | if (x == NULL) { \ |
| 4964 | call_trace_protected(tstate->c_profilefunc, \ |
| 4965 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4966 | tstate, tstate->frame, \ |
| 4967 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4968 | /* XXX should pass (type, value, tb) */ \ |
| 4969 | } else { \ |
| 4970 | if (call_trace(tstate->c_profilefunc, \ |
| 4971 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4972 | tstate, tstate->frame, \ |
| 4973 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4974 | Py_DECREF(x); \ |
| 4975 | x = NULL; \ |
| 4976 | } \ |
| 4977 | } \ |
| 4978 | } \ |
| 4979 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4980 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | x = call; \ |
| 4982 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4983 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4984 | |
| 4985 | static PyObject * |
| 4986 | trace_call_function(PyThreadState *tstate, |
| 4987 | PyObject *func, |
| 4988 | PyObject **args, Py_ssize_t nargs, |
| 4989 | PyObject *kwnames) |
| 4990 | { |
| 4991 | PyObject *x; |
| 4992 | if (PyCFunction_Check(func)) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4993 | C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4994 | return x; |
| 4995 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 4996 | else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) { |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4997 | /* We need to create a temporary bound method as argument |
| 4998 | for profiling. |
| 4999 | |
| 5000 | If nargs == 0, then this cannot work because we have no |
| 5001 | "self". In any case, the call itself would raise |
| 5002 | TypeError (foo needs an argument), so we just skip |
| 5003 | profiling. */ |
| 5004 | PyObject *self = args[0]; |
| 5005 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5006 | if (func == NULL) { |
| 5007 | return NULL; |
| 5008 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5009 | C_TRACE(x, PyObject_Vectorcall(func, |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 5010 | args+1, nargs-1, |
| 5011 | kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5012 | Py_DECREF(func); |
| 5013 | return x; |
| 5014 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5015 | return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5016 | } |
| 5017 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 5018 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 5019 | to reduce the stack consumption. */ |
| 5020 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 5021 | 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] | 5022 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5023 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | PyObject *func = *pfunc; |
| 5025 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 5026 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 5027 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 5028 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5029 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5030 | if (tstate->use_tracing) { |
| 5031 | x = trace_call_function(tstate, func, stack, nargs, kwnames); |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 5032 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 5033 | else { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5034 | x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5035 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 5036 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5037 | assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5038 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 5039 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5040 | while ((*pp_stack) > pfunc) { |
| 5041 | w = EXT_POP(*pp_stack); |
| 5042 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5043 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 5044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5048 | static PyObject * |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 5049 | do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5050 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5051 | PyObject *result; |
| 5052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5053 | if (PyCFunction_Check(func)) { |
Jeroen Demeyer | 7a6873c | 2019-09-11 13:01:01 +0200 | [diff] [blame] | 5054 | C_TRACE(result, PyObject_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5055 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 5057 | else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5058 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 5059 | if (nargs > 0 && tstate->use_tracing) { |
| 5060 | /* We need to create a temporary bound method as argument |
| 5061 | for profiling. |
| 5062 | |
| 5063 | If nargs == 0, then this cannot work because we have no |
| 5064 | "self". In any case, the call itself would raise |
| 5065 | TypeError (foo needs an argument), so we just skip |
| 5066 | profiling. */ |
| 5067 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 5068 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5069 | if (func == NULL) { |
| 5070 | return NULL; |
| 5071 | } |
| 5072 | |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 5073 | C_TRACE(result, _PyObject_FastCallDictTstate( |
| 5074 | tstate, func, |
| 5075 | &_PyTuple_ITEMS(callargs)[1], |
| 5076 | nargs - 1, |
| 5077 | kwdict)); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5078 | Py_DECREF(func); |
| 5079 | return result; |
| 5080 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5081 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5082 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5085 | /* 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] | 5086 | nb_index slot defined, and store in *pi. |
| 5087 | 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] | 5088 | 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] | 5089 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5090 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5091 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5092 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5093 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5094 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5095 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | Py_ssize_t x; |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 5097 | if (_PyIndex_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5099 | if (x == -1 && _PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5100 | return 0; |
| 5101 | } |
| 5102 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5103 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5104 | "slice indices must be integers or " |
| 5105 | "None or have an __index__ method"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | return 0; |
| 5107 | } |
| 5108 | *pi = x; |
| 5109 | } |
| 5110 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5113 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5114 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5115 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5116 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5117 | Py_ssize_t x; |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 5118 | if (_PyIndex_Check(v)) { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5119 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5120 | if (x == -1 && _PyErr_Occurred(tstate)) |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5121 | return 0; |
| 5122 | } |
| 5123 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5124 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5125 | "slice indices must be integers or " |
| 5126 | "have an __index__ method"); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5127 | return 0; |
| 5128 | } |
| 5129 | *pi = x; |
| 5130 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 5131 | } |
| 5132 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5133 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5134 | import_name(PyThreadState *tstate, PyFrameObject *f, |
| 5135 | PyObject *name, PyObject *fromlist, PyObject *level) |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5136 | { |
| 5137 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5138 | PyObject *import_func, *res; |
| 5139 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5140 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5141 | import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5142 | if (import_func == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5143 | if (!_PyErr_Occurred(tstate)) { |
| 5144 | _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5145 | } |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5146 | return NULL; |
| 5147 | } |
| 5148 | |
| 5149 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5150 | if (import_func == tstate->interp->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5151 | int ilevel = _PyLong_AsInt(level); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5152 | if (ilevel == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5153 | return NULL; |
| 5154 | } |
| 5155 | res = PyImport_ImportModuleLevelObject( |
| 5156 | name, |
| 5157 | f->f_globals, |
| 5158 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 5159 | fromlist, |
| 5160 | ilevel); |
| 5161 | return res; |
| 5162 | } |
| 5163 | |
| 5164 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 5165 | |
| 5166 | stack[0] = name; |
| 5167 | stack[1] = f->f_globals; |
| 5168 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 5169 | stack[3] = fromlist; |
| 5170 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 5171 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 5172 | Py_DECREF(import_func); |
| 5173 | return res; |
| 5174 | } |
| 5175 | |
| 5176 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5177 | import_from(PyThreadState *tstate, PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | PyObject *x; |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5180 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5181 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5182 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5183 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5184 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5185 | /* Issue #17636: in case this failed because of a circular relative |
| 5186 | import, try to fallback on reading the module directly from |
| 5187 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5188 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5189 | if (pkgname == NULL) { |
| 5190 | goto error; |
| 5191 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 5192 | if (!PyUnicode_Check(pkgname)) { |
| 5193 | Py_CLEAR(pkgname); |
| 5194 | goto error; |
| 5195 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5196 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5197 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5198 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5199 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5200 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 5201 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 5202 | Py_DECREF(fullmodname); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5203 | if (x == NULL && !_PyErr_Occurred(tstate)) { |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5204 | goto error; |
| 5205 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5206 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5207 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5208 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5209 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5210 | if (pkgname == NULL) { |
| 5211 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 5212 | if (pkgname_or_unknown == NULL) { |
| 5213 | Py_XDECREF(pkgpath); |
| 5214 | return NULL; |
| 5215 | } |
| 5216 | } else { |
| 5217 | pkgname_or_unknown = pkgname; |
| 5218 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5219 | |
| 5220 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5221 | _PyErr_Clear(tstate); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5222 | errmsg = PyUnicode_FromFormat( |
| 5223 | "cannot import name %R from %R (unknown location)", |
| 5224 | name, pkgname_or_unknown |
| 5225 | ); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5226 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5227 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 5228 | } |
| 5229 | else { |
Anthony Sottile | 65366bc | 2019-09-09 08:17:50 -0700 | [diff] [blame] | 5230 | _Py_IDENTIFIER(__spec__); |
| 5231 | PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__); |
Anthony Sottile | 65366bc | 2019-09-09 08:17:50 -0700 | [diff] [blame] | 5232 | const char *fmt = |
| 5233 | _PyModuleSpec_IsInitializing(spec) ? |
| 5234 | "cannot import name %R from partially initialized module %R " |
| 5235 | "(most likely due to a circular import) (%S)" : |
| 5236 | "cannot import name %R from %R (%S)"; |
| 5237 | Py_XDECREF(spec); |
| 5238 | |
| 5239 | errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 5240 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5241 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 5242 | } |
| 5243 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 5244 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 5245 | Py_XDECREF(pkgname_or_unknown); |
| 5246 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 5247 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5248 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5249 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5250 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5251 | import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5252 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5253 | _Py_IDENTIFIER(__all__); |
| 5254 | _Py_IDENTIFIER(__dict__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5255 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5256 | int skip_leading_underscores = 0; |
| 5257 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 5258 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5259 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 5260 | return -1; /* Unexpected error */ |
| 5261 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5263 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 5264 | return -1; |
| 5265 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | if (dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5267 | _PyErr_SetString(tstate, PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5268 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5269 | return -1; |
| 5270 | } |
| 5271 | all = PyMapping_Keys(dict); |
| 5272 | Py_DECREF(dict); |
| 5273 | if (all == NULL) |
| 5274 | return -1; |
| 5275 | skip_leading_underscores = 1; |
| 5276 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 5277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | for (pos = 0, err = 0; ; pos++) { |
| 5279 | name = PySequence_GetItem(all, pos); |
| 5280 | if (name == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5281 | if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5282 | err = -1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5283 | } |
| 5284 | else { |
| 5285 | _PyErr_Clear(tstate); |
| 5286 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | break; |
| 5288 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5289 | if (!PyUnicode_Check(name)) { |
| 5290 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 5291 | if (modname == NULL) { |
| 5292 | Py_DECREF(name); |
| 5293 | err = -1; |
| 5294 | break; |
| 5295 | } |
| 5296 | if (!PyUnicode_Check(modname)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5297 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5298 | "module __name__ must be a string, not %.100s", |
| 5299 | Py_TYPE(modname)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5300 | } |
| 5301 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5302 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5303 | "%s in %U.%s must be str, not %.100s", |
| 5304 | skip_leading_underscores ? "Key" : "Item", |
| 5305 | modname, |
| 5306 | skip_leading_underscores ? "__dict__" : "__all__", |
| 5307 | Py_TYPE(name)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 5308 | } |
| 5309 | Py_DECREF(modname); |
| 5310 | Py_DECREF(name); |
| 5311 | err = -1; |
| 5312 | break; |
| 5313 | } |
| 5314 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 5315 | if (PyUnicode_READY(name) == -1) { |
| 5316 | Py_DECREF(name); |
| 5317 | err = -1; |
| 5318 | break; |
| 5319 | } |
| 5320 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 5321 | Py_DECREF(name); |
| 5322 | continue; |
| 5323 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | } |
| 5325 | value = PyObject_GetAttr(v, name); |
| 5326 | if (value == NULL) |
| 5327 | err = -1; |
| 5328 | else if (PyDict_CheckExact(locals)) |
| 5329 | err = PyDict_SetItem(locals, name, value); |
| 5330 | else |
| 5331 | err = PyObject_SetItem(locals, name, value); |
| 5332 | Py_DECREF(name); |
| 5333 | Py_XDECREF(value); |
| 5334 | if (err != 0) |
| 5335 | break; |
| 5336 | } |
| 5337 | Py_DECREF(all); |
| 5338 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5341 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5342 | check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5343 | { |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 5344 | if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) { |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5345 | /* check_args_iterable() may be called with a live exception: |
| 5346 | * clear it to prevent calling _PyObject_FunctionStr() with an |
| 5347 | * exception set. */ |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5348 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5349 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 5350 | if (funcstr != NULL) { |
| 5351 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5352 | "%U argument after * must be an iterable, not %.200s", |
| 5353 | funcstr, Py_TYPE(args)->tp_name); |
| 5354 | Py_DECREF(funcstr); |
| 5355 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5356 | return -1; |
| 5357 | } |
| 5358 | return 0; |
| 5359 | } |
| 5360 | |
| 5361 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5362 | format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5363 | { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5364 | /* _PyDict_MergeEx raises attribute |
| 5365 | * error (percolated from an attempt |
| 5366 | * to get 'keys' attribute) instead of |
| 5367 | * a type error if its second argument |
| 5368 | * is not a mapping. |
| 5369 | */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5370 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5371 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5372 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 5373 | if (funcstr != NULL) { |
| 5374 | _PyErr_Format( |
| 5375 | tstate, PyExc_TypeError, |
| 5376 | "%U argument after ** must be a mapping, not %.200s", |
| 5377 | funcstr, Py_TYPE(kwargs)->tp_name); |
| 5378 | Py_DECREF(funcstr); |
| 5379 | } |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5380 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5381 | else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5382 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5383 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5384 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5385 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 5386 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 5387 | if (funcstr != NULL) { |
| 5388 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 5389 | _PyErr_Format( |
| 5390 | tstate, PyExc_TypeError, |
| 5391 | "%U got multiple values for keyword argument '%S'", |
| 5392 | funcstr, key); |
| 5393 | Py_DECREF(funcstr); |
| 5394 | } |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5395 | Py_XDECREF(exc); |
| 5396 | Py_XDECREF(val); |
| 5397 | Py_XDECREF(tb); |
| 5398 | } |
| 5399 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5400 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5401 | } |
| 5402 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5403 | } |
| 5404 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5405 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5406 | format_exc_check_arg(PyThreadState *tstate, PyObject *exc, |
| 5407 | const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | if (!obj) |
| 5412 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5413 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5414 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | if (!obj_str) |
| 5416 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5417 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5418 | _PyErr_Format(tstate, exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5419 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5420 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5421 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5422 | format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5423 | { |
| 5424 | PyObject *name; |
| 5425 | /* Don't stomp existing exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5426 | if (_PyErr_Occurred(tstate)) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5427 | return; |
| 5428 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5429 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5430 | oparg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5431 | format_exc_check_arg(tstate, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5432 | PyExc_UnboundLocalError, |
| 5433 | UNBOUNDLOCAL_ERROR_MSG, |
| 5434 | name); |
| 5435 | } else { |
| 5436 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5437 | PyTuple_GET_SIZE(co->co_cellvars)); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5438 | format_exc_check_arg(tstate, PyExc_NameError, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5439 | UNBOUNDFREE_ERROR_MSG, name); |
| 5440 | } |
| 5441 | } |
| 5442 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5443 | static void |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5444 | format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode) |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5445 | { |
| 5446 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5447 | if (prevopcode == BEFORE_ASYNC_WITH) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5448 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5449 | "'async with' received an object from __aenter__ " |
| 5450 | "that does not implement __await__: %.100s", |
| 5451 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5452 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5453 | else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5454 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5455 | "'async with' received an object from __aexit__ " |
| 5456 | "that does not implement __await__: %.100s", |
| 5457 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5462 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5463 | unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5464 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5465 | { |
| 5466 | PyObject *res; |
| 5467 | if (Py_REFCNT(v) == 2) { |
| 5468 | /* In the common case, there are 2 references to the value |
| 5469 | * stored in 'variable' when the += is performed: one on the |
| 5470 | * value stack (in 'v') and one still stored in the |
| 5471 | * 'variable'. We try to delete the variable now to reduce |
| 5472 | * the refcnt to 1. |
| 5473 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5474 | int opcode, oparg; |
| 5475 | NEXTOPARG(); |
| 5476 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5477 | case STORE_FAST: |
| 5478 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5479 | PyObject **fastlocals = f->f_localsplus; |
| 5480 | if (GETLOCAL(oparg) == v) |
| 5481 | SETLOCAL(oparg, NULL); |
| 5482 | break; |
| 5483 | } |
| 5484 | case STORE_DEREF: |
| 5485 | { |
| 5486 | PyObject **freevars = (f->f_localsplus + |
| 5487 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5488 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5489 | if (PyCell_GET(c) == v) { |
| 5490 | PyCell_SET(c, NULL); |
| 5491 | Py_DECREF(v); |
| 5492 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5493 | break; |
| 5494 | } |
| 5495 | case STORE_NAME: |
| 5496 | { |
| 5497 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5498 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5499 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5500 | if (locals && PyDict_CheckExact(locals)) { |
| 5501 | PyObject *w = PyDict_GetItemWithError(locals, name); |
| 5502 | if ((w == v && PyDict_DelItem(locals, name) != 0) || |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5503 | (w == NULL && _PyErr_Occurred(tstate))) |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5504 | { |
| 5505 | Py_DECREF(v); |
| 5506 | return NULL; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5507 | } |
| 5508 | } |
| 5509 | break; |
| 5510 | } |
| 5511 | } |
| 5512 | } |
| 5513 | res = v; |
| 5514 | PyUnicode_Append(&res, w); |
| 5515 | return res; |
| 5516 | } |
| 5517 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5518 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5519 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5520 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5521 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5523 | int i; |
| 5524 | PyObject *l = PyList_New(256); |
| 5525 | if (l == NULL) return NULL; |
| 5526 | for (i = 0; i < 256; i++) { |
| 5527 | PyObject *x = PyLong_FromLong(a[i]); |
| 5528 | if (x == NULL) { |
| 5529 | Py_DECREF(l); |
| 5530 | return NULL; |
| 5531 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5532 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5533 | } |
| 5534 | for (i = 0; i < 256; i++) |
| 5535 | a[i] = 0; |
| 5536 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5540 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5541 | { |
| 5542 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5543 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5544 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | int i; |
| 5546 | PyObject *l = PyList_New(257); |
| 5547 | if (l == NULL) return NULL; |
| 5548 | for (i = 0; i < 257; i++) { |
| 5549 | PyObject *x = getarray(dxpairs[i]); |
| 5550 | if (x == NULL) { |
| 5551 | Py_DECREF(l); |
| 5552 | return NULL; |
| 5553 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5554 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | } |
| 5556 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5557 | #endif |
| 5558 | } |
| 5559 | |
| 5560 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5561 | |
| 5562 | Py_ssize_t |
| 5563 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5564 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 5565 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5566 | Py_ssize_t new_index; |
| 5567 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5568 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5569 | return -1; |
| 5570 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5571 | new_index = interp->co_extra_user_count++; |
| 5572 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5573 | return new_index; |
| 5574 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5575 | |
| 5576 | static void |
| 5577 | dtrace_function_entry(PyFrameObject *f) |
| 5578 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5579 | const char *filename; |
| 5580 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5581 | int lineno; |
| 5582 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 5583 | PyCodeObject *code = f->f_code; |
| 5584 | filename = PyUnicode_AsUTF8(code->co_filename); |
| 5585 | funcname = PyUnicode_AsUTF8(code->co_name); |
| 5586 | lineno = PyCode_Addr2Line(code, f->f_lasti); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5587 | |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 5588 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | static void |
| 5592 | dtrace_function_return(PyFrameObject *f) |
| 5593 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5594 | const char *filename; |
| 5595 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5596 | int lineno; |
| 5597 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 5598 | PyCodeObject *code = f->f_code; |
| 5599 | filename = PyUnicode_AsUTF8(code->co_filename); |
| 5600 | funcname = PyUnicode_AsUTF8(code->co_name); |
| 5601 | lineno = PyCode_Addr2Line(code, f->f_lasti); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5602 | |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 5603 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5607 | static void |
| 5608 | maybe_dtrace_line(PyFrameObject *frame, |
| 5609 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5610 | { |
| 5611 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5612 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5613 | |
| 5614 | /* If the last instruction executed isn't in the current |
| 5615 | instruction window, reset the window. |
| 5616 | */ |
| 5617 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5618 | PyAddrPair bounds; |
| 5619 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5620 | &bounds); |
| 5621 | *instr_lb = bounds.ap_lower; |
| 5622 | *instr_ub = bounds.ap_upper; |
| 5623 | } |
| 5624 | /* If the last instruction falls at the start of a line or if |
| 5625 | it represents a jump backwards, update the frame's line |
| 5626 | number and call the trace function. */ |
| 5627 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5628 | frame->f_lineno = line; |
| 5629 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5630 | if (!co_filename) |
| 5631 | co_filename = "?"; |
| 5632 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5633 | if (!co_name) |
| 5634 | co_name = "?"; |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 5635 | PyDTrace_LINE(co_filename, co_name, line); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5636 | } |
| 5637 | *instr_prev = frame->f_lasti; |
| 5638 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 5639 | |
| 5640 | |
| 5641 | /* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions |
| 5642 | for the limited API. */ |
| 5643 | |
| 5644 | #undef Py_EnterRecursiveCall |
| 5645 | |
| 5646 | int Py_EnterRecursiveCall(const char *where) |
| 5647 | { |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 5648 | return _Py_EnterRecursiveCall_inline(where); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | #undef Py_LeaveRecursiveCall |
| 5652 | |
| 5653 | void Py_LeaveRecursiveCall(void) |
| 5654 | { |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 5655 | _Py_LeaveRecursiveCall_inline(); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 5656 | } |