blob: 1a72413c9e1522a3ef6f6c52d0156ab32502b7d4 [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
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400503static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000504static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000505
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600506#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000507
Guido van Rossum374a9221991-04-04 10:40:29 +0000508
Guido van Rossumb209a111997-04-29 18:18:01 +0000509PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000510PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyEval_EvalCodeEx(co,
513 globals, locals,
514 (PyObject **)NULL, 0,
515 (PyObject **)NULL, 0,
516 (PyObject **)NULL, 0,
517 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000518}
519
520
521/* Interpreter main loop */
522
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000523PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000524PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 /* This is for backward compatibility with extension modules that
526 used this API; core interpreter code should call
527 PyEval_EvalFrameEx() */
528 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000529}
530
531PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000533{
Brett Cannon3cebf932016-09-05 15:33:46 -0700534 PyThreadState *tstate = PyThreadState_GET();
535 return tstate->interp->eval_frame(f, throwflag);
536}
537
Victor Stinnerc6944e72016-11-11 02:13:35 +0100538PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700539_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
540{
Guido van Rossum950361c1997-01-24 13:49:28 +0000541#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000543#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200544 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300545 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200546 int opcode; /* Current opcode */
547 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200548 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyObject *retval = NULL; /* Return value */
550 PyThreadState *tstate = PyThreadState_GET();
551 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 is true when the line being executed has changed. The
558 initial values are such as to make this false the first
559 time it is tested. */
560 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000561
Serhiy Storchakaab874002016-09-11 13:48:15 +0300562 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 PyObject *names;
564 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000565
Brett Cannon368b4b72012-04-02 12:17:59 -0400566#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200567 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400568#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200569
Antoine Pitroub52ec782009-01-25 16:34:23 +0000570/* Computed GOTOs, or
571 the-optimization-commonly-but-improperly-known-as-"threaded code"
572 using gcc's labels-as-values extension
573 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
574
575 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000577 combined with a lookup table of jump addresses. However, since the
578 indirect jump instruction is shared by all opcodes, the CPU will have a
579 hard time making the right prediction for where to jump next (actually,
580 it will be always wrong except in the uncommon case of a sequence of
581 several identical opcodes).
582
583 "Threaded code" in contrast, uses an explicit jump table and an explicit
584 indirect jump instruction at the end of each opcode. Since the jump
585 instruction is at a different address for each opcode, the CPU will make a
586 separate prediction for each of these instructions, which is equivalent to
587 predicting the second opcode of each opcode pair. These predictions have
588 a much better chance to turn out valid, especially in small bytecode loops.
589
590 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000592 and potentially many more instructions (depending on the pipeline width).
593 A correctly predicted branch, however, is nearly free.
594
595 At the time of this writing, the "threaded code" version is up to 15-20%
596 faster than the normal "switch" version, depending on the compiler and the
597 CPU architecture.
598
599 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
600 because it would render the measurements invalid.
601
602
603 NOTE: care must be taken that the compiler doesn't try to "optimize" the
604 indirect jumps by sharing them between all opcodes. Such optimizations
605 can be disabled on gcc by using the -fno-gcse flag (or possibly
606 -fno-crossjumping).
607*/
608
Antoine Pitrou042b1282010-08-13 21:15:58 +0000609#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000610#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000611#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000612#endif
613
Antoine Pitrou042b1282010-08-13 21:15:58 +0000614#ifdef HAVE_COMPUTED_GOTOS
615 #ifndef USE_COMPUTED_GOTOS
616 #define USE_COMPUTED_GOTOS 1
617 #endif
618#else
619 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
620 #error "Computed gotos are not supported on this compiler."
621 #endif
622 #undef USE_COMPUTED_GOTOS
623 #define USE_COMPUTED_GOTOS 0
624#endif
625
626#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000627/* Import the static jump table */
628#include "opcode_targets.h"
629
Antoine Pitroub52ec782009-01-25 16:34:23 +0000630#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000633
Antoine Pitroub52ec782009-01-25 16:34:23 +0000634#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600636 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 FAST_DISPATCH(); \
638 } \
639 continue; \
640 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000641
642#ifdef LLTRACE
643#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700645 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300647 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300648 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 } \
650 goto fast_next_opcode; \
651 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000652#else
653#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700655 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300657 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300658 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 } \
660 goto fast_next_opcode; \
661 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000662#endif
663
664#else
665#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300667
Antoine Pitroub52ec782009-01-25 16:34:23 +0000668#define DISPATCH() continue
669#define FAST_DISPATCH() goto fast_next_opcode
670#endif
671
672
Neal Norwitza81d2202002-07-14 00:27:26 +0000673/* Tuple access macros */
674
675#ifndef Py_DEBUG
676#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
677#else
678#define GETITEM(v, i) PyTuple_GetItem((v), (i))
679#endif
680
Guido van Rossum374a9221991-04-04 10:40:29 +0000681/* Code access macros */
682
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300683/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600684#define INSTR_OFFSET() \
685 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300686#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300687 _Py_CODEUNIT word = *next_instr; \
688 opcode = _Py_OPCODE(word); \
689 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300690 next_instr++; \
691 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300692#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
693#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000694
Raymond Hettingerf606f872003-03-16 03:11:04 +0000695/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 Some opcodes tend to come in pairs thus making it possible to
697 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300698 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 Verifying the prediction costs a single high-speed test of a register
701 variable against a constant. If the pairing was good, then the
702 processor's own internal branch predication has a high likelihood of
703 success, resulting in a nearly zero-overhead transition to the
704 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300705 including its unpredictable switch-case branch. Combined with the
706 processor's internal branch prediction, a successful PREDICT has the
707 effect of making the two opcodes run as if they were a single new opcode
708 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000709
Georg Brandl86b2fb92008-07-16 03:43:04 +0000710 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 predictions turned-on and interpret the results as if some opcodes
712 had been combined or turn-off predictions so that the opcode frequency
713 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000714
715 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 the CPU to record separate branch prediction information for each
717 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000718
Raymond Hettingerf606f872003-03-16 03:11:04 +0000719*/
720
Antoine Pitrou042b1282010-08-13 21:15:58 +0000721#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000723#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300724#define PREDICT(op) \
725 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300726 _Py_CODEUNIT word = *next_instr; \
727 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300728 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300729 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300730 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300731 goto PRED_##op; \
732 } \
733 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000734#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300735#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736
Raymond Hettingerf606f872003-03-16 03:11:04 +0000737
Guido van Rossum374a9221991-04-04 10:40:29 +0000738/* Stack manipulation macros */
739
Martin v. Löwis18e16552006-02-15 17:27:45 +0000740/* The stack can grow at most MAXINT deep, as co_nlocals and
741 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000742#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
743#define EMPTY() (STACK_LEVEL() == 0)
744#define TOP() (stack_pointer[-1])
745#define SECOND() (stack_pointer[-2])
746#define THIRD() (stack_pointer[-3])
747#define FOURTH() (stack_pointer[-4])
748#define PEEK(n) (stack_pointer[-(n)])
749#define SET_TOP(v) (stack_pointer[-1] = (v))
750#define SET_SECOND(v) (stack_pointer[-2] = (v))
751#define SET_THIRD(v) (stack_pointer[-3] = (v))
752#define SET_FOURTH(v) (stack_pointer[-4] = (v))
753#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
754#define BASIC_STACKADJ(n) (stack_pointer += n)
755#define BASIC_PUSH(v) (*stack_pointer++ = (v))
756#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000757
Guido van Rossum96a42c81992-01-12 02:29:51 +0000758#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000760 lltrace && prtrace(TOP(), "push")); \
761 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000763 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000765 lltrace && prtrace(TOP(), "stackadj")); \
766 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000767#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000768 prtrace((STACK_POINTER)[-1], "ext_pop")), \
769 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000770#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000771#define PUSH(v) BASIC_PUSH(v)
772#define POP() BASIC_POP()
773#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000774#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000775#endif
776
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777/* Local variable macros */
778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000780
781/* The SETLOCAL() macro must not DECREF the local variable in-place and
782 then store the new value; it must copy the old value to a temporary
783 value, then store the new value, and then DECREF the temporary value.
784 This is because it is possible that during the DECREF the frame is
785 accessed by other code (e.g. a __del__ method or gc.collect()) and the
786 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000788 GETLOCAL(i) = value; \
789 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000790
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000791
792#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 while (STACK_LEVEL() > (b)->b_level) { \
794 PyObject *v = POP(); \
795 Py_XDECREF(v); \
796 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000797
798#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300799 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100801 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 assert(STACK_LEVEL() >= (b)->b_level + 3); \
803 while (STACK_LEVEL() > (b)->b_level + 3) { \
804 value = POP(); \
805 Py_XDECREF(value); \
806 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100807 exc_info = tstate->exc_info; \
808 type = exc_info->exc_type; \
809 value = exc_info->exc_value; \
810 traceback = exc_info->exc_traceback; \
811 exc_info->exc_type = POP(); \
812 exc_info->exc_value = POP(); \
813 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_XDECREF(type); \
815 Py_XDECREF(value); \
816 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300817 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000818
Guido van Rossuma027efa1997-05-05 20:56:21 +0000819/* Start of code */
820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* push frame */
822 if (Py_EnterRecursiveCall(""))
823 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (tstate->use_tracing) {
828 if (tstate->c_tracefunc != NULL) {
829 /* tstate->c_tracefunc, if defined, is a
830 function that will be called on *every* entry
831 to a code block. Its return value, if not
832 None, is a function that will be called at
833 the start of each executed line of code.
834 (Actually, the function must return itself
835 in order to continue tracing.) The trace
836 functions are called with three arguments:
837 a pointer to the current frame, a string
838 indicating why the function is called, and
839 an argument which depends on the situation.
840 The global trace function is also called
841 whenever an exception is detected. */
842 if (call_trace_protected(tstate->c_tracefunc,
843 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100844 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 /* Trace function raised an error */
846 goto exit_eval_frame;
847 }
848 }
849 if (tstate->c_profilefunc != NULL) {
850 /* Similar for c_profilefunc, except it needn't
851 return itself and isn't called for "line" events */
852 if (call_trace_protected(tstate->c_profilefunc,
853 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100854 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* Profile function raised an error */
856 goto exit_eval_frame;
857 }
858 }
859 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000860
Łukasz Langaa785c872016-09-09 17:37:37 -0700861 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
862 dtrace_function_entry(f);
863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 co = f->f_code;
865 names = co->co_names;
866 consts = co->co_consts;
867 fastlocals = f->f_localsplus;
868 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300869 assert(PyBytes_Check(co->co_code));
870 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300871 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
872 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
873 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300874 /*
875 f->f_lasti refers to the index of the last instruction,
876 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000877
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300878 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500879 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 When the PREDICT() macros are enabled, some opcode pairs follow in
882 direct succession without updating f->f_lasti. A successful
883 prediction effectively links the two codes together as if they
884 were a single new opcode; accordingly,f->f_lasti will point to
885 the first code in the pair (for instance, GET_ITER followed by
886 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300887 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300889 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300890 next_instr = first_instr;
891 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300892 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
893 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 stack_pointer = f->f_stacktop;
896 assert(stack_pointer != NULL);
897 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200898 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000899
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000900
Tim Peters5ca576e2001-06-18 22:08:13 +0000901#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200902 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000903#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000904
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400905 if (throwflag) /* support for generator.throw() */
906 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907
Victor Stinnerace47d72013-07-18 01:41:08 +0200908#ifdef Py_DEBUG
909 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100910 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000911 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200912 assert(!PyErr_Occurred());
913#endif
914
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200915main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 assert(stack_pointer >= f->f_valuestack); /* else underflow */
918 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200919 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* Do periodic things. Doing this every time through
922 the loop would add too much overhead, so we do it
923 only every Nth instruction. We also do it if
924 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
925 event needs attention (e.g. a signal handler or
926 async I/O handler); see Py_AddPendingCall() and
927 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000928
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600929 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700930 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
931 _Py_OPCODE(*next_instr) == YIELD_FROM) {
932 /* Two cases where we skip running signal handlers and other
933 pending calls:
934 - If we're about to enter the try: of a try/finally (not
935 *very* useful, but might help in some cases and it's
936 traditional)
937 - If we're resuming a chain of nested 'yield from' or
938 'await' calls, then each frame is parked with YIELD_FROM
939 as its next opcode. If the user hit control-C we want to
940 wait until we've reached the innermost frame before
941 running the signal handler and raising KeyboardInterrupt
942 (see bpo-30039).
943 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 goto fast_next_opcode;
945 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600946 if (_Py_atomic_load_relaxed(
947 &_PyRuntime.ceval.pending.calls_to_do))
948 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400949 if (Py_MakePendingCalls() < 0)
950 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600952 if (_Py_atomic_load_relaxed(
953 &_PyRuntime.ceval.gil_drop_request))
954 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Give another thread a chance */
956 if (PyThreadState_Swap(NULL) != tstate)
957 Py_FatalError("ceval: tstate mix-up");
958 drop_gil(tstate);
959
960 /* Other threads may run now */
961
962 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700963
964 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600965 if (_Py_IsFinalizing() &&
966 !_Py_CURRENTLY_FINALIZING(tstate))
967 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700968 drop_gil(tstate);
969 PyThread_exit_thread();
970 }
971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (PyThreadState_Swap(tstate) != NULL)
973 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 }
975 /* Check for asynchronous exceptions. */
976 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400977 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 tstate->async_exc = NULL;
979 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400980 PyErr_SetNone(exc);
981 Py_DECREF(exc);
982 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 }
984 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 fast_next_opcode:
987 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000988
Łukasz Langaa785c872016-09-09 17:37:37 -0700989 if (PyDTrace_LINE_ENABLED())
990 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -0500995 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400996 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 /* see maybe_call_line_trace
998 for expository comments */
999 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 err = maybe_call_line_trace(tstate->c_tracefunc,
1002 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001003 tstate, f,
1004 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* Reload possibly changed frame fields */
1006 JUMPTO(f->f_lasti);
1007 if (f->f_stacktop != NULL) {
1008 stack_pointer = f->f_stacktop;
1009 f->f_stacktop = NULL;
1010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001011 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001013 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001017
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001018 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001019 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001020#ifdef DYNAMIC_EXECUTION_PROFILE
1021#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 dxpairs[lastopcode][opcode]++;
1023 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001024#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001026#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001027
Guido van Rossum96a42c81992-01-12 02:29:51 +00001028#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (lltrace) {
1032 if (HAS_ARG(opcode)) {
1033 printf("%d: %d, %d\n",
1034 f->f_lasti, opcode, oparg);
1035 }
1036 else {
1037 printf("%d: %d\n",
1038 f->f_lasti, opcode);
1039 }
1040 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001041#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001046 It is essential that any operation that fails must goto error
1047 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 TARGET(NOP)
1050 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001051
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001052 TARGET(LOAD_FAST) {
1053 PyObject *value = GETLOCAL(oparg);
1054 if (value == NULL) {
1055 format_exc_check_arg(PyExc_UnboundLocalError,
1056 UNBOUNDLOCAL_ERROR_MSG,
1057 PyTuple_GetItem(co->co_varnames, oparg));
1058 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 Py_INCREF(value);
1061 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001063 }
1064
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001065 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001066 TARGET(LOAD_CONST) {
1067 PyObject *value = GETITEM(consts, oparg);
1068 Py_INCREF(value);
1069 PUSH(value);
1070 FAST_DISPATCH();
1071 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001072
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001073 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 TARGET(STORE_FAST) {
1075 PyObject *value = POP();
1076 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001078 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001080 TARGET(POP_TOP) {
1081 PyObject *value = POP();
1082 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001084 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001086 TARGET(ROT_TWO) {
1087 PyObject *top = TOP();
1088 PyObject *second = SECOND();
1089 SET_TOP(second);
1090 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001093
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001094 TARGET(ROT_THREE) {
1095 PyObject *top = TOP();
1096 PyObject *second = SECOND();
1097 PyObject *third = THIRD();
1098 SET_TOP(second);
1099 SET_SECOND(third);
1100 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001104 TARGET(ROT_FOUR) {
1105 PyObject *top = TOP();
1106 PyObject *second = SECOND();
1107 PyObject *third = THIRD();
1108 PyObject *fourth = FOURTH();
1109 SET_TOP(second);
1110 SET_SECOND(third);
1111 SET_THIRD(fourth);
1112 SET_FOURTH(top);
1113 FAST_DISPATCH();
1114 }
1115
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001116 TARGET(DUP_TOP) {
1117 PyObject *top = TOP();
1118 Py_INCREF(top);
1119 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001121 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001123 TARGET(DUP_TOP_TWO) {
1124 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001125 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001126 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001128 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 SET_TOP(top);
1130 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001131 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001133
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 TARGET(UNARY_POSITIVE) {
1135 PyObject *value = TOP();
1136 PyObject *res = PyNumber_Positive(value);
1137 Py_DECREF(value);
1138 SET_TOP(res);
1139 if (res == NULL)
1140 goto error;
1141 DISPATCH();
1142 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001143
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 TARGET(UNARY_NEGATIVE) {
1145 PyObject *value = TOP();
1146 PyObject *res = PyNumber_Negative(value);
1147 Py_DECREF(value);
1148 SET_TOP(res);
1149 if (res == NULL)
1150 goto error;
1151 DISPATCH();
1152 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001154 TARGET(UNARY_NOT) {
1155 PyObject *value = TOP();
1156 int err = PyObject_IsTrue(value);
1157 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (err == 0) {
1159 Py_INCREF(Py_True);
1160 SET_TOP(Py_True);
1161 DISPATCH();
1162 }
1163 else if (err > 0) {
1164 Py_INCREF(Py_False);
1165 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 DISPATCH();
1167 }
1168 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 goto error;
1170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 TARGET(UNARY_INVERT) {
1173 PyObject *value = TOP();
1174 PyObject *res = PyNumber_Invert(value);
1175 Py_DECREF(value);
1176 SET_TOP(res);
1177 if (res == NULL)
1178 goto error;
1179 DISPATCH();
1180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 TARGET(BINARY_POWER) {
1183 PyObject *exp = POP();
1184 PyObject *base = TOP();
1185 PyObject *res = PyNumber_Power(base, exp, Py_None);
1186 Py_DECREF(base);
1187 Py_DECREF(exp);
1188 SET_TOP(res);
1189 if (res == NULL)
1190 goto error;
1191 DISPATCH();
1192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001194 TARGET(BINARY_MULTIPLY) {
1195 PyObject *right = POP();
1196 PyObject *left = TOP();
1197 PyObject *res = PyNumber_Multiply(left, right);
1198 Py_DECREF(left);
1199 Py_DECREF(right);
1200 SET_TOP(res);
1201 if (res == NULL)
1202 goto error;
1203 DISPATCH();
1204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Benjamin Petersond51374e2014-04-09 23:55:56 -04001206 TARGET(BINARY_MATRIX_MULTIPLY) {
1207 PyObject *right = POP();
1208 PyObject *left = TOP();
1209 PyObject *res = PyNumber_MatrixMultiply(left, right);
1210 Py_DECREF(left);
1211 Py_DECREF(right);
1212 SET_TOP(res);
1213 if (res == NULL)
1214 goto error;
1215 DISPATCH();
1216 }
1217
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 TARGET(BINARY_TRUE_DIVIDE) {
1219 PyObject *divisor = POP();
1220 PyObject *dividend = TOP();
1221 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1222 Py_DECREF(dividend);
1223 Py_DECREF(divisor);
1224 SET_TOP(quotient);
1225 if (quotient == NULL)
1226 goto error;
1227 DISPATCH();
1228 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 TARGET(BINARY_FLOOR_DIVIDE) {
1231 PyObject *divisor = POP();
1232 PyObject *dividend = TOP();
1233 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1234 Py_DECREF(dividend);
1235 Py_DECREF(divisor);
1236 SET_TOP(quotient);
1237 if (quotient == NULL)
1238 goto error;
1239 DISPATCH();
1240 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001241
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 TARGET(BINARY_MODULO) {
1243 PyObject *divisor = POP();
1244 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001245 PyObject *res;
1246 if (PyUnicode_CheckExact(dividend) && (
1247 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1248 // fast path; string formatting, but not if the RHS is a str subclass
1249 // (see issue28598)
1250 res = PyUnicode_Format(dividend, divisor);
1251 } else {
1252 res = PyNumber_Remainder(dividend, divisor);
1253 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 Py_DECREF(divisor);
1255 Py_DECREF(dividend);
1256 SET_TOP(res);
1257 if (res == NULL)
1258 goto error;
1259 DISPATCH();
1260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 TARGET(BINARY_ADD) {
1263 PyObject *right = POP();
1264 PyObject *left = TOP();
1265 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001266 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1267 CPython using bytecode, it is simply worthless.
1268 See http://bugs.python.org/issue21955 and
1269 http://bugs.python.org/issue10044 for the discussion. In short,
1270 no patch shown any impact on a realistic benchmark, only a minor
1271 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 if (PyUnicode_CheckExact(left) &&
1273 PyUnicode_CheckExact(right)) {
1274 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001275 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001276 }
1277 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 sum = PyNumber_Add(left, right);
1279 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 Py_DECREF(right);
1282 SET_TOP(sum);
1283 if (sum == NULL)
1284 goto error;
1285 DISPATCH();
1286 }
1287
1288 TARGET(BINARY_SUBTRACT) {
1289 PyObject *right = POP();
1290 PyObject *left = TOP();
1291 PyObject *diff = PyNumber_Subtract(left, right);
1292 Py_DECREF(right);
1293 Py_DECREF(left);
1294 SET_TOP(diff);
1295 if (diff == NULL)
1296 goto error;
1297 DISPATCH();
1298 }
1299
1300 TARGET(BINARY_SUBSCR) {
1301 PyObject *sub = POP();
1302 PyObject *container = TOP();
1303 PyObject *res = PyObject_GetItem(container, sub);
1304 Py_DECREF(container);
1305 Py_DECREF(sub);
1306 SET_TOP(res);
1307 if (res == NULL)
1308 goto error;
1309 DISPATCH();
1310 }
1311
1312 TARGET(BINARY_LSHIFT) {
1313 PyObject *right = POP();
1314 PyObject *left = TOP();
1315 PyObject *res = PyNumber_Lshift(left, right);
1316 Py_DECREF(left);
1317 Py_DECREF(right);
1318 SET_TOP(res);
1319 if (res == NULL)
1320 goto error;
1321 DISPATCH();
1322 }
1323
1324 TARGET(BINARY_RSHIFT) {
1325 PyObject *right = POP();
1326 PyObject *left = TOP();
1327 PyObject *res = PyNumber_Rshift(left, right);
1328 Py_DECREF(left);
1329 Py_DECREF(right);
1330 SET_TOP(res);
1331 if (res == NULL)
1332 goto error;
1333 DISPATCH();
1334 }
1335
1336 TARGET(BINARY_AND) {
1337 PyObject *right = POP();
1338 PyObject *left = TOP();
1339 PyObject *res = PyNumber_And(left, right);
1340 Py_DECREF(left);
1341 Py_DECREF(right);
1342 SET_TOP(res);
1343 if (res == NULL)
1344 goto error;
1345 DISPATCH();
1346 }
1347
1348 TARGET(BINARY_XOR) {
1349 PyObject *right = POP();
1350 PyObject *left = TOP();
1351 PyObject *res = PyNumber_Xor(left, right);
1352 Py_DECREF(left);
1353 Py_DECREF(right);
1354 SET_TOP(res);
1355 if (res == NULL)
1356 goto error;
1357 DISPATCH();
1358 }
1359
1360 TARGET(BINARY_OR) {
1361 PyObject *right = POP();
1362 PyObject *left = TOP();
1363 PyObject *res = PyNumber_Or(left, right);
1364 Py_DECREF(left);
1365 Py_DECREF(right);
1366 SET_TOP(res);
1367 if (res == NULL)
1368 goto error;
1369 DISPATCH();
1370 }
1371
1372 TARGET(LIST_APPEND) {
1373 PyObject *v = POP();
1374 PyObject *list = PEEK(oparg);
1375 int err;
1376 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 if (err != 0)
1379 goto error;
1380 PREDICT(JUMP_ABSOLUTE);
1381 DISPATCH();
1382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 TARGET(SET_ADD) {
1385 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001386 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 int err;
1388 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 if (err != 0)
1391 goto error;
1392 PREDICT(JUMP_ABSOLUTE);
1393 DISPATCH();
1394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(INPLACE_POWER) {
1397 PyObject *exp = POP();
1398 PyObject *base = TOP();
1399 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1400 Py_DECREF(base);
1401 Py_DECREF(exp);
1402 SET_TOP(res);
1403 if (res == NULL)
1404 goto error;
1405 DISPATCH();
1406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 TARGET(INPLACE_MULTIPLY) {
1409 PyObject *right = POP();
1410 PyObject *left = TOP();
1411 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1412 Py_DECREF(left);
1413 Py_DECREF(right);
1414 SET_TOP(res);
1415 if (res == NULL)
1416 goto error;
1417 DISPATCH();
1418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Benjamin Petersond51374e2014-04-09 23:55:56 -04001420 TARGET(INPLACE_MATRIX_MULTIPLY) {
1421 PyObject *right = POP();
1422 PyObject *left = TOP();
1423 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1424 Py_DECREF(left);
1425 Py_DECREF(right);
1426 SET_TOP(res);
1427 if (res == NULL)
1428 goto error;
1429 DISPATCH();
1430 }
1431
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 TARGET(INPLACE_TRUE_DIVIDE) {
1433 PyObject *divisor = POP();
1434 PyObject *dividend = TOP();
1435 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1436 Py_DECREF(dividend);
1437 Py_DECREF(divisor);
1438 SET_TOP(quotient);
1439 if (quotient == NULL)
1440 goto error;
1441 DISPATCH();
1442 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 TARGET(INPLACE_FLOOR_DIVIDE) {
1445 PyObject *divisor = POP();
1446 PyObject *dividend = TOP();
1447 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1448 Py_DECREF(dividend);
1449 Py_DECREF(divisor);
1450 SET_TOP(quotient);
1451 if (quotient == NULL)
1452 goto error;
1453 DISPATCH();
1454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 TARGET(INPLACE_MODULO) {
1457 PyObject *right = POP();
1458 PyObject *left = TOP();
1459 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1460 Py_DECREF(left);
1461 Py_DECREF(right);
1462 SET_TOP(mod);
1463 if (mod == NULL)
1464 goto error;
1465 DISPATCH();
1466 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 TARGET(INPLACE_ADD) {
1469 PyObject *right = POP();
1470 PyObject *left = TOP();
1471 PyObject *sum;
1472 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1473 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001474 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001475 }
1476 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 sum = PyNumber_InPlaceAdd(left, right);
1478 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 Py_DECREF(right);
1481 SET_TOP(sum);
1482 if (sum == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 TARGET(INPLACE_SUBTRACT) {
1488 PyObject *right = POP();
1489 PyObject *left = TOP();
1490 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1491 Py_DECREF(left);
1492 Py_DECREF(right);
1493 SET_TOP(diff);
1494 if (diff == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 TARGET(INPLACE_LSHIFT) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_InPlaceLshift(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 TARGET(INPLACE_RSHIFT) {
1512 PyObject *right = POP();
1513 PyObject *left = TOP();
1514 PyObject *res = PyNumber_InPlaceRshift(left, right);
1515 Py_DECREF(left);
1516 Py_DECREF(right);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(INPLACE_AND) {
1524 PyObject *right = POP();
1525 PyObject *left = TOP();
1526 PyObject *res = PyNumber_InPlaceAnd(left, right);
1527 Py_DECREF(left);
1528 Py_DECREF(right);
1529 SET_TOP(res);
1530 if (res == NULL)
1531 goto error;
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(INPLACE_XOR) {
1536 PyObject *right = POP();
1537 PyObject *left = TOP();
1538 PyObject *res = PyNumber_InPlaceXor(left, right);
1539 Py_DECREF(left);
1540 Py_DECREF(right);
1541 SET_TOP(res);
1542 if (res == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(INPLACE_OR) {
1548 PyObject *right = POP();
1549 PyObject *left = TOP();
1550 PyObject *res = PyNumber_InPlaceOr(left, right);
1551 Py_DECREF(left);
1552 Py_DECREF(right);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 TARGET(STORE_SUBSCR) {
1560 PyObject *sub = TOP();
1561 PyObject *container = SECOND();
1562 PyObject *v = THIRD();
1563 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001565 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 Py_DECREF(container);
1569 Py_DECREF(sub);
1570 if (err != 0)
1571 goto error;
1572 DISPATCH();
1573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 TARGET(DELETE_SUBSCR) {
1576 PyObject *sub = TOP();
1577 PyObject *container = SECOND();
1578 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001580 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 err = PyObject_DelItem(container, sub);
1582 Py_DECREF(container);
1583 Py_DECREF(sub);
1584 if (err != 0)
1585 goto error;
1586 DISPATCH();
1587 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001588
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001590 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001592 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001593 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 PyErr_SetString(PyExc_RuntimeError,
1596 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 Py_DECREF(value);
1598 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001600 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001601 Py_DECREF(value);
1602 if (res == NULL)
1603 goto error;
1604 Py_DECREF(res);
1605 DISPATCH();
1606 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001607
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001608 TARGET(RAISE_VARARGS) {
1609 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 switch (oparg) {
1611 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001612 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001613 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001616 /* fall through */
1617 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001619 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 break;
1622 default:
1623 PyErr_SetString(PyExc_SystemError,
1624 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 break;
1626 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 goto error;
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001632 assert(f->f_iblock == 0);
1633 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001635
Yury Selivanov75445082015-05-11 22:57:16 -04001636 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001637 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001638 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001639 PyObject *obj = TOP();
1640 PyTypeObject *type = Py_TYPE(obj);
1641
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001642 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001643 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001644 }
Yury Selivanov75445082015-05-11 22:57:16 -04001645
1646 if (getter != NULL) {
1647 iter = (*getter)(obj);
1648 Py_DECREF(obj);
1649 if (iter == NULL) {
1650 SET_TOP(NULL);
1651 goto error;
1652 }
1653 }
1654 else {
1655 SET_TOP(NULL);
1656 PyErr_Format(
1657 PyExc_TypeError,
1658 "'async for' requires an object with "
1659 "__aiter__ method, got %.100s",
1660 type->tp_name);
1661 Py_DECREF(obj);
1662 goto error;
1663 }
1664
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001665 if (Py_TYPE(iter)->tp_as_async == NULL ||
1666 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001667
Yury Selivanov398ff912017-03-02 22:20:00 -05001668 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001669 PyErr_Format(
1670 PyExc_TypeError,
1671 "'async for' received an object from __aiter__ "
1672 "that does not implement __anext__: %.100s",
1673 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001674 Py_DECREF(iter);
1675 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001676 }
1677
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001678 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001679 DISPATCH();
1680 }
1681
1682 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001683 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001684 PyObject *next_iter = NULL;
1685 PyObject *awaitable = NULL;
1686 PyObject *aiter = TOP();
1687 PyTypeObject *type = Py_TYPE(aiter);
1688
Yury Selivanoveb636452016-09-08 22:01:51 -07001689 if (PyAsyncGen_CheckExact(aiter)) {
1690 awaitable = type->tp_as_async->am_anext(aiter);
1691 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001692 goto error;
1693 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001694 } else {
1695 if (type->tp_as_async != NULL){
1696 getter = type->tp_as_async->am_anext;
1697 }
Yury Selivanov75445082015-05-11 22:57:16 -04001698
Yury Selivanoveb636452016-09-08 22:01:51 -07001699 if (getter != NULL) {
1700 next_iter = (*getter)(aiter);
1701 if (next_iter == NULL) {
1702 goto error;
1703 }
1704 }
1705 else {
1706 PyErr_Format(
1707 PyExc_TypeError,
1708 "'async for' requires an iterator with "
1709 "__anext__ method, got %.100s",
1710 type->tp_name);
1711 goto error;
1712 }
Yury Selivanov75445082015-05-11 22:57:16 -04001713
Yury Selivanoveb636452016-09-08 22:01:51 -07001714 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1715 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001716 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001717 PyExc_TypeError,
1718 "'async for' received an invalid object "
1719 "from __anext__: %.100s",
1720 Py_TYPE(next_iter)->tp_name);
1721
1722 Py_DECREF(next_iter);
1723 goto error;
1724 } else {
1725 Py_DECREF(next_iter);
1726 }
1727 }
Yury Selivanov75445082015-05-11 22:57:16 -04001728
1729 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001730 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001731 DISPATCH();
1732 }
1733
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001734 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001735 TARGET(GET_AWAITABLE) {
1736 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001737 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001738
1739 Py_DECREF(iterable);
1740
Yury Selivanovc724bae2016-03-02 11:30:46 -05001741 if (iter != NULL && PyCoro_CheckExact(iter)) {
1742 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1743 if (yf != NULL) {
1744 /* `iter` is a coroutine object that is being
1745 awaited, `yf` is a pointer to the current awaitable
1746 being awaited on. */
1747 Py_DECREF(yf);
1748 Py_CLEAR(iter);
1749 PyErr_SetString(
1750 PyExc_RuntimeError,
1751 "coroutine is being awaited already");
1752 /* The code below jumps to `error` if `iter` is NULL. */
1753 }
1754 }
1755
Yury Selivanov75445082015-05-11 22:57:16 -04001756 SET_TOP(iter); /* Even if it's NULL */
1757
1758 if (iter == NULL) {
1759 goto error;
1760 }
1761
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001762 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001763 DISPATCH();
1764 }
1765
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 TARGET(YIELD_FROM) {
1767 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001768 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001770 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1771 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001772 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001773 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001775 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001776 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001777 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001778 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 Py_DECREF(v);
1780 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001781 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001782 if (tstate->c_tracefunc != NULL
1783 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001784 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001785 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 if (err < 0)
1787 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001788 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 SET_TOP(val);
1790 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001791 }
Martin Panter95f53c12016-07-18 08:23:26 +00001792 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001793 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001794 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001795 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001796 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001797 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001799
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001802
1803 if (co->co_flags & CO_ASYNC_GENERATOR) {
1804 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1805 Py_DECREF(retval);
1806 if (w == NULL) {
1807 retval = NULL;
1808 goto error;
1809 }
1810 retval = w;
1811 }
1812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001814 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 TARGET(POP_EXCEPT) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001818 PyObject *type, *value, *traceback;
1819 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 PyTryBlock *b = PyFrame_BlockPop(f);
1821 if (b->b_type != EXCEPT_HANDLER) {
1822 PyErr_SetString(PyExc_SystemError,
1823 "popped block is not an except handler");
1824 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001826 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1827 STACK_LEVEL() <= (b)->b_level + 4);
1828 exc_info = tstate->exc_info;
1829 type = exc_info->exc_type;
1830 value = exc_info->exc_value;
1831 traceback = exc_info->exc_traceback;
1832 exc_info->exc_type = POP();
1833 exc_info->exc_value = POP();
1834 exc_info->exc_traceback = POP();
1835 Py_XDECREF(type);
1836 Py_XDECREF(value);
1837 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001840
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001841 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 TARGET(POP_BLOCK) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001843 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001846
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001847 TARGET(POP_FINALLY) {
1848 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1849 Either:
1850 - TOP = NULL or an integer
1851 or:
1852 - (TOP, SECOND, THIRD) = exc_info()
1853 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1854
1855 If oparg is 1 the value for 'return' was additionally pushed
1856 at the top of the stack.
1857 */
1858 PyObject *res = NULL;
1859 if (oparg) {
1860 res = POP();
1861 }
1862 PyObject *exc = POP();
1863 if (exc == NULL || PyLong_CheckExact(exc)) {
1864 Py_XDECREF(exc);
1865 }
1866 else {
1867 Py_DECREF(exc);
1868 Py_DECREF(POP());
1869 Py_DECREF(POP());
1870
1871 PyObject *type, *value, *traceback;
1872 _PyErr_StackItem *exc_info;
1873 PyTryBlock *b = PyFrame_BlockPop(f);
1874 if (b->b_type != EXCEPT_HANDLER) {
1875 PyErr_SetString(PyExc_SystemError,
1876 "popped block is not an except handler");
1877 Py_XDECREF(res);
1878 goto error;
1879 }
1880 assert(STACK_LEVEL() == (b)->b_level + 3);
1881 exc_info = tstate->exc_info;
1882 type = exc_info->exc_type;
1883 value = exc_info->exc_value;
1884 traceback = exc_info->exc_traceback;
1885 exc_info->exc_type = POP();
1886 exc_info->exc_value = POP();
1887 exc_info->exc_traceback = POP();
1888 Py_XDECREF(type);
1889 Py_XDECREF(value);
1890 Py_XDECREF(traceback);
1891 }
1892 if (oparg) {
1893 PUSH(res);
1894 }
1895 DISPATCH();
1896 }
1897
1898 TARGET(CALL_FINALLY) {
1899 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1900 if (ret == NULL) {
1901 goto error;
1902 }
1903 PUSH(ret);
1904 JUMPBY(oparg);
1905 FAST_DISPATCH();
1906 }
1907
1908 TARGET(BEGIN_FINALLY) {
1909 /* Push NULL onto the stack for using it in END_FINALLY,
1910 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1911 */
1912 PUSH(NULL);
1913 FAST_DISPATCH();
1914 }
1915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 TARGET(END_FINALLY) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001918 /* At the top of the stack are 1 or 6 values:
1919 Either:
1920 - TOP = NULL or an integer
1921 or:
1922 - (TOP, SECOND, THIRD) = exc_info()
1923 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1924 */
1925 PyObject *exc = POP();
1926 if (exc == NULL) {
1927 FAST_DISPATCH();
1928 }
1929 else if (PyLong_CheckExact(exc)) {
1930 int ret = _PyLong_AsInt(exc);
1931 Py_DECREF(exc);
1932 if (ret == -1 && PyErr_Occurred()) {
1933 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001935 JUMPTO(ret);
1936 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001938 else {
1939 assert(PyExceptionClass_Check(exc));
1940 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001942 PyErr_Restore(exc, val, tb);
1943 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001948 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001949
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001951 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001952 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1953 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001954 PyErr_SetString(PyExc_NameError,
1955 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001956 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001959 }
1960 else {
1961 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1962 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001963 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1965 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001966 if (PyErr_ExceptionMatches(PyExc_KeyError))
1967 PyErr_SetString(PyExc_NameError,
1968 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001973 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001974 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001975
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 TARGET(STORE_NAME) {
1977 PyObject *name = GETITEM(names, oparg);
1978 PyObject *v = POP();
1979 PyObject *ns = f->f_locals;
1980 int err;
1981 if (ns == NULL) {
1982 PyErr_Format(PyExc_SystemError,
1983 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 if (PyDict_CheckExact(ns))
1988 err = PyDict_SetItem(ns, name, v);
1989 else
1990 err = PyObject_SetItem(ns, name, v);
1991 Py_DECREF(v);
1992 if (err != 0)
1993 goto error;
1994 DISPATCH();
1995 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 TARGET(DELETE_NAME) {
1998 PyObject *name = GETITEM(names, oparg);
1999 PyObject *ns = f->f_locals;
2000 int err;
2001 if (ns == NULL) {
2002 PyErr_Format(PyExc_SystemError,
2003 "no locals when deleting %R", name);
2004 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 err = PyObject_DelItem(ns, name);
2007 if (err != 0) {
2008 format_exc_check_arg(PyExc_NameError,
2009 NAME_ERROR_MSG,
2010 name);
2011 goto error;
2012 }
2013 DISPATCH();
2014 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002015
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002016 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 TARGET(UNPACK_SEQUENCE) {
2018 PyObject *seq = POP(), *item, **items;
2019 if (PyTuple_CheckExact(seq) &&
2020 PyTuple_GET_SIZE(seq) == oparg) {
2021 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 item = items[oparg];
2024 Py_INCREF(item);
2025 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 } else if (PyList_CheckExact(seq) &&
2028 PyList_GET_SIZE(seq) == oparg) {
2029 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 item = items[oparg];
2032 Py_INCREF(item);
2033 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 stack_pointer + oparg)) {
2037 STACKADJ(oparg);
2038 } else {
2039 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 Py_DECREF(seq);
2041 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002044 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002046
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 TARGET(UNPACK_EX) {
2048 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2049 PyObject *seq = POP();
2050
2051 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2052 stack_pointer + totalargs)) {
2053 stack_pointer += totalargs;
2054 } else {
2055 Py_DECREF(seq);
2056 goto error;
2057 }
2058 Py_DECREF(seq);
2059 DISPATCH();
2060 }
2061
2062 TARGET(STORE_ATTR) {
2063 PyObject *name = GETITEM(names, oparg);
2064 PyObject *owner = TOP();
2065 PyObject *v = SECOND();
2066 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 Py_DECREF(owner);
2071 if (err != 0)
2072 goto error;
2073 DISPATCH();
2074 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002075
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 TARGET(DELETE_ATTR) {
2077 PyObject *name = GETITEM(names, oparg);
2078 PyObject *owner = POP();
2079 int err;
2080 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2081 Py_DECREF(owner);
2082 if (err != 0)
2083 goto error;
2084 DISPATCH();
2085 }
2086
2087 TARGET(STORE_GLOBAL) {
2088 PyObject *name = GETITEM(names, oparg);
2089 PyObject *v = POP();
2090 int err;
2091 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 if (err != 0)
2094 goto error;
2095 DISPATCH();
2096 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 TARGET(DELETE_GLOBAL) {
2099 PyObject *name = GETITEM(names, oparg);
2100 int err;
2101 err = PyDict_DelItem(f->f_globals, name);
2102 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002104 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002106 }
2107 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002109
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 TARGET(LOAD_NAME) {
2111 PyObject *name = GETITEM(names, oparg);
2112 PyObject *locals = f->f_locals;
2113 PyObject *v;
2114 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 "no locals when loading %R", name);
2117 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 if (PyDict_CheckExact(locals)) {
2120 v = PyDict_GetItem(locals, name);
2121 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
2123 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002125 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002126 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 PyErr_Clear();
2129 }
2130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 if (v == NULL) {
2132 v = PyDict_GetItem(f->f_globals, name);
2133 Py_XINCREF(v);
2134 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002135 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 v = PyDict_GetItem(f->f_builtins, name);
2137 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002138 format_exc_check_arg(
2139 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 NAME_ERROR_MSG, name);
2141 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002142 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002144 }
2145 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 v = PyObject_GetItem(f->f_builtins, name);
2147 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002148 if (PyErr_ExceptionMatches(PyExc_KeyError))
2149 format_exc_check_arg(
2150 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 NAME_ERROR_MSG, name);
2152 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002153 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002160
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 TARGET(LOAD_GLOBAL) {
2162 PyObject *name = GETITEM(names, oparg);
2163 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002164 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002165 && PyDict_CheckExact(f->f_builtins))
2166 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002168 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 name);
2170 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002171 if (!_PyErr_OCCURRED()) {
2172 /* _PyDict_LoadGlobal() returns NULL without raising
2173 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002174 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002175 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 else {
2182 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002183
2184 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 v = PyObject_GetItem(f->f_globals, name);
2186 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002187 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2188 goto error;
2189 PyErr_Clear();
2190
Victor Stinnerb4efc962015-11-20 09:24:02 +01002191 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 v = PyObject_GetItem(f->f_builtins, name);
2193 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 if (PyErr_ExceptionMatches(PyExc_KeyError))
2195 format_exc_check_arg(
2196 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002197 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002199 }
2200 }
2201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002205
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 TARGET(DELETE_FAST) {
2207 PyObject *v = GETLOCAL(oparg);
2208 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 SETLOCAL(oparg, NULL);
2210 DISPATCH();
2211 }
2212 format_exc_check_arg(
2213 PyExc_UnboundLocalError,
2214 UNBOUNDLOCAL_ERROR_MSG,
2215 PyTuple_GetItem(co->co_varnames, oparg)
2216 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 goto error;
2218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 TARGET(DELETE_DEREF) {
2221 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002222 PyObject *oldobj = PyCell_GET(cell);
2223 if (oldobj != NULL) {
2224 PyCell_SET(cell, NULL);
2225 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002226 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002227 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002228 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 goto error;
2230 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 TARGET(LOAD_CLOSURE) {
2233 PyObject *cell = freevars[oparg];
2234 Py_INCREF(cell);
2235 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002238
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002239 TARGET(LOAD_CLASSDEREF) {
2240 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002241 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002242 assert(locals);
2243 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2244 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2245 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2246 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2247 if (PyDict_CheckExact(locals)) {
2248 value = PyDict_GetItem(locals, name);
2249 Py_XINCREF(value);
2250 }
2251 else {
2252 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002253 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002254 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2255 goto error;
2256 PyErr_Clear();
2257 }
2258 }
2259 if (!value) {
2260 PyObject *cell = freevars[oparg];
2261 value = PyCell_GET(cell);
2262 if (value == NULL) {
2263 format_exc_unbound(co, oparg);
2264 goto error;
2265 }
2266 Py_INCREF(value);
2267 }
2268 PUSH(value);
2269 DISPATCH();
2270 }
2271
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 TARGET(LOAD_DEREF) {
2273 PyObject *cell = freevars[oparg];
2274 PyObject *value = PyCell_GET(cell);
2275 if (value == NULL) {
2276 format_exc_unbound(co, oparg);
2277 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 Py_INCREF(value);
2280 PUSH(value);
2281 DISPATCH();
2282 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 TARGET(STORE_DEREF) {
2285 PyObject *v = POP();
2286 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002287 PyObject *oldobj = PyCell_GET(cell);
2288 PyCell_SET(cell, v);
2289 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 DISPATCH();
2291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002293 TARGET(BUILD_STRING) {
2294 PyObject *str;
2295 PyObject *empty = PyUnicode_New(0, 0);
2296 if (empty == NULL) {
2297 goto error;
2298 }
2299 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2300 Py_DECREF(empty);
2301 if (str == NULL)
2302 goto error;
2303 while (--oparg >= 0) {
2304 PyObject *item = POP();
2305 Py_DECREF(item);
2306 }
2307 PUSH(str);
2308 DISPATCH();
2309 }
2310
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 TARGET(BUILD_TUPLE) {
2312 PyObject *tup = PyTuple_New(oparg);
2313 if (tup == NULL)
2314 goto error;
2315 while (--oparg >= 0) {
2316 PyObject *item = POP();
2317 PyTuple_SET_ITEM(tup, oparg, item);
2318 }
2319 PUSH(tup);
2320 DISPATCH();
2321 }
2322
2323 TARGET(BUILD_LIST) {
2324 PyObject *list = PyList_New(oparg);
2325 if (list == NULL)
2326 goto error;
2327 while (--oparg >= 0) {
2328 PyObject *item = POP();
2329 PyList_SET_ITEM(list, oparg, item);
2330 }
2331 PUSH(list);
2332 DISPATCH();
2333 }
2334
Serhiy Storchaka73442852016-10-02 10:33:46 +03002335 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002336 TARGET(BUILD_TUPLE_UNPACK)
2337 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002338 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002339 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002340 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002341 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002342
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002343 if (sum == NULL)
2344 goto error;
2345
2346 for (i = oparg; i > 0; i--) {
2347 PyObject *none_val;
2348
2349 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2350 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002351 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002352 PyErr_ExceptionMatches(PyExc_TypeError))
2353 {
2354 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002355 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002356 Py_DECREF(sum);
2357 goto error;
2358 }
2359 Py_DECREF(none_val);
2360 }
2361
2362 if (convert_to_tuple) {
2363 return_value = PyList_AsTuple(sum);
2364 Py_DECREF(sum);
2365 if (return_value == NULL)
2366 goto error;
2367 }
2368 else {
2369 return_value = sum;
2370 }
2371
2372 while (oparg--)
2373 Py_DECREF(POP());
2374 PUSH(return_value);
2375 DISPATCH();
2376 }
2377
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 TARGET(BUILD_SET) {
2379 PyObject *set = PySet_New(NULL);
2380 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002381 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 if (set == NULL)
2383 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002384 for (i = oparg; i > 0; i--) {
2385 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 if (err == 0)
2387 err = PySet_Add(set, item);
2388 Py_DECREF(item);
2389 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002390 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 if (err != 0) {
2392 Py_DECREF(set);
2393 goto error;
2394 }
2395 PUSH(set);
2396 DISPATCH();
2397 }
2398
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002399 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002400 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002401 PyObject *sum = PySet_New(NULL);
2402 if (sum == NULL)
2403 goto error;
2404
2405 for (i = oparg; i > 0; i--) {
2406 if (_PySet_Update(sum, PEEK(i)) < 0) {
2407 Py_DECREF(sum);
2408 goto error;
2409 }
2410 }
2411
2412 while (oparg--)
2413 Py_DECREF(POP());
2414 PUSH(sum);
2415 DISPATCH();
2416 }
2417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002419 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2421 if (map == NULL)
2422 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002423 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002424 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002425 PyObject *key = PEEK(2*i);
2426 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002427 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002428 if (err != 0) {
2429 Py_DECREF(map);
2430 goto error;
2431 }
2432 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002433
2434 while (oparg--) {
2435 Py_DECREF(POP());
2436 Py_DECREF(POP());
2437 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 PUSH(map);
2439 DISPATCH();
2440 }
2441
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002442 TARGET(SETUP_ANNOTATIONS) {
2443 _Py_IDENTIFIER(__annotations__);
2444 int err;
2445 PyObject *ann_dict;
2446 if (f->f_locals == NULL) {
2447 PyErr_Format(PyExc_SystemError,
2448 "no locals found when setting up annotations");
2449 goto error;
2450 }
2451 /* check if __annotations__ in locals()... */
2452 if (PyDict_CheckExact(f->f_locals)) {
2453 ann_dict = _PyDict_GetItemId(f->f_locals,
2454 &PyId___annotations__);
2455 if (ann_dict == NULL) {
2456 /* ...if not, create a new one */
2457 ann_dict = PyDict_New();
2458 if (ann_dict == NULL) {
2459 goto error;
2460 }
2461 err = _PyDict_SetItemId(f->f_locals,
2462 &PyId___annotations__, ann_dict);
2463 Py_DECREF(ann_dict);
2464 if (err != 0) {
2465 goto error;
2466 }
2467 }
2468 }
2469 else {
2470 /* do the same if locals() is not a dict */
2471 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2472 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002473 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002474 }
2475 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2476 if (ann_dict == NULL) {
2477 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2478 goto error;
2479 }
2480 PyErr_Clear();
2481 ann_dict = PyDict_New();
2482 if (ann_dict == NULL) {
2483 goto error;
2484 }
2485 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2486 Py_DECREF(ann_dict);
2487 if (err != 0) {
2488 goto error;
2489 }
2490 }
2491 else {
2492 Py_DECREF(ann_dict);
2493 }
2494 }
2495 DISPATCH();
2496 }
2497
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002498 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002499 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002500 PyObject *map;
2501 PyObject *keys = TOP();
2502 if (!PyTuple_CheckExact(keys) ||
2503 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2504 PyErr_SetString(PyExc_SystemError,
2505 "bad BUILD_CONST_KEY_MAP keys argument");
2506 goto error;
2507 }
2508 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2509 if (map == NULL) {
2510 goto error;
2511 }
2512 for (i = oparg; i > 0; i--) {
2513 int err;
2514 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2515 PyObject *value = PEEK(i + 1);
2516 err = PyDict_SetItem(map, key, value);
2517 if (err != 0) {
2518 Py_DECREF(map);
2519 goto error;
2520 }
2521 }
2522
2523 Py_DECREF(POP());
2524 while (oparg--) {
2525 Py_DECREF(POP());
2526 }
2527 PUSH(map);
2528 DISPATCH();
2529 }
2530
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002531 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002532 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002533 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002534 if (sum == NULL)
2535 goto error;
2536
2537 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002538 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002539 if (PyDict_Update(sum, arg) < 0) {
2540 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2541 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002542 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002543 arg->ob_type->tp_name);
2544 }
2545 Py_DECREF(sum);
2546 goto error;
2547 }
2548 }
2549
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002550 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002551 Py_DECREF(POP());
2552 PUSH(sum);
2553 DISPATCH();
2554 }
2555
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002556 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2557 Py_ssize_t i;
2558 PyObject *sum = PyDict_New();
2559 if (sum == NULL)
2560 goto error;
2561
2562 for (i = oparg; i > 0; i--) {
2563 PyObject *arg = PEEK(i);
2564 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2565 PyObject *func = PEEK(2 + oparg);
2566 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002567 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002568 }
2569 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2570 PyObject *exc, *val, *tb;
2571 PyErr_Fetch(&exc, &val, &tb);
2572 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2573 PyObject *key = PyTuple_GET_ITEM(val, 0);
2574 if (!PyUnicode_Check(key)) {
2575 PyErr_Format(PyExc_TypeError,
2576 "%.200s%.200s keywords must be strings",
2577 PyEval_GetFuncName(func),
2578 PyEval_GetFuncDesc(func));
2579 } else {
2580 PyErr_Format(PyExc_TypeError,
2581 "%.200s%.200s got multiple "
2582 "values for keyword argument '%U'",
2583 PyEval_GetFuncName(func),
2584 PyEval_GetFuncDesc(func),
2585 key);
2586 }
2587 Py_XDECREF(exc);
2588 Py_XDECREF(val);
2589 Py_XDECREF(tb);
2590 }
2591 else {
2592 PyErr_Restore(exc, val, tb);
2593 }
2594 }
2595 Py_DECREF(sum);
2596 goto error;
2597 }
2598 }
2599
2600 while (oparg--)
2601 Py_DECREF(POP());
2602 PUSH(sum);
2603 DISPATCH();
2604 }
2605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 TARGET(MAP_ADD) {
2607 PyObject *key = TOP();
2608 PyObject *value = SECOND();
2609 PyObject *map;
2610 int err;
2611 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002612 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002614 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 Py_DECREF(value);
2616 Py_DECREF(key);
2617 if (err != 0)
2618 goto error;
2619 PREDICT(JUMP_ABSOLUTE);
2620 DISPATCH();
2621 }
2622
2623 TARGET(LOAD_ATTR) {
2624 PyObject *name = GETITEM(names, oparg);
2625 PyObject *owner = TOP();
2626 PyObject *res = PyObject_GetAttr(owner, name);
2627 Py_DECREF(owner);
2628 SET_TOP(res);
2629 if (res == NULL)
2630 goto error;
2631 DISPATCH();
2632 }
2633
2634 TARGET(COMPARE_OP) {
2635 PyObject *right = POP();
2636 PyObject *left = TOP();
2637 PyObject *res = cmp_outcome(oparg, left, right);
2638 Py_DECREF(left);
2639 Py_DECREF(right);
2640 SET_TOP(res);
2641 if (res == NULL)
2642 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 PREDICT(POP_JUMP_IF_FALSE);
2644 PREDICT(POP_JUMP_IF_TRUE);
2645 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002647
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002650 PyObject *fromlist = POP();
2651 PyObject *level = TOP();
2652 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002653 res = import_name(f, name, fromlist, level);
2654 Py_DECREF(level);
2655 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 SET_TOP(res);
2657 if (res == NULL)
2658 goto error;
2659 DISPATCH();
2660 }
2661
2662 TARGET(IMPORT_STAR) {
2663 PyObject *from = POP(), *locals;
2664 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002665 if (PyFrame_FastToLocalsWithError(f) < 0) {
2666 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002667 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002668 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002669
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 locals = f->f_locals;
2671 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 PyErr_SetString(PyExc_SystemError,
2673 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002674 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 Py_DECREF(from);
2680 if (err != 0)
2681 goto error;
2682 DISPATCH();
2683 }
Guido van Rossum25831651993-05-19 14:50:45 +00002684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 TARGET(IMPORT_FROM) {
2686 PyObject *name = GETITEM(names, oparg);
2687 PyObject *from = TOP();
2688 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 PUSH(res);
2691 if (res == NULL)
2692 goto error;
2693 DISPATCH();
2694 }
Thomas Wouters52152252000-08-17 22:55:00 +00002695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 JUMPBY(oparg);
2698 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002700
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002701 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 TARGET(POP_JUMP_IF_FALSE) {
2703 PyObject *cond = POP();
2704 int err;
2705 if (cond == Py_True) {
2706 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 FAST_DISPATCH();
2708 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 if (cond == Py_False) {
2710 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 JUMPTO(oparg);
2712 FAST_DISPATCH();
2713 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 err = PyObject_IsTrue(cond);
2715 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002717 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 else if (err == 0)
2719 JUMPTO(oparg);
2720 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002721 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002724
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002725 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 TARGET(POP_JUMP_IF_TRUE) {
2727 PyObject *cond = POP();
2728 int err;
2729 if (cond == Py_False) {
2730 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 FAST_DISPATCH();
2732 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 if (cond == Py_True) {
2734 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 JUMPTO(oparg);
2736 FAST_DISPATCH();
2737 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 err = PyObject_IsTrue(cond);
2739 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 JUMPTO(oparg);
2742 }
2743 else if (err == 0)
2744 ;
2745 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002749
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 TARGET(JUMP_IF_FALSE_OR_POP) {
2751 PyObject *cond = TOP();
2752 int err;
2753 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 FAST_DISPATCH();
2757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 JUMPTO(oparg);
2760 FAST_DISPATCH();
2761 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 if (err > 0) {
2764 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 }
2767 else if (err == 0)
2768 JUMPTO(oparg);
2769 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002773
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 TARGET(JUMP_IF_TRUE_OR_POP) {
2775 PyObject *cond = TOP();
2776 int err;
2777 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 FAST_DISPATCH();
2781 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 JUMPTO(oparg);
2784 FAST_DISPATCH();
2785 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 JUMPTO(oparg);
2789 }
2790 else if (err == 0) {
2791 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 }
2794 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002798
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002799 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002802#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 /* Enabling this path speeds-up all while and for-loops by bypassing
2804 the per-loop checks for signals. By default, this should be turned-off
2805 because it prevents detection of a control-break in tight loops like
2806 "while 1: pass". Compile with this option turned-on when you need
2807 the speed-up and do not need break checking inside tight loops (ones
2808 that contain only instructions ending with FAST_DISPATCH).
2809 */
2810 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002811#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002813#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002815
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002819 PyObject *iter = PyObject_GetIter(iterable);
2820 Py_DECREF(iterable);
2821 SET_TOP(iter);
2822 if (iter == NULL)
2823 goto error;
2824 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002825 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002826 DISPATCH();
2827 }
2828
2829 TARGET(GET_YIELD_FROM_ITER) {
2830 /* before: [obj]; after [getiter(obj)] */
2831 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002832 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002833 if (PyCoro_CheckExact(iterable)) {
2834 /* `iterable` is a coroutine */
2835 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2836 /* and it is used in a 'yield from' expression of a
2837 regular generator. */
2838 Py_DECREF(iterable);
2839 SET_TOP(NULL);
2840 PyErr_SetString(PyExc_TypeError,
2841 "cannot 'yield from' a coroutine object "
2842 "in a non-coroutine generator");
2843 goto error;
2844 }
2845 }
2846 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002847 /* `iterable` is not a generator. */
2848 iter = PyObject_GetIter(iterable);
2849 Py_DECREF(iterable);
2850 SET_TOP(iter);
2851 if (iter == NULL)
2852 goto error;
2853 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002854 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 DISPATCH();
2856 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002857
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002858 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 PyObject *iter = TOP();
2862 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2863 if (next != NULL) {
2864 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 PREDICT(STORE_FAST);
2866 PREDICT(UNPACK_SEQUENCE);
2867 DISPATCH();
2868 }
2869 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2871 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002872 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002873 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 PyErr_Clear();
2875 }
2876 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 STACKADJ(-1);
2878 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002880 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002883
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002884 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 /* NOTE: If you add any new block-setup opcodes that
2886 are not try/except/finally handlers, you may need
2887 to update the PyGen_NeedsFinalizing() function.
2888 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002889
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002890 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 STACK_LEVEL());
2892 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002894
Yury Selivanov75445082015-05-11 22:57:16 -04002895 TARGET(BEFORE_ASYNC_WITH) {
2896 _Py_IDENTIFIER(__aexit__);
2897 _Py_IDENTIFIER(__aenter__);
2898
2899 PyObject *mgr = TOP();
2900 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2901 *enter;
2902 PyObject *res;
2903 if (exit == NULL)
2904 goto error;
2905 SET_TOP(exit);
2906 enter = special_lookup(mgr, &PyId___aenter__);
2907 Py_DECREF(mgr);
2908 if (enter == NULL)
2909 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002910 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002911 Py_DECREF(enter);
2912 if (res == NULL)
2913 goto error;
2914 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002915 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002916 DISPATCH();
2917 }
2918
2919 TARGET(SETUP_ASYNC_WITH) {
2920 PyObject *res = POP();
2921 /* Setup the finally block before pushing the result
2922 of __aenter__ on the stack. */
2923 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2924 STACK_LEVEL());
2925 PUSH(res);
2926 DISPATCH();
2927 }
2928
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002930 _Py_IDENTIFIER(__exit__);
2931 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002933 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002935 if (enter == NULL)
2936 goto error;
2937 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002938 if (exit == NULL) {
2939 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002941 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002944 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 Py_DECREF(enter);
2946 if (res == NULL)
2947 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 /* Setup the finally block before pushing the result
2949 of __enter__ on the stack. */
2950 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2951 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002952
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 DISPATCH();
2955 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002956
Yury Selivanov75445082015-05-11 22:57:16 -04002957 TARGET(WITH_CLEANUP_START) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002958 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002960 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 - (TOP, SECOND, THIRD) = exc_info()
2962 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002963 Below them is EXIT, the context.__exit__ or context.__aexit__
2964 bound method.
2965 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002967 otherwise we must call
2968 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002969
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002970 In the first case, we remove EXIT from the
2971 stack, leaving TOP, and push TOP on the stack.
2972 Otherwise we shift the bottom 3 values of the
2973 stack down, replace the empty spot with NULL, and push
2974 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002975
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002976 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002978 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01002980 PyObject *exc, *val, *tb, *res;
2981
2982 val = tb = Py_None;
2983 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002984 if (exc == NULL) {
2985 STACKADJ(-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 }
2990 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002991 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 val = SECOND();
2995 tb = THIRD();
2996 tp2 = FOURTH();
2997 exc2 = PEEK(5);
2998 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 SET_VALUE(7, tb2);
3001 SET_VALUE(6, exc2);
3002 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3004 SET_FOURTH(NULL);
3005 /* We just shifted the stack down, so we have
3006 to tell the except handler block that the
3007 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003008 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 block = &f->f_blockstack[f->f_iblock - 1];
3010 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003011 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 block->b_level--;
3013 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003014
3015 stack[0] = exc;
3016 stack[1] = val;
3017 stack[2] = tb;
3018 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 if (res == NULL)
3021 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003022
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003023 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003024 PUSH(exc);
3025 PUSH(res);
3026 PREDICT(WITH_CLEANUP_FINISH);
3027 DISPATCH();
3028 }
3029
3030 PREDICTED(WITH_CLEANUP_FINISH);
3031 TARGET(WITH_CLEANUP_FINISH) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003032 /* TOP = the result of calling the context.__exit__ bound method
3033 SECOND = either None or exception type
3034
3035 If SECOND is None below is NULL or the return address,
3036 otherwise below are 7 values representing an exception.
3037 */
Yury Selivanov75445082015-05-11 22:57:16 -04003038 PyObject *res = POP();
3039 PyObject *exc = POP();
3040 int err;
3041
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 if (exc != Py_None)
3043 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 else
3045 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003046
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003048 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003053 /* There was an exception and a True return.
3054 * We must manually unwind the EXCEPT_HANDLER block
3055 * which was created when the exception was caught,
3056 * otherwise the stack will be in an inconsisten state.
3057 */
3058 PyTryBlock *b = PyFrame_BlockPop(f);
3059 assert(b->b_type == EXCEPT_HANDLER);
3060 UNWIND_EXCEPT_HANDLER(b);
3061 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 }
3063 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003066
Yury Selivanovf2392132016-12-13 19:03:51 -05003067 TARGET(LOAD_METHOD) {
3068 /* Designed to work in tamdem with CALL_METHOD. */
3069 PyObject *name = GETITEM(names, oparg);
3070 PyObject *obj = TOP();
3071 PyObject *meth = NULL;
3072
3073 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3074
Yury Selivanovf2392132016-12-13 19:03:51 -05003075 if (meth == NULL) {
3076 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003077 goto error;
3078 }
3079
3080 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003081 /* We can bypass temporary bound method object.
3082 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003083
INADA Naoki015bce62017-01-16 17:23:30 +09003084 meth | self | arg1 | ... | argN
3085 */
3086 SET_TOP(meth);
3087 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003088 }
3089 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003090 /* meth is not an unbound method (but a regular attr, or
3091 something was returned by a descriptor protocol). Set
3092 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003093 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003094
3095 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003096 */
INADA Naoki015bce62017-01-16 17:23:30 +09003097 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003098 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003099 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003100 }
3101 DISPATCH();
3102 }
3103
3104 TARGET(CALL_METHOD) {
3105 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003106 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003107
3108 sp = stack_pointer;
3109
INADA Naoki015bce62017-01-16 17:23:30 +09003110 meth = PEEK(oparg + 2);
3111 if (meth == NULL) {
3112 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3113 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003114
3115 Stack layout:
3116
INADA Naoki015bce62017-01-16 17:23:30 +09003117 ... | NULL | callable | arg1 | ... | argN
3118 ^- TOP()
3119 ^- (-oparg)
3120 ^- (-oparg-1)
3121 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003122
Ville Skyttä49b27342017-08-03 09:00:59 +03003123 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003124 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003125 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003126 res = call_function(&sp, oparg, NULL);
3127 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003128 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003129 }
3130 else {
3131 /* This is a method call. Stack layout:
3132
INADA Naoki015bce62017-01-16 17:23:30 +09003133 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003134 ^- TOP()
3135 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003136 ^- (-oparg-1)
3137 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003138
INADA Naoki015bce62017-01-16 17:23:30 +09003139 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003140 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003141 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003142 */
3143 res = call_function(&sp, oparg + 1, NULL);
3144 stack_pointer = sp;
3145 }
3146
3147 PUSH(res);
3148 if (res == NULL)
3149 goto error;
3150 DISPATCH();
3151 }
3152
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003153 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 TARGET(CALL_FUNCTION) {
3155 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003157 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003160 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003165
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003166 TARGET(CALL_FUNCTION_KW) {
3167 PyObject **sp, *res, *names;
3168
3169 names = POP();
3170 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003172 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003175 Py_DECREF(names);
3176
3177 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003179 }
3180 DISPATCH();
3181 }
3182
3183 TARGET(CALL_FUNCTION_EX) {
3184 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003185 if (oparg & 0x01) {
3186 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003187 if (!PyDict_CheckExact(kwargs)) {
3188 PyObject *d = PyDict_New();
3189 if (d == NULL)
3190 goto error;
3191 if (PyDict_Update(d, kwargs) != 0) {
3192 Py_DECREF(d);
3193 /* PyDict_Update raises attribute
3194 * error (percolated from an attempt
3195 * to get 'keys' attribute) instead of
3196 * a type error if its second argument
3197 * is not a mapping.
3198 */
3199 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003200 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003201 }
Victor Stinnereece2222016-09-12 11:16:37 +02003202 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003203 goto error;
3204 }
3205 Py_DECREF(kwargs);
3206 kwargs = d;
3207 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003208 assert(PyDict_CheckExact(kwargs));
3209 }
3210 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003211 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003212 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003213 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003214 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003215 goto error;
3216 }
3217 Py_SETREF(callargs, PySequence_Tuple(callargs));
3218 if (callargs == NULL) {
3219 goto error;
3220 }
3221 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003222 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003223
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003224 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003225 Py_DECREF(func);
3226 Py_DECREF(callargs);
3227 Py_XDECREF(kwargs);
3228
3229 SET_TOP(result);
3230 if (result == NULL) {
3231 goto error;
3232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003235
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003236 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003237 PyObject *qualname = POP();
3238 PyObject *codeobj = POP();
3239 PyFunctionObject *func = (PyFunctionObject *)
3240 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003241
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003242 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003244 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003247
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003248 if (oparg & 0x08) {
3249 assert(PyTuple_CheckExact(TOP()));
3250 func ->func_closure = POP();
3251 }
3252 if (oparg & 0x04) {
3253 assert(PyDict_CheckExact(TOP()));
3254 func->func_annotations = POP();
3255 }
3256 if (oparg & 0x02) {
3257 assert(PyDict_CheckExact(TOP()));
3258 func->func_kwdefaults = POP();
3259 }
3260 if (oparg & 0x01) {
3261 assert(PyTuple_CheckExact(TOP()));
3262 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003264
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003265 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003266 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003268
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 TARGET(BUILD_SLICE) {
3270 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003274 step = NULL;
3275 stop = POP();
3276 start = TOP();
3277 slice = PySlice_New(start, stop, step);
3278 Py_DECREF(start);
3279 Py_DECREF(stop);
3280 Py_XDECREF(step);
3281 SET_TOP(slice);
3282 if (slice == NULL)
3283 goto error;
3284 DISPATCH();
3285 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003286
Eric V. Smitha78c7952015-11-03 12:45:05 -05003287 TARGET(FORMAT_VALUE) {
3288 /* Handles f-string value formatting. */
3289 PyObject *result;
3290 PyObject *fmt_spec;
3291 PyObject *value;
3292 PyObject *(*conv_fn)(PyObject *);
3293 int which_conversion = oparg & FVC_MASK;
3294 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3295
3296 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003297 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003298
3299 /* See if any conversion is specified. */
3300 switch (which_conversion) {
3301 case FVC_STR: conv_fn = PyObject_Str; break;
3302 case FVC_REPR: conv_fn = PyObject_Repr; break;
3303 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3304
3305 /* Must be 0 (meaning no conversion), since only four
3306 values are allowed by (oparg & FVC_MASK). */
3307 default: conv_fn = NULL; break;
3308 }
3309
3310 /* If there's a conversion function, call it and replace
3311 value with that result. Otherwise, just use value,
3312 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003313 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003314 result = conv_fn(value);
3315 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003316 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003317 Py_XDECREF(fmt_spec);
3318 goto error;
3319 }
3320 value = result;
3321 }
3322
3323 /* If value is a unicode object, and there's no fmt_spec,
3324 then we know the result of format(value) is value
3325 itself. In that case, skip calling format(). I plan to
3326 move this optimization in to PyObject_Format()
3327 itself. */
3328 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3329 /* Do nothing, just transfer ownership to result. */
3330 result = value;
3331 } else {
3332 /* Actually call format(). */
3333 result = PyObject_Format(value, fmt_spec);
3334 Py_DECREF(value);
3335 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003336 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003337 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003338 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003339 }
3340
Eric V. Smith135d5f42016-02-05 18:23:08 -05003341 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003342 DISPATCH();
3343 }
3344
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003346 int oldoparg = oparg;
3347 NEXTOPARG();
3348 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003351
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003352
Antoine Pitrou042b1282010-08-13 21:15:58 +00003353#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003355#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 default:
3357 fprintf(stderr,
3358 "XXX lineno: %d, opcode: %d\n",
3359 PyFrame_GetLineNumber(f),
3360 opcode);
3361 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003362 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 /* This should never be reached. Every opcode should end with DISPATCH()
3367 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003368 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003369
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003372#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 if (!PyErr_Occurred())
3374 PyErr_SetString(PyExc_SystemError,
3375 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003376#else
3377 assert(PyErr_Occurred());
3378#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003379
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 /* Log traceback info. */
3381 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003382
Benjamin Peterson51f46162013-01-23 08:38:47 -05003383 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003384 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3385 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003386
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003387exception_unwind:
3388 /* Unwind stacks if an exception occurred */
3389 while (f->f_iblock > 0) {
3390 /* Pop the current block. */
3391 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 if (b->b_type == EXCEPT_HANDLER) {
3394 UNWIND_EXCEPT_HANDLER(b);
3395 continue;
3396 }
3397 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003398 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 PyObject *exc, *val, *tb;
3400 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003401 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 /* Beware, this invalidates all b->b_* fields */
3403 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003404 PUSH(exc_info->exc_traceback);
3405 PUSH(exc_info->exc_value);
3406 if (exc_info->exc_type != NULL) {
3407 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 }
3409 else {
3410 Py_INCREF(Py_None);
3411 PUSH(Py_None);
3412 }
3413 PyErr_Fetch(&exc, &val, &tb);
3414 /* Make the raw exception data
3415 available to the handler,
3416 so a program can emulate the
3417 Python main loop. */
3418 PyErr_NormalizeException(
3419 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003420 if (tb != NULL)
3421 PyException_SetTraceback(val, tb);
3422 else
3423 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003425 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003427 exc_info->exc_value = val;
3428 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 if (tb == NULL)
3430 tb = Py_None;
3431 Py_INCREF(tb);
3432 PUSH(tb);
3433 PUSH(val);
3434 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003436 /* Resume normal execution */
3437 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 }
3439 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003440
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003441 /* End the loop as we still have an error */
3442 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 /* Pop remaining stack entries. */
3446 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003447 PyObject *o = POP();
3448 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003450
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003451 assert(retval == NULL);
3452 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003453
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003454return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003456 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003457 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3458 tstate, f, PyTrace_RETURN, retval)) {
3459 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 }
3461 }
3462 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003463 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3464 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003465 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 }
3467 }
3468 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003471exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003472 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3473 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003475 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003477
Victor Stinnerefde1462015-03-21 15:04:43 +01003478 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003479}
3480
Benjamin Petersonb204a422011-06-05 22:04:07 -05003481static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003482format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3483{
3484 int err;
3485 Py_ssize_t len = PyList_GET_SIZE(names);
3486 PyObject *name_str, *comma, *tail, *tmp;
3487
3488 assert(PyList_CheckExact(names));
3489 assert(len >= 1);
3490 /* Deal with the joys of natural language. */
3491 switch (len) {
3492 case 1:
3493 name_str = PyList_GET_ITEM(names, 0);
3494 Py_INCREF(name_str);
3495 break;
3496 case 2:
3497 name_str = PyUnicode_FromFormat("%U and %U",
3498 PyList_GET_ITEM(names, len - 2),
3499 PyList_GET_ITEM(names, len - 1));
3500 break;
3501 default:
3502 tail = PyUnicode_FromFormat(", %U, and %U",
3503 PyList_GET_ITEM(names, len - 2),
3504 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003505 if (tail == NULL)
3506 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003507 /* Chop off the last two objects in the list. This shouldn't actually
3508 fail, but we can't be too careful. */
3509 err = PyList_SetSlice(names, len - 2, len, NULL);
3510 if (err == -1) {
3511 Py_DECREF(tail);
3512 return;
3513 }
3514 /* Stitch everything up into a nice comma-separated list. */
3515 comma = PyUnicode_FromString(", ");
3516 if (comma == NULL) {
3517 Py_DECREF(tail);
3518 return;
3519 }
3520 tmp = PyUnicode_Join(comma, names);
3521 Py_DECREF(comma);
3522 if (tmp == NULL) {
3523 Py_DECREF(tail);
3524 return;
3525 }
3526 name_str = PyUnicode_Concat(tmp, tail);
3527 Py_DECREF(tmp);
3528 Py_DECREF(tail);
3529 break;
3530 }
3531 if (name_str == NULL)
3532 return;
3533 PyErr_Format(PyExc_TypeError,
3534 "%U() missing %i required %s argument%s: %U",
3535 co->co_name,
3536 len,
3537 kind,
3538 len == 1 ? "" : "s",
3539 name_str);
3540 Py_DECREF(name_str);
3541}
3542
3543static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003544missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003545 PyObject **fastlocals)
3546{
Victor Stinner74319ae2016-08-25 00:04:09 +02003547 Py_ssize_t i, j = 0;
3548 Py_ssize_t start, end;
3549 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003550 const char *kind = positional ? "positional" : "keyword-only";
3551 PyObject *missing_names;
3552
3553 /* Compute the names of the arguments that are missing. */
3554 missing_names = PyList_New(missing);
3555 if (missing_names == NULL)
3556 return;
3557 if (positional) {
3558 start = 0;
3559 end = co->co_argcount - defcount;
3560 }
3561 else {
3562 start = co->co_argcount;
3563 end = start + co->co_kwonlyargcount;
3564 }
3565 for (i = start; i < end; i++) {
3566 if (GETLOCAL(i) == NULL) {
3567 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3568 PyObject *name = PyObject_Repr(raw);
3569 if (name == NULL) {
3570 Py_DECREF(missing_names);
3571 return;
3572 }
3573 PyList_SET_ITEM(missing_names, j++, name);
3574 }
3575 }
3576 assert(j == missing);
3577 format_missing(kind, co, missing_names);
3578 Py_DECREF(missing_names);
3579}
3580
3581static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003582too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3583 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003584{
3585 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003586 Py_ssize_t kwonly_given = 0;
3587 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003588 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003589 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003590
Benjamin Petersone109c702011-06-24 09:37:26 -05003591 assert((co->co_flags & CO_VARARGS) == 0);
3592 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003593 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3594 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003595 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003596 }
3597 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003598 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003599 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003600 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003601 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003602 }
3603 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003604 plural = (co_argcount != 1);
3605 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003606 }
3607 if (sig == NULL)
3608 return;
3609 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003610 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3611 kwonly_sig = PyUnicode_FromFormat(format,
3612 given != 1 ? "s" : "",
3613 kwonly_given,
3614 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003615 if (kwonly_sig == NULL) {
3616 Py_DECREF(sig);
3617 return;
3618 }
3619 }
3620 else {
3621 /* This will not fail. */
3622 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003623 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003624 }
3625 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003626 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003627 co->co_name,
3628 sig,
3629 plural ? "s" : "",
3630 given,
3631 kwonly_sig,
3632 given == 1 && !kwonly_given ? "was" : "were");
3633 Py_DECREF(sig);
3634 Py_DECREF(kwonly_sig);
3635}
3636
Guido van Rossumc2e20742006-02-27 22:32:47 +00003637/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003638 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003639 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003640
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003641PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003642_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003643 PyObject *const *args, Py_ssize_t argcount,
3644 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003645 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003646 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003647 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003648 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003649{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003650 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003651 PyFrameObject *f;
3652 PyObject *retval = NULL;
3653 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003654 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003656 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3657 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003658 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 if (globals == NULL) {
3661 PyErr_SetString(PyExc_SystemError,
3662 "PyEval_EvalCodeEx: NULL globals");
3663 return NULL;
3664 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003665
Victor Stinnerc7020012016-08-16 23:40:29 +02003666 /* Create the frame */
3667 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003669 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003670 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003672 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 fastlocals = f->f_localsplus;
3674 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003675
Victor Stinnerc7020012016-08-16 23:40:29 +02003676 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003677 if (co->co_flags & CO_VARKEYWORDS) {
3678 kwdict = PyDict_New();
3679 if (kwdict == NULL)
3680 goto fail;
3681 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003682 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003683 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003684 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003685 SETLOCAL(i, kwdict);
3686 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003687 else {
3688 kwdict = NULL;
3689 }
3690
3691 /* Copy positional arguments into local variables */
3692 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003693 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003694 }
3695 else {
3696 n = argcount;
3697 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003698 for (i = 0; i < n; i++) {
3699 x = args[i];
3700 Py_INCREF(x);
3701 SETLOCAL(i, x);
3702 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003703
3704 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705 if (co->co_flags & CO_VARARGS) {
3706 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003707 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003708 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003709 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003710 SETLOCAL(total_args, u);
3711 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 x = args[i];
3713 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003714 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003716 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003717
Serhiy Storchakab7281052016-09-12 00:52:40 +03003718 /* Handle keyword arguments passed as two strided arrays */
3719 kwcount *= kwstep;
3720 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003721 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003722 PyObject *keyword = kwnames[i];
3723 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003724 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003725
Benjamin Petersonb204a422011-06-05 22:04:07 -05003726 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3727 PyErr_Format(PyExc_TypeError,
3728 "%U() keywords must be strings",
3729 co->co_name);
3730 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003732
Benjamin Petersonb204a422011-06-05 22:04:07 -05003733 /* Speed hack: do raw pointer compares. As names are
3734 normally interned this should almost always hit. */
3735 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3736 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003737 PyObject *name = co_varnames[j];
3738 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003739 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003740 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003742
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 /* Slow fallback, just in case */
3744 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003745 PyObject *name = co_varnames[j];
3746 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3747 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003749 }
3750 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003752 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003753 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003754
Victor Stinner231d1f32017-01-11 02:12:06 +01003755 assert(j >= total_args);
3756 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003757 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003758 "%U() got an unexpected keyword argument '%S'",
3759 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003760 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003762
Christian Heimes0bd447f2013-07-20 14:48:10 +02003763 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3764 goto fail;
3765 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003766 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003767
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 kw_found:
3769 if (GETLOCAL(j) != NULL) {
3770 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003771 "%U() got multiple values for argument '%S'",
3772 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003773 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003775 Py_INCREF(value);
3776 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003778
3779 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003780 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003781 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 goto fail;
3783 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003784
3785 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003787 Py_ssize_t m = co->co_argcount - defcount;
3788 Py_ssize_t missing = 0;
3789 for (i = argcount; i < m; i++) {
3790 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003791 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003792 }
3793 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003794 if (missing) {
3795 missing_arguments(co, missing, defcount, fastlocals);
3796 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 }
3798 if (n > m)
3799 i = n - m;
3800 else
3801 i = 0;
3802 for (; i < defcount; i++) {
3803 if (GETLOCAL(m+i) == NULL) {
3804 PyObject *def = defs[i];
3805 Py_INCREF(def);
3806 SETLOCAL(m+i, def);
3807 }
3808 }
3809 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003810
3811 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003813 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 for (i = co->co_argcount; i < total_args; i++) {
3815 PyObject *name;
3816 if (GETLOCAL(i) != NULL)
3817 continue;
3818 name = PyTuple_GET_ITEM(co->co_varnames, i);
3819 if (kwdefs != NULL) {
3820 PyObject *def = PyDict_GetItem(kwdefs, name);
3821 if (def) {
3822 Py_INCREF(def);
3823 SETLOCAL(i, def);
3824 continue;
3825 }
3826 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003827 missing++;
3828 }
3829 if (missing) {
3830 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 goto fail;
3832 }
3833 }
3834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003836 vars into frame. */
3837 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003839 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003840 /* Possibly account for the cell variable being an argument. */
3841 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003842 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003843 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003844 /* Clear the local copy. */
3845 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003846 }
3847 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003848 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003849 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003850 if (c == NULL)
3851 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003852 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003854
3855 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003856 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3857 PyObject *o = PyTuple_GET_ITEM(closure, i);
3858 Py_INCREF(o);
3859 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003861
Yury Selivanoveb636452016-09-08 22:01:51 -07003862 /* Handle generator/coroutine/asynchronous generator */
3863 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003864 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003865 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003866 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003867
3868 if (is_coro && tstate->in_coroutine_wrapper) {
3869 assert(coro_wrapper != NULL);
3870 PyErr_Format(PyExc_RuntimeError,
3871 "coroutine wrapper %.200R attempted "
3872 "to recursively wrap %.200R",
3873 coro_wrapper,
3874 co);
3875 goto fail;
3876 }
Yury Selivanov75445082015-05-11 22:57:16 -04003877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 /* Don't need to keep the reference to f_back, it will be set
3879 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003880 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 /* Create a new generator that owns the ready to run frame
3883 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003884 if (is_coro) {
3885 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003886 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3887 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003888 } else {
3889 gen = PyGen_NewWithQualName(f, name, qualname);
3890 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003891 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003892 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003893 }
INADA Naoki9c157762016-12-26 18:52:46 +09003894
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003895 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003896
Yury Selivanov94c22632015-06-04 10:16:51 -04003897 if (is_coro && coro_wrapper != NULL) {
3898 PyObject *wrapped;
3899 tstate->in_coroutine_wrapper = 1;
3900 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3901 tstate->in_coroutine_wrapper = 0;
3902 return wrapped;
3903 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003904
Yury Selivanov75445082015-05-11 22:57:16 -04003905 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003907
Victor Stinner59a73272016-12-09 18:51:13 +01003908 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003909
Thomas Woutersce272b62007-09-19 21:19:28 +00003910fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 /* decref'ing the frame can cause __del__ methods to get invoked,
3913 which can call back into Python. While we're done with the
3914 current Python frame (f), the associated C stack is still in use,
3915 so recursion_depth must be boosted for the duration.
3916 */
3917 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003918 if (Py_REFCNT(f) > 1) {
3919 Py_DECREF(f);
3920 _PyObject_GC_TRACK(f);
3921 }
3922 else {
3923 ++tstate->recursion_depth;
3924 Py_DECREF(f);
3925 --tstate->recursion_depth;
3926 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003928}
3929
Victor Stinner40ee3012014-06-16 15:59:28 +02003930PyObject *
3931PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003932 PyObject *const *args, int argcount,
3933 PyObject *const *kws, int kwcount,
3934 PyObject *const *defs, int defcount,
3935 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003936{
3937 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003938 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003939 kws, kws != NULL ? kws + 1 : NULL,
3940 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003941 defs, defcount,
3942 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003943 NULL, NULL);
3944}
Tim Peters5ca576e2001-06-18 22:08:13 +00003945
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003946static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003947special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003950 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003952 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 return NULL;
3954 }
3955 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003956}
3957
3958
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003959/* Logic for the raise statement (too complicated for inlining).
3960 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003961static int
Collin Winter828f04a2007-08-31 00:04:24 +00003962do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 if (exc == NULL) {
3967 /* Reraise */
3968 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01003969 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01003971 type = exc_info->exc_type;
3972 value = exc_info->exc_value;
3973 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02003974 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 PyErr_SetString(PyExc_RuntimeError,
3976 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003977 return 0;
3978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 Py_XINCREF(type);
3980 Py_XINCREF(value);
3981 Py_XINCREF(tb);
3982 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003983 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 /* We support the following forms of raise:
3987 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003988 raise <instance>
3989 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 if (PyExceptionClass_Check(exc)) {
3992 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01003993 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 if (value == NULL)
3995 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003996 if (!PyExceptionInstance_Check(value)) {
3997 PyErr_Format(PyExc_TypeError,
3998 "calling %R should have returned an instance of "
3999 "BaseException, not %R",
4000 type, Py_TYPE(value));
4001 goto raise_error;
4002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 }
4004 else if (PyExceptionInstance_Check(exc)) {
4005 value = exc;
4006 type = PyExceptionInstance_Class(exc);
4007 Py_INCREF(type);
4008 }
4009 else {
4010 /* Not something you can raise. You get an exception
4011 anyway, just not what you specified :-) */
4012 Py_DECREF(exc);
4013 PyErr_SetString(PyExc_TypeError,
4014 "exceptions must derive from BaseException");
4015 goto raise_error;
4016 }
Collin Winter828f04a2007-08-31 00:04:24 +00004017
Serhiy Storchakac0191582016-09-27 11:37:10 +03004018 assert(type != NULL);
4019 assert(value != NULL);
4020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 if (cause) {
4022 PyObject *fixed_cause;
4023 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004024 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 if (fixed_cause == NULL)
4026 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004027 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004029 else if (PyExceptionInstance_Check(cause)) {
4030 fixed_cause = cause;
4031 }
4032 else if (cause == Py_None) {
4033 Py_DECREF(cause);
4034 fixed_cause = NULL;
4035 }
4036 else {
4037 PyErr_SetString(PyExc_TypeError,
4038 "exception causes must derive from "
4039 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 goto raise_error;
4041 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004042 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Collin Winter828f04a2007-08-31 00:04:24 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 PyErr_SetObject(type, value);
4046 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004047 Py_DECREF(value);
4048 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004049 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004050
4051raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 Py_XDECREF(value);
4053 Py_XDECREF(type);
4054 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004055 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004056}
4057
Tim Petersd6d010b2001-06-21 02:49:55 +00004058/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004059 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004060
Guido van Rossum0368b722007-05-11 16:50:42 +00004061 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4062 with a variable target.
4063*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004064
Barry Warsawe42b18f1997-08-25 22:13:04 +00004065static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004066unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 int i = 0, j = 0;
4069 Py_ssize_t ll = 0;
4070 PyObject *it; /* iter(v) */
4071 PyObject *w;
4072 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004077 if (it == NULL) {
4078 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4079 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4080 {
4081 PyErr_Format(PyExc_TypeError,
4082 "cannot unpack non-iterable %.200s object",
4083 v->ob_type->tp_name);
4084 }
4085 return 0;
4086 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 for (; i < argcnt; i++) {
4089 w = PyIter_Next(it);
4090 if (w == NULL) {
4091 /* Iterator done, via error or exhaustion. */
4092 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004093 if (argcntafter == -1) {
4094 PyErr_Format(PyExc_ValueError,
4095 "not enough values to unpack (expected %d, got %d)",
4096 argcnt, i);
4097 }
4098 else {
4099 PyErr_Format(PyExc_ValueError,
4100 "not enough values to unpack "
4101 "(expected at least %d, got %d)",
4102 argcnt + argcntafter, i);
4103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 }
4105 goto Error;
4106 }
4107 *--sp = w;
4108 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 if (argcntafter == -1) {
4111 /* We better have exhausted the iterator now. */
4112 w = PyIter_Next(it);
4113 if (w == NULL) {
4114 if (PyErr_Occurred())
4115 goto Error;
4116 Py_DECREF(it);
4117 return 1;
4118 }
4119 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004120 PyErr_Format(PyExc_ValueError,
4121 "too many values to unpack (expected %d)",
4122 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 goto Error;
4124 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 l = PySequence_List(it);
4127 if (l == NULL)
4128 goto Error;
4129 *--sp = l;
4130 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 ll = PyList_GET_SIZE(l);
4133 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004134 PyErr_Format(PyExc_ValueError,
4135 "not enough values to unpack (expected at least %d, got %zd)",
4136 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 goto Error;
4138 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 /* Pop the "after-variable" args off the list. */
4141 for (j = argcntafter; j > 0; j--, i++) {
4142 *--sp = PyList_GET_ITEM(l, ll - j);
4143 }
4144 /* Resize the list. */
4145 Py_SIZE(l) = ll - argcntafter;
4146 Py_DECREF(it);
4147 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004148
Tim Petersd6d010b2001-06-21 02:49:55 +00004149Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 for (; i > 0; i--, sp++)
4151 Py_DECREF(*sp);
4152 Py_XDECREF(it);
4153 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004154}
4155
4156
Guido van Rossum96a42c81992-01-12 02:29:51 +00004157#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004158static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004159prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 printf("%s ", str);
4162 if (PyObject_Print(v, stdout, 0) != 0)
4163 PyErr_Clear(); /* Don't know what else to do */
4164 printf("\n");
4165 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004166}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004167#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004168
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004169static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004170call_exc_trace(Py_tracefunc func, PyObject *self,
4171 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004172{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004173 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004175 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 if (value == NULL) {
4177 value = Py_None;
4178 Py_INCREF(value);
4179 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004180 PyErr_NormalizeException(&type, &value, &orig_traceback);
4181 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 arg = PyTuple_Pack(3, type, value, traceback);
4183 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004184 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 return;
4186 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004187 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 Py_DECREF(arg);
4189 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004190 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 else {
4192 Py_XDECREF(type);
4193 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004194 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004196}
4197
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004198static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004199call_trace_protected(Py_tracefunc func, PyObject *obj,
4200 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 PyObject *type, *value, *traceback;
4204 int err;
4205 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004206 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 if (err == 0)
4208 {
4209 PyErr_Restore(type, value, traceback);
4210 return 0;
4211 }
4212 else {
4213 Py_XDECREF(type);
4214 Py_XDECREF(value);
4215 Py_XDECREF(traceback);
4216 return -1;
4217 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004218}
4219
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004220static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004221call_trace(Py_tracefunc func, PyObject *obj,
4222 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 int result;
4226 if (tstate->tracing)
4227 return 0;
4228 tstate->tracing++;
4229 tstate->use_tracing = 0;
4230 result = func(obj, frame, what, arg);
4231 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4232 || (tstate->c_profilefunc != NULL));
4233 tstate->tracing--;
4234 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004235}
4236
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004237PyObject *
4238_PyEval_CallTracing(PyObject *func, PyObject *args)
4239{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004240 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 int save_tracing = tstate->tracing;
4242 int save_use_tracing = tstate->use_tracing;
4243 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 tstate->tracing = 0;
4246 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4247 || (tstate->c_profilefunc != NULL));
4248 result = PyObject_Call(func, args, NULL);
4249 tstate->tracing = save_tracing;
4250 tstate->use_tracing = save_use_tracing;
4251 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004252}
4253
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004254/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004255static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004256maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004257 PyThreadState *tstate, PyFrameObject *frame,
4258 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 int result = 0;
4261 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 /* If the last instruction executed isn't in the current
4264 instruction window, reset the window.
4265 */
4266 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4267 PyAddrPair bounds;
4268 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4269 &bounds);
4270 *instr_lb = bounds.ap_lower;
4271 *instr_ub = bounds.ap_upper;
4272 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004273 /* If the last instruction falls at the start of a line or if it
4274 represents a jump backwards, update the frame's line number and
4275 then call the trace function if we're tracing source lines.
4276 */
4277 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004279 if (frame->f_trace_lines) {
4280 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 }
George King20faa682017-10-18 17:44:22 -07004283 /* Always emit an opcode event if we're tracing all opcodes. */
4284 if (frame->f_trace_opcodes) {
4285 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 *instr_prev = frame->f_lasti;
4288 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004289}
4290
Fred Drake5755ce62001-06-27 19:19:46 +00004291void
4292PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 PyThreadState *tstate = PyThreadState_GET();
4295 PyObject *temp = tstate->c_profileobj;
4296 Py_XINCREF(arg);
4297 tstate->c_profilefunc = NULL;
4298 tstate->c_profileobj = NULL;
4299 /* Must make sure that tracing is not ignored if 'temp' is freed */
4300 tstate->use_tracing = tstate->c_tracefunc != NULL;
4301 Py_XDECREF(temp);
4302 tstate->c_profilefunc = func;
4303 tstate->c_profileobj = arg;
4304 /* Flag that tracing or profiling is turned on */
4305 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004306}
4307
4308void
4309PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 PyThreadState *tstate = PyThreadState_GET();
4312 PyObject *temp = tstate->c_traceobj;
4313 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4314 Py_XINCREF(arg);
4315 tstate->c_tracefunc = NULL;
4316 tstate->c_traceobj = NULL;
4317 /* Must make sure that profiling is not ignored if 'temp' is freed */
4318 tstate->use_tracing = tstate->c_profilefunc != NULL;
4319 Py_XDECREF(temp);
4320 tstate->c_tracefunc = func;
4321 tstate->c_traceobj = arg;
4322 /* Flag that tracing or profiling is turned on */
4323 tstate->use_tracing = ((func != NULL)
4324 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004325}
4326
Yury Selivanov75445082015-05-11 22:57:16 -04004327void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004328_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4329{
4330 assert(new_depth >= 0);
4331 PyThreadState *tstate = PyThreadState_GET();
4332 tstate->coroutine_origin_tracking_depth = new_depth;
4333}
4334
4335int
4336_PyEval_GetCoroutineOriginTrackingDepth(void)
4337{
4338 PyThreadState *tstate = PyThreadState_GET();
4339 return tstate->coroutine_origin_tracking_depth;
4340}
4341
4342void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004343_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004344{
4345 PyThreadState *tstate = PyThreadState_GET();
4346
Yury Selivanov75445082015-05-11 22:57:16 -04004347 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004348 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004349}
4350
4351PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004352_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004353{
4354 PyThreadState *tstate = PyThreadState_GET();
4355 return tstate->coroutine_wrapper;
4356}
4357
Yury Selivanoveb636452016-09-08 22:01:51 -07004358void
4359_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4360{
4361 PyThreadState *tstate = PyThreadState_GET();
4362
4363 Py_XINCREF(firstiter);
4364 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4365}
4366
4367PyObject *
4368_PyEval_GetAsyncGenFirstiter(void)
4369{
4370 PyThreadState *tstate = PyThreadState_GET();
4371 return tstate->async_gen_firstiter;
4372}
4373
4374void
4375_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4376{
4377 PyThreadState *tstate = PyThreadState_GET();
4378
4379 Py_XINCREF(finalizer);
4380 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4381}
4382
4383PyObject *
4384_PyEval_GetAsyncGenFinalizer(void)
4385{
4386 PyThreadState *tstate = PyThreadState_GET();
4387 return tstate->async_gen_finalizer;
4388}
4389
Guido van Rossumb209a111997-04-29 18:18:01 +00004390PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004391PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 PyFrameObject *current_frame = PyEval_GetFrame();
4394 if (current_frame == NULL)
4395 return PyThreadState_GET()->interp->builtins;
4396 else
4397 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004398}
4399
Guido van Rossumb209a111997-04-29 18:18:01 +00004400PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004401PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004404 if (current_frame == NULL) {
4405 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004407 }
4408
4409 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4410 return NULL;
4411
4412 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004414}
4415
Guido van Rossumb209a111997-04-29 18:18:01 +00004416PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004417PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 PyFrameObject *current_frame = PyEval_GetFrame();
4420 if (current_frame == NULL)
4421 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004422
4423 assert(current_frame->f_globals != NULL);
4424 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004425}
4426
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004427PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004428PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 PyThreadState *tstate = PyThreadState_GET();
4431 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004432}
4433
Guido van Rossum6135a871995-01-09 17:53:26 +00004434int
Tim Peters5ba58662001-07-16 02:29:45 +00004435PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 PyFrameObject *current_frame = PyEval_GetFrame();
4438 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 if (current_frame != NULL) {
4441 const int codeflags = current_frame->f_code->co_flags;
4442 const int compilerflags = codeflags & PyCF_MASK;
4443 if (compilerflags) {
4444 result = 1;
4445 cf->cf_flags |= compilerflags;
4446 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004447#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 if (codeflags & CO_GENERATOR_ALLOWED) {
4449 result = 1;
4450 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4451 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 }
4454 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004455}
4456
Guido van Rossum3f5da241990-12-20 15:06:42 +00004457
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004458const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004459PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 if (PyMethod_Check(func))
4462 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4463 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004464 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 else if (PyCFunction_Check(func))
4466 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4467 else
4468 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004469}
4470
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004471const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004472PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 if (PyMethod_Check(func))
4475 return "()";
4476 else if (PyFunction_Check(func))
4477 return "()";
4478 else if (PyCFunction_Check(func))
4479 return "()";
4480 else
4481 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004482}
4483
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004484#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004485if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004486 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4487 tstate, tstate->frame, \
4488 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 x = NULL; \
4490 } \
4491 else { \
4492 x = call; \
4493 if (tstate->c_profilefunc != NULL) { \
4494 if (x == NULL) { \
4495 call_trace_protected(tstate->c_profilefunc, \
4496 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004497 tstate, tstate->frame, \
4498 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 /* XXX should pass (type, value, tb) */ \
4500 } else { \
4501 if (call_trace(tstate->c_profilefunc, \
4502 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004503 tstate, tstate->frame, \
4504 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 Py_DECREF(x); \
4506 x = NULL; \
4507 } \
4508 } \
4509 } \
4510 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004511} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 x = call; \
4513 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004514
Victor Stinner415c5102017-01-11 00:54:57 +01004515/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4516 to reduce the stack consumption. */
4517Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004518call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004519{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004520 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 PyObject *func = *pfunc;
4522 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004523 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4524 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004525 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 /* Always dispatch PyCFunction first, because these are
4528 presumed to be the most frequent callable object.
4529 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004530 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004532 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004533 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004534 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4535 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004536 if (tstate->use_tracing && tstate->c_profilefunc) {
4537 // We need to create PyCFunctionObject for tracing.
4538 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4539 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4540 if (func == NULL) {
4541 return NULL;
4542 }
4543 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4544 kwnames));
4545 Py_DECREF(func);
4546 }
4547 else {
4548 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4549 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004550 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004551 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004552 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004553 /* Optimize access to bound methods. Reuse the Python stack
4554 to pass 'self' as the first argument, replace 'func'
4555 with 'self'. It avoids the creation of a new temporary tuple
4556 for arguments (to replace func with self) when the method uses
4557 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004558 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004559 Py_INCREF(self);
4560 func = PyMethod_GET_FUNCTION(func);
4561 Py_INCREF(func);
4562 Py_SETREF(*pfunc, self);
4563 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004564 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004565 }
4566 else {
4567 Py_INCREF(func);
4568 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004569
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004570 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004571 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004572 }
4573 else {
4574 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4575 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004576 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004578
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004579 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4580
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004581 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 while ((*pp_stack) > pfunc) {
4583 w = EXT_POP(*pp_stack);
4584 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004588}
4589
Jeremy Hylton52820442001-01-03 23:52:36 +00004590static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004591do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004594 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 PyThreadState *tstate = PyThreadState_GET();
4596 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004597 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004599 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004600 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004601 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004602}
4603
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004604/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004605 nb_index slot defined, and store in *pi.
4606 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004607 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004608 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004609*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004610int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004611_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004612{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004613 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 Py_ssize_t x;
4615 if (PyIndex_Check(v)) {
4616 x = PyNumber_AsSsize_t(v, NULL);
4617 if (x == -1 && PyErr_Occurred())
4618 return 0;
4619 }
4620 else {
4621 PyErr_SetString(PyExc_TypeError,
4622 "slice indices must be integers or "
4623 "None or have an __index__ method");
4624 return 0;
4625 }
4626 *pi = x;
4627 }
4628 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004629}
4630
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004631int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004632_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004633{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004634 Py_ssize_t x;
4635 if (PyIndex_Check(v)) {
4636 x = PyNumber_AsSsize_t(v, NULL);
4637 if (x == -1 && PyErr_Occurred())
4638 return 0;
4639 }
4640 else {
4641 PyErr_SetString(PyExc_TypeError,
4642 "slice indices must be integers or "
4643 "have an __index__ method");
4644 return 0;
4645 }
4646 *pi = x;
4647 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004648}
4649
4650
Guido van Rossum486364b2007-06-30 05:01:58 +00004651#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004653
Guido van Rossumb209a111997-04-29 18:18:01 +00004654static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004655cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 int res = 0;
4658 switch (op) {
4659 case PyCmp_IS:
4660 res = (v == w);
4661 break;
4662 case PyCmp_IS_NOT:
4663 res = (v != w);
4664 break;
4665 case PyCmp_IN:
4666 res = PySequence_Contains(w, v);
4667 if (res < 0)
4668 return NULL;
4669 break;
4670 case PyCmp_NOT_IN:
4671 res = PySequence_Contains(w, v);
4672 if (res < 0)
4673 return NULL;
4674 res = !res;
4675 break;
4676 case PyCmp_EXC_MATCH:
4677 if (PyTuple_Check(w)) {
4678 Py_ssize_t i, length;
4679 length = PyTuple_Size(w);
4680 for (i = 0; i < length; i += 1) {
4681 PyObject *exc = PyTuple_GET_ITEM(w, i);
4682 if (!PyExceptionClass_Check(exc)) {
4683 PyErr_SetString(PyExc_TypeError,
4684 CANNOT_CATCH_MSG);
4685 return NULL;
4686 }
4687 }
4688 }
4689 else {
4690 if (!PyExceptionClass_Check(w)) {
4691 PyErr_SetString(PyExc_TypeError,
4692 CANNOT_CATCH_MSG);
4693 return NULL;
4694 }
4695 }
4696 res = PyErr_GivenExceptionMatches(v, w);
4697 break;
4698 default:
4699 return PyObject_RichCompare(v, w, op);
4700 }
4701 v = res ? Py_True : Py_False;
4702 Py_INCREF(v);
4703 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004704}
4705
Thomas Wouters52152252000-08-17 22:55:00 +00004706static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004707import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4708{
4709 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004710 PyObject *import_func, *res;
4711 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004712
4713 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4714 if (import_func == NULL) {
4715 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4716 return NULL;
4717 }
4718
4719 /* Fast path for not overloaded __import__. */
4720 if (import_func == PyThreadState_GET()->interp->import_func) {
4721 int ilevel = _PyLong_AsInt(level);
4722 if (ilevel == -1 && PyErr_Occurred()) {
4723 return NULL;
4724 }
4725 res = PyImport_ImportModuleLevelObject(
4726 name,
4727 f->f_globals,
4728 f->f_locals == NULL ? Py_None : f->f_locals,
4729 fromlist,
4730 ilevel);
4731 return res;
4732 }
4733
4734 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004735
4736 stack[0] = name;
4737 stack[1] = f->f_globals;
4738 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4739 stack[3] = fromlist;
4740 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004741 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004742 Py_DECREF(import_func);
4743 return res;
4744}
4745
4746static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004747import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004750 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004751 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004752
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004753 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004754 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004755 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004756 /* Issue #17636: in case this failed because of a circular relative
4757 import, try to fallback on reading the module directly from
4758 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004759 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004760 if (pkgname == NULL) {
4761 goto error;
4762 }
Oren Milman6db70332017-09-19 14:23:01 +03004763 if (!PyUnicode_Check(pkgname)) {
4764 Py_CLEAR(pkgname);
4765 goto error;
4766 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004767 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004768 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004769 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004770 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004771 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004772 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004773 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004774 if (x == NULL) {
4775 goto error;
4776 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004777 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004779 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004780 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004781 if (pkgname == NULL) {
4782 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4783 if (pkgname_or_unknown == NULL) {
4784 Py_XDECREF(pkgpath);
4785 return NULL;
4786 }
4787 } else {
4788 pkgname_or_unknown = pkgname;
4789 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004790
4791 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4792 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004793 errmsg = PyUnicode_FromFormat(
4794 "cannot import name %R from %R (unknown location)",
4795 name, pkgname_or_unknown
4796 );
4797 /* NULL check for errmsg done by PyErr_SetImportError. */
4798 PyErr_SetImportError(errmsg, pkgname, NULL);
4799 }
4800 else {
4801 errmsg = PyUnicode_FromFormat(
4802 "cannot import name %R from %R (%S)",
4803 name, pkgname_or_unknown, pkgpath
4804 );
4805 /* NULL check for errmsg done by PyErr_SetImportError. */
4806 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004807 }
4808
Xiang Zhang4830f582017-03-21 11:13:42 +08004809 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004810 Py_XDECREF(pkgname_or_unknown);
4811 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004812 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004813}
Guido van Rossumac7be682001-01-17 15:42:30 +00004814
Thomas Wouters52152252000-08-17 22:55:00 +00004815static int
4816import_all_from(PyObject *locals, PyObject *v)
4817{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004818 _Py_IDENTIFIER(__all__);
4819 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004820 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 int skip_leading_underscores = 0;
4822 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004823
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004824 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4825 return -1; /* Unexpected error */
4826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004828 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4829 return -1;
4830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004833 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004834 return -1;
4835 }
4836 all = PyMapping_Keys(dict);
4837 Py_DECREF(dict);
4838 if (all == NULL)
4839 return -1;
4840 skip_leading_underscores = 1;
4841 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 for (pos = 0, err = 0; ; pos++) {
4844 name = PySequence_GetItem(all, pos);
4845 if (name == NULL) {
4846 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4847 err = -1;
4848 else
4849 PyErr_Clear();
4850 break;
4851 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004852 if (skip_leading_underscores && PyUnicode_Check(name)) {
4853 if (PyUnicode_READY(name) == -1) {
4854 Py_DECREF(name);
4855 err = -1;
4856 break;
4857 }
4858 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4859 Py_DECREF(name);
4860 continue;
4861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 }
4863 value = PyObject_GetAttr(v, name);
4864 if (value == NULL)
4865 err = -1;
4866 else if (PyDict_CheckExact(locals))
4867 err = PyDict_SetItem(locals, name, value);
4868 else
4869 err = PyObject_SetItem(locals, name, value);
4870 Py_DECREF(name);
4871 Py_XDECREF(value);
4872 if (err != 0)
4873 break;
4874 }
4875 Py_DECREF(all);
4876 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004877}
4878
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004879static int
4880check_args_iterable(PyObject *func, PyObject *args)
4881{
4882 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4883 PyErr_Format(PyExc_TypeError,
4884 "%.200s%.200s argument after * "
4885 "must be an iterable, not %.200s",
4886 PyEval_GetFuncName(func),
4887 PyEval_GetFuncDesc(func),
4888 args->ob_type->tp_name);
4889 return -1;
4890 }
4891 return 0;
4892}
4893
4894static void
4895format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4896{
4897 PyErr_Format(PyExc_TypeError,
4898 "%.200s%.200s argument after ** "
4899 "must be a mapping, not %.200s",
4900 PyEval_GetFuncName(func),
4901 PyEval_GetFuncDesc(func),
4902 kwargs->ob_type->tp_name);
4903}
4904
Guido van Rossumac7be682001-01-17 15:42:30 +00004905static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004906format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 if (!obj)
4911 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004912
Serhiy Storchaka06515832016-11-20 09:13:07 +02004913 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 if (!obj_str)
4915 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004918}
Guido van Rossum950361c1997-01-24 13:49:28 +00004919
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004920static void
4921format_exc_unbound(PyCodeObject *co, int oparg)
4922{
4923 PyObject *name;
4924 /* Don't stomp existing exception */
4925 if (PyErr_Occurred())
4926 return;
4927 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4928 name = PyTuple_GET_ITEM(co->co_cellvars,
4929 oparg);
4930 format_exc_check_arg(
4931 PyExc_UnboundLocalError,
4932 UNBOUNDLOCAL_ERROR_MSG,
4933 name);
4934 } else {
4935 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4936 PyTuple_GET_SIZE(co->co_cellvars));
4937 format_exc_check_arg(PyExc_NameError,
4938 UNBOUNDFREE_ERROR_MSG, name);
4939 }
4940}
4941
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004942static PyObject *
4943unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03004944 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004945{
4946 PyObject *res;
4947 if (Py_REFCNT(v) == 2) {
4948 /* In the common case, there are 2 references to the value
4949 * stored in 'variable' when the += is performed: one on the
4950 * value stack (in 'v') and one still stored in the
4951 * 'variable'. We try to delete the variable now to reduce
4952 * the refcnt to 1.
4953 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004954 int opcode, oparg;
4955 NEXTOPARG();
4956 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004957 case STORE_FAST:
4958 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004959 PyObject **fastlocals = f->f_localsplus;
4960 if (GETLOCAL(oparg) == v)
4961 SETLOCAL(oparg, NULL);
4962 break;
4963 }
4964 case STORE_DEREF:
4965 {
4966 PyObject **freevars = (f->f_localsplus +
4967 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004968 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05004969 if (PyCell_GET(c) == v) {
4970 PyCell_SET(c, NULL);
4971 Py_DECREF(v);
4972 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004973 break;
4974 }
4975 case STORE_NAME:
4976 {
4977 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004978 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004979 PyObject *locals = f->f_locals;
4980 if (PyDict_CheckExact(locals) &&
4981 PyDict_GetItem(locals, name) == v) {
4982 if (PyDict_DelItem(locals, name) != 0) {
4983 PyErr_Clear();
4984 }
4985 }
4986 break;
4987 }
4988 }
4989 }
4990 res = v;
4991 PyUnicode_Append(&res, w);
4992 return res;
4993}
4994
Guido van Rossum950361c1997-01-24 13:49:28 +00004995#ifdef DYNAMIC_EXECUTION_PROFILE
4996
Skip Montanarof118cb12001-10-15 20:51:38 +00004997static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004998getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 int i;
5001 PyObject *l = PyList_New(256);
5002 if (l == NULL) return NULL;
5003 for (i = 0; i < 256; i++) {
5004 PyObject *x = PyLong_FromLong(a[i]);
5005 if (x == NULL) {
5006 Py_DECREF(l);
5007 return NULL;
5008 }
5009 PyList_SetItem(l, i, x);
5010 }
5011 for (i = 0; i < 256; i++)
5012 a[i] = 0;
5013 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005014}
5015
5016PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005017_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005018{
5019#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005020 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005021#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 int i;
5023 PyObject *l = PyList_New(257);
5024 if (l == NULL) return NULL;
5025 for (i = 0; i < 257; i++) {
5026 PyObject *x = getarray(dxpairs[i]);
5027 if (x == NULL) {
5028 Py_DECREF(l);
5029 return NULL;
5030 }
5031 PyList_SetItem(l, i, x);
5032 }
5033 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005034#endif
5035}
5036
5037#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005038
5039Py_ssize_t
5040_PyEval_RequestCodeExtraIndex(freefunc free)
5041{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005042 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005043 Py_ssize_t new_index;
5044
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005045 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005046 return -1;
5047 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005048 new_index = interp->co_extra_user_count++;
5049 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005050 return new_index;
5051}
Łukasz Langaa785c872016-09-09 17:37:37 -07005052
5053static void
5054dtrace_function_entry(PyFrameObject *f)
5055{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005056 const char *filename;
5057 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005058 int lineno;
5059
5060 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5061 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5062 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5063
5064 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5065}
5066
5067static void
5068dtrace_function_return(PyFrameObject *f)
5069{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005070 const char *filename;
5071 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005072 int lineno;
5073
5074 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5075 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5076 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5077
5078 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5079}
5080
5081/* DTrace equivalent of maybe_call_line_trace. */
5082static void
5083maybe_dtrace_line(PyFrameObject *frame,
5084 int *instr_lb, int *instr_ub, int *instr_prev)
5085{
5086 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005087 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005088
5089 /* If the last instruction executed isn't in the current
5090 instruction window, reset the window.
5091 */
5092 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5093 PyAddrPair bounds;
5094 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5095 &bounds);
5096 *instr_lb = bounds.ap_lower;
5097 *instr_ub = bounds.ap_upper;
5098 }
5099 /* If the last instruction falls at the start of a line or if
5100 it represents a jump backwards, update the frame's line
5101 number and call the trace function. */
5102 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5103 frame->f_lineno = line;
5104 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5105 if (!co_filename)
5106 co_filename = "?";
5107 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5108 if (!co_name)
5109 co_name = "?";
5110 PyDTrace_LINE(co_filename, co_name, line);
5111 }
5112 *instr_prev = frame->f_lasti;
5113}