blob: 52a42b00724e6f442dc6e6198023d7bb1e6d2d55 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "internal/pystate.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040016#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070019#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040020#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000021#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Guido van Rossumc6004111993-11-05 10:22:19 +000023#include <ctype.h>
24
Guido van Rossum408027e1996-12-30 16:17:54 +000025#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000026/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027#define LLTRACE 1 /* Low-level trace feature */
28#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#endif
30
Yury Selivanovf2392132016-12-13 19:03:51 -050031/* Private API for the LOAD_METHOD opcode. */
32extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
33
Jeremy Hylton52820442001-01-03 23:52:36 +000034typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000035
Guido van Rossum374a9221991-04-04 10:40:29 +000036/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060037Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
38 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070039static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000040
Guido van Rossum0a066c01992-03-27 17:29:15 +000041#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000042static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020043static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010045static int call_trace(Py_tracefunc, PyObject *,
46 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000048static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010049 PyThreadState *, PyFrameObject *,
50 int, PyObject *);
51static void call_exc_trace(Py_tracefunc, PyObject *,
52 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000053static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060054 PyThreadState *, PyFrameObject *,
55 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070056static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
57static void dtrace_function_entry(PyFrameObject *);
58static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060061static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
62 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000064static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000065static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000066static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020067static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030068 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050069static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030070static int check_args_iterable(PyObject *func, PyObject *vararg);
71static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000072
Paul Prescode68140d2000-08-30 20:25:01 +000073#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000075#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000077#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 "free variable '%.200s' referenced before assignment" \
79 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000080
Guido van Rossum950361c1997-01-24 13:49:28 +000081/* Dynamic execution profile */
82#ifdef DYNAMIC_EXECUTION_PROFILE
83#ifdef DXPAIRS
84static long dxpairs[257][256];
85#define dxp dxpairs[256]
86#else
87static long dxp[256];
88#endif
89#endif
90
Eric Snow2ebc5ce2017-09-07 23:51:28 -060091#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000092
Jeffrey Yasskin39370832010-05-03 19:29:34 +000093/* This can set eval_breaker to 0 even though gil_drop_request became
94 1. We believe this is all right because the eval loop will release
95 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000096#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -060098 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +000099 GIL_REQUEST | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600100 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
101 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000102
103#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600105 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
106 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000108
109#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600111 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 COMPUTE_EVAL_BREAKER(); \
113 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000114
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000115/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000116#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600118 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
119 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000121
122#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600124 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 COMPUTE_EVAL_BREAKER(); \
126 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000127
128#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600130 _PyRuntime.ceval.pending.async_exc = 1; \
131 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
134#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600135 do { \
136 _PyRuntime.ceval.pending.async_exc = 0; \
137 COMPUTE_EVAL_BREAKER(); \
138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
140
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000142#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000143#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000144#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000145#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146
Tim Peters7f468f22004-10-11 02:40:51 +0000147int
148PyEval_ThreadsInitialized(void)
149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000151}
152
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000153void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000154PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (gil_created())
157 return;
158 create_gil();
159 take_gil(PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600160 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
161 if (!_PyRuntime.ceval.pending.lock)
162 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000164
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000165void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000166_PyEval_FiniThreads(void)
167{
168 if (!gil_created())
169 return;
170 destroy_gil();
171 assert(!gil_created());
172}
173
174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyThreadState *tstate = PyThreadState_GET();
178 if (tstate == NULL)
179 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
180 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000181}
182
183void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000184PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 /* This function must succeed when the current thread state is NULL.
187 We therefore avoid PyThreadState_GET() which dumps a fatal error
188 in debug mode.
189 */
190 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
191 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192}
193
194void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000195PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
199 /* Check someone has called PyEval_InitThreads() to create the lock */
200 assert(gil_created());
201 take_gil(tstate);
202 if (PyThreadState_Swap(tstate) != NULL)
203 Py_FatalError(
204 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000205}
206
207void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (tstate == NULL)
211 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
212 if (PyThreadState_Swap(NULL) != tstate)
213 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
214 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000216
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200217/* This function is called from PyOS_AfterFork_Child to destroy all threads
218 * which are not running in the child process, and clear internal locks
219 * which might be held by those threads.
220 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000221
222void
223PyEval_ReInitThreads(void)
224{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200225 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (!gil_created())
228 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600230 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200231 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600232 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000233
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200234 /* Destroy all threads except the current one */
235 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000236}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238/* This function is used to signal that async exceptions are waiting to be
239 raised, therefore it is also useful in non-threaded builds. */
240
241void
242_PyEval_SignalAsyncExc(void)
243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000245}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246
Guido van Rossumff4949e1992-08-05 19:58:53 +0000247/* Functions save_thread and restore_thread are always defined so
248 dynamically loaded modules needn't be compiled separately for use
249 with and without threads: */
250
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000251PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyThreadState *tstate = PyThreadState_Swap(NULL);
255 if (tstate == NULL)
256 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100257 assert(gil_created());
258 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (tstate == NULL)
266 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100267 assert(gil_created());
268
269 int err = errno;
270 take_gil(tstate);
271 /* _Py_Finalizing is protected by the GIL */
272 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
273 drop_gil(tstate);
274 PyThread_exit_thread();
275 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100277 errno = err;
278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000280}
281
282
Guido van Rossuma9672091994-09-14 13:31:22 +0000283/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
284 signal handlers or Mac I/O completion routines) can schedule calls
285 to a function to be called synchronously.
286 The synchronous function is called with one void* argument.
287 It should return 0 for success or -1 for failure -- failure should
288 be accompanied by an exception.
289
290 If registry succeeds, the registry function returns 0; if it fails
291 (e.g. due to too many pending calls) it returns -1 (without setting
292 an exception condition).
293
294 Note that because registry may occur from within signal handlers,
295 or other asynchronous events, calling malloc() is unsafe!
296
Guido van Rossuma9672091994-09-14 13:31:22 +0000297 Any thread can schedule pending calls, but only the main thread
298 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000299 There is no facility to schedule calls to a particular thread, but
300 that should be easy to change, should that ever be required. In
301 that case, the static variables here should go into the python
302 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000303*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000304
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200305void
306_PyEval_SignalReceived(void)
307{
308 /* bpo-30703: Function called when the C signal handler of Python gets a
309 signal. We cannot queue a callback using Py_AddPendingCall() since
310 that function is not async-signal-safe. */
311 SIGNAL_PENDING_CALLS();
312}
313
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200314/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000315 scheduling to be made from any thread, and even from an executing
316 callback.
317 */
318
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000319int
320Py_AddPendingCall(int (*func)(void *), void *arg)
321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600323 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 /* try a few times for the lock. Since this mechanism is used
326 * for signal handling (on the main thread), there is a (slim)
327 * chance that a signal is delivered on the same thread while we
328 * hold the lock during the Py_MakePendingCalls() function.
329 * This avoids a deadlock in that case.
330 * Note that signals can be delivered on any thread. In particular,
331 * on Windows, a SIGINT is delivered on a system-created worker
332 * thread.
333 * We also check for lock being NULL, in the unlikely case that
334 * this function is called before any bytecode evaluation takes place.
335 */
336 if (lock != NULL) {
337 for (i = 0; i<100; i++) {
338 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
339 break;
340 }
341 if (i == 100)
342 return -1;
343 }
344
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600345 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600347 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 result = -1; /* Queue full */
349 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600350 _PyRuntime.ceval.pending.calls[i].func = func;
351 _PyRuntime.ceval.pending.calls[i].arg = arg;
352 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 /* signal main loop */
355 SIGNAL_PENDING_CALLS();
356 if (lock != NULL)
357 PyThread_release_lock(lock);
358 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000359}
360
361int
362Py_MakePendingCalls(void)
363{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200364 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 int i;
366 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000367
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200368 assert(PyGILState_Check());
369
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600370 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600372 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
373 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return -1;
375 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600378 if (_PyRuntime.ceval.pending.main_thread &&
379 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
380 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200384 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200386 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200387 /* unsignal before starting to call callbacks, so that any callback
388 added in-between re-signals */
389 UNSIGNAL_PENDING_CALLS();
390
391 /* Python signal handler doesn't really queue a callback: it only signals
392 that a signal was received, see _PyEval_SignalReceived(). */
393 if (PyErr_CheckSignals() < 0) {
394 goto error;
395 }
396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* perform a bounded number of calls, in case of recursion */
398 for (i=0; i<NPENDINGCALLS; i++) {
399 int j;
400 int (*func)(void *);
401 void *arg = NULL;
402
403 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600404 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
405 j = _PyRuntime.ceval.pending.first;
406 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 func = NULL; /* Queue empty */
408 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600409 func = _PyRuntime.ceval.pending.calls[j].func;
410 arg = _PyRuntime.ceval.pending.calls[j].arg;
411 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600413 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* having released the lock, perform the callback */
415 if (func == NULL)
416 break;
417 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200418 if (r) {
419 goto error;
420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422
Charles-François Natalif23339a2011-07-23 18:15:43 +0200423 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200425
426error:
427 busy = 0;
428 SIGNAL_PENDING_CALLS(); /* We're not done yet */
429 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000430}
431
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000432/* The interpreter's recursion limit */
433
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000434#ifndef Py_DEFAULT_RECURSION_LIMIT
435#define Py_DEFAULT_RECURSION_LIMIT 1000
436#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600437
Eric Snow05351c12017-09-05 21:43:08 -0700438int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000439
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600440void
441_PyEval_Initialize(struct _ceval_runtime_state *state)
442{
443 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
444 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
445 _gil_initialize(&state->gil);
446}
447
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000448int
449Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000450{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600451 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452}
453
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000454void
455Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000456{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600457 _PyRuntime.ceval.recursion_limit = new_limit;
458 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000459}
460
Armin Rigo2b3eb402003-10-28 12:05:48 +0000461/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
462 if the recursion_depth reaches _Py_CheckRecursionLimit.
463 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
464 to guarantee that _Py_CheckRecursiveCall() is regularly called.
465 Without USE_STACKCHECK, there is no need for this. */
466int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300467_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyThreadState *tstate = PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600470 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000471
472#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700473 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (PyOS_CheckStack()) {
475 --tstate->recursion_depth;
476 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
477 return -1;
478 }
pdox18967932017-10-25 23:03:01 -0700479 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700480 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700481#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (tstate->recursion_critical)
483 /* Somebody asked that we don't check for recursion. */
484 return 0;
485 if (tstate->overflowed) {
486 if (tstate->recursion_depth > recursion_limit + 50) {
487 /* Overflowing while handling an overflow. Give up. */
488 Py_FatalError("Cannot recover from stack overflow.");
489 }
490 return 0;
491 }
492 if (tstate->recursion_depth > recursion_limit) {
493 --tstate->recursion_depth;
494 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400495 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 "maximum recursion depth exceeded%s",
497 where);
498 return -1;
499 }
500 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000501}
502
Guido van Rossum374a9221991-04-04 10:40:29 +0000503/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000504enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000505 WHY_NOT = 0x0001, /* No error */
506 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000507 WHY_RETURN = 0x0008, /* 'return' statement */
508 WHY_BREAK = 0x0010, /* 'break' statement */
509 WHY_CONTINUE = 0x0020, /* 'continue' statement */
510 WHY_YIELD = 0x0040, /* 'yield' operator */
511 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000512};
Guido van Rossum374a9221991-04-04 10:40:29 +0000513
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400514static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000515static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000516
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600517#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000518
Guido van Rossum374a9221991-04-04 10:40:29 +0000519
Guido van Rossumb209a111997-04-29 18:18:01 +0000520PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000521PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 return PyEval_EvalCodeEx(co,
524 globals, locals,
525 (PyObject **)NULL, 0,
526 (PyObject **)NULL, 0,
527 (PyObject **)NULL, 0,
528 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000529}
530
531
532/* Interpreter main loop */
533
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000534PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000535PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* This is for backward compatibility with extension modules that
537 used this API; core interpreter code should call
538 PyEval_EvalFrameEx() */
539 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000540}
541
542PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000544{
Brett Cannon3cebf932016-09-05 15:33:46 -0700545 PyThreadState *tstate = PyThreadState_GET();
546 return tstate->interp->eval_frame(f, throwflag);
547}
548
Victor Stinnerc6944e72016-11-11 02:13:35 +0100549PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700550_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
551{
Guido van Rossum950361c1997-01-24 13:49:28 +0000552#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000554#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200555 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300556 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200557 int opcode; /* Current opcode */
558 int oparg; /* Current opcode argument, if any */
559 enum why_code why; /* Reason for block stack unwind */
560 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 PyObject *retval = NULL; /* Return value */
562 PyThreadState *tstate = PyThreadState_GET();
563 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 is true when the line being executed has changed. The
570 initial values are such as to make this false the first
571 time it is tested. */
572 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000573
Serhiy Storchakaab874002016-09-11 13:48:15 +0300574 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 PyObject *names;
576 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000577
Brett Cannon368b4b72012-04-02 12:17:59 -0400578#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200579 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400580#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200581
Antoine Pitroub52ec782009-01-25 16:34:23 +0000582/* Computed GOTOs, or
583 the-optimization-commonly-but-improperly-known-as-"threaded code"
584 using gcc's labels-as-values extension
585 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
586
587 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000589 combined with a lookup table of jump addresses. However, since the
590 indirect jump instruction is shared by all opcodes, the CPU will have a
591 hard time making the right prediction for where to jump next (actually,
592 it will be always wrong except in the uncommon case of a sequence of
593 several identical opcodes).
594
595 "Threaded code" in contrast, uses an explicit jump table and an explicit
596 indirect jump instruction at the end of each opcode. Since the jump
597 instruction is at a different address for each opcode, the CPU will make a
598 separate prediction for each of these instructions, which is equivalent to
599 predicting the second opcode of each opcode pair. These predictions have
600 a much better chance to turn out valid, especially in small bytecode loops.
601
602 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000604 and potentially many more instructions (depending on the pipeline width).
605 A correctly predicted branch, however, is nearly free.
606
607 At the time of this writing, the "threaded code" version is up to 15-20%
608 faster than the normal "switch" version, depending on the compiler and the
609 CPU architecture.
610
611 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
612 because it would render the measurements invalid.
613
614
615 NOTE: care must be taken that the compiler doesn't try to "optimize" the
616 indirect jumps by sharing them between all opcodes. Such optimizations
617 can be disabled on gcc by using the -fno-gcse flag (or possibly
618 -fno-crossjumping).
619*/
620
Antoine Pitrou042b1282010-08-13 21:15:58 +0000621#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000622#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000623#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000624#endif
625
Antoine Pitrou042b1282010-08-13 21:15:58 +0000626#ifdef HAVE_COMPUTED_GOTOS
627 #ifndef USE_COMPUTED_GOTOS
628 #define USE_COMPUTED_GOTOS 1
629 #endif
630#else
631 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
632 #error "Computed gotos are not supported on this compiler."
633 #endif
634 #undef USE_COMPUTED_GOTOS
635 #define USE_COMPUTED_GOTOS 0
636#endif
637
638#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000639/* Import the static jump table */
640#include "opcode_targets.h"
641
Antoine Pitroub52ec782009-01-25 16:34:23 +0000642#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000645
Antoine Pitroub52ec782009-01-25 16:34:23 +0000646#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600648 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 FAST_DISPATCH(); \
650 } \
651 continue; \
652 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000653
654#ifdef LLTRACE
655#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700657 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300659 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300660 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 } \
662 goto fast_next_opcode; \
663 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000664#else
665#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700667 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300669 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300670 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 } \
672 goto fast_next_opcode; \
673 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000674#endif
675
676#else
677#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300679
Antoine Pitroub52ec782009-01-25 16:34:23 +0000680#define DISPATCH() continue
681#define FAST_DISPATCH() goto fast_next_opcode
682#endif
683
684
Neal Norwitza81d2202002-07-14 00:27:26 +0000685/* Tuple access macros */
686
687#ifndef Py_DEBUG
688#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
689#else
690#define GETITEM(v, i) PyTuple_GetItem((v), (i))
691#endif
692
Guido van Rossum374a9221991-04-04 10:40:29 +0000693/* Code access macros */
694
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300695/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600696#define INSTR_OFFSET() \
697 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300698#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300699 _Py_CODEUNIT word = *next_instr; \
700 opcode = _Py_OPCODE(word); \
701 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300702 next_instr++; \
703 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300704#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
705#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000706
Raymond Hettingerf606f872003-03-16 03:11:04 +0000707/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 Some opcodes tend to come in pairs thus making it possible to
709 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300710 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 Verifying the prediction costs a single high-speed test of a register
713 variable against a constant. If the pairing was good, then the
714 processor's own internal branch predication has a high likelihood of
715 success, resulting in a nearly zero-overhead transition to the
716 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300717 including its unpredictable switch-case branch. Combined with the
718 processor's internal branch prediction, a successful PREDICT has the
719 effect of making the two opcodes run as if they were a single new opcode
720 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000721
Georg Brandl86b2fb92008-07-16 03:43:04 +0000722 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 predictions turned-on and interpret the results as if some opcodes
724 had been combined or turn-off predictions so that the opcode frequency
725 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000726
727 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 the CPU to record separate branch prediction information for each
729 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730
Raymond Hettingerf606f872003-03-16 03:11:04 +0000731*/
732
Antoine Pitrou042b1282010-08-13 21:15:58 +0000733#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000735#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300736#define PREDICT(op) \
737 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300738 _Py_CODEUNIT word = *next_instr; \
739 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300740 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300741 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300742 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300743 goto PRED_##op; \
744 } \
745 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000746#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300747#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000748
Raymond Hettingerf606f872003-03-16 03:11:04 +0000749
Guido van Rossum374a9221991-04-04 10:40:29 +0000750/* Stack manipulation macros */
751
Martin v. Löwis18e16552006-02-15 17:27:45 +0000752/* The stack can grow at most MAXINT deep, as co_nlocals and
753 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000754#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
755#define EMPTY() (STACK_LEVEL() == 0)
756#define TOP() (stack_pointer[-1])
757#define SECOND() (stack_pointer[-2])
758#define THIRD() (stack_pointer[-3])
759#define FOURTH() (stack_pointer[-4])
760#define PEEK(n) (stack_pointer[-(n)])
761#define SET_TOP(v) (stack_pointer[-1] = (v))
762#define SET_SECOND(v) (stack_pointer[-2] = (v))
763#define SET_THIRD(v) (stack_pointer[-3] = (v))
764#define SET_FOURTH(v) (stack_pointer[-4] = (v))
765#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
766#define BASIC_STACKADJ(n) (stack_pointer += n)
767#define BASIC_PUSH(v) (*stack_pointer++ = (v))
768#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000769
Guido van Rossum96a42c81992-01-12 02:29:51 +0000770#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000772 lltrace && prtrace(TOP(), "push")); \
773 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000775 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000777 lltrace && prtrace(TOP(), "stackadj")); \
778 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000779#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000780 prtrace((STACK_POINTER)[-1], "ext_pop")), \
781 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000782#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000783#define PUSH(v) BASIC_PUSH(v)
784#define POP() BASIC_POP()
785#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000786#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000787#endif
788
Guido van Rossum681d79a1995-07-18 14:51:37 +0000789/* Local variable macros */
790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000792
793/* The SETLOCAL() macro must not DECREF the local variable in-place and
794 then store the new value; it must copy the old value to a temporary
795 value, then store the new value, and then DECREF the temporary value.
796 This is because it is possible that during the DECREF the frame is
797 accessed by other code (e.g. a __del__ method or gc.collect()) and the
798 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000800 GETLOCAL(i) = value; \
801 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000802
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000803
804#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 while (STACK_LEVEL() > (b)->b_level) { \
806 PyObject *v = POP(); \
807 Py_XDECREF(v); \
808 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000809
810#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300811 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100813 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 assert(STACK_LEVEL() >= (b)->b_level + 3); \
815 while (STACK_LEVEL() > (b)->b_level + 3) { \
816 value = POP(); \
817 Py_XDECREF(value); \
818 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100819 exc_info = tstate->exc_info; \
820 type = exc_info->exc_type; \
821 value = exc_info->exc_value; \
822 traceback = exc_info->exc_traceback; \
823 exc_info->exc_type = POP(); \
824 exc_info->exc_value = POP(); \
825 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 Py_XDECREF(type); \
827 Py_XDECREF(value); \
828 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300829 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000830
Guido van Rossuma027efa1997-05-05 20:56:21 +0000831/* Start of code */
832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 /* push frame */
834 if (Py_EnterRecursiveCall(""))
835 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (tstate->use_tracing) {
840 if (tstate->c_tracefunc != NULL) {
841 /* tstate->c_tracefunc, if defined, is a
842 function that will be called on *every* entry
843 to a code block. Its return value, if not
844 None, is a function that will be called at
845 the start of each executed line of code.
846 (Actually, the function must return itself
847 in order to continue tracing.) The trace
848 functions are called with three arguments:
849 a pointer to the current frame, a string
850 indicating why the function is called, and
851 an argument which depends on the situation.
852 The global trace function is also called
853 whenever an exception is detected. */
854 if (call_trace_protected(tstate->c_tracefunc,
855 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100856 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 /* Trace function raised an error */
858 goto exit_eval_frame;
859 }
860 }
861 if (tstate->c_profilefunc != NULL) {
862 /* Similar for c_profilefunc, except it needn't
863 return itself and isn't called for "line" events */
864 if (call_trace_protected(tstate->c_profilefunc,
865 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100866 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 /* Profile function raised an error */
868 goto exit_eval_frame;
869 }
870 }
871 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000872
Łukasz Langaa785c872016-09-09 17:37:37 -0700873 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
874 dtrace_function_entry(f);
875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 co = f->f_code;
877 names = co->co_names;
878 consts = co->co_consts;
879 fastlocals = f->f_localsplus;
880 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300881 assert(PyBytes_Check(co->co_code));
882 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300883 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
884 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
885 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300886 /*
887 f->f_lasti refers to the index of the last instruction,
888 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000889
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300890 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500891 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 When the PREDICT() macros are enabled, some opcode pairs follow in
894 direct succession without updating f->f_lasti. A successful
895 prediction effectively links the two codes together as if they
896 were a single new opcode; accordingly,f->f_lasti will point to
897 the first code in the pair (for instance, GET_ITER followed by
898 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300899 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300901 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300902 next_instr = first_instr;
903 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300904 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
905 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 stack_pointer = f->f_stacktop;
908 assert(stack_pointer != NULL);
909 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200910 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000911
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000912
Tim Peters5ca576e2001-06-18 22:08:13 +0000913#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200914 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000915#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +0000918
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400919 if (throwflag) /* support for generator.throw() */
920 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921
Victor Stinnerace47d72013-07-18 01:41:08 +0200922#ifdef Py_DEBUG
923 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100924 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000925 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200926 assert(!PyErr_Occurred());
927#endif
928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 assert(stack_pointer >= f->f_valuestack); /* else underflow */
931 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200932 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* Do periodic things. Doing this every time through
935 the loop would add too much overhead, so we do it
936 only every Nth instruction. We also do it if
937 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
938 event needs attention (e.g. a signal handler or
939 async I/O handler); see Py_AddPendingCall() and
940 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000941
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600942 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700943 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
944 _Py_OPCODE(*next_instr) == YIELD_FROM) {
945 /* Two cases where we skip running signal handlers and other
946 pending calls:
947 - If we're about to enter the try: of a try/finally (not
948 *very* useful, but might help in some cases and it's
949 traditional)
950 - If we're resuming a chain of nested 'yield from' or
951 'await' calls, then each frame is parked with YIELD_FROM
952 as its next opcode. If the user hit control-C we want to
953 wait until we've reached the innermost frame before
954 running the signal handler and raising KeyboardInterrupt
955 (see bpo-30039).
956 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 goto fast_next_opcode;
958 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600959 if (_Py_atomic_load_relaxed(
960 &_PyRuntime.ceval.pending.calls_to_do))
961 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400962 if (Py_MakePendingCalls() < 0)
963 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600965 if (_Py_atomic_load_relaxed(
966 &_PyRuntime.ceval.gil_drop_request))
967 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* Give another thread a chance */
969 if (PyThreadState_Swap(NULL) != tstate)
970 Py_FatalError("ceval: tstate mix-up");
971 drop_gil(tstate);
972
973 /* Other threads may run now */
974
975 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700976
977 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600978 if (_Py_IsFinalizing() &&
979 !_Py_CURRENTLY_FINALIZING(tstate))
980 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700981 drop_gil(tstate);
982 PyThread_exit_thread();
983 }
984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (PyThreadState_Swap(tstate) != NULL)
986 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 }
988 /* Check for asynchronous exceptions. */
989 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400990 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 tstate->async_exc = NULL;
992 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400993 PyErr_SetNone(exc);
994 Py_DECREF(exc);
995 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 }
997 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 fast_next_opcode:
1000 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Łukasz Langaa785c872016-09-09 17:37:37 -07001002 if (PyDTrace_LINE_ENABLED())
1003 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001008 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001009 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 /* see maybe_call_line_trace
1011 for expository comments */
1012 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 err = maybe_call_line_trace(tstate->c_tracefunc,
1015 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001016 tstate, f,
1017 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 /* Reload possibly changed frame fields */
1019 JUMPTO(f->f_lasti);
1020 if (f->f_stacktop != NULL) {
1021 stack_pointer = f->f_stacktop;
1022 f->f_stacktop = NULL;
1023 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001024 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001026 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001030
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001031 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001032 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001033#ifdef DYNAMIC_EXECUTION_PROFILE
1034#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 dxpairs[lastopcode][opcode]++;
1036 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001037#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001039#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001040
Guido van Rossum96a42c81992-01-12 02:29:51 +00001041#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (lltrace) {
1045 if (HAS_ARG(opcode)) {
1046 printf("%d: %d, %d\n",
1047 f->f_lasti, opcode, oparg);
1048 }
1049 else {
1050 printf("%d: %d\n",
1051 f->f_lasti, opcode);
1052 }
1053 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001054#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /* BEWARE!
1059 It is essential that any operation that fails sets either
1060 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1061 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 TARGET(NOP)
1064 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001065
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001066 TARGET(LOAD_FAST) {
1067 PyObject *value = GETLOCAL(oparg);
1068 if (value == NULL) {
1069 format_exc_check_arg(PyExc_UnboundLocalError,
1070 UNBOUNDLOCAL_ERROR_MSG,
1071 PyTuple_GetItem(co->co_varnames, oparg));
1072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 Py_INCREF(value);
1075 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001077 }
1078
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001079 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001080 TARGET(LOAD_CONST) {
1081 PyObject *value = GETITEM(consts, oparg);
1082 Py_INCREF(value);
1083 PUSH(value);
1084 FAST_DISPATCH();
1085 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001086
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001087 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001088 TARGET(STORE_FAST) {
1089 PyObject *value = POP();
1090 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001093
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001094 TARGET(POP_TOP) {
1095 PyObject *value = POP();
1096 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001098 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001100 TARGET(ROT_TWO) {
1101 PyObject *top = TOP();
1102 PyObject *second = SECOND();
1103 SET_TOP(second);
1104 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001106 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001107
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001108 TARGET(ROT_THREE) {
1109 PyObject *top = TOP();
1110 PyObject *second = SECOND();
1111 PyObject *third = THIRD();
1112 SET_TOP(second);
1113 SET_SECOND(third);
1114 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001116 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001117
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001118 TARGET(DUP_TOP) {
1119 PyObject *top = TOP();
1120 Py_INCREF(top);
1121 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001123 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001124
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001125 TARGET(DUP_TOP_TWO) {
1126 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001128 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001130 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001131 SET_TOP(top);
1132 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001133 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001135
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 TARGET(UNARY_POSITIVE) {
1137 PyObject *value = TOP();
1138 PyObject *res = PyNumber_Positive(value);
1139 Py_DECREF(value);
1140 SET_TOP(res);
1141 if (res == NULL)
1142 goto error;
1143 DISPATCH();
1144 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001146 TARGET(UNARY_NEGATIVE) {
1147 PyObject *value = TOP();
1148 PyObject *res = PyNumber_Negative(value);
1149 Py_DECREF(value);
1150 SET_TOP(res);
1151 if (res == NULL)
1152 goto error;
1153 DISPATCH();
1154 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 TARGET(UNARY_NOT) {
1157 PyObject *value = TOP();
1158 int err = PyObject_IsTrue(value);
1159 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (err == 0) {
1161 Py_INCREF(Py_True);
1162 SET_TOP(Py_True);
1163 DISPATCH();
1164 }
1165 else if (err > 0) {
1166 Py_INCREF(Py_False);
1167 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 DISPATCH();
1169 }
1170 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001171 goto error;
1172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001173
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001174 TARGET(UNARY_INVERT) {
1175 PyObject *value = TOP();
1176 PyObject *res = PyNumber_Invert(value);
1177 Py_DECREF(value);
1178 SET_TOP(res);
1179 if (res == NULL)
1180 goto error;
1181 DISPATCH();
1182 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001183
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 TARGET(BINARY_POWER) {
1185 PyObject *exp = POP();
1186 PyObject *base = TOP();
1187 PyObject *res = PyNumber_Power(base, exp, Py_None);
1188 Py_DECREF(base);
1189 Py_DECREF(exp);
1190 SET_TOP(res);
1191 if (res == NULL)
1192 goto error;
1193 DISPATCH();
1194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001196 TARGET(BINARY_MULTIPLY) {
1197 PyObject *right = POP();
1198 PyObject *left = TOP();
1199 PyObject *res = PyNumber_Multiply(left, right);
1200 Py_DECREF(left);
1201 Py_DECREF(right);
1202 SET_TOP(res);
1203 if (res == NULL)
1204 goto error;
1205 DISPATCH();
1206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Benjamin Petersond51374e2014-04-09 23:55:56 -04001208 TARGET(BINARY_MATRIX_MULTIPLY) {
1209 PyObject *right = POP();
1210 PyObject *left = TOP();
1211 PyObject *res = PyNumber_MatrixMultiply(left, right);
1212 Py_DECREF(left);
1213 Py_DECREF(right);
1214 SET_TOP(res);
1215 if (res == NULL)
1216 goto error;
1217 DISPATCH();
1218 }
1219
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001220 TARGET(BINARY_TRUE_DIVIDE) {
1221 PyObject *divisor = POP();
1222 PyObject *dividend = TOP();
1223 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1224 Py_DECREF(dividend);
1225 Py_DECREF(divisor);
1226 SET_TOP(quotient);
1227 if (quotient == NULL)
1228 goto error;
1229 DISPATCH();
1230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 TARGET(BINARY_FLOOR_DIVIDE) {
1233 PyObject *divisor = POP();
1234 PyObject *dividend = TOP();
1235 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1236 Py_DECREF(dividend);
1237 Py_DECREF(divisor);
1238 SET_TOP(quotient);
1239 if (quotient == NULL)
1240 goto error;
1241 DISPATCH();
1242 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001243
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 TARGET(BINARY_MODULO) {
1245 PyObject *divisor = POP();
1246 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001247 PyObject *res;
1248 if (PyUnicode_CheckExact(dividend) && (
1249 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1250 // fast path; string formatting, but not if the RHS is a str subclass
1251 // (see issue28598)
1252 res = PyUnicode_Format(dividend, divisor);
1253 } else {
1254 res = PyNumber_Remainder(dividend, divisor);
1255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 Py_DECREF(divisor);
1257 Py_DECREF(dividend);
1258 SET_TOP(res);
1259 if (res == NULL)
1260 goto error;
1261 DISPATCH();
1262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 TARGET(BINARY_ADD) {
1265 PyObject *right = POP();
1266 PyObject *left = TOP();
1267 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001268 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1269 CPython using bytecode, it is simply worthless.
1270 See http://bugs.python.org/issue21955 and
1271 http://bugs.python.org/issue10044 for the discussion. In short,
1272 no patch shown any impact on a realistic benchmark, only a minor
1273 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001274 if (PyUnicode_CheckExact(left) &&
1275 PyUnicode_CheckExact(right)) {
1276 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001277 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001278 }
1279 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 sum = PyNumber_Add(left, right);
1281 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001283 Py_DECREF(right);
1284 SET_TOP(sum);
1285 if (sum == NULL)
1286 goto error;
1287 DISPATCH();
1288 }
1289
1290 TARGET(BINARY_SUBTRACT) {
1291 PyObject *right = POP();
1292 PyObject *left = TOP();
1293 PyObject *diff = PyNumber_Subtract(left, right);
1294 Py_DECREF(right);
1295 Py_DECREF(left);
1296 SET_TOP(diff);
1297 if (diff == NULL)
1298 goto error;
1299 DISPATCH();
1300 }
1301
1302 TARGET(BINARY_SUBSCR) {
1303 PyObject *sub = POP();
1304 PyObject *container = TOP();
1305 PyObject *res = PyObject_GetItem(container, sub);
1306 Py_DECREF(container);
1307 Py_DECREF(sub);
1308 SET_TOP(res);
1309 if (res == NULL)
1310 goto error;
1311 DISPATCH();
1312 }
1313
1314 TARGET(BINARY_LSHIFT) {
1315 PyObject *right = POP();
1316 PyObject *left = TOP();
1317 PyObject *res = PyNumber_Lshift(left, right);
1318 Py_DECREF(left);
1319 Py_DECREF(right);
1320 SET_TOP(res);
1321 if (res == NULL)
1322 goto error;
1323 DISPATCH();
1324 }
1325
1326 TARGET(BINARY_RSHIFT) {
1327 PyObject *right = POP();
1328 PyObject *left = TOP();
1329 PyObject *res = PyNumber_Rshift(left, right);
1330 Py_DECREF(left);
1331 Py_DECREF(right);
1332 SET_TOP(res);
1333 if (res == NULL)
1334 goto error;
1335 DISPATCH();
1336 }
1337
1338 TARGET(BINARY_AND) {
1339 PyObject *right = POP();
1340 PyObject *left = TOP();
1341 PyObject *res = PyNumber_And(left, right);
1342 Py_DECREF(left);
1343 Py_DECREF(right);
1344 SET_TOP(res);
1345 if (res == NULL)
1346 goto error;
1347 DISPATCH();
1348 }
1349
1350 TARGET(BINARY_XOR) {
1351 PyObject *right = POP();
1352 PyObject *left = TOP();
1353 PyObject *res = PyNumber_Xor(left, right);
1354 Py_DECREF(left);
1355 Py_DECREF(right);
1356 SET_TOP(res);
1357 if (res == NULL)
1358 goto error;
1359 DISPATCH();
1360 }
1361
1362 TARGET(BINARY_OR) {
1363 PyObject *right = POP();
1364 PyObject *left = TOP();
1365 PyObject *res = PyNumber_Or(left, right);
1366 Py_DECREF(left);
1367 Py_DECREF(right);
1368 SET_TOP(res);
1369 if (res == NULL)
1370 goto error;
1371 DISPATCH();
1372 }
1373
1374 TARGET(LIST_APPEND) {
1375 PyObject *v = POP();
1376 PyObject *list = PEEK(oparg);
1377 int err;
1378 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 if (err != 0)
1381 goto error;
1382 PREDICT(JUMP_ABSOLUTE);
1383 DISPATCH();
1384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 TARGET(SET_ADD) {
1387 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001388 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 int err;
1390 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 if (err != 0)
1393 goto error;
1394 PREDICT(JUMP_ABSOLUTE);
1395 DISPATCH();
1396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 TARGET(INPLACE_POWER) {
1399 PyObject *exp = POP();
1400 PyObject *base = TOP();
1401 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1402 Py_DECREF(base);
1403 Py_DECREF(exp);
1404 SET_TOP(res);
1405 if (res == NULL)
1406 goto error;
1407 DISPATCH();
1408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 TARGET(INPLACE_MULTIPLY) {
1411 PyObject *right = POP();
1412 PyObject *left = TOP();
1413 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1414 Py_DECREF(left);
1415 Py_DECREF(right);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Petersond51374e2014-04-09 23:55:56 -04001422 TARGET(INPLACE_MATRIX_MULTIPLY) {
1423 PyObject *right = POP();
1424 PyObject *left = TOP();
1425 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1426 Py_DECREF(left);
1427 Py_DECREF(right);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
1433
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 TARGET(INPLACE_TRUE_DIVIDE) {
1435 PyObject *divisor = POP();
1436 PyObject *dividend = TOP();
1437 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1438 Py_DECREF(dividend);
1439 Py_DECREF(divisor);
1440 SET_TOP(quotient);
1441 if (quotient == NULL)
1442 goto error;
1443 DISPATCH();
1444 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001445
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 TARGET(INPLACE_FLOOR_DIVIDE) {
1447 PyObject *divisor = POP();
1448 PyObject *dividend = TOP();
1449 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1450 Py_DECREF(dividend);
1451 Py_DECREF(divisor);
1452 SET_TOP(quotient);
1453 if (quotient == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 TARGET(INPLACE_MODULO) {
1459 PyObject *right = POP();
1460 PyObject *left = TOP();
1461 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1462 Py_DECREF(left);
1463 Py_DECREF(right);
1464 SET_TOP(mod);
1465 if (mod == NULL)
1466 goto error;
1467 DISPATCH();
1468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 TARGET(INPLACE_ADD) {
1471 PyObject *right = POP();
1472 PyObject *left = TOP();
1473 PyObject *sum;
1474 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1475 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001476 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001477 }
1478 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001479 sum = PyNumber_InPlaceAdd(left, right);
1480 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001481 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 Py_DECREF(right);
1483 SET_TOP(sum);
1484 if (sum == NULL)
1485 goto error;
1486 DISPATCH();
1487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 TARGET(INPLACE_SUBTRACT) {
1490 PyObject *right = POP();
1491 PyObject *left = TOP();
1492 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1493 Py_DECREF(left);
1494 Py_DECREF(right);
1495 SET_TOP(diff);
1496 if (diff == NULL)
1497 goto error;
1498 DISPATCH();
1499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 TARGET(INPLACE_LSHIFT) {
1502 PyObject *right = POP();
1503 PyObject *left = TOP();
1504 PyObject *res = PyNumber_InPlaceLshift(left, right);
1505 Py_DECREF(left);
1506 Py_DECREF(right);
1507 SET_TOP(res);
1508 if (res == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 TARGET(INPLACE_RSHIFT) {
1514 PyObject *right = POP();
1515 PyObject *left = TOP();
1516 PyObject *res = PyNumber_InPlaceRshift(left, right);
1517 Py_DECREF(left);
1518 Py_DECREF(right);
1519 SET_TOP(res);
1520 if (res == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 TARGET(INPLACE_AND) {
1526 PyObject *right = POP();
1527 PyObject *left = TOP();
1528 PyObject *res = PyNumber_InPlaceAnd(left, right);
1529 Py_DECREF(left);
1530 Py_DECREF(right);
1531 SET_TOP(res);
1532 if (res == NULL)
1533 goto error;
1534 DISPATCH();
1535 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 TARGET(INPLACE_XOR) {
1538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *res = PyNumber_InPlaceXor(left, right);
1541 Py_DECREF(left);
1542 Py_DECREF(right);
1543 SET_TOP(res);
1544 if (res == NULL)
1545 goto error;
1546 DISPATCH();
1547 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 TARGET(INPLACE_OR) {
1550 PyObject *right = POP();
1551 PyObject *left = TOP();
1552 PyObject *res = PyNumber_InPlaceOr(left, right);
1553 Py_DECREF(left);
1554 Py_DECREF(right);
1555 SET_TOP(res);
1556 if (res == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 TARGET(STORE_SUBSCR) {
1562 PyObject *sub = TOP();
1563 PyObject *container = SECOND();
1564 PyObject *v = THIRD();
1565 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001567 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 Py_DECREF(container);
1571 Py_DECREF(sub);
1572 if (err != 0)
1573 goto error;
1574 DISPATCH();
1575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001577 TARGET(STORE_ANNOTATION) {
1578 _Py_IDENTIFIER(__annotations__);
1579 PyObject *ann_dict;
1580 PyObject *ann = POP();
1581 PyObject *name = GETITEM(names, oparg);
1582 int err;
1583 if (f->f_locals == NULL) {
1584 PyErr_Format(PyExc_SystemError,
1585 "no locals found when storing annotation");
1586 Py_DECREF(ann);
1587 goto error;
1588 }
1589 /* first try to get __annotations__ from locals... */
1590 if (PyDict_CheckExact(f->f_locals)) {
1591 ann_dict = _PyDict_GetItemId(f->f_locals,
1592 &PyId___annotations__);
1593 if (ann_dict == NULL) {
1594 PyErr_SetString(PyExc_NameError,
1595 "__annotations__ not found");
1596 Py_DECREF(ann);
1597 goto error;
1598 }
1599 Py_INCREF(ann_dict);
1600 }
1601 else {
1602 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1603 if (ann_str == NULL) {
1604 Py_DECREF(ann);
1605 goto error;
1606 }
1607 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1608 if (ann_dict == NULL) {
1609 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1610 PyErr_SetString(PyExc_NameError,
1611 "__annotations__ not found");
1612 }
1613 Py_DECREF(ann);
1614 goto error;
1615 }
1616 }
1617 /* ...if succeeded, __annotations__[name] = ann */
1618 if (PyDict_CheckExact(ann_dict)) {
1619 err = PyDict_SetItem(ann_dict, name, ann);
1620 }
1621 else {
1622 err = PyObject_SetItem(ann_dict, name, ann);
1623 }
1624 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001625 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001626 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001627 goto error;
1628 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001629 DISPATCH();
1630 }
1631
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 TARGET(DELETE_SUBSCR) {
1633 PyObject *sub = TOP();
1634 PyObject *container = SECOND();
1635 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001637 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 err = PyObject_DelItem(container, sub);
1639 Py_DECREF(container);
1640 Py_DECREF(sub);
1641 if (err != 0)
1642 goto error;
1643 DISPATCH();
1644 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001645
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001647 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001649 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001650 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 PyErr_SetString(PyExc_RuntimeError,
1653 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 Py_DECREF(value);
1655 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001657 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 Py_DECREF(value);
1659 if (res == NULL)
1660 goto error;
1661 Py_DECREF(res);
1662 DISPATCH();
1663 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001664
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 TARGET(RAISE_VARARGS) {
1666 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 switch (oparg) {
1668 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001670 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001673 /* fall through */
1674 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 if (do_raise(exc, cause)) {
1676 why = WHY_EXCEPTION;
1677 goto fast_block_end;
1678 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 break;
1680 default:
1681 PyErr_SetString(PyExc_SystemError,
1682 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 break;
1684 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 goto error;
1686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 retval = POP();
1690 why = WHY_RETURN;
1691 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001693
Yury Selivanov75445082015-05-11 22:57:16 -04001694 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001695 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001696 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001697 PyObject *obj = TOP();
1698 PyTypeObject *type = Py_TYPE(obj);
1699
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001700 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001701 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001702 }
Yury Selivanov75445082015-05-11 22:57:16 -04001703
1704 if (getter != NULL) {
1705 iter = (*getter)(obj);
1706 Py_DECREF(obj);
1707 if (iter == NULL) {
1708 SET_TOP(NULL);
1709 goto error;
1710 }
1711 }
1712 else {
1713 SET_TOP(NULL);
1714 PyErr_Format(
1715 PyExc_TypeError,
1716 "'async for' requires an object with "
1717 "__aiter__ method, got %.100s",
1718 type->tp_name);
1719 Py_DECREF(obj);
1720 goto error;
1721 }
1722
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001723 if (Py_TYPE(iter)->tp_as_async == NULL ||
1724 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001725
Yury Selivanov398ff912017-03-02 22:20:00 -05001726 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001727 PyErr_Format(
1728 PyExc_TypeError,
1729 "'async for' received an object from __aiter__ "
1730 "that does not implement __anext__: %.100s",
1731 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001732 Py_DECREF(iter);
1733 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001734 }
1735
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001736 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001737 DISPATCH();
1738 }
1739
1740 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001741 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001742 PyObject *next_iter = NULL;
1743 PyObject *awaitable = NULL;
1744 PyObject *aiter = TOP();
1745 PyTypeObject *type = Py_TYPE(aiter);
1746
Yury Selivanoveb636452016-09-08 22:01:51 -07001747 if (PyAsyncGen_CheckExact(aiter)) {
1748 awaitable = type->tp_as_async->am_anext(aiter);
1749 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001750 goto error;
1751 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001752 } else {
1753 if (type->tp_as_async != NULL){
1754 getter = type->tp_as_async->am_anext;
1755 }
Yury Selivanov75445082015-05-11 22:57:16 -04001756
Yury Selivanoveb636452016-09-08 22:01:51 -07001757 if (getter != NULL) {
1758 next_iter = (*getter)(aiter);
1759 if (next_iter == NULL) {
1760 goto error;
1761 }
1762 }
1763 else {
1764 PyErr_Format(
1765 PyExc_TypeError,
1766 "'async for' requires an iterator with "
1767 "__anext__ method, got %.100s",
1768 type->tp_name);
1769 goto error;
1770 }
Yury Selivanov75445082015-05-11 22:57:16 -04001771
Yury Selivanoveb636452016-09-08 22:01:51 -07001772 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1773 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001774 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001775 PyExc_TypeError,
1776 "'async for' received an invalid object "
1777 "from __anext__: %.100s",
1778 Py_TYPE(next_iter)->tp_name);
1779
1780 Py_DECREF(next_iter);
1781 goto error;
1782 } else {
1783 Py_DECREF(next_iter);
1784 }
1785 }
Yury Selivanov75445082015-05-11 22:57:16 -04001786
1787 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001788 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001789 DISPATCH();
1790 }
1791
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001792 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001793 TARGET(GET_AWAITABLE) {
1794 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001795 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001796
1797 Py_DECREF(iterable);
1798
Yury Selivanovc724bae2016-03-02 11:30:46 -05001799 if (iter != NULL && PyCoro_CheckExact(iter)) {
1800 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1801 if (yf != NULL) {
1802 /* `iter` is a coroutine object that is being
1803 awaited, `yf` is a pointer to the current awaitable
1804 being awaited on. */
1805 Py_DECREF(yf);
1806 Py_CLEAR(iter);
1807 PyErr_SetString(
1808 PyExc_RuntimeError,
1809 "coroutine is being awaited already");
1810 /* The code below jumps to `error` if `iter` is NULL. */
1811 }
1812 }
1813
Yury Selivanov75445082015-05-11 22:57:16 -04001814 SET_TOP(iter); /* Even if it's NULL */
1815
1816 if (iter == NULL) {
1817 goto error;
1818 }
1819
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001820 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001821 DISPATCH();
1822 }
1823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 TARGET(YIELD_FROM) {
1825 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001826 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001827 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001828 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1829 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001830 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001831 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001833 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001834 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001835 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001836 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 Py_DECREF(v);
1838 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001839 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001840 if (tstate->c_tracefunc != NULL
1841 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001842 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001843 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 if (err < 0)
1845 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001846 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 SET_TOP(val);
1848 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001849 }
Martin Panter95f53c12016-07-18 08:23:26 +00001850 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001851 f->f_stacktop = stack_pointer;
1852 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001853 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001854 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001855 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001856 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001858
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001861
1862 if (co->co_flags & CO_ASYNC_GENERATOR) {
1863 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1864 Py_DECREF(retval);
1865 if (w == NULL) {
1866 retval = NULL;
1867 goto error;
1868 }
1869 retval = w;
1870 }
1871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 f->f_stacktop = stack_pointer;
1873 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001876
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 TARGET(POP_EXCEPT) {
1878 PyTryBlock *b = PyFrame_BlockPop(f);
1879 if (b->b_type != EXCEPT_HANDLER) {
1880 PyErr_SetString(PyExc_SystemError,
1881 "popped block is not an except handler");
1882 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001887
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001888 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 TARGET(POP_BLOCK) {
1890 PyTryBlock *b = PyFrame_BlockPop(f);
1891 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 TARGET(END_FINALLY) {
1897 PyObject *status = POP();
1898 if (PyLong_Check(status)) {
1899 why = (enum why_code) PyLong_AS_LONG(status);
1900 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 if (why == WHY_RETURN ||
1902 why == WHY_CONTINUE)
1903 retval = POP();
1904 if (why == WHY_SILENCED) {
1905 /* An exception was silenced by 'with', we must
1906 manually unwind the EXCEPT_HANDLER block which was
1907 created when the exception was caught, otherwise
1908 the stack will be in an inconsistent state. */
1909 PyTryBlock *b = PyFrame_BlockPop(f);
1910 assert(b->b_type == EXCEPT_HANDLER);
1911 UNWIND_EXCEPT_HANDLER(b);
1912 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 Py_DECREF(status);
1914 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 Py_DECREF(status);
1917 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 else if (PyExceptionClass_Check(status)) {
1920 PyObject *exc = POP();
1921 PyObject *tb = POP();
1922 PyErr_Restore(status, exc, tb);
1923 why = WHY_EXCEPTION;
1924 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 PyErr_SetString(PyExc_SystemError,
1928 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 Py_DECREF(status);
1930 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 Py_DECREF(status);
1933 DISPATCH();
1934 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001935
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001937 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001938
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001939 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001940 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1942 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001943 PyErr_SetString(PyExc_NameError,
1944 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001946 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001948 }
1949 else {
1950 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1951 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001952 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1954 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001955 if (PyErr_ExceptionMatches(PyExc_KeyError))
1956 PyErr_SetString(PyExc_NameError,
1957 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001962 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001963 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 TARGET(STORE_NAME) {
1966 PyObject *name = GETITEM(names, oparg);
1967 PyObject *v = POP();
1968 PyObject *ns = f->f_locals;
1969 int err;
1970 if (ns == NULL) {
1971 PyErr_Format(PyExc_SystemError,
1972 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 if (PyDict_CheckExact(ns))
1977 err = PyDict_SetItem(ns, name, v);
1978 else
1979 err = PyObject_SetItem(ns, name, v);
1980 Py_DECREF(v);
1981 if (err != 0)
1982 goto error;
1983 DISPATCH();
1984 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001985
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 TARGET(DELETE_NAME) {
1987 PyObject *name = GETITEM(names, oparg);
1988 PyObject *ns = f->f_locals;
1989 int err;
1990 if (ns == NULL) {
1991 PyErr_Format(PyExc_SystemError,
1992 "no locals when deleting %R", name);
1993 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 err = PyObject_DelItem(ns, name);
1996 if (err != 0) {
1997 format_exc_check_arg(PyExc_NameError,
1998 NAME_ERROR_MSG,
1999 name);
2000 goto error;
2001 }
2002 DISPATCH();
2003 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002004
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002005 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 TARGET(UNPACK_SEQUENCE) {
2007 PyObject *seq = POP(), *item, **items;
2008 if (PyTuple_CheckExact(seq) &&
2009 PyTuple_GET_SIZE(seq) == oparg) {
2010 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 item = items[oparg];
2013 Py_INCREF(item);
2014 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 } else if (PyList_CheckExact(seq) &&
2017 PyList_GET_SIZE(seq) == oparg) {
2018 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 item = items[oparg];
2021 Py_INCREF(item);
2022 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 stack_pointer + oparg)) {
2026 STACKADJ(oparg);
2027 } else {
2028 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 Py_DECREF(seq);
2030 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002033 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002035
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 TARGET(UNPACK_EX) {
2037 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2038 PyObject *seq = POP();
2039
2040 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2041 stack_pointer + totalargs)) {
2042 stack_pointer += totalargs;
2043 } else {
2044 Py_DECREF(seq);
2045 goto error;
2046 }
2047 Py_DECREF(seq);
2048 DISPATCH();
2049 }
2050
2051 TARGET(STORE_ATTR) {
2052 PyObject *name = GETITEM(names, oparg);
2053 PyObject *owner = TOP();
2054 PyObject *v = SECOND();
2055 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 Py_DECREF(owner);
2060 if (err != 0)
2061 goto error;
2062 DISPATCH();
2063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 TARGET(DELETE_ATTR) {
2066 PyObject *name = GETITEM(names, oparg);
2067 PyObject *owner = POP();
2068 int err;
2069 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2070 Py_DECREF(owner);
2071 if (err != 0)
2072 goto error;
2073 DISPATCH();
2074 }
2075
2076 TARGET(STORE_GLOBAL) {
2077 PyObject *name = GETITEM(names, oparg);
2078 PyObject *v = POP();
2079 int err;
2080 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 if (err != 0)
2083 goto error;
2084 DISPATCH();
2085 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 TARGET(DELETE_GLOBAL) {
2088 PyObject *name = GETITEM(names, oparg);
2089 int err;
2090 err = PyDict_DelItem(f->f_globals, name);
2091 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002093 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002095 }
2096 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 TARGET(LOAD_NAME) {
2100 PyObject *name = GETITEM(names, oparg);
2101 PyObject *locals = f->f_locals;
2102 PyObject *v;
2103 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 "no locals when loading %R", name);
2106 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 if (PyDict_CheckExact(locals)) {
2109 v = PyDict_GetItem(locals, name);
2110 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
2112 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002114 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002115 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2116 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 PyErr_Clear();
2118 }
2119 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 if (v == NULL) {
2121 v = PyDict_GetItem(f->f_globals, name);
2122 Py_XINCREF(v);
2123 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002124 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 v = PyDict_GetItem(f->f_builtins, name);
2126 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002127 format_exc_check_arg(
2128 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 NAME_ERROR_MSG, name);
2130 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002133 }
2134 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 v = PyObject_GetItem(f->f_builtins, name);
2136 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002137 if (PyErr_ExceptionMatches(PyExc_KeyError))
2138 format_exc_check_arg(
2139 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 NAME_ERROR_MSG, name);
2141 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002142 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002149
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 TARGET(LOAD_GLOBAL) {
2151 PyObject *name = GETITEM(names, oparg);
2152 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002153 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002154 && PyDict_CheckExact(f->f_builtins))
2155 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002157 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 name);
2159 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002160 if (!_PyErr_OCCURRED()) {
2161 /* _PyDict_LoadGlobal() returns NULL without raising
2162 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002163 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002164 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002165 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002170 else {
2171 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002172
2173 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 v = PyObject_GetItem(f->f_globals, name);
2175 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002176 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2177 goto error;
2178 PyErr_Clear();
2179
Victor Stinnerb4efc962015-11-20 09:24:02 +01002180 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 v = PyObject_GetItem(f->f_builtins, name);
2182 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002183 if (PyErr_ExceptionMatches(PyExc_KeyError))
2184 format_exc_check_arg(
2185 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002186 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002188 }
2189 }
2190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002194
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 TARGET(DELETE_FAST) {
2196 PyObject *v = GETLOCAL(oparg);
2197 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 SETLOCAL(oparg, NULL);
2199 DISPATCH();
2200 }
2201 format_exc_check_arg(
2202 PyExc_UnboundLocalError,
2203 UNBOUNDLOCAL_ERROR_MSG,
2204 PyTuple_GetItem(co->co_varnames, oparg)
2205 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 goto error;
2207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 TARGET(DELETE_DEREF) {
2210 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002211 PyObject *oldobj = PyCell_GET(cell);
2212 if (oldobj != NULL) {
2213 PyCell_SET(cell, NULL);
2214 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002215 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002216 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002217 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 goto error;
2219 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002220
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 TARGET(LOAD_CLOSURE) {
2222 PyObject *cell = freevars[oparg];
2223 Py_INCREF(cell);
2224 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002227
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002228 TARGET(LOAD_CLASSDEREF) {
2229 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002230 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002231 assert(locals);
2232 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2233 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2234 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2235 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2236 if (PyDict_CheckExact(locals)) {
2237 value = PyDict_GetItem(locals, name);
2238 Py_XINCREF(value);
2239 }
2240 else {
2241 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002242 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002243 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2244 goto error;
2245 PyErr_Clear();
2246 }
2247 }
2248 if (!value) {
2249 PyObject *cell = freevars[oparg];
2250 value = PyCell_GET(cell);
2251 if (value == NULL) {
2252 format_exc_unbound(co, oparg);
2253 goto error;
2254 }
2255 Py_INCREF(value);
2256 }
2257 PUSH(value);
2258 DISPATCH();
2259 }
2260
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 TARGET(LOAD_DEREF) {
2262 PyObject *cell = freevars[oparg];
2263 PyObject *value = PyCell_GET(cell);
2264 if (value == NULL) {
2265 format_exc_unbound(co, oparg);
2266 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 Py_INCREF(value);
2269 PUSH(value);
2270 DISPATCH();
2271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 TARGET(STORE_DEREF) {
2274 PyObject *v = POP();
2275 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002276 PyObject *oldobj = PyCell_GET(cell);
2277 PyCell_SET(cell, v);
2278 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 DISPATCH();
2280 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002282 TARGET(BUILD_STRING) {
2283 PyObject *str;
2284 PyObject *empty = PyUnicode_New(0, 0);
2285 if (empty == NULL) {
2286 goto error;
2287 }
2288 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2289 Py_DECREF(empty);
2290 if (str == NULL)
2291 goto error;
2292 while (--oparg >= 0) {
2293 PyObject *item = POP();
2294 Py_DECREF(item);
2295 }
2296 PUSH(str);
2297 DISPATCH();
2298 }
2299
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 TARGET(BUILD_TUPLE) {
2301 PyObject *tup = PyTuple_New(oparg);
2302 if (tup == NULL)
2303 goto error;
2304 while (--oparg >= 0) {
2305 PyObject *item = POP();
2306 PyTuple_SET_ITEM(tup, oparg, item);
2307 }
2308 PUSH(tup);
2309 DISPATCH();
2310 }
2311
2312 TARGET(BUILD_LIST) {
2313 PyObject *list = PyList_New(oparg);
2314 if (list == NULL)
2315 goto error;
2316 while (--oparg >= 0) {
2317 PyObject *item = POP();
2318 PyList_SET_ITEM(list, oparg, item);
2319 }
2320 PUSH(list);
2321 DISPATCH();
2322 }
2323
Serhiy Storchaka73442852016-10-02 10:33:46 +03002324 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002325 TARGET(BUILD_TUPLE_UNPACK)
2326 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002327 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002328 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002329 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002330 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002331
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002332 if (sum == NULL)
2333 goto error;
2334
2335 for (i = oparg; i > 0; i--) {
2336 PyObject *none_val;
2337
2338 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2339 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002340 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002341 PyErr_ExceptionMatches(PyExc_TypeError))
2342 {
2343 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002344 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002345 Py_DECREF(sum);
2346 goto error;
2347 }
2348 Py_DECREF(none_val);
2349 }
2350
2351 if (convert_to_tuple) {
2352 return_value = PyList_AsTuple(sum);
2353 Py_DECREF(sum);
2354 if (return_value == NULL)
2355 goto error;
2356 }
2357 else {
2358 return_value = sum;
2359 }
2360
2361 while (oparg--)
2362 Py_DECREF(POP());
2363 PUSH(return_value);
2364 DISPATCH();
2365 }
2366
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 TARGET(BUILD_SET) {
2368 PyObject *set = PySet_New(NULL);
2369 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002370 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 if (set == NULL)
2372 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002373 for (i = oparg; i > 0; i--) {
2374 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 if (err == 0)
2376 err = PySet_Add(set, item);
2377 Py_DECREF(item);
2378 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002379 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 if (err != 0) {
2381 Py_DECREF(set);
2382 goto error;
2383 }
2384 PUSH(set);
2385 DISPATCH();
2386 }
2387
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002388 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002389 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002390 PyObject *sum = PySet_New(NULL);
2391 if (sum == NULL)
2392 goto error;
2393
2394 for (i = oparg; i > 0; i--) {
2395 if (_PySet_Update(sum, PEEK(i)) < 0) {
2396 Py_DECREF(sum);
2397 goto error;
2398 }
2399 }
2400
2401 while (oparg--)
2402 Py_DECREF(POP());
2403 PUSH(sum);
2404 DISPATCH();
2405 }
2406
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002408 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2410 if (map == NULL)
2411 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002412 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002413 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002414 PyObject *key = PEEK(2*i);
2415 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002416 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002417 if (err != 0) {
2418 Py_DECREF(map);
2419 goto error;
2420 }
2421 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002422
2423 while (oparg--) {
2424 Py_DECREF(POP());
2425 Py_DECREF(POP());
2426 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PUSH(map);
2428 DISPATCH();
2429 }
2430
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002431 TARGET(SETUP_ANNOTATIONS) {
2432 _Py_IDENTIFIER(__annotations__);
2433 int err;
2434 PyObject *ann_dict;
2435 if (f->f_locals == NULL) {
2436 PyErr_Format(PyExc_SystemError,
2437 "no locals found when setting up annotations");
2438 goto error;
2439 }
2440 /* check if __annotations__ in locals()... */
2441 if (PyDict_CheckExact(f->f_locals)) {
2442 ann_dict = _PyDict_GetItemId(f->f_locals,
2443 &PyId___annotations__);
2444 if (ann_dict == NULL) {
2445 /* ...if not, create a new one */
2446 ann_dict = PyDict_New();
2447 if (ann_dict == NULL) {
2448 goto error;
2449 }
2450 err = _PyDict_SetItemId(f->f_locals,
2451 &PyId___annotations__, ann_dict);
2452 Py_DECREF(ann_dict);
2453 if (err != 0) {
2454 goto error;
2455 }
2456 }
2457 }
2458 else {
2459 /* do the same if locals() is not a dict */
2460 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2461 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002462 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002463 }
2464 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2465 if (ann_dict == NULL) {
2466 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2467 goto error;
2468 }
2469 PyErr_Clear();
2470 ann_dict = PyDict_New();
2471 if (ann_dict == NULL) {
2472 goto error;
2473 }
2474 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2475 Py_DECREF(ann_dict);
2476 if (err != 0) {
2477 goto error;
2478 }
2479 }
2480 else {
2481 Py_DECREF(ann_dict);
2482 }
2483 }
2484 DISPATCH();
2485 }
2486
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002487 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002488 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002489 PyObject *map;
2490 PyObject *keys = TOP();
2491 if (!PyTuple_CheckExact(keys) ||
2492 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2493 PyErr_SetString(PyExc_SystemError,
2494 "bad BUILD_CONST_KEY_MAP keys argument");
2495 goto error;
2496 }
2497 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2498 if (map == NULL) {
2499 goto error;
2500 }
2501 for (i = oparg; i > 0; i--) {
2502 int err;
2503 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2504 PyObject *value = PEEK(i + 1);
2505 err = PyDict_SetItem(map, key, value);
2506 if (err != 0) {
2507 Py_DECREF(map);
2508 goto error;
2509 }
2510 }
2511
2512 Py_DECREF(POP());
2513 while (oparg--) {
2514 Py_DECREF(POP());
2515 }
2516 PUSH(map);
2517 DISPATCH();
2518 }
2519
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002520 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002521 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002522 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002523 if (sum == NULL)
2524 goto error;
2525
2526 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002528 if (PyDict_Update(sum, arg) < 0) {
2529 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2530 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002531 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002532 arg->ob_type->tp_name);
2533 }
2534 Py_DECREF(sum);
2535 goto error;
2536 }
2537 }
2538
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002539 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002540 Py_DECREF(POP());
2541 PUSH(sum);
2542 DISPATCH();
2543 }
2544
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002545 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2546 Py_ssize_t i;
2547 PyObject *sum = PyDict_New();
2548 if (sum == NULL)
2549 goto error;
2550
2551 for (i = oparg; i > 0; i--) {
2552 PyObject *arg = PEEK(i);
2553 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2554 PyObject *func = PEEK(2 + oparg);
2555 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002556 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002557 }
2558 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2559 PyObject *exc, *val, *tb;
2560 PyErr_Fetch(&exc, &val, &tb);
2561 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2562 PyObject *key = PyTuple_GET_ITEM(val, 0);
2563 if (!PyUnicode_Check(key)) {
2564 PyErr_Format(PyExc_TypeError,
2565 "%.200s%.200s keywords must be strings",
2566 PyEval_GetFuncName(func),
2567 PyEval_GetFuncDesc(func));
2568 } else {
2569 PyErr_Format(PyExc_TypeError,
2570 "%.200s%.200s got multiple "
2571 "values for keyword argument '%U'",
2572 PyEval_GetFuncName(func),
2573 PyEval_GetFuncDesc(func),
2574 key);
2575 }
2576 Py_XDECREF(exc);
2577 Py_XDECREF(val);
2578 Py_XDECREF(tb);
2579 }
2580 else {
2581 PyErr_Restore(exc, val, tb);
2582 }
2583 }
2584 Py_DECREF(sum);
2585 goto error;
2586 }
2587 }
2588
2589 while (oparg--)
2590 Py_DECREF(POP());
2591 PUSH(sum);
2592 DISPATCH();
2593 }
2594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 TARGET(MAP_ADD) {
2596 PyObject *key = TOP();
2597 PyObject *value = SECOND();
2598 PyObject *map;
2599 int err;
2600 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002601 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002603 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 Py_DECREF(value);
2605 Py_DECREF(key);
2606 if (err != 0)
2607 goto error;
2608 PREDICT(JUMP_ABSOLUTE);
2609 DISPATCH();
2610 }
2611
2612 TARGET(LOAD_ATTR) {
2613 PyObject *name = GETITEM(names, oparg);
2614 PyObject *owner = TOP();
2615 PyObject *res = PyObject_GetAttr(owner, name);
2616 Py_DECREF(owner);
2617 SET_TOP(res);
2618 if (res == NULL)
2619 goto error;
2620 DISPATCH();
2621 }
2622
2623 TARGET(COMPARE_OP) {
2624 PyObject *right = POP();
2625 PyObject *left = TOP();
2626 PyObject *res = cmp_outcome(oparg, left, right);
2627 Py_DECREF(left);
2628 Py_DECREF(right);
2629 SET_TOP(res);
2630 if (res == NULL)
2631 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 PREDICT(POP_JUMP_IF_FALSE);
2633 PREDICT(POP_JUMP_IF_TRUE);
2634 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002635 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002636
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002638 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002639 PyObject *fromlist = POP();
2640 PyObject *level = TOP();
2641 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002642 res = import_name(f, name, fromlist, level);
2643 Py_DECREF(level);
2644 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002645 SET_TOP(res);
2646 if (res == NULL)
2647 goto error;
2648 DISPATCH();
2649 }
2650
2651 TARGET(IMPORT_STAR) {
2652 PyObject *from = POP(), *locals;
2653 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002654 if (PyFrame_FastToLocalsWithError(f) < 0) {
2655 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002656 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002657 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002658
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 locals = f->f_locals;
2660 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 PyErr_SetString(PyExc_SystemError,
2662 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002663 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 Py_DECREF(from);
2669 if (err != 0)
2670 goto error;
2671 DISPATCH();
2672 }
Guido van Rossum25831651993-05-19 14:50:45 +00002673
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 TARGET(IMPORT_FROM) {
2675 PyObject *name = GETITEM(names, oparg);
2676 PyObject *from = TOP();
2677 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 PUSH(res);
2680 if (res == NULL)
2681 goto error;
2682 DISPATCH();
2683 }
Thomas Wouters52152252000-08-17 22:55:00 +00002684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 JUMPBY(oparg);
2687 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002689
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002690 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 TARGET(POP_JUMP_IF_FALSE) {
2692 PyObject *cond = POP();
2693 int err;
2694 if (cond == Py_True) {
2695 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 FAST_DISPATCH();
2697 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 if (cond == Py_False) {
2699 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 JUMPTO(oparg);
2701 FAST_DISPATCH();
2702 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 err = PyObject_IsTrue(cond);
2704 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002706 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 else if (err == 0)
2708 JUMPTO(oparg);
2709 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002713
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002714 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 TARGET(POP_JUMP_IF_TRUE) {
2716 PyObject *cond = POP();
2717 int err;
2718 if (cond == Py_False) {
2719 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 FAST_DISPATCH();
2721 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 if (cond == Py_True) {
2723 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 JUMPTO(oparg);
2725 FAST_DISPATCH();
2726 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 err = PyObject_IsTrue(cond);
2728 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 JUMPTO(oparg);
2731 }
2732 else if (err == 0)
2733 ;
2734 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002738
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 TARGET(JUMP_IF_FALSE_OR_POP) {
2740 PyObject *cond = TOP();
2741 int err;
2742 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 FAST_DISPATCH();
2746 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 JUMPTO(oparg);
2749 FAST_DISPATCH();
2750 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (err > 0) {
2753 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 }
2756 else if (err == 0)
2757 JUMPTO(oparg);
2758 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002762
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 TARGET(JUMP_IF_TRUE_OR_POP) {
2764 PyObject *cond = TOP();
2765 int err;
2766 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 FAST_DISPATCH();
2770 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 JUMPTO(oparg);
2773 FAST_DISPATCH();
2774 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 JUMPTO(oparg);
2778 }
2779 else if (err == 0) {
2780 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 }
2783 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002787
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002788 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002791#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 /* Enabling this path speeds-up all while and for-loops by bypassing
2793 the per-loop checks for signals. By default, this should be turned-off
2794 because it prevents detection of a control-break in tight loops like
2795 "while 1: pass". Compile with this option turned-on when you need
2796 the speed-up and do not need break checking inside tight loops (ones
2797 that contain only instructions ending with FAST_DISPATCH).
2798 */
2799 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002800#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002802#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002804
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002808 PyObject *iter = PyObject_GetIter(iterable);
2809 Py_DECREF(iterable);
2810 SET_TOP(iter);
2811 if (iter == NULL)
2812 goto error;
2813 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002814 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002815 DISPATCH();
2816 }
2817
2818 TARGET(GET_YIELD_FROM_ITER) {
2819 /* before: [obj]; after [getiter(obj)] */
2820 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002821 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002822 if (PyCoro_CheckExact(iterable)) {
2823 /* `iterable` is a coroutine */
2824 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2825 /* and it is used in a 'yield from' expression of a
2826 regular generator. */
2827 Py_DECREF(iterable);
2828 SET_TOP(NULL);
2829 PyErr_SetString(PyExc_TypeError,
2830 "cannot 'yield from' a coroutine object "
2831 "in a non-coroutine generator");
2832 goto error;
2833 }
2834 }
2835 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002836 /* `iterable` is not a generator. */
2837 iter = PyObject_GetIter(iterable);
2838 Py_DECREF(iterable);
2839 SET_TOP(iter);
2840 if (iter == NULL)
2841 goto error;
2842 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002843 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 DISPATCH();
2845 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002846
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002847 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 PyObject *iter = TOP();
2851 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2852 if (next != NULL) {
2853 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 PREDICT(STORE_FAST);
2855 PREDICT(UNPACK_SEQUENCE);
2856 DISPATCH();
2857 }
2858 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2860 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002861 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002862 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 PyErr_Clear();
2864 }
2865 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 STACKADJ(-1);
2867 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002869 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002872
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 why = WHY_BREAK;
2875 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002877
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 if (retval == NULL)
2881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 why = WHY_CONTINUE;
2883 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002885
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002886 TARGET(SETUP_LOOP)
2887 TARGET(SETUP_EXCEPT)
2888 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 /* NOTE: If you add any new block-setup opcodes that
2890 are not try/except/finally handlers, you may need
2891 to update the PyGen_NeedsFinalizing() function.
2892 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2895 STACK_LEVEL());
2896 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002898
Yury Selivanov75445082015-05-11 22:57:16 -04002899 TARGET(BEFORE_ASYNC_WITH) {
2900 _Py_IDENTIFIER(__aexit__);
2901 _Py_IDENTIFIER(__aenter__);
2902
2903 PyObject *mgr = TOP();
2904 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2905 *enter;
2906 PyObject *res;
2907 if (exit == NULL)
2908 goto error;
2909 SET_TOP(exit);
2910 enter = special_lookup(mgr, &PyId___aenter__);
2911 Py_DECREF(mgr);
2912 if (enter == NULL)
2913 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002914 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002915 Py_DECREF(enter);
2916 if (res == NULL)
2917 goto error;
2918 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002919 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002920 DISPATCH();
2921 }
2922
2923 TARGET(SETUP_ASYNC_WITH) {
2924 PyObject *res = POP();
2925 /* Setup the finally block before pushing the result
2926 of __aenter__ on the stack. */
2927 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2928 STACK_LEVEL());
2929 PUSH(res);
2930 DISPATCH();
2931 }
2932
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002934 _Py_IDENTIFIER(__exit__);
2935 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002937 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002939 if (enter == NULL)
2940 goto error;
2941 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002942 if (exit == NULL) {
2943 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002945 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002948 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 Py_DECREF(enter);
2950 if (res == NULL)
2951 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 /* Setup the finally block before pushing the result
2953 of __enter__ on the stack. */
2954 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2955 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002956
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 DISPATCH();
2959 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002960
Yury Selivanov75445082015-05-11 22:57:16 -04002961 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002962 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 how/why we entered the finally clause:
2964 - TOP = None
2965 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2966 - TOP = WHY_*; no retval below it
2967 - (TOP, SECOND, THIRD) = exc_info()
2968 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2969 Below them is EXIT, the context.__exit__ bound method.
2970 In the last case, we must call
2971 EXIT(TOP, SECOND, THIRD)
2972 otherwise we must call
2973 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002974
Benjamin Peterson8f169482013-10-29 22:25:06 -04002975 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002977 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 In addition, if the stack represents an exception,
2981 *and* the function call returns a 'true' value, we
2982 push WHY_SILENCED onto the stack. END_FINALLY will
2983 then not re-raise the exception. (But non-local
2984 gotos should still be resumed.)
2985 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002986
Victor Stinner842cfff2016-12-01 14:45:31 +01002987 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01002989 PyObject *exc, *val, *tb, *res;
2990
2991 val = tb = Py_None;
2992 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 (void)POP();
2995 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 else if (PyLong_Check(exc)) {
2999 STACKADJ(-1);
3000 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 case WHY_RETURN:
3002 case WHY_CONTINUE:
3003 /* Retval in TOP. */
3004 exit_func = SECOND();
3005 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 break;
3008 default:
3009 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 break;
3012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 }
3015 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 val = SECOND();
3019 tb = THIRD();
3020 tp2 = FOURTH();
3021 exc2 = PEEK(5);
3022 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 SET_VALUE(7, tb2);
3025 SET_VALUE(6, exc2);
3026 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3028 SET_FOURTH(NULL);
3029 /* We just shifted the stack down, so we have
3030 to tell the except handler block that the
3031 values are lower than it expects. */
3032 block = &f->f_blockstack[f->f_iblock - 1];
3033 assert(block->b_type == EXCEPT_HANDLER);
3034 block->b_level--;
3035 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003036
3037 stack[0] = exc;
3038 stack[1] = val;
3039 stack[2] = tb;
3040 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 if (res == NULL)
3043 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003044
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003045 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003046 PUSH(exc);
3047 PUSH(res);
3048 PREDICT(WITH_CLEANUP_FINISH);
3049 DISPATCH();
3050 }
3051
3052 PREDICTED(WITH_CLEANUP_FINISH);
3053 TARGET(WITH_CLEANUP_FINISH) {
3054 PyObject *res = POP();
3055 PyObject *exc = POP();
3056 int err;
3057
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 if (exc != Py_None)
3059 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 else
3061 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003062
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003064 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 /* There was an exception and a True return */
3070 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3071 }
3072 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003075
Yury Selivanovf2392132016-12-13 19:03:51 -05003076 TARGET(LOAD_METHOD) {
3077 /* Designed to work in tamdem with CALL_METHOD. */
3078 PyObject *name = GETITEM(names, oparg);
3079 PyObject *obj = TOP();
3080 PyObject *meth = NULL;
3081
3082 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3083
Yury Selivanovf2392132016-12-13 19:03:51 -05003084 if (meth == NULL) {
3085 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003086 goto error;
3087 }
3088
3089 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003090 /* We can bypass temporary bound method object.
3091 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003092
INADA Naoki015bce62017-01-16 17:23:30 +09003093 meth | self | arg1 | ... | argN
3094 */
3095 SET_TOP(meth);
3096 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003097 }
3098 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003099 /* meth is not an unbound method (but a regular attr, or
3100 something was returned by a descriptor protocol). Set
3101 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003102 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003103
3104 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003105 */
INADA Naoki015bce62017-01-16 17:23:30 +09003106 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003107 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003108 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003109 }
3110 DISPATCH();
3111 }
3112
3113 TARGET(CALL_METHOD) {
3114 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003115 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003116
3117 sp = stack_pointer;
3118
INADA Naoki015bce62017-01-16 17:23:30 +09003119 meth = PEEK(oparg + 2);
3120 if (meth == NULL) {
3121 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3122 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003123
3124 Stack layout:
3125
INADA Naoki015bce62017-01-16 17:23:30 +09003126 ... | NULL | callable | arg1 | ... | argN
3127 ^- TOP()
3128 ^- (-oparg)
3129 ^- (-oparg-1)
3130 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003131
Ville Skyttä49b27342017-08-03 09:00:59 +03003132 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003133 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003134 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003135 res = call_function(&sp, oparg, NULL);
3136 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003137 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003138 }
3139 else {
3140 /* This is a method call. Stack layout:
3141
INADA Naoki015bce62017-01-16 17:23:30 +09003142 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003143 ^- TOP()
3144 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003145 ^- (-oparg-1)
3146 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003147
INADA Naoki015bce62017-01-16 17:23:30 +09003148 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003149 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003150 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003151 */
3152 res = call_function(&sp, oparg + 1, NULL);
3153 stack_pointer = sp;
3154 }
3155
3156 PUSH(res);
3157 if (res == NULL)
3158 goto error;
3159 DISPATCH();
3160 }
3161
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003162 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 TARGET(CALL_FUNCTION) {
3164 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003166 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003169 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003174
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003175 TARGET(CALL_FUNCTION_KW) {
3176 PyObject **sp, *res, *names;
3177
3178 names = POP();
3179 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003181 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003184 Py_DECREF(names);
3185
3186 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003188 }
3189 DISPATCH();
3190 }
3191
3192 TARGET(CALL_FUNCTION_EX) {
3193 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003194 if (oparg & 0x01) {
3195 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003196 if (!PyDict_CheckExact(kwargs)) {
3197 PyObject *d = PyDict_New();
3198 if (d == NULL)
3199 goto error;
3200 if (PyDict_Update(d, kwargs) != 0) {
3201 Py_DECREF(d);
3202 /* PyDict_Update raises attribute
3203 * error (percolated from an attempt
3204 * to get 'keys' attribute) instead of
3205 * a type error if its second argument
3206 * is not a mapping.
3207 */
3208 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003209 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003210 }
Victor Stinnereece2222016-09-12 11:16:37 +02003211 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003212 goto error;
3213 }
3214 Py_DECREF(kwargs);
3215 kwargs = d;
3216 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003217 assert(PyDict_CheckExact(kwargs));
3218 }
3219 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003220 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003221 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003222 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003223 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003224 goto error;
3225 }
3226 Py_SETREF(callargs, PySequence_Tuple(callargs));
3227 if (callargs == NULL) {
3228 goto error;
3229 }
3230 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003231 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003232
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003233 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003234 Py_DECREF(func);
3235 Py_DECREF(callargs);
3236 Py_XDECREF(kwargs);
3237
3238 SET_TOP(result);
3239 if (result == NULL) {
3240 goto error;
3241 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003244
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003245 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003246 PyObject *qualname = POP();
3247 PyObject *codeobj = POP();
3248 PyFunctionObject *func = (PyFunctionObject *)
3249 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003250
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003251 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003253 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003256
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003257 if (oparg & 0x08) {
3258 assert(PyTuple_CheckExact(TOP()));
3259 func ->func_closure = POP();
3260 }
3261 if (oparg & 0x04) {
3262 assert(PyDict_CheckExact(TOP()));
3263 func->func_annotations = POP();
3264 }
3265 if (oparg & 0x02) {
3266 assert(PyDict_CheckExact(TOP()));
3267 func->func_kwdefaults = POP();
3268 }
3269 if (oparg & 0x01) {
3270 assert(PyTuple_CheckExact(TOP()));
3271 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003273
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003274 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003277
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 TARGET(BUILD_SLICE) {
3279 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 step = NULL;
3284 stop = POP();
3285 start = TOP();
3286 slice = PySlice_New(start, stop, step);
3287 Py_DECREF(start);
3288 Py_DECREF(stop);
3289 Py_XDECREF(step);
3290 SET_TOP(slice);
3291 if (slice == NULL)
3292 goto error;
3293 DISPATCH();
3294 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003295
Eric V. Smitha78c7952015-11-03 12:45:05 -05003296 TARGET(FORMAT_VALUE) {
3297 /* Handles f-string value formatting. */
3298 PyObject *result;
3299 PyObject *fmt_spec;
3300 PyObject *value;
3301 PyObject *(*conv_fn)(PyObject *);
3302 int which_conversion = oparg & FVC_MASK;
3303 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3304
3305 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003306 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003307
3308 /* See if any conversion is specified. */
3309 switch (which_conversion) {
3310 case FVC_STR: conv_fn = PyObject_Str; break;
3311 case FVC_REPR: conv_fn = PyObject_Repr; break;
3312 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3313
3314 /* Must be 0 (meaning no conversion), since only four
3315 values are allowed by (oparg & FVC_MASK). */
3316 default: conv_fn = NULL; break;
3317 }
3318
3319 /* If there's a conversion function, call it and replace
3320 value with that result. Otherwise, just use value,
3321 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003322 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003323 result = conv_fn(value);
3324 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003325 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003326 Py_XDECREF(fmt_spec);
3327 goto error;
3328 }
3329 value = result;
3330 }
3331
3332 /* If value is a unicode object, and there's no fmt_spec,
3333 then we know the result of format(value) is value
3334 itself. In that case, skip calling format(). I plan to
3335 move this optimization in to PyObject_Format()
3336 itself. */
3337 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3338 /* Do nothing, just transfer ownership to result. */
3339 result = value;
3340 } else {
3341 /* Actually call format(). */
3342 result = PyObject_Format(value, fmt_spec);
3343 Py_DECREF(value);
3344 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003345 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003346 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003347 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003348 }
3349
Eric V. Smith135d5f42016-02-05 18:23:08 -05003350 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003351 DISPATCH();
3352 }
3353
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003355 int oldoparg = oparg;
3356 NEXTOPARG();
3357 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003360
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003361
Antoine Pitrou042b1282010-08-13 21:15:58 +00003362#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003364#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 default:
3366 fprintf(stderr,
3367 "XXX lineno: %d, opcode: %d\n",
3368 PyFrame_GetLineNumber(f),
3369 opcode);
3370 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003374
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003375 /* This should never be reached. Every opcode should end with DISPATCH()
3376 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003377 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003378
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 assert(why == WHY_NOT);
3382 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003383
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003384 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003385#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 if (!PyErr_Occurred())
3387 PyErr_SetString(PyExc_SystemError,
3388 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003389#else
3390 assert(PyErr_Occurred());
3391#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 /* Log traceback info. */
3394 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003395
Benjamin Peterson51f46162013-01-23 08:38:47 -05003396 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003397 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3398 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003399
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003400fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 assert(why != WHY_NOT);
3402
3403 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 while (why != WHY_NOT && f->f_iblock > 0) {
3405 /* Peek at the current block. */
3406 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 assert(why != WHY_YIELD);
3409 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3410 why = WHY_NOT;
3411 JUMPTO(PyLong_AS_LONG(retval));
3412 Py_DECREF(retval);
3413 break;
3414 }
3415 /* Now we have to pop the block. */
3416 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 if (b->b_type == EXCEPT_HANDLER) {
3419 UNWIND_EXCEPT_HANDLER(b);
3420 continue;
3421 }
3422 UNWIND_BLOCK(b);
3423 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3424 why = WHY_NOT;
3425 JUMPTO(b->b_handler);
3426 break;
3427 }
3428 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3429 || b->b_type == SETUP_FINALLY)) {
3430 PyObject *exc, *val, *tb;
3431 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003432 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 /* Beware, this invalidates all b->b_* fields */
3434 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003435 PUSH(exc_info->exc_traceback);
3436 PUSH(exc_info->exc_value);
3437 if (exc_info->exc_type != NULL) {
3438 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 }
3440 else {
3441 Py_INCREF(Py_None);
3442 PUSH(Py_None);
3443 }
3444 PyErr_Fetch(&exc, &val, &tb);
3445 /* Make the raw exception data
3446 available to the handler,
3447 so a program can emulate the
3448 Python main loop. */
3449 PyErr_NormalizeException(
3450 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003451 if (tb != NULL)
3452 PyException_SetTraceback(val, tb);
3453 else
3454 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003456 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003458 exc_info->exc_value = val;
3459 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 if (tb == NULL)
3461 tb = Py_None;
3462 Py_INCREF(tb);
3463 PUSH(tb);
3464 PUSH(val);
3465 PUSH(exc);
3466 why = WHY_NOT;
3467 JUMPTO(handler);
3468 break;
3469 }
3470 if (b->b_type == SETUP_FINALLY) {
3471 if (why & (WHY_RETURN | WHY_CONTINUE))
3472 PUSH(retval);
3473 PUSH(PyLong_FromLong((long)why));
3474 why = WHY_NOT;
3475 JUMPTO(b->b_handler);
3476 break;
3477 }
3478 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 if (why != WHY_NOT)
3483 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003484
Victor Stinnerace47d72013-07-18 01:41:08 +02003485 assert(!PyErr_Occurred());
3486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 assert(why != WHY_YIELD);
3490 /* Pop remaining stack entries. */
3491 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 PyObject *o = POP();
3493 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 if (why != WHY_RETURN)
3497 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003498
Victor Stinner4a7cc882015-03-06 23:35:27 +01003499 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003500
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003501fast_yield:
Benjamin Peterson83195c32011-07-03 13:44:00 -05003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003504 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003506 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3507 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003509 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 why = WHY_EXCEPTION;
3511 }
3512 }
3513 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003514 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3515 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyTrace_RETURN, NULL);
3517 }
3518 }
3519 if (tstate->c_profilefunc) {
3520 if (why == WHY_EXCEPTION)
3521 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003522 tstate->c_profileobj,
3523 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003525 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3526 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003528 Py_CLEAR(retval);
Xiang Zhang997478e2018-01-29 11:32:12 +08003529 /* why = WHY_EXCEPTION; useless yet but cause compiler warnings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 }
3531 }
3532 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003535exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003536 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3537 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003539 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003541
Victor Stinnerefde1462015-03-21 15:04:43 +01003542 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003543}
3544
Benjamin Petersonb204a422011-06-05 22:04:07 -05003545static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003546format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3547{
3548 int err;
3549 Py_ssize_t len = PyList_GET_SIZE(names);
3550 PyObject *name_str, *comma, *tail, *tmp;
3551
3552 assert(PyList_CheckExact(names));
3553 assert(len >= 1);
3554 /* Deal with the joys of natural language. */
3555 switch (len) {
3556 case 1:
3557 name_str = PyList_GET_ITEM(names, 0);
3558 Py_INCREF(name_str);
3559 break;
3560 case 2:
3561 name_str = PyUnicode_FromFormat("%U and %U",
3562 PyList_GET_ITEM(names, len - 2),
3563 PyList_GET_ITEM(names, len - 1));
3564 break;
3565 default:
3566 tail = PyUnicode_FromFormat(", %U, and %U",
3567 PyList_GET_ITEM(names, len - 2),
3568 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003569 if (tail == NULL)
3570 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003571 /* Chop off the last two objects in the list. This shouldn't actually
3572 fail, but we can't be too careful. */
3573 err = PyList_SetSlice(names, len - 2, len, NULL);
3574 if (err == -1) {
3575 Py_DECREF(tail);
3576 return;
3577 }
3578 /* Stitch everything up into a nice comma-separated list. */
3579 comma = PyUnicode_FromString(", ");
3580 if (comma == NULL) {
3581 Py_DECREF(tail);
3582 return;
3583 }
3584 tmp = PyUnicode_Join(comma, names);
3585 Py_DECREF(comma);
3586 if (tmp == NULL) {
3587 Py_DECREF(tail);
3588 return;
3589 }
3590 name_str = PyUnicode_Concat(tmp, tail);
3591 Py_DECREF(tmp);
3592 Py_DECREF(tail);
3593 break;
3594 }
3595 if (name_str == NULL)
3596 return;
3597 PyErr_Format(PyExc_TypeError,
3598 "%U() missing %i required %s argument%s: %U",
3599 co->co_name,
3600 len,
3601 kind,
3602 len == 1 ? "" : "s",
3603 name_str);
3604 Py_DECREF(name_str);
3605}
3606
3607static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003608missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003609 PyObject **fastlocals)
3610{
Victor Stinner74319ae2016-08-25 00:04:09 +02003611 Py_ssize_t i, j = 0;
3612 Py_ssize_t start, end;
3613 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003614 const char *kind = positional ? "positional" : "keyword-only";
3615 PyObject *missing_names;
3616
3617 /* Compute the names of the arguments that are missing. */
3618 missing_names = PyList_New(missing);
3619 if (missing_names == NULL)
3620 return;
3621 if (positional) {
3622 start = 0;
3623 end = co->co_argcount - defcount;
3624 }
3625 else {
3626 start = co->co_argcount;
3627 end = start + co->co_kwonlyargcount;
3628 }
3629 for (i = start; i < end; i++) {
3630 if (GETLOCAL(i) == NULL) {
3631 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3632 PyObject *name = PyObject_Repr(raw);
3633 if (name == NULL) {
3634 Py_DECREF(missing_names);
3635 return;
3636 }
3637 PyList_SET_ITEM(missing_names, j++, name);
3638 }
3639 }
3640 assert(j == missing);
3641 format_missing(kind, co, missing_names);
3642 Py_DECREF(missing_names);
3643}
3644
3645static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003646too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3647 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003648{
3649 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003650 Py_ssize_t kwonly_given = 0;
3651 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003652 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003653 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003654
Benjamin Petersone109c702011-06-24 09:37:26 -05003655 assert((co->co_flags & CO_VARARGS) == 0);
3656 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003657 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3658 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003659 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003660 }
3661 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003662 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003663 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003664 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003665 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003666 }
3667 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003668 plural = (co_argcount != 1);
3669 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003670 }
3671 if (sig == NULL)
3672 return;
3673 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003674 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3675 kwonly_sig = PyUnicode_FromFormat(format,
3676 given != 1 ? "s" : "",
3677 kwonly_given,
3678 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003679 if (kwonly_sig == NULL) {
3680 Py_DECREF(sig);
3681 return;
3682 }
3683 }
3684 else {
3685 /* This will not fail. */
3686 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003687 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003688 }
3689 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003690 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003691 co->co_name,
3692 sig,
3693 plural ? "s" : "",
3694 given,
3695 kwonly_sig,
3696 given == 1 && !kwonly_given ? "was" : "were");
3697 Py_DECREF(sig);
3698 Py_DECREF(kwonly_sig);
3699}
3700
Guido van Rossumc2e20742006-02-27 22:32:47 +00003701/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003702 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003703 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003704
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003705PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003706_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003707 PyObject *const *args, Py_ssize_t argcount,
3708 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003709 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003710 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003711 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003712 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003713{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003714 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003715 PyFrameObject *f;
3716 PyObject *retval = NULL;
3717 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003718 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003720 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3721 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003722 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 if (globals == NULL) {
3725 PyErr_SetString(PyExc_SystemError,
3726 "PyEval_EvalCodeEx: NULL globals");
3727 return NULL;
3728 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003729
Victor Stinnerc7020012016-08-16 23:40:29 +02003730 /* Create the frame */
3731 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003733 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003734 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 fastlocals = f->f_localsplus;
3738 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003739
Victor Stinnerc7020012016-08-16 23:40:29 +02003740 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 if (co->co_flags & CO_VARKEYWORDS) {
3742 kwdict = PyDict_New();
3743 if (kwdict == NULL)
3744 goto fail;
3745 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003746 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003748 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 SETLOCAL(i, kwdict);
3750 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003751 else {
3752 kwdict = NULL;
3753 }
3754
3755 /* Copy positional arguments into local variables */
3756 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003757 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003758 }
3759 else {
3760 n = argcount;
3761 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003762 for (i = 0; i < n; i++) {
3763 x = args[i];
3764 Py_INCREF(x);
3765 SETLOCAL(i, x);
3766 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003767
3768 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 if (co->co_flags & CO_VARARGS) {
3770 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003771 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003773 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 SETLOCAL(total_args, u);
3775 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 x = args[i];
3777 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003778 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003780 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003781
Serhiy Storchakab7281052016-09-12 00:52:40 +03003782 /* Handle keyword arguments passed as two strided arrays */
3783 kwcount *= kwstep;
3784 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003785 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003786 PyObject *keyword = kwnames[i];
3787 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003788 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003789
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3791 PyErr_Format(PyExc_TypeError,
3792 "%U() keywords must be strings",
3793 co->co_name);
3794 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003796
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 /* Speed hack: do raw pointer compares. As names are
3798 normally interned this should almost always hit. */
3799 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3800 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003801 PyObject *name = co_varnames[j];
3802 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003803 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003804 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003805 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003806
Benjamin Petersonb204a422011-06-05 22:04:07 -05003807 /* Slow fallback, just in case */
3808 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003809 PyObject *name = co_varnames[j];
3810 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3811 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003813 }
3814 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003816 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003818
Victor Stinner231d1f32017-01-11 02:12:06 +01003819 assert(j >= total_args);
3820 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003822 "%U() got an unexpected keyword argument '%S'",
3823 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003826
Christian Heimes0bd447f2013-07-20 14:48:10 +02003827 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3828 goto fail;
3829 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003831
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832 kw_found:
3833 if (GETLOCAL(j) != NULL) {
3834 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003835 "%U() got multiple values for argument '%S'",
3836 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 Py_INCREF(value);
3840 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003842
3843 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003845 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 goto fail;
3847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003848
3849 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003851 Py_ssize_t m = co->co_argcount - defcount;
3852 Py_ssize_t missing = 0;
3853 for (i = argcount; i < m; i++) {
3854 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003855 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003856 }
3857 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003858 if (missing) {
3859 missing_arguments(co, missing, defcount, fastlocals);
3860 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 }
3862 if (n > m)
3863 i = n - m;
3864 else
3865 i = 0;
3866 for (; i < defcount; i++) {
3867 if (GETLOCAL(m+i) == NULL) {
3868 PyObject *def = defs[i];
3869 Py_INCREF(def);
3870 SETLOCAL(m+i, def);
3871 }
3872 }
3873 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003874
3875 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003877 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003878 for (i = co->co_argcount; i < total_args; i++) {
3879 PyObject *name;
3880 if (GETLOCAL(i) != NULL)
3881 continue;
3882 name = PyTuple_GET_ITEM(co->co_varnames, i);
3883 if (kwdefs != NULL) {
3884 PyObject *def = PyDict_GetItem(kwdefs, name);
3885 if (def) {
3886 Py_INCREF(def);
3887 SETLOCAL(i, def);
3888 continue;
3889 }
3890 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003891 missing++;
3892 }
3893 if (missing) {
3894 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003895 goto fail;
3896 }
3897 }
3898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003900 vars into frame. */
3901 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003903 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003904 /* Possibly account for the cell variable being an argument. */
3905 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003906 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003907 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003908 /* Clear the local copy. */
3909 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003910 }
3911 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003912 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003913 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003914 if (c == NULL)
3915 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003916 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003918
3919 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003920 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3921 PyObject *o = PyTuple_GET_ITEM(closure, i);
3922 Py_INCREF(o);
3923 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003925
Yury Selivanoveb636452016-09-08 22:01:51 -07003926 /* Handle generator/coroutine/asynchronous generator */
3927 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003928 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003929 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003930 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003931
3932 if (is_coro && tstate->in_coroutine_wrapper) {
3933 assert(coro_wrapper != NULL);
3934 PyErr_Format(PyExc_RuntimeError,
3935 "coroutine wrapper %.200R attempted "
3936 "to recursively wrap %.200R",
3937 coro_wrapper,
3938 co);
3939 goto fail;
3940 }
Yury Selivanov75445082015-05-11 22:57:16 -04003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 /* Don't need to keep the reference to f_back, it will be set
3943 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003944 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 /* Create a new generator that owns the ready to run frame
3947 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003948 if (is_coro) {
3949 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003950 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3951 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003952 } else {
3953 gen = PyGen_NewWithQualName(f, name, qualname);
3954 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003955 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003956 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003957 }
INADA Naoki9c157762016-12-26 18:52:46 +09003958
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003959 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003960
Yury Selivanov94c22632015-06-04 10:16:51 -04003961 if (is_coro && coro_wrapper != NULL) {
3962 PyObject *wrapped;
3963 tstate->in_coroutine_wrapper = 1;
3964 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3965 tstate->in_coroutine_wrapper = 0;
3966 return wrapped;
3967 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003968
Yury Selivanov75445082015-05-11 22:57:16 -04003969 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003971
Victor Stinner59a73272016-12-09 18:51:13 +01003972 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003973
Thomas Woutersce272b62007-09-19 21:19:28 +00003974fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 /* decref'ing the frame can cause __del__ methods to get invoked,
3977 which can call back into Python. While we're done with the
3978 current Python frame (f), the associated C stack is still in use,
3979 so recursion_depth must be boosted for the duration.
3980 */
3981 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003982 if (Py_REFCNT(f) > 1) {
3983 Py_DECREF(f);
3984 _PyObject_GC_TRACK(f);
3985 }
3986 else {
3987 ++tstate->recursion_depth;
3988 Py_DECREF(f);
3989 --tstate->recursion_depth;
3990 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003992}
3993
Victor Stinner40ee3012014-06-16 15:59:28 +02003994PyObject *
3995PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003996 PyObject *const *args, int argcount,
3997 PyObject *const *kws, int kwcount,
3998 PyObject *const *defs, int defcount,
3999 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004000{
4001 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004002 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004003 kws, kws != NULL ? kws + 1 : NULL,
4004 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004005 defs, defcount,
4006 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004007 NULL, NULL);
4008}
Tim Peters5ca576e2001-06-18 22:08:13 +00004009
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004010static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004011special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004014 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004016 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 return NULL;
4018 }
4019 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004020}
4021
4022
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004023/* Logic for the raise statement (too complicated for inlining).
4024 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004025static int
Collin Winter828f04a2007-08-31 00:04:24 +00004026do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (exc == NULL) {
4031 /* Reraise */
4032 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004033 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004035 type = exc_info->exc_type;
4036 value = exc_info->exc_value;
4037 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004038 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 PyErr_SetString(PyExc_RuntimeError,
4040 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004041 return 0;
4042 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 Py_XINCREF(type);
4044 Py_XINCREF(value);
4045 Py_XINCREF(tb);
4046 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004047 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 /* We support the following forms of raise:
4051 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004052 raise <instance>
4053 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 if (PyExceptionClass_Check(exc)) {
4056 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004057 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 if (value == NULL)
4059 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004060 if (!PyExceptionInstance_Check(value)) {
4061 PyErr_Format(PyExc_TypeError,
4062 "calling %R should have returned an instance of "
4063 "BaseException, not %R",
4064 type, Py_TYPE(value));
4065 goto raise_error;
4066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
4068 else if (PyExceptionInstance_Check(exc)) {
4069 value = exc;
4070 type = PyExceptionInstance_Class(exc);
4071 Py_INCREF(type);
4072 }
4073 else {
4074 /* Not something you can raise. You get an exception
4075 anyway, just not what you specified :-) */
4076 Py_DECREF(exc);
4077 PyErr_SetString(PyExc_TypeError,
4078 "exceptions must derive from BaseException");
4079 goto raise_error;
4080 }
Collin Winter828f04a2007-08-31 00:04:24 +00004081
Serhiy Storchakac0191582016-09-27 11:37:10 +03004082 assert(type != NULL);
4083 assert(value != NULL);
4084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (cause) {
4086 PyObject *fixed_cause;
4087 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004088 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 if (fixed_cause == NULL)
4090 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004091 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004093 else if (PyExceptionInstance_Check(cause)) {
4094 fixed_cause = cause;
4095 }
4096 else if (cause == Py_None) {
4097 Py_DECREF(cause);
4098 fixed_cause = NULL;
4099 }
4100 else {
4101 PyErr_SetString(PyExc_TypeError,
4102 "exception causes must derive from "
4103 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 goto raise_error;
4105 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004106 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 }
Collin Winter828f04a2007-08-31 00:04:24 +00004108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 PyErr_SetObject(type, value);
4110 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004111 Py_DECREF(value);
4112 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004113 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004114
4115raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 Py_XDECREF(value);
4117 Py_XDECREF(type);
4118 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004119 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004120}
4121
Tim Petersd6d010b2001-06-21 02:49:55 +00004122/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004123 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004124
Guido van Rossum0368b722007-05-11 16:50:42 +00004125 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4126 with a variable target.
4127*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004128
Barry Warsawe42b18f1997-08-25 22:13:04 +00004129static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004130unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 int i = 0, j = 0;
4133 Py_ssize_t ll = 0;
4134 PyObject *it; /* iter(v) */
4135 PyObject *w;
4136 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004141 if (it == NULL) {
4142 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4143 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4144 {
4145 PyErr_Format(PyExc_TypeError,
4146 "cannot unpack non-iterable %.200s object",
4147 v->ob_type->tp_name);
4148 }
4149 return 0;
4150 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 for (; i < argcnt; i++) {
4153 w = PyIter_Next(it);
4154 if (w == NULL) {
4155 /* Iterator done, via error or exhaustion. */
4156 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004157 if (argcntafter == -1) {
4158 PyErr_Format(PyExc_ValueError,
4159 "not enough values to unpack (expected %d, got %d)",
4160 argcnt, i);
4161 }
4162 else {
4163 PyErr_Format(PyExc_ValueError,
4164 "not enough values to unpack "
4165 "(expected at least %d, got %d)",
4166 argcnt + argcntafter, i);
4167 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 }
4169 goto Error;
4170 }
4171 *--sp = w;
4172 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 if (argcntafter == -1) {
4175 /* We better have exhausted the iterator now. */
4176 w = PyIter_Next(it);
4177 if (w == NULL) {
4178 if (PyErr_Occurred())
4179 goto Error;
4180 Py_DECREF(it);
4181 return 1;
4182 }
4183 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004184 PyErr_Format(PyExc_ValueError,
4185 "too many values to unpack (expected %d)",
4186 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 goto Error;
4188 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 l = PySequence_List(it);
4191 if (l == NULL)
4192 goto Error;
4193 *--sp = l;
4194 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 ll = PyList_GET_SIZE(l);
4197 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004198 PyErr_Format(PyExc_ValueError,
4199 "not enough values to unpack (expected at least %d, got %zd)",
4200 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 goto Error;
4202 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 /* Pop the "after-variable" args off the list. */
4205 for (j = argcntafter; j > 0; j--, i++) {
4206 *--sp = PyList_GET_ITEM(l, ll - j);
4207 }
4208 /* Resize the list. */
4209 Py_SIZE(l) = ll - argcntafter;
4210 Py_DECREF(it);
4211 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004212
Tim Petersd6d010b2001-06-21 02:49:55 +00004213Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 for (; i > 0; i--, sp++)
4215 Py_DECREF(*sp);
4216 Py_XDECREF(it);
4217 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004218}
4219
4220
Guido van Rossum96a42c81992-01-12 02:29:51 +00004221#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004222static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004223prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 printf("%s ", str);
4226 if (PyObject_Print(v, stdout, 0) != 0)
4227 PyErr_Clear(); /* Don't know what else to do */
4228 printf("\n");
4229 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004230}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004231#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004232
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004233static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004234call_exc_trace(Py_tracefunc func, PyObject *self,
4235 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004236{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004237 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004239 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (value == NULL) {
4241 value = Py_None;
4242 Py_INCREF(value);
4243 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004244 PyErr_NormalizeException(&type, &value, &orig_traceback);
4245 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 arg = PyTuple_Pack(3, type, value, traceback);
4247 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004248 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 return;
4250 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004251 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 Py_DECREF(arg);
4253 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004254 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 else {
4256 Py_XDECREF(type);
4257 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004258 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004260}
4261
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004262static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004263call_trace_protected(Py_tracefunc func, PyObject *obj,
4264 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 PyObject *type, *value, *traceback;
4268 int err;
4269 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004270 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 if (err == 0)
4272 {
4273 PyErr_Restore(type, value, traceback);
4274 return 0;
4275 }
4276 else {
4277 Py_XDECREF(type);
4278 Py_XDECREF(value);
4279 Py_XDECREF(traceback);
4280 return -1;
4281 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004282}
4283
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004284static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004285call_trace(Py_tracefunc func, PyObject *obj,
4286 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 int result;
4290 if (tstate->tracing)
4291 return 0;
4292 tstate->tracing++;
4293 tstate->use_tracing = 0;
4294 result = func(obj, frame, what, arg);
4295 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4296 || (tstate->c_profilefunc != NULL));
4297 tstate->tracing--;
4298 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004299}
4300
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004301PyObject *
4302_PyEval_CallTracing(PyObject *func, PyObject *args)
4303{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004304 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 int save_tracing = tstate->tracing;
4306 int save_use_tracing = tstate->use_tracing;
4307 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 tstate->tracing = 0;
4310 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4311 || (tstate->c_profilefunc != NULL));
4312 result = PyObject_Call(func, args, NULL);
4313 tstate->tracing = save_tracing;
4314 tstate->use_tracing = save_use_tracing;
4315 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004316}
4317
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004318/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004319static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004320maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004321 PyThreadState *tstate, PyFrameObject *frame,
4322 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 int result = 0;
4325 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 /* If the last instruction executed isn't in the current
4328 instruction window, reset the window.
4329 */
4330 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4331 PyAddrPair bounds;
4332 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4333 &bounds);
4334 *instr_lb = bounds.ap_lower;
4335 *instr_ub = bounds.ap_upper;
4336 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004337 /* If the last instruction falls at the start of a line or if it
4338 represents a jump backwards, update the frame's line number and
4339 then call the trace function if we're tracing source lines.
4340 */
4341 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004343 if (frame->f_trace_lines) {
4344 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4345 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 }
George King20faa682017-10-18 17:44:22 -07004347 /* Always emit an opcode event if we're tracing all opcodes. */
4348 if (frame->f_trace_opcodes) {
4349 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 *instr_prev = frame->f_lasti;
4352 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004353}
4354
Fred Drake5755ce62001-06-27 19:19:46 +00004355void
4356PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 PyThreadState *tstate = PyThreadState_GET();
4359 PyObject *temp = tstate->c_profileobj;
4360 Py_XINCREF(arg);
4361 tstate->c_profilefunc = NULL;
4362 tstate->c_profileobj = NULL;
4363 /* Must make sure that tracing is not ignored if 'temp' is freed */
4364 tstate->use_tracing = tstate->c_tracefunc != NULL;
4365 Py_XDECREF(temp);
4366 tstate->c_profilefunc = func;
4367 tstate->c_profileobj = arg;
4368 /* Flag that tracing or profiling is turned on */
4369 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004370}
4371
4372void
4373PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 PyThreadState *tstate = PyThreadState_GET();
4376 PyObject *temp = tstate->c_traceobj;
4377 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4378 Py_XINCREF(arg);
4379 tstate->c_tracefunc = NULL;
4380 tstate->c_traceobj = NULL;
4381 /* Must make sure that profiling is not ignored if 'temp' is freed */
4382 tstate->use_tracing = tstate->c_profilefunc != NULL;
4383 Py_XDECREF(temp);
4384 tstate->c_tracefunc = func;
4385 tstate->c_traceobj = arg;
4386 /* Flag that tracing or profiling is turned on */
4387 tstate->use_tracing = ((func != NULL)
4388 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004389}
4390
Yury Selivanov75445082015-05-11 22:57:16 -04004391void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004392_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4393{
4394 assert(new_depth >= 0);
4395 PyThreadState *tstate = PyThreadState_GET();
4396 tstate->coroutine_origin_tracking_depth = new_depth;
4397}
4398
4399int
4400_PyEval_GetCoroutineOriginTrackingDepth(void)
4401{
4402 PyThreadState *tstate = PyThreadState_GET();
4403 return tstate->coroutine_origin_tracking_depth;
4404}
4405
4406void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004407_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004408{
4409 PyThreadState *tstate = PyThreadState_GET();
4410
Yury Selivanov75445082015-05-11 22:57:16 -04004411 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004412 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004413}
4414
4415PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004416_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004417{
4418 PyThreadState *tstate = PyThreadState_GET();
4419 return tstate->coroutine_wrapper;
4420}
4421
Yury Selivanoveb636452016-09-08 22:01:51 -07004422void
4423_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4424{
4425 PyThreadState *tstate = PyThreadState_GET();
4426
4427 Py_XINCREF(firstiter);
4428 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4429}
4430
4431PyObject *
4432_PyEval_GetAsyncGenFirstiter(void)
4433{
4434 PyThreadState *tstate = PyThreadState_GET();
4435 return tstate->async_gen_firstiter;
4436}
4437
4438void
4439_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4440{
4441 PyThreadState *tstate = PyThreadState_GET();
4442
4443 Py_XINCREF(finalizer);
4444 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4445}
4446
4447PyObject *
4448_PyEval_GetAsyncGenFinalizer(void)
4449{
4450 PyThreadState *tstate = PyThreadState_GET();
4451 return tstate->async_gen_finalizer;
4452}
4453
Guido van Rossumb209a111997-04-29 18:18:01 +00004454PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004455PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyFrameObject *current_frame = PyEval_GetFrame();
4458 if (current_frame == NULL)
4459 return PyThreadState_GET()->interp->builtins;
4460 else
4461 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004462}
4463
Guido van Rossumb209a111997-04-29 18:18:01 +00004464PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004465PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004468 if (current_frame == NULL) {
4469 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004471 }
4472
4473 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4474 return NULL;
4475
4476 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004478}
4479
Guido van Rossumb209a111997-04-29 18:18:01 +00004480PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004481PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 PyFrameObject *current_frame = PyEval_GetFrame();
4484 if (current_frame == NULL)
4485 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004486
4487 assert(current_frame->f_globals != NULL);
4488 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004489}
4490
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004491PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004492PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 PyThreadState *tstate = PyThreadState_GET();
4495 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004496}
4497
Guido van Rossum6135a871995-01-09 17:53:26 +00004498int
Tim Peters5ba58662001-07-16 02:29:45 +00004499PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 PyFrameObject *current_frame = PyEval_GetFrame();
4502 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 if (current_frame != NULL) {
4505 const int codeflags = current_frame->f_code->co_flags;
4506 const int compilerflags = codeflags & PyCF_MASK;
4507 if (compilerflags) {
4508 result = 1;
4509 cf->cf_flags |= compilerflags;
4510 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004511#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 if (codeflags & CO_GENERATOR_ALLOWED) {
4513 result = 1;
4514 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4515 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004516#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 }
4518 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004519}
4520
Guido van Rossum3f5da241990-12-20 15:06:42 +00004521
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004522const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004523PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 if (PyMethod_Check(func))
4526 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4527 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004528 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 else if (PyCFunction_Check(func))
4530 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4531 else
4532 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004533}
4534
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004535const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004536PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 if (PyMethod_Check(func))
4539 return "()";
4540 else if (PyFunction_Check(func))
4541 return "()";
4542 else if (PyCFunction_Check(func))
4543 return "()";
4544 else
4545 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004546}
4547
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004548#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004549if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004550 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4551 tstate, tstate->frame, \
4552 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 x = NULL; \
4554 } \
4555 else { \
4556 x = call; \
4557 if (tstate->c_profilefunc != NULL) { \
4558 if (x == NULL) { \
4559 call_trace_protected(tstate->c_profilefunc, \
4560 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004561 tstate, tstate->frame, \
4562 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 /* XXX should pass (type, value, tb) */ \
4564 } else { \
4565 if (call_trace(tstate->c_profilefunc, \
4566 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004567 tstate, tstate->frame, \
4568 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 Py_DECREF(x); \
4570 x = NULL; \
4571 } \
4572 } \
4573 } \
4574 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004575} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 x = call; \
4577 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004578
Victor Stinner415c5102017-01-11 00:54:57 +01004579/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4580 to reduce the stack consumption. */
4581Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004582call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004583{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004584 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 PyObject *func = *pfunc;
4586 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004587 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4588 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004589 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 /* Always dispatch PyCFunction first, because these are
4592 presumed to be the most frequent callable object.
4593 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004594 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004596 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004597 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004598 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4599 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004600 if (tstate->use_tracing && tstate->c_profilefunc) {
4601 // We need to create PyCFunctionObject for tracing.
4602 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4603 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4604 if (func == NULL) {
4605 return NULL;
4606 }
4607 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4608 kwnames));
4609 Py_DECREF(func);
4610 }
4611 else {
4612 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4613 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004614 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004615 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004616 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004617 /* Optimize access to bound methods. Reuse the Python stack
4618 to pass 'self' as the first argument, replace 'func'
4619 with 'self'. It avoids the creation of a new temporary tuple
4620 for arguments (to replace func with self) when the method uses
4621 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004622 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004623 Py_INCREF(self);
4624 func = PyMethod_GET_FUNCTION(func);
4625 Py_INCREF(func);
4626 Py_SETREF(*pfunc, self);
4627 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004628 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004629 }
4630 else {
4631 Py_INCREF(func);
4632 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004633
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004634 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004635 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004636 }
4637 else {
4638 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4639 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004640 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004642
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004643 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4644
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004645 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 while ((*pp_stack) > pfunc) {
4647 w = EXT_POP(*pp_stack);
4648 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004652}
4653
Jeremy Hylton52820442001-01-03 23:52:36 +00004654static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004655do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004658 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 PyThreadState *tstate = PyThreadState_GET();
4660 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004661 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004663 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004664 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004665 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004666}
4667
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004668/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004669 nb_index slot defined, and store in *pi.
4670 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004671 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004672 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004673*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004674int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004675_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004676{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004677 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 Py_ssize_t x;
4679 if (PyIndex_Check(v)) {
4680 x = PyNumber_AsSsize_t(v, NULL);
4681 if (x == -1 && PyErr_Occurred())
4682 return 0;
4683 }
4684 else {
4685 PyErr_SetString(PyExc_TypeError,
4686 "slice indices must be integers or "
4687 "None or have an __index__ method");
4688 return 0;
4689 }
4690 *pi = x;
4691 }
4692 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004693}
4694
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004695int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004696_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004697{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004698 Py_ssize_t x;
4699 if (PyIndex_Check(v)) {
4700 x = PyNumber_AsSsize_t(v, NULL);
4701 if (x == -1 && PyErr_Occurred())
4702 return 0;
4703 }
4704 else {
4705 PyErr_SetString(PyExc_TypeError,
4706 "slice indices must be integers or "
4707 "have an __index__ method");
4708 return 0;
4709 }
4710 *pi = x;
4711 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004712}
4713
4714
Guido van Rossum486364b2007-06-30 05:01:58 +00004715#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004717
Guido van Rossumb209a111997-04-29 18:18:01 +00004718static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004719cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 int res = 0;
4722 switch (op) {
4723 case PyCmp_IS:
4724 res = (v == w);
4725 break;
4726 case PyCmp_IS_NOT:
4727 res = (v != w);
4728 break;
4729 case PyCmp_IN:
4730 res = PySequence_Contains(w, v);
4731 if (res < 0)
4732 return NULL;
4733 break;
4734 case PyCmp_NOT_IN:
4735 res = PySequence_Contains(w, v);
4736 if (res < 0)
4737 return NULL;
4738 res = !res;
4739 break;
4740 case PyCmp_EXC_MATCH:
4741 if (PyTuple_Check(w)) {
4742 Py_ssize_t i, length;
4743 length = PyTuple_Size(w);
4744 for (i = 0; i < length; i += 1) {
4745 PyObject *exc = PyTuple_GET_ITEM(w, i);
4746 if (!PyExceptionClass_Check(exc)) {
4747 PyErr_SetString(PyExc_TypeError,
4748 CANNOT_CATCH_MSG);
4749 return NULL;
4750 }
4751 }
4752 }
4753 else {
4754 if (!PyExceptionClass_Check(w)) {
4755 PyErr_SetString(PyExc_TypeError,
4756 CANNOT_CATCH_MSG);
4757 return NULL;
4758 }
4759 }
4760 res = PyErr_GivenExceptionMatches(v, w);
4761 break;
4762 default:
4763 return PyObject_RichCompare(v, w, op);
4764 }
4765 v = res ? Py_True : Py_False;
4766 Py_INCREF(v);
4767 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004768}
4769
Thomas Wouters52152252000-08-17 22:55:00 +00004770static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004771import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4772{
4773 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004774 PyObject *import_func, *res;
4775 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004776
4777 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4778 if (import_func == NULL) {
4779 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4780 return NULL;
4781 }
4782
4783 /* Fast path for not overloaded __import__. */
4784 if (import_func == PyThreadState_GET()->interp->import_func) {
4785 int ilevel = _PyLong_AsInt(level);
4786 if (ilevel == -1 && PyErr_Occurred()) {
4787 return NULL;
4788 }
4789 res = PyImport_ImportModuleLevelObject(
4790 name,
4791 f->f_globals,
4792 f->f_locals == NULL ? Py_None : f->f_locals,
4793 fromlist,
4794 ilevel);
4795 return res;
4796 }
4797
4798 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004799
4800 stack[0] = name;
4801 stack[1] = f->f_globals;
4802 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4803 stack[3] = fromlist;
4804 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004805 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004806 Py_DECREF(import_func);
4807 return res;
4808}
4809
4810static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004811import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004814 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004815 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004816
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004817 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004818 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004819 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004820 /* Issue #17636: in case this failed because of a circular relative
4821 import, try to fallback on reading the module directly from
4822 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004823 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004824 if (pkgname == NULL) {
4825 goto error;
4826 }
Oren Milman6db70332017-09-19 14:23:01 +03004827 if (!PyUnicode_Check(pkgname)) {
4828 Py_CLEAR(pkgname);
4829 goto error;
4830 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004831 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004832 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004833 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004834 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004835 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004836 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004837 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004838 if (x == NULL) {
4839 goto error;
4840 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004841 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004843 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004844 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004845 if (pkgname == NULL) {
4846 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4847 if (pkgname_or_unknown == NULL) {
4848 Py_XDECREF(pkgpath);
4849 return NULL;
4850 }
4851 } else {
4852 pkgname_or_unknown = pkgname;
4853 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004854
4855 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4856 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004857 errmsg = PyUnicode_FromFormat(
4858 "cannot import name %R from %R (unknown location)",
4859 name, pkgname_or_unknown
4860 );
4861 /* NULL check for errmsg done by PyErr_SetImportError. */
4862 PyErr_SetImportError(errmsg, pkgname, NULL);
4863 }
4864 else {
4865 errmsg = PyUnicode_FromFormat(
4866 "cannot import name %R from %R (%S)",
4867 name, pkgname_or_unknown, pkgpath
4868 );
4869 /* NULL check for errmsg done by PyErr_SetImportError. */
4870 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004871 }
4872
Xiang Zhang4830f582017-03-21 11:13:42 +08004873 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004874 Py_XDECREF(pkgname_or_unknown);
4875 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004876 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004877}
Guido van Rossumac7be682001-01-17 15:42:30 +00004878
Thomas Wouters52152252000-08-17 22:55:00 +00004879static int
4880import_all_from(PyObject *locals, PyObject *v)
4881{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004882 _Py_IDENTIFIER(__all__);
4883 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004884 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 int skip_leading_underscores = 0;
4886 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004887
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004888 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4889 return -1; /* Unexpected error */
4890 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004892 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4893 return -1;
4894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004897 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 return -1;
4899 }
4900 all = PyMapping_Keys(dict);
4901 Py_DECREF(dict);
4902 if (all == NULL)
4903 return -1;
4904 skip_leading_underscores = 1;
4905 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 for (pos = 0, err = 0; ; pos++) {
4908 name = PySequence_GetItem(all, pos);
4909 if (name == NULL) {
4910 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4911 err = -1;
4912 else
4913 PyErr_Clear();
4914 break;
4915 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004916 if (skip_leading_underscores && PyUnicode_Check(name)) {
4917 if (PyUnicode_READY(name) == -1) {
4918 Py_DECREF(name);
4919 err = -1;
4920 break;
4921 }
4922 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4923 Py_DECREF(name);
4924 continue;
4925 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 }
4927 value = PyObject_GetAttr(v, name);
4928 if (value == NULL)
4929 err = -1;
4930 else if (PyDict_CheckExact(locals))
4931 err = PyDict_SetItem(locals, name, value);
4932 else
4933 err = PyObject_SetItem(locals, name, value);
4934 Py_DECREF(name);
4935 Py_XDECREF(value);
4936 if (err != 0)
4937 break;
4938 }
4939 Py_DECREF(all);
4940 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004941}
4942
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004943static int
4944check_args_iterable(PyObject *func, PyObject *args)
4945{
4946 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4947 PyErr_Format(PyExc_TypeError,
4948 "%.200s%.200s argument after * "
4949 "must be an iterable, not %.200s",
4950 PyEval_GetFuncName(func),
4951 PyEval_GetFuncDesc(func),
4952 args->ob_type->tp_name);
4953 return -1;
4954 }
4955 return 0;
4956}
4957
4958static void
4959format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4960{
4961 PyErr_Format(PyExc_TypeError,
4962 "%.200s%.200s argument after ** "
4963 "must be a mapping, not %.200s",
4964 PyEval_GetFuncName(func),
4965 PyEval_GetFuncDesc(func),
4966 kwargs->ob_type->tp_name);
4967}
4968
Guido van Rossumac7be682001-01-17 15:42:30 +00004969static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004970format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 if (!obj)
4975 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004976
Serhiy Storchaka06515832016-11-20 09:13:07 +02004977 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004978 if (!obj_str)
4979 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004982}
Guido van Rossum950361c1997-01-24 13:49:28 +00004983
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004984static void
4985format_exc_unbound(PyCodeObject *co, int oparg)
4986{
4987 PyObject *name;
4988 /* Don't stomp existing exception */
4989 if (PyErr_Occurred())
4990 return;
4991 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4992 name = PyTuple_GET_ITEM(co->co_cellvars,
4993 oparg);
4994 format_exc_check_arg(
4995 PyExc_UnboundLocalError,
4996 UNBOUNDLOCAL_ERROR_MSG,
4997 name);
4998 } else {
4999 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5000 PyTuple_GET_SIZE(co->co_cellvars));
5001 format_exc_check_arg(PyExc_NameError,
5002 UNBOUNDFREE_ERROR_MSG, name);
5003 }
5004}
5005
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005006static PyObject *
5007unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005008 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005009{
5010 PyObject *res;
5011 if (Py_REFCNT(v) == 2) {
5012 /* In the common case, there are 2 references to the value
5013 * stored in 'variable' when the += is performed: one on the
5014 * value stack (in 'v') and one still stored in the
5015 * 'variable'. We try to delete the variable now to reduce
5016 * the refcnt to 1.
5017 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005018 int opcode, oparg;
5019 NEXTOPARG();
5020 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005021 case STORE_FAST:
5022 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005023 PyObject **fastlocals = f->f_localsplus;
5024 if (GETLOCAL(oparg) == v)
5025 SETLOCAL(oparg, NULL);
5026 break;
5027 }
5028 case STORE_DEREF:
5029 {
5030 PyObject **freevars = (f->f_localsplus +
5031 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005032 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005033 if (PyCell_GET(c) == v) {
5034 PyCell_SET(c, NULL);
5035 Py_DECREF(v);
5036 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005037 break;
5038 }
5039 case STORE_NAME:
5040 {
5041 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005042 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005043 PyObject *locals = f->f_locals;
5044 if (PyDict_CheckExact(locals) &&
5045 PyDict_GetItem(locals, name) == v) {
5046 if (PyDict_DelItem(locals, name) != 0) {
5047 PyErr_Clear();
5048 }
5049 }
5050 break;
5051 }
5052 }
5053 }
5054 res = v;
5055 PyUnicode_Append(&res, w);
5056 return res;
5057}
5058
Guido van Rossum950361c1997-01-24 13:49:28 +00005059#ifdef DYNAMIC_EXECUTION_PROFILE
5060
Skip Montanarof118cb12001-10-15 20:51:38 +00005061static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005062getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 int i;
5065 PyObject *l = PyList_New(256);
5066 if (l == NULL) return NULL;
5067 for (i = 0; i < 256; i++) {
5068 PyObject *x = PyLong_FromLong(a[i]);
5069 if (x == NULL) {
5070 Py_DECREF(l);
5071 return NULL;
5072 }
5073 PyList_SetItem(l, i, x);
5074 }
5075 for (i = 0; i < 256; i++)
5076 a[i] = 0;
5077 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005078}
5079
5080PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005081_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005082{
5083#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005085#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 int i;
5087 PyObject *l = PyList_New(257);
5088 if (l == NULL) return NULL;
5089 for (i = 0; i < 257; i++) {
5090 PyObject *x = getarray(dxpairs[i]);
5091 if (x == NULL) {
5092 Py_DECREF(l);
5093 return NULL;
5094 }
5095 PyList_SetItem(l, i, x);
5096 }
5097 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005098#endif
5099}
5100
5101#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005102
5103Py_ssize_t
5104_PyEval_RequestCodeExtraIndex(freefunc free)
5105{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005106 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005107 Py_ssize_t new_index;
5108
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005109 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005110 return -1;
5111 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005112 new_index = interp->co_extra_user_count++;
5113 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005114 return new_index;
5115}
Łukasz Langaa785c872016-09-09 17:37:37 -07005116
5117static void
5118dtrace_function_entry(PyFrameObject *f)
5119{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005120 const char *filename;
5121 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005122 int lineno;
5123
5124 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5125 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5126 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5127
5128 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5129}
5130
5131static void
5132dtrace_function_return(PyFrameObject *f)
5133{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005134 const char *filename;
5135 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005136 int lineno;
5137
5138 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5139 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5140 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5141
5142 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5143}
5144
5145/* DTrace equivalent of maybe_call_line_trace. */
5146static void
5147maybe_dtrace_line(PyFrameObject *frame,
5148 int *instr_lb, int *instr_ub, int *instr_prev)
5149{
5150 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005151 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005152
5153 /* If the last instruction executed isn't in the current
5154 instruction window, reset the window.
5155 */
5156 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5157 PyAddrPair bounds;
5158 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5159 &bounds);
5160 *instr_lb = bounds.ap_lower;
5161 *instr_ub = bounds.ap_upper;
5162 }
5163 /* If the last instruction falls at the start of a line or if
5164 it represents a jump backwards, update the frame's line
5165 number and call the trace function. */
5166 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5167 frame->f_lineno = line;
5168 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5169 if (!co_filename)
5170 co_filename = "?";
5171 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5172 if (!co_name)
5173 co_name = "?";
5174 PyDTrace_LINE(co_filename, co_name, line);
5175 }
5176 *instr_prev = frame->f_lasti;
5177}