blob: f6519cff590fe165e1254f3ae527e08cf7a3f71b [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");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (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");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (gil_created()) {
268 int err = errno;
269 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200270 /* _Py_Finalizing is protected by the GIL */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600271 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200272 drop_gil(tstate);
273 PyThread_exit_thread();
Barry Warsawb2e57942017-09-14 18:13:16 -0700274 Py_UNREACHABLE();
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 errno = err;
277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279}
280
281
Guido van Rossuma9672091994-09-14 13:31:22 +0000282/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
283 signal handlers or Mac I/O completion routines) can schedule calls
284 to a function to be called synchronously.
285 The synchronous function is called with one void* argument.
286 It should return 0 for success or -1 for failure -- failure should
287 be accompanied by an exception.
288
289 If registry succeeds, the registry function returns 0; if it fails
290 (e.g. due to too many pending calls) it returns -1 (without setting
291 an exception condition).
292
293 Note that because registry may occur from within signal handlers,
294 or other asynchronous events, calling malloc() is unsafe!
295
Guido van Rossuma9672091994-09-14 13:31:22 +0000296 Any thread can schedule pending calls, but only the main thread
297 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000298 There is no facility to schedule calls to a particular thread, but
299 that should be easy to change, should that ever be required. In
300 that case, the static variables here should go into the python
301 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000302*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000303
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200304void
305_PyEval_SignalReceived(void)
306{
307 /* bpo-30703: Function called when the C signal handler of Python gets a
308 signal. We cannot queue a callback using Py_AddPendingCall() since
309 that function is not async-signal-safe. */
310 SIGNAL_PENDING_CALLS();
311}
312
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200313/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000314 scheduling to be made from any thread, and even from an executing
315 callback.
316 */
317
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000318int
319Py_AddPendingCall(int (*func)(void *), void *arg)
320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600322 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 /* try a few times for the lock. Since this mechanism is used
325 * for signal handling (on the main thread), there is a (slim)
326 * chance that a signal is delivered on the same thread while we
327 * hold the lock during the Py_MakePendingCalls() function.
328 * This avoids a deadlock in that case.
329 * Note that signals can be delivered on any thread. In particular,
330 * on Windows, a SIGINT is delivered on a system-created worker
331 * thread.
332 * We also check for lock being NULL, in the unlikely case that
333 * this function is called before any bytecode evaluation takes place.
334 */
335 if (lock != NULL) {
336 for (i = 0; i<100; i++) {
337 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
338 break;
339 }
340 if (i == 100)
341 return -1;
342 }
343
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600344 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600346 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 result = -1; /* Queue full */
348 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600349 _PyRuntime.ceval.pending.calls[i].func = func;
350 _PyRuntime.ceval.pending.calls[i].arg = arg;
351 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 }
353 /* signal main loop */
354 SIGNAL_PENDING_CALLS();
355 if (lock != NULL)
356 PyThread_release_lock(lock);
357 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000358}
359
360int
361Py_MakePendingCalls(void)
362{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200363 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 int i;
365 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000366
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200367 assert(PyGILState_Check());
368
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600369 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600371 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
372 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 return -1;
374 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600377 if (_PyRuntime.ceval.pending.main_thread &&
378 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
379 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600381 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200383 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200385 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200386 /* unsignal before starting to call callbacks, so that any callback
387 added in-between re-signals */
388 UNSIGNAL_PENDING_CALLS();
389
390 /* Python signal handler doesn't really queue a callback: it only signals
391 that a signal was received, see _PyEval_SignalReceived(). */
392 if (PyErr_CheckSignals() < 0) {
393 goto error;
394 }
395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 /* perform a bounded number of calls, in case of recursion */
397 for (i=0; i<NPENDINGCALLS; i++) {
398 int j;
399 int (*func)(void *);
400 void *arg = NULL;
401
402 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600403 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
404 j = _PyRuntime.ceval.pending.first;
405 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 func = NULL; /* Queue empty */
407 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600408 func = _PyRuntime.ceval.pending.calls[j].func;
409 arg = _PyRuntime.ceval.pending.calls[j].arg;
410 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600412 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 /* having released the lock, perform the callback */
414 if (func == NULL)
415 break;
416 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200417 if (r) {
418 goto error;
419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200421
Charles-François Natalif23339a2011-07-23 18:15:43 +0200422 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200424
425error:
426 busy = 0;
427 SIGNAL_PENDING_CALLS(); /* We're not done yet */
428 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000429}
430
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000431/* The interpreter's recursion limit */
432
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000433#ifndef Py_DEFAULT_RECURSION_LIMIT
434#define Py_DEFAULT_RECURSION_LIMIT 1000
435#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600436
Eric Snow05351c12017-09-05 21:43:08 -0700437int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000438
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600439void
440_PyEval_Initialize(struct _ceval_runtime_state *state)
441{
442 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
443 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
444 _gil_initialize(&state->gil);
445}
446
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000447int
448Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000449{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600450 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000451}
452
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000453void
454Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000455{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600456 _PyRuntime.ceval.recursion_limit = new_limit;
457 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000458}
459
Armin Rigo2b3eb402003-10-28 12:05:48 +0000460/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
461 if the recursion_depth reaches _Py_CheckRecursionLimit.
462 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
463 to guarantee that _Py_CheckRecursiveCall() is regularly called.
464 Without USE_STACKCHECK, there is no need for this. */
465int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300466_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyThreadState *tstate = PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600469 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000470
471#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700472 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (PyOS_CheckStack()) {
474 --tstate->recursion_depth;
475 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
476 return -1;
477 }
pdox18967932017-10-25 23:03:01 -0700478 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700479 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700480#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (tstate->recursion_critical)
482 /* Somebody asked that we don't check for recursion. */
483 return 0;
484 if (tstate->overflowed) {
485 if (tstate->recursion_depth > recursion_limit + 50) {
486 /* Overflowing while handling an overflow. Give up. */
487 Py_FatalError("Cannot recover from stack overflow.");
488 }
489 return 0;
490 }
491 if (tstate->recursion_depth > recursion_limit) {
492 --tstate->recursion_depth;
493 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400494 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 "maximum recursion depth exceeded%s",
496 where);
497 return -1;
498 }
499 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000500}
501
Guido van Rossum374a9221991-04-04 10:40:29 +0000502/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000503enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000504 WHY_NOT = 0x0001, /* No error */
505 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000506 WHY_RETURN = 0x0008, /* 'return' statement */
507 WHY_BREAK = 0x0010, /* 'break' statement */
508 WHY_CONTINUE = 0x0020, /* 'continue' statement */
509 WHY_YIELD = 0x0040, /* 'yield' operator */
510 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000511};
Guido van Rossum374a9221991-04-04 10:40:29 +0000512
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400513static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000514static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000515
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600516#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000517
Guido van Rossum374a9221991-04-04 10:40:29 +0000518
Guido van Rossumb209a111997-04-29 18:18:01 +0000519PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000520PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 return PyEval_EvalCodeEx(co,
523 globals, locals,
524 (PyObject **)NULL, 0,
525 (PyObject **)NULL, 0,
526 (PyObject **)NULL, 0,
527 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000528}
529
530
531/* Interpreter main loop */
532
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000533PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000534PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* This is for backward compatibility with extension modules that
536 used this API; core interpreter code should call
537 PyEval_EvalFrameEx() */
538 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000539}
540
541PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000543{
Brett Cannon3cebf932016-09-05 15:33:46 -0700544 PyThreadState *tstate = PyThreadState_GET();
545 return tstate->interp->eval_frame(f, throwflag);
546}
547
Victor Stinnerc6944e72016-11-11 02:13:35 +0100548PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700549_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
550{
Guido van Rossum950361c1997-01-24 13:49:28 +0000551#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000553#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200554 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300555 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200556 int opcode; /* Current opcode */
557 int oparg; /* Current opcode argument, if any */
558 enum why_code why; /* Reason for block stack unwind */
559 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 PyObject *retval = NULL; /* Return value */
561 PyThreadState *tstate = PyThreadState_GET();
562 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 is true when the line being executed has changed. The
569 initial values are such as to make this false the first
570 time it is tested. */
571 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000572
Serhiy Storchakaab874002016-09-11 13:48:15 +0300573 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 PyObject *names;
575 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000576
Brett Cannon368b4b72012-04-02 12:17:59 -0400577#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200578 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400579#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200580
Antoine Pitroub52ec782009-01-25 16:34:23 +0000581/* Computed GOTOs, or
582 the-optimization-commonly-but-improperly-known-as-"threaded code"
583 using gcc's labels-as-values extension
584 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
585
586 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000588 combined with a lookup table of jump addresses. However, since the
589 indirect jump instruction is shared by all opcodes, the CPU will have a
590 hard time making the right prediction for where to jump next (actually,
591 it will be always wrong except in the uncommon case of a sequence of
592 several identical opcodes).
593
594 "Threaded code" in contrast, uses an explicit jump table and an explicit
595 indirect jump instruction at the end of each opcode. Since the jump
596 instruction is at a different address for each opcode, the CPU will make a
597 separate prediction for each of these instructions, which is equivalent to
598 predicting the second opcode of each opcode pair. These predictions have
599 a much better chance to turn out valid, especially in small bytecode loops.
600
601 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000603 and potentially many more instructions (depending on the pipeline width).
604 A correctly predicted branch, however, is nearly free.
605
606 At the time of this writing, the "threaded code" version is up to 15-20%
607 faster than the normal "switch" version, depending on the compiler and the
608 CPU architecture.
609
610 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
611 because it would render the measurements invalid.
612
613
614 NOTE: care must be taken that the compiler doesn't try to "optimize" the
615 indirect jumps by sharing them between all opcodes. Such optimizations
616 can be disabled on gcc by using the -fno-gcse flag (or possibly
617 -fno-crossjumping).
618*/
619
Antoine Pitrou042b1282010-08-13 21:15:58 +0000620#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000621#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000622#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000623#endif
624
Antoine Pitrou042b1282010-08-13 21:15:58 +0000625#ifdef HAVE_COMPUTED_GOTOS
626 #ifndef USE_COMPUTED_GOTOS
627 #define USE_COMPUTED_GOTOS 1
628 #endif
629#else
630 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
631 #error "Computed gotos are not supported on this compiler."
632 #endif
633 #undef USE_COMPUTED_GOTOS
634 #define USE_COMPUTED_GOTOS 0
635#endif
636
637#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000638/* Import the static jump table */
639#include "opcode_targets.h"
640
Antoine Pitroub52ec782009-01-25 16:34:23 +0000641#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000644
Antoine Pitroub52ec782009-01-25 16:34:23 +0000645#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600647 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 FAST_DISPATCH(); \
649 } \
650 continue; \
651 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000652
653#ifdef LLTRACE
654#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700656 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300658 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300659 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 } \
661 goto fast_next_opcode; \
662 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000663#else
664#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700666 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300668 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300669 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 } \
671 goto fast_next_opcode; \
672 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000673#endif
674
675#else
676#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300678
Antoine Pitroub52ec782009-01-25 16:34:23 +0000679#define DISPATCH() continue
680#define FAST_DISPATCH() goto fast_next_opcode
681#endif
682
683
Neal Norwitza81d2202002-07-14 00:27:26 +0000684/* Tuple access macros */
685
686#ifndef Py_DEBUG
687#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
688#else
689#define GETITEM(v, i) PyTuple_GetItem((v), (i))
690#endif
691
Guido van Rossum374a9221991-04-04 10:40:29 +0000692/* Code access macros */
693
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300694/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600695#define INSTR_OFFSET() \
696 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300697#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300698 _Py_CODEUNIT word = *next_instr; \
699 opcode = _Py_OPCODE(word); \
700 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300701 next_instr++; \
702 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300703#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
704#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000705
Raymond Hettingerf606f872003-03-16 03:11:04 +0000706/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 Some opcodes tend to come in pairs thus making it possible to
708 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300709 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 Verifying the prediction costs a single high-speed test of a register
712 variable against a constant. If the pairing was good, then the
713 processor's own internal branch predication has a high likelihood of
714 success, resulting in a nearly zero-overhead transition to the
715 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300716 including its unpredictable switch-case branch. Combined with the
717 processor's internal branch prediction, a successful PREDICT has the
718 effect of making the two opcodes run as if they were a single new opcode
719 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000720
Georg Brandl86b2fb92008-07-16 03:43:04 +0000721 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 predictions turned-on and interpret the results as if some opcodes
723 had been combined or turn-off predictions so that the opcode frequency
724 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000725
726 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 the CPU to record separate branch prediction information for each
728 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000729
Raymond Hettingerf606f872003-03-16 03:11:04 +0000730*/
731
Antoine Pitrou042b1282010-08-13 21:15:58 +0000732#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000734#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300735#define PREDICT(op) \
736 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300737 _Py_CODEUNIT word = *next_instr; \
738 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300739 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300740 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300741 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300742 goto PRED_##op; \
743 } \
744 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000745#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300746#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000747
Raymond Hettingerf606f872003-03-16 03:11:04 +0000748
Guido van Rossum374a9221991-04-04 10:40:29 +0000749/* Stack manipulation macros */
750
Martin v. Löwis18e16552006-02-15 17:27:45 +0000751/* The stack can grow at most MAXINT deep, as co_nlocals and
752 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000753#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
754#define EMPTY() (STACK_LEVEL() == 0)
755#define TOP() (stack_pointer[-1])
756#define SECOND() (stack_pointer[-2])
757#define THIRD() (stack_pointer[-3])
758#define FOURTH() (stack_pointer[-4])
759#define PEEK(n) (stack_pointer[-(n)])
760#define SET_TOP(v) (stack_pointer[-1] = (v))
761#define SET_SECOND(v) (stack_pointer[-2] = (v))
762#define SET_THIRD(v) (stack_pointer[-3] = (v))
763#define SET_FOURTH(v) (stack_pointer[-4] = (v))
764#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
765#define BASIC_STACKADJ(n) (stack_pointer += n)
766#define BASIC_PUSH(v) (*stack_pointer++ = (v))
767#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000768
Guido van Rossum96a42c81992-01-12 02:29:51 +0000769#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000771 lltrace && prtrace(TOP(), "push")); \
772 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000774 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000776 lltrace && prtrace(TOP(), "stackadj")); \
777 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000778#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000779 prtrace((STACK_POINTER)[-1], "ext_pop")), \
780 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000781#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000782#define PUSH(v) BASIC_PUSH(v)
783#define POP() BASIC_POP()
784#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000785#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000786#endif
787
Guido van Rossum681d79a1995-07-18 14:51:37 +0000788/* Local variable macros */
789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000791
792/* The SETLOCAL() macro must not DECREF the local variable in-place and
793 then store the new value; it must copy the old value to a temporary
794 value, then store the new value, and then DECREF the temporary value.
795 This is because it is possible that during the DECREF the frame is
796 accessed by other code (e.g. a __del__ method or gc.collect()) and the
797 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000799 GETLOCAL(i) = value; \
800 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000801
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000802
803#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 while (STACK_LEVEL() > (b)->b_level) { \
805 PyObject *v = POP(); \
806 Py_XDECREF(v); \
807 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000808
809#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300810 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100812 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 assert(STACK_LEVEL() >= (b)->b_level + 3); \
814 while (STACK_LEVEL() > (b)->b_level + 3) { \
815 value = POP(); \
816 Py_XDECREF(value); \
817 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100818 exc_info = tstate->exc_info; \
819 type = exc_info->exc_type; \
820 value = exc_info->exc_value; \
821 traceback = exc_info->exc_traceback; \
822 exc_info->exc_type = POP(); \
823 exc_info->exc_value = POP(); \
824 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 Py_XDECREF(type); \
826 Py_XDECREF(value); \
827 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300828 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000829
Guido van Rossuma027efa1997-05-05 20:56:21 +0000830/* Start of code */
831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 /* push frame */
833 if (Py_EnterRecursiveCall(""))
834 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (tstate->use_tracing) {
839 if (tstate->c_tracefunc != NULL) {
840 /* tstate->c_tracefunc, if defined, is a
841 function that will be called on *every* entry
842 to a code block. Its return value, if not
843 None, is a function that will be called at
844 the start of each executed line of code.
845 (Actually, the function must return itself
846 in order to continue tracing.) The trace
847 functions are called with three arguments:
848 a pointer to the current frame, a string
849 indicating why the function is called, and
850 an argument which depends on the situation.
851 The global trace function is also called
852 whenever an exception is detected. */
853 if (call_trace_protected(tstate->c_tracefunc,
854 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100855 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 /* Trace function raised an error */
857 goto exit_eval_frame;
858 }
859 }
860 if (tstate->c_profilefunc != NULL) {
861 /* Similar for c_profilefunc, except it needn't
862 return itself and isn't called for "line" events */
863 if (call_trace_protected(tstate->c_profilefunc,
864 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100865 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 /* Profile function raised an error */
867 goto exit_eval_frame;
868 }
869 }
870 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000871
Łukasz Langaa785c872016-09-09 17:37:37 -0700872 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
873 dtrace_function_entry(f);
874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 co = f->f_code;
876 names = co->co_names;
877 consts = co->co_consts;
878 fastlocals = f->f_localsplus;
879 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300880 assert(PyBytes_Check(co->co_code));
881 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300882 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
883 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
884 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300885 /*
886 f->f_lasti refers to the index of the last instruction,
887 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000888
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300889 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500890 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 When the PREDICT() macros are enabled, some opcode pairs follow in
893 direct succession without updating f->f_lasti. A successful
894 prediction effectively links the two codes together as if they
895 were a single new opcode; accordingly,f->f_lasti will point to
896 the first code in the pair (for instance, GET_ITER followed by
897 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300898 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300900 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300901 next_instr = first_instr;
902 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300903 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
904 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 stack_pointer = f->f_stacktop;
907 assert(stack_pointer != NULL);
908 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200909 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000910
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000911
Tim Peters5ca576e2001-06-18 22:08:13 +0000912#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200913 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000914#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +0000917
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400918 if (throwflag) /* support for generator.throw() */
919 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920
Victor Stinnerace47d72013-07-18 01:41:08 +0200921#ifdef Py_DEBUG
922 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100923 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000924 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200925 assert(!PyErr_Occurred());
926#endif
927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 assert(stack_pointer >= f->f_valuestack); /* else underflow */
930 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200931 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Do periodic things. Doing this every time through
934 the loop would add too much overhead, so we do it
935 only every Nth instruction. We also do it if
936 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
937 event needs attention (e.g. a signal handler or
938 async I/O handler); see Py_AddPendingCall() and
939 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000940
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600941 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700942 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
943 _Py_OPCODE(*next_instr) == YIELD_FROM) {
944 /* Two cases where we skip running signal handlers and other
945 pending calls:
946 - If we're about to enter the try: of a try/finally (not
947 *very* useful, but might help in some cases and it's
948 traditional)
949 - If we're resuming a chain of nested 'yield from' or
950 'await' calls, then each frame is parked with YIELD_FROM
951 as its next opcode. If the user hit control-C we want to
952 wait until we've reached the innermost frame before
953 running the signal handler and raising KeyboardInterrupt
954 (see bpo-30039).
955 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 goto fast_next_opcode;
957 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600958 if (_Py_atomic_load_relaxed(
959 &_PyRuntime.ceval.pending.calls_to_do))
960 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400961 if (Py_MakePendingCalls() < 0)
962 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600964 if (_Py_atomic_load_relaxed(
965 &_PyRuntime.ceval.gil_drop_request))
966 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 /* Give another thread a chance */
968 if (PyThreadState_Swap(NULL) != tstate)
969 Py_FatalError("ceval: tstate mix-up");
970 drop_gil(tstate);
971
972 /* Other threads may run now */
973
974 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700975
976 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600977 if (_Py_IsFinalizing() &&
978 !_Py_CURRENTLY_FINALIZING(tstate))
979 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700980 drop_gil(tstate);
981 PyThread_exit_thread();
982 }
983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (PyThreadState_Swap(tstate) != NULL)
985 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 }
987 /* Check for asynchronous exceptions. */
988 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400989 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 tstate->async_exc = NULL;
991 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400992 PyErr_SetNone(exc);
993 Py_DECREF(exc);
994 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 }
996 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 fast_next_opcode:
999 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Łukasz Langaa785c872016-09-09 17:37:37 -07001001 if (PyDTrace_LINE_ENABLED())
1002 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001007 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001008 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 /* see maybe_call_line_trace
1010 for expository comments */
1011 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 err = maybe_call_line_trace(tstate->c_tracefunc,
1014 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001015 tstate, f,
1016 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 /* Reload possibly changed frame fields */
1018 JUMPTO(f->f_lasti);
1019 if (f->f_stacktop != NULL) {
1020 stack_pointer = f->f_stacktop;
1021 f->f_stacktop = NULL;
1022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001023 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001025 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001029
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001030 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001031 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001032#ifdef DYNAMIC_EXECUTION_PROFILE
1033#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 dxpairs[lastopcode][opcode]++;
1035 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001036#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001038#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001039
Guido van Rossum96a42c81992-01-12 02:29:51 +00001040#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 if (lltrace) {
1044 if (HAS_ARG(opcode)) {
1045 printf("%d: %d, %d\n",
1046 f->f_lasti, opcode, oparg);
1047 }
1048 else {
1049 printf("%d: %d\n",
1050 f->f_lasti, opcode);
1051 }
1052 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001053#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 /* BEWARE!
1058 It is essential that any operation that fails sets either
1059 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1060 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 TARGET(NOP)
1063 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001064
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001065 TARGET(LOAD_FAST) {
1066 PyObject *value = GETLOCAL(oparg);
1067 if (value == NULL) {
1068 format_exc_check_arg(PyExc_UnboundLocalError,
1069 UNBOUNDLOCAL_ERROR_MSG,
1070 PyTuple_GetItem(co->co_varnames, oparg));
1071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001073 Py_INCREF(value);
1074 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001076 }
1077
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001078 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001079 TARGET(LOAD_CONST) {
1080 PyObject *value = GETITEM(consts, oparg);
1081 Py_INCREF(value);
1082 PUSH(value);
1083 FAST_DISPATCH();
1084 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001085
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001086 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001087 TARGET(STORE_FAST) {
1088 PyObject *value = POP();
1089 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001091 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001092
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 TARGET(POP_TOP) {
1094 PyObject *value = POP();
1095 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001097 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001098
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001099 TARGET(ROT_TWO) {
1100 PyObject *top = TOP();
1101 PyObject *second = SECOND();
1102 SET_TOP(second);
1103 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001105 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001107 TARGET(ROT_THREE) {
1108 PyObject *top = TOP();
1109 PyObject *second = SECOND();
1110 PyObject *third = THIRD();
1111 SET_TOP(second);
1112 SET_SECOND(third);
1113 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001117 TARGET(DUP_TOP) {
1118 PyObject *top = TOP();
1119 Py_INCREF(top);
1120 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001122 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001123
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001124 TARGET(DUP_TOP_TWO) {
1125 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001126 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001127 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001129 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001130 SET_TOP(top);
1131 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001132 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 TARGET(UNARY_POSITIVE) {
1136 PyObject *value = TOP();
1137 PyObject *res = PyNumber_Positive(value);
1138 Py_DECREF(value);
1139 SET_TOP(res);
1140 if (res == NULL)
1141 goto error;
1142 DISPATCH();
1143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001144
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001145 TARGET(UNARY_NEGATIVE) {
1146 PyObject *value = TOP();
1147 PyObject *res = PyNumber_Negative(value);
1148 Py_DECREF(value);
1149 SET_TOP(res);
1150 if (res == NULL)
1151 goto error;
1152 DISPATCH();
1153 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001155 TARGET(UNARY_NOT) {
1156 PyObject *value = TOP();
1157 int err = PyObject_IsTrue(value);
1158 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (err == 0) {
1160 Py_INCREF(Py_True);
1161 SET_TOP(Py_True);
1162 DISPATCH();
1163 }
1164 else if (err > 0) {
1165 Py_INCREF(Py_False);
1166 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 DISPATCH();
1168 }
1169 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001170 goto error;
1171 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 TARGET(UNARY_INVERT) {
1174 PyObject *value = TOP();
1175 PyObject *res = PyNumber_Invert(value);
1176 Py_DECREF(value);
1177 SET_TOP(res);
1178 if (res == NULL)
1179 goto error;
1180 DISPATCH();
1181 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001182
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 TARGET(BINARY_POWER) {
1184 PyObject *exp = POP();
1185 PyObject *base = TOP();
1186 PyObject *res = PyNumber_Power(base, exp, Py_None);
1187 Py_DECREF(base);
1188 Py_DECREF(exp);
1189 SET_TOP(res);
1190 if (res == NULL)
1191 goto error;
1192 DISPATCH();
1193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 TARGET(BINARY_MULTIPLY) {
1196 PyObject *right = POP();
1197 PyObject *left = TOP();
1198 PyObject *res = PyNumber_Multiply(left, right);
1199 Py_DECREF(left);
1200 Py_DECREF(right);
1201 SET_TOP(res);
1202 if (res == NULL)
1203 goto error;
1204 DISPATCH();
1205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Benjamin Petersond51374e2014-04-09 23:55:56 -04001207 TARGET(BINARY_MATRIX_MULTIPLY) {
1208 PyObject *right = POP();
1209 PyObject *left = TOP();
1210 PyObject *res = PyNumber_MatrixMultiply(left, right);
1211 Py_DECREF(left);
1212 Py_DECREF(right);
1213 SET_TOP(res);
1214 if (res == NULL)
1215 goto error;
1216 DISPATCH();
1217 }
1218
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001219 TARGET(BINARY_TRUE_DIVIDE) {
1220 PyObject *divisor = POP();
1221 PyObject *dividend = TOP();
1222 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1223 Py_DECREF(dividend);
1224 Py_DECREF(divisor);
1225 SET_TOP(quotient);
1226 if (quotient == NULL)
1227 goto error;
1228 DISPATCH();
1229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001231 TARGET(BINARY_FLOOR_DIVIDE) {
1232 PyObject *divisor = POP();
1233 PyObject *dividend = TOP();
1234 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1235 Py_DECREF(dividend);
1236 Py_DECREF(divisor);
1237 SET_TOP(quotient);
1238 if (quotient == NULL)
1239 goto error;
1240 DISPATCH();
1241 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 TARGET(BINARY_MODULO) {
1244 PyObject *divisor = POP();
1245 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001246 PyObject *res;
1247 if (PyUnicode_CheckExact(dividend) && (
1248 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1249 // fast path; string formatting, but not if the RHS is a str subclass
1250 // (see issue28598)
1251 res = PyUnicode_Format(dividend, divisor);
1252 } else {
1253 res = PyNumber_Remainder(dividend, divisor);
1254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 Py_DECREF(divisor);
1256 Py_DECREF(dividend);
1257 SET_TOP(res);
1258 if (res == NULL)
1259 goto error;
1260 DISPATCH();
1261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 TARGET(BINARY_ADD) {
1264 PyObject *right = POP();
1265 PyObject *left = TOP();
1266 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001267 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1268 CPython using bytecode, it is simply worthless.
1269 See http://bugs.python.org/issue21955 and
1270 http://bugs.python.org/issue10044 for the discussion. In short,
1271 no patch shown any impact on a realistic benchmark, only a minor
1272 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 if (PyUnicode_CheckExact(left) &&
1274 PyUnicode_CheckExact(right)) {
1275 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001276 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001277 }
1278 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001279 sum = PyNumber_Add(left, right);
1280 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 Py_DECREF(right);
1283 SET_TOP(sum);
1284 if (sum == NULL)
1285 goto error;
1286 DISPATCH();
1287 }
1288
1289 TARGET(BINARY_SUBTRACT) {
1290 PyObject *right = POP();
1291 PyObject *left = TOP();
1292 PyObject *diff = PyNumber_Subtract(left, right);
1293 Py_DECREF(right);
1294 Py_DECREF(left);
1295 SET_TOP(diff);
1296 if (diff == NULL)
1297 goto error;
1298 DISPATCH();
1299 }
1300
1301 TARGET(BINARY_SUBSCR) {
1302 PyObject *sub = POP();
1303 PyObject *container = TOP();
1304 PyObject *res = PyObject_GetItem(container, sub);
1305 Py_DECREF(container);
1306 Py_DECREF(sub);
1307 SET_TOP(res);
1308 if (res == NULL)
1309 goto error;
1310 DISPATCH();
1311 }
1312
1313 TARGET(BINARY_LSHIFT) {
1314 PyObject *right = POP();
1315 PyObject *left = TOP();
1316 PyObject *res = PyNumber_Lshift(left, right);
1317 Py_DECREF(left);
1318 Py_DECREF(right);
1319 SET_TOP(res);
1320 if (res == NULL)
1321 goto error;
1322 DISPATCH();
1323 }
1324
1325 TARGET(BINARY_RSHIFT) {
1326 PyObject *right = POP();
1327 PyObject *left = TOP();
1328 PyObject *res = PyNumber_Rshift(left, right);
1329 Py_DECREF(left);
1330 Py_DECREF(right);
1331 SET_TOP(res);
1332 if (res == NULL)
1333 goto error;
1334 DISPATCH();
1335 }
1336
1337 TARGET(BINARY_AND) {
1338 PyObject *right = POP();
1339 PyObject *left = TOP();
1340 PyObject *res = PyNumber_And(left, right);
1341 Py_DECREF(left);
1342 Py_DECREF(right);
1343 SET_TOP(res);
1344 if (res == NULL)
1345 goto error;
1346 DISPATCH();
1347 }
1348
1349 TARGET(BINARY_XOR) {
1350 PyObject *right = POP();
1351 PyObject *left = TOP();
1352 PyObject *res = PyNumber_Xor(left, right);
1353 Py_DECREF(left);
1354 Py_DECREF(right);
1355 SET_TOP(res);
1356 if (res == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
1360
1361 TARGET(BINARY_OR) {
1362 PyObject *right = POP();
1363 PyObject *left = TOP();
1364 PyObject *res = PyNumber_Or(left, right);
1365 Py_DECREF(left);
1366 Py_DECREF(right);
1367 SET_TOP(res);
1368 if (res == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
1372
1373 TARGET(LIST_APPEND) {
1374 PyObject *v = POP();
1375 PyObject *list = PEEK(oparg);
1376 int err;
1377 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 if (err != 0)
1380 goto error;
1381 PREDICT(JUMP_ABSOLUTE);
1382 DISPATCH();
1383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 TARGET(SET_ADD) {
1386 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001387 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 int err;
1389 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 if (err != 0)
1392 goto error;
1393 PREDICT(JUMP_ABSOLUTE);
1394 DISPATCH();
1395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(INPLACE_POWER) {
1398 PyObject *exp = POP();
1399 PyObject *base = TOP();
1400 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1401 Py_DECREF(base);
1402 Py_DECREF(exp);
1403 SET_TOP(res);
1404 if (res == NULL)
1405 goto error;
1406 DISPATCH();
1407 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 TARGET(INPLACE_MULTIPLY) {
1410 PyObject *right = POP();
1411 PyObject *left = TOP();
1412 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1413 Py_DECREF(left);
1414 Py_DECREF(right);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Petersond51374e2014-04-09 23:55:56 -04001421 TARGET(INPLACE_MATRIX_MULTIPLY) {
1422 PyObject *right = POP();
1423 PyObject *left = TOP();
1424 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1425 Py_DECREF(left);
1426 Py_DECREF(right);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
1432
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 TARGET(INPLACE_TRUE_DIVIDE) {
1434 PyObject *divisor = POP();
1435 PyObject *dividend = TOP();
1436 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1437 Py_DECREF(dividend);
1438 Py_DECREF(divisor);
1439 SET_TOP(quotient);
1440 if (quotient == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 TARGET(INPLACE_FLOOR_DIVIDE) {
1446 PyObject *divisor = POP();
1447 PyObject *dividend = TOP();
1448 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1449 Py_DECREF(dividend);
1450 Py_DECREF(divisor);
1451 SET_TOP(quotient);
1452 if (quotient == NULL)
1453 goto error;
1454 DISPATCH();
1455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 TARGET(INPLACE_MODULO) {
1458 PyObject *right = POP();
1459 PyObject *left = TOP();
1460 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1461 Py_DECREF(left);
1462 Py_DECREF(right);
1463 SET_TOP(mod);
1464 if (mod == NULL)
1465 goto error;
1466 DISPATCH();
1467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001469 TARGET(INPLACE_ADD) {
1470 PyObject *right = POP();
1471 PyObject *left = TOP();
1472 PyObject *sum;
1473 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1474 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001475 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001476 }
1477 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 sum = PyNumber_InPlaceAdd(left, right);
1479 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 Py_DECREF(right);
1482 SET_TOP(sum);
1483 if (sum == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 TARGET(INPLACE_SUBTRACT) {
1489 PyObject *right = POP();
1490 PyObject *left = TOP();
1491 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1492 Py_DECREF(left);
1493 Py_DECREF(right);
1494 SET_TOP(diff);
1495 if (diff == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 TARGET(INPLACE_LSHIFT) {
1501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *res = PyNumber_InPlaceLshift(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(res);
1507 if (res == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 TARGET(INPLACE_RSHIFT) {
1513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_InPlaceRshift(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 TARGET(INPLACE_AND) {
1525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceAnd(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(INPLACE_XOR) {
1537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_InPlaceXor(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 TARGET(INPLACE_OR) {
1549 PyObject *right = POP();
1550 PyObject *left = TOP();
1551 PyObject *res = PyNumber_InPlaceOr(left, right);
1552 Py_DECREF(left);
1553 Py_DECREF(right);
1554 SET_TOP(res);
1555 if (res == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001559
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 TARGET(STORE_SUBSCR) {
1561 PyObject *sub = TOP();
1562 PyObject *container = SECOND();
1563 PyObject *v = THIRD();
1564 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001566 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 Py_DECREF(container);
1570 Py_DECREF(sub);
1571 if (err != 0)
1572 goto error;
1573 DISPATCH();
1574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001576 TARGET(STORE_ANNOTATION) {
1577 _Py_IDENTIFIER(__annotations__);
1578 PyObject *ann_dict;
1579 PyObject *ann = POP();
1580 PyObject *name = GETITEM(names, oparg);
1581 int err;
1582 if (f->f_locals == NULL) {
1583 PyErr_Format(PyExc_SystemError,
1584 "no locals found when storing annotation");
1585 Py_DECREF(ann);
1586 goto error;
1587 }
1588 /* first try to get __annotations__ from locals... */
1589 if (PyDict_CheckExact(f->f_locals)) {
1590 ann_dict = _PyDict_GetItemId(f->f_locals,
1591 &PyId___annotations__);
1592 if (ann_dict == NULL) {
1593 PyErr_SetString(PyExc_NameError,
1594 "__annotations__ not found");
1595 Py_DECREF(ann);
1596 goto error;
1597 }
1598 Py_INCREF(ann_dict);
1599 }
1600 else {
1601 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1602 if (ann_str == NULL) {
1603 Py_DECREF(ann);
1604 goto error;
1605 }
1606 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1607 if (ann_dict == NULL) {
1608 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1609 PyErr_SetString(PyExc_NameError,
1610 "__annotations__ not found");
1611 }
1612 Py_DECREF(ann);
1613 goto error;
1614 }
1615 }
1616 /* ...if succeeded, __annotations__[name] = ann */
1617 if (PyDict_CheckExact(ann_dict)) {
1618 err = PyDict_SetItem(ann_dict, name, ann);
1619 }
1620 else {
1621 err = PyObject_SetItem(ann_dict, name, ann);
1622 }
1623 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001624 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001625 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001626 goto error;
1627 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001628 DISPATCH();
1629 }
1630
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 TARGET(DELETE_SUBSCR) {
1632 PyObject *sub = TOP();
1633 PyObject *container = SECOND();
1634 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001636 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 err = PyObject_DelItem(container, sub);
1638 Py_DECREF(container);
1639 Py_DECREF(sub);
1640 if (err != 0)
1641 goto error;
1642 DISPATCH();
1643 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001644
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001646 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001648 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001649 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 PyErr_SetString(PyExc_RuntimeError,
1652 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 Py_DECREF(value);
1654 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001656 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 Py_DECREF(value);
1658 if (res == NULL)
1659 goto error;
1660 Py_DECREF(res);
1661 DISPATCH();
1662 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001663
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 TARGET(RAISE_VARARGS) {
1665 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 switch (oparg) {
1667 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001669 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001672 /* fall through */
1673 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 if (do_raise(exc, cause)) {
1675 why = WHY_EXCEPTION;
1676 goto fast_block_end;
1677 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 break;
1679 default:
1680 PyErr_SetString(PyExc_SystemError,
1681 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 break;
1683 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 goto error;
1685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 retval = POP();
1689 why = WHY_RETURN;
1690 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001692
Yury Selivanov75445082015-05-11 22:57:16 -04001693 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001694 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001695 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001696 PyObject *obj = TOP();
1697 PyTypeObject *type = Py_TYPE(obj);
1698
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001699 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001700 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001701 }
Yury Selivanov75445082015-05-11 22:57:16 -04001702
1703 if (getter != NULL) {
1704 iter = (*getter)(obj);
1705 Py_DECREF(obj);
1706 if (iter == NULL) {
1707 SET_TOP(NULL);
1708 goto error;
1709 }
1710 }
1711 else {
1712 SET_TOP(NULL);
1713 PyErr_Format(
1714 PyExc_TypeError,
1715 "'async for' requires an object with "
1716 "__aiter__ method, got %.100s",
1717 type->tp_name);
1718 Py_DECREF(obj);
1719 goto error;
1720 }
1721
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001722 if (Py_TYPE(iter)->tp_as_async == NULL ||
1723 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001724
Yury Selivanov398ff912017-03-02 22:20:00 -05001725 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001726 PyErr_Format(
1727 PyExc_TypeError,
1728 "'async for' received an object from __aiter__ "
1729 "that does not implement __anext__: %.100s",
1730 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001731 Py_DECREF(iter);
1732 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001733 }
1734
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001735 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001736 DISPATCH();
1737 }
1738
1739 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001740 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001741 PyObject *next_iter = NULL;
1742 PyObject *awaitable = NULL;
1743 PyObject *aiter = TOP();
1744 PyTypeObject *type = Py_TYPE(aiter);
1745
Yury Selivanoveb636452016-09-08 22:01:51 -07001746 if (PyAsyncGen_CheckExact(aiter)) {
1747 awaitable = type->tp_as_async->am_anext(aiter);
1748 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001749 goto error;
1750 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001751 } else {
1752 if (type->tp_as_async != NULL){
1753 getter = type->tp_as_async->am_anext;
1754 }
Yury Selivanov75445082015-05-11 22:57:16 -04001755
Yury Selivanoveb636452016-09-08 22:01:51 -07001756 if (getter != NULL) {
1757 next_iter = (*getter)(aiter);
1758 if (next_iter == NULL) {
1759 goto error;
1760 }
1761 }
1762 else {
1763 PyErr_Format(
1764 PyExc_TypeError,
1765 "'async for' requires an iterator with "
1766 "__anext__ method, got %.100s",
1767 type->tp_name);
1768 goto error;
1769 }
Yury Selivanov75445082015-05-11 22:57:16 -04001770
Yury Selivanoveb636452016-09-08 22:01:51 -07001771 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1772 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001773 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001774 PyExc_TypeError,
1775 "'async for' received an invalid object "
1776 "from __anext__: %.100s",
1777 Py_TYPE(next_iter)->tp_name);
1778
1779 Py_DECREF(next_iter);
1780 goto error;
1781 } else {
1782 Py_DECREF(next_iter);
1783 }
1784 }
Yury Selivanov75445082015-05-11 22:57:16 -04001785
1786 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001787 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001788 DISPATCH();
1789 }
1790
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001791 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001792 TARGET(GET_AWAITABLE) {
1793 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001794 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001795
1796 Py_DECREF(iterable);
1797
Yury Selivanovc724bae2016-03-02 11:30:46 -05001798 if (iter != NULL && PyCoro_CheckExact(iter)) {
1799 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1800 if (yf != NULL) {
1801 /* `iter` is a coroutine object that is being
1802 awaited, `yf` is a pointer to the current awaitable
1803 being awaited on. */
1804 Py_DECREF(yf);
1805 Py_CLEAR(iter);
1806 PyErr_SetString(
1807 PyExc_RuntimeError,
1808 "coroutine is being awaited already");
1809 /* The code below jumps to `error` if `iter` is NULL. */
1810 }
1811 }
1812
Yury Selivanov75445082015-05-11 22:57:16 -04001813 SET_TOP(iter); /* Even if it's NULL */
1814
1815 if (iter == NULL) {
1816 goto error;
1817 }
1818
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001819 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001820 DISPATCH();
1821 }
1822
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 TARGET(YIELD_FROM) {
1824 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001825 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001827 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1828 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001829 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001830 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001832 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001833 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001834 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001835 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 Py_DECREF(v);
1837 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001838 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001839 if (tstate->c_tracefunc != NULL
1840 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001841 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001842 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 if (err < 0)
1844 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001845 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 SET_TOP(val);
1847 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001848 }
Martin Panter95f53c12016-07-18 08:23:26 +00001849 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001850 f->f_stacktop = stack_pointer;
1851 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001852 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001853 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001854 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001855 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001857
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001860
1861 if (co->co_flags & CO_ASYNC_GENERATOR) {
1862 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1863 Py_DECREF(retval);
1864 if (w == NULL) {
1865 retval = NULL;
1866 goto error;
1867 }
1868 retval = w;
1869 }
1870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 f->f_stacktop = stack_pointer;
1872 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001875
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 TARGET(POP_EXCEPT) {
1877 PyTryBlock *b = PyFrame_BlockPop(f);
1878 if (b->b_type != EXCEPT_HANDLER) {
1879 PyErr_SetString(PyExc_SystemError,
1880 "popped block is not an except handler");
1881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001886
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001887 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 TARGET(POP_BLOCK) {
1889 PyTryBlock *b = PyFrame_BlockPop(f);
1890 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 TARGET(END_FINALLY) {
1896 PyObject *status = POP();
1897 if (PyLong_Check(status)) {
1898 why = (enum why_code) PyLong_AS_LONG(status);
1899 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 if (why == WHY_RETURN ||
1901 why == WHY_CONTINUE)
1902 retval = POP();
1903 if (why == WHY_SILENCED) {
1904 /* An exception was silenced by 'with', we must
1905 manually unwind the EXCEPT_HANDLER block which was
1906 created when the exception was caught, otherwise
1907 the stack will be in an inconsistent state. */
1908 PyTryBlock *b = PyFrame_BlockPop(f);
1909 assert(b->b_type == EXCEPT_HANDLER);
1910 UNWIND_EXCEPT_HANDLER(b);
1911 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 Py_DECREF(status);
1913 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 Py_DECREF(status);
1916 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 else if (PyExceptionClass_Check(status)) {
1919 PyObject *exc = POP();
1920 PyObject *tb = POP();
1921 PyErr_Restore(status, exc, tb);
1922 why = WHY_EXCEPTION;
1923 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 PyErr_SetString(PyExc_SystemError,
1927 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 Py_DECREF(status);
1929 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 Py_DECREF(status);
1932 DISPATCH();
1933 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001936 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001937
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001939 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1941 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001942 PyErr_SetString(PyExc_NameError,
1943 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001945 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001947 }
1948 else {
1949 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1950 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001951 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001952 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1953 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001954 if (PyErr_ExceptionMatches(PyExc_KeyError))
1955 PyErr_SetString(PyExc_NameError,
1956 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001961 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001963
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 TARGET(STORE_NAME) {
1965 PyObject *name = GETITEM(names, oparg);
1966 PyObject *v = POP();
1967 PyObject *ns = f->f_locals;
1968 int err;
1969 if (ns == NULL) {
1970 PyErr_Format(PyExc_SystemError,
1971 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 if (PyDict_CheckExact(ns))
1976 err = PyDict_SetItem(ns, name, v);
1977 else
1978 err = PyObject_SetItem(ns, name, v);
1979 Py_DECREF(v);
1980 if (err != 0)
1981 goto error;
1982 DISPATCH();
1983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 TARGET(DELETE_NAME) {
1986 PyObject *name = GETITEM(names, oparg);
1987 PyObject *ns = f->f_locals;
1988 int err;
1989 if (ns == NULL) {
1990 PyErr_Format(PyExc_SystemError,
1991 "no locals when deleting %R", name);
1992 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001994 err = PyObject_DelItem(ns, name);
1995 if (err != 0) {
1996 format_exc_check_arg(PyExc_NameError,
1997 NAME_ERROR_MSG,
1998 name);
1999 goto error;
2000 }
2001 DISPATCH();
2002 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002003
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002004 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 TARGET(UNPACK_SEQUENCE) {
2006 PyObject *seq = POP(), *item, **items;
2007 if (PyTuple_CheckExact(seq) &&
2008 PyTuple_GET_SIZE(seq) == oparg) {
2009 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 item = items[oparg];
2012 Py_INCREF(item);
2013 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 } else if (PyList_CheckExact(seq) &&
2016 PyList_GET_SIZE(seq) == oparg) {
2017 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 item = items[oparg];
2020 Py_INCREF(item);
2021 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 stack_pointer + oparg)) {
2025 STACKADJ(oparg);
2026 } else {
2027 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 Py_DECREF(seq);
2029 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002032 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002034
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 TARGET(UNPACK_EX) {
2036 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2037 PyObject *seq = POP();
2038
2039 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2040 stack_pointer + totalargs)) {
2041 stack_pointer += totalargs;
2042 } else {
2043 Py_DECREF(seq);
2044 goto error;
2045 }
2046 Py_DECREF(seq);
2047 DISPATCH();
2048 }
2049
2050 TARGET(STORE_ATTR) {
2051 PyObject *name = GETITEM(names, oparg);
2052 PyObject *owner = TOP();
2053 PyObject *v = SECOND();
2054 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 Py_DECREF(owner);
2059 if (err != 0)
2060 goto error;
2061 DISPATCH();
2062 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002063
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 TARGET(DELETE_ATTR) {
2065 PyObject *name = GETITEM(names, oparg);
2066 PyObject *owner = POP();
2067 int err;
2068 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2069 Py_DECREF(owner);
2070 if (err != 0)
2071 goto error;
2072 DISPATCH();
2073 }
2074
2075 TARGET(STORE_GLOBAL) {
2076 PyObject *name = GETITEM(names, oparg);
2077 PyObject *v = POP();
2078 int err;
2079 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 if (err != 0)
2082 goto error;
2083 DISPATCH();
2084 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 TARGET(DELETE_GLOBAL) {
2087 PyObject *name = GETITEM(names, oparg);
2088 int err;
2089 err = PyDict_DelItem(f->f_globals, name);
2090 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002092 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002094 }
2095 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 TARGET(LOAD_NAME) {
2099 PyObject *name = GETITEM(names, oparg);
2100 PyObject *locals = f->f_locals;
2101 PyObject *v;
2102 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 "no locals when loading %R", name);
2105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 if (PyDict_CheckExact(locals)) {
2108 v = PyDict_GetItem(locals, name);
2109 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 }
2111 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002113 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002114 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2115 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 PyErr_Clear();
2117 }
2118 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 if (v == NULL) {
2120 v = PyDict_GetItem(f->f_globals, name);
2121 Py_XINCREF(v);
2122 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002123 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 v = PyDict_GetItem(f->f_builtins, name);
2125 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002126 format_exc_check_arg(
2127 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 NAME_ERROR_MSG, name);
2129 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002132 }
2133 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 v = PyObject_GetItem(f->f_builtins, name);
2135 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002136 if (PyErr_ExceptionMatches(PyExc_KeyError))
2137 format_exc_check_arg(
2138 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 NAME_ERROR_MSG, name);
2140 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002141 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 TARGET(LOAD_GLOBAL) {
2150 PyObject *name = GETITEM(names, oparg);
2151 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002152 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002153 && PyDict_CheckExact(f->f_builtins))
2154 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002156 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 name);
2158 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002159 if (!_PyErr_OCCURRED()) {
2160 /* _PyDict_LoadGlobal() returns NULL without raising
2161 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002162 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002163 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002169 else {
2170 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002171
2172 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 v = PyObject_GetItem(f->f_globals, name);
2174 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002175 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2176 goto error;
2177 PyErr_Clear();
2178
Victor Stinnerb4efc962015-11-20 09:24:02 +01002179 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 v = PyObject_GetItem(f->f_builtins, name);
2181 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002182 if (PyErr_ExceptionMatches(PyExc_KeyError))
2183 format_exc_check_arg(
2184 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002185 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002187 }
2188 }
2189 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002193
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 TARGET(DELETE_FAST) {
2195 PyObject *v = GETLOCAL(oparg);
2196 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 SETLOCAL(oparg, NULL);
2198 DISPATCH();
2199 }
2200 format_exc_check_arg(
2201 PyExc_UnboundLocalError,
2202 UNBOUNDLOCAL_ERROR_MSG,
2203 PyTuple_GetItem(co->co_varnames, oparg)
2204 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 goto error;
2206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 TARGET(DELETE_DEREF) {
2209 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002210 PyObject *oldobj = PyCell_GET(cell);
2211 if (oldobj != NULL) {
2212 PyCell_SET(cell, NULL);
2213 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002214 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002215 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002216 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 goto error;
2218 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002219
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 TARGET(LOAD_CLOSURE) {
2221 PyObject *cell = freevars[oparg];
2222 Py_INCREF(cell);
2223 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002226
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002227 TARGET(LOAD_CLASSDEREF) {
2228 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002229 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002230 assert(locals);
2231 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2232 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2233 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2234 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2235 if (PyDict_CheckExact(locals)) {
2236 value = PyDict_GetItem(locals, name);
2237 Py_XINCREF(value);
2238 }
2239 else {
2240 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002241 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002242 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2243 goto error;
2244 PyErr_Clear();
2245 }
2246 }
2247 if (!value) {
2248 PyObject *cell = freevars[oparg];
2249 value = PyCell_GET(cell);
2250 if (value == NULL) {
2251 format_exc_unbound(co, oparg);
2252 goto error;
2253 }
2254 Py_INCREF(value);
2255 }
2256 PUSH(value);
2257 DISPATCH();
2258 }
2259
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 TARGET(LOAD_DEREF) {
2261 PyObject *cell = freevars[oparg];
2262 PyObject *value = PyCell_GET(cell);
2263 if (value == NULL) {
2264 format_exc_unbound(co, oparg);
2265 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 Py_INCREF(value);
2268 PUSH(value);
2269 DISPATCH();
2270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 TARGET(STORE_DEREF) {
2273 PyObject *v = POP();
2274 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002275 PyObject *oldobj = PyCell_GET(cell);
2276 PyCell_SET(cell, v);
2277 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 DISPATCH();
2279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002280
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002281 TARGET(BUILD_STRING) {
2282 PyObject *str;
2283 PyObject *empty = PyUnicode_New(0, 0);
2284 if (empty == NULL) {
2285 goto error;
2286 }
2287 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2288 Py_DECREF(empty);
2289 if (str == NULL)
2290 goto error;
2291 while (--oparg >= 0) {
2292 PyObject *item = POP();
2293 Py_DECREF(item);
2294 }
2295 PUSH(str);
2296 DISPATCH();
2297 }
2298
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 TARGET(BUILD_TUPLE) {
2300 PyObject *tup = PyTuple_New(oparg);
2301 if (tup == NULL)
2302 goto error;
2303 while (--oparg >= 0) {
2304 PyObject *item = POP();
2305 PyTuple_SET_ITEM(tup, oparg, item);
2306 }
2307 PUSH(tup);
2308 DISPATCH();
2309 }
2310
2311 TARGET(BUILD_LIST) {
2312 PyObject *list = PyList_New(oparg);
2313 if (list == NULL)
2314 goto error;
2315 while (--oparg >= 0) {
2316 PyObject *item = POP();
2317 PyList_SET_ITEM(list, oparg, item);
2318 }
2319 PUSH(list);
2320 DISPATCH();
2321 }
2322
Serhiy Storchaka73442852016-10-02 10:33:46 +03002323 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002324 TARGET(BUILD_TUPLE_UNPACK)
2325 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002326 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002327 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002328 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002329 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002330
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002331 if (sum == NULL)
2332 goto error;
2333
2334 for (i = oparg; i > 0; i--) {
2335 PyObject *none_val;
2336
2337 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2338 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002339 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002340 PyErr_ExceptionMatches(PyExc_TypeError))
2341 {
2342 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002343 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002344 Py_DECREF(sum);
2345 goto error;
2346 }
2347 Py_DECREF(none_val);
2348 }
2349
2350 if (convert_to_tuple) {
2351 return_value = PyList_AsTuple(sum);
2352 Py_DECREF(sum);
2353 if (return_value == NULL)
2354 goto error;
2355 }
2356 else {
2357 return_value = sum;
2358 }
2359
2360 while (oparg--)
2361 Py_DECREF(POP());
2362 PUSH(return_value);
2363 DISPATCH();
2364 }
2365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 TARGET(BUILD_SET) {
2367 PyObject *set = PySet_New(NULL);
2368 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002369 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 if (set == NULL)
2371 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002372 for (i = oparg; i > 0; i--) {
2373 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 if (err == 0)
2375 err = PySet_Add(set, item);
2376 Py_DECREF(item);
2377 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002378 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 if (err != 0) {
2380 Py_DECREF(set);
2381 goto error;
2382 }
2383 PUSH(set);
2384 DISPATCH();
2385 }
2386
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002387 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002388 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002389 PyObject *sum = PySet_New(NULL);
2390 if (sum == NULL)
2391 goto error;
2392
2393 for (i = oparg; i > 0; i--) {
2394 if (_PySet_Update(sum, PEEK(i)) < 0) {
2395 Py_DECREF(sum);
2396 goto error;
2397 }
2398 }
2399
2400 while (oparg--)
2401 Py_DECREF(POP());
2402 PUSH(sum);
2403 DISPATCH();
2404 }
2405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002407 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2409 if (map == NULL)
2410 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002411 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002412 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002413 PyObject *key = PEEK(2*i);
2414 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002415 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002416 if (err != 0) {
2417 Py_DECREF(map);
2418 goto error;
2419 }
2420 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002421
2422 while (oparg--) {
2423 Py_DECREF(POP());
2424 Py_DECREF(POP());
2425 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 PUSH(map);
2427 DISPATCH();
2428 }
2429
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002430 TARGET(SETUP_ANNOTATIONS) {
2431 _Py_IDENTIFIER(__annotations__);
2432 int err;
2433 PyObject *ann_dict;
2434 if (f->f_locals == NULL) {
2435 PyErr_Format(PyExc_SystemError,
2436 "no locals found when setting up annotations");
2437 goto error;
2438 }
2439 /* check if __annotations__ in locals()... */
2440 if (PyDict_CheckExact(f->f_locals)) {
2441 ann_dict = _PyDict_GetItemId(f->f_locals,
2442 &PyId___annotations__);
2443 if (ann_dict == NULL) {
2444 /* ...if not, create a new one */
2445 ann_dict = PyDict_New();
2446 if (ann_dict == NULL) {
2447 goto error;
2448 }
2449 err = _PyDict_SetItemId(f->f_locals,
2450 &PyId___annotations__, ann_dict);
2451 Py_DECREF(ann_dict);
2452 if (err != 0) {
2453 goto error;
2454 }
2455 }
2456 }
2457 else {
2458 /* do the same if locals() is not a dict */
2459 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2460 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002461 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002462 }
2463 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2464 if (ann_dict == NULL) {
2465 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2466 goto error;
2467 }
2468 PyErr_Clear();
2469 ann_dict = PyDict_New();
2470 if (ann_dict == NULL) {
2471 goto error;
2472 }
2473 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2474 Py_DECREF(ann_dict);
2475 if (err != 0) {
2476 goto error;
2477 }
2478 }
2479 else {
2480 Py_DECREF(ann_dict);
2481 }
2482 }
2483 DISPATCH();
2484 }
2485
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002486 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002487 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002488 PyObject *map;
2489 PyObject *keys = TOP();
2490 if (!PyTuple_CheckExact(keys) ||
2491 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2492 PyErr_SetString(PyExc_SystemError,
2493 "bad BUILD_CONST_KEY_MAP keys argument");
2494 goto error;
2495 }
2496 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2497 if (map == NULL) {
2498 goto error;
2499 }
2500 for (i = oparg; i > 0; i--) {
2501 int err;
2502 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2503 PyObject *value = PEEK(i + 1);
2504 err = PyDict_SetItem(map, key, value);
2505 if (err != 0) {
2506 Py_DECREF(map);
2507 goto error;
2508 }
2509 }
2510
2511 Py_DECREF(POP());
2512 while (oparg--) {
2513 Py_DECREF(POP());
2514 }
2515 PUSH(map);
2516 DISPATCH();
2517 }
2518
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002519 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002520 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002521 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002522 if (sum == NULL)
2523 goto error;
2524
2525 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 if (PyDict_Update(sum, arg) < 0) {
2528 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2529 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002530 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002531 arg->ob_type->tp_name);
2532 }
2533 Py_DECREF(sum);
2534 goto error;
2535 }
2536 }
2537
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002538 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002539 Py_DECREF(POP());
2540 PUSH(sum);
2541 DISPATCH();
2542 }
2543
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002544 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2545 Py_ssize_t i;
2546 PyObject *sum = PyDict_New();
2547 if (sum == NULL)
2548 goto error;
2549
2550 for (i = oparg; i > 0; i--) {
2551 PyObject *arg = PEEK(i);
2552 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2553 PyObject *func = PEEK(2 + oparg);
2554 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002555 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002556 }
2557 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2558 PyObject *exc, *val, *tb;
2559 PyErr_Fetch(&exc, &val, &tb);
2560 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2561 PyObject *key = PyTuple_GET_ITEM(val, 0);
2562 if (!PyUnicode_Check(key)) {
2563 PyErr_Format(PyExc_TypeError,
2564 "%.200s%.200s keywords must be strings",
2565 PyEval_GetFuncName(func),
2566 PyEval_GetFuncDesc(func));
2567 } else {
2568 PyErr_Format(PyExc_TypeError,
2569 "%.200s%.200s got multiple "
2570 "values for keyword argument '%U'",
2571 PyEval_GetFuncName(func),
2572 PyEval_GetFuncDesc(func),
2573 key);
2574 }
2575 Py_XDECREF(exc);
2576 Py_XDECREF(val);
2577 Py_XDECREF(tb);
2578 }
2579 else {
2580 PyErr_Restore(exc, val, tb);
2581 }
2582 }
2583 Py_DECREF(sum);
2584 goto error;
2585 }
2586 }
2587
2588 while (oparg--)
2589 Py_DECREF(POP());
2590 PUSH(sum);
2591 DISPATCH();
2592 }
2593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002594 TARGET(MAP_ADD) {
2595 PyObject *key = TOP();
2596 PyObject *value = SECOND();
2597 PyObject *map;
2598 int err;
2599 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002600 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002602 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 Py_DECREF(value);
2604 Py_DECREF(key);
2605 if (err != 0)
2606 goto error;
2607 PREDICT(JUMP_ABSOLUTE);
2608 DISPATCH();
2609 }
2610
2611 TARGET(LOAD_ATTR) {
2612 PyObject *name = GETITEM(names, oparg);
2613 PyObject *owner = TOP();
2614 PyObject *res = PyObject_GetAttr(owner, name);
2615 Py_DECREF(owner);
2616 SET_TOP(res);
2617 if (res == NULL)
2618 goto error;
2619 DISPATCH();
2620 }
2621
2622 TARGET(COMPARE_OP) {
2623 PyObject *right = POP();
2624 PyObject *left = TOP();
2625 PyObject *res = cmp_outcome(oparg, left, right);
2626 Py_DECREF(left);
2627 Py_DECREF(right);
2628 SET_TOP(res);
2629 if (res == NULL)
2630 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 PREDICT(POP_JUMP_IF_FALSE);
2632 PREDICT(POP_JUMP_IF_TRUE);
2633 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002634 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002635
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002638 PyObject *fromlist = POP();
2639 PyObject *level = TOP();
2640 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002641 res = import_name(f, name, fromlist, level);
2642 Py_DECREF(level);
2643 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002644 SET_TOP(res);
2645 if (res == NULL)
2646 goto error;
2647 DISPATCH();
2648 }
2649
2650 TARGET(IMPORT_STAR) {
2651 PyObject *from = POP(), *locals;
2652 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002653 if (PyFrame_FastToLocalsWithError(f) < 0) {
2654 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002655 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002656 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002657
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 locals = f->f_locals;
2659 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 PyErr_SetString(PyExc_SystemError,
2661 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002662 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 Py_DECREF(from);
2668 if (err != 0)
2669 goto error;
2670 DISPATCH();
2671 }
Guido van Rossum25831651993-05-19 14:50:45 +00002672
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 TARGET(IMPORT_FROM) {
2674 PyObject *name = GETITEM(names, oparg);
2675 PyObject *from = TOP();
2676 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 PUSH(res);
2679 if (res == NULL)
2680 goto error;
2681 DISPATCH();
2682 }
Thomas Wouters52152252000-08-17 22:55:00 +00002683
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 JUMPBY(oparg);
2686 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002688
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002689 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 TARGET(POP_JUMP_IF_FALSE) {
2691 PyObject *cond = POP();
2692 int err;
2693 if (cond == Py_True) {
2694 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 FAST_DISPATCH();
2696 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 if (cond == Py_False) {
2698 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 JUMPTO(oparg);
2700 FAST_DISPATCH();
2701 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 err = PyObject_IsTrue(cond);
2703 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002705 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 else if (err == 0)
2707 JUMPTO(oparg);
2708 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002712
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002713 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 TARGET(POP_JUMP_IF_TRUE) {
2715 PyObject *cond = POP();
2716 int err;
2717 if (cond == Py_False) {
2718 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 FAST_DISPATCH();
2720 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002721 if (cond == Py_True) {
2722 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 JUMPTO(oparg);
2724 FAST_DISPATCH();
2725 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 err = PyObject_IsTrue(cond);
2727 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 JUMPTO(oparg);
2730 }
2731 else if (err == 0)
2732 ;
2733 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002737
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 TARGET(JUMP_IF_FALSE_OR_POP) {
2739 PyObject *cond = TOP();
2740 int err;
2741 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 FAST_DISPATCH();
2745 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 JUMPTO(oparg);
2748 FAST_DISPATCH();
2749 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 if (err > 0) {
2752 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 }
2755 else if (err == 0)
2756 JUMPTO(oparg);
2757 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002761
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 TARGET(JUMP_IF_TRUE_OR_POP) {
2763 PyObject *cond = TOP();
2764 int err;
2765 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 FAST_DISPATCH();
2769 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 JUMPTO(oparg);
2772 FAST_DISPATCH();
2773 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 JUMPTO(oparg);
2777 }
2778 else if (err == 0) {
2779 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 }
2782 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002786
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002787 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002790#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 /* Enabling this path speeds-up all while and for-loops by bypassing
2792 the per-loop checks for signals. By default, this should be turned-off
2793 because it prevents detection of a control-break in tight loops like
2794 "while 1: pass". Compile with this option turned-on when you need
2795 the speed-up and do not need break checking inside tight loops (ones
2796 that contain only instructions ending with FAST_DISPATCH).
2797 */
2798 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002799#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002801#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002807 PyObject *iter = PyObject_GetIter(iterable);
2808 Py_DECREF(iterable);
2809 SET_TOP(iter);
2810 if (iter == NULL)
2811 goto error;
2812 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002813 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002814 DISPATCH();
2815 }
2816
2817 TARGET(GET_YIELD_FROM_ITER) {
2818 /* before: [obj]; after [getiter(obj)] */
2819 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002820 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002821 if (PyCoro_CheckExact(iterable)) {
2822 /* `iterable` is a coroutine */
2823 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2824 /* and it is used in a 'yield from' expression of a
2825 regular generator. */
2826 Py_DECREF(iterable);
2827 SET_TOP(NULL);
2828 PyErr_SetString(PyExc_TypeError,
2829 "cannot 'yield from' a coroutine object "
2830 "in a non-coroutine generator");
2831 goto error;
2832 }
2833 }
2834 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002835 /* `iterable` is not a generator. */
2836 iter = PyObject_GetIter(iterable);
2837 Py_DECREF(iterable);
2838 SET_TOP(iter);
2839 if (iter == NULL)
2840 goto error;
2841 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002842 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 DISPATCH();
2844 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002845
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002846 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 PyObject *iter = TOP();
2850 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2851 if (next != NULL) {
2852 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 PREDICT(STORE_FAST);
2854 PREDICT(UNPACK_SEQUENCE);
2855 DISPATCH();
2856 }
2857 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2859 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002860 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002861 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 PyErr_Clear();
2863 }
2864 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 STACKADJ(-1);
2866 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002868 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002871
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 why = WHY_BREAK;
2874 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002876
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 if (retval == NULL)
2880 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 why = WHY_CONTINUE;
2882 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002884
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002885 TARGET(SETUP_LOOP)
2886 TARGET(SETUP_EXCEPT)
2887 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 /* NOTE: If you add any new block-setup opcodes that
2889 are not try/except/finally handlers, you may need
2890 to update the PyGen_NeedsFinalizing() function.
2891 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2894 STACK_LEVEL());
2895 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002897
Yury Selivanov75445082015-05-11 22:57:16 -04002898 TARGET(BEFORE_ASYNC_WITH) {
2899 _Py_IDENTIFIER(__aexit__);
2900 _Py_IDENTIFIER(__aenter__);
2901
2902 PyObject *mgr = TOP();
2903 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2904 *enter;
2905 PyObject *res;
2906 if (exit == NULL)
2907 goto error;
2908 SET_TOP(exit);
2909 enter = special_lookup(mgr, &PyId___aenter__);
2910 Py_DECREF(mgr);
2911 if (enter == NULL)
2912 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002913 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002914 Py_DECREF(enter);
2915 if (res == NULL)
2916 goto error;
2917 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002918 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002919 DISPATCH();
2920 }
2921
2922 TARGET(SETUP_ASYNC_WITH) {
2923 PyObject *res = POP();
2924 /* Setup the finally block before pushing the result
2925 of __aenter__ on the stack. */
2926 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2927 STACK_LEVEL());
2928 PUSH(res);
2929 DISPATCH();
2930 }
2931
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002933 _Py_IDENTIFIER(__exit__);
2934 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002936 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002938 if (enter == NULL)
2939 goto error;
2940 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002941 if (exit == NULL) {
2942 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002947 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 Py_DECREF(enter);
2949 if (res == NULL)
2950 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 /* Setup the finally block before pushing the result
2952 of __enter__ on the stack. */
2953 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2954 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002955
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 DISPATCH();
2958 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002959
Yury Selivanov75445082015-05-11 22:57:16 -04002960 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002961 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 how/why we entered the finally clause:
2963 - TOP = None
2964 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2965 - TOP = WHY_*; no retval below it
2966 - (TOP, SECOND, THIRD) = exc_info()
2967 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2968 Below them is EXIT, the context.__exit__ bound method.
2969 In the last case, we must call
2970 EXIT(TOP, SECOND, THIRD)
2971 otherwise we must call
2972 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002973
Benjamin Peterson8f169482013-10-29 22:25:06 -04002974 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002976 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 In addition, if the stack represents an exception,
2980 *and* the function call returns a 'true' value, we
2981 push WHY_SILENCED onto the stack. END_FINALLY will
2982 then not re-raise the exception. (But non-local
2983 gotos should still be resumed.)
2984 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002985
Victor Stinner842cfff2016-12-01 14:45:31 +01002986 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01002988 PyObject *exc, *val, *tb, *res;
2989
2990 val = tb = Py_None;
2991 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 (void)POP();
2994 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 else if (PyLong_Check(exc)) {
2998 STACKADJ(-1);
2999 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 case WHY_RETURN:
3001 case WHY_CONTINUE:
3002 /* Retval in TOP. */
3003 exit_func = SECOND();
3004 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 break;
3007 default:
3008 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 break;
3011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 }
3014 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 val = SECOND();
3018 tb = THIRD();
3019 tp2 = FOURTH();
3020 exc2 = PEEK(5);
3021 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 SET_VALUE(7, tb2);
3024 SET_VALUE(6, exc2);
3025 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3027 SET_FOURTH(NULL);
3028 /* We just shifted the stack down, so we have
3029 to tell the except handler block that the
3030 values are lower than it expects. */
3031 block = &f->f_blockstack[f->f_iblock - 1];
3032 assert(block->b_type == EXCEPT_HANDLER);
3033 block->b_level--;
3034 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003035
3036 stack[0] = exc;
3037 stack[1] = val;
3038 stack[2] = tb;
3039 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 if (res == NULL)
3042 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003043
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003044 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003045 PUSH(exc);
3046 PUSH(res);
3047 PREDICT(WITH_CLEANUP_FINISH);
3048 DISPATCH();
3049 }
3050
3051 PREDICTED(WITH_CLEANUP_FINISH);
3052 TARGET(WITH_CLEANUP_FINISH) {
3053 PyObject *res = POP();
3054 PyObject *exc = POP();
3055 int err;
3056
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 if (exc != Py_None)
3058 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 else
3060 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003061
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003063 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 /* There was an exception and a True return */
3069 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3070 }
3071 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003074
Yury Selivanovf2392132016-12-13 19:03:51 -05003075 TARGET(LOAD_METHOD) {
3076 /* Designed to work in tamdem with CALL_METHOD. */
3077 PyObject *name = GETITEM(names, oparg);
3078 PyObject *obj = TOP();
3079 PyObject *meth = NULL;
3080
3081 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3082
Yury Selivanovf2392132016-12-13 19:03:51 -05003083 if (meth == NULL) {
3084 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003085 goto error;
3086 }
3087
3088 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003089 /* We can bypass temporary bound method object.
3090 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003091
INADA Naoki015bce62017-01-16 17:23:30 +09003092 meth | self | arg1 | ... | argN
3093 */
3094 SET_TOP(meth);
3095 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003096 }
3097 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003098 /* meth is not an unbound method (but a regular attr, or
3099 something was returned by a descriptor protocol). Set
3100 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003101 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003102
3103 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003104 */
INADA Naoki015bce62017-01-16 17:23:30 +09003105 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003106 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003107 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003108 }
3109 DISPATCH();
3110 }
3111
3112 TARGET(CALL_METHOD) {
3113 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003114 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003115
3116 sp = stack_pointer;
3117
INADA Naoki015bce62017-01-16 17:23:30 +09003118 meth = PEEK(oparg + 2);
3119 if (meth == NULL) {
3120 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3121 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003122
3123 Stack layout:
3124
INADA Naoki015bce62017-01-16 17:23:30 +09003125 ... | NULL | callable | arg1 | ... | argN
3126 ^- TOP()
3127 ^- (-oparg)
3128 ^- (-oparg-1)
3129 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003130
Ville Skyttä49b27342017-08-03 09:00:59 +03003131 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003132 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003133 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003134 res = call_function(&sp, oparg, NULL);
3135 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003136 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003137 }
3138 else {
3139 /* This is a method call. Stack layout:
3140
INADA Naoki015bce62017-01-16 17:23:30 +09003141 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003142 ^- TOP()
3143 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003144 ^- (-oparg-1)
3145 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003146
INADA Naoki015bce62017-01-16 17:23:30 +09003147 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003148 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003149 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003150 */
3151 res = call_function(&sp, oparg + 1, NULL);
3152 stack_pointer = sp;
3153 }
3154
3155 PUSH(res);
3156 if (res == NULL)
3157 goto error;
3158 DISPATCH();
3159 }
3160
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003161 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 TARGET(CALL_FUNCTION) {
3163 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003165 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003168 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003173
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003174 TARGET(CALL_FUNCTION_KW) {
3175 PyObject **sp, *res, *names;
3176
3177 names = POP();
3178 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003180 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003183 Py_DECREF(names);
3184
3185 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003187 }
3188 DISPATCH();
3189 }
3190
3191 TARGET(CALL_FUNCTION_EX) {
3192 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003193 if (oparg & 0x01) {
3194 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003195 if (!PyDict_CheckExact(kwargs)) {
3196 PyObject *d = PyDict_New();
3197 if (d == NULL)
3198 goto error;
3199 if (PyDict_Update(d, kwargs) != 0) {
3200 Py_DECREF(d);
3201 /* PyDict_Update raises attribute
3202 * error (percolated from an attempt
3203 * to get 'keys' attribute) instead of
3204 * a type error if its second argument
3205 * is not a mapping.
3206 */
3207 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003208 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003209 }
Victor Stinnereece2222016-09-12 11:16:37 +02003210 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003211 goto error;
3212 }
3213 Py_DECREF(kwargs);
3214 kwargs = d;
3215 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003216 assert(PyDict_CheckExact(kwargs));
3217 }
3218 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003219 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003220 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003221 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003222 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003223 goto error;
3224 }
3225 Py_SETREF(callargs, PySequence_Tuple(callargs));
3226 if (callargs == NULL) {
3227 goto error;
3228 }
3229 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003230 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003231
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003232 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003233 Py_DECREF(func);
3234 Py_DECREF(callargs);
3235 Py_XDECREF(kwargs);
3236
3237 SET_TOP(result);
3238 if (result == NULL) {
3239 goto error;
3240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003243
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003244 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003245 PyObject *qualname = POP();
3246 PyObject *codeobj = POP();
3247 PyFunctionObject *func = (PyFunctionObject *)
3248 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003249
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003250 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003252 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003255
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003256 if (oparg & 0x08) {
3257 assert(PyTuple_CheckExact(TOP()));
3258 func ->func_closure = POP();
3259 }
3260 if (oparg & 0x04) {
3261 assert(PyDict_CheckExact(TOP()));
3262 func->func_annotations = POP();
3263 }
3264 if (oparg & 0x02) {
3265 assert(PyDict_CheckExact(TOP()));
3266 func->func_kwdefaults = POP();
3267 }
3268 if (oparg & 0x01) {
3269 assert(PyTuple_CheckExact(TOP()));
3270 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003272
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003273 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003274 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003276
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 TARGET(BUILD_SLICE) {
3278 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 step = NULL;
3283 stop = POP();
3284 start = TOP();
3285 slice = PySlice_New(start, stop, step);
3286 Py_DECREF(start);
3287 Py_DECREF(stop);
3288 Py_XDECREF(step);
3289 SET_TOP(slice);
3290 if (slice == NULL)
3291 goto error;
3292 DISPATCH();
3293 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003294
Eric V. Smitha78c7952015-11-03 12:45:05 -05003295 TARGET(FORMAT_VALUE) {
3296 /* Handles f-string value formatting. */
3297 PyObject *result;
3298 PyObject *fmt_spec;
3299 PyObject *value;
3300 PyObject *(*conv_fn)(PyObject *);
3301 int which_conversion = oparg & FVC_MASK;
3302 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3303
3304 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003305 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003306
3307 /* See if any conversion is specified. */
3308 switch (which_conversion) {
3309 case FVC_STR: conv_fn = PyObject_Str; break;
3310 case FVC_REPR: conv_fn = PyObject_Repr; break;
3311 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3312
3313 /* Must be 0 (meaning no conversion), since only four
3314 values are allowed by (oparg & FVC_MASK). */
3315 default: conv_fn = NULL; break;
3316 }
3317
3318 /* If there's a conversion function, call it and replace
3319 value with that result. Otherwise, just use value,
3320 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003321 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003322 result = conv_fn(value);
3323 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003324 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003325 Py_XDECREF(fmt_spec);
3326 goto error;
3327 }
3328 value = result;
3329 }
3330
3331 /* If value is a unicode object, and there's no fmt_spec,
3332 then we know the result of format(value) is value
3333 itself. In that case, skip calling format(). I plan to
3334 move this optimization in to PyObject_Format()
3335 itself. */
3336 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3337 /* Do nothing, just transfer ownership to result. */
3338 result = value;
3339 } else {
3340 /* Actually call format(). */
3341 result = PyObject_Format(value, fmt_spec);
3342 Py_DECREF(value);
3343 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003344 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003345 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003346 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003347 }
3348
Eric V. Smith135d5f42016-02-05 18:23:08 -05003349 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003350 DISPATCH();
3351 }
3352
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003354 int oldoparg = oparg;
3355 NEXTOPARG();
3356 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003359
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003360
Antoine Pitrou042b1282010-08-13 21:15:58 +00003361#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003363#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 default:
3365 fprintf(stderr,
3366 "XXX lineno: %d, opcode: %d\n",
3367 PyFrame_GetLineNumber(f),
3368 opcode);
3369 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003373
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 /* This should never be reached. Every opcode should end with DISPATCH()
3375 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003376 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003377
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003379
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 assert(why == WHY_NOT);
3381 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003382
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003384#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 if (!PyErr_Occurred())
3386 PyErr_SetString(PyExc_SystemError,
3387 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003388#else
3389 assert(PyErr_Occurred());
3390#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003392 /* Log traceback info. */
3393 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003394
Benjamin Peterson51f46162013-01-23 08:38:47 -05003395 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003396 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3397 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003398
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003399fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 assert(why != WHY_NOT);
3401
3402 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 while (why != WHY_NOT && f->f_iblock > 0) {
3404 /* Peek at the current block. */
3405 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 assert(why != WHY_YIELD);
3408 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3409 why = WHY_NOT;
3410 JUMPTO(PyLong_AS_LONG(retval));
3411 Py_DECREF(retval);
3412 break;
3413 }
3414 /* Now we have to pop the block. */
3415 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 if (b->b_type == EXCEPT_HANDLER) {
3418 UNWIND_EXCEPT_HANDLER(b);
3419 continue;
3420 }
3421 UNWIND_BLOCK(b);
3422 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3423 why = WHY_NOT;
3424 JUMPTO(b->b_handler);
3425 break;
3426 }
3427 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3428 || b->b_type == SETUP_FINALLY)) {
3429 PyObject *exc, *val, *tb;
3430 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003431 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 /* Beware, this invalidates all b->b_* fields */
3433 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003434 PUSH(exc_info->exc_traceback);
3435 PUSH(exc_info->exc_value);
3436 if (exc_info->exc_type != NULL) {
3437 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 }
3439 else {
3440 Py_INCREF(Py_None);
3441 PUSH(Py_None);
3442 }
3443 PyErr_Fetch(&exc, &val, &tb);
3444 /* Make the raw exception data
3445 available to the handler,
3446 so a program can emulate the
3447 Python main loop. */
3448 PyErr_NormalizeException(
3449 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003450 if (tb != NULL)
3451 PyException_SetTraceback(val, tb);
3452 else
3453 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003455 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003457 exc_info->exc_value = val;
3458 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 if (tb == NULL)
3460 tb = Py_None;
3461 Py_INCREF(tb);
3462 PUSH(tb);
3463 PUSH(val);
3464 PUSH(exc);
3465 why = WHY_NOT;
3466 JUMPTO(handler);
3467 break;
3468 }
3469 if (b->b_type == SETUP_FINALLY) {
3470 if (why & (WHY_RETURN | WHY_CONTINUE))
3471 PUSH(retval);
3472 PUSH(PyLong_FromLong((long)why));
3473 why = WHY_NOT;
3474 JUMPTO(b->b_handler);
3475 break;
3476 }
3477 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (why != WHY_NOT)
3482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003483
Victor Stinnerace47d72013-07-18 01:41:08 +02003484 assert(!PyErr_Occurred());
3485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 assert(why != WHY_YIELD);
3489 /* Pop remaining stack entries. */
3490 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003491 PyObject *o = POP();
3492 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 if (why != WHY_RETURN)
3496 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003497
Victor Stinner4a7cc882015-03-06 23:35:27 +01003498 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003499
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003500fast_yield:
Benjamin Peterson83195c32011-07-03 13:44:00 -05003501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003503 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003505 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3506 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003508 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 why = WHY_EXCEPTION;
3510 }
3511 }
3512 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003513 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3514 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 PyTrace_RETURN, NULL);
3516 }
3517 }
3518 if (tstate->c_profilefunc) {
3519 if (why == WHY_EXCEPTION)
3520 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003521 tstate->c_profileobj,
3522 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003524 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3525 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003527 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003528 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 }
3530 }
3531 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003534exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003535 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3536 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003538 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003540
Victor Stinnerefde1462015-03-21 15:04:43 +01003541 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003542}
3543
Benjamin Petersonb204a422011-06-05 22:04:07 -05003544static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003545format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3546{
3547 int err;
3548 Py_ssize_t len = PyList_GET_SIZE(names);
3549 PyObject *name_str, *comma, *tail, *tmp;
3550
3551 assert(PyList_CheckExact(names));
3552 assert(len >= 1);
3553 /* Deal with the joys of natural language. */
3554 switch (len) {
3555 case 1:
3556 name_str = PyList_GET_ITEM(names, 0);
3557 Py_INCREF(name_str);
3558 break;
3559 case 2:
3560 name_str = PyUnicode_FromFormat("%U and %U",
3561 PyList_GET_ITEM(names, len - 2),
3562 PyList_GET_ITEM(names, len - 1));
3563 break;
3564 default:
3565 tail = PyUnicode_FromFormat(", %U, and %U",
3566 PyList_GET_ITEM(names, len - 2),
3567 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003568 if (tail == NULL)
3569 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003570 /* Chop off the last two objects in the list. This shouldn't actually
3571 fail, but we can't be too careful. */
3572 err = PyList_SetSlice(names, len - 2, len, NULL);
3573 if (err == -1) {
3574 Py_DECREF(tail);
3575 return;
3576 }
3577 /* Stitch everything up into a nice comma-separated list. */
3578 comma = PyUnicode_FromString(", ");
3579 if (comma == NULL) {
3580 Py_DECREF(tail);
3581 return;
3582 }
3583 tmp = PyUnicode_Join(comma, names);
3584 Py_DECREF(comma);
3585 if (tmp == NULL) {
3586 Py_DECREF(tail);
3587 return;
3588 }
3589 name_str = PyUnicode_Concat(tmp, tail);
3590 Py_DECREF(tmp);
3591 Py_DECREF(tail);
3592 break;
3593 }
3594 if (name_str == NULL)
3595 return;
3596 PyErr_Format(PyExc_TypeError,
3597 "%U() missing %i required %s argument%s: %U",
3598 co->co_name,
3599 len,
3600 kind,
3601 len == 1 ? "" : "s",
3602 name_str);
3603 Py_DECREF(name_str);
3604}
3605
3606static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003607missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003608 PyObject **fastlocals)
3609{
Victor Stinner74319ae2016-08-25 00:04:09 +02003610 Py_ssize_t i, j = 0;
3611 Py_ssize_t start, end;
3612 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003613 const char *kind = positional ? "positional" : "keyword-only";
3614 PyObject *missing_names;
3615
3616 /* Compute the names of the arguments that are missing. */
3617 missing_names = PyList_New(missing);
3618 if (missing_names == NULL)
3619 return;
3620 if (positional) {
3621 start = 0;
3622 end = co->co_argcount - defcount;
3623 }
3624 else {
3625 start = co->co_argcount;
3626 end = start + co->co_kwonlyargcount;
3627 }
3628 for (i = start; i < end; i++) {
3629 if (GETLOCAL(i) == NULL) {
3630 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3631 PyObject *name = PyObject_Repr(raw);
3632 if (name == NULL) {
3633 Py_DECREF(missing_names);
3634 return;
3635 }
3636 PyList_SET_ITEM(missing_names, j++, name);
3637 }
3638 }
3639 assert(j == missing);
3640 format_missing(kind, co, missing_names);
3641 Py_DECREF(missing_names);
3642}
3643
3644static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003645too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3646 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003647{
3648 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003649 Py_ssize_t kwonly_given = 0;
3650 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003651 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003652 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003653
Benjamin Petersone109c702011-06-24 09:37:26 -05003654 assert((co->co_flags & CO_VARARGS) == 0);
3655 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003656 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3657 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003658 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003659 }
3660 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003661 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003662 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003663 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003664 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003665 }
3666 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003667 plural = (co_argcount != 1);
3668 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003669 }
3670 if (sig == NULL)
3671 return;
3672 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003673 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3674 kwonly_sig = PyUnicode_FromFormat(format,
3675 given != 1 ? "s" : "",
3676 kwonly_given,
3677 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003678 if (kwonly_sig == NULL) {
3679 Py_DECREF(sig);
3680 return;
3681 }
3682 }
3683 else {
3684 /* This will not fail. */
3685 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003686 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003687 }
3688 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003689 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003690 co->co_name,
3691 sig,
3692 plural ? "s" : "",
3693 given,
3694 kwonly_sig,
3695 given == 1 && !kwonly_given ? "was" : "were");
3696 Py_DECREF(sig);
3697 Py_DECREF(kwonly_sig);
3698}
3699
Guido van Rossumc2e20742006-02-27 22:32:47 +00003700/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003701 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003702 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003703
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003704PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003705_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003706 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003707 PyObject **kwnames, PyObject **kwargs,
3708 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003709 PyObject **defs, Py_ssize_t defcount,
3710 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003711 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003712{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003713 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003714 PyFrameObject *f;
3715 PyObject *retval = NULL;
3716 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003717 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003719 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3720 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003721 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 if (globals == NULL) {
3724 PyErr_SetString(PyExc_SystemError,
3725 "PyEval_EvalCodeEx: NULL globals");
3726 return NULL;
3727 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003728
Victor Stinnerc7020012016-08-16 23:40:29 +02003729 /* Create the frame */
3730 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003732 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003733 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003735 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 fastlocals = f->f_localsplus;
3737 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003738
Victor Stinnerc7020012016-08-16 23:40:29 +02003739 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003740 if (co->co_flags & CO_VARKEYWORDS) {
3741 kwdict = PyDict_New();
3742 if (kwdict == NULL)
3743 goto fail;
3744 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003745 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003747 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 SETLOCAL(i, kwdict);
3749 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003750 else {
3751 kwdict = NULL;
3752 }
3753
3754 /* Copy positional arguments into local variables */
3755 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003757 }
3758 else {
3759 n = argcount;
3760 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003761 for (i = 0; i < n; i++) {
3762 x = args[i];
3763 Py_INCREF(x);
3764 SETLOCAL(i, x);
3765 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003766
3767 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 if (co->co_flags & CO_VARARGS) {
3769 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003770 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003771 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003772 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003773 SETLOCAL(total_args, u);
3774 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 x = args[i];
3776 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003780
Serhiy Storchakab7281052016-09-12 00:52:40 +03003781 /* Handle keyword arguments passed as two strided arrays */
3782 kwcount *= kwstep;
3783 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003784 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003785 PyObject *keyword = kwnames[i];
3786 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003787 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003788
Benjamin Petersonb204a422011-06-05 22:04:07 -05003789 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3790 PyErr_Format(PyExc_TypeError,
3791 "%U() keywords must be strings",
3792 co->co_name);
3793 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003795
Benjamin Petersonb204a422011-06-05 22:04:07 -05003796 /* Speed hack: do raw pointer compares. As names are
3797 normally interned this should almost always hit. */
3798 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3799 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003800 PyObject *name = co_varnames[j];
3801 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003803 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003805
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 /* Slow fallback, just in case */
3807 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003808 PyObject *name = co_varnames[j];
3809 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3810 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003812 }
3813 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003815 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003816 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003817
Victor Stinner231d1f32017-01-11 02:12:06 +01003818 assert(j >= total_args);
3819 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003820 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003821 "%U() got an unexpected keyword argument '%S'",
3822 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003825
Christian Heimes0bd447f2013-07-20 14:48:10 +02003826 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3827 goto fail;
3828 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003830
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 kw_found:
3832 if (GETLOCAL(j) != NULL) {
3833 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003834 "%U() got multiple values for argument '%S'",
3835 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003838 Py_INCREF(value);
3839 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003841
3842 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003844 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 goto fail;
3846 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003847
3848 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003850 Py_ssize_t m = co->co_argcount - defcount;
3851 Py_ssize_t missing = 0;
3852 for (i = argcount; i < m; i++) {
3853 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003854 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003855 }
3856 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003857 if (missing) {
3858 missing_arguments(co, missing, defcount, fastlocals);
3859 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 }
3861 if (n > m)
3862 i = n - m;
3863 else
3864 i = 0;
3865 for (; i < defcount; i++) {
3866 if (GETLOCAL(m+i) == NULL) {
3867 PyObject *def = defs[i];
3868 Py_INCREF(def);
3869 SETLOCAL(m+i, def);
3870 }
3871 }
3872 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003873
3874 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003876 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 for (i = co->co_argcount; i < total_args; i++) {
3878 PyObject *name;
3879 if (GETLOCAL(i) != NULL)
3880 continue;
3881 name = PyTuple_GET_ITEM(co->co_varnames, i);
3882 if (kwdefs != NULL) {
3883 PyObject *def = PyDict_GetItem(kwdefs, name);
3884 if (def) {
3885 Py_INCREF(def);
3886 SETLOCAL(i, def);
3887 continue;
3888 }
3889 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003890 missing++;
3891 }
3892 if (missing) {
3893 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003894 goto fail;
3895 }
3896 }
3897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003899 vars into frame. */
3900 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003902 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003903 /* Possibly account for the cell variable being an argument. */
3904 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003905 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003906 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003907 /* Clear the local copy. */
3908 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003909 }
3910 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003911 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003912 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003913 if (c == NULL)
3914 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003915 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003917
3918 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003919 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3920 PyObject *o = PyTuple_GET_ITEM(closure, i);
3921 Py_INCREF(o);
3922 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003924
Yury Selivanoveb636452016-09-08 22:01:51 -07003925 /* Handle generator/coroutine/asynchronous generator */
3926 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003927 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003928 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003929 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003930
3931 if (is_coro && tstate->in_coroutine_wrapper) {
3932 assert(coro_wrapper != NULL);
3933 PyErr_Format(PyExc_RuntimeError,
3934 "coroutine wrapper %.200R attempted "
3935 "to recursively wrap %.200R",
3936 coro_wrapper,
3937 co);
3938 goto fail;
3939 }
Yury Selivanov75445082015-05-11 22:57:16 -04003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 /* Don't need to keep the reference to f_back, it will be set
3942 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003943 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 /* Create a new generator that owns the ready to run frame
3946 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003947 if (is_coro) {
3948 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003949 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3950 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003951 } else {
3952 gen = PyGen_NewWithQualName(f, name, qualname);
3953 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003954 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003955 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003956 }
INADA Naoki9c157762016-12-26 18:52:46 +09003957
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003958 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003959
Yury Selivanov94c22632015-06-04 10:16:51 -04003960 if (is_coro && coro_wrapper != NULL) {
3961 PyObject *wrapped;
3962 tstate->in_coroutine_wrapper = 1;
3963 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3964 tstate->in_coroutine_wrapper = 0;
3965 return wrapped;
3966 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003967
Yury Selivanov75445082015-05-11 22:57:16 -04003968 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003970
Victor Stinner59a73272016-12-09 18:51:13 +01003971 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003972
Thomas Woutersce272b62007-09-19 21:19:28 +00003973fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 /* decref'ing the frame can cause __del__ methods to get invoked,
3976 which can call back into Python. While we're done with the
3977 current Python frame (f), the associated C stack is still in use,
3978 so recursion_depth must be boosted for the duration.
3979 */
3980 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003981 if (Py_REFCNT(f) > 1) {
3982 Py_DECREF(f);
3983 _PyObject_GC_TRACK(f);
3984 }
3985 else {
3986 ++tstate->recursion_depth;
3987 Py_DECREF(f);
3988 --tstate->recursion_depth;
3989 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003991}
3992
Victor Stinner40ee3012014-06-16 15:59:28 +02003993PyObject *
3994PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
3995 PyObject **args, int argcount, PyObject **kws, int kwcount,
3996 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
3997{
3998 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003999 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004000 kws, kws != NULL ? kws + 1 : NULL,
4001 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004002 defs, defcount,
4003 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004004 NULL, NULL);
4005}
Tim Peters5ca576e2001-06-18 22:08:13 +00004006
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004007static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004008special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004011 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004013 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 return NULL;
4015 }
4016 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004017}
4018
4019
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004020/* Logic for the raise statement (too complicated for inlining).
4021 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004022static int
Collin Winter828f04a2007-08-31 00:04:24 +00004023do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 if (exc == NULL) {
4028 /* Reraise */
4029 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004030 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004032 type = exc_info->exc_type;
4033 value = exc_info->exc_value;
4034 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004035 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 PyErr_SetString(PyExc_RuntimeError,
4037 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004038 return 0;
4039 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 Py_XINCREF(type);
4041 Py_XINCREF(value);
4042 Py_XINCREF(tb);
4043 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004044 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 /* We support the following forms of raise:
4048 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004049 raise <instance>
4050 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 if (PyExceptionClass_Check(exc)) {
4053 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004054 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 if (value == NULL)
4056 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004057 if (!PyExceptionInstance_Check(value)) {
4058 PyErr_Format(PyExc_TypeError,
4059 "calling %R should have returned an instance of "
4060 "BaseException, not %R",
4061 type, Py_TYPE(value));
4062 goto raise_error;
4063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 }
4065 else if (PyExceptionInstance_Check(exc)) {
4066 value = exc;
4067 type = PyExceptionInstance_Class(exc);
4068 Py_INCREF(type);
4069 }
4070 else {
4071 /* Not something you can raise. You get an exception
4072 anyway, just not what you specified :-) */
4073 Py_DECREF(exc);
4074 PyErr_SetString(PyExc_TypeError,
4075 "exceptions must derive from BaseException");
4076 goto raise_error;
4077 }
Collin Winter828f04a2007-08-31 00:04:24 +00004078
Serhiy Storchakac0191582016-09-27 11:37:10 +03004079 assert(type != NULL);
4080 assert(value != NULL);
4081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 if (cause) {
4083 PyObject *fixed_cause;
4084 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004085 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 if (fixed_cause == NULL)
4087 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004088 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004090 else if (PyExceptionInstance_Check(cause)) {
4091 fixed_cause = cause;
4092 }
4093 else if (cause == Py_None) {
4094 Py_DECREF(cause);
4095 fixed_cause = NULL;
4096 }
4097 else {
4098 PyErr_SetString(PyExc_TypeError,
4099 "exception causes must derive from "
4100 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 goto raise_error;
4102 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004103 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 }
Collin Winter828f04a2007-08-31 00:04:24 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 PyErr_SetObject(type, value);
4107 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004108 Py_DECREF(value);
4109 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004110 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004111
4112raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 Py_XDECREF(value);
4114 Py_XDECREF(type);
4115 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004116 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004117}
4118
Tim Petersd6d010b2001-06-21 02:49:55 +00004119/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004120 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004121
Guido van Rossum0368b722007-05-11 16:50:42 +00004122 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4123 with a variable target.
4124*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004125
Barry Warsawe42b18f1997-08-25 22:13:04 +00004126static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004127unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 int i = 0, j = 0;
4130 Py_ssize_t ll = 0;
4131 PyObject *it; /* iter(v) */
4132 PyObject *w;
4133 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 it = PyObject_GetIter(v);
4138 if (it == NULL)
4139 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 for (; i < argcnt; i++) {
4142 w = PyIter_Next(it);
4143 if (w == NULL) {
4144 /* Iterator done, via error or exhaustion. */
4145 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004146 if (argcntafter == -1) {
4147 PyErr_Format(PyExc_ValueError,
4148 "not enough values to unpack (expected %d, got %d)",
4149 argcnt, i);
4150 }
4151 else {
4152 PyErr_Format(PyExc_ValueError,
4153 "not enough values to unpack "
4154 "(expected at least %d, got %d)",
4155 argcnt + argcntafter, i);
4156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 }
4158 goto Error;
4159 }
4160 *--sp = w;
4161 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 if (argcntafter == -1) {
4164 /* We better have exhausted the iterator now. */
4165 w = PyIter_Next(it);
4166 if (w == NULL) {
4167 if (PyErr_Occurred())
4168 goto Error;
4169 Py_DECREF(it);
4170 return 1;
4171 }
4172 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004173 PyErr_Format(PyExc_ValueError,
4174 "too many values to unpack (expected %d)",
4175 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 goto Error;
4177 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 l = PySequence_List(it);
4180 if (l == NULL)
4181 goto Error;
4182 *--sp = l;
4183 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 ll = PyList_GET_SIZE(l);
4186 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004187 PyErr_Format(PyExc_ValueError,
4188 "not enough values to unpack (expected at least %d, got %zd)",
4189 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 goto Error;
4191 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 /* Pop the "after-variable" args off the list. */
4194 for (j = argcntafter; j > 0; j--, i++) {
4195 *--sp = PyList_GET_ITEM(l, ll - j);
4196 }
4197 /* Resize the list. */
4198 Py_SIZE(l) = ll - argcntafter;
4199 Py_DECREF(it);
4200 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004201
Tim Petersd6d010b2001-06-21 02:49:55 +00004202Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 for (; i > 0; i--, sp++)
4204 Py_DECREF(*sp);
4205 Py_XDECREF(it);
4206 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004207}
4208
4209
Guido van Rossum96a42c81992-01-12 02:29:51 +00004210#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004211static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004212prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 printf("%s ", str);
4215 if (PyObject_Print(v, stdout, 0) != 0)
4216 PyErr_Clear(); /* Don't know what else to do */
4217 printf("\n");
4218 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004219}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004220#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004221
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004222static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004223call_exc_trace(Py_tracefunc func, PyObject *self,
4224 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004225{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004226 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004228 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 if (value == NULL) {
4230 value = Py_None;
4231 Py_INCREF(value);
4232 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004233 PyErr_NormalizeException(&type, &value, &orig_traceback);
4234 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 arg = PyTuple_Pack(3, type, value, traceback);
4236 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004237 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 return;
4239 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004240 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 Py_DECREF(arg);
4242 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004243 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 else {
4245 Py_XDECREF(type);
4246 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004247 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004249}
4250
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004251static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004252call_trace_protected(Py_tracefunc func, PyObject *obj,
4253 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 PyObject *type, *value, *traceback;
4257 int err;
4258 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004259 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 if (err == 0)
4261 {
4262 PyErr_Restore(type, value, traceback);
4263 return 0;
4264 }
4265 else {
4266 Py_XDECREF(type);
4267 Py_XDECREF(value);
4268 Py_XDECREF(traceback);
4269 return -1;
4270 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004271}
4272
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004273static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004274call_trace(Py_tracefunc func, PyObject *obj,
4275 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 int result;
4279 if (tstate->tracing)
4280 return 0;
4281 tstate->tracing++;
4282 tstate->use_tracing = 0;
4283 result = func(obj, frame, what, arg);
4284 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4285 || (tstate->c_profilefunc != NULL));
4286 tstate->tracing--;
4287 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004288}
4289
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004290PyObject *
4291_PyEval_CallTracing(PyObject *func, PyObject *args)
4292{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004293 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 int save_tracing = tstate->tracing;
4295 int save_use_tracing = tstate->use_tracing;
4296 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 tstate->tracing = 0;
4299 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4300 || (tstate->c_profilefunc != NULL));
4301 result = PyObject_Call(func, args, NULL);
4302 tstate->tracing = save_tracing;
4303 tstate->use_tracing = save_use_tracing;
4304 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004305}
4306
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004307/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004308static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004309maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004310 PyThreadState *tstate, PyFrameObject *frame,
4311 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 int result = 0;
4314 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 /* If the last instruction executed isn't in the current
4317 instruction window, reset the window.
4318 */
4319 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4320 PyAddrPair bounds;
4321 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4322 &bounds);
4323 *instr_lb = bounds.ap_lower;
4324 *instr_ub = bounds.ap_upper;
4325 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004326 /* If the last instruction falls at the start of a line or if it
4327 represents a jump backwards, update the frame's line number and
4328 then call the trace function if we're tracing source lines.
4329 */
4330 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004332 if (frame->f_trace_lines) {
4333 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 }
George King20faa682017-10-18 17:44:22 -07004336 /* Always emit an opcode event if we're tracing all opcodes. */
4337 if (frame->f_trace_opcodes) {
4338 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 *instr_prev = frame->f_lasti;
4341 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004342}
4343
Fred Drake5755ce62001-06-27 19:19:46 +00004344void
4345PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 PyThreadState *tstate = PyThreadState_GET();
4348 PyObject *temp = tstate->c_profileobj;
4349 Py_XINCREF(arg);
4350 tstate->c_profilefunc = NULL;
4351 tstate->c_profileobj = NULL;
4352 /* Must make sure that tracing is not ignored if 'temp' is freed */
4353 tstate->use_tracing = tstate->c_tracefunc != NULL;
4354 Py_XDECREF(temp);
4355 tstate->c_profilefunc = func;
4356 tstate->c_profileobj = arg;
4357 /* Flag that tracing or profiling is turned on */
4358 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004359}
4360
4361void
4362PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyThreadState *tstate = PyThreadState_GET();
4365 PyObject *temp = tstate->c_traceobj;
4366 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4367 Py_XINCREF(arg);
4368 tstate->c_tracefunc = NULL;
4369 tstate->c_traceobj = NULL;
4370 /* Must make sure that profiling is not ignored if 'temp' is freed */
4371 tstate->use_tracing = tstate->c_profilefunc != NULL;
4372 Py_XDECREF(temp);
4373 tstate->c_tracefunc = func;
4374 tstate->c_traceobj = arg;
4375 /* Flag that tracing or profiling is turned on */
4376 tstate->use_tracing = ((func != NULL)
4377 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004378}
4379
Yury Selivanov75445082015-05-11 22:57:16 -04004380void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004381_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004382{
4383 PyThreadState *tstate = PyThreadState_GET();
4384
Yury Selivanov75445082015-05-11 22:57:16 -04004385 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004386 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004387}
4388
4389PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004390_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004391{
4392 PyThreadState *tstate = PyThreadState_GET();
4393 return tstate->coroutine_wrapper;
4394}
4395
Yury Selivanoveb636452016-09-08 22:01:51 -07004396void
4397_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4398{
4399 PyThreadState *tstate = PyThreadState_GET();
4400
4401 Py_XINCREF(firstiter);
4402 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4403}
4404
4405PyObject *
4406_PyEval_GetAsyncGenFirstiter(void)
4407{
4408 PyThreadState *tstate = PyThreadState_GET();
4409 return tstate->async_gen_firstiter;
4410}
4411
4412void
4413_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4414{
4415 PyThreadState *tstate = PyThreadState_GET();
4416
4417 Py_XINCREF(finalizer);
4418 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4419}
4420
4421PyObject *
4422_PyEval_GetAsyncGenFinalizer(void)
4423{
4424 PyThreadState *tstate = PyThreadState_GET();
4425 return tstate->async_gen_finalizer;
4426}
4427
Guido van Rossumb209a111997-04-29 18:18:01 +00004428PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004429PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 PyFrameObject *current_frame = PyEval_GetFrame();
4432 if (current_frame == NULL)
4433 return PyThreadState_GET()->interp->builtins;
4434 else
4435 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004436}
4437
Guido van Rossumb209a111997-04-29 18:18:01 +00004438PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004439PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004442 if (current_frame == NULL) {
4443 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004445 }
4446
4447 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4448 return NULL;
4449
4450 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004452}
4453
Guido van Rossumb209a111997-04-29 18:18:01 +00004454PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004455PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyFrameObject *current_frame = PyEval_GetFrame();
4458 if (current_frame == NULL)
4459 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004460
4461 assert(current_frame->f_globals != NULL);
4462 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004463}
4464
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004465PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004466PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 PyThreadState *tstate = PyThreadState_GET();
4469 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004470}
4471
Guido van Rossum6135a871995-01-09 17:53:26 +00004472int
Tim Peters5ba58662001-07-16 02:29:45 +00004473PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 PyFrameObject *current_frame = PyEval_GetFrame();
4476 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 if (current_frame != NULL) {
4479 const int codeflags = current_frame->f_code->co_flags;
4480 const int compilerflags = codeflags & PyCF_MASK;
4481 if (compilerflags) {
4482 result = 1;
4483 cf->cf_flags |= compilerflags;
4484 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004485#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (codeflags & CO_GENERATOR_ALLOWED) {
4487 result = 1;
4488 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4489 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 }
4492 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004493}
4494
Guido van Rossum3f5da241990-12-20 15:06:42 +00004495
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004496const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004497PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 if (PyMethod_Check(func))
4500 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4501 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004502 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 else if (PyCFunction_Check(func))
4504 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4505 else
4506 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004507}
4508
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004509const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004510PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 if (PyMethod_Check(func))
4513 return "()";
4514 else if (PyFunction_Check(func))
4515 return "()";
4516 else if (PyCFunction_Check(func))
4517 return "()";
4518 else
4519 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004520}
4521
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004522#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004523if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004524 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4525 tstate, tstate->frame, \
4526 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 x = NULL; \
4528 } \
4529 else { \
4530 x = call; \
4531 if (tstate->c_profilefunc != NULL) { \
4532 if (x == NULL) { \
4533 call_trace_protected(tstate->c_profilefunc, \
4534 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004535 tstate, tstate->frame, \
4536 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 /* XXX should pass (type, value, tb) */ \
4538 } else { \
4539 if (call_trace(tstate->c_profilefunc, \
4540 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004541 tstate, tstate->frame, \
4542 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 Py_DECREF(x); \
4544 x = NULL; \
4545 } \
4546 } \
4547 } \
4548 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004549} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 x = call; \
4551 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004552
Victor Stinner415c5102017-01-11 00:54:57 +01004553/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4554 to reduce the stack consumption. */
4555Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004556call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004557{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004558 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 PyObject *func = *pfunc;
4560 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004561 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4562 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004563 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 /* Always dispatch PyCFunction first, because these are
4566 presumed to be the most frequent callable object.
4567 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004568 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004570 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004571 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004572 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4573 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004574 if (tstate->use_tracing && tstate->c_profilefunc) {
4575 // We need to create PyCFunctionObject for tracing.
4576 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4577 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4578 if (func == NULL) {
4579 return NULL;
4580 }
4581 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4582 kwnames));
4583 Py_DECREF(func);
4584 }
4585 else {
4586 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4587 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004588 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004589 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004590 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004591 /* Optimize access to bound methods. Reuse the Python stack
4592 to pass 'self' as the first argument, replace 'func'
4593 with 'self'. It avoids the creation of a new temporary tuple
4594 for arguments (to replace func with self) when the method uses
4595 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004596 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004597 Py_INCREF(self);
4598 func = PyMethod_GET_FUNCTION(func);
4599 Py_INCREF(func);
4600 Py_SETREF(*pfunc, self);
4601 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004602 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004603 }
4604 else {
4605 Py_INCREF(func);
4606 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004607
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004608 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004609 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004610 }
4611 else {
4612 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4613 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004614 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004616
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004617 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4618
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004619 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 while ((*pp_stack) > pfunc) {
4621 w = EXT_POP(*pp_stack);
4622 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004626}
4627
Jeremy Hylton52820442001-01-03 23:52:36 +00004628static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004629do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004632 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 PyThreadState *tstate = PyThreadState_GET();
4634 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004635 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004637 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004638 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004639 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004640}
4641
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004642/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004643 nb_index slot defined, and store in *pi.
4644 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004645 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004646 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004647*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004648int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004649_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004650{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004651 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 Py_ssize_t x;
4653 if (PyIndex_Check(v)) {
4654 x = PyNumber_AsSsize_t(v, NULL);
4655 if (x == -1 && PyErr_Occurred())
4656 return 0;
4657 }
4658 else {
4659 PyErr_SetString(PyExc_TypeError,
4660 "slice indices must be integers or "
4661 "None or have an __index__ method");
4662 return 0;
4663 }
4664 *pi = x;
4665 }
4666 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004667}
4668
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004669int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004670_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004671{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004672 Py_ssize_t x;
4673 if (PyIndex_Check(v)) {
4674 x = PyNumber_AsSsize_t(v, NULL);
4675 if (x == -1 && PyErr_Occurred())
4676 return 0;
4677 }
4678 else {
4679 PyErr_SetString(PyExc_TypeError,
4680 "slice indices must be integers or "
4681 "have an __index__ method");
4682 return 0;
4683 }
4684 *pi = x;
4685 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004686}
4687
4688
Guido van Rossum486364b2007-06-30 05:01:58 +00004689#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004691
Guido van Rossumb209a111997-04-29 18:18:01 +00004692static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004693cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 int res = 0;
4696 switch (op) {
4697 case PyCmp_IS:
4698 res = (v == w);
4699 break;
4700 case PyCmp_IS_NOT:
4701 res = (v != w);
4702 break;
4703 case PyCmp_IN:
4704 res = PySequence_Contains(w, v);
4705 if (res < 0)
4706 return NULL;
4707 break;
4708 case PyCmp_NOT_IN:
4709 res = PySequence_Contains(w, v);
4710 if (res < 0)
4711 return NULL;
4712 res = !res;
4713 break;
4714 case PyCmp_EXC_MATCH:
4715 if (PyTuple_Check(w)) {
4716 Py_ssize_t i, length;
4717 length = PyTuple_Size(w);
4718 for (i = 0; i < length; i += 1) {
4719 PyObject *exc = PyTuple_GET_ITEM(w, i);
4720 if (!PyExceptionClass_Check(exc)) {
4721 PyErr_SetString(PyExc_TypeError,
4722 CANNOT_CATCH_MSG);
4723 return NULL;
4724 }
4725 }
4726 }
4727 else {
4728 if (!PyExceptionClass_Check(w)) {
4729 PyErr_SetString(PyExc_TypeError,
4730 CANNOT_CATCH_MSG);
4731 return NULL;
4732 }
4733 }
4734 res = PyErr_GivenExceptionMatches(v, w);
4735 break;
4736 default:
4737 return PyObject_RichCompare(v, w, op);
4738 }
4739 v = res ? Py_True : Py_False;
4740 Py_INCREF(v);
4741 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004742}
4743
Thomas Wouters52152252000-08-17 22:55:00 +00004744static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004745import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4746{
4747 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004748 PyObject *import_func, *res;
4749 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004750
4751 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4752 if (import_func == NULL) {
4753 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4754 return NULL;
4755 }
4756
4757 /* Fast path for not overloaded __import__. */
4758 if (import_func == PyThreadState_GET()->interp->import_func) {
4759 int ilevel = _PyLong_AsInt(level);
4760 if (ilevel == -1 && PyErr_Occurred()) {
4761 return NULL;
4762 }
4763 res = PyImport_ImportModuleLevelObject(
4764 name,
4765 f->f_globals,
4766 f->f_locals == NULL ? Py_None : f->f_locals,
4767 fromlist,
4768 ilevel);
4769 return res;
4770 }
4771
4772 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004773
4774 stack[0] = name;
4775 stack[1] = f->f_globals;
4776 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4777 stack[3] = fromlist;
4778 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004779 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004780 Py_DECREF(import_func);
4781 return res;
4782}
4783
4784static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004785import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004788 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004789 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004792 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
4793 return x;
4794 /* Issue #17636: in case this failed because of a circular relative
4795 import, try to fallback on reading the module directly from
4796 sys.modules. */
4797 PyErr_Clear();
4798 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004799 if (pkgname == NULL) {
4800 goto error;
4801 }
Oren Milman6db70332017-09-19 14:23:01 +03004802 if (!PyUnicode_Check(pkgname)) {
4803 Py_CLEAR(pkgname);
4804 goto error;
4805 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004806 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004807 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004808 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004809 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004810 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004811 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004812 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004813 if (x == NULL) {
4814 goto error;
4815 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004816 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004818 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004819 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004820 if (pkgname == NULL) {
4821 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4822 if (pkgname_or_unknown == NULL) {
4823 Py_XDECREF(pkgpath);
4824 return NULL;
4825 }
4826 } else {
4827 pkgname_or_unknown = pkgname;
4828 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004829
4830 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4831 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004832 errmsg = PyUnicode_FromFormat(
4833 "cannot import name %R from %R (unknown location)",
4834 name, pkgname_or_unknown
4835 );
4836 /* NULL check for errmsg done by PyErr_SetImportError. */
4837 PyErr_SetImportError(errmsg, pkgname, NULL);
4838 }
4839 else {
4840 errmsg = PyUnicode_FromFormat(
4841 "cannot import name %R from %R (%S)",
4842 name, pkgname_or_unknown, pkgpath
4843 );
4844 /* NULL check for errmsg done by PyErr_SetImportError. */
4845 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004846 }
4847
Xiang Zhang4830f582017-03-21 11:13:42 +08004848 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004849 Py_XDECREF(pkgname_or_unknown);
4850 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004851 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004852}
Guido van Rossumac7be682001-01-17 15:42:30 +00004853
Thomas Wouters52152252000-08-17 22:55:00 +00004854static int
4855import_all_from(PyObject *locals, PyObject *v)
4856{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004857 _Py_IDENTIFIER(__all__);
4858 _Py_IDENTIFIER(__dict__);
4859 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 PyObject *dict, *name, *value;
4861 int skip_leading_underscores = 0;
4862 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 if (all == NULL) {
4865 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4866 return -1; /* Unexpected error */
4867 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004868 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 if (dict == NULL) {
4870 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4871 return -1;
4872 PyErr_SetString(PyExc_ImportError,
4873 "from-import-* object has no __dict__ and no __all__");
4874 return -1;
4875 }
4876 all = PyMapping_Keys(dict);
4877 Py_DECREF(dict);
4878 if (all == NULL)
4879 return -1;
4880 skip_leading_underscores = 1;
4881 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 for (pos = 0, err = 0; ; pos++) {
4884 name = PySequence_GetItem(all, pos);
4885 if (name == NULL) {
4886 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4887 err = -1;
4888 else
4889 PyErr_Clear();
4890 break;
4891 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004892 if (skip_leading_underscores && PyUnicode_Check(name)) {
4893 if (PyUnicode_READY(name) == -1) {
4894 Py_DECREF(name);
4895 err = -1;
4896 break;
4897 }
4898 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4899 Py_DECREF(name);
4900 continue;
4901 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 }
4903 value = PyObject_GetAttr(v, name);
4904 if (value == NULL)
4905 err = -1;
4906 else if (PyDict_CheckExact(locals))
4907 err = PyDict_SetItem(locals, name, value);
4908 else
4909 err = PyObject_SetItem(locals, name, value);
4910 Py_DECREF(name);
4911 Py_XDECREF(value);
4912 if (err != 0)
4913 break;
4914 }
4915 Py_DECREF(all);
4916 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004917}
4918
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004919static int
4920check_args_iterable(PyObject *func, PyObject *args)
4921{
4922 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4923 PyErr_Format(PyExc_TypeError,
4924 "%.200s%.200s argument after * "
4925 "must be an iterable, not %.200s",
4926 PyEval_GetFuncName(func),
4927 PyEval_GetFuncDesc(func),
4928 args->ob_type->tp_name);
4929 return -1;
4930 }
4931 return 0;
4932}
4933
4934static void
4935format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4936{
4937 PyErr_Format(PyExc_TypeError,
4938 "%.200s%.200s argument after ** "
4939 "must be a mapping, not %.200s",
4940 PyEval_GetFuncName(func),
4941 PyEval_GetFuncDesc(func),
4942 kwargs->ob_type->tp_name);
4943}
4944
Guido van Rossumac7be682001-01-17 15:42:30 +00004945static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004946format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 if (!obj)
4951 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004952
Serhiy Storchaka06515832016-11-20 09:13:07 +02004953 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 if (!obj_str)
4955 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004958}
Guido van Rossum950361c1997-01-24 13:49:28 +00004959
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004960static void
4961format_exc_unbound(PyCodeObject *co, int oparg)
4962{
4963 PyObject *name;
4964 /* Don't stomp existing exception */
4965 if (PyErr_Occurred())
4966 return;
4967 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4968 name = PyTuple_GET_ITEM(co->co_cellvars,
4969 oparg);
4970 format_exc_check_arg(
4971 PyExc_UnboundLocalError,
4972 UNBOUNDLOCAL_ERROR_MSG,
4973 name);
4974 } else {
4975 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4976 PyTuple_GET_SIZE(co->co_cellvars));
4977 format_exc_check_arg(PyExc_NameError,
4978 UNBOUNDFREE_ERROR_MSG, name);
4979 }
4980}
4981
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004982static PyObject *
4983unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03004984 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004985{
4986 PyObject *res;
4987 if (Py_REFCNT(v) == 2) {
4988 /* In the common case, there are 2 references to the value
4989 * stored in 'variable' when the += is performed: one on the
4990 * value stack (in 'v') and one still stored in the
4991 * 'variable'. We try to delete the variable now to reduce
4992 * the refcnt to 1.
4993 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004994 int opcode, oparg;
4995 NEXTOPARG();
4996 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004997 case STORE_FAST:
4998 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004999 PyObject **fastlocals = f->f_localsplus;
5000 if (GETLOCAL(oparg) == v)
5001 SETLOCAL(oparg, NULL);
5002 break;
5003 }
5004 case STORE_DEREF:
5005 {
5006 PyObject **freevars = (f->f_localsplus +
5007 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005008 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005009 if (PyCell_GET(c) == v) {
5010 PyCell_SET(c, NULL);
5011 Py_DECREF(v);
5012 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005013 break;
5014 }
5015 case STORE_NAME:
5016 {
5017 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005018 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005019 PyObject *locals = f->f_locals;
5020 if (PyDict_CheckExact(locals) &&
5021 PyDict_GetItem(locals, name) == v) {
5022 if (PyDict_DelItem(locals, name) != 0) {
5023 PyErr_Clear();
5024 }
5025 }
5026 break;
5027 }
5028 }
5029 }
5030 res = v;
5031 PyUnicode_Append(&res, w);
5032 return res;
5033}
5034
Guido van Rossum950361c1997-01-24 13:49:28 +00005035#ifdef DYNAMIC_EXECUTION_PROFILE
5036
Skip Montanarof118cb12001-10-15 20:51:38 +00005037static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005038getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 int i;
5041 PyObject *l = PyList_New(256);
5042 if (l == NULL) return NULL;
5043 for (i = 0; i < 256; i++) {
5044 PyObject *x = PyLong_FromLong(a[i]);
5045 if (x == NULL) {
5046 Py_DECREF(l);
5047 return NULL;
5048 }
5049 PyList_SetItem(l, i, x);
5050 }
5051 for (i = 0; i < 256; i++)
5052 a[i] = 0;
5053 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005054}
5055
5056PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005057_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005058{
5059#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005061#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 int i;
5063 PyObject *l = PyList_New(257);
5064 if (l == NULL) return NULL;
5065 for (i = 0; i < 257; i++) {
5066 PyObject *x = getarray(dxpairs[i]);
5067 if (x == NULL) {
5068 Py_DECREF(l);
5069 return NULL;
5070 }
5071 PyList_SetItem(l, i, x);
5072 }
5073 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005074#endif
5075}
5076
5077#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005078
5079Py_ssize_t
5080_PyEval_RequestCodeExtraIndex(freefunc free)
5081{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005082 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005083 Py_ssize_t new_index;
5084
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005085 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005086 return -1;
5087 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005088 new_index = interp->co_extra_user_count++;
5089 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005090 return new_index;
5091}
Łukasz Langaa785c872016-09-09 17:37:37 -07005092
5093static void
5094dtrace_function_entry(PyFrameObject *f)
5095{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005096 const char *filename;
5097 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005098 int lineno;
5099
5100 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5101 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5102 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5103
5104 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5105}
5106
5107static void
5108dtrace_function_return(PyFrameObject *f)
5109{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005110 const char *filename;
5111 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005112 int lineno;
5113
5114 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5115 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5116 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5117
5118 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5119}
5120
5121/* DTrace equivalent of maybe_call_line_trace. */
5122static void
5123maybe_dtrace_line(PyFrameObject *frame,
5124 int *instr_lb, int *instr_ub, int *instr_prev)
5125{
5126 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005127 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005128
5129 /* If the last instruction executed isn't in the current
5130 instruction window, reset the window.
5131 */
5132 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5133 PyAddrPair bounds;
5134 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5135 &bounds);
5136 *instr_lb = bounds.ap_lower;
5137 *instr_ub = bounds.ap_upper;
5138 }
5139 /* If the last instruction falls at the start of a line or if
5140 it represents a jump backwards, update the frame's line
5141 number and call the trace function. */
5142 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5143 frame->f_lineno = line;
5144 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5145 if (!co_filename)
5146 co_filename = "?";
5147 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5148 if (!co_name)
5149 co_name = "?";
5150 PyDTrace_LINE(co_filename, co_name, line);
5151 }
5152 *instr_prev = frame->f_lasti;
5153}