blob: d18a284e9f9d220f0de91a9f0b1ddd37003419d0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "internal/pystate.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040016#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070019#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040020#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000021#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Guido van Rossumc6004111993-11-05 10:22:19 +000023#include <ctype.h>
24
Guido van Rossum408027e1996-12-30 16:17:54 +000025#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000026/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027#define LLTRACE 1 /* Low-level trace feature */
28#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#endif
30
Yury Selivanovf2392132016-12-13 19:03:51 -050031/* Private API for the LOAD_METHOD opcode. */
32extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
33
Jeremy Hylton52820442001-01-03 23:52:36 +000034typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000035
Guido van Rossum374a9221991-04-04 10:40:29 +000036/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060037Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
38 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070039static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000040
Guido van Rossum0a066c01992-03-27 17:29:15 +000041#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000042static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020043static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010045static int call_trace(Py_tracefunc, PyObject *,
46 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000048static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010049 PyThreadState *, PyFrameObject *,
50 int, PyObject *);
51static void call_exc_trace(Py_tracefunc, PyObject *,
52 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000053static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060054 PyThreadState *, PyFrameObject *,
55 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070056static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
57static void dtrace_function_entry(PyFrameObject *);
58static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060061static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
62 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000064static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000065static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000066static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020067static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030068 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050069static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030070static int check_args_iterable(PyObject *func, PyObject *vararg);
71static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000072
Paul Prescode68140d2000-08-30 20:25:01 +000073#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000075#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000077#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 "free variable '%.200s' referenced before assignment" \
79 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000080
Guido van Rossum950361c1997-01-24 13:49:28 +000081/* Dynamic execution profile */
82#ifdef DYNAMIC_EXECUTION_PROFILE
83#ifdef DXPAIRS
84static long dxpairs[257][256];
85#define dxp dxpairs[256]
86#else
87static long dxp[256];
88#endif
89#endif
90
Eric Snow2ebc5ce2017-09-07 23:51:28 -060091#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000092
Jeffrey Yasskin39370832010-05-03 19:29:34 +000093/* This can set eval_breaker to 0 even though gil_drop_request became
94 1. We believe this is all right because the eval loop will release
95 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000096#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -060098 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +000099 GIL_REQUEST | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600100 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
101 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000102
103#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600105 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
106 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000108
109#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600111 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 COMPUTE_EVAL_BREAKER(); \
113 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000114
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000115/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000116#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600118 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
119 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000121
122#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600124 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 COMPUTE_EVAL_BREAKER(); \
126 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000127
128#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600130 _PyRuntime.ceval.pending.async_exc = 1; \
131 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
134#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600135 do { \
136 _PyRuntime.ceval.pending.async_exc = 0; \
137 COMPUTE_EVAL_BREAKER(); \
138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
140
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000142#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000143#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000144#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000145#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146
Tim Peters7f468f22004-10-11 02:40:51 +0000147int
148PyEval_ThreadsInitialized(void)
149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000151}
152
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000153void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000154PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (gil_created())
157 return;
158 create_gil();
159 take_gil(PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600160 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
161 if (!_PyRuntime.ceval.pending.lock)
162 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000164
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000165void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000166_PyEval_FiniThreads(void)
167{
168 if (!gil_created())
169 return;
170 destroy_gil();
171 assert(!gil_created());
172}
173
174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyThreadState *tstate = PyThreadState_GET();
178 if (tstate == NULL)
179 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
180 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000181}
182
183void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000184PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 /* This function must succeed when the current thread state is NULL.
187 We therefore avoid PyThreadState_GET() which dumps a fatal error
188 in debug mode.
189 */
190 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
191 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192}
193
194void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000195PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
199 /* Check someone has called PyEval_InitThreads() to create the lock */
200 assert(gil_created());
201 take_gil(tstate);
202 if (PyThreadState_Swap(tstate) != NULL)
203 Py_FatalError(
204 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000205}
206
207void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (tstate == NULL)
211 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
212 if (PyThreadState_Swap(NULL) != tstate)
213 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
214 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000216
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200217/* This function is called from PyOS_AfterFork_Child to destroy all threads
218 * which are not running in the child process, and clear internal locks
219 * which might be held by those threads.
220 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000221
222void
223PyEval_ReInitThreads(void)
224{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200225 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (!gil_created())
228 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600230 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200231 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600232 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000233
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200234 /* Destroy all threads except the current one */
235 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000236}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238/* This function is used to signal that async exceptions are waiting to be
239 raised, therefore it is also useful in non-threaded builds. */
240
241void
242_PyEval_SignalAsyncExc(void)
243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000245}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246
Guido van Rossumff4949e1992-08-05 19:58:53 +0000247/* Functions save_thread and restore_thread are always defined so
248 dynamically loaded modules needn't be compiled separately for use
249 with and without threads: */
250
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000251PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyThreadState *tstate = PyThreadState_Swap(NULL);
255 if (tstate == NULL)
256 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100257 assert(gil_created());
258 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (tstate == NULL)
266 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100267 assert(gil_created());
268
269 int err = errno;
270 take_gil(tstate);
271 /* _Py_Finalizing is protected by the GIL */
272 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
273 drop_gil(tstate);
274 PyThread_exit_thread();
275 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100277 errno = err;
278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000280}
281
282
Guido van Rossuma9672091994-09-14 13:31:22 +0000283/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
284 signal handlers or Mac I/O completion routines) can schedule calls
285 to a function to be called synchronously.
286 The synchronous function is called with one void* argument.
287 It should return 0 for success or -1 for failure -- failure should
288 be accompanied by an exception.
289
290 If registry succeeds, the registry function returns 0; if it fails
291 (e.g. due to too many pending calls) it returns -1 (without setting
292 an exception condition).
293
294 Note that because registry may occur from within signal handlers,
295 or other asynchronous events, calling malloc() is unsafe!
296
Guido van Rossuma9672091994-09-14 13:31:22 +0000297 Any thread can schedule pending calls, but only the main thread
298 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000299 There is no facility to schedule calls to a particular thread, but
300 that should be easy to change, should that ever be required. In
301 that case, the static variables here should go into the python
302 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000303*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000304
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200305void
306_PyEval_SignalReceived(void)
307{
308 /* bpo-30703: Function called when the C signal handler of Python gets a
309 signal. We cannot queue a callback using Py_AddPendingCall() since
310 that function is not async-signal-safe. */
311 SIGNAL_PENDING_CALLS();
312}
313
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200314/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000315 scheduling to be made from any thread, and even from an executing
316 callback.
317 */
318
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000319int
320Py_AddPendingCall(int (*func)(void *), void *arg)
321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600323 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 /* try a few times for the lock. Since this mechanism is used
326 * for signal handling (on the main thread), there is a (slim)
327 * chance that a signal is delivered on the same thread while we
328 * hold the lock during the Py_MakePendingCalls() function.
329 * This avoids a deadlock in that case.
330 * Note that signals can be delivered on any thread. In particular,
331 * on Windows, a SIGINT is delivered on a system-created worker
332 * thread.
333 * We also check for lock being NULL, in the unlikely case that
334 * this function is called before any bytecode evaluation takes place.
335 */
336 if (lock != NULL) {
337 for (i = 0; i<100; i++) {
338 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
339 break;
340 }
341 if (i == 100)
342 return -1;
343 }
344
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600345 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600347 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 result = -1; /* Queue full */
349 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600350 _PyRuntime.ceval.pending.calls[i].func = func;
351 _PyRuntime.ceval.pending.calls[i].arg = arg;
352 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 /* signal main loop */
355 SIGNAL_PENDING_CALLS();
356 if (lock != NULL)
357 PyThread_release_lock(lock);
358 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000359}
360
361int
362Py_MakePendingCalls(void)
363{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200364 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 int i;
366 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000367
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200368 assert(PyGILState_Check());
369
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600370 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600372 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
373 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return -1;
375 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600378 if (_PyRuntime.ceval.pending.main_thread &&
379 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
380 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200384 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200386 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200387 /* unsignal before starting to call callbacks, so that any callback
388 added in-between re-signals */
389 UNSIGNAL_PENDING_CALLS();
390
391 /* Python signal handler doesn't really queue a callback: it only signals
392 that a signal was received, see _PyEval_SignalReceived(). */
393 if (PyErr_CheckSignals() < 0) {
394 goto error;
395 }
396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* perform a bounded number of calls, in case of recursion */
398 for (i=0; i<NPENDINGCALLS; i++) {
399 int j;
400 int (*func)(void *);
401 void *arg = NULL;
402
403 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600404 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
405 j = _PyRuntime.ceval.pending.first;
406 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 func = NULL; /* Queue empty */
408 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600409 func = _PyRuntime.ceval.pending.calls[j].func;
410 arg = _PyRuntime.ceval.pending.calls[j].arg;
411 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600413 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* having released the lock, perform the callback */
415 if (func == NULL)
416 break;
417 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200418 if (r) {
419 goto error;
420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422
Charles-François Natalif23339a2011-07-23 18:15:43 +0200423 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200425
426error:
427 busy = 0;
428 SIGNAL_PENDING_CALLS(); /* We're not done yet */
429 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000430}
431
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000432/* The interpreter's recursion limit */
433
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000434#ifndef Py_DEFAULT_RECURSION_LIMIT
435#define Py_DEFAULT_RECURSION_LIMIT 1000
436#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600437
Eric Snow05351c12017-09-05 21:43:08 -0700438int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000439
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600440void
441_PyEval_Initialize(struct _ceval_runtime_state *state)
442{
443 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
444 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
445 _gil_initialize(&state->gil);
446}
447
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000448int
449Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000450{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600451 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452}
453
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000454void
455Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000456{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600457 _PyRuntime.ceval.recursion_limit = new_limit;
458 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000459}
460
Armin Rigo2b3eb402003-10-28 12:05:48 +0000461/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
462 if the recursion_depth reaches _Py_CheckRecursionLimit.
463 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
464 to guarantee that _Py_CheckRecursiveCall() is regularly called.
465 Without USE_STACKCHECK, there is no need for this. */
466int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300467_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyThreadState *tstate = PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600470 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000471
472#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700473 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (PyOS_CheckStack()) {
475 --tstate->recursion_depth;
476 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
477 return -1;
478 }
pdox18967932017-10-25 23:03:01 -0700479 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700480 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700481#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (tstate->recursion_critical)
483 /* Somebody asked that we don't check for recursion. */
484 return 0;
485 if (tstate->overflowed) {
486 if (tstate->recursion_depth > recursion_limit + 50) {
487 /* Overflowing while handling an overflow. Give up. */
488 Py_FatalError("Cannot recover from stack overflow.");
489 }
490 return 0;
491 }
492 if (tstate->recursion_depth > recursion_limit) {
493 --tstate->recursion_depth;
494 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400495 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 "maximum recursion depth exceeded%s",
497 where);
498 return -1;
499 }
500 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000501}
502
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400503static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000504static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000505
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600506#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000507
Guido van Rossum374a9221991-04-04 10:40:29 +0000508
Guido van Rossumb209a111997-04-29 18:18:01 +0000509PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000510PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyEval_EvalCodeEx(co,
513 globals, locals,
514 (PyObject **)NULL, 0,
515 (PyObject **)NULL, 0,
516 (PyObject **)NULL, 0,
517 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000518}
519
520
521/* Interpreter main loop */
522
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000523PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000524PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 /* This is for backward compatibility with extension modules that
526 used this API; core interpreter code should call
527 PyEval_EvalFrameEx() */
528 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000529}
530
531PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000533{
Brett Cannon3cebf932016-09-05 15:33:46 -0700534 PyThreadState *tstate = PyThreadState_GET();
535 return tstate->interp->eval_frame(f, throwflag);
536}
537
Victor Stinnerc6944e72016-11-11 02:13:35 +0100538PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700539_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
540{
Guido van Rossum950361c1997-01-24 13:49:28 +0000541#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000543#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200544 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300545 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200546 int opcode; /* Current opcode */
547 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200548 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyObject *retval = NULL; /* Return value */
550 PyThreadState *tstate = PyThreadState_GET();
551 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 is true when the line being executed has changed. The
558 initial values are such as to make this false the first
559 time it is tested. */
560 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000561
Serhiy Storchakaab874002016-09-11 13:48:15 +0300562 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 PyObject *names;
564 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000565
Brett Cannon368b4b72012-04-02 12:17:59 -0400566#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200567 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400568#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200569
Antoine Pitroub52ec782009-01-25 16:34:23 +0000570/* Computed GOTOs, or
571 the-optimization-commonly-but-improperly-known-as-"threaded code"
572 using gcc's labels-as-values extension
573 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
574
575 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000577 combined with a lookup table of jump addresses. However, since the
578 indirect jump instruction is shared by all opcodes, the CPU will have a
579 hard time making the right prediction for where to jump next (actually,
580 it will be always wrong except in the uncommon case of a sequence of
581 several identical opcodes).
582
583 "Threaded code" in contrast, uses an explicit jump table and an explicit
584 indirect jump instruction at the end of each opcode. Since the jump
585 instruction is at a different address for each opcode, the CPU will make a
586 separate prediction for each of these instructions, which is equivalent to
587 predicting the second opcode of each opcode pair. These predictions have
588 a much better chance to turn out valid, especially in small bytecode loops.
589
590 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000592 and potentially many more instructions (depending on the pipeline width).
593 A correctly predicted branch, however, is nearly free.
594
595 At the time of this writing, the "threaded code" version is up to 15-20%
596 faster than the normal "switch" version, depending on the compiler and the
597 CPU architecture.
598
599 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
600 because it would render the measurements invalid.
601
602
603 NOTE: care must be taken that the compiler doesn't try to "optimize" the
604 indirect jumps by sharing them between all opcodes. Such optimizations
605 can be disabled on gcc by using the -fno-gcse flag (or possibly
606 -fno-crossjumping).
607*/
608
Antoine Pitrou042b1282010-08-13 21:15:58 +0000609#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000610#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000611#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000612#endif
613
Antoine Pitrou042b1282010-08-13 21:15:58 +0000614#ifdef HAVE_COMPUTED_GOTOS
615 #ifndef USE_COMPUTED_GOTOS
616 #define USE_COMPUTED_GOTOS 1
617 #endif
618#else
619 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
620 #error "Computed gotos are not supported on this compiler."
621 #endif
622 #undef USE_COMPUTED_GOTOS
623 #define USE_COMPUTED_GOTOS 0
624#endif
625
626#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000627/* Import the static jump table */
628#include "opcode_targets.h"
629
Antoine Pitroub52ec782009-01-25 16:34:23 +0000630#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000633
Antoine Pitroub52ec782009-01-25 16:34:23 +0000634#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600636 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 FAST_DISPATCH(); \
638 } \
639 continue; \
640 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000641
642#ifdef LLTRACE
643#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700645 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300647 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300648 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 } \
650 goto fast_next_opcode; \
651 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000652#else
653#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700655 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300657 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300658 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 } \
660 goto fast_next_opcode; \
661 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000662#endif
663
664#else
665#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300667
Antoine Pitroub52ec782009-01-25 16:34:23 +0000668#define DISPATCH() continue
669#define FAST_DISPATCH() goto fast_next_opcode
670#endif
671
672
Neal Norwitza81d2202002-07-14 00:27:26 +0000673/* Tuple access macros */
674
675#ifndef Py_DEBUG
676#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
677#else
678#define GETITEM(v, i) PyTuple_GetItem((v), (i))
679#endif
680
Guido van Rossum374a9221991-04-04 10:40:29 +0000681/* Code access macros */
682
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300683/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600684#define INSTR_OFFSET() \
685 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300686#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300687 _Py_CODEUNIT word = *next_instr; \
688 opcode = _Py_OPCODE(word); \
689 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300690 next_instr++; \
691 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300692#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
693#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000694
Raymond Hettingerf606f872003-03-16 03:11:04 +0000695/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 Some opcodes tend to come in pairs thus making it possible to
697 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300698 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 Verifying the prediction costs a single high-speed test of a register
701 variable against a constant. If the pairing was good, then the
702 processor's own internal branch predication has a high likelihood of
703 success, resulting in a nearly zero-overhead transition to the
704 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300705 including its unpredictable switch-case branch. Combined with the
706 processor's internal branch prediction, a successful PREDICT has the
707 effect of making the two opcodes run as if they were a single new opcode
708 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000709
Georg Brandl86b2fb92008-07-16 03:43:04 +0000710 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 predictions turned-on and interpret the results as if some opcodes
712 had been combined or turn-off predictions so that the opcode frequency
713 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000714
715 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 the CPU to record separate branch prediction information for each
717 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000718
Raymond Hettingerf606f872003-03-16 03:11:04 +0000719*/
720
Antoine Pitrou042b1282010-08-13 21:15:58 +0000721#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000723#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300724#define PREDICT(op) \
725 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300726 _Py_CODEUNIT word = *next_instr; \
727 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300728 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300729 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300730 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300731 goto PRED_##op; \
732 } \
733 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000734#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300735#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736
Raymond Hettingerf606f872003-03-16 03:11:04 +0000737
Guido van Rossum374a9221991-04-04 10:40:29 +0000738/* Stack manipulation macros */
739
Martin v. Löwis18e16552006-02-15 17:27:45 +0000740/* The stack can grow at most MAXINT deep, as co_nlocals and
741 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000742#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
743#define EMPTY() (STACK_LEVEL() == 0)
744#define TOP() (stack_pointer[-1])
745#define SECOND() (stack_pointer[-2])
746#define THIRD() (stack_pointer[-3])
747#define FOURTH() (stack_pointer[-4])
748#define PEEK(n) (stack_pointer[-(n)])
749#define SET_TOP(v) (stack_pointer[-1] = (v))
750#define SET_SECOND(v) (stack_pointer[-2] = (v))
751#define SET_THIRD(v) (stack_pointer[-3] = (v))
752#define SET_FOURTH(v) (stack_pointer[-4] = (v))
753#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
754#define BASIC_STACKADJ(n) (stack_pointer += n)
755#define BASIC_PUSH(v) (*stack_pointer++ = (v))
756#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000757
Guido van Rossum96a42c81992-01-12 02:29:51 +0000758#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000760 lltrace && prtrace(TOP(), "push")); \
761 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000763 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000765 lltrace && prtrace(TOP(), "stackadj")); \
766 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000767#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000768 prtrace((STACK_POINTER)[-1], "ext_pop")), \
769 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000770#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000771#define PUSH(v) BASIC_PUSH(v)
772#define POP() BASIC_POP()
773#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000774#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000775#endif
776
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777/* Local variable macros */
778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000780
781/* The SETLOCAL() macro must not DECREF the local variable in-place and
782 then store the new value; it must copy the old value to a temporary
783 value, then store the new value, and then DECREF the temporary value.
784 This is because it is possible that during the DECREF the frame is
785 accessed by other code (e.g. a __del__ method or gc.collect()) and the
786 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000788 GETLOCAL(i) = value; \
789 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000790
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000791
792#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 while (STACK_LEVEL() > (b)->b_level) { \
794 PyObject *v = POP(); \
795 Py_XDECREF(v); \
796 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000797
798#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300799 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100801 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 assert(STACK_LEVEL() >= (b)->b_level + 3); \
803 while (STACK_LEVEL() > (b)->b_level + 3) { \
804 value = POP(); \
805 Py_XDECREF(value); \
806 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100807 exc_info = tstate->exc_info; \
808 type = exc_info->exc_type; \
809 value = exc_info->exc_value; \
810 traceback = exc_info->exc_traceback; \
811 exc_info->exc_type = POP(); \
812 exc_info->exc_value = POP(); \
813 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_XDECREF(type); \
815 Py_XDECREF(value); \
816 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300817 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000818
Guido van Rossuma027efa1997-05-05 20:56:21 +0000819/* Start of code */
820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* push frame */
822 if (Py_EnterRecursiveCall(""))
823 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (tstate->use_tracing) {
828 if (tstate->c_tracefunc != NULL) {
829 /* tstate->c_tracefunc, if defined, is a
830 function that will be called on *every* entry
831 to a code block. Its return value, if not
832 None, is a function that will be called at
833 the start of each executed line of code.
834 (Actually, the function must return itself
835 in order to continue tracing.) The trace
836 functions are called with three arguments:
837 a pointer to the current frame, a string
838 indicating why the function is called, and
839 an argument which depends on the situation.
840 The global trace function is also called
841 whenever an exception is detected. */
842 if (call_trace_protected(tstate->c_tracefunc,
843 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100844 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 /* Trace function raised an error */
846 goto exit_eval_frame;
847 }
848 }
849 if (tstate->c_profilefunc != NULL) {
850 /* Similar for c_profilefunc, except it needn't
851 return itself and isn't called for "line" events */
852 if (call_trace_protected(tstate->c_profilefunc,
853 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100854 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* Profile function raised an error */
856 goto exit_eval_frame;
857 }
858 }
859 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000860
Łukasz Langaa785c872016-09-09 17:37:37 -0700861 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
862 dtrace_function_entry(f);
863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 co = f->f_code;
865 names = co->co_names;
866 consts = co->co_consts;
867 fastlocals = f->f_localsplus;
868 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300869 assert(PyBytes_Check(co->co_code));
870 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300871 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
872 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
873 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300874 /*
875 f->f_lasti refers to the index of the last instruction,
876 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000877
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300878 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500879 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 When the PREDICT() macros are enabled, some opcode pairs follow in
882 direct succession without updating f->f_lasti. A successful
883 prediction effectively links the two codes together as if they
884 were a single new opcode; accordingly,f->f_lasti will point to
885 the first code in the pair (for instance, GET_ITER followed by
886 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300887 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300889 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300890 next_instr = first_instr;
891 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300892 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
893 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 stack_pointer = f->f_stacktop;
896 assert(stack_pointer != NULL);
897 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200898 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000899
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000900
Tim Peters5ca576e2001-06-18 22:08:13 +0000901#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200902 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000903#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000904
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400905 if (throwflag) /* support for generator.throw() */
906 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907
Victor Stinnerace47d72013-07-18 01:41:08 +0200908#ifdef Py_DEBUG
909 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100910 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000911 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200912 assert(!PyErr_Occurred());
913#endif
914
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200915main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 assert(stack_pointer >= f->f_valuestack); /* else underflow */
918 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200919 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* Do periodic things. Doing this every time through
922 the loop would add too much overhead, so we do it
923 only every Nth instruction. We also do it if
924 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
925 event needs attention (e.g. a signal handler or
926 async I/O handler); see Py_AddPendingCall() and
927 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000928
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600929 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700930 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
931 _Py_OPCODE(*next_instr) == YIELD_FROM) {
932 /* Two cases where we skip running signal handlers and other
933 pending calls:
934 - If we're about to enter the try: of a try/finally (not
935 *very* useful, but might help in some cases and it's
936 traditional)
937 - If we're resuming a chain of nested 'yield from' or
938 'await' calls, then each frame is parked with YIELD_FROM
939 as its next opcode. If the user hit control-C we want to
940 wait until we've reached the innermost frame before
941 running the signal handler and raising KeyboardInterrupt
942 (see bpo-30039).
943 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 goto fast_next_opcode;
945 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600946 if (_Py_atomic_load_relaxed(
947 &_PyRuntime.ceval.pending.calls_to_do))
948 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400949 if (Py_MakePendingCalls() < 0)
950 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600952 if (_Py_atomic_load_relaxed(
953 &_PyRuntime.ceval.gil_drop_request))
954 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Give another thread a chance */
956 if (PyThreadState_Swap(NULL) != tstate)
957 Py_FatalError("ceval: tstate mix-up");
958 drop_gil(tstate);
959
960 /* Other threads may run now */
961
962 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700963
964 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600965 if (_Py_IsFinalizing() &&
966 !_Py_CURRENTLY_FINALIZING(tstate))
967 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700968 drop_gil(tstate);
969 PyThread_exit_thread();
970 }
971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (PyThreadState_Swap(tstate) != NULL)
973 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 }
975 /* Check for asynchronous exceptions. */
976 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400977 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 tstate->async_exc = NULL;
979 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400980 PyErr_SetNone(exc);
981 Py_DECREF(exc);
982 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 }
984 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 fast_next_opcode:
987 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000988
Łukasz Langaa785c872016-09-09 17:37:37 -0700989 if (PyDTrace_LINE_ENABLED())
990 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -0500995 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400996 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 /* see maybe_call_line_trace
998 for expository comments */
999 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 err = maybe_call_line_trace(tstate->c_tracefunc,
1002 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001003 tstate, f,
1004 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* Reload possibly changed frame fields */
1006 JUMPTO(f->f_lasti);
1007 if (f->f_stacktop != NULL) {
1008 stack_pointer = f->f_stacktop;
1009 f->f_stacktop = NULL;
1010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001011 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001013 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001017
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001018 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001019 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001020#ifdef DYNAMIC_EXECUTION_PROFILE
1021#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 dxpairs[lastopcode][opcode]++;
1023 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001024#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001026#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001027
Guido van Rossum96a42c81992-01-12 02:29:51 +00001028#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (lltrace) {
1032 if (HAS_ARG(opcode)) {
1033 printf("%d: %d, %d\n",
1034 f->f_lasti, opcode, oparg);
1035 }
1036 else {
1037 printf("%d: %d\n",
1038 f->f_lasti, opcode);
1039 }
1040 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001041#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001046 It is essential that any operation that fails must goto error
1047 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 TARGET(NOP)
1050 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001051
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001052 TARGET(LOAD_FAST) {
1053 PyObject *value = GETLOCAL(oparg);
1054 if (value == NULL) {
1055 format_exc_check_arg(PyExc_UnboundLocalError,
1056 UNBOUNDLOCAL_ERROR_MSG,
1057 PyTuple_GetItem(co->co_varnames, oparg));
1058 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 Py_INCREF(value);
1061 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001063 }
1064
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001065 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001066 TARGET(LOAD_CONST) {
1067 PyObject *value = GETITEM(consts, oparg);
1068 Py_INCREF(value);
1069 PUSH(value);
1070 FAST_DISPATCH();
1071 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001072
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001073 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 TARGET(STORE_FAST) {
1075 PyObject *value = POP();
1076 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001078 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001080 TARGET(POP_TOP) {
1081 PyObject *value = POP();
1082 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001084 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001086 TARGET(ROT_TWO) {
1087 PyObject *top = TOP();
1088 PyObject *second = SECOND();
1089 SET_TOP(second);
1090 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001093
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001094 TARGET(ROT_THREE) {
1095 PyObject *top = TOP();
1096 PyObject *second = SECOND();
1097 PyObject *third = THIRD();
1098 SET_TOP(second);
1099 SET_SECOND(third);
1100 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001104 TARGET(ROT_FOUR) {
1105 PyObject *top = TOP();
1106 PyObject *second = SECOND();
1107 PyObject *third = THIRD();
1108 PyObject *fourth = FOURTH();
1109 SET_TOP(second);
1110 SET_SECOND(third);
1111 SET_THIRD(fourth);
1112 SET_FOURTH(top);
1113 FAST_DISPATCH();
1114 }
1115
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001116 TARGET(DUP_TOP) {
1117 PyObject *top = TOP();
1118 Py_INCREF(top);
1119 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001121 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001123 TARGET(DUP_TOP_TWO) {
1124 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001125 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001126 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001128 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 SET_TOP(top);
1130 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001131 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001133
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 TARGET(UNARY_POSITIVE) {
1135 PyObject *value = TOP();
1136 PyObject *res = PyNumber_Positive(value);
1137 Py_DECREF(value);
1138 SET_TOP(res);
1139 if (res == NULL)
1140 goto error;
1141 DISPATCH();
1142 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001143
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 TARGET(UNARY_NEGATIVE) {
1145 PyObject *value = TOP();
1146 PyObject *res = PyNumber_Negative(value);
1147 Py_DECREF(value);
1148 SET_TOP(res);
1149 if (res == NULL)
1150 goto error;
1151 DISPATCH();
1152 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001154 TARGET(UNARY_NOT) {
1155 PyObject *value = TOP();
1156 int err = PyObject_IsTrue(value);
1157 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (err == 0) {
1159 Py_INCREF(Py_True);
1160 SET_TOP(Py_True);
1161 DISPATCH();
1162 }
1163 else if (err > 0) {
1164 Py_INCREF(Py_False);
1165 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 DISPATCH();
1167 }
1168 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 goto error;
1170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 TARGET(UNARY_INVERT) {
1173 PyObject *value = TOP();
1174 PyObject *res = PyNumber_Invert(value);
1175 Py_DECREF(value);
1176 SET_TOP(res);
1177 if (res == NULL)
1178 goto error;
1179 DISPATCH();
1180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 TARGET(BINARY_POWER) {
1183 PyObject *exp = POP();
1184 PyObject *base = TOP();
1185 PyObject *res = PyNumber_Power(base, exp, Py_None);
1186 Py_DECREF(base);
1187 Py_DECREF(exp);
1188 SET_TOP(res);
1189 if (res == NULL)
1190 goto error;
1191 DISPATCH();
1192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001194 TARGET(BINARY_MULTIPLY) {
1195 PyObject *right = POP();
1196 PyObject *left = TOP();
1197 PyObject *res = PyNumber_Multiply(left, right);
1198 Py_DECREF(left);
1199 Py_DECREF(right);
1200 SET_TOP(res);
1201 if (res == NULL)
1202 goto error;
1203 DISPATCH();
1204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Benjamin Petersond51374e2014-04-09 23:55:56 -04001206 TARGET(BINARY_MATRIX_MULTIPLY) {
1207 PyObject *right = POP();
1208 PyObject *left = TOP();
1209 PyObject *res = PyNumber_MatrixMultiply(left, right);
1210 Py_DECREF(left);
1211 Py_DECREF(right);
1212 SET_TOP(res);
1213 if (res == NULL)
1214 goto error;
1215 DISPATCH();
1216 }
1217
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 TARGET(BINARY_TRUE_DIVIDE) {
1219 PyObject *divisor = POP();
1220 PyObject *dividend = TOP();
1221 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1222 Py_DECREF(dividend);
1223 Py_DECREF(divisor);
1224 SET_TOP(quotient);
1225 if (quotient == NULL)
1226 goto error;
1227 DISPATCH();
1228 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 TARGET(BINARY_FLOOR_DIVIDE) {
1231 PyObject *divisor = POP();
1232 PyObject *dividend = TOP();
1233 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1234 Py_DECREF(dividend);
1235 Py_DECREF(divisor);
1236 SET_TOP(quotient);
1237 if (quotient == NULL)
1238 goto error;
1239 DISPATCH();
1240 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001241
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 TARGET(BINARY_MODULO) {
1243 PyObject *divisor = POP();
1244 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001245 PyObject *res;
1246 if (PyUnicode_CheckExact(dividend) && (
1247 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1248 // fast path; string formatting, but not if the RHS is a str subclass
1249 // (see issue28598)
1250 res = PyUnicode_Format(dividend, divisor);
1251 } else {
1252 res = PyNumber_Remainder(dividend, divisor);
1253 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 Py_DECREF(divisor);
1255 Py_DECREF(dividend);
1256 SET_TOP(res);
1257 if (res == NULL)
1258 goto error;
1259 DISPATCH();
1260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 TARGET(BINARY_ADD) {
1263 PyObject *right = POP();
1264 PyObject *left = TOP();
1265 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001266 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1267 CPython using bytecode, it is simply worthless.
1268 See http://bugs.python.org/issue21955 and
1269 http://bugs.python.org/issue10044 for the discussion. In short,
1270 no patch shown any impact on a realistic benchmark, only a minor
1271 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 if (PyUnicode_CheckExact(left) &&
1273 PyUnicode_CheckExact(right)) {
1274 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001275 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001276 }
1277 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 sum = PyNumber_Add(left, right);
1279 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 Py_DECREF(right);
1282 SET_TOP(sum);
1283 if (sum == NULL)
1284 goto error;
1285 DISPATCH();
1286 }
1287
1288 TARGET(BINARY_SUBTRACT) {
1289 PyObject *right = POP();
1290 PyObject *left = TOP();
1291 PyObject *diff = PyNumber_Subtract(left, right);
1292 Py_DECREF(right);
1293 Py_DECREF(left);
1294 SET_TOP(diff);
1295 if (diff == NULL)
1296 goto error;
1297 DISPATCH();
1298 }
1299
1300 TARGET(BINARY_SUBSCR) {
1301 PyObject *sub = POP();
1302 PyObject *container = TOP();
1303 PyObject *res = PyObject_GetItem(container, sub);
1304 Py_DECREF(container);
1305 Py_DECREF(sub);
1306 SET_TOP(res);
1307 if (res == NULL)
1308 goto error;
1309 DISPATCH();
1310 }
1311
1312 TARGET(BINARY_LSHIFT) {
1313 PyObject *right = POP();
1314 PyObject *left = TOP();
1315 PyObject *res = PyNumber_Lshift(left, right);
1316 Py_DECREF(left);
1317 Py_DECREF(right);
1318 SET_TOP(res);
1319 if (res == NULL)
1320 goto error;
1321 DISPATCH();
1322 }
1323
1324 TARGET(BINARY_RSHIFT) {
1325 PyObject *right = POP();
1326 PyObject *left = TOP();
1327 PyObject *res = PyNumber_Rshift(left, right);
1328 Py_DECREF(left);
1329 Py_DECREF(right);
1330 SET_TOP(res);
1331 if (res == NULL)
1332 goto error;
1333 DISPATCH();
1334 }
1335
1336 TARGET(BINARY_AND) {
1337 PyObject *right = POP();
1338 PyObject *left = TOP();
1339 PyObject *res = PyNumber_And(left, right);
1340 Py_DECREF(left);
1341 Py_DECREF(right);
1342 SET_TOP(res);
1343 if (res == NULL)
1344 goto error;
1345 DISPATCH();
1346 }
1347
1348 TARGET(BINARY_XOR) {
1349 PyObject *right = POP();
1350 PyObject *left = TOP();
1351 PyObject *res = PyNumber_Xor(left, right);
1352 Py_DECREF(left);
1353 Py_DECREF(right);
1354 SET_TOP(res);
1355 if (res == NULL)
1356 goto error;
1357 DISPATCH();
1358 }
1359
1360 TARGET(BINARY_OR) {
1361 PyObject *right = POP();
1362 PyObject *left = TOP();
1363 PyObject *res = PyNumber_Or(left, right);
1364 Py_DECREF(left);
1365 Py_DECREF(right);
1366 SET_TOP(res);
1367 if (res == NULL)
1368 goto error;
1369 DISPATCH();
1370 }
1371
1372 TARGET(LIST_APPEND) {
1373 PyObject *v = POP();
1374 PyObject *list = PEEK(oparg);
1375 int err;
1376 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 if (err != 0)
1379 goto error;
1380 PREDICT(JUMP_ABSOLUTE);
1381 DISPATCH();
1382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 TARGET(SET_ADD) {
1385 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001386 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 int err;
1388 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 if (err != 0)
1391 goto error;
1392 PREDICT(JUMP_ABSOLUTE);
1393 DISPATCH();
1394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(INPLACE_POWER) {
1397 PyObject *exp = POP();
1398 PyObject *base = TOP();
1399 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1400 Py_DECREF(base);
1401 Py_DECREF(exp);
1402 SET_TOP(res);
1403 if (res == NULL)
1404 goto error;
1405 DISPATCH();
1406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 TARGET(INPLACE_MULTIPLY) {
1409 PyObject *right = POP();
1410 PyObject *left = TOP();
1411 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1412 Py_DECREF(left);
1413 Py_DECREF(right);
1414 SET_TOP(res);
1415 if (res == NULL)
1416 goto error;
1417 DISPATCH();
1418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Benjamin Petersond51374e2014-04-09 23:55:56 -04001420 TARGET(INPLACE_MATRIX_MULTIPLY) {
1421 PyObject *right = POP();
1422 PyObject *left = TOP();
1423 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1424 Py_DECREF(left);
1425 Py_DECREF(right);
1426 SET_TOP(res);
1427 if (res == NULL)
1428 goto error;
1429 DISPATCH();
1430 }
1431
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 TARGET(INPLACE_TRUE_DIVIDE) {
1433 PyObject *divisor = POP();
1434 PyObject *dividend = TOP();
1435 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1436 Py_DECREF(dividend);
1437 Py_DECREF(divisor);
1438 SET_TOP(quotient);
1439 if (quotient == NULL)
1440 goto error;
1441 DISPATCH();
1442 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 TARGET(INPLACE_FLOOR_DIVIDE) {
1445 PyObject *divisor = POP();
1446 PyObject *dividend = TOP();
1447 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1448 Py_DECREF(dividend);
1449 Py_DECREF(divisor);
1450 SET_TOP(quotient);
1451 if (quotient == NULL)
1452 goto error;
1453 DISPATCH();
1454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 TARGET(INPLACE_MODULO) {
1457 PyObject *right = POP();
1458 PyObject *left = TOP();
1459 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1460 Py_DECREF(left);
1461 Py_DECREF(right);
1462 SET_TOP(mod);
1463 if (mod == NULL)
1464 goto error;
1465 DISPATCH();
1466 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 TARGET(INPLACE_ADD) {
1469 PyObject *right = POP();
1470 PyObject *left = TOP();
1471 PyObject *sum;
1472 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1473 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001474 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001475 }
1476 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 sum = PyNumber_InPlaceAdd(left, right);
1478 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 Py_DECREF(right);
1481 SET_TOP(sum);
1482 if (sum == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 TARGET(INPLACE_SUBTRACT) {
1488 PyObject *right = POP();
1489 PyObject *left = TOP();
1490 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1491 Py_DECREF(left);
1492 Py_DECREF(right);
1493 SET_TOP(diff);
1494 if (diff == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 TARGET(INPLACE_LSHIFT) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_InPlaceLshift(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 TARGET(INPLACE_RSHIFT) {
1512 PyObject *right = POP();
1513 PyObject *left = TOP();
1514 PyObject *res = PyNumber_InPlaceRshift(left, right);
1515 Py_DECREF(left);
1516 Py_DECREF(right);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(INPLACE_AND) {
1524 PyObject *right = POP();
1525 PyObject *left = TOP();
1526 PyObject *res = PyNumber_InPlaceAnd(left, right);
1527 Py_DECREF(left);
1528 Py_DECREF(right);
1529 SET_TOP(res);
1530 if (res == NULL)
1531 goto error;
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(INPLACE_XOR) {
1536 PyObject *right = POP();
1537 PyObject *left = TOP();
1538 PyObject *res = PyNumber_InPlaceXor(left, right);
1539 Py_DECREF(left);
1540 Py_DECREF(right);
1541 SET_TOP(res);
1542 if (res == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(INPLACE_OR) {
1548 PyObject *right = POP();
1549 PyObject *left = TOP();
1550 PyObject *res = PyNumber_InPlaceOr(left, right);
1551 Py_DECREF(left);
1552 Py_DECREF(right);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 TARGET(STORE_SUBSCR) {
1560 PyObject *sub = TOP();
1561 PyObject *container = SECOND();
1562 PyObject *v = THIRD();
1563 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001565 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 Py_DECREF(container);
1569 Py_DECREF(sub);
1570 if (err != 0)
1571 goto error;
1572 DISPATCH();
1573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 TARGET(DELETE_SUBSCR) {
1576 PyObject *sub = TOP();
1577 PyObject *container = SECOND();
1578 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001580 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 err = PyObject_DelItem(container, sub);
1582 Py_DECREF(container);
1583 Py_DECREF(sub);
1584 if (err != 0)
1585 goto error;
1586 DISPATCH();
1587 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001588
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001590 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001592 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001593 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 PyErr_SetString(PyExc_RuntimeError,
1596 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 Py_DECREF(value);
1598 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001600 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001601 Py_DECREF(value);
1602 if (res == NULL)
1603 goto error;
1604 Py_DECREF(res);
1605 DISPATCH();
1606 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001607
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001608 TARGET(RAISE_VARARGS) {
1609 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 switch (oparg) {
1611 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001612 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001613 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001616 /* fall through */
1617 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001619 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 break;
1622 default:
1623 PyErr_SetString(PyExc_SystemError,
1624 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 break;
1626 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 goto error;
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001632 assert(f->f_iblock == 0);
1633 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001635
Yury Selivanov75445082015-05-11 22:57:16 -04001636 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001637 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001638 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001639 PyObject *obj = TOP();
1640 PyTypeObject *type = Py_TYPE(obj);
1641
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001642 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001643 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001644 }
Yury Selivanov75445082015-05-11 22:57:16 -04001645
1646 if (getter != NULL) {
1647 iter = (*getter)(obj);
1648 Py_DECREF(obj);
1649 if (iter == NULL) {
1650 SET_TOP(NULL);
1651 goto error;
1652 }
1653 }
1654 else {
1655 SET_TOP(NULL);
1656 PyErr_Format(
1657 PyExc_TypeError,
1658 "'async for' requires an object with "
1659 "__aiter__ method, got %.100s",
1660 type->tp_name);
1661 Py_DECREF(obj);
1662 goto error;
1663 }
1664
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001665 if (Py_TYPE(iter)->tp_as_async == NULL ||
1666 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001667
Yury Selivanov398ff912017-03-02 22:20:00 -05001668 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001669 PyErr_Format(
1670 PyExc_TypeError,
1671 "'async for' received an object from __aiter__ "
1672 "that does not implement __anext__: %.100s",
1673 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001674 Py_DECREF(iter);
1675 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001676 }
1677
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001678 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001679 DISPATCH();
1680 }
1681
1682 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001683 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001684 PyObject *next_iter = NULL;
1685 PyObject *awaitable = NULL;
1686 PyObject *aiter = TOP();
1687 PyTypeObject *type = Py_TYPE(aiter);
1688
Yury Selivanoveb636452016-09-08 22:01:51 -07001689 if (PyAsyncGen_CheckExact(aiter)) {
1690 awaitable = type->tp_as_async->am_anext(aiter);
1691 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001692 goto error;
1693 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001694 } else {
1695 if (type->tp_as_async != NULL){
1696 getter = type->tp_as_async->am_anext;
1697 }
Yury Selivanov75445082015-05-11 22:57:16 -04001698
Yury Selivanoveb636452016-09-08 22:01:51 -07001699 if (getter != NULL) {
1700 next_iter = (*getter)(aiter);
1701 if (next_iter == NULL) {
1702 goto error;
1703 }
1704 }
1705 else {
1706 PyErr_Format(
1707 PyExc_TypeError,
1708 "'async for' requires an iterator with "
1709 "__anext__ method, got %.100s",
1710 type->tp_name);
1711 goto error;
1712 }
Yury Selivanov75445082015-05-11 22:57:16 -04001713
Yury Selivanoveb636452016-09-08 22:01:51 -07001714 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1715 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001716 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001717 PyExc_TypeError,
1718 "'async for' received an invalid object "
1719 "from __anext__: %.100s",
1720 Py_TYPE(next_iter)->tp_name);
1721
1722 Py_DECREF(next_iter);
1723 goto error;
1724 } else {
1725 Py_DECREF(next_iter);
1726 }
1727 }
Yury Selivanov75445082015-05-11 22:57:16 -04001728
1729 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001730 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001731 DISPATCH();
1732 }
1733
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001734 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001735 TARGET(GET_AWAITABLE) {
1736 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001737 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001738
1739 Py_DECREF(iterable);
1740
Yury Selivanovc724bae2016-03-02 11:30:46 -05001741 if (iter != NULL && PyCoro_CheckExact(iter)) {
1742 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1743 if (yf != NULL) {
1744 /* `iter` is a coroutine object that is being
1745 awaited, `yf` is a pointer to the current awaitable
1746 being awaited on. */
1747 Py_DECREF(yf);
1748 Py_CLEAR(iter);
1749 PyErr_SetString(
1750 PyExc_RuntimeError,
1751 "coroutine is being awaited already");
1752 /* The code below jumps to `error` if `iter` is NULL. */
1753 }
1754 }
1755
Yury Selivanov75445082015-05-11 22:57:16 -04001756 SET_TOP(iter); /* Even if it's NULL */
1757
1758 if (iter == NULL) {
1759 goto error;
1760 }
1761
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001762 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001763 DISPATCH();
1764 }
1765
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 TARGET(YIELD_FROM) {
1767 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001768 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001770 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1771 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001772 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001773 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001775 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001776 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001777 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001778 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 Py_DECREF(v);
1780 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001781 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001782 if (tstate->c_tracefunc != NULL
1783 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001784 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001785 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 if (err < 0)
1787 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001788 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 SET_TOP(val);
1790 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001791 }
Martin Panter95f53c12016-07-18 08:23:26 +00001792 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001793 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001794 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001795 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001796 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001797 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001799
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001802
1803 if (co->co_flags & CO_ASYNC_GENERATOR) {
1804 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1805 Py_DECREF(retval);
1806 if (w == NULL) {
1807 retval = NULL;
1808 goto error;
1809 }
1810 retval = w;
1811 }
1812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001814 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 TARGET(POP_EXCEPT) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001818 PyObject *type, *value, *traceback;
1819 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 PyTryBlock *b = PyFrame_BlockPop(f);
1821 if (b->b_type != EXCEPT_HANDLER) {
1822 PyErr_SetString(PyExc_SystemError,
1823 "popped block is not an except handler");
1824 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001826 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1827 STACK_LEVEL() <= (b)->b_level + 4);
1828 exc_info = tstate->exc_info;
1829 type = exc_info->exc_type;
1830 value = exc_info->exc_value;
1831 traceback = exc_info->exc_traceback;
1832 exc_info->exc_type = POP();
1833 exc_info->exc_value = POP();
1834 exc_info->exc_traceback = POP();
1835 Py_XDECREF(type);
1836 Py_XDECREF(value);
1837 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001840
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001841 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 TARGET(POP_BLOCK) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001843 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001846
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001847 TARGET(POP_FINALLY) {
1848 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1849 Either:
1850 - TOP = NULL or an integer
1851 or:
1852 - (TOP, SECOND, THIRD) = exc_info()
1853 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1854
1855 If oparg is 1 the value for 'return' was additionally pushed
1856 at the top of the stack.
1857 */
1858 PyObject *res = NULL;
1859 if (oparg) {
1860 res = POP();
1861 }
1862 PyObject *exc = POP();
1863 if (exc == NULL || PyLong_CheckExact(exc)) {
1864 Py_XDECREF(exc);
1865 }
1866 else {
1867 Py_DECREF(exc);
1868 Py_DECREF(POP());
1869 Py_DECREF(POP());
1870
1871 PyObject *type, *value, *traceback;
1872 _PyErr_StackItem *exc_info;
1873 PyTryBlock *b = PyFrame_BlockPop(f);
1874 if (b->b_type != EXCEPT_HANDLER) {
1875 PyErr_SetString(PyExc_SystemError,
1876 "popped block is not an except handler");
1877 Py_XDECREF(res);
1878 goto error;
1879 }
1880 assert(STACK_LEVEL() == (b)->b_level + 3);
1881 exc_info = tstate->exc_info;
1882 type = exc_info->exc_type;
1883 value = exc_info->exc_value;
1884 traceback = exc_info->exc_traceback;
1885 exc_info->exc_type = POP();
1886 exc_info->exc_value = POP();
1887 exc_info->exc_traceback = POP();
1888 Py_XDECREF(type);
1889 Py_XDECREF(value);
1890 Py_XDECREF(traceback);
1891 }
1892 if (oparg) {
1893 PUSH(res);
1894 }
1895 DISPATCH();
1896 }
1897
1898 TARGET(CALL_FINALLY) {
1899 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1900 if (ret == NULL) {
1901 goto error;
1902 }
1903 PUSH(ret);
1904 JUMPBY(oparg);
1905 FAST_DISPATCH();
1906 }
1907
1908 TARGET(BEGIN_FINALLY) {
1909 /* Push NULL onto the stack for using it in END_FINALLY,
1910 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1911 */
1912 PUSH(NULL);
1913 FAST_DISPATCH();
1914 }
1915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 TARGET(END_FINALLY) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001918 /* At the top of the stack are 1 or 6 values:
1919 Either:
1920 - TOP = NULL or an integer
1921 or:
1922 - (TOP, SECOND, THIRD) = exc_info()
1923 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1924 */
1925 PyObject *exc = POP();
1926 if (exc == NULL) {
1927 FAST_DISPATCH();
1928 }
1929 else if (PyLong_CheckExact(exc)) {
1930 int ret = _PyLong_AsInt(exc);
1931 Py_DECREF(exc);
1932 if (ret == -1 && PyErr_Occurred()) {
1933 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001935 JUMPTO(ret);
1936 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001938 else {
1939 assert(PyExceptionClass_Check(exc));
1940 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001942 PyErr_Restore(exc, val, tb);
1943 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001947 TARGET(END_ASYNC_FOR) {
1948 PyObject *exc = POP();
1949 assert(PyExceptionClass_Check(exc));
1950 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1951 PyTryBlock *b = PyFrame_BlockPop(f);
1952 assert(b->b_type == EXCEPT_HANDLER);
1953 Py_DECREF(exc);
1954 UNWIND_EXCEPT_HANDLER(b);
1955 Py_DECREF(POP());
1956 JUMPBY(oparg);
1957 FAST_DISPATCH();
1958 }
1959 else {
1960 PyObject *val = POP();
1961 PyObject *tb = POP();
1962 PyErr_Restore(exc, val, tb);
1963 goto exception_unwind;
1964 }
1965 }
1966
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001968 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001969
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001971 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1973 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001974 PyErr_SetString(PyExc_NameError,
1975 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001979 }
1980 else {
1981 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1982 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001983 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1985 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001986 if (PyErr_ExceptionMatches(PyExc_KeyError))
1987 PyErr_SetString(PyExc_NameError,
1988 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001990 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001992 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001993 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001994 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001995
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 TARGET(STORE_NAME) {
1997 PyObject *name = GETITEM(names, oparg);
1998 PyObject *v = POP();
1999 PyObject *ns = f->f_locals;
2000 int err;
2001 if (ns == NULL) {
2002 PyErr_Format(PyExc_SystemError,
2003 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 if (PyDict_CheckExact(ns))
2008 err = PyDict_SetItem(ns, name, v);
2009 else
2010 err = PyObject_SetItem(ns, name, v);
2011 Py_DECREF(v);
2012 if (err != 0)
2013 goto error;
2014 DISPATCH();
2015 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002016
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 TARGET(DELETE_NAME) {
2018 PyObject *name = GETITEM(names, oparg);
2019 PyObject *ns = f->f_locals;
2020 int err;
2021 if (ns == NULL) {
2022 PyErr_Format(PyExc_SystemError,
2023 "no locals when deleting %R", name);
2024 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 err = PyObject_DelItem(ns, name);
2027 if (err != 0) {
2028 format_exc_check_arg(PyExc_NameError,
2029 NAME_ERROR_MSG,
2030 name);
2031 goto error;
2032 }
2033 DISPATCH();
2034 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002035
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002036 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 TARGET(UNPACK_SEQUENCE) {
2038 PyObject *seq = POP(), *item, **items;
2039 if (PyTuple_CheckExact(seq) &&
2040 PyTuple_GET_SIZE(seq) == oparg) {
2041 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 item = items[oparg];
2044 Py_INCREF(item);
2045 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 } else if (PyList_CheckExact(seq) &&
2048 PyList_GET_SIZE(seq) == oparg) {
2049 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 item = items[oparg];
2052 Py_INCREF(item);
2053 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 stack_pointer + oparg)) {
2057 STACKADJ(oparg);
2058 } else {
2059 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 Py_DECREF(seq);
2061 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002064 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002066
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 TARGET(UNPACK_EX) {
2068 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2069 PyObject *seq = POP();
2070
2071 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2072 stack_pointer + totalargs)) {
2073 stack_pointer += totalargs;
2074 } else {
2075 Py_DECREF(seq);
2076 goto error;
2077 }
2078 Py_DECREF(seq);
2079 DISPATCH();
2080 }
2081
2082 TARGET(STORE_ATTR) {
2083 PyObject *name = GETITEM(names, oparg);
2084 PyObject *owner = TOP();
2085 PyObject *v = SECOND();
2086 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 Py_DECREF(owner);
2091 if (err != 0)
2092 goto error;
2093 DISPATCH();
2094 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 TARGET(DELETE_ATTR) {
2097 PyObject *name = GETITEM(names, oparg);
2098 PyObject *owner = POP();
2099 int err;
2100 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2101 Py_DECREF(owner);
2102 if (err != 0)
2103 goto error;
2104 DISPATCH();
2105 }
2106
2107 TARGET(STORE_GLOBAL) {
2108 PyObject *name = GETITEM(names, oparg);
2109 PyObject *v = POP();
2110 int err;
2111 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 if (err != 0)
2114 goto error;
2115 DISPATCH();
2116 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002117
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 TARGET(DELETE_GLOBAL) {
2119 PyObject *name = GETITEM(names, oparg);
2120 int err;
2121 err = PyDict_DelItem(f->f_globals, name);
2122 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002124 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002126 }
2127 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002129
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 TARGET(LOAD_NAME) {
2131 PyObject *name = GETITEM(names, oparg);
2132 PyObject *locals = f->f_locals;
2133 PyObject *v;
2134 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 "no locals when loading %R", name);
2137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 if (PyDict_CheckExact(locals)) {
2140 v = PyDict_GetItem(locals, name);
2141 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 }
2143 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002145 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002146 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2147 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 PyErr_Clear();
2149 }
2150 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 if (v == NULL) {
2152 v = PyDict_GetItem(f->f_globals, name);
2153 Py_XINCREF(v);
2154 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002155 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 v = PyDict_GetItem(f->f_builtins, name);
2157 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002158 format_exc_check_arg(
2159 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 NAME_ERROR_MSG, name);
2161 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002164 }
2165 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 v = PyObject_GetItem(f->f_builtins, name);
2167 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002168 if (PyErr_ExceptionMatches(PyExc_KeyError))
2169 format_exc_check_arg(
2170 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 NAME_ERROR_MSG, name);
2172 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002173 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002174 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002180
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 TARGET(LOAD_GLOBAL) {
2182 PyObject *name = GETITEM(names, oparg);
2183 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002185 && PyDict_CheckExact(f->f_builtins))
2186 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002188 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 name);
2190 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002191 if (!_PyErr_OCCURRED()) {
2192 /* _PyDict_LoadGlobal() returns NULL without raising
2193 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002195 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002196 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002201 else {
2202 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002203
2204 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 v = PyObject_GetItem(f->f_globals, name);
2206 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002207 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2208 goto error;
2209 PyErr_Clear();
2210
Victor Stinnerb4efc962015-11-20 09:24:02 +01002211 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 v = PyObject_GetItem(f->f_builtins, name);
2213 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 if (PyErr_ExceptionMatches(PyExc_KeyError))
2215 format_exc_check_arg(
2216 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002217 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002219 }
2220 }
2221 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002225
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 TARGET(DELETE_FAST) {
2227 PyObject *v = GETLOCAL(oparg);
2228 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 SETLOCAL(oparg, NULL);
2230 DISPATCH();
2231 }
2232 format_exc_check_arg(
2233 PyExc_UnboundLocalError,
2234 UNBOUNDLOCAL_ERROR_MSG,
2235 PyTuple_GetItem(co->co_varnames, oparg)
2236 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 goto error;
2238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 TARGET(DELETE_DEREF) {
2241 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002242 PyObject *oldobj = PyCell_GET(cell);
2243 if (oldobj != NULL) {
2244 PyCell_SET(cell, NULL);
2245 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002246 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002247 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002248 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 goto error;
2250 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002251
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 TARGET(LOAD_CLOSURE) {
2253 PyObject *cell = freevars[oparg];
2254 Py_INCREF(cell);
2255 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002258
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002259 TARGET(LOAD_CLASSDEREF) {
2260 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002261 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002262 assert(locals);
2263 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2264 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2265 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2266 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2267 if (PyDict_CheckExact(locals)) {
2268 value = PyDict_GetItem(locals, name);
2269 Py_XINCREF(value);
2270 }
2271 else {
2272 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002273 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002274 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2275 goto error;
2276 PyErr_Clear();
2277 }
2278 }
2279 if (!value) {
2280 PyObject *cell = freevars[oparg];
2281 value = PyCell_GET(cell);
2282 if (value == NULL) {
2283 format_exc_unbound(co, oparg);
2284 goto error;
2285 }
2286 Py_INCREF(value);
2287 }
2288 PUSH(value);
2289 DISPATCH();
2290 }
2291
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 TARGET(LOAD_DEREF) {
2293 PyObject *cell = freevars[oparg];
2294 PyObject *value = PyCell_GET(cell);
2295 if (value == NULL) {
2296 format_exc_unbound(co, oparg);
2297 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 Py_INCREF(value);
2300 PUSH(value);
2301 DISPATCH();
2302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002303
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 TARGET(STORE_DEREF) {
2305 PyObject *v = POP();
2306 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002307 PyObject *oldobj = PyCell_GET(cell);
2308 PyCell_SET(cell, v);
2309 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 DISPATCH();
2311 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002312
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002313 TARGET(BUILD_STRING) {
2314 PyObject *str;
2315 PyObject *empty = PyUnicode_New(0, 0);
2316 if (empty == NULL) {
2317 goto error;
2318 }
2319 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2320 Py_DECREF(empty);
2321 if (str == NULL)
2322 goto error;
2323 while (--oparg >= 0) {
2324 PyObject *item = POP();
2325 Py_DECREF(item);
2326 }
2327 PUSH(str);
2328 DISPATCH();
2329 }
2330
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 TARGET(BUILD_TUPLE) {
2332 PyObject *tup = PyTuple_New(oparg);
2333 if (tup == NULL)
2334 goto error;
2335 while (--oparg >= 0) {
2336 PyObject *item = POP();
2337 PyTuple_SET_ITEM(tup, oparg, item);
2338 }
2339 PUSH(tup);
2340 DISPATCH();
2341 }
2342
2343 TARGET(BUILD_LIST) {
2344 PyObject *list = PyList_New(oparg);
2345 if (list == NULL)
2346 goto error;
2347 while (--oparg >= 0) {
2348 PyObject *item = POP();
2349 PyList_SET_ITEM(list, oparg, item);
2350 }
2351 PUSH(list);
2352 DISPATCH();
2353 }
2354
Serhiy Storchaka73442852016-10-02 10:33:46 +03002355 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002356 TARGET(BUILD_TUPLE_UNPACK)
2357 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002358 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002359 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002360 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002361 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002362
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002363 if (sum == NULL)
2364 goto error;
2365
2366 for (i = oparg; i > 0; i--) {
2367 PyObject *none_val;
2368
2369 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2370 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002371 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002372 PyErr_ExceptionMatches(PyExc_TypeError))
2373 {
2374 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002375 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002376 Py_DECREF(sum);
2377 goto error;
2378 }
2379 Py_DECREF(none_val);
2380 }
2381
2382 if (convert_to_tuple) {
2383 return_value = PyList_AsTuple(sum);
2384 Py_DECREF(sum);
2385 if (return_value == NULL)
2386 goto error;
2387 }
2388 else {
2389 return_value = sum;
2390 }
2391
2392 while (oparg--)
2393 Py_DECREF(POP());
2394 PUSH(return_value);
2395 DISPATCH();
2396 }
2397
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 TARGET(BUILD_SET) {
2399 PyObject *set = PySet_New(NULL);
2400 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002401 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 if (set == NULL)
2403 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002404 for (i = oparg; i > 0; i--) {
2405 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 if (err == 0)
2407 err = PySet_Add(set, item);
2408 Py_DECREF(item);
2409 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002410 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 if (err != 0) {
2412 Py_DECREF(set);
2413 goto error;
2414 }
2415 PUSH(set);
2416 DISPATCH();
2417 }
2418
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002419 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002420 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002421 PyObject *sum = PySet_New(NULL);
2422 if (sum == NULL)
2423 goto error;
2424
2425 for (i = oparg; i > 0; i--) {
2426 if (_PySet_Update(sum, PEEK(i)) < 0) {
2427 Py_DECREF(sum);
2428 goto error;
2429 }
2430 }
2431
2432 while (oparg--)
2433 Py_DECREF(POP());
2434 PUSH(sum);
2435 DISPATCH();
2436 }
2437
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002439 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2441 if (map == NULL)
2442 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002443 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002444 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002445 PyObject *key = PEEK(2*i);
2446 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002447 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002448 if (err != 0) {
2449 Py_DECREF(map);
2450 goto error;
2451 }
2452 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002453
2454 while (oparg--) {
2455 Py_DECREF(POP());
2456 Py_DECREF(POP());
2457 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 PUSH(map);
2459 DISPATCH();
2460 }
2461
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002462 TARGET(SETUP_ANNOTATIONS) {
2463 _Py_IDENTIFIER(__annotations__);
2464 int err;
2465 PyObject *ann_dict;
2466 if (f->f_locals == NULL) {
2467 PyErr_Format(PyExc_SystemError,
2468 "no locals found when setting up annotations");
2469 goto error;
2470 }
2471 /* check if __annotations__ in locals()... */
2472 if (PyDict_CheckExact(f->f_locals)) {
2473 ann_dict = _PyDict_GetItemId(f->f_locals,
2474 &PyId___annotations__);
2475 if (ann_dict == NULL) {
2476 /* ...if not, create a new one */
2477 ann_dict = PyDict_New();
2478 if (ann_dict == NULL) {
2479 goto error;
2480 }
2481 err = _PyDict_SetItemId(f->f_locals,
2482 &PyId___annotations__, ann_dict);
2483 Py_DECREF(ann_dict);
2484 if (err != 0) {
2485 goto error;
2486 }
2487 }
2488 }
2489 else {
2490 /* do the same if locals() is not a dict */
2491 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2492 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002493 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002494 }
2495 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2496 if (ann_dict == NULL) {
2497 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2498 goto error;
2499 }
2500 PyErr_Clear();
2501 ann_dict = PyDict_New();
2502 if (ann_dict == NULL) {
2503 goto error;
2504 }
2505 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2506 Py_DECREF(ann_dict);
2507 if (err != 0) {
2508 goto error;
2509 }
2510 }
2511 else {
2512 Py_DECREF(ann_dict);
2513 }
2514 }
2515 DISPATCH();
2516 }
2517
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002518 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002519 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002520 PyObject *map;
2521 PyObject *keys = TOP();
2522 if (!PyTuple_CheckExact(keys) ||
2523 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2524 PyErr_SetString(PyExc_SystemError,
2525 "bad BUILD_CONST_KEY_MAP keys argument");
2526 goto error;
2527 }
2528 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2529 if (map == NULL) {
2530 goto error;
2531 }
2532 for (i = oparg; i > 0; i--) {
2533 int err;
2534 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2535 PyObject *value = PEEK(i + 1);
2536 err = PyDict_SetItem(map, key, value);
2537 if (err != 0) {
2538 Py_DECREF(map);
2539 goto error;
2540 }
2541 }
2542
2543 Py_DECREF(POP());
2544 while (oparg--) {
2545 Py_DECREF(POP());
2546 }
2547 PUSH(map);
2548 DISPATCH();
2549 }
2550
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002551 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002552 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002553 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002554 if (sum == NULL)
2555 goto error;
2556
2557 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002558 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002559 if (PyDict_Update(sum, arg) < 0) {
2560 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2561 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002562 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002563 arg->ob_type->tp_name);
2564 }
2565 Py_DECREF(sum);
2566 goto error;
2567 }
2568 }
2569
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002570 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002571 Py_DECREF(POP());
2572 PUSH(sum);
2573 DISPATCH();
2574 }
2575
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002576 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2577 Py_ssize_t i;
2578 PyObject *sum = PyDict_New();
2579 if (sum == NULL)
2580 goto error;
2581
2582 for (i = oparg; i > 0; i--) {
2583 PyObject *arg = PEEK(i);
2584 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2585 PyObject *func = PEEK(2 + oparg);
2586 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002587 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002588 }
2589 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2590 PyObject *exc, *val, *tb;
2591 PyErr_Fetch(&exc, &val, &tb);
2592 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2593 PyObject *key = PyTuple_GET_ITEM(val, 0);
2594 if (!PyUnicode_Check(key)) {
2595 PyErr_Format(PyExc_TypeError,
2596 "%.200s%.200s keywords must be strings",
2597 PyEval_GetFuncName(func),
2598 PyEval_GetFuncDesc(func));
2599 } else {
2600 PyErr_Format(PyExc_TypeError,
2601 "%.200s%.200s got multiple "
2602 "values for keyword argument '%U'",
2603 PyEval_GetFuncName(func),
2604 PyEval_GetFuncDesc(func),
2605 key);
2606 }
2607 Py_XDECREF(exc);
2608 Py_XDECREF(val);
2609 Py_XDECREF(tb);
2610 }
2611 else {
2612 PyErr_Restore(exc, val, tb);
2613 }
2614 }
2615 Py_DECREF(sum);
2616 goto error;
2617 }
2618 }
2619
2620 while (oparg--)
2621 Py_DECREF(POP());
2622 PUSH(sum);
2623 DISPATCH();
2624 }
2625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 TARGET(MAP_ADD) {
2627 PyObject *key = TOP();
2628 PyObject *value = SECOND();
2629 PyObject *map;
2630 int err;
2631 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002632 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002634 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 Py_DECREF(value);
2636 Py_DECREF(key);
2637 if (err != 0)
2638 goto error;
2639 PREDICT(JUMP_ABSOLUTE);
2640 DISPATCH();
2641 }
2642
2643 TARGET(LOAD_ATTR) {
2644 PyObject *name = GETITEM(names, oparg);
2645 PyObject *owner = TOP();
2646 PyObject *res = PyObject_GetAttr(owner, name);
2647 Py_DECREF(owner);
2648 SET_TOP(res);
2649 if (res == NULL)
2650 goto error;
2651 DISPATCH();
2652 }
2653
2654 TARGET(COMPARE_OP) {
2655 PyObject *right = POP();
2656 PyObject *left = TOP();
2657 PyObject *res = cmp_outcome(oparg, left, right);
2658 Py_DECREF(left);
2659 Py_DECREF(right);
2660 SET_TOP(res);
2661 if (res == NULL)
2662 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 PREDICT(POP_JUMP_IF_FALSE);
2664 PREDICT(POP_JUMP_IF_TRUE);
2665 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002667
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002670 PyObject *fromlist = POP();
2671 PyObject *level = TOP();
2672 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002673 res = import_name(f, name, fromlist, level);
2674 Py_DECREF(level);
2675 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 SET_TOP(res);
2677 if (res == NULL)
2678 goto error;
2679 DISPATCH();
2680 }
2681
2682 TARGET(IMPORT_STAR) {
2683 PyObject *from = POP(), *locals;
2684 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002685 if (PyFrame_FastToLocalsWithError(f) < 0) {
2686 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002687 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002688 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002689
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 locals = f->f_locals;
2691 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 PyErr_SetString(PyExc_SystemError,
2693 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002694 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002695 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 Py_DECREF(from);
2700 if (err != 0)
2701 goto error;
2702 DISPATCH();
2703 }
Guido van Rossum25831651993-05-19 14:50:45 +00002704
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 TARGET(IMPORT_FROM) {
2706 PyObject *name = GETITEM(names, oparg);
2707 PyObject *from = TOP();
2708 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 PUSH(res);
2711 if (res == NULL)
2712 goto error;
2713 DISPATCH();
2714 }
Thomas Wouters52152252000-08-17 22:55:00 +00002715
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 JUMPBY(oparg);
2718 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002720
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002721 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 TARGET(POP_JUMP_IF_FALSE) {
2723 PyObject *cond = POP();
2724 int err;
2725 if (cond == Py_True) {
2726 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 FAST_DISPATCH();
2728 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 if (cond == Py_False) {
2730 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 JUMPTO(oparg);
2732 FAST_DISPATCH();
2733 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 err = PyObject_IsTrue(cond);
2735 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002737 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 else if (err == 0)
2739 JUMPTO(oparg);
2740 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002744
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002745 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 TARGET(POP_JUMP_IF_TRUE) {
2747 PyObject *cond = POP();
2748 int err;
2749 if (cond == Py_False) {
2750 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 FAST_DISPATCH();
2752 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 if (cond == Py_True) {
2754 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 JUMPTO(oparg);
2756 FAST_DISPATCH();
2757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 err = PyObject_IsTrue(cond);
2759 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 JUMPTO(oparg);
2762 }
2763 else if (err == 0)
2764 ;
2765 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002769
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 TARGET(JUMP_IF_FALSE_OR_POP) {
2771 PyObject *cond = TOP();
2772 int err;
2773 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 FAST_DISPATCH();
2777 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 JUMPTO(oparg);
2780 FAST_DISPATCH();
2781 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 if (err > 0) {
2784 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 }
2787 else if (err == 0)
2788 JUMPTO(oparg);
2789 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002793
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 TARGET(JUMP_IF_TRUE_OR_POP) {
2795 PyObject *cond = TOP();
2796 int err;
2797 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 FAST_DISPATCH();
2801 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 JUMPTO(oparg);
2804 FAST_DISPATCH();
2805 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 JUMPTO(oparg);
2809 }
2810 else if (err == 0) {
2811 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 }
2814 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002818
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002819 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002822#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 /* Enabling this path speeds-up all while and for-loops by bypassing
2824 the per-loop checks for signals. By default, this should be turned-off
2825 because it prevents detection of a control-break in tight loops like
2826 "while 1: pass". Compile with this option turned-on when you need
2827 the speed-up and do not need break checking inside tight loops (ones
2828 that contain only instructions ending with FAST_DISPATCH).
2829 */
2830 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002831#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002833#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002839 PyObject *iter = PyObject_GetIter(iterable);
2840 Py_DECREF(iterable);
2841 SET_TOP(iter);
2842 if (iter == NULL)
2843 goto error;
2844 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002845 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002846 DISPATCH();
2847 }
2848
2849 TARGET(GET_YIELD_FROM_ITER) {
2850 /* before: [obj]; after [getiter(obj)] */
2851 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002852 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002853 if (PyCoro_CheckExact(iterable)) {
2854 /* `iterable` is a coroutine */
2855 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2856 /* and it is used in a 'yield from' expression of a
2857 regular generator. */
2858 Py_DECREF(iterable);
2859 SET_TOP(NULL);
2860 PyErr_SetString(PyExc_TypeError,
2861 "cannot 'yield from' a coroutine object "
2862 "in a non-coroutine generator");
2863 goto error;
2864 }
2865 }
2866 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002867 /* `iterable` is not a generator. */
2868 iter = PyObject_GetIter(iterable);
2869 Py_DECREF(iterable);
2870 SET_TOP(iter);
2871 if (iter == NULL)
2872 goto error;
2873 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002874 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 DISPATCH();
2876 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002877
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002878 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 PyObject *iter = TOP();
2882 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2883 if (next != NULL) {
2884 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 PREDICT(STORE_FAST);
2886 PREDICT(UNPACK_SEQUENCE);
2887 DISPATCH();
2888 }
2889 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2891 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002892 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002893 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 PyErr_Clear();
2895 }
2896 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 STACKADJ(-1);
2898 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002900 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002903
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002904 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 /* NOTE: If you add any new block-setup opcodes that
2906 are not try/except/finally handlers, you may need
2907 to update the PyGen_NeedsFinalizing() function.
2908 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002909
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002910 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 STACK_LEVEL());
2912 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Yury Selivanov75445082015-05-11 22:57:16 -04002915 TARGET(BEFORE_ASYNC_WITH) {
2916 _Py_IDENTIFIER(__aexit__);
2917 _Py_IDENTIFIER(__aenter__);
2918
2919 PyObject *mgr = TOP();
2920 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2921 *enter;
2922 PyObject *res;
2923 if (exit == NULL)
2924 goto error;
2925 SET_TOP(exit);
2926 enter = special_lookup(mgr, &PyId___aenter__);
2927 Py_DECREF(mgr);
2928 if (enter == NULL)
2929 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002930 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002931 Py_DECREF(enter);
2932 if (res == NULL)
2933 goto error;
2934 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002935 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002936 DISPATCH();
2937 }
2938
2939 TARGET(SETUP_ASYNC_WITH) {
2940 PyObject *res = POP();
2941 /* Setup the finally block before pushing the result
2942 of __aenter__ on the stack. */
2943 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2944 STACK_LEVEL());
2945 PUSH(res);
2946 DISPATCH();
2947 }
2948
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002950 _Py_IDENTIFIER(__exit__);
2951 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002953 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002955 if (enter == NULL)
2956 goto error;
2957 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002958 if (exit == NULL) {
2959 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002961 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002964 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 Py_DECREF(enter);
2966 if (res == NULL)
2967 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 /* Setup the finally block before pushing the result
2969 of __enter__ on the stack. */
2970 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2971 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002972
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 DISPATCH();
2975 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002976
Yury Selivanov75445082015-05-11 22:57:16 -04002977 TARGET(WITH_CLEANUP_START) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002978 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002980 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 - (TOP, SECOND, THIRD) = exc_info()
2982 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002983 Below them is EXIT, the context.__exit__ or context.__aexit__
2984 bound method.
2985 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002987 otherwise we must call
2988 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002989
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002990 In the first case, we remove EXIT from the
2991 stack, leaving TOP, and push TOP on the stack.
2992 Otherwise we shift the bottom 3 values of the
2993 stack down, replace the empty spot with NULL, and push
2994 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002995
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002996 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002998 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003000 PyObject *exc, *val, *tb, *res;
3001
3002 val = tb = Py_None;
3003 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003004 if (exc == NULL) {
3005 STACKADJ(-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 }
3010 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003011 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 val = SECOND();
3015 tb = THIRD();
3016 tp2 = FOURTH();
3017 exc2 = PEEK(5);
3018 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 SET_VALUE(7, tb2);
3021 SET_VALUE(6, exc2);
3022 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3024 SET_FOURTH(NULL);
3025 /* We just shifted the stack down, so we have
3026 to tell the except handler block that the
3027 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003028 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 block = &f->f_blockstack[f->f_iblock - 1];
3030 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003031 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 block->b_level--;
3033 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003034
3035 stack[0] = exc;
3036 stack[1] = val;
3037 stack[2] = tb;
3038 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 if (res == NULL)
3041 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003042
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003043 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003044 PUSH(exc);
3045 PUSH(res);
3046 PREDICT(WITH_CLEANUP_FINISH);
3047 DISPATCH();
3048 }
3049
3050 PREDICTED(WITH_CLEANUP_FINISH);
3051 TARGET(WITH_CLEANUP_FINISH) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003052 /* TOP = the result of calling the context.__exit__ bound method
3053 SECOND = either None or exception type
3054
3055 If SECOND is None below is NULL or the return address,
3056 otherwise below are 7 values representing an exception.
3057 */
Yury Selivanov75445082015-05-11 22:57:16 -04003058 PyObject *res = POP();
3059 PyObject *exc = POP();
3060 int err;
3061
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 if (exc != Py_None)
3063 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 else
3065 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003066
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003068 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003073 /* There was an exception and a True return.
3074 * We must manually unwind the EXCEPT_HANDLER block
3075 * which was created when the exception was caught,
3076 * otherwise the stack will be in an inconsisten state.
3077 */
3078 PyTryBlock *b = PyFrame_BlockPop(f);
3079 assert(b->b_type == EXCEPT_HANDLER);
3080 UNWIND_EXCEPT_HANDLER(b);
3081 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 }
3083 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003086
Yury Selivanovf2392132016-12-13 19:03:51 -05003087 TARGET(LOAD_METHOD) {
3088 /* Designed to work in tamdem with CALL_METHOD. */
3089 PyObject *name = GETITEM(names, oparg);
3090 PyObject *obj = TOP();
3091 PyObject *meth = NULL;
3092
3093 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3094
Yury Selivanovf2392132016-12-13 19:03:51 -05003095 if (meth == NULL) {
3096 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003097 goto error;
3098 }
3099
3100 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003101 /* We can bypass temporary bound method object.
3102 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003103
INADA Naoki015bce62017-01-16 17:23:30 +09003104 meth | self | arg1 | ... | argN
3105 */
3106 SET_TOP(meth);
3107 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003108 }
3109 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003110 /* meth is not an unbound method (but a regular attr, or
3111 something was returned by a descriptor protocol). Set
3112 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003113 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003114
3115 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003116 */
INADA Naoki015bce62017-01-16 17:23:30 +09003117 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003118 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003119 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003120 }
3121 DISPATCH();
3122 }
3123
3124 TARGET(CALL_METHOD) {
3125 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003126 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003127
3128 sp = stack_pointer;
3129
INADA Naoki015bce62017-01-16 17:23:30 +09003130 meth = PEEK(oparg + 2);
3131 if (meth == NULL) {
3132 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3133 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003134
3135 Stack layout:
3136
INADA Naoki015bce62017-01-16 17:23:30 +09003137 ... | NULL | callable | arg1 | ... | argN
3138 ^- TOP()
3139 ^- (-oparg)
3140 ^- (-oparg-1)
3141 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003142
Ville Skyttä49b27342017-08-03 09:00:59 +03003143 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003144 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003145 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003146 res = call_function(&sp, oparg, NULL);
3147 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003148 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003149 }
3150 else {
3151 /* This is a method call. Stack layout:
3152
INADA Naoki015bce62017-01-16 17:23:30 +09003153 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003154 ^- TOP()
3155 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003156 ^- (-oparg-1)
3157 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003158
INADA Naoki015bce62017-01-16 17:23:30 +09003159 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003160 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003161 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003162 */
3163 res = call_function(&sp, oparg + 1, NULL);
3164 stack_pointer = sp;
3165 }
3166
3167 PUSH(res);
3168 if (res == NULL)
3169 goto error;
3170 DISPATCH();
3171 }
3172
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003173 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 TARGET(CALL_FUNCTION) {
3175 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003177 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003180 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003185
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003186 TARGET(CALL_FUNCTION_KW) {
3187 PyObject **sp, *res, *names;
3188
3189 names = POP();
3190 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003192 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003195 Py_DECREF(names);
3196
3197 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003199 }
3200 DISPATCH();
3201 }
3202
3203 TARGET(CALL_FUNCTION_EX) {
3204 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003205 if (oparg & 0x01) {
3206 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003207 if (!PyDict_CheckExact(kwargs)) {
3208 PyObject *d = PyDict_New();
3209 if (d == NULL)
3210 goto error;
3211 if (PyDict_Update(d, kwargs) != 0) {
3212 Py_DECREF(d);
3213 /* PyDict_Update raises attribute
3214 * error (percolated from an attempt
3215 * to get 'keys' attribute) instead of
3216 * a type error if its second argument
3217 * is not a mapping.
3218 */
3219 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003220 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003221 }
Victor Stinnereece2222016-09-12 11:16:37 +02003222 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003223 goto error;
3224 }
3225 Py_DECREF(kwargs);
3226 kwargs = d;
3227 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003228 assert(PyDict_CheckExact(kwargs));
3229 }
3230 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003231 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003232 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003233 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003234 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003235 goto error;
3236 }
3237 Py_SETREF(callargs, PySequence_Tuple(callargs));
3238 if (callargs == NULL) {
3239 goto error;
3240 }
3241 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003242 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003243
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003244 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003245 Py_DECREF(func);
3246 Py_DECREF(callargs);
3247 Py_XDECREF(kwargs);
3248
3249 SET_TOP(result);
3250 if (result == NULL) {
3251 goto error;
3252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003255
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003256 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003257 PyObject *qualname = POP();
3258 PyObject *codeobj = POP();
3259 PyFunctionObject *func = (PyFunctionObject *)
3260 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003261
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003262 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003264 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003267
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003268 if (oparg & 0x08) {
3269 assert(PyTuple_CheckExact(TOP()));
3270 func ->func_closure = POP();
3271 }
3272 if (oparg & 0x04) {
3273 assert(PyDict_CheckExact(TOP()));
3274 func->func_annotations = POP();
3275 }
3276 if (oparg & 0x02) {
3277 assert(PyDict_CheckExact(TOP()));
3278 func->func_kwdefaults = POP();
3279 }
3280 if (oparg & 0x01) {
3281 assert(PyTuple_CheckExact(TOP()));
3282 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003284
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003285 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003288
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003289 TARGET(BUILD_SLICE) {
3290 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 step = NULL;
3295 stop = POP();
3296 start = TOP();
3297 slice = PySlice_New(start, stop, step);
3298 Py_DECREF(start);
3299 Py_DECREF(stop);
3300 Py_XDECREF(step);
3301 SET_TOP(slice);
3302 if (slice == NULL)
3303 goto error;
3304 DISPATCH();
3305 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003306
Eric V. Smitha78c7952015-11-03 12:45:05 -05003307 TARGET(FORMAT_VALUE) {
3308 /* Handles f-string value formatting. */
3309 PyObject *result;
3310 PyObject *fmt_spec;
3311 PyObject *value;
3312 PyObject *(*conv_fn)(PyObject *);
3313 int which_conversion = oparg & FVC_MASK;
3314 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3315
3316 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003317 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003318
3319 /* See if any conversion is specified. */
3320 switch (which_conversion) {
3321 case FVC_STR: conv_fn = PyObject_Str; break;
3322 case FVC_REPR: conv_fn = PyObject_Repr; break;
3323 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3324
3325 /* Must be 0 (meaning no conversion), since only four
3326 values are allowed by (oparg & FVC_MASK). */
3327 default: conv_fn = NULL; break;
3328 }
3329
3330 /* If there's a conversion function, call it and replace
3331 value with that result. Otherwise, just use value,
3332 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003333 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003334 result = conv_fn(value);
3335 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003336 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003337 Py_XDECREF(fmt_spec);
3338 goto error;
3339 }
3340 value = result;
3341 }
3342
3343 /* If value is a unicode object, and there's no fmt_spec,
3344 then we know the result of format(value) is value
3345 itself. In that case, skip calling format(). I plan to
3346 move this optimization in to PyObject_Format()
3347 itself. */
3348 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3349 /* Do nothing, just transfer ownership to result. */
3350 result = value;
3351 } else {
3352 /* Actually call format(). */
3353 result = PyObject_Format(value, fmt_spec);
3354 Py_DECREF(value);
3355 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003356 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003357 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003358 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003359 }
3360
Eric V. Smith135d5f42016-02-05 18:23:08 -05003361 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003362 DISPATCH();
3363 }
3364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003365 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003366 int oldoparg = oparg;
3367 NEXTOPARG();
3368 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003371
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003372
Antoine Pitrou042b1282010-08-13 21:15:58 +00003373#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003375#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 default:
3377 fprintf(stderr,
3378 "XXX lineno: %d, opcode: %d\n",
3379 PyFrame_GetLineNumber(f),
3380 opcode);
3381 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003385
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 /* This should never be reached. Every opcode should end with DISPATCH()
3387 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003388 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003389
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003390error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003392#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 if (!PyErr_Occurred())
3394 PyErr_SetString(PyExc_SystemError,
3395 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003396#else
3397 assert(PyErr_Occurred());
3398#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003399
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 /* Log traceback info. */
3401 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003402
Benjamin Peterson51f46162013-01-23 08:38:47 -05003403 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003404 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3405 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003406
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003407exception_unwind:
3408 /* Unwind stacks if an exception occurred */
3409 while (f->f_iblock > 0) {
3410 /* Pop the current block. */
3411 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 if (b->b_type == EXCEPT_HANDLER) {
3414 UNWIND_EXCEPT_HANDLER(b);
3415 continue;
3416 }
3417 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003418 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 PyObject *exc, *val, *tb;
3420 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003421 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 /* Beware, this invalidates all b->b_* fields */
3423 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003424 PUSH(exc_info->exc_traceback);
3425 PUSH(exc_info->exc_value);
3426 if (exc_info->exc_type != NULL) {
3427 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 }
3429 else {
3430 Py_INCREF(Py_None);
3431 PUSH(Py_None);
3432 }
3433 PyErr_Fetch(&exc, &val, &tb);
3434 /* Make the raw exception data
3435 available to the handler,
3436 so a program can emulate the
3437 Python main loop. */
3438 PyErr_NormalizeException(
3439 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003440 if (tb != NULL)
3441 PyException_SetTraceback(val, tb);
3442 else
3443 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003445 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003447 exc_info->exc_value = val;
3448 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 if (tb == NULL)
3450 tb = Py_None;
3451 Py_INCREF(tb);
3452 PUSH(tb);
3453 PUSH(val);
3454 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003456 /* Resume normal execution */
3457 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 }
3459 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003460
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003461 /* End the loop as we still have an error */
3462 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 /* Pop remaining stack entries. */
3466 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003467 PyObject *o = POP();
3468 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003470
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003471 assert(retval == NULL);
3472 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003473
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003474return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003476 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003477 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3478 tstate, f, PyTrace_RETURN, retval)) {
3479 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 }
3481 }
3482 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003483 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3484 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003485 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 }
3487 }
3488 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003491exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003492 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3493 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003495 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003497
Victor Stinnerefde1462015-03-21 15:04:43 +01003498 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003499}
3500
Benjamin Petersonb204a422011-06-05 22:04:07 -05003501static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003502format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3503{
3504 int err;
3505 Py_ssize_t len = PyList_GET_SIZE(names);
3506 PyObject *name_str, *comma, *tail, *tmp;
3507
3508 assert(PyList_CheckExact(names));
3509 assert(len >= 1);
3510 /* Deal with the joys of natural language. */
3511 switch (len) {
3512 case 1:
3513 name_str = PyList_GET_ITEM(names, 0);
3514 Py_INCREF(name_str);
3515 break;
3516 case 2:
3517 name_str = PyUnicode_FromFormat("%U and %U",
3518 PyList_GET_ITEM(names, len - 2),
3519 PyList_GET_ITEM(names, len - 1));
3520 break;
3521 default:
3522 tail = PyUnicode_FromFormat(", %U, and %U",
3523 PyList_GET_ITEM(names, len - 2),
3524 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003525 if (tail == NULL)
3526 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003527 /* Chop off the last two objects in the list. This shouldn't actually
3528 fail, but we can't be too careful. */
3529 err = PyList_SetSlice(names, len - 2, len, NULL);
3530 if (err == -1) {
3531 Py_DECREF(tail);
3532 return;
3533 }
3534 /* Stitch everything up into a nice comma-separated list. */
3535 comma = PyUnicode_FromString(", ");
3536 if (comma == NULL) {
3537 Py_DECREF(tail);
3538 return;
3539 }
3540 tmp = PyUnicode_Join(comma, names);
3541 Py_DECREF(comma);
3542 if (tmp == NULL) {
3543 Py_DECREF(tail);
3544 return;
3545 }
3546 name_str = PyUnicode_Concat(tmp, tail);
3547 Py_DECREF(tmp);
3548 Py_DECREF(tail);
3549 break;
3550 }
3551 if (name_str == NULL)
3552 return;
3553 PyErr_Format(PyExc_TypeError,
3554 "%U() missing %i required %s argument%s: %U",
3555 co->co_name,
3556 len,
3557 kind,
3558 len == 1 ? "" : "s",
3559 name_str);
3560 Py_DECREF(name_str);
3561}
3562
3563static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003564missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003565 PyObject **fastlocals)
3566{
Victor Stinner74319ae2016-08-25 00:04:09 +02003567 Py_ssize_t i, j = 0;
3568 Py_ssize_t start, end;
3569 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003570 const char *kind = positional ? "positional" : "keyword-only";
3571 PyObject *missing_names;
3572
3573 /* Compute the names of the arguments that are missing. */
3574 missing_names = PyList_New(missing);
3575 if (missing_names == NULL)
3576 return;
3577 if (positional) {
3578 start = 0;
3579 end = co->co_argcount - defcount;
3580 }
3581 else {
3582 start = co->co_argcount;
3583 end = start + co->co_kwonlyargcount;
3584 }
3585 for (i = start; i < end; i++) {
3586 if (GETLOCAL(i) == NULL) {
3587 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3588 PyObject *name = PyObject_Repr(raw);
3589 if (name == NULL) {
3590 Py_DECREF(missing_names);
3591 return;
3592 }
3593 PyList_SET_ITEM(missing_names, j++, name);
3594 }
3595 }
3596 assert(j == missing);
3597 format_missing(kind, co, missing_names);
3598 Py_DECREF(missing_names);
3599}
3600
3601static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003602too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3603 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003604{
3605 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003606 Py_ssize_t kwonly_given = 0;
3607 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003608 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003609 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003610
Benjamin Petersone109c702011-06-24 09:37:26 -05003611 assert((co->co_flags & CO_VARARGS) == 0);
3612 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003613 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3614 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003615 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003616 }
3617 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003618 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003619 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003620 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003621 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003622 }
3623 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003624 plural = (co_argcount != 1);
3625 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003626 }
3627 if (sig == NULL)
3628 return;
3629 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003630 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3631 kwonly_sig = PyUnicode_FromFormat(format,
3632 given != 1 ? "s" : "",
3633 kwonly_given,
3634 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003635 if (kwonly_sig == NULL) {
3636 Py_DECREF(sig);
3637 return;
3638 }
3639 }
3640 else {
3641 /* This will not fail. */
3642 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003643 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003644 }
3645 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003646 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003647 co->co_name,
3648 sig,
3649 plural ? "s" : "",
3650 given,
3651 kwonly_sig,
3652 given == 1 && !kwonly_given ? "was" : "were");
3653 Py_DECREF(sig);
3654 Py_DECREF(kwonly_sig);
3655}
3656
Guido van Rossumc2e20742006-02-27 22:32:47 +00003657/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003658 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003659 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003660
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003661PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003662_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003663 PyObject *const *args, Py_ssize_t argcount,
3664 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003665 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003666 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003667 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003668 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003669{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003670 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003671 PyFrameObject *f;
3672 PyObject *retval = NULL;
3673 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003674 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003676 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3677 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003678 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 if (globals == NULL) {
3681 PyErr_SetString(PyExc_SystemError,
3682 "PyEval_EvalCodeEx: NULL globals");
3683 return NULL;
3684 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003685
Victor Stinnerc7020012016-08-16 23:40:29 +02003686 /* Create the frame */
3687 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003689 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003690 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003692 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 fastlocals = f->f_localsplus;
3694 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003695
Victor Stinnerc7020012016-08-16 23:40:29 +02003696 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003697 if (co->co_flags & CO_VARKEYWORDS) {
3698 kwdict = PyDict_New();
3699 if (kwdict == NULL)
3700 goto fail;
3701 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003702 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003703 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003704 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705 SETLOCAL(i, kwdict);
3706 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003707 else {
3708 kwdict = NULL;
3709 }
3710
3711 /* Copy positional arguments into local variables */
3712 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003713 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003714 }
3715 else {
3716 n = argcount;
3717 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003718 for (i = 0; i < n; i++) {
3719 x = args[i];
3720 Py_INCREF(x);
3721 SETLOCAL(i, x);
3722 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003723
3724 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725 if (co->co_flags & CO_VARARGS) {
3726 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003727 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003728 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003729 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003730 SETLOCAL(total_args, u);
3731 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 x = args[i];
3733 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003734 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003737
Serhiy Storchakab7281052016-09-12 00:52:40 +03003738 /* Handle keyword arguments passed as two strided arrays */
3739 kwcount *= kwstep;
3740 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003742 PyObject *keyword = kwnames[i];
3743 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003744 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003745
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3747 PyErr_Format(PyExc_TypeError,
3748 "%U() keywords must be strings",
3749 co->co_name);
3750 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003752
Benjamin Petersonb204a422011-06-05 22:04:07 -05003753 /* Speed hack: do raw pointer compares. As names are
3754 normally interned this should almost always hit. */
3755 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3756 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003757 PyObject *name = co_varnames[j];
3758 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003759 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003760 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003761 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003762
Benjamin Petersonb204a422011-06-05 22:04:07 -05003763 /* Slow fallback, just in case */
3764 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003765 PyObject *name = co_varnames[j];
3766 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3767 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003769 }
3770 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003772 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003773 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003774
Victor Stinner231d1f32017-01-11 02:12:06 +01003775 assert(j >= total_args);
3776 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003778 "%U() got an unexpected keyword argument '%S'",
3779 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003780 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003782
Christian Heimes0bd447f2013-07-20 14:48:10 +02003783 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3784 goto fail;
3785 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003787
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 kw_found:
3789 if (GETLOCAL(j) != NULL) {
3790 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003791 "%U() got multiple values for argument '%S'",
3792 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003793 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 Py_INCREF(value);
3796 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003798
3799 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003800 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003801 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 goto fail;
3803 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003804
3805 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003807 Py_ssize_t m = co->co_argcount - defcount;
3808 Py_ssize_t missing = 0;
3809 for (i = argcount; i < m; i++) {
3810 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003811 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003812 }
3813 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003814 if (missing) {
3815 missing_arguments(co, missing, defcount, fastlocals);
3816 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 }
3818 if (n > m)
3819 i = n - m;
3820 else
3821 i = 0;
3822 for (; i < defcount; i++) {
3823 if (GETLOCAL(m+i) == NULL) {
3824 PyObject *def = defs[i];
3825 Py_INCREF(def);
3826 SETLOCAL(m+i, def);
3827 }
3828 }
3829 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003830
3831 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003833 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003834 for (i = co->co_argcount; i < total_args; i++) {
3835 PyObject *name;
3836 if (GETLOCAL(i) != NULL)
3837 continue;
3838 name = PyTuple_GET_ITEM(co->co_varnames, i);
3839 if (kwdefs != NULL) {
3840 PyObject *def = PyDict_GetItem(kwdefs, name);
3841 if (def) {
3842 Py_INCREF(def);
3843 SETLOCAL(i, def);
3844 continue;
3845 }
3846 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003847 missing++;
3848 }
3849 if (missing) {
3850 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 goto fail;
3852 }
3853 }
3854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003856 vars into frame. */
3857 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003859 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003860 /* Possibly account for the cell variable being an argument. */
3861 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003862 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003863 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003864 /* Clear the local copy. */
3865 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003866 }
3867 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003868 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003869 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003870 if (c == NULL)
3871 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003872 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003874
3875 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003876 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3877 PyObject *o = PyTuple_GET_ITEM(closure, i);
3878 Py_INCREF(o);
3879 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003881
Yury Selivanoveb636452016-09-08 22:01:51 -07003882 /* Handle generator/coroutine/asynchronous generator */
3883 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003884 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003885 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003886 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003887
3888 if (is_coro && tstate->in_coroutine_wrapper) {
3889 assert(coro_wrapper != NULL);
3890 PyErr_Format(PyExc_RuntimeError,
3891 "coroutine wrapper %.200R attempted "
3892 "to recursively wrap %.200R",
3893 coro_wrapper,
3894 co);
3895 goto fail;
3896 }
Yury Selivanov75445082015-05-11 22:57:16 -04003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 /* Don't need to keep the reference to f_back, it will be set
3899 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003900 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 /* Create a new generator that owns the ready to run frame
3903 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003904 if (is_coro) {
3905 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003906 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3907 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003908 } else {
3909 gen = PyGen_NewWithQualName(f, name, qualname);
3910 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003911 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003912 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003913 }
INADA Naoki9c157762016-12-26 18:52:46 +09003914
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003915 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003916
Yury Selivanov94c22632015-06-04 10:16:51 -04003917 if (is_coro && coro_wrapper != NULL) {
3918 PyObject *wrapped;
3919 tstate->in_coroutine_wrapper = 1;
3920 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3921 tstate->in_coroutine_wrapper = 0;
3922 return wrapped;
3923 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003924
Yury Selivanov75445082015-05-11 22:57:16 -04003925 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003927
Victor Stinner59a73272016-12-09 18:51:13 +01003928 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003929
Thomas Woutersce272b62007-09-19 21:19:28 +00003930fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 /* decref'ing the frame can cause __del__ methods to get invoked,
3933 which can call back into Python. While we're done with the
3934 current Python frame (f), the associated C stack is still in use,
3935 so recursion_depth must be boosted for the duration.
3936 */
3937 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003938 if (Py_REFCNT(f) > 1) {
3939 Py_DECREF(f);
3940 _PyObject_GC_TRACK(f);
3941 }
3942 else {
3943 ++tstate->recursion_depth;
3944 Py_DECREF(f);
3945 --tstate->recursion_depth;
3946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003948}
3949
Victor Stinner40ee3012014-06-16 15:59:28 +02003950PyObject *
3951PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003952 PyObject *const *args, int argcount,
3953 PyObject *const *kws, int kwcount,
3954 PyObject *const *defs, int defcount,
3955 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003956{
3957 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003958 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003959 kws, kws != NULL ? kws + 1 : NULL,
3960 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003961 defs, defcount,
3962 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003963 NULL, NULL);
3964}
Tim Peters5ca576e2001-06-18 22:08:13 +00003965
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003966static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003967special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003970 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003972 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 return NULL;
3974 }
3975 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003976}
3977
3978
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003979/* Logic for the raise statement (too complicated for inlining).
3980 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003981static int
Collin Winter828f04a2007-08-31 00:04:24 +00003982do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 if (exc == NULL) {
3987 /* Reraise */
3988 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01003989 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01003991 type = exc_info->exc_type;
3992 value = exc_info->exc_value;
3993 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02003994 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 PyErr_SetString(PyExc_RuntimeError,
3996 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003997 return 0;
3998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 Py_XINCREF(type);
4000 Py_XINCREF(value);
4001 Py_XINCREF(tb);
4002 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004003 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 /* We support the following forms of raise:
4007 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004008 raise <instance>
4009 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 if (PyExceptionClass_Check(exc)) {
4012 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004013 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 if (value == NULL)
4015 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004016 if (!PyExceptionInstance_Check(value)) {
4017 PyErr_Format(PyExc_TypeError,
4018 "calling %R should have returned an instance of "
4019 "BaseException, not %R",
4020 type, Py_TYPE(value));
4021 goto raise_error;
4022 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 }
4024 else if (PyExceptionInstance_Check(exc)) {
4025 value = exc;
4026 type = PyExceptionInstance_Class(exc);
4027 Py_INCREF(type);
4028 }
4029 else {
4030 /* Not something you can raise. You get an exception
4031 anyway, just not what you specified :-) */
4032 Py_DECREF(exc);
4033 PyErr_SetString(PyExc_TypeError,
4034 "exceptions must derive from BaseException");
4035 goto raise_error;
4036 }
Collin Winter828f04a2007-08-31 00:04:24 +00004037
Serhiy Storchakac0191582016-09-27 11:37:10 +03004038 assert(type != NULL);
4039 assert(value != NULL);
4040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 if (cause) {
4042 PyObject *fixed_cause;
4043 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004044 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 if (fixed_cause == NULL)
4046 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004047 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004049 else if (PyExceptionInstance_Check(cause)) {
4050 fixed_cause = cause;
4051 }
4052 else if (cause == Py_None) {
4053 Py_DECREF(cause);
4054 fixed_cause = NULL;
4055 }
4056 else {
4057 PyErr_SetString(PyExc_TypeError,
4058 "exception causes must derive from "
4059 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 goto raise_error;
4061 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004062 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Collin Winter828f04a2007-08-31 00:04:24 +00004064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 PyErr_SetObject(type, value);
4066 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004067 Py_DECREF(value);
4068 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004069 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004070
4071raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 Py_XDECREF(value);
4073 Py_XDECREF(type);
4074 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004075 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004076}
4077
Tim Petersd6d010b2001-06-21 02:49:55 +00004078/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004079 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004080
Guido van Rossum0368b722007-05-11 16:50:42 +00004081 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4082 with a variable target.
4083*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004084
Barry Warsawe42b18f1997-08-25 22:13:04 +00004085static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004086unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 int i = 0, j = 0;
4089 Py_ssize_t ll = 0;
4090 PyObject *it; /* iter(v) */
4091 PyObject *w;
4092 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004097 if (it == NULL) {
4098 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4099 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4100 {
4101 PyErr_Format(PyExc_TypeError,
4102 "cannot unpack non-iterable %.200s object",
4103 v->ob_type->tp_name);
4104 }
4105 return 0;
4106 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 for (; i < argcnt; i++) {
4109 w = PyIter_Next(it);
4110 if (w == NULL) {
4111 /* Iterator done, via error or exhaustion. */
4112 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004113 if (argcntafter == -1) {
4114 PyErr_Format(PyExc_ValueError,
4115 "not enough values to unpack (expected %d, got %d)",
4116 argcnt, i);
4117 }
4118 else {
4119 PyErr_Format(PyExc_ValueError,
4120 "not enough values to unpack "
4121 "(expected at least %d, got %d)",
4122 argcnt + argcntafter, i);
4123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 }
4125 goto Error;
4126 }
4127 *--sp = w;
4128 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 if (argcntafter == -1) {
4131 /* We better have exhausted the iterator now. */
4132 w = PyIter_Next(it);
4133 if (w == NULL) {
4134 if (PyErr_Occurred())
4135 goto Error;
4136 Py_DECREF(it);
4137 return 1;
4138 }
4139 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004140 PyErr_Format(PyExc_ValueError,
4141 "too many values to unpack (expected %d)",
4142 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 goto Error;
4144 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 l = PySequence_List(it);
4147 if (l == NULL)
4148 goto Error;
4149 *--sp = l;
4150 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 ll = PyList_GET_SIZE(l);
4153 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004154 PyErr_Format(PyExc_ValueError,
4155 "not enough values to unpack (expected at least %d, got %zd)",
4156 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 goto Error;
4158 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 /* Pop the "after-variable" args off the list. */
4161 for (j = argcntafter; j > 0; j--, i++) {
4162 *--sp = PyList_GET_ITEM(l, ll - j);
4163 }
4164 /* Resize the list. */
4165 Py_SIZE(l) = ll - argcntafter;
4166 Py_DECREF(it);
4167 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004168
Tim Petersd6d010b2001-06-21 02:49:55 +00004169Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 for (; i > 0; i--, sp++)
4171 Py_DECREF(*sp);
4172 Py_XDECREF(it);
4173 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004174}
4175
4176
Guido van Rossum96a42c81992-01-12 02:29:51 +00004177#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004178static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004179prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 printf("%s ", str);
4182 if (PyObject_Print(v, stdout, 0) != 0)
4183 PyErr_Clear(); /* Don't know what else to do */
4184 printf("\n");
4185 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004186}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004187#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004188
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004189static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004190call_exc_trace(Py_tracefunc func, PyObject *self,
4191 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004192{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004193 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004195 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 if (value == NULL) {
4197 value = Py_None;
4198 Py_INCREF(value);
4199 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004200 PyErr_NormalizeException(&type, &value, &orig_traceback);
4201 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 arg = PyTuple_Pack(3, type, value, traceback);
4203 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004204 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 return;
4206 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004207 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 Py_DECREF(arg);
4209 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004210 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 else {
4212 Py_XDECREF(type);
4213 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004214 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004216}
4217
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004218static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004219call_trace_protected(Py_tracefunc func, PyObject *obj,
4220 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 PyObject *type, *value, *traceback;
4224 int err;
4225 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004226 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 if (err == 0)
4228 {
4229 PyErr_Restore(type, value, traceback);
4230 return 0;
4231 }
4232 else {
4233 Py_XDECREF(type);
4234 Py_XDECREF(value);
4235 Py_XDECREF(traceback);
4236 return -1;
4237 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004238}
4239
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004240static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004241call_trace(Py_tracefunc func, PyObject *obj,
4242 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 int result;
4246 if (tstate->tracing)
4247 return 0;
4248 tstate->tracing++;
4249 tstate->use_tracing = 0;
4250 result = func(obj, frame, what, arg);
4251 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4252 || (tstate->c_profilefunc != NULL));
4253 tstate->tracing--;
4254 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004255}
4256
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004257PyObject *
4258_PyEval_CallTracing(PyObject *func, PyObject *args)
4259{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004260 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 int save_tracing = tstate->tracing;
4262 int save_use_tracing = tstate->use_tracing;
4263 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 tstate->tracing = 0;
4266 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4267 || (tstate->c_profilefunc != NULL));
4268 result = PyObject_Call(func, args, NULL);
4269 tstate->tracing = save_tracing;
4270 tstate->use_tracing = save_use_tracing;
4271 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004272}
4273
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004274/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004275static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004276maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004277 PyThreadState *tstate, PyFrameObject *frame,
4278 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 int result = 0;
4281 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 /* If the last instruction executed isn't in the current
4284 instruction window, reset the window.
4285 */
4286 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4287 PyAddrPair bounds;
4288 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4289 &bounds);
4290 *instr_lb = bounds.ap_lower;
4291 *instr_ub = bounds.ap_upper;
4292 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004293 /* If the last instruction falls at the start of a line or if it
4294 represents a jump backwards, update the frame's line number and
4295 then call the trace function if we're tracing source lines.
4296 */
4297 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004299 if (frame->f_trace_lines) {
4300 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 }
George King20faa682017-10-18 17:44:22 -07004303 /* Always emit an opcode event if we're tracing all opcodes. */
4304 if (frame->f_trace_opcodes) {
4305 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4306 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 *instr_prev = frame->f_lasti;
4308 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004309}
4310
Fred Drake5755ce62001-06-27 19:19:46 +00004311void
4312PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 PyThreadState *tstate = PyThreadState_GET();
4315 PyObject *temp = tstate->c_profileobj;
4316 Py_XINCREF(arg);
4317 tstate->c_profilefunc = NULL;
4318 tstate->c_profileobj = NULL;
4319 /* Must make sure that tracing is not ignored if 'temp' is freed */
4320 tstate->use_tracing = tstate->c_tracefunc != NULL;
4321 Py_XDECREF(temp);
4322 tstate->c_profilefunc = func;
4323 tstate->c_profileobj = arg;
4324 /* Flag that tracing or profiling is turned on */
4325 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004326}
4327
4328void
4329PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 PyThreadState *tstate = PyThreadState_GET();
4332 PyObject *temp = tstate->c_traceobj;
4333 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4334 Py_XINCREF(arg);
4335 tstate->c_tracefunc = NULL;
4336 tstate->c_traceobj = NULL;
4337 /* Must make sure that profiling is not ignored if 'temp' is freed */
4338 tstate->use_tracing = tstate->c_profilefunc != NULL;
4339 Py_XDECREF(temp);
4340 tstate->c_tracefunc = func;
4341 tstate->c_traceobj = arg;
4342 /* Flag that tracing or profiling is turned on */
4343 tstate->use_tracing = ((func != NULL)
4344 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004345}
4346
Yury Selivanov75445082015-05-11 22:57:16 -04004347void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004348_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4349{
4350 assert(new_depth >= 0);
4351 PyThreadState *tstate = PyThreadState_GET();
4352 tstate->coroutine_origin_tracking_depth = new_depth;
4353}
4354
4355int
4356_PyEval_GetCoroutineOriginTrackingDepth(void)
4357{
4358 PyThreadState *tstate = PyThreadState_GET();
4359 return tstate->coroutine_origin_tracking_depth;
4360}
4361
4362void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004363_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004364{
4365 PyThreadState *tstate = PyThreadState_GET();
4366
Yury Selivanov75445082015-05-11 22:57:16 -04004367 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004368 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004369}
4370
4371PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004372_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004373{
4374 PyThreadState *tstate = PyThreadState_GET();
4375 return tstate->coroutine_wrapper;
4376}
4377
Yury Selivanoveb636452016-09-08 22:01:51 -07004378void
4379_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4380{
4381 PyThreadState *tstate = PyThreadState_GET();
4382
4383 Py_XINCREF(firstiter);
4384 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4385}
4386
4387PyObject *
4388_PyEval_GetAsyncGenFirstiter(void)
4389{
4390 PyThreadState *tstate = PyThreadState_GET();
4391 return tstate->async_gen_firstiter;
4392}
4393
4394void
4395_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4396{
4397 PyThreadState *tstate = PyThreadState_GET();
4398
4399 Py_XINCREF(finalizer);
4400 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4401}
4402
4403PyObject *
4404_PyEval_GetAsyncGenFinalizer(void)
4405{
4406 PyThreadState *tstate = PyThreadState_GET();
4407 return tstate->async_gen_finalizer;
4408}
4409
Guido van Rossumb209a111997-04-29 18:18:01 +00004410PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004411PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 PyFrameObject *current_frame = PyEval_GetFrame();
4414 if (current_frame == NULL)
4415 return PyThreadState_GET()->interp->builtins;
4416 else
4417 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004418}
4419
Guido van Rossumb209a111997-04-29 18:18:01 +00004420PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004421PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004424 if (current_frame == NULL) {
4425 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004427 }
4428
4429 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4430 return NULL;
4431
4432 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004434}
4435
Guido van Rossumb209a111997-04-29 18:18:01 +00004436PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004437PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 PyFrameObject *current_frame = PyEval_GetFrame();
4440 if (current_frame == NULL)
4441 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004442
4443 assert(current_frame->f_globals != NULL);
4444 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004445}
4446
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004447PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004448PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 PyThreadState *tstate = PyThreadState_GET();
4451 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004452}
4453
Guido van Rossum6135a871995-01-09 17:53:26 +00004454int
Tim Peters5ba58662001-07-16 02:29:45 +00004455PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyFrameObject *current_frame = PyEval_GetFrame();
4458 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 if (current_frame != NULL) {
4461 const int codeflags = current_frame->f_code->co_flags;
4462 const int compilerflags = codeflags & PyCF_MASK;
4463 if (compilerflags) {
4464 result = 1;
4465 cf->cf_flags |= compilerflags;
4466 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004467#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 if (codeflags & CO_GENERATOR_ALLOWED) {
4469 result = 1;
4470 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4471 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004472#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 }
4474 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004475}
4476
Guido van Rossum3f5da241990-12-20 15:06:42 +00004477
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004478const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004479PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 if (PyMethod_Check(func))
4482 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4483 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004484 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 else if (PyCFunction_Check(func))
4486 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4487 else
4488 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004489}
4490
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004491const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004492PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 if (PyMethod_Check(func))
4495 return "()";
4496 else if (PyFunction_Check(func))
4497 return "()";
4498 else if (PyCFunction_Check(func))
4499 return "()";
4500 else
4501 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004502}
4503
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004504#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004505if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004506 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4507 tstate, tstate->frame, \
4508 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 x = NULL; \
4510 } \
4511 else { \
4512 x = call; \
4513 if (tstate->c_profilefunc != NULL) { \
4514 if (x == NULL) { \
4515 call_trace_protected(tstate->c_profilefunc, \
4516 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004517 tstate, tstate->frame, \
4518 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 /* XXX should pass (type, value, tb) */ \
4520 } else { \
4521 if (call_trace(tstate->c_profilefunc, \
4522 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004523 tstate, tstate->frame, \
4524 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 Py_DECREF(x); \
4526 x = NULL; \
4527 } \
4528 } \
4529 } \
4530 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004531} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 x = call; \
4533 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004534
Victor Stinner415c5102017-01-11 00:54:57 +01004535/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4536 to reduce the stack consumption. */
4537Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004538call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004539{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004540 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 PyObject *func = *pfunc;
4542 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004543 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4544 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004545 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 /* Always dispatch PyCFunction first, because these are
4548 presumed to be the most frequent callable object.
4549 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004550 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004552 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004553 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004554 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4555 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004556 if (tstate->use_tracing && tstate->c_profilefunc) {
4557 // We need to create PyCFunctionObject for tracing.
4558 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4559 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4560 if (func == NULL) {
4561 return NULL;
4562 }
4563 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4564 kwnames));
4565 Py_DECREF(func);
4566 }
4567 else {
4568 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4569 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004570 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004571 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004572 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004573 /* Optimize access to bound methods. Reuse the Python stack
4574 to pass 'self' as the first argument, replace 'func'
4575 with 'self'. It avoids the creation of a new temporary tuple
4576 for arguments (to replace func with self) when the method uses
4577 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004578 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004579 Py_INCREF(self);
4580 func = PyMethod_GET_FUNCTION(func);
4581 Py_INCREF(func);
4582 Py_SETREF(*pfunc, self);
4583 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004584 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004585 }
4586 else {
4587 Py_INCREF(func);
4588 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004589
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004590 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004591 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004592 }
4593 else {
4594 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4595 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004596 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004598
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004599 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4600
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004601 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 while ((*pp_stack) > pfunc) {
4603 w = EXT_POP(*pp_stack);
4604 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004608}
4609
Jeremy Hylton52820442001-01-03 23:52:36 +00004610static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004611do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004614 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 PyThreadState *tstate = PyThreadState_GET();
4616 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004617 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004619 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004620 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004621 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004622}
4623
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004624/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004625 nb_index slot defined, and store in *pi.
4626 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004627 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004628 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004629*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004630int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004631_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004632{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004633 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 Py_ssize_t x;
4635 if (PyIndex_Check(v)) {
4636 x = PyNumber_AsSsize_t(v, NULL);
4637 if (x == -1 && PyErr_Occurred())
4638 return 0;
4639 }
4640 else {
4641 PyErr_SetString(PyExc_TypeError,
4642 "slice indices must be integers or "
4643 "None or have an __index__ method");
4644 return 0;
4645 }
4646 *pi = x;
4647 }
4648 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004649}
4650
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004651int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004652_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004653{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004654 Py_ssize_t x;
4655 if (PyIndex_Check(v)) {
4656 x = PyNumber_AsSsize_t(v, NULL);
4657 if (x == -1 && PyErr_Occurred())
4658 return 0;
4659 }
4660 else {
4661 PyErr_SetString(PyExc_TypeError,
4662 "slice indices must be integers or "
4663 "have an __index__ method");
4664 return 0;
4665 }
4666 *pi = x;
4667 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004668}
4669
4670
Guido van Rossum486364b2007-06-30 05:01:58 +00004671#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004673
Guido van Rossumb209a111997-04-29 18:18:01 +00004674static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004675cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 int res = 0;
4678 switch (op) {
4679 case PyCmp_IS:
4680 res = (v == w);
4681 break;
4682 case PyCmp_IS_NOT:
4683 res = (v != w);
4684 break;
4685 case PyCmp_IN:
4686 res = PySequence_Contains(w, v);
4687 if (res < 0)
4688 return NULL;
4689 break;
4690 case PyCmp_NOT_IN:
4691 res = PySequence_Contains(w, v);
4692 if (res < 0)
4693 return NULL;
4694 res = !res;
4695 break;
4696 case PyCmp_EXC_MATCH:
4697 if (PyTuple_Check(w)) {
4698 Py_ssize_t i, length;
4699 length = PyTuple_Size(w);
4700 for (i = 0; i < length; i += 1) {
4701 PyObject *exc = PyTuple_GET_ITEM(w, i);
4702 if (!PyExceptionClass_Check(exc)) {
4703 PyErr_SetString(PyExc_TypeError,
4704 CANNOT_CATCH_MSG);
4705 return NULL;
4706 }
4707 }
4708 }
4709 else {
4710 if (!PyExceptionClass_Check(w)) {
4711 PyErr_SetString(PyExc_TypeError,
4712 CANNOT_CATCH_MSG);
4713 return NULL;
4714 }
4715 }
4716 res = PyErr_GivenExceptionMatches(v, w);
4717 break;
4718 default:
4719 return PyObject_RichCompare(v, w, op);
4720 }
4721 v = res ? Py_True : Py_False;
4722 Py_INCREF(v);
4723 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004724}
4725
Thomas Wouters52152252000-08-17 22:55:00 +00004726static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004727import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4728{
4729 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004730 PyObject *import_func, *res;
4731 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004732
4733 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4734 if (import_func == NULL) {
4735 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4736 return NULL;
4737 }
4738
4739 /* Fast path for not overloaded __import__. */
4740 if (import_func == PyThreadState_GET()->interp->import_func) {
4741 int ilevel = _PyLong_AsInt(level);
4742 if (ilevel == -1 && PyErr_Occurred()) {
4743 return NULL;
4744 }
4745 res = PyImport_ImportModuleLevelObject(
4746 name,
4747 f->f_globals,
4748 f->f_locals == NULL ? Py_None : f->f_locals,
4749 fromlist,
4750 ilevel);
4751 return res;
4752 }
4753
4754 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004755
4756 stack[0] = name;
4757 stack[1] = f->f_globals;
4758 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4759 stack[3] = fromlist;
4760 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004761 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004762 Py_DECREF(import_func);
4763 return res;
4764}
4765
4766static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004767import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004770 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004771 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004772
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004773 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004774 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004775 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004776 /* Issue #17636: in case this failed because of a circular relative
4777 import, try to fallback on reading the module directly from
4778 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004779 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004780 if (pkgname == NULL) {
4781 goto error;
4782 }
Oren Milman6db70332017-09-19 14:23:01 +03004783 if (!PyUnicode_Check(pkgname)) {
4784 Py_CLEAR(pkgname);
4785 goto error;
4786 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004787 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004788 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004789 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004790 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004791 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004792 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004793 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004794 if (x == NULL) {
4795 goto error;
4796 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004797 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004799 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004800 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004801 if (pkgname == NULL) {
4802 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4803 if (pkgname_or_unknown == NULL) {
4804 Py_XDECREF(pkgpath);
4805 return NULL;
4806 }
4807 } else {
4808 pkgname_or_unknown = pkgname;
4809 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004810
4811 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4812 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004813 errmsg = PyUnicode_FromFormat(
4814 "cannot import name %R from %R (unknown location)",
4815 name, pkgname_or_unknown
4816 );
4817 /* NULL check for errmsg done by PyErr_SetImportError. */
4818 PyErr_SetImportError(errmsg, pkgname, NULL);
4819 }
4820 else {
4821 errmsg = PyUnicode_FromFormat(
4822 "cannot import name %R from %R (%S)",
4823 name, pkgname_or_unknown, pkgpath
4824 );
4825 /* NULL check for errmsg done by PyErr_SetImportError. */
4826 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004827 }
4828
Xiang Zhang4830f582017-03-21 11:13:42 +08004829 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004830 Py_XDECREF(pkgname_or_unknown);
4831 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004832 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004833}
Guido van Rossumac7be682001-01-17 15:42:30 +00004834
Thomas Wouters52152252000-08-17 22:55:00 +00004835static int
4836import_all_from(PyObject *locals, PyObject *v)
4837{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004838 _Py_IDENTIFIER(__all__);
4839 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004840 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004841 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 int skip_leading_underscores = 0;
4843 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004844
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004845 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4846 return -1; /* Unexpected error */
4847 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004849 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4850 return -1;
4851 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004854 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 return -1;
4856 }
4857 all = PyMapping_Keys(dict);
4858 Py_DECREF(dict);
4859 if (all == NULL)
4860 return -1;
4861 skip_leading_underscores = 1;
4862 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 for (pos = 0, err = 0; ; pos++) {
4865 name = PySequence_GetItem(all, pos);
4866 if (name == NULL) {
4867 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4868 err = -1;
4869 else
4870 PyErr_Clear();
4871 break;
4872 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004873 if (!PyUnicode_Check(name)) {
4874 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4875 if (modname == NULL) {
4876 Py_DECREF(name);
4877 err = -1;
4878 break;
4879 }
4880 if (!PyUnicode_Check(modname)) {
4881 PyErr_Format(PyExc_TypeError,
4882 "module __name__ must be a string, not %.100s",
4883 Py_TYPE(modname)->tp_name);
4884 }
4885 else {
4886 PyErr_Format(PyExc_TypeError,
4887 "%s in %U.%s must be str, not %.100s",
4888 skip_leading_underscores ? "Key" : "Item",
4889 modname,
4890 skip_leading_underscores ? "__dict__" : "__all__",
4891 Py_TYPE(name)->tp_name);
4892 }
4893 Py_DECREF(modname);
4894 Py_DECREF(name);
4895 err = -1;
4896 break;
4897 }
4898 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004899 if (PyUnicode_READY(name) == -1) {
4900 Py_DECREF(name);
4901 err = -1;
4902 break;
4903 }
4904 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4905 Py_DECREF(name);
4906 continue;
4907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 }
4909 value = PyObject_GetAttr(v, name);
4910 if (value == NULL)
4911 err = -1;
4912 else if (PyDict_CheckExact(locals))
4913 err = PyDict_SetItem(locals, name, value);
4914 else
4915 err = PyObject_SetItem(locals, name, value);
4916 Py_DECREF(name);
4917 Py_XDECREF(value);
4918 if (err != 0)
4919 break;
4920 }
4921 Py_DECREF(all);
4922 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004923}
4924
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004925static int
4926check_args_iterable(PyObject *func, PyObject *args)
4927{
4928 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4929 PyErr_Format(PyExc_TypeError,
4930 "%.200s%.200s argument after * "
4931 "must be an iterable, not %.200s",
4932 PyEval_GetFuncName(func),
4933 PyEval_GetFuncDesc(func),
4934 args->ob_type->tp_name);
4935 return -1;
4936 }
4937 return 0;
4938}
4939
4940static void
4941format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4942{
4943 PyErr_Format(PyExc_TypeError,
4944 "%.200s%.200s argument after ** "
4945 "must be a mapping, not %.200s",
4946 PyEval_GetFuncName(func),
4947 PyEval_GetFuncDesc(func),
4948 kwargs->ob_type->tp_name);
4949}
4950
Guido van Rossumac7be682001-01-17 15:42:30 +00004951static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004952format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 if (!obj)
4957 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004958
Serhiy Storchaka06515832016-11-20 09:13:07 +02004959 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 if (!obj_str)
4961 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004964}
Guido van Rossum950361c1997-01-24 13:49:28 +00004965
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004966static void
4967format_exc_unbound(PyCodeObject *co, int oparg)
4968{
4969 PyObject *name;
4970 /* Don't stomp existing exception */
4971 if (PyErr_Occurred())
4972 return;
4973 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4974 name = PyTuple_GET_ITEM(co->co_cellvars,
4975 oparg);
4976 format_exc_check_arg(
4977 PyExc_UnboundLocalError,
4978 UNBOUNDLOCAL_ERROR_MSG,
4979 name);
4980 } else {
4981 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4982 PyTuple_GET_SIZE(co->co_cellvars));
4983 format_exc_check_arg(PyExc_NameError,
4984 UNBOUNDFREE_ERROR_MSG, name);
4985 }
4986}
4987
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004988static PyObject *
4989unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03004990 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004991{
4992 PyObject *res;
4993 if (Py_REFCNT(v) == 2) {
4994 /* In the common case, there are 2 references to the value
4995 * stored in 'variable' when the += is performed: one on the
4996 * value stack (in 'v') and one still stored in the
4997 * 'variable'. We try to delete the variable now to reduce
4998 * the refcnt to 1.
4999 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005000 int opcode, oparg;
5001 NEXTOPARG();
5002 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005003 case STORE_FAST:
5004 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005005 PyObject **fastlocals = f->f_localsplus;
5006 if (GETLOCAL(oparg) == v)
5007 SETLOCAL(oparg, NULL);
5008 break;
5009 }
5010 case STORE_DEREF:
5011 {
5012 PyObject **freevars = (f->f_localsplus +
5013 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005014 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005015 if (PyCell_GET(c) == v) {
5016 PyCell_SET(c, NULL);
5017 Py_DECREF(v);
5018 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005019 break;
5020 }
5021 case STORE_NAME:
5022 {
5023 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005024 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005025 PyObject *locals = f->f_locals;
5026 if (PyDict_CheckExact(locals) &&
5027 PyDict_GetItem(locals, name) == v) {
5028 if (PyDict_DelItem(locals, name) != 0) {
5029 PyErr_Clear();
5030 }
5031 }
5032 break;
5033 }
5034 }
5035 }
5036 res = v;
5037 PyUnicode_Append(&res, w);
5038 return res;
5039}
5040
Guido van Rossum950361c1997-01-24 13:49:28 +00005041#ifdef DYNAMIC_EXECUTION_PROFILE
5042
Skip Montanarof118cb12001-10-15 20:51:38 +00005043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005044getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 int i;
5047 PyObject *l = PyList_New(256);
5048 if (l == NULL) return NULL;
5049 for (i = 0; i < 256; i++) {
5050 PyObject *x = PyLong_FromLong(a[i]);
5051 if (x == NULL) {
5052 Py_DECREF(l);
5053 return NULL;
5054 }
5055 PyList_SetItem(l, i, x);
5056 }
5057 for (i = 0; i < 256; i++)
5058 a[i] = 0;
5059 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005060}
5061
5062PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005063_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005064{
5065#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005067#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 int i;
5069 PyObject *l = PyList_New(257);
5070 if (l == NULL) return NULL;
5071 for (i = 0; i < 257; i++) {
5072 PyObject *x = getarray(dxpairs[i]);
5073 if (x == NULL) {
5074 Py_DECREF(l);
5075 return NULL;
5076 }
5077 PyList_SetItem(l, i, x);
5078 }
5079 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005080#endif
5081}
5082
5083#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005084
5085Py_ssize_t
5086_PyEval_RequestCodeExtraIndex(freefunc free)
5087{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005088 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005089 Py_ssize_t new_index;
5090
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005091 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005092 return -1;
5093 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005094 new_index = interp->co_extra_user_count++;
5095 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005096 return new_index;
5097}
Łukasz Langaa785c872016-09-09 17:37:37 -07005098
5099static void
5100dtrace_function_entry(PyFrameObject *f)
5101{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005102 const char *filename;
5103 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005104 int lineno;
5105
5106 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5107 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5108 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5109
5110 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5111}
5112
5113static void
5114dtrace_function_return(PyFrameObject *f)
5115{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005116 const char *filename;
5117 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005118 int lineno;
5119
5120 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5121 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5122 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5123
5124 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5125}
5126
5127/* DTrace equivalent of maybe_call_line_trace. */
5128static void
5129maybe_dtrace_line(PyFrameObject *frame,
5130 int *instr_lb, int *instr_ub, int *instr_prev)
5131{
5132 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005133 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005134
5135 /* If the last instruction executed isn't in the current
5136 instruction window, reset the window.
5137 */
5138 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5139 PyAddrPair bounds;
5140 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5141 &bounds);
5142 *instr_lb = bounds.ap_lower;
5143 *instr_ub = bounds.ap_upper;
5144 }
5145 /* If the last instruction falls at the start of a line or if
5146 it represents a jump backwards, update the frame's line
5147 number and call the trace function. */
5148 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5149 frame->f_lineno = line;
5150 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5151 if (!co_filename)
5152 co_filename = "?";
5153 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5154 if (!co_name)
5155 co_name = "?";
5156 PyDTrace_LINE(co_filename, co_name, line);
5157 }
5158 *instr_prev = frame->f_lasti;
5159}