Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
| 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 13 | #include "pycore_object.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 14 | #include "pycore_pystate.h" |
Victor Stinner | ec13b93 | 2018-11-25 23:56:17 +0100 | [diff] [blame] | 15 | #include "pycore_tupleobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 17 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 18 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 20 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 21 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 22 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 23 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
| 26 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 27 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 28 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | #define LLTRACE 1 /* Low-level trace feature */ |
| 30 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 33 | /* Private API for the LOAD_METHOD opcode. */ |
| 34 | extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **); |
| 35 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 36 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 38 | /* Forward declarations */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 39 | Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t, |
| 40 | PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 41 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 43 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 44 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 45 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 46 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 47 | static int call_trace(Py_tracefunc, PyObject *, |
| 48 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 50 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 51 | PyThreadState *, PyFrameObject *, |
| 52 | int, PyObject *); |
| 53 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 54 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 55 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 56 | PyThreadState *, PyFrameObject *, |
| 57 | int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 58 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 59 | static void dtrace_function_entry(PyFrameObject *); |
| 60 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 63 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, |
| 64 | PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 65 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 66 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 67 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 68 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 69 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 70 | PyFrameObject *, const _Py_CODEUNIT *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 71 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 72 | static int check_args_iterable(PyObject *func, PyObject *vararg); |
| 73 | static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 74 | static void format_awaitable_error(PyTypeObject *, int); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 75 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 76 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 78 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 80 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | "free variable '%.200s' referenced before assignment" \ |
| 82 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 84 | /* Dynamic execution profile */ |
| 85 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 86 | #ifdef DXPAIRS |
| 87 | static long dxpairs[257][256]; |
| 88 | #define dxp dxpairs[256] |
| 89 | #else |
| 90 | static long dxp[256]; |
| 91 | #endif |
| 92 | #endif |
| 93 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 94 | #define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request) |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 95 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 96 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 97 | 1. We believe this is all right because the eval loop will release |
| 98 | the GIL eventually anyway. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 99 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | _Py_atomic_store_relaxed( \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 101 | &_PyRuntime.ceval.eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 102 | GIL_REQUEST | \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 103 | _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 104 | _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \ |
| 105 | _PyRuntime.ceval.pending.async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 106 | |
| 107 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 109 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \ |
| 110 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 112 | |
| 113 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 115 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | COMPUTE_EVAL_BREAKER(); \ |
| 117 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 118 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 119 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 120 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 122 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \ |
| 123 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 125 | |
| 126 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 128 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | COMPUTE_EVAL_BREAKER(); \ |
| 130 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 131 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 132 | #define SIGNAL_PENDING_SIGNALS() \ |
| 133 | do { \ |
| 134 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \ |
| 135 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
| 136 | } while (0) |
| 137 | |
| 138 | #define UNSIGNAL_PENDING_SIGNALS() \ |
| 139 | do { \ |
| 140 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \ |
| 141 | COMPUTE_EVAL_BREAKER(); \ |
| 142 | } while (0) |
| 143 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 144 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 146 | _PyRuntime.ceval.pending.async_exc = 1; \ |
| 147 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 149 | |
| 150 | #define UNSIGNAL_ASYNC_EXC() \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 151 | do { \ |
| 152 | _PyRuntime.ceval.pending.async_exc = 0; \ |
| 153 | COMPUTE_EVAL_BREAKER(); \ |
| 154 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 155 | |
| 156 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 157 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 158 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 159 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 160 | #include "pythread.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 161 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 162 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 163 | int |
| 164 | PyEval_ThreadsInitialized(void) |
| 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 169 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 170 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (gil_created()) |
| 173 | return; |
| 174 | create_gil(); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 175 | take_gil(_PyThreadState_GET()); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 176 | _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); |
| 177 | if (!_PyRuntime.ceval.pending.lock) |
| 178 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 179 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 181 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 182 | _PyEval_FiniThreads(void) |
| 183 | { |
| 184 | if (!gil_created()) |
| 185 | return; |
| 186 | destroy_gil(); |
| 187 | assert(!gil_created()); |
| 188 | } |
| 189 | |
| 190 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 191 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 192 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 193 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | if (tstate == NULL) |
| 195 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 196 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 200 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 203 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | in debug mode. |
| 205 | */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 206 | drop_gil(_PyThreadState_GET()); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 210 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 211 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | if (tstate == NULL) |
| 213 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 214 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 215 | assert(gil_created()); |
| 216 | take_gil(tstate); |
| 217 | if (PyThreadState_Swap(tstate) != NULL) |
| 218 | Py_FatalError( |
| 219 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 223 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | if (tstate == NULL) |
| 226 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 227 | if (PyThreadState_Swap(NULL) != tstate) |
| 228 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 229 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 230 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 232 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
| 233 | * which are not running in the child process, and clear internal locks |
| 234 | * which might be held by those threads. |
| 235 | */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 236 | |
| 237 | void |
| 238 | PyEval_ReInitThreads(void) |
| 239 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 240 | PyThreadState *current_tstate = _PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | if (!gil_created()) |
| 243 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | recreate_gil(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 245 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 246 | take_gil(current_tstate); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 247 | _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 249 | /* Destroy all threads except the current one */ |
| 250 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 251 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 253 | /* 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] | 254 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 255 | |
| 256 | void |
| 257 | _PyEval_SignalAsyncExc(void) |
| 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 260 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 262 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 263 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 266 | if (tstate == NULL) |
| 267 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 268 | assert(gil_created()); |
| 269 | drop_gil(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 274 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | if (tstate == NULL) |
| 277 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 278 | assert(gil_created()); |
| 279 | |
| 280 | int err = errno; |
| 281 | take_gil(tstate); |
| 282 | /* _Py_Finalizing is protected by the GIL */ |
| 283 | if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) { |
| 284 | drop_gil(tstate); |
| 285 | PyThread_exit_thread(); |
| 286 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | } |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 288 | errno = err; |
| 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 294 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 295 | signal handlers or Mac I/O completion routines) can schedule calls |
| 296 | to a function to be called synchronously. |
| 297 | The synchronous function is called with one void* argument. |
| 298 | It should return 0 for success or -1 for failure -- failure should |
| 299 | be accompanied by an exception. |
| 300 | |
| 301 | If registry succeeds, the registry function returns 0; if it fails |
| 302 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 303 | an exception condition). |
| 304 | |
| 305 | Note that because registry may occur from within signal handlers, |
| 306 | or other asynchronous events, calling malloc() is unsafe! |
| 307 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 308 | Any thread can schedule pending calls, but only the main thread |
| 309 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 310 | There is no facility to schedule calls to a particular thread, but |
| 311 | that should be easy to change, should that ever be required. In |
| 312 | that case, the static variables here should go into the python |
| 313 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 314 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 316 | void |
| 317 | _PyEval_SignalReceived(void) |
| 318 | { |
| 319 | /* bpo-30703: Function called when the C signal handler of Python gets a |
| 320 | signal. We cannot queue a callback using Py_AddPendingCall() since |
| 321 | that function is not async-signal-safe. */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 322 | SIGNAL_PENDING_SIGNALS(); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 325 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 326 | scheduling to be made from any thread, and even from an executing |
| 327 | callback. |
| 328 | */ |
| 329 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 330 | int |
| 331 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | int i, j, result=0; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 334 | PyThread_type_lock lock = _PyRuntime.ceval.pending.lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | /* try a few times for the lock. Since this mechanism is used |
| 337 | * for signal handling (on the main thread), there is a (slim) |
| 338 | * chance that a signal is delivered on the same thread while we |
| 339 | * hold the lock during the Py_MakePendingCalls() function. |
| 340 | * This avoids a deadlock in that case. |
| 341 | * Note that signals can be delivered on any thread. In particular, |
| 342 | * on Windows, a SIGINT is delivered on a system-created worker |
| 343 | * thread. |
| 344 | * We also check for lock being NULL, in the unlikely case that |
| 345 | * this function is called before any bytecode evaluation takes place. |
| 346 | */ |
| 347 | if (lock != NULL) { |
| 348 | for (i = 0; i<100; i++) { |
| 349 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 350 | break; |
| 351 | } |
| 352 | if (i == 100) |
| 353 | return -1; |
| 354 | } |
| 355 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 356 | i = _PyRuntime.ceval.pending.last; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 358 | if (j == _PyRuntime.ceval.pending.first) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | result = -1; /* Queue full */ |
| 360 | } else { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 361 | _PyRuntime.ceval.pending.calls[i].func = func; |
| 362 | _PyRuntime.ceval.pending.calls[i].arg = arg; |
| 363 | _PyRuntime.ceval.pending.last = j; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | } |
| 365 | /* signal main loop */ |
| 366 | SIGNAL_PENDING_CALLS(); |
| 367 | if (lock != NULL) |
| 368 | PyThread_release_lock(lock); |
| 369 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 372 | static int |
| 373 | handle_signals(void) |
| 374 | { |
| 375 | /* Only handle signals on main thread. */ |
| 376 | if (_PyRuntime.ceval.pending.main_thread && |
| 377 | PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread) |
| 378 | { |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | UNSIGNAL_PENDING_SIGNALS(); |
| 383 | if (PyErr_CheckSignals() < 0) { |
| 384 | SIGNAL_PENDING_SIGNALS(); /* We're not done yet */ |
| 385 | return -1; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int |
| 391 | make_pending_calls(void) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 392 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 393 | static int busy = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | /* only service pending calls on main thread */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 396 | if (_PyRuntime.ceval.pending.main_thread && |
| 397 | PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread) |
| 398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | return 0; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 400 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | /* don't perform recursive pending calls */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 403 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 405 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 406 | busy = 1; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 407 | /* unsignal before starting to call callbacks, so that any callback |
| 408 | added in-between re-signals */ |
| 409 | UNSIGNAL_PENDING_CALLS(); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 410 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 411 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 412 | if (!_PyRuntime.ceval.pending.lock) { |
| 413 | /* initial allocation of the lock */ |
| 414 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
| 415 | if (_PyRuntime.ceval.pending.lock == NULL) { |
| 416 | res = -1; |
| 417 | goto error; |
| 418 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | /* perform a bounded number of calls, in case of recursion */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 422 | for (int i=0; i<NPENDINGCALLS; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | int j; |
| 424 | int (*func)(void *); |
| 425 | void *arg = NULL; |
| 426 | |
| 427 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 428 | PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK); |
| 429 | j = _PyRuntime.ceval.pending.first; |
| 430 | if (j == _PyRuntime.ceval.pending.last) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | func = NULL; /* Queue empty */ |
| 432 | } else { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 433 | func = _PyRuntime.ceval.pending.calls[j].func; |
| 434 | arg = _PyRuntime.ceval.pending.calls[j].arg; |
| 435 | _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 437 | PyThread_release_lock(_PyRuntime.ceval.pending.lock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | /* having released the lock, perform the callback */ |
| 439 | if (func == NULL) |
| 440 | break; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 441 | res = func(arg); |
| 442 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 443 | goto error; |
| 444 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 446 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 447 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 448 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 449 | |
| 450 | error: |
| 451 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 452 | SIGNAL_PENDING_CALLS(); |
| 453 | return res; |
| 454 | } |
| 455 | |
| 456 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 457 | of backward-compatibility. */ |
| 458 | int |
| 459 | Py_MakePendingCalls(void) |
| 460 | { |
| 461 | assert(PyGILState_Check()); |
| 462 | |
| 463 | /* Python signal handler doesn't really queue a callback: it only signals |
| 464 | that a signal was received, see _PyEval_SignalReceived(). */ |
| 465 | int res = handle_signals(); |
| 466 | if (res != 0) { |
| 467 | return res; |
| 468 | } |
| 469 | |
| 470 | res = make_pending_calls(); |
| 471 | if (res != 0) { |
| 472 | return res; |
| 473 | } |
| 474 | |
| 475 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 478 | /* The interpreter's recursion limit */ |
| 479 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 480 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 481 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 482 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 483 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 484 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 485 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 486 | void |
| 487 | _PyEval_Initialize(struct _ceval_runtime_state *state) |
| 488 | { |
| 489 | state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 490 | _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
| 491 | _gil_initialize(&state->gil); |
| 492 | } |
| 493 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 494 | int |
| 495 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 496 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 497 | return _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 500 | void |
| 501 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 502 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 503 | _PyRuntime.ceval.recursion_limit = new_limit; |
| 504 | _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 507 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 508 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 509 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 510 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 511 | Without USE_STACKCHECK, there is no need for this. */ |
| 512 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 513 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 514 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 515 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 516 | int recursion_limit = _PyRuntime.ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 517 | |
| 518 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 519 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (PyOS_CheckStack()) { |
| 521 | --tstate->recursion_depth; |
| 522 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 523 | return -1; |
| 524 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 525 | /* Needed for ABI backwards-compatibility (see bpo-31857) */ |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 526 | _Py_CheckRecursionLimit = recursion_limit; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 527 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | if (tstate->recursion_critical) |
| 529 | /* Somebody asked that we don't check for recursion. */ |
| 530 | return 0; |
| 531 | if (tstate->overflowed) { |
| 532 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 533 | /* Overflowing while handling an overflow. Give up. */ |
| 534 | Py_FatalError("Cannot recover from stack overflow."); |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | if (tstate->recursion_depth > recursion_limit) { |
| 539 | --tstate->recursion_depth; |
| 540 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 541 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | "maximum recursion depth exceeded%s", |
| 543 | where); |
| 544 | return -1; |
| 545 | } |
| 546 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 549 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 550 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 551 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 552 | #define _Py_TracingPossible _PyRuntime.ceval.tracing_possible |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 553 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 554 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 555 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 556 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | return PyEval_EvalCodeEx(co, |
| 559 | globals, locals, |
| 560 | (PyObject **)NULL, 0, |
| 561 | (PyObject **)NULL, 0, |
| 562 | (PyObject **)NULL, 0, |
| 563 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
| 567 | /* Interpreter main loop */ |
| 568 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 569 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 570 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | /* This is for backward compatibility with extension modules that |
| 572 | used this API; core interpreter code should call |
| 573 | PyEval_EvalFrameEx() */ |
| 574 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 578 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 579 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 580 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 581 | return interp->eval_frame(f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 584 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 585 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 586 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 587 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 589 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 590 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 591 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 592 | int opcode; /* Current opcode */ |
| 593 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 594 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 596 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | is true when the line being executed has changed. The |
| 604 | initial values are such as to make this false the first |
| 605 | time it is tested. */ |
| 606 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 607 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 608 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | PyObject *names; |
| 610 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 611 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 612 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 613 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 614 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 615 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 616 | /* Computed GOTOs, or |
| 617 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 618 | using gcc's labels-as-values extension |
| 619 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 620 | |
| 621 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 623 | combined with a lookup table of jump addresses. However, since the |
| 624 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 625 | hard time making the right prediction for where to jump next (actually, |
| 626 | it will be always wrong except in the uncommon case of a sequence of |
| 627 | several identical opcodes). |
| 628 | |
| 629 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 630 | indirect jump instruction at the end of each opcode. Since the jump |
| 631 | instruction is at a different address for each opcode, the CPU will make a |
| 632 | separate prediction for each of these instructions, which is equivalent to |
| 633 | predicting the second opcode of each opcode pair. These predictions have |
| 634 | a much better chance to turn out valid, especially in small bytecode loops. |
| 635 | |
| 636 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 638 | and potentially many more instructions (depending on the pipeline width). |
| 639 | A correctly predicted branch, however, is nearly free. |
| 640 | |
| 641 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 642 | faster than the normal "switch" version, depending on the compiler and the |
| 643 | CPU architecture. |
| 644 | |
| 645 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 646 | because it would render the measurements invalid. |
| 647 | |
| 648 | |
| 649 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 650 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 651 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 652 | -fno-crossjumping). |
| 653 | */ |
| 654 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 655 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 656 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 657 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 658 | #endif |
| 659 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 660 | #ifdef HAVE_COMPUTED_GOTOS |
| 661 | #ifndef USE_COMPUTED_GOTOS |
| 662 | #define USE_COMPUTED_GOTOS 1 |
| 663 | #endif |
| 664 | #else |
| 665 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 666 | #error "Computed gotos are not supported on this compiler." |
| 667 | #endif |
| 668 | #undef USE_COMPUTED_GOTOS |
| 669 | #define USE_COMPUTED_GOTOS 0 |
| 670 | #endif |
| 671 | |
| 672 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 673 | /* Import the static jump table */ |
| 674 | #include "opcode_targets.h" |
| 675 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 676 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 677 | op: \ |
| 678 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 680 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 682 | if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | FAST_DISPATCH(); \ |
| 684 | } \ |
| 685 | continue; \ |
| 686 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 687 | |
| 688 | #ifdef LLTRACE |
| 689 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 691 | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 693 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 694 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | } \ |
| 696 | goto fast_next_opcode; \ |
| 697 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 698 | #else |
| 699 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 701 | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 703 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 704 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | } \ |
| 706 | goto fast_next_opcode; \ |
| 707 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 708 | #endif |
| 709 | |
| 710 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 711 | #define TARGET(op) op |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 712 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 713 | #define DISPATCH() continue |
| 714 | #define FAST_DISPATCH() goto fast_next_opcode |
| 715 | #endif |
| 716 | |
| 717 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 718 | /* Tuple access macros */ |
| 719 | |
| 720 | #ifndef Py_DEBUG |
| 721 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 722 | #else |
| 723 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 724 | #endif |
| 725 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 726 | /* Code access macros */ |
| 727 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 728 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 729 | #define INSTR_OFFSET() \ |
| 730 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 731 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 732 | _Py_CODEUNIT word = *next_instr; \ |
| 733 | opcode = _Py_OPCODE(word); \ |
| 734 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 735 | next_instr++; \ |
| 736 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 737 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 738 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 739 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 740 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | Some opcodes tend to come in pairs thus making it possible to |
| 742 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 743 | 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] | 744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | Verifying the prediction costs a single high-speed test of a register |
| 746 | variable against a constant. If the pairing was good, then the |
| 747 | processor's own internal branch predication has a high likelihood of |
| 748 | success, resulting in a nearly zero-overhead transition to the |
| 749 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 750 | including its unpredictable switch-case branch. Combined with the |
| 751 | processor's internal branch prediction, a successful PREDICT has the |
| 752 | effect of making the two opcodes run as if they were a single new opcode |
| 753 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 754 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 755 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | predictions turned-on and interpret the results as if some opcodes |
| 757 | had been combined or turn-off predictions so that the opcode frequency |
| 758 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 759 | |
| 760 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | the CPU to record separate branch prediction information for each |
| 762 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 763 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 764 | */ |
| 765 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 766 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 768 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 769 | #define PREDICT(op) \ |
| 770 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 771 | _Py_CODEUNIT word = *next_instr; \ |
| 772 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 773 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 774 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 775 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 776 | goto PRED_##op; \ |
| 777 | } \ |
| 778 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 779 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 780 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 781 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 782 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 783 | /* Stack manipulation macros */ |
| 784 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 785 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 786 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 787 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 788 | #define EMPTY() (STACK_LEVEL() == 0) |
| 789 | #define TOP() (stack_pointer[-1]) |
| 790 | #define SECOND() (stack_pointer[-2]) |
| 791 | #define THIRD() (stack_pointer[-3]) |
| 792 | #define FOURTH() (stack_pointer[-4]) |
| 793 | #define PEEK(n) (stack_pointer[-(n)]) |
| 794 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 795 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 796 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 797 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 798 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 799 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 800 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 801 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 802 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 803 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 805 | lltrace && prtrace(TOP(), "push")); \ |
| 806 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 808 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 809 | #define STACK_GROW(n) do { \ |
| 810 | assert(n >= 0); \ |
| 811 | (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 812 | lltrace && prtrace(TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 813 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 814 | } while (0) |
| 815 | #define STACK_SHRINK(n) do { \ |
| 816 | assert(n >= 0); \ |
| 817 | (void)(lltrace && prtrace(TOP(), "stackadj")); \ |
| 818 | (void)(BASIC_STACKADJ(-n)); \ |
| 819 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 820 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 821 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 822 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 823 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 824 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 825 | #define PUSH(v) BASIC_PUSH(v) |
| 826 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 827 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 828 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 829 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 830 | #endif |
| 831 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 832 | /* Local variable macros */ |
| 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 835 | |
| 836 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 837 | then store the new value; it must copy the old value to a temporary |
| 838 | value, then store the new value, and then DECREF the temporary value. |
| 839 | This is because it is possible that during the DECREF the frame is |
| 840 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 841 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 843 | GETLOCAL(i) = value; \ |
| 844 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 845 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 846 | |
| 847 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 849 | PyObject *v = POP(); \ |
| 850 | Py_XDECREF(v); \ |
| 851 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 852 | |
| 853 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 854 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 856 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 858 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 859 | value = POP(); \ |
| 860 | Py_XDECREF(value); \ |
| 861 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 862 | exc_info = tstate->exc_info; \ |
| 863 | type = exc_info->exc_type; \ |
| 864 | value = exc_info->exc_value; \ |
| 865 | traceback = exc_info->exc_traceback; \ |
| 866 | exc_info->exc_type = POP(); \ |
| 867 | exc_info->exc_value = POP(); \ |
| 868 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | Py_XDECREF(type); \ |
| 870 | Py_XDECREF(value); \ |
| 871 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 872 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 873 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 874 | /* Start of code */ |
| 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | /* push frame */ |
| 877 | if (Py_EnterRecursiveCall("")) |
| 878 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | if (tstate->use_tracing) { |
| 883 | if (tstate->c_tracefunc != NULL) { |
| 884 | /* tstate->c_tracefunc, if defined, is a |
| 885 | function that will be called on *every* entry |
| 886 | to a code block. Its return value, if not |
| 887 | None, is a function that will be called at |
| 888 | the start of each executed line of code. |
| 889 | (Actually, the function must return itself |
| 890 | in order to continue tracing.) The trace |
| 891 | functions are called with three arguments: |
| 892 | a pointer to the current frame, a string |
| 893 | indicating why the function is called, and |
| 894 | an argument which depends on the situation. |
| 895 | The global trace function is also called |
| 896 | whenever an exception is detected. */ |
| 897 | if (call_trace_protected(tstate->c_tracefunc, |
| 898 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 899 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | /* Trace function raised an error */ |
| 901 | goto exit_eval_frame; |
| 902 | } |
| 903 | } |
| 904 | if (tstate->c_profilefunc != NULL) { |
| 905 | /* Similar for c_profilefunc, except it needn't |
| 906 | return itself and isn't called for "line" events */ |
| 907 | if (call_trace_protected(tstate->c_profilefunc, |
| 908 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 909 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | /* Profile function raised an error */ |
| 911 | goto exit_eval_frame; |
| 912 | } |
| 913 | } |
| 914 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 915 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 916 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 917 | dtrace_function_entry(f); |
| 918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | co = f->f_code; |
| 920 | names = co->co_names; |
| 921 | consts = co->co_consts; |
| 922 | fastlocals = f->f_localsplus; |
| 923 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 924 | assert(PyBytes_Check(co->co_code)); |
| 925 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 926 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 927 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 928 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 929 | /* |
| 930 | f->f_lasti refers to the index of the last instruction, |
| 931 | 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] | 932 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 933 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 934 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 937 | direct succession without updating f->f_lasti. A successful |
| 938 | prediction effectively links the two codes together as if they |
| 939 | were a single new opcode; accordingly,f->f_lasti will point to |
| 940 | the first code in the pair (for instance, GET_ITER followed by |
| 941 | 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] | 942 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 944 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 945 | next_instr = first_instr; |
| 946 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 947 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 948 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 949 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | stack_pointer = f->f_stacktop; |
| 951 | assert(stack_pointer != NULL); |
| 952 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 953 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 954 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 955 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 956 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 957 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 958 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 959 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 960 | if (throwflag) /* support for generator.throw() */ |
| 961 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 962 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 963 | #ifdef Py_DEBUG |
| 964 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 965 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 966 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 967 | assert(!PyErr_Occurred()); |
| 968 | #endif |
| 969 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 970 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 973 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 974 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | /* Do periodic things. Doing this every time through |
| 977 | the loop would add too much overhead, so we do it |
| 978 | only every Nth instruction. We also do it if |
| 979 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 980 | event needs attention (e.g. a signal handler or |
| 981 | async I/O handler); see Py_AddPendingCall() and |
| 982 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 983 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 984 | if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 985 | opcode = _Py_OPCODE(*next_instr); |
| 986 | if (opcode == SETUP_FINALLY || |
| 987 | opcode == SETUP_WITH || |
| 988 | opcode == BEFORE_ASYNC_WITH || |
| 989 | opcode == YIELD_FROM) { |
| 990 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 991 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 992 | - If we're about to enter the 'with:'. It will prevent |
| 993 | emitting a resource warning in the common idiom |
| 994 | 'with open(path) as file:'. |
| 995 | - If we're about to enter the 'async with:'. |
| 996 | - 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] | 997 | *very* useful, but might help in some cases and it's |
| 998 | traditional) |
| 999 | - If we're resuming a chain of nested 'yield from' or |
| 1000 | 'await' calls, then each frame is parked with YIELD_FROM |
| 1001 | as its next opcode. If the user hit control-C we want to |
| 1002 | wait until we've reached the innermost frame before |
| 1003 | running the signal handler and raising KeyboardInterrupt |
| 1004 | (see bpo-30039). |
| 1005 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | goto fast_next_opcode; |
| 1007 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 1008 | |
| 1009 | if (_Py_atomic_load_relaxed( |
| 1010 | &_PyRuntime.ceval.signals_pending)) |
| 1011 | { |
| 1012 | if (handle_signals() != 0) { |
| 1013 | goto error; |
| 1014 | } |
| 1015 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1016 | if (_Py_atomic_load_relaxed( |
| 1017 | &_PyRuntime.ceval.pending.calls_to_do)) |
| 1018 | { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 1019 | if (make_pending_calls() != 0) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1020 | goto error; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 1021 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame^] | 1023 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1024 | if (_Py_atomic_load_relaxed( |
| 1025 | &_PyRuntime.ceval.gil_drop_request)) |
| 1026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | /* Give another thread a chance */ |
| 1028 | if (PyThreadState_Swap(NULL) != tstate) |
| 1029 | Py_FatalError("ceval: tstate mix-up"); |
| 1030 | drop_gil(tstate); |
| 1031 | |
| 1032 | /* Other threads may run now */ |
| 1033 | |
| 1034 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* Check if we should make a quick exit. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1037 | if (_Py_IsFinalizing() && |
| 1038 | !_Py_CURRENTLY_FINALIZING(tstate)) |
| 1039 | { |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1040 | drop_gil(tstate); |
| 1041 | PyThread_exit_thread(); |
| 1042 | } |
| 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | if (PyThreadState_Swap(tstate) != NULL) |
| 1045 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | } |
| 1047 | /* Check for asynchronous exceptions. */ |
| 1048 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1049 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | tstate->async_exc = NULL; |
| 1051 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1052 | PyErr_SetNone(exc); |
| 1053 | Py_DECREF(exc); |
| 1054 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | fast_next_opcode: |
| 1059 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1060 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1061 | if (PyDTrace_LINE_ENABLED()) |
| 1062 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1067 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1068 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | /* see maybe_call_line_trace |
| 1070 | for expository comments */ |
| 1071 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1074 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1075 | tstate, f, |
| 1076 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | /* Reload possibly changed frame fields */ |
| 1078 | JUMPTO(f->f_lasti); |
| 1079 | if (f->f_stacktop != NULL) { |
| 1080 | stack_pointer = f->f_stacktop; |
| 1081 | f->f_stacktop = NULL; |
| 1082 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1083 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1085 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1089 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1090 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1091 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1092 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1093 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | dxpairs[lastopcode][opcode]++; |
| 1095 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1096 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1098 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1099 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1100 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | if (lltrace) { |
| 1104 | if (HAS_ARG(opcode)) { |
| 1105 | printf("%d: %d, %d\n", |
| 1106 | f->f_lasti, opcode, oparg); |
| 1107 | } |
| 1108 | else { |
| 1109 | printf("%d: %d\n", |
| 1110 | f->f_lasti, opcode); |
| 1111 | } |
| 1112 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1113 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1118 | It is essential that any operation that fails must goto error |
| 1119 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1120 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1121 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1123 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1124 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1125 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1126 | PyObject *value = GETLOCAL(oparg); |
| 1127 | if (value == NULL) { |
| 1128 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1129 | UNBOUNDLOCAL_ERROR_MSG, |
| 1130 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1131 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1133 | Py_INCREF(value); |
| 1134 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1138 | case TARGET(LOAD_CONST): { |
| 1139 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1140 | PyObject *value = GETITEM(consts, oparg); |
| 1141 | Py_INCREF(value); |
| 1142 | PUSH(value); |
| 1143 | FAST_DISPATCH(); |
| 1144 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1145 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1146 | case TARGET(STORE_FAST): { |
| 1147 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1148 | PyObject *value = POP(); |
| 1149 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1151 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1152 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1153 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1154 | PyObject *value = POP(); |
| 1155 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1157 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1158 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1159 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1160 | PyObject *top = TOP(); |
| 1161 | PyObject *second = SECOND(); |
| 1162 | SET_TOP(second); |
| 1163 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1165 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1166 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1167 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1168 | PyObject *top = TOP(); |
| 1169 | PyObject *second = SECOND(); |
| 1170 | PyObject *third = THIRD(); |
| 1171 | SET_TOP(second); |
| 1172 | SET_SECOND(third); |
| 1173 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1175 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1176 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1177 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1178 | PyObject *top = TOP(); |
| 1179 | PyObject *second = SECOND(); |
| 1180 | PyObject *third = THIRD(); |
| 1181 | PyObject *fourth = FOURTH(); |
| 1182 | SET_TOP(second); |
| 1183 | SET_SECOND(third); |
| 1184 | SET_THIRD(fourth); |
| 1185 | SET_FOURTH(top); |
| 1186 | FAST_DISPATCH(); |
| 1187 | } |
| 1188 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1189 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1190 | PyObject *top = TOP(); |
| 1191 | Py_INCREF(top); |
| 1192 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1194 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1195 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1196 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1197 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1198 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1199 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1200 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1201 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1202 | SET_TOP(top); |
| 1203 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1204 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1205 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1206 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1207 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1208 | PyObject *value = TOP(); |
| 1209 | PyObject *res = PyNumber_Positive(value); |
| 1210 | Py_DECREF(value); |
| 1211 | SET_TOP(res); |
| 1212 | if (res == NULL) |
| 1213 | goto error; |
| 1214 | DISPATCH(); |
| 1215 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1216 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1217 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1218 | PyObject *value = TOP(); |
| 1219 | PyObject *res = PyNumber_Negative(value); |
| 1220 | Py_DECREF(value); |
| 1221 | SET_TOP(res); |
| 1222 | if (res == NULL) |
| 1223 | goto error; |
| 1224 | DISPATCH(); |
| 1225 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1226 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1227 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1228 | PyObject *value = TOP(); |
| 1229 | int err = PyObject_IsTrue(value); |
| 1230 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | if (err == 0) { |
| 1232 | Py_INCREF(Py_True); |
| 1233 | SET_TOP(Py_True); |
| 1234 | DISPATCH(); |
| 1235 | } |
| 1236 | else if (err > 0) { |
| 1237 | Py_INCREF(Py_False); |
| 1238 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | DISPATCH(); |
| 1240 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1241 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1242 | goto error; |
| 1243 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1244 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1245 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1246 | PyObject *value = TOP(); |
| 1247 | PyObject *res = PyNumber_Invert(value); |
| 1248 | Py_DECREF(value); |
| 1249 | SET_TOP(res); |
| 1250 | if (res == NULL) |
| 1251 | goto error; |
| 1252 | DISPATCH(); |
| 1253 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1254 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1255 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1256 | PyObject *exp = POP(); |
| 1257 | PyObject *base = TOP(); |
| 1258 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1259 | Py_DECREF(base); |
| 1260 | Py_DECREF(exp); |
| 1261 | SET_TOP(res); |
| 1262 | if (res == NULL) |
| 1263 | goto error; |
| 1264 | DISPATCH(); |
| 1265 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1266 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1267 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1268 | PyObject *right = POP(); |
| 1269 | PyObject *left = TOP(); |
| 1270 | PyObject *res = PyNumber_Multiply(left, right); |
| 1271 | Py_DECREF(left); |
| 1272 | Py_DECREF(right); |
| 1273 | SET_TOP(res); |
| 1274 | if (res == NULL) |
| 1275 | goto error; |
| 1276 | DISPATCH(); |
| 1277 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1278 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1279 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1280 | PyObject *right = POP(); |
| 1281 | PyObject *left = TOP(); |
| 1282 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1283 | Py_DECREF(left); |
| 1284 | Py_DECREF(right); |
| 1285 | SET_TOP(res); |
| 1286 | if (res == NULL) |
| 1287 | goto error; |
| 1288 | DISPATCH(); |
| 1289 | } |
| 1290 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1291 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1292 | PyObject *divisor = POP(); |
| 1293 | PyObject *dividend = TOP(); |
| 1294 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1295 | Py_DECREF(dividend); |
| 1296 | Py_DECREF(divisor); |
| 1297 | SET_TOP(quotient); |
| 1298 | if (quotient == NULL) |
| 1299 | goto error; |
| 1300 | DISPATCH(); |
| 1301 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1302 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1303 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1304 | PyObject *divisor = POP(); |
| 1305 | PyObject *dividend = TOP(); |
| 1306 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1307 | Py_DECREF(dividend); |
| 1308 | Py_DECREF(divisor); |
| 1309 | SET_TOP(quotient); |
| 1310 | if (quotient == NULL) |
| 1311 | goto error; |
| 1312 | DISPATCH(); |
| 1313 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1314 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1315 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1316 | PyObject *divisor = POP(); |
| 1317 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1318 | PyObject *res; |
| 1319 | if (PyUnicode_CheckExact(dividend) && ( |
| 1320 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1321 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1322 | // (see issue28598) |
| 1323 | res = PyUnicode_Format(dividend, divisor); |
| 1324 | } else { |
| 1325 | res = PyNumber_Remainder(dividend, divisor); |
| 1326 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1327 | Py_DECREF(divisor); |
| 1328 | Py_DECREF(dividend); |
| 1329 | SET_TOP(res); |
| 1330 | if (res == NULL) |
| 1331 | goto error; |
| 1332 | DISPATCH(); |
| 1333 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1334 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1335 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1336 | PyObject *right = POP(); |
| 1337 | PyObject *left = TOP(); |
| 1338 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1339 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1340 | CPython using bytecode, it is simply worthless. |
| 1341 | See http://bugs.python.org/issue21955 and |
| 1342 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1343 | no patch shown any impact on a realistic benchmark, only a minor |
| 1344 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1345 | if (PyUnicode_CheckExact(left) && |
| 1346 | PyUnicode_CheckExact(right)) { |
| 1347 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1348 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1349 | } |
| 1350 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1351 | sum = PyNumber_Add(left, right); |
| 1352 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1353 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1354 | Py_DECREF(right); |
| 1355 | SET_TOP(sum); |
| 1356 | if (sum == NULL) |
| 1357 | goto error; |
| 1358 | DISPATCH(); |
| 1359 | } |
| 1360 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1361 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1362 | PyObject *right = POP(); |
| 1363 | PyObject *left = TOP(); |
| 1364 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1365 | Py_DECREF(right); |
| 1366 | Py_DECREF(left); |
| 1367 | SET_TOP(diff); |
| 1368 | if (diff == NULL) |
| 1369 | goto error; |
| 1370 | DISPATCH(); |
| 1371 | } |
| 1372 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1373 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1374 | PyObject *sub = POP(); |
| 1375 | PyObject *container = TOP(); |
| 1376 | PyObject *res = PyObject_GetItem(container, sub); |
| 1377 | Py_DECREF(container); |
| 1378 | Py_DECREF(sub); |
| 1379 | SET_TOP(res); |
| 1380 | if (res == NULL) |
| 1381 | goto error; |
| 1382 | DISPATCH(); |
| 1383 | } |
| 1384 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1385 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1386 | PyObject *right = POP(); |
| 1387 | PyObject *left = TOP(); |
| 1388 | PyObject *res = PyNumber_Lshift(left, right); |
| 1389 | Py_DECREF(left); |
| 1390 | Py_DECREF(right); |
| 1391 | SET_TOP(res); |
| 1392 | if (res == NULL) |
| 1393 | goto error; |
| 1394 | DISPATCH(); |
| 1395 | } |
| 1396 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1397 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1398 | PyObject *right = POP(); |
| 1399 | PyObject *left = TOP(); |
| 1400 | PyObject *res = PyNumber_Rshift(left, right); |
| 1401 | Py_DECREF(left); |
| 1402 | Py_DECREF(right); |
| 1403 | SET_TOP(res); |
| 1404 | if (res == NULL) |
| 1405 | goto error; |
| 1406 | DISPATCH(); |
| 1407 | } |
| 1408 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1409 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1410 | PyObject *right = POP(); |
| 1411 | PyObject *left = TOP(); |
| 1412 | PyObject *res = PyNumber_And(left, right); |
| 1413 | Py_DECREF(left); |
| 1414 | Py_DECREF(right); |
| 1415 | SET_TOP(res); |
| 1416 | if (res == NULL) |
| 1417 | goto error; |
| 1418 | DISPATCH(); |
| 1419 | } |
| 1420 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1421 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1422 | PyObject *right = POP(); |
| 1423 | PyObject *left = TOP(); |
| 1424 | PyObject *res = PyNumber_Xor(left, right); |
| 1425 | Py_DECREF(left); |
| 1426 | Py_DECREF(right); |
| 1427 | SET_TOP(res); |
| 1428 | if (res == NULL) |
| 1429 | goto error; |
| 1430 | DISPATCH(); |
| 1431 | } |
| 1432 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1433 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1434 | PyObject *right = POP(); |
| 1435 | PyObject *left = TOP(); |
| 1436 | PyObject *res = PyNumber_Or(left, right); |
| 1437 | Py_DECREF(left); |
| 1438 | Py_DECREF(right); |
| 1439 | SET_TOP(res); |
| 1440 | if (res == NULL) |
| 1441 | goto error; |
| 1442 | DISPATCH(); |
| 1443 | } |
| 1444 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1445 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1446 | PyObject *v = POP(); |
| 1447 | PyObject *list = PEEK(oparg); |
| 1448 | int err; |
| 1449 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1451 | if (err != 0) |
| 1452 | goto error; |
| 1453 | PREDICT(JUMP_ABSOLUTE); |
| 1454 | DISPATCH(); |
| 1455 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1456 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1457 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1458 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1459 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1460 | int err; |
| 1461 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1463 | if (err != 0) |
| 1464 | goto error; |
| 1465 | PREDICT(JUMP_ABSOLUTE); |
| 1466 | DISPATCH(); |
| 1467 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1468 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1469 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1470 | PyObject *exp = POP(); |
| 1471 | PyObject *base = TOP(); |
| 1472 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1473 | Py_DECREF(base); |
| 1474 | Py_DECREF(exp); |
| 1475 | SET_TOP(res); |
| 1476 | if (res == NULL) |
| 1477 | goto error; |
| 1478 | DISPATCH(); |
| 1479 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1480 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1481 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1482 | PyObject *right = POP(); |
| 1483 | PyObject *left = TOP(); |
| 1484 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1485 | Py_DECREF(left); |
| 1486 | Py_DECREF(right); |
| 1487 | SET_TOP(res); |
| 1488 | if (res == NULL) |
| 1489 | goto error; |
| 1490 | DISPATCH(); |
| 1491 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1492 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1493 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1494 | PyObject *right = POP(); |
| 1495 | PyObject *left = TOP(); |
| 1496 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1497 | Py_DECREF(left); |
| 1498 | Py_DECREF(right); |
| 1499 | SET_TOP(res); |
| 1500 | if (res == NULL) |
| 1501 | goto error; |
| 1502 | DISPATCH(); |
| 1503 | } |
| 1504 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1505 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1506 | PyObject *divisor = POP(); |
| 1507 | PyObject *dividend = TOP(); |
| 1508 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1509 | Py_DECREF(dividend); |
| 1510 | Py_DECREF(divisor); |
| 1511 | SET_TOP(quotient); |
| 1512 | if (quotient == NULL) |
| 1513 | goto error; |
| 1514 | DISPATCH(); |
| 1515 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1516 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1517 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1518 | PyObject *divisor = POP(); |
| 1519 | PyObject *dividend = TOP(); |
| 1520 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1521 | Py_DECREF(dividend); |
| 1522 | Py_DECREF(divisor); |
| 1523 | SET_TOP(quotient); |
| 1524 | if (quotient == NULL) |
| 1525 | goto error; |
| 1526 | DISPATCH(); |
| 1527 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1528 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1529 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1530 | PyObject *right = POP(); |
| 1531 | PyObject *left = TOP(); |
| 1532 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1533 | Py_DECREF(left); |
| 1534 | Py_DECREF(right); |
| 1535 | SET_TOP(mod); |
| 1536 | if (mod == NULL) |
| 1537 | goto error; |
| 1538 | DISPATCH(); |
| 1539 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1540 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1541 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1542 | PyObject *right = POP(); |
| 1543 | PyObject *left = TOP(); |
| 1544 | PyObject *sum; |
| 1545 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1546 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1547 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1548 | } |
| 1549 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1550 | sum = PyNumber_InPlaceAdd(left, right); |
| 1551 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1552 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1553 | Py_DECREF(right); |
| 1554 | SET_TOP(sum); |
| 1555 | if (sum == NULL) |
| 1556 | goto error; |
| 1557 | DISPATCH(); |
| 1558 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1559 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1560 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1561 | PyObject *right = POP(); |
| 1562 | PyObject *left = TOP(); |
| 1563 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1564 | Py_DECREF(left); |
| 1565 | Py_DECREF(right); |
| 1566 | SET_TOP(diff); |
| 1567 | if (diff == NULL) |
| 1568 | goto error; |
| 1569 | DISPATCH(); |
| 1570 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1571 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1572 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1573 | PyObject *right = POP(); |
| 1574 | PyObject *left = TOP(); |
| 1575 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1576 | Py_DECREF(left); |
| 1577 | Py_DECREF(right); |
| 1578 | SET_TOP(res); |
| 1579 | if (res == NULL) |
| 1580 | goto error; |
| 1581 | DISPATCH(); |
| 1582 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1583 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1584 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1585 | PyObject *right = POP(); |
| 1586 | PyObject *left = TOP(); |
| 1587 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1588 | Py_DECREF(left); |
| 1589 | Py_DECREF(right); |
| 1590 | SET_TOP(res); |
| 1591 | if (res == NULL) |
| 1592 | goto error; |
| 1593 | DISPATCH(); |
| 1594 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1595 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1596 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1597 | PyObject *right = POP(); |
| 1598 | PyObject *left = TOP(); |
| 1599 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1600 | Py_DECREF(left); |
| 1601 | Py_DECREF(right); |
| 1602 | SET_TOP(res); |
| 1603 | if (res == NULL) |
| 1604 | goto error; |
| 1605 | DISPATCH(); |
| 1606 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1607 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1608 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1609 | PyObject *right = POP(); |
| 1610 | PyObject *left = TOP(); |
| 1611 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1612 | Py_DECREF(left); |
| 1613 | Py_DECREF(right); |
| 1614 | SET_TOP(res); |
| 1615 | if (res == NULL) |
| 1616 | goto error; |
| 1617 | DISPATCH(); |
| 1618 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1619 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1620 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1621 | PyObject *right = POP(); |
| 1622 | PyObject *left = TOP(); |
| 1623 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1624 | Py_DECREF(left); |
| 1625 | Py_DECREF(right); |
| 1626 | SET_TOP(res); |
| 1627 | if (res == NULL) |
| 1628 | goto error; |
| 1629 | DISPATCH(); |
| 1630 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1631 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1632 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1633 | PyObject *sub = TOP(); |
| 1634 | PyObject *container = SECOND(); |
| 1635 | PyObject *v = THIRD(); |
| 1636 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1637 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1638 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1639 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1641 | Py_DECREF(container); |
| 1642 | Py_DECREF(sub); |
| 1643 | if (err != 0) |
| 1644 | goto error; |
| 1645 | DISPATCH(); |
| 1646 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1647 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1648 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1649 | PyObject *sub = TOP(); |
| 1650 | PyObject *container = SECOND(); |
| 1651 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1652 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1653 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1654 | err = PyObject_DelItem(container, sub); |
| 1655 | Py_DECREF(container); |
| 1656 | Py_DECREF(sub); |
| 1657 | if (err != 0) |
| 1658 | goto error; |
| 1659 | DISPATCH(); |
| 1660 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1661 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1662 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1663 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1664 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1665 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1666 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1667 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | PyErr_SetString(PyExc_RuntimeError, |
| 1669 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1670 | Py_DECREF(value); |
| 1671 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1673 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1674 | Py_DECREF(value); |
| 1675 | if (res == NULL) |
| 1676 | goto error; |
| 1677 | Py_DECREF(res); |
| 1678 | DISPATCH(); |
| 1679 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1680 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1681 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1682 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | switch (oparg) { |
| 1684 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1685 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1686 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1688 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1689 | /* fall through */ |
| 1690 | case 0: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1691 | if (do_raise(exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1692 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1693 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | default: |
| 1696 | PyErr_SetString(PyExc_SystemError, |
| 1697 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | break; |
| 1699 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1700 | goto error; |
| 1701 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1702 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1703 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1705 | assert(f->f_iblock == 0); |
| 1706 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1707 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1708 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1709 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1710 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1711 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1712 | PyObject *obj = TOP(); |
| 1713 | PyTypeObject *type = Py_TYPE(obj); |
| 1714 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1715 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1716 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1717 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1718 | |
| 1719 | if (getter != NULL) { |
| 1720 | iter = (*getter)(obj); |
| 1721 | Py_DECREF(obj); |
| 1722 | if (iter == NULL) { |
| 1723 | SET_TOP(NULL); |
| 1724 | goto error; |
| 1725 | } |
| 1726 | } |
| 1727 | else { |
| 1728 | SET_TOP(NULL); |
| 1729 | PyErr_Format( |
| 1730 | PyExc_TypeError, |
| 1731 | "'async for' requires an object with " |
| 1732 | "__aiter__ method, got %.100s", |
| 1733 | type->tp_name); |
| 1734 | Py_DECREF(obj); |
| 1735 | goto error; |
| 1736 | } |
| 1737 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1738 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 1739 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1740 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1741 | SET_TOP(NULL); |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1742 | PyErr_Format( |
| 1743 | PyExc_TypeError, |
| 1744 | "'async for' received an object from __aiter__ " |
| 1745 | "that does not implement __anext__: %.100s", |
| 1746 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1747 | Py_DECREF(iter); |
| 1748 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1749 | } |
| 1750 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1751 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1752 | DISPATCH(); |
| 1753 | } |
| 1754 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1755 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1756 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1757 | PyObject *next_iter = NULL; |
| 1758 | PyObject *awaitable = NULL; |
| 1759 | PyObject *aiter = TOP(); |
| 1760 | PyTypeObject *type = Py_TYPE(aiter); |
| 1761 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1762 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1763 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1764 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1765 | goto error; |
| 1766 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1767 | } else { |
| 1768 | if (type->tp_as_async != NULL){ |
| 1769 | getter = type->tp_as_async->am_anext; |
| 1770 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1771 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1772 | if (getter != NULL) { |
| 1773 | next_iter = (*getter)(aiter); |
| 1774 | if (next_iter == NULL) { |
| 1775 | goto error; |
| 1776 | } |
| 1777 | } |
| 1778 | else { |
| 1779 | PyErr_Format( |
| 1780 | PyExc_TypeError, |
| 1781 | "'async for' requires an iterator with " |
| 1782 | "__anext__ method, got %.100s", |
| 1783 | type->tp_name); |
| 1784 | goto error; |
| 1785 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1786 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1787 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1788 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1789 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1790 | PyExc_TypeError, |
| 1791 | "'async for' received an invalid object " |
| 1792 | "from __anext__: %.100s", |
| 1793 | Py_TYPE(next_iter)->tp_name); |
| 1794 | |
| 1795 | Py_DECREF(next_iter); |
| 1796 | goto error; |
| 1797 | } else { |
| 1798 | Py_DECREF(next_iter); |
| 1799 | } |
| 1800 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1801 | |
| 1802 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1803 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1804 | DISPATCH(); |
| 1805 | } |
| 1806 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1807 | case TARGET(GET_AWAITABLE): { |
| 1808 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1809 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1810 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1811 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 1812 | if (iter == NULL) { |
| 1813 | format_awaitable_error(Py_TYPE(iterable), |
| 1814 | _Py_OPCODE(next_instr[-2])); |
| 1815 | } |
| 1816 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1817 | Py_DECREF(iterable); |
| 1818 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1819 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1820 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1821 | if (yf != NULL) { |
| 1822 | /* `iter` is a coroutine object that is being |
| 1823 | awaited, `yf` is a pointer to the current awaitable |
| 1824 | being awaited on. */ |
| 1825 | Py_DECREF(yf); |
| 1826 | Py_CLEAR(iter); |
| 1827 | PyErr_SetString( |
| 1828 | PyExc_RuntimeError, |
| 1829 | "coroutine is being awaited already"); |
| 1830 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 1831 | } |
| 1832 | } |
| 1833 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1834 | SET_TOP(iter); /* Even if it's NULL */ |
| 1835 | |
| 1836 | if (iter == NULL) { |
| 1837 | goto error; |
| 1838 | } |
| 1839 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1840 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1841 | DISPATCH(); |
| 1842 | } |
| 1843 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1844 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1845 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1846 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1847 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1848 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 1849 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1850 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 1851 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1852 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1853 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1854 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1855 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1856 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1857 | Py_DECREF(v); |
| 1858 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1859 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1860 | if (tstate->c_tracefunc != NULL |
| 1861 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1862 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 1863 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1864 | if (err < 0) |
| 1865 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1866 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1867 | SET_TOP(val); |
| 1868 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1869 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1870 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1871 | f->f_stacktop = stack_pointer; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1872 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 1873 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1874 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1875 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1876 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1877 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1878 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1880 | |
| 1881 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 1882 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 1883 | Py_DECREF(retval); |
| 1884 | if (w == NULL) { |
| 1885 | retval = NULL; |
| 1886 | goto error; |
| 1887 | } |
| 1888 | retval = w; |
| 1889 | } |
| 1890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | f->f_stacktop = stack_pointer; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1892 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1893 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1894 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1895 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1896 | PyObject *type, *value, *traceback; |
| 1897 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1898 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1899 | if (b->b_type != EXCEPT_HANDLER) { |
| 1900 | PyErr_SetString(PyExc_SystemError, |
| 1901 | "popped block is not an except handler"); |
| 1902 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1904 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 1905 | STACK_LEVEL() <= (b)->b_level + 4); |
| 1906 | exc_info = tstate->exc_info; |
| 1907 | type = exc_info->exc_type; |
| 1908 | value = exc_info->exc_value; |
| 1909 | traceback = exc_info->exc_traceback; |
| 1910 | exc_info->exc_type = POP(); |
| 1911 | exc_info->exc_value = POP(); |
| 1912 | exc_info->exc_traceback = POP(); |
| 1913 | Py_XDECREF(type); |
| 1914 | Py_XDECREF(value); |
| 1915 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1917 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1918 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1919 | case TARGET(POP_BLOCK): { |
| 1920 | PREDICTED(POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1921 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1923 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1924 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1925 | case TARGET(POP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1926 | /* If oparg is 0 at the top of the stack are 1 or 6 values: |
| 1927 | Either: |
| 1928 | - TOP = NULL or an integer |
| 1929 | or: |
| 1930 | - (TOP, SECOND, THIRD) = exc_info() |
| 1931 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 1932 | |
| 1933 | If oparg is 1 the value for 'return' was additionally pushed |
| 1934 | at the top of the stack. |
| 1935 | */ |
| 1936 | PyObject *res = NULL; |
| 1937 | if (oparg) { |
| 1938 | res = POP(); |
| 1939 | } |
| 1940 | PyObject *exc = POP(); |
| 1941 | if (exc == NULL || PyLong_CheckExact(exc)) { |
| 1942 | Py_XDECREF(exc); |
| 1943 | } |
| 1944 | else { |
| 1945 | Py_DECREF(exc); |
| 1946 | Py_DECREF(POP()); |
| 1947 | Py_DECREF(POP()); |
| 1948 | |
| 1949 | PyObject *type, *value, *traceback; |
| 1950 | _PyErr_StackItem *exc_info; |
| 1951 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1952 | if (b->b_type != EXCEPT_HANDLER) { |
| 1953 | PyErr_SetString(PyExc_SystemError, |
| 1954 | "popped block is not an except handler"); |
| 1955 | Py_XDECREF(res); |
| 1956 | goto error; |
| 1957 | } |
| 1958 | assert(STACK_LEVEL() == (b)->b_level + 3); |
| 1959 | exc_info = tstate->exc_info; |
| 1960 | type = exc_info->exc_type; |
| 1961 | value = exc_info->exc_value; |
| 1962 | traceback = exc_info->exc_traceback; |
| 1963 | exc_info->exc_type = POP(); |
| 1964 | exc_info->exc_value = POP(); |
| 1965 | exc_info->exc_traceback = POP(); |
| 1966 | Py_XDECREF(type); |
| 1967 | Py_XDECREF(value); |
| 1968 | Py_XDECREF(traceback); |
| 1969 | } |
| 1970 | if (oparg) { |
| 1971 | PUSH(res); |
| 1972 | } |
| 1973 | DISPATCH(); |
| 1974 | } |
| 1975 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1976 | case TARGET(CALL_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1977 | PyObject *ret = PyLong_FromLong(INSTR_OFFSET()); |
| 1978 | if (ret == NULL) { |
| 1979 | goto error; |
| 1980 | } |
| 1981 | PUSH(ret); |
| 1982 | JUMPBY(oparg); |
| 1983 | FAST_DISPATCH(); |
| 1984 | } |
| 1985 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1986 | case TARGET(BEGIN_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1987 | /* Push NULL onto the stack for using it in END_FINALLY, |
| 1988 | POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH. |
| 1989 | */ |
| 1990 | PUSH(NULL); |
| 1991 | FAST_DISPATCH(); |
| 1992 | } |
| 1993 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1994 | case TARGET(END_FINALLY): { |
| 1995 | PREDICTED(END_FINALLY); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1996 | /* At the top of the stack are 1 or 6 values: |
| 1997 | Either: |
| 1998 | - TOP = NULL or an integer |
| 1999 | or: |
| 2000 | - (TOP, SECOND, THIRD) = exc_info() |
| 2001 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 2002 | */ |
| 2003 | PyObject *exc = POP(); |
| 2004 | if (exc == NULL) { |
| 2005 | FAST_DISPATCH(); |
| 2006 | } |
| 2007 | else if (PyLong_CheckExact(exc)) { |
| 2008 | int ret = _PyLong_AsInt(exc); |
| 2009 | Py_DECREF(exc); |
| 2010 | if (ret == -1 && PyErr_Occurred()) { |
| 2011 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2013 | JUMPTO(ret); |
| 2014 | FAST_DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2016 | else { |
| 2017 | assert(PyExceptionClass_Check(exc)); |
| 2018 | PyObject *val = POP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2019 | PyObject *tb = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2020 | PyErr_Restore(exc, val, tb); |
| 2021 | goto exception_unwind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2023 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2024 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2025 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2026 | PyObject *exc = POP(); |
| 2027 | assert(PyExceptionClass_Check(exc)); |
| 2028 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2029 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2030 | assert(b->b_type == EXCEPT_HANDLER); |
| 2031 | Py_DECREF(exc); |
| 2032 | UNWIND_EXCEPT_HANDLER(b); |
| 2033 | Py_DECREF(POP()); |
| 2034 | JUMPBY(oparg); |
| 2035 | FAST_DISPATCH(); |
| 2036 | } |
| 2037 | else { |
| 2038 | PyObject *val = POP(); |
| 2039 | PyObject *tb = POP(); |
| 2040 | PyErr_Restore(exc, val, tb); |
| 2041 | goto exception_unwind; |
| 2042 | } |
| 2043 | } |
| 2044 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2045 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2046 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2047 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2048 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2049 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2050 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 2051 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2052 | PyErr_SetString(PyExc_NameError, |
| 2053 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2054 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2055 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2056 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2057 | } |
| 2058 | else { |
| 2059 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2060 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2061 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2062 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2063 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2064 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2065 | PyErr_SetString(PyExc_NameError, |
| 2066 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2067 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2068 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2070 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2071 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2072 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2073 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2074 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2075 | PyObject *name = GETITEM(names, oparg); |
| 2076 | PyObject *v = POP(); |
| 2077 | PyObject *ns = f->f_locals; |
| 2078 | int err; |
| 2079 | if (ns == NULL) { |
| 2080 | PyErr_Format(PyExc_SystemError, |
| 2081 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2083 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2085 | if (PyDict_CheckExact(ns)) |
| 2086 | err = PyDict_SetItem(ns, name, v); |
| 2087 | else |
| 2088 | err = PyObject_SetItem(ns, name, v); |
| 2089 | Py_DECREF(v); |
| 2090 | if (err != 0) |
| 2091 | goto error; |
| 2092 | DISPATCH(); |
| 2093 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2094 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2095 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2096 | PyObject *name = GETITEM(names, oparg); |
| 2097 | PyObject *ns = f->f_locals; |
| 2098 | int err; |
| 2099 | if (ns == NULL) { |
| 2100 | PyErr_Format(PyExc_SystemError, |
| 2101 | "no locals when deleting %R", name); |
| 2102 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2103 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2104 | err = PyObject_DelItem(ns, name); |
| 2105 | if (err != 0) { |
| 2106 | format_exc_check_arg(PyExc_NameError, |
| 2107 | NAME_ERROR_MSG, |
| 2108 | name); |
| 2109 | goto error; |
| 2110 | } |
| 2111 | DISPATCH(); |
| 2112 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2113 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2114 | case TARGET(UNPACK_SEQUENCE): { |
| 2115 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2116 | PyObject *seq = POP(), *item, **items; |
| 2117 | if (PyTuple_CheckExact(seq) && |
| 2118 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2119 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2121 | item = items[oparg]; |
| 2122 | Py_INCREF(item); |
| 2123 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2125 | } else if (PyList_CheckExact(seq) && |
| 2126 | PyList_GET_SIZE(seq) == oparg) { |
| 2127 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2129 | item = items[oparg]; |
| 2130 | Py_INCREF(item); |
| 2131 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2133 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2135 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | } else { |
| 2137 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2138 | Py_DECREF(seq); |
| 2139 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2141 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2142 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2144 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2145 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2146 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2147 | PyObject *seq = POP(); |
| 2148 | |
| 2149 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2150 | stack_pointer + totalargs)) { |
| 2151 | stack_pointer += totalargs; |
| 2152 | } else { |
| 2153 | Py_DECREF(seq); |
| 2154 | goto error; |
| 2155 | } |
| 2156 | Py_DECREF(seq); |
| 2157 | DISPATCH(); |
| 2158 | } |
| 2159 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2160 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2161 | PyObject *name = GETITEM(names, oparg); |
| 2162 | PyObject *owner = TOP(); |
| 2163 | PyObject *v = SECOND(); |
| 2164 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2165 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2166 | err = PyObject_SetAttr(owner, name, 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 | Py_DECREF(owner); |
| 2169 | if (err != 0) |
| 2170 | goto error; |
| 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(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2175 | PyObject *name = GETITEM(names, oparg); |
| 2176 | PyObject *owner = POP(); |
| 2177 | int err; |
| 2178 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2179 | Py_DECREF(owner); |
| 2180 | if (err != 0) |
| 2181 | goto error; |
| 2182 | DISPATCH(); |
| 2183 | } |
| 2184 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2185 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2186 | PyObject *name = GETITEM(names, oparg); |
| 2187 | PyObject *v = POP(); |
| 2188 | int err; |
| 2189 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2191 | if (err != 0) |
| 2192 | goto error; |
| 2193 | DISPATCH(); |
| 2194 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2195 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2196 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2197 | PyObject *name = GETITEM(names, oparg); |
| 2198 | int err; |
| 2199 | err = PyDict_DelItem(f->f_globals, name); |
| 2200 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2202 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2203 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2204 | } |
| 2205 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2206 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2207 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2208 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2209 | PyObject *name = GETITEM(names, oparg); |
| 2210 | PyObject *locals = f->f_locals; |
| 2211 | PyObject *v; |
| 2212 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2214 | "no locals when loading %R", name); |
| 2215 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2217 | if (PyDict_CheckExact(locals)) { |
| 2218 | v = PyDict_GetItem(locals, name); |
| 2219 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2220 | } |
| 2221 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2222 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2223 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2224 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2225 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | PyErr_Clear(); |
| 2227 | } |
| 2228 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2229 | if (v == NULL) { |
| 2230 | v = PyDict_GetItem(f->f_globals, name); |
| 2231 | Py_XINCREF(v); |
| 2232 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2233 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2234 | v = PyDict_GetItem(f->f_builtins, name); |
| 2235 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2236 | format_exc_check_arg( |
| 2237 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2238 | NAME_ERROR_MSG, name); |
| 2239 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2240 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2241 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2242 | } |
| 2243 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2244 | v = PyObject_GetItem(f->f_builtins, name); |
| 2245 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2246 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2247 | format_exc_check_arg( |
| 2248 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2249 | NAME_ERROR_MSG, name); |
| 2250 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2251 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2252 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2255 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2257 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2258 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2259 | case TARGET(LOAD_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2260 | PyObject *name = GETITEM(names, oparg); |
| 2261 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2262 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2263 | && PyDict_CheckExact(f->f_builtins)) |
| 2264 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2265 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2266 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2267 | name); |
| 2268 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2269 | if (!_PyErr_OCCURRED()) { |
| 2270 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2271 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2272 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2273 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2274 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2275 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2277 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2279 | else { |
| 2280 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2281 | |
| 2282 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2283 | v = PyObject_GetItem(f->f_globals, name); |
| 2284 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2285 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2286 | goto error; |
| 2287 | PyErr_Clear(); |
| 2288 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2289 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2290 | v = PyObject_GetItem(f->f_builtins, name); |
| 2291 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2292 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2293 | format_exc_check_arg( |
| 2294 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2295 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2296 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2300 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2302 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2303 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2304 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2305 | PyObject *v = GETLOCAL(oparg); |
| 2306 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | SETLOCAL(oparg, NULL); |
| 2308 | DISPATCH(); |
| 2309 | } |
| 2310 | format_exc_check_arg( |
| 2311 | PyExc_UnboundLocalError, |
| 2312 | UNBOUNDLOCAL_ERROR_MSG, |
| 2313 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2314 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2315 | goto error; |
| 2316 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2317 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2318 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2319 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2320 | PyObject *oldobj = PyCell_GET(cell); |
| 2321 | if (oldobj != NULL) { |
| 2322 | PyCell_SET(cell, NULL); |
| 2323 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2324 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2325 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2326 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2327 | goto error; |
| 2328 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2329 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2330 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2331 | PyObject *cell = freevars[oparg]; |
| 2332 | Py_INCREF(cell); |
| 2333 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2335 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2336 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2337 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2338 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2339 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2340 | assert(locals); |
| 2341 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2342 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2343 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2344 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2345 | if (PyDict_CheckExact(locals)) { |
| 2346 | value = PyDict_GetItem(locals, name); |
| 2347 | Py_XINCREF(value); |
| 2348 | } |
| 2349 | else { |
| 2350 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2351 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2352 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2353 | goto error; |
| 2354 | PyErr_Clear(); |
| 2355 | } |
| 2356 | } |
| 2357 | if (!value) { |
| 2358 | PyObject *cell = freevars[oparg]; |
| 2359 | value = PyCell_GET(cell); |
| 2360 | if (value == NULL) { |
| 2361 | format_exc_unbound(co, oparg); |
| 2362 | goto error; |
| 2363 | } |
| 2364 | Py_INCREF(value); |
| 2365 | } |
| 2366 | PUSH(value); |
| 2367 | DISPATCH(); |
| 2368 | } |
| 2369 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2370 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2371 | PyObject *cell = freevars[oparg]; |
| 2372 | PyObject *value = PyCell_GET(cell); |
| 2373 | if (value == NULL) { |
| 2374 | format_exc_unbound(co, oparg); |
| 2375 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2377 | Py_INCREF(value); |
| 2378 | PUSH(value); |
| 2379 | DISPATCH(); |
| 2380 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2381 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2382 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2383 | PyObject *v = POP(); |
| 2384 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2385 | PyObject *oldobj = PyCell_GET(cell); |
| 2386 | PyCell_SET(cell, v); |
| 2387 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2388 | DISPATCH(); |
| 2389 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2390 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2391 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2392 | PyObject *str; |
| 2393 | PyObject *empty = PyUnicode_New(0, 0); |
| 2394 | if (empty == NULL) { |
| 2395 | goto error; |
| 2396 | } |
| 2397 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2398 | Py_DECREF(empty); |
| 2399 | if (str == NULL) |
| 2400 | goto error; |
| 2401 | while (--oparg >= 0) { |
| 2402 | PyObject *item = POP(); |
| 2403 | Py_DECREF(item); |
| 2404 | } |
| 2405 | PUSH(str); |
| 2406 | DISPATCH(); |
| 2407 | } |
| 2408 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2409 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2410 | PyObject *tup = PyTuple_New(oparg); |
| 2411 | if (tup == NULL) |
| 2412 | goto error; |
| 2413 | while (--oparg >= 0) { |
| 2414 | PyObject *item = POP(); |
| 2415 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2416 | } |
| 2417 | PUSH(tup); |
| 2418 | DISPATCH(); |
| 2419 | } |
| 2420 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2421 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2422 | PyObject *list = PyList_New(oparg); |
| 2423 | if (list == NULL) |
| 2424 | goto error; |
| 2425 | while (--oparg >= 0) { |
| 2426 | PyObject *item = POP(); |
| 2427 | PyList_SET_ITEM(list, oparg, item); |
| 2428 | } |
| 2429 | PUSH(list); |
| 2430 | DISPATCH(); |
| 2431 | } |
| 2432 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2433 | case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL): |
| 2434 | case TARGET(BUILD_TUPLE_UNPACK): |
| 2435 | case TARGET(BUILD_LIST_UNPACK): { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2436 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2437 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2438 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2439 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2440 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2441 | if (sum == NULL) |
| 2442 | goto error; |
| 2443 | |
| 2444 | for (i = oparg; i > 0; i--) { |
| 2445 | PyObject *none_val; |
| 2446 | |
| 2447 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2448 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2449 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2450 | PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2451 | { |
| 2452 | check_args_iterable(PEEK(1 + oparg), PEEK(i)); |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2453 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2454 | Py_DECREF(sum); |
| 2455 | goto error; |
| 2456 | } |
| 2457 | Py_DECREF(none_val); |
| 2458 | } |
| 2459 | |
| 2460 | if (convert_to_tuple) { |
| 2461 | return_value = PyList_AsTuple(sum); |
| 2462 | Py_DECREF(sum); |
| 2463 | if (return_value == NULL) |
| 2464 | goto error; |
| 2465 | } |
| 2466 | else { |
| 2467 | return_value = sum; |
| 2468 | } |
| 2469 | |
| 2470 | while (oparg--) |
| 2471 | Py_DECREF(POP()); |
| 2472 | PUSH(return_value); |
| 2473 | DISPATCH(); |
| 2474 | } |
| 2475 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2476 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2477 | PyObject *set = PySet_New(NULL); |
| 2478 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2479 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2480 | if (set == NULL) |
| 2481 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2482 | for (i = oparg; i > 0; i--) { |
| 2483 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2484 | if (err == 0) |
| 2485 | err = PySet_Add(set, item); |
| 2486 | Py_DECREF(item); |
| 2487 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2488 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2489 | if (err != 0) { |
| 2490 | Py_DECREF(set); |
| 2491 | goto error; |
| 2492 | } |
| 2493 | PUSH(set); |
| 2494 | DISPATCH(); |
| 2495 | } |
| 2496 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2497 | case TARGET(BUILD_SET_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2498 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2499 | PyObject *sum = PySet_New(NULL); |
| 2500 | if (sum == NULL) |
| 2501 | goto error; |
| 2502 | |
| 2503 | for (i = oparg; i > 0; i--) { |
| 2504 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2505 | Py_DECREF(sum); |
| 2506 | goto error; |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | while (oparg--) |
| 2511 | Py_DECREF(POP()); |
| 2512 | PUSH(sum); |
| 2513 | DISPATCH(); |
| 2514 | } |
| 2515 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2516 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2517 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2518 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2519 | if (map == NULL) |
| 2520 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2521 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2522 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2523 | PyObject *key = PEEK(2*i); |
| 2524 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2525 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2526 | if (err != 0) { |
| 2527 | Py_DECREF(map); |
| 2528 | goto error; |
| 2529 | } |
| 2530 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2531 | |
| 2532 | while (oparg--) { |
| 2533 | Py_DECREF(POP()); |
| 2534 | Py_DECREF(POP()); |
| 2535 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2536 | PUSH(map); |
| 2537 | DISPATCH(); |
| 2538 | } |
| 2539 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2540 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2541 | _Py_IDENTIFIER(__annotations__); |
| 2542 | int err; |
| 2543 | PyObject *ann_dict; |
| 2544 | if (f->f_locals == NULL) { |
| 2545 | PyErr_Format(PyExc_SystemError, |
| 2546 | "no locals found when setting up annotations"); |
| 2547 | goto error; |
| 2548 | } |
| 2549 | /* check if __annotations__ in locals()... */ |
| 2550 | if (PyDict_CheckExact(f->f_locals)) { |
| 2551 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2552 | &PyId___annotations__); |
| 2553 | if (ann_dict == NULL) { |
| 2554 | /* ...if not, create a new one */ |
| 2555 | ann_dict = PyDict_New(); |
| 2556 | if (ann_dict == NULL) { |
| 2557 | goto error; |
| 2558 | } |
| 2559 | err = _PyDict_SetItemId(f->f_locals, |
| 2560 | &PyId___annotations__, ann_dict); |
| 2561 | Py_DECREF(ann_dict); |
| 2562 | if (err != 0) { |
| 2563 | goto error; |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | else { |
| 2568 | /* do the same if locals() is not a dict */ |
| 2569 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2570 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2571 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2572 | } |
| 2573 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2574 | if (ann_dict == NULL) { |
| 2575 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2576 | goto error; |
| 2577 | } |
| 2578 | PyErr_Clear(); |
| 2579 | ann_dict = PyDict_New(); |
| 2580 | if (ann_dict == NULL) { |
| 2581 | goto error; |
| 2582 | } |
| 2583 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2584 | Py_DECREF(ann_dict); |
| 2585 | if (err != 0) { |
| 2586 | goto error; |
| 2587 | } |
| 2588 | } |
| 2589 | else { |
| 2590 | Py_DECREF(ann_dict); |
| 2591 | } |
| 2592 | } |
| 2593 | DISPATCH(); |
| 2594 | } |
| 2595 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2596 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2597 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2598 | PyObject *map; |
| 2599 | PyObject *keys = TOP(); |
| 2600 | if (!PyTuple_CheckExact(keys) || |
| 2601 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2602 | PyErr_SetString(PyExc_SystemError, |
| 2603 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2604 | goto error; |
| 2605 | } |
| 2606 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2607 | if (map == NULL) { |
| 2608 | goto error; |
| 2609 | } |
| 2610 | for (i = oparg; i > 0; i--) { |
| 2611 | int err; |
| 2612 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2613 | PyObject *value = PEEK(i + 1); |
| 2614 | err = PyDict_SetItem(map, key, value); |
| 2615 | if (err != 0) { |
| 2616 | Py_DECREF(map); |
| 2617 | goto error; |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | Py_DECREF(POP()); |
| 2622 | while (oparg--) { |
| 2623 | Py_DECREF(POP()); |
| 2624 | } |
| 2625 | PUSH(map); |
| 2626 | DISPATCH(); |
| 2627 | } |
| 2628 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2629 | case TARGET(BUILD_MAP_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2630 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2631 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2632 | if (sum == NULL) |
| 2633 | goto error; |
| 2634 | |
| 2635 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2636 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2637 | if (PyDict_Update(sum, arg) < 0) { |
| 2638 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2639 | PyErr_Format(PyExc_TypeError, |
Berker Peksag | 8e9045d | 2016-10-02 13:08:25 +0300 | [diff] [blame] | 2640 | "'%.200s' object is not a mapping", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2641 | arg->ob_type->tp_name); |
| 2642 | } |
| 2643 | Py_DECREF(sum); |
| 2644 | goto error; |
| 2645 | } |
| 2646 | } |
| 2647 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2648 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2649 | Py_DECREF(POP()); |
| 2650 | PUSH(sum); |
| 2651 | DISPATCH(); |
| 2652 | } |
| 2653 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2654 | case TARGET(BUILD_MAP_UNPACK_WITH_CALL): { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2655 | Py_ssize_t i; |
| 2656 | PyObject *sum = PyDict_New(); |
| 2657 | if (sum == NULL) |
| 2658 | goto error; |
| 2659 | |
| 2660 | for (i = oparg; i > 0; i--) { |
| 2661 | PyObject *arg = PEEK(i); |
| 2662 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
| 2663 | PyObject *func = PEEK(2 + oparg); |
| 2664 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2665 | format_kwargs_mapping_error(func, arg); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2666 | } |
| 2667 | else if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2668 | PyObject *exc, *val, *tb; |
| 2669 | PyErr_Fetch(&exc, &val, &tb); |
| 2670 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 2671 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 2672 | if (!PyUnicode_Check(key)) { |
| 2673 | PyErr_Format(PyExc_TypeError, |
| 2674 | "%.200s%.200s keywords must be strings", |
| 2675 | PyEval_GetFuncName(func), |
| 2676 | PyEval_GetFuncDesc(func)); |
| 2677 | } else { |
| 2678 | PyErr_Format(PyExc_TypeError, |
| 2679 | "%.200s%.200s got multiple " |
| 2680 | "values for keyword argument '%U'", |
| 2681 | PyEval_GetFuncName(func), |
| 2682 | PyEval_GetFuncDesc(func), |
| 2683 | key); |
| 2684 | } |
| 2685 | Py_XDECREF(exc); |
| 2686 | Py_XDECREF(val); |
| 2687 | Py_XDECREF(tb); |
| 2688 | } |
| 2689 | else { |
| 2690 | PyErr_Restore(exc, val, tb); |
| 2691 | } |
| 2692 | } |
| 2693 | Py_DECREF(sum); |
| 2694 | goto error; |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | while (oparg--) |
| 2699 | Py_DECREF(POP()); |
| 2700 | PUSH(sum); |
| 2701 | DISPATCH(); |
| 2702 | } |
| 2703 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2704 | case TARGET(MAP_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2705 | PyObject *key = TOP(); |
| 2706 | PyObject *value = SECOND(); |
| 2707 | PyObject *map; |
| 2708 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2709 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2710 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2711 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2712 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2713 | Py_DECREF(value); |
| 2714 | Py_DECREF(key); |
| 2715 | if (err != 0) |
| 2716 | goto error; |
| 2717 | PREDICT(JUMP_ABSOLUTE); |
| 2718 | DISPATCH(); |
| 2719 | } |
| 2720 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2721 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2722 | PyObject *name = GETITEM(names, oparg); |
| 2723 | PyObject *owner = TOP(); |
| 2724 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2725 | Py_DECREF(owner); |
| 2726 | SET_TOP(res); |
| 2727 | if (res == NULL) |
| 2728 | goto error; |
| 2729 | DISPATCH(); |
| 2730 | } |
| 2731 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2732 | case TARGET(COMPARE_OP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2733 | PyObject *right = POP(); |
| 2734 | PyObject *left = TOP(); |
| 2735 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2736 | Py_DECREF(left); |
| 2737 | Py_DECREF(right); |
| 2738 | SET_TOP(res); |
| 2739 | if (res == NULL) |
| 2740 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | PREDICT(POP_JUMP_IF_FALSE); |
| 2742 | PREDICT(POP_JUMP_IF_TRUE); |
| 2743 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2744 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2745 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2746 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2747 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2748 | PyObject *fromlist = POP(); |
| 2749 | PyObject *level = TOP(); |
| 2750 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2751 | res = import_name(f, name, fromlist, level); |
| 2752 | Py_DECREF(level); |
| 2753 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2754 | SET_TOP(res); |
| 2755 | if (res == NULL) |
| 2756 | goto error; |
| 2757 | DISPATCH(); |
| 2758 | } |
| 2759 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2760 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2761 | PyObject *from = POP(), *locals; |
| 2762 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2763 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 2764 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2765 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2766 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2767 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2768 | locals = f->f_locals; |
| 2769 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | PyErr_SetString(PyExc_SystemError, |
| 2771 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2772 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2773 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2775 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2777 | Py_DECREF(from); |
| 2778 | if (err != 0) |
| 2779 | goto error; |
| 2780 | DISPATCH(); |
| 2781 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2782 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2783 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2784 | PyObject *name = GETITEM(names, oparg); |
| 2785 | PyObject *from = TOP(); |
| 2786 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2787 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2788 | PUSH(res); |
| 2789 | if (res == NULL) |
| 2790 | goto error; |
| 2791 | DISPATCH(); |
| 2792 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2793 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2794 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | JUMPBY(oparg); |
| 2796 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2797 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2798 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2799 | case TARGET(POP_JUMP_IF_FALSE): { |
| 2800 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2801 | PyObject *cond = POP(); |
| 2802 | int err; |
| 2803 | if (cond == Py_True) { |
| 2804 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2805 | FAST_DISPATCH(); |
| 2806 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2807 | if (cond == Py_False) { |
| 2808 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | JUMPTO(oparg); |
| 2810 | FAST_DISPATCH(); |
| 2811 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2812 | err = PyObject_IsTrue(cond); |
| 2813 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 2815 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | else if (err == 0) |
| 2817 | JUMPTO(oparg); |
| 2818 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2819 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2821 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2822 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2823 | case TARGET(POP_JUMP_IF_TRUE): { |
| 2824 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2825 | PyObject *cond = POP(); |
| 2826 | int err; |
| 2827 | if (cond == Py_False) { |
| 2828 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2829 | FAST_DISPATCH(); |
| 2830 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2831 | if (cond == Py_True) { |
| 2832 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | JUMPTO(oparg); |
| 2834 | FAST_DISPATCH(); |
| 2835 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2836 | err = PyObject_IsTrue(cond); |
| 2837 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | JUMPTO(oparg); |
| 2840 | } |
| 2841 | else if (err == 0) |
| 2842 | ; |
| 2843 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2844 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2846 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2847 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2848 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2849 | PyObject *cond = TOP(); |
| 2850 | int err; |
| 2851 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2852 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2853 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | FAST_DISPATCH(); |
| 2855 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2856 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | JUMPTO(oparg); |
| 2858 | FAST_DISPATCH(); |
| 2859 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2860 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2862 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2863 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | } |
| 2865 | else if (err == 0) |
| 2866 | JUMPTO(oparg); |
| 2867 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2868 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2870 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2871 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2872 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2873 | PyObject *cond = TOP(); |
| 2874 | int err; |
| 2875 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2876 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2877 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | FAST_DISPATCH(); |
| 2879 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2880 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2881 | JUMPTO(oparg); |
| 2882 | FAST_DISPATCH(); |
| 2883 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2884 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | JUMPTO(oparg); |
| 2887 | } |
| 2888 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2889 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2890 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | } |
| 2892 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2893 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2895 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2896 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2897 | case TARGET(JUMP_ABSOLUTE): { |
| 2898 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2900 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2902 | the per-loop checks for signals. By default, this should be turned-off |
| 2903 | because it prevents detection of a control-break in tight loops like |
| 2904 | "while 1: pass". Compile with this option turned-on when you need |
| 2905 | the speed-up and do not need break checking inside tight loops (ones |
| 2906 | that contain only instructions ending with FAST_DISPATCH). |
| 2907 | */ |
| 2908 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2909 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2911 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2912 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2913 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2914 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2915 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2916 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2917 | PyObject *iter = PyObject_GetIter(iterable); |
| 2918 | Py_DECREF(iterable); |
| 2919 | SET_TOP(iter); |
| 2920 | if (iter == NULL) |
| 2921 | goto error; |
| 2922 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2923 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2924 | DISPATCH(); |
| 2925 | } |
| 2926 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2927 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2928 | /* before: [obj]; after [getiter(obj)] */ |
| 2929 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2930 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2931 | if (PyCoro_CheckExact(iterable)) { |
| 2932 | /* `iterable` is a coroutine */ |
| 2933 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 2934 | /* and it is used in a 'yield from' expression of a |
| 2935 | regular generator. */ |
| 2936 | Py_DECREF(iterable); |
| 2937 | SET_TOP(NULL); |
| 2938 | PyErr_SetString(PyExc_TypeError, |
| 2939 | "cannot 'yield from' a coroutine object " |
| 2940 | "in a non-coroutine generator"); |
| 2941 | goto error; |
| 2942 | } |
| 2943 | } |
| 2944 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2945 | /* `iterable` is not a generator. */ |
| 2946 | iter = PyObject_GetIter(iterable); |
| 2947 | Py_DECREF(iterable); |
| 2948 | SET_TOP(iter); |
| 2949 | if (iter == NULL) |
| 2950 | goto error; |
| 2951 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2952 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2953 | DISPATCH(); |
| 2954 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2955 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2956 | case TARGET(FOR_ITER): { |
| 2957 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2959 | PyObject *iter = TOP(); |
| 2960 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 2961 | if (next != NULL) { |
| 2962 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | PREDICT(STORE_FAST); |
| 2964 | PREDICT(UNPACK_SEQUENCE); |
| 2965 | DISPATCH(); |
| 2966 | } |
| 2967 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2968 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2969 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2970 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2971 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | PyErr_Clear(); |
| 2973 | } |
| 2974 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2975 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2976 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2978 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2980 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2981 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2982 | case TARGET(SETUP_FINALLY): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | /* NOTE: If you add any new block-setup opcodes that |
| 2984 | are not try/except/finally handlers, you may need |
| 2985 | to update the PyGen_NeedsFinalizing() function. |
| 2986 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2987 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2988 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2989 | STACK_LEVEL()); |
| 2990 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2991 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2992 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2993 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2994 | _Py_IDENTIFIER(__aexit__); |
| 2995 | _Py_IDENTIFIER(__aenter__); |
| 2996 | |
| 2997 | PyObject *mgr = TOP(); |
| 2998 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 2999 | *enter; |
| 3000 | PyObject *res; |
| 3001 | if (exit == NULL) |
| 3002 | goto error; |
| 3003 | SET_TOP(exit); |
| 3004 | enter = special_lookup(mgr, &PyId___aenter__); |
| 3005 | Py_DECREF(mgr); |
| 3006 | if (enter == NULL) |
| 3007 | goto error; |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3008 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3009 | Py_DECREF(enter); |
| 3010 | if (res == NULL) |
| 3011 | goto error; |
| 3012 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3013 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3014 | DISPATCH(); |
| 3015 | } |
| 3016 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3017 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3018 | PyObject *res = POP(); |
| 3019 | /* Setup the finally block before pushing the result |
| 3020 | of __aenter__ on the stack. */ |
| 3021 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3022 | STACK_LEVEL()); |
| 3023 | PUSH(res); |
| 3024 | DISPATCH(); |
| 3025 | } |
| 3026 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3027 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3028 | _Py_IDENTIFIER(__exit__); |
| 3029 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3030 | PyObject *mgr = TOP(); |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3031 | PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3032 | PyObject *res; |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3033 | if (enter == NULL) |
| 3034 | goto error; |
| 3035 | exit = special_lookup(mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3036 | if (exit == NULL) { |
| 3037 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3038 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3039 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3040 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3041 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3042 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3043 | Py_DECREF(enter); |
| 3044 | if (res == NULL) |
| 3045 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | /* Setup the finally block before pushing the result |
| 3047 | of __enter__ on the stack. */ |
| 3048 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3049 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3050 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3051 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | DISPATCH(); |
| 3053 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3054 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3055 | case TARGET(WITH_CLEANUP_START): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3056 | /* At the top of the stack are 1 or 6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3057 | how/why we entered the finally clause: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3058 | - TOP = NULL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | - (TOP, SECOND, THIRD) = exc_info() |
| 3060 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3061 | Below them is EXIT, the context.__exit__ or context.__aexit__ |
| 3062 | bound method. |
| 3063 | In the first case, we must call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | EXIT(None, None, None) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3065 | otherwise we must call |
| 3066 | EXIT(TOP, SECOND, THIRD) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3067 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3068 | In the first case, we remove EXIT from the |
| 3069 | stack, leaving TOP, and push TOP on the stack. |
| 3070 | Otherwise we shift the bottom 3 values of the |
| 3071 | stack down, replace the empty spot with NULL, and push |
| 3072 | None on the stack. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3073 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3074 | Finally we push the result of the call. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3076 | PyObject *stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3078 | PyObject *exc, *val, *tb, *res; |
| 3079 | |
| 3080 | val = tb = Py_None; |
| 3081 | exc = TOP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3082 | if (exc == NULL) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3083 | STACK_SHRINK(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3085 | SET_TOP(exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3086 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | } |
| 3088 | else { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3089 | assert(PyExceptionClass_Check(exc)); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3090 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3092 | val = SECOND(); |
| 3093 | tb = THIRD(); |
| 3094 | tp2 = FOURTH(); |
| 3095 | exc2 = PEEK(5); |
| 3096 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3098 | SET_VALUE(7, tb2); |
| 3099 | SET_VALUE(6, exc2); |
| 3100 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3102 | SET_FOURTH(NULL); |
| 3103 | /* We just shifted the stack down, so we have |
| 3104 | to tell the except handler block that the |
| 3105 | values are lower than it expects. */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3106 | assert(f->f_iblock > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3108 | assert(block->b_type == EXCEPT_HANDLER); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3109 | assert(block->b_level > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | block->b_level--; |
| 3111 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3112 | |
| 3113 | stack[0] = exc; |
| 3114 | stack[1] = val; |
| 3115 | stack[2] = tb; |
| 3116 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3118 | if (res == NULL) |
| 3119 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3120 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3121 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3122 | PUSH(exc); |
| 3123 | PUSH(res); |
| 3124 | PREDICT(WITH_CLEANUP_FINISH); |
| 3125 | DISPATCH(); |
| 3126 | } |
| 3127 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3128 | case TARGET(WITH_CLEANUP_FINISH): { |
| 3129 | PREDICTED(WITH_CLEANUP_FINISH); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3130 | /* TOP = the result of calling the context.__exit__ bound method |
| 3131 | SECOND = either None or exception type |
| 3132 | |
| 3133 | If SECOND is None below is NULL or the return address, |
| 3134 | otherwise below are 7 values representing an exception. |
| 3135 | */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3136 | PyObject *res = POP(); |
| 3137 | PyObject *exc = POP(); |
| 3138 | int err; |
| 3139 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3140 | if (exc != Py_None) |
| 3141 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | else |
| 3143 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3144 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3145 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3146 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3149 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | else if (err > 0) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3151 | /* There was an exception and a True return. |
| 3152 | * We must manually unwind the EXCEPT_HANDLER block |
| 3153 | * which was created when the exception was caught, |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 3154 | * otherwise the stack will be in an inconsistent state. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3155 | */ |
| 3156 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 3157 | assert(b->b_type == EXCEPT_HANDLER); |
| 3158 | UNWIND_EXCEPT_HANDLER(b); |
| 3159 | PUSH(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | } |
| 3161 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3162 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3164 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3165 | case TARGET(LOAD_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3166 | /* Designed to work in tamdem with CALL_METHOD. */ |
| 3167 | PyObject *name = GETITEM(names, oparg); |
| 3168 | PyObject *obj = TOP(); |
| 3169 | PyObject *meth = NULL; |
| 3170 | |
| 3171 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3172 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3173 | if (meth == NULL) { |
| 3174 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3175 | goto error; |
| 3176 | } |
| 3177 | |
| 3178 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3179 | /* We can bypass temporary bound method object. |
| 3180 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3181 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3182 | meth | self | arg1 | ... | argN |
| 3183 | */ |
| 3184 | SET_TOP(meth); |
| 3185 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3186 | } |
| 3187 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3188 | /* meth is not an unbound method (but a regular attr, or |
| 3189 | something was returned by a descriptor protocol). Set |
| 3190 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3191 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3192 | |
| 3193 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3194 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3195 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3196 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3197 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3198 | } |
| 3199 | DISPATCH(); |
| 3200 | } |
| 3201 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3202 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3203 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3204 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3205 | |
| 3206 | sp = stack_pointer; |
| 3207 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3208 | meth = PEEK(oparg + 2); |
| 3209 | if (meth == NULL) { |
| 3210 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3211 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3212 | |
| 3213 | Stack layout: |
| 3214 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3215 | ... | NULL | callable | arg1 | ... | argN |
| 3216 | ^- TOP() |
| 3217 | ^- (-oparg) |
| 3218 | ^- (-oparg-1) |
| 3219 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3220 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3221 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3222 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3223 | */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3224 | res = call_function(&sp, oparg, NULL); |
| 3225 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3226 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3227 | } |
| 3228 | else { |
| 3229 | /* This is a method call. Stack layout: |
| 3230 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3231 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3232 | ^- TOP() |
| 3233 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3234 | ^- (-oparg-1) |
| 3235 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3236 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3237 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3238 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3239 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3240 | */ |
| 3241 | res = call_function(&sp, oparg + 1, NULL); |
| 3242 | stack_pointer = sp; |
| 3243 | } |
| 3244 | |
| 3245 | PUSH(res); |
| 3246 | if (res == NULL) |
| 3247 | goto error; |
| 3248 | DISPATCH(); |
| 3249 | } |
| 3250 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3251 | case TARGET(CALL_FUNCTION): { |
| 3252 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3253 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3255 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3257 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3258 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3259 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3260 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3261 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3262 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3263 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3264 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3265 | PyObject **sp, *res, *names; |
| 3266 | |
| 3267 | names = POP(); |
| 3268 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3270 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3272 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3273 | Py_DECREF(names); |
| 3274 | |
| 3275 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3276 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3277 | } |
| 3278 | DISPATCH(); |
| 3279 | } |
| 3280 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3281 | case TARGET(CALL_FUNCTION_EX): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3282 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3283 | if (oparg & 0x01) { |
| 3284 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3285 | if (!PyDict_CheckExact(kwargs)) { |
| 3286 | PyObject *d = PyDict_New(); |
| 3287 | if (d == NULL) |
| 3288 | goto error; |
| 3289 | if (PyDict_Update(d, kwargs) != 0) { |
| 3290 | Py_DECREF(d); |
| 3291 | /* PyDict_Update raises attribute |
| 3292 | * error (percolated from an attempt |
| 3293 | * to get 'keys' attribute) instead of |
| 3294 | * a type error if its second argument |
| 3295 | * is not a mapping. |
| 3296 | */ |
| 3297 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 3298 | format_kwargs_mapping_error(SECOND(), kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3299 | } |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3300 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3301 | goto error; |
| 3302 | } |
| 3303 | Py_DECREF(kwargs); |
| 3304 | kwargs = d; |
| 3305 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3306 | assert(PyDict_CheckExact(kwargs)); |
| 3307 | } |
| 3308 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3309 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3310 | if (!PyTuple_CheckExact(callargs)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 3311 | if (check_args_iterable(func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3312 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3313 | goto error; |
| 3314 | } |
| 3315 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3316 | if (callargs == NULL) { |
| 3317 | goto error; |
| 3318 | } |
| 3319 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3320 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3321 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3322 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3323 | Py_DECREF(func); |
| 3324 | Py_DECREF(callargs); |
| 3325 | Py_XDECREF(kwargs); |
| 3326 | |
| 3327 | SET_TOP(result); |
| 3328 | if (result == NULL) { |
| 3329 | goto error; |
| 3330 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3331 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3333 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3334 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3335 | PyObject *qualname = POP(); |
| 3336 | PyObject *codeobj = POP(); |
| 3337 | PyFunctionObject *func = (PyFunctionObject *) |
| 3338 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3339 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3340 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3341 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3342 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3343 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3345 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3346 | if (oparg & 0x08) { |
| 3347 | assert(PyTuple_CheckExact(TOP())); |
| 3348 | func ->func_closure = POP(); |
| 3349 | } |
| 3350 | if (oparg & 0x04) { |
| 3351 | assert(PyDict_CheckExact(TOP())); |
| 3352 | func->func_annotations = POP(); |
| 3353 | } |
| 3354 | if (oparg & 0x02) { |
| 3355 | assert(PyDict_CheckExact(TOP())); |
| 3356 | func->func_kwdefaults = POP(); |
| 3357 | } |
| 3358 | if (oparg & 0x01) { |
| 3359 | assert(PyTuple_CheckExact(TOP())); |
| 3360 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3362 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3363 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3364 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3366 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3367 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3368 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3370 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3372 | step = NULL; |
| 3373 | stop = POP(); |
| 3374 | start = TOP(); |
| 3375 | slice = PySlice_New(start, stop, step); |
| 3376 | Py_DECREF(start); |
| 3377 | Py_DECREF(stop); |
| 3378 | Py_XDECREF(step); |
| 3379 | SET_TOP(slice); |
| 3380 | if (slice == NULL) |
| 3381 | goto error; |
| 3382 | DISPATCH(); |
| 3383 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3384 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3385 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3386 | /* Handles f-string value formatting. */ |
| 3387 | PyObject *result; |
| 3388 | PyObject *fmt_spec; |
| 3389 | PyObject *value; |
| 3390 | PyObject *(*conv_fn)(PyObject *); |
| 3391 | int which_conversion = oparg & FVC_MASK; |
| 3392 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3393 | |
| 3394 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3395 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3396 | |
| 3397 | /* See if any conversion is specified. */ |
| 3398 | switch (which_conversion) { |
| 3399 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3400 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3401 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3402 | |
| 3403 | /* Must be 0 (meaning no conversion), since only four |
| 3404 | values are allowed by (oparg & FVC_MASK). */ |
| 3405 | default: conv_fn = NULL; break; |
| 3406 | } |
| 3407 | |
| 3408 | /* If there's a conversion function, call it and replace |
| 3409 | value with that result. Otherwise, just use value, |
| 3410 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3411 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3412 | result = conv_fn(value); |
| 3413 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3414 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3415 | Py_XDECREF(fmt_spec); |
| 3416 | goto error; |
| 3417 | } |
| 3418 | value = result; |
| 3419 | } |
| 3420 | |
| 3421 | /* If value is a unicode object, and there's no fmt_spec, |
| 3422 | then we know the result of format(value) is value |
| 3423 | itself. In that case, skip calling format(). I plan to |
| 3424 | move this optimization in to PyObject_Format() |
| 3425 | itself. */ |
| 3426 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3427 | /* Do nothing, just transfer ownership to result. */ |
| 3428 | result = value; |
| 3429 | } else { |
| 3430 | /* Actually call format(). */ |
| 3431 | result = PyObject_Format(value, fmt_spec); |
| 3432 | Py_DECREF(value); |
| 3433 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3434 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3435 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3436 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3437 | } |
| 3438 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3439 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3440 | DISPATCH(); |
| 3441 | } |
| 3442 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3443 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3444 | int oldoparg = oparg; |
| 3445 | NEXTOPARG(); |
| 3446 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3447 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3448 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3449 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3450 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3451 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3453 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | default: |
| 3455 | fprintf(stderr, |
| 3456 | "XXX lineno: %d, opcode: %d\n", |
| 3457 | PyFrame_GetLineNumber(f), |
| 3458 | opcode); |
| 3459 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3460 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3463 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3464 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3465 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3466 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3467 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3468 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3469 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3470 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3471 | if (!PyErr_Occurred()) |
| 3472 | PyErr_SetString(PyExc_SystemError, |
| 3473 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3474 | #else |
| 3475 | assert(PyErr_Occurred()); |
| 3476 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3477 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3478 | /* Log traceback info. */ |
| 3479 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3480 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3481 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3482 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3483 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3484 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3485 | exception_unwind: |
| 3486 | /* Unwind stacks if an exception occurred */ |
| 3487 | while (f->f_iblock > 0) { |
| 3488 | /* Pop the current block. */ |
| 3489 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | if (b->b_type == EXCEPT_HANDLER) { |
| 3492 | UNWIND_EXCEPT_HANDLER(b); |
| 3493 | continue; |
| 3494 | } |
| 3495 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3496 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3497 | PyObject *exc, *val, *tb; |
| 3498 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3499 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | /* Beware, this invalidates all b->b_* fields */ |
| 3501 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3502 | PUSH(exc_info->exc_traceback); |
| 3503 | PUSH(exc_info->exc_value); |
| 3504 | if (exc_info->exc_type != NULL) { |
| 3505 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | } |
| 3507 | else { |
| 3508 | Py_INCREF(Py_None); |
| 3509 | PUSH(Py_None); |
| 3510 | } |
| 3511 | PyErr_Fetch(&exc, &val, &tb); |
| 3512 | /* Make the raw exception data |
| 3513 | available to the handler, |
| 3514 | so a program can emulate the |
| 3515 | Python main loop. */ |
| 3516 | PyErr_NormalizeException( |
| 3517 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3518 | if (tb != NULL) |
| 3519 | PyException_SetTraceback(val, tb); |
| 3520 | else |
| 3521 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3523 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3525 | exc_info->exc_value = val; |
| 3526 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | if (tb == NULL) |
| 3528 | tb = Py_None; |
| 3529 | Py_INCREF(tb); |
| 3530 | PUSH(tb); |
| 3531 | PUSH(val); |
| 3532 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | JUMPTO(handler); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3534 | /* Resume normal execution */ |
| 3535 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3538 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3539 | /* End the loop as we still have an error */ |
| 3540 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3543 | /* Pop remaining stack entries. */ |
| 3544 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3545 | PyObject *o = POP(); |
| 3546 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3548 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3549 | assert(retval == NULL); |
| 3550 | assert(PyErr_Occurred()); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3551 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3552 | return_or_yield: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3554 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3555 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3556 | tstate, f, PyTrace_RETURN, retval)) { |
| 3557 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3558 | } |
| 3559 | } |
| 3560 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3561 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 3562 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3563 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | } |
| 3565 | } |
| 3566 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3569 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3570 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3571 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3573 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3574 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3575 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3576 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3579 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3580 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3581 | { |
| 3582 | int err; |
| 3583 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3584 | PyObject *name_str, *comma, *tail, *tmp; |
| 3585 | |
| 3586 | assert(PyList_CheckExact(names)); |
| 3587 | assert(len >= 1); |
| 3588 | /* Deal with the joys of natural language. */ |
| 3589 | switch (len) { |
| 3590 | case 1: |
| 3591 | name_str = PyList_GET_ITEM(names, 0); |
| 3592 | Py_INCREF(name_str); |
| 3593 | break; |
| 3594 | case 2: |
| 3595 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3596 | PyList_GET_ITEM(names, len - 2), |
| 3597 | PyList_GET_ITEM(names, len - 1)); |
| 3598 | break; |
| 3599 | default: |
| 3600 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3601 | PyList_GET_ITEM(names, len - 2), |
| 3602 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3603 | if (tail == NULL) |
| 3604 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3605 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3606 | fail, but we can't be too careful. */ |
| 3607 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3608 | if (err == -1) { |
| 3609 | Py_DECREF(tail); |
| 3610 | return; |
| 3611 | } |
| 3612 | /* Stitch everything up into a nice comma-separated list. */ |
| 3613 | comma = PyUnicode_FromString(", "); |
| 3614 | if (comma == NULL) { |
| 3615 | Py_DECREF(tail); |
| 3616 | return; |
| 3617 | } |
| 3618 | tmp = PyUnicode_Join(comma, names); |
| 3619 | Py_DECREF(comma); |
| 3620 | if (tmp == NULL) { |
| 3621 | Py_DECREF(tail); |
| 3622 | return; |
| 3623 | } |
| 3624 | name_str = PyUnicode_Concat(tmp, tail); |
| 3625 | Py_DECREF(tmp); |
| 3626 | Py_DECREF(tail); |
| 3627 | break; |
| 3628 | } |
| 3629 | if (name_str == NULL) |
| 3630 | return; |
| 3631 | PyErr_Format(PyExc_TypeError, |
| 3632 | "%U() missing %i required %s argument%s: %U", |
| 3633 | co->co_name, |
| 3634 | len, |
| 3635 | kind, |
| 3636 | len == 1 ? "" : "s", |
| 3637 | name_str); |
| 3638 | Py_DECREF(name_str); |
| 3639 | } |
| 3640 | |
| 3641 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3642 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3643 | PyObject **fastlocals) |
| 3644 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3645 | Py_ssize_t i, j = 0; |
| 3646 | Py_ssize_t start, end; |
| 3647 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3648 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3649 | PyObject *missing_names; |
| 3650 | |
| 3651 | /* Compute the names of the arguments that are missing. */ |
| 3652 | missing_names = PyList_New(missing); |
| 3653 | if (missing_names == NULL) |
| 3654 | return; |
| 3655 | if (positional) { |
| 3656 | start = 0; |
| 3657 | end = co->co_argcount - defcount; |
| 3658 | } |
| 3659 | else { |
| 3660 | start = co->co_argcount; |
| 3661 | end = start + co->co_kwonlyargcount; |
| 3662 | } |
| 3663 | for (i = start; i < end; i++) { |
| 3664 | if (GETLOCAL(i) == NULL) { |
| 3665 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3666 | PyObject *name = PyObject_Repr(raw); |
| 3667 | if (name == NULL) { |
| 3668 | Py_DECREF(missing_names); |
| 3669 | return; |
| 3670 | } |
| 3671 | PyList_SET_ITEM(missing_names, j++, name); |
| 3672 | } |
| 3673 | } |
| 3674 | assert(j == missing); |
| 3675 | format_missing(kind, co, missing_names); |
| 3676 | Py_DECREF(missing_names); |
| 3677 | } |
| 3678 | |
| 3679 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3680 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3681 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3682 | { |
| 3683 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3684 | Py_ssize_t kwonly_given = 0; |
| 3685 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3686 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3687 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3688 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3689 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3690 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3691 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3692 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3693 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3694 | } |
| 3695 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3696 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3697 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3698 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3699 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3700 | } |
| 3701 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3702 | plural = (co_argcount != 1); |
| 3703 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3704 | } |
| 3705 | if (sig == NULL) |
| 3706 | return; |
| 3707 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3708 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3709 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3710 | given != 1 ? "s" : "", |
| 3711 | kwonly_given, |
| 3712 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3713 | if (kwonly_sig == NULL) { |
| 3714 | Py_DECREF(sig); |
| 3715 | return; |
| 3716 | } |
| 3717 | } |
| 3718 | else { |
| 3719 | /* This will not fail. */ |
| 3720 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3721 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3722 | } |
| 3723 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3724 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3725 | co->co_name, |
| 3726 | sig, |
| 3727 | plural ? "s" : "", |
| 3728 | given, |
| 3729 | kwonly_sig, |
| 3730 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3731 | Py_DECREF(sig); |
| 3732 | Py_DECREF(kwonly_sig); |
| 3733 | } |
| 3734 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3735 | /* This is gonna seem *real weird*, but if you put some other code between |
Marcel Plch | 3a9ccee | 2018-04-06 23:22:04 +0200 | [diff] [blame] | 3736 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3737 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3738 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 3739 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3740 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3741 | PyObject *const *args, Py_ssize_t argcount, |
| 3742 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3743 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3744 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3745 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3746 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3747 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3748 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3749 | PyFrameObject *f; |
| 3750 | PyObject *retval = NULL; |
| 3751 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3752 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3754 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3755 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3756 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | if (globals == NULL) { |
| 3759 | PyErr_SetString(PyExc_SystemError, |
| 3760 | "PyEval_EvalCodeEx: NULL globals"); |
| 3761 | return NULL; |
| 3762 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3763 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3764 | /* Create the frame */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 3765 | tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 3767 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3768 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3769 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3770 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | fastlocals = f->f_localsplus; |
| 3772 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3773 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3774 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3775 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3776 | kwdict = PyDict_New(); |
| 3777 | if (kwdict == NULL) |
| 3778 | goto fail; |
| 3779 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3780 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3781 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3782 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3783 | SETLOCAL(i, kwdict); |
| 3784 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3785 | else { |
| 3786 | kwdict = NULL; |
| 3787 | } |
| 3788 | |
| 3789 | /* Copy positional arguments into local variables */ |
| 3790 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3791 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3792 | } |
| 3793 | else { |
| 3794 | n = argcount; |
| 3795 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3796 | for (i = 0; i < n; i++) { |
| 3797 | x = args[i]; |
| 3798 | Py_INCREF(x); |
| 3799 | SETLOCAL(i, x); |
| 3800 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3801 | |
| 3802 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3803 | if (co->co_flags & CO_VARARGS) { |
| 3804 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3805 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3806 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3807 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3808 | SETLOCAL(total_args, u); |
| 3809 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | x = args[i]; |
| 3811 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3812 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3814 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3815 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3816 | /* Handle keyword arguments passed as two strided arrays */ |
| 3817 | kwcount *= kwstep; |
| 3818 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3819 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3820 | PyObject *keyword = kwnames[i]; |
| 3821 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3822 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3823 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3824 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3825 | PyErr_Format(PyExc_TypeError, |
| 3826 | "%U() keywords must be strings", |
| 3827 | co->co_name); |
| 3828 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3830 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3831 | /* Speed hack: do raw pointer compares. As names are |
| 3832 | normally interned this should almost always hit. */ |
| 3833 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3834 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3835 | PyObject *name = co_varnames[j]; |
| 3836 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3837 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3838 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3839 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3840 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3841 | /* Slow fallback, just in case */ |
| 3842 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3843 | PyObject *name = co_varnames[j]; |
| 3844 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 3845 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3846 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3847 | } |
| 3848 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3849 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3850 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3851 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3852 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 3853 | assert(j >= total_args); |
| 3854 | if (kwdict == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3855 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3856 | "%U() got an unexpected keyword argument '%S'", |
| 3857 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3858 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3859 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3860 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 3861 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3862 | goto fail; |
| 3863 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3864 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3865 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3866 | kw_found: |
| 3867 | if (GETLOCAL(j) != NULL) { |
| 3868 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3869 | "%U() got multiple values for argument '%S'", |
| 3870 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3871 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3873 | Py_INCREF(value); |
| 3874 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3876 | |
| 3877 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3878 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3879 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | goto fail; |
| 3881 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3882 | |
| 3883 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3884 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3885 | Py_ssize_t m = co->co_argcount - defcount; |
| 3886 | Py_ssize_t missing = 0; |
| 3887 | for (i = argcount; i < m; i++) { |
| 3888 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3889 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3890 | } |
| 3891 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3892 | if (missing) { |
| 3893 | missing_arguments(co, missing, defcount, fastlocals); |
| 3894 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3895 | } |
| 3896 | if (n > m) |
| 3897 | i = n - m; |
| 3898 | else |
| 3899 | i = 0; |
| 3900 | for (; i < defcount; i++) { |
| 3901 | if (GETLOCAL(m+i) == NULL) { |
| 3902 | PyObject *def = defs[i]; |
| 3903 | Py_INCREF(def); |
| 3904 | SETLOCAL(m+i, def); |
| 3905 | } |
| 3906 | } |
| 3907 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3908 | |
| 3909 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3910 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3911 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3912 | for (i = co->co_argcount; i < total_args; i++) { |
| 3913 | PyObject *name; |
| 3914 | if (GETLOCAL(i) != NULL) |
| 3915 | continue; |
| 3916 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3917 | if (kwdefs != NULL) { |
| 3918 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 3919 | if (def) { |
| 3920 | Py_INCREF(def); |
| 3921 | SETLOCAL(i, def); |
| 3922 | continue; |
| 3923 | } |
| 3924 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3925 | missing++; |
| 3926 | } |
| 3927 | if (missing) { |
| 3928 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3929 | goto fail; |
| 3930 | } |
| 3931 | } |
| 3932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3933 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3934 | vars into frame. */ |
| 3935 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3936 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 3937 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3938 | /* Possibly account for the cell variable being an argument. */ |
| 3939 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3940 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3941 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 3942 | /* Clear the local copy. */ |
| 3943 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3944 | } |
| 3945 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3946 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3947 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 3948 | if (c == NULL) |
| 3949 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3950 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3951 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3952 | |
| 3953 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3954 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 3955 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3956 | Py_INCREF(o); |
| 3957 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3959 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3960 | /* Handle generator/coroutine/asynchronous generator */ |
| 3961 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3962 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3963 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3964 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3965 | |
| 3966 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 3967 | assert(coro_wrapper != NULL); |
| 3968 | PyErr_Format(PyExc_RuntimeError, |
| 3969 | "coroutine wrapper %.200R attempted " |
| 3970 | "to recursively wrap %.200R", |
| 3971 | coro_wrapper, |
| 3972 | co); |
| 3973 | goto fail; |
| 3974 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | /* Don't need to keep the reference to f_back, it will be set |
| 3977 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3978 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 | /* Create a new generator that owns the ready to run frame |
| 3981 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3982 | if (is_coro) { |
| 3983 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3984 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 3985 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3986 | } else { |
| 3987 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 3988 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3989 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3990 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3991 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 3992 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3993 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3994 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3995 | if (is_coro && coro_wrapper != NULL) { |
| 3996 | PyObject *wrapped; |
| 3997 | tstate->in_coroutine_wrapper = 1; |
| 3998 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 3999 | tstate->in_coroutine_wrapper = 0; |
| 4000 | return wrapped; |
| 4001 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 4002 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4003 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4005 | |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 4006 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4007 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 4008 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 4011 | which can call back into Python. While we're done with the |
| 4012 | current Python frame (f), the associated C stack is still in use, |
| 4013 | so recursion_depth must be boosted for the duration. |
| 4014 | */ |
| 4015 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 4016 | if (Py_REFCNT(f) > 1) { |
| 4017 | Py_DECREF(f); |
| 4018 | _PyObject_GC_TRACK(f); |
| 4019 | } |
| 4020 | else { |
| 4021 | ++tstate->recursion_depth; |
| 4022 | Py_DECREF(f); |
| 4023 | --tstate->recursion_depth; |
| 4024 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4025 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4028 | PyObject * |
| 4029 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4030 | PyObject *const *args, int argcount, |
| 4031 | PyObject *const *kws, int kwcount, |
| 4032 | PyObject *const *defs, int defcount, |
| 4033 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4034 | { |
| 4035 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4036 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 4037 | kws, kws != NULL ? kws + 1 : NULL, |
| 4038 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4039 | defs, defcount, |
| 4040 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4041 | NULL, NULL); |
| 4042 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4043 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4044 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4045 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4047 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4048 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4050 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4051 | return NULL; |
| 4052 | } |
| 4053 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4057 | /* Logic for the raise statement (too complicated for inlining). |
| 4058 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4059 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4060 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4061 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4062 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | if (exc == NULL) { |
| 4065 | /* Reraise */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4066 | PyThreadState *tstate = _PyThreadState_GET(); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4067 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4069 | type = exc_info->exc_type; |
| 4070 | value = exc_info->exc_value; |
| 4071 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4072 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | PyErr_SetString(PyExc_RuntimeError, |
| 4074 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4075 | return 0; |
| 4076 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | Py_XINCREF(type); |
| 4078 | Py_XINCREF(value); |
| 4079 | Py_XINCREF(tb); |
| 4080 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4081 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 | /* We support the following forms of raise: |
| 4085 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4086 | raise <instance> |
| 4087 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | if (PyExceptionClass_Check(exc)) { |
| 4090 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4091 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | if (value == NULL) |
| 4093 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4094 | if (!PyExceptionInstance_Check(value)) { |
| 4095 | PyErr_Format(PyExc_TypeError, |
| 4096 | "calling %R should have returned an instance of " |
| 4097 | "BaseException, not %R", |
| 4098 | type, Py_TYPE(value)); |
| 4099 | goto raise_error; |
| 4100 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | } |
| 4102 | else if (PyExceptionInstance_Check(exc)) { |
| 4103 | value = exc; |
| 4104 | type = PyExceptionInstance_Class(exc); |
| 4105 | Py_INCREF(type); |
| 4106 | } |
| 4107 | else { |
| 4108 | /* Not something you can raise. You get an exception |
| 4109 | anyway, just not what you specified :-) */ |
| 4110 | Py_DECREF(exc); |
| 4111 | PyErr_SetString(PyExc_TypeError, |
| 4112 | "exceptions must derive from BaseException"); |
| 4113 | goto raise_error; |
| 4114 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4115 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4116 | assert(type != NULL); |
| 4117 | assert(value != NULL); |
| 4118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4119 | if (cause) { |
| 4120 | PyObject *fixed_cause; |
| 4121 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4122 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4123 | if (fixed_cause == NULL) |
| 4124 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4125 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4127 | else if (PyExceptionInstance_Check(cause)) { |
| 4128 | fixed_cause = cause; |
| 4129 | } |
| 4130 | else if (cause == Py_None) { |
| 4131 | Py_DECREF(cause); |
| 4132 | fixed_cause = NULL; |
| 4133 | } |
| 4134 | else { |
| 4135 | PyErr_SetString(PyExc_TypeError, |
| 4136 | "exception causes must derive from " |
| 4137 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4138 | goto raise_error; |
| 4139 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4140 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4143 | PyErr_SetObject(type, value); |
| 4144 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4145 | Py_DECREF(value); |
| 4146 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4147 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4148 | |
| 4149 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | Py_XDECREF(value); |
| 4151 | Py_XDECREF(type); |
| 4152 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4153 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4156 | /* 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] | 4157 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4158 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4159 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4160 | with a variable target. |
| 4161 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4162 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4163 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4164 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | int i = 0, j = 0; |
| 4167 | Py_ssize_t ll = 0; |
| 4168 | PyObject *it; /* iter(v) */ |
| 4169 | PyObject *w; |
| 4170 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4174 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4175 | if (it == NULL) { |
| 4176 | if (PyErr_ExceptionMatches(PyExc_TypeError) && |
| 4177 | v->ob_type->tp_iter == NULL && !PySequence_Check(v)) |
| 4178 | { |
| 4179 | PyErr_Format(PyExc_TypeError, |
| 4180 | "cannot unpack non-iterable %.200s object", |
| 4181 | v->ob_type->tp_name); |
| 4182 | } |
| 4183 | return 0; |
| 4184 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | for (; i < argcnt; i++) { |
| 4187 | w = PyIter_Next(it); |
| 4188 | if (w == NULL) { |
| 4189 | /* Iterator done, via error or exhaustion. */ |
| 4190 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4191 | if (argcntafter == -1) { |
| 4192 | PyErr_Format(PyExc_ValueError, |
| 4193 | "not enough values to unpack (expected %d, got %d)", |
| 4194 | argcnt, i); |
| 4195 | } |
| 4196 | else { |
| 4197 | PyErr_Format(PyExc_ValueError, |
| 4198 | "not enough values to unpack " |
| 4199 | "(expected at least %d, got %d)", |
| 4200 | argcnt + argcntafter, i); |
| 4201 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | } |
| 4203 | goto Error; |
| 4204 | } |
| 4205 | *--sp = w; |
| 4206 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | if (argcntafter == -1) { |
| 4209 | /* We better have exhausted the iterator now. */ |
| 4210 | w = PyIter_Next(it); |
| 4211 | if (w == NULL) { |
| 4212 | if (PyErr_Occurred()) |
| 4213 | goto Error; |
| 4214 | Py_DECREF(it); |
| 4215 | return 1; |
| 4216 | } |
| 4217 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4218 | PyErr_Format(PyExc_ValueError, |
| 4219 | "too many values to unpack (expected %d)", |
| 4220 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4221 | goto Error; |
| 4222 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | l = PySequence_List(it); |
| 4225 | if (l == NULL) |
| 4226 | goto Error; |
| 4227 | *--sp = l; |
| 4228 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4230 | ll = PyList_GET_SIZE(l); |
| 4231 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4232 | PyErr_Format(PyExc_ValueError, |
| 4233 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4234 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | goto Error; |
| 4236 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | /* Pop the "after-variable" args off the list. */ |
| 4239 | for (j = argcntafter; j > 0; j--, i++) { |
| 4240 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4241 | } |
| 4242 | /* Resize the list. */ |
| 4243 | Py_SIZE(l) = ll - argcntafter; |
| 4244 | Py_DECREF(it); |
| 4245 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4246 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4247 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | for (; i > 0; i--, sp++) |
| 4249 | Py_DECREF(*sp); |
| 4250 | Py_XDECREF(it); |
| 4251 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4255 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4256 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4257 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4259 | printf("%s ", str); |
| 4260 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4261 | PyErr_Clear(); /* Don't know what else to do */ |
| 4262 | printf("\n"); |
| 4263 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4264 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4265 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4266 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4267 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4268 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4269 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4270 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4271 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4272 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4273 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | if (value == NULL) { |
| 4275 | value = Py_None; |
| 4276 | Py_INCREF(value); |
| 4277 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4278 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4279 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4280 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4281 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4282 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | return; |
| 4284 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4285 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | Py_DECREF(arg); |
| 4287 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4288 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | else { |
| 4290 | Py_XDECREF(type); |
| 4291 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4292 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4296 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4297 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4298 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4301 | PyObject *type, *value, *traceback; |
| 4302 | int err; |
| 4303 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4304 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4305 | if (err == 0) |
| 4306 | { |
| 4307 | PyErr_Restore(type, value, traceback); |
| 4308 | return 0; |
| 4309 | } |
| 4310 | else { |
| 4311 | Py_XDECREF(type); |
| 4312 | Py_XDECREF(value); |
| 4313 | Py_XDECREF(traceback); |
| 4314 | return -1; |
| 4315 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4318 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4319 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4320 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | int result; |
| 4324 | if (tstate->tracing) |
| 4325 | return 0; |
| 4326 | tstate->tracing++; |
| 4327 | tstate->use_tracing = 0; |
| 4328 | result = func(obj, frame, what, arg); |
| 4329 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4330 | || (tstate->c_profilefunc != NULL)); |
| 4331 | tstate->tracing--; |
| 4332 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4335 | PyObject * |
| 4336 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4337 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4338 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 | int save_tracing = tstate->tracing; |
| 4340 | int save_use_tracing = tstate->use_tracing; |
| 4341 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4343 | tstate->tracing = 0; |
| 4344 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4345 | || (tstate->c_profilefunc != NULL)); |
| 4346 | result = PyObject_Call(func, args, NULL); |
| 4347 | tstate->tracing = save_tracing; |
| 4348 | tstate->use_tracing = save_use_tracing; |
| 4349 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4352 | /* 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] | 4353 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4354 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4355 | PyThreadState *tstate, PyFrameObject *frame, |
| 4356 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4358 | int result = 0; |
| 4359 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | /* If the last instruction executed isn't in the current |
| 4362 | instruction window, reset the window. |
| 4363 | */ |
| 4364 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4365 | PyAddrPair bounds; |
| 4366 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4367 | &bounds); |
| 4368 | *instr_lb = bounds.ap_lower; |
| 4369 | *instr_ub = bounds.ap_upper; |
| 4370 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4371 | /* If the last instruction falls at the start of a line or if it |
| 4372 | represents a jump backwards, update the frame's line number and |
| 4373 | then call the trace function if we're tracing source lines. |
| 4374 | */ |
| 4375 | if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | frame->f_lineno = line; |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4377 | if (frame->f_trace_lines) { |
| 4378 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 4379 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 4381 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 4382 | if (frame->f_trace_opcodes) { |
| 4383 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 4384 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | *instr_prev = frame->f_lasti; |
| 4386 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4389 | void |
| 4390 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4391 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4392 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | PyObject *temp = tstate->c_profileobj; |
| 4394 | Py_XINCREF(arg); |
| 4395 | tstate->c_profilefunc = NULL; |
| 4396 | tstate->c_profileobj = NULL; |
| 4397 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4398 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4399 | Py_XDECREF(temp); |
| 4400 | tstate->c_profilefunc = func; |
| 4401 | tstate->c_profileobj = arg; |
| 4402 | /* Flag that tracing or profiling is turned on */ |
| 4403 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | void |
| 4407 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4408 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4409 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | PyObject *temp = tstate->c_traceobj; |
| 4411 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4412 | Py_XINCREF(arg); |
| 4413 | tstate->c_tracefunc = NULL; |
| 4414 | tstate->c_traceobj = NULL; |
| 4415 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4416 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4417 | Py_XDECREF(temp); |
| 4418 | tstate->c_tracefunc = func; |
| 4419 | tstate->c_traceobj = arg; |
| 4420 | /* Flag that tracing or profiling is turned on */ |
| 4421 | tstate->use_tracing = ((func != NULL) |
| 4422 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4425 | void |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4426 | _PyEval_SetCoroutineOriginTrackingDepth(int new_depth) |
| 4427 | { |
| 4428 | assert(new_depth >= 0); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4429 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4430 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 4431 | } |
| 4432 | |
| 4433 | int |
| 4434 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 4435 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4436 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4437 | return tstate->coroutine_origin_tracking_depth; |
| 4438 | } |
| 4439 | |
| 4440 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4441 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4442 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4443 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4444 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4445 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4446 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4450 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4451 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4452 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4453 | return tstate->coroutine_wrapper; |
| 4454 | } |
| 4455 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4456 | void |
| 4457 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4458 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4459 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4460 | |
| 4461 | Py_XINCREF(firstiter); |
| 4462 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4463 | } |
| 4464 | |
| 4465 | PyObject * |
| 4466 | _PyEval_GetAsyncGenFirstiter(void) |
| 4467 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4468 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4469 | return tstate->async_gen_firstiter; |
| 4470 | } |
| 4471 | |
| 4472 | void |
| 4473 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4474 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4475 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4476 | |
| 4477 | Py_XINCREF(finalizer); |
| 4478 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4479 | } |
| 4480 | |
| 4481 | PyObject * |
| 4482 | _PyEval_GetAsyncGenFinalizer(void) |
| 4483 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4484 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4485 | return tstate->async_gen_finalizer; |
| 4486 | } |
| 4487 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4488 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4489 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4492 | if (current_frame == NULL) |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 4493 | return _PyInterpreterState_GET_UNSAFE()->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | else |
| 4495 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4498 | /* Convenience function to get a builtin from its name */ |
| 4499 | PyObject * |
| 4500 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 4501 | { |
| 4502 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 4503 | if (attr) { |
| 4504 | Py_INCREF(attr); |
| 4505 | } |
| 4506 | else if (!PyErr_Occurred()) { |
| 4507 | PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name)); |
| 4508 | } |
| 4509 | return attr; |
| 4510 | } |
| 4511 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4512 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4513 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4514 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4515 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4516 | if (current_frame == NULL) { |
| 4517 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4522 | return NULL; |
| 4523 | |
| 4524 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4528 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4529 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4531 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4532 | if (current_frame == NULL) |
| 4533 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4534 | |
| 4535 | assert(current_frame->f_globals != NULL); |
| 4536 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4539 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4540 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4541 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4542 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4546 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4547 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4550 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | if (current_frame != NULL) { |
| 4553 | const int codeflags = current_frame->f_code->co_flags; |
| 4554 | const int compilerflags = codeflags & PyCF_MASK; |
| 4555 | if (compilerflags) { |
| 4556 | result = 1; |
| 4557 | cf->cf_flags |= compilerflags; |
| 4558 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4559 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4561 | result = 1; |
| 4562 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4563 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4564 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4565 | } |
| 4566 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4569 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4570 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4571 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4573 | if (PyMethod_Check(func)) |
| 4574 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4575 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4576 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | else if (PyCFunction_Check(func)) |
| 4578 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4579 | else |
| 4580 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4583 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4584 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4585 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | if (PyMethod_Check(func)) |
| 4587 | return "()"; |
| 4588 | else if (PyFunction_Check(func)) |
| 4589 | return "()"; |
| 4590 | else if (PyCFunction_Check(func)) |
| 4591 | return "()"; |
| 4592 | else |
| 4593 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4596 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4597 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4598 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4599 | tstate, tstate->frame, \ |
| 4600 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | x = NULL; \ |
| 4602 | } \ |
| 4603 | else { \ |
| 4604 | x = call; \ |
| 4605 | if (tstate->c_profilefunc != NULL) { \ |
| 4606 | if (x == NULL) { \ |
| 4607 | call_trace_protected(tstate->c_profilefunc, \ |
| 4608 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4609 | tstate, tstate->frame, \ |
| 4610 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | /* XXX should pass (type, value, tb) */ \ |
| 4612 | } else { \ |
| 4613 | if (call_trace(tstate->c_profilefunc, \ |
| 4614 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4615 | tstate, tstate->frame, \ |
| 4616 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | Py_DECREF(x); \ |
| 4618 | x = NULL; \ |
| 4619 | } \ |
| 4620 | } \ |
| 4621 | } \ |
| 4622 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4623 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | x = call; \ |
| 4625 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4626 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 4627 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 4628 | to reduce the stack consumption. */ |
| 4629 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4630 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4631 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4632 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | PyObject *func = *pfunc; |
| 4634 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4635 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4636 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4637 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4639 | /* Always dispatch PyCFunction first, because these are |
| 4640 | presumed to be the most frequent callable object. |
| 4641 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4642 | if (PyCFunction_Check(func)) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4643 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4644 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4645 | } |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4646 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4647 | PyThreadState *tstate = _PyThreadState_GET(); |
jdemeyer | 56868f9 | 2018-07-21 10:30:59 +0200 | [diff] [blame] | 4648 | if (nargs > 0 && tstate->use_tracing) { |
| 4649 | /* We need to create a temporary bound method as argument |
| 4650 | for profiling. |
| 4651 | |
| 4652 | If nargs == 0, then this cannot work because we have no |
| 4653 | "self". In any case, the call itself would raise |
| 4654 | TypeError (foo needs an argument), so we just skip |
| 4655 | profiling. */ |
| 4656 | PyObject *self = stack[0]; |
| 4657 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
jdemeyer | 147d955 | 2018-07-23 18:41:20 +0200 | [diff] [blame] | 4658 | if (func != NULL) { |
| 4659 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, |
| 4660 | stack+1, nargs-1, |
| 4661 | kwnames)); |
| 4662 | Py_DECREF(func); |
INADA Naoki | 93fac8d | 2017-03-07 14:24:37 +0900 | [diff] [blame] | 4663 | } |
jdemeyer | 147d955 | 2018-07-23 18:41:20 +0200 | [diff] [blame] | 4664 | else { |
| 4665 | x = NULL; |
| 4666 | } |
INADA Naoki | 93fac8d | 2017-03-07 14:24:37 +0900 | [diff] [blame] | 4667 | } |
| 4668 | else { |
| 4669 | x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames); |
| 4670 | } |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4671 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4672 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4673 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
Victor Stinner | b69ee8c | 2016-11-28 18:32:31 +0100 | [diff] [blame] | 4674 | /* Optimize access to bound methods. Reuse the Python stack |
| 4675 | to pass 'self' as the first argument, replace 'func' |
| 4676 | with 'self'. It avoids the creation of a new temporary tuple |
| 4677 | for arguments (to replace func with self) when the method uses |
| 4678 | FASTCALL. */ |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4679 | PyObject *self = PyMethod_GET_SELF(func); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4680 | Py_INCREF(self); |
| 4681 | func = PyMethod_GET_FUNCTION(func); |
| 4682 | Py_INCREF(func); |
| 4683 | Py_SETREF(*pfunc, self); |
| 4684 | nargs++; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4685 | stack--; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4686 | } |
| 4687 | else { |
| 4688 | Py_INCREF(func); |
| 4689 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4690 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4691 | if (PyFunction_Check(func)) { |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4692 | x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4693 | } |
| 4694 | else { |
| 4695 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4696 | } |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4697 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4698 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4699 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4700 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4701 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4702 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | while ((*pp_stack) > pfunc) { |
| 4704 | w = EXT_POP(*pp_stack); |
| 4705 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4708 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4711 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4712 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4713 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4714 | PyObject *result; |
| 4715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4716 | if (PyCFunction_Check(func)) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4717 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4719 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4720 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4721 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4722 | PyThreadState *tstate = _PyThreadState_GET(); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4723 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 4724 | if (nargs > 0 && tstate->use_tracing) { |
| 4725 | /* We need to create a temporary bound method as argument |
| 4726 | for profiling. |
| 4727 | |
| 4728 | If nargs == 0, then this cannot work because we have no |
| 4729 | "self". In any case, the call itself would raise |
| 4730 | TypeError (foo needs an argument), so we just skip |
| 4731 | profiling. */ |
| 4732 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 4733 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 4734 | if (func == NULL) { |
| 4735 | return NULL; |
| 4736 | } |
| 4737 | |
| 4738 | C_TRACE(result, _PyCFunction_FastCallDict(func, |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4739 | &_PyTuple_ITEMS(callargs)[1], |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4740 | nargs - 1, |
| 4741 | kwdict)); |
| 4742 | Py_DECREF(func); |
| 4743 | return result; |
| 4744 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4745 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4746 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 4749 | /* 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] | 4750 | nb_index slot defined, and store in *pi. |
| 4751 | 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] | 4752 | 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] | 4753 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4754 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4755 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4756 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4757 | { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4758 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | Py_ssize_t x; |
| 4760 | if (PyIndex_Check(v)) { |
| 4761 | x = PyNumber_AsSsize_t(v, NULL); |
| 4762 | if (x == -1 && PyErr_Occurred()) |
| 4763 | return 0; |
| 4764 | } |
| 4765 | else { |
| 4766 | PyErr_SetString(PyExc_TypeError, |
| 4767 | "slice indices must be integers or " |
| 4768 | "None or have an __index__ method"); |
| 4769 | return 0; |
| 4770 | } |
| 4771 | *pi = x; |
| 4772 | } |
| 4773 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4776 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4777 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4778 | { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4779 | Py_ssize_t x; |
| 4780 | if (PyIndex_Check(v)) { |
| 4781 | x = PyNumber_AsSsize_t(v, NULL); |
| 4782 | if (x == -1 && PyErr_Occurred()) |
| 4783 | return 0; |
| 4784 | } |
| 4785 | else { |
| 4786 | PyErr_SetString(PyExc_TypeError, |
| 4787 | "slice indices must be integers or " |
| 4788 | "have an __index__ method"); |
| 4789 | return 0; |
| 4790 | } |
| 4791 | *pi = x; |
| 4792 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 4796 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4797 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 4798 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4799 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4800 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4802 | int res = 0; |
| 4803 | switch (op) { |
| 4804 | case PyCmp_IS: |
| 4805 | res = (v == w); |
| 4806 | break; |
| 4807 | case PyCmp_IS_NOT: |
| 4808 | res = (v != w); |
| 4809 | break; |
| 4810 | case PyCmp_IN: |
| 4811 | res = PySequence_Contains(w, v); |
| 4812 | if (res < 0) |
| 4813 | return NULL; |
| 4814 | break; |
| 4815 | case PyCmp_NOT_IN: |
| 4816 | res = PySequence_Contains(w, v); |
| 4817 | if (res < 0) |
| 4818 | return NULL; |
| 4819 | res = !res; |
| 4820 | break; |
| 4821 | case PyCmp_EXC_MATCH: |
| 4822 | if (PyTuple_Check(w)) { |
| 4823 | Py_ssize_t i, length; |
| 4824 | length = PyTuple_Size(w); |
| 4825 | for (i = 0; i < length; i += 1) { |
| 4826 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 4827 | if (!PyExceptionClass_Check(exc)) { |
| 4828 | PyErr_SetString(PyExc_TypeError, |
| 4829 | CANNOT_CATCH_MSG); |
| 4830 | return NULL; |
| 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | else { |
| 4835 | if (!PyExceptionClass_Check(w)) { |
| 4836 | PyErr_SetString(PyExc_TypeError, |
| 4837 | CANNOT_CATCH_MSG); |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | } |
| 4841 | res = PyErr_GivenExceptionMatches(v, w); |
| 4842 | break; |
| 4843 | default: |
| 4844 | return PyObject_RichCompare(v, w, op); |
| 4845 | } |
| 4846 | v = res ? Py_True : Py_False; |
| 4847 | Py_INCREF(v); |
| 4848 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4851 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4852 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 4853 | { |
| 4854 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 4855 | PyObject *import_func, *res; |
| 4856 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4857 | |
| 4858 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 4859 | if (import_func == NULL) { |
| 4860 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 4861 | return NULL; |
| 4862 | } |
| 4863 | |
| 4864 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 4865 | if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4866 | int ilevel = _PyLong_AsInt(level); |
| 4867 | if (ilevel == -1 && PyErr_Occurred()) { |
| 4868 | return NULL; |
| 4869 | } |
| 4870 | res = PyImport_ImportModuleLevelObject( |
| 4871 | name, |
| 4872 | f->f_globals, |
| 4873 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 4874 | fromlist, |
| 4875 | ilevel); |
| 4876 | return res; |
| 4877 | } |
| 4878 | |
| 4879 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 4880 | |
| 4881 | stack[0] = name; |
| 4882 | stack[1] = f->f_globals; |
| 4883 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 4884 | stack[3] = fromlist; |
| 4885 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4886 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4887 | Py_DECREF(import_func); |
| 4888 | return res; |
| 4889 | } |
| 4890 | |
| 4891 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4892 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4895 | _Py_IDENTIFIER(__name__); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4896 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4897 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4898 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4899 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4900 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4901 | /* Issue #17636: in case this failed because of a circular relative |
| 4902 | import, try to fallback on reading the module directly from |
| 4903 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4904 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4905 | if (pkgname == NULL) { |
| 4906 | goto error; |
| 4907 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 4908 | if (!PyUnicode_Check(pkgname)) { |
| 4909 | Py_CLEAR(pkgname); |
| 4910 | goto error; |
| 4911 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4912 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4913 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4914 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4915 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4916 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 4917 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4918 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4919 | if (x == NULL) { |
| 4920 | goto error; |
| 4921 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4922 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4923 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4924 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4925 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4926 | if (pkgname == NULL) { |
| 4927 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 4928 | if (pkgname_or_unknown == NULL) { |
| 4929 | Py_XDECREF(pkgpath); |
| 4930 | return NULL; |
| 4931 | } |
| 4932 | } else { |
| 4933 | pkgname_or_unknown = pkgname; |
| 4934 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4935 | |
| 4936 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
| 4937 | PyErr_Clear(); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4938 | errmsg = PyUnicode_FromFormat( |
| 4939 | "cannot import name %R from %R (unknown location)", |
| 4940 | name, pkgname_or_unknown |
| 4941 | ); |
| 4942 | /* NULL check for errmsg done by PyErr_SetImportError. */ |
| 4943 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 4944 | } |
| 4945 | else { |
| 4946 | errmsg = PyUnicode_FromFormat( |
| 4947 | "cannot import name %R from %R (%S)", |
| 4948 | name, pkgname_or_unknown, pkgpath |
| 4949 | ); |
| 4950 | /* NULL check for errmsg done by PyErr_SetImportError. */ |
| 4951 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4952 | } |
| 4953 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4954 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4955 | Py_XDECREF(pkgname_or_unknown); |
| 4956 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4957 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4958 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4959 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4960 | static int |
| 4961 | import_all_from(PyObject *locals, PyObject *v) |
| 4962 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 4963 | _Py_IDENTIFIER(__all__); |
| 4964 | _Py_IDENTIFIER(__dict__); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 4965 | _Py_IDENTIFIER(__name__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4966 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4967 | int skip_leading_underscores = 0; |
| 4968 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4969 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4970 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 4971 | return -1; /* Unexpected error */ |
| 4972 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4973 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4974 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 4975 | return -1; |
| 4976 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | if (dict == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4978 | PyErr_SetString(PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4979 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4980 | return -1; |
| 4981 | } |
| 4982 | all = PyMapping_Keys(dict); |
| 4983 | Py_DECREF(dict); |
| 4984 | if (all == NULL) |
| 4985 | return -1; |
| 4986 | skip_leading_underscores = 1; |
| 4987 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | for (pos = 0, err = 0; ; pos++) { |
| 4990 | name = PySequence_GetItem(all, pos); |
| 4991 | if (name == NULL) { |
| 4992 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4993 | err = -1; |
| 4994 | else |
| 4995 | PyErr_Clear(); |
| 4996 | break; |
| 4997 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 4998 | if (!PyUnicode_Check(name)) { |
| 4999 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 5000 | if (modname == NULL) { |
| 5001 | Py_DECREF(name); |
| 5002 | err = -1; |
| 5003 | break; |
| 5004 | } |
| 5005 | if (!PyUnicode_Check(modname)) { |
| 5006 | PyErr_Format(PyExc_TypeError, |
| 5007 | "module __name__ must be a string, not %.100s", |
| 5008 | Py_TYPE(modname)->tp_name); |
| 5009 | } |
| 5010 | else { |
| 5011 | PyErr_Format(PyExc_TypeError, |
| 5012 | "%s in %U.%s must be str, not %.100s", |
| 5013 | skip_leading_underscores ? "Key" : "Item", |
| 5014 | modname, |
| 5015 | skip_leading_underscores ? "__dict__" : "__all__", |
| 5016 | Py_TYPE(name)->tp_name); |
| 5017 | } |
| 5018 | Py_DECREF(modname); |
| 5019 | Py_DECREF(name); |
| 5020 | err = -1; |
| 5021 | break; |
| 5022 | } |
| 5023 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 5024 | if (PyUnicode_READY(name) == -1) { |
| 5025 | Py_DECREF(name); |
| 5026 | err = -1; |
| 5027 | break; |
| 5028 | } |
| 5029 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 5030 | Py_DECREF(name); |
| 5031 | continue; |
| 5032 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5033 | } |
| 5034 | value = PyObject_GetAttr(v, name); |
| 5035 | if (value == NULL) |
| 5036 | err = -1; |
| 5037 | else if (PyDict_CheckExact(locals)) |
| 5038 | err = PyDict_SetItem(locals, name, value); |
| 5039 | else |
| 5040 | err = PyObject_SetItem(locals, name, value); |
| 5041 | Py_DECREF(name); |
| 5042 | Py_XDECREF(value); |
| 5043 | if (err != 0) |
| 5044 | break; |
| 5045 | } |
| 5046 | Py_DECREF(all); |
| 5047 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5050 | static int |
| 5051 | check_args_iterable(PyObject *func, PyObject *args) |
| 5052 | { |
| 5053 | if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) { |
| 5054 | PyErr_Format(PyExc_TypeError, |
| 5055 | "%.200s%.200s argument after * " |
| 5056 | "must be an iterable, not %.200s", |
| 5057 | PyEval_GetFuncName(func), |
| 5058 | PyEval_GetFuncDesc(func), |
| 5059 | args->ob_type->tp_name); |
| 5060 | return -1; |
| 5061 | } |
| 5062 | return 0; |
| 5063 | } |
| 5064 | |
| 5065 | static void |
| 5066 | format_kwargs_mapping_error(PyObject *func, PyObject *kwargs) |
| 5067 | { |
| 5068 | PyErr_Format(PyExc_TypeError, |
| 5069 | "%.200s%.200s argument after ** " |
| 5070 | "must be a mapping, not %.200s", |
| 5071 | PyEval_GetFuncName(func), |
| 5072 | PyEval_GetFuncDesc(func), |
| 5073 | kwargs->ob_type->tp_name); |
| 5074 | } |
| 5075 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5076 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5077 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5080 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5081 | if (!obj) |
| 5082 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5083 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5084 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | if (!obj_str) |
| 5086 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5088 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5089 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5090 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5091 | static void |
| 5092 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5093 | { |
| 5094 | PyObject *name; |
| 5095 | /* Don't stomp existing exception */ |
| 5096 | if (PyErr_Occurred()) |
| 5097 | return; |
| 5098 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5099 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5100 | oparg); |
| 5101 | format_exc_check_arg( |
| 5102 | PyExc_UnboundLocalError, |
| 5103 | UNBOUNDLOCAL_ERROR_MSG, |
| 5104 | name); |
| 5105 | } else { |
| 5106 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5107 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5108 | format_exc_check_arg(PyExc_NameError, |
| 5109 | UNBOUNDFREE_ERROR_MSG, name); |
| 5110 | } |
| 5111 | } |
| 5112 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5113 | static void |
| 5114 | format_awaitable_error(PyTypeObject *type, int prevopcode) |
| 5115 | { |
| 5116 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5117 | if (prevopcode == BEFORE_ASYNC_WITH) { |
| 5118 | PyErr_Format(PyExc_TypeError, |
| 5119 | "'async with' received an object from __aenter__ " |
| 5120 | "that does not implement __await__: %.100s", |
| 5121 | type->tp_name); |
| 5122 | } |
| 5123 | else if (prevopcode == WITH_CLEANUP_START) { |
| 5124 | PyErr_Format(PyExc_TypeError, |
| 5125 | "'async with' received an object from __aexit__ " |
| 5126 | "that does not implement __await__: %.100s", |
| 5127 | type->tp_name); |
| 5128 | } |
| 5129 | } |
| 5130 | } |
| 5131 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5132 | static PyObject * |
| 5133 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5134 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5135 | { |
| 5136 | PyObject *res; |
| 5137 | if (Py_REFCNT(v) == 2) { |
| 5138 | /* In the common case, there are 2 references to the value |
| 5139 | * stored in 'variable' when the += is performed: one on the |
| 5140 | * value stack (in 'v') and one still stored in the |
| 5141 | * 'variable'. We try to delete the variable now to reduce |
| 5142 | * the refcnt to 1. |
| 5143 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5144 | int opcode, oparg; |
| 5145 | NEXTOPARG(); |
| 5146 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5147 | case STORE_FAST: |
| 5148 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5149 | PyObject **fastlocals = f->f_localsplus; |
| 5150 | if (GETLOCAL(oparg) == v) |
| 5151 | SETLOCAL(oparg, NULL); |
| 5152 | break; |
| 5153 | } |
| 5154 | case STORE_DEREF: |
| 5155 | { |
| 5156 | PyObject **freevars = (f->f_localsplus + |
| 5157 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5158 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5159 | if (PyCell_GET(c) == v) { |
| 5160 | PyCell_SET(c, NULL); |
| 5161 | Py_DECREF(v); |
| 5162 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5163 | break; |
| 5164 | } |
| 5165 | case STORE_NAME: |
| 5166 | { |
| 5167 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5168 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5169 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | 8905fcc | 2018-12-11 08:38:03 +0200 | [diff] [blame] | 5170 | if (locals && PyDict_CheckExact(locals) && |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5171 | PyDict_GetItem(locals, name) == v) { |
| 5172 | if (PyDict_DelItem(locals, name) != 0) { |
| 5173 | PyErr_Clear(); |
| 5174 | } |
| 5175 | } |
| 5176 | break; |
| 5177 | } |
| 5178 | } |
| 5179 | } |
| 5180 | res = v; |
| 5181 | PyUnicode_Append(&res, w); |
| 5182 | return res; |
| 5183 | } |
| 5184 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5185 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5186 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5187 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5188 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5189 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5190 | int i; |
| 5191 | PyObject *l = PyList_New(256); |
| 5192 | if (l == NULL) return NULL; |
| 5193 | for (i = 0; i < 256; i++) { |
| 5194 | PyObject *x = PyLong_FromLong(a[i]); |
| 5195 | if (x == NULL) { |
| 5196 | Py_DECREF(l); |
| 5197 | return NULL; |
| 5198 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5199 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5200 | } |
| 5201 | for (i = 0; i < 256; i++) |
| 5202 | a[i] = 0; |
| 5203 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5207 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5208 | { |
| 5209 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5210 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5211 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5212 | int i; |
| 5213 | PyObject *l = PyList_New(257); |
| 5214 | if (l == NULL) return NULL; |
| 5215 | for (i = 0; i < 257; i++) { |
| 5216 | PyObject *x = getarray(dxpairs[i]); |
| 5217 | if (x == NULL) { |
| 5218 | Py_DECREF(l); |
| 5219 | return NULL; |
| 5220 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5221 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | } |
| 5223 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5224 | #endif |
| 5225 | } |
| 5226 | |
| 5227 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5228 | |
| 5229 | Py_ssize_t |
| 5230 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5231 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5232 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5233 | Py_ssize_t new_index; |
| 5234 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5235 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5236 | return -1; |
| 5237 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5238 | new_index = interp->co_extra_user_count++; |
| 5239 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5240 | return new_index; |
| 5241 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5242 | |
| 5243 | static void |
| 5244 | dtrace_function_entry(PyFrameObject *f) |
| 5245 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5246 | const char *filename; |
| 5247 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5248 | int lineno; |
| 5249 | |
| 5250 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5251 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5252 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5253 | |
| 5254 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5255 | } |
| 5256 | |
| 5257 | static void |
| 5258 | dtrace_function_return(PyFrameObject *f) |
| 5259 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5260 | const char *filename; |
| 5261 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5262 | int lineno; |
| 5263 | |
| 5264 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5265 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5266 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5267 | |
| 5268 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5269 | } |
| 5270 | |
| 5271 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5272 | static void |
| 5273 | maybe_dtrace_line(PyFrameObject *frame, |
| 5274 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5275 | { |
| 5276 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5277 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5278 | |
| 5279 | /* If the last instruction executed isn't in the current |
| 5280 | instruction window, reset the window. |
| 5281 | */ |
| 5282 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5283 | PyAddrPair bounds; |
| 5284 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5285 | &bounds); |
| 5286 | *instr_lb = bounds.ap_lower; |
| 5287 | *instr_ub = bounds.ap_upper; |
| 5288 | } |
| 5289 | /* If the last instruction falls at the start of a line or if |
| 5290 | it represents a jump backwards, update the frame's line |
| 5291 | number and call the trace function. */ |
| 5292 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5293 | frame->f_lineno = line; |
| 5294 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5295 | if (!co_filename) |
| 5296 | co_filename = "?"; |
| 5297 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5298 | if (!co_name) |
| 5299 | co_name = "?"; |
| 5300 | PyDTrace_LINE(co_filename, co_name, line); |
| 5301 | } |
| 5302 | *instr_prev = frame->f_lasti; |
| 5303 | } |