blob: af5eb99d6c90a7f9da975ca757c8f659ec2dd5d6 [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
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 TARGET(DELETE_SUBSCR) {
1578 PyObject *sub = TOP();
1579 PyObject *container = SECOND();
1580 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001582 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 err = PyObject_DelItem(container, sub);
1584 Py_DECREF(container);
1585 Py_DECREF(sub);
1586 if (err != 0)
1587 goto error;
1588 DISPATCH();
1589 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001590
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001592 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001594 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001595 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 PyErr_SetString(PyExc_RuntimeError,
1598 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 Py_DECREF(value);
1600 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001602 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 Py_DECREF(value);
1604 if (res == NULL)
1605 goto error;
1606 Py_DECREF(res);
1607 DISPATCH();
1608 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001609
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 TARGET(RAISE_VARARGS) {
1611 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 switch (oparg) {
1613 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001615 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001618 /* fall through */
1619 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 if (do_raise(exc, cause)) {
1621 why = WHY_EXCEPTION;
1622 goto fast_block_end;
1623 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 break;
1625 default:
1626 PyErr_SetString(PyExc_SystemError,
1627 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 break;
1629 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 goto error;
1631 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001632
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 retval = POP();
1635 why = WHY_RETURN;
1636 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001638
Yury Selivanov75445082015-05-11 22:57:16 -04001639 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001640 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001641 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001642 PyObject *obj = TOP();
1643 PyTypeObject *type = Py_TYPE(obj);
1644
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001645 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001646 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001647 }
Yury Selivanov75445082015-05-11 22:57:16 -04001648
1649 if (getter != NULL) {
1650 iter = (*getter)(obj);
1651 Py_DECREF(obj);
1652 if (iter == NULL) {
1653 SET_TOP(NULL);
1654 goto error;
1655 }
1656 }
1657 else {
1658 SET_TOP(NULL);
1659 PyErr_Format(
1660 PyExc_TypeError,
1661 "'async for' requires an object with "
1662 "__aiter__ method, got %.100s",
1663 type->tp_name);
1664 Py_DECREF(obj);
1665 goto error;
1666 }
1667
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001668 if (Py_TYPE(iter)->tp_as_async == NULL ||
1669 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001670
Yury Selivanov398ff912017-03-02 22:20:00 -05001671 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001672 PyErr_Format(
1673 PyExc_TypeError,
1674 "'async for' received an object from __aiter__ "
1675 "that does not implement __anext__: %.100s",
1676 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001677 Py_DECREF(iter);
1678 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001679 }
1680
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001681 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001682 DISPATCH();
1683 }
1684
1685 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001686 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001687 PyObject *next_iter = NULL;
1688 PyObject *awaitable = NULL;
1689 PyObject *aiter = TOP();
1690 PyTypeObject *type = Py_TYPE(aiter);
1691
Yury Selivanoveb636452016-09-08 22:01:51 -07001692 if (PyAsyncGen_CheckExact(aiter)) {
1693 awaitable = type->tp_as_async->am_anext(aiter);
1694 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001695 goto error;
1696 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001697 } else {
1698 if (type->tp_as_async != NULL){
1699 getter = type->tp_as_async->am_anext;
1700 }
Yury Selivanov75445082015-05-11 22:57:16 -04001701
Yury Selivanoveb636452016-09-08 22:01:51 -07001702 if (getter != NULL) {
1703 next_iter = (*getter)(aiter);
1704 if (next_iter == NULL) {
1705 goto error;
1706 }
1707 }
1708 else {
1709 PyErr_Format(
1710 PyExc_TypeError,
1711 "'async for' requires an iterator with "
1712 "__anext__ method, got %.100s",
1713 type->tp_name);
1714 goto error;
1715 }
Yury Selivanov75445082015-05-11 22:57:16 -04001716
Yury Selivanoveb636452016-09-08 22:01:51 -07001717 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1718 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001719 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001720 PyExc_TypeError,
1721 "'async for' received an invalid object "
1722 "from __anext__: %.100s",
1723 Py_TYPE(next_iter)->tp_name);
1724
1725 Py_DECREF(next_iter);
1726 goto error;
1727 } else {
1728 Py_DECREF(next_iter);
1729 }
1730 }
Yury Selivanov75445082015-05-11 22:57:16 -04001731
1732 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001733 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001734 DISPATCH();
1735 }
1736
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001737 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001738 TARGET(GET_AWAITABLE) {
1739 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001740 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001741
1742 Py_DECREF(iterable);
1743
Yury Selivanovc724bae2016-03-02 11:30:46 -05001744 if (iter != NULL && PyCoro_CheckExact(iter)) {
1745 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1746 if (yf != NULL) {
1747 /* `iter` is a coroutine object that is being
1748 awaited, `yf` is a pointer to the current awaitable
1749 being awaited on. */
1750 Py_DECREF(yf);
1751 Py_CLEAR(iter);
1752 PyErr_SetString(
1753 PyExc_RuntimeError,
1754 "coroutine is being awaited already");
1755 /* The code below jumps to `error` if `iter` is NULL. */
1756 }
1757 }
1758
Yury Selivanov75445082015-05-11 22:57:16 -04001759 SET_TOP(iter); /* Even if it's NULL */
1760
1761 if (iter == NULL) {
1762 goto error;
1763 }
1764
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001765 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001766 DISPATCH();
1767 }
1768
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 TARGET(YIELD_FROM) {
1770 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001771 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001773 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1774 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001775 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001776 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001778 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001779 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001780 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001781 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 Py_DECREF(v);
1783 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001784 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001785 if (tstate->c_tracefunc != NULL
1786 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001787 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001788 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 if (err < 0)
1790 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001791 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 SET_TOP(val);
1793 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001794 }
Martin Panter95f53c12016-07-18 08:23:26 +00001795 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001796 f->f_stacktop = stack_pointer;
1797 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001798 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001799 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001800 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001801 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001806
1807 if (co->co_flags & CO_ASYNC_GENERATOR) {
1808 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1809 Py_DECREF(retval);
1810 if (w == NULL) {
1811 retval = NULL;
1812 goto error;
1813 }
1814 retval = w;
1815 }
1816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 f->f_stacktop = stack_pointer;
1818 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001821
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 TARGET(POP_EXCEPT) {
1823 PyTryBlock *b = PyFrame_BlockPop(f);
1824 if (b->b_type != EXCEPT_HANDLER) {
1825 PyErr_SetString(PyExc_SystemError,
1826 "popped block is not an except handler");
1827 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001829 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001832
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001833 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 TARGET(POP_BLOCK) {
1835 PyTryBlock *b = PyFrame_BlockPop(f);
1836 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 TARGET(END_FINALLY) {
1842 PyObject *status = POP();
1843 if (PyLong_Check(status)) {
1844 why = (enum why_code) PyLong_AS_LONG(status);
1845 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 if (why == WHY_RETURN ||
1847 why == WHY_CONTINUE)
1848 retval = POP();
1849 if (why == WHY_SILENCED) {
1850 /* An exception was silenced by 'with', we must
1851 manually unwind the EXCEPT_HANDLER block which was
1852 created when the exception was caught, otherwise
1853 the stack will be in an inconsistent state. */
1854 PyTryBlock *b = PyFrame_BlockPop(f);
1855 assert(b->b_type == EXCEPT_HANDLER);
1856 UNWIND_EXCEPT_HANDLER(b);
1857 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 Py_DECREF(status);
1859 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 Py_DECREF(status);
1862 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 else if (PyExceptionClass_Check(status)) {
1865 PyObject *exc = POP();
1866 PyObject *tb = POP();
1867 PyErr_Restore(status, exc, tb);
1868 why = WHY_EXCEPTION;
1869 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 PyErr_SetString(PyExc_SystemError,
1873 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 Py_DECREF(status);
1875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 Py_DECREF(status);
1878 DISPATCH();
1879 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001882 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001883
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001885 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1887 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001888 PyErr_SetString(PyExc_NameError,
1889 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001891 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001893 }
1894 else {
1895 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1896 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001897 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1899 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001900 if (PyErr_ExceptionMatches(PyExc_KeyError))
1901 PyErr_SetString(PyExc_NameError,
1902 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001907 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 TARGET(STORE_NAME) {
1911 PyObject *name = GETITEM(names, oparg);
1912 PyObject *v = POP();
1913 PyObject *ns = f->f_locals;
1914 int err;
1915 if (ns == NULL) {
1916 PyErr_Format(PyExc_SystemError,
1917 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 if (PyDict_CheckExact(ns))
1922 err = PyDict_SetItem(ns, name, v);
1923 else
1924 err = PyObject_SetItem(ns, name, v);
1925 Py_DECREF(v);
1926 if (err != 0)
1927 goto error;
1928 DISPATCH();
1929 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 TARGET(DELETE_NAME) {
1932 PyObject *name = GETITEM(names, oparg);
1933 PyObject *ns = f->f_locals;
1934 int err;
1935 if (ns == NULL) {
1936 PyErr_Format(PyExc_SystemError,
1937 "no locals when deleting %R", name);
1938 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 err = PyObject_DelItem(ns, name);
1941 if (err != 0) {
1942 format_exc_check_arg(PyExc_NameError,
1943 NAME_ERROR_MSG,
1944 name);
1945 goto error;
1946 }
1947 DISPATCH();
1948 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001949
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001950 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 TARGET(UNPACK_SEQUENCE) {
1952 PyObject *seq = POP(), *item, **items;
1953 if (PyTuple_CheckExact(seq) &&
1954 PyTuple_GET_SIZE(seq) == oparg) {
1955 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 item = items[oparg];
1958 Py_INCREF(item);
1959 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 } else if (PyList_CheckExact(seq) &&
1962 PyList_GET_SIZE(seq) == oparg) {
1963 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 item = items[oparg];
1966 Py_INCREF(item);
1967 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 stack_pointer + oparg)) {
1971 STACKADJ(oparg);
1972 } else {
1973 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 Py_DECREF(seq);
1975 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001978 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001980
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 TARGET(UNPACK_EX) {
1982 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1983 PyObject *seq = POP();
1984
1985 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
1986 stack_pointer + totalargs)) {
1987 stack_pointer += totalargs;
1988 } else {
1989 Py_DECREF(seq);
1990 goto error;
1991 }
1992 Py_DECREF(seq);
1993 DISPATCH();
1994 }
1995
1996 TARGET(STORE_ATTR) {
1997 PyObject *name = GETITEM(names, oparg);
1998 PyObject *owner = TOP();
1999 PyObject *v = SECOND();
2000 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 Py_DECREF(owner);
2005 if (err != 0)
2006 goto error;
2007 DISPATCH();
2008 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002009
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002010 TARGET(DELETE_ATTR) {
2011 PyObject *name = GETITEM(names, oparg);
2012 PyObject *owner = POP();
2013 int err;
2014 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2015 Py_DECREF(owner);
2016 if (err != 0)
2017 goto error;
2018 DISPATCH();
2019 }
2020
2021 TARGET(STORE_GLOBAL) {
2022 PyObject *name = GETITEM(names, oparg);
2023 PyObject *v = POP();
2024 int err;
2025 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 if (err != 0)
2028 goto error;
2029 DISPATCH();
2030 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002031
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 TARGET(DELETE_GLOBAL) {
2033 PyObject *name = GETITEM(names, oparg);
2034 int err;
2035 err = PyDict_DelItem(f->f_globals, name);
2036 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002038 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002040 }
2041 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 TARGET(LOAD_NAME) {
2045 PyObject *name = GETITEM(names, oparg);
2046 PyObject *locals = f->f_locals;
2047 PyObject *v;
2048 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 "no locals when loading %R", name);
2051 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 if (PyDict_CheckExact(locals)) {
2054 v = PyDict_GetItem(locals, name);
2055 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 }
2057 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002059 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002060 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2061 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 PyErr_Clear();
2063 }
2064 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 if (v == NULL) {
2066 v = PyDict_GetItem(f->f_globals, name);
2067 Py_XINCREF(v);
2068 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002069 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 v = PyDict_GetItem(f->f_builtins, name);
2071 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002072 format_exc_check_arg(
2073 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 NAME_ERROR_MSG, name);
2075 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002076 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002078 }
2079 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 v = PyObject_GetItem(f->f_builtins, name);
2081 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002082 if (PyErr_ExceptionMatches(PyExc_KeyError))
2083 format_exc_check_arg(
2084 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 NAME_ERROR_MSG, name);
2086 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002087 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 TARGET(LOAD_GLOBAL) {
2096 PyObject *name = GETITEM(names, oparg);
2097 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002098 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002099 && PyDict_CheckExact(f->f_builtins))
2100 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002102 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 name);
2104 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002105 if (!_PyErr_OCCURRED()) {
2106 /* _PyDict_LoadGlobal() returns NULL without raising
2107 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002108 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002109 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002110 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002115 else {
2116 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002117
2118 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 v = PyObject_GetItem(f->f_globals, name);
2120 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002121 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2122 goto error;
2123 PyErr_Clear();
2124
Victor Stinnerb4efc962015-11-20 09:24:02 +01002125 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 v = PyObject_GetItem(f->f_builtins, name);
2127 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002128 if (PyErr_ExceptionMatches(PyExc_KeyError))
2129 format_exc_check_arg(
2130 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002131 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002133 }
2134 }
2135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002139
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 TARGET(DELETE_FAST) {
2141 PyObject *v = GETLOCAL(oparg);
2142 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 SETLOCAL(oparg, NULL);
2144 DISPATCH();
2145 }
2146 format_exc_check_arg(
2147 PyExc_UnboundLocalError,
2148 UNBOUNDLOCAL_ERROR_MSG,
2149 PyTuple_GetItem(co->co_varnames, oparg)
2150 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 goto error;
2152 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002153
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 TARGET(DELETE_DEREF) {
2155 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002156 PyObject *oldobj = PyCell_GET(cell);
2157 if (oldobj != NULL) {
2158 PyCell_SET(cell, NULL);
2159 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002160 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002161 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002162 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 goto error;
2164 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002165
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 TARGET(LOAD_CLOSURE) {
2167 PyObject *cell = freevars[oparg];
2168 Py_INCREF(cell);
2169 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002172
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002173 TARGET(LOAD_CLASSDEREF) {
2174 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002175 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002176 assert(locals);
2177 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2178 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2179 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2180 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2181 if (PyDict_CheckExact(locals)) {
2182 value = PyDict_GetItem(locals, name);
2183 Py_XINCREF(value);
2184 }
2185 else {
2186 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002187 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002188 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2189 goto error;
2190 PyErr_Clear();
2191 }
2192 }
2193 if (!value) {
2194 PyObject *cell = freevars[oparg];
2195 value = PyCell_GET(cell);
2196 if (value == NULL) {
2197 format_exc_unbound(co, oparg);
2198 goto error;
2199 }
2200 Py_INCREF(value);
2201 }
2202 PUSH(value);
2203 DISPATCH();
2204 }
2205
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 TARGET(LOAD_DEREF) {
2207 PyObject *cell = freevars[oparg];
2208 PyObject *value = PyCell_GET(cell);
2209 if (value == NULL) {
2210 format_exc_unbound(co, oparg);
2211 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 Py_INCREF(value);
2214 PUSH(value);
2215 DISPATCH();
2216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002217
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 TARGET(STORE_DEREF) {
2219 PyObject *v = POP();
2220 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002221 PyObject *oldobj = PyCell_GET(cell);
2222 PyCell_SET(cell, v);
2223 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 DISPATCH();
2225 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002226
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002227 TARGET(BUILD_STRING) {
2228 PyObject *str;
2229 PyObject *empty = PyUnicode_New(0, 0);
2230 if (empty == NULL) {
2231 goto error;
2232 }
2233 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2234 Py_DECREF(empty);
2235 if (str == NULL)
2236 goto error;
2237 while (--oparg >= 0) {
2238 PyObject *item = POP();
2239 Py_DECREF(item);
2240 }
2241 PUSH(str);
2242 DISPATCH();
2243 }
2244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 TARGET(BUILD_TUPLE) {
2246 PyObject *tup = PyTuple_New(oparg);
2247 if (tup == NULL)
2248 goto error;
2249 while (--oparg >= 0) {
2250 PyObject *item = POP();
2251 PyTuple_SET_ITEM(tup, oparg, item);
2252 }
2253 PUSH(tup);
2254 DISPATCH();
2255 }
2256
2257 TARGET(BUILD_LIST) {
2258 PyObject *list = PyList_New(oparg);
2259 if (list == NULL)
2260 goto error;
2261 while (--oparg >= 0) {
2262 PyObject *item = POP();
2263 PyList_SET_ITEM(list, oparg, item);
2264 }
2265 PUSH(list);
2266 DISPATCH();
2267 }
2268
Serhiy Storchaka73442852016-10-02 10:33:46 +03002269 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002270 TARGET(BUILD_TUPLE_UNPACK)
2271 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002272 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002273 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002274 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002275 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002276
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002277 if (sum == NULL)
2278 goto error;
2279
2280 for (i = oparg; i > 0; i--) {
2281 PyObject *none_val;
2282
2283 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2284 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002285 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002286 PyErr_ExceptionMatches(PyExc_TypeError))
2287 {
2288 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002289 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002290 Py_DECREF(sum);
2291 goto error;
2292 }
2293 Py_DECREF(none_val);
2294 }
2295
2296 if (convert_to_tuple) {
2297 return_value = PyList_AsTuple(sum);
2298 Py_DECREF(sum);
2299 if (return_value == NULL)
2300 goto error;
2301 }
2302 else {
2303 return_value = sum;
2304 }
2305
2306 while (oparg--)
2307 Py_DECREF(POP());
2308 PUSH(return_value);
2309 DISPATCH();
2310 }
2311
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 TARGET(BUILD_SET) {
2313 PyObject *set = PySet_New(NULL);
2314 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002315 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 if (set == NULL)
2317 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002318 for (i = oparg; i > 0; i--) {
2319 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 if (err == 0)
2321 err = PySet_Add(set, item);
2322 Py_DECREF(item);
2323 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002324 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 if (err != 0) {
2326 Py_DECREF(set);
2327 goto error;
2328 }
2329 PUSH(set);
2330 DISPATCH();
2331 }
2332
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002333 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002334 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002335 PyObject *sum = PySet_New(NULL);
2336 if (sum == NULL)
2337 goto error;
2338
2339 for (i = oparg; i > 0; i--) {
2340 if (_PySet_Update(sum, PEEK(i)) < 0) {
2341 Py_DECREF(sum);
2342 goto error;
2343 }
2344 }
2345
2346 while (oparg--)
2347 Py_DECREF(POP());
2348 PUSH(sum);
2349 DISPATCH();
2350 }
2351
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002353 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2355 if (map == NULL)
2356 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002357 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002358 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002359 PyObject *key = PEEK(2*i);
2360 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002361 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002362 if (err != 0) {
2363 Py_DECREF(map);
2364 goto error;
2365 }
2366 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002367
2368 while (oparg--) {
2369 Py_DECREF(POP());
2370 Py_DECREF(POP());
2371 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 PUSH(map);
2373 DISPATCH();
2374 }
2375
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002376 TARGET(SETUP_ANNOTATIONS) {
2377 _Py_IDENTIFIER(__annotations__);
2378 int err;
2379 PyObject *ann_dict;
2380 if (f->f_locals == NULL) {
2381 PyErr_Format(PyExc_SystemError,
2382 "no locals found when setting up annotations");
2383 goto error;
2384 }
2385 /* check if __annotations__ in locals()... */
2386 if (PyDict_CheckExact(f->f_locals)) {
2387 ann_dict = _PyDict_GetItemId(f->f_locals,
2388 &PyId___annotations__);
2389 if (ann_dict == NULL) {
2390 /* ...if not, create a new one */
2391 ann_dict = PyDict_New();
2392 if (ann_dict == NULL) {
2393 goto error;
2394 }
2395 err = _PyDict_SetItemId(f->f_locals,
2396 &PyId___annotations__, ann_dict);
2397 Py_DECREF(ann_dict);
2398 if (err != 0) {
2399 goto error;
2400 }
2401 }
2402 }
2403 else {
2404 /* do the same if locals() is not a dict */
2405 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2406 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002407 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002408 }
2409 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2410 if (ann_dict == NULL) {
2411 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2412 goto error;
2413 }
2414 PyErr_Clear();
2415 ann_dict = PyDict_New();
2416 if (ann_dict == NULL) {
2417 goto error;
2418 }
2419 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2420 Py_DECREF(ann_dict);
2421 if (err != 0) {
2422 goto error;
2423 }
2424 }
2425 else {
2426 Py_DECREF(ann_dict);
2427 }
2428 }
2429 DISPATCH();
2430 }
2431
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002432 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002433 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002434 PyObject *map;
2435 PyObject *keys = TOP();
2436 if (!PyTuple_CheckExact(keys) ||
2437 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2438 PyErr_SetString(PyExc_SystemError,
2439 "bad BUILD_CONST_KEY_MAP keys argument");
2440 goto error;
2441 }
2442 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2443 if (map == NULL) {
2444 goto error;
2445 }
2446 for (i = oparg; i > 0; i--) {
2447 int err;
2448 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2449 PyObject *value = PEEK(i + 1);
2450 err = PyDict_SetItem(map, key, value);
2451 if (err != 0) {
2452 Py_DECREF(map);
2453 goto error;
2454 }
2455 }
2456
2457 Py_DECREF(POP());
2458 while (oparg--) {
2459 Py_DECREF(POP());
2460 }
2461 PUSH(map);
2462 DISPATCH();
2463 }
2464
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002465 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002466 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002467 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002468 if (sum == NULL)
2469 goto error;
2470
2471 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002472 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002473 if (PyDict_Update(sum, arg) < 0) {
2474 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2475 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002476 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002477 arg->ob_type->tp_name);
2478 }
2479 Py_DECREF(sum);
2480 goto error;
2481 }
2482 }
2483
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002484 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002485 Py_DECREF(POP());
2486 PUSH(sum);
2487 DISPATCH();
2488 }
2489
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002490 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2491 Py_ssize_t i;
2492 PyObject *sum = PyDict_New();
2493 if (sum == NULL)
2494 goto error;
2495
2496 for (i = oparg; i > 0; i--) {
2497 PyObject *arg = PEEK(i);
2498 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2499 PyObject *func = PEEK(2 + oparg);
2500 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002501 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002502 }
2503 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2504 PyObject *exc, *val, *tb;
2505 PyErr_Fetch(&exc, &val, &tb);
2506 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2507 PyObject *key = PyTuple_GET_ITEM(val, 0);
2508 if (!PyUnicode_Check(key)) {
2509 PyErr_Format(PyExc_TypeError,
2510 "%.200s%.200s keywords must be strings",
2511 PyEval_GetFuncName(func),
2512 PyEval_GetFuncDesc(func));
2513 } else {
2514 PyErr_Format(PyExc_TypeError,
2515 "%.200s%.200s got multiple "
2516 "values for keyword argument '%U'",
2517 PyEval_GetFuncName(func),
2518 PyEval_GetFuncDesc(func),
2519 key);
2520 }
2521 Py_XDECREF(exc);
2522 Py_XDECREF(val);
2523 Py_XDECREF(tb);
2524 }
2525 else {
2526 PyErr_Restore(exc, val, tb);
2527 }
2528 }
2529 Py_DECREF(sum);
2530 goto error;
2531 }
2532 }
2533
2534 while (oparg--)
2535 Py_DECREF(POP());
2536 PUSH(sum);
2537 DISPATCH();
2538 }
2539
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 TARGET(MAP_ADD) {
2541 PyObject *key = TOP();
2542 PyObject *value = SECOND();
2543 PyObject *map;
2544 int err;
2545 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002546 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002548 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 Py_DECREF(value);
2550 Py_DECREF(key);
2551 if (err != 0)
2552 goto error;
2553 PREDICT(JUMP_ABSOLUTE);
2554 DISPATCH();
2555 }
2556
2557 TARGET(LOAD_ATTR) {
2558 PyObject *name = GETITEM(names, oparg);
2559 PyObject *owner = TOP();
2560 PyObject *res = PyObject_GetAttr(owner, name);
2561 Py_DECREF(owner);
2562 SET_TOP(res);
2563 if (res == NULL)
2564 goto error;
2565 DISPATCH();
2566 }
2567
2568 TARGET(COMPARE_OP) {
2569 PyObject *right = POP();
2570 PyObject *left = TOP();
2571 PyObject *res = cmp_outcome(oparg, left, right);
2572 Py_DECREF(left);
2573 Py_DECREF(right);
2574 SET_TOP(res);
2575 if (res == NULL)
2576 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 PREDICT(POP_JUMP_IF_FALSE);
2578 PREDICT(POP_JUMP_IF_TRUE);
2579 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002581
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002582 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002584 PyObject *fromlist = POP();
2585 PyObject *level = TOP();
2586 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002587 res = import_name(f, name, fromlist, level);
2588 Py_DECREF(level);
2589 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 SET_TOP(res);
2591 if (res == NULL)
2592 goto error;
2593 DISPATCH();
2594 }
2595
2596 TARGET(IMPORT_STAR) {
2597 PyObject *from = POP(), *locals;
2598 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002599 if (PyFrame_FastToLocalsWithError(f) < 0) {
2600 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002601 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002602 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002603
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 locals = f->f_locals;
2605 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 PyErr_SetString(PyExc_SystemError,
2607 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002608 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 Py_DECREF(from);
2614 if (err != 0)
2615 goto error;
2616 DISPATCH();
2617 }
Guido van Rossum25831651993-05-19 14:50:45 +00002618
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 TARGET(IMPORT_FROM) {
2620 PyObject *name = GETITEM(names, oparg);
2621 PyObject *from = TOP();
2622 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 PUSH(res);
2625 if (res == NULL)
2626 goto error;
2627 DISPATCH();
2628 }
Thomas Wouters52152252000-08-17 22:55:00 +00002629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 JUMPBY(oparg);
2632 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002634
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002635 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 TARGET(POP_JUMP_IF_FALSE) {
2637 PyObject *cond = POP();
2638 int err;
2639 if (cond == Py_True) {
2640 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 FAST_DISPATCH();
2642 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002643 if (cond == Py_False) {
2644 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 JUMPTO(oparg);
2646 FAST_DISPATCH();
2647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 err = PyObject_IsTrue(cond);
2649 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002651 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 else if (err == 0)
2653 JUMPTO(oparg);
2654 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002657 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002658
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002659 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 TARGET(POP_JUMP_IF_TRUE) {
2661 PyObject *cond = POP();
2662 int err;
2663 if (cond == Py_False) {
2664 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 FAST_DISPATCH();
2666 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 if (cond == Py_True) {
2668 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 JUMPTO(oparg);
2670 FAST_DISPATCH();
2671 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 err = PyObject_IsTrue(cond);
2673 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 JUMPTO(oparg);
2676 }
2677 else if (err == 0)
2678 ;
2679 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002683
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 TARGET(JUMP_IF_FALSE_OR_POP) {
2685 PyObject *cond = TOP();
2686 int err;
2687 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 FAST_DISPATCH();
2691 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 JUMPTO(oparg);
2694 FAST_DISPATCH();
2695 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 if (err > 0) {
2698 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 }
2701 else if (err == 0)
2702 JUMPTO(oparg);
2703 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002707
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 TARGET(JUMP_IF_TRUE_OR_POP) {
2709 PyObject *cond = TOP();
2710 int err;
2711 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 FAST_DISPATCH();
2715 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 JUMPTO(oparg);
2718 FAST_DISPATCH();
2719 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 JUMPTO(oparg);
2723 }
2724 else if (err == 0) {
2725 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 }
2728 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002732
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002733 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002736#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 /* Enabling this path speeds-up all while and for-loops by bypassing
2738 the per-loop checks for signals. By default, this should be turned-off
2739 because it prevents detection of a control-break in tight loops like
2740 "while 1: pass". Compile with this option turned-on when you need
2741 the speed-up and do not need break checking inside tight loops (ones
2742 that contain only instructions ending with FAST_DISPATCH).
2743 */
2744 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002745#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002747#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002749
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002753 PyObject *iter = PyObject_GetIter(iterable);
2754 Py_DECREF(iterable);
2755 SET_TOP(iter);
2756 if (iter == NULL)
2757 goto error;
2758 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002759 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002760 DISPATCH();
2761 }
2762
2763 TARGET(GET_YIELD_FROM_ITER) {
2764 /* before: [obj]; after [getiter(obj)] */
2765 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002766 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002767 if (PyCoro_CheckExact(iterable)) {
2768 /* `iterable` is a coroutine */
2769 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2770 /* and it is used in a 'yield from' expression of a
2771 regular generator. */
2772 Py_DECREF(iterable);
2773 SET_TOP(NULL);
2774 PyErr_SetString(PyExc_TypeError,
2775 "cannot 'yield from' a coroutine object "
2776 "in a non-coroutine generator");
2777 goto error;
2778 }
2779 }
2780 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002781 /* `iterable` is not a generator. */
2782 iter = PyObject_GetIter(iterable);
2783 Py_DECREF(iterable);
2784 SET_TOP(iter);
2785 if (iter == NULL)
2786 goto error;
2787 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002788 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 DISPATCH();
2790 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002791
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002792 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 PyObject *iter = TOP();
2796 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2797 if (next != NULL) {
2798 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 PREDICT(STORE_FAST);
2800 PREDICT(UNPACK_SEQUENCE);
2801 DISPATCH();
2802 }
2803 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2805 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002806 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002807 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 PyErr_Clear();
2809 }
2810 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 STACKADJ(-1);
2812 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002814 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002817
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 why = WHY_BREAK;
2820 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002822
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 if (retval == NULL)
2826 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 why = WHY_CONTINUE;
2828 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002830
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002831 TARGET(SETUP_LOOP)
2832 TARGET(SETUP_EXCEPT)
2833 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 /* NOTE: If you add any new block-setup opcodes that
2835 are not try/except/finally handlers, you may need
2836 to update the PyGen_NeedsFinalizing() function.
2837 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2840 STACK_LEVEL());
2841 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002843
Yury Selivanov75445082015-05-11 22:57:16 -04002844 TARGET(BEFORE_ASYNC_WITH) {
2845 _Py_IDENTIFIER(__aexit__);
2846 _Py_IDENTIFIER(__aenter__);
2847
2848 PyObject *mgr = TOP();
2849 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2850 *enter;
2851 PyObject *res;
2852 if (exit == NULL)
2853 goto error;
2854 SET_TOP(exit);
2855 enter = special_lookup(mgr, &PyId___aenter__);
2856 Py_DECREF(mgr);
2857 if (enter == NULL)
2858 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002859 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002860 Py_DECREF(enter);
2861 if (res == NULL)
2862 goto error;
2863 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002864 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002865 DISPATCH();
2866 }
2867
2868 TARGET(SETUP_ASYNC_WITH) {
2869 PyObject *res = POP();
2870 /* Setup the finally block before pushing the result
2871 of __aenter__ on the stack. */
2872 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2873 STACK_LEVEL());
2874 PUSH(res);
2875 DISPATCH();
2876 }
2877
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002879 _Py_IDENTIFIER(__exit__);
2880 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002882 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002884 if (enter == NULL)
2885 goto error;
2886 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002887 if (exit == NULL) {
2888 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002890 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002893 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 Py_DECREF(enter);
2895 if (res == NULL)
2896 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 /* Setup the finally block before pushing the result
2898 of __enter__ on the stack. */
2899 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2900 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002901
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 DISPATCH();
2904 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002905
Yury Selivanov75445082015-05-11 22:57:16 -04002906 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002907 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 how/why we entered the finally clause:
2909 - TOP = None
2910 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2911 - TOP = WHY_*; no retval below it
2912 - (TOP, SECOND, THIRD) = exc_info()
2913 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2914 Below them is EXIT, the context.__exit__ bound method.
2915 In the last case, we must call
2916 EXIT(TOP, SECOND, THIRD)
2917 otherwise we must call
2918 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002919
Benjamin Peterson8f169482013-10-29 22:25:06 -04002920 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002922 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 In addition, if the stack represents an exception,
2926 *and* the function call returns a 'true' value, we
2927 push WHY_SILENCED onto the stack. END_FINALLY will
2928 then not re-raise the exception. (But non-local
2929 gotos should still be resumed.)
2930 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002931
Victor Stinner842cfff2016-12-01 14:45:31 +01002932 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01002934 PyObject *exc, *val, *tb, *res;
2935
2936 val = tb = Py_None;
2937 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 (void)POP();
2940 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 else if (PyLong_Check(exc)) {
2944 STACKADJ(-1);
2945 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 case WHY_RETURN:
2947 case WHY_CONTINUE:
2948 /* Retval in TOP. */
2949 exit_func = SECOND();
2950 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 break;
2953 default:
2954 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 break;
2957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
2960 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 val = SECOND();
2964 tb = THIRD();
2965 tp2 = FOURTH();
2966 exc2 = PEEK(5);
2967 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 SET_VALUE(7, tb2);
2970 SET_VALUE(6, exc2);
2971 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2973 SET_FOURTH(NULL);
2974 /* We just shifted the stack down, so we have
2975 to tell the except handler block that the
2976 values are lower than it expects. */
2977 block = &f->f_blockstack[f->f_iblock - 1];
2978 assert(block->b_type == EXCEPT_HANDLER);
2979 block->b_level--;
2980 }
Victor Stinner842cfff2016-12-01 14:45:31 +01002981
2982 stack[0] = exc;
2983 stack[1] = val;
2984 stack[2] = tb;
2985 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 if (res == NULL)
2988 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002989
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10002990 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04002991 PUSH(exc);
2992 PUSH(res);
2993 PREDICT(WITH_CLEANUP_FINISH);
2994 DISPATCH();
2995 }
2996
2997 PREDICTED(WITH_CLEANUP_FINISH);
2998 TARGET(WITH_CLEANUP_FINISH) {
2999 PyObject *res = POP();
3000 PyObject *exc = POP();
3001 int err;
3002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 if (exc != Py_None)
3004 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 else
3006 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003007
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003009 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 /* There was an exception and a True return */
3015 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3016 }
3017 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003020
Yury Selivanovf2392132016-12-13 19:03:51 -05003021 TARGET(LOAD_METHOD) {
3022 /* Designed to work in tamdem with CALL_METHOD. */
3023 PyObject *name = GETITEM(names, oparg);
3024 PyObject *obj = TOP();
3025 PyObject *meth = NULL;
3026
3027 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3028
Yury Selivanovf2392132016-12-13 19:03:51 -05003029 if (meth == NULL) {
3030 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003031 goto error;
3032 }
3033
3034 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003035 /* We can bypass temporary bound method object.
3036 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003037
INADA Naoki015bce62017-01-16 17:23:30 +09003038 meth | self | arg1 | ... | argN
3039 */
3040 SET_TOP(meth);
3041 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003042 }
3043 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003044 /* meth is not an unbound method (but a regular attr, or
3045 something was returned by a descriptor protocol). Set
3046 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003047 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003048
3049 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003050 */
INADA Naoki015bce62017-01-16 17:23:30 +09003051 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003052 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003053 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003054 }
3055 DISPATCH();
3056 }
3057
3058 TARGET(CALL_METHOD) {
3059 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003060 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003061
3062 sp = stack_pointer;
3063
INADA Naoki015bce62017-01-16 17:23:30 +09003064 meth = PEEK(oparg + 2);
3065 if (meth == NULL) {
3066 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3067 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003068
3069 Stack layout:
3070
INADA Naoki015bce62017-01-16 17:23:30 +09003071 ... | NULL | callable | arg1 | ... | argN
3072 ^- TOP()
3073 ^- (-oparg)
3074 ^- (-oparg-1)
3075 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003076
Ville Skyttä49b27342017-08-03 09:00:59 +03003077 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003078 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003079 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003080 res = call_function(&sp, oparg, NULL);
3081 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003082 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003083 }
3084 else {
3085 /* This is a method call. Stack layout:
3086
INADA Naoki015bce62017-01-16 17:23:30 +09003087 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003088 ^- TOP()
3089 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003090 ^- (-oparg-1)
3091 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003092
INADA Naoki015bce62017-01-16 17:23:30 +09003093 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003094 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003095 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003096 */
3097 res = call_function(&sp, oparg + 1, NULL);
3098 stack_pointer = sp;
3099 }
3100
3101 PUSH(res);
3102 if (res == NULL)
3103 goto error;
3104 DISPATCH();
3105 }
3106
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003107 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 TARGET(CALL_FUNCTION) {
3109 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003111 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003114 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003119
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003120 TARGET(CALL_FUNCTION_KW) {
3121 PyObject **sp, *res, *names;
3122
3123 names = POP();
3124 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003126 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003129 Py_DECREF(names);
3130
3131 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003133 }
3134 DISPATCH();
3135 }
3136
3137 TARGET(CALL_FUNCTION_EX) {
3138 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003139 if (oparg & 0x01) {
3140 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003141 if (!PyDict_CheckExact(kwargs)) {
3142 PyObject *d = PyDict_New();
3143 if (d == NULL)
3144 goto error;
3145 if (PyDict_Update(d, kwargs) != 0) {
3146 Py_DECREF(d);
3147 /* PyDict_Update raises attribute
3148 * error (percolated from an attempt
3149 * to get 'keys' attribute) instead of
3150 * a type error if its second argument
3151 * is not a mapping.
3152 */
3153 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003154 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003155 }
Victor Stinnereece2222016-09-12 11:16:37 +02003156 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003157 goto error;
3158 }
3159 Py_DECREF(kwargs);
3160 kwargs = d;
3161 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003162 assert(PyDict_CheckExact(kwargs));
3163 }
3164 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003165 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003166 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003167 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003168 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003169 goto error;
3170 }
3171 Py_SETREF(callargs, PySequence_Tuple(callargs));
3172 if (callargs == NULL) {
3173 goto error;
3174 }
3175 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003176 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003177
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003178 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003179 Py_DECREF(func);
3180 Py_DECREF(callargs);
3181 Py_XDECREF(kwargs);
3182
3183 SET_TOP(result);
3184 if (result == NULL) {
3185 goto error;
3186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003189
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003190 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003191 PyObject *qualname = POP();
3192 PyObject *codeobj = POP();
3193 PyFunctionObject *func = (PyFunctionObject *)
3194 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003195
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003196 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003198 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003201
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003202 if (oparg & 0x08) {
3203 assert(PyTuple_CheckExact(TOP()));
3204 func ->func_closure = POP();
3205 }
3206 if (oparg & 0x04) {
3207 assert(PyDict_CheckExact(TOP()));
3208 func->func_annotations = POP();
3209 }
3210 if (oparg & 0x02) {
3211 assert(PyDict_CheckExact(TOP()));
3212 func->func_kwdefaults = POP();
3213 }
3214 if (oparg & 0x01) {
3215 assert(PyTuple_CheckExact(TOP()));
3216 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003218
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003219 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003222
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003223 TARGET(BUILD_SLICE) {
3224 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 step = NULL;
3229 stop = POP();
3230 start = TOP();
3231 slice = PySlice_New(start, stop, step);
3232 Py_DECREF(start);
3233 Py_DECREF(stop);
3234 Py_XDECREF(step);
3235 SET_TOP(slice);
3236 if (slice == NULL)
3237 goto error;
3238 DISPATCH();
3239 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003240
Eric V. Smitha78c7952015-11-03 12:45:05 -05003241 TARGET(FORMAT_VALUE) {
3242 /* Handles f-string value formatting. */
3243 PyObject *result;
3244 PyObject *fmt_spec;
3245 PyObject *value;
3246 PyObject *(*conv_fn)(PyObject *);
3247 int which_conversion = oparg & FVC_MASK;
3248 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3249
3250 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003251 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003252
3253 /* See if any conversion is specified. */
3254 switch (which_conversion) {
3255 case FVC_STR: conv_fn = PyObject_Str; break;
3256 case FVC_REPR: conv_fn = PyObject_Repr; break;
3257 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3258
3259 /* Must be 0 (meaning no conversion), since only four
3260 values are allowed by (oparg & FVC_MASK). */
3261 default: conv_fn = NULL; break;
3262 }
3263
3264 /* If there's a conversion function, call it and replace
3265 value with that result. Otherwise, just use value,
3266 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003267 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003268 result = conv_fn(value);
3269 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003270 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003271 Py_XDECREF(fmt_spec);
3272 goto error;
3273 }
3274 value = result;
3275 }
3276
3277 /* If value is a unicode object, and there's no fmt_spec,
3278 then we know the result of format(value) is value
3279 itself. In that case, skip calling format(). I plan to
3280 move this optimization in to PyObject_Format()
3281 itself. */
3282 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3283 /* Do nothing, just transfer ownership to result. */
3284 result = value;
3285 } else {
3286 /* Actually call format(). */
3287 result = PyObject_Format(value, fmt_spec);
3288 Py_DECREF(value);
3289 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003290 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003291 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003292 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003293 }
3294
Eric V. Smith135d5f42016-02-05 18:23:08 -05003295 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003296 DISPATCH();
3297 }
3298
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003300 int oldoparg = oparg;
3301 NEXTOPARG();
3302 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003305
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003306
Antoine Pitrou042b1282010-08-13 21:15:58 +00003307#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 default:
3311 fprintf(stderr,
3312 "XXX lineno: %d, opcode: %d\n",
3313 PyFrame_GetLineNumber(f),
3314 opcode);
3315 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003319
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 /* This should never be reached. Every opcode should end with DISPATCH()
3321 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003322 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003323
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003325
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 assert(why == WHY_NOT);
3327 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003328
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003329 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003330#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 if (!PyErr_Occurred())
3332 PyErr_SetString(PyExc_SystemError,
3333 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003334#else
3335 assert(PyErr_Occurred());
3336#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003337
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 /* Log traceback info. */
3339 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003340
Benjamin Peterson51f46162013-01-23 08:38:47 -05003341 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003342 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3343 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003344
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003345fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 assert(why != WHY_NOT);
3347
3348 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 while (why != WHY_NOT && f->f_iblock > 0) {
3350 /* Peek at the current block. */
3351 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 assert(why != WHY_YIELD);
3354 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3355 why = WHY_NOT;
3356 JUMPTO(PyLong_AS_LONG(retval));
3357 Py_DECREF(retval);
3358 break;
3359 }
3360 /* Now we have to pop the block. */
3361 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 if (b->b_type == EXCEPT_HANDLER) {
3364 UNWIND_EXCEPT_HANDLER(b);
3365 continue;
3366 }
3367 UNWIND_BLOCK(b);
3368 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3369 why = WHY_NOT;
3370 JUMPTO(b->b_handler);
3371 break;
3372 }
3373 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3374 || b->b_type == SETUP_FINALLY)) {
3375 PyObject *exc, *val, *tb;
3376 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003377 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 /* Beware, this invalidates all b->b_* fields */
3379 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003380 PUSH(exc_info->exc_traceback);
3381 PUSH(exc_info->exc_value);
3382 if (exc_info->exc_type != NULL) {
3383 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 }
3385 else {
3386 Py_INCREF(Py_None);
3387 PUSH(Py_None);
3388 }
3389 PyErr_Fetch(&exc, &val, &tb);
3390 /* Make the raw exception data
3391 available to the handler,
3392 so a program can emulate the
3393 Python main loop. */
3394 PyErr_NormalizeException(
3395 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003396 if (tb != NULL)
3397 PyException_SetTraceback(val, tb);
3398 else
3399 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003401 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003403 exc_info->exc_value = val;
3404 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (tb == NULL)
3406 tb = Py_None;
3407 Py_INCREF(tb);
3408 PUSH(tb);
3409 PUSH(val);
3410 PUSH(exc);
3411 why = WHY_NOT;
3412 JUMPTO(handler);
3413 break;
3414 }
3415 if (b->b_type == SETUP_FINALLY) {
3416 if (why & (WHY_RETURN | WHY_CONTINUE))
3417 PUSH(retval);
3418 PUSH(PyLong_FromLong((long)why));
3419 why = WHY_NOT;
3420 JUMPTO(b->b_handler);
3421 break;
3422 }
3423 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 if (why != WHY_NOT)
3428 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003429
Victor Stinnerace47d72013-07-18 01:41:08 +02003430 assert(!PyErr_Occurred());
3431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 assert(why != WHY_YIELD);
3435 /* Pop remaining stack entries. */
3436 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 PyObject *o = POP();
3438 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 if (why != WHY_RETURN)
3442 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003443
Victor Stinner4a7cc882015-03-06 23:35:27 +01003444 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003445
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003446fast_yield:
Benjamin Peterson83195c32011-07-03 13:44:00 -05003447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003449 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003451 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3452 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003454 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 why = WHY_EXCEPTION;
3456 }
3457 }
3458 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003459 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3460 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 PyTrace_RETURN, NULL);
3462 }
3463 }
3464 if (tstate->c_profilefunc) {
3465 if (why == WHY_EXCEPTION)
3466 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003467 tstate->c_profileobj,
3468 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003470 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3471 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003473 Py_CLEAR(retval);
Xiang Zhang997478e2018-01-29 11:32:12 +08003474 /* why = WHY_EXCEPTION; useless yet but cause compiler warnings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 }
3476 }
3477 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003480exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003481 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3482 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003484 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003486
Victor Stinnerefde1462015-03-21 15:04:43 +01003487 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003488}
3489
Benjamin Petersonb204a422011-06-05 22:04:07 -05003490static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003491format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3492{
3493 int err;
3494 Py_ssize_t len = PyList_GET_SIZE(names);
3495 PyObject *name_str, *comma, *tail, *tmp;
3496
3497 assert(PyList_CheckExact(names));
3498 assert(len >= 1);
3499 /* Deal with the joys of natural language. */
3500 switch (len) {
3501 case 1:
3502 name_str = PyList_GET_ITEM(names, 0);
3503 Py_INCREF(name_str);
3504 break;
3505 case 2:
3506 name_str = PyUnicode_FromFormat("%U and %U",
3507 PyList_GET_ITEM(names, len - 2),
3508 PyList_GET_ITEM(names, len - 1));
3509 break;
3510 default:
3511 tail = PyUnicode_FromFormat(", %U, and %U",
3512 PyList_GET_ITEM(names, len - 2),
3513 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003514 if (tail == NULL)
3515 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003516 /* Chop off the last two objects in the list. This shouldn't actually
3517 fail, but we can't be too careful. */
3518 err = PyList_SetSlice(names, len - 2, len, NULL);
3519 if (err == -1) {
3520 Py_DECREF(tail);
3521 return;
3522 }
3523 /* Stitch everything up into a nice comma-separated list. */
3524 comma = PyUnicode_FromString(", ");
3525 if (comma == NULL) {
3526 Py_DECREF(tail);
3527 return;
3528 }
3529 tmp = PyUnicode_Join(comma, names);
3530 Py_DECREF(comma);
3531 if (tmp == NULL) {
3532 Py_DECREF(tail);
3533 return;
3534 }
3535 name_str = PyUnicode_Concat(tmp, tail);
3536 Py_DECREF(tmp);
3537 Py_DECREF(tail);
3538 break;
3539 }
3540 if (name_str == NULL)
3541 return;
3542 PyErr_Format(PyExc_TypeError,
3543 "%U() missing %i required %s argument%s: %U",
3544 co->co_name,
3545 len,
3546 kind,
3547 len == 1 ? "" : "s",
3548 name_str);
3549 Py_DECREF(name_str);
3550}
3551
3552static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003553missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003554 PyObject **fastlocals)
3555{
Victor Stinner74319ae2016-08-25 00:04:09 +02003556 Py_ssize_t i, j = 0;
3557 Py_ssize_t start, end;
3558 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003559 const char *kind = positional ? "positional" : "keyword-only";
3560 PyObject *missing_names;
3561
3562 /* Compute the names of the arguments that are missing. */
3563 missing_names = PyList_New(missing);
3564 if (missing_names == NULL)
3565 return;
3566 if (positional) {
3567 start = 0;
3568 end = co->co_argcount - defcount;
3569 }
3570 else {
3571 start = co->co_argcount;
3572 end = start + co->co_kwonlyargcount;
3573 }
3574 for (i = start; i < end; i++) {
3575 if (GETLOCAL(i) == NULL) {
3576 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3577 PyObject *name = PyObject_Repr(raw);
3578 if (name == NULL) {
3579 Py_DECREF(missing_names);
3580 return;
3581 }
3582 PyList_SET_ITEM(missing_names, j++, name);
3583 }
3584 }
3585 assert(j == missing);
3586 format_missing(kind, co, missing_names);
3587 Py_DECREF(missing_names);
3588}
3589
3590static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003591too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3592 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003593{
3594 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003595 Py_ssize_t kwonly_given = 0;
3596 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003597 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003598 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003599
Benjamin Petersone109c702011-06-24 09:37:26 -05003600 assert((co->co_flags & CO_VARARGS) == 0);
3601 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003602 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3603 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003604 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003605 }
3606 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003607 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003608 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003609 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003610 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003611 }
3612 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003613 plural = (co_argcount != 1);
3614 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003615 }
3616 if (sig == NULL)
3617 return;
3618 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003619 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3620 kwonly_sig = PyUnicode_FromFormat(format,
3621 given != 1 ? "s" : "",
3622 kwonly_given,
3623 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003624 if (kwonly_sig == NULL) {
3625 Py_DECREF(sig);
3626 return;
3627 }
3628 }
3629 else {
3630 /* This will not fail. */
3631 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003632 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003633 }
3634 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003635 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003636 co->co_name,
3637 sig,
3638 plural ? "s" : "",
3639 given,
3640 kwonly_sig,
3641 given == 1 && !kwonly_given ? "was" : "were");
3642 Py_DECREF(sig);
3643 Py_DECREF(kwonly_sig);
3644}
3645
Guido van Rossumc2e20742006-02-27 22:32:47 +00003646/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003647 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003648 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003649
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003650PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003651_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003652 PyObject *const *args, Py_ssize_t argcount,
3653 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003654 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003655 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003656 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003657 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003658{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003659 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003660 PyFrameObject *f;
3661 PyObject *retval = NULL;
3662 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003663 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003665 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3666 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003667 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 if (globals == NULL) {
3670 PyErr_SetString(PyExc_SystemError,
3671 "PyEval_EvalCodeEx: NULL globals");
3672 return NULL;
3673 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003674
Victor Stinnerc7020012016-08-16 23:40:29 +02003675 /* Create the frame */
3676 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003678 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003679 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 fastlocals = f->f_localsplus;
3683 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003684
Victor Stinnerc7020012016-08-16 23:40:29 +02003685 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003686 if (co->co_flags & CO_VARKEYWORDS) {
3687 kwdict = PyDict_New();
3688 if (kwdict == NULL)
3689 goto fail;
3690 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003691 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003692 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003693 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003694 SETLOCAL(i, kwdict);
3695 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003696 else {
3697 kwdict = NULL;
3698 }
3699
3700 /* Copy positional arguments into local variables */
3701 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003702 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003703 }
3704 else {
3705 n = argcount;
3706 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003707 for (i = 0; i < n; i++) {
3708 x = args[i];
3709 Py_INCREF(x);
3710 SETLOCAL(i, x);
3711 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003712
3713 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003714 if (co->co_flags & CO_VARARGS) {
3715 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003716 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003717 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003718 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003719 SETLOCAL(total_args, u);
3720 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 x = args[i];
3722 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003726
Serhiy Storchakab7281052016-09-12 00:52:40 +03003727 /* Handle keyword arguments passed as two strided arrays */
3728 kwcount *= kwstep;
3729 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003730 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003731 PyObject *keyword = kwnames[i];
3732 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003733 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003734
Benjamin Petersonb204a422011-06-05 22:04:07 -05003735 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3736 PyErr_Format(PyExc_TypeError,
3737 "%U() keywords must be strings",
3738 co->co_name);
3739 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003741
Benjamin Petersonb204a422011-06-05 22:04:07 -05003742 /* Speed hack: do raw pointer compares. As names are
3743 normally interned this should almost always hit. */
3744 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3745 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003746 PyObject *name = co_varnames[j];
3747 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003749 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003750 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003751
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752 /* Slow fallback, just in case */
3753 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003754 PyObject *name = co_varnames[j];
3755 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3756 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003757 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003758 }
3759 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003761 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003762 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003763
Victor Stinner231d1f32017-01-11 02:12:06 +01003764 assert(j >= total_args);
3765 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003766 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003767 "%U() got an unexpected keyword argument '%S'",
3768 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003771
Christian Heimes0bd447f2013-07-20 14:48:10 +02003772 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3773 goto fail;
3774 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003775 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003776
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 kw_found:
3778 if (GETLOCAL(j) != NULL) {
3779 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003780 "%U() got multiple values for argument '%S'",
3781 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003782 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003784 Py_INCREF(value);
3785 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003787
3788 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003789 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003790 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 goto fail;
3792 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003793
3794 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003796 Py_ssize_t m = co->co_argcount - defcount;
3797 Py_ssize_t missing = 0;
3798 for (i = argcount; i < m; i++) {
3799 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003800 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003801 }
3802 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003803 if (missing) {
3804 missing_arguments(co, missing, defcount, fastlocals);
3805 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 }
3807 if (n > m)
3808 i = n - m;
3809 else
3810 i = 0;
3811 for (; i < defcount; i++) {
3812 if (GETLOCAL(m+i) == NULL) {
3813 PyObject *def = defs[i];
3814 Py_INCREF(def);
3815 SETLOCAL(m+i, def);
3816 }
3817 }
3818 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003819
3820 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003822 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 for (i = co->co_argcount; i < total_args; i++) {
3824 PyObject *name;
3825 if (GETLOCAL(i) != NULL)
3826 continue;
3827 name = PyTuple_GET_ITEM(co->co_varnames, i);
3828 if (kwdefs != NULL) {
3829 PyObject *def = PyDict_GetItem(kwdefs, name);
3830 if (def) {
3831 Py_INCREF(def);
3832 SETLOCAL(i, def);
3833 continue;
3834 }
3835 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003836 missing++;
3837 }
3838 if (missing) {
3839 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 goto fail;
3841 }
3842 }
3843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003845 vars into frame. */
3846 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003848 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003849 /* Possibly account for the cell variable being an argument. */
3850 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003851 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003852 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003853 /* Clear the local copy. */
3854 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003855 }
3856 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003857 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003858 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003859 if (c == NULL)
3860 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003861 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003863
3864 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003865 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3866 PyObject *o = PyTuple_GET_ITEM(closure, i);
3867 Py_INCREF(o);
3868 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003870
Yury Selivanoveb636452016-09-08 22:01:51 -07003871 /* Handle generator/coroutine/asynchronous generator */
3872 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003873 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003874 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003875 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003876
3877 if (is_coro && tstate->in_coroutine_wrapper) {
3878 assert(coro_wrapper != NULL);
3879 PyErr_Format(PyExc_RuntimeError,
3880 "coroutine wrapper %.200R attempted "
3881 "to recursively wrap %.200R",
3882 coro_wrapper,
3883 co);
3884 goto fail;
3885 }
Yury Selivanov75445082015-05-11 22:57:16 -04003886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 /* Don't need to keep the reference to f_back, it will be set
3888 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003889 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 /* Create a new generator that owns the ready to run frame
3892 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003893 if (is_coro) {
3894 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003895 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3896 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003897 } else {
3898 gen = PyGen_NewWithQualName(f, name, qualname);
3899 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003900 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003901 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003902 }
INADA Naoki9c157762016-12-26 18:52:46 +09003903
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003904 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003905
Yury Selivanov94c22632015-06-04 10:16:51 -04003906 if (is_coro && coro_wrapper != NULL) {
3907 PyObject *wrapped;
3908 tstate->in_coroutine_wrapper = 1;
3909 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3910 tstate->in_coroutine_wrapper = 0;
3911 return wrapped;
3912 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003913
Yury Selivanov75445082015-05-11 22:57:16 -04003914 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003916
Victor Stinner59a73272016-12-09 18:51:13 +01003917 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003918
Thomas Woutersce272b62007-09-19 21:19:28 +00003919fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 /* decref'ing the frame can cause __del__ methods to get invoked,
3922 which can call back into Python. While we're done with the
3923 current Python frame (f), the associated C stack is still in use,
3924 so recursion_depth must be boosted for the duration.
3925 */
3926 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003927 if (Py_REFCNT(f) > 1) {
3928 Py_DECREF(f);
3929 _PyObject_GC_TRACK(f);
3930 }
3931 else {
3932 ++tstate->recursion_depth;
3933 Py_DECREF(f);
3934 --tstate->recursion_depth;
3935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003937}
3938
Victor Stinner40ee3012014-06-16 15:59:28 +02003939PyObject *
3940PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003941 PyObject *const *args, int argcount,
3942 PyObject *const *kws, int kwcount,
3943 PyObject *const *defs, int defcount,
3944 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003945{
3946 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003947 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003948 kws, kws != NULL ? kws + 1 : NULL,
3949 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003950 defs, defcount,
3951 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003952 NULL, NULL);
3953}
Tim Peters5ca576e2001-06-18 22:08:13 +00003954
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003955static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003956special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003959 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003961 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 return NULL;
3963 }
3964 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003965}
3966
3967
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003968/* Logic for the raise statement (too complicated for inlining).
3969 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003970static int
Collin Winter828f04a2007-08-31 00:04:24 +00003971do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 if (exc == NULL) {
3976 /* Reraise */
3977 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01003978 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01003980 type = exc_info->exc_type;
3981 value = exc_info->exc_value;
3982 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02003983 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 PyErr_SetString(PyExc_RuntimeError,
3985 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003986 return 0;
3987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 Py_XINCREF(type);
3989 Py_XINCREF(value);
3990 Py_XINCREF(tb);
3991 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003992 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 /* We support the following forms of raise:
3996 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003997 raise <instance>
3998 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 if (PyExceptionClass_Check(exc)) {
4001 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004002 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 if (value == NULL)
4004 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004005 if (!PyExceptionInstance_Check(value)) {
4006 PyErr_Format(PyExc_TypeError,
4007 "calling %R should have returned an instance of "
4008 "BaseException, not %R",
4009 type, Py_TYPE(value));
4010 goto raise_error;
4011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 }
4013 else if (PyExceptionInstance_Check(exc)) {
4014 value = exc;
4015 type = PyExceptionInstance_Class(exc);
4016 Py_INCREF(type);
4017 }
4018 else {
4019 /* Not something you can raise. You get an exception
4020 anyway, just not what you specified :-) */
4021 Py_DECREF(exc);
4022 PyErr_SetString(PyExc_TypeError,
4023 "exceptions must derive from BaseException");
4024 goto raise_error;
4025 }
Collin Winter828f04a2007-08-31 00:04:24 +00004026
Serhiy Storchakac0191582016-09-27 11:37:10 +03004027 assert(type != NULL);
4028 assert(value != NULL);
4029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (cause) {
4031 PyObject *fixed_cause;
4032 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004033 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 if (fixed_cause == NULL)
4035 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004036 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004038 else if (PyExceptionInstance_Check(cause)) {
4039 fixed_cause = cause;
4040 }
4041 else if (cause == Py_None) {
4042 Py_DECREF(cause);
4043 fixed_cause = NULL;
4044 }
4045 else {
4046 PyErr_SetString(PyExc_TypeError,
4047 "exception causes must derive from "
4048 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 goto raise_error;
4050 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004051 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 }
Collin Winter828f04a2007-08-31 00:04:24 +00004053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyErr_SetObject(type, value);
4055 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004056 Py_DECREF(value);
4057 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004058 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004059
4060raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 Py_XDECREF(value);
4062 Py_XDECREF(type);
4063 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004064 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004065}
4066
Tim Petersd6d010b2001-06-21 02:49:55 +00004067/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004068 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004069
Guido van Rossum0368b722007-05-11 16:50:42 +00004070 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4071 with a variable target.
4072*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004073
Barry Warsawe42b18f1997-08-25 22:13:04 +00004074static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004075unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 int i = 0, j = 0;
4078 Py_ssize_t ll = 0;
4079 PyObject *it; /* iter(v) */
4080 PyObject *w;
4081 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004086 if (it == NULL) {
4087 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4088 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4089 {
4090 PyErr_Format(PyExc_TypeError,
4091 "cannot unpack non-iterable %.200s object",
4092 v->ob_type->tp_name);
4093 }
4094 return 0;
4095 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 for (; i < argcnt; i++) {
4098 w = PyIter_Next(it);
4099 if (w == NULL) {
4100 /* Iterator done, via error or exhaustion. */
4101 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004102 if (argcntafter == -1) {
4103 PyErr_Format(PyExc_ValueError,
4104 "not enough values to unpack (expected %d, got %d)",
4105 argcnt, i);
4106 }
4107 else {
4108 PyErr_Format(PyExc_ValueError,
4109 "not enough values to unpack "
4110 "(expected at least %d, got %d)",
4111 argcnt + argcntafter, i);
4112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 }
4114 goto Error;
4115 }
4116 *--sp = w;
4117 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 if (argcntafter == -1) {
4120 /* We better have exhausted the iterator now. */
4121 w = PyIter_Next(it);
4122 if (w == NULL) {
4123 if (PyErr_Occurred())
4124 goto Error;
4125 Py_DECREF(it);
4126 return 1;
4127 }
4128 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004129 PyErr_Format(PyExc_ValueError,
4130 "too many values to unpack (expected %d)",
4131 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 goto Error;
4133 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 l = PySequence_List(it);
4136 if (l == NULL)
4137 goto Error;
4138 *--sp = l;
4139 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 ll = PyList_GET_SIZE(l);
4142 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004143 PyErr_Format(PyExc_ValueError,
4144 "not enough values to unpack (expected at least %d, got %zd)",
4145 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 goto Error;
4147 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 /* Pop the "after-variable" args off the list. */
4150 for (j = argcntafter; j > 0; j--, i++) {
4151 *--sp = PyList_GET_ITEM(l, ll - j);
4152 }
4153 /* Resize the list. */
4154 Py_SIZE(l) = ll - argcntafter;
4155 Py_DECREF(it);
4156 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004157
Tim Petersd6d010b2001-06-21 02:49:55 +00004158Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 for (; i > 0; i--, sp++)
4160 Py_DECREF(*sp);
4161 Py_XDECREF(it);
4162 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004163}
4164
4165
Guido van Rossum96a42c81992-01-12 02:29:51 +00004166#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004167static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004168prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 printf("%s ", str);
4171 if (PyObject_Print(v, stdout, 0) != 0)
4172 PyErr_Clear(); /* Don't know what else to do */
4173 printf("\n");
4174 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004175}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004176#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004177
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004178static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004179call_exc_trace(Py_tracefunc func, PyObject *self,
4180 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004181{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004182 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004184 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 if (value == NULL) {
4186 value = Py_None;
4187 Py_INCREF(value);
4188 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004189 PyErr_NormalizeException(&type, &value, &orig_traceback);
4190 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 arg = PyTuple_Pack(3, type, value, traceback);
4192 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004193 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 return;
4195 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004196 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 Py_DECREF(arg);
4198 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004199 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 else {
4201 Py_XDECREF(type);
4202 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004203 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004205}
4206
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004207static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004208call_trace_protected(Py_tracefunc func, PyObject *obj,
4209 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 PyObject *type, *value, *traceback;
4213 int err;
4214 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004215 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (err == 0)
4217 {
4218 PyErr_Restore(type, value, traceback);
4219 return 0;
4220 }
4221 else {
4222 Py_XDECREF(type);
4223 Py_XDECREF(value);
4224 Py_XDECREF(traceback);
4225 return -1;
4226 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004227}
4228
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004229static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004230call_trace(Py_tracefunc func, PyObject *obj,
4231 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 int result;
4235 if (tstate->tracing)
4236 return 0;
4237 tstate->tracing++;
4238 tstate->use_tracing = 0;
4239 result = func(obj, frame, what, arg);
4240 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4241 || (tstate->c_profilefunc != NULL));
4242 tstate->tracing--;
4243 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004244}
4245
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004246PyObject *
4247_PyEval_CallTracing(PyObject *func, PyObject *args)
4248{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004249 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 int save_tracing = tstate->tracing;
4251 int save_use_tracing = tstate->use_tracing;
4252 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 tstate->tracing = 0;
4255 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4256 || (tstate->c_profilefunc != NULL));
4257 result = PyObject_Call(func, args, NULL);
4258 tstate->tracing = save_tracing;
4259 tstate->use_tracing = save_use_tracing;
4260 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004261}
4262
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004263/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004264static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004265maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004266 PyThreadState *tstate, PyFrameObject *frame,
4267 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 int result = 0;
4270 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 /* If the last instruction executed isn't in the current
4273 instruction window, reset the window.
4274 */
4275 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4276 PyAddrPair bounds;
4277 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4278 &bounds);
4279 *instr_lb = bounds.ap_lower;
4280 *instr_ub = bounds.ap_upper;
4281 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004282 /* If the last instruction falls at the start of a line or if it
4283 represents a jump backwards, update the frame's line number and
4284 then call the trace function if we're tracing source lines.
4285 */
4286 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004288 if (frame->f_trace_lines) {
4289 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 }
George King20faa682017-10-18 17:44:22 -07004292 /* Always emit an opcode event if we're tracing all opcodes. */
4293 if (frame->f_trace_opcodes) {
4294 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 *instr_prev = frame->f_lasti;
4297 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004298}
4299
Fred Drake5755ce62001-06-27 19:19:46 +00004300void
4301PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 PyThreadState *tstate = PyThreadState_GET();
4304 PyObject *temp = tstate->c_profileobj;
4305 Py_XINCREF(arg);
4306 tstate->c_profilefunc = NULL;
4307 tstate->c_profileobj = NULL;
4308 /* Must make sure that tracing is not ignored if 'temp' is freed */
4309 tstate->use_tracing = tstate->c_tracefunc != NULL;
4310 Py_XDECREF(temp);
4311 tstate->c_profilefunc = func;
4312 tstate->c_profileobj = arg;
4313 /* Flag that tracing or profiling is turned on */
4314 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004315}
4316
4317void
4318PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 PyThreadState *tstate = PyThreadState_GET();
4321 PyObject *temp = tstate->c_traceobj;
4322 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4323 Py_XINCREF(arg);
4324 tstate->c_tracefunc = NULL;
4325 tstate->c_traceobj = NULL;
4326 /* Must make sure that profiling is not ignored if 'temp' is freed */
4327 tstate->use_tracing = tstate->c_profilefunc != NULL;
4328 Py_XDECREF(temp);
4329 tstate->c_tracefunc = func;
4330 tstate->c_traceobj = arg;
4331 /* Flag that tracing or profiling is turned on */
4332 tstate->use_tracing = ((func != NULL)
4333 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004334}
4335
Yury Selivanov75445082015-05-11 22:57:16 -04004336void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004337_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4338{
4339 assert(new_depth >= 0);
4340 PyThreadState *tstate = PyThreadState_GET();
4341 tstate->coroutine_origin_tracking_depth = new_depth;
4342}
4343
4344int
4345_PyEval_GetCoroutineOriginTrackingDepth(void)
4346{
4347 PyThreadState *tstate = PyThreadState_GET();
4348 return tstate->coroutine_origin_tracking_depth;
4349}
4350
4351void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004352_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004353{
4354 PyThreadState *tstate = PyThreadState_GET();
4355
Yury Selivanov75445082015-05-11 22:57:16 -04004356 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004357 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004358}
4359
4360PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004361_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004362{
4363 PyThreadState *tstate = PyThreadState_GET();
4364 return tstate->coroutine_wrapper;
4365}
4366
Yury Selivanoveb636452016-09-08 22:01:51 -07004367void
4368_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4369{
4370 PyThreadState *tstate = PyThreadState_GET();
4371
4372 Py_XINCREF(firstiter);
4373 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4374}
4375
4376PyObject *
4377_PyEval_GetAsyncGenFirstiter(void)
4378{
4379 PyThreadState *tstate = PyThreadState_GET();
4380 return tstate->async_gen_firstiter;
4381}
4382
4383void
4384_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4385{
4386 PyThreadState *tstate = PyThreadState_GET();
4387
4388 Py_XINCREF(finalizer);
4389 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4390}
4391
4392PyObject *
4393_PyEval_GetAsyncGenFinalizer(void)
4394{
4395 PyThreadState *tstate = PyThreadState_GET();
4396 return tstate->async_gen_finalizer;
4397}
4398
Guido van Rossumb209a111997-04-29 18:18:01 +00004399PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004400PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 PyFrameObject *current_frame = PyEval_GetFrame();
4403 if (current_frame == NULL)
4404 return PyThreadState_GET()->interp->builtins;
4405 else
4406 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004407}
4408
Guido van Rossumb209a111997-04-29 18:18:01 +00004409PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004410PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004413 if (current_frame == NULL) {
4414 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004416 }
4417
4418 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4419 return NULL;
4420
4421 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004423}
4424
Guido van Rossumb209a111997-04-29 18:18:01 +00004425PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004426PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 PyFrameObject *current_frame = PyEval_GetFrame();
4429 if (current_frame == NULL)
4430 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004431
4432 assert(current_frame->f_globals != NULL);
4433 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004434}
4435
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004436PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004437PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 PyThreadState *tstate = PyThreadState_GET();
4440 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004441}
4442
Guido van Rossum6135a871995-01-09 17:53:26 +00004443int
Tim Peters5ba58662001-07-16 02:29:45 +00004444PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 PyFrameObject *current_frame = PyEval_GetFrame();
4447 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 if (current_frame != NULL) {
4450 const int codeflags = current_frame->f_code->co_flags;
4451 const int compilerflags = codeflags & PyCF_MASK;
4452 if (compilerflags) {
4453 result = 1;
4454 cf->cf_flags |= compilerflags;
4455 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004456#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 if (codeflags & CO_GENERATOR_ALLOWED) {
4458 result = 1;
4459 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4460 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004461#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 }
4463 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004464}
4465
Guido van Rossum3f5da241990-12-20 15:06:42 +00004466
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004467const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004468PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 if (PyMethod_Check(func))
4471 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4472 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004473 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 else if (PyCFunction_Check(func))
4475 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4476 else
4477 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004478}
4479
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004480const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004481PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 if (PyMethod_Check(func))
4484 return "()";
4485 else if (PyFunction_Check(func))
4486 return "()";
4487 else if (PyCFunction_Check(func))
4488 return "()";
4489 else
4490 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004491}
4492
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004493#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004494if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004495 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4496 tstate, tstate->frame, \
4497 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 x = NULL; \
4499 } \
4500 else { \
4501 x = call; \
4502 if (tstate->c_profilefunc != NULL) { \
4503 if (x == NULL) { \
4504 call_trace_protected(tstate->c_profilefunc, \
4505 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004506 tstate, tstate->frame, \
4507 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 /* XXX should pass (type, value, tb) */ \
4509 } else { \
4510 if (call_trace(tstate->c_profilefunc, \
4511 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004512 tstate, tstate->frame, \
4513 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 Py_DECREF(x); \
4515 x = NULL; \
4516 } \
4517 } \
4518 } \
4519 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004520} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 x = call; \
4522 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004523
Victor Stinner415c5102017-01-11 00:54:57 +01004524/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4525 to reduce the stack consumption. */
4526Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004527call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004528{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004529 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 PyObject *func = *pfunc;
4531 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004532 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4533 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004534 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 /* Always dispatch PyCFunction first, because these are
4537 presumed to be the most frequent callable object.
4538 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004539 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004541 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004542 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004543 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4544 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004545 if (tstate->use_tracing && tstate->c_profilefunc) {
4546 // We need to create PyCFunctionObject for tracing.
4547 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4548 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4549 if (func == NULL) {
4550 return NULL;
4551 }
4552 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4553 kwnames));
4554 Py_DECREF(func);
4555 }
4556 else {
4557 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4558 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004559 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004560 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004561 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004562 /* Optimize access to bound methods. Reuse the Python stack
4563 to pass 'self' as the first argument, replace 'func'
4564 with 'self'. It avoids the creation of a new temporary tuple
4565 for arguments (to replace func with self) when the method uses
4566 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004567 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004568 Py_INCREF(self);
4569 func = PyMethod_GET_FUNCTION(func);
4570 Py_INCREF(func);
4571 Py_SETREF(*pfunc, self);
4572 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004573 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004574 }
4575 else {
4576 Py_INCREF(func);
4577 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004578
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004579 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004580 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004581 }
4582 else {
4583 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4584 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004585 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004587
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004588 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4589
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004590 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 while ((*pp_stack) > pfunc) {
4592 w = EXT_POP(*pp_stack);
4593 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004597}
4598
Jeremy Hylton52820442001-01-03 23:52:36 +00004599static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004600do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004603 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 PyThreadState *tstate = PyThreadState_GET();
4605 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004606 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004608 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004609 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004610 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004611}
4612
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004613/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004614 nb_index slot defined, and store in *pi.
4615 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004616 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004617 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004618*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004619int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004620_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004621{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004622 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 Py_ssize_t x;
4624 if (PyIndex_Check(v)) {
4625 x = PyNumber_AsSsize_t(v, NULL);
4626 if (x == -1 && PyErr_Occurred())
4627 return 0;
4628 }
4629 else {
4630 PyErr_SetString(PyExc_TypeError,
4631 "slice indices must be integers or "
4632 "None or have an __index__ method");
4633 return 0;
4634 }
4635 *pi = x;
4636 }
4637 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004638}
4639
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004640int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004641_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004642{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004643 Py_ssize_t x;
4644 if (PyIndex_Check(v)) {
4645 x = PyNumber_AsSsize_t(v, NULL);
4646 if (x == -1 && PyErr_Occurred())
4647 return 0;
4648 }
4649 else {
4650 PyErr_SetString(PyExc_TypeError,
4651 "slice indices must be integers or "
4652 "have an __index__ method");
4653 return 0;
4654 }
4655 *pi = x;
4656 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004657}
4658
4659
Guido van Rossum486364b2007-06-30 05:01:58 +00004660#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004662
Guido van Rossumb209a111997-04-29 18:18:01 +00004663static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004664cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 int res = 0;
4667 switch (op) {
4668 case PyCmp_IS:
4669 res = (v == w);
4670 break;
4671 case PyCmp_IS_NOT:
4672 res = (v != w);
4673 break;
4674 case PyCmp_IN:
4675 res = PySequence_Contains(w, v);
4676 if (res < 0)
4677 return NULL;
4678 break;
4679 case PyCmp_NOT_IN:
4680 res = PySequence_Contains(w, v);
4681 if (res < 0)
4682 return NULL;
4683 res = !res;
4684 break;
4685 case PyCmp_EXC_MATCH:
4686 if (PyTuple_Check(w)) {
4687 Py_ssize_t i, length;
4688 length = PyTuple_Size(w);
4689 for (i = 0; i < length; i += 1) {
4690 PyObject *exc = PyTuple_GET_ITEM(w, i);
4691 if (!PyExceptionClass_Check(exc)) {
4692 PyErr_SetString(PyExc_TypeError,
4693 CANNOT_CATCH_MSG);
4694 return NULL;
4695 }
4696 }
4697 }
4698 else {
4699 if (!PyExceptionClass_Check(w)) {
4700 PyErr_SetString(PyExc_TypeError,
4701 CANNOT_CATCH_MSG);
4702 return NULL;
4703 }
4704 }
4705 res = PyErr_GivenExceptionMatches(v, w);
4706 break;
4707 default:
4708 return PyObject_RichCompare(v, w, op);
4709 }
4710 v = res ? Py_True : Py_False;
4711 Py_INCREF(v);
4712 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004713}
4714
Thomas Wouters52152252000-08-17 22:55:00 +00004715static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004716import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4717{
4718 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004719 PyObject *import_func, *res;
4720 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004721
4722 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4723 if (import_func == NULL) {
4724 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4725 return NULL;
4726 }
4727
4728 /* Fast path for not overloaded __import__. */
4729 if (import_func == PyThreadState_GET()->interp->import_func) {
4730 int ilevel = _PyLong_AsInt(level);
4731 if (ilevel == -1 && PyErr_Occurred()) {
4732 return NULL;
4733 }
4734 res = PyImport_ImportModuleLevelObject(
4735 name,
4736 f->f_globals,
4737 f->f_locals == NULL ? Py_None : f->f_locals,
4738 fromlist,
4739 ilevel);
4740 return res;
4741 }
4742
4743 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004744
4745 stack[0] = name;
4746 stack[1] = f->f_globals;
4747 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4748 stack[3] = fromlist;
4749 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004750 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004751 Py_DECREF(import_func);
4752 return res;
4753}
4754
4755static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004756import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004759 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004760 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004761
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004762 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004763 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004764 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004765 /* Issue #17636: in case this failed because of a circular relative
4766 import, try to fallback on reading the module directly from
4767 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004768 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004769 if (pkgname == NULL) {
4770 goto error;
4771 }
Oren Milman6db70332017-09-19 14:23:01 +03004772 if (!PyUnicode_Check(pkgname)) {
4773 Py_CLEAR(pkgname);
4774 goto error;
4775 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004776 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004777 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004778 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004779 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004780 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004781 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004782 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004783 if (x == NULL) {
4784 goto error;
4785 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004786 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004788 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004789 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004790 if (pkgname == NULL) {
4791 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4792 if (pkgname_or_unknown == NULL) {
4793 Py_XDECREF(pkgpath);
4794 return NULL;
4795 }
4796 } else {
4797 pkgname_or_unknown = pkgname;
4798 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004799
4800 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4801 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004802 errmsg = PyUnicode_FromFormat(
4803 "cannot import name %R from %R (unknown location)",
4804 name, pkgname_or_unknown
4805 );
4806 /* NULL check for errmsg done by PyErr_SetImportError. */
4807 PyErr_SetImportError(errmsg, pkgname, NULL);
4808 }
4809 else {
4810 errmsg = PyUnicode_FromFormat(
4811 "cannot import name %R from %R (%S)",
4812 name, pkgname_or_unknown, pkgpath
4813 );
4814 /* NULL check for errmsg done by PyErr_SetImportError. */
4815 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004816 }
4817
Xiang Zhang4830f582017-03-21 11:13:42 +08004818 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004819 Py_XDECREF(pkgname_or_unknown);
4820 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004821 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004822}
Guido van Rossumac7be682001-01-17 15:42:30 +00004823
Thomas Wouters52152252000-08-17 22:55:00 +00004824static int
4825import_all_from(PyObject *locals, PyObject *v)
4826{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004827 _Py_IDENTIFIER(__all__);
4828 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004829 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 int skip_leading_underscores = 0;
4831 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004832
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004833 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4834 return -1; /* Unexpected error */
4835 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004837 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4838 return -1;
4839 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004842 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 return -1;
4844 }
4845 all = PyMapping_Keys(dict);
4846 Py_DECREF(dict);
4847 if (all == NULL)
4848 return -1;
4849 skip_leading_underscores = 1;
4850 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 for (pos = 0, err = 0; ; pos++) {
4853 name = PySequence_GetItem(all, pos);
4854 if (name == NULL) {
4855 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4856 err = -1;
4857 else
4858 PyErr_Clear();
4859 break;
4860 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004861 if (skip_leading_underscores && PyUnicode_Check(name)) {
4862 if (PyUnicode_READY(name) == -1) {
4863 Py_DECREF(name);
4864 err = -1;
4865 break;
4866 }
4867 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4868 Py_DECREF(name);
4869 continue;
4870 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 }
4872 value = PyObject_GetAttr(v, name);
4873 if (value == NULL)
4874 err = -1;
4875 else if (PyDict_CheckExact(locals))
4876 err = PyDict_SetItem(locals, name, value);
4877 else
4878 err = PyObject_SetItem(locals, name, value);
4879 Py_DECREF(name);
4880 Py_XDECREF(value);
4881 if (err != 0)
4882 break;
4883 }
4884 Py_DECREF(all);
4885 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004886}
4887
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004888static int
4889check_args_iterable(PyObject *func, PyObject *args)
4890{
4891 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4892 PyErr_Format(PyExc_TypeError,
4893 "%.200s%.200s argument after * "
4894 "must be an iterable, not %.200s",
4895 PyEval_GetFuncName(func),
4896 PyEval_GetFuncDesc(func),
4897 args->ob_type->tp_name);
4898 return -1;
4899 }
4900 return 0;
4901}
4902
4903static void
4904format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4905{
4906 PyErr_Format(PyExc_TypeError,
4907 "%.200s%.200s argument after ** "
4908 "must be a mapping, not %.200s",
4909 PyEval_GetFuncName(func),
4910 PyEval_GetFuncDesc(func),
4911 kwargs->ob_type->tp_name);
4912}
4913
Guido van Rossumac7be682001-01-17 15:42:30 +00004914static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004915format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 if (!obj)
4920 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004921
Serhiy Storchaka06515832016-11-20 09:13:07 +02004922 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 if (!obj_str)
4924 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004927}
Guido van Rossum950361c1997-01-24 13:49:28 +00004928
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004929static void
4930format_exc_unbound(PyCodeObject *co, int oparg)
4931{
4932 PyObject *name;
4933 /* Don't stomp existing exception */
4934 if (PyErr_Occurred())
4935 return;
4936 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4937 name = PyTuple_GET_ITEM(co->co_cellvars,
4938 oparg);
4939 format_exc_check_arg(
4940 PyExc_UnboundLocalError,
4941 UNBOUNDLOCAL_ERROR_MSG,
4942 name);
4943 } else {
4944 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4945 PyTuple_GET_SIZE(co->co_cellvars));
4946 format_exc_check_arg(PyExc_NameError,
4947 UNBOUNDFREE_ERROR_MSG, name);
4948 }
4949}
4950
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004951static PyObject *
4952unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03004953 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004954{
4955 PyObject *res;
4956 if (Py_REFCNT(v) == 2) {
4957 /* In the common case, there are 2 references to the value
4958 * stored in 'variable' when the += is performed: one on the
4959 * value stack (in 'v') and one still stored in the
4960 * 'variable'. We try to delete the variable now to reduce
4961 * the refcnt to 1.
4962 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004963 int opcode, oparg;
4964 NEXTOPARG();
4965 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004966 case STORE_FAST:
4967 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004968 PyObject **fastlocals = f->f_localsplus;
4969 if (GETLOCAL(oparg) == v)
4970 SETLOCAL(oparg, NULL);
4971 break;
4972 }
4973 case STORE_DEREF:
4974 {
4975 PyObject **freevars = (f->f_localsplus +
4976 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004977 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05004978 if (PyCell_GET(c) == v) {
4979 PyCell_SET(c, NULL);
4980 Py_DECREF(v);
4981 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004982 break;
4983 }
4984 case STORE_NAME:
4985 {
4986 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004987 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004988 PyObject *locals = f->f_locals;
4989 if (PyDict_CheckExact(locals) &&
4990 PyDict_GetItem(locals, name) == v) {
4991 if (PyDict_DelItem(locals, name) != 0) {
4992 PyErr_Clear();
4993 }
4994 }
4995 break;
4996 }
4997 }
4998 }
4999 res = v;
5000 PyUnicode_Append(&res, w);
5001 return res;
5002}
5003
Guido van Rossum950361c1997-01-24 13:49:28 +00005004#ifdef DYNAMIC_EXECUTION_PROFILE
5005
Skip Montanarof118cb12001-10-15 20:51:38 +00005006static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005007getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 int i;
5010 PyObject *l = PyList_New(256);
5011 if (l == NULL) return NULL;
5012 for (i = 0; i < 256; i++) {
5013 PyObject *x = PyLong_FromLong(a[i]);
5014 if (x == NULL) {
5015 Py_DECREF(l);
5016 return NULL;
5017 }
5018 PyList_SetItem(l, i, x);
5019 }
5020 for (i = 0; i < 256; i++)
5021 a[i] = 0;
5022 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005023}
5024
5025PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005026_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005027{
5028#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005030#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 int i;
5032 PyObject *l = PyList_New(257);
5033 if (l == NULL) return NULL;
5034 for (i = 0; i < 257; i++) {
5035 PyObject *x = getarray(dxpairs[i]);
5036 if (x == NULL) {
5037 Py_DECREF(l);
5038 return NULL;
5039 }
5040 PyList_SetItem(l, i, x);
5041 }
5042 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005043#endif
5044}
5045
5046#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005047
5048Py_ssize_t
5049_PyEval_RequestCodeExtraIndex(freefunc free)
5050{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005051 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005052 Py_ssize_t new_index;
5053
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005054 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005055 return -1;
5056 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005057 new_index = interp->co_extra_user_count++;
5058 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005059 return new_index;
5060}
Łukasz Langaa785c872016-09-09 17:37:37 -07005061
5062static void
5063dtrace_function_entry(PyFrameObject *f)
5064{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005065 const char *filename;
5066 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005067 int lineno;
5068
5069 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5070 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5071 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5072
5073 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5074}
5075
5076static void
5077dtrace_function_return(PyFrameObject *f)
5078{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005079 const char *filename;
5080 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005081 int lineno;
5082
5083 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5084 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5085 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5086
5087 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5088}
5089
5090/* DTrace equivalent of maybe_call_line_trace. */
5091static void
5092maybe_dtrace_line(PyFrameObject *frame,
5093 int *instr_lb, int *instr_ub, int *instr_prev)
5094{
5095 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005096 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005097
5098 /* If the last instruction executed isn't in the current
5099 instruction window, reset the window.
5100 */
5101 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5102 PyAddrPair bounds;
5103 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5104 &bounds);
5105 *instr_lb = bounds.ap_lower;
5106 *instr_ub = bounds.ap_upper;
5107 }
5108 /* If the last instruction falls at the start of a line or if
5109 it represents a jump backwards, update the frame's line
5110 number and call the trace function. */
5111 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5112 frame->f_lineno = line;
5113 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5114 if (!co_filename)
5115 co_filename = "?";
5116 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5117 if (!co_name)
5118 co_name = "?";
5119 PyDTrace_LINE(co_filename, co_name, line);
5120 }
5121 *instr_prev = frame->f_lasti;
5122}