blob: 9572918dc3b234106a25860bdf8f031f98006f07 [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"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018
Guido van Rossumc6004111993-11-05 10:22:19 +000019#include <ctype.h>
20
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000022
23#define READ_TIMESTAMP(var)
24
25#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000026
27typedef unsigned long long uint64;
28
David Malcolmf1397ad2011-01-06 17:01:36 +000029/* PowerPC suppport.
30 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
31 "__powerpc__" appears to be the correct one for Linux with GCC
32*/
33#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000141static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000220#ifdef WITH_THREAD
221#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222#else
223#define GIL_REQUEST 0
224#endif
225
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000226/* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000232 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000236#ifdef WITH_THREAD
237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000250#endif
251
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000252/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
259#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274
Guido van Rossume59214e1994-08-30 08:01:59 +0000275#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000276
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000279#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000280#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000282static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000283static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284/* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000286static _Py_atomic_int eval_breaker = {0};
287/* Request for dropping the GIL */
288static _Py_atomic_int gil_drop_request = {0};
289/* Request for running pending calls. */
290static _Py_atomic_int pendingcalls_to_do = {0};
291/* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000294
295#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296
Tim Peters7f468f22004-10-11 02:40:51 +0000297int
298PyEval_ThreadsInitialized(void)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000301}
302
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (gil_created())
307 return;
308 create_gil();
309 take_gil(PyThreadState_GET());
310 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000316_PyEval_FiniThreads(void)
317{
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331}
332
333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* This function must succeed when the current thread state is NULL.
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created());
351 take_gil(tstate);
352 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000366
367/* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371
372void
373PyEval_ReInitThreads(void)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *threading, *result;
376 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (!gil_created())
379 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 recreate_gil();
381 pending_lock = PyThread_allocate_lock();
382 take_gil(tstate);
383 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* Update the threading module with the new state.
386 */
387 tstate = PyThreadState_GET();
388 threading = PyMapping_GetItemString(tstate->interp->modules,
389 "threading");
390 if (threading == NULL) {
391 /* threading not imported */
392 PyErr_Clear();
393 return;
394 }
395 result = PyObject_CallMethod(threading, "_after_fork", NULL);
396 if (result == NULL)
397 PyErr_WriteUnraisable(threading);
398 else
399 Py_DECREF(result);
400 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000401}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000402
403#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000404static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000405static int pending_async_exc = 0;
406#endif /* WITH_THREAD */
407
408/* This function is used to signal that async exceptions are waiting to be
409 raised, therefore it is also useful in non-threaded builds. */
410
411void
412_PyEval_SignalAsyncExc(void)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000415}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000416
Guido van Rossumff4949e1992-08-05 19:58:53 +0000417/* Functions save_thread and restore_thread are always defined so
418 dynamically loaded modules needn't be compiled separately for use
419 with and without threads: */
420
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000421PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000422PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 PyThreadState *tstate = PyThreadState_Swap(NULL);
425 if (tstate == NULL)
426 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000427#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (gil_created())
429 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (tstate == NULL)
438 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000439#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (gil_created()) {
441 int err = errno;
442 take_gil(tstate);
443 errno = err;
444 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000447}
448
449
Guido van Rossuma9672091994-09-14 13:31:22 +0000450/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
451 signal handlers or Mac I/O completion routines) can schedule calls
452 to a function to be called synchronously.
453 The synchronous function is called with one void* argument.
454 It should return 0 for success or -1 for failure -- failure should
455 be accompanied by an exception.
456
457 If registry succeeds, the registry function returns 0; if it fails
458 (e.g. due to too many pending calls) it returns -1 (without setting
459 an exception condition).
460
461 Note that because registry may occur from within signal handlers,
462 or other asynchronous events, calling malloc() is unsafe!
463
464#ifdef WITH_THREAD
465 Any thread can schedule pending calls, but only the main thread
466 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467 There is no facility to schedule calls to a particular thread, but
468 that should be easy to change, should that ever be required. In
469 that case, the static variables here should go into the python
470 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000471#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000473
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474#ifdef WITH_THREAD
475
476/* The WITH_THREAD implementation is thread-safe. It allows
477 scheduling to be made from any thread, and even from an executing
478 callback.
479 */
480
481#define NPENDINGCALLS 32
482static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 int (*func)(void *);
484 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485} pendingcalls[NPENDINGCALLS];
486static int pendingfirst = 0;
487static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488static char pendingbusy = 0;
489
490int
491Py_AddPendingCall(int (*func)(void *), void *arg)
492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 int i, j, result=0;
494 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* try a few times for the lock. Since this mechanism is used
497 * for signal handling (on the main thread), there is a (slim)
498 * chance that a signal is delivered on the same thread while we
499 * hold the lock during the Py_MakePendingCalls() function.
500 * This avoids a deadlock in that case.
501 * Note that signals can be delivered on any thread. In particular,
502 * on Windows, a SIGINT is delivered on a system-created worker
503 * thread.
504 * We also check for lock being NULL, in the unlikely case that
505 * this function is called before any bytecode evaluation takes place.
506 */
507 if (lock != NULL) {
508 for (i = 0; i<100; i++) {
509 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
510 break;
511 }
512 if (i == 100)
513 return -1;
514 }
515
516 i = pendinglast;
517 j = (i + 1) % NPENDINGCALLS;
518 if (j == pendingfirst) {
519 result = -1; /* Queue full */
520 } else {
521 pendingcalls[i].func = func;
522 pendingcalls[i].arg = arg;
523 pendinglast = j;
524 }
525 /* signal main loop */
526 SIGNAL_PENDING_CALLS();
527 if (lock != NULL)
528 PyThread_release_lock(lock);
529 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000530}
531
532int
533Py_MakePendingCalls(void)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 int i;
536 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (!pending_lock) {
539 /* initial allocation of the lock */
540 pending_lock = PyThread_allocate_lock();
541 if (pending_lock == NULL)
542 return -1;
543 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* only service pending calls on main thread */
546 if (main_thread && PyThread_get_thread_ident() != main_thread)
547 return 0;
548 /* don't perform recursive pending calls */
549 if (pendingbusy)
550 return 0;
551 pendingbusy = 1;
552 /* perform a bounded number of calls, in case of recursion */
553 for (i=0; i<NPENDINGCALLS; i++) {
554 int j;
555 int (*func)(void *);
556 void *arg = NULL;
557
558 /* pop one item off the queue while holding the lock */
559 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
560 j = pendingfirst;
561 if (j == pendinglast) {
562 func = NULL; /* Queue empty */
563 } else {
564 func = pendingcalls[j].func;
565 arg = pendingcalls[j].arg;
566 pendingfirst = (j + 1) % NPENDINGCALLS;
567 }
568 if (pendingfirst != pendinglast)
569 SIGNAL_PENDING_CALLS();
570 else
571 UNSIGNAL_PENDING_CALLS();
572 PyThread_release_lock(pending_lock);
573 /* having released the lock, perform the callback */
574 if (func == NULL)
575 break;
576 r = func(arg);
577 if (r)
578 break;
579 }
580 pendingbusy = 0;
581 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000582}
583
584#else /* if ! defined WITH_THREAD */
585
586/*
587 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
588 This code is used for signal handling in python that isn't built
589 with WITH_THREAD.
590 Don't use this implementation when Py_AddPendingCalls() can happen
591 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592
Guido van Rossuma9672091994-09-14 13:31:22 +0000593 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000594 (1) nested asynchronous calls to Py_AddPendingCall()
595 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000597 (1) is very unlikely because typically signal delivery
598 is blocked during signal handling. So it should be impossible.
599 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 The current code is safe against (2), but not against (1).
601 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000602 thread is present, interrupted by signals, and that the critical
603 section is protected with the "busy" variable. On Windows, which
604 delivers SIGINT on a system thread, this does not hold and therefore
605 Windows really shouldn't use this version.
606 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000607*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000608
Guido van Rossuma9672091994-09-14 13:31:22 +0000609#define NPENDINGCALLS 32
610static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 int (*func)(void *);
612 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000613} pendingcalls[NPENDINGCALLS];
614static volatile int pendingfirst = 0;
615static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000616static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000617
618int
Thomas Wouters334fb892000-07-25 12:56:38 +0000619Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 static volatile int busy = 0;
622 int i, j;
623 /* XXX Begin critical section */
624 if (busy)
625 return -1;
626 busy = 1;
627 i = pendinglast;
628 j = (i + 1) % NPENDINGCALLS;
629 if (j == pendingfirst) {
630 busy = 0;
631 return -1; /* Queue full */
632 }
633 pendingcalls[i].func = func;
634 pendingcalls[i].arg = arg;
635 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 SIGNAL_PENDING_CALLS();
638 busy = 0;
639 /* XXX End critical section */
640 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000641}
642
Guido van Rossum180d7b41994-09-29 09:45:57 +0000643int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000644Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 static int busy = 0;
647 if (busy)
648 return 0;
649 busy = 1;
650 UNSIGNAL_PENDING_CALLS();
651 for (;;) {
652 int i;
653 int (*func)(void *);
654 void *arg;
655 i = pendingfirst;
656 if (i == pendinglast)
657 break; /* Queue empty */
658 func = pendingcalls[i].func;
659 arg = pendingcalls[i].arg;
660 pendingfirst = (i + 1) % NPENDINGCALLS;
661 if (func(arg) < 0) {
662 busy = 0;
663 SIGNAL_PENDING_CALLS(); /* We're not done yet */
664 return -1;
665 }
666 }
667 busy = 0;
668 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000669}
670
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000671#endif /* WITH_THREAD */
672
Guido van Rossuma9672091994-09-14 13:31:22 +0000673
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000674/* The interpreter's recursion limit */
675
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000676#ifndef Py_DEFAULT_RECURSION_LIMIT
677#define Py_DEFAULT_RECURSION_LIMIT 1000
678#endif
679static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
680int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000682int
683Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686}
687
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000688void
689Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 recursion_limit = new_limit;
692 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Armin Rigo2b3eb402003-10-28 12:05:48 +0000695/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
696 if the recursion_depth reaches _Py_CheckRecursionLimit.
697 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
698 to guarantee that _Py_CheckRecursiveCall() is regularly called.
699 Without USE_STACKCHECK, there is no need for this. */
700int
701_Py_CheckRecursiveCall(char *where)
702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000704
705#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (PyOS_CheckStack()) {
707 --tstate->recursion_depth;
708 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
709 return -1;
710 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 _Py_CheckRecursionLimit = recursion_limit;
713 if (tstate->recursion_critical)
714 /* Somebody asked that we don't check for recursion. */
715 return 0;
716 if (tstate->overflowed) {
717 if (tstate->recursion_depth > recursion_limit + 50) {
718 /* Overflowing while handling an overflow. Give up. */
719 Py_FatalError("Cannot recover from stack overflow.");
720 }
721 return 0;
722 }
723 if (tstate->recursion_depth > recursion_limit) {
724 --tstate->recursion_depth;
725 tstate->overflowed = 1;
726 PyErr_Format(PyExc_RuntimeError,
727 "maximum recursion depth exceeded%s",
728 where);
729 return -1;
730 }
731 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000732}
733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000735enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000736 WHY_NOT = 0x0001, /* No error */
737 WHY_EXCEPTION = 0x0002, /* Exception occurred */
738 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
739 WHY_RETURN = 0x0008, /* 'return' statement */
740 WHY_BREAK = 0x0010, /* 'break' statement */
741 WHY_CONTINUE = 0x0020, /* 'continue' statement */
742 WHY_YIELD = 0x0040, /* 'yield' operator */
743 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000744};
Guido van Rossum374a9221991-04-04 10:40:29 +0000745
Collin Winter828f04a2007-08-31 00:04:24 +0000746static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000747static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000748
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000749/* Records whether tracing is on for any thread. Counts the number of
750 threads for which tstate->c_tracefunc is non-NULL, so if the value
751 is 0, we know we don't have to check this thread's c_tracefunc.
752 This speeds up the if statement in PyEval_EvalFrameEx() after
753 fast_next_opcode*/
754static int _Py_TracingPossible = 0;
755
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000756
Guido van Rossum374a9221991-04-04 10:40:29 +0000757
Guido van Rossumb209a111997-04-29 18:18:01 +0000758PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000759PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return PyEval_EvalCodeEx(co,
762 globals, locals,
763 (PyObject **)NULL, 0,
764 (PyObject **)NULL, 0,
765 (PyObject **)NULL, 0,
766 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000767}
768
769
770/* Interpreter main loop */
771
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000772PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000773PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* This is for backward compatibility with extension modules that
775 used this API; core interpreter code should call
776 PyEval_EvalFrameEx() */
777 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000778}
779
780PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000782{
Guido van Rossum950361c1997-01-24 13:49:28 +0000783#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 register PyObject **stack_pointer; /* Next free slot in value stack */
787 register unsigned char *next_instr;
788 register int opcode; /* Current opcode */
789 register int oparg; /* Current opcode argument, if any */
790 register enum why_code why; /* Reason for block stack unwind */
791 register int err; /* Error status -- nonzero if error */
792 register PyObject *x; /* Result object -- NULL if error */
793 register PyObject *v; /* Temporary objects popped off stack */
794 register PyObject *w;
795 register PyObject *u;
796 register PyObject *t;
797 register PyObject **fastlocals, **freevars;
798 PyObject *retval = NULL; /* Return value */
799 PyThreadState *tstate = PyThreadState_GET();
800 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 is true when the line being executed has changed. The
807 initial values are such as to make this false the first
808 time it is tested. */
809 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 unsigned char *first_instr;
812 PyObject *names;
813 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000814
Antoine Pitroub52ec782009-01-25 16:34:23 +0000815/* Computed GOTOs, or
816 the-optimization-commonly-but-improperly-known-as-"threaded code"
817 using gcc's labels-as-values extension
818 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
819
820 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822 combined with a lookup table of jump addresses. However, since the
823 indirect jump instruction is shared by all opcodes, the CPU will have a
824 hard time making the right prediction for where to jump next (actually,
825 it will be always wrong except in the uncommon case of a sequence of
826 several identical opcodes).
827
828 "Threaded code" in contrast, uses an explicit jump table and an explicit
829 indirect jump instruction at the end of each opcode. Since the jump
830 instruction is at a different address for each opcode, the CPU will make a
831 separate prediction for each of these instructions, which is equivalent to
832 predicting the second opcode of each opcode pair. These predictions have
833 a much better chance to turn out valid, especially in small bytecode loops.
834
835 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837 and potentially many more instructions (depending on the pipeline width).
838 A correctly predicted branch, however, is nearly free.
839
840 At the time of this writing, the "threaded code" version is up to 15-20%
841 faster than the normal "switch" version, depending on the compiler and the
842 CPU architecture.
843
844 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
845 because it would render the measurements invalid.
846
847
848 NOTE: care must be taken that the compiler doesn't try to "optimize" the
849 indirect jumps by sharing them between all opcodes. Such optimizations
850 can be disabled on gcc by using the -fno-gcse flag (or possibly
851 -fno-crossjumping).
852*/
853
Antoine Pitrou042b1282010-08-13 21:15:58 +0000854#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000855#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000856#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000857#endif
858
Antoine Pitrou042b1282010-08-13 21:15:58 +0000859#ifdef HAVE_COMPUTED_GOTOS
860 #ifndef USE_COMPUTED_GOTOS
861 #define USE_COMPUTED_GOTOS 1
862 #endif
863#else
864 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
865 #error "Computed gotos are not supported on this compiler."
866 #endif
867 #undef USE_COMPUTED_GOTOS
868 #define USE_COMPUTED_GOTOS 0
869#endif
870
871#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872/* Import the static jump table */
873#include "opcode_targets.h"
874
875/* This macro is used when several opcodes defer to the same implementation
876 (e.g. SETUP_LOOP, SETUP_FINALLY) */
877#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 TARGET_##op: \
879 opcode = op; \
880 if (HAS_ARG(op)) \
881 oparg = NEXTARG(); \
882 case op: \
883 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000884
885#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 TARGET_##op: \
887 opcode = op; \
888 if (HAS_ARG(op)) \
889 oparg = NEXTARG(); \
890 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000891
892
893#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 { \
895 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
896 FAST_DISPATCH(); \
897 } \
898 continue; \
899 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900
901#ifdef LLTRACE
902#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 { \
904 if (!lltrace && !_Py_TracingPossible) { \
905 f->f_lasti = INSTR_OFFSET(); \
906 goto *opcode_targets[*next_instr++]; \
907 } \
908 goto fast_next_opcode; \
909 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910#else
911#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 { \
913 if (!_Py_TracingPossible) { \
914 f->f_lasti = INSTR_OFFSET(); \
915 goto *opcode_targets[*next_instr++]; \
916 } \
917 goto fast_next_opcode; \
918 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919#endif
920
921#else
922#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 /* silence compiler warnings about `impl` unused */ \
926 if (0) goto impl; \
927 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000928#define DISPATCH() continue
929#define FAST_DISPATCH() goto fast_next_opcode
930#endif
931
932
Neal Norwitza81d2202002-07-14 00:27:26 +0000933/* Tuple access macros */
934
935#ifndef Py_DEBUG
936#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
937#else
938#define GETITEM(v, i) PyTuple_GetItem((v), (i))
939#endif
940
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000941#ifdef WITH_TSC
942/* Use Pentium timestamp counter to mark certain events:
943 inst0 -- beginning of switch statement for opcode dispatch
944 inst1 -- end of switch statement (may be skipped)
945 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000947 (may be skipped)
948 intr1 -- beginning of long interruption
949 intr2 -- end of long interruption
950
951 Many opcodes call out to helper C functions. In some cases, the
952 time in those functions should be counted towards the time for the
953 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
954 calls another Python function; there's no point in charge all the
955 bytecode executed by the called function to the caller.
956
957 It's hard to make a useful judgement statically. In the presence
958 of operator overloading, it's impossible to tell if a call will
959 execute new Python code or not.
960
961 It's a case-by-case judgement. I'll use intr1 for the following
962 cases:
963
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000964 IMPORT_STAR
965 IMPORT_FROM
966 CALL_FUNCTION (and friends)
967
968 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
970 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 READ_TIMESTAMP(inst0);
973 READ_TIMESTAMP(inst1);
974 READ_TIMESTAMP(loop0);
975 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* shut up the compiler */
978 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000979#endif
980
Guido van Rossum374a9221991-04-04 10:40:29 +0000981/* Code access macros */
982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983#define INSTR_OFFSET() ((int)(next_instr - first_instr))
984#define NEXTOP() (*next_instr++)
985#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
986#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
987#define JUMPTO(x) (next_instr = first_instr + (x))
988#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000989
Raymond Hettingerf606f872003-03-16 03:11:04 +0000990/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 Some opcodes tend to come in pairs thus making it possible to
992 predict the second code when the first is run. For example,
993 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
994 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 Verifying the prediction costs a single high-speed test of a register
997 variable against a constant. If the pairing was good, then the
998 processor's own internal branch predication has a high likelihood of
999 success, resulting in a nearly zero-overhead transition to the
1000 next opcode. A successful prediction saves a trip through the eval-loop
1001 including its two unpredictable branches, the HAS_ARG test and the
1002 switch-case. Combined with the processor's internal branch prediction,
1003 a successful PREDICT has the effect of making the two opcodes run as if
1004 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005
Georg Brandl86b2fb92008-07-16 03:43:04 +00001006 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 predictions turned-on and interpret the results as if some opcodes
1008 had been combined or turn-off predictions so that the opcode frequency
1009 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001010
1011 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 the CPU to record separate branch prediction information for each
1013 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001014
Raymond Hettingerf606f872003-03-16 03:11:04 +00001015*/
1016
Antoine Pitrou042b1282010-08-13 21:15:58 +00001017#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018#define PREDICT(op) if (0) goto PRED_##op
1019#define PREDICTED(op) PRED_##op:
1020#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001021#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1023#define PREDICTED(op) PRED_##op: next_instr++
1024#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001025#endif
1026
Raymond Hettingerf606f872003-03-16 03:11:04 +00001027
Guido van Rossum374a9221991-04-04 10:40:29 +00001028/* Stack manipulation macros */
1029
Martin v. Löwis18e16552006-02-15 17:27:45 +00001030/* The stack can grow at most MAXINT deep, as co_nlocals and
1031 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001032#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1033#define EMPTY() (STACK_LEVEL() == 0)
1034#define TOP() (stack_pointer[-1])
1035#define SECOND() (stack_pointer[-2])
1036#define THIRD() (stack_pointer[-3])
1037#define FOURTH() (stack_pointer[-4])
1038#define PEEK(n) (stack_pointer[-(n)])
1039#define SET_TOP(v) (stack_pointer[-1] = (v))
1040#define SET_SECOND(v) (stack_pointer[-2] = (v))
1041#define SET_THIRD(v) (stack_pointer[-3] = (v))
1042#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1043#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1044#define BASIC_STACKADJ(n) (stack_pointer += n)
1045#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1046#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001047
Guido van Rossum96a42c81992-01-12 02:29:51 +00001048#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001050 lltrace && prtrace(TOP(), "push")); \
1051 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001053 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001055 lltrace && prtrace(TOP(), "stackadj")); \
1056 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001057#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001058 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1059 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001060#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001061#define PUSH(v) BASIC_PUSH(v)
1062#define POP() BASIC_POP()
1063#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001064#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001065#endif
1066
Guido van Rossum681d79a1995-07-18 14:51:37 +00001067/* Local variable macros */
1068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001070
1071/* The SETLOCAL() macro must not DECREF the local variable in-place and
1072 then store the new value; it must copy the old value to a temporary
1073 value, then store the new value, and then DECREF the temporary value.
1074 This is because it is possible that during the DECREF the frame is
1075 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1076 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001078 GETLOCAL(i) = value; \
1079 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001080
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001081
1082#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 while (STACK_LEVEL() > (b)->b_level) { \
1084 PyObject *v = POP(); \
1085 Py_XDECREF(v); \
1086 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001087
1088#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 { \
1090 PyObject *type, *value, *traceback; \
1091 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1092 while (STACK_LEVEL() > (b)->b_level + 3) { \
1093 value = POP(); \
1094 Py_XDECREF(value); \
1095 } \
1096 type = tstate->exc_type; \
1097 value = tstate->exc_value; \
1098 traceback = tstate->exc_traceback; \
1099 tstate->exc_type = POP(); \
1100 tstate->exc_value = POP(); \
1101 tstate->exc_traceback = POP(); \
1102 Py_XDECREF(type); \
1103 Py_XDECREF(value); \
1104 Py_XDECREF(traceback); \
1105 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001106
1107#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 { \
1109 PyObject *type, *value, *traceback; \
1110 Py_XINCREF(tstate->exc_type); \
1111 Py_XINCREF(tstate->exc_value); \
1112 Py_XINCREF(tstate->exc_traceback); \
1113 type = f->f_exc_type; \
1114 value = f->f_exc_value; \
1115 traceback = f->f_exc_traceback; \
1116 f->f_exc_type = tstate->exc_type; \
1117 f->f_exc_value = tstate->exc_value; \
1118 f->f_exc_traceback = tstate->exc_traceback; \
1119 Py_XDECREF(type); \
1120 Py_XDECREF(value); \
1121 Py_XDECREF(traceback); \
1122 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001123
1124#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 { \
1126 PyObject *tmp; \
1127 tmp = tstate->exc_type; \
1128 tstate->exc_type = f->f_exc_type; \
1129 f->f_exc_type = tmp; \
1130 tmp = tstate->exc_value; \
1131 tstate->exc_value = f->f_exc_value; \
1132 f->f_exc_value = tmp; \
1133 tmp = tstate->exc_traceback; \
1134 tstate->exc_traceback = f->f_exc_traceback; \
1135 f->f_exc_traceback = tmp; \
1136 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001137
Guido van Rossuma027efa1997-05-05 20:56:21 +00001138/* Start of code */
1139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (f == NULL)
1141 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 /* push frame */
1144 if (Py_EnterRecursiveCall(""))
1145 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (tstate->use_tracing) {
1150 if (tstate->c_tracefunc != NULL) {
1151 /* tstate->c_tracefunc, if defined, is a
1152 function that will be called on *every* entry
1153 to a code block. Its return value, if not
1154 None, is a function that will be called at
1155 the start of each executed line of code.
1156 (Actually, the function must return itself
1157 in order to continue tracing.) The trace
1158 functions are called with three arguments:
1159 a pointer to the current frame, a string
1160 indicating why the function is called, and
1161 an argument which depends on the situation.
1162 The global trace function is also called
1163 whenever an exception is detected. */
1164 if (call_trace_protected(tstate->c_tracefunc,
1165 tstate->c_traceobj,
1166 f, PyTrace_CALL, Py_None)) {
1167 /* Trace function raised an error */
1168 goto exit_eval_frame;
1169 }
1170 }
1171 if (tstate->c_profilefunc != NULL) {
1172 /* Similar for c_profilefunc, except it needn't
1173 return itself and isn't called for "line" events */
1174 if (call_trace_protected(tstate->c_profilefunc,
1175 tstate->c_profileobj,
1176 f, PyTrace_CALL, Py_None)) {
1177 /* Profile function raised an error */
1178 goto exit_eval_frame;
1179 }
1180 }
1181 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 co = f->f_code;
1184 names = co->co_names;
1185 consts = co->co_consts;
1186 fastlocals = f->f_localsplus;
1187 freevars = f->f_localsplus + co->co_nlocals;
1188 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1189 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 f->f_lasti now refers to the index of the last instruction
1192 executed. You might think this was obvious from the name, but
1193 this wasn't always true before 2.3! PyFrame_New now sets
1194 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1195 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1196 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 When the PREDICT() macros are enabled, some opcode pairs follow in
1199 direct succession without updating f->f_lasti. A successful
1200 prediction effectively links the two codes together as if they
1201 were a single new opcode; accordingly,f->f_lasti will point to
1202 the first code in the pair (for instance, GET_ITER followed by
1203 FOR_ITER is effectively a single opcode and f->f_lasti will point
1204 at to the beginning of the combined pair.)
1205 */
1206 next_instr = first_instr + f->f_lasti + 1;
1207 stack_pointer = f->f_stacktop;
1208 assert(stack_pointer != NULL);
1209 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (co->co_flags & CO_GENERATOR && !throwflag) {
1212 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1213 /* We were in an except handler when we left,
1214 restore the exception state which was put aside
1215 (see YIELD_VALUE). */
1216 SWAP_EXC_STATE();
1217 }
1218 else {
1219 SAVE_EXC_STATE();
1220 }
1221 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001222
Tim Peters5ca576e2001-06-18 22:08:13 +00001223#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001225#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 why = WHY_NOT;
1228 err = 0;
1229 x = Py_None; /* Not a reference, just anything non-NULL */
1230 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 if (throwflag) { /* support for generator.throw() */
1233 why = WHY_EXCEPTION;
1234 goto on_error;
1235 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001238#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (inst1 == 0) {
1240 /* Almost surely, the opcode executed a break
1241 or a continue, preventing inst1 from being set
1242 on the way out of the loop.
1243 */
1244 READ_TIMESTAMP(inst1);
1245 loop1 = inst1;
1246 }
1247 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1248 intr0, intr1);
1249 ticked = 0;
1250 inst1 = 0;
1251 intr0 = 0;
1252 intr1 = 0;
1253 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1256 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 /* Do periodic things. Doing this every time through
1259 the loop would add too much overhead, so we do it
1260 only every Nth instruction. We also do it if
1261 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1262 event needs attention (e.g. a signal handler or
1263 async I/O handler); see Py_AddPendingCall() and
1264 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1267 if (*next_instr == SETUP_FINALLY) {
1268 /* Make the last opcode before
1269 a try: finally: block uninterruptable. */
1270 goto fast_next_opcode;
1271 }
1272 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001273#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1277 if (Py_MakePendingCalls() < 0) {
1278 why = WHY_EXCEPTION;
1279 goto on_error;
1280 }
1281 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001282#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001283 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* Give another thread a chance */
1285 if (PyThreadState_Swap(NULL) != tstate)
1286 Py_FatalError("ceval: tstate mix-up");
1287 drop_gil(tstate);
1288
1289 /* Other threads may run now */
1290
1291 take_gil(tstate);
1292 if (PyThreadState_Swap(tstate) != NULL)
1293 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001295#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 /* Check for asynchronous exceptions. */
1297 if (tstate->async_exc != NULL) {
1298 x = tstate->async_exc;
1299 tstate->async_exc = NULL;
1300 UNSIGNAL_ASYNC_EXC();
1301 PyErr_SetNone(x);
1302 Py_DECREF(x);
1303 why = WHY_EXCEPTION;
1304 goto on_error;
1305 }
1306 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 fast_next_opcode:
1309 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if (_Py_TracingPossible &&
1314 tstate->c_tracefunc != NULL && !tstate->tracing) {
1315 /* see maybe_call_line_trace
1316 for expository comments */
1317 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 err = maybe_call_line_trace(tstate->c_tracefunc,
1320 tstate->c_traceobj,
1321 f, &instr_lb, &instr_ub,
1322 &instr_prev);
1323 /* Reload possibly changed frame fields */
1324 JUMPTO(f->f_lasti);
1325 if (f->f_stacktop != NULL) {
1326 stack_pointer = f->f_stacktop;
1327 f->f_stacktop = NULL;
1328 }
1329 if (err) {
1330 /* trace function raised an exception */
1331 goto on_error;
1332 }
1333 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 opcode = NEXTOP();
1338 oparg = 0; /* allows oparg to be stored in a register because
1339 it doesn't have to be remembered across a full loop */
1340 if (HAS_ARG(opcode))
1341 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001342 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001343#ifdef DYNAMIC_EXECUTION_PROFILE
1344#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 dxpairs[lastopcode][opcode]++;
1346 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001347#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001349#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001350
Guido van Rossum96a42c81992-01-12 02:29:51 +00001351#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (lltrace) {
1355 if (HAS_ARG(opcode)) {
1356 printf("%d: %d, %d\n",
1357 f->f_lasti, opcode, oparg);
1358 }
1359 else {
1360 printf("%d: %d\n",
1361 f->f_lasti, opcode);
1362 }
1363 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001364#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 /* Main switch on opcode */
1367 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 /* BEWARE!
1372 It is essential that any operation that fails sets either
1373 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1374 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 TARGET(NOP)
1379 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 TARGET(LOAD_FAST)
1382 x = GETLOCAL(oparg);
1383 if (x != NULL) {
1384 Py_INCREF(x);
1385 PUSH(x);
1386 FAST_DISPATCH();
1387 }
1388 format_exc_check_arg(PyExc_UnboundLocalError,
1389 UNBOUNDLOCAL_ERROR_MSG,
1390 PyTuple_GetItem(co->co_varnames, oparg));
1391 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 TARGET(LOAD_CONST)
1394 x = GETITEM(consts, oparg);
1395 Py_INCREF(x);
1396 PUSH(x);
1397 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 PREDICTED_WITH_ARG(STORE_FAST);
1400 TARGET(STORE_FAST)
1401 v = POP();
1402 SETLOCAL(oparg, v);
1403 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 TARGET(POP_TOP)
1406 v = POP();
1407 Py_DECREF(v);
1408 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 TARGET(ROT_TWO)
1411 v = TOP();
1412 w = SECOND();
1413 SET_TOP(w);
1414 SET_SECOND(v);
1415 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 TARGET(ROT_THREE)
1418 v = TOP();
1419 w = SECOND();
1420 x = THIRD();
1421 SET_TOP(w);
1422 SET_SECOND(x);
1423 SET_THIRD(v);
1424 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 TARGET(DUP_TOP)
1427 v = TOP();
1428 Py_INCREF(v);
1429 PUSH(v);
1430 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001432 TARGET(DUP_TOP_TWO)
1433 x = TOP();
1434 Py_INCREF(x);
1435 w = SECOND();
1436 Py_INCREF(w);
1437 STACKADJ(2);
1438 SET_TOP(x);
1439 SET_SECOND(w);
1440 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 TARGET(UNARY_POSITIVE)
1443 v = TOP();
1444 x = PyNumber_Positive(v);
1445 Py_DECREF(v);
1446 SET_TOP(x);
1447 if (x != NULL) DISPATCH();
1448 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 TARGET(UNARY_NEGATIVE)
1451 v = TOP();
1452 x = PyNumber_Negative(v);
1453 Py_DECREF(v);
1454 SET_TOP(x);
1455 if (x != NULL) DISPATCH();
1456 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 TARGET(UNARY_NOT)
1459 v = TOP();
1460 err = PyObject_IsTrue(v);
1461 Py_DECREF(v);
1462 if (err == 0) {
1463 Py_INCREF(Py_True);
1464 SET_TOP(Py_True);
1465 DISPATCH();
1466 }
1467 else if (err > 0) {
1468 Py_INCREF(Py_False);
1469 SET_TOP(Py_False);
1470 err = 0;
1471 DISPATCH();
1472 }
1473 STACKADJ(-1);
1474 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 TARGET(UNARY_INVERT)
1477 v = TOP();
1478 x = PyNumber_Invert(v);
1479 Py_DECREF(v);
1480 SET_TOP(x);
1481 if (x != NULL) DISPATCH();
1482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 TARGET(BINARY_POWER)
1485 w = POP();
1486 v = TOP();
1487 x = PyNumber_Power(v, w, Py_None);
1488 Py_DECREF(v);
1489 Py_DECREF(w);
1490 SET_TOP(x);
1491 if (x != NULL) DISPATCH();
1492 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 TARGET(BINARY_MULTIPLY)
1495 w = POP();
1496 v = TOP();
1497 x = PyNumber_Multiply(v, w);
1498 Py_DECREF(v);
1499 Py_DECREF(w);
1500 SET_TOP(x);
1501 if (x != NULL) DISPATCH();
1502 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 TARGET(BINARY_TRUE_DIVIDE)
1505 w = POP();
1506 v = TOP();
1507 x = PyNumber_TrueDivide(v, w);
1508 Py_DECREF(v);
1509 Py_DECREF(w);
1510 SET_TOP(x);
1511 if (x != NULL) DISPATCH();
1512 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 TARGET(BINARY_FLOOR_DIVIDE)
1515 w = POP();
1516 v = TOP();
1517 x = PyNumber_FloorDivide(v, w);
1518 Py_DECREF(v);
1519 Py_DECREF(w);
1520 SET_TOP(x);
1521 if (x != NULL) DISPATCH();
1522 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 TARGET(BINARY_MODULO)
1525 w = POP();
1526 v = TOP();
1527 if (PyUnicode_CheckExact(v))
1528 x = PyUnicode_Format(v, w);
1529 else
1530 x = PyNumber_Remainder(v, w);
1531 Py_DECREF(v);
1532 Py_DECREF(w);
1533 SET_TOP(x);
1534 if (x != NULL) DISPATCH();
1535 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 TARGET(BINARY_ADD)
1538 w = POP();
1539 v = TOP();
1540 if (PyUnicode_CheckExact(v) &&
1541 PyUnicode_CheckExact(w)) {
1542 x = unicode_concatenate(v, w, f, next_instr);
1543 /* unicode_concatenate consumed the ref to v */
1544 goto skip_decref_vx;
1545 }
1546 else {
1547 x = PyNumber_Add(v, w);
1548 }
1549 Py_DECREF(v);
1550 skip_decref_vx:
1551 Py_DECREF(w);
1552 SET_TOP(x);
1553 if (x != NULL) DISPATCH();
1554 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 TARGET(BINARY_SUBTRACT)
1557 w = POP();
1558 v = TOP();
1559 x = PyNumber_Subtract(v, w);
1560 Py_DECREF(v);
1561 Py_DECREF(w);
1562 SET_TOP(x);
1563 if (x != NULL) DISPATCH();
1564 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 TARGET(BINARY_SUBSCR)
1567 w = POP();
1568 v = TOP();
1569 x = PyObject_GetItem(v, w);
1570 Py_DECREF(v);
1571 Py_DECREF(w);
1572 SET_TOP(x);
1573 if (x != NULL) DISPATCH();
1574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 TARGET(BINARY_LSHIFT)
1577 w = POP();
1578 v = TOP();
1579 x = PyNumber_Lshift(v, w);
1580 Py_DECREF(v);
1581 Py_DECREF(w);
1582 SET_TOP(x);
1583 if (x != NULL) DISPATCH();
1584 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 TARGET(BINARY_RSHIFT)
1587 w = POP();
1588 v = TOP();
1589 x = PyNumber_Rshift(v, w);
1590 Py_DECREF(v);
1591 Py_DECREF(w);
1592 SET_TOP(x);
1593 if (x != NULL) DISPATCH();
1594 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 TARGET(BINARY_AND)
1597 w = POP();
1598 v = TOP();
1599 x = PyNumber_And(v, w);
1600 Py_DECREF(v);
1601 Py_DECREF(w);
1602 SET_TOP(x);
1603 if (x != NULL) DISPATCH();
1604 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 TARGET(BINARY_XOR)
1607 w = POP();
1608 v = TOP();
1609 x = PyNumber_Xor(v, w);
1610 Py_DECREF(v);
1611 Py_DECREF(w);
1612 SET_TOP(x);
1613 if (x != NULL) DISPATCH();
1614 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 TARGET(BINARY_OR)
1617 w = POP();
1618 v = TOP();
1619 x = PyNumber_Or(v, w);
1620 Py_DECREF(v);
1621 Py_DECREF(w);
1622 SET_TOP(x);
1623 if (x != NULL) DISPATCH();
1624 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 TARGET(LIST_APPEND)
1627 w = POP();
1628 v = PEEK(oparg);
1629 err = PyList_Append(v, w);
1630 Py_DECREF(w);
1631 if (err == 0) {
1632 PREDICT(JUMP_ABSOLUTE);
1633 DISPATCH();
1634 }
1635 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 TARGET(SET_ADD)
1638 w = POP();
1639 v = stack_pointer[-oparg];
1640 err = PySet_Add(v, w);
1641 Py_DECREF(w);
1642 if (err == 0) {
1643 PREDICT(JUMP_ABSOLUTE);
1644 DISPATCH();
1645 }
1646 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 TARGET(INPLACE_POWER)
1649 w = POP();
1650 v = TOP();
1651 x = PyNumber_InPlacePower(v, w, Py_None);
1652 Py_DECREF(v);
1653 Py_DECREF(w);
1654 SET_TOP(x);
1655 if (x != NULL) DISPATCH();
1656 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 TARGET(INPLACE_MULTIPLY)
1659 w = POP();
1660 v = TOP();
1661 x = PyNumber_InPlaceMultiply(v, w);
1662 Py_DECREF(v);
1663 Py_DECREF(w);
1664 SET_TOP(x);
1665 if (x != NULL) DISPATCH();
1666 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 TARGET(INPLACE_TRUE_DIVIDE)
1669 w = POP();
1670 v = TOP();
1671 x = PyNumber_InPlaceTrueDivide(v, w);
1672 Py_DECREF(v);
1673 Py_DECREF(w);
1674 SET_TOP(x);
1675 if (x != NULL) DISPATCH();
1676 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 TARGET(INPLACE_FLOOR_DIVIDE)
1679 w = POP();
1680 v = TOP();
1681 x = PyNumber_InPlaceFloorDivide(v, w);
1682 Py_DECREF(v);
1683 Py_DECREF(w);
1684 SET_TOP(x);
1685 if (x != NULL) DISPATCH();
1686 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 TARGET(INPLACE_MODULO)
1689 w = POP();
1690 v = TOP();
1691 x = PyNumber_InPlaceRemainder(v, w);
1692 Py_DECREF(v);
1693 Py_DECREF(w);
1694 SET_TOP(x);
1695 if (x != NULL) DISPATCH();
1696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 TARGET(INPLACE_ADD)
1699 w = POP();
1700 v = TOP();
1701 if (PyUnicode_CheckExact(v) &&
1702 PyUnicode_CheckExact(w)) {
1703 x = unicode_concatenate(v, w, f, next_instr);
1704 /* unicode_concatenate consumed the ref to v */
1705 goto skip_decref_v;
1706 }
1707 else {
1708 x = PyNumber_InPlaceAdd(v, w);
1709 }
1710 Py_DECREF(v);
1711 skip_decref_v:
1712 Py_DECREF(w);
1713 SET_TOP(x);
1714 if (x != NULL) DISPATCH();
1715 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 TARGET(INPLACE_SUBTRACT)
1718 w = POP();
1719 v = TOP();
1720 x = PyNumber_InPlaceSubtract(v, w);
1721 Py_DECREF(v);
1722 Py_DECREF(w);
1723 SET_TOP(x);
1724 if (x != NULL) DISPATCH();
1725 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 TARGET(INPLACE_LSHIFT)
1728 w = POP();
1729 v = TOP();
1730 x = PyNumber_InPlaceLshift(v, w);
1731 Py_DECREF(v);
1732 Py_DECREF(w);
1733 SET_TOP(x);
1734 if (x != NULL) DISPATCH();
1735 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 TARGET(INPLACE_RSHIFT)
1738 w = POP();
1739 v = TOP();
1740 x = PyNumber_InPlaceRshift(v, w);
1741 Py_DECREF(v);
1742 Py_DECREF(w);
1743 SET_TOP(x);
1744 if (x != NULL) DISPATCH();
1745 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 TARGET(INPLACE_AND)
1748 w = POP();
1749 v = TOP();
1750 x = PyNumber_InPlaceAnd(v, w);
1751 Py_DECREF(v);
1752 Py_DECREF(w);
1753 SET_TOP(x);
1754 if (x != NULL) DISPATCH();
1755 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 TARGET(INPLACE_XOR)
1758 w = POP();
1759 v = TOP();
1760 x = PyNumber_InPlaceXor(v, w);
1761 Py_DECREF(v);
1762 Py_DECREF(w);
1763 SET_TOP(x);
1764 if (x != NULL) DISPATCH();
1765 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 TARGET(INPLACE_OR)
1768 w = POP();
1769 v = TOP();
1770 x = PyNumber_InPlaceOr(v, w);
1771 Py_DECREF(v);
1772 Py_DECREF(w);
1773 SET_TOP(x);
1774 if (x != NULL) DISPATCH();
1775 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 TARGET(STORE_SUBSCR)
1778 w = TOP();
1779 v = SECOND();
1780 u = THIRD();
1781 STACKADJ(-3);
1782 /* v[w] = u */
1783 err = PyObject_SetItem(v, w, u);
1784 Py_DECREF(u);
1785 Py_DECREF(v);
1786 Py_DECREF(w);
1787 if (err == 0) DISPATCH();
1788 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 TARGET(DELETE_SUBSCR)
1791 w = TOP();
1792 v = SECOND();
1793 STACKADJ(-2);
1794 /* del v[w] */
1795 err = PyObject_DelItem(v, w);
1796 Py_DECREF(v);
1797 Py_DECREF(w);
1798 if (err == 0) DISPATCH();
1799 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 TARGET(PRINT_EXPR)
1802 v = POP();
1803 w = PySys_GetObject("displayhook");
1804 if (w == NULL) {
1805 PyErr_SetString(PyExc_RuntimeError,
1806 "lost sys.displayhook");
1807 err = -1;
1808 x = NULL;
1809 }
1810 if (err == 0) {
1811 x = PyTuple_Pack(1, v);
1812 if (x == NULL)
1813 err = -1;
1814 }
1815 if (err == 0) {
1816 w = PyEval_CallObject(w, x);
1817 Py_XDECREF(w);
1818 if (w == NULL)
1819 err = -1;
1820 }
1821 Py_DECREF(v);
1822 Py_XDECREF(x);
1823 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001824
Thomas Wouters434d0822000-08-24 20:11:32 +00001825#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001827#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 TARGET(RAISE_VARARGS)
1829 v = w = NULL;
1830 switch (oparg) {
1831 case 2:
1832 v = POP(); /* cause */
1833 case 1:
1834 w = POP(); /* exc */
1835 case 0: /* Fallthrough */
1836 why = do_raise(w, v);
1837 break;
1838 default:
1839 PyErr_SetString(PyExc_SystemError,
1840 "bad RAISE_VARARGS oparg");
1841 why = WHY_EXCEPTION;
1842 break;
1843 }
1844 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 TARGET(STORE_LOCALS)
1847 x = POP();
1848 v = f->f_locals;
1849 Py_XDECREF(v);
1850 f->f_locals = x;
1851 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 TARGET(RETURN_VALUE)
1854 retval = POP();
1855 why = WHY_RETURN;
1856 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 TARGET(YIELD_VALUE)
1859 retval = POP();
1860 f->f_stacktop = stack_pointer;
1861 why = WHY_YIELD;
1862 /* Put aside the current exception state and restore
1863 that of the calling frame. This only serves when
1864 "yield" is used inside an except handler. */
1865 SWAP_EXC_STATE();
1866 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 TARGET(POP_EXCEPT)
1869 {
1870 PyTryBlock *b = PyFrame_BlockPop(f);
1871 if (b->b_type != EXCEPT_HANDLER) {
1872 PyErr_SetString(PyExc_SystemError,
1873 "popped block is not an except handler");
1874 why = WHY_EXCEPTION;
1875 break;
1876 }
1877 UNWIND_EXCEPT_HANDLER(b);
1878 }
1879 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 TARGET(POP_BLOCK)
1882 {
1883 PyTryBlock *b = PyFrame_BlockPop(f);
1884 UNWIND_BLOCK(b);
1885 }
1886 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PREDICTED(END_FINALLY);
1889 TARGET(END_FINALLY)
1890 v = POP();
1891 if (PyLong_Check(v)) {
1892 why = (enum why_code) PyLong_AS_LONG(v);
1893 assert(why != WHY_YIELD);
1894 if (why == WHY_RETURN ||
1895 why == WHY_CONTINUE)
1896 retval = POP();
1897 if (why == WHY_SILENCED) {
1898 /* An exception was silenced by 'with', we must
1899 manually unwind the EXCEPT_HANDLER block which was
1900 created when the exception was caught, otherwise
1901 the stack will be in an inconsistent state. */
1902 PyTryBlock *b = PyFrame_BlockPop(f);
1903 assert(b->b_type == EXCEPT_HANDLER);
1904 UNWIND_EXCEPT_HANDLER(b);
1905 why = WHY_NOT;
1906 }
1907 }
1908 else if (PyExceptionClass_Check(v)) {
1909 w = POP();
1910 u = POP();
1911 PyErr_Restore(v, w, u);
1912 why = WHY_RERAISE;
1913 break;
1914 }
1915 else if (v != Py_None) {
1916 PyErr_SetString(PyExc_SystemError,
1917 "'finally' pops bad exception");
1918 why = WHY_EXCEPTION;
1919 }
1920 Py_DECREF(v);
1921 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 TARGET(LOAD_BUILD_CLASS)
1924 x = PyDict_GetItemString(f->f_builtins,
1925 "__build_class__");
1926 if (x == NULL) {
1927 PyErr_SetString(PyExc_ImportError,
1928 "__build_class__ not found");
1929 break;
1930 }
1931 Py_INCREF(x);
1932 PUSH(x);
1933 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 TARGET(STORE_NAME)
1936 w = GETITEM(names, oparg);
1937 v = POP();
1938 if ((x = f->f_locals) != NULL) {
1939 if (PyDict_CheckExact(x))
1940 err = PyDict_SetItem(x, w, v);
1941 else
1942 err = PyObject_SetItem(x, w, v);
1943 Py_DECREF(v);
1944 if (err == 0) DISPATCH();
1945 break;
1946 }
1947 PyErr_Format(PyExc_SystemError,
1948 "no locals found when storing %R", w);
1949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 TARGET(DELETE_NAME)
1952 w = GETITEM(names, oparg);
1953 if ((x = f->f_locals) != NULL) {
1954 if ((err = PyObject_DelItem(x, w)) != 0)
1955 format_exc_check_arg(PyExc_NameError,
1956 NAME_ERROR_MSG,
1957 w);
1958 break;
1959 }
1960 PyErr_Format(PyExc_SystemError,
1961 "no locals when deleting %R", w);
1962 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1965 TARGET(UNPACK_SEQUENCE)
1966 v = POP();
1967 if (PyTuple_CheckExact(v) &&
1968 PyTuple_GET_SIZE(v) == oparg) {
1969 PyObject **items = \
1970 ((PyTupleObject *)v)->ob_item;
1971 while (oparg--) {
1972 w = items[oparg];
1973 Py_INCREF(w);
1974 PUSH(w);
1975 }
1976 Py_DECREF(v);
1977 DISPATCH();
1978 } else if (PyList_CheckExact(v) &&
1979 PyList_GET_SIZE(v) == oparg) {
1980 PyObject **items = \
1981 ((PyListObject *)v)->ob_item;
1982 while (oparg--) {
1983 w = items[oparg];
1984 Py_INCREF(w);
1985 PUSH(w);
1986 }
1987 } else if (unpack_iterable(v, oparg, -1,
1988 stack_pointer + oparg)) {
1989 STACKADJ(oparg);
1990 } else {
1991 /* unpack_iterable() raised an exception */
1992 why = WHY_EXCEPTION;
1993 }
1994 Py_DECREF(v);
1995 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 TARGET(UNPACK_EX)
1998 {
1999 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2000 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2003 stack_pointer + totalargs)) {
2004 stack_pointer += totalargs;
2005 } else {
2006 why = WHY_EXCEPTION;
2007 }
2008 Py_DECREF(v);
2009 break;
2010 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 TARGET(STORE_ATTR)
2013 w = GETITEM(names, oparg);
2014 v = TOP();
2015 u = SECOND();
2016 STACKADJ(-2);
2017 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2018 Py_DECREF(v);
2019 Py_DECREF(u);
2020 if (err == 0) DISPATCH();
2021 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 TARGET(DELETE_ATTR)
2024 w = GETITEM(names, oparg);
2025 v = POP();
2026 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2027 /* del v.w */
2028 Py_DECREF(v);
2029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 TARGET(STORE_GLOBAL)
2032 w = GETITEM(names, oparg);
2033 v = POP();
2034 err = PyDict_SetItem(f->f_globals, w, v);
2035 Py_DECREF(v);
2036 if (err == 0) DISPATCH();
2037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 TARGET(DELETE_GLOBAL)
2040 w = GETITEM(names, oparg);
2041 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2042 format_exc_check_arg(
2043 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 TARGET(LOAD_NAME)
2047 w = GETITEM(names, oparg);
2048 if ((v = f->f_locals) == NULL) {
2049 PyErr_Format(PyExc_SystemError,
2050 "no locals when loading %R", w);
2051 why = WHY_EXCEPTION;
2052 break;
2053 }
2054 if (PyDict_CheckExact(v)) {
2055 x = PyDict_GetItem(v, w);
2056 Py_XINCREF(x);
2057 }
2058 else {
2059 x = PyObject_GetItem(v, w);
2060 if (x == NULL && PyErr_Occurred()) {
2061 if (!PyErr_ExceptionMatches(
2062 PyExc_KeyError))
2063 break;
2064 PyErr_Clear();
2065 }
2066 }
2067 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002068 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002070 x = PyDict_GetItem(f->f_builtins, w);
2071 if (x == NULL) {
2072 format_exc_check_arg(
2073 PyExc_NameError,
2074 NAME_ERROR_MSG, w);
2075 break;
2076 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
2078 Py_INCREF(x);
2079 }
2080 PUSH(x);
2081 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 TARGET(LOAD_GLOBAL)
2084 w = GETITEM(names, oparg);
2085 if (PyUnicode_CheckExact(w)) {
2086 /* Inline the PyDict_GetItem() calls.
2087 WARNING: this is an extreme speed hack.
2088 Do not try this at home. */
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002089 Py_hash_t hash = ((PyUnicodeObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (hash != -1) {
2091 PyDictObject *d;
2092 PyDictEntry *e;
2093 d = (PyDictObject *)(f->f_globals);
2094 e = d->ma_lookup(d, w, hash);
2095 if (e == NULL) {
2096 x = NULL;
2097 break;
2098 }
2099 x = e->me_value;
2100 if (x != NULL) {
2101 Py_INCREF(x);
2102 PUSH(x);
2103 DISPATCH();
2104 }
2105 d = (PyDictObject *)(f->f_builtins);
2106 e = d->ma_lookup(d, w, hash);
2107 if (e == NULL) {
2108 x = NULL;
2109 break;
2110 }
2111 x = e->me_value;
2112 if (x != NULL) {
2113 Py_INCREF(x);
2114 PUSH(x);
2115 DISPATCH();
2116 }
2117 goto load_global_error;
2118 }
2119 }
2120 /* This is the un-inlined version of the code above */
2121 x = PyDict_GetItem(f->f_globals, w);
2122 if (x == NULL) {
2123 x = PyDict_GetItem(f->f_builtins, w);
2124 if (x == NULL) {
2125 load_global_error:
2126 format_exc_check_arg(
2127 PyExc_NameError,
2128 GLOBAL_NAME_ERROR_MSG, w);
2129 break;
2130 }
2131 }
2132 Py_INCREF(x);
2133 PUSH(x);
2134 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 TARGET(DELETE_FAST)
2137 x = GETLOCAL(oparg);
2138 if (x != NULL) {
2139 SETLOCAL(oparg, NULL);
2140 DISPATCH();
2141 }
2142 format_exc_check_arg(
2143 PyExc_UnboundLocalError,
2144 UNBOUNDLOCAL_ERROR_MSG,
2145 PyTuple_GetItem(co->co_varnames, oparg)
2146 );
2147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002149 TARGET(DELETE_DEREF)
2150 x = freevars[oparg];
2151 if (PyCell_GET(x) != NULL) {
2152 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002153 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002154 }
2155 err = -1;
2156 format_exc_unbound(co, oparg);
2157 break;
2158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 TARGET(LOAD_CLOSURE)
2160 x = freevars[oparg];
2161 Py_INCREF(x);
2162 PUSH(x);
2163 if (x != NULL) DISPATCH();
2164 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 TARGET(LOAD_DEREF)
2167 x = freevars[oparg];
2168 w = PyCell_Get(x);
2169 if (w != NULL) {
2170 PUSH(w);
2171 DISPATCH();
2172 }
2173 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002174 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 TARGET(STORE_DEREF)
2178 w = POP();
2179 x = freevars[oparg];
2180 PyCell_Set(x, w);
2181 Py_DECREF(w);
2182 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 TARGET(BUILD_TUPLE)
2185 x = PyTuple_New(oparg);
2186 if (x != NULL) {
2187 for (; --oparg >= 0;) {
2188 w = POP();
2189 PyTuple_SET_ITEM(x, oparg, w);
2190 }
2191 PUSH(x);
2192 DISPATCH();
2193 }
2194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 TARGET(BUILD_LIST)
2197 x = PyList_New(oparg);
2198 if (x != NULL) {
2199 for (; --oparg >= 0;) {
2200 w = POP();
2201 PyList_SET_ITEM(x, oparg, w);
2202 }
2203 PUSH(x);
2204 DISPATCH();
2205 }
2206 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 TARGET(BUILD_SET)
2209 x = PySet_New(NULL);
2210 if (x != NULL) {
2211 for (; --oparg >= 0;) {
2212 w = POP();
2213 if (err == 0)
2214 err = PySet_Add(x, w);
2215 Py_DECREF(w);
2216 }
2217 if (err != 0) {
2218 Py_DECREF(x);
2219 break;
2220 }
2221 PUSH(x);
2222 DISPATCH();
2223 }
2224 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 TARGET(BUILD_MAP)
2227 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2228 PUSH(x);
2229 if (x != NULL) DISPATCH();
2230 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 TARGET(STORE_MAP)
2233 w = TOP(); /* key */
2234 u = SECOND(); /* value */
2235 v = THIRD(); /* dict */
2236 STACKADJ(-2);
2237 assert (PyDict_CheckExact(v));
2238 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2239 Py_DECREF(u);
2240 Py_DECREF(w);
2241 if (err == 0) DISPATCH();
2242 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 TARGET(MAP_ADD)
2245 w = TOP(); /* key */
2246 u = SECOND(); /* value */
2247 STACKADJ(-2);
2248 v = stack_pointer[-oparg]; /* dict */
2249 assert (PyDict_CheckExact(v));
2250 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2251 Py_DECREF(u);
2252 Py_DECREF(w);
2253 if (err == 0) {
2254 PREDICT(JUMP_ABSOLUTE);
2255 DISPATCH();
2256 }
2257 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 TARGET(LOAD_ATTR)
2260 w = GETITEM(names, oparg);
2261 v = TOP();
2262 x = PyObject_GetAttr(v, w);
2263 Py_DECREF(v);
2264 SET_TOP(x);
2265 if (x != NULL) DISPATCH();
2266 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 TARGET(COMPARE_OP)
2269 w = POP();
2270 v = TOP();
2271 x = cmp_outcome(oparg, v, w);
2272 Py_DECREF(v);
2273 Py_DECREF(w);
2274 SET_TOP(x);
2275 if (x == NULL) break;
2276 PREDICT(POP_JUMP_IF_FALSE);
2277 PREDICT(POP_JUMP_IF_TRUE);
2278 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 TARGET(IMPORT_NAME)
2281 w = GETITEM(names, oparg);
2282 x = PyDict_GetItemString(f->f_builtins, "__import__");
2283 if (x == NULL) {
2284 PyErr_SetString(PyExc_ImportError,
2285 "__import__ not found");
2286 break;
2287 }
2288 Py_INCREF(x);
2289 v = POP();
2290 u = TOP();
2291 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2292 w = PyTuple_Pack(5,
2293 w,
2294 f->f_globals,
2295 f->f_locals == NULL ?
2296 Py_None : f->f_locals,
2297 v,
2298 u);
2299 else
2300 w = PyTuple_Pack(4,
2301 w,
2302 f->f_globals,
2303 f->f_locals == NULL ?
2304 Py_None : f->f_locals,
2305 v);
2306 Py_DECREF(v);
2307 Py_DECREF(u);
2308 if (w == NULL) {
2309 u = POP();
2310 Py_DECREF(x);
2311 x = NULL;
2312 break;
2313 }
2314 READ_TIMESTAMP(intr0);
2315 v = x;
2316 x = PyEval_CallObject(v, w);
2317 Py_DECREF(v);
2318 READ_TIMESTAMP(intr1);
2319 Py_DECREF(w);
2320 SET_TOP(x);
2321 if (x != NULL) DISPATCH();
2322 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 TARGET(IMPORT_STAR)
2325 v = POP();
2326 PyFrame_FastToLocals(f);
2327 if ((x = f->f_locals) == NULL) {
2328 PyErr_SetString(PyExc_SystemError,
2329 "no locals found during 'import *'");
2330 break;
2331 }
2332 READ_TIMESTAMP(intr0);
2333 err = import_all_from(x, v);
2334 READ_TIMESTAMP(intr1);
2335 PyFrame_LocalsToFast(f, 0);
2336 Py_DECREF(v);
2337 if (err == 0) DISPATCH();
2338 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 TARGET(IMPORT_FROM)
2341 w = GETITEM(names, oparg);
2342 v = TOP();
2343 READ_TIMESTAMP(intr0);
2344 x = import_from(v, w);
2345 READ_TIMESTAMP(intr1);
2346 PUSH(x);
2347 if (x != NULL) DISPATCH();
2348 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 TARGET(JUMP_FORWARD)
2351 JUMPBY(oparg);
2352 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2355 TARGET(POP_JUMP_IF_FALSE)
2356 w = POP();
2357 if (w == Py_True) {
2358 Py_DECREF(w);
2359 FAST_DISPATCH();
2360 }
2361 if (w == Py_False) {
2362 Py_DECREF(w);
2363 JUMPTO(oparg);
2364 FAST_DISPATCH();
2365 }
2366 err = PyObject_IsTrue(w);
2367 Py_DECREF(w);
2368 if (err > 0)
2369 err = 0;
2370 else if (err == 0)
2371 JUMPTO(oparg);
2372 else
2373 break;
2374 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2377 TARGET(POP_JUMP_IF_TRUE)
2378 w = POP();
2379 if (w == Py_False) {
2380 Py_DECREF(w);
2381 FAST_DISPATCH();
2382 }
2383 if (w == Py_True) {
2384 Py_DECREF(w);
2385 JUMPTO(oparg);
2386 FAST_DISPATCH();
2387 }
2388 err = PyObject_IsTrue(w);
2389 Py_DECREF(w);
2390 if (err > 0) {
2391 err = 0;
2392 JUMPTO(oparg);
2393 }
2394 else if (err == 0)
2395 ;
2396 else
2397 break;
2398 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 TARGET(JUMP_IF_FALSE_OR_POP)
2401 w = TOP();
2402 if (w == Py_True) {
2403 STACKADJ(-1);
2404 Py_DECREF(w);
2405 FAST_DISPATCH();
2406 }
2407 if (w == Py_False) {
2408 JUMPTO(oparg);
2409 FAST_DISPATCH();
2410 }
2411 err = PyObject_IsTrue(w);
2412 if (err > 0) {
2413 STACKADJ(-1);
2414 Py_DECREF(w);
2415 err = 0;
2416 }
2417 else if (err == 0)
2418 JUMPTO(oparg);
2419 else
2420 break;
2421 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 TARGET(JUMP_IF_TRUE_OR_POP)
2424 w = TOP();
2425 if (w == Py_False) {
2426 STACKADJ(-1);
2427 Py_DECREF(w);
2428 FAST_DISPATCH();
2429 }
2430 if (w == Py_True) {
2431 JUMPTO(oparg);
2432 FAST_DISPATCH();
2433 }
2434 err = PyObject_IsTrue(w);
2435 if (err > 0) {
2436 err = 0;
2437 JUMPTO(oparg);
2438 }
2439 else if (err == 0) {
2440 STACKADJ(-1);
2441 Py_DECREF(w);
2442 }
2443 else
2444 break;
2445 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2448 TARGET(JUMP_ABSOLUTE)
2449 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002450#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Enabling this path speeds-up all while and for-loops by bypassing
2452 the per-loop checks for signals. By default, this should be turned-off
2453 because it prevents detection of a control-break in tight loops like
2454 "while 1: pass". Compile with this option turned-on when you need
2455 the speed-up and do not need break checking inside tight loops (ones
2456 that contain only instructions ending with FAST_DISPATCH).
2457 */
2458 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002459#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002461#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 TARGET(GET_ITER)
2464 /* before: [obj]; after [getiter(obj)] */
2465 v = TOP();
2466 x = PyObject_GetIter(v);
2467 Py_DECREF(v);
2468 if (x != NULL) {
2469 SET_TOP(x);
2470 PREDICT(FOR_ITER);
2471 DISPATCH();
2472 }
2473 STACKADJ(-1);
2474 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 PREDICTED_WITH_ARG(FOR_ITER);
2477 TARGET(FOR_ITER)
2478 /* before: [iter]; after: [iter, iter()] *or* [] */
2479 v = TOP();
2480 x = (*v->ob_type->tp_iternext)(v);
2481 if (x != NULL) {
2482 PUSH(x);
2483 PREDICT(STORE_FAST);
2484 PREDICT(UNPACK_SEQUENCE);
2485 DISPATCH();
2486 }
2487 if (PyErr_Occurred()) {
2488 if (!PyErr_ExceptionMatches(
2489 PyExc_StopIteration))
2490 break;
2491 PyErr_Clear();
2492 }
2493 /* iterator ended normally */
2494 x = v = POP();
2495 Py_DECREF(v);
2496 JUMPBY(oparg);
2497 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 TARGET(BREAK_LOOP)
2500 why = WHY_BREAK;
2501 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 TARGET(CONTINUE_LOOP)
2504 retval = PyLong_FromLong(oparg);
2505 if (!retval) {
2506 x = NULL;
2507 break;
2508 }
2509 why = WHY_CONTINUE;
2510 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2513 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2514 TARGET(SETUP_FINALLY)
2515 _setup_finally:
2516 /* NOTE: If you add any new block-setup opcodes that
2517 are not try/except/finally handlers, you may need
2518 to update the PyGen_NeedsFinalizing() function.
2519 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2522 STACK_LEVEL());
2523 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 TARGET(SETUP_WITH)
2526 {
2527 static PyObject *exit, *enter;
2528 w = TOP();
2529 x = special_lookup(w, "__exit__", &exit);
2530 if (!x)
2531 break;
2532 SET_TOP(x);
2533 u = special_lookup(w, "__enter__", &enter);
2534 Py_DECREF(w);
2535 if (!u) {
2536 x = NULL;
2537 break;
2538 }
2539 x = PyObject_CallFunctionObjArgs(u, NULL);
2540 Py_DECREF(u);
2541 if (!x)
2542 break;
2543 /* Setup the finally block before pushing the result
2544 of __enter__ on the stack. */
2545 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2546 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 PUSH(x);
2549 DISPATCH();
2550 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 TARGET(WITH_CLEANUP)
2553 {
2554 /* At the top of the stack are 1-3 values indicating
2555 how/why we entered the finally clause:
2556 - TOP = None
2557 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2558 - TOP = WHY_*; no retval below it
2559 - (TOP, SECOND, THIRD) = exc_info()
2560 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2561 Below them is EXIT, the context.__exit__ bound method.
2562 In the last case, we must call
2563 EXIT(TOP, SECOND, THIRD)
2564 otherwise we must call
2565 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 In the first two cases, we remove EXIT from the
2568 stack, leaving the rest in the same order. In the
2569 third case, we shift the bottom 3 values of the
2570 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 In addition, if the stack represents an exception,
2573 *and* the function call returns a 'true' value, we
2574 push WHY_SILENCED onto the stack. END_FINALLY will
2575 then not re-raise the exception. (But non-local
2576 gotos should still be resumed.)
2577 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 PyObject *exit_func;
2580 u = TOP();
2581 if (u == Py_None) {
2582 (void)POP();
2583 exit_func = TOP();
2584 SET_TOP(u);
2585 v = w = Py_None;
2586 }
2587 else if (PyLong_Check(u)) {
2588 (void)POP();
2589 switch(PyLong_AsLong(u)) {
2590 case WHY_RETURN:
2591 case WHY_CONTINUE:
2592 /* Retval in TOP. */
2593 exit_func = SECOND();
2594 SET_SECOND(TOP());
2595 SET_TOP(u);
2596 break;
2597 default:
2598 exit_func = TOP();
2599 SET_TOP(u);
2600 break;
2601 }
2602 u = v = w = Py_None;
2603 }
2604 else {
2605 PyObject *tp, *exc, *tb;
2606 PyTryBlock *block;
2607 v = SECOND();
2608 w = THIRD();
2609 tp = FOURTH();
2610 exc = PEEK(5);
2611 tb = PEEK(6);
2612 exit_func = PEEK(7);
2613 SET_VALUE(7, tb);
2614 SET_VALUE(6, exc);
2615 SET_VALUE(5, tp);
2616 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2617 SET_FOURTH(NULL);
2618 /* We just shifted the stack down, so we have
2619 to tell the except handler block that the
2620 values are lower than it expects. */
2621 block = &f->f_blockstack[f->f_iblock - 1];
2622 assert(block->b_type == EXCEPT_HANDLER);
2623 block->b_level--;
2624 }
2625 /* XXX Not the fastest way to call it... */
2626 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2627 NULL);
2628 Py_DECREF(exit_func);
2629 if (x == NULL)
2630 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 if (u != Py_None)
2633 err = PyObject_IsTrue(x);
2634 else
2635 err = 0;
2636 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 if (err < 0)
2639 break; /* Go to error exit */
2640 else if (err > 0) {
2641 err = 0;
2642 /* There was an exception and a True return */
2643 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2644 }
2645 PREDICT(END_FINALLY);
2646 break;
2647 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 TARGET(CALL_FUNCTION)
2650 {
2651 PyObject **sp;
2652 PCALL(PCALL_ALL);
2653 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002654#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002656#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002658#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 stack_pointer = sp;
2660 PUSH(x);
2661 if (x != NULL)
2662 DISPATCH();
2663 break;
2664 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2667 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2668 TARGET(CALL_FUNCTION_VAR_KW)
2669 _call_function_var_kw:
2670 {
2671 int na = oparg & 0xff;
2672 int nk = (oparg>>8) & 0xff;
2673 int flags = (opcode - CALL_FUNCTION) & 3;
2674 int n = na + 2 * nk;
2675 PyObject **pfunc, *func, **sp;
2676 PCALL(PCALL_ALL);
2677 if (flags & CALL_FLAG_VAR)
2678 n++;
2679 if (flags & CALL_FLAG_KW)
2680 n++;
2681 pfunc = stack_pointer - n - 1;
2682 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002685 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 PyObject *self = PyMethod_GET_SELF(func);
2687 Py_INCREF(self);
2688 func = PyMethod_GET_FUNCTION(func);
2689 Py_INCREF(func);
2690 Py_DECREF(*pfunc);
2691 *pfunc = self;
2692 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002693 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 } else
2695 Py_INCREF(func);
2696 sp = stack_pointer;
2697 READ_TIMESTAMP(intr0);
2698 x = ext_do_call(func, &sp, flags, na, nk);
2699 READ_TIMESTAMP(intr1);
2700 stack_pointer = sp;
2701 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 while (stack_pointer > pfunc) {
2704 w = POP();
2705 Py_DECREF(w);
2706 }
2707 PUSH(x);
2708 if (x != NULL)
2709 DISPATCH();
2710 break;
2711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2714 TARGET(MAKE_FUNCTION)
2715 _make_function:
2716 {
2717 int posdefaults = oparg & 0xff;
2718 int kwdefaults = (oparg>>8) & 0xff;
2719 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 v = POP(); /* code object */
2722 x = PyFunction_New(v, f->f_globals);
2723 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 if (x != NULL && opcode == MAKE_CLOSURE) {
2726 v = POP();
2727 if (PyFunction_SetClosure(x, v) != 0) {
2728 /* Can't happen unless bytecode is corrupt. */
2729 why = WHY_EXCEPTION;
2730 }
2731 Py_DECREF(v);
2732 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 if (x != NULL && num_annotations > 0) {
2735 Py_ssize_t name_ix;
2736 u = POP(); /* names of args with annotations */
2737 v = PyDict_New();
2738 if (v == NULL) {
2739 Py_DECREF(x);
2740 x = NULL;
2741 break;
2742 }
2743 name_ix = PyTuple_Size(u);
2744 assert(num_annotations == name_ix+1);
2745 while (name_ix > 0) {
2746 --name_ix;
2747 t = PyTuple_GET_ITEM(u, name_ix);
2748 w = POP();
2749 /* XXX(nnorwitz): check for errors */
2750 PyDict_SetItem(v, t, w);
2751 Py_DECREF(w);
2752 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 if (PyFunction_SetAnnotations(x, v) != 0) {
2755 /* Can't happen unless
2756 PyFunction_SetAnnotations changes. */
2757 why = WHY_EXCEPTION;
2758 }
2759 Py_DECREF(v);
2760 Py_DECREF(u);
2761 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 /* XXX Maybe this should be a separate opcode? */
2764 if (x != NULL && posdefaults > 0) {
2765 v = PyTuple_New(posdefaults);
2766 if (v == NULL) {
2767 Py_DECREF(x);
2768 x = NULL;
2769 break;
2770 }
2771 while (--posdefaults >= 0) {
2772 w = POP();
2773 PyTuple_SET_ITEM(v, posdefaults, w);
2774 }
2775 if (PyFunction_SetDefaults(x, v) != 0) {
2776 /* Can't happen unless
2777 PyFunction_SetDefaults changes. */
2778 why = WHY_EXCEPTION;
2779 }
2780 Py_DECREF(v);
2781 }
2782 if (x != NULL && kwdefaults > 0) {
2783 v = PyDict_New();
2784 if (v == NULL) {
2785 Py_DECREF(x);
2786 x = NULL;
2787 break;
2788 }
2789 while (--kwdefaults >= 0) {
2790 w = POP(); /* default value */
2791 u = POP(); /* kw only arg name */
2792 /* XXX(nnorwitz): check for errors */
2793 PyDict_SetItem(v, u, w);
2794 Py_DECREF(w);
2795 Py_DECREF(u);
2796 }
2797 if (PyFunction_SetKwDefaults(x, v) != 0) {
2798 /* Can't happen unless
2799 PyFunction_SetKwDefaults changes. */
2800 why = WHY_EXCEPTION;
2801 }
2802 Py_DECREF(v);
2803 }
2804 PUSH(x);
2805 break;
2806 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 TARGET(BUILD_SLICE)
2809 if (oparg == 3)
2810 w = POP();
2811 else
2812 w = NULL;
2813 v = POP();
2814 u = TOP();
2815 x = PySlice_New(u, v, w);
2816 Py_DECREF(u);
2817 Py_DECREF(v);
2818 Py_XDECREF(w);
2819 SET_TOP(x);
2820 if (x != NULL) DISPATCH();
2821 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 TARGET(EXTENDED_ARG)
2824 opcode = NEXTOP();
2825 oparg = oparg<<16 | NEXTARG();
2826 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002827
Antoine Pitrou042b1282010-08-13 21:15:58 +00002828#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002830#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 default:
2832 fprintf(stderr,
2833 "XXX lineno: %d, opcode: %d\n",
2834 PyFrame_GetLineNumber(f),
2835 opcode);
2836 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2837 why = WHY_EXCEPTION;
2838 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002839
2840#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002842#endif
2843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (why == WHY_NOT) {
2853 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002854#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 /* This check is expensive! */
2856 if (PyErr_Occurred())
2857 fprintf(stderr,
2858 "XXX undetected error\n");
2859 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 READ_TIMESTAMP(loop1);
2862 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002863#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002865#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 }
2867 why = WHY_EXCEPTION;
2868 x = Py_None;
2869 err = 0;
2870 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2875 if (!PyErr_Occurred()) {
2876 PyErr_SetString(PyExc_SystemError,
2877 "error return without exception set");
2878 why = WHY_EXCEPTION;
2879 }
2880 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002881#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 else {
2883 /* This check is expensive! */
2884 if (PyErr_Occurred()) {
2885 char buf[128];
2886 sprintf(buf, "Stack unwind with exception "
2887 "set and why=%d", why);
2888 Py_FatalError(buf);
2889 }
2890 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002891#endif
2892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 if (why == WHY_EXCEPTION) {
2896 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 if (tstate->c_tracefunc != NULL)
2899 call_exc_trace(tstate->c_tracefunc,
2900 tstate->c_traceobj, f);
2901 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (why == WHY_RERAISE)
2906 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002910fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 while (why != WHY_NOT && f->f_iblock > 0) {
2912 /* Peek at the current block. */
2913 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 assert(why != WHY_YIELD);
2916 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2917 why = WHY_NOT;
2918 JUMPTO(PyLong_AS_LONG(retval));
2919 Py_DECREF(retval);
2920 break;
2921 }
2922 /* Now we have to pop the block. */
2923 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 if (b->b_type == EXCEPT_HANDLER) {
2926 UNWIND_EXCEPT_HANDLER(b);
2927 continue;
2928 }
2929 UNWIND_BLOCK(b);
2930 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2931 why = WHY_NOT;
2932 JUMPTO(b->b_handler);
2933 break;
2934 }
2935 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2936 || b->b_type == SETUP_FINALLY)) {
2937 PyObject *exc, *val, *tb;
2938 int handler = b->b_handler;
2939 /* Beware, this invalidates all b->b_* fields */
2940 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2941 PUSH(tstate->exc_traceback);
2942 PUSH(tstate->exc_value);
2943 if (tstate->exc_type != NULL) {
2944 PUSH(tstate->exc_type);
2945 }
2946 else {
2947 Py_INCREF(Py_None);
2948 PUSH(Py_None);
2949 }
2950 PyErr_Fetch(&exc, &val, &tb);
2951 /* Make the raw exception data
2952 available to the handler,
2953 so a program can emulate the
2954 Python main loop. */
2955 PyErr_NormalizeException(
2956 &exc, &val, &tb);
2957 PyException_SetTraceback(val, tb);
2958 Py_INCREF(exc);
2959 tstate->exc_type = exc;
2960 Py_INCREF(val);
2961 tstate->exc_value = val;
2962 tstate->exc_traceback = tb;
2963 if (tb == NULL)
2964 tb = Py_None;
2965 Py_INCREF(tb);
2966 PUSH(tb);
2967 PUSH(val);
2968 PUSH(exc);
2969 why = WHY_NOT;
2970 JUMPTO(handler);
2971 break;
2972 }
2973 if (b->b_type == SETUP_FINALLY) {
2974 if (why & (WHY_RETURN | WHY_CONTINUE))
2975 PUSH(retval);
2976 PUSH(PyLong_FromLong((long)why));
2977 why = WHY_NOT;
2978 JUMPTO(b->b_handler);
2979 break;
2980 }
2981 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 if (why != WHY_NOT)
2986 break;
2987 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 assert(why != WHY_YIELD);
2992 /* Pop remaining stack entries. */
2993 while (!EMPTY()) {
2994 v = POP();
2995 Py_XDECREF(v);
2996 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 if (why != WHY_RETURN)
2999 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003000
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003001fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 if (tstate->use_tracing) {
3003 if (tstate->c_tracefunc) {
3004 if (why == WHY_RETURN || why == WHY_YIELD) {
3005 if (call_trace(tstate->c_tracefunc,
3006 tstate->c_traceobj, f,
3007 PyTrace_RETURN, retval)) {
3008 Py_XDECREF(retval);
3009 retval = NULL;
3010 why = WHY_EXCEPTION;
3011 }
3012 }
3013 else if (why == WHY_EXCEPTION) {
3014 call_trace_protected(tstate->c_tracefunc,
3015 tstate->c_traceobj, f,
3016 PyTrace_RETURN, NULL);
3017 }
3018 }
3019 if (tstate->c_profilefunc) {
3020 if (why == WHY_EXCEPTION)
3021 call_trace_protected(tstate->c_profilefunc,
3022 tstate->c_profileobj, f,
3023 PyTrace_RETURN, NULL);
3024 else if (call_trace(tstate->c_profilefunc,
3025 tstate->c_profileobj, f,
3026 PyTrace_RETURN, retval)) {
3027 Py_XDECREF(retval);
3028 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003029 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 }
3031 }
3032 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003035exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 Py_LeaveRecursiveCall();
3037 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003040}
3041
Guido van Rossumc2e20742006-02-27 22:32:47 +00003042/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003043 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003044 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003045
Tim Peters6d6c1a32001-08-02 04:15:00 +00003046PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003047PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyObject **args, int argcount, PyObject **kws, int kwcount,
3049 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003050{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003051 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 register PyFrameObject *f;
3053 register PyObject *retval = NULL;
3054 register PyObject **fastlocals, **freevars;
3055 PyThreadState *tstate = PyThreadState_GET();
3056 PyObject *x, *u;
3057 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 if (globals == NULL) {
3060 PyErr_SetString(PyExc_SystemError,
3061 "PyEval_EvalCodeEx: NULL globals");
3062 return NULL;
3063 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 assert(tstate != NULL);
3066 assert(globals != NULL);
3067 f = PyFrame_New(tstate, co, globals, locals);
3068 if (f == NULL)
3069 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 fastlocals = f->f_localsplus;
3072 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3075 int i;
3076 int n = argcount;
3077 PyObject *kwdict = NULL;
3078 if (co->co_flags & CO_VARKEYWORDS) {
3079 kwdict = PyDict_New();
3080 if (kwdict == NULL)
3081 goto fail;
3082 i = total_args;
3083 if (co->co_flags & CO_VARARGS)
3084 i++;
3085 SETLOCAL(i, kwdict);
3086 }
3087 if (argcount > co->co_argcount) {
3088 if (!(co->co_flags & CO_VARARGS)) {
3089 PyErr_Format(PyExc_TypeError,
3090 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003091 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 co->co_name,
3093 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003094 co->co_argcount,
3095 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonaa7fbd92010-09-25 03:25:42 +00003096 argcount + kwcount);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 goto fail;
3098 }
3099 n = co->co_argcount;
3100 }
3101 for (i = 0; i < n; i++) {
3102 x = args[i];
3103 Py_INCREF(x);
3104 SETLOCAL(i, x);
3105 }
3106 if (co->co_flags & CO_VARARGS) {
3107 u = PyTuple_New(argcount - n);
3108 if (u == NULL)
3109 goto fail;
3110 SETLOCAL(total_args, u);
3111 for (i = n; i < argcount; i++) {
3112 x = args[i];
3113 Py_INCREF(x);
3114 PyTuple_SET_ITEM(u, i-n, x);
3115 }
3116 }
3117 for (i = 0; i < kwcount; i++) {
3118 PyObject **co_varnames;
3119 PyObject *keyword = kws[2*i];
3120 PyObject *value = kws[2*i + 1];
3121 int j;
3122 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3123 PyErr_Format(PyExc_TypeError,
3124 "%U() keywords must be strings",
3125 co->co_name);
3126 goto fail;
3127 }
3128 /* Speed hack: do raw pointer compares. As names are
3129 normally interned this should almost always hit. */
3130 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3131 for (j = 0; j < total_args; j++) {
3132 PyObject *nm = co_varnames[j];
3133 if (nm == keyword)
3134 goto kw_found;
3135 }
3136 /* Slow fallback, just in case */
3137 for (j = 0; j < total_args; j++) {
3138 PyObject *nm = co_varnames[j];
3139 int cmp = PyObject_RichCompareBool(
3140 keyword, nm, Py_EQ);
3141 if (cmp > 0)
3142 goto kw_found;
3143 else if (cmp < 0)
3144 goto fail;
3145 }
3146 if (j >= total_args && kwdict == NULL) {
3147 PyErr_Format(PyExc_TypeError,
3148 "%U() got an unexpected "
3149 "keyword argument '%S'",
3150 co->co_name,
3151 keyword);
3152 goto fail;
3153 }
3154 PyDict_SetItem(kwdict, keyword, value);
3155 continue;
3156 kw_found:
3157 if (GETLOCAL(j) != NULL) {
3158 PyErr_Format(PyExc_TypeError,
3159 "%U() got multiple "
3160 "values for keyword "
3161 "argument '%S'",
3162 co->co_name,
3163 keyword);
3164 goto fail;
3165 }
3166 Py_INCREF(value);
3167 SETLOCAL(j, value);
3168 }
3169 if (co->co_kwonlyargcount > 0) {
3170 for (i = co->co_argcount; i < total_args; i++) {
3171 PyObject *name;
3172 if (GETLOCAL(i) != NULL)
3173 continue;
3174 name = PyTuple_GET_ITEM(co->co_varnames, i);
3175 if (kwdefs != NULL) {
3176 PyObject *def = PyDict_GetItem(kwdefs, name);
3177 if (def) {
3178 Py_INCREF(def);
3179 SETLOCAL(i, def);
3180 continue;
3181 }
3182 }
3183 PyErr_Format(PyExc_TypeError,
3184 "%U() needs keyword-only argument %S",
3185 co->co_name, name);
3186 goto fail;
3187 }
3188 }
3189 if (argcount < co->co_argcount) {
3190 int m = co->co_argcount - defcount;
3191 for (i = argcount; i < m; i++) {
3192 if (GETLOCAL(i) == NULL) {
3193 int j, given = 0;
3194 for (j = 0; j < co->co_argcount; j++)
3195 if (GETLOCAL(j))
3196 given++;
3197 PyErr_Format(PyExc_TypeError,
3198 "%U() takes %s %d "
3199 "argument%s "
3200 "(%d given)",
3201 co->co_name,
3202 ((co->co_flags & CO_VARARGS) ||
3203 defcount) ? "at least"
3204 : "exactly",
3205 m, m == 1 ? "" : "s", given);
3206 goto fail;
3207 }
3208 }
3209 if (n > m)
3210 i = n - m;
3211 else
3212 i = 0;
3213 for (; i < defcount; i++) {
3214 if (GETLOCAL(m+i) == NULL) {
3215 PyObject *def = defs[i];
3216 Py_INCREF(def);
3217 SETLOCAL(m+i, def);
3218 }
3219 }
3220 }
3221 }
3222 else if (argcount > 0 || kwcount > 0) {
3223 PyErr_Format(PyExc_TypeError,
3224 "%U() takes no arguments (%d given)",
3225 co->co_name,
3226 argcount + kwcount);
3227 goto fail;
3228 }
3229 /* Allocate and initialize storage for cell vars, and copy free
3230 vars into frame. This isn't too efficient right now. */
3231 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3232 int i, j, nargs, found;
3233 Py_UNICODE *cellname, *argname;
3234 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 nargs = total_args;
3237 if (co->co_flags & CO_VARARGS)
3238 nargs++;
3239 if (co->co_flags & CO_VARKEYWORDS)
3240 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 /* Initialize each cell var, taking into account
3243 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 Should arrange for the compiler to put cellvars
3246 that are arguments at the beginning of the cellvars
3247 list so that we can march over it more efficiently?
3248 */
3249 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3250 cellname = PyUnicode_AS_UNICODE(
3251 PyTuple_GET_ITEM(co->co_cellvars, i));
3252 found = 0;
3253 for (j = 0; j < nargs; j++) {
3254 argname = PyUnicode_AS_UNICODE(
3255 PyTuple_GET_ITEM(co->co_varnames, j));
3256 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3257 c = PyCell_New(GETLOCAL(j));
3258 if (c == NULL)
3259 goto fail;
3260 GETLOCAL(co->co_nlocals + i) = c;
3261 found = 1;
3262 break;
3263 }
3264 }
3265 if (found == 0) {
3266 c = PyCell_New(NULL);
3267 if (c == NULL)
3268 goto fail;
3269 SETLOCAL(co->co_nlocals + i, c);
3270 }
3271 }
3272 }
3273 if (PyTuple_GET_SIZE(co->co_freevars)) {
3274 int i;
3275 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3276 PyObject *o = PyTuple_GET_ITEM(closure, i);
3277 Py_INCREF(o);
3278 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3279 }
3280 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 if (co->co_flags & CO_GENERATOR) {
3283 /* Don't need to keep the reference to f_back, it will be set
3284 * when the generator is resumed. */
3285 Py_XDECREF(f->f_back);
3286 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 /* Create a new generator that owns the ready to run frame
3291 * and return that as the value. */
3292 return PyGen_New(f);
3293 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003296
Thomas Woutersce272b62007-09-19 21:19:28 +00003297fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 /* decref'ing the frame can cause __del__ methods to get invoked,
3300 which can call back into Python. While we're done with the
3301 current Python frame (f), the associated C stack is still in use,
3302 so recursion_depth must be boosted for the duration.
3303 */
3304 assert(tstate != NULL);
3305 ++tstate->recursion_depth;
3306 Py_DECREF(f);
3307 --tstate->recursion_depth;
3308 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003309}
3310
3311
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003312static PyObject *
3313special_lookup(PyObject *o, char *meth, PyObject **cache)
3314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 PyObject *res;
3316 res = _PyObject_LookupSpecial(o, meth, cache);
3317 if (res == NULL && !PyErr_Occurred()) {
3318 PyErr_SetObject(PyExc_AttributeError, *cache);
3319 return NULL;
3320 }
3321 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003322}
3323
3324
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003325/* Logic for the raise statement (too complicated for inlining).
3326 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003327static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003328do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 if (exc == NULL) {
3333 /* Reraise */
3334 PyThreadState *tstate = PyThreadState_GET();
3335 PyObject *tb;
3336 type = tstate->exc_type;
3337 value = tstate->exc_value;
3338 tb = tstate->exc_traceback;
3339 if (type == Py_None) {
3340 PyErr_SetString(PyExc_RuntimeError,
3341 "No active exception to reraise");
3342 return WHY_EXCEPTION;
3343 }
3344 Py_XINCREF(type);
3345 Py_XINCREF(value);
3346 Py_XINCREF(tb);
3347 PyErr_Restore(type, value, tb);
3348 return WHY_RERAISE;
3349 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 /* We support the following forms of raise:
3352 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003353 raise <instance>
3354 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 if (PyExceptionClass_Check(exc)) {
3357 type = exc;
3358 value = PyObject_CallObject(exc, NULL);
3359 if (value == NULL)
3360 goto raise_error;
3361 }
3362 else if (PyExceptionInstance_Check(exc)) {
3363 value = exc;
3364 type = PyExceptionInstance_Class(exc);
3365 Py_INCREF(type);
3366 }
3367 else {
3368 /* Not something you can raise. You get an exception
3369 anyway, just not what you specified :-) */
3370 Py_DECREF(exc);
3371 PyErr_SetString(PyExc_TypeError,
3372 "exceptions must derive from BaseException");
3373 goto raise_error;
3374 }
Collin Winter828f04a2007-08-31 00:04:24 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 if (cause) {
3377 PyObject *fixed_cause;
3378 if (PyExceptionClass_Check(cause)) {
3379 fixed_cause = PyObject_CallObject(cause, NULL);
3380 if (fixed_cause == NULL)
3381 goto raise_error;
3382 Py_DECREF(cause);
3383 }
3384 else if (PyExceptionInstance_Check(cause)) {
3385 fixed_cause = cause;
3386 }
3387 else {
3388 PyErr_SetString(PyExc_TypeError,
3389 "exception causes must derive from "
3390 "BaseException");
3391 goto raise_error;
3392 }
3393 PyException_SetCause(value, fixed_cause);
3394 }
Collin Winter828f04a2007-08-31 00:04:24 +00003395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 PyErr_SetObject(type, value);
3397 /* PyErr_SetObject incref's its arguments */
3398 Py_XDECREF(value);
3399 Py_XDECREF(type);
3400 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003401
3402raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 Py_XDECREF(value);
3404 Py_XDECREF(type);
3405 Py_XDECREF(cause);
3406 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003407}
3408
Tim Petersd6d010b2001-06-21 02:49:55 +00003409/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003410 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003411
Guido van Rossum0368b722007-05-11 16:50:42 +00003412 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3413 with a variable target.
3414*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003415
Barry Warsawe42b18f1997-08-25 22:13:04 +00003416static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003417unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 int i = 0, j = 0;
3420 Py_ssize_t ll = 0;
3421 PyObject *it; /* iter(v) */
3422 PyObject *w;
3423 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 it = PyObject_GetIter(v);
3428 if (it == NULL)
3429 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 for (; i < argcnt; i++) {
3432 w = PyIter_Next(it);
3433 if (w == NULL) {
3434 /* Iterator done, via error or exhaustion. */
3435 if (!PyErr_Occurred()) {
3436 PyErr_Format(PyExc_ValueError,
3437 "need more than %d value%s to unpack",
3438 i, i == 1 ? "" : "s");
3439 }
3440 goto Error;
3441 }
3442 *--sp = w;
3443 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 if (argcntafter == -1) {
3446 /* We better have exhausted the iterator now. */
3447 w = PyIter_Next(it);
3448 if (w == NULL) {
3449 if (PyErr_Occurred())
3450 goto Error;
3451 Py_DECREF(it);
3452 return 1;
3453 }
3454 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003455 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3456 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 goto Error;
3458 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 l = PySequence_List(it);
3461 if (l == NULL)
3462 goto Error;
3463 *--sp = l;
3464 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 ll = PyList_GET_SIZE(l);
3467 if (ll < argcntafter) {
3468 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3469 argcnt + ll);
3470 goto Error;
3471 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 /* Pop the "after-variable" args off the list. */
3474 for (j = argcntafter; j > 0; j--, i++) {
3475 *--sp = PyList_GET_ITEM(l, ll - j);
3476 }
3477 /* Resize the list. */
3478 Py_SIZE(l) = ll - argcntafter;
3479 Py_DECREF(it);
3480 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003481
Tim Petersd6d010b2001-06-21 02:49:55 +00003482Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 for (; i > 0; i--, sp++)
3484 Py_DECREF(*sp);
3485 Py_XDECREF(it);
3486 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003487}
3488
3489
Guido van Rossum96a42c81992-01-12 02:29:51 +00003490#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003491static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003492prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 printf("%s ", str);
3495 if (PyObject_Print(v, stdout, 0) != 0)
3496 PyErr_Clear(); /* Don't know what else to do */
3497 printf("\n");
3498 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003499}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003500#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003501
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003502static void
Fred Drake5755ce62001-06-27 19:19:46 +00003503call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 PyObject *type, *value, *traceback, *arg;
3506 int err;
3507 PyErr_Fetch(&type, &value, &traceback);
3508 if (value == NULL) {
3509 value = Py_None;
3510 Py_INCREF(value);
3511 }
3512 arg = PyTuple_Pack(3, type, value, traceback);
3513 if (arg == NULL) {
3514 PyErr_Restore(type, value, traceback);
3515 return;
3516 }
3517 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3518 Py_DECREF(arg);
3519 if (err == 0)
3520 PyErr_Restore(type, value, traceback);
3521 else {
3522 Py_XDECREF(type);
3523 Py_XDECREF(value);
3524 Py_XDECREF(traceback);
3525 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003526}
3527
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003528static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003529call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 PyObject *type, *value, *traceback;
3533 int err;
3534 PyErr_Fetch(&type, &value, &traceback);
3535 err = call_trace(func, obj, frame, what, arg);
3536 if (err == 0)
3537 {
3538 PyErr_Restore(type, value, traceback);
3539 return 0;
3540 }
3541 else {
3542 Py_XDECREF(type);
3543 Py_XDECREF(value);
3544 Py_XDECREF(traceback);
3545 return -1;
3546 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003547}
3548
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003549static int
Fred Drake5755ce62001-06-27 19:19:46 +00003550call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 register PyThreadState *tstate = frame->f_tstate;
3554 int result;
3555 if (tstate->tracing)
3556 return 0;
3557 tstate->tracing++;
3558 tstate->use_tracing = 0;
3559 result = func(obj, frame, what, arg);
3560 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3561 || (tstate->c_profilefunc != NULL));
3562 tstate->tracing--;
3563 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003564}
3565
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003566PyObject *
3567_PyEval_CallTracing(PyObject *func, PyObject *args)
3568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 PyFrameObject *frame = PyEval_GetFrame();
3570 PyThreadState *tstate = frame->f_tstate;
3571 int save_tracing = tstate->tracing;
3572 int save_use_tracing = tstate->use_tracing;
3573 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 tstate->tracing = 0;
3576 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3577 || (tstate->c_profilefunc != NULL));
3578 result = PyObject_Call(func, args, NULL);
3579 tstate->tracing = save_tracing;
3580 tstate->use_tracing = save_use_tracing;
3581 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003582}
3583
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003584/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003585static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003586maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3588 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 int result = 0;
3591 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 /* If the last instruction executed isn't in the current
3594 instruction window, reset the window.
3595 */
3596 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3597 PyAddrPair bounds;
3598 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3599 &bounds);
3600 *instr_lb = bounds.ap_lower;
3601 *instr_ub = bounds.ap_upper;
3602 }
3603 /* If the last instruction falls at the start of a line or if
3604 it represents a jump backwards, update the frame's line
3605 number and call the trace function. */
3606 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3607 frame->f_lineno = line;
3608 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3609 }
3610 *instr_prev = frame->f_lasti;
3611 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003612}
3613
Fred Drake5755ce62001-06-27 19:19:46 +00003614void
3615PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 PyThreadState *tstate = PyThreadState_GET();
3618 PyObject *temp = tstate->c_profileobj;
3619 Py_XINCREF(arg);
3620 tstate->c_profilefunc = NULL;
3621 tstate->c_profileobj = NULL;
3622 /* Must make sure that tracing is not ignored if 'temp' is freed */
3623 tstate->use_tracing = tstate->c_tracefunc != NULL;
3624 Py_XDECREF(temp);
3625 tstate->c_profilefunc = func;
3626 tstate->c_profileobj = arg;
3627 /* Flag that tracing or profiling is turned on */
3628 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003629}
3630
3631void
3632PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 PyThreadState *tstate = PyThreadState_GET();
3635 PyObject *temp = tstate->c_traceobj;
3636 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3637 Py_XINCREF(arg);
3638 tstate->c_tracefunc = NULL;
3639 tstate->c_traceobj = NULL;
3640 /* Must make sure that profiling is not ignored if 'temp' is freed */
3641 tstate->use_tracing = tstate->c_profilefunc != NULL;
3642 Py_XDECREF(temp);
3643 tstate->c_tracefunc = func;
3644 tstate->c_traceobj = arg;
3645 /* Flag that tracing or profiling is turned on */
3646 tstate->use_tracing = ((func != NULL)
3647 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003648}
3649
Guido van Rossumb209a111997-04-29 18:18:01 +00003650PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003651PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 PyFrameObject *current_frame = PyEval_GetFrame();
3654 if (current_frame == NULL)
3655 return PyThreadState_GET()->interp->builtins;
3656 else
3657 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003658}
3659
Guido van Rossumb209a111997-04-29 18:18:01 +00003660PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003661PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 PyFrameObject *current_frame = PyEval_GetFrame();
3664 if (current_frame == NULL)
3665 return NULL;
3666 PyFrame_FastToLocals(current_frame);
3667 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003668}
3669
Guido van Rossumb209a111997-04-29 18:18:01 +00003670PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003671PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 PyFrameObject *current_frame = PyEval_GetFrame();
3674 if (current_frame == NULL)
3675 return NULL;
3676 else
3677 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003678}
3679
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003680PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003681PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 PyThreadState *tstate = PyThreadState_GET();
3684 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003685}
3686
Guido van Rossum6135a871995-01-09 17:53:26 +00003687int
Tim Peters5ba58662001-07-16 02:29:45 +00003688PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyFrameObject *current_frame = PyEval_GetFrame();
3691 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 if (current_frame != NULL) {
3694 const int codeflags = current_frame->f_code->co_flags;
3695 const int compilerflags = codeflags & PyCF_MASK;
3696 if (compilerflags) {
3697 result = 1;
3698 cf->cf_flags |= compilerflags;
3699 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003700#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 if (codeflags & CO_GENERATOR_ALLOWED) {
3702 result = 1;
3703 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3704 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003705#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 }
3707 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003708}
3709
Guido van Rossum3f5da241990-12-20 15:06:42 +00003710
Guido van Rossum681d79a1995-07-18 14:51:37 +00003711/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003712 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003713
Guido van Rossumb209a111997-04-29 18:18:01 +00003714PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003715PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 if (arg == NULL) {
3720 arg = PyTuple_New(0);
3721 if (arg == NULL)
3722 return NULL;
3723 }
3724 else if (!PyTuple_Check(arg)) {
3725 PyErr_SetString(PyExc_TypeError,
3726 "argument list must be a tuple");
3727 return NULL;
3728 }
3729 else
3730 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 if (kw != NULL && !PyDict_Check(kw)) {
3733 PyErr_SetString(PyExc_TypeError,
3734 "keyword list must be a dictionary");
3735 Py_DECREF(arg);
3736 return NULL;
3737 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 result = PyObject_Call(func, arg, kw);
3740 Py_DECREF(arg);
3741 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003742}
3743
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003744const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003745PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 if (PyMethod_Check(func))
3748 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3749 else if (PyFunction_Check(func))
3750 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3751 else if (PyCFunction_Check(func))
3752 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3753 else
3754 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003755}
3756
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003757const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003758PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 if (PyMethod_Check(func))
3761 return "()";
3762 else if (PyFunction_Check(func))
3763 return "()";
3764 else if (PyCFunction_Check(func))
3765 return "()";
3766 else
3767 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003768}
3769
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003770static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003771err_args(PyObject *func, int flags, int nargs)
3772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 if (flags & METH_NOARGS)
3774 PyErr_Format(PyExc_TypeError,
3775 "%.200s() takes no arguments (%d given)",
3776 ((PyCFunctionObject *)func)->m_ml->ml_name,
3777 nargs);
3778 else
3779 PyErr_Format(PyExc_TypeError,
3780 "%.200s() takes exactly one argument (%d given)",
3781 ((PyCFunctionObject *)func)->m_ml->ml_name,
3782 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003783}
3784
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003785#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003786if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 if (call_trace(tstate->c_profilefunc, \
3788 tstate->c_profileobj, \
3789 tstate->frame, PyTrace_C_CALL, \
3790 func)) { \
3791 x = NULL; \
3792 } \
3793 else { \
3794 x = call; \
3795 if (tstate->c_profilefunc != NULL) { \
3796 if (x == NULL) { \
3797 call_trace_protected(tstate->c_profilefunc, \
3798 tstate->c_profileobj, \
3799 tstate->frame, PyTrace_C_EXCEPTION, \
3800 func); \
3801 /* XXX should pass (type, value, tb) */ \
3802 } else { \
3803 if (call_trace(tstate->c_profilefunc, \
3804 tstate->c_profileobj, \
3805 tstate->frame, PyTrace_C_RETURN, \
3806 func)) { \
3807 Py_DECREF(x); \
3808 x = NULL; \
3809 } \
3810 } \
3811 } \
3812 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003813} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 x = call; \
3815 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003816
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003817static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003818call_function(PyObject ***pp_stack, int oparg
3819#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003821#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 int na = oparg & 0xff;
3825 int nk = (oparg>>8) & 0xff;
3826 int n = na + 2 * nk;
3827 PyObject **pfunc = (*pp_stack) - n - 1;
3828 PyObject *func = *pfunc;
3829 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 /* Always dispatch PyCFunction first, because these are
3832 presumed to be the most frequent callable object.
3833 */
3834 if (PyCFunction_Check(func) && nk == 0) {
3835 int flags = PyCFunction_GET_FLAGS(func);
3836 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 PCALL(PCALL_CFUNCTION);
3839 if (flags & (METH_NOARGS | METH_O)) {
3840 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3841 PyObject *self = PyCFunction_GET_SELF(func);
3842 if (flags & METH_NOARGS && na == 0) {
3843 C_TRACE(x, (*meth)(self,NULL));
3844 }
3845 else if (flags & METH_O && na == 1) {
3846 PyObject *arg = EXT_POP(*pp_stack);
3847 C_TRACE(x, (*meth)(self,arg));
3848 Py_DECREF(arg);
3849 }
3850 else {
3851 err_args(func, flags, na);
3852 x = NULL;
3853 }
3854 }
3855 else {
3856 PyObject *callargs;
3857 callargs = load_args(pp_stack, na);
3858 READ_TIMESTAMP(*pintr0);
3859 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3860 READ_TIMESTAMP(*pintr1);
3861 Py_XDECREF(callargs);
3862 }
3863 } else {
3864 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3865 /* optimize access to bound methods */
3866 PyObject *self = PyMethod_GET_SELF(func);
3867 PCALL(PCALL_METHOD);
3868 PCALL(PCALL_BOUND_METHOD);
3869 Py_INCREF(self);
3870 func = PyMethod_GET_FUNCTION(func);
3871 Py_INCREF(func);
3872 Py_DECREF(*pfunc);
3873 *pfunc = self;
3874 na++;
3875 n++;
3876 } else
3877 Py_INCREF(func);
3878 READ_TIMESTAMP(*pintr0);
3879 if (PyFunction_Check(func))
3880 x = fast_function(func, pp_stack, n, na, nk);
3881 else
3882 x = do_call(func, pp_stack, na, nk);
3883 READ_TIMESTAMP(*pintr1);
3884 Py_DECREF(func);
3885 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 /* Clear the stack of the function object. Also removes
3888 the arguments in case they weren't consumed already
3889 (fast_function() and err_args() leave them on the stack).
3890 */
3891 while ((*pp_stack) > pfunc) {
3892 w = EXT_POP(*pp_stack);
3893 Py_DECREF(w);
3894 PCALL(PCALL_POP);
3895 }
3896 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003897}
3898
Jeremy Hylton192690e2002-08-16 18:36:11 +00003899/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003900 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003901 For the simplest case -- a function that takes only positional
3902 arguments and is called with only positional arguments -- it
3903 inlines the most primitive frame setup code from
3904 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3905 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003906*/
3907
3908static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003909fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3912 PyObject *globals = PyFunction_GET_GLOBALS(func);
3913 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3914 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3915 PyObject **d = NULL;
3916 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 PCALL(PCALL_FUNCTION);
3919 PCALL(PCALL_FAST_FUNCTION);
3920 if (argdefs == NULL && co->co_argcount == n &&
3921 co->co_kwonlyargcount == 0 && nk==0 &&
3922 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3923 PyFrameObject *f;
3924 PyObject *retval = NULL;
3925 PyThreadState *tstate = PyThreadState_GET();
3926 PyObject **fastlocals, **stack;
3927 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PCALL(PCALL_FASTER_FUNCTION);
3930 assert(globals != NULL);
3931 /* XXX Perhaps we should create a specialized
3932 PyFrame_New() that doesn't take locals, but does
3933 take builtins without sanity checking them.
3934 */
3935 assert(tstate != NULL);
3936 f = PyFrame_New(tstate, co, globals, NULL);
3937 if (f == NULL)
3938 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 fastlocals = f->f_localsplus;
3941 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 for (i = 0; i < n; i++) {
3944 Py_INCREF(*stack);
3945 fastlocals[i] = *stack++;
3946 }
3947 retval = PyEval_EvalFrameEx(f,0);
3948 ++tstate->recursion_depth;
3949 Py_DECREF(f);
3950 --tstate->recursion_depth;
3951 return retval;
3952 }
3953 if (argdefs != NULL) {
3954 d = &PyTuple_GET_ITEM(argdefs, 0);
3955 nd = Py_SIZE(argdefs);
3956 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003957 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 (PyObject *)NULL, (*pp_stack)-n, na,
3959 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3960 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003961}
3962
3963static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003964update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3965 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 PyObject *kwdict = NULL;
3968 if (orig_kwdict == NULL)
3969 kwdict = PyDict_New();
3970 else {
3971 kwdict = PyDict_Copy(orig_kwdict);
3972 Py_DECREF(orig_kwdict);
3973 }
3974 if (kwdict == NULL)
3975 return NULL;
3976 while (--nk >= 0) {
3977 int err;
3978 PyObject *value = EXT_POP(*pp_stack);
3979 PyObject *key = EXT_POP(*pp_stack);
3980 if (PyDict_GetItem(kwdict, key) != NULL) {
3981 PyErr_Format(PyExc_TypeError,
3982 "%.200s%s got multiple values "
3983 "for keyword argument '%U'",
3984 PyEval_GetFuncName(func),
3985 PyEval_GetFuncDesc(func),
3986 key);
3987 Py_DECREF(key);
3988 Py_DECREF(value);
3989 Py_DECREF(kwdict);
3990 return NULL;
3991 }
3992 err = PyDict_SetItem(kwdict, key, value);
3993 Py_DECREF(key);
3994 Py_DECREF(value);
3995 if (err) {
3996 Py_DECREF(kwdict);
3997 return NULL;
3998 }
3999 }
4000 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004001}
4002
4003static PyObject *
4004update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 callargs = PyTuple_New(nstack + nstar);
4010 if (callargs == NULL) {
4011 return NULL;
4012 }
4013 if (nstar) {
4014 int i;
4015 for (i = 0; i < nstar; i++) {
4016 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4017 Py_INCREF(a);
4018 PyTuple_SET_ITEM(callargs, nstack + i, a);
4019 }
4020 }
4021 while (--nstack >= 0) {
4022 w = EXT_POP(*pp_stack);
4023 PyTuple_SET_ITEM(callargs, nstack, w);
4024 }
4025 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004026}
4027
4028static PyObject *
4029load_args(PyObject ***pp_stack, int na)
4030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 PyObject *args = PyTuple_New(na);
4032 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 if (args == NULL)
4035 return NULL;
4036 while (--na >= 0) {
4037 w = EXT_POP(*pp_stack);
4038 PyTuple_SET_ITEM(args, na, w);
4039 }
4040 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004041}
4042
4043static PyObject *
4044do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 PyObject *callargs = NULL;
4047 PyObject *kwdict = NULL;
4048 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 if (nk > 0) {
4051 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4052 if (kwdict == NULL)
4053 goto call_fail;
4054 }
4055 callargs = load_args(pp_stack, na);
4056 if (callargs == NULL)
4057 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004058#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 /* At this point, we have to look at the type of func to
4060 update the call stats properly. Do it here so as to avoid
4061 exposing the call stats machinery outside ceval.c
4062 */
4063 if (PyFunction_Check(func))
4064 PCALL(PCALL_FUNCTION);
4065 else if (PyMethod_Check(func))
4066 PCALL(PCALL_METHOD);
4067 else if (PyType_Check(func))
4068 PCALL(PCALL_TYPE);
4069 else if (PyCFunction_Check(func))
4070 PCALL(PCALL_CFUNCTION);
4071 else
4072 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 if (PyCFunction_Check(func)) {
4075 PyThreadState *tstate = PyThreadState_GET();
4076 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4077 }
4078 else
4079 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004080call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 Py_XDECREF(callargs);
4082 Py_XDECREF(kwdict);
4083 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004084}
4085
4086static PyObject *
4087ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 int nstar = 0;
4090 PyObject *callargs = NULL;
4091 PyObject *stararg = NULL;
4092 PyObject *kwdict = NULL;
4093 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 if (flags & CALL_FLAG_KW) {
4096 kwdict = EXT_POP(*pp_stack);
4097 if (!PyDict_Check(kwdict)) {
4098 PyObject *d;
4099 d = PyDict_New();
4100 if (d == NULL)
4101 goto ext_call_fail;
4102 if (PyDict_Update(d, kwdict) != 0) {
4103 Py_DECREF(d);
4104 /* PyDict_Update raises attribute
4105 * error (percolated from an attempt
4106 * to get 'keys' attribute) instead of
4107 * a type error if its second argument
4108 * is not a mapping.
4109 */
4110 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4111 PyErr_Format(PyExc_TypeError,
4112 "%.200s%.200s argument after ** "
4113 "must be a mapping, not %.200s",
4114 PyEval_GetFuncName(func),
4115 PyEval_GetFuncDesc(func),
4116 kwdict->ob_type->tp_name);
4117 }
4118 goto ext_call_fail;
4119 }
4120 Py_DECREF(kwdict);
4121 kwdict = d;
4122 }
4123 }
4124 if (flags & CALL_FLAG_VAR) {
4125 stararg = EXT_POP(*pp_stack);
4126 if (!PyTuple_Check(stararg)) {
4127 PyObject *t = NULL;
4128 t = PySequence_Tuple(stararg);
4129 if (t == NULL) {
4130 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4131 PyErr_Format(PyExc_TypeError,
4132 "%.200s%.200s argument after * "
4133 "must be a sequence, not %200s",
4134 PyEval_GetFuncName(func),
4135 PyEval_GetFuncDesc(func),
4136 stararg->ob_type->tp_name);
4137 }
4138 goto ext_call_fail;
4139 }
4140 Py_DECREF(stararg);
4141 stararg = t;
4142 }
4143 nstar = PyTuple_GET_SIZE(stararg);
4144 }
4145 if (nk > 0) {
4146 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4147 if (kwdict == NULL)
4148 goto ext_call_fail;
4149 }
4150 callargs = update_star_args(na, nstar, stararg, pp_stack);
4151 if (callargs == NULL)
4152 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004153#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 /* At this point, we have to look at the type of func to
4155 update the call stats properly. Do it here so as to avoid
4156 exposing the call stats machinery outside ceval.c
4157 */
4158 if (PyFunction_Check(func))
4159 PCALL(PCALL_FUNCTION);
4160 else if (PyMethod_Check(func))
4161 PCALL(PCALL_METHOD);
4162 else if (PyType_Check(func))
4163 PCALL(PCALL_TYPE);
4164 else if (PyCFunction_Check(func))
4165 PCALL(PCALL_CFUNCTION);
4166 else
4167 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004168#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 if (PyCFunction_Check(func)) {
4170 PyThreadState *tstate = PyThreadState_GET();
4171 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4172 }
4173 else
4174 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004175ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 Py_XDECREF(callargs);
4177 Py_XDECREF(kwdict);
4178 Py_XDECREF(stararg);
4179 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004180}
4181
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004182/* Extract a slice index from a PyInt or PyLong or an object with the
4183 nb_index slot defined, and store in *pi.
4184 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4185 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004186 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004187*/
Tim Petersb5196382001-12-16 19:44:20 +00004188/* Note: If v is NULL, return success without storing into *pi. This
4189 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4190 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004191*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004192int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004193_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 if (v != NULL) {
4196 Py_ssize_t x;
4197 if (PyIndex_Check(v)) {
4198 x = PyNumber_AsSsize_t(v, NULL);
4199 if (x == -1 && PyErr_Occurred())
4200 return 0;
4201 }
4202 else {
4203 PyErr_SetString(PyExc_TypeError,
4204 "slice indices must be integers or "
4205 "None or have an __index__ method");
4206 return 0;
4207 }
4208 *pi = x;
4209 }
4210 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004211}
4212
Guido van Rossum486364b2007-06-30 05:01:58 +00004213#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004215
Guido van Rossumb209a111997-04-29 18:18:01 +00004216static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004217cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 int res = 0;
4220 switch (op) {
4221 case PyCmp_IS:
4222 res = (v == w);
4223 break;
4224 case PyCmp_IS_NOT:
4225 res = (v != w);
4226 break;
4227 case PyCmp_IN:
4228 res = PySequence_Contains(w, v);
4229 if (res < 0)
4230 return NULL;
4231 break;
4232 case PyCmp_NOT_IN:
4233 res = PySequence_Contains(w, v);
4234 if (res < 0)
4235 return NULL;
4236 res = !res;
4237 break;
4238 case PyCmp_EXC_MATCH:
4239 if (PyTuple_Check(w)) {
4240 Py_ssize_t i, length;
4241 length = PyTuple_Size(w);
4242 for (i = 0; i < length; i += 1) {
4243 PyObject *exc = PyTuple_GET_ITEM(w, i);
4244 if (!PyExceptionClass_Check(exc)) {
4245 PyErr_SetString(PyExc_TypeError,
4246 CANNOT_CATCH_MSG);
4247 return NULL;
4248 }
4249 }
4250 }
4251 else {
4252 if (!PyExceptionClass_Check(w)) {
4253 PyErr_SetString(PyExc_TypeError,
4254 CANNOT_CATCH_MSG);
4255 return NULL;
4256 }
4257 }
4258 res = PyErr_GivenExceptionMatches(v, w);
4259 break;
4260 default:
4261 return PyObject_RichCompare(v, w, op);
4262 }
4263 v = res ? Py_True : Py_False;
4264 Py_INCREF(v);
4265 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004266}
4267
Thomas Wouters52152252000-08-17 22:55:00 +00004268static PyObject *
4269import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 x = PyObject_GetAttr(v, name);
4274 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4275 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4276 }
4277 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004278}
Guido van Rossumac7be682001-01-17 15:42:30 +00004279
Thomas Wouters52152252000-08-17 22:55:00 +00004280static int
4281import_all_from(PyObject *locals, PyObject *v)
4282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 PyObject *all = PyObject_GetAttrString(v, "__all__");
4284 PyObject *dict, *name, *value;
4285 int skip_leading_underscores = 0;
4286 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 if (all == NULL) {
4289 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4290 return -1; /* Unexpected error */
4291 PyErr_Clear();
4292 dict = PyObject_GetAttrString(v, "__dict__");
4293 if (dict == NULL) {
4294 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4295 return -1;
4296 PyErr_SetString(PyExc_ImportError,
4297 "from-import-* object has no __dict__ and no __all__");
4298 return -1;
4299 }
4300 all = PyMapping_Keys(dict);
4301 Py_DECREF(dict);
4302 if (all == NULL)
4303 return -1;
4304 skip_leading_underscores = 1;
4305 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 for (pos = 0, err = 0; ; pos++) {
4308 name = PySequence_GetItem(all, pos);
4309 if (name == NULL) {
4310 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4311 err = -1;
4312 else
4313 PyErr_Clear();
4314 break;
4315 }
4316 if (skip_leading_underscores &&
4317 PyUnicode_Check(name) &&
4318 PyUnicode_AS_UNICODE(name)[0] == '_')
4319 {
4320 Py_DECREF(name);
4321 continue;
4322 }
4323 value = PyObject_GetAttr(v, name);
4324 if (value == NULL)
4325 err = -1;
4326 else if (PyDict_CheckExact(locals))
4327 err = PyDict_SetItem(locals, name, value);
4328 else
4329 err = PyObject_SetItem(locals, name, value);
4330 Py_DECREF(name);
4331 Py_XDECREF(value);
4332 if (err != 0)
4333 break;
4334 }
4335 Py_DECREF(all);
4336 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004337}
4338
Guido van Rossumac7be682001-01-17 15:42:30 +00004339static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004340format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 if (!obj)
4345 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 obj_str = _PyUnicode_AsString(obj);
4348 if (!obj_str)
4349 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004352}
Guido van Rossum950361c1997-01-24 13:49:28 +00004353
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004354static void
4355format_exc_unbound(PyCodeObject *co, int oparg)
4356{
4357 PyObject *name;
4358 /* Don't stomp existing exception */
4359 if (PyErr_Occurred())
4360 return;
4361 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4362 name = PyTuple_GET_ITEM(co->co_cellvars,
4363 oparg);
4364 format_exc_check_arg(
4365 PyExc_UnboundLocalError,
4366 UNBOUNDLOCAL_ERROR_MSG,
4367 name);
4368 } else {
4369 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4370 PyTuple_GET_SIZE(co->co_cellvars));
4371 format_exc_check_arg(PyExc_NameError,
4372 UNBOUNDFREE_ERROR_MSG, name);
4373 }
4374}
4375
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004376static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004377unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 /* This function implements 'variable += expr' when both arguments
4381 are (Unicode) strings. */
4382 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4383 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4384 Py_ssize_t new_len = v_len + w_len;
4385 if (new_len < 0) {
4386 PyErr_SetString(PyExc_OverflowError,
4387 "strings are too large to concat");
4388 return NULL;
4389 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004390
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004391 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 /* In the common case, there are 2 references to the value
4393 * stored in 'variable' when the += is performed: one on the
4394 * value stack (in 'v') and one still stored in the
4395 * 'variable'. We try to delete the variable now to reduce
4396 * the refcnt to 1.
4397 */
4398 switch (*next_instr) {
4399 case STORE_FAST:
4400 {
4401 int oparg = PEEKARG();
4402 PyObject **fastlocals = f->f_localsplus;
4403 if (GETLOCAL(oparg) == v)
4404 SETLOCAL(oparg, NULL);
4405 break;
4406 }
4407 case STORE_DEREF:
4408 {
4409 PyObject **freevars = (f->f_localsplus +
4410 f->f_code->co_nlocals);
4411 PyObject *c = freevars[PEEKARG()];
4412 if (PyCell_GET(c) == v)
4413 PyCell_Set(c, NULL);
4414 break;
4415 }
4416 case STORE_NAME:
4417 {
4418 PyObject *names = f->f_code->co_names;
4419 PyObject *name = GETITEM(names, PEEKARG());
4420 PyObject *locals = f->f_locals;
4421 if (PyDict_CheckExact(locals) &&
4422 PyDict_GetItem(locals, name) == v) {
4423 if (PyDict_DelItem(locals, name) != 0) {
4424 PyErr_Clear();
4425 }
4426 }
4427 break;
4428 }
4429 }
4430 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004431
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004432 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 /* Now we own the last reference to 'v', so we can resize it
4434 * in-place.
4435 */
4436 if (PyUnicode_Resize(&v, new_len) != 0) {
4437 /* XXX if PyUnicode_Resize() fails, 'v' has been
4438 * deallocated so it cannot be put back into
4439 * 'variable'. The MemoryError is raised when there
4440 * is no value in 'variable', which might (very
4441 * remotely) be a cause of incompatibilities.
4442 */
4443 return NULL;
4444 }
4445 /* copy 'w' into the newly allocated area of 'v' */
4446 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4447 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4448 return v;
4449 }
4450 else {
4451 /* When in-place resizing is not an option. */
4452 w = PyUnicode_Concat(v, w);
4453 Py_DECREF(v);
4454 return w;
4455 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004456}
4457
Guido van Rossum950361c1997-01-24 13:49:28 +00004458#ifdef DYNAMIC_EXECUTION_PROFILE
4459
Skip Montanarof118cb12001-10-15 20:51:38 +00004460static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004461getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 int i;
4464 PyObject *l = PyList_New(256);
4465 if (l == NULL) return NULL;
4466 for (i = 0; i < 256; i++) {
4467 PyObject *x = PyLong_FromLong(a[i]);
4468 if (x == NULL) {
4469 Py_DECREF(l);
4470 return NULL;
4471 }
4472 PyList_SetItem(l, i, x);
4473 }
4474 for (i = 0; i < 256; i++)
4475 a[i] = 0;
4476 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004477}
4478
4479PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004480_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004481{
4482#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004484#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 int i;
4486 PyObject *l = PyList_New(257);
4487 if (l == NULL) return NULL;
4488 for (i = 0; i < 257; i++) {
4489 PyObject *x = getarray(dxpairs[i]);
4490 if (x == NULL) {
4491 Py_DECREF(l);
4492 return NULL;
4493 }
4494 PyList_SetItem(l, i, x);
4495 }
4496 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004497#endif
4498}
4499
4500#endif