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 | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 103 | _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \ |
| 104 | _PyRuntime.ceval.pending.async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 105 | |
| 106 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 108 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \ |
| 109 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 111 | |
| 112 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 114 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | COMPUTE_EVAL_BREAKER(); \ |
| 116 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 117 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 118 | /* Pending calls are only modified under pending_lock */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 119 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 121 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \ |
| 122 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 124 | |
| 125 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 127 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | COMPUTE_EVAL_BREAKER(); \ |
| 129 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 130 | |
| 131 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 133 | _PyRuntime.ceval.pending.async_exc = 1; \ |
| 134 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 136 | |
| 137 | #define UNSIGNAL_ASYNC_EXC() \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 138 | do { \ |
| 139 | _PyRuntime.ceval.pending.async_exc = 0; \ |
| 140 | COMPUTE_EVAL_BREAKER(); \ |
| 141 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 142 | |
| 143 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 144 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 145 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 146 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 147 | #include "pythread.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 148 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 149 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 150 | int |
| 151 | PyEval_ThreadsInitialized(void) |
| 152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 156 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 157 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | if (gil_created()) |
| 160 | return; |
| 161 | create_gil(); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 162 | take_gil(_PyThreadState_GET()); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 163 | _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); |
| 164 | if (!_PyRuntime.ceval.pending.lock) |
| 165 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 166 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 168 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 169 | _PyEval_FiniThreads(void) |
| 170 | { |
| 171 | if (!gil_created()) |
| 172 | return; |
| 173 | destroy_gil(); |
| 174 | assert(!gil_created()); |
| 175 | } |
| 176 | |
| 177 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 178 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 179 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 180 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | if (tstate == NULL) |
| 182 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 183 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 187 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 190 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | in debug mode. |
| 192 | */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 193 | drop_gil(_PyThreadState_GET()); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 197 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | if (tstate == NULL) |
| 200 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 201 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 202 | assert(gil_created()); |
| 203 | take_gil(tstate); |
| 204 | if (PyThreadState_Swap(tstate) != NULL) |
| 205 | Py_FatalError( |
| 206 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 210 | PyEval_ReleaseThread(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_ReleaseThread: NULL thread state"); |
| 214 | if (PyThreadState_Swap(NULL) != tstate) |
| 215 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 216 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 217 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 219 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
| 220 | * which are not running in the child process, and clear internal locks |
| 221 | * which might be held by those threads. |
| 222 | */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 223 | |
| 224 | void |
| 225 | PyEval_ReInitThreads(void) |
| 226 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 227 | PyThreadState *current_tstate = _PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | if (!gil_created()) |
| 230 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | recreate_gil(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 232 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 233 | take_gil(current_tstate); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 234 | _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 236 | /* Destroy all threads except the current one */ |
| 237 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 238 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 240 | /* 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] | 241 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 242 | |
| 243 | void |
| 244 | _PyEval_SignalAsyncExc(void) |
| 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 247 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 249 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 250 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 253 | if (tstate == NULL) |
| 254 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 255 | assert(gil_created()); |
| 256 | drop_gil(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 261 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | if (tstate == NULL) |
| 264 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 265 | assert(gil_created()); |
| 266 | |
| 267 | int err = errno; |
| 268 | take_gil(tstate); |
| 269 | /* _Py_Finalizing is protected by the GIL */ |
| 270 | if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) { |
| 271 | drop_gil(tstate); |
| 272 | PyThread_exit_thread(); |
| 273 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | } |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 275 | errno = err; |
| 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 281 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 282 | signal handlers or Mac I/O completion routines) can schedule calls |
| 283 | to a function to be called synchronously. |
| 284 | The synchronous function is called with one void* argument. |
| 285 | It should return 0 for success or -1 for failure -- failure should |
| 286 | be accompanied by an exception. |
| 287 | |
| 288 | If registry succeeds, the registry function returns 0; if it fails |
| 289 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 290 | an exception condition). |
| 291 | |
| 292 | Note that because registry may occur from within signal handlers, |
| 293 | or other asynchronous events, calling malloc() is unsafe! |
| 294 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 295 | Any thread can schedule pending calls, but only the main thread |
| 296 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 297 | There is no facility to schedule calls to a particular thread, but |
| 298 | that should be easy to change, should that ever be required. In |
| 299 | that case, the static variables here should go into the python |
| 300 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 301 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 303 | void |
| 304 | _PyEval_SignalReceived(void) |
| 305 | { |
| 306 | /* bpo-30703: Function called when the C signal handler of Python gets a |
| 307 | signal. We cannot queue a callback using Py_AddPendingCall() since |
| 308 | that function is not async-signal-safe. */ |
| 309 | SIGNAL_PENDING_CALLS(); |
| 310 | } |
| 311 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 312 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 313 | scheduling to be made from any thread, and even from an executing |
| 314 | callback. |
| 315 | */ |
| 316 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 317 | int |
| 318 | Py_AddPendingCall(int (*func)(void *), void *arg) |
| 319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | int i, j, result=0; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 321 | PyThread_type_lock lock = _PyRuntime.ceval.pending.lock; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | /* try a few times for the lock. Since this mechanism is used |
| 324 | * for signal handling (on the main thread), there is a (slim) |
| 325 | * chance that a signal is delivered on the same thread while we |
| 326 | * hold the lock during the Py_MakePendingCalls() function. |
| 327 | * This avoids a deadlock in that case. |
| 328 | * Note that signals can be delivered on any thread. In particular, |
| 329 | * on Windows, a SIGINT is delivered on a system-created worker |
| 330 | * thread. |
| 331 | * We also check for lock being NULL, in the unlikely case that |
| 332 | * this function is called before any bytecode evaluation takes place. |
| 333 | */ |
| 334 | if (lock != NULL) { |
| 335 | for (i = 0; i<100; i++) { |
| 336 | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
| 337 | break; |
| 338 | } |
| 339 | if (i == 100) |
| 340 | return -1; |
| 341 | } |
| 342 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 343 | i = _PyRuntime.ceval.pending.last; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | j = (i + 1) % NPENDINGCALLS; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 345 | if (j == _PyRuntime.ceval.pending.first) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | result = -1; /* Queue full */ |
| 347 | } else { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 348 | _PyRuntime.ceval.pending.calls[i].func = func; |
| 349 | _PyRuntime.ceval.pending.calls[i].arg = arg; |
| 350 | _PyRuntime.ceval.pending.last = j; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | } |
| 352 | /* signal main loop */ |
| 353 | SIGNAL_PENDING_CALLS(); |
| 354 | if (lock != NULL) |
| 355 | PyThread_release_lock(lock); |
| 356 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | int |
| 360 | Py_MakePendingCalls(void) |
| 361 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 362 | static int busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | int i; |
| 364 | int r = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 366 | assert(PyGILState_Check()); |
| 367 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 368 | if (!_PyRuntime.ceval.pending.lock) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | /* initial allocation of the lock */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 370 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
| 371 | if (_PyRuntime.ceval.pending.lock == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | return -1; |
| 373 | } |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | /* only service pending calls on main thread */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 376 | if (_PyRuntime.ceval.pending.main_thread && |
| 377 | PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread) |
| 378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | return 0; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 380 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | /* don't perform recursive pending calls */ |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 382 | if (busy) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | return 0; |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 384 | busy = 1; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 385 | /* unsignal before starting to call callbacks, so that any callback |
| 386 | added in-between re-signals */ |
| 387 | UNSIGNAL_PENDING_CALLS(); |
| 388 | |
| 389 | /* Python signal handler doesn't really queue a callback: it only signals |
| 390 | that a signal was received, see _PyEval_SignalReceived(). */ |
| 391 | if (PyErr_CheckSignals() < 0) { |
| 392 | goto error; |
| 393 | } |
| 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | /* perform a bounded number of calls, in case of recursion */ |
| 396 | for (i=0; i<NPENDINGCALLS; i++) { |
| 397 | int j; |
| 398 | int (*func)(void *); |
| 399 | void *arg = NULL; |
| 400 | |
| 401 | /* pop one item off the queue while holding the lock */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 402 | PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK); |
| 403 | j = _PyRuntime.ceval.pending.first; |
| 404 | if (j == _PyRuntime.ceval.pending.last) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | func = NULL; /* Queue empty */ |
| 406 | } else { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 407 | func = _PyRuntime.ceval.pending.calls[j].func; |
| 408 | arg = _PyRuntime.ceval.pending.calls[j].arg; |
| 409 | _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 411 | PyThread_release_lock(_PyRuntime.ceval.pending.lock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | /* having released the lock, perform the callback */ |
| 413 | if (func == NULL) |
| 414 | break; |
| 415 | r = func(arg); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 416 | if (r) { |
| 417 | goto error; |
| 418 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 420 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 421 | busy = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | return r; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 423 | |
| 424 | error: |
| 425 | busy = 0; |
| 426 | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
| 427 | return -1; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 430 | /* The interpreter's recursion limit */ |
| 431 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 432 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 433 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 434 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 435 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 436 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 437 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 438 | void |
| 439 | _PyEval_Initialize(struct _ceval_runtime_state *state) |
| 440 | { |
| 441 | state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 442 | _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
| 443 | _gil_initialize(&state->gil); |
| 444 | } |
| 445 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 446 | int |
| 447 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 448 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 449 | return _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 452 | void |
| 453 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 454 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 455 | _PyRuntime.ceval.recursion_limit = new_limit; |
| 456 | _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 459 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 460 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 461 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 462 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 463 | Without USE_STACKCHECK, there is no need for this. */ |
| 464 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 465 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 466 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 467 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 468 | int recursion_limit = _PyRuntime.ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 469 | |
| 470 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 471 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | if (PyOS_CheckStack()) { |
| 473 | --tstate->recursion_depth; |
| 474 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 475 | return -1; |
| 476 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 477 | /* Needed for ABI backwards-compatibility (see bpo-31857) */ |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 478 | _Py_CheckRecursionLimit = recursion_limit; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 479 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | if (tstate->recursion_critical) |
| 481 | /* Somebody asked that we don't check for recursion. */ |
| 482 | return 0; |
| 483 | if (tstate->overflowed) { |
| 484 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 485 | /* Overflowing while handling an overflow. Give up. */ |
| 486 | Py_FatalError("Cannot recover from stack overflow."); |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | if (tstate->recursion_depth > recursion_limit) { |
| 491 | --tstate->recursion_depth; |
| 492 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 493 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | "maximum recursion depth exceeded%s", |
| 495 | where); |
| 496 | return -1; |
| 497 | } |
| 498 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 501 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 502 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 503 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 504 | #define _Py_TracingPossible _PyRuntime.ceval.tracing_possible |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 507 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 508 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | return PyEval_EvalCodeEx(co, |
| 511 | globals, locals, |
| 512 | (PyObject **)NULL, 0, |
| 513 | (PyObject **)NULL, 0, |
| 514 | (PyObject **)NULL, 0, |
| 515 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | |
| 519 | /* Interpreter main loop */ |
| 520 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 521 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 522 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | /* This is for backward compatibility with extension modules that |
| 524 | used this API; core interpreter code should call |
| 525 | PyEval_EvalFrameEx() */ |
| 526 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 530 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 531 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 532 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 533 | return interp->eval_frame(f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 536 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 537 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 538 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 539 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 541 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 542 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 543 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 544 | int opcode; /* Current opcode */ |
| 545 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 546 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 548 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | is true when the line being executed has changed. The |
| 556 | initial values are such as to make this false the first |
| 557 | time it is tested. */ |
| 558 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 559 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 560 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | PyObject *names; |
| 562 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 563 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 564 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 565 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 566 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 567 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 568 | /* Computed GOTOs, or |
| 569 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 570 | using gcc's labels-as-values extension |
| 571 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 572 | |
| 573 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 575 | combined with a lookup table of jump addresses. However, since the |
| 576 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 577 | hard time making the right prediction for where to jump next (actually, |
| 578 | it will be always wrong except in the uncommon case of a sequence of |
| 579 | several identical opcodes). |
| 580 | |
| 581 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 582 | indirect jump instruction at the end of each opcode. Since the jump |
| 583 | instruction is at a different address for each opcode, the CPU will make a |
| 584 | separate prediction for each of these instructions, which is equivalent to |
| 585 | predicting the second opcode of each opcode pair. These predictions have |
| 586 | a much better chance to turn out valid, especially in small bytecode loops. |
| 587 | |
| 588 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 590 | and potentially many more instructions (depending on the pipeline width). |
| 591 | A correctly predicted branch, however, is nearly free. |
| 592 | |
| 593 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 594 | faster than the normal "switch" version, depending on the compiler and the |
| 595 | CPU architecture. |
| 596 | |
| 597 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 598 | because it would render the measurements invalid. |
| 599 | |
| 600 | |
| 601 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 602 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 603 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 604 | -fno-crossjumping). |
| 605 | */ |
| 606 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 607 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 608 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 609 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 610 | #endif |
| 611 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 612 | #ifdef HAVE_COMPUTED_GOTOS |
| 613 | #ifndef USE_COMPUTED_GOTOS |
| 614 | #define USE_COMPUTED_GOTOS 1 |
| 615 | #endif |
| 616 | #else |
| 617 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 618 | #error "Computed gotos are not supported on this compiler." |
| 619 | #endif |
| 620 | #undef USE_COMPUTED_GOTOS |
| 621 | #define USE_COMPUTED_GOTOS 0 |
| 622 | #endif |
| 623 | |
| 624 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 625 | /* Import the static jump table */ |
| 626 | #include "opcode_targets.h" |
| 627 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 628 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 629 | op: \ |
| 630 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 632 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 634 | if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | FAST_DISPATCH(); \ |
| 636 | } \ |
| 637 | continue; \ |
| 638 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 639 | |
| 640 | #ifdef LLTRACE |
| 641 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 643 | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 645 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 646 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | } \ |
| 648 | goto fast_next_opcode; \ |
| 649 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 650 | #else |
| 651 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 653 | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 655 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 656 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | } \ |
| 658 | goto fast_next_opcode; \ |
| 659 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 660 | #endif |
| 661 | |
| 662 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 663 | #define TARGET(op) op |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 664 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 665 | #define DISPATCH() continue |
| 666 | #define FAST_DISPATCH() goto fast_next_opcode |
| 667 | #endif |
| 668 | |
| 669 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 670 | /* Tuple access macros */ |
| 671 | |
| 672 | #ifndef Py_DEBUG |
| 673 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 674 | #else |
| 675 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 676 | #endif |
| 677 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 678 | /* Code access macros */ |
| 679 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 680 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 681 | #define INSTR_OFFSET() \ |
| 682 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 683 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 684 | _Py_CODEUNIT word = *next_instr; \ |
| 685 | opcode = _Py_OPCODE(word); \ |
| 686 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 687 | next_instr++; \ |
| 688 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 689 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 690 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 691 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 692 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | Some opcodes tend to come in pairs thus making it possible to |
| 694 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 695 | 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] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | Verifying the prediction costs a single high-speed test of a register |
| 698 | variable against a constant. If the pairing was good, then the |
| 699 | processor's own internal branch predication has a high likelihood of |
| 700 | success, resulting in a nearly zero-overhead transition to the |
| 701 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 702 | including its unpredictable switch-case branch. Combined with the |
| 703 | processor's internal branch prediction, a successful PREDICT has the |
| 704 | effect of making the two opcodes run as if they were a single new opcode |
| 705 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 706 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 707 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | predictions turned-on and interpret the results as if some opcodes |
| 709 | had been combined or turn-off predictions so that the opcode frequency |
| 710 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 711 | |
| 712 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | the CPU to record separate branch prediction information for each |
| 714 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 715 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 716 | */ |
| 717 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 718 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 720 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 721 | #define PREDICT(op) \ |
| 722 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 723 | _Py_CODEUNIT word = *next_instr; \ |
| 724 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 725 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 726 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 727 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 728 | goto PRED_##op; \ |
| 729 | } \ |
| 730 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 731 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 732 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 733 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 734 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 735 | /* Stack manipulation macros */ |
| 736 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 737 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 738 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 739 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 740 | #define EMPTY() (STACK_LEVEL() == 0) |
| 741 | #define TOP() (stack_pointer[-1]) |
| 742 | #define SECOND() (stack_pointer[-2]) |
| 743 | #define THIRD() (stack_pointer[-3]) |
| 744 | #define FOURTH() (stack_pointer[-4]) |
| 745 | #define PEEK(n) (stack_pointer[-(n)]) |
| 746 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 747 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 748 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 749 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 750 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 751 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 752 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 753 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 755 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 757 | lltrace && prtrace(TOP(), "push")); \ |
| 758 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 760 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 761 | #define STACK_GROW(n) do { \ |
| 762 | assert(n >= 0); \ |
| 763 | (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 764 | lltrace && prtrace(TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 765 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 766 | } while (0) |
| 767 | #define STACK_SHRINK(n) do { \ |
| 768 | assert(n >= 0); \ |
| 769 | (void)(lltrace && prtrace(TOP(), "stackadj")); \ |
| 770 | (void)(BASIC_STACKADJ(-n)); \ |
| 771 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 772 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 773 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 774 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 775 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 776 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 777 | #define PUSH(v) BASIC_PUSH(v) |
| 778 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 779 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 780 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 781 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 782 | #endif |
| 783 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 784 | /* Local variable macros */ |
| 785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 787 | |
| 788 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 789 | then store the new value; it must copy the old value to a temporary |
| 790 | value, then store the new value, and then DECREF the temporary value. |
| 791 | This is because it is possible that during the DECREF the frame is |
| 792 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 793 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 795 | GETLOCAL(i) = value; \ |
| 796 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 797 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 798 | |
| 799 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 801 | PyObject *v = POP(); \ |
| 802 | Py_XDECREF(v); \ |
| 803 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 804 | |
| 805 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 806 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 808 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 810 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 811 | value = POP(); \ |
| 812 | Py_XDECREF(value); \ |
| 813 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 814 | exc_info = tstate->exc_info; \ |
| 815 | type = exc_info->exc_type; \ |
| 816 | value = exc_info->exc_value; \ |
| 817 | traceback = exc_info->exc_traceback; \ |
| 818 | exc_info->exc_type = POP(); \ |
| 819 | exc_info->exc_value = POP(); \ |
| 820 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | Py_XDECREF(type); \ |
| 822 | Py_XDECREF(value); \ |
| 823 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 824 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 825 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 826 | /* Start of code */ |
| 827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | /* push frame */ |
| 829 | if (Py_EnterRecursiveCall("")) |
| 830 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | if (tstate->use_tracing) { |
| 835 | if (tstate->c_tracefunc != NULL) { |
| 836 | /* tstate->c_tracefunc, if defined, is a |
| 837 | function that will be called on *every* entry |
| 838 | to a code block. Its return value, if not |
| 839 | None, is a function that will be called at |
| 840 | the start of each executed line of code. |
| 841 | (Actually, the function must return itself |
| 842 | in order to continue tracing.) The trace |
| 843 | functions are called with three arguments: |
| 844 | a pointer to the current frame, a string |
| 845 | indicating why the function is called, and |
| 846 | an argument which depends on the situation. |
| 847 | The global trace function is also called |
| 848 | whenever an exception is detected. */ |
| 849 | if (call_trace_protected(tstate->c_tracefunc, |
| 850 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 851 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | /* Trace function raised an error */ |
| 853 | goto exit_eval_frame; |
| 854 | } |
| 855 | } |
| 856 | if (tstate->c_profilefunc != NULL) { |
| 857 | /* Similar for c_profilefunc, except it needn't |
| 858 | return itself and isn't called for "line" events */ |
| 859 | if (call_trace_protected(tstate->c_profilefunc, |
| 860 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 861 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | /* Profile function raised an error */ |
| 863 | goto exit_eval_frame; |
| 864 | } |
| 865 | } |
| 866 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 867 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 868 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 869 | dtrace_function_entry(f); |
| 870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | co = f->f_code; |
| 872 | names = co->co_names; |
| 873 | consts = co->co_consts; |
| 874 | fastlocals = f->f_localsplus; |
| 875 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 876 | assert(PyBytes_Check(co->co_code)); |
| 877 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 878 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 879 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 880 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 881 | /* |
| 882 | f->f_lasti refers to the index of the last instruction, |
| 883 | 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] | 884 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 885 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 886 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 889 | direct succession without updating f->f_lasti. A successful |
| 890 | prediction effectively links the two codes together as if they |
| 891 | were a single new opcode; accordingly,f->f_lasti will point to |
| 892 | the first code in the pair (for instance, GET_ITER followed by |
| 893 | 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] | 894 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 896 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 897 | next_instr = first_instr; |
| 898 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 899 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 900 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 901 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | stack_pointer = f->f_stacktop; |
| 903 | assert(stack_pointer != NULL); |
| 904 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 905 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 906 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 907 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 908 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 909 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 910 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 911 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 912 | if (throwflag) /* support for generator.throw() */ |
| 913 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 914 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 915 | #ifdef Py_DEBUG |
| 916 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 917 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 918 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 919 | assert(!PyErr_Occurred()); |
| 920 | #endif |
| 921 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 922 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 925 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 926 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | /* Do periodic things. Doing this every time through |
| 929 | the loop would add too much overhead, so we do it |
| 930 | only every Nth instruction. We also do it if |
| 931 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 932 | event needs attention (e.g. a signal handler or |
| 933 | async I/O handler); see Py_AddPendingCall() and |
| 934 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 935 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 936 | if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 937 | opcode = _Py_OPCODE(*next_instr); |
| 938 | if (opcode == SETUP_FINALLY || |
| 939 | opcode == SETUP_WITH || |
| 940 | opcode == BEFORE_ASYNC_WITH || |
| 941 | opcode == YIELD_FROM) { |
| 942 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 943 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 944 | - If we're about to enter the 'with:'. It will prevent |
| 945 | emitting a resource warning in the common idiom |
| 946 | 'with open(path) as file:'. |
| 947 | - If we're about to enter the 'async with:'. |
| 948 | - 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] | 949 | *very* useful, but might help in some cases and it's |
| 950 | traditional) |
| 951 | - If we're resuming a chain of nested 'yield from' or |
| 952 | 'await' calls, then each frame is parked with YIELD_FROM |
| 953 | as its next opcode. If the user hit control-C we want to |
| 954 | wait until we've reached the innermost frame before |
| 955 | running the signal handler and raising KeyboardInterrupt |
| 956 | (see bpo-30039). |
| 957 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | goto fast_next_opcode; |
| 959 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 960 | if (_Py_atomic_load_relaxed( |
| 961 | &_PyRuntime.ceval.pending.calls_to_do)) |
| 962 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 963 | if (Py_MakePendingCalls() < 0) |
| 964 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 966 | if (_Py_atomic_load_relaxed( |
| 967 | &_PyRuntime.ceval.gil_drop_request)) |
| 968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | /* Give another thread a chance */ |
| 970 | if (PyThreadState_Swap(NULL) != tstate) |
| 971 | Py_FatalError("ceval: tstate mix-up"); |
| 972 | drop_gil(tstate); |
| 973 | |
| 974 | /* Other threads may run now */ |
| 975 | |
| 976 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 977 | |
| 978 | /* Check if we should make a quick exit. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 979 | if (_Py_IsFinalizing() && |
| 980 | !_Py_CURRENTLY_FINALIZING(tstate)) |
| 981 | { |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 982 | drop_gil(tstate); |
| 983 | PyThread_exit_thread(); |
| 984 | } |
| 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | if (PyThreadState_Swap(tstate) != NULL) |
| 987 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | } |
| 989 | /* Check for asynchronous exceptions. */ |
| 990 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 991 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | tstate->async_exc = NULL; |
| 993 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 994 | PyErr_SetNone(exc); |
| 995 | Py_DECREF(exc); |
| 996 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | fast_next_opcode: |
| 1001 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1002 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1003 | if (PyDTrace_LINE_ENABLED()) |
| 1004 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1009 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1010 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | /* see maybe_call_line_trace |
| 1012 | for expository comments */ |
| 1013 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1016 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1017 | tstate, f, |
| 1018 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | /* Reload possibly changed frame fields */ |
| 1020 | JUMPTO(f->f_lasti); |
| 1021 | if (f->f_stacktop != NULL) { |
| 1022 | stack_pointer = f->f_stacktop; |
| 1023 | f->f_stacktop = NULL; |
| 1024 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1025 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1027 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1031 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1032 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1033 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1034 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1035 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | dxpairs[lastopcode][opcode]++; |
| 1037 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1038 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1040 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1041 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1042 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | if (lltrace) { |
| 1046 | if (HAS_ARG(opcode)) { |
| 1047 | printf("%d: %d, %d\n", |
| 1048 | f->f_lasti, opcode, oparg); |
| 1049 | } |
| 1050 | else { |
| 1051 | printf("%d: %d\n", |
| 1052 | f->f_lasti, opcode); |
| 1053 | } |
| 1054 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1055 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1060 | It is essential that any operation that fails must goto error |
| 1061 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1062 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1063 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1065 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1066 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1067 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1068 | PyObject *value = GETLOCAL(oparg); |
| 1069 | if (value == NULL) { |
| 1070 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1071 | UNBOUNDLOCAL_ERROR_MSG, |
| 1072 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1073 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1075 | Py_INCREF(value); |
| 1076 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1080 | case TARGET(LOAD_CONST): { |
| 1081 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1082 | PyObject *value = GETITEM(consts, oparg); |
| 1083 | Py_INCREF(value); |
| 1084 | PUSH(value); |
| 1085 | FAST_DISPATCH(); |
| 1086 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1087 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1088 | case TARGET(STORE_FAST): { |
| 1089 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1090 | PyObject *value = POP(); |
| 1091 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1093 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1094 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1095 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1096 | PyObject *value = POP(); |
| 1097 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1099 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1100 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1101 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1102 | PyObject *top = TOP(); |
| 1103 | PyObject *second = SECOND(); |
| 1104 | SET_TOP(second); |
| 1105 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1107 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1108 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1109 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1110 | PyObject *top = TOP(); |
| 1111 | PyObject *second = SECOND(); |
| 1112 | PyObject *third = THIRD(); |
| 1113 | SET_TOP(second); |
| 1114 | SET_SECOND(third); |
| 1115 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1117 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1118 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1119 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1120 | PyObject *top = TOP(); |
| 1121 | PyObject *second = SECOND(); |
| 1122 | PyObject *third = THIRD(); |
| 1123 | PyObject *fourth = FOURTH(); |
| 1124 | SET_TOP(second); |
| 1125 | SET_SECOND(third); |
| 1126 | SET_THIRD(fourth); |
| 1127 | SET_FOURTH(top); |
| 1128 | FAST_DISPATCH(); |
| 1129 | } |
| 1130 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1131 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1132 | PyObject *top = TOP(); |
| 1133 | Py_INCREF(top); |
| 1134 | PUSH(top); |
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 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1137 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1138 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1139 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1140 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1141 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1142 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1143 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1144 | SET_TOP(top); |
| 1145 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1146 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1147 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1148 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1149 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1150 | PyObject *value = TOP(); |
| 1151 | PyObject *res = PyNumber_Positive(value); |
| 1152 | Py_DECREF(value); |
| 1153 | SET_TOP(res); |
| 1154 | if (res == NULL) |
| 1155 | goto error; |
| 1156 | DISPATCH(); |
| 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(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1160 | PyObject *value = TOP(); |
| 1161 | PyObject *res = PyNumber_Negative(value); |
| 1162 | Py_DECREF(value); |
| 1163 | SET_TOP(res); |
| 1164 | if (res == NULL) |
| 1165 | goto error; |
| 1166 | DISPATCH(); |
| 1167 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1168 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1169 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1170 | PyObject *value = TOP(); |
| 1171 | int err = PyObject_IsTrue(value); |
| 1172 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | if (err == 0) { |
| 1174 | Py_INCREF(Py_True); |
| 1175 | SET_TOP(Py_True); |
| 1176 | DISPATCH(); |
| 1177 | } |
| 1178 | else if (err > 0) { |
| 1179 | Py_INCREF(Py_False); |
| 1180 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | DISPATCH(); |
| 1182 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1183 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1184 | goto error; |
| 1185 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1186 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1187 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1188 | PyObject *value = TOP(); |
| 1189 | PyObject *res = PyNumber_Invert(value); |
| 1190 | Py_DECREF(value); |
| 1191 | SET_TOP(res); |
| 1192 | if (res == NULL) |
| 1193 | goto error; |
| 1194 | DISPATCH(); |
| 1195 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1196 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1197 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1198 | PyObject *exp = POP(); |
| 1199 | PyObject *base = TOP(); |
| 1200 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1201 | Py_DECREF(base); |
| 1202 | Py_DECREF(exp); |
| 1203 | SET_TOP(res); |
| 1204 | if (res == NULL) |
| 1205 | goto error; |
| 1206 | DISPATCH(); |
| 1207 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1208 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1209 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1210 | PyObject *right = POP(); |
| 1211 | PyObject *left = TOP(); |
| 1212 | PyObject *res = PyNumber_Multiply(left, right); |
| 1213 | Py_DECREF(left); |
| 1214 | Py_DECREF(right); |
| 1215 | SET_TOP(res); |
| 1216 | if (res == NULL) |
| 1217 | goto error; |
| 1218 | DISPATCH(); |
| 1219 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1220 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1221 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1222 | PyObject *right = POP(); |
| 1223 | PyObject *left = TOP(); |
| 1224 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1225 | Py_DECREF(left); |
| 1226 | Py_DECREF(right); |
| 1227 | SET_TOP(res); |
| 1228 | if (res == NULL) |
| 1229 | goto error; |
| 1230 | DISPATCH(); |
| 1231 | } |
| 1232 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1233 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1234 | PyObject *divisor = POP(); |
| 1235 | PyObject *dividend = TOP(); |
| 1236 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1237 | Py_DECREF(dividend); |
| 1238 | Py_DECREF(divisor); |
| 1239 | SET_TOP(quotient); |
| 1240 | if (quotient == NULL) |
| 1241 | goto error; |
| 1242 | DISPATCH(); |
| 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(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1246 | PyObject *divisor = POP(); |
| 1247 | PyObject *dividend = TOP(); |
| 1248 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1249 | Py_DECREF(dividend); |
| 1250 | Py_DECREF(divisor); |
| 1251 | SET_TOP(quotient); |
| 1252 | if (quotient == NULL) |
| 1253 | goto error; |
| 1254 | DISPATCH(); |
| 1255 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1256 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1257 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1258 | PyObject *divisor = POP(); |
| 1259 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1260 | PyObject *res; |
| 1261 | if (PyUnicode_CheckExact(dividend) && ( |
| 1262 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1263 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1264 | // (see issue28598) |
| 1265 | res = PyUnicode_Format(dividend, divisor); |
| 1266 | } else { |
| 1267 | res = PyNumber_Remainder(dividend, divisor); |
| 1268 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1269 | Py_DECREF(divisor); |
| 1270 | Py_DECREF(dividend); |
| 1271 | SET_TOP(res); |
| 1272 | if (res == NULL) |
| 1273 | goto error; |
| 1274 | DISPATCH(); |
| 1275 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1276 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1277 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1278 | PyObject *right = POP(); |
| 1279 | PyObject *left = TOP(); |
| 1280 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1281 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1282 | CPython using bytecode, it is simply worthless. |
| 1283 | See http://bugs.python.org/issue21955 and |
| 1284 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1285 | no patch shown any impact on a realistic benchmark, only a minor |
| 1286 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1287 | if (PyUnicode_CheckExact(left) && |
| 1288 | PyUnicode_CheckExact(right)) { |
| 1289 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1290 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1291 | } |
| 1292 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1293 | sum = PyNumber_Add(left, right); |
| 1294 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1295 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1296 | Py_DECREF(right); |
| 1297 | SET_TOP(sum); |
| 1298 | if (sum == NULL) |
| 1299 | goto error; |
| 1300 | DISPATCH(); |
| 1301 | } |
| 1302 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1303 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1304 | PyObject *right = POP(); |
| 1305 | PyObject *left = TOP(); |
| 1306 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1307 | Py_DECREF(right); |
| 1308 | Py_DECREF(left); |
| 1309 | SET_TOP(diff); |
| 1310 | if (diff == NULL) |
| 1311 | goto error; |
| 1312 | DISPATCH(); |
| 1313 | } |
| 1314 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1315 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1316 | PyObject *sub = POP(); |
| 1317 | PyObject *container = TOP(); |
| 1318 | PyObject *res = PyObject_GetItem(container, sub); |
| 1319 | Py_DECREF(container); |
| 1320 | Py_DECREF(sub); |
| 1321 | SET_TOP(res); |
| 1322 | if (res == NULL) |
| 1323 | goto error; |
| 1324 | DISPATCH(); |
| 1325 | } |
| 1326 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1327 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1328 | PyObject *right = POP(); |
| 1329 | PyObject *left = TOP(); |
| 1330 | PyObject *res = PyNumber_Lshift(left, right); |
| 1331 | Py_DECREF(left); |
| 1332 | Py_DECREF(right); |
| 1333 | SET_TOP(res); |
| 1334 | if (res == NULL) |
| 1335 | goto error; |
| 1336 | DISPATCH(); |
| 1337 | } |
| 1338 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1339 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1340 | PyObject *right = POP(); |
| 1341 | PyObject *left = TOP(); |
| 1342 | PyObject *res = PyNumber_Rshift(left, right); |
| 1343 | Py_DECREF(left); |
| 1344 | Py_DECREF(right); |
| 1345 | SET_TOP(res); |
| 1346 | if (res == NULL) |
| 1347 | goto error; |
| 1348 | DISPATCH(); |
| 1349 | } |
| 1350 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1351 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1352 | PyObject *right = POP(); |
| 1353 | PyObject *left = TOP(); |
| 1354 | PyObject *res = PyNumber_And(left, right); |
| 1355 | Py_DECREF(left); |
| 1356 | Py_DECREF(right); |
| 1357 | SET_TOP(res); |
| 1358 | if (res == NULL) |
| 1359 | goto error; |
| 1360 | DISPATCH(); |
| 1361 | } |
| 1362 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1363 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1364 | PyObject *right = POP(); |
| 1365 | PyObject *left = TOP(); |
| 1366 | PyObject *res = PyNumber_Xor(left, right); |
| 1367 | Py_DECREF(left); |
| 1368 | Py_DECREF(right); |
| 1369 | SET_TOP(res); |
| 1370 | if (res == NULL) |
| 1371 | goto error; |
| 1372 | DISPATCH(); |
| 1373 | } |
| 1374 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1375 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1376 | PyObject *right = POP(); |
| 1377 | PyObject *left = TOP(); |
| 1378 | PyObject *res = PyNumber_Or(left, right); |
| 1379 | Py_DECREF(left); |
| 1380 | Py_DECREF(right); |
| 1381 | SET_TOP(res); |
| 1382 | if (res == NULL) |
| 1383 | goto error; |
| 1384 | DISPATCH(); |
| 1385 | } |
| 1386 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1387 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1388 | PyObject *v = POP(); |
| 1389 | PyObject *list = PEEK(oparg); |
| 1390 | int err; |
| 1391 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1393 | if (err != 0) |
| 1394 | goto error; |
| 1395 | PREDICT(JUMP_ABSOLUTE); |
| 1396 | DISPATCH(); |
| 1397 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1398 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1399 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1400 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1401 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1402 | int err; |
| 1403 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1405 | if (err != 0) |
| 1406 | goto error; |
| 1407 | PREDICT(JUMP_ABSOLUTE); |
| 1408 | DISPATCH(); |
| 1409 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1410 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1411 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1412 | PyObject *exp = POP(); |
| 1413 | PyObject *base = TOP(); |
| 1414 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1415 | Py_DECREF(base); |
| 1416 | Py_DECREF(exp); |
| 1417 | SET_TOP(res); |
| 1418 | if (res == NULL) |
| 1419 | goto error; |
| 1420 | DISPATCH(); |
| 1421 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1422 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1423 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1424 | PyObject *right = POP(); |
| 1425 | PyObject *left = TOP(); |
| 1426 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1427 | Py_DECREF(left); |
| 1428 | Py_DECREF(right); |
| 1429 | SET_TOP(res); |
| 1430 | if (res == NULL) |
| 1431 | goto error; |
| 1432 | DISPATCH(); |
| 1433 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1434 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1435 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1436 | PyObject *right = POP(); |
| 1437 | PyObject *left = TOP(); |
| 1438 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1439 | Py_DECREF(left); |
| 1440 | Py_DECREF(right); |
| 1441 | SET_TOP(res); |
| 1442 | if (res == NULL) |
| 1443 | goto error; |
| 1444 | DISPATCH(); |
| 1445 | } |
| 1446 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1447 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1448 | PyObject *divisor = POP(); |
| 1449 | PyObject *dividend = TOP(); |
| 1450 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1451 | Py_DECREF(dividend); |
| 1452 | Py_DECREF(divisor); |
| 1453 | SET_TOP(quotient); |
| 1454 | if (quotient == NULL) |
| 1455 | goto error; |
| 1456 | DISPATCH(); |
| 1457 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1458 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1459 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1460 | PyObject *divisor = POP(); |
| 1461 | PyObject *dividend = TOP(); |
| 1462 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1463 | Py_DECREF(dividend); |
| 1464 | Py_DECREF(divisor); |
| 1465 | SET_TOP(quotient); |
| 1466 | if (quotient == NULL) |
| 1467 | goto error; |
| 1468 | DISPATCH(); |
| 1469 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1470 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1471 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1472 | PyObject *right = POP(); |
| 1473 | PyObject *left = TOP(); |
| 1474 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1475 | Py_DECREF(left); |
| 1476 | Py_DECREF(right); |
| 1477 | SET_TOP(mod); |
| 1478 | if (mod == NULL) |
| 1479 | goto error; |
| 1480 | DISPATCH(); |
| 1481 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1482 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1483 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1484 | PyObject *right = POP(); |
| 1485 | PyObject *left = TOP(); |
| 1486 | PyObject *sum; |
| 1487 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1488 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1489 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1490 | } |
| 1491 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1492 | sum = PyNumber_InPlaceAdd(left, right); |
| 1493 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1494 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1495 | Py_DECREF(right); |
| 1496 | SET_TOP(sum); |
| 1497 | if (sum == NULL) |
| 1498 | goto error; |
| 1499 | DISPATCH(); |
| 1500 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1501 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1502 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1503 | PyObject *right = POP(); |
| 1504 | PyObject *left = TOP(); |
| 1505 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1506 | Py_DECREF(left); |
| 1507 | Py_DECREF(right); |
| 1508 | SET_TOP(diff); |
| 1509 | if (diff == NULL) |
| 1510 | goto error; |
| 1511 | DISPATCH(); |
| 1512 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1513 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1514 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1515 | PyObject *right = POP(); |
| 1516 | PyObject *left = TOP(); |
| 1517 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1518 | Py_DECREF(left); |
| 1519 | Py_DECREF(right); |
| 1520 | SET_TOP(res); |
| 1521 | if (res == NULL) |
| 1522 | goto error; |
| 1523 | DISPATCH(); |
| 1524 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1525 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1526 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1527 | PyObject *right = POP(); |
| 1528 | PyObject *left = TOP(); |
| 1529 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1530 | Py_DECREF(left); |
| 1531 | Py_DECREF(right); |
| 1532 | SET_TOP(res); |
| 1533 | if (res == NULL) |
| 1534 | goto error; |
| 1535 | DISPATCH(); |
| 1536 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1537 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1538 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1539 | PyObject *right = POP(); |
| 1540 | PyObject *left = TOP(); |
| 1541 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1542 | Py_DECREF(left); |
| 1543 | Py_DECREF(right); |
| 1544 | SET_TOP(res); |
| 1545 | if (res == NULL) |
| 1546 | goto error; |
| 1547 | DISPATCH(); |
| 1548 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1549 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1550 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1551 | PyObject *right = POP(); |
| 1552 | PyObject *left = TOP(); |
| 1553 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1554 | Py_DECREF(left); |
| 1555 | Py_DECREF(right); |
| 1556 | SET_TOP(res); |
| 1557 | if (res == NULL) |
| 1558 | goto error; |
| 1559 | DISPATCH(); |
| 1560 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1561 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1562 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1563 | PyObject *right = POP(); |
| 1564 | PyObject *left = TOP(); |
| 1565 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1566 | Py_DECREF(left); |
| 1567 | Py_DECREF(right); |
| 1568 | SET_TOP(res); |
| 1569 | if (res == NULL) |
| 1570 | goto error; |
| 1571 | DISPATCH(); |
| 1572 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1573 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1574 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1575 | PyObject *sub = TOP(); |
| 1576 | PyObject *container = SECOND(); |
| 1577 | PyObject *v = THIRD(); |
| 1578 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1579 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1580 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1581 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1583 | Py_DECREF(container); |
| 1584 | Py_DECREF(sub); |
| 1585 | if (err != 0) |
| 1586 | goto error; |
| 1587 | DISPATCH(); |
| 1588 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1589 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1590 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1591 | PyObject *sub = TOP(); |
| 1592 | PyObject *container = SECOND(); |
| 1593 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1594 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1595 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1596 | err = PyObject_DelItem(container, sub); |
| 1597 | Py_DECREF(container); |
| 1598 | Py_DECREF(sub); |
| 1599 | if (err != 0) |
| 1600 | goto error; |
| 1601 | DISPATCH(); |
| 1602 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1603 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1604 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1605 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1606 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1607 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1608 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1609 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | PyErr_SetString(PyExc_RuntimeError, |
| 1611 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1612 | Py_DECREF(value); |
| 1613 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1615 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1616 | Py_DECREF(value); |
| 1617 | if (res == NULL) |
| 1618 | goto error; |
| 1619 | Py_DECREF(res); |
| 1620 | DISPATCH(); |
| 1621 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1622 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1623 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1624 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | switch (oparg) { |
| 1626 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1627 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1628 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1630 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1631 | /* fall through */ |
| 1632 | case 0: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1633 | if (do_raise(exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1634 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1635 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | break; |
| 1637 | default: |
| 1638 | PyErr_SetString(PyExc_SystemError, |
| 1639 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | break; |
| 1641 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1642 | goto error; |
| 1643 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1644 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1645 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1647 | assert(f->f_iblock == 0); |
| 1648 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1649 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1650 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1651 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1652 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1653 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1654 | PyObject *obj = TOP(); |
| 1655 | PyTypeObject *type = Py_TYPE(obj); |
| 1656 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1657 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1658 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1659 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1660 | |
| 1661 | if (getter != NULL) { |
| 1662 | iter = (*getter)(obj); |
| 1663 | Py_DECREF(obj); |
| 1664 | if (iter == NULL) { |
| 1665 | SET_TOP(NULL); |
| 1666 | goto error; |
| 1667 | } |
| 1668 | } |
| 1669 | else { |
| 1670 | SET_TOP(NULL); |
| 1671 | PyErr_Format( |
| 1672 | PyExc_TypeError, |
| 1673 | "'async for' requires an object with " |
| 1674 | "__aiter__ method, got %.100s", |
| 1675 | type->tp_name); |
| 1676 | Py_DECREF(obj); |
| 1677 | goto error; |
| 1678 | } |
| 1679 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1680 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 1681 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1682 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1683 | SET_TOP(NULL); |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1684 | PyErr_Format( |
| 1685 | PyExc_TypeError, |
| 1686 | "'async for' received an object from __aiter__ " |
| 1687 | "that does not implement __anext__: %.100s", |
| 1688 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1689 | Py_DECREF(iter); |
| 1690 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1691 | } |
| 1692 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1693 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1694 | DISPATCH(); |
| 1695 | } |
| 1696 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1697 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1698 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1699 | PyObject *next_iter = NULL; |
| 1700 | PyObject *awaitable = NULL; |
| 1701 | PyObject *aiter = TOP(); |
| 1702 | PyTypeObject *type = Py_TYPE(aiter); |
| 1703 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1704 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1705 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1706 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1707 | goto error; |
| 1708 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1709 | } else { |
| 1710 | if (type->tp_as_async != NULL){ |
| 1711 | getter = type->tp_as_async->am_anext; |
| 1712 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1713 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1714 | if (getter != NULL) { |
| 1715 | next_iter = (*getter)(aiter); |
| 1716 | if (next_iter == NULL) { |
| 1717 | goto error; |
| 1718 | } |
| 1719 | } |
| 1720 | else { |
| 1721 | PyErr_Format( |
| 1722 | PyExc_TypeError, |
| 1723 | "'async for' requires an iterator with " |
| 1724 | "__anext__ method, got %.100s", |
| 1725 | type->tp_name); |
| 1726 | goto error; |
| 1727 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1728 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1729 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1730 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1731 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1732 | PyExc_TypeError, |
| 1733 | "'async for' received an invalid object " |
| 1734 | "from __anext__: %.100s", |
| 1735 | Py_TYPE(next_iter)->tp_name); |
| 1736 | |
| 1737 | Py_DECREF(next_iter); |
| 1738 | goto error; |
| 1739 | } else { |
| 1740 | Py_DECREF(next_iter); |
| 1741 | } |
| 1742 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1743 | |
| 1744 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1745 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1746 | DISPATCH(); |
| 1747 | } |
| 1748 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1749 | case TARGET(GET_AWAITABLE): { |
| 1750 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1751 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1752 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1753 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 1754 | if (iter == NULL) { |
| 1755 | format_awaitable_error(Py_TYPE(iterable), |
| 1756 | _Py_OPCODE(next_instr[-2])); |
| 1757 | } |
| 1758 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1759 | Py_DECREF(iterable); |
| 1760 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1761 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1762 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1763 | if (yf != NULL) { |
| 1764 | /* `iter` is a coroutine object that is being |
| 1765 | awaited, `yf` is a pointer to the current awaitable |
| 1766 | being awaited on. */ |
| 1767 | Py_DECREF(yf); |
| 1768 | Py_CLEAR(iter); |
| 1769 | PyErr_SetString( |
| 1770 | PyExc_RuntimeError, |
| 1771 | "coroutine is being awaited already"); |
| 1772 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 1773 | } |
| 1774 | } |
| 1775 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1776 | SET_TOP(iter); /* Even if it's NULL */ |
| 1777 | |
| 1778 | if (iter == NULL) { |
| 1779 | goto error; |
| 1780 | } |
| 1781 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1782 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1783 | DISPATCH(); |
| 1784 | } |
| 1785 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1786 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1787 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1788 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1789 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1790 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 1791 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1792 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 1793 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1794 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1795 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1796 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1797 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1798 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1799 | Py_DECREF(v); |
| 1800 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1801 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1802 | if (tstate->c_tracefunc != NULL |
| 1803 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1804 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 1805 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1806 | if (err < 0) |
| 1807 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1808 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1809 | SET_TOP(val); |
| 1810 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1811 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1812 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1813 | f->f_stacktop = stack_pointer; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1814 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 1815 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1816 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1817 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1818 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1819 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1820 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1822 | |
| 1823 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 1824 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 1825 | Py_DECREF(retval); |
| 1826 | if (w == NULL) { |
| 1827 | retval = NULL; |
| 1828 | goto error; |
| 1829 | } |
| 1830 | retval = w; |
| 1831 | } |
| 1832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | f->f_stacktop = stack_pointer; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1834 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1835 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1836 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1837 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1838 | PyObject *type, *value, *traceback; |
| 1839 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1840 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1841 | if (b->b_type != EXCEPT_HANDLER) { |
| 1842 | PyErr_SetString(PyExc_SystemError, |
| 1843 | "popped block is not an except handler"); |
| 1844 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1846 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 1847 | STACK_LEVEL() <= (b)->b_level + 4); |
| 1848 | exc_info = tstate->exc_info; |
| 1849 | type = exc_info->exc_type; |
| 1850 | value = exc_info->exc_value; |
| 1851 | traceback = exc_info->exc_traceback; |
| 1852 | exc_info->exc_type = POP(); |
| 1853 | exc_info->exc_value = POP(); |
| 1854 | exc_info->exc_traceback = POP(); |
| 1855 | Py_XDECREF(type); |
| 1856 | Py_XDECREF(value); |
| 1857 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1859 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1860 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1861 | case TARGET(POP_BLOCK): { |
| 1862 | PREDICTED(POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1863 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1865 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1866 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1867 | case TARGET(POP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1868 | /* If oparg is 0 at the top of the stack are 1 or 6 values: |
| 1869 | Either: |
| 1870 | - TOP = NULL or an integer |
| 1871 | or: |
| 1872 | - (TOP, SECOND, THIRD) = exc_info() |
| 1873 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 1874 | |
| 1875 | If oparg is 1 the value for 'return' was additionally pushed |
| 1876 | at the top of the stack. |
| 1877 | */ |
| 1878 | PyObject *res = NULL; |
| 1879 | if (oparg) { |
| 1880 | res = POP(); |
| 1881 | } |
| 1882 | PyObject *exc = POP(); |
| 1883 | if (exc == NULL || PyLong_CheckExact(exc)) { |
| 1884 | Py_XDECREF(exc); |
| 1885 | } |
| 1886 | else { |
| 1887 | Py_DECREF(exc); |
| 1888 | Py_DECREF(POP()); |
| 1889 | Py_DECREF(POP()); |
| 1890 | |
| 1891 | PyObject *type, *value, *traceback; |
| 1892 | _PyErr_StackItem *exc_info; |
| 1893 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1894 | if (b->b_type != EXCEPT_HANDLER) { |
| 1895 | PyErr_SetString(PyExc_SystemError, |
| 1896 | "popped block is not an except handler"); |
| 1897 | Py_XDECREF(res); |
| 1898 | goto error; |
| 1899 | } |
| 1900 | assert(STACK_LEVEL() == (b)->b_level + 3); |
| 1901 | exc_info = tstate->exc_info; |
| 1902 | type = exc_info->exc_type; |
| 1903 | value = exc_info->exc_value; |
| 1904 | traceback = exc_info->exc_traceback; |
| 1905 | exc_info->exc_type = POP(); |
| 1906 | exc_info->exc_value = POP(); |
| 1907 | exc_info->exc_traceback = POP(); |
| 1908 | Py_XDECREF(type); |
| 1909 | Py_XDECREF(value); |
| 1910 | Py_XDECREF(traceback); |
| 1911 | } |
| 1912 | if (oparg) { |
| 1913 | PUSH(res); |
| 1914 | } |
| 1915 | DISPATCH(); |
| 1916 | } |
| 1917 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1918 | case TARGET(CALL_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1919 | PyObject *ret = PyLong_FromLong(INSTR_OFFSET()); |
| 1920 | if (ret == NULL) { |
| 1921 | goto error; |
| 1922 | } |
| 1923 | PUSH(ret); |
| 1924 | JUMPBY(oparg); |
| 1925 | FAST_DISPATCH(); |
| 1926 | } |
| 1927 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1928 | case TARGET(BEGIN_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1929 | /* Push NULL onto the stack for using it in END_FINALLY, |
| 1930 | POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH. |
| 1931 | */ |
| 1932 | PUSH(NULL); |
| 1933 | FAST_DISPATCH(); |
| 1934 | } |
| 1935 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1936 | case TARGET(END_FINALLY): { |
| 1937 | PREDICTED(END_FINALLY); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1938 | /* At the top of the stack are 1 or 6 values: |
| 1939 | Either: |
| 1940 | - TOP = NULL or an integer |
| 1941 | or: |
| 1942 | - (TOP, SECOND, THIRD) = exc_info() |
| 1943 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 1944 | */ |
| 1945 | PyObject *exc = POP(); |
| 1946 | if (exc == NULL) { |
| 1947 | FAST_DISPATCH(); |
| 1948 | } |
| 1949 | else if (PyLong_CheckExact(exc)) { |
| 1950 | int ret = _PyLong_AsInt(exc); |
| 1951 | Py_DECREF(exc); |
| 1952 | if (ret == -1 && PyErr_Occurred()) { |
| 1953 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1955 | JUMPTO(ret); |
| 1956 | FAST_DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1958 | else { |
| 1959 | assert(PyExceptionClass_Check(exc)); |
| 1960 | PyObject *val = POP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1961 | PyObject *tb = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1962 | PyErr_Restore(exc, val, tb); |
| 1963 | goto exception_unwind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1965 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1966 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1967 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1968 | PyObject *exc = POP(); |
| 1969 | assert(PyExceptionClass_Check(exc)); |
| 1970 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 1971 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1972 | assert(b->b_type == EXCEPT_HANDLER); |
| 1973 | Py_DECREF(exc); |
| 1974 | UNWIND_EXCEPT_HANDLER(b); |
| 1975 | Py_DECREF(POP()); |
| 1976 | JUMPBY(oparg); |
| 1977 | FAST_DISPATCH(); |
| 1978 | } |
| 1979 | else { |
| 1980 | PyObject *val = POP(); |
| 1981 | PyObject *tb = POP(); |
| 1982 | PyErr_Restore(exc, val, tb); |
| 1983 | goto exception_unwind; |
| 1984 | } |
| 1985 | } |
| 1986 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1987 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 1988 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 1989 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1990 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 1991 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1992 | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
| 1993 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 1994 | PyErr_SetString(PyExc_NameError, |
| 1995 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1996 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 1997 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1998 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 1999 | } |
| 2000 | else { |
| 2001 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2002 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2003 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2004 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2005 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2006 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2007 | PyErr_SetString(PyExc_NameError, |
| 2008 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2009 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2010 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2011 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2012 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2013 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2014 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2015 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2016 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2017 | PyObject *name = GETITEM(names, oparg); |
| 2018 | PyObject *v = POP(); |
| 2019 | PyObject *ns = f->f_locals; |
| 2020 | int err; |
| 2021 | if (ns == NULL) { |
| 2022 | PyErr_Format(PyExc_SystemError, |
| 2023 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2025 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2027 | if (PyDict_CheckExact(ns)) |
| 2028 | err = PyDict_SetItem(ns, name, v); |
| 2029 | else |
| 2030 | err = PyObject_SetItem(ns, name, v); |
| 2031 | Py_DECREF(v); |
| 2032 | if (err != 0) |
| 2033 | goto error; |
| 2034 | DISPATCH(); |
| 2035 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2036 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2037 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2038 | PyObject *name = GETITEM(names, oparg); |
| 2039 | PyObject *ns = f->f_locals; |
| 2040 | int err; |
| 2041 | if (ns == NULL) { |
| 2042 | PyErr_Format(PyExc_SystemError, |
| 2043 | "no locals when deleting %R", name); |
| 2044 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2046 | err = PyObject_DelItem(ns, name); |
| 2047 | if (err != 0) { |
| 2048 | format_exc_check_arg(PyExc_NameError, |
| 2049 | NAME_ERROR_MSG, |
| 2050 | name); |
| 2051 | goto error; |
| 2052 | } |
| 2053 | DISPATCH(); |
| 2054 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2055 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2056 | case TARGET(UNPACK_SEQUENCE): { |
| 2057 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2058 | PyObject *seq = POP(), *item, **items; |
| 2059 | if (PyTuple_CheckExact(seq) && |
| 2060 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2061 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2063 | item = items[oparg]; |
| 2064 | Py_INCREF(item); |
| 2065 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2067 | } else if (PyList_CheckExact(seq) && |
| 2068 | PyList_GET_SIZE(seq) == oparg) { |
| 2069 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2071 | item = items[oparg]; |
| 2072 | Py_INCREF(item); |
| 2073 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2075 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2077 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | } else { |
| 2079 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2080 | Py_DECREF(seq); |
| 2081 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2083 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2084 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2086 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2087 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2088 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2089 | PyObject *seq = POP(); |
| 2090 | |
| 2091 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2092 | stack_pointer + totalargs)) { |
| 2093 | stack_pointer += totalargs; |
| 2094 | } else { |
| 2095 | Py_DECREF(seq); |
| 2096 | goto error; |
| 2097 | } |
| 2098 | Py_DECREF(seq); |
| 2099 | DISPATCH(); |
| 2100 | } |
| 2101 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2102 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2103 | PyObject *name = GETITEM(names, oparg); |
| 2104 | PyObject *owner = TOP(); |
| 2105 | PyObject *v = SECOND(); |
| 2106 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2107 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2108 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2110 | Py_DECREF(owner); |
| 2111 | if (err != 0) |
| 2112 | goto error; |
| 2113 | DISPATCH(); |
| 2114 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2115 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2116 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2117 | PyObject *name = GETITEM(names, oparg); |
| 2118 | PyObject *owner = POP(); |
| 2119 | int err; |
| 2120 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2121 | Py_DECREF(owner); |
| 2122 | if (err != 0) |
| 2123 | goto error; |
| 2124 | DISPATCH(); |
| 2125 | } |
| 2126 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2127 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2128 | PyObject *name = GETITEM(names, oparg); |
| 2129 | PyObject *v = POP(); |
| 2130 | int err; |
| 2131 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2133 | if (err != 0) |
| 2134 | goto error; |
| 2135 | DISPATCH(); |
| 2136 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2137 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2138 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2139 | PyObject *name = GETITEM(names, oparg); |
| 2140 | int err; |
| 2141 | err = PyDict_DelItem(f->f_globals, name); |
| 2142 | if (err != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | format_exc_check_arg( |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2144 | PyExc_NameError, NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2145 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2146 | } |
| 2147 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2148 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2149 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2150 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2151 | PyObject *name = GETITEM(names, oparg); |
| 2152 | PyObject *locals = f->f_locals; |
| 2153 | PyObject *v; |
| 2154 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2156 | "no locals when loading %R", name); |
| 2157 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2158 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2159 | if (PyDict_CheckExact(locals)) { |
| 2160 | v = PyDict_GetItem(locals, name); |
| 2161 | Py_XINCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | } |
| 2163 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2164 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2165 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2166 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2167 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | PyErr_Clear(); |
| 2169 | } |
| 2170 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2171 | if (v == NULL) { |
| 2172 | v = PyDict_GetItem(f->f_globals, name); |
| 2173 | Py_XINCREF(v); |
| 2174 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2175 | if (PyDict_CheckExact(f->f_builtins)) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2176 | v = PyDict_GetItem(f->f_builtins, name); |
| 2177 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2178 | format_exc_check_arg( |
| 2179 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2180 | NAME_ERROR_MSG, name); |
| 2181 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2182 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2183 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2184 | } |
| 2185 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2186 | v = PyObject_GetItem(f->f_builtins, name); |
| 2187 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2188 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2189 | format_exc_check_arg( |
| 2190 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2191 | NAME_ERROR_MSG, name); |
| 2192 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2193 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2194 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2197 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2199 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2200 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2201 | case TARGET(LOAD_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2202 | PyObject *name = GETITEM(names, oparg); |
| 2203 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2204 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2205 | && PyDict_CheckExact(f->f_builtins)) |
| 2206 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2207 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2208 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2209 | name); |
| 2210 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2211 | if (!_PyErr_OCCURRED()) { |
| 2212 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2213 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2214 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2215 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2216 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2217 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2219 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2220 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2221 | else { |
| 2222 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2223 | |
| 2224 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2225 | v = PyObject_GetItem(f->f_globals, name); |
| 2226 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2227 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2228 | goto error; |
| 2229 | PyErr_Clear(); |
| 2230 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2231 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2232 | v = PyObject_GetItem(f->f_builtins, name); |
| 2233 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2234 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2235 | format_exc_check_arg( |
| 2236 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2237 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2238 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2242 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2244 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2245 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2246 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2247 | PyObject *v = GETLOCAL(oparg); |
| 2248 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | SETLOCAL(oparg, NULL); |
| 2250 | DISPATCH(); |
| 2251 | } |
| 2252 | format_exc_check_arg( |
| 2253 | PyExc_UnboundLocalError, |
| 2254 | UNBOUNDLOCAL_ERROR_MSG, |
| 2255 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2256 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2257 | goto error; |
| 2258 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2259 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2260 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2261 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2262 | PyObject *oldobj = PyCell_GET(cell); |
| 2263 | if (oldobj != NULL) { |
| 2264 | PyCell_SET(cell, NULL); |
| 2265 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2266 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2267 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2268 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2269 | goto error; |
| 2270 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2271 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2272 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2273 | PyObject *cell = freevars[oparg]; |
| 2274 | Py_INCREF(cell); |
| 2275 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2277 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2278 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2279 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2280 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2281 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2282 | assert(locals); |
| 2283 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2284 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2285 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2286 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2287 | if (PyDict_CheckExact(locals)) { |
| 2288 | value = PyDict_GetItem(locals, name); |
| 2289 | Py_XINCREF(value); |
| 2290 | } |
| 2291 | else { |
| 2292 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2293 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2294 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2295 | goto error; |
| 2296 | PyErr_Clear(); |
| 2297 | } |
| 2298 | } |
| 2299 | if (!value) { |
| 2300 | PyObject *cell = freevars[oparg]; |
| 2301 | value = PyCell_GET(cell); |
| 2302 | if (value == NULL) { |
| 2303 | format_exc_unbound(co, oparg); |
| 2304 | goto error; |
| 2305 | } |
| 2306 | Py_INCREF(value); |
| 2307 | } |
| 2308 | PUSH(value); |
| 2309 | DISPATCH(); |
| 2310 | } |
| 2311 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2312 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2313 | PyObject *cell = freevars[oparg]; |
| 2314 | PyObject *value = PyCell_GET(cell); |
| 2315 | if (value == NULL) { |
| 2316 | format_exc_unbound(co, oparg); |
| 2317 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2319 | Py_INCREF(value); |
| 2320 | PUSH(value); |
| 2321 | DISPATCH(); |
| 2322 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2323 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2324 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2325 | PyObject *v = POP(); |
| 2326 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2327 | PyObject *oldobj = PyCell_GET(cell); |
| 2328 | PyCell_SET(cell, v); |
| 2329 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2330 | DISPATCH(); |
| 2331 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2332 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2333 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2334 | PyObject *str; |
| 2335 | PyObject *empty = PyUnicode_New(0, 0); |
| 2336 | if (empty == NULL) { |
| 2337 | goto error; |
| 2338 | } |
| 2339 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2340 | Py_DECREF(empty); |
| 2341 | if (str == NULL) |
| 2342 | goto error; |
| 2343 | while (--oparg >= 0) { |
| 2344 | PyObject *item = POP(); |
| 2345 | Py_DECREF(item); |
| 2346 | } |
| 2347 | PUSH(str); |
| 2348 | DISPATCH(); |
| 2349 | } |
| 2350 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2351 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2352 | PyObject *tup = PyTuple_New(oparg); |
| 2353 | if (tup == NULL) |
| 2354 | goto error; |
| 2355 | while (--oparg >= 0) { |
| 2356 | PyObject *item = POP(); |
| 2357 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2358 | } |
| 2359 | PUSH(tup); |
| 2360 | DISPATCH(); |
| 2361 | } |
| 2362 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2363 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2364 | PyObject *list = PyList_New(oparg); |
| 2365 | if (list == NULL) |
| 2366 | goto error; |
| 2367 | while (--oparg >= 0) { |
| 2368 | PyObject *item = POP(); |
| 2369 | PyList_SET_ITEM(list, oparg, item); |
| 2370 | } |
| 2371 | PUSH(list); |
| 2372 | DISPATCH(); |
| 2373 | } |
| 2374 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2375 | case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL): |
| 2376 | case TARGET(BUILD_TUPLE_UNPACK): |
| 2377 | case TARGET(BUILD_LIST_UNPACK): { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2378 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2379 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2380 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2381 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2382 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2383 | if (sum == NULL) |
| 2384 | goto error; |
| 2385 | |
| 2386 | for (i = oparg; i > 0; i--) { |
| 2387 | PyObject *none_val; |
| 2388 | |
| 2389 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2390 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2391 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2392 | PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2393 | { |
| 2394 | check_args_iterable(PEEK(1 + oparg), PEEK(i)); |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2395 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2396 | Py_DECREF(sum); |
| 2397 | goto error; |
| 2398 | } |
| 2399 | Py_DECREF(none_val); |
| 2400 | } |
| 2401 | |
| 2402 | if (convert_to_tuple) { |
| 2403 | return_value = PyList_AsTuple(sum); |
| 2404 | Py_DECREF(sum); |
| 2405 | if (return_value == NULL) |
| 2406 | goto error; |
| 2407 | } |
| 2408 | else { |
| 2409 | return_value = sum; |
| 2410 | } |
| 2411 | |
| 2412 | while (oparg--) |
| 2413 | Py_DECREF(POP()); |
| 2414 | PUSH(return_value); |
| 2415 | DISPATCH(); |
| 2416 | } |
| 2417 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2418 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2419 | PyObject *set = PySet_New(NULL); |
| 2420 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2421 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2422 | if (set == NULL) |
| 2423 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2424 | for (i = oparg; i > 0; i--) { |
| 2425 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2426 | if (err == 0) |
| 2427 | err = PySet_Add(set, item); |
| 2428 | Py_DECREF(item); |
| 2429 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2430 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2431 | if (err != 0) { |
| 2432 | Py_DECREF(set); |
| 2433 | goto error; |
| 2434 | } |
| 2435 | PUSH(set); |
| 2436 | DISPATCH(); |
| 2437 | } |
| 2438 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2439 | case TARGET(BUILD_SET_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2440 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2441 | PyObject *sum = PySet_New(NULL); |
| 2442 | if (sum == NULL) |
| 2443 | goto error; |
| 2444 | |
| 2445 | for (i = oparg; i > 0; i--) { |
| 2446 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2447 | Py_DECREF(sum); |
| 2448 | goto error; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | while (oparg--) |
| 2453 | Py_DECREF(POP()); |
| 2454 | PUSH(sum); |
| 2455 | DISPATCH(); |
| 2456 | } |
| 2457 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2458 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2459 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2460 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2461 | if (map == NULL) |
| 2462 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2463 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2464 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2465 | PyObject *key = PEEK(2*i); |
| 2466 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2467 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2468 | if (err != 0) { |
| 2469 | Py_DECREF(map); |
| 2470 | goto error; |
| 2471 | } |
| 2472 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2473 | |
| 2474 | while (oparg--) { |
| 2475 | Py_DECREF(POP()); |
| 2476 | Py_DECREF(POP()); |
| 2477 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2478 | PUSH(map); |
| 2479 | DISPATCH(); |
| 2480 | } |
| 2481 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2482 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2483 | _Py_IDENTIFIER(__annotations__); |
| 2484 | int err; |
| 2485 | PyObject *ann_dict; |
| 2486 | if (f->f_locals == NULL) { |
| 2487 | PyErr_Format(PyExc_SystemError, |
| 2488 | "no locals found when setting up annotations"); |
| 2489 | goto error; |
| 2490 | } |
| 2491 | /* check if __annotations__ in locals()... */ |
| 2492 | if (PyDict_CheckExact(f->f_locals)) { |
| 2493 | ann_dict = _PyDict_GetItemId(f->f_locals, |
| 2494 | &PyId___annotations__); |
| 2495 | if (ann_dict == NULL) { |
| 2496 | /* ...if not, create a new one */ |
| 2497 | ann_dict = PyDict_New(); |
| 2498 | if (ann_dict == NULL) { |
| 2499 | goto error; |
| 2500 | } |
| 2501 | err = _PyDict_SetItemId(f->f_locals, |
| 2502 | &PyId___annotations__, ann_dict); |
| 2503 | Py_DECREF(ann_dict); |
| 2504 | if (err != 0) { |
| 2505 | goto error; |
| 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | else { |
| 2510 | /* do the same if locals() is not a dict */ |
| 2511 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2512 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2513 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2514 | } |
| 2515 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2516 | if (ann_dict == NULL) { |
| 2517 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2518 | goto error; |
| 2519 | } |
| 2520 | PyErr_Clear(); |
| 2521 | ann_dict = PyDict_New(); |
| 2522 | if (ann_dict == NULL) { |
| 2523 | goto error; |
| 2524 | } |
| 2525 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2526 | Py_DECREF(ann_dict); |
| 2527 | if (err != 0) { |
| 2528 | goto error; |
| 2529 | } |
| 2530 | } |
| 2531 | else { |
| 2532 | Py_DECREF(ann_dict); |
| 2533 | } |
| 2534 | } |
| 2535 | DISPATCH(); |
| 2536 | } |
| 2537 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2538 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2539 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2540 | PyObject *map; |
| 2541 | PyObject *keys = TOP(); |
| 2542 | if (!PyTuple_CheckExact(keys) || |
| 2543 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2544 | PyErr_SetString(PyExc_SystemError, |
| 2545 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2546 | goto error; |
| 2547 | } |
| 2548 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2549 | if (map == NULL) { |
| 2550 | goto error; |
| 2551 | } |
| 2552 | for (i = oparg; i > 0; i--) { |
| 2553 | int err; |
| 2554 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2555 | PyObject *value = PEEK(i + 1); |
| 2556 | err = PyDict_SetItem(map, key, value); |
| 2557 | if (err != 0) { |
| 2558 | Py_DECREF(map); |
| 2559 | goto error; |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | Py_DECREF(POP()); |
| 2564 | while (oparg--) { |
| 2565 | Py_DECREF(POP()); |
| 2566 | } |
| 2567 | PUSH(map); |
| 2568 | DISPATCH(); |
| 2569 | } |
| 2570 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2571 | case TARGET(BUILD_MAP_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2572 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2573 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2574 | if (sum == NULL) |
| 2575 | goto error; |
| 2576 | |
| 2577 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2578 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2579 | if (PyDict_Update(sum, arg) < 0) { |
| 2580 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2581 | PyErr_Format(PyExc_TypeError, |
Berker Peksag | 8e9045d | 2016-10-02 13:08:25 +0300 | [diff] [blame] | 2582 | "'%.200s' object is not a mapping", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2583 | arg->ob_type->tp_name); |
| 2584 | } |
| 2585 | Py_DECREF(sum); |
| 2586 | goto error; |
| 2587 | } |
| 2588 | } |
| 2589 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2590 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2591 | Py_DECREF(POP()); |
| 2592 | PUSH(sum); |
| 2593 | DISPATCH(); |
| 2594 | } |
| 2595 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2596 | case TARGET(BUILD_MAP_UNPACK_WITH_CALL): { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2597 | Py_ssize_t i; |
| 2598 | PyObject *sum = PyDict_New(); |
| 2599 | if (sum == NULL) |
| 2600 | goto error; |
| 2601 | |
| 2602 | for (i = oparg; i > 0; i--) { |
| 2603 | PyObject *arg = PEEK(i); |
| 2604 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
| 2605 | PyObject *func = PEEK(2 + oparg); |
| 2606 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2607 | format_kwargs_mapping_error(func, arg); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2608 | } |
| 2609 | else if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2610 | PyObject *exc, *val, *tb; |
| 2611 | PyErr_Fetch(&exc, &val, &tb); |
| 2612 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 2613 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 2614 | if (!PyUnicode_Check(key)) { |
| 2615 | PyErr_Format(PyExc_TypeError, |
| 2616 | "%.200s%.200s keywords must be strings", |
| 2617 | PyEval_GetFuncName(func), |
| 2618 | PyEval_GetFuncDesc(func)); |
| 2619 | } else { |
| 2620 | PyErr_Format(PyExc_TypeError, |
| 2621 | "%.200s%.200s got multiple " |
| 2622 | "values for keyword argument '%U'", |
| 2623 | PyEval_GetFuncName(func), |
| 2624 | PyEval_GetFuncDesc(func), |
| 2625 | key); |
| 2626 | } |
| 2627 | Py_XDECREF(exc); |
| 2628 | Py_XDECREF(val); |
| 2629 | Py_XDECREF(tb); |
| 2630 | } |
| 2631 | else { |
| 2632 | PyErr_Restore(exc, val, tb); |
| 2633 | } |
| 2634 | } |
| 2635 | Py_DECREF(sum); |
| 2636 | goto error; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | while (oparg--) |
| 2641 | Py_DECREF(POP()); |
| 2642 | PUSH(sum); |
| 2643 | DISPATCH(); |
| 2644 | } |
| 2645 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2646 | case TARGET(MAP_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2647 | PyObject *key = TOP(); |
| 2648 | PyObject *value = SECOND(); |
| 2649 | PyObject *map; |
| 2650 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2651 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2652 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2653 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2654 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2655 | Py_DECREF(value); |
| 2656 | Py_DECREF(key); |
| 2657 | if (err != 0) |
| 2658 | goto error; |
| 2659 | PREDICT(JUMP_ABSOLUTE); |
| 2660 | DISPATCH(); |
| 2661 | } |
| 2662 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2663 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2664 | PyObject *name = GETITEM(names, oparg); |
| 2665 | PyObject *owner = TOP(); |
| 2666 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2667 | Py_DECREF(owner); |
| 2668 | SET_TOP(res); |
| 2669 | if (res == NULL) |
| 2670 | goto error; |
| 2671 | DISPATCH(); |
| 2672 | } |
| 2673 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2674 | case TARGET(COMPARE_OP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2675 | PyObject *right = POP(); |
| 2676 | PyObject *left = TOP(); |
| 2677 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2678 | Py_DECREF(left); |
| 2679 | Py_DECREF(right); |
| 2680 | SET_TOP(res); |
| 2681 | if (res == NULL) |
| 2682 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | PREDICT(POP_JUMP_IF_FALSE); |
| 2684 | PREDICT(POP_JUMP_IF_TRUE); |
| 2685 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2686 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2687 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2688 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2689 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2690 | PyObject *fromlist = POP(); |
| 2691 | PyObject *level = TOP(); |
| 2692 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2693 | res = import_name(f, name, fromlist, level); |
| 2694 | Py_DECREF(level); |
| 2695 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2696 | SET_TOP(res); |
| 2697 | if (res == NULL) |
| 2698 | goto error; |
| 2699 | DISPATCH(); |
| 2700 | } |
| 2701 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2702 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2703 | PyObject *from = POP(), *locals; |
| 2704 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2705 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 2706 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2707 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2708 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2709 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2710 | locals = f->f_locals; |
| 2711 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2712 | PyErr_SetString(PyExc_SystemError, |
| 2713 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2714 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2715 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2717 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2719 | Py_DECREF(from); |
| 2720 | if (err != 0) |
| 2721 | goto error; |
| 2722 | DISPATCH(); |
| 2723 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2724 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2725 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2726 | PyObject *name = GETITEM(names, oparg); |
| 2727 | PyObject *from = TOP(); |
| 2728 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2729 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2730 | PUSH(res); |
| 2731 | if (res == NULL) |
| 2732 | goto error; |
| 2733 | DISPATCH(); |
| 2734 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2735 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2736 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | JUMPBY(oparg); |
| 2738 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2739 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2740 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2741 | case TARGET(POP_JUMP_IF_FALSE): { |
| 2742 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2743 | PyObject *cond = POP(); |
| 2744 | int err; |
| 2745 | if (cond == Py_True) { |
| 2746 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2747 | FAST_DISPATCH(); |
| 2748 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2749 | if (cond == Py_False) { |
| 2750 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | JUMPTO(oparg); |
| 2752 | FAST_DISPATCH(); |
| 2753 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2754 | err = PyObject_IsTrue(cond); |
| 2755 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 2757 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | else if (err == 0) |
| 2759 | JUMPTO(oparg); |
| 2760 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2761 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2762 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2763 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2764 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2765 | case TARGET(POP_JUMP_IF_TRUE): { |
| 2766 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2767 | PyObject *cond = POP(); |
| 2768 | int err; |
| 2769 | if (cond == Py_False) { |
| 2770 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | FAST_DISPATCH(); |
| 2772 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2773 | if (cond == Py_True) { |
| 2774 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | JUMPTO(oparg); |
| 2776 | FAST_DISPATCH(); |
| 2777 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2778 | err = PyObject_IsTrue(cond); |
| 2779 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2780 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | JUMPTO(oparg); |
| 2782 | } |
| 2783 | else if (err == 0) |
| 2784 | ; |
| 2785 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2786 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2788 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2789 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2790 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2791 | PyObject *cond = TOP(); |
| 2792 | int err; |
| 2793 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2794 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2795 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | FAST_DISPATCH(); |
| 2797 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2798 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | JUMPTO(oparg); |
| 2800 | FAST_DISPATCH(); |
| 2801 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2802 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2804 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2805 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | } |
| 2807 | else if (err == 0) |
| 2808 | JUMPTO(oparg); |
| 2809 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2810 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2812 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2813 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2814 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2815 | PyObject *cond = TOP(); |
| 2816 | int err; |
| 2817 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2818 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2819 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | FAST_DISPATCH(); |
| 2821 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2822 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | JUMPTO(oparg); |
| 2824 | FAST_DISPATCH(); |
| 2825 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2826 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | JUMPTO(oparg); |
| 2829 | } |
| 2830 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2831 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2832 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | } |
| 2834 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2835 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2837 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2838 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2839 | case TARGET(JUMP_ABSOLUTE): { |
| 2840 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2842 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2844 | the per-loop checks for signals. By default, this should be turned-off |
| 2845 | because it prevents detection of a control-break in tight loops like |
| 2846 | "while 1: pass". Compile with this option turned-on when you need |
| 2847 | the speed-up and do not need break checking inside tight loops (ones |
| 2848 | that contain only instructions ending with FAST_DISPATCH). |
| 2849 | */ |
| 2850 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2851 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2853 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2854 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2855 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2856 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2858 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2859 | PyObject *iter = PyObject_GetIter(iterable); |
| 2860 | Py_DECREF(iterable); |
| 2861 | SET_TOP(iter); |
| 2862 | if (iter == NULL) |
| 2863 | goto error; |
| 2864 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2865 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2866 | DISPATCH(); |
| 2867 | } |
| 2868 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2869 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2870 | /* before: [obj]; after [getiter(obj)] */ |
| 2871 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2872 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2873 | if (PyCoro_CheckExact(iterable)) { |
| 2874 | /* `iterable` is a coroutine */ |
| 2875 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 2876 | /* and it is used in a 'yield from' expression of a |
| 2877 | regular generator. */ |
| 2878 | Py_DECREF(iterable); |
| 2879 | SET_TOP(NULL); |
| 2880 | PyErr_SetString(PyExc_TypeError, |
| 2881 | "cannot 'yield from' a coroutine object " |
| 2882 | "in a non-coroutine generator"); |
| 2883 | goto error; |
| 2884 | } |
| 2885 | } |
| 2886 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2887 | /* `iterable` is not a generator. */ |
| 2888 | iter = PyObject_GetIter(iterable); |
| 2889 | Py_DECREF(iterable); |
| 2890 | SET_TOP(iter); |
| 2891 | if (iter == NULL) |
| 2892 | goto error; |
| 2893 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2894 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2895 | DISPATCH(); |
| 2896 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2897 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2898 | case TARGET(FOR_ITER): { |
| 2899 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2901 | PyObject *iter = TOP(); |
| 2902 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 2903 | if (next != NULL) { |
| 2904 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 | PREDICT(STORE_FAST); |
| 2906 | PREDICT(UNPACK_SEQUENCE); |
| 2907 | DISPATCH(); |
| 2908 | } |
| 2909 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2910 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2911 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2912 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2913 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | PyErr_Clear(); |
| 2915 | } |
| 2916 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2917 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2918 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2919 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2920 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2922 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2923 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2924 | case TARGET(SETUP_FINALLY): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | /* NOTE: If you add any new block-setup opcodes that |
| 2926 | are not try/except/finally handlers, you may need |
| 2927 | to update the PyGen_NeedsFinalizing() function. |
| 2928 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2929 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2930 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2931 | STACK_LEVEL()); |
| 2932 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2933 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2934 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2935 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2936 | _Py_IDENTIFIER(__aexit__); |
| 2937 | _Py_IDENTIFIER(__aenter__); |
| 2938 | |
| 2939 | PyObject *mgr = TOP(); |
| 2940 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 2941 | *enter; |
| 2942 | PyObject *res; |
| 2943 | if (exit == NULL) |
| 2944 | goto error; |
| 2945 | SET_TOP(exit); |
| 2946 | enter = special_lookup(mgr, &PyId___aenter__); |
| 2947 | Py_DECREF(mgr); |
| 2948 | if (enter == NULL) |
| 2949 | goto error; |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 2950 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2951 | Py_DECREF(enter); |
| 2952 | if (res == NULL) |
| 2953 | goto error; |
| 2954 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2955 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2956 | DISPATCH(); |
| 2957 | } |
| 2958 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2959 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2960 | PyObject *res = POP(); |
| 2961 | /* Setup the finally block before pushing the result |
| 2962 | of __aenter__ on the stack. */ |
| 2963 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 2964 | STACK_LEVEL()); |
| 2965 | PUSH(res); |
| 2966 | DISPATCH(); |
| 2967 | } |
| 2968 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2969 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 2970 | _Py_IDENTIFIER(__exit__); |
| 2971 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2972 | PyObject *mgr = TOP(); |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 2973 | PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2974 | PyObject *res; |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 2975 | if (enter == NULL) |
| 2976 | goto error; |
| 2977 | exit = special_lookup(mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 2978 | if (exit == NULL) { |
| 2979 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2980 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 2981 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2982 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2983 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 2984 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2985 | Py_DECREF(enter); |
| 2986 | if (res == NULL) |
| 2987 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | /* Setup the finally block before pushing the result |
| 2989 | of __enter__ on the stack. */ |
| 2990 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 2991 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 2992 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2993 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | DISPATCH(); |
| 2995 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 2996 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2997 | case TARGET(WITH_CLEANUP_START): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2998 | /* At the top of the stack are 1 or 6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | how/why we entered the finally clause: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3000 | - TOP = NULL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | - (TOP, SECOND, THIRD) = exc_info() |
| 3002 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3003 | Below them is EXIT, the context.__exit__ or context.__aexit__ |
| 3004 | bound method. |
| 3005 | In the first case, we must call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3006 | EXIT(None, None, None) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3007 | otherwise we must call |
| 3008 | EXIT(TOP, SECOND, THIRD) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3009 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3010 | In the first case, we remove EXIT from the |
| 3011 | stack, leaving TOP, and push TOP on the stack. |
| 3012 | Otherwise we shift the bottom 3 values of the |
| 3013 | stack down, replace the empty spot with NULL, and push |
| 3014 | None on the stack. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3015 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3016 | Finally we push the result of the call. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3018 | PyObject *stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3020 | PyObject *exc, *val, *tb, *res; |
| 3021 | |
| 3022 | val = tb = Py_None; |
| 3023 | exc = TOP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3024 | if (exc == NULL) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3025 | STACK_SHRINK(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3027 | SET_TOP(exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3028 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | } |
| 3030 | else { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3031 | assert(PyExceptionClass_Check(exc)); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3032 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3034 | val = SECOND(); |
| 3035 | tb = THIRD(); |
| 3036 | tp2 = FOURTH(); |
| 3037 | exc2 = PEEK(5); |
| 3038 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3040 | SET_VALUE(7, tb2); |
| 3041 | SET_VALUE(6, exc2); |
| 3042 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3044 | SET_FOURTH(NULL); |
| 3045 | /* We just shifted the stack down, so we have |
| 3046 | to tell the except handler block that the |
| 3047 | values are lower than it expects. */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3048 | assert(f->f_iblock > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3050 | assert(block->b_type == EXCEPT_HANDLER); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3051 | assert(block->b_level > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | block->b_level--; |
| 3053 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3054 | |
| 3055 | stack[0] = exc; |
| 3056 | stack[1] = val; |
| 3057 | stack[2] = tb; |
| 3058 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3060 | if (res == NULL) |
| 3061 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3062 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3063 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3064 | PUSH(exc); |
| 3065 | PUSH(res); |
| 3066 | PREDICT(WITH_CLEANUP_FINISH); |
| 3067 | DISPATCH(); |
| 3068 | } |
| 3069 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3070 | case TARGET(WITH_CLEANUP_FINISH): { |
| 3071 | PREDICTED(WITH_CLEANUP_FINISH); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3072 | /* TOP = the result of calling the context.__exit__ bound method |
| 3073 | SECOND = either None or exception type |
| 3074 | |
| 3075 | If SECOND is None below is NULL or the return address, |
| 3076 | otherwise below are 7 values representing an exception. |
| 3077 | */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3078 | PyObject *res = POP(); |
| 3079 | PyObject *exc = POP(); |
| 3080 | int err; |
| 3081 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3082 | if (exc != Py_None) |
| 3083 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | else |
| 3085 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3086 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3087 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3088 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3091 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | else if (err > 0) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3093 | /* There was an exception and a True return. |
| 3094 | * We must manually unwind the EXCEPT_HANDLER block |
| 3095 | * which was created when the exception was caught, |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 3096 | * otherwise the stack will be in an inconsistent state. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3097 | */ |
| 3098 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 3099 | assert(b->b_type == EXCEPT_HANDLER); |
| 3100 | UNWIND_EXCEPT_HANDLER(b); |
| 3101 | PUSH(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3102 | } |
| 3103 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3104 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3106 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3107 | case TARGET(LOAD_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3108 | /* Designed to work in tamdem with CALL_METHOD. */ |
| 3109 | PyObject *name = GETITEM(names, oparg); |
| 3110 | PyObject *obj = TOP(); |
| 3111 | PyObject *meth = NULL; |
| 3112 | |
| 3113 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3114 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3115 | if (meth == NULL) { |
| 3116 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3117 | goto error; |
| 3118 | } |
| 3119 | |
| 3120 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3121 | /* We can bypass temporary bound method object. |
| 3122 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3123 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3124 | meth | self | arg1 | ... | argN |
| 3125 | */ |
| 3126 | SET_TOP(meth); |
| 3127 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3128 | } |
| 3129 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3130 | /* meth is not an unbound method (but a regular attr, or |
| 3131 | something was returned by a descriptor protocol). Set |
| 3132 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3133 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3134 | |
| 3135 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3136 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3137 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3138 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3139 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3140 | } |
| 3141 | DISPATCH(); |
| 3142 | } |
| 3143 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3144 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3145 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3146 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3147 | |
| 3148 | sp = stack_pointer; |
| 3149 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3150 | meth = PEEK(oparg + 2); |
| 3151 | if (meth == NULL) { |
| 3152 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3153 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3154 | |
| 3155 | Stack layout: |
| 3156 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3157 | ... | NULL | callable | arg1 | ... | argN |
| 3158 | ^- TOP() |
| 3159 | ^- (-oparg) |
| 3160 | ^- (-oparg-1) |
| 3161 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3162 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3163 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3164 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3165 | */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3166 | res = call_function(&sp, oparg, NULL); |
| 3167 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3168 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3169 | } |
| 3170 | else { |
| 3171 | /* This is a method call. Stack layout: |
| 3172 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3173 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3174 | ^- TOP() |
| 3175 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3176 | ^- (-oparg-1) |
| 3177 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3178 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3179 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3180 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3181 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3182 | */ |
| 3183 | res = call_function(&sp, oparg + 1, NULL); |
| 3184 | stack_pointer = sp; |
| 3185 | } |
| 3186 | |
| 3187 | PUSH(res); |
| 3188 | if (res == NULL) |
| 3189 | goto error; |
| 3190 | DISPATCH(); |
| 3191 | } |
| 3192 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3193 | case TARGET(CALL_FUNCTION): { |
| 3194 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3195 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3196 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3197 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3199 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3200 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3201 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3202 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3203 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3205 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3206 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3207 | PyObject **sp, *res, *names; |
| 3208 | |
| 3209 | names = POP(); |
| 3210 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3212 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3214 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3215 | Py_DECREF(names); |
| 3216 | |
| 3217 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3218 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3219 | } |
| 3220 | DISPATCH(); |
| 3221 | } |
| 3222 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3223 | case TARGET(CALL_FUNCTION_EX): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3224 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3225 | if (oparg & 0x01) { |
| 3226 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3227 | if (!PyDict_CheckExact(kwargs)) { |
| 3228 | PyObject *d = PyDict_New(); |
| 3229 | if (d == NULL) |
| 3230 | goto error; |
| 3231 | if (PyDict_Update(d, kwargs) != 0) { |
| 3232 | Py_DECREF(d); |
| 3233 | /* PyDict_Update raises attribute |
| 3234 | * error (percolated from an attempt |
| 3235 | * to get 'keys' attribute) instead of |
| 3236 | * a type error if its second argument |
| 3237 | * is not a mapping. |
| 3238 | */ |
| 3239 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 3240 | format_kwargs_mapping_error(SECOND(), kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3241 | } |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3242 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3243 | goto error; |
| 3244 | } |
| 3245 | Py_DECREF(kwargs); |
| 3246 | kwargs = d; |
| 3247 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3248 | assert(PyDict_CheckExact(kwargs)); |
| 3249 | } |
| 3250 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3251 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3252 | if (!PyTuple_CheckExact(callargs)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 3253 | if (check_args_iterable(func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3254 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3255 | goto error; |
| 3256 | } |
| 3257 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3258 | if (callargs == NULL) { |
| 3259 | goto error; |
| 3260 | } |
| 3261 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3262 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3263 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3264 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3265 | Py_DECREF(func); |
| 3266 | Py_DECREF(callargs); |
| 3267 | Py_XDECREF(kwargs); |
| 3268 | |
| 3269 | SET_TOP(result); |
| 3270 | if (result == NULL) { |
| 3271 | goto error; |
| 3272 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3273 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3275 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3276 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3277 | PyObject *qualname = POP(); |
| 3278 | PyObject *codeobj = POP(); |
| 3279 | PyFunctionObject *func = (PyFunctionObject *) |
| 3280 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3281 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3282 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3283 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3284 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3285 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3286 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3287 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3288 | if (oparg & 0x08) { |
| 3289 | assert(PyTuple_CheckExact(TOP())); |
| 3290 | func ->func_closure = POP(); |
| 3291 | } |
| 3292 | if (oparg & 0x04) { |
| 3293 | assert(PyDict_CheckExact(TOP())); |
| 3294 | func->func_annotations = POP(); |
| 3295 | } |
| 3296 | if (oparg & 0x02) { |
| 3297 | assert(PyDict_CheckExact(TOP())); |
| 3298 | func->func_kwdefaults = POP(); |
| 3299 | } |
| 3300 | if (oparg & 0x01) { |
| 3301 | assert(PyTuple_CheckExact(TOP())); |
| 3302 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3304 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3305 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3306 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3308 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3309 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3310 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3312 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3313 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3314 | step = NULL; |
| 3315 | stop = POP(); |
| 3316 | start = TOP(); |
| 3317 | slice = PySlice_New(start, stop, step); |
| 3318 | Py_DECREF(start); |
| 3319 | Py_DECREF(stop); |
| 3320 | Py_XDECREF(step); |
| 3321 | SET_TOP(slice); |
| 3322 | if (slice == NULL) |
| 3323 | goto error; |
| 3324 | DISPATCH(); |
| 3325 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3326 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3327 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3328 | /* Handles f-string value formatting. */ |
| 3329 | PyObject *result; |
| 3330 | PyObject *fmt_spec; |
| 3331 | PyObject *value; |
| 3332 | PyObject *(*conv_fn)(PyObject *); |
| 3333 | int which_conversion = oparg & FVC_MASK; |
| 3334 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3335 | |
| 3336 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3337 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3338 | |
| 3339 | /* See if any conversion is specified. */ |
| 3340 | switch (which_conversion) { |
| 3341 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3342 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3343 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3344 | |
| 3345 | /* Must be 0 (meaning no conversion), since only four |
| 3346 | values are allowed by (oparg & FVC_MASK). */ |
| 3347 | default: conv_fn = NULL; break; |
| 3348 | } |
| 3349 | |
| 3350 | /* If there's a conversion function, call it and replace |
| 3351 | value with that result. Otherwise, just use value, |
| 3352 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3353 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3354 | result = conv_fn(value); |
| 3355 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3356 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3357 | Py_XDECREF(fmt_spec); |
| 3358 | goto error; |
| 3359 | } |
| 3360 | value = result; |
| 3361 | } |
| 3362 | |
| 3363 | /* If value is a unicode object, and there's no fmt_spec, |
| 3364 | then we know the result of format(value) is value |
| 3365 | itself. In that case, skip calling format(). I plan to |
| 3366 | move this optimization in to PyObject_Format() |
| 3367 | itself. */ |
| 3368 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3369 | /* Do nothing, just transfer ownership to result. */ |
| 3370 | result = value; |
| 3371 | } else { |
| 3372 | /* Actually call format(). */ |
| 3373 | result = PyObject_Format(value, fmt_spec); |
| 3374 | Py_DECREF(value); |
| 3375 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3376 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3377 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3378 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3379 | } |
| 3380 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3381 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3382 | DISPATCH(); |
| 3383 | } |
| 3384 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3385 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3386 | int oldoparg = oparg; |
| 3387 | NEXTOPARG(); |
| 3388 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3390 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3391 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3392 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3393 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3394 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3395 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | default: |
| 3397 | fprintf(stderr, |
| 3398 | "XXX lineno: %d, opcode: %d\n", |
| 3399 | PyFrame_GetLineNumber(f), |
| 3400 | opcode); |
| 3401 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3402 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3405 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3406 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3407 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3408 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3409 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3410 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3411 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3412 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3413 | if (!PyErr_Occurred()) |
| 3414 | PyErr_SetString(PyExc_SystemError, |
| 3415 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3416 | #else |
| 3417 | assert(PyErr_Occurred()); |
| 3418 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3419 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3420 | /* Log traceback info. */ |
| 3421 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3422 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3423 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3424 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3425 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3426 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3427 | exception_unwind: |
| 3428 | /* Unwind stacks if an exception occurred */ |
| 3429 | while (f->f_iblock > 0) { |
| 3430 | /* Pop the current block. */ |
| 3431 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | if (b->b_type == EXCEPT_HANDLER) { |
| 3434 | UNWIND_EXCEPT_HANDLER(b); |
| 3435 | continue; |
| 3436 | } |
| 3437 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3438 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | PyObject *exc, *val, *tb; |
| 3440 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3441 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | /* Beware, this invalidates all b->b_* fields */ |
| 3443 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3444 | PUSH(exc_info->exc_traceback); |
| 3445 | PUSH(exc_info->exc_value); |
| 3446 | if (exc_info->exc_type != NULL) { |
| 3447 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | } |
| 3449 | else { |
| 3450 | Py_INCREF(Py_None); |
| 3451 | PUSH(Py_None); |
| 3452 | } |
| 3453 | PyErr_Fetch(&exc, &val, &tb); |
| 3454 | /* Make the raw exception data |
| 3455 | available to the handler, |
| 3456 | so a program can emulate the |
| 3457 | Python main loop. */ |
| 3458 | PyErr_NormalizeException( |
| 3459 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3460 | if (tb != NULL) |
| 3461 | PyException_SetTraceback(val, tb); |
| 3462 | else |
| 3463 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3465 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3467 | exc_info->exc_value = val; |
| 3468 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | if (tb == NULL) |
| 3470 | tb = Py_None; |
| 3471 | Py_INCREF(tb); |
| 3472 | PUSH(tb); |
| 3473 | PUSH(val); |
| 3474 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3475 | JUMPTO(handler); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3476 | /* Resume normal execution */ |
| 3477 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3478 | } |
| 3479 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3480 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3481 | /* End the loop as we still have an error */ |
| 3482 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | /* Pop remaining stack entries. */ |
| 3486 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3487 | PyObject *o = POP(); |
| 3488 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3490 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3491 | assert(retval == NULL); |
| 3492 | assert(PyErr_Occurred()); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3493 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3494 | return_or_yield: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3496 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3497 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3498 | tstate, f, PyTrace_RETURN, retval)) { |
| 3499 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | } |
| 3501 | } |
| 3502 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3503 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 3504 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3505 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3511 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3512 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3513 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3515 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3517 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3518 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3521 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3522 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3523 | { |
| 3524 | int err; |
| 3525 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3526 | PyObject *name_str, *comma, *tail, *tmp; |
| 3527 | |
| 3528 | assert(PyList_CheckExact(names)); |
| 3529 | assert(len >= 1); |
| 3530 | /* Deal with the joys of natural language. */ |
| 3531 | switch (len) { |
| 3532 | case 1: |
| 3533 | name_str = PyList_GET_ITEM(names, 0); |
| 3534 | Py_INCREF(name_str); |
| 3535 | break; |
| 3536 | case 2: |
| 3537 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3538 | PyList_GET_ITEM(names, len - 2), |
| 3539 | PyList_GET_ITEM(names, len - 1)); |
| 3540 | break; |
| 3541 | default: |
| 3542 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3543 | PyList_GET_ITEM(names, len - 2), |
| 3544 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3545 | if (tail == NULL) |
| 3546 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3547 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3548 | fail, but we can't be too careful. */ |
| 3549 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3550 | if (err == -1) { |
| 3551 | Py_DECREF(tail); |
| 3552 | return; |
| 3553 | } |
| 3554 | /* Stitch everything up into a nice comma-separated list. */ |
| 3555 | comma = PyUnicode_FromString(", "); |
| 3556 | if (comma == NULL) { |
| 3557 | Py_DECREF(tail); |
| 3558 | return; |
| 3559 | } |
| 3560 | tmp = PyUnicode_Join(comma, names); |
| 3561 | Py_DECREF(comma); |
| 3562 | if (tmp == NULL) { |
| 3563 | Py_DECREF(tail); |
| 3564 | return; |
| 3565 | } |
| 3566 | name_str = PyUnicode_Concat(tmp, tail); |
| 3567 | Py_DECREF(tmp); |
| 3568 | Py_DECREF(tail); |
| 3569 | break; |
| 3570 | } |
| 3571 | if (name_str == NULL) |
| 3572 | return; |
| 3573 | PyErr_Format(PyExc_TypeError, |
| 3574 | "%U() missing %i required %s argument%s: %U", |
| 3575 | co->co_name, |
| 3576 | len, |
| 3577 | kind, |
| 3578 | len == 1 ? "" : "s", |
| 3579 | name_str); |
| 3580 | Py_DECREF(name_str); |
| 3581 | } |
| 3582 | |
| 3583 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3584 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3585 | PyObject **fastlocals) |
| 3586 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3587 | Py_ssize_t i, j = 0; |
| 3588 | Py_ssize_t start, end; |
| 3589 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3590 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3591 | PyObject *missing_names; |
| 3592 | |
| 3593 | /* Compute the names of the arguments that are missing. */ |
| 3594 | missing_names = PyList_New(missing); |
| 3595 | if (missing_names == NULL) |
| 3596 | return; |
| 3597 | if (positional) { |
| 3598 | start = 0; |
| 3599 | end = co->co_argcount - defcount; |
| 3600 | } |
| 3601 | else { |
| 3602 | start = co->co_argcount; |
| 3603 | end = start + co->co_kwonlyargcount; |
| 3604 | } |
| 3605 | for (i = start; i < end; i++) { |
| 3606 | if (GETLOCAL(i) == NULL) { |
| 3607 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3608 | PyObject *name = PyObject_Repr(raw); |
| 3609 | if (name == NULL) { |
| 3610 | Py_DECREF(missing_names); |
| 3611 | return; |
| 3612 | } |
| 3613 | PyList_SET_ITEM(missing_names, j++, name); |
| 3614 | } |
| 3615 | } |
| 3616 | assert(j == missing); |
| 3617 | format_missing(kind, co, missing_names); |
| 3618 | Py_DECREF(missing_names); |
| 3619 | } |
| 3620 | |
| 3621 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3622 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3623 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3624 | { |
| 3625 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3626 | Py_ssize_t kwonly_given = 0; |
| 3627 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3628 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3629 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3630 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3631 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3632 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3633 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3634 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3635 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3636 | } |
| 3637 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3638 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3639 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3640 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3641 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3642 | } |
| 3643 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3644 | plural = (co_argcount != 1); |
| 3645 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3646 | } |
| 3647 | if (sig == NULL) |
| 3648 | return; |
| 3649 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3650 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3651 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3652 | given != 1 ? "s" : "", |
| 3653 | kwonly_given, |
| 3654 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3655 | if (kwonly_sig == NULL) { |
| 3656 | Py_DECREF(sig); |
| 3657 | return; |
| 3658 | } |
| 3659 | } |
| 3660 | else { |
| 3661 | /* This will not fail. */ |
| 3662 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3663 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3664 | } |
| 3665 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3666 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3667 | co->co_name, |
| 3668 | sig, |
| 3669 | plural ? "s" : "", |
| 3670 | given, |
| 3671 | kwonly_sig, |
| 3672 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3673 | Py_DECREF(sig); |
| 3674 | Py_DECREF(kwonly_sig); |
| 3675 | } |
| 3676 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3677 | /* 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] | 3678 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3679 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3680 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 3681 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3682 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3683 | PyObject *const *args, Py_ssize_t argcount, |
| 3684 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3685 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3686 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3687 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3688 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3689 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3690 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3691 | PyFrameObject *f; |
| 3692 | PyObject *retval = NULL; |
| 3693 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3694 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3695 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3696 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3697 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3698 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3700 | if (globals == NULL) { |
| 3701 | PyErr_SetString(PyExc_SystemError, |
| 3702 | "PyEval_EvalCodeEx: NULL globals"); |
| 3703 | return NULL; |
| 3704 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3705 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3706 | /* Create the frame */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 3707 | tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3708 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 3709 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3710 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3711 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3712 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3713 | fastlocals = f->f_localsplus; |
| 3714 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3715 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3716 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3717 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3718 | kwdict = PyDict_New(); |
| 3719 | if (kwdict == NULL) |
| 3720 | goto fail; |
| 3721 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3722 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3723 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3724 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3725 | SETLOCAL(i, kwdict); |
| 3726 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3727 | else { |
| 3728 | kwdict = NULL; |
| 3729 | } |
| 3730 | |
| 3731 | /* Copy positional arguments into local variables */ |
| 3732 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3733 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3734 | } |
| 3735 | else { |
| 3736 | n = argcount; |
| 3737 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3738 | for (i = 0; i < n; i++) { |
| 3739 | x = args[i]; |
| 3740 | Py_INCREF(x); |
| 3741 | SETLOCAL(i, x); |
| 3742 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3743 | |
| 3744 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3745 | if (co->co_flags & CO_VARARGS) { |
| 3746 | u = PyTuple_New(argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3747 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3748 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3749 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3750 | SETLOCAL(total_args, u); |
| 3751 | for (i = n; i < argcount; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | x = args[i]; |
| 3753 | Py_INCREF(x); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3754 | PyTuple_SET_ITEM(u, i-n, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3756 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3757 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3758 | /* Handle keyword arguments passed as two strided arrays */ |
| 3759 | kwcount *= kwstep; |
| 3760 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3761 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3762 | PyObject *keyword = kwnames[i]; |
| 3763 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3764 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3765 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3766 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3767 | PyErr_Format(PyExc_TypeError, |
| 3768 | "%U() keywords must be strings", |
| 3769 | co->co_name); |
| 3770 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3772 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3773 | /* Speed hack: do raw pointer compares. As names are |
| 3774 | normally interned this should almost always hit. */ |
| 3775 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3776 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3777 | PyObject *name = co_varnames[j]; |
| 3778 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3779 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3780 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3781 | } |
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 | /* Slow fallback, just in case */ |
| 3784 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3785 | PyObject *name = co_varnames[j]; |
| 3786 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 3787 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3788 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3789 | } |
| 3790 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3792 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3793 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3794 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 3795 | assert(j >= total_args); |
| 3796 | if (kwdict == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3797 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3798 | "%U() got an unexpected keyword argument '%S'", |
| 3799 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3800 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3801 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3802 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 3803 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3804 | goto fail; |
| 3805 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3806 | continue; |
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 | kw_found: |
| 3809 | if (GETLOCAL(j) != NULL) { |
| 3810 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3811 | "%U() got multiple values for argument '%S'", |
| 3812 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3813 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3814 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3815 | Py_INCREF(value); |
| 3816 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3818 | |
| 3819 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3820 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3821 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3822 | goto fail; |
| 3823 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3824 | |
| 3825 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3826 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3827 | Py_ssize_t m = co->co_argcount - defcount; |
| 3828 | Py_ssize_t missing = 0; |
| 3829 | for (i = argcount; i < m; i++) { |
| 3830 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3831 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3832 | } |
| 3833 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3834 | if (missing) { |
| 3835 | missing_arguments(co, missing, defcount, fastlocals); |
| 3836 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3837 | } |
| 3838 | if (n > m) |
| 3839 | i = n - m; |
| 3840 | else |
| 3841 | i = 0; |
| 3842 | for (; i < defcount; i++) { |
| 3843 | if (GETLOCAL(m+i) == NULL) { |
| 3844 | PyObject *def = defs[i]; |
| 3845 | Py_INCREF(def); |
| 3846 | SETLOCAL(m+i, def); |
| 3847 | } |
| 3848 | } |
| 3849 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3850 | |
| 3851 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3852 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3853 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3854 | for (i = co->co_argcount; i < total_args; i++) { |
| 3855 | PyObject *name; |
| 3856 | if (GETLOCAL(i) != NULL) |
| 3857 | continue; |
| 3858 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3859 | if (kwdefs != NULL) { |
| 3860 | PyObject *def = PyDict_GetItem(kwdefs, name); |
| 3861 | if (def) { |
| 3862 | Py_INCREF(def); |
| 3863 | SETLOCAL(i, def); |
| 3864 | continue; |
| 3865 | } |
| 3866 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3867 | missing++; |
| 3868 | } |
| 3869 | if (missing) { |
| 3870 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3871 | goto fail; |
| 3872 | } |
| 3873 | } |
| 3874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3876 | vars into frame. */ |
| 3877 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 3879 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3880 | /* Possibly account for the cell variable being an argument. */ |
| 3881 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3882 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3883 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 3884 | /* Clear the local copy. */ |
| 3885 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3886 | } |
| 3887 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3888 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3889 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 3890 | if (c == NULL) |
| 3891 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3892 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3894 | |
| 3895 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3896 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 3897 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3898 | Py_INCREF(o); |
| 3899 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3901 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3902 | /* Handle generator/coroutine/asynchronous generator */ |
| 3903 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3904 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3905 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3906 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3907 | |
| 3908 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 3909 | assert(coro_wrapper != NULL); |
| 3910 | PyErr_Format(PyExc_RuntimeError, |
| 3911 | "coroutine wrapper %.200R attempted " |
| 3912 | "to recursively wrap %.200R", |
| 3913 | coro_wrapper, |
| 3914 | co); |
| 3915 | goto fail; |
| 3916 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | /* Don't need to keep the reference to f_back, it will be set |
| 3919 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3920 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3922 | /* Create a new generator that owns the ready to run frame |
| 3923 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3924 | if (is_coro) { |
| 3925 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3926 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 3927 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3928 | } else { |
| 3929 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 3930 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3931 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3932 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3933 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 3934 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3935 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3936 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3937 | if (is_coro && coro_wrapper != NULL) { |
| 3938 | PyObject *wrapped; |
| 3939 | tstate->in_coroutine_wrapper = 1; |
| 3940 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 3941 | tstate->in_coroutine_wrapper = 0; |
| 3942 | return wrapped; |
| 3943 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 3944 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3945 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3946 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3947 | |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 3948 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3949 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3950 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3953 | which can call back into Python. While we're done with the |
| 3954 | current Python frame (f), the associated C stack is still in use, |
| 3955 | so recursion_depth must be boosted for the duration. |
| 3956 | */ |
| 3957 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 3958 | if (Py_REFCNT(f) > 1) { |
| 3959 | Py_DECREF(f); |
| 3960 | _PyObject_GC_TRACK(f); |
| 3961 | } |
| 3962 | else { |
| 3963 | ++tstate->recursion_depth; |
| 3964 | Py_DECREF(f); |
| 3965 | --tstate->recursion_depth; |
| 3966 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3967 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3970 | PyObject * |
| 3971 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3972 | PyObject *const *args, int argcount, |
| 3973 | PyObject *const *kws, int kwcount, |
| 3974 | PyObject *const *defs, int defcount, |
| 3975 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3976 | { |
| 3977 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 3978 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 3979 | kws, kws != NULL ? kws + 1 : NULL, |
| 3980 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 3981 | defs, defcount, |
| 3982 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3983 | NULL, NULL); |
| 3984 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3985 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3986 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3987 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3990 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3992 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3993 | return NULL; |
| 3994 | } |
| 3995 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 3999 | /* Logic for the raise statement (too complicated for inlining). |
| 4000 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4001 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4002 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4006 | if (exc == NULL) { |
| 4007 | /* Reraise */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4008 | PyThreadState *tstate = _PyThreadState_GET(); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4009 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4011 | type = exc_info->exc_type; |
| 4012 | value = exc_info->exc_value; |
| 4013 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4014 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4015 | PyErr_SetString(PyExc_RuntimeError, |
| 4016 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4017 | return 0; |
| 4018 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | Py_XINCREF(type); |
| 4020 | Py_XINCREF(value); |
| 4021 | Py_XINCREF(tb); |
| 4022 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4023 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4024 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4026 | /* We support the following forms of raise: |
| 4027 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4028 | raise <instance> |
| 4029 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4031 | if (PyExceptionClass_Check(exc)) { |
| 4032 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4033 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4034 | if (value == NULL) |
| 4035 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4036 | if (!PyExceptionInstance_Check(value)) { |
| 4037 | PyErr_Format(PyExc_TypeError, |
| 4038 | "calling %R should have returned an instance of " |
| 4039 | "BaseException, not %R", |
| 4040 | type, Py_TYPE(value)); |
| 4041 | goto raise_error; |
| 4042 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | } |
| 4044 | else if (PyExceptionInstance_Check(exc)) { |
| 4045 | value = exc; |
| 4046 | type = PyExceptionInstance_Class(exc); |
| 4047 | Py_INCREF(type); |
| 4048 | } |
| 4049 | else { |
| 4050 | /* Not something you can raise. You get an exception |
| 4051 | anyway, just not what you specified :-) */ |
| 4052 | Py_DECREF(exc); |
| 4053 | PyErr_SetString(PyExc_TypeError, |
| 4054 | "exceptions must derive from BaseException"); |
| 4055 | goto raise_error; |
| 4056 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4057 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4058 | assert(type != NULL); |
| 4059 | assert(value != NULL); |
| 4060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4061 | if (cause) { |
| 4062 | PyObject *fixed_cause; |
| 4063 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4064 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | if (fixed_cause == NULL) |
| 4066 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4067 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4069 | else if (PyExceptionInstance_Check(cause)) { |
| 4070 | fixed_cause = cause; |
| 4071 | } |
| 4072 | else if (cause == Py_None) { |
| 4073 | Py_DECREF(cause); |
| 4074 | fixed_cause = NULL; |
| 4075 | } |
| 4076 | else { |
| 4077 | PyErr_SetString(PyExc_TypeError, |
| 4078 | "exception causes must derive from " |
| 4079 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4080 | goto raise_error; |
| 4081 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4082 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4083 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4085 | PyErr_SetObject(type, value); |
| 4086 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4087 | Py_DECREF(value); |
| 4088 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4089 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4090 | |
| 4091 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | Py_XDECREF(value); |
| 4093 | Py_XDECREF(type); |
| 4094 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4095 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4098 | /* 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] | 4099 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4100 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4101 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4102 | with a variable target. |
| 4103 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4104 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4105 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4106 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 | int i = 0, j = 0; |
| 4109 | Py_ssize_t ll = 0; |
| 4110 | PyObject *it; /* iter(v) */ |
| 4111 | PyObject *w; |
| 4112 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4114 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4117 | if (it == NULL) { |
| 4118 | if (PyErr_ExceptionMatches(PyExc_TypeError) && |
| 4119 | v->ob_type->tp_iter == NULL && !PySequence_Check(v)) |
| 4120 | { |
| 4121 | PyErr_Format(PyExc_TypeError, |
| 4122 | "cannot unpack non-iterable %.200s object", |
| 4123 | v->ob_type->tp_name); |
| 4124 | } |
| 4125 | return 0; |
| 4126 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | for (; i < argcnt; i++) { |
| 4129 | w = PyIter_Next(it); |
| 4130 | if (w == NULL) { |
| 4131 | /* Iterator done, via error or exhaustion. */ |
| 4132 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4133 | if (argcntafter == -1) { |
| 4134 | PyErr_Format(PyExc_ValueError, |
| 4135 | "not enough values to unpack (expected %d, got %d)", |
| 4136 | argcnt, i); |
| 4137 | } |
| 4138 | else { |
| 4139 | PyErr_Format(PyExc_ValueError, |
| 4140 | "not enough values to unpack " |
| 4141 | "(expected at least %d, got %d)", |
| 4142 | argcnt + argcntafter, i); |
| 4143 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4144 | } |
| 4145 | goto Error; |
| 4146 | } |
| 4147 | *--sp = w; |
| 4148 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | if (argcntafter == -1) { |
| 4151 | /* We better have exhausted the iterator now. */ |
| 4152 | w = PyIter_Next(it); |
| 4153 | if (w == NULL) { |
| 4154 | if (PyErr_Occurred()) |
| 4155 | goto Error; |
| 4156 | Py_DECREF(it); |
| 4157 | return 1; |
| 4158 | } |
| 4159 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4160 | PyErr_Format(PyExc_ValueError, |
| 4161 | "too many values to unpack (expected %d)", |
| 4162 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4163 | goto Error; |
| 4164 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | l = PySequence_List(it); |
| 4167 | if (l == NULL) |
| 4168 | goto Error; |
| 4169 | *--sp = l; |
| 4170 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | ll = PyList_GET_SIZE(l); |
| 4173 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4174 | PyErr_Format(PyExc_ValueError, |
| 4175 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4176 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 | goto Error; |
| 4178 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | /* Pop the "after-variable" args off the list. */ |
| 4181 | for (j = argcntafter; j > 0; j--, i++) { |
| 4182 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4183 | } |
| 4184 | /* Resize the list. */ |
| 4185 | Py_SIZE(l) = ll - argcntafter; |
| 4186 | Py_DECREF(it); |
| 4187 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4188 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4189 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | for (; i > 0; i--, sp++) |
| 4191 | Py_DECREF(*sp); |
| 4192 | Py_XDECREF(it); |
| 4193 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4197 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4198 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4199 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4201 | printf("%s ", str); |
| 4202 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4203 | PyErr_Clear(); /* Don't know what else to do */ |
| 4204 | printf("\n"); |
| 4205 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4206 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4207 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4208 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4209 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4210 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4211 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4212 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4213 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4215 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | if (value == NULL) { |
| 4217 | value = Py_None; |
| 4218 | Py_INCREF(value); |
| 4219 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4220 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4221 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4222 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4223 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4224 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4225 | return; |
| 4226 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4227 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4228 | Py_DECREF(arg); |
| 4229 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4230 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4231 | else { |
| 4232 | Py_XDECREF(type); |
| 4233 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4234 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4238 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4239 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4240 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4241 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | PyObject *type, *value, *traceback; |
| 4244 | int err; |
| 4245 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4246 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | if (err == 0) |
| 4248 | { |
| 4249 | PyErr_Restore(type, value, traceback); |
| 4250 | return 0; |
| 4251 | } |
| 4252 | else { |
| 4253 | Py_XDECREF(type); |
| 4254 | Py_XDECREF(value); |
| 4255 | Py_XDECREF(traceback); |
| 4256 | return -1; |
| 4257 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4260 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4261 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4262 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4265 | int result; |
| 4266 | if (tstate->tracing) |
| 4267 | return 0; |
| 4268 | tstate->tracing++; |
| 4269 | tstate->use_tracing = 0; |
| 4270 | result = func(obj, frame, what, arg); |
| 4271 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4272 | || (tstate->c_profilefunc != NULL)); |
| 4273 | tstate->tracing--; |
| 4274 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4277 | PyObject * |
| 4278 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4279 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4280 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | int save_tracing = tstate->tracing; |
| 4282 | int save_use_tracing = tstate->use_tracing; |
| 4283 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | tstate->tracing = 0; |
| 4286 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4287 | || (tstate->c_profilefunc != NULL)); |
| 4288 | result = PyObject_Call(func, args, NULL); |
| 4289 | tstate->tracing = save_tracing; |
| 4290 | tstate->use_tracing = save_use_tracing; |
| 4291 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4294 | /* 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] | 4295 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4296 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4297 | PyThreadState *tstate, PyFrameObject *frame, |
| 4298 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4300 | int result = 0; |
| 4301 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4303 | /* If the last instruction executed isn't in the current |
| 4304 | instruction window, reset the window. |
| 4305 | */ |
| 4306 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4307 | PyAddrPair bounds; |
| 4308 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4309 | &bounds); |
| 4310 | *instr_lb = bounds.ap_lower; |
| 4311 | *instr_ub = bounds.ap_upper; |
| 4312 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4313 | /* If the last instruction falls at the start of a line or if it |
| 4314 | represents a jump backwards, update the frame's line number and |
| 4315 | then call the trace function if we're tracing source lines. |
| 4316 | */ |
| 4317 | if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4318 | frame->f_lineno = line; |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4319 | if (frame->f_trace_lines) { |
| 4320 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 4321 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 4323 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 4324 | if (frame->f_trace_opcodes) { |
| 4325 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 4326 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | *instr_prev = frame->f_lasti; |
| 4328 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4331 | void |
| 4332 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4333 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4334 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | PyObject *temp = tstate->c_profileobj; |
| 4336 | Py_XINCREF(arg); |
| 4337 | tstate->c_profilefunc = NULL; |
| 4338 | tstate->c_profileobj = NULL; |
| 4339 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4340 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4341 | Py_XDECREF(temp); |
| 4342 | tstate->c_profilefunc = func; |
| 4343 | tstate->c_profileobj = arg; |
| 4344 | /* Flag that tracing or profiling is turned on */ |
| 4345 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | void |
| 4349 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4350 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4351 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | PyObject *temp = tstate->c_traceobj; |
| 4353 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4354 | Py_XINCREF(arg); |
| 4355 | tstate->c_tracefunc = NULL; |
| 4356 | tstate->c_traceobj = NULL; |
| 4357 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4358 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4359 | Py_XDECREF(temp); |
| 4360 | tstate->c_tracefunc = func; |
| 4361 | tstate->c_traceobj = arg; |
| 4362 | /* Flag that tracing or profiling is turned on */ |
| 4363 | tstate->use_tracing = ((func != NULL) |
| 4364 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4367 | void |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4368 | _PyEval_SetCoroutineOriginTrackingDepth(int new_depth) |
| 4369 | { |
| 4370 | assert(new_depth >= 0); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4371 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4372 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 4373 | } |
| 4374 | |
| 4375 | int |
| 4376 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 4377 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4378 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4379 | return tstate->coroutine_origin_tracking_depth; |
| 4380 | } |
| 4381 | |
| 4382 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4383 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4384 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4385 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4386 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4387 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4388 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4392 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4393 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4394 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4395 | return tstate->coroutine_wrapper; |
| 4396 | } |
| 4397 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4398 | void |
| 4399 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4400 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4401 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4402 | |
| 4403 | Py_XINCREF(firstiter); |
| 4404 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4405 | } |
| 4406 | |
| 4407 | PyObject * |
| 4408 | _PyEval_GetAsyncGenFirstiter(void) |
| 4409 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4410 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4411 | return tstate->async_gen_firstiter; |
| 4412 | } |
| 4413 | |
| 4414 | void |
| 4415 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4416 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4417 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4418 | |
| 4419 | Py_XINCREF(finalizer); |
| 4420 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4421 | } |
| 4422 | |
| 4423 | PyObject * |
| 4424 | _PyEval_GetAsyncGenFinalizer(void) |
| 4425 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4426 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4427 | return tstate->async_gen_finalizer; |
| 4428 | } |
| 4429 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4430 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4431 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4434 | if (current_frame == NULL) |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 4435 | return _PyInterpreterState_GET_UNSAFE()->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | else |
| 4437 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame^] | 4440 | /* Convenience function to get a builtin from its name */ |
| 4441 | PyObject * |
| 4442 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 4443 | { |
| 4444 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 4445 | if (attr) { |
| 4446 | Py_INCREF(attr); |
| 4447 | } |
| 4448 | else if (!PyErr_Occurred()) { |
| 4449 | PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name)); |
| 4450 | } |
| 4451 | return attr; |
| 4452 | } |
| 4453 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4454 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4455 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4458 | if (current_frame == NULL) { |
| 4459 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4464 | return NULL; |
| 4465 | |
| 4466 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4467 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4470 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4471 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4474 | if (current_frame == NULL) |
| 4475 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4476 | |
| 4477 | assert(current_frame->f_globals != NULL); |
| 4478 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4481 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4482 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4483 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4484 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4488 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4489 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4492 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | if (current_frame != NULL) { |
| 4495 | const int codeflags = current_frame->f_code->co_flags; |
| 4496 | const int compilerflags = codeflags & PyCF_MASK; |
| 4497 | if (compilerflags) { |
| 4498 | result = 1; |
| 4499 | cf->cf_flags |= compilerflags; |
| 4500 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4501 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4502 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4503 | result = 1; |
| 4504 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4505 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4506 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | } |
| 4508 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4511 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4512 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4513 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4514 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4515 | if (PyMethod_Check(func)) |
| 4516 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4517 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4518 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4519 | else if (PyCFunction_Check(func)) |
| 4520 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4521 | else |
| 4522 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4525 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4526 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4527 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | if (PyMethod_Check(func)) |
| 4529 | return "()"; |
| 4530 | else if (PyFunction_Check(func)) |
| 4531 | return "()"; |
| 4532 | else if (PyCFunction_Check(func)) |
| 4533 | return "()"; |
| 4534 | else |
| 4535 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4538 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4539 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4540 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4541 | tstate, tstate->frame, \ |
| 4542 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | x = NULL; \ |
| 4544 | } \ |
| 4545 | else { \ |
| 4546 | x = call; \ |
| 4547 | if (tstate->c_profilefunc != NULL) { \ |
| 4548 | if (x == NULL) { \ |
| 4549 | call_trace_protected(tstate->c_profilefunc, \ |
| 4550 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4551 | tstate, tstate->frame, \ |
| 4552 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | /* XXX should pass (type, value, tb) */ \ |
| 4554 | } else { \ |
| 4555 | if (call_trace(tstate->c_profilefunc, \ |
| 4556 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4557 | tstate, tstate->frame, \ |
| 4558 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | Py_DECREF(x); \ |
| 4560 | x = NULL; \ |
| 4561 | } \ |
| 4562 | } \ |
| 4563 | } \ |
| 4564 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4565 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | x = call; \ |
| 4567 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4568 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 4569 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 4570 | to reduce the stack consumption. */ |
| 4571 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4572 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4573 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4574 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | PyObject *func = *pfunc; |
| 4576 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4577 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4578 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4579 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | /* Always dispatch PyCFunction first, because these are |
| 4582 | presumed to be the most frequent callable object. |
| 4583 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4584 | if (PyCFunction_Check(func)) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4585 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4586 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4587 | } |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4588 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4589 | PyThreadState *tstate = _PyThreadState_GET(); |
jdemeyer | 56868f9 | 2018-07-21 10:30:59 +0200 | [diff] [blame] | 4590 | if (nargs > 0 && tstate->use_tracing) { |
| 4591 | /* We need to create a temporary bound method as argument |
| 4592 | for profiling. |
| 4593 | |
| 4594 | If nargs == 0, then this cannot work because we have no |
| 4595 | "self". In any case, the call itself would raise |
| 4596 | TypeError (foo needs an argument), so we just skip |
| 4597 | profiling. */ |
| 4598 | PyObject *self = stack[0]; |
| 4599 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
jdemeyer | 147d955 | 2018-07-23 18:41:20 +0200 | [diff] [blame] | 4600 | if (func != NULL) { |
| 4601 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, |
| 4602 | stack+1, nargs-1, |
| 4603 | kwnames)); |
| 4604 | Py_DECREF(func); |
INADA Naoki | 93fac8d | 2017-03-07 14:24:37 +0900 | [diff] [blame] | 4605 | } |
jdemeyer | 147d955 | 2018-07-23 18:41:20 +0200 | [diff] [blame] | 4606 | else { |
| 4607 | x = NULL; |
| 4608 | } |
INADA Naoki | 93fac8d | 2017-03-07 14:24:37 +0900 | [diff] [blame] | 4609 | } |
| 4610 | else { |
| 4611 | x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames); |
| 4612 | } |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4613 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4614 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4615 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
Victor Stinner | b69ee8c | 2016-11-28 18:32:31 +0100 | [diff] [blame] | 4616 | /* Optimize access to bound methods. Reuse the Python stack |
| 4617 | to pass 'self' as the first argument, replace 'func' |
| 4618 | with 'self'. It avoids the creation of a new temporary tuple |
| 4619 | for arguments (to replace func with self) when the method uses |
| 4620 | FASTCALL. */ |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4621 | PyObject *self = PyMethod_GET_SELF(func); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4622 | Py_INCREF(self); |
| 4623 | func = PyMethod_GET_FUNCTION(func); |
| 4624 | Py_INCREF(func); |
| 4625 | Py_SETREF(*pfunc, self); |
| 4626 | nargs++; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4627 | stack--; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4628 | } |
| 4629 | else { |
| 4630 | Py_INCREF(func); |
| 4631 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4632 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4633 | if (PyFunction_Check(func)) { |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4634 | x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4635 | } |
| 4636 | else { |
| 4637 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4638 | } |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4639 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4641 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4642 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4643 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4644 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | while ((*pp_stack) > pfunc) { |
| 4646 | w = EXT_POP(*pp_stack); |
| 4647 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4653 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4654 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4655 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4656 | PyObject *result; |
| 4657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4658 | if (PyCFunction_Check(func)) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4659 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4661 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4662 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4663 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4664 | PyThreadState *tstate = _PyThreadState_GET(); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4665 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 4666 | if (nargs > 0 && tstate->use_tracing) { |
| 4667 | /* We need to create a temporary bound method as argument |
| 4668 | for profiling. |
| 4669 | |
| 4670 | If nargs == 0, then this cannot work because we have no |
| 4671 | "self". In any case, the call itself would raise |
| 4672 | TypeError (foo needs an argument), so we just skip |
| 4673 | profiling. */ |
| 4674 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 4675 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 4676 | if (func == NULL) { |
| 4677 | return NULL; |
| 4678 | } |
| 4679 | |
| 4680 | C_TRACE(result, _PyCFunction_FastCallDict(func, |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4681 | &_PyTuple_ITEMS(callargs)[1], |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4682 | nargs - 1, |
| 4683 | kwdict)); |
| 4684 | Py_DECREF(func); |
| 4685 | return result; |
| 4686 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4687 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4688 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 4691 | /* 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] | 4692 | nb_index slot defined, and store in *pi. |
| 4693 | 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] | 4694 | 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] | 4695 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4696 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4697 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4698 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4699 | { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4700 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4701 | Py_ssize_t x; |
| 4702 | if (PyIndex_Check(v)) { |
| 4703 | x = PyNumber_AsSsize_t(v, NULL); |
| 4704 | if (x == -1 && PyErr_Occurred()) |
| 4705 | return 0; |
| 4706 | } |
| 4707 | else { |
| 4708 | PyErr_SetString(PyExc_TypeError, |
| 4709 | "slice indices must be integers or " |
| 4710 | "None or have an __index__ method"); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | *pi = x; |
| 4714 | } |
| 4715 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4718 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4719 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4720 | { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4721 | Py_ssize_t x; |
| 4722 | if (PyIndex_Check(v)) { |
| 4723 | x = PyNumber_AsSsize_t(v, NULL); |
| 4724 | if (x == -1 && PyErr_Occurred()) |
| 4725 | return 0; |
| 4726 | } |
| 4727 | else { |
| 4728 | PyErr_SetString(PyExc_TypeError, |
| 4729 | "slice indices must be integers or " |
| 4730 | "have an __index__ method"); |
| 4731 | return 0; |
| 4732 | } |
| 4733 | *pi = x; |
| 4734 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 4738 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 4740 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4741 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4742 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4743 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4744 | int res = 0; |
| 4745 | switch (op) { |
| 4746 | case PyCmp_IS: |
| 4747 | res = (v == w); |
| 4748 | break; |
| 4749 | case PyCmp_IS_NOT: |
| 4750 | res = (v != w); |
| 4751 | break; |
| 4752 | case PyCmp_IN: |
| 4753 | res = PySequence_Contains(w, v); |
| 4754 | if (res < 0) |
| 4755 | return NULL; |
| 4756 | break; |
| 4757 | case PyCmp_NOT_IN: |
| 4758 | res = PySequence_Contains(w, v); |
| 4759 | if (res < 0) |
| 4760 | return NULL; |
| 4761 | res = !res; |
| 4762 | break; |
| 4763 | case PyCmp_EXC_MATCH: |
| 4764 | if (PyTuple_Check(w)) { |
| 4765 | Py_ssize_t i, length; |
| 4766 | length = PyTuple_Size(w); |
| 4767 | for (i = 0; i < length; i += 1) { |
| 4768 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 4769 | if (!PyExceptionClass_Check(exc)) { |
| 4770 | PyErr_SetString(PyExc_TypeError, |
| 4771 | CANNOT_CATCH_MSG); |
| 4772 | return NULL; |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | else { |
| 4777 | if (!PyExceptionClass_Check(w)) { |
| 4778 | PyErr_SetString(PyExc_TypeError, |
| 4779 | CANNOT_CATCH_MSG); |
| 4780 | return NULL; |
| 4781 | } |
| 4782 | } |
| 4783 | res = PyErr_GivenExceptionMatches(v, w); |
| 4784 | break; |
| 4785 | default: |
| 4786 | return PyObject_RichCompare(v, w, op); |
| 4787 | } |
| 4788 | v = res ? Py_True : Py_False; |
| 4789 | Py_INCREF(v); |
| 4790 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4793 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4794 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 4795 | { |
| 4796 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 4797 | PyObject *import_func, *res; |
| 4798 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4799 | |
| 4800 | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
| 4801 | if (import_func == NULL) { |
| 4802 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 4803 | return NULL; |
| 4804 | } |
| 4805 | |
| 4806 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 4807 | if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4808 | int ilevel = _PyLong_AsInt(level); |
| 4809 | if (ilevel == -1 && PyErr_Occurred()) { |
| 4810 | return NULL; |
| 4811 | } |
| 4812 | res = PyImport_ImportModuleLevelObject( |
| 4813 | name, |
| 4814 | f->f_globals, |
| 4815 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 4816 | fromlist, |
| 4817 | ilevel); |
| 4818 | return res; |
| 4819 | } |
| 4820 | |
| 4821 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 4822 | |
| 4823 | stack[0] = name; |
| 4824 | stack[1] = f->f_globals; |
| 4825 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 4826 | stack[3] = fromlist; |
| 4827 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4828 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4829 | Py_DECREF(import_func); |
| 4830 | return res; |
| 4831 | } |
| 4832 | |
| 4833 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4834 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4837 | _Py_IDENTIFIER(__name__); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4838 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4839 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4840 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4841 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4842 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4843 | /* Issue #17636: in case this failed because of a circular relative |
| 4844 | import, try to fallback on reading the module directly from |
| 4845 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4846 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4847 | if (pkgname == NULL) { |
| 4848 | goto error; |
| 4849 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 4850 | if (!PyUnicode_Check(pkgname)) { |
| 4851 | Py_CLEAR(pkgname); |
| 4852 | goto error; |
| 4853 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4854 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4855 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4856 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4857 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4858 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 4859 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4860 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4861 | if (x == NULL) { |
| 4862 | goto error; |
| 4863 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4864 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4865 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4866 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4867 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4868 | if (pkgname == NULL) { |
| 4869 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 4870 | if (pkgname_or_unknown == NULL) { |
| 4871 | Py_XDECREF(pkgpath); |
| 4872 | return NULL; |
| 4873 | } |
| 4874 | } else { |
| 4875 | pkgname_or_unknown = pkgname; |
| 4876 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4877 | |
| 4878 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
| 4879 | PyErr_Clear(); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4880 | errmsg = PyUnicode_FromFormat( |
| 4881 | "cannot import name %R from %R (unknown location)", |
| 4882 | name, pkgname_or_unknown |
| 4883 | ); |
| 4884 | /* NULL check for errmsg done by PyErr_SetImportError. */ |
| 4885 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 4886 | } |
| 4887 | else { |
| 4888 | errmsg = PyUnicode_FromFormat( |
| 4889 | "cannot import name %R from %R (%S)", |
| 4890 | name, pkgname_or_unknown, pkgpath |
| 4891 | ); |
| 4892 | /* NULL check for errmsg done by PyErr_SetImportError. */ |
| 4893 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4894 | } |
| 4895 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4896 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4897 | Py_XDECREF(pkgname_or_unknown); |
| 4898 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4899 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4900 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4901 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4902 | static int |
| 4903 | import_all_from(PyObject *locals, PyObject *v) |
| 4904 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 4905 | _Py_IDENTIFIER(__all__); |
| 4906 | _Py_IDENTIFIER(__dict__); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 4907 | _Py_IDENTIFIER(__name__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4908 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4909 | int skip_leading_underscores = 0; |
| 4910 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4911 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4912 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 4913 | return -1; /* Unexpected error */ |
| 4914 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4916 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 4917 | return -1; |
| 4918 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4919 | if (dict == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4920 | PyErr_SetString(PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4921 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4922 | return -1; |
| 4923 | } |
| 4924 | all = PyMapping_Keys(dict); |
| 4925 | Py_DECREF(dict); |
| 4926 | if (all == NULL) |
| 4927 | return -1; |
| 4928 | skip_leading_underscores = 1; |
| 4929 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | for (pos = 0, err = 0; ; pos++) { |
| 4932 | name = PySequence_GetItem(all, pos); |
| 4933 | if (name == NULL) { |
| 4934 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4935 | err = -1; |
| 4936 | else |
| 4937 | PyErr_Clear(); |
| 4938 | break; |
| 4939 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 4940 | if (!PyUnicode_Check(name)) { |
| 4941 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 4942 | if (modname == NULL) { |
| 4943 | Py_DECREF(name); |
| 4944 | err = -1; |
| 4945 | break; |
| 4946 | } |
| 4947 | if (!PyUnicode_Check(modname)) { |
| 4948 | PyErr_Format(PyExc_TypeError, |
| 4949 | "module __name__ must be a string, not %.100s", |
| 4950 | Py_TYPE(modname)->tp_name); |
| 4951 | } |
| 4952 | else { |
| 4953 | PyErr_Format(PyExc_TypeError, |
| 4954 | "%s in %U.%s must be str, not %.100s", |
| 4955 | skip_leading_underscores ? "Key" : "Item", |
| 4956 | modname, |
| 4957 | skip_leading_underscores ? "__dict__" : "__all__", |
| 4958 | Py_TYPE(name)->tp_name); |
| 4959 | } |
| 4960 | Py_DECREF(modname); |
| 4961 | Py_DECREF(name); |
| 4962 | err = -1; |
| 4963 | break; |
| 4964 | } |
| 4965 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 4966 | if (PyUnicode_READY(name) == -1) { |
| 4967 | Py_DECREF(name); |
| 4968 | err = -1; |
| 4969 | break; |
| 4970 | } |
| 4971 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 4972 | Py_DECREF(name); |
| 4973 | continue; |
| 4974 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4975 | } |
| 4976 | value = PyObject_GetAttr(v, name); |
| 4977 | if (value == NULL) |
| 4978 | err = -1; |
| 4979 | else if (PyDict_CheckExact(locals)) |
| 4980 | err = PyDict_SetItem(locals, name, value); |
| 4981 | else |
| 4982 | err = PyObject_SetItem(locals, name, value); |
| 4983 | Py_DECREF(name); |
| 4984 | Py_XDECREF(value); |
| 4985 | if (err != 0) |
| 4986 | break; |
| 4987 | } |
| 4988 | Py_DECREF(all); |
| 4989 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 4992 | static int |
| 4993 | check_args_iterable(PyObject *func, PyObject *args) |
| 4994 | { |
| 4995 | if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) { |
| 4996 | PyErr_Format(PyExc_TypeError, |
| 4997 | "%.200s%.200s argument after * " |
| 4998 | "must be an iterable, not %.200s", |
| 4999 | PyEval_GetFuncName(func), |
| 5000 | PyEval_GetFuncDesc(func), |
| 5001 | args->ob_type->tp_name); |
| 5002 | return -1; |
| 5003 | } |
| 5004 | return 0; |
| 5005 | } |
| 5006 | |
| 5007 | static void |
| 5008 | format_kwargs_mapping_error(PyObject *func, PyObject *kwargs) |
| 5009 | { |
| 5010 | PyErr_Format(PyExc_TypeError, |
| 5011 | "%.200s%.200s argument after ** " |
| 5012 | "must be a mapping, not %.200s", |
| 5013 | PyEval_GetFuncName(func), |
| 5014 | PyEval_GetFuncDesc(func), |
| 5015 | kwargs->ob_type->tp_name); |
| 5016 | } |
| 5017 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5018 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5019 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | if (!obj) |
| 5024 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5025 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5026 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5027 | if (!obj_str) |
| 5028 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5030 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5031 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5032 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5033 | static void |
| 5034 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5035 | { |
| 5036 | PyObject *name; |
| 5037 | /* Don't stomp existing exception */ |
| 5038 | if (PyErr_Occurred()) |
| 5039 | return; |
| 5040 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5041 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5042 | oparg); |
| 5043 | format_exc_check_arg( |
| 5044 | PyExc_UnboundLocalError, |
| 5045 | UNBOUNDLOCAL_ERROR_MSG, |
| 5046 | name); |
| 5047 | } else { |
| 5048 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5049 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5050 | format_exc_check_arg(PyExc_NameError, |
| 5051 | UNBOUNDFREE_ERROR_MSG, name); |
| 5052 | } |
| 5053 | } |
| 5054 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5055 | static void |
| 5056 | format_awaitable_error(PyTypeObject *type, int prevopcode) |
| 5057 | { |
| 5058 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5059 | if (prevopcode == BEFORE_ASYNC_WITH) { |
| 5060 | PyErr_Format(PyExc_TypeError, |
| 5061 | "'async with' received an object from __aenter__ " |
| 5062 | "that does not implement __await__: %.100s", |
| 5063 | type->tp_name); |
| 5064 | } |
| 5065 | else if (prevopcode == WITH_CLEANUP_START) { |
| 5066 | PyErr_Format(PyExc_TypeError, |
| 5067 | "'async with' received an object from __aexit__ " |
| 5068 | "that does not implement __await__: %.100s", |
| 5069 | type->tp_name); |
| 5070 | } |
| 5071 | } |
| 5072 | } |
| 5073 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5074 | static PyObject * |
| 5075 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5076 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5077 | { |
| 5078 | PyObject *res; |
| 5079 | if (Py_REFCNT(v) == 2) { |
| 5080 | /* In the common case, there are 2 references to the value |
| 5081 | * stored in 'variable' when the += is performed: one on the |
| 5082 | * value stack (in 'v') and one still stored in the |
| 5083 | * 'variable'. We try to delete the variable now to reduce |
| 5084 | * the refcnt to 1. |
| 5085 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5086 | int opcode, oparg; |
| 5087 | NEXTOPARG(); |
| 5088 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5089 | case STORE_FAST: |
| 5090 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5091 | PyObject **fastlocals = f->f_localsplus; |
| 5092 | if (GETLOCAL(oparg) == v) |
| 5093 | SETLOCAL(oparg, NULL); |
| 5094 | break; |
| 5095 | } |
| 5096 | case STORE_DEREF: |
| 5097 | { |
| 5098 | PyObject **freevars = (f->f_localsplus + |
| 5099 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5100 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5101 | if (PyCell_GET(c) == v) { |
| 5102 | PyCell_SET(c, NULL); |
| 5103 | Py_DECREF(v); |
| 5104 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5105 | break; |
| 5106 | } |
| 5107 | case STORE_NAME: |
| 5108 | { |
| 5109 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5110 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5111 | PyObject *locals = f->f_locals; |
| 5112 | if (PyDict_CheckExact(locals) && |
| 5113 | PyDict_GetItem(locals, name) == v) { |
| 5114 | if (PyDict_DelItem(locals, name) != 0) { |
| 5115 | PyErr_Clear(); |
| 5116 | } |
| 5117 | } |
| 5118 | break; |
| 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | res = v; |
| 5123 | PyUnicode_Append(&res, w); |
| 5124 | return res; |
| 5125 | } |
| 5126 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5127 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5128 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5129 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5130 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5132 | int i; |
| 5133 | PyObject *l = PyList_New(256); |
| 5134 | if (l == NULL) return NULL; |
| 5135 | for (i = 0; i < 256; i++) { |
| 5136 | PyObject *x = PyLong_FromLong(a[i]); |
| 5137 | if (x == NULL) { |
| 5138 | Py_DECREF(l); |
| 5139 | return NULL; |
| 5140 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5141 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5142 | } |
| 5143 | for (i = 0; i < 256; i++) |
| 5144 | a[i] = 0; |
| 5145 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5149 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5150 | { |
| 5151 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5153 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5154 | int i; |
| 5155 | PyObject *l = PyList_New(257); |
| 5156 | if (l == NULL) return NULL; |
| 5157 | for (i = 0; i < 257; i++) { |
| 5158 | PyObject *x = getarray(dxpairs[i]); |
| 5159 | if (x == NULL) { |
| 5160 | Py_DECREF(l); |
| 5161 | return NULL; |
| 5162 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5163 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | } |
| 5165 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5166 | #endif |
| 5167 | } |
| 5168 | |
| 5169 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5170 | |
| 5171 | Py_ssize_t |
| 5172 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5173 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5174 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5175 | Py_ssize_t new_index; |
| 5176 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5177 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5178 | return -1; |
| 5179 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5180 | new_index = interp->co_extra_user_count++; |
| 5181 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5182 | return new_index; |
| 5183 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5184 | |
| 5185 | static void |
| 5186 | dtrace_function_entry(PyFrameObject *f) |
| 5187 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5188 | const char *filename; |
| 5189 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5190 | int lineno; |
| 5191 | |
| 5192 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5193 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5194 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5195 | |
| 5196 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5197 | } |
| 5198 | |
| 5199 | static void |
| 5200 | dtrace_function_return(PyFrameObject *f) |
| 5201 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5202 | const char *filename; |
| 5203 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5204 | int lineno; |
| 5205 | |
| 5206 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5207 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5208 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5209 | |
| 5210 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5211 | } |
| 5212 | |
| 5213 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5214 | static void |
| 5215 | maybe_dtrace_line(PyFrameObject *frame, |
| 5216 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5217 | { |
| 5218 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5219 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5220 | |
| 5221 | /* If the last instruction executed isn't in the current |
| 5222 | instruction window, reset the window. |
| 5223 | */ |
| 5224 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5225 | PyAddrPair bounds; |
| 5226 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5227 | &bounds); |
| 5228 | *instr_lb = bounds.ap_lower; |
| 5229 | *instr_ub = bounds.ap_upper; |
| 5230 | } |
| 5231 | /* If the last instruction falls at the start of a line or if |
| 5232 | it represents a jump backwards, update the frame's line |
| 5233 | number and call the trace function. */ |
| 5234 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5235 | frame->f_lineno = line; |
| 5236 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5237 | if (!co_filename) |
| 5238 | co_filename = "?"; |
| 5239 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5240 | if (!co_name) |
| 5241 | co_name = "?"; |
| 5242 | PyDTrace_LINE(co_filename, co_name, line); |
| 5243 | } |
| 5244 | *instr_prev = frame->f_lasti; |
| 5245 | } |