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 */ |
db3l | 131d551 | 2021-03-03 22:09:48 -0500 | [diff] [blame] | 10 | /* affects both release and debug builds - see bpo-43271 */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11 | #define PY_LOCAL_AGGRESSIVE |
| 12 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Victor Stinner | e560f90 | 2020-04-14 18:30:41 +0200 | [diff] [blame] | 14 | #include "pycore_abstract.h" // _PyIndex_Check() |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 15 | #include "pycore_call.h" // _PyObject_FastCallDictTstate() |
| 16 | #include "pycore_ceval.h" // _PyEval_SignalAsyncExc() |
| 17 | #include "pycore_code.h" // _PyCode_InitOpcache() |
| 18 | #include "pycore_initconfig.h" // _PyStatus_OK() |
| 19 | #include "pycore_object.h" // _PyObject_GC_TRACK() |
| 20 | #include "pycore_pyerrors.h" // _PyErr_Fetch() |
| 21 | #include "pycore_pylifecycle.h" // _PyErr_Print() |
Victor Stinner | e560f90 | 2020-04-14 18:30:41 +0200 | [diff] [blame] | 22 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
| 23 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 24 | #include "pycore_sysmodule.h" // _PySys_Audit() |
| 25 | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 26 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 27 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 28 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 29 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 30 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 31 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 32 | #include "setobject.h" |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 33 | #include "structmember.h" // struct PyMemberDef, T_OFFSET_EX |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 35 | #include <ctype.h> |
| 36 | |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 37 | typedef struct { |
| 38 | PyCodeObject *code; // The code object for the bounds. May be NULL. |
| 39 | int instr_prev; // Only valid if code != NULL. |
| 40 | PyCodeAddressRange bounds; // Only valid if code != NULL. |
| 41 | } PyTraceInfo; |
| 42 | |
| 43 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 44 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 45 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | #define LLTRACE 1 /* Low-level trace feature */ |
| 47 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 50 | #if !defined(Py_BUILD_CORE) |
| 51 | # error "ceval.c must be build with Py_BUILD_CORE define for best performance" |
| 52 | #endif |
| 53 | |
Hai Shi | 46874c2 | 2020-01-30 17:20:25 -0600 | [diff] [blame] | 54 | _Py_IDENTIFIER(__name__); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 56 | /* Forward declarations */ |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 57 | Py_LOCAL_INLINE(PyObject *) call_function( |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 58 | PyThreadState *tstate, PyTraceInfo *, PyObject ***pp_stack, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 59 | Py_ssize_t oparg, PyObject *kwnames); |
| 60 | static PyObject * do_call_core( |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 61 | PyThreadState *tstate, PyTraceInfo *, PyObject *func, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 62 | PyObject *callargs, PyObject *kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 64 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 65 | static int lltrace; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 66 | static int prtrace(PyThreadState *, PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 67 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 68 | static int call_trace(Py_tracefunc, PyObject *, |
| 69 | PyThreadState *, PyFrameObject *, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 70 | PyTraceInfo *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 72 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 73 | PyThreadState *, PyFrameObject *, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 74 | PyTraceInfo *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 75 | int, PyObject *); |
| 76 | static void call_exc_trace(Py_tracefunc, PyObject *, |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 77 | PyThreadState *, PyFrameObject *, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 78 | PyTraceInfo *trace_info); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 79 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 80 | PyThreadState *, PyFrameObject *, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 81 | PyTraceInfo *); |
| 82 | static void maybe_dtrace_line(PyFrameObject *, PyTraceInfo *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 83 | static void dtrace_function_entry(PyFrameObject *); |
| 84 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 85 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 86 | static PyObject * import_name(PyThreadState *, PyFrameObject *, |
| 87 | PyObject *, PyObject *, PyObject *); |
| 88 | static PyObject * import_from(PyThreadState *, PyObject *, PyObject *); |
| 89 | static int import_all_from(PyThreadState *, PyObject *, PyObject *); |
| 90 | static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *); |
| 91 | static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg); |
| 92 | static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 93 | PyFrameObject *, const _Py_CODEUNIT *); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 94 | static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *); |
| 95 | static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg); |
| 96 | static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 97 | static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 98 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 99 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 101 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 103 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | "free variable '%.200s' referenced before assignment" \ |
| 105 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 107 | /* Dynamic execution profile */ |
| 108 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 109 | #ifdef DXPAIRS |
| 110 | static long dxpairs[257][256]; |
| 111 | #define dxp dxpairs[256] |
| 112 | #else |
| 113 | static long dxp[256]; |
| 114 | #endif |
| 115 | #endif |
| 116 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 117 | /* per opcode cache */ |
Pablo Galindo | af5fa13 | 2021-02-28 22:41:09 +0000 | [diff] [blame] | 118 | static int opcache_min_runs = 1024; /* create opcache when code executed this many times */ |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 119 | #define OPCODE_CACHE_MAX_TRIES 20 |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 120 | #define OPCACHE_STATS 0 /* Enable stats */ |
| 121 | |
Pablo Galindo | af5fa13 | 2021-02-28 22:41:09 +0000 | [diff] [blame] | 122 | // This function allows to deactivate the opcode cache. As different cache mechanisms may hold |
| 123 | // references, this can mess with the reference leak detector functionality so the cache needs |
| 124 | // to be deactivated in such scenarios to avoid false positives. See bpo-3714 for more information. |
| 125 | void |
| 126 | _PyEval_DeactivateOpCache(void) |
| 127 | { |
| 128 | opcache_min_runs = 0; |
| 129 | } |
| 130 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 131 | #if OPCACHE_STATS |
| 132 | static size_t opcache_code_objects = 0; |
| 133 | static size_t opcache_code_objects_extra_mem = 0; |
| 134 | |
| 135 | static size_t opcache_global_opts = 0; |
| 136 | static size_t opcache_global_hits = 0; |
| 137 | static size_t opcache_global_misses = 0; |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 138 | |
| 139 | static size_t opcache_attr_opts = 0; |
| 140 | static size_t opcache_attr_hits = 0; |
| 141 | static size_t opcache_attr_misses = 0; |
| 142 | static size_t opcache_attr_deopts = 0; |
| 143 | static size_t opcache_attr_total = 0; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 144 | #endif |
| 145 | |
Victor Stinner | 5a3a71d | 2020-03-19 17:40:12 +0100 | [diff] [blame] | 146 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 147 | #ifndef NDEBUG |
| 148 | /* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and |
| 149 | PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen |
| 150 | when a thread continues to run after Python finalization, especially |
| 151 | daemon threads. */ |
| 152 | static int |
| 153 | is_tstate_valid(PyThreadState *tstate) |
| 154 | { |
| 155 | assert(!_PyMem_IsPtrFreed(tstate)); |
| 156 | assert(!_PyMem_IsPtrFreed(tstate->interp)); |
| 157 | return 1; |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 162 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 163 | 1. We believe this is all right because the eval loop will release |
| 164 | the GIL eventually anyway. */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 165 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 166 | COMPUTE_EVAL_BREAKER(PyInterpreterState *interp, |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 167 | struct _ceval_runtime_state *ceval, |
| 168 | struct _ceval_state *ceval2) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 169 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 170 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, |
| 171 | _Py_atomic_load_relaxed(&ceval2->gil_drop_request) |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 172 | | (_Py_atomic_load_relaxed(&ceval->signals_pending) |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 173 | && _Py_ThreadCanHandleSignals(interp)) |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 174 | | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do) |
Victor Stinner | d831688 | 2020-03-20 14:50:35 +0100 | [diff] [blame] | 175 | && _Py_ThreadCanHandlePendingCalls()) |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 176 | | ceval2->pending.async_exc); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 177 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 178 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 179 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 180 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 181 | SET_GIL_DROP_REQUEST(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 182 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 183 | struct _ceval_state *ceval2 = &interp->ceval; |
| 184 | _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1); |
| 185 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 186 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [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 | RESET_GIL_DROP_REQUEST(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 191 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 192 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 193 | struct _ceval_state *ceval2 = &interp->ceval; |
| 194 | _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0); |
| 195 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 196 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 197 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 198 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 199 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 200 | SIGNAL_PENDING_CALLS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 201 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 202 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 203 | struct _ceval_state *ceval2 = &interp->ceval; |
| 204 | _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1); |
| 205 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 206 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 207 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 208 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 209 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 210 | UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 211 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 212 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 213 | struct _ceval_state *ceval2 = &interp->ceval; |
| 214 | _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0); |
| 215 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | static inline void |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 220 | SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 221 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 222 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 223 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 224 | _Py_atomic_store_relaxed(&ceval->signals_pending, 1); |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 225 | if (force) { |
| 226 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
| 227 | } |
| 228 | else { |
| 229 | /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */ |
| 230 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
| 231 | } |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
| 235 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 236 | UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 237 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 238 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 239 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 240 | _Py_atomic_store_relaxed(&ceval->signals_pending, 0); |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 241 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
| 245 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 246 | SIGNAL_ASYNC_EXC(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 247 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 248 | struct _ceval_state *ceval2 = &interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 249 | ceval2->pending.async_exc = 1; |
| 250 | _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | static inline void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 255 | UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp) |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 256 | { |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 257 | struct _ceval_runtime_state *ceval = &interp->runtime->ceval; |
| 258 | struct _ceval_state *ceval2 = &interp->ceval; |
| 259 | ceval2->pending.async_exc = 0; |
| 260 | COMPUTE_EVAL_BREAKER(interp, ceval, ceval2); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 261 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 262 | |
| 263 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 265 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 266 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 267 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 268 | |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 269 | void _Py_NO_RETURN |
| 270 | _Py_FatalError_TstateNULL(const char *func) |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 271 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 272 | _Py_FatalErrorFunc(func, |
| 273 | "the function must be called with the GIL held, " |
| 274 | "but the GIL is released " |
| 275 | "(the current Python thread state is NULL)"); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 278 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 279 | int |
| 280 | _PyEval_ThreadsInitialized(PyInterpreterState *interp) |
| 281 | { |
| 282 | return gil_created(&interp->ceval.gil); |
| 283 | } |
| 284 | |
| 285 | int |
| 286 | PyEval_ThreadsInitialized(void) |
| 287 | { |
| 288 | // Fatal error if there is no current interpreter |
| 289 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 290 | return _PyEval_ThreadsInitialized(interp); |
| 291 | } |
| 292 | #else |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 293 | int |
Victor Stinner | 175a704 | 2020-03-10 00:37:48 +0100 | [diff] [blame] | 294 | _PyEval_ThreadsInitialized(_PyRuntimeState *runtime) |
| 295 | { |
| 296 | return gil_created(&runtime->ceval.gil); |
| 297 | } |
| 298 | |
| 299 | int |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 300 | PyEval_ThreadsInitialized(void) |
| 301 | { |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 302 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 175a704 | 2020-03-10 00:37:48 +0100 | [diff] [blame] | 303 | return _PyEval_ThreadsInitialized(runtime); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 304 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 305 | #endif |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 306 | |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 307 | PyStatus |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 308 | _PyEval_InitGIL(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 309 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 310 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | 101bf69 | 2021-02-19 13:33:31 +0100 | [diff] [blame] | 311 | if (!_Py_IsMainInterpreter(tstate->interp)) { |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 312 | /* Currently, the GIL is shared by all interpreters, |
| 313 | and only the main interpreter is responsible to create |
| 314 | and destroy it. */ |
| 315 | return _PyStatus_OK(); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 316 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 317 | #endif |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 318 | |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 319 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 320 | struct _gil_runtime_state *gil = &tstate->interp->ceval.gil; |
| 321 | #else |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 322 | struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 323 | #endif |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 324 | assert(!gil_created(gil)); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 325 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 326 | PyThread_init_thread(); |
| 327 | create_gil(gil); |
| 328 | |
| 329 | take_gil(tstate); |
| 330 | |
| 331 | assert(gil_created(gil)); |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 332 | return _PyStatus_OK(); |
| 333 | } |
| 334 | |
| 335 | void |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 336 | _PyEval_FiniGIL(PyInterpreterState *interp) |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 337 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 338 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 339 | if (!_Py_IsMainInterpreter(interp)) { |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 340 | /* Currently, the GIL is shared by all interpreters, |
| 341 | and only the main interpreter is responsible to create |
| 342 | and destroy it. */ |
| 343 | return; |
| 344 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 345 | #endif |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 346 | |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 347 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 348 | struct _gil_runtime_state *gil = &interp->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 349 | #else |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 350 | struct _gil_runtime_state *gil = &interp->runtime->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 351 | #endif |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 352 | if (!gil_created(gil)) { |
| 353 | /* First Py_InitializeFromConfig() call: the GIL doesn't exist |
| 354 | yet: do nothing. */ |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | destroy_gil(gil); |
| 359 | assert(!gil_created(gil)); |
| 360 | } |
| 361 | |
| 362 | void |
Victor Stinner | 111e4ee | 2020-03-09 21:24:14 +0100 | [diff] [blame] | 363 | PyEval_InitThreads(void) |
| 364 | { |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 365 | /* Do nothing: kept for backward compatibility */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 366 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 368 | void |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 369 | _PyEval_Fini(void) |
| 370 | { |
| 371 | #if OPCACHE_STATS |
| 372 | fprintf(stderr, "-- Opcode cache number of objects = %zd\n", |
| 373 | opcache_code_objects); |
| 374 | |
| 375 | fprintf(stderr, "-- Opcode cache total extra mem = %zd\n", |
| 376 | opcache_code_objects_extra_mem); |
| 377 | |
| 378 | fprintf(stderr, "\n"); |
| 379 | |
| 380 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n", |
| 381 | opcache_global_hits, |
| 382 | (int) (100.0 * opcache_global_hits / |
| 383 | (opcache_global_hits + opcache_global_misses))); |
| 384 | |
| 385 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n", |
| 386 | opcache_global_misses, |
| 387 | (int) (100.0 * opcache_global_misses / |
| 388 | (opcache_global_hits + opcache_global_misses))); |
| 389 | |
| 390 | fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n", |
| 391 | opcache_global_opts); |
| 392 | |
| 393 | fprintf(stderr, "\n"); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 394 | |
| 395 | fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n", |
| 396 | opcache_attr_hits, |
| 397 | (int) (100.0 * opcache_attr_hits / |
| 398 | opcache_attr_total)); |
| 399 | |
| 400 | fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n", |
| 401 | opcache_attr_misses, |
| 402 | (int) (100.0 * opcache_attr_misses / |
| 403 | opcache_attr_total)); |
| 404 | |
| 405 | fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n", |
| 406 | opcache_attr_opts); |
| 407 | |
| 408 | fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n", |
| 409 | opcache_attr_deopts); |
| 410 | |
| 411 | fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n", |
| 412 | opcache_attr_total); |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 413 | #endif |
| 414 | } |
| 415 | |
| 416 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 417 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 418 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 419 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 420 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 421 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 422 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 423 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 427 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 428 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 429 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 430 | PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 432 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 433 | in debug mode. */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 434 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 435 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 436 | drop_gil(ceval, ceval2, tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 440 | _PyEval_ReleaseLock(PyThreadState *tstate) |
| 441 | { |
| 442 | struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval; |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 443 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 444 | drop_gil(ceval, ceval2, tstate); |
Victor Stinner | 23ef89d | 2020-03-18 02:26:04 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 448 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 449 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 450 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 451 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 452 | take_gil(tstate); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 453 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 454 | struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 455 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 456 | (void)_PyThreadState_Swap(gilstate, tstate); |
| 457 | #else |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 458 | if (_PyThreadState_Swap(gilstate, tstate) != NULL) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 459 | Py_FatalError("non-NULL old thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 460 | } |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 461 | #endif |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 465 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 466 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 467 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 468 | |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 469 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 470 | PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
| 471 | if (new_tstate != tstate) { |
Victor Stinner | 9e5d30c | 2020-03-07 00:54:20 +0100 | [diff] [blame] | 472 | Py_FatalError("wrong thread state"); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 473 | } |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 474 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 475 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
| 476 | drop_gil(ceval, ceval2, tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 477 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 478 | |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 479 | #ifdef HAVE_FORK |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 480 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 481 | which are not running in the child process, and clear internal locks |
| 482 | which might be held by those threads. */ |
| 483 | PyStatus |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 484 | _PyEval_ReInitThreads(PyThreadState *tstate) |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 485 | { |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 486 | _PyRuntimeState *runtime = tstate->interp->runtime; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 487 | |
| 488 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 489 | struct _gil_runtime_state *gil = &tstate->interp->ceval.gil; |
| 490 | #else |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 491 | struct _gil_runtime_state *gil = &runtime->ceval.gil; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 492 | #endif |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 493 | if (!gil_created(gil)) { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 494 | return _PyStatus_OK(); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 495 | } |
Victor Stinner | 56bfdeb | 2020-03-18 09:26:25 +0100 | [diff] [blame] | 496 | recreate_gil(gil); |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 497 | |
| 498 | take_gil(tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 499 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 500 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 501 | if (_PyThread_at_fork_reinit(&pending->lock) < 0) { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 502 | return _PyStatus_ERR("Can't reinitialize pending calls lock"); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 503 | } |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 505 | /* Destroy all threads except the current one */ |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 506 | _PyThreadState_DeleteExcept(runtime, tstate); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 507 | return _PyStatus_OK(); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 508 | } |
Dong-hee Na | 62f75fe | 2020-04-15 01:16:24 +0900 | [diff] [blame] | 509 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 511 | /* 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] | 512 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 513 | |
| 514 | void |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 515 | _PyEval_SignalAsyncExc(PyInterpreterState *interp) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 516 | { |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 517 | SIGNAL_ASYNC_EXC(interp); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 518 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 519 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 520 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 521 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 522 | { |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 523 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 524 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 525 | PyThreadState *old_tstate = _PyThreadState_GET(); |
| 526 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate); |
| 527 | #else |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 528 | PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 529 | #endif |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 530 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 531 | |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 532 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
| 533 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 534 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 535 | assert(gil_created(&ceval2->gil)); |
| 536 | #else |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 537 | assert(gil_created(&ceval->gil)); |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 538 | #endif |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 539 | drop_gil(ceval, ceval2, tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 544 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 545 | { |
Victor Stinner | 3026cad | 2020-06-01 16:02:40 +0200 | [diff] [blame] | 546 | _Py_EnsureTstateNotNULL(tstate); |
Victor Stinner | eb4e2ae | 2020-03-08 11:57:45 +0100 | [diff] [blame] | 547 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 548 | take_gil(tstate); |
Victor Stinner | 17c68b8 | 2020-01-30 12:20:48 +0100 | [diff] [blame] | 549 | |
Victor Stinner | 85f5a69 | 2020-03-09 22:12:04 +0100 | [diff] [blame] | 550 | struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; |
| 551 | _PyThreadState_Swap(gilstate, tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 555 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 556 | signal handlers or Mac I/O completion routines) can schedule calls |
| 557 | to a function to be called synchronously. |
| 558 | The synchronous function is called with one void* argument. |
| 559 | It should return 0 for success or -1 for failure -- failure should |
| 560 | be accompanied by an exception. |
| 561 | |
| 562 | If registry succeeds, the registry function returns 0; if it fails |
| 563 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 564 | an exception condition). |
| 565 | |
| 566 | Note that because registry may occur from within signal handlers, |
| 567 | or other asynchronous events, calling malloc() is unsafe! |
| 568 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 569 | Any thread can schedule pending calls, but only the main thread |
| 570 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 571 | There is no facility to schedule calls to a particular thread, but |
| 572 | that should be easy to change, should that ever be required. In |
| 573 | that case, the static variables here should go into the python |
| 574 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 575 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 577 | void |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 578 | _PyEval_SignalReceived(PyInterpreterState *interp) |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 579 | { |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 580 | #ifdef MS_WINDOWS |
| 581 | // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal |
| 582 | // handler which can run in a thread different than the Python thread, in |
| 583 | // which case _Py_ThreadCanHandleSignals() is wrong. Ignore |
| 584 | // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1. |
| 585 | // |
| 586 | // The next eval_frame_handle_pending() call will call |
| 587 | // _Py_ThreadCanHandleSignals() to recompute eval_breaker. |
| 588 | int force = 1; |
| 589 | #else |
| 590 | int force = 0; |
| 591 | #endif |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 592 | /* 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] | 593 | signal. We cannot queue a callback using _PyEval_AddPendingCall() since |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 594 | that function is not async-signal-safe. */ |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 595 | SIGNAL_PENDING_SIGNALS(interp, force); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 598 | /* Push one item onto the queue while holding the lock. */ |
| 599 | static int |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 600 | _push_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 601 | int (*func)(void *), void *arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 602 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 603 | int i = pending->last; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 604 | int j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 605 | if (j == pending->first) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 606 | return -1; /* Queue full */ |
| 607 | } |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 608 | pending->calls[i].func = func; |
| 609 | pending->calls[i].arg = arg; |
| 610 | pending->last = j; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* Pop one item off the queue while holding the lock. */ |
| 615 | static void |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 616 | _pop_pending_call(struct _pending_calls *pending, |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 617 | int (**func)(void *), void **arg) |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 618 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 619 | int i = pending->first; |
| 620 | if (i == pending->last) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 621 | return; /* Queue empty */ |
| 622 | } |
| 623 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 624 | *func = pending->calls[i].func; |
| 625 | *arg = pending->calls[i].arg; |
| 626 | pending->first = (i + 1) % NPENDINGCALLS; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 629 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 630 | scheduling to be made from any thread, and even from an executing |
| 631 | callback. |
| 632 | */ |
| 633 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 634 | int |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 635 | _PyEval_AddPendingCall(PyInterpreterState *interp, |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 636 | int (*func)(void *), void *arg) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 637 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 638 | struct _pending_calls *pending = &interp->ceval.pending; |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 639 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 640 | /* Ensure that _PyEval_InitPendingCalls() was called |
| 641 | and that _PyEval_FiniPendingCalls() is not called yet. */ |
| 642 | assert(pending->lock != NULL); |
| 643 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 644 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 645 | int result = _push_pending_call(pending, func, arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 646 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 647 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 648 | /* signal main loop */ |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 649 | SIGNAL_PENDING_CALLS(interp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 653 | int |
| 654 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 655 | { |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 656 | /* Best-effort to support subinterpreters and calls with the GIL released. |
| 657 | |
| 658 | First attempt _PyThreadState_GET() since it supports subinterpreters. |
| 659 | |
| 660 | If the GIL is released, _PyThreadState_GET() returns NULL . In this |
| 661 | case, use PyGILState_GetThisThreadState() which works even if the GIL |
| 662 | is released. |
| 663 | |
| 664 | Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters: |
| 665 | see bpo-10915 and bpo-15751. |
| 666 | |
Victor Stinner | 8849e59 | 2020-03-18 19:28:53 +0100 | [diff] [blame] | 667 | Py_AddPendingCall() doesn't require the caller to hold the GIL. */ |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 668 | PyThreadState *tstate = _PyThreadState_GET(); |
| 669 | if (tstate == NULL) { |
| 670 | tstate = PyGILState_GetThisThreadState(); |
| 671 | } |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 672 | |
| 673 | PyInterpreterState *interp; |
| 674 | if (tstate != NULL) { |
| 675 | interp = tstate->interp; |
| 676 | } |
| 677 | else { |
| 678 | /* Last resort: use the main interpreter */ |
| 679 | interp = _PyRuntime.interpreters.main; |
| 680 | } |
| 681 | return _PyEval_AddPendingCall(interp, func, arg); |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 684 | static int |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 685 | handle_signals(PyThreadState *tstate) |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 686 | { |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 687 | assert(is_tstate_valid(tstate)); |
| 688 | if (!_Py_ThreadCanHandleSignals(tstate->interp)) { |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 689 | return 0; |
| 690 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 691 | |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 692 | UNSIGNAL_PENDING_SIGNALS(tstate->interp); |
Victor Stinner | 7281898 | 2020-03-26 22:28:11 +0100 | [diff] [blame] | 693 | if (_PyErr_CheckSignalsTstate(tstate) < 0) { |
| 694 | /* On failure, re-schedule a call to handle_signals(). */ |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 695 | SIGNAL_PENDING_SIGNALS(tstate->interp, 0); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 696 | return -1; |
| 697 | } |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static int |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 702 | make_pending_calls(PyInterpreterState *interp) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 703 | { |
Victor Stinner | d831688 | 2020-03-20 14:50:35 +0100 | [diff] [blame] | 704 | /* only execute pending calls on main thread */ |
| 705 | if (!_Py_ThreadCanHandlePendingCalls()) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | /* don't perform recursive pending calls */ |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 710 | static int busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 711 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 713 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 714 | busy = 1; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 715 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 716 | /* unsignal before starting to call callbacks, so that any callback |
| 717 | added in-between re-signals */ |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 718 | UNSIGNAL_PENDING_CALLS(interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 719 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | /* perform a bounded number of calls, in case of recursion */ |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 722 | struct _pending_calls *pending = &interp->ceval.pending; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 723 | for (int i=0; i<NPENDINGCALLS; i++) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 724 | int (*func)(void *) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | void *arg = NULL; |
| 726 | |
| 727 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 728 | PyThread_acquire_lock(pending->lock, WAIT_LOCK); |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 729 | _pop_pending_call(pending, &func, &arg); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 730 | PyThread_release_lock(pending->lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 731 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 732 | /* having released the lock, perform the callback */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 733 | if (func == NULL) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 734 | break; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 735 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 736 | res = func(arg); |
| 737 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 738 | goto error; |
| 739 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 741 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 742 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 743 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 744 | |
| 745 | error: |
| 746 | busy = 0; |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 747 | SIGNAL_PENDING_CALLS(interp); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 748 | return res; |
| 749 | } |
| 750 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 751 | void |
Victor Stinner | 2b1df45 | 2020-01-13 18:46:59 +0100 | [diff] [blame] | 752 | _Py_FinishPendingCalls(PyThreadState *tstate) |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 753 | { |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 754 | assert(PyGILState_Check()); |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 755 | assert(is_tstate_valid(tstate)); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 756 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 757 | struct _pending_calls *pending = &tstate->interp->ceval.pending; |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 758 | |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 759 | if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) { |
| 760 | return; |
| 761 | } |
| 762 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 763 | if (make_pending_calls(tstate->interp) < 0) { |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 764 | PyObject *exc, *val, *tb; |
| 765 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 766 | PyErr_BadInternalCall(); |
| 767 | _PyErr_ChainExceptions(exc, val, tb); |
| 768 | _PyErr_Print(tstate); |
Eric Snow | 842a2f0 | 2019-03-15 15:47:51 -0600 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 772 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 773 | of backward-compatibility. */ |
| 774 | int |
| 775 | Py_MakePendingCalls(void) |
| 776 | { |
| 777 | assert(PyGILState_Check()); |
| 778 | |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 779 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 780 | assert(is_tstate_valid(tstate)); |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 781 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 782 | /* Python signal handler doesn't really queue a callback: it only signals |
| 783 | that a signal was received, see _PyEval_SignalReceived(). */ |
Victor Stinner | d7fabc1 | 2020-03-18 01:56:21 +0100 | [diff] [blame] | 784 | int res = handle_signals(tstate); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 785 | if (res != 0) { |
| 786 | return res; |
| 787 | } |
| 788 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 789 | res = make_pending_calls(tstate->interp); |
Eric Snow | b75b1a35 | 2019-04-12 10:20:10 -0600 | [diff] [blame] | 790 | if (res != 0) { |
| 791 | return res; |
| 792 | } |
| 793 | |
| 794 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 797 | /* The interpreter's recursion limit */ |
| 798 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 799 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
Victor Stinner | 19c3ac9 | 2020-09-23 14:04:57 +0200 | [diff] [blame] | 800 | # define Py_DEFAULT_RECURSION_LIMIT 1000 |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 801 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 802 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 803 | void |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 804 | _PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 805 | { |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 806 | #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 807 | _gil_initialize(&ceval->gil); |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 808 | #endif |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 811 | int |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 812 | _PyEval_InitState(struct _ceval_state *ceval) |
| 813 | { |
Victor Stinner | 4e30ed3 | 2020-05-05 16:52:52 +0200 | [diff] [blame] | 814 | ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 815 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 816 | struct _pending_calls *pending = &ceval->pending; |
| 817 | assert(pending->lock == NULL); |
| 818 | |
| 819 | pending->lock = PyThread_allocate_lock(); |
| 820 | if (pending->lock == NULL) { |
| 821 | return -1; |
| 822 | } |
Victor Stinner | 7be4e35 | 2020-05-05 20:27:47 +0200 | [diff] [blame] | 823 | |
| 824 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 825 | _gil_initialize(&ceval->gil); |
| 826 | #endif |
| 827 | |
Victor Stinner | dda5d6e | 2020-04-08 17:54:59 +0200 | [diff] [blame] | 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | void |
| 832 | _PyEval_FiniState(struct _ceval_state *ceval) |
| 833 | { |
| 834 | struct _pending_calls *pending = &ceval->pending; |
| 835 | if (pending->lock != NULL) { |
| 836 | PyThread_free_lock(pending->lock); |
| 837 | pending->lock = NULL; |
| 838 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 839 | } |
| 840 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 841 | int |
| 842 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 843 | { |
Victor Stinner | 1bcc32f | 2020-06-10 20:08:26 +0200 | [diff] [blame] | 844 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 845 | return interp->ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 848 | void |
| 849 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 850 | { |
Victor Stinner | 4e30ed3 | 2020-05-05 16:52:52 +0200 | [diff] [blame] | 851 | PyThreadState *tstate = _PyThreadState_GET(); |
| 852 | tstate->interp->ceval.recursion_limit = new_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 855 | /* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
Victor Stinner | 19c3ac9 | 2020-09-23 14:04:57 +0200 | [diff] [blame] | 856 | if the recursion_depth reaches recursion_limit. |
| 857 | If USE_STACKCHECK, the macro decrements recursion_limit |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 858 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 859 | Without USE_STACKCHECK, there is no need for this. */ |
| 860 | int |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 861 | _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 862 | { |
Victor Stinner | 4e30ed3 | 2020-05-05 16:52:52 +0200 | [diff] [blame] | 863 | int recursion_limit = tstate->interp->ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 864 | |
| 865 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 866 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | if (PyOS_CheckStack()) { |
| 868 | --tstate->recursion_depth; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 869 | _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | return -1; |
| 871 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 872 | #endif |
Mark Shannon | 4e7a69b | 2020-12-02 13:30:55 +0000 | [diff] [blame] | 873 | if (tstate->recursion_headroom) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 875 | /* Overflowing while handling an overflow. Give up. */ |
| 876 | Py_FatalError("Cannot recover from stack overflow."); |
| 877 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | } |
Mark Shannon | 4e7a69b | 2020-12-02 13:30:55 +0000 | [diff] [blame] | 879 | else { |
| 880 | if (tstate->recursion_depth > recursion_limit) { |
| 881 | tstate->recursion_headroom++; |
| 882 | _PyErr_Format(tstate, PyExc_RecursionError, |
| 883 | "maximum recursion depth exceeded%s", |
| 884 | where); |
| 885 | tstate->recursion_headroom--; |
| 886 | --tstate->recursion_depth; |
| 887 | return -1; |
| 888 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | } |
| 890 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 893 | |
| 894 | // PEP 634: Structural Pattern Matching |
| 895 | |
| 896 | |
| 897 | // Return a tuple of values corresponding to keys, with error checks for |
| 898 | // duplicate/missing keys. |
| 899 | static PyObject* |
| 900 | match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys) |
| 901 | { |
| 902 | assert(PyTuple_CheckExact(keys)); |
| 903 | Py_ssize_t nkeys = PyTuple_GET_SIZE(keys); |
| 904 | if (!nkeys) { |
| 905 | // No keys means no items. |
| 906 | return PyTuple_New(0); |
| 907 | } |
| 908 | PyObject *seen = NULL; |
| 909 | PyObject *dummy = NULL; |
| 910 | PyObject *values = NULL; |
| 911 | // We use the two argument form of map.get(key, default) for two reasons: |
| 912 | // - Atomically check for a key and get its value without error handling. |
| 913 | // - Don't cause key creation or resizing in dict subclasses like |
| 914 | // collections.defaultdict that define __missing__ (or similar). |
| 915 | _Py_IDENTIFIER(get); |
| 916 | PyObject *get = _PyObject_GetAttrId(map, &PyId_get); |
| 917 | if (get == NULL) { |
| 918 | goto fail; |
| 919 | } |
| 920 | seen = PySet_New(NULL); |
| 921 | if (seen == NULL) { |
| 922 | goto fail; |
| 923 | } |
| 924 | // dummy = object() |
| 925 | dummy = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); |
| 926 | if (dummy == NULL) { |
| 927 | goto fail; |
| 928 | } |
| 929 | values = PyList_New(0); |
| 930 | if (values == NULL) { |
| 931 | goto fail; |
| 932 | } |
| 933 | for (Py_ssize_t i = 0; i < nkeys; i++) { |
| 934 | PyObject *key = PyTuple_GET_ITEM(keys, i); |
| 935 | if (PySet_Contains(seen, key) || PySet_Add(seen, key)) { |
| 936 | if (!_PyErr_Occurred(tstate)) { |
| 937 | // Seen it before! |
| 938 | _PyErr_Format(tstate, PyExc_ValueError, |
| 939 | "mapping pattern checks duplicate key (%R)", key); |
| 940 | } |
| 941 | goto fail; |
| 942 | } |
| 943 | PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL); |
| 944 | if (value == NULL) { |
| 945 | goto fail; |
| 946 | } |
| 947 | if (value == dummy) { |
| 948 | // key not in map! |
| 949 | Py_DECREF(value); |
| 950 | Py_DECREF(values); |
| 951 | // Return None: |
| 952 | Py_INCREF(Py_None); |
| 953 | values = Py_None; |
| 954 | goto done; |
| 955 | } |
| 956 | PyList_Append(values, value); |
| 957 | Py_DECREF(value); |
| 958 | } |
| 959 | Py_SETREF(values, PyList_AsTuple(values)); |
| 960 | // Success: |
| 961 | done: |
| 962 | Py_DECREF(get); |
| 963 | Py_DECREF(seen); |
| 964 | Py_DECREF(dummy); |
| 965 | return values; |
| 966 | fail: |
| 967 | Py_XDECREF(get); |
| 968 | Py_XDECREF(seen); |
| 969 | Py_XDECREF(dummy); |
| 970 | Py_XDECREF(values); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | // Extract a named attribute from the subject, with additional bookkeeping to |
| 975 | // raise TypeErrors for repeated lookups. On failure, return NULL (with no |
| 976 | // error set). Use _PyErr_Occurred(tstate) to disambiguate. |
| 977 | static PyObject* |
| 978 | match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type, |
| 979 | PyObject *name, PyObject *seen) |
| 980 | { |
| 981 | assert(PyUnicode_CheckExact(name)); |
| 982 | assert(PySet_CheckExact(seen)); |
| 983 | if (PySet_Contains(seen, name) || PySet_Add(seen, name)) { |
| 984 | if (!_PyErr_Occurred(tstate)) { |
| 985 | // Seen it before! |
| 986 | _PyErr_Format(tstate, PyExc_TypeError, |
| 987 | "%s() got multiple sub-patterns for attribute %R", |
| 988 | ((PyTypeObject*)type)->tp_name, name); |
| 989 | } |
| 990 | return NULL; |
| 991 | } |
| 992 | PyObject *attr = PyObject_GetAttr(subject, name); |
| 993 | if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 994 | _PyErr_Clear(tstate); |
| 995 | } |
| 996 | return attr; |
| 997 | } |
| 998 | |
| 999 | // On success (match), return a tuple of extracted attributes. On failure (no |
| 1000 | // match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate. |
| 1001 | static PyObject* |
| 1002 | match_class(PyThreadState *tstate, PyObject *subject, PyObject *type, |
| 1003 | Py_ssize_t nargs, PyObject *kwargs) |
| 1004 | { |
| 1005 | if (!PyType_Check(type)) { |
| 1006 | const char *e = "called match pattern must be a type"; |
| 1007 | _PyErr_Format(tstate, PyExc_TypeError, e); |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | assert(PyTuple_CheckExact(kwargs)); |
| 1011 | // First, an isinstance check: |
| 1012 | if (PyObject_IsInstance(subject, type) <= 0) { |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | // So far so good: |
| 1016 | PyObject *seen = PySet_New(NULL); |
| 1017 | if (seen == NULL) { |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | PyObject *attrs = PyList_New(0); |
| 1021 | if (attrs == NULL) { |
| 1022 | Py_DECREF(seen); |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | // NOTE: From this point on, goto fail on failure: |
| 1026 | PyObject *match_args = NULL; |
| 1027 | // First, the positional subpatterns: |
| 1028 | if (nargs) { |
| 1029 | int match_self = 0; |
| 1030 | match_args = PyObject_GetAttrString(type, "__match_args__"); |
| 1031 | if (match_args) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1032 | if (!PyTuple_CheckExact(match_args)) { |
Brandt Bucher | f84d5a1 | 2021-04-05 19:17:08 -0700 | [diff] [blame] | 1033 | const char *e = "%s.__match_args__ must be a tuple (got %s)"; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1034 | _PyErr_Format(tstate, PyExc_TypeError, e, |
| 1035 | ((PyTypeObject *)type)->tp_name, |
| 1036 | Py_TYPE(match_args)->tp_name); |
| 1037 | goto fail; |
| 1038 | } |
| 1039 | } |
| 1040 | else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 1041 | _PyErr_Clear(tstate); |
| 1042 | // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not |
| 1043 | // define __match_args__. This is natural behavior for subclasses: |
| 1044 | // it's as if __match_args__ is some "magic" value that is lost as |
| 1045 | // soon as they redefine it. |
| 1046 | match_args = PyTuple_New(0); |
| 1047 | match_self = PyType_HasFeature((PyTypeObject*)type, |
| 1048 | _Py_TPFLAGS_MATCH_SELF); |
| 1049 | } |
| 1050 | else { |
| 1051 | goto fail; |
| 1052 | } |
| 1053 | assert(PyTuple_CheckExact(match_args)); |
| 1054 | Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args); |
| 1055 | if (allowed < nargs) { |
| 1056 | const char *plural = (allowed == 1) ? "" : "s"; |
| 1057 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1058 | "%s() accepts %d positional sub-pattern%s (%d given)", |
| 1059 | ((PyTypeObject*)type)->tp_name, |
| 1060 | allowed, plural, nargs); |
| 1061 | goto fail; |
| 1062 | } |
| 1063 | if (match_self) { |
| 1064 | // Easy. Copy the subject itself, and move on to kwargs. |
| 1065 | PyList_Append(attrs, subject); |
| 1066 | } |
| 1067 | else { |
| 1068 | for (Py_ssize_t i = 0; i < nargs; i++) { |
| 1069 | PyObject *name = PyTuple_GET_ITEM(match_args, i); |
| 1070 | if (!PyUnicode_CheckExact(name)) { |
| 1071 | _PyErr_Format(tstate, PyExc_TypeError, |
| 1072 | "__match_args__ elements must be strings " |
| 1073 | "(got %s)", Py_TYPE(name)->tp_name); |
| 1074 | goto fail; |
| 1075 | } |
| 1076 | PyObject *attr = match_class_attr(tstate, subject, type, name, |
| 1077 | seen); |
| 1078 | if (attr == NULL) { |
| 1079 | goto fail; |
| 1080 | } |
| 1081 | PyList_Append(attrs, attr); |
| 1082 | Py_DECREF(attr); |
| 1083 | } |
| 1084 | } |
| 1085 | Py_CLEAR(match_args); |
| 1086 | } |
| 1087 | // Finally, the keyword subpatterns: |
| 1088 | for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) { |
| 1089 | PyObject *name = PyTuple_GET_ITEM(kwargs, i); |
| 1090 | PyObject *attr = match_class_attr(tstate, subject, type, name, seen); |
| 1091 | if (attr == NULL) { |
| 1092 | goto fail; |
| 1093 | } |
| 1094 | PyList_Append(attrs, attr); |
| 1095 | Py_DECREF(attr); |
| 1096 | } |
| 1097 | Py_SETREF(attrs, PyList_AsTuple(attrs)); |
| 1098 | Py_DECREF(seen); |
| 1099 | return attrs; |
| 1100 | fail: |
| 1101 | // We really don't care whether an error was raised or not... that's our |
| 1102 | // caller's problem. All we know is that the match failed. |
| 1103 | Py_XDECREF(match_args); |
| 1104 | Py_DECREF(seen); |
| 1105 | Py_DECREF(attrs); |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 1110 | static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1111 | static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 1112 | |
Victor Stinner | e225beb | 2019-06-03 18:14:24 +0200 | [diff] [blame] | 1113 | #define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 1114 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1115 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1116 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1117 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1118 | { |
Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 1119 | PyThreadState *tstate = PyThreadState_GET(); |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 1120 | if (locals == NULL) { |
| 1121 | locals = globals; |
| 1122 | } |
Victor Stinner | fc980e0 | 2021-03-18 14:51:24 +0100 | [diff] [blame] | 1123 | PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 1124 | if (builtins == NULL) { |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | PyFrameConstructor desc = { |
| 1128 | .fc_globals = globals, |
| 1129 | .fc_builtins = builtins, |
| 1130 | .fc_name = ((PyCodeObject *)co)->co_name, |
| 1131 | .fc_qualname = ((PyCodeObject *)co)->co_name, |
| 1132 | .fc_code = co, |
| 1133 | .fc_defaults = NULL, |
| 1134 | .fc_kwdefaults = NULL, |
| 1135 | .fc_closure = NULL |
| 1136 | }; |
Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 1137 | return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | /* Interpreter main loop */ |
| 1142 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 1143 | PyObject * |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 1144 | PyEval_EvalFrame(PyFrameObject *f) |
| 1145 | { |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 1146 | /* Function kept for backward compatibility */ |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 1147 | PyThreadState *tstate = _PyThreadState_GET(); |
| 1148 | return _PyEval_EvalFrame(tstate, f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1152 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1153 | { |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 1154 | PyThreadState *tstate = _PyThreadState_GET(); |
| 1155 | return _PyEval_EvalFrame(tstate, f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1158 | |
| 1159 | /* Handle signals, pending calls, GIL drop request |
| 1160 | and asynchronous exception */ |
| 1161 | static int |
| 1162 | eval_frame_handle_pending(PyThreadState *tstate) |
| 1163 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1164 | _PyRuntimeState * const runtime = &_PyRuntime; |
| 1165 | struct _ceval_runtime_state *ceval = &runtime->ceval; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 1166 | |
| 1167 | /* Pending signals */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 1168 | if (_Py_atomic_load_relaxed(&ceval->signals_pending)) { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1169 | if (handle_signals(tstate) != 0) { |
| 1170 | return -1; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /* Pending calls */ |
Victor Stinner | 299b8c6 | 2020-05-05 17:40:18 +0200 | [diff] [blame] | 1175 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1176 | if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) { |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 1177 | if (make_pending_calls(tstate->interp) != 0) { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1178 | return -1; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | /* GIL drop request */ |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 1183 | if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1184 | /* Give another thread a chance */ |
| 1185 | if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { |
| 1186 | Py_FatalError("tstate mix-up"); |
| 1187 | } |
Victor Stinner | 0b1e330 | 2020-05-05 16:14:31 +0200 | [diff] [blame] | 1188 | drop_gil(ceval, ceval2, tstate); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1189 | |
| 1190 | /* Other threads may run now */ |
| 1191 | |
| 1192 | take_gil(tstate); |
| 1193 | |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 1194 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 1195 | (void)_PyThreadState_Swap(&runtime->gilstate, tstate); |
| 1196 | #else |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1197 | if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { |
| 1198 | Py_FatalError("orphan tstate"); |
| 1199 | } |
Victor Stinner | e838a93 | 2020-05-05 19:56:48 +0200 | [diff] [blame] | 1200 | #endif |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /* Check for asynchronous exception. */ |
| 1204 | if (tstate->async_exc != NULL) { |
| 1205 | PyObject *exc = tstate->async_exc; |
| 1206 | tstate->async_exc = NULL; |
Victor Stinner | b54a99d | 2020-04-08 23:35:05 +0200 | [diff] [blame] | 1207 | UNSIGNAL_ASYNC_EXC(tstate->interp); |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1208 | _PyErr_SetNone(tstate, exc); |
| 1209 | Py_DECREF(exc); |
| 1210 | return -1; |
| 1211 | } |
| 1212 | |
Victor Stinner | d96a7a8 | 2020-11-13 14:44:42 +0100 | [diff] [blame] | 1213 | #ifdef MS_WINDOWS |
| 1214 | // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a |
| 1215 | // different thread than the Python thread, in which case |
| 1216 | // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the |
| 1217 | // current Python thread with the correct _Py_ThreadCanHandleSignals() |
| 1218 | // value. It prevents to interrupt the eval loop at every instruction if |
| 1219 | // the current Python thread cannot handle signals (if |
| 1220 | // _Py_ThreadCanHandleSignals() is false). |
| 1221 | COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2); |
| 1222 | #endif |
| 1223 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1227 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1228 | /* Computed GOTOs, or |
| 1229 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 1230 | using gcc's labels-as-values extension |
| 1231 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 1232 | |
| 1233 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1235 | combined with a lookup table of jump addresses. However, since the |
| 1236 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 1237 | hard time making the right prediction for where to jump next (actually, |
| 1238 | it will be always wrong except in the uncommon case of a sequence of |
| 1239 | several identical opcodes). |
| 1240 | |
| 1241 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 1242 | indirect jump instruction at the end of each opcode. Since the jump |
| 1243 | instruction is at a different address for each opcode, the CPU will make a |
| 1244 | separate prediction for each of these instructions, which is equivalent to |
| 1245 | predicting the second opcode of each opcode pair. These predictions have |
| 1246 | a much better chance to turn out valid, especially in small bytecode loops. |
| 1247 | |
| 1248 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1250 | and potentially many more instructions (depending on the pipeline width). |
| 1251 | A correctly predicted branch, however, is nearly free. |
| 1252 | |
| 1253 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 1254 | faster than the normal "switch" version, depending on the compiler and the |
| 1255 | CPU architecture. |
| 1256 | |
| 1257 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 1258 | because it would render the measurements invalid. |
| 1259 | |
| 1260 | |
| 1261 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 1262 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 1263 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 1264 | -fno-crossjumping). |
| 1265 | */ |
| 1266 | |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1267 | /* Use macros rather than inline functions, to make it as clear as possible |
| 1268 | * to the C compiler that the tracing check is a simple test then branch. |
| 1269 | * We want to be sure that the compiler knows this before it generates |
| 1270 | * the CFG. |
| 1271 | */ |
| 1272 | #ifdef LLTRACE |
| 1273 | #define OR_LLTRACE || lltrace |
| 1274 | #else |
| 1275 | #define OR_LLTRACE |
| 1276 | #endif |
| 1277 | |
| 1278 | #ifdef WITH_DTRACE |
| 1279 | #define OR_DTRACE_LINE || PyDTrace_LINE_ENABLED() |
| 1280 | #else |
| 1281 | #define OR_DTRACE_LINE |
| 1282 | #endif |
| 1283 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1284 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1285 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1286 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1287 | #endif |
| 1288 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1289 | #ifdef HAVE_COMPUTED_GOTOS |
| 1290 | #ifndef USE_COMPUTED_GOTOS |
| 1291 | #define USE_COMPUTED_GOTOS 1 |
| 1292 | #endif |
| 1293 | #else |
| 1294 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 1295 | #error "Computed gotos are not supported on this compiler." |
| 1296 | #endif |
| 1297 | #undef USE_COMPUTED_GOTOS |
| 1298 | #define USE_COMPUTED_GOTOS 0 |
| 1299 | #endif |
| 1300 | |
| 1301 | #if USE_COMPUTED_GOTOS |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1302 | #define TARGET(op) op: TARGET_##op |
| 1303 | #define DISPATCH_GOTO() goto *opcode_targets[opcode] |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1304 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1305 | #define TARGET(op) op |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1306 | #define DISPATCH_GOTO() goto dispatch_opcode |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1307 | #endif |
| 1308 | |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1309 | #define DISPATCH() \ |
| 1310 | { \ |
| 1311 | if (_Py_TracingPossible(ceval2) OR_DTRACE_LINE OR_LLTRACE) { \ |
| 1312 | goto tracing_dispatch; \ |
| 1313 | } \ |
| 1314 | f->f_lasti = INSTR_OFFSET(); \ |
| 1315 | NEXTOPARG(); \ |
| 1316 | DISPATCH_GOTO(); \ |
| 1317 | } |
| 1318 | |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1319 | #define CHECK_EVAL_BREAKER() \ |
| 1320 | if (_Py_atomic_load_relaxed(eval_breaker)) { \ |
| 1321 | continue; \ |
| 1322 | } |
| 1323 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1324 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 1325 | /* Tuple access macros */ |
| 1326 | |
| 1327 | #ifndef Py_DEBUG |
| 1328 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 1329 | #else |
| 1330 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 1331 | #endif |
| 1332 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1333 | /* Code access macros */ |
| 1334 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1335 | /* The integer overflow is checked by an assertion below. */ |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 1336 | #define INSTR_OFFSET() ((int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1337 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1338 | _Py_CODEUNIT word = *next_instr; \ |
| 1339 | opcode = _Py_OPCODE(word); \ |
| 1340 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1341 | next_instr++; \ |
| 1342 | } while (0) |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 1343 | #define JUMPTO(x) (next_instr = first_instr + (x)) |
| 1344 | #define JUMPBY(x) (next_instr += (x)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1345 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1346 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | Some opcodes tend to come in pairs thus making it possible to |
| 1348 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1349 | 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] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | Verifying the prediction costs a single high-speed test of a register |
| 1352 | variable against a constant. If the pairing was good, then the |
| 1353 | processor's own internal branch predication has a high likelihood of |
| 1354 | success, resulting in a nearly zero-overhead transition to the |
| 1355 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1356 | including its unpredictable switch-case branch. Combined with the |
| 1357 | processor's internal branch prediction, a successful PREDICT has the |
| 1358 | effect of making the two opcodes run as if they were a single new opcode |
| 1359 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1360 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 1361 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | predictions turned-on and interpret the results as if some opcodes |
| 1363 | had been combined or turn-off predictions so that the opcode frequency |
| 1364 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1365 | |
| 1366 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | the CPU to record separate branch prediction information for each |
| 1368 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1369 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1370 | */ |
| 1371 | |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1372 | #define PREDICT_ID(op) PRED_##op |
| 1373 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 1374 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1375 | #define PREDICT(op) if (0) goto PREDICT_ID(op) |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 1376 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1377 | #define PREDICT(op) \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1378 | do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1379 | _Py_CODEUNIT word = *next_instr; \ |
| 1380 | opcode = _Py_OPCODE(word); \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1381 | if (opcode == op) { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1382 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1383 | next_instr++; \ |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1384 | goto PREDICT_ID(op); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1385 | } \ |
| 1386 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1387 | #endif |
Denis Chernikov | baf29b2 | 2020-02-21 12:17:50 +0300 | [diff] [blame] | 1388 | #define PREDICTED(op) PREDICT_ID(op): |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1389 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 1390 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1391 | /* Stack manipulation macros */ |
| 1392 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1393 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 1394 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1395 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 1396 | #define EMPTY() (STACK_LEVEL() == 0) |
| 1397 | #define TOP() (stack_pointer[-1]) |
| 1398 | #define SECOND() (stack_pointer[-2]) |
| 1399 | #define THIRD() (stack_pointer[-3]) |
| 1400 | #define FOURTH() (stack_pointer[-4]) |
| 1401 | #define PEEK(n) (stack_pointer[-(n)]) |
| 1402 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 1403 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 1404 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 1405 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1406 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 1407 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 1408 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1409 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1410 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1412 | lltrace && prtrace(tstate, TOP(), "push")); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1413 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1414 | #define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1415 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1416 | #define STACK_GROW(n) do { \ |
| 1417 | assert(n >= 0); \ |
| 1418 | (void)(BASIC_STACKADJ(n), \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1419 | lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1420 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 1421 | } while (0) |
| 1422 | #define STACK_SHRINK(n) do { \ |
| 1423 | assert(n >= 0); \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1424 | (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1425 | (void)(BASIC_STACKADJ(-n)); \ |
| 1426 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 1427 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 1428 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1429 | prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1430 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1431 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1432 | #define PUSH(v) BASIC_PUSH(v) |
| 1433 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1434 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 1435 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 1436 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1437 | #endif |
| 1438 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1439 | /* Local variable macros */ |
| 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 1442 | |
| 1443 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 1444 | then store the new value; it must copy the old value to a temporary |
| 1445 | value, then store the new value, and then DECREF the temporary value. |
| 1446 | This is because it is possible that during the DECREF the frame is |
| 1447 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 1448 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1449 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1450 | GETLOCAL(i) = value; \ |
| 1451 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1452 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1453 | |
| 1454 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 1456 | PyObject *v = POP(); \ |
| 1457 | Py_XDECREF(v); \ |
| 1458 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1459 | |
| 1460 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1461 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1463 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 1465 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 1466 | value = POP(); \ |
| 1467 | Py_XDECREF(value); \ |
| 1468 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 1469 | exc_info = tstate->exc_info; \ |
| 1470 | type = exc_info->exc_type; \ |
| 1471 | value = exc_info->exc_value; \ |
| 1472 | traceback = exc_info->exc_traceback; \ |
| 1473 | exc_info->exc_type = POP(); \ |
| 1474 | exc_info->exc_value = POP(); \ |
| 1475 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | Py_XDECREF(type); \ |
| 1477 | Py_XDECREF(value); \ |
| 1478 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1479 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1480 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1481 | /* macros for opcode cache */ |
| 1482 | #define OPCACHE_CHECK() \ |
| 1483 | do { \ |
| 1484 | co_opcache = NULL; \ |
| 1485 | if (co->co_opcache != NULL) { \ |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1486 | unsigned char co_opcache_offset = \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1487 | co->co_opcache_map[next_instr - first_instr]; \ |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1488 | if (co_opcache_offset > 0) { \ |
| 1489 | assert(co_opcache_offset <= co->co_opcache_size); \ |
| 1490 | co_opcache = &co->co_opcache[co_opcache_offset - 1]; \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1491 | assert(co_opcache != NULL); \ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1492 | } \ |
| 1493 | } \ |
| 1494 | } while (0) |
| 1495 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1496 | #define OPCACHE_DEOPT() \ |
| 1497 | do { \ |
| 1498 | if (co_opcache != NULL) { \ |
| 1499 | co_opcache->optimized = -1; \ |
| 1500 | unsigned char co_opcache_offset = \ |
| 1501 | co->co_opcache_map[next_instr - first_instr]; \ |
| 1502 | assert(co_opcache_offset <= co->co_opcache_size); \ |
| 1503 | co->co_opcache_map[co_opcache_offset] = 0; \ |
| 1504 | co_opcache = NULL; \ |
| 1505 | } \ |
| 1506 | } while (0) |
| 1507 | |
| 1508 | #define OPCACHE_DEOPT_LOAD_ATTR() \ |
| 1509 | do { \ |
| 1510 | if (co_opcache != NULL) { \ |
| 1511 | OPCACHE_STAT_ATTR_DEOPT(); \ |
| 1512 | OPCACHE_DEOPT(); \ |
| 1513 | } \ |
| 1514 | } while (0) |
| 1515 | |
| 1516 | #define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \ |
| 1517 | do { \ |
| 1518 | if (co_opcache != NULL && --co_opcache->optimized <= 0) { \ |
| 1519 | OPCACHE_DEOPT_LOAD_ATTR(); \ |
| 1520 | } \ |
| 1521 | } while (0) |
| 1522 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1523 | #if OPCACHE_STATS |
| 1524 | |
| 1525 | #define OPCACHE_STAT_GLOBAL_HIT() \ |
| 1526 | do { \ |
| 1527 | if (co->co_opcache != NULL) opcache_global_hits++; \ |
| 1528 | } while (0) |
| 1529 | |
| 1530 | #define OPCACHE_STAT_GLOBAL_MISS() \ |
| 1531 | do { \ |
| 1532 | if (co->co_opcache != NULL) opcache_global_misses++; \ |
| 1533 | } while (0) |
| 1534 | |
| 1535 | #define OPCACHE_STAT_GLOBAL_OPT() \ |
| 1536 | do { \ |
| 1537 | if (co->co_opcache != NULL) opcache_global_opts++; \ |
| 1538 | } while (0) |
| 1539 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1540 | #define OPCACHE_STAT_ATTR_HIT() \ |
| 1541 | do { \ |
| 1542 | if (co->co_opcache != NULL) opcache_attr_hits++; \ |
| 1543 | } while (0) |
| 1544 | |
| 1545 | #define OPCACHE_STAT_ATTR_MISS() \ |
| 1546 | do { \ |
| 1547 | if (co->co_opcache != NULL) opcache_attr_misses++; \ |
| 1548 | } while (0) |
| 1549 | |
| 1550 | #define OPCACHE_STAT_ATTR_OPT() \ |
| 1551 | do { \ |
| 1552 | if (co->co_opcache!= NULL) opcache_attr_opts++; \ |
| 1553 | } while (0) |
| 1554 | |
| 1555 | #define OPCACHE_STAT_ATTR_DEOPT() \ |
| 1556 | do { \ |
| 1557 | if (co->co_opcache != NULL) opcache_attr_deopts++; \ |
| 1558 | } while (0) |
| 1559 | |
| 1560 | #define OPCACHE_STAT_ATTR_TOTAL() \ |
| 1561 | do { \ |
| 1562 | if (co->co_opcache != NULL) opcache_attr_total++; \ |
| 1563 | } while (0) |
| 1564 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1565 | #else /* OPCACHE_STATS */ |
| 1566 | |
| 1567 | #define OPCACHE_STAT_GLOBAL_HIT() |
| 1568 | #define OPCACHE_STAT_GLOBAL_MISS() |
| 1569 | #define OPCACHE_STAT_GLOBAL_OPT() |
| 1570 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 1571 | #define OPCACHE_STAT_ATTR_HIT() |
| 1572 | #define OPCACHE_STAT_ATTR_MISS() |
| 1573 | #define OPCACHE_STAT_ATTR_OPT() |
| 1574 | #define OPCACHE_STAT_ATTR_DEOPT() |
| 1575 | #define OPCACHE_STAT_ATTR_TOTAL() |
| 1576 | |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1577 | #endif |
| 1578 | |
Mark Shannon | d41bddd | 2021-03-25 12:00:30 +0000 | [diff] [blame] | 1579 | |
| 1580 | PyObject* _Py_HOT_FUNCTION |
| 1581 | _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag) |
| 1582 | { |
| 1583 | _Py_EnsureTstateNotNULL(tstate); |
| 1584 | |
| 1585 | #if USE_COMPUTED_GOTOS |
| 1586 | /* Import the static jump table */ |
| 1587 | #include "opcode_targets.h" |
| 1588 | #endif |
| 1589 | |
| 1590 | #ifdef DXPAIRS |
| 1591 | int lastopcode = 0; |
| 1592 | #endif |
| 1593 | PyObject **stack_pointer; /* Next free slot in value stack */ |
| 1594 | const _Py_CODEUNIT *next_instr; |
| 1595 | int opcode; /* Current opcode */ |
| 1596 | int oparg; /* Current opcode argument, if any */ |
| 1597 | PyObject **fastlocals, **freevars; |
| 1598 | PyObject *retval = NULL; /* Return value */ |
| 1599 | struct _ceval_state * const ceval2 = &tstate->interp->ceval; |
| 1600 | _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker; |
| 1601 | PyCodeObject *co; |
| 1602 | |
Mark Shannon | d41bddd | 2021-03-25 12:00:30 +0000 | [diff] [blame] | 1603 | const _Py_CODEUNIT *first_instr; |
| 1604 | PyObject *names; |
| 1605 | PyObject *consts; |
| 1606 | _PyOpcache *co_opcache; |
| 1607 | |
| 1608 | #ifdef LLTRACE |
| 1609 | _Py_IDENTIFIER(__ltrace__); |
| 1610 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1611 | |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 1612 | if (_Py_EnterRecursiveCall(tstate, "")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | return NULL; |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 1614 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1615 | |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 1616 | PyTraceInfo trace_info; |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1617 | /* Mark trace_info as uninitialized */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 1618 | trace_info.code = NULL; |
| 1619 | |
| 1620 | /* push frame */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | tstate->frame = f; |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 1622 | co = f->f_code; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | if (tstate->use_tracing) { |
| 1625 | if (tstate->c_tracefunc != NULL) { |
| 1626 | /* tstate->c_tracefunc, if defined, is a |
| 1627 | function that will be called on *every* entry |
| 1628 | to a code block. Its return value, if not |
| 1629 | None, is a function that will be called at |
| 1630 | the start of each executed line of code. |
| 1631 | (Actually, the function must return itself |
| 1632 | in order to continue tracing.) The trace |
| 1633 | functions are called with three arguments: |
| 1634 | a pointer to the current frame, a string |
| 1635 | indicating why the function is called, and |
| 1636 | an argument which depends on the situation. |
| 1637 | The global trace function is also called |
| 1638 | whenever an exception is detected. */ |
| 1639 | if (call_trace_protected(tstate->c_tracefunc, |
| 1640 | tstate->c_traceobj, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 1641 | tstate, f, &trace_info, |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 1642 | PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | /* Trace function raised an error */ |
| 1644 | goto exit_eval_frame; |
| 1645 | } |
| 1646 | } |
| 1647 | if (tstate->c_profilefunc != NULL) { |
| 1648 | /* Similar for c_profilefunc, except it needn't |
| 1649 | return itself and isn't called for "line" events */ |
| 1650 | if (call_trace_protected(tstate->c_profilefunc, |
| 1651 | tstate->c_profileobj, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 1652 | tstate, f, &trace_info, |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 1653 | PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | /* Profile function raised an error */ |
| 1655 | goto exit_eval_frame; |
| 1656 | } |
| 1657 | } |
| 1658 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 1659 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1660 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 1661 | dtrace_function_entry(f); |
| 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | names = co->co_names; |
| 1664 | consts = co->co_consts; |
| 1665 | fastlocals = f->f_localsplus; |
| 1666 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1667 | assert(PyBytes_Check(co->co_code)); |
| 1668 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1669 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 1670 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 1671 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1672 | /* |
| 1673 | f->f_lasti refers to the index of the last instruction, |
| 1674 | 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] | 1675 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1676 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1677 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 1680 | direct succession without updating f->f_lasti. A successful |
| 1681 | prediction effectively links the two codes together as if they |
| 1682 | were a single new opcode; accordingly,f->f_lasti will point to |
| 1683 | the first code in the pair (for instance, GET_ITER followed by |
| 1684 | 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] | 1685 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1687 | assert(f->f_lasti >= -1); |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 1688 | next_instr = first_instr + f->f_lasti + 1; |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 1689 | stack_pointer = f->f_valuestack + f->f_stackdepth; |
| 1690 | /* Set f->f_stackdepth to -1. |
| 1691 | * Update when returning or calling trace function. |
| 1692 | Having f_stackdepth <= 0 ensures that invalid |
| 1693 | values are not visible to the cycle GC. |
| 1694 | We choose -1 rather than 0 to assist debugging. |
| 1695 | */ |
| 1696 | f->f_stackdepth = -1; |
| 1697 | f->f_state = FRAME_EXECUTING; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1698 | |
Pablo Galindo | af5fa13 | 2021-02-28 22:41:09 +0000 | [diff] [blame] | 1699 | if (co->co_opcache_flag < opcache_min_runs) { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1700 | co->co_opcache_flag++; |
Pablo Galindo | af5fa13 | 2021-02-28 22:41:09 +0000 | [diff] [blame] | 1701 | if (co->co_opcache_flag == opcache_min_runs) { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1702 | if (_PyCode_InitOpcache(co) < 0) { |
Victor Stinner | 2510494 | 2020-04-24 02:43:18 +0200 | [diff] [blame] | 1703 | goto exit_eval_frame; |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 1704 | } |
| 1705 | #if OPCACHE_STATS |
| 1706 | opcache_code_objects_extra_mem += |
| 1707 | PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) + |
| 1708 | sizeof(_PyOpcache) * co->co_opcache_size; |
| 1709 | opcache_code_objects++; |
| 1710 | #endif |
| 1711 | } |
| 1712 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1713 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1714 | #ifdef LLTRACE |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1715 | { |
| 1716 | int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__); |
| 1717 | if (r < 0) { |
| 1718 | goto exit_eval_frame; |
| 1719 | } |
| 1720 | lltrace = r; |
| 1721 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1722 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1723 | |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1724 | if (throwflag) { /* support for generator.throw() */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1725 | goto error; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1726 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1727 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1728 | #ifdef Py_DEBUG |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 1729 | /* _PyEval_EvalFrameDefault() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 1730 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 1731 | caller loses its exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1732 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 1733 | #endif |
| 1734 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1735 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 1738 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1739 | assert(!_PyErr_Occurred(tstate)); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | /* Do periodic things. Doing this every time through |
| 1742 | the loop would add too much overhead, so we do it |
| 1743 | only every Nth instruction. We also do it if |
Chris Jerdonek | 4a12d12 | 2020-05-14 19:25:45 -0700 | [diff] [blame] | 1744 | ``pending.calls_to_do'' is set, i.e. when an asynchronous |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | event needs attention (e.g. a signal handler or |
| 1746 | async I/O handler); see Py_AddPendingCall() and |
| 1747 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1748 | |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 1749 | if (_Py_atomic_load_relaxed(eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1750 | opcode = _Py_OPCODE(*next_instr); |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1751 | if (opcode != SETUP_FINALLY && |
| 1752 | opcode != SETUP_WITH && |
| 1753 | opcode != BEFORE_ASYNC_WITH && |
| 1754 | opcode != YIELD_FROM) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1755 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 1756 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 1757 | - If we're about to enter the 'with:'. It will prevent |
| 1758 | emitting a resource warning in the common idiom |
| 1759 | 'with open(path) as file:'. |
| 1760 | - If we're about to enter the 'async with:'. |
| 1761 | - 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] | 1762 | *very* useful, but might help in some cases and it's |
| 1763 | traditional) |
| 1764 | - If we're resuming a chain of nested 'yield from' or |
| 1765 | 'await' calls, then each frame is parked with YIELD_FROM |
| 1766 | as its next opcode. If the user hit control-C we want to |
| 1767 | wait until we've reached the innermost frame before |
| 1768 | running the signal handler and raising KeyboardInterrupt |
| 1769 | (see bpo-30039). |
| 1770 | */ |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1771 | if (eval_frame_handle_pending(tstate) != 0) { |
| 1772 | goto error; |
| 1773 | } |
| 1774 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1776 | |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1777 | tracing_dispatch: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | f->f_lasti = INSTR_OFFSET(); |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1779 | NEXTOPARG(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1780 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1781 | if (PyDTrace_LINE_ENABLED()) |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 1782 | maybe_dtrace_line(f, &trace_info); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1785 | |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 1786 | if (_Py_TracingPossible(ceval2) && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1787 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1788 | int err; |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 1789 | /* see maybe_call_line_trace() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | for expository comments */ |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 1791 | f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1794 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1795 | tstate, f, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 1796 | &trace_info); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | /* Reload possibly changed frame fields */ |
| 1798 | JUMPTO(f->f_lasti); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 1799 | stack_pointer = f->f_valuestack+f->f_stackdepth; |
| 1800 | f->f_stackdepth = -1; |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1801 | if (err) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1802 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1803 | goto error; |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1804 | } |
| 1805 | NEXTOPARG(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1807 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1808 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | if (lltrace) { |
| 1812 | if (HAS_ARG(opcode)) { |
| 1813 | printf("%d: %d, %d\n", |
| 1814 | f->f_lasti, opcode, oparg); |
| 1815 | } |
| 1816 | else { |
| 1817 | printf("%d: %d\n", |
| 1818 | f->f_lasti, opcode); |
| 1819 | } |
| 1820 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1821 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1822 | |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1823 | dispatch_opcode: |
| 1824 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1825 | #ifdef DXPAIRS |
| 1826 | dxpairs[lastopcode][opcode]++; |
| 1827 | lastopcode = opcode; |
| 1828 | #endif |
| 1829 | dxp[opcode]++; |
| 1830 | #endif |
| 1831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1835 | It is essential that any operation that fails must goto error |
Mark Shannon | 28d28e0 | 2021-04-08 11:22:55 +0100 | [diff] [blame^] | 1836 | and that all operation that succeed call DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1837 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1838 | case TARGET(NOP): { |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1839 | DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1840 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1841 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1842 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1843 | PyObject *value = GETLOCAL(oparg); |
| 1844 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 1845 | format_exc_check_arg(tstate, PyExc_UnboundLocalError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1846 | UNBOUNDLOCAL_ERROR_MSG, |
| 1847 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1848 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1850 | Py_INCREF(value); |
| 1851 | PUSH(value); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1852 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1853 | } |
| 1854 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1855 | case TARGET(LOAD_CONST): { |
| 1856 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1857 | PyObject *value = GETITEM(consts, oparg); |
| 1858 | Py_INCREF(value); |
| 1859 | PUSH(value); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1860 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1861 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1862 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1863 | case TARGET(STORE_FAST): { |
| 1864 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1865 | PyObject *value = POP(); |
| 1866 | SETLOCAL(oparg, value); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1867 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1868 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1869 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1870 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1871 | PyObject *value = POP(); |
| 1872 | Py_DECREF(value); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1873 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1874 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1875 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1876 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1877 | PyObject *top = TOP(); |
| 1878 | PyObject *second = SECOND(); |
| 1879 | SET_TOP(second); |
| 1880 | SET_SECOND(top); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1881 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1882 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1883 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1884 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1885 | PyObject *top = TOP(); |
| 1886 | PyObject *second = SECOND(); |
| 1887 | PyObject *third = THIRD(); |
| 1888 | SET_TOP(second); |
| 1889 | SET_SECOND(third); |
| 1890 | SET_THIRD(top); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1891 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1892 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1893 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1894 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1895 | PyObject *top = TOP(); |
| 1896 | PyObject *second = SECOND(); |
| 1897 | PyObject *third = THIRD(); |
| 1898 | PyObject *fourth = FOURTH(); |
| 1899 | SET_TOP(second); |
| 1900 | SET_SECOND(third); |
| 1901 | SET_THIRD(fourth); |
| 1902 | SET_FOURTH(top); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1903 | DISPATCH(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1906 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1907 | PyObject *top = TOP(); |
| 1908 | Py_INCREF(top); |
| 1909 | PUSH(top); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1910 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1911 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1912 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1913 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1914 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1915 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1916 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1917 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1918 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1919 | SET_TOP(top); |
| 1920 | SET_SECOND(second); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 1921 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1922 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1923 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1924 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1925 | PyObject *value = TOP(); |
| 1926 | PyObject *res = PyNumber_Positive(value); |
| 1927 | Py_DECREF(value); |
| 1928 | SET_TOP(res); |
| 1929 | if (res == NULL) |
| 1930 | goto error; |
| 1931 | DISPATCH(); |
| 1932 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1933 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1934 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1935 | PyObject *value = TOP(); |
| 1936 | PyObject *res = PyNumber_Negative(value); |
| 1937 | Py_DECREF(value); |
| 1938 | SET_TOP(res); |
| 1939 | if (res == NULL) |
| 1940 | goto error; |
| 1941 | DISPATCH(); |
| 1942 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1943 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1944 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1945 | PyObject *value = TOP(); |
| 1946 | int err = PyObject_IsTrue(value); |
| 1947 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | if (err == 0) { |
| 1949 | Py_INCREF(Py_True); |
| 1950 | SET_TOP(Py_True); |
| 1951 | DISPATCH(); |
| 1952 | } |
| 1953 | else if (err > 0) { |
| 1954 | Py_INCREF(Py_False); |
| 1955 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | DISPATCH(); |
| 1957 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1958 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1959 | goto error; |
| 1960 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1961 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1962 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1963 | PyObject *value = TOP(); |
| 1964 | PyObject *res = PyNumber_Invert(value); |
| 1965 | Py_DECREF(value); |
| 1966 | SET_TOP(res); |
| 1967 | if (res == NULL) |
| 1968 | goto error; |
| 1969 | DISPATCH(); |
| 1970 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1971 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1972 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1973 | PyObject *exp = POP(); |
| 1974 | PyObject *base = TOP(); |
| 1975 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1976 | Py_DECREF(base); |
| 1977 | Py_DECREF(exp); |
| 1978 | SET_TOP(res); |
| 1979 | if (res == NULL) |
| 1980 | goto error; |
| 1981 | DISPATCH(); |
| 1982 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1983 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1984 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1985 | PyObject *right = POP(); |
| 1986 | PyObject *left = TOP(); |
| 1987 | PyObject *res = PyNumber_Multiply(left, right); |
| 1988 | Py_DECREF(left); |
| 1989 | Py_DECREF(right); |
| 1990 | SET_TOP(res); |
| 1991 | if (res == NULL) |
| 1992 | goto error; |
| 1993 | DISPATCH(); |
| 1994 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1995 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1996 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1997 | PyObject *right = POP(); |
| 1998 | PyObject *left = TOP(); |
| 1999 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 2000 | Py_DECREF(left); |
| 2001 | Py_DECREF(right); |
| 2002 | SET_TOP(res); |
| 2003 | if (res == NULL) |
| 2004 | goto error; |
| 2005 | DISPATCH(); |
| 2006 | } |
| 2007 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2008 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2009 | PyObject *divisor = POP(); |
| 2010 | PyObject *dividend = TOP(); |
| 2011 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 2012 | Py_DECREF(dividend); |
| 2013 | Py_DECREF(divisor); |
| 2014 | SET_TOP(quotient); |
| 2015 | if (quotient == NULL) |
| 2016 | goto error; |
| 2017 | DISPATCH(); |
| 2018 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2019 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2020 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2021 | PyObject *divisor = POP(); |
| 2022 | PyObject *dividend = TOP(); |
| 2023 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 2024 | Py_DECREF(dividend); |
| 2025 | Py_DECREF(divisor); |
| 2026 | SET_TOP(quotient); |
| 2027 | if (quotient == NULL) |
| 2028 | goto error; |
| 2029 | DISPATCH(); |
| 2030 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2031 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2032 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2033 | PyObject *divisor = POP(); |
| 2034 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 2035 | PyObject *res; |
| 2036 | if (PyUnicode_CheckExact(dividend) && ( |
| 2037 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 2038 | // fast path; string formatting, but not if the RHS is a str subclass |
| 2039 | // (see issue28598) |
| 2040 | res = PyUnicode_Format(dividend, divisor); |
| 2041 | } else { |
| 2042 | res = PyNumber_Remainder(dividend, divisor); |
| 2043 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2044 | Py_DECREF(divisor); |
| 2045 | Py_DECREF(dividend); |
| 2046 | SET_TOP(res); |
| 2047 | if (res == NULL) |
| 2048 | goto error; |
| 2049 | DISPATCH(); |
| 2050 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2051 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2052 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2053 | PyObject *right = POP(); |
| 2054 | PyObject *left = TOP(); |
| 2055 | PyObject *sum; |
Victor Stinner | bd0a08e | 2020-10-01 18:57:37 +0200 | [diff] [blame] | 2056 | /* NOTE(vstinner): Please don't try to micro-optimize int+int on |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 2057 | CPython using bytecode, it is simply worthless. |
| 2058 | See http://bugs.python.org/issue21955 and |
| 2059 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 2060 | no patch shown any impact on a realistic benchmark, only a minor |
| 2061 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2062 | if (PyUnicode_CheckExact(left) && |
| 2063 | PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2064 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2065 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 2066 | } |
| 2067 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2068 | sum = PyNumber_Add(left, right); |
| 2069 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 2070 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2071 | Py_DECREF(right); |
| 2072 | SET_TOP(sum); |
| 2073 | if (sum == NULL) |
| 2074 | goto error; |
| 2075 | DISPATCH(); |
| 2076 | } |
| 2077 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2078 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2079 | PyObject *right = POP(); |
| 2080 | PyObject *left = TOP(); |
| 2081 | PyObject *diff = PyNumber_Subtract(left, right); |
| 2082 | Py_DECREF(right); |
| 2083 | Py_DECREF(left); |
| 2084 | SET_TOP(diff); |
| 2085 | if (diff == NULL) |
| 2086 | goto error; |
| 2087 | DISPATCH(); |
| 2088 | } |
| 2089 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2090 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2091 | PyObject *sub = POP(); |
| 2092 | PyObject *container = TOP(); |
| 2093 | PyObject *res = PyObject_GetItem(container, sub); |
| 2094 | Py_DECREF(container); |
| 2095 | Py_DECREF(sub); |
| 2096 | SET_TOP(res); |
| 2097 | if (res == NULL) |
| 2098 | goto error; |
| 2099 | DISPATCH(); |
| 2100 | } |
| 2101 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2102 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2103 | PyObject *right = POP(); |
| 2104 | PyObject *left = TOP(); |
| 2105 | PyObject *res = PyNumber_Lshift(left, right); |
| 2106 | Py_DECREF(left); |
| 2107 | Py_DECREF(right); |
| 2108 | SET_TOP(res); |
| 2109 | if (res == NULL) |
| 2110 | goto error; |
| 2111 | DISPATCH(); |
| 2112 | } |
| 2113 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2114 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2115 | PyObject *right = POP(); |
| 2116 | PyObject *left = TOP(); |
| 2117 | PyObject *res = PyNumber_Rshift(left, right); |
| 2118 | Py_DECREF(left); |
| 2119 | Py_DECREF(right); |
| 2120 | SET_TOP(res); |
| 2121 | if (res == NULL) |
| 2122 | goto error; |
| 2123 | DISPATCH(); |
| 2124 | } |
| 2125 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2126 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2127 | PyObject *right = POP(); |
| 2128 | PyObject *left = TOP(); |
| 2129 | PyObject *res = PyNumber_And(left, right); |
| 2130 | Py_DECREF(left); |
| 2131 | Py_DECREF(right); |
| 2132 | SET_TOP(res); |
| 2133 | if (res == NULL) |
| 2134 | goto error; |
| 2135 | DISPATCH(); |
| 2136 | } |
| 2137 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2138 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2139 | PyObject *right = POP(); |
| 2140 | PyObject *left = TOP(); |
| 2141 | PyObject *res = PyNumber_Xor(left, right); |
| 2142 | Py_DECREF(left); |
| 2143 | Py_DECREF(right); |
| 2144 | SET_TOP(res); |
| 2145 | if (res == NULL) |
| 2146 | goto error; |
| 2147 | DISPATCH(); |
| 2148 | } |
| 2149 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2150 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2151 | PyObject *right = POP(); |
| 2152 | PyObject *left = TOP(); |
| 2153 | PyObject *res = PyNumber_Or(left, right); |
| 2154 | Py_DECREF(left); |
| 2155 | Py_DECREF(right); |
| 2156 | SET_TOP(res); |
| 2157 | if (res == NULL) |
| 2158 | goto error; |
| 2159 | DISPATCH(); |
| 2160 | } |
| 2161 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2162 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2163 | PyObject *v = POP(); |
| 2164 | PyObject *list = PEEK(oparg); |
| 2165 | int err; |
| 2166 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2168 | if (err != 0) |
| 2169 | goto error; |
| 2170 | PREDICT(JUMP_ABSOLUTE); |
| 2171 | DISPATCH(); |
| 2172 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2173 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2174 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2175 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2176 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2177 | int err; |
| 2178 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2180 | if (err != 0) |
| 2181 | goto error; |
| 2182 | PREDICT(JUMP_ABSOLUTE); |
| 2183 | DISPATCH(); |
| 2184 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2185 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2186 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2187 | PyObject *exp = POP(); |
| 2188 | PyObject *base = TOP(); |
| 2189 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 2190 | Py_DECREF(base); |
| 2191 | Py_DECREF(exp); |
| 2192 | SET_TOP(res); |
| 2193 | if (res == NULL) |
| 2194 | goto error; |
| 2195 | DISPATCH(); |
| 2196 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2197 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2198 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2199 | PyObject *right = POP(); |
| 2200 | PyObject *left = TOP(); |
| 2201 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 2202 | Py_DECREF(left); |
| 2203 | Py_DECREF(right); |
| 2204 | SET_TOP(res); |
| 2205 | if (res == NULL) |
| 2206 | goto error; |
| 2207 | DISPATCH(); |
| 2208 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2209 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2210 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 2211 | PyObject *right = POP(); |
| 2212 | PyObject *left = TOP(); |
| 2213 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 2214 | Py_DECREF(left); |
| 2215 | Py_DECREF(right); |
| 2216 | SET_TOP(res); |
| 2217 | if (res == NULL) |
| 2218 | goto error; |
| 2219 | DISPATCH(); |
| 2220 | } |
| 2221 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2222 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2223 | PyObject *divisor = POP(); |
| 2224 | PyObject *dividend = TOP(); |
| 2225 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 2226 | Py_DECREF(dividend); |
| 2227 | Py_DECREF(divisor); |
| 2228 | SET_TOP(quotient); |
| 2229 | if (quotient == NULL) |
| 2230 | goto error; |
| 2231 | DISPATCH(); |
| 2232 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2233 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2234 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2235 | PyObject *divisor = POP(); |
| 2236 | PyObject *dividend = TOP(); |
| 2237 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 2238 | Py_DECREF(dividend); |
| 2239 | Py_DECREF(divisor); |
| 2240 | SET_TOP(quotient); |
| 2241 | if (quotient == NULL) |
| 2242 | goto error; |
| 2243 | DISPATCH(); |
| 2244 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2245 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2246 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2247 | PyObject *right = POP(); |
| 2248 | PyObject *left = TOP(); |
| 2249 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 2250 | Py_DECREF(left); |
| 2251 | Py_DECREF(right); |
| 2252 | SET_TOP(mod); |
| 2253 | if (mod == NULL) |
| 2254 | goto error; |
| 2255 | DISPATCH(); |
| 2256 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2257 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2258 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2259 | PyObject *right = POP(); |
| 2260 | PyObject *left = TOP(); |
| 2261 | PyObject *sum; |
| 2262 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2263 | sum = unicode_concatenate(tstate, left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2264 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 2265 | } |
| 2266 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2267 | sum = PyNumber_InPlaceAdd(left, right); |
| 2268 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 2269 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2270 | Py_DECREF(right); |
| 2271 | SET_TOP(sum); |
| 2272 | if (sum == NULL) |
| 2273 | goto error; |
| 2274 | DISPATCH(); |
| 2275 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2276 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2277 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2278 | PyObject *right = POP(); |
| 2279 | PyObject *left = TOP(); |
| 2280 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 2281 | Py_DECREF(left); |
| 2282 | Py_DECREF(right); |
| 2283 | SET_TOP(diff); |
| 2284 | if (diff == NULL) |
| 2285 | goto error; |
| 2286 | DISPATCH(); |
| 2287 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2288 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2289 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2290 | PyObject *right = POP(); |
| 2291 | PyObject *left = TOP(); |
| 2292 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 2293 | Py_DECREF(left); |
| 2294 | Py_DECREF(right); |
| 2295 | SET_TOP(res); |
| 2296 | if (res == NULL) |
| 2297 | goto error; |
| 2298 | DISPATCH(); |
| 2299 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2300 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2301 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2302 | PyObject *right = POP(); |
| 2303 | PyObject *left = TOP(); |
| 2304 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 2305 | Py_DECREF(left); |
| 2306 | Py_DECREF(right); |
| 2307 | SET_TOP(res); |
| 2308 | if (res == NULL) |
| 2309 | goto error; |
| 2310 | DISPATCH(); |
| 2311 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2312 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2313 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2314 | PyObject *right = POP(); |
| 2315 | PyObject *left = TOP(); |
| 2316 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 2317 | Py_DECREF(left); |
| 2318 | Py_DECREF(right); |
| 2319 | SET_TOP(res); |
| 2320 | if (res == NULL) |
| 2321 | goto error; |
| 2322 | DISPATCH(); |
| 2323 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2324 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2325 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2326 | PyObject *right = POP(); |
| 2327 | PyObject *left = TOP(); |
| 2328 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 2329 | Py_DECREF(left); |
| 2330 | Py_DECREF(right); |
| 2331 | SET_TOP(res); |
| 2332 | if (res == NULL) |
| 2333 | goto error; |
| 2334 | DISPATCH(); |
| 2335 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2336 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2337 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2338 | PyObject *right = POP(); |
| 2339 | PyObject *left = TOP(); |
| 2340 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 2341 | Py_DECREF(left); |
| 2342 | Py_DECREF(right); |
| 2343 | SET_TOP(res); |
| 2344 | if (res == NULL) |
| 2345 | goto error; |
| 2346 | DISPATCH(); |
| 2347 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2348 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2349 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2350 | PyObject *sub = TOP(); |
| 2351 | PyObject *container = SECOND(); |
| 2352 | PyObject *v = THIRD(); |
| 2353 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2354 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2355 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2356 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2358 | Py_DECREF(container); |
| 2359 | Py_DECREF(sub); |
| 2360 | if (err != 0) |
| 2361 | goto error; |
| 2362 | DISPATCH(); |
| 2363 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2364 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2365 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2366 | PyObject *sub = TOP(); |
| 2367 | PyObject *container = SECOND(); |
| 2368 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2369 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2370 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2371 | err = PyObject_DelItem(container, sub); |
| 2372 | Py_DECREF(container); |
| 2373 | Py_DECREF(sub); |
| 2374 | if (err != 0) |
| 2375 | goto error; |
| 2376 | DISPATCH(); |
| 2377 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 2378 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2379 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 2380 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2381 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 2382 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 2383 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2384 | if (hook == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2385 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2386 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2387 | Py_DECREF(value); |
| 2388 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2389 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 2390 | res = PyObject_CallOneArg(hook, value); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2391 | Py_DECREF(value); |
| 2392 | if (res == NULL) |
| 2393 | goto error; |
| 2394 | Py_DECREF(res); |
| 2395 | DISPATCH(); |
| 2396 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 2397 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2398 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2399 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | switch (oparg) { |
| 2401 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2402 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2403 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2405 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2406 | /* fall through */ |
| 2407 | case 0: |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 2408 | if (do_raise(tstate, exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2409 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2410 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2413 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2414 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2417 | goto error; |
| 2418 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2419 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2420 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2422 | assert(f->f_iblock == 0); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2423 | assert(EMPTY()); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 2424 | f->f_state = FRAME_RETURNED; |
| 2425 | f->f_stackdepth = 0; |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2426 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2427 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2428 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2429 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2430 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2431 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2432 | PyObject *obj = TOP(); |
| 2433 | PyTypeObject *type = Py_TYPE(obj); |
| 2434 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2435 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2436 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2437 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2438 | |
| 2439 | if (getter != NULL) { |
| 2440 | iter = (*getter)(obj); |
| 2441 | Py_DECREF(obj); |
| 2442 | if (iter == NULL) { |
| 2443 | SET_TOP(NULL); |
| 2444 | goto error; |
| 2445 | } |
| 2446 | } |
| 2447 | else { |
| 2448 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2449 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2450 | "'async for' requires an object with " |
| 2451 | "__aiter__ method, got %.100s", |
| 2452 | type->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2453 | Py_DECREF(obj); |
| 2454 | goto error; |
| 2455 | } |
| 2456 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 2457 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 2458 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2459 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 2460 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2461 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2462 | "'async for' received an object from __aiter__ " |
| 2463 | "that does not implement __anext__: %.100s", |
| 2464 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2465 | Py_DECREF(iter); |
| 2466 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 2467 | } |
| 2468 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 2469 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2470 | DISPATCH(); |
| 2471 | } |
| 2472 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2473 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 2474 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2475 | PyObject *next_iter = NULL; |
| 2476 | PyObject *awaitable = NULL; |
| 2477 | PyObject *aiter = TOP(); |
| 2478 | PyTypeObject *type = Py_TYPE(aiter); |
| 2479 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2480 | if (PyAsyncGen_CheckExact(aiter)) { |
| 2481 | awaitable = type->tp_as_async->am_anext(aiter); |
| 2482 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2483 | goto error; |
| 2484 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2485 | } else { |
| 2486 | if (type->tp_as_async != NULL){ |
| 2487 | getter = type->tp_as_async->am_anext; |
| 2488 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2489 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2490 | if (getter != NULL) { |
| 2491 | next_iter = (*getter)(aiter); |
| 2492 | if (next_iter == NULL) { |
| 2493 | goto error; |
| 2494 | } |
| 2495 | } |
| 2496 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2497 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2498 | "'async for' requires an iterator with " |
| 2499 | "__anext__ method, got %.100s", |
| 2500 | type->tp_name); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2501 | goto error; |
| 2502 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2503 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2504 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 2505 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 2506 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2507 | PyExc_TypeError, |
| 2508 | "'async for' received an invalid object " |
| 2509 | "from __anext__: %.100s", |
| 2510 | Py_TYPE(next_iter)->tp_name); |
| 2511 | |
| 2512 | Py_DECREF(next_iter); |
| 2513 | goto error; |
| 2514 | } else { |
| 2515 | Py_DECREF(next_iter); |
| 2516 | } |
| 2517 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2518 | |
| 2519 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2520 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2521 | DISPATCH(); |
| 2522 | } |
| 2523 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2524 | case TARGET(GET_AWAITABLE): { |
| 2525 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2526 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2527 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2528 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2529 | if (iter == NULL) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2530 | int opcode_at_minus_3 = 0; |
| 2531 | if ((next_instr - first_instr) > 2) { |
| 2532 | opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]); |
| 2533 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2534 | format_awaitable_error(tstate, Py_TYPE(iterable), |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2535 | opcode_at_minus_3, |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 2536 | _Py_OPCODE(next_instr[-2])); |
| 2537 | } |
| 2538 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2539 | Py_DECREF(iterable); |
| 2540 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2541 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 2542 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 2543 | if (yf != NULL) { |
| 2544 | /* `iter` is a coroutine object that is being |
| 2545 | awaited, `yf` is a pointer to the current awaitable |
| 2546 | being awaited on. */ |
| 2547 | Py_DECREF(yf); |
| 2548 | Py_CLEAR(iter); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2549 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 2550 | "coroutine is being awaited already"); |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 2551 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 2552 | } |
| 2553 | } |
| 2554 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2555 | SET_TOP(iter); /* Even if it's NULL */ |
| 2556 | |
| 2557 | if (iter == NULL) { |
| 2558 | goto error; |
| 2559 | } |
| 2560 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2561 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2562 | DISPATCH(); |
| 2563 | } |
| 2564 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2565 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2566 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2567 | PyObject *receiver = TOP(); |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 2568 | PySendResult gen_status; |
| 2569 | if (tstate->c_tracefunc == NULL) { |
| 2570 | gen_status = PyIter_Send(receiver, v, &retval); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2571 | } else { |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 2572 | _Py_IDENTIFIER(send); |
| 2573 | if (v == Py_None && PyIter_Check(receiver)) { |
| 2574 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2575 | } |
| 2576 | else { |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 2577 | retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2578 | } |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2579 | if (retval == NULL) { |
| 2580 | if (tstate->c_tracefunc != NULL |
| 2581 | && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 2582 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2583 | if (_PyGen_FetchStopIterationValue(&retval) == 0) { |
| 2584 | gen_status = PYGEN_RETURN; |
| 2585 | } |
| 2586 | else { |
| 2587 | gen_status = PYGEN_ERROR; |
| 2588 | } |
| 2589 | } |
| 2590 | else { |
| 2591 | gen_status = PYGEN_NEXT; |
| 2592 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2593 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2594 | Py_DECREF(v); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2595 | if (gen_status == PYGEN_ERROR) { |
| 2596 | assert (retval == NULL); |
| 2597 | goto error; |
| 2598 | } |
| 2599 | if (gen_status == PYGEN_RETURN) { |
| 2600 | assert (retval != NULL); |
| 2601 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2602 | Py_DECREF(receiver); |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2603 | SET_TOP(retval); |
| 2604 | retval = NULL; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2605 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2606 | } |
Vladimir Matveev | 2b05361 | 2020-09-18 18:38:38 -0700 | [diff] [blame] | 2607 | assert (gen_status == PYGEN_NEXT); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2608 | /* receiver remains on stack, retval is value to be yielded */ |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 2609 | /* and repeat... */ |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 2610 | assert(f->f_lasti > 0); |
| 2611 | f->f_lasti -= 1; |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 2612 | f->f_state = FRAME_SUSPENDED; |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 2613 | f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2614 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2615 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 2616 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2617 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2619 | |
| 2620 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 2621 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 2622 | Py_DECREF(retval); |
| 2623 | if (w == NULL) { |
| 2624 | retval = NULL; |
| 2625 | goto error; |
| 2626 | } |
| 2627 | retval = w; |
| 2628 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 2629 | f->f_state = FRAME_SUSPENDED; |
Victor Stinner | b7d8d8d | 2020-09-23 14:07:16 +0200 | [diff] [blame] | 2630 | f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 2631 | goto exiting; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2632 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2633 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 2634 | case TARGET(GEN_START): { |
| 2635 | PyObject *none = POP(); |
| 2636 | Py_DECREF(none); |
| 2637 | if (none != Py_None) { |
| 2638 | if (oparg > 2) { |
| 2639 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2640 | "Illegal kind for GEN_START"); |
| 2641 | } |
| 2642 | else { |
| 2643 | static const char *gen_kind[3] = { |
| 2644 | "generator", |
| 2645 | "coroutine", |
| 2646 | "async generator" |
| 2647 | }; |
| 2648 | _PyErr_Format(tstate, PyExc_TypeError, |
| 2649 | "can't send non-None value to a " |
| 2650 | "just-started %s", |
| 2651 | gen_kind[oparg]); |
| 2652 | } |
| 2653 | goto error; |
| 2654 | } |
| 2655 | DISPATCH(); |
| 2656 | } |
| 2657 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2658 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2659 | PyObject *type, *value, *traceback; |
| 2660 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2661 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2662 | if (b->b_type != EXCEPT_HANDLER) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2663 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 2664 | "popped block is not an except handler"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2665 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2667 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 2668 | STACK_LEVEL() <= (b)->b_level + 4); |
| 2669 | exc_info = tstate->exc_info; |
| 2670 | type = exc_info->exc_type; |
| 2671 | value = exc_info->exc_value; |
| 2672 | traceback = exc_info->exc_traceback; |
| 2673 | exc_info->exc_type = POP(); |
| 2674 | exc_info->exc_value = POP(); |
| 2675 | exc_info->exc_traceback = POP(); |
| 2676 | Py_XDECREF(type); |
| 2677 | Py_XDECREF(value); |
| 2678 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2680 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 2681 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2682 | case TARGET(POP_BLOCK): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2683 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2685 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2686 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2687 | case TARGET(RERAISE): { |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 2688 | assert(f->f_iblock > 0); |
| 2689 | if (oparg) { |
| 2690 | f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler; |
| 2691 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2692 | PyObject *exc = POP(); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2693 | PyObject *val = POP(); |
| 2694 | PyObject *tb = POP(); |
| 2695 | assert(PyExceptionClass_Check(exc)); |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 2696 | _PyErr_Restore(tstate, exc, val, tb); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2697 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2698 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2699 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2700 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2701 | PyObject *exc = POP(); |
| 2702 | assert(PyExceptionClass_Check(exc)); |
| 2703 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2704 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2705 | assert(b->b_type == EXCEPT_HANDLER); |
| 2706 | Py_DECREF(exc); |
| 2707 | UNWIND_EXCEPT_HANDLER(b); |
| 2708 | Py_DECREF(POP()); |
| 2709 | JUMPBY(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 2710 | DISPATCH(); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2711 | } |
| 2712 | else { |
| 2713 | PyObject *val = POP(); |
| 2714 | PyObject *tb = POP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2715 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2716 | goto exception_unwind; |
| 2717 | } |
| 2718 | } |
| 2719 | |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 2720 | case TARGET(LOAD_ASSERTION_ERROR): { |
| 2721 | PyObject *value = PyExc_AssertionError; |
| 2722 | Py_INCREF(value); |
| 2723 | PUSH(value); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 2724 | DISPATCH(); |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 2725 | } |
| 2726 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2727 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2728 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2729 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2730 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2731 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2732 | bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2733 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2734 | if (!_PyErr_Occurred(tstate)) { |
| 2735 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2736 | "__build_class__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2737 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2738 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2739 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2740 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2741 | } |
| 2742 | else { |
| 2743 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2744 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2745 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2746 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2747 | if (bc == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2748 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
| 2749 | _PyErr_SetString(tstate, PyExc_NameError, |
| 2750 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2751 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2752 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2753 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2754 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2755 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2756 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2757 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2758 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2759 | PyObject *name = GETITEM(names, oparg); |
| 2760 | PyObject *v = POP(); |
| 2761 | PyObject *ns = f->f_locals; |
| 2762 | int err; |
| 2763 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2764 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2765 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2767 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2768 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2769 | if (PyDict_CheckExact(ns)) |
| 2770 | err = PyDict_SetItem(ns, name, v); |
| 2771 | else |
| 2772 | err = PyObject_SetItem(ns, name, v); |
| 2773 | Py_DECREF(v); |
| 2774 | if (err != 0) |
| 2775 | goto error; |
| 2776 | DISPATCH(); |
| 2777 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2778 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2779 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2780 | PyObject *name = GETITEM(names, oparg); |
| 2781 | PyObject *ns = f->f_locals; |
| 2782 | int err; |
| 2783 | if (ns == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2784 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2785 | "no locals when deleting %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2786 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2788 | err = PyObject_DelItem(ns, name); |
| 2789 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2790 | format_exc_check_arg(tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2791 | NAME_ERROR_MSG, |
| 2792 | name); |
| 2793 | goto error; |
| 2794 | } |
| 2795 | DISPATCH(); |
| 2796 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2797 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2798 | case TARGET(UNPACK_SEQUENCE): { |
| 2799 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2800 | PyObject *seq = POP(), *item, **items; |
| 2801 | if (PyTuple_CheckExact(seq) && |
| 2802 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2803 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2805 | item = items[oparg]; |
| 2806 | Py_INCREF(item); |
| 2807 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2809 | } else if (PyList_CheckExact(seq) && |
| 2810 | PyList_GET_SIZE(seq) == oparg) { |
| 2811 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2812 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2813 | item = items[oparg]; |
| 2814 | Py_INCREF(item); |
| 2815 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2817 | } else if (unpack_iterable(tstate, seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2819 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | } else { |
| 2821 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2822 | Py_DECREF(seq); |
| 2823 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2825 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2826 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2828 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2829 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2830 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2831 | PyObject *seq = POP(); |
| 2832 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2833 | if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2834 | stack_pointer + totalargs)) { |
| 2835 | stack_pointer += totalargs; |
| 2836 | } else { |
| 2837 | Py_DECREF(seq); |
| 2838 | goto error; |
| 2839 | } |
| 2840 | Py_DECREF(seq); |
| 2841 | DISPATCH(); |
| 2842 | } |
| 2843 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2844 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2845 | PyObject *name = GETITEM(names, oparg); |
| 2846 | PyObject *owner = TOP(); |
| 2847 | PyObject *v = SECOND(); |
| 2848 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2849 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2850 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2852 | Py_DECREF(owner); |
| 2853 | if (err != 0) |
| 2854 | goto error; |
| 2855 | DISPATCH(); |
| 2856 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2857 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2858 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2859 | PyObject *name = GETITEM(names, oparg); |
| 2860 | PyObject *owner = POP(); |
| 2861 | int err; |
| 2862 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2863 | Py_DECREF(owner); |
| 2864 | if (err != 0) |
| 2865 | goto error; |
| 2866 | DISPATCH(); |
| 2867 | } |
| 2868 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2869 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2870 | PyObject *name = GETITEM(names, oparg); |
| 2871 | PyObject *v = POP(); |
| 2872 | int err; |
| 2873 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2875 | if (err != 0) |
| 2876 | goto error; |
| 2877 | DISPATCH(); |
| 2878 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2879 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2880 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2881 | PyObject *name = GETITEM(names, oparg); |
| 2882 | int err; |
| 2883 | err = PyDict_DelItem(f->f_globals, name); |
| 2884 | if (err != 0) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2885 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
| 2886 | format_exc_check_arg(tstate, PyExc_NameError, |
| 2887 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2888 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2889 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2890 | } |
| 2891 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2892 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2893 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2894 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2895 | PyObject *name = GETITEM(names, oparg); |
| 2896 | PyObject *locals = f->f_locals; |
| 2897 | PyObject *v; |
| 2898 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2899 | _PyErr_Format(tstate, PyExc_SystemError, |
| 2900 | "no locals when loading %R", name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2901 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2903 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2904 | v = PyDict_GetItemWithError(locals, name); |
| 2905 | if (v != NULL) { |
| 2906 | Py_INCREF(v); |
| 2907 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2908 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2909 | goto error; |
| 2910 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | } |
| 2912 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2913 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2914 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2915 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2916 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2917 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | } |
| 2919 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2920 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2921 | v = PyDict_GetItemWithError(f->f_globals, name); |
| 2922 | if (v != NULL) { |
| 2923 | Py_INCREF(v); |
| 2924 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2925 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2926 | goto error; |
| 2927 | } |
| 2928 | else { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2929 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2930 | v = PyDict_GetItemWithError(f->f_builtins, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2931 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2932 | if (!_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2933 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2934 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2935 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2936 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2937 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2938 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2939 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2940 | } |
| 2941 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2942 | v = PyObject_GetItem(f->f_builtins, name); |
| 2943 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2944 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2945 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2946 | tstate, PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2947 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2948 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2949 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2950 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2951 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2954 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2956 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2957 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2958 | case TARGET(LOAD_GLOBAL): { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2959 | PyObject *name; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2960 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2961 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2962 | && PyDict_CheckExact(f->f_builtins)) |
| 2963 | { |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2964 | OPCACHE_CHECK(); |
| 2965 | if (co_opcache != NULL && co_opcache->optimized > 0) { |
| 2966 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2967 | |
| 2968 | if (lg->globals_ver == |
| 2969 | ((PyDictObject *)f->f_globals)->ma_version_tag |
| 2970 | && lg->builtins_ver == |
| 2971 | ((PyDictObject *)f->f_builtins)->ma_version_tag) |
| 2972 | { |
| 2973 | PyObject *ptr = lg->ptr; |
| 2974 | OPCACHE_STAT_GLOBAL_HIT(); |
| 2975 | assert(ptr != NULL); |
| 2976 | Py_INCREF(ptr); |
| 2977 | PUSH(ptr); |
| 2978 | DISPATCH(); |
| 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2983 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2984 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2985 | name); |
| 2986 | if (v == NULL) { |
Victor Stinner | a486054 | 2021-02-19 15:08:54 +0100 | [diff] [blame] | 2987 | if (!_PyErr_Occurred(tstate)) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2988 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2989 | * an exception if the key doesn't exist */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 2990 | format_exc_check_arg(tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2991 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2992 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2993 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | } |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 2995 | |
| 2996 | if (co_opcache != NULL) { |
| 2997 | _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; |
| 2998 | |
| 2999 | if (co_opcache->optimized == 0) { |
| 3000 | /* Wasn't optimized before. */ |
| 3001 | OPCACHE_STAT_GLOBAL_OPT(); |
| 3002 | } else { |
| 3003 | OPCACHE_STAT_GLOBAL_MISS(); |
| 3004 | } |
| 3005 | |
| 3006 | co_opcache->optimized = 1; |
| 3007 | lg->globals_ver = |
| 3008 | ((PyDictObject *)f->f_globals)->ma_version_tag; |
| 3009 | lg->builtins_ver = |
| 3010 | ((PyDictObject *)f->f_builtins)->ma_version_tag; |
| 3011 | lg->ptr = v; /* borrowed */ |
| 3012 | } |
| 3013 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3014 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3016 | else { |
| 3017 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 3018 | |
| 3019 | /* namespace 1: globals */ |
Inada Naoki | 91234a1 | 2019-06-03 21:30:58 +0900 | [diff] [blame] | 3020 | name = GETITEM(names, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3021 | v = PyObject_GetItem(f->f_globals, name); |
| 3022 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3023 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 3024 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3025 | } |
| 3026 | _PyErr_Clear(tstate); |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 3027 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 3028 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3029 | v = PyObject_GetItem(f->f_builtins, name); |
| 3030 | if (v == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3031 | if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3032 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3033 | tstate, PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 3034 | NAME_ERROR_MSG, name); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3035 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3036 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3040 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3041 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3042 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3043 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3044 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3045 | PyObject *v = GETLOCAL(oparg); |
| 3046 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | SETLOCAL(oparg, NULL); |
| 3048 | DISPATCH(); |
| 3049 | } |
| 3050 | format_exc_check_arg( |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3051 | tstate, PyExc_UnboundLocalError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | UNBOUNDLOCAL_ERROR_MSG, |
| 3053 | PyTuple_GetItem(co->co_varnames, oparg) |
| 3054 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3055 | goto error; |
| 3056 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3057 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3058 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3059 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 3060 | PyObject *oldobj = PyCell_GET(cell); |
| 3061 | if (oldobj != NULL) { |
| 3062 | PyCell_SET(cell, NULL); |
| 3063 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 3064 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3065 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3066 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3067 | goto error; |
| 3068 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3069 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3070 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3071 | PyObject *cell = freevars[oparg]; |
| 3072 | Py_INCREF(cell); |
| 3073 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3075 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3076 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3077 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3078 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 3079 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3080 | assert(locals); |
| 3081 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 3082 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 3083 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 3084 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 3085 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3086 | value = PyDict_GetItemWithError(locals, name); |
| 3087 | if (value != NULL) { |
| 3088 | Py_INCREF(value); |
| 3089 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3090 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3091 | goto error; |
| 3092 | } |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3093 | } |
| 3094 | else { |
| 3095 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 3096 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3097 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3098 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3099 | } |
| 3100 | _PyErr_Clear(tstate); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3101 | } |
| 3102 | } |
| 3103 | if (!value) { |
| 3104 | PyObject *cell = freevars[oparg]; |
| 3105 | value = PyCell_GET(cell); |
| 3106 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3107 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3108 | goto error; |
| 3109 | } |
| 3110 | Py_INCREF(value); |
| 3111 | } |
| 3112 | PUSH(value); |
| 3113 | DISPATCH(); |
| 3114 | } |
| 3115 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3116 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3117 | PyObject *cell = freevars[oparg]; |
| 3118 | PyObject *value = PyCell_GET(cell); |
| 3119 | if (value == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3120 | format_exc_unbound(tstate, co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3121 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3123 | Py_INCREF(value); |
| 3124 | PUSH(value); |
| 3125 | DISPATCH(); |
| 3126 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3127 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3128 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3129 | PyObject *v = POP(); |
| 3130 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 3131 | PyObject *oldobj = PyCell_GET(cell); |
| 3132 | PyCell_SET(cell, v); |
| 3133 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3134 | DISPATCH(); |
| 3135 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3136 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3137 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 3138 | PyObject *str; |
| 3139 | PyObject *empty = PyUnicode_New(0, 0); |
| 3140 | if (empty == NULL) { |
| 3141 | goto error; |
| 3142 | } |
| 3143 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 3144 | Py_DECREF(empty); |
| 3145 | if (str == NULL) |
| 3146 | goto error; |
| 3147 | while (--oparg >= 0) { |
| 3148 | PyObject *item = POP(); |
| 3149 | Py_DECREF(item); |
| 3150 | } |
| 3151 | PUSH(str); |
| 3152 | DISPATCH(); |
| 3153 | } |
| 3154 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3155 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3156 | PyObject *tup = PyTuple_New(oparg); |
| 3157 | if (tup == NULL) |
| 3158 | goto error; |
| 3159 | while (--oparg >= 0) { |
| 3160 | PyObject *item = POP(); |
| 3161 | PyTuple_SET_ITEM(tup, oparg, item); |
| 3162 | } |
| 3163 | PUSH(tup); |
| 3164 | DISPATCH(); |
| 3165 | } |
| 3166 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3167 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3168 | PyObject *list = PyList_New(oparg); |
| 3169 | if (list == NULL) |
| 3170 | goto error; |
| 3171 | while (--oparg >= 0) { |
| 3172 | PyObject *item = POP(); |
| 3173 | PyList_SET_ITEM(list, oparg, item); |
| 3174 | } |
| 3175 | PUSH(list); |
| 3176 | DISPATCH(); |
| 3177 | } |
| 3178 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3179 | case TARGET(LIST_TO_TUPLE): { |
| 3180 | PyObject *list = POP(); |
| 3181 | PyObject *tuple = PyList_AsTuple(list); |
| 3182 | Py_DECREF(list); |
| 3183 | if (tuple == NULL) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3184 | goto error; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3185 | } |
| 3186 | PUSH(tuple); |
| 3187 | DISPATCH(); |
| 3188 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3189 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3190 | case TARGET(LIST_EXTEND): { |
| 3191 | PyObject *iterable = POP(); |
| 3192 | PyObject *list = PEEK(oparg); |
| 3193 | PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable); |
| 3194 | if (none_val == NULL) { |
| 3195 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3196 | (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable))) |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3197 | { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 3198 | _PyErr_Clear(tstate); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3199 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3200 | "Value after * must be an iterable, not %.200s", |
| 3201 | Py_TYPE(iterable)->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3202 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3203 | Py_DECREF(iterable); |
| 3204 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3205 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3206 | Py_DECREF(none_val); |
| 3207 | Py_DECREF(iterable); |
| 3208 | DISPATCH(); |
| 3209 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3210 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3211 | case TARGET(SET_UPDATE): { |
| 3212 | PyObject *iterable = POP(); |
| 3213 | PyObject *set = PEEK(oparg); |
| 3214 | int err = _PySet_Update(set, iterable); |
| 3215 | Py_DECREF(iterable); |
| 3216 | if (err < 0) { |
| 3217 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3218 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3219 | DISPATCH(); |
| 3220 | } |
| 3221 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3222 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3223 | PyObject *set = PySet_New(NULL); |
| 3224 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 3225 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3226 | if (set == NULL) |
| 3227 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 3228 | for (i = oparg; i > 0; i--) { |
| 3229 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3230 | if (err == 0) |
| 3231 | err = PySet_Add(set, item); |
| 3232 | Py_DECREF(item); |
| 3233 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3234 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3235 | if (err != 0) { |
| 3236 | Py_DECREF(set); |
| 3237 | goto error; |
| 3238 | } |
| 3239 | PUSH(set); |
| 3240 | DISPATCH(); |
| 3241 | } |
| 3242 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3243 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3244 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3245 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 3246 | if (map == NULL) |
| 3247 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 3248 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3249 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 3250 | PyObject *key = PEEK(2*i); |
| 3251 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3252 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3253 | if (err != 0) { |
| 3254 | Py_DECREF(map); |
| 3255 | goto error; |
| 3256 | } |
| 3257 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 3258 | |
| 3259 | while (oparg--) { |
| 3260 | Py_DECREF(POP()); |
| 3261 | Py_DECREF(POP()); |
| 3262 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3263 | PUSH(map); |
| 3264 | DISPATCH(); |
| 3265 | } |
| 3266 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3267 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3268 | _Py_IDENTIFIER(__annotations__); |
| 3269 | int err; |
| 3270 | PyObject *ann_dict; |
| 3271 | if (f->f_locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3272 | _PyErr_Format(tstate, PyExc_SystemError, |
| 3273 | "no locals found when setting up annotations"); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3274 | goto error; |
| 3275 | } |
| 3276 | /* check if __annotations__ in locals()... */ |
| 3277 | if (PyDict_CheckExact(f->f_locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3278 | ann_dict = _PyDict_GetItemIdWithError(f->f_locals, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3279 | &PyId___annotations__); |
| 3280 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3281 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3282 | goto error; |
| 3283 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3284 | /* ...if not, create a new one */ |
| 3285 | ann_dict = PyDict_New(); |
| 3286 | if (ann_dict == NULL) { |
| 3287 | goto error; |
| 3288 | } |
| 3289 | err = _PyDict_SetItemId(f->f_locals, |
| 3290 | &PyId___annotations__, ann_dict); |
| 3291 | Py_DECREF(ann_dict); |
| 3292 | if (err != 0) { |
| 3293 | goto error; |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | else { |
| 3298 | /* do the same if locals() is not a dict */ |
| 3299 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 3300 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 3301 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3302 | } |
| 3303 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 3304 | if (ann_dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3305 | if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3306 | goto error; |
| 3307 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3308 | _PyErr_Clear(tstate); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3309 | ann_dict = PyDict_New(); |
| 3310 | if (ann_dict == NULL) { |
| 3311 | goto error; |
| 3312 | } |
| 3313 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 3314 | Py_DECREF(ann_dict); |
| 3315 | if (err != 0) { |
| 3316 | goto error; |
| 3317 | } |
| 3318 | } |
| 3319 | else { |
| 3320 | Py_DECREF(ann_dict); |
| 3321 | } |
| 3322 | } |
| 3323 | DISPATCH(); |
| 3324 | } |
| 3325 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3326 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3327 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3328 | PyObject *map; |
| 3329 | PyObject *keys = TOP(); |
| 3330 | if (!PyTuple_CheckExact(keys) || |
| 3331 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3332 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3333 | "bad BUILD_CONST_KEY_MAP keys argument"); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3334 | goto error; |
| 3335 | } |
| 3336 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 3337 | if (map == NULL) { |
| 3338 | goto error; |
| 3339 | } |
| 3340 | for (i = oparg; i > 0; i--) { |
| 3341 | int err; |
| 3342 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 3343 | PyObject *value = PEEK(i + 1); |
| 3344 | err = PyDict_SetItem(map, key, value); |
| 3345 | if (err != 0) { |
| 3346 | Py_DECREF(map); |
| 3347 | goto error; |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | Py_DECREF(POP()); |
| 3352 | while (oparg--) { |
| 3353 | Py_DECREF(POP()); |
| 3354 | } |
| 3355 | PUSH(map); |
| 3356 | DISPATCH(); |
| 3357 | } |
| 3358 | |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3359 | case TARGET(DICT_UPDATE): { |
| 3360 | PyObject *update = POP(); |
| 3361 | PyObject *dict = PEEK(oparg); |
| 3362 | if (PyDict_Update(dict, update) < 0) { |
| 3363 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
| 3364 | _PyErr_Format(tstate, PyExc_TypeError, |
| 3365 | "'%.200s' object is not a mapping", |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3366 | Py_TYPE(update)->tp_name); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3367 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3368 | Py_DECREF(update); |
| 3369 | goto error; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3370 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3371 | Py_DECREF(update); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3372 | DISPATCH(); |
| 3373 | } |
| 3374 | |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3375 | case TARGET(DICT_MERGE): { |
| 3376 | PyObject *update = POP(); |
| 3377 | PyObject *dict = PEEK(oparg); |
| 3378 | |
| 3379 | if (_PyDict_MergeEx(dict, update, 2) < 0) { |
| 3380 | format_kwargs_error(tstate, PEEK(2 + oparg), update); |
| 3381 | Py_DECREF(update); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 3382 | goto error; |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 3383 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3384 | Py_DECREF(update); |
Brandt Bucher | f185a73 | 2019-09-28 17:12:49 -0700 | [diff] [blame] | 3385 | PREDICT(CALL_FUNCTION_EX); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 3386 | DISPATCH(); |
| 3387 | } |
| 3388 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3389 | case TARGET(MAP_ADD): { |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 3390 | PyObject *value = TOP(); |
| 3391 | PyObject *key = SECOND(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3392 | PyObject *map; |
| 3393 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3394 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 3395 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3396 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 3397 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3398 | Py_DECREF(value); |
| 3399 | Py_DECREF(key); |
| 3400 | if (err != 0) |
| 3401 | goto error; |
| 3402 | PREDICT(JUMP_ABSOLUTE); |
| 3403 | DISPATCH(); |
| 3404 | } |
| 3405 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3406 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3407 | PyObject *name = GETITEM(names, oparg); |
| 3408 | PyObject *owner = TOP(); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3409 | |
| 3410 | PyTypeObject *type = Py_TYPE(owner); |
| 3411 | PyObject *res; |
| 3412 | PyObject **dictptr; |
| 3413 | PyObject *dict; |
| 3414 | _PyOpCodeOpt_LoadAttr *la; |
| 3415 | |
| 3416 | OPCACHE_STAT_ATTR_TOTAL(); |
| 3417 | |
| 3418 | OPCACHE_CHECK(); |
| 3419 | if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) |
| 3420 | { |
| 3421 | if (co_opcache->optimized > 0) { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3422 | // Fast path -- cache hit makes LOAD_ATTR ~30% faster. |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3423 | la = &co_opcache->u.la; |
| 3424 | if (la->type == type && la->tp_version_tag == type->tp_version_tag) |
| 3425 | { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3426 | // Hint >= 0 is a dict index; hint == -1 is a dict miss. |
| 3427 | // Hint < -1 is an inverted slot offset: offset is strictly > 0, |
| 3428 | // so ~offset is strictly < -1 (assuming 2's complement). |
| 3429 | if (la->hint < -1) { |
| 3430 | // Even faster path -- slot hint. |
| 3431 | Py_ssize_t offset = ~la->hint; |
| 3432 | // fprintf(stderr, "Using hint for offset %zd\n", offset); |
| 3433 | char *addr = (char *)owner + offset; |
| 3434 | res = *(PyObject **)addr; |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3435 | if (res != NULL) { |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3436 | Py_INCREF(res); |
| 3437 | SET_TOP(res); |
| 3438 | Py_DECREF(owner); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3439 | DISPATCH(); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3440 | } |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3441 | // Else slot is NULL. Fall through to slow path to raise AttributeError(name). |
| 3442 | // Don't DEOPT, since the slot is still there. |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3443 | } else { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3444 | // Fast path for dict. |
| 3445 | assert(type->tp_dict != NULL); |
| 3446 | assert(type->tp_dictoffset > 0); |
| 3447 | |
| 3448 | dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset); |
| 3449 | dict = *dictptr; |
| 3450 | if (dict != NULL && PyDict_CheckExact(dict)) { |
| 3451 | Py_ssize_t hint = la->hint; |
| 3452 | Py_INCREF(dict); |
| 3453 | res = NULL; |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3454 | assert(!_PyErr_Occurred(tstate)); |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3455 | la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res); |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3456 | if (res != NULL) { |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3457 | assert(la->hint >= 0); |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3458 | if (la->hint == hint && hint >= 0) { |
| 3459 | // Our hint has helped -- cache hit. |
| 3460 | OPCACHE_STAT_ATTR_HIT(); |
| 3461 | } else { |
| 3462 | // The hint we provided didn't work. |
| 3463 | // Maybe next time? |
| 3464 | OPCACHE_MAYBE_DEOPT_LOAD_ATTR(); |
| 3465 | } |
| 3466 | |
| 3467 | Py_INCREF(res); |
| 3468 | SET_TOP(res); |
| 3469 | Py_DECREF(owner); |
| 3470 | Py_DECREF(dict); |
| 3471 | DISPATCH(); |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3472 | } |
| 3473 | else { |
| 3474 | _PyErr_Clear(tstate); |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3475 | // This attribute can be missing sometimes; |
| 3476 | // we don't want to optimize this lookup. |
| 3477 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3478 | Py_DECREF(dict); |
| 3479 | } |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3480 | } |
| 3481 | else { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3482 | // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact. |
| 3483 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3484 | } |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3485 | } |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3486 | } |
| 3487 | else { |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3488 | // The type of the object has either been updated, |
| 3489 | // or is different. Maybe it will stabilize? |
| 3490 | OPCACHE_MAYBE_DEOPT_LOAD_ATTR(); |
| 3491 | } |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3492 | OPCACHE_STAT_ATTR_MISS(); |
| 3493 | } |
| 3494 | |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3495 | if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call. |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3496 | type->tp_getattro == PyObject_GenericGetAttr) |
| 3497 | { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3498 | if (type->tp_dict == NULL) { |
| 3499 | if (PyType_Ready(type) < 0) { |
| 3500 | Py_DECREF(owner); |
| 3501 | SET_TOP(NULL); |
| 3502 | goto error; |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3503 | } |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3504 | } |
| 3505 | PyObject *descr = _PyType_Lookup(type, name); |
| 3506 | if (descr != NULL) { |
| 3507 | // We found an attribute with a data-like descriptor. |
| 3508 | PyTypeObject *dtype = Py_TYPE(descr); |
| 3509 | if (dtype == &PyMemberDescr_Type) { // It's a slot |
| 3510 | PyMemberDescrObject *member = (PyMemberDescrObject *)descr; |
| 3511 | struct PyMemberDef *dmem = member->d_member; |
| 3512 | if (dmem->type == T_OBJECT_EX) { |
| 3513 | Py_ssize_t offset = dmem->offset; |
| 3514 | assert(offset > 0); // 0 would be confused with dict hint == -1 (miss). |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3515 | |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3516 | if (co_opcache->optimized == 0) { |
| 3517 | // First time we optimize this opcode. |
| 3518 | OPCACHE_STAT_ATTR_OPT(); |
| 3519 | co_opcache->optimized = OPCODE_CACHE_MAX_TRIES; |
| 3520 | // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset); |
| 3521 | } |
| 3522 | |
| 3523 | la = &co_opcache->u.la; |
| 3524 | la->type = type; |
| 3525 | la->tp_version_tag = type->tp_version_tag; |
| 3526 | la->hint = ~offset; |
| 3527 | |
| 3528 | char *addr = (char *)owner + offset; |
| 3529 | res = *(PyObject **)addr; |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3530 | if (res != NULL) { |
| 3531 | Py_INCREF(res); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3532 | Py_DECREF(owner); |
| 3533 | SET_TOP(res); |
| 3534 | |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3535 | DISPATCH(); |
| 3536 | } |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3537 | // Else slot is NULL. Fall through to slow path to raise AttributeError(name). |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3538 | } |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3539 | // Else it's a slot of a different type. We don't handle those. |
| 3540 | } |
| 3541 | // Else it's some other kind of descriptor that we don't handle. |
| 3542 | OPCACHE_DEOPT_LOAD_ATTR(); |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3543 | } |
| 3544 | else if (type->tp_dictoffset > 0) { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3545 | // We found an instance with a __dict__. |
| 3546 | dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset); |
| 3547 | dict = *dictptr; |
| 3548 | |
| 3549 | if (dict != NULL && PyDict_CheckExact(dict)) { |
| 3550 | Py_INCREF(dict); |
| 3551 | res = NULL; |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3552 | assert(!_PyErr_Occurred(tstate)); |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3553 | Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res); |
| 3554 | if (res != NULL) { |
| 3555 | Py_INCREF(res); |
| 3556 | Py_DECREF(dict); |
| 3557 | Py_DECREF(owner); |
| 3558 | SET_TOP(res); |
| 3559 | |
| 3560 | if (co_opcache->optimized == 0) { |
| 3561 | // First time we optimize this opcode. |
| 3562 | OPCACHE_STAT_ATTR_OPT(); |
| 3563 | co_opcache->optimized = OPCODE_CACHE_MAX_TRIES; |
| 3564 | } |
| 3565 | |
| 3566 | la = &co_opcache->u.la; |
| 3567 | la->type = type; |
| 3568 | la->tp_version_tag = type->tp_version_tag; |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3569 | assert(hint >= 0); |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3570 | la->hint = hint; |
| 3571 | |
| 3572 | DISPATCH(); |
| 3573 | } |
Victor Stinner | d5fc998 | 2021-02-21 12:02:04 +0100 | [diff] [blame] | 3574 | else { |
| 3575 | _PyErr_Clear(tstate); |
| 3576 | } |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3577 | Py_DECREF(dict); |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3578 | } else { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3579 | // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact. |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3580 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3581 | } |
| 3582 | } else { |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3583 | // The object's class does not have a tp_dictoffset we can use. |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3584 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3585 | } |
| 3586 | } else if (type->tp_getattro != PyObject_GenericGetAttr) { |
| 3587 | OPCACHE_DEOPT_LOAD_ATTR(); |
| 3588 | } |
| 3589 | } |
| 3590 | |
Guido van Rossum | 5c5a938 | 2021-01-29 18:02:29 -0800 | [diff] [blame] | 3591 | // Slow path. |
Pablo Galindo | 109826c | 2020-10-20 06:22:44 +0100 | [diff] [blame] | 3592 | res = PyObject_GetAttr(owner, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3593 | Py_DECREF(owner); |
| 3594 | SET_TOP(res); |
| 3595 | if (res == NULL) |
| 3596 | goto error; |
| 3597 | DISPATCH(); |
| 3598 | } |
| 3599 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3600 | case TARGET(COMPARE_OP): { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3601 | assert(oparg <= Py_GE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3602 | PyObject *right = POP(); |
| 3603 | PyObject *left = TOP(); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3604 | PyObject *res = PyObject_RichCompare(left, right, oparg); |
| 3605 | SET_TOP(res); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3606 | Py_DECREF(left); |
| 3607 | Py_DECREF(right); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3608 | if (res == NULL) |
| 3609 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3610 | PREDICT(POP_JUMP_IF_FALSE); |
| 3611 | PREDICT(POP_JUMP_IF_TRUE); |
| 3612 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3613 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3614 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3615 | case TARGET(IS_OP): { |
| 3616 | PyObject *right = POP(); |
| 3617 | PyObject *left = TOP(); |
| 3618 | int res = (left == right)^oparg; |
| 3619 | PyObject *b = res ? Py_True : Py_False; |
| 3620 | Py_INCREF(b); |
| 3621 | SET_TOP(b); |
| 3622 | Py_DECREF(left); |
| 3623 | Py_DECREF(right); |
| 3624 | PREDICT(POP_JUMP_IF_FALSE); |
| 3625 | PREDICT(POP_JUMP_IF_TRUE); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3626 | DISPATCH(); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | case TARGET(CONTAINS_OP): { |
| 3630 | PyObject *right = POP(); |
| 3631 | PyObject *left = POP(); |
| 3632 | int res = PySequence_Contains(right, left); |
| 3633 | Py_DECREF(left); |
| 3634 | Py_DECREF(right); |
| 3635 | if (res < 0) { |
| 3636 | goto error; |
| 3637 | } |
| 3638 | PyObject *b = (res^oparg) ? Py_True : Py_False; |
| 3639 | Py_INCREF(b); |
| 3640 | PUSH(b); |
| 3641 | PREDICT(POP_JUMP_IF_FALSE); |
| 3642 | PREDICT(POP_JUMP_IF_TRUE); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3643 | DISPATCH(); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
| 3647 | "BaseException is not allowed" |
| 3648 | |
| 3649 | case TARGET(JUMP_IF_NOT_EXC_MATCH): { |
| 3650 | PyObject *right = POP(); |
| 3651 | PyObject *left = POP(); |
| 3652 | if (PyTuple_Check(right)) { |
| 3653 | Py_ssize_t i, length; |
| 3654 | length = PyTuple_GET_SIZE(right); |
| 3655 | for (i = 0; i < length; i++) { |
| 3656 | PyObject *exc = PyTuple_GET_ITEM(right, i); |
| 3657 | if (!PyExceptionClass_Check(exc)) { |
| 3658 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3659 | CANNOT_CATCH_MSG); |
| 3660 | Py_DECREF(left); |
| 3661 | Py_DECREF(right); |
| 3662 | goto error; |
| 3663 | } |
| 3664 | } |
| 3665 | } |
| 3666 | else { |
| 3667 | if (!PyExceptionClass_Check(right)) { |
| 3668 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 3669 | CANNOT_CATCH_MSG); |
| 3670 | Py_DECREF(left); |
| 3671 | Py_DECREF(right); |
| 3672 | goto error; |
| 3673 | } |
| 3674 | } |
| 3675 | int res = PyErr_GivenExceptionMatches(left, right); |
| 3676 | Py_DECREF(left); |
| 3677 | Py_DECREF(right); |
| 3678 | if (res > 0) { |
| 3679 | /* Exception matches -- Do nothing */; |
| 3680 | } |
| 3681 | else if (res == 0) { |
| 3682 | JUMPTO(oparg); |
| 3683 | } |
| 3684 | else { |
| 3685 | goto error; |
| 3686 | } |
| 3687 | DISPATCH(); |
| 3688 | } |
| 3689 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3690 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3691 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 3692 | PyObject *fromlist = POP(); |
| 3693 | PyObject *level = TOP(); |
| 3694 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3695 | res = import_name(tstate, f, name, fromlist, level); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 3696 | Py_DECREF(level); |
| 3697 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3698 | SET_TOP(res); |
| 3699 | if (res == NULL) |
| 3700 | goto error; |
| 3701 | DISPATCH(); |
| 3702 | } |
| 3703 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3704 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3705 | PyObject *from = POP(), *locals; |
| 3706 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3707 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 3708 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3709 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3710 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 3711 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3712 | locals = f->f_locals; |
| 3713 | if (locals == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3714 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 3715 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 3716 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3717 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3719 | err = import_all_from(tstate, locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3721 | Py_DECREF(from); |
| 3722 | if (err != 0) |
| 3723 | goto error; |
| 3724 | DISPATCH(); |
| 3725 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3726 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3727 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3728 | PyObject *name = GETITEM(names, oparg); |
| 3729 | PyObject *from = TOP(); |
| 3730 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 3731 | res = import_from(tstate, from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3732 | PUSH(res); |
| 3733 | if (res == NULL) |
| 3734 | goto error; |
| 3735 | DISPATCH(); |
| 3736 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3737 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3738 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | JUMPBY(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3740 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3741 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3742 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3743 | case TARGET(POP_JUMP_IF_FALSE): { |
| 3744 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3745 | PyObject *cond = POP(); |
| 3746 | int err; |
| 3747 | if (cond == Py_True) { |
| 3748 | Py_DECREF(cond); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3749 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3751 | if (cond == Py_False) { |
| 3752 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | JUMPTO(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3754 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3756 | err = PyObject_IsTrue(cond); |
| 3757 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 3759 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | else if (err == 0) |
| 3761 | JUMPTO(oparg); |
| 3762 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3763 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3764 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3765 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3766 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3767 | case TARGET(POP_JUMP_IF_TRUE): { |
| 3768 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3769 | PyObject *cond = POP(); |
| 3770 | int err; |
| 3771 | if (cond == Py_False) { |
| 3772 | Py_DECREF(cond); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3773 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3775 | if (cond == Py_True) { |
| 3776 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3777 | JUMPTO(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3778 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3779 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3780 | err = PyObject_IsTrue(cond); |
| 3781 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | JUMPTO(oparg); |
| 3784 | } |
| 3785 | else if (err == 0) |
| 3786 | ; |
| 3787 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3788 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3789 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3790 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3791 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3792 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3793 | PyObject *cond = TOP(); |
| 3794 | int err; |
| 3795 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3796 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3797 | Py_DECREF(cond); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3798 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3799 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3800 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3801 | JUMPTO(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3802 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3804 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3806 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3807 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | } |
| 3809 | else if (err == 0) |
| 3810 | JUMPTO(oparg); |
| 3811 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3812 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3814 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3815 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3816 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3817 | PyObject *cond = TOP(); |
| 3818 | int err; |
| 3819 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3820 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3821 | Py_DECREF(cond); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3822 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3824 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | JUMPTO(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3826 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3827 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3828 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3830 | JUMPTO(oparg); |
| 3831 | } |
| 3832 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3833 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3834 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | } |
| 3836 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3837 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3839 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3840 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3841 | case TARGET(JUMP_ABSOLUTE): { |
| 3842 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3843 | JUMPTO(oparg); |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 3844 | CHECK_EVAL_BREAKER(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3845 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3846 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3847 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3848 | case TARGET(GET_LEN): { |
| 3849 | // PUSH(len(TOS)) |
| 3850 | Py_ssize_t len_i = PyObject_Length(TOP()); |
| 3851 | if (len_i < 0) { |
| 3852 | goto error; |
| 3853 | } |
| 3854 | PyObject *len_o = PyLong_FromSsize_t(len_i); |
| 3855 | if (len_o == NULL) { |
| 3856 | goto error; |
| 3857 | } |
| 3858 | PUSH(len_o); |
| 3859 | DISPATCH(); |
| 3860 | } |
| 3861 | |
| 3862 | case TARGET(MATCH_CLASS): { |
| 3863 | // Pop TOS. On success, set TOS to True and TOS1 to a tuple of |
| 3864 | // attributes. On failure, set TOS to False. |
| 3865 | PyObject *names = POP(); |
| 3866 | PyObject *type = TOP(); |
| 3867 | PyObject *subject = SECOND(); |
| 3868 | assert(PyTuple_CheckExact(names)); |
| 3869 | PyObject *attrs = match_class(tstate, subject, type, oparg, names); |
| 3870 | Py_DECREF(names); |
| 3871 | if (attrs) { |
| 3872 | // Success! |
| 3873 | assert(PyTuple_CheckExact(attrs)); |
| 3874 | Py_DECREF(subject); |
| 3875 | SET_SECOND(attrs); |
| 3876 | } |
| 3877 | else if (_PyErr_Occurred(tstate)) { |
| 3878 | goto error; |
| 3879 | } |
| 3880 | Py_DECREF(type); |
| 3881 | SET_TOP(PyBool_FromLong(!!attrs)); |
| 3882 | DISPATCH(); |
| 3883 | } |
| 3884 | |
| 3885 | case TARGET(MATCH_MAPPING): { |
| 3886 | // PUSH(isinstance(TOS, _collections_abc.Mapping)) |
| 3887 | PyObject *subject = TOP(); |
| 3888 | // Fast path for dicts: |
| 3889 | if (PyDict_Check(subject)) { |
| 3890 | Py_INCREF(Py_True); |
| 3891 | PUSH(Py_True); |
| 3892 | DISPATCH(); |
| 3893 | } |
| 3894 | // Lazily import _collections_abc.Mapping, and keep it handy on the |
| 3895 | // PyInterpreterState struct (it gets cleaned up at exit): |
| 3896 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 3897 | if (interp->map_abc == NULL) { |
| 3898 | PyObject *abc = PyImport_ImportModule("_collections_abc"); |
| 3899 | if (abc == NULL) { |
| 3900 | goto error; |
| 3901 | } |
| 3902 | interp->map_abc = PyObject_GetAttrString(abc, "Mapping"); |
| 3903 | if (interp->map_abc == NULL) { |
| 3904 | goto error; |
| 3905 | } |
| 3906 | } |
| 3907 | int match = PyObject_IsInstance(subject, interp->map_abc); |
| 3908 | if (match < 0) { |
| 3909 | goto error; |
| 3910 | } |
| 3911 | PUSH(PyBool_FromLong(match)); |
| 3912 | DISPATCH(); |
| 3913 | } |
| 3914 | |
| 3915 | case TARGET(MATCH_SEQUENCE): { |
| 3916 | // PUSH(not isinstance(TOS, (bytearray, bytes, str)) |
| 3917 | // and isinstance(TOS, _collections_abc.Sequence)) |
| 3918 | PyObject *subject = TOP(); |
| 3919 | // Fast path for lists and tuples: |
| 3920 | if (PyType_FastSubclass(Py_TYPE(subject), |
| 3921 | Py_TPFLAGS_LIST_SUBCLASS | |
| 3922 | Py_TPFLAGS_TUPLE_SUBCLASS)) |
| 3923 | { |
| 3924 | Py_INCREF(Py_True); |
| 3925 | PUSH(Py_True); |
| 3926 | DISPATCH(); |
| 3927 | } |
| 3928 | // Bail on some possible Sequences that we intentionally exclude: |
| 3929 | if (PyType_FastSubclass(Py_TYPE(subject), |
| 3930 | Py_TPFLAGS_BYTES_SUBCLASS | |
| 3931 | Py_TPFLAGS_UNICODE_SUBCLASS) || |
| 3932 | PyByteArray_Check(subject)) |
| 3933 | { |
| 3934 | Py_INCREF(Py_False); |
| 3935 | PUSH(Py_False); |
| 3936 | DISPATCH(); |
| 3937 | } |
| 3938 | // Lazily import _collections_abc.Sequence, and keep it handy on the |
| 3939 | // PyInterpreterState struct (it gets cleaned up at exit): |
| 3940 | PyInterpreterState *interp = PyInterpreterState_Get(); |
| 3941 | if (interp->seq_abc == NULL) { |
| 3942 | PyObject *abc = PyImport_ImportModule("_collections_abc"); |
| 3943 | if (abc == NULL) { |
| 3944 | goto error; |
| 3945 | } |
| 3946 | interp->seq_abc = PyObject_GetAttrString(abc, "Sequence"); |
| 3947 | if (interp->seq_abc == NULL) { |
| 3948 | goto error; |
| 3949 | } |
| 3950 | } |
| 3951 | int match = PyObject_IsInstance(subject, interp->seq_abc); |
| 3952 | if (match < 0) { |
| 3953 | goto error; |
| 3954 | } |
| 3955 | PUSH(PyBool_FromLong(match)); |
| 3956 | DISPATCH(); |
| 3957 | } |
| 3958 | |
| 3959 | case TARGET(MATCH_KEYS): { |
| 3960 | // On successful match for all keys, PUSH(values) and PUSH(True). |
| 3961 | // Otherwise, PUSH(None) and PUSH(False). |
| 3962 | PyObject *keys = TOP(); |
| 3963 | PyObject *subject = SECOND(); |
| 3964 | PyObject *values_or_none = match_keys(tstate, subject, keys); |
| 3965 | if (values_or_none == NULL) { |
| 3966 | goto error; |
| 3967 | } |
| 3968 | PUSH(values_or_none); |
| 3969 | if (values_or_none == Py_None) { |
| 3970 | Py_INCREF(Py_False); |
| 3971 | PUSH(Py_False); |
| 3972 | DISPATCH(); |
| 3973 | } |
| 3974 | assert(PyTuple_CheckExact(values_or_none)); |
| 3975 | Py_INCREF(Py_True); |
| 3976 | PUSH(Py_True); |
| 3977 | DISPATCH(); |
| 3978 | } |
| 3979 | |
| 3980 | case TARGET(COPY_DICT_WITHOUT_KEYS): { |
| 3981 | // rest = dict(TOS1) |
| 3982 | // for key in TOS: |
| 3983 | // del rest[key] |
| 3984 | // SET_TOP(rest) |
| 3985 | PyObject *keys = TOP(); |
| 3986 | PyObject *subject = SECOND(); |
| 3987 | PyObject *rest = PyDict_New(); |
| 3988 | if (rest == NULL || PyDict_Update(rest, subject)) { |
| 3989 | Py_XDECREF(rest); |
| 3990 | goto error; |
| 3991 | } |
| 3992 | // This may seem a bit inefficient, but keys is rarely big enough to |
| 3993 | // actually impact runtime. |
| 3994 | assert(PyTuple_CheckExact(keys)); |
| 3995 | for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) { |
| 3996 | if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) { |
| 3997 | Py_DECREF(rest); |
| 3998 | goto error; |
| 3999 | } |
| 4000 | } |
| 4001 | Py_DECREF(keys); |
| 4002 | SET_TOP(rest); |
| 4003 | DISPATCH(); |
| 4004 | } |
| 4005 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4006 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4007 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4008 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4009 | PyObject *iter = PyObject_GetIter(iterable); |
| 4010 | Py_DECREF(iterable); |
| 4011 | SET_TOP(iter); |
| 4012 | if (iter == NULL) |
| 4013 | goto error; |
| 4014 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 4015 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4016 | DISPATCH(); |
| 4017 | } |
| 4018 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4019 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4020 | /* before: [obj]; after [getiter(obj)] */ |
| 4021 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4022 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4023 | if (PyCoro_CheckExact(iterable)) { |
| 4024 | /* `iterable` is a coroutine */ |
| 4025 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 4026 | /* and it is used in a 'yield from' expression of a |
| 4027 | regular generator. */ |
| 4028 | Py_DECREF(iterable); |
| 4029 | SET_TOP(NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4030 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 4031 | "cannot 'yield from' a coroutine object " |
| 4032 | "in a non-coroutine generator"); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4033 | goto error; |
| 4034 | } |
| 4035 | } |
| 4036 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4037 | /* `iterable` is not a generator. */ |
| 4038 | iter = PyObject_GetIter(iterable); |
| 4039 | Py_DECREF(iterable); |
| 4040 | SET_TOP(iter); |
| 4041 | if (iter == NULL) |
| 4042 | goto error; |
| 4043 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 4044 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4045 | DISPATCH(); |
| 4046 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 4047 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4048 | case TARGET(FOR_ITER): { |
| 4049 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4050 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4051 | PyObject *iter = TOP(); |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4052 | PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4053 | if (next != NULL) { |
| 4054 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4055 | PREDICT(STORE_FAST); |
| 4056 | PREDICT(UNPACK_SEQUENCE); |
| 4057 | DISPATCH(); |
| 4058 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4059 | if (_PyErr_Occurred(tstate)) { |
| 4060 | if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4061 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4062 | } |
| 4063 | else if (tstate->c_tracefunc != NULL) { |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4064 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4065 | } |
| 4066 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | } |
| 4068 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 4069 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4070 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | JUMPBY(oparg); |
| 4072 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4073 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 4074 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4075 | case TARGET(SETUP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4076 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | STACK_LEVEL()); |
| 4078 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4079 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4080 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4081 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4082 | _Py_IDENTIFIER(__aenter__); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 4083 | _Py_IDENTIFIER(__aexit__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4084 | PyObject *mgr = TOP(); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 4085 | PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4086 | PyObject *res; |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 4087 | if (enter == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4088 | goto error; |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 4089 | } |
| 4090 | PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__); |
| 4091 | if (exit == NULL) { |
| 4092 | Py_DECREF(enter); |
| 4093 | goto error; |
| 4094 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4095 | SET_TOP(exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4096 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 4097 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4098 | Py_DECREF(enter); |
| 4099 | if (res == NULL) |
| 4100 | goto error; |
| 4101 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 4102 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4103 | DISPATCH(); |
| 4104 | } |
| 4105 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4106 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4107 | PyObject *res = POP(); |
| 4108 | /* Setup the finally block before pushing the result |
| 4109 | of __aenter__ on the stack. */ |
| 4110 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 4111 | STACK_LEVEL()); |
| 4112 | PUSH(res); |
| 4113 | DISPATCH(); |
| 4114 | } |
| 4115 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4116 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4117 | _Py_IDENTIFIER(__enter__); |
Géry Ogam | 1d1b97a | 2020-01-14 12:58:29 +0100 | [diff] [blame] | 4118 | _Py_IDENTIFIER(__exit__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4119 | PyObject *mgr = TOP(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4120 | PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4121 | PyObject *res; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4122 | if (enter == NULL) { |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 4123 | goto error; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4124 | } |
| 4125 | PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 4126 | if (exit == NULL) { |
| 4127 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4128 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 4129 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4130 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4131 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 4132 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4133 | Py_DECREF(enter); |
| 4134 | if (res == NULL) |
| 4135 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4136 | /* Setup the finally block before pushing the result |
| 4137 | of __enter__ on the stack. */ |
| 4138 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 4139 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4140 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4141 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | DISPATCH(); |
| 4143 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4144 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4145 | case TARGET(WITH_EXCEPT_START): { |
| 4146 | /* At the top of the stack are 7 values: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | - (TOP, SECOND, THIRD) = exc_info() |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4148 | - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 4149 | - SEVENTH: the context.__exit__ bound method |
| 4150 | We call SEVENTH(TOP, SECOND, THIRD). |
| 4151 | Then we push again the TOP exception and the __exit__ |
| 4152 | return value. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4153 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4154 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 4155 | PyObject *exc, *val, *tb, *res; |
| 4156 | |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 4157 | exc = TOP(); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4158 | val = SECOND(); |
| 4159 | tb = THIRD(); |
| 4160 | assert(exc != Py_None); |
| 4161 | assert(!PyLong_Check(exc)); |
| 4162 | exit_func = PEEK(7); |
Jeroen Demeyer | 469d1a7 | 2019-07-03 12:52:21 +0200 | [diff] [blame] | 4163 | PyObject *stack[4] = {NULL, exc, val, tb}; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4164 | res = PyObject_Vectorcall(exit_func, stack + 1, |
Jeroen Demeyer | 469d1a7 | 2019-07-03 12:52:21 +0200 | [diff] [blame] | 4165 | 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4166 | if (res == NULL) |
| 4167 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 4168 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4169 | PUSH(res); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4170 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4172 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4173 | case TARGET(LOAD_METHOD): { |
Andrey | b021ba5 | 2019-04-29 14:33:26 +1000 | [diff] [blame] | 4174 | /* Designed to work in tandem with CALL_METHOD. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4175 | PyObject *name = GETITEM(names, oparg); |
| 4176 | PyObject *obj = TOP(); |
| 4177 | PyObject *meth = NULL; |
| 4178 | |
| 4179 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 4180 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4181 | if (meth == NULL) { |
| 4182 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4183 | goto error; |
| 4184 | } |
| 4185 | |
| 4186 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4187 | /* We can bypass temporary bound method object. |
| 4188 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 4189 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4190 | meth | self | arg1 | ... | argN |
| 4191 | */ |
| 4192 | SET_TOP(meth); |
| 4193 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4194 | } |
| 4195 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4196 | /* meth is not an unbound method (but a regular attr, or |
| 4197 | something was returned by a descriptor protocol). Set |
| 4198 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4199 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4200 | |
| 4201 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4202 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4203 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4204 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4205 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4206 | } |
| 4207 | DISPATCH(); |
| 4208 | } |
| 4209 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4210 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4211 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4212 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4213 | |
| 4214 | sp = stack_pointer; |
| 4215 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4216 | meth = PEEK(oparg + 2); |
| 4217 | if (meth == NULL) { |
| 4218 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 4219 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4220 | |
| 4221 | Stack layout: |
| 4222 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4223 | ... | NULL | callable | arg1 | ... | argN |
| 4224 | ^- TOP() |
| 4225 | ^- (-oparg) |
| 4226 | ^- (-oparg-1) |
| 4227 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4228 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 4229 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4230 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4231 | */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4232 | res = call_function(tstate, &trace_info, &sp, oparg, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4233 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4234 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4235 | } |
| 4236 | else { |
| 4237 | /* This is a method call. Stack layout: |
| 4238 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4239 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4240 | ^- TOP() |
| 4241 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4242 | ^- (-oparg-1) |
| 4243 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4244 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4245 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4246 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 4247 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4248 | */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4249 | res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4250 | stack_pointer = sp; |
| 4251 | } |
| 4252 | |
| 4253 | PUSH(res); |
| 4254 | if (res == NULL) |
| 4255 | goto error; |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 4256 | CHECK_EVAL_BREAKER(); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4257 | DISPATCH(); |
| 4258 | } |
| 4259 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4260 | case TARGET(CALL_FUNCTION): { |
| 4261 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4262 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | sp = stack_pointer; |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4264 | res = call_function(tstate, &trace_info, &sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4265 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4266 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4267 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4268 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4269 | } |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 4270 | CHECK_EVAL_BREAKER(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4271 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4273 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4274 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4275 | PyObject **sp, *res, *names; |
| 4276 | |
| 4277 | names = POP(); |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 4278 | assert(PyTuple_Check(names)); |
| 4279 | assert(PyTuple_GET_SIZE(names) <= oparg); |
| 4280 | /* We assume without checking that names contains only strings */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | sp = stack_pointer; |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4282 | res = call_function(tstate, &trace_info, &sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4284 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4285 | Py_DECREF(names); |
| 4286 | |
| 4287 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4288 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4289 | } |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 4290 | CHECK_EVAL_BREAKER(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4291 | DISPATCH(); |
| 4292 | } |
| 4293 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4294 | case TARGET(CALL_FUNCTION_EX): { |
Brandt Bucher | f185a73 | 2019-09-28 17:12:49 -0700 | [diff] [blame] | 4295 | PREDICTED(CALL_FUNCTION_EX); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4296 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4297 | if (oparg & 0x01) { |
| 4298 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4299 | if (!PyDict_CheckExact(kwargs)) { |
| 4300 | PyObject *d = PyDict_New(); |
| 4301 | if (d == NULL) |
| 4302 | goto error; |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 4303 | if (_PyDict_MergeEx(d, kwargs, 2) < 0) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4304 | Py_DECREF(d); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4305 | format_kwargs_error(tstate, SECOND(), kwargs); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 4306 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4307 | goto error; |
| 4308 | } |
| 4309 | Py_DECREF(kwargs); |
| 4310 | kwargs = d; |
| 4311 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4312 | assert(PyDict_CheckExact(kwargs)); |
| 4313 | } |
| 4314 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4315 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 4316 | if (!PyTuple_CheckExact(callargs)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4317 | if (check_args_iterable(tstate, func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 4318 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4319 | goto error; |
| 4320 | } |
| 4321 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 4322 | if (callargs == NULL) { |
| 4323 | goto error; |
| 4324 | } |
| 4325 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 4326 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4327 | |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4328 | result = do_call_core(tstate, &trace_info, func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4329 | Py_DECREF(func); |
| 4330 | Py_DECREF(callargs); |
| 4331 | Py_XDECREF(kwargs); |
| 4332 | |
| 4333 | SET_TOP(result); |
| 4334 | if (result == NULL) { |
| 4335 | goto error; |
| 4336 | } |
Mark Shannon | 4958f5d | 2021-03-24 17:56:12 +0000 | [diff] [blame] | 4337 | CHECK_EVAL_BREAKER(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4338 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4340 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4341 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4342 | PyObject *qualname = POP(); |
| 4343 | PyObject *codeobj = POP(); |
| 4344 | PyFunctionObject *func = (PyFunctionObject *) |
| 4345 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 4346 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4347 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4348 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4349 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4350 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4351 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4352 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4353 | if (oparg & 0x08) { |
| 4354 | assert(PyTuple_CheckExact(TOP())); |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4355 | func->func_closure = POP(); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4356 | } |
| 4357 | if (oparg & 0x04) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 4358 | assert(PyTuple_CheckExact(TOP())); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4359 | func->func_annotations = POP(); |
| 4360 | } |
| 4361 | if (oparg & 0x02) { |
| 4362 | assert(PyDict_CheckExact(TOP())); |
| 4363 | func->func_kwdefaults = POP(); |
| 4364 | } |
| 4365 | if (oparg & 0x01) { |
| 4366 | assert(PyTuple_CheckExact(TOP())); |
| 4367 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 4369 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 4370 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4371 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 4373 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4374 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4375 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4377 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4379 | step = NULL; |
| 4380 | stop = POP(); |
| 4381 | start = TOP(); |
| 4382 | slice = PySlice_New(start, stop, step); |
| 4383 | Py_DECREF(start); |
| 4384 | Py_DECREF(stop); |
| 4385 | Py_XDECREF(step); |
| 4386 | SET_TOP(slice); |
| 4387 | if (slice == NULL) |
| 4388 | goto error; |
| 4389 | DISPATCH(); |
| 4390 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 4391 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4392 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4393 | /* Handles f-string value formatting. */ |
| 4394 | PyObject *result; |
| 4395 | PyObject *fmt_spec; |
| 4396 | PyObject *value; |
| 4397 | PyObject *(*conv_fn)(PyObject *); |
| 4398 | int which_conversion = oparg & FVC_MASK; |
| 4399 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 4400 | |
| 4401 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 4402 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4403 | |
| 4404 | /* See if any conversion is specified. */ |
| 4405 | switch (which_conversion) { |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4406 | case FVC_NONE: conv_fn = NULL; break; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4407 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 4408 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 4409 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4410 | default: |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4411 | _PyErr_Format(tstate, PyExc_SystemError, |
| 4412 | "unexpected conversion flag %d", |
| 4413 | which_conversion); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4414 | goto error; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | /* If there's a conversion function, call it and replace |
| 4418 | value with that result. Otherwise, just use value, |
| 4419 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 4420 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4421 | result = conv_fn(value); |
| 4422 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 4423 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4424 | Py_XDECREF(fmt_spec); |
| 4425 | goto error; |
| 4426 | } |
| 4427 | value = result; |
| 4428 | } |
| 4429 | |
| 4430 | /* If value is a unicode object, and there's no fmt_spec, |
| 4431 | then we know the result of format(value) is value |
| 4432 | itself. In that case, skip calling format(). I plan to |
| 4433 | move this optimization in to PyObject_Format() |
| 4434 | itself. */ |
| 4435 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 4436 | /* Do nothing, just transfer ownership to result. */ |
| 4437 | result = value; |
| 4438 | } else { |
| 4439 | /* Actually call format(). */ |
| 4440 | result = PyObject_Format(value, fmt_spec); |
| 4441 | Py_DECREF(value); |
| 4442 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 4443 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4444 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 4445 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4446 | } |
| 4447 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 4448 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4449 | DISPATCH(); |
| 4450 | } |
| 4451 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 4452 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 4453 | int oldoparg = oparg; |
| 4454 | NEXTOPARG(); |
| 4455 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4456 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4457 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 4458 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4459 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 4460 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 4462 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | default: |
| 4464 | fprintf(stderr, |
| 4465 | "XXX lineno: %d, opcode: %d\n", |
| 4466 | PyFrame_GetLineNumber(f), |
| 4467 | opcode); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4468 | _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4469 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 4470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4472 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4473 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 4474 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 4475 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4476 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4477 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4478 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 4479 | #ifdef NDEBUG |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4480 | if (!_PyErr_Occurred(tstate)) { |
| 4481 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 4482 | "error return without exception set"); |
| 4483 | } |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 4484 | #else |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4485 | assert(_PyErr_Occurred(tstate)); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 4486 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4487 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4488 | /* Log traceback info. */ |
| 4489 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4490 | |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4491 | if (tstate->c_tracefunc != NULL) { |
| 4492 | /* Make sure state is set to FRAME_EXECUTING for tracing */ |
| 4493 | assert(f->f_state == FRAME_EXECUTING); |
| 4494 | f->f_state = FRAME_UNWINDING; |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4495 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4496 | tstate, f, &trace_info); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4497 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4498 | exception_unwind: |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4499 | f->f_state = FRAME_UNWINDING; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4500 | /* Unwind stacks if an exception occurred */ |
| 4501 | while (f->f_iblock > 0) { |
| 4502 | /* Pop the current block. */ |
| 4503 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | if (b->b_type == EXCEPT_HANDLER) { |
| 4506 | UNWIND_EXCEPT_HANDLER(b); |
| 4507 | continue; |
| 4508 | } |
| 4509 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4510 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | PyObject *exc, *val, *tb; |
| 4512 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4513 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | /* Beware, this invalidates all b->b_* fields */ |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4515 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4516 | PUSH(exc_info->exc_traceback); |
| 4517 | PUSH(exc_info->exc_value); |
| 4518 | if (exc_info->exc_type != NULL) { |
| 4519 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | } |
| 4521 | else { |
| 4522 | Py_INCREF(Py_None); |
| 4523 | PUSH(Py_None); |
| 4524 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4525 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | /* Make the raw exception data |
| 4527 | available to the handler, |
| 4528 | so a program can emulate the |
| 4529 | Python main loop. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4530 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 4531 | if (tb != NULL) |
| 4532 | PyException_SetTraceback(val, tb); |
| 4533 | else |
| 4534 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4536 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4537 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4538 | exc_info->exc_value = val; |
| 4539 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | if (tb == NULL) |
| 4541 | tb = Py_None; |
| 4542 | Py_INCREF(tb); |
| 4543 | PUSH(tb); |
| 4544 | PUSH(val); |
| 4545 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | JUMPTO(handler); |
Victor Stinner | dab8423 | 2020-03-17 18:56:44 +0100 | [diff] [blame] | 4547 | if (_Py_TracingPossible(ceval2)) { |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4548 | trace_info.instr_prev = INT_MAX; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4549 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4550 | /* Resume normal execution */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4551 | f->f_state = FRAME_EXECUTING; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4552 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | } |
| 4554 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4555 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4556 | /* End the loop as we still have an error */ |
| 4557 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4559 | |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 4560 | assert(retval == NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4561 | assert(_PyErr_Occurred(tstate)); |
Pablo Galindo | f00828a | 2019-05-09 16:52:02 +0100 | [diff] [blame] | 4562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | /* Pop remaining stack entries. */ |
| 4564 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4565 | PyObject *o = POP(); |
| 4566 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 4568 | f->f_stackdepth = 0; |
| 4569 | f->f_state = FRAME_RAISED; |
Mark Shannon | e7c9f4a | 2020-01-13 12:51:26 +0000 | [diff] [blame] | 4570 | exiting: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4571 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 4572 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4573 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4574 | tstate, f, &trace_info, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4575 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | } |
| 4577 | } |
| 4578 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4579 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 4580 | tstate, f, &trace_info, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4581 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 4585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4587 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 4588 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 4589 | dtrace_function_return(f); |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 4590 | _Py_LeaveRecursiveCall(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4592 | |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 4593 | return _Py_CheckFunctionResult(tstate, NULL, retval, __func__); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4596 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4597 | format_missing(PyThreadState *tstate, const char *kind, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4598 | PyCodeObject *co, PyObject *names, PyObject *qualname) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4599 | { |
| 4600 | int err; |
| 4601 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 4602 | PyObject *name_str, *comma, *tail, *tmp; |
| 4603 | |
| 4604 | assert(PyList_CheckExact(names)); |
| 4605 | assert(len >= 1); |
| 4606 | /* Deal with the joys of natural language. */ |
| 4607 | switch (len) { |
| 4608 | case 1: |
| 4609 | name_str = PyList_GET_ITEM(names, 0); |
| 4610 | Py_INCREF(name_str); |
| 4611 | break; |
| 4612 | case 2: |
| 4613 | name_str = PyUnicode_FromFormat("%U and %U", |
| 4614 | PyList_GET_ITEM(names, len - 2), |
| 4615 | PyList_GET_ITEM(names, len - 1)); |
| 4616 | break; |
| 4617 | default: |
| 4618 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 4619 | PyList_GET_ITEM(names, len - 2), |
| 4620 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 4621 | if (tail == NULL) |
| 4622 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4623 | /* Chop off the last two objects in the list. This shouldn't actually |
| 4624 | fail, but we can't be too careful. */ |
| 4625 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 4626 | if (err == -1) { |
| 4627 | Py_DECREF(tail); |
| 4628 | return; |
| 4629 | } |
| 4630 | /* Stitch everything up into a nice comma-separated list. */ |
| 4631 | comma = PyUnicode_FromString(", "); |
| 4632 | if (comma == NULL) { |
| 4633 | Py_DECREF(tail); |
| 4634 | return; |
| 4635 | } |
| 4636 | tmp = PyUnicode_Join(comma, names); |
| 4637 | Py_DECREF(comma); |
| 4638 | if (tmp == NULL) { |
| 4639 | Py_DECREF(tail); |
| 4640 | return; |
| 4641 | } |
| 4642 | name_str = PyUnicode_Concat(tmp, tail); |
| 4643 | Py_DECREF(tmp); |
| 4644 | Py_DECREF(tail); |
| 4645 | break; |
| 4646 | } |
| 4647 | if (name_str == NULL) |
| 4648 | return; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4649 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4650 | "%U() missing %i required %s argument%s: %U", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4651 | qualname, |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4652 | len, |
| 4653 | kind, |
| 4654 | len == 1 ? "" : "s", |
| 4655 | name_str); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4656 | Py_DECREF(name_str); |
| 4657 | } |
| 4658 | |
| 4659 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4660 | missing_arguments(PyThreadState *tstate, PyCodeObject *co, |
| 4661 | Py_ssize_t missing, Py_ssize_t defcount, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4662 | PyObject **fastlocals, PyObject *qualname) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4663 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4664 | Py_ssize_t i, j = 0; |
| 4665 | Py_ssize_t start, end; |
| 4666 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4667 | const char *kind = positional ? "positional" : "keyword-only"; |
| 4668 | PyObject *missing_names; |
| 4669 | |
| 4670 | /* Compute the names of the arguments that are missing. */ |
| 4671 | missing_names = PyList_New(missing); |
| 4672 | if (missing_names == NULL) |
| 4673 | return; |
| 4674 | if (positional) { |
| 4675 | start = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4676 | end = co->co_argcount - defcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4677 | } |
| 4678 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4679 | start = co->co_argcount; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4680 | end = start + co->co_kwonlyargcount; |
| 4681 | } |
| 4682 | for (i = start; i < end; i++) { |
| 4683 | if (GETLOCAL(i) == NULL) { |
| 4684 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 4685 | PyObject *name = PyObject_Repr(raw); |
| 4686 | if (name == NULL) { |
| 4687 | Py_DECREF(missing_names); |
| 4688 | return; |
| 4689 | } |
| 4690 | PyList_SET_ITEM(missing_names, j++, name); |
| 4691 | } |
| 4692 | } |
| 4693 | assert(j == missing); |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4694 | format_missing(tstate, kind, co, missing_names, qualname); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4695 | Py_DECREF(missing_names); |
| 4696 | } |
| 4697 | |
| 4698 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4699 | too_many_positional(PyThreadState *tstate, PyCodeObject *co, |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4700 | Py_ssize_t given, PyObject *defaults, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4701 | PyObject **fastlocals, PyObject *qualname) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4702 | { |
| 4703 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4704 | Py_ssize_t kwonly_given = 0; |
| 4705 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4706 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4707 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4708 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4709 | assert((co->co_flags & CO_VARARGS) == 0); |
| 4710 | /* Count missing keyword-only args. */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4711 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4712 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4713 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4714 | } |
| 4715 | } |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4716 | Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4717 | if (defcount) { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4718 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4719 | plural = 1; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4720 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4721 | } |
| 4722 | else { |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4723 | plural = (co_argcount != 1); |
| 4724 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4725 | } |
| 4726 | if (sig == NULL) |
| 4727 | return; |
| 4728 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4729 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 4730 | kwonly_sig = PyUnicode_FromFormat(format, |
| 4731 | given != 1 ? "s" : "", |
| 4732 | kwonly_given, |
| 4733 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4734 | if (kwonly_sig == NULL) { |
| 4735 | Py_DECREF(sig); |
| 4736 | return; |
| 4737 | } |
| 4738 | } |
| 4739 | else { |
| 4740 | /* This will not fail. */ |
| 4741 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4742 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4743 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4744 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4745 | "%U() takes %U positional argument%s but %zd%U %s given", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4746 | qualname, |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4747 | sig, |
| 4748 | plural ? "s" : "", |
| 4749 | given, |
| 4750 | kwonly_sig, |
| 4751 | given == 1 && !kwonly_given ? "was" : "were"); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4752 | Py_DECREF(sig); |
| 4753 | Py_DECREF(kwonly_sig); |
| 4754 | } |
| 4755 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4756 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4757 | positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co, |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4758 | Py_ssize_t kwcount, PyObject* kwnames, |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4759 | PyObject *qualname) |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4760 | { |
| 4761 | int posonly_conflicts = 0; |
| 4762 | PyObject* posonly_names = PyList_New(0); |
| 4763 | |
| 4764 | for(int k=0; k < co->co_posonlyargcount; k++){ |
| 4765 | PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k); |
| 4766 | |
| 4767 | for (int k2=0; k2<kwcount; k2++){ |
| 4768 | /* Compare the pointers first and fallback to PyObject_RichCompareBool*/ |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4769 | PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4770 | if (kwname == posonly_name){ |
| 4771 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 4772 | goto fail; |
| 4773 | } |
| 4774 | posonly_conflicts++; |
| 4775 | continue; |
| 4776 | } |
| 4777 | |
| 4778 | int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ); |
| 4779 | |
| 4780 | if ( cmp > 0) { |
| 4781 | if(PyList_Append(posonly_names, kwname) != 0) { |
| 4782 | goto fail; |
| 4783 | } |
| 4784 | posonly_conflicts++; |
| 4785 | } else if (cmp < 0) { |
| 4786 | goto fail; |
| 4787 | } |
| 4788 | |
| 4789 | } |
| 4790 | } |
| 4791 | if (posonly_conflicts) { |
| 4792 | PyObject* comma = PyUnicode_FromString(", "); |
| 4793 | if (comma == NULL) { |
| 4794 | goto fail; |
| 4795 | } |
| 4796 | PyObject* error_names = PyUnicode_Join(comma, posonly_names); |
| 4797 | Py_DECREF(comma); |
| 4798 | if (error_names == NULL) { |
| 4799 | goto fail; |
| 4800 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 4801 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4802 | "%U() got some positional-only arguments passed" |
| 4803 | " as keyword arguments: '%U'", |
Dennis Sweeney | b5cc208 | 2020-05-22 16:40:17 -0400 | [diff] [blame] | 4804 | qualname, error_names); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4805 | Py_DECREF(error_names); |
| 4806 | goto fail; |
| 4807 | } |
| 4808 | |
| 4809 | Py_DECREF(posonly_names); |
| 4810 | return 0; |
| 4811 | |
| 4812 | fail: |
| 4813 | Py_XDECREF(posonly_names); |
| 4814 | return 1; |
| 4815 | |
| 4816 | } |
| 4817 | |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 4818 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4819 | PyFrameObject * |
| 4820 | _PyEval_MakeFrameVector(PyThreadState *tstate, |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4821 | PyFrameConstructor *con, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4822 | PyObject *const *args, Py_ssize_t argcount, |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4823 | PyObject *kwnames) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4824 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 4825 | assert(is_tstate_valid(tstate)); |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 4826 | |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4827 | PyCodeObject *co = (PyCodeObject*)con->fc_code; |
| 4828 | assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults)); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4829 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4830 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4831 | /* Create the frame */ |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4832 | PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4833 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4834 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4835 | } |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4836 | PyObject **fastlocals = f->f_localsplus; |
| 4837 | PyObject **freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4838 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4839 | /* Create a dictionary for keyword parameters (**kwags) */ |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4840 | PyObject *kwdict; |
| 4841 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4842 | if (co->co_flags & CO_VARKEYWORDS) { |
| 4843 | kwdict = PyDict_New(); |
| 4844 | if (kwdict == NULL) |
| 4845 | goto fail; |
| 4846 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4847 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4848 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4849 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4850 | SETLOCAL(i, kwdict); |
| 4851 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4852 | else { |
| 4853 | kwdict = NULL; |
| 4854 | } |
| 4855 | |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4856 | /* Copy all positional arguments into local variables */ |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4857 | Py_ssize_t j, n; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4858 | if (argcount > co->co_argcount) { |
| 4859 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4860 | } |
| 4861 | else { |
| 4862 | n = argcount; |
| 4863 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4864 | for (j = 0; j < n; j++) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4865 | PyObject *x = args[j]; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4866 | Py_INCREF(x); |
| 4867 | SETLOCAL(j, x); |
| 4868 | } |
| 4869 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4870 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4871 | if (co->co_flags & CO_VARARGS) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4872 | PyObject *u = _PyTuple_FromArray(args + n, argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4873 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4874 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4875 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4876 | SETLOCAL(total_args, u); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4877 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4878 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4879 | /* Handle keyword arguments */ |
| 4880 | if (kwnames != NULL) { |
| 4881 | Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames); |
| 4882 | for (i = 0; i < kwcount; i++) { |
| 4883 | PyObject **co_varnames; |
| 4884 | PyObject *keyword = PyTuple_GET_ITEM(kwnames, i); |
| 4885 | PyObject *value = args[i+argcount]; |
| 4886 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4887 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4888 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 4889 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4890 | "%U() keywords must be strings", |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4891 | con->fc_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4892 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 4893 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4894 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4895 | /* Speed hack: do raw pointer compares. As names are |
| 4896 | normally interned this should almost always hit. */ |
| 4897 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 4898 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
| 4899 | PyObject *varname = co_varnames[j]; |
| 4900 | if (varname == keyword) { |
| 4901 | goto kw_found; |
| 4902 | } |
| 4903 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4904 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4905 | /* Slow fallback, just in case */ |
| 4906 | for (j = co->co_posonlyargcount; j < total_args; j++) { |
| 4907 | PyObject *varname = co_varnames[j]; |
| 4908 | int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ); |
| 4909 | if (cmp > 0) { |
| 4910 | goto kw_found; |
| 4911 | } |
| 4912 | else if (cmp < 0) { |
| 4913 | goto fail; |
| 4914 | } |
| 4915 | } |
| 4916 | |
| 4917 | assert(j >= total_args); |
| 4918 | if (kwdict == NULL) { |
| 4919 | |
| 4920 | if (co->co_posonlyargcount |
| 4921 | && positional_only_passed_as_keyword(tstate, co, |
| 4922 | kwcount, kwnames, |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4923 | con->fc_qualname)) |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4924 | { |
| 4925 | goto fail; |
| 4926 | } |
| 4927 | |
| 4928 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4929 | "%U() got an unexpected keyword argument '%S'", |
| 4930 | con->fc_qualname, keyword); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 4931 | goto fail; |
| 4932 | } |
| 4933 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4934 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 4935 | goto fail; |
| 4936 | } |
| 4937 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4938 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4939 | kw_found: |
| 4940 | if (GETLOCAL(j) != NULL) { |
| 4941 | _PyErr_Format(tstate, PyExc_TypeError, |
| 4942 | "%U() got multiple values for argument '%S'", |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4943 | con->fc_qualname, keyword); |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 4944 | goto fail; |
| 4945 | } |
| 4946 | Py_INCREF(value); |
| 4947 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4948 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4950 | |
| 4951 | /* Check the number of positional arguments */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4952 | if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) { |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4953 | too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals, |
| 4954 | con->fc_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4955 | goto fail; |
| 4956 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4957 | |
| 4958 | /* Add missing positional arguments (copy default values from defs) */ |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4959 | if (argcount < co->co_argcount) { |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4960 | Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4961 | Py_ssize_t m = co->co_argcount - defcount; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4962 | Py_ssize_t missing = 0; |
| 4963 | for (i = argcount; i < m; i++) { |
| 4964 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4965 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4966 | } |
| 4967 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4968 | if (missing) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4969 | missing_arguments(tstate, co, missing, defcount, fastlocals, |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4970 | con->fc_qualname); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 4971 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4972 | } |
| 4973 | if (n > m) |
| 4974 | i = n - m; |
| 4975 | else |
| 4976 | i = 0; |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4977 | if (defcount) { |
| 4978 | PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0); |
| 4979 | for (; i < defcount; i++) { |
| 4980 | if (GETLOCAL(m+i) == NULL) { |
| 4981 | PyObject *def = defs[i]; |
| 4982 | Py_INCREF(def); |
| 4983 | SETLOCAL(m+i, def); |
| 4984 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4985 | } |
| 4986 | } |
| 4987 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 4988 | |
| 4989 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4990 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 4991 | Py_ssize_t missing = 0; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 4992 | for (i = co->co_argcount; i < total_args; i++) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4993 | if (GETLOCAL(i) != NULL) |
| 4994 | continue; |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 4995 | PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i); |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 4996 | if (con->fc_kwdefaults != NULL) { |
| 4997 | PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 4998 | if (def) { |
| 4999 | Py_INCREF(def); |
| 5000 | SETLOCAL(i, def); |
| 5001 | continue; |
| 5002 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5003 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5004 | goto fail; |
| 5005 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 5006 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 5007 | missing++; |
| 5008 | } |
| 5009 | if (missing) { |
Victor Stinner | 232dda6 | 2020-06-04 15:19:02 +0200 | [diff] [blame] | 5010 | missing_arguments(tstate, co, missing, -1, fastlocals, |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5011 | con->fc_qualname); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 5012 | goto fail; |
| 5013 | } |
| 5014 | } |
| 5015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5017 | vars into frame. */ |
| 5018 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5019 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 5020 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5021 | /* Possibly account for the cell variable being an argument. */ |
| 5022 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 5023 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5024 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 5025 | /* Clear the local copy. */ |
| 5026 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 5027 | } |
| 5028 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5029 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 5030 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 5031 | if (c == NULL) |
| 5032 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5033 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5034 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 5035 | |
| 5036 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5037 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5038 | PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i); |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 5039 | Py_INCREF(o); |
| 5040 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5042 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5043 | return f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5044 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 5045 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 5048 | which can call back into Python. While we're done with the |
| 5049 | current Python frame (f), the associated C stack is still in use, |
| 5050 | so recursion_depth must be boosted for the duration. |
| 5051 | */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 5052 | if (Py_REFCNT(f) > 1) { |
| 5053 | Py_DECREF(f); |
| 5054 | _PyObject_GC_TRACK(f); |
| 5055 | } |
| 5056 | else { |
| 5057 | ++tstate->recursion_depth; |
| 5058 | Py_DECREF(f); |
| 5059 | --tstate->recursion_depth; |
| 5060 | } |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5061 | return NULL; |
| 5062 | } |
| 5063 | |
| 5064 | static PyObject * |
| 5065 | make_coro(PyFrameConstructor *con, PyFrameObject *f) |
| 5066 | { |
| 5067 | assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)); |
| 5068 | PyObject *gen; |
| 5069 | int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE; |
| 5070 | |
| 5071 | /* Don't need to keep the reference to f_back, it will be set |
| 5072 | * when the generator is resumed. */ |
| 5073 | Py_CLEAR(f->f_back); |
| 5074 | |
| 5075 | /* Create a new generator that owns the ready to run frame |
| 5076 | * and return that as the value. */ |
| 5077 | if (is_coro) { |
| 5078 | gen = PyCoro_New(f, con->fc_name, con->fc_qualname); |
| 5079 | } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) { |
| 5080 | gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname); |
| 5081 | } else { |
| 5082 | gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname); |
| 5083 | } |
| 5084 | if (gen == NULL) { |
| 5085 | return NULL; |
| 5086 | } |
| 5087 | |
| 5088 | _PyObject_GC_TRACK(f); |
| 5089 | |
| 5090 | return gen; |
| 5091 | } |
| 5092 | |
| 5093 | PyObject * |
| 5094 | _PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con, |
| 5095 | PyObject *locals, |
| 5096 | PyObject* const* args, size_t argcount, |
| 5097 | PyObject *kwnames) |
| 5098 | { |
| 5099 | PyFrameObject *f = _PyEval_MakeFrameVector( |
| 5100 | tstate, con, locals, args, argcount, kwnames); |
| 5101 | if (f == NULL) { |
| 5102 | return NULL; |
| 5103 | } |
| 5104 | if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 5105 | return make_coro(con, f); |
| 5106 | } |
| 5107 | PyObject *retval = _PyEval_EvalFrame(tstate, f, 0); |
| 5108 | |
| 5109 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 5110 | which can call back into Python. While we're done with the |
| 5111 | current Python frame (f), the associated C stack is still in use, |
| 5112 | so recursion_depth must be boosted for the duration. |
| 5113 | */ |
| 5114 | if (Py_REFCNT(f) > 1) { |
| 5115 | Py_DECREF(f); |
| 5116 | _PyObject_GC_TRACK(f); |
| 5117 | } |
| 5118 | else { |
| 5119 | ++tstate->recursion_depth; |
| 5120 | Py_DECREF(f); |
| 5121 | --tstate->recursion_depth; |
| 5122 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5123 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5126 | /* Legacy API */ |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 5127 | PyObject * |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5128 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
| 5129 | PyObject *const *args, int argcount, |
| 5130 | PyObject *const *kws, int kwcount, |
| 5131 | PyObject *const *defs, int defcount, |
| 5132 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 5133 | { |
Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 5134 | PyThreadState *tstate = _PyThreadState_GET(); |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5135 | PyObject *res; |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5136 | PyObject *defaults = _PyTuple_FromArray(defs, defcount); |
| 5137 | if (defaults == NULL) { |
| 5138 | return NULL; |
| 5139 | } |
Victor Stinner | fc980e0 | 2021-03-18 14:51:24 +0100 | [diff] [blame] | 5140 | PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5141 | if (builtins == NULL) { |
| 5142 | Py_DECREF(defaults); |
| 5143 | return NULL; |
| 5144 | } |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5145 | if (locals == NULL) { |
| 5146 | locals = globals; |
| 5147 | } |
| 5148 | PyObject *kwnames; |
| 5149 | PyObject *const *allargs; |
| 5150 | PyObject **newargs; |
| 5151 | if (kwcount == 0) { |
| 5152 | allargs = args; |
| 5153 | kwnames = NULL; |
| 5154 | } |
| 5155 | else { |
| 5156 | kwnames = PyTuple_New(kwcount); |
| 5157 | if (kwnames == NULL) { |
| 5158 | res = NULL; |
| 5159 | goto fail; |
| 5160 | } |
| 5161 | newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount)); |
| 5162 | if (newargs == NULL) { |
| 5163 | res = NULL; |
| 5164 | Py_DECREF(kwnames); |
| 5165 | goto fail; |
| 5166 | } |
| 5167 | for (int i = 0; i < argcount; i++) { |
| 5168 | newargs[i] = args[i]; |
| 5169 | } |
| 5170 | for (int i = 0; i < kwcount; i++) { |
| 5171 | Py_INCREF(kws[2*i]); |
| 5172 | PyTuple_SET_ITEM(kwnames, i, kws[2*i]); |
| 5173 | newargs[argcount+i] = kws[2*i+1]; |
| 5174 | } |
| 5175 | allargs = newargs; |
| 5176 | } |
| 5177 | PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount); |
| 5178 | if (kwargs == NULL) { |
| 5179 | res = NULL; |
| 5180 | Py_DECREF(kwnames); |
| 5181 | goto fail; |
| 5182 | } |
| 5183 | for (int i = 0; i < kwcount; i++) { |
| 5184 | Py_INCREF(kws[2*i]); |
| 5185 | PyTuple_SET_ITEM(kwnames, i, kws[2*i]); |
| 5186 | kwargs[i] = kws[2*i+1]; |
| 5187 | } |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5188 | PyFrameConstructor constr = { |
| 5189 | .fc_globals = globals, |
| 5190 | .fc_builtins = builtins, |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5191 | .fc_name = ((PyCodeObject *)_co)->co_name, |
| 5192 | .fc_qualname = ((PyCodeObject *)_co)->co_name, |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5193 | .fc_code = _co, |
| 5194 | .fc_defaults = defaults, |
| 5195 | .fc_kwdefaults = kwdefs, |
| 5196 | .fc_closure = closure |
| 5197 | }; |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5198 | res = _PyEval_Vector(tstate, &constr, locals, |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame] | 5199 | allargs, argcount, |
| 5200 | kwnames); |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 5201 | if (kwcount) { |
| 5202 | Py_DECREF(kwnames); |
| 5203 | PyMem_Free(newargs); |
| 5204 | } |
| 5205 | fail: |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5206 | Py_DECREF(defaults); |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 5207 | return res; |
Victor Stinner | b5e170f | 2019-11-16 01:03:22 +0100 | [diff] [blame] | 5208 | } |
| 5209 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5210 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5211 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5212 | special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5214 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 5215 | res = _PyObject_LookupSpecial(o, id); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5216 | if (res == NULL && !_PyErr_Occurred(tstate)) { |
Victor Stinner | 4804b5b | 2020-05-12 01:43:38 +0200 | [diff] [blame] | 5217 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | return NULL; |
| 5219 | } |
| 5220 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 5224 | /* Logic for the raise statement (too complicated for inlining). |
| 5225 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 5226 | static int |
Victor Stinner | 09532fe | 2019-05-10 23:39:09 +0200 | [diff] [blame] | 5227 | do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 5228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 5230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5231 | if (exc == NULL) { |
| 5232 | /* Reraise */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 5233 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 5235 | type = exc_info->exc_type; |
| 5236 | value = exc_info->exc_value; |
| 5237 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 5238 | if (type == Py_None || type == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5239 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
| 5240 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 5241 | return 0; |
| 5242 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5243 | Py_XINCREF(type); |
| 5244 | Py_XINCREF(value); |
| 5245 | Py_XINCREF(tb); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5246 | _PyErr_Restore(tstate, type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 5247 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5248 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | /* We support the following forms of raise: |
| 5251 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 5252 | raise <instance> |
| 5253 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 5254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | if (PyExceptionClass_Check(exc)) { |
| 5256 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 5257 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | if (value == NULL) |
| 5259 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 5260 | if (!PyExceptionInstance_Check(value)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5261 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5262 | "calling %R should have returned an instance of " |
| 5263 | "BaseException, not %R", |
| 5264 | type, Py_TYPE(value)); |
| 5265 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 5266 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | } |
| 5268 | else if (PyExceptionInstance_Check(exc)) { |
| 5269 | value = exc; |
| 5270 | type = PyExceptionInstance_Class(exc); |
| 5271 | Py_INCREF(type); |
| 5272 | } |
| 5273 | else { |
| 5274 | /* Not something you can raise. You get an exception |
| 5275 | anyway, just not what you specified :-) */ |
| 5276 | Py_DECREF(exc); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5277 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5278 | "exceptions must derive from BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | goto raise_error; |
| 5280 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 5281 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 5282 | assert(type != NULL); |
| 5283 | assert(value != NULL); |
| 5284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | if (cause) { |
| 5286 | PyObject *fixed_cause; |
| 5287 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 5288 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | if (fixed_cause == NULL) |
| 5290 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 5291 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5292 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 5293 | else if (PyExceptionInstance_Check(cause)) { |
| 5294 | fixed_cause = cause; |
| 5295 | } |
| 5296 | else if (cause == Py_None) { |
| 5297 | Py_DECREF(cause); |
| 5298 | fixed_cause = NULL; |
| 5299 | } |
| 5300 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5301 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5302 | "exception causes must derive from " |
| 5303 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | goto raise_error; |
| 5305 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 5306 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5307 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 5308 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5309 | _PyErr_SetObject(tstate, type, value); |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 5310 | /* _PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 5311 | Py_DECREF(value); |
| 5312 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 5313 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 5314 | |
| 5315 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5316 | Py_XDECREF(value); |
| 5317 | Py_XDECREF(type); |
| 5318 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 5319 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 5322 | /* 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] | 5323 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 5324 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 5325 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 5326 | with a variable target. |
| 5327 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 5328 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 5329 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5330 | unpack_iterable(PyThreadState *tstate, PyObject *v, |
| 5331 | int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 5332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5333 | int i = 0, j = 0; |
| 5334 | Py_ssize_t ll = 0; |
| 5335 | PyObject *it; /* iter(v) */ |
| 5336 | PyObject *w; |
| 5337 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 5340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5341 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 5342 | if (it == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5343 | if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 5344 | Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v)) |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 5345 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5346 | _PyErr_Format(tstate, PyExc_TypeError, |
| 5347 | "cannot unpack non-iterable %.200s object", |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 5348 | Py_TYPE(v)->tp_name); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 5349 | } |
| 5350 | return 0; |
| 5351 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 5352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | for (; i < argcnt; i++) { |
| 5354 | w = PyIter_Next(it); |
| 5355 | if (w == NULL) { |
| 5356 | /* Iterator done, via error or exhaustion. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5357 | if (!_PyErr_Occurred(tstate)) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 5358 | if (argcntafter == -1) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5359 | _PyErr_Format(tstate, PyExc_ValueError, |
| 5360 | "not enough values to unpack " |
| 5361 | "(expected %d, got %d)", |
| 5362 | argcnt, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 5363 | } |
| 5364 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5365 | _PyErr_Format(tstate, PyExc_ValueError, |
| 5366 | "not enough values to unpack " |
| 5367 | "(expected at least %d, got %d)", |
| 5368 | argcnt + argcntafter, i); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 5369 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | } |
| 5371 | goto Error; |
| 5372 | } |
| 5373 | *--sp = w; |
| 5374 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 5375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5376 | if (argcntafter == -1) { |
| 5377 | /* We better have exhausted the iterator now. */ |
| 5378 | w = PyIter_Next(it); |
| 5379 | if (w == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5380 | if (_PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | goto Error; |
| 5382 | Py_DECREF(it); |
| 5383 | return 1; |
| 5384 | } |
| 5385 | Py_DECREF(w); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5386 | _PyErr_Format(tstate, PyExc_ValueError, |
| 5387 | "too many values to unpack (expected %d)", |
| 5388 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5389 | goto Error; |
| 5390 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 5391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | l = PySequence_List(it); |
| 5393 | if (l == NULL) |
| 5394 | goto Error; |
| 5395 | *--sp = l; |
| 5396 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 5397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | ll = PyList_GET_SIZE(l); |
| 5399 | if (ll < argcntafter) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5400 | _PyErr_Format(tstate, PyExc_ValueError, |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 5401 | "not enough values to unpack (expected at least %d, got %zd)", |
| 5402 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | goto Error; |
| 5404 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 5405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5406 | /* Pop the "after-variable" args off the list. */ |
| 5407 | for (j = argcntafter; j > 0; j--, i++) { |
| 5408 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 5409 | } |
| 5410 | /* Resize the list. */ |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 5411 | Py_SET_SIZE(l, ll - argcntafter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5412 | Py_DECREF(it); |
| 5413 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 5414 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 5415 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5416 | for (; i > 0; i--, sp++) |
| 5417 | Py_DECREF(*sp); |
| 5418 | Py_XDECREF(it); |
| 5419 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 5422 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5423 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5424 | prtrace(PyThreadState *tstate, PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5426 | printf("%s ", str); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5427 | if (PyObject_Print(v, stdout, 0) != 0) { |
| 5428 | /* Don't know what else to do */ |
| 5429 | _PyErr_Clear(tstate); |
| 5430 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | printf("\n"); |
| 5432 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5433 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5434 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5435 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 5436 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5437 | call_exc_trace(Py_tracefunc func, PyObject *self, |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5438 | PyThreadState *tstate, |
| 5439 | PyFrameObject *f, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5440 | PyTraceInfo *trace_info) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 5441 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 5442 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5443 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5444 | _PyErr_Fetch(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | if (value == NULL) { |
| 5446 | value = Py_None; |
| 5447 | Py_INCREF(value); |
| 5448 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5449 | _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback); |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 5450 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | arg = PyTuple_Pack(3, type, value, traceback); |
| 5452 | if (arg == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5453 | _PyErr_Restore(tstate, type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5454 | return; |
| 5455 | } |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5456 | err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5457 | Py_DECREF(arg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5458 | if (err == 0) { |
| 5459 | _PyErr_Restore(tstate, type, value, orig_traceback); |
| 5460 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 | else { |
| 5462 | Py_XDECREF(type); |
| 5463 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 5464 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 5468 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5469 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 5470 | PyThreadState *tstate, PyFrameObject *frame, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5471 | PyTraceInfo *trace_info, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5472 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 5473 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5474 | PyObject *type, *value, *traceback; |
| 5475 | int err; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5476 | _PyErr_Fetch(tstate, &type, &value, &traceback); |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5477 | err = call_trace(func, obj, tstate, frame, trace_info, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5478 | if (err == 0) |
| 5479 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5480 | _PyErr_Restore(tstate, type, value, traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5481 | return 0; |
| 5482 | } |
| 5483 | else { |
| 5484 | Py_XDECREF(type); |
| 5485 | Py_XDECREF(value); |
| 5486 | Py_XDECREF(traceback); |
| 5487 | return -1; |
| 5488 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5491 | static void |
| 5492 | initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame) |
| 5493 | { |
| 5494 | if (trace_info->code != frame->f_code) { |
| 5495 | trace_info->code = frame->f_code; |
| 5496 | trace_info->instr_prev = -1; |
| 5497 | _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds); |
| 5498 | } |
| 5499 | } |
| 5500 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 5501 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5502 | call_trace(Py_tracefunc func, PyObject *obj, |
| 5503 | PyThreadState *tstate, PyFrameObject *frame, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5504 | PyTraceInfo *trace_info, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5505 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 5506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5507 | int result; |
| 5508 | if (tstate->tracing) |
| 5509 | return 0; |
| 5510 | tstate->tracing++; |
| 5511 | tstate->use_tracing = 0; |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5512 | if (frame->f_lasti < 0) { |
| 5513 | frame->f_lineno = frame->f_code->co_firstlineno; |
| 5514 | } |
| 5515 | else { |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5516 | initialize_trace_info(trace_info, frame); |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 5517 | frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds); |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5518 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | result = func(obj, frame, what, arg); |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5520 | frame->f_lineno = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5521 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 5522 | || (tstate->c_profilefunc != NULL)); |
| 5523 | tstate->tracing--; |
| 5524 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 5527 | PyObject * |
| 5528 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 5529 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5530 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5531 | int save_tracing = tstate->tracing; |
| 5532 | int save_use_tracing = tstate->use_tracing; |
| 5533 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 5534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | tstate->tracing = 0; |
| 5536 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 5537 | || (tstate->c_profilefunc != NULL)); |
| 5538 | result = PyObject_Call(func, args, NULL); |
| 5539 | tstate->tracing = save_tracing; |
| 5540 | tstate->use_tracing = save_use_tracing; |
| 5541 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5544 | /* 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] | 5545 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 5546 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5547 | PyThreadState *tstate, PyFrameObject *frame, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5548 | PyTraceInfo *trace_info) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 5549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | int result = 0; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 5551 | |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 5552 | /* If the last instruction falls at the start of a line or if it |
| 5553 | represents a jump backwards, update the frame's line number and |
| 5554 | then call the trace function if we're tracing source lines. |
| 5555 | */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5556 | initialize_trace_info(trace_info, frame); |
| 5557 | int lastline = trace_info->bounds.ar_line; |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 5558 | int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds); |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 5559 | if (line != -1 && frame->f_trace_lines) { |
| 5560 | /* Trace backward edges or first instruction of a new line */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5561 | if (frame->f_lasti < trace_info->instr_prev || |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 5562 | (line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start)) |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 5563 | { |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5564 | result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None); |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 5565 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 5567 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 5568 | if (frame->f_trace_opcodes) { |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5569 | result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None); |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 5570 | } |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5571 | trace_info->instr_prev = frame->f_lasti; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5572 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5575 | int |
| 5576 | _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) |
| 5577 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5578 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5579 | /* The caller must hold the GIL */ |
| 5580 | assert(PyGILState_Check()); |
| 5581 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5582 | /* Call _PySys_Audit() in the context of the current thread state, |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5583 | even if tstate is not the current thread state. */ |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5584 | PyThreadState *current_tstate = _PyThreadState_GET(); |
| 5585 | if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5586 | return -1; |
| 5587 | } |
| 5588 | |
| 5589 | PyObject *profileobj = tstate->c_profileobj; |
| 5590 | |
| 5591 | tstate->c_profilefunc = NULL; |
| 5592 | tstate->c_profileobj = NULL; |
| 5593 | /* Must make sure that tracing is not ignored if 'profileobj' is freed */ |
| 5594 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 5595 | Py_XDECREF(profileobj); |
| 5596 | |
| 5597 | Py_XINCREF(arg); |
| 5598 | tstate->c_profileobj = arg; |
| 5599 | tstate->c_profilefunc = func; |
| 5600 | |
| 5601 | /* Flag that tracing or profiling is turned on */ |
| 5602 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
| 5603 | return 0; |
| 5604 | } |
| 5605 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 5606 | void |
| 5607 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 5608 | { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5609 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5610 | if (_PyEval_SetProfile(tstate, func, arg) < 0) { |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5611 | /* Log _PySys_Audit() error */ |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5612 | _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL); |
| 5613 | } |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | int |
| 5617 | _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg) |
| 5618 | { |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5619 | assert(is_tstate_valid(tstate)); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5620 | /* The caller must hold the GIL */ |
| 5621 | assert(PyGILState_Check()); |
| 5622 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5623 | /* Call _PySys_Audit() in the context of the current thread state, |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5624 | even if tstate is not the current thread state. */ |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5625 | PyThreadState *current_tstate = _PyThreadState_GET(); |
| 5626 | if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5627 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5628 | } |
| 5629 | |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5630 | struct _ceval_state *ceval2 = &tstate->interp->ceval; |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5631 | PyObject *traceobj = tstate->c_traceobj; |
Victor Stinner | da2914d | 2020-03-20 09:29:08 +0100 | [diff] [blame] | 5632 | ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5633 | |
| 5634 | tstate->c_tracefunc = NULL; |
| 5635 | tstate->c_traceobj = NULL; |
| 5636 | /* Must make sure that profiling is not ignored if 'traceobj' is freed */ |
| 5637 | tstate->use_tracing = (tstate->c_profilefunc != NULL); |
| 5638 | Py_XDECREF(traceobj); |
| 5639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5640 | Py_XINCREF(arg); |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5641 | tstate->c_traceobj = arg; |
| 5642 | tstate->c_tracefunc = func; |
| 5643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5644 | /* Flag that tracing or profiling is turned on */ |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5645 | tstate->use_tracing = ((func != NULL) |
| 5646 | || (tstate->c_profilefunc != NULL)); |
| 5647 | |
| 5648 | return 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | void |
| 5652 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 5653 | { |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5654 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5655 | if (_PyEval_SetTrace(tstate, func, arg) < 0) { |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5656 | /* Log _PySys_Audit() error */ |
Victor Stinner | f6a5850 | 2020-03-16 17:41:44 +0100 | [diff] [blame] | 5657 | _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL); |
| 5658 | } |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 5661 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5662 | void |
Victor Stinner | 838f264 | 2019-06-13 22:41:23 +0200 | [diff] [blame] | 5663 | _PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth) |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 5664 | { |
| 5665 | assert(new_depth >= 0); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 5666 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 5667 | } |
| 5668 | |
| 5669 | int |
| 5670 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 5671 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5672 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 5673 | return tstate->coroutine_origin_tracking_depth; |
| 5674 | } |
| 5675 | |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5676 | int |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5677 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 5678 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5679 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5680 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5681 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) { |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5682 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5683 | } |
| 5684 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5685 | Py_XINCREF(firstiter); |
| 5686 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5687 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | PyObject * |
| 5691 | _PyEval_GetAsyncGenFirstiter(void) |
| 5692 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5693 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5694 | return tstate->async_gen_firstiter; |
| 5695 | } |
| 5696 | |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5697 | int |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5698 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 5699 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5700 | PyThreadState *tstate = _PyThreadState_GET(); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5701 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 5702 | if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) { |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5703 | return -1; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 5704 | } |
| 5705 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5706 | Py_XINCREF(finalizer); |
| 5707 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
Zackery Spytz | 79ceccd | 2020-03-26 06:11:13 -0600 | [diff] [blame] | 5708 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | PyObject * |
| 5712 | _PyEval_GetAsyncGenFinalizer(void) |
| 5713 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 5714 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5715 | return tstate->async_gen_finalizer; |
| 5716 | } |
| 5717 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5718 | PyFrameObject * |
| 5719 | PyEval_GetFrame(void) |
| 5720 | { |
| 5721 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5722 | return tstate->frame; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5725 | PyObject * |
Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 5726 | _PyEval_GetBuiltins(PyThreadState *tstate) |
| 5727 | { |
| 5728 | PyFrameObject *frame = tstate->frame; |
| 5729 | if (frame != NULL) { |
| 5730 | return frame->f_builtins; |
| 5731 | } |
| 5732 | return tstate->interp->builtins; |
| 5733 | } |
| 5734 | |
| 5735 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5736 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 5737 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5738 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 46496f9 | 2021-02-20 15:17:18 +0100 | [diff] [blame] | 5739 | return _PyEval_GetBuiltins(tstate); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 5742 | /* Convenience function to get a builtin from its name */ |
| 5743 | PyObject * |
| 5744 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 5745 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5746 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 5747 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 5748 | if (attr) { |
| 5749 | Py_INCREF(attr); |
| 5750 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5751 | else if (!_PyErr_Occurred(tstate)) { |
| 5752 | _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name)); |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 5753 | } |
| 5754 | return attr; |
| 5755 | } |
| 5756 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5757 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5758 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 5759 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5760 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5761 | PyFrameObject *current_frame = tstate->frame; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5762 | if (current_frame == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5763 | _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5764 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5765 | } |
| 5766 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5767 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5768 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5769 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5770 | |
| 5771 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5772 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 5775 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5776 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5777 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5778 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5779 | PyFrameObject *current_frame = tstate->frame; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5780 | if (current_frame == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5781 | return NULL; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5782 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 5783 | |
| 5784 | assert(current_frame->f_globals != NULL); |
| 5785 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 5788 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 5789 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 5790 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5791 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 6723e93 | 2020-03-20 17:46:56 +0100 | [diff] [blame] | 5792 | PyFrameObject *current_frame = tstate->frame; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 5794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5795 | if (current_frame != NULL) { |
| 5796 | const int codeflags = current_frame->f_code->co_flags; |
| 5797 | const int compilerflags = codeflags & PyCF_MASK; |
| 5798 | if (compilerflags) { |
| 5799 | result = 1; |
| 5800 | cf->cf_flags |= compilerflags; |
| 5801 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 5802 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5803 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 5804 | result = 1; |
| 5805 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 5806 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 5807 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | } |
| 5809 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5812 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 5813 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5814 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5816 | if (PyMethod_Check(func)) |
| 5817 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 5818 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5819 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 | else if (PyCFunction_Check(func)) |
| 5821 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 5822 | else |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 5823 | return Py_TYPE(func)->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 5826 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5827 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5829 | if (PyMethod_Check(func)) |
| 5830 | return "()"; |
| 5831 | else if (PyFunction_Check(func)) |
| 5832 | return "()"; |
| 5833 | else if (PyCFunction_Check(func)) |
| 5834 | return "()"; |
| 5835 | else |
| 5836 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 5839 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 5840 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5841 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5842 | tstate, tstate->frame, trace_info, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5843 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | x = NULL; \ |
| 5845 | } \ |
| 5846 | else { \ |
| 5847 | x = call; \ |
| 5848 | if (tstate->c_profilefunc != NULL) { \ |
| 5849 | if (x == NULL) { \ |
| 5850 | call_trace_protected(tstate->c_profilefunc, \ |
| 5851 | tstate->c_profileobj, \ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5852 | tstate, tstate->frame, trace_info, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5853 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5854 | /* XXX should pass (type, value, tb) */ \ |
| 5855 | } else { \ |
| 5856 | if (call_trace(tstate->c_profilefunc, \ |
| 5857 | tstate->c_profileobj, \ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5858 | tstate, tstate->frame, trace_info, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5859 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5860 | Py_DECREF(x); \ |
| 5861 | x = NULL; \ |
| 5862 | } \ |
| 5863 | } \ |
| 5864 | } \ |
| 5865 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 5866 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5867 | x = call; \ |
| 5868 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 5869 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5870 | |
| 5871 | static PyObject * |
| 5872 | trace_call_function(PyThreadState *tstate, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5873 | PyTraceInfo *trace_info, |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5874 | PyObject *func, |
| 5875 | PyObject **args, Py_ssize_t nargs, |
| 5876 | PyObject *kwnames) |
| 5877 | { |
| 5878 | PyObject *x; |
scoder | 4c9ea09 | 2020-05-12 16:12:41 +0200 | [diff] [blame] | 5879 | if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5880 | C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5881 | return x; |
| 5882 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 5883 | else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) { |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5884 | /* We need to create a temporary bound method as argument |
| 5885 | for profiling. |
| 5886 | |
| 5887 | If nargs == 0, then this cannot work because we have no |
| 5888 | "self". In any case, the call itself would raise |
| 5889 | TypeError (foo needs an argument), so we just skip |
| 5890 | profiling. */ |
| 5891 | PyObject *self = args[0]; |
| 5892 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5893 | if (func == NULL) { |
| 5894 | return NULL; |
| 5895 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5896 | C_TRACE(x, PyObject_Vectorcall(func, |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 5897 | args+1, nargs-1, |
| 5898 | kwnames)); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5899 | Py_DECREF(func); |
| 5900 | return x; |
| 5901 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5902 | return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5903 | } |
| 5904 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 5905 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 5906 | to reduce the stack consumption. */ |
| 5907 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5908 | call_function(PyThreadState *tstate, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5909 | PyTraceInfo *trace_info, |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5910 | PyObject ***pp_stack, |
| 5911 | Py_ssize_t oparg, |
| 5912 | PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5913 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5914 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5915 | PyObject *func = *pfunc; |
| 5916 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 5917 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 5918 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 5919 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5920 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5921 | if (tstate->use_tracing) { |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5922 | x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames); |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 5923 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 5924 | else { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5925 | x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5926 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 5927 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5928 | assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5929 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 5930 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5931 | while ((*pp_stack) > pfunc) { |
| 5932 | w = EXT_POP(*pp_stack); |
| 5933 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5934 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 5935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5939 | static PyObject * |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5940 | do_call_core(PyThreadState *tstate, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 5941 | PyTraceInfo *trace_info, |
Mark Shannon | 8643345 | 2021-01-07 16:49:02 +0000 | [diff] [blame] | 5942 | PyObject *func, |
| 5943 | PyObject *callargs, |
| 5944 | PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5945 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5946 | PyObject *result; |
| 5947 | |
scoder | 4c9ea09 | 2020-05-12 16:12:41 +0200 | [diff] [blame] | 5948 | if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) { |
Jeroen Demeyer | 7a6873c | 2019-09-11 13:01:01 +0200 | [diff] [blame] | 5949 | C_TRACE(result, PyObject_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 5950 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5951 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 5952 | else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5953 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 5954 | if (nargs > 0 && tstate->use_tracing) { |
| 5955 | /* We need to create a temporary bound method as argument |
| 5956 | for profiling. |
| 5957 | |
| 5958 | If nargs == 0, then this cannot work because we have no |
| 5959 | "self". In any case, the call itself would raise |
| 5960 | TypeError (foo needs an argument), so we just skip |
| 5961 | profiling. */ |
| 5962 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 5963 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 5964 | if (func == NULL) { |
| 5965 | return NULL; |
| 5966 | } |
| 5967 | |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 5968 | C_TRACE(result, _PyObject_FastCallDictTstate( |
| 5969 | tstate, func, |
| 5970 | &_PyTuple_ITEMS(callargs)[1], |
| 5971 | nargs - 1, |
| 5972 | kwdict)); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5973 | Py_DECREF(func); |
| 5974 | return result; |
| 5975 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 5976 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 5977 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 5978 | } |
| 5979 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 5980 | /* 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] | 5981 | nb_index slot defined, and store in *pi. |
| 5982 | 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] | 5983 | 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] | 5984 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 5985 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 5986 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5987 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5988 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5989 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 5990 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5991 | Py_ssize_t x; |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 5992 | if (_PyIndex_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5993 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5994 | if (x == -1 && _PyErr_Occurred(tstate)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5995 | return 0; |
| 5996 | } |
| 5997 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 5998 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 5999 | "slice indices must be integers or " |
| 6000 | "None or have an __index__ method"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6001 | return 0; |
| 6002 | } |
| 6003 | *pi = x; |
| 6004 | } |
| 6005 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6006 | } |
| 6007 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 6008 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 6009 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 6010 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6011 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 6012 | Py_ssize_t x; |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 6013 | if (_PyIndex_Check(v)) { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 6014 | x = PyNumber_AsSsize_t(v, NULL); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6015 | if (x == -1 && _PyErr_Occurred(tstate)) |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 6016 | return 0; |
| 6017 | } |
| 6018 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6019 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 6020 | "slice indices must be integers or " |
| 6021 | "have an __index__ method"); |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 6022 | return 0; |
| 6023 | } |
| 6024 | *pi = x; |
| 6025 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 6026 | } |
| 6027 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 6028 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6029 | import_name(PyThreadState *tstate, PyFrameObject *f, |
| 6030 | PyObject *name, PyObject *fromlist, PyObject *level) |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6031 | { |
| 6032 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 6033 | PyObject *import_func, *res; |
| 6034 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6035 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 6036 | import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6037 | if (import_func == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6038 | if (!_PyErr_Occurred(tstate)) { |
| 6039 | _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 6040 | } |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6041 | return NULL; |
| 6042 | } |
| 6043 | |
| 6044 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6045 | if (import_func == tstate->interp->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6046 | int ilevel = _PyLong_AsInt(level); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6047 | if (ilevel == -1 && _PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6048 | return NULL; |
| 6049 | } |
| 6050 | res = PyImport_ImportModuleLevelObject( |
| 6051 | name, |
| 6052 | f->f_globals, |
| 6053 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 6054 | fromlist, |
| 6055 | ilevel); |
| 6056 | return res; |
| 6057 | } |
| 6058 | |
| 6059 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 6060 | |
| 6061 | stack[0] = name; |
| 6062 | stack[1] = f->f_globals; |
| 6063 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 6064 | stack[3] = fromlist; |
| 6065 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 6066 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 6067 | Py_DECREF(import_func); |
| 6068 | return res; |
| 6069 | } |
| 6070 | |
| 6071 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6072 | import_from(PyThreadState *tstate, PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 6073 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6074 | PyObject *x; |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 6075 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 6076 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6077 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 6078 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6079 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 6080 | /* Issue #17636: in case this failed because of a circular relative |
| 6081 | import, try to fallback on reading the module directly from |
| 6082 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 6083 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 6084 | if (pkgname == NULL) { |
| 6085 | goto error; |
| 6086 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 6087 | if (!PyUnicode_Check(pkgname)) { |
| 6088 | Py_CLEAR(pkgname); |
| 6089 | goto error; |
| 6090 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 6091 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 6092 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 6093 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 6094 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 6095 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6096 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 6097 | Py_DECREF(fullmodname); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6098 | if (x == NULL && !_PyErr_Occurred(tstate)) { |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 6099 | goto error; |
| 6100 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 6101 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6102 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 6103 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 6104 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 6105 | if (pkgname == NULL) { |
| 6106 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 6107 | if (pkgname_or_unknown == NULL) { |
| 6108 | Py_XDECREF(pkgpath); |
| 6109 | return NULL; |
| 6110 | } |
| 6111 | } else { |
| 6112 | pkgname_or_unknown = pkgname; |
| 6113 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 6114 | |
| 6115 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6116 | _PyErr_Clear(tstate); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 6117 | errmsg = PyUnicode_FromFormat( |
| 6118 | "cannot import name %R from %R (unknown location)", |
| 6119 | name, pkgname_or_unknown |
| 6120 | ); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 6121 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 6122 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 6123 | } |
| 6124 | else { |
Anthony Sottile | 65366bc | 2019-09-09 08:17:50 -0700 | [diff] [blame] | 6125 | _Py_IDENTIFIER(__spec__); |
| 6126 | PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__); |
Anthony Sottile | 65366bc | 2019-09-09 08:17:50 -0700 | [diff] [blame] | 6127 | const char *fmt = |
| 6128 | _PyModuleSpec_IsInitializing(spec) ? |
| 6129 | "cannot import name %R from partially initialized module %R " |
| 6130 | "(most likely due to a circular import) (%S)" : |
| 6131 | "cannot import name %R from %R (%S)"; |
| 6132 | Py_XDECREF(spec); |
| 6133 | |
| 6134 | errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath); |
Stefan Krah | 027b09c | 2019-03-25 21:50:58 +0100 | [diff] [blame] | 6135 | /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 6136 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 6137 | } |
| 6138 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 6139 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 6140 | Py_XDECREF(pkgname_or_unknown); |
| 6141 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 6142 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 6143 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 6144 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 6145 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6146 | import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 6147 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6148 | _Py_IDENTIFIER(__all__); |
| 6149 | _Py_IDENTIFIER(__dict__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6150 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6151 | int skip_leading_underscores = 0; |
| 6152 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 6153 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6154 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 6155 | return -1; /* Unexpected error */ |
| 6156 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6157 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6158 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 6159 | return -1; |
| 6160 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6161 | if (dict == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6162 | _PyErr_SetString(tstate, PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6163 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6164 | return -1; |
| 6165 | } |
| 6166 | all = PyMapping_Keys(dict); |
| 6167 | Py_DECREF(dict); |
| 6168 | if (all == NULL) |
| 6169 | return -1; |
| 6170 | skip_leading_underscores = 1; |
| 6171 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 6172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6173 | for (pos = 0, err = 0; ; pos++) { |
| 6174 | name = PySequence_GetItem(all, pos); |
| 6175 | if (name == NULL) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6176 | if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6177 | err = -1; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6178 | } |
| 6179 | else { |
| 6180 | _PyErr_Clear(tstate); |
| 6181 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6182 | break; |
| 6183 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 6184 | if (!PyUnicode_Check(name)) { |
| 6185 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 6186 | if (modname == NULL) { |
| 6187 | Py_DECREF(name); |
| 6188 | err = -1; |
| 6189 | break; |
| 6190 | } |
| 6191 | if (!PyUnicode_Check(modname)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6192 | _PyErr_Format(tstate, PyExc_TypeError, |
| 6193 | "module __name__ must be a string, not %.100s", |
| 6194 | Py_TYPE(modname)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 6195 | } |
| 6196 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6197 | _PyErr_Format(tstate, PyExc_TypeError, |
| 6198 | "%s in %U.%s must be str, not %.100s", |
| 6199 | skip_leading_underscores ? "Key" : "Item", |
| 6200 | modname, |
| 6201 | skip_leading_underscores ? "__dict__" : "__all__", |
| 6202 | Py_TYPE(name)->tp_name); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 6203 | } |
| 6204 | Py_DECREF(modname); |
| 6205 | Py_DECREF(name); |
| 6206 | err = -1; |
| 6207 | break; |
| 6208 | } |
| 6209 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 6210 | if (PyUnicode_READY(name) == -1) { |
| 6211 | Py_DECREF(name); |
| 6212 | err = -1; |
| 6213 | break; |
| 6214 | } |
| 6215 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 6216 | Py_DECREF(name); |
| 6217 | continue; |
| 6218 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6219 | } |
| 6220 | value = PyObject_GetAttr(v, name); |
| 6221 | if (value == NULL) |
| 6222 | err = -1; |
| 6223 | else if (PyDict_CheckExact(locals)) |
| 6224 | err = PyDict_SetItem(locals, name, value); |
| 6225 | else |
| 6226 | err = PyObject_SetItem(locals, name, value); |
| 6227 | Py_DECREF(name); |
| 6228 | Py_XDECREF(value); |
| 6229 | if (err != 0) |
| 6230 | break; |
| 6231 | } |
| 6232 | Py_DECREF(all); |
| 6233 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 6236 | static int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6237 | check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 6238 | { |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 6239 | if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) { |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 6240 | /* check_args_iterable() may be called with a live exception: |
| 6241 | * clear it to prevent calling _PyObject_FunctionStr() with an |
| 6242 | * exception set. */ |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 6243 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 6244 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 6245 | if (funcstr != NULL) { |
| 6246 | _PyErr_Format(tstate, PyExc_TypeError, |
| 6247 | "%U argument after * must be an iterable, not %.200s", |
| 6248 | funcstr, Py_TYPE(args)->tp_name); |
| 6249 | Py_DECREF(funcstr); |
| 6250 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 6251 | return -1; |
| 6252 | } |
| 6253 | return 0; |
| 6254 | } |
| 6255 | |
| 6256 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6257 | format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 6258 | { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 6259 | /* _PyDict_MergeEx raises attribute |
| 6260 | * error (percolated from an attempt |
| 6261 | * to get 'keys' attribute) instead of |
| 6262 | * a type error if its second argument |
| 6263 | * is not a mapping. |
| 6264 | */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6265 | if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 6266 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 6267 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 6268 | if (funcstr != NULL) { |
| 6269 | _PyErr_Format( |
| 6270 | tstate, PyExc_TypeError, |
| 6271 | "%U argument after ** must be a mapping, not %.200s", |
| 6272 | funcstr, Py_TYPE(kwargs)->tp_name); |
| 6273 | Py_DECREF(funcstr); |
| 6274 | } |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 6275 | } |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6276 | else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 6277 | PyObject *exc, *val, *tb; |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6278 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 6279 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
Victor Stinner | 61f4db8 | 2020-01-28 03:37:45 +0100 | [diff] [blame] | 6280 | _PyErr_Clear(tstate); |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 6281 | PyObject *funcstr = _PyObject_FunctionStr(func); |
| 6282 | if (funcstr != NULL) { |
| 6283 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 6284 | _PyErr_Format( |
| 6285 | tstate, PyExc_TypeError, |
| 6286 | "%U got multiple values for keyword argument '%S'", |
| 6287 | funcstr, key); |
| 6288 | Py_DECREF(funcstr); |
| 6289 | } |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 6290 | Py_XDECREF(exc); |
| 6291 | Py_XDECREF(val); |
| 6292 | Py_XDECREF(tb); |
| 6293 | } |
| 6294 | else { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6295 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 6296 | } |
| 6297 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 6298 | } |
| 6299 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 6300 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6301 | format_exc_check_arg(PyThreadState *tstate, PyObject *exc, |
| 6302 | const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 6303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6304 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 6305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6306 | if (!obj) |
| 6307 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 6308 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 6309 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6310 | if (!obj_str) |
| 6311 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 6312 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6313 | _PyErr_Format(tstate, exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 6314 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6315 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 6316 | static void |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6317 | format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 6318 | { |
| 6319 | PyObject *name; |
| 6320 | /* Don't stomp existing exception */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6321 | if (_PyErr_Occurred(tstate)) |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 6322 | return; |
| 6323 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 6324 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 6325 | oparg); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6326 | format_exc_check_arg(tstate, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 6327 | PyExc_UnboundLocalError, |
| 6328 | UNBOUNDLOCAL_ERROR_MSG, |
| 6329 | name); |
| 6330 | } else { |
| 6331 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 6332 | PyTuple_GET_SIZE(co->co_cellvars)); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6333 | format_exc_check_arg(tstate, PyExc_NameError, |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 6334 | UNBOUNDFREE_ERROR_MSG, name); |
| 6335 | } |
| 6336 | } |
| 6337 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 6338 | static void |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6339 | format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode) |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 6340 | { |
| 6341 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 6342 | if (prevopcode == BEFORE_ASYNC_WITH) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6343 | _PyErr_Format(tstate, PyExc_TypeError, |
| 6344 | "'async with' received an object from __aenter__ " |
| 6345 | "that does not implement __await__: %.100s", |
| 6346 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 6347 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6348 | else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6349 | _PyErr_Format(tstate, PyExc_TypeError, |
| 6350 | "'async with' received an object from __aexit__ " |
| 6351 | "that does not implement __await__: %.100s", |
| 6352 | type->tp_name); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 6353 | } |
| 6354 | } |
| 6355 | } |
| 6356 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6357 | static PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6358 | unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6359 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6360 | { |
| 6361 | PyObject *res; |
| 6362 | if (Py_REFCNT(v) == 2) { |
| 6363 | /* In the common case, there are 2 references to the value |
| 6364 | * stored in 'variable' when the += is performed: one on the |
| 6365 | * value stack (in 'v') and one still stored in the |
| 6366 | * 'variable'. We try to delete the variable now to reduce |
| 6367 | * the refcnt to 1. |
| 6368 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 6369 | int opcode, oparg; |
| 6370 | NEXTOPARG(); |
| 6371 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6372 | case STORE_FAST: |
| 6373 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6374 | PyObject **fastlocals = f->f_localsplus; |
| 6375 | if (GETLOCAL(oparg) == v) |
| 6376 | SETLOCAL(oparg, NULL); |
| 6377 | break; |
| 6378 | } |
| 6379 | case STORE_DEREF: |
| 6380 | { |
| 6381 | PyObject **freevars = (f->f_localsplus + |
| 6382 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 6383 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 6384 | if (PyCell_GET(c) == v) { |
| 6385 | PyCell_SET(c, NULL); |
| 6386 | Py_DECREF(v); |
| 6387 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6388 | break; |
| 6389 | } |
| 6390 | case STORE_NAME: |
| 6391 | { |
| 6392 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 6393 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6394 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 6395 | if (locals && PyDict_CheckExact(locals)) { |
| 6396 | PyObject *w = PyDict_GetItemWithError(locals, name); |
| 6397 | if ((w == v && PyDict_DelItem(locals, name) != 0) || |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6398 | (w == NULL && _PyErr_Occurred(tstate))) |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 6399 | { |
| 6400 | Py_DECREF(v); |
| 6401 | return NULL; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 6402 | } |
| 6403 | } |
| 6404 | break; |
| 6405 | } |
| 6406 | } |
| 6407 | } |
| 6408 | res = v; |
| 6409 | PyUnicode_Append(&res, w); |
| 6410 | return res; |
| 6411 | } |
| 6412 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6413 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 6414 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 6415 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 6416 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | int i; |
| 6419 | PyObject *l = PyList_New(256); |
| 6420 | if (l == NULL) return NULL; |
| 6421 | for (i = 0; i < 256; i++) { |
| 6422 | PyObject *x = PyLong_FromLong(a[i]); |
| 6423 | if (x == NULL) { |
| 6424 | Py_DECREF(l); |
| 6425 | return NULL; |
| 6426 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 6427 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6428 | } |
| 6429 | for (i = 0; i < 256; i++) |
| 6430 | a[i] = 0; |
| 6431 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 6435 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6436 | { |
| 6437 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6438 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6439 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6440 | int i; |
| 6441 | PyObject *l = PyList_New(257); |
| 6442 | if (l == NULL) return NULL; |
| 6443 | for (i = 0; i < 257; i++) { |
| 6444 | PyObject *x = getarray(dxpairs[i]); |
| 6445 | if (x == NULL) { |
| 6446 | Py_DECREF(l); |
| 6447 | return NULL; |
| 6448 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 6449 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6450 | } |
| 6451 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 6452 | #endif |
| 6453 | } |
| 6454 | |
| 6455 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 6456 | |
| 6457 | Py_ssize_t |
| 6458 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 6459 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6460 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 6461 | Py_ssize_t new_index; |
| 6462 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 6463 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 6464 | return -1; |
| 6465 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 6466 | new_index = interp->co_extra_user_count++; |
| 6467 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 6468 | return new_index; |
| 6469 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6470 | |
| 6471 | static void |
| 6472 | dtrace_function_entry(PyFrameObject *f) |
| 6473 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 6474 | const char *filename; |
| 6475 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6476 | int lineno; |
| 6477 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 6478 | PyCodeObject *code = f->f_code; |
| 6479 | filename = PyUnicode_AsUTF8(code->co_filename); |
| 6480 | funcname = PyUnicode_AsUTF8(code->co_name); |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 6481 | lineno = PyFrame_GetLineNumber(f); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6482 | |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 6483 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6484 | } |
| 6485 | |
| 6486 | static void |
| 6487 | dtrace_function_return(PyFrameObject *f) |
| 6488 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 6489 | const char *filename; |
| 6490 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6491 | int lineno; |
| 6492 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 6493 | PyCodeObject *code = f->f_code; |
| 6494 | filename = PyUnicode_AsUTF8(code->co_filename); |
| 6495 | funcname = PyUnicode_AsUTF8(code->co_name); |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 6496 | lineno = PyFrame_GetLineNumber(f); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6497 | |
Andy Lester | e6be9b5 | 2020-02-11 20:28:35 -0600 | [diff] [blame] | 6498 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 6502 | static void |
| 6503 | maybe_dtrace_line(PyFrameObject *frame, |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 6504 | PyTraceInfo *trace_info) |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6505 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 6506 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6507 | |
| 6508 | /* If the last instruction executed isn't in the current |
| 6509 | instruction window, reset the window. |
| 6510 | */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 6511 | initialize_trace_info(trace_info, frame); |
Mark Shannon | fcb55c0 | 2021-04-01 16:00:31 +0100 | [diff] [blame] | 6512 | int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6513 | /* If the last instruction falls at the start of a line or if |
| 6514 | it represents a jump backwards, update the frame's line |
| 6515 | number and call the trace function. */ |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 6516 | if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6517 | if (line != -1) { |
| 6518 | frame->f_lineno = line; |
| 6519 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 6520 | if (!co_filename) |
| 6521 | co_filename = "?"; |
| 6522 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 6523 | if (!co_name) |
| 6524 | co_name = "?"; |
| 6525 | PyDTrace_LINE(co_filename, co_name, line); |
| 6526 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6527 | } |
Mark Shannon | 8e1b406 | 2021-03-05 14:45:50 +0000 | [diff] [blame] | 6528 | trace_info->instr_prev = frame->f_lasti; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 6529 | } |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 6530 | |
| 6531 | |
| 6532 | /* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions |
| 6533 | for the limited API. */ |
| 6534 | |
| 6535 | #undef Py_EnterRecursiveCall |
| 6536 | |
| 6537 | int Py_EnterRecursiveCall(const char *where) |
| 6538 | { |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 6539 | return _Py_EnterRecursiveCall_inline(where); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | #undef Py_LeaveRecursiveCall |
| 6543 | |
| 6544 | void Py_LeaveRecursiveCall(void) |
| 6545 | { |
Victor Stinner | be434dc | 2019-11-05 00:51:22 +0100 | [diff] [blame] | 6546 | _Py_LeaveRecursiveCall_inline(); |
Victor Stinner | f4b1e3d | 2019-11-04 19:48:34 +0100 | [diff] [blame] | 6547 | } |