blob: e83339b0fd8caec11ea06066868e49195f5777a6 [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
Ezio Melotti13925002011-03-16 11:05:33 +020029/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000030 "__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);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500141static PyObject * special_lookup(PyObject *, _Py_Identifier *);
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{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200375 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject *threading, *result;
377 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!gil_created())
380 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 recreate_gil();
382 pending_lock = PyThread_allocate_lock();
383 take_gil(tstate);
384 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Update the threading module with the new state.
387 */
388 tstate = PyThreadState_GET();
389 threading = PyMapping_GetItemString(tstate->interp->modules,
390 "threading");
391 if (threading == NULL) {
392 /* threading not imported */
393 PyErr_Clear();
394 return;
395 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (result == NULL)
398 PyErr_WriteUnraisable(threading);
399 else
400 Py_DECREF(result);
401 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000402}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000403
404#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000405static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406static int pending_async_exc = 0;
407#endif /* WITH_THREAD */
408
409/* This function is used to signal that async exceptions are waiting to be
410 raised, therefore it is also useful in non-threaded builds. */
411
412void
413_PyEval_SignalAsyncExc(void)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000416}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417
Guido van Rossumff4949e1992-08-05 19:58:53 +0000418/* Functions save_thread and restore_thread are always defined so
419 dynamically loaded modules needn't be compiled separately for use
420 with and without threads: */
421
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000422PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyThreadState *tstate = PyThreadState_Swap(NULL);
426 if (tstate == NULL)
427 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000428#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (gil_created())
430 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433}
434
435void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (tstate == NULL)
439 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (gil_created()) {
442 int err = errno;
443 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200444 /* _Py_Finalizing is protected by the GIL */
445 if (_Py_Finalizing && tstate != _Py_Finalizing) {
446 drop_gil(tstate);
447 PyThread_exit_thread();
448 assert(0); /* unreachable */
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 errno = err;
451 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454}
455
456
Guido van Rossuma9672091994-09-14 13:31:22 +0000457/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
458 signal handlers or Mac I/O completion routines) can schedule calls
459 to a function to be called synchronously.
460 The synchronous function is called with one void* argument.
461 It should return 0 for success or -1 for failure -- failure should
462 be accompanied by an exception.
463
464 If registry succeeds, the registry function returns 0; if it fails
465 (e.g. due to too many pending calls) it returns -1 (without setting
466 an exception condition).
467
468 Note that because registry may occur from within signal handlers,
469 or other asynchronous events, calling malloc() is unsafe!
470
471#ifdef WITH_THREAD
472 Any thread can schedule pending calls, but only the main thread
473 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474 There is no facility to schedule calls to a particular thread, but
475 that should be easy to change, should that ever be required. In
476 that case, the static variables here should go into the python
477 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000480
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481#ifdef WITH_THREAD
482
483/* The WITH_THREAD implementation is thread-safe. It allows
484 scheduling to be made from any thread, and even from an executing
485 callback.
486 */
487
488#define NPENDINGCALLS 32
489static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 int (*func)(void *);
491 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492} pendingcalls[NPENDINGCALLS];
493static int pendingfirst = 0;
494static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200541 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int i;
543 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!pending_lock) {
546 /* initial allocation of the lock */
547 pending_lock = PyThread_allocate_lock();
548 if (pending_lock == NULL)
549 return -1;
550 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* only service pending calls on main thread */
553 if (main_thread && PyThread_get_thread_ident() != main_thread)
554 return 0;
555 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200556 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200558 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* perform a bounded number of calls, in case of recursion */
560 for (i=0; i<NPENDINGCALLS; i++) {
561 int j;
562 int (*func)(void *);
563 void *arg = NULL;
564
565 /* pop one item off the queue while holding the lock */
566 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
567 j = pendingfirst;
568 if (j == pendinglast) {
569 func = NULL; /* Queue empty */
570 } else {
571 func = pendingcalls[j].func;
572 arg = pendingcalls[j].arg;
573 pendingfirst = (j + 1) % NPENDINGCALLS;
574 }
575 if (pendingfirst != pendinglast)
576 SIGNAL_PENDING_CALLS();
577 else
578 UNSIGNAL_PENDING_CALLS();
579 PyThread_release_lock(pending_lock);
580 /* having released the lock, perform the callback */
581 if (func == NULL)
582 break;
583 r = func(arg);
584 if (r)
585 break;
586 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200587 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000589}
590
591#else /* if ! defined WITH_THREAD */
592
593/*
594 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
595 This code is used for signal handling in python that isn't built
596 with WITH_THREAD.
597 Don't use this implementation when Py_AddPendingCalls() can happen
598 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 (1) nested asynchronous calls to Py_AddPendingCall()
602 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) is very unlikely because typically signal delivery
605 is blocked during signal handling. So it should be impossible.
606 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 The current code is safe against (2), but not against (1).
608 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 thread is present, interrupted by signals, and that the critical
610 section is protected with the "busy" variable. On Windows, which
611 delivers SIGINT on a system thread, this does not hold and therefore
612 Windows really shouldn't use this version.
613 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000614*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000615
Guido van Rossuma9672091994-09-14 13:31:22 +0000616#define NPENDINGCALLS 32
617static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 int (*func)(void *);
619 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000620} pendingcalls[NPENDINGCALLS];
621static volatile int pendingfirst = 0;
622static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000623static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000624
625int
Thomas Wouters334fb892000-07-25 12:56:38 +0000626Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 static volatile int busy = 0;
629 int i, j;
630 /* XXX Begin critical section */
631 if (busy)
632 return -1;
633 busy = 1;
634 i = pendinglast;
635 j = (i + 1) % NPENDINGCALLS;
636 if (j == pendingfirst) {
637 busy = 0;
638 return -1; /* Queue full */
639 }
640 pendingcalls[i].func = func;
641 pendingcalls[i].arg = arg;
642 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 SIGNAL_PENDING_CALLS();
645 busy = 0;
646 /* XXX End critical section */
647 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000648}
649
Guido van Rossum180d7b41994-09-29 09:45:57 +0000650int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 static int busy = 0;
654 if (busy)
655 return 0;
656 busy = 1;
657 UNSIGNAL_PENDING_CALLS();
658 for (;;) {
659 int i;
660 int (*func)(void *);
661 void *arg;
662 i = pendingfirst;
663 if (i == pendinglast)
664 break; /* Queue empty */
665 func = pendingcalls[i].func;
666 arg = pendingcalls[i].arg;
667 pendingfirst = (i + 1) % NPENDINGCALLS;
668 if (func(arg) < 0) {
669 busy = 0;
670 SIGNAL_PENDING_CALLS(); /* We're not done yet */
671 return -1;
672 }
673 }
674 busy = 0;
675 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000676}
677
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000678#endif /* WITH_THREAD */
679
Guido van Rossuma9672091994-09-14 13:31:22 +0000680
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681/* The interpreter's recursion limit */
682
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000683#ifndef Py_DEFAULT_RECURSION_LIMIT
684#define Py_DEFAULT_RECURSION_LIMIT 1000
685#endif
686static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
687int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000689int
690Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000695void
696Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 recursion_limit = new_limit;
699 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700}
701
Armin Rigo2b3eb402003-10-28 12:05:48 +0000702/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
703 if the recursion_depth reaches _Py_CheckRecursionLimit.
704 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
705 to guarantee that _Py_CheckRecursiveCall() is regularly called.
706 Without USE_STACKCHECK, there is no need for this. */
707int
708_Py_CheckRecursiveCall(char *where)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711
712#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (PyOS_CheckStack()) {
714 --tstate->recursion_depth;
715 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
716 return -1;
717 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 _Py_CheckRecursionLimit = recursion_limit;
720 if (tstate->recursion_critical)
721 /* Somebody asked that we don't check for recursion. */
722 return 0;
723 if (tstate->overflowed) {
724 if (tstate->recursion_depth > recursion_limit + 50) {
725 /* Overflowing while handling an overflow. Give up. */
726 Py_FatalError("Cannot recover from stack overflow.");
727 }
728 return 0;
729 }
730 if (tstate->recursion_depth > recursion_limit) {
731 --tstate->recursion_depth;
732 tstate->overflowed = 1;
733 PyErr_Format(PyExc_RuntimeError,
734 "maximum recursion depth exceeded%s",
735 where);
736 return -1;
737 }
738 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000739}
740
Guido van Rossum374a9221991-04-04 10:40:29 +0000741/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000742enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000743 WHY_NOT = 0x0001, /* No error */
744 WHY_EXCEPTION = 0x0002, /* Exception occurred */
745 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
746 WHY_RETURN = 0x0008, /* 'return' statement */
747 WHY_BREAK = 0x0010, /* 'break' statement */
748 WHY_CONTINUE = 0x0020, /* 'continue' statement */
749 WHY_YIELD = 0x0040, /* 'yield' operator */
750 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000751};
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Benjamin Peterson87880242011-07-03 16:48:31 -0500753static void save_exc_state(PyThreadState *, PyFrameObject *);
754static void swap_exc_state(PyThreadState *, PyFrameObject *);
755static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Collin Winter828f04a2007-08-31 00:04:24 +0000756static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000757static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000758
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000759/* Records whether tracing is on for any thread. Counts the number of
760 threads for which tstate->c_tracefunc is non-NULL, so if the value
761 is 0, we know we don't have to check this thread's c_tracefunc.
762 This speeds up the if statement in PyEval_EvalFrameEx() after
763 fast_next_opcode*/
764static int _Py_TracingPossible = 0;
765
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767
Guido van Rossumb209a111997-04-29 18:18:01 +0000768PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000769PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return PyEval_EvalCodeEx(co,
772 globals, locals,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 (PyObject **)NULL, 0,
776 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777}
778
779
780/* Interpreter main loop */
781
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000782PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000783PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* This is for backward compatibility with extension modules that
785 used this API; core interpreter code should call
786 PyEval_EvalFrameEx() */
787 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000788}
789
790PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000792{
Guido van Rossum950361c1997-01-24 13:49:28 +0000793#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000795#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 register PyObject **stack_pointer; /* Next free slot in value stack */
797 register unsigned char *next_instr;
798 register int opcode; /* Current opcode */
799 register int oparg; /* Current opcode argument, if any */
800 register enum why_code why; /* Reason for block stack unwind */
801 register int err; /* Error status -- nonzero if error */
802 register PyObject *x; /* Result object -- NULL if error */
803 register PyObject *v; /* Temporary objects popped off stack */
804 register PyObject *w;
805 register PyObject *u;
806 register PyObject *t;
807 register PyObject **fastlocals, **freevars;
808 PyObject *retval = NULL; /* Return value */
809 PyThreadState *tstate = PyThreadState_GET();
810 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 is true when the line being executed has changed. The
817 initial values are such as to make this false the first
818 time it is tested. */
819 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 unsigned char *first_instr;
822 PyObject *names;
823 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000824
Brett Cannon368b4b72012-04-02 12:17:59 -0400825#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200826 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400827#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200828
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829/* Computed GOTOs, or
830 the-optimization-commonly-but-improperly-known-as-"threaded code"
831 using gcc's labels-as-values extension
832 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
833
834 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836 combined with a lookup table of jump addresses. However, since the
837 indirect jump instruction is shared by all opcodes, the CPU will have a
838 hard time making the right prediction for where to jump next (actually,
839 it will be always wrong except in the uncommon case of a sequence of
840 several identical opcodes).
841
842 "Threaded code" in contrast, uses an explicit jump table and an explicit
843 indirect jump instruction at the end of each opcode. Since the jump
844 instruction is at a different address for each opcode, the CPU will make a
845 separate prediction for each of these instructions, which is equivalent to
846 predicting the second opcode of each opcode pair. These predictions have
847 a much better chance to turn out valid, especially in small bytecode loops.
848
849 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000851 and potentially many more instructions (depending on the pipeline width).
852 A correctly predicted branch, however, is nearly free.
853
854 At the time of this writing, the "threaded code" version is up to 15-20%
855 faster than the normal "switch" version, depending on the compiler and the
856 CPU architecture.
857
858 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
859 because it would render the measurements invalid.
860
861
862 NOTE: care must be taken that the compiler doesn't try to "optimize" the
863 indirect jumps by sharing them between all opcodes. Such optimizations
864 can be disabled on gcc by using the -fno-gcse flag (or possibly
865 -fno-crossjumping).
866*/
867
Antoine Pitrou042b1282010-08-13 21:15:58 +0000868#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000870#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#endif
872
Antoine Pitrou042b1282010-08-13 21:15:58 +0000873#ifdef HAVE_COMPUTED_GOTOS
874 #ifndef USE_COMPUTED_GOTOS
875 #define USE_COMPUTED_GOTOS 1
876 #endif
877#else
878 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
879 #error "Computed gotos are not supported on this compiler."
880 #endif
881 #undef USE_COMPUTED_GOTOS
882 #define USE_COMPUTED_GOTOS 0
883#endif
884
885#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000886/* Import the static jump table */
887#include "opcode_targets.h"
888
889/* This macro is used when several opcodes defer to the same implementation
890 (e.g. SETUP_LOOP, SETUP_FINALLY) */
891#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 TARGET_##op: \
893 opcode = op; \
894 if (HAS_ARG(op)) \
895 oparg = NEXTARG(); \
896 case op: \
897 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000898
899#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 TARGET_##op: \
901 opcode = op; \
902 if (HAS_ARG(op)) \
903 oparg = NEXTARG(); \
904 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000905
906
907#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 { \
909 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
910 FAST_DISPATCH(); \
911 } \
912 continue; \
913 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914
915#ifdef LLTRACE
916#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 { \
918 if (!lltrace && !_Py_TracingPossible) { \
919 f->f_lasti = INSTR_OFFSET(); \
920 goto *opcode_targets[*next_instr++]; \
921 } \
922 goto fast_next_opcode; \
923 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924#else
925#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 { \
927 if (!_Py_TracingPossible) { \
928 f->f_lasti = INSTR_OFFSET(); \
929 goto *opcode_targets[*next_instr++]; \
930 } \
931 goto fast_next_opcode; \
932 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000933#endif
934
935#else
936#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 /* silence compiler warnings about `impl` unused */ \
940 if (0) goto impl; \
941 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000942#define DISPATCH() continue
943#define FAST_DISPATCH() goto fast_next_opcode
944#endif
945
946
Neal Norwitza81d2202002-07-14 00:27:26 +0000947/* Tuple access macros */
948
949#ifndef Py_DEBUG
950#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
951#else
952#define GETITEM(v, i) PyTuple_GetItem((v), (i))
953#endif
954
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000955#ifdef WITH_TSC
956/* Use Pentium timestamp counter to mark certain events:
957 inst0 -- beginning of switch statement for opcode dispatch
958 inst1 -- end of switch statement (may be skipped)
959 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000961 (may be skipped)
962 intr1 -- beginning of long interruption
963 intr2 -- end of long interruption
964
965 Many opcodes call out to helper C functions. In some cases, the
966 time in those functions should be counted towards the time for the
967 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
968 calls another Python function; there's no point in charge all the
969 bytecode executed by the called function to the caller.
970
971 It's hard to make a useful judgement statically. In the presence
972 of operator overloading, it's impossible to tell if a call will
973 execute new Python code or not.
974
975 It's a case-by-case judgement. I'll use intr1 for the following
976 cases:
977
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978 IMPORT_STAR
979 IMPORT_FROM
980 CALL_FUNCTION (and friends)
981
982 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
984 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 READ_TIMESTAMP(inst0);
987 READ_TIMESTAMP(inst1);
988 READ_TIMESTAMP(loop0);
989 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 /* shut up the compiler */
992 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000993#endif
994
Guido van Rossum374a9221991-04-04 10:40:29 +0000995/* Code access macros */
996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997#define INSTR_OFFSET() ((int)(next_instr - first_instr))
998#define NEXTOP() (*next_instr++)
999#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
1000#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
1001#define JUMPTO(x) (next_instr = first_instr + (x))
1002#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001003
Raymond Hettingerf606f872003-03-16 03:11:04 +00001004/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Some opcodes tend to come in pairs thus making it possible to
1006 predict the second code when the first is run. For example,
1007 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1008 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Verifying the prediction costs a single high-speed test of a register
1011 variable against a constant. If the pairing was good, then the
1012 processor's own internal branch predication has a high likelihood of
1013 success, resulting in a nearly zero-overhead transition to the
1014 next opcode. A successful prediction saves a trip through the eval-loop
1015 including its two unpredictable branches, the HAS_ARG test and the
1016 switch-case. Combined with the processor's internal branch prediction,
1017 a successful PREDICT has the effect of making the two opcodes run as if
1018 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001019
Georg Brandl86b2fb92008-07-16 03:43:04 +00001020 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 predictions turned-on and interpret the results as if some opcodes
1022 had been combined or turn-off predictions so that the opcode frequency
1023 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
1025 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 the CPU to record separate branch prediction information for each
1027 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001028
Raymond Hettingerf606f872003-03-16 03:11:04 +00001029*/
1030
Antoine Pitrou042b1282010-08-13 21:15:58 +00001031#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032#define PREDICT(op) if (0) goto PRED_##op
1033#define PREDICTED(op) PRED_##op:
1034#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001035#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1037#define PREDICTED(op) PRED_##op: next_instr++
1038#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001039#endif
1040
Raymond Hettingerf606f872003-03-16 03:11:04 +00001041
Guido van Rossum374a9221991-04-04 10:40:29 +00001042/* Stack manipulation macros */
1043
Martin v. Löwis18e16552006-02-15 17:27:45 +00001044/* The stack can grow at most MAXINT deep, as co_nlocals and
1045 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001046#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1047#define EMPTY() (STACK_LEVEL() == 0)
1048#define TOP() (stack_pointer[-1])
1049#define SECOND() (stack_pointer[-2])
1050#define THIRD() (stack_pointer[-3])
1051#define FOURTH() (stack_pointer[-4])
1052#define PEEK(n) (stack_pointer[-(n)])
1053#define SET_TOP(v) (stack_pointer[-1] = (v))
1054#define SET_SECOND(v) (stack_pointer[-2] = (v))
1055#define SET_THIRD(v) (stack_pointer[-3] = (v))
1056#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1057#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1058#define BASIC_STACKADJ(n) (stack_pointer += n)
1059#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1060#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001061
Guido van Rossum96a42c81992-01-12 02:29:51 +00001062#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001064 lltrace && prtrace(TOP(), "push")); \
1065 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001069 lltrace && prtrace(TOP(), "stackadj")); \
1070 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001071#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001072 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1073 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001074#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001075#define PUSH(v) BASIC_PUSH(v)
1076#define POP() BASIC_POP()
1077#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001078#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001079#endif
1080
Guido van Rossum681d79a1995-07-18 14:51:37 +00001081/* Local variable macros */
1082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001084
1085/* The SETLOCAL() macro must not DECREF the local variable in-place and
1086 then store the new value; it must copy the old value to a temporary
1087 value, then store the new value, and then DECREF the temporary value.
1088 This is because it is possible that during the DECREF the frame is
1089 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1090 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001092 GETLOCAL(i) = value; \
1093 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001094
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001095
1096#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 while (STACK_LEVEL() > (b)->b_level) { \
1098 PyObject *v = POP(); \
1099 Py_XDECREF(v); \
1100 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001101
1102#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 { \
1104 PyObject *type, *value, *traceback; \
1105 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1106 while (STACK_LEVEL() > (b)->b_level + 3) { \
1107 value = POP(); \
1108 Py_XDECREF(value); \
1109 } \
1110 type = tstate->exc_type; \
1111 value = tstate->exc_value; \
1112 traceback = tstate->exc_traceback; \
1113 tstate->exc_type = POP(); \
1114 tstate->exc_value = POP(); \
1115 tstate->exc_traceback = POP(); \
1116 Py_XDECREF(type); \
1117 Py_XDECREF(value); \
1118 Py_XDECREF(traceback); \
1119 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001120
Guido van Rossuma027efa1997-05-05 20:56:21 +00001121/* Start of code */
1122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* push frame */
1124 if (Py_EnterRecursiveCall(""))
1125 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (tstate->use_tracing) {
1130 if (tstate->c_tracefunc != NULL) {
1131 /* tstate->c_tracefunc, if defined, is a
1132 function that will be called on *every* entry
1133 to a code block. Its return value, if not
1134 None, is a function that will be called at
1135 the start of each executed line of code.
1136 (Actually, the function must return itself
1137 in order to continue tracing.) The trace
1138 functions are called with three arguments:
1139 a pointer to the current frame, a string
1140 indicating why the function is called, and
1141 an argument which depends on the situation.
1142 The global trace function is also called
1143 whenever an exception is detected. */
1144 if (call_trace_protected(tstate->c_tracefunc,
1145 tstate->c_traceobj,
1146 f, PyTrace_CALL, Py_None)) {
1147 /* Trace function raised an error */
1148 goto exit_eval_frame;
1149 }
1150 }
1151 if (tstate->c_profilefunc != NULL) {
1152 /* Similar for c_profilefunc, except it needn't
1153 return itself and isn't called for "line" events */
1154 if (call_trace_protected(tstate->c_profilefunc,
1155 tstate->c_profileobj,
1156 f, PyTrace_CALL, Py_None)) {
1157 /* Profile function raised an error */
1158 goto exit_eval_frame;
1159 }
1160 }
1161 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 co = f->f_code;
1164 names = co->co_names;
1165 consts = co->co_consts;
1166 fastlocals = f->f_localsplus;
1167 freevars = f->f_localsplus + co->co_nlocals;
1168 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1169 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 f->f_lasti now refers to the index of the last instruction
1172 executed. You might think this was obvious from the name, but
1173 this wasn't always true before 2.3! PyFrame_New now sets
1174 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1175 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1176 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001177 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1178 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 When the PREDICT() macros are enabled, some opcode pairs follow in
1181 direct succession without updating f->f_lasti. A successful
1182 prediction effectively links the two codes together as if they
1183 were a single new opcode; accordingly,f->f_lasti will point to
1184 the first code in the pair (for instance, GET_ITER followed by
1185 FOR_ITER is effectively a single opcode and f->f_lasti will point
1186 at to the beginning of the combined pair.)
1187 */
1188 next_instr = first_instr + f->f_lasti + 1;
1189 stack_pointer = f->f_stacktop;
1190 assert(stack_pointer != NULL);
1191 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 if (co->co_flags & CO_GENERATOR && !throwflag) {
1194 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1195 /* We were in an except handler when we left,
1196 restore the exception state which was put aside
1197 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001198 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001200 else
1201 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001203
Tim Peters5ca576e2001-06-18 22:08:13 +00001204#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001205 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001206#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 why = WHY_NOT;
1209 err = 0;
1210 x = Py_None; /* Not a reference, just anything non-NULL */
1211 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (throwflag) { /* support for generator.throw() */
1214 why = WHY_EXCEPTION;
1215 goto on_error;
1216 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001219#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (inst1 == 0) {
1221 /* Almost surely, the opcode executed a break
1222 or a continue, preventing inst1 from being set
1223 on the way out of the loop.
1224 */
1225 READ_TIMESTAMP(inst1);
1226 loop1 = inst1;
1227 }
1228 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1229 intr0, intr1);
1230 ticked = 0;
1231 inst1 = 0;
1232 intr0 = 0;
1233 intr1 = 0;
1234 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1237 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 /* Do periodic things. Doing this every time through
1240 the loop would add too much overhead, so we do it
1241 only every Nth instruction. We also do it if
1242 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1243 event needs attention (e.g. a signal handler or
1244 async I/O handler); see Py_AddPendingCall() and
1245 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1248 if (*next_instr == SETUP_FINALLY) {
1249 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001250 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 goto fast_next_opcode;
1252 }
1253 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001254#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001256#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1258 if (Py_MakePendingCalls() < 0) {
1259 why = WHY_EXCEPTION;
1260 goto on_error;
1261 }
1262 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001263#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001264 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 /* Give another thread a chance */
1266 if (PyThreadState_Swap(NULL) != tstate)
1267 Py_FatalError("ceval: tstate mix-up");
1268 drop_gil(tstate);
1269
1270 /* Other threads may run now */
1271
1272 take_gil(tstate);
1273 if (PyThreadState_Swap(tstate) != NULL)
1274 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001276#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* Check for asynchronous exceptions. */
1278 if (tstate->async_exc != NULL) {
1279 x = tstate->async_exc;
1280 tstate->async_exc = NULL;
1281 UNSIGNAL_ASYNC_EXC();
1282 PyErr_SetNone(x);
1283 Py_DECREF(x);
1284 why = WHY_EXCEPTION;
1285 goto on_error;
1286 }
1287 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 fast_next_opcode:
1290 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (_Py_TracingPossible &&
1295 tstate->c_tracefunc != NULL && !tstate->tracing) {
1296 /* see maybe_call_line_trace
1297 for expository comments */
1298 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 err = maybe_call_line_trace(tstate->c_tracefunc,
1301 tstate->c_traceobj,
1302 f, &instr_lb, &instr_ub,
1303 &instr_prev);
1304 /* Reload possibly changed frame fields */
1305 JUMPTO(f->f_lasti);
1306 if (f->f_stacktop != NULL) {
1307 stack_pointer = f->f_stacktop;
1308 f->f_stacktop = NULL;
1309 }
1310 if (err) {
1311 /* trace function raised an exception */
1312 goto on_error;
1313 }
1314 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 opcode = NEXTOP();
1319 oparg = 0; /* allows oparg to be stored in a register because
1320 it doesn't have to be remembered across a full loop */
1321 if (HAS_ARG(opcode))
1322 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001323 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001324#ifdef DYNAMIC_EXECUTION_PROFILE
1325#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 dxpairs[lastopcode][opcode]++;
1327 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001328#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001330#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001331
Guido van Rossum96a42c81992-01-12 02:29:51 +00001332#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (lltrace) {
1336 if (HAS_ARG(opcode)) {
1337 printf("%d: %d, %d\n",
1338 f->f_lasti, opcode, oparg);
1339 }
1340 else {
1341 printf("%d: %d\n",
1342 f->f_lasti, opcode);
1343 }
1344 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001345#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 /* Main switch on opcode */
1348 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 /* BEWARE!
1353 It is essential that any operation that fails sets either
1354 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1355 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 TARGET(NOP)
1358 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 TARGET(LOAD_FAST)
1361 x = GETLOCAL(oparg);
1362 if (x != NULL) {
1363 Py_INCREF(x);
1364 PUSH(x);
1365 FAST_DISPATCH();
1366 }
1367 format_exc_check_arg(PyExc_UnboundLocalError,
1368 UNBOUNDLOCAL_ERROR_MSG,
1369 PyTuple_GetItem(co->co_varnames, oparg));
1370 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 TARGET(LOAD_CONST)
1373 x = GETITEM(consts, oparg);
1374 Py_INCREF(x);
1375 PUSH(x);
1376 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PREDICTED_WITH_ARG(STORE_FAST);
1379 TARGET(STORE_FAST)
1380 v = POP();
1381 SETLOCAL(oparg, v);
1382 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 TARGET(POP_TOP)
1385 v = POP();
1386 Py_DECREF(v);
1387 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 TARGET(ROT_TWO)
1390 v = TOP();
1391 w = SECOND();
1392 SET_TOP(w);
1393 SET_SECOND(v);
1394 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 TARGET(ROT_THREE)
1397 v = TOP();
1398 w = SECOND();
1399 x = THIRD();
1400 SET_TOP(w);
1401 SET_SECOND(x);
1402 SET_THIRD(v);
1403 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 TARGET(DUP_TOP)
1406 v = TOP();
1407 Py_INCREF(v);
1408 PUSH(v);
1409 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001411 TARGET(DUP_TOP_TWO)
1412 x = TOP();
1413 Py_INCREF(x);
1414 w = SECOND();
1415 Py_INCREF(w);
1416 STACKADJ(2);
1417 SET_TOP(x);
1418 SET_SECOND(w);
1419 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 TARGET(UNARY_POSITIVE)
1422 v = TOP();
1423 x = PyNumber_Positive(v);
1424 Py_DECREF(v);
1425 SET_TOP(x);
1426 if (x != NULL) DISPATCH();
1427 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 TARGET(UNARY_NEGATIVE)
1430 v = TOP();
1431 x = PyNumber_Negative(v);
1432 Py_DECREF(v);
1433 SET_TOP(x);
1434 if (x != NULL) DISPATCH();
1435 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 TARGET(UNARY_NOT)
1438 v = TOP();
1439 err = PyObject_IsTrue(v);
1440 Py_DECREF(v);
1441 if (err == 0) {
1442 Py_INCREF(Py_True);
1443 SET_TOP(Py_True);
1444 DISPATCH();
1445 }
1446 else if (err > 0) {
1447 Py_INCREF(Py_False);
1448 SET_TOP(Py_False);
1449 err = 0;
1450 DISPATCH();
1451 }
1452 STACKADJ(-1);
1453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 TARGET(UNARY_INVERT)
1456 v = TOP();
1457 x = PyNumber_Invert(v);
1458 Py_DECREF(v);
1459 SET_TOP(x);
1460 if (x != NULL) DISPATCH();
1461 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 TARGET(BINARY_POWER)
1464 w = POP();
1465 v = TOP();
1466 x = PyNumber_Power(v, w, Py_None);
1467 Py_DECREF(v);
1468 Py_DECREF(w);
1469 SET_TOP(x);
1470 if (x != NULL) DISPATCH();
1471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 TARGET(BINARY_MULTIPLY)
1474 w = POP();
1475 v = TOP();
1476 x = PyNumber_Multiply(v, w);
1477 Py_DECREF(v);
1478 Py_DECREF(w);
1479 SET_TOP(x);
1480 if (x != NULL) DISPATCH();
1481 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 TARGET(BINARY_TRUE_DIVIDE)
1484 w = POP();
1485 v = TOP();
1486 x = PyNumber_TrueDivide(v, w);
1487 Py_DECREF(v);
1488 Py_DECREF(w);
1489 SET_TOP(x);
1490 if (x != NULL) DISPATCH();
1491 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 TARGET(BINARY_FLOOR_DIVIDE)
1494 w = POP();
1495 v = TOP();
1496 x = PyNumber_FloorDivide(v, w);
1497 Py_DECREF(v);
1498 Py_DECREF(w);
1499 SET_TOP(x);
1500 if (x != NULL) DISPATCH();
1501 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 TARGET(BINARY_MODULO)
1504 w = POP();
1505 v = TOP();
1506 if (PyUnicode_CheckExact(v))
1507 x = PyUnicode_Format(v, w);
1508 else
1509 x = PyNumber_Remainder(v, w);
1510 Py_DECREF(v);
1511 Py_DECREF(w);
1512 SET_TOP(x);
1513 if (x != NULL) DISPATCH();
1514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 TARGET(BINARY_ADD)
1517 w = POP();
1518 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001519 if (PyUnicode_CheckExact(v) &&
1520 PyUnicode_CheckExact(w)) {
1521 x = unicode_concatenate(v, w, f, next_instr);
1522 /* unicode_concatenate consumed the ref to v */
1523 goto skip_decref_vx;
1524 }
1525 else {
1526 x = PyNumber_Add(v, w);
1527 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001529 skip_decref_vx:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_DECREF(w);
1531 SET_TOP(x);
1532 if (x != NULL) DISPATCH();
1533 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 TARGET(BINARY_SUBTRACT)
1536 w = POP();
1537 v = TOP();
1538 x = PyNumber_Subtract(v, w);
1539 Py_DECREF(v);
1540 Py_DECREF(w);
1541 SET_TOP(x);
1542 if (x != NULL) DISPATCH();
1543 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 TARGET(BINARY_SUBSCR)
1546 w = POP();
1547 v = TOP();
1548 x = PyObject_GetItem(v, w);
1549 Py_DECREF(v);
1550 Py_DECREF(w);
1551 SET_TOP(x);
1552 if (x != NULL) DISPATCH();
1553 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 TARGET(BINARY_LSHIFT)
1556 w = POP();
1557 v = TOP();
1558 x = PyNumber_Lshift(v, w);
1559 Py_DECREF(v);
1560 Py_DECREF(w);
1561 SET_TOP(x);
1562 if (x != NULL) DISPATCH();
1563 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 TARGET(BINARY_RSHIFT)
1566 w = POP();
1567 v = TOP();
1568 x = PyNumber_Rshift(v, w);
1569 Py_DECREF(v);
1570 Py_DECREF(w);
1571 SET_TOP(x);
1572 if (x != NULL) DISPATCH();
1573 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 TARGET(BINARY_AND)
1576 w = POP();
1577 v = TOP();
1578 x = PyNumber_And(v, w);
1579 Py_DECREF(v);
1580 Py_DECREF(w);
1581 SET_TOP(x);
1582 if (x != NULL) DISPATCH();
1583 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 TARGET(BINARY_XOR)
1586 w = POP();
1587 v = TOP();
1588 x = PyNumber_Xor(v, w);
1589 Py_DECREF(v);
1590 Py_DECREF(w);
1591 SET_TOP(x);
1592 if (x != NULL) DISPATCH();
1593 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 TARGET(BINARY_OR)
1596 w = POP();
1597 v = TOP();
1598 x = PyNumber_Or(v, w);
1599 Py_DECREF(v);
1600 Py_DECREF(w);
1601 SET_TOP(x);
1602 if (x != NULL) DISPATCH();
1603 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 TARGET(LIST_APPEND)
1606 w = POP();
1607 v = PEEK(oparg);
1608 err = PyList_Append(v, w);
1609 Py_DECREF(w);
1610 if (err == 0) {
1611 PREDICT(JUMP_ABSOLUTE);
1612 DISPATCH();
1613 }
1614 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 TARGET(SET_ADD)
1617 w = POP();
1618 v = stack_pointer[-oparg];
1619 err = PySet_Add(v, w);
1620 Py_DECREF(w);
1621 if (err == 0) {
1622 PREDICT(JUMP_ABSOLUTE);
1623 DISPATCH();
1624 }
1625 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 TARGET(INPLACE_POWER)
1628 w = POP();
1629 v = TOP();
1630 x = PyNumber_InPlacePower(v, w, Py_None);
1631 Py_DECREF(v);
1632 Py_DECREF(w);
1633 SET_TOP(x);
1634 if (x != NULL) DISPATCH();
1635 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 TARGET(INPLACE_MULTIPLY)
1638 w = POP();
1639 v = TOP();
1640 x = PyNumber_InPlaceMultiply(v, w);
1641 Py_DECREF(v);
1642 Py_DECREF(w);
1643 SET_TOP(x);
1644 if (x != NULL) DISPATCH();
1645 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 TARGET(INPLACE_TRUE_DIVIDE)
1648 w = POP();
1649 v = TOP();
1650 x = PyNumber_InPlaceTrueDivide(v, w);
1651 Py_DECREF(v);
1652 Py_DECREF(w);
1653 SET_TOP(x);
1654 if (x != NULL) DISPATCH();
1655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 TARGET(INPLACE_FLOOR_DIVIDE)
1658 w = POP();
1659 v = TOP();
1660 x = PyNumber_InPlaceFloorDivide(v, w);
1661 Py_DECREF(v);
1662 Py_DECREF(w);
1663 SET_TOP(x);
1664 if (x != NULL) DISPATCH();
1665 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 TARGET(INPLACE_MODULO)
1668 w = POP();
1669 v = TOP();
1670 x = PyNumber_InPlaceRemainder(v, w);
1671 Py_DECREF(v);
1672 Py_DECREF(w);
1673 SET_TOP(x);
1674 if (x != NULL) DISPATCH();
1675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 TARGET(INPLACE_ADD)
1678 w = POP();
1679 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001680 if (PyUnicode_CheckExact(v) &&
1681 PyUnicode_CheckExact(w)) {
1682 x = unicode_concatenate(v, w, f, next_instr);
1683 /* unicode_concatenate consumed the ref to v */
1684 goto skip_decref_v;
1685 }
1686 else {
1687 x = PyNumber_InPlaceAdd(v, w);
1688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001690 skip_decref_v:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 Py_DECREF(w);
1692 SET_TOP(x);
1693 if (x != NULL) DISPATCH();
1694 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 TARGET(INPLACE_SUBTRACT)
1697 w = POP();
1698 v = TOP();
1699 x = PyNumber_InPlaceSubtract(v, w);
1700 Py_DECREF(v);
1701 Py_DECREF(w);
1702 SET_TOP(x);
1703 if (x != NULL) DISPATCH();
1704 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 TARGET(INPLACE_LSHIFT)
1707 w = POP();
1708 v = TOP();
1709 x = PyNumber_InPlaceLshift(v, w);
1710 Py_DECREF(v);
1711 Py_DECREF(w);
1712 SET_TOP(x);
1713 if (x != NULL) DISPATCH();
1714 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 TARGET(INPLACE_RSHIFT)
1717 w = POP();
1718 v = TOP();
1719 x = PyNumber_InPlaceRshift(v, w);
1720 Py_DECREF(v);
1721 Py_DECREF(w);
1722 SET_TOP(x);
1723 if (x != NULL) DISPATCH();
1724 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 TARGET(INPLACE_AND)
1727 w = POP();
1728 v = TOP();
1729 x = PyNumber_InPlaceAnd(v, w);
1730 Py_DECREF(v);
1731 Py_DECREF(w);
1732 SET_TOP(x);
1733 if (x != NULL) DISPATCH();
1734 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 TARGET(INPLACE_XOR)
1737 w = POP();
1738 v = TOP();
1739 x = PyNumber_InPlaceXor(v, w);
1740 Py_DECREF(v);
1741 Py_DECREF(w);
1742 SET_TOP(x);
1743 if (x != NULL) DISPATCH();
1744 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 TARGET(INPLACE_OR)
1747 w = POP();
1748 v = TOP();
1749 x = PyNumber_InPlaceOr(v, w);
1750 Py_DECREF(v);
1751 Py_DECREF(w);
1752 SET_TOP(x);
1753 if (x != NULL) DISPATCH();
1754 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 TARGET(STORE_SUBSCR)
1757 w = TOP();
1758 v = SECOND();
1759 u = THIRD();
1760 STACKADJ(-3);
1761 /* v[w] = u */
1762 err = PyObject_SetItem(v, w, u);
1763 Py_DECREF(u);
1764 Py_DECREF(v);
1765 Py_DECREF(w);
1766 if (err == 0) DISPATCH();
1767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 TARGET(DELETE_SUBSCR)
1770 w = TOP();
1771 v = SECOND();
1772 STACKADJ(-2);
1773 /* del v[w] */
1774 err = PyObject_DelItem(v, w);
1775 Py_DECREF(v);
1776 Py_DECREF(w);
1777 if (err == 0) DISPATCH();
1778 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 TARGET(PRINT_EXPR)
1781 v = POP();
1782 w = PySys_GetObject("displayhook");
1783 if (w == NULL) {
1784 PyErr_SetString(PyExc_RuntimeError,
1785 "lost sys.displayhook");
1786 err = -1;
1787 x = NULL;
1788 }
1789 if (err == 0) {
1790 x = PyTuple_Pack(1, v);
1791 if (x == NULL)
1792 err = -1;
1793 }
1794 if (err == 0) {
1795 w = PyEval_CallObject(w, x);
1796 Py_XDECREF(w);
1797 if (w == NULL)
1798 err = -1;
1799 }
1800 Py_DECREF(v);
1801 Py_XDECREF(x);
1802 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001803
Thomas Wouters434d0822000-08-24 20:11:32 +00001804#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001806#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 TARGET(RAISE_VARARGS)
1808 v = w = NULL;
1809 switch (oparg) {
1810 case 2:
1811 v = POP(); /* cause */
1812 case 1:
1813 w = POP(); /* exc */
1814 case 0: /* Fallthrough */
1815 why = do_raise(w, v);
1816 break;
1817 default:
1818 PyErr_SetString(PyExc_SystemError,
1819 "bad RAISE_VARARGS oparg");
1820 why = WHY_EXCEPTION;
1821 break;
1822 }
1823 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 TARGET(STORE_LOCALS)
1826 x = POP();
1827 v = f->f_locals;
1828 Py_XDECREF(v);
1829 f->f_locals = x;
1830 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 TARGET(RETURN_VALUE)
1833 retval = POP();
1834 why = WHY_RETURN;
1835 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001836
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001837 TARGET(YIELD_FROM)
1838 u = POP();
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001839 x = TOP();
1840 /* send u to x */
1841 if (PyGen_CheckExact(x)) {
1842 retval = _PyGen_Send((PyGenObject *)x, u);
1843 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001844 _Py_IDENTIFIER(send);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001845 if (u == Py_None)
Benjamin Petersonb37df512012-08-06 17:53:09 -07001846 retval = Py_TYPE(x)->tp_iternext(x);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001847 else
Benjamin Peterson302e7902012-03-20 23:17:04 -04001848 retval = _PyObject_CallMethodId(x, &PyId_send, "O", u);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001849 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001850 Py_DECREF(u);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001851 if (!retval) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001852 PyObject *val;
1853 x = POP(); /* Remove iter from stack */
1854 Py_DECREF(x);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001855 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001856 if (err < 0) {
1857 x = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001858 break;
1859 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001860 x = val;
1861 PUSH(x);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001862 continue;
1863 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001864 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001865 f->f_stacktop = stack_pointer;
1866 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001867 /* and repeat... */
1868 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001869 goto fast_yield;
1870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 TARGET(YIELD_VALUE)
1872 retval = POP();
1873 f->f_stacktop = stack_pointer;
1874 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 TARGET(POP_EXCEPT)
1878 {
1879 PyTryBlock *b = PyFrame_BlockPop(f);
1880 if (b->b_type != EXCEPT_HANDLER) {
1881 PyErr_SetString(PyExc_SystemError,
1882 "popped block is not an except handler");
1883 why = WHY_EXCEPTION;
1884 break;
1885 }
1886 UNWIND_EXCEPT_HANDLER(b);
1887 }
1888 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 TARGET(POP_BLOCK)
1891 {
1892 PyTryBlock *b = PyFrame_BlockPop(f);
1893 UNWIND_BLOCK(b);
1894 }
1895 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 PREDICTED(END_FINALLY);
1898 TARGET(END_FINALLY)
1899 v = POP();
1900 if (PyLong_Check(v)) {
1901 why = (enum why_code) PyLong_AS_LONG(v);
1902 assert(why != WHY_YIELD);
1903 if (why == WHY_RETURN ||
1904 why == WHY_CONTINUE)
1905 retval = POP();
1906 if (why == WHY_SILENCED) {
1907 /* An exception was silenced by 'with', we must
1908 manually unwind the EXCEPT_HANDLER block which was
1909 created when the exception was caught, otherwise
1910 the stack will be in an inconsistent state. */
1911 PyTryBlock *b = PyFrame_BlockPop(f);
1912 assert(b->b_type == EXCEPT_HANDLER);
1913 UNWIND_EXCEPT_HANDLER(b);
1914 why = WHY_NOT;
1915 }
1916 }
1917 else if (PyExceptionClass_Check(v)) {
1918 w = POP();
1919 u = POP();
1920 PyErr_Restore(v, w, u);
1921 why = WHY_RERAISE;
1922 break;
1923 }
1924 else if (v != Py_None) {
1925 PyErr_SetString(PyExc_SystemError,
1926 "'finally' pops bad exception");
1927 why = WHY_EXCEPTION;
1928 }
1929 Py_DECREF(v);
1930 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 TARGET(LOAD_BUILD_CLASS)
Victor Stinner3c1e4812012-03-26 22:10:51 +02001933 {
1934 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001935
1936 if (PyDict_CheckExact(f->f_builtins)) {
1937 x = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1938 if (x == NULL) {
1939 PyErr_SetString(PyExc_NameError,
1940 "__build_class__ not found");
1941 break;
1942 }
Antoine Pitroubf35c152012-04-19 18:21:04 +02001943 Py_INCREF(x);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001944 }
1945 else {
1946 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1947 if (build_class_str == NULL)
1948 break;
1949 x = PyObject_GetItem(f->f_builtins, build_class_str);
1950 if (x == NULL) {
1951 if (PyErr_ExceptionMatches(PyExc_KeyError))
1952 PyErr_SetString(PyExc_NameError,
1953 "__build_class__ not found");
1954 break;
1955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 PUSH(x);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001958 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 TARGET(STORE_NAME)
1962 w = GETITEM(names, oparg);
1963 v = POP();
1964 if ((x = f->f_locals) != NULL) {
1965 if (PyDict_CheckExact(x))
1966 err = PyDict_SetItem(x, w, v);
1967 else
1968 err = PyObject_SetItem(x, w, v);
1969 Py_DECREF(v);
1970 if (err == 0) DISPATCH();
1971 break;
1972 }
1973 PyErr_Format(PyExc_SystemError,
1974 "no locals found when storing %R", w);
1975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 TARGET(DELETE_NAME)
1978 w = GETITEM(names, oparg);
1979 if ((x = f->f_locals) != NULL) {
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001980 if ((err = PyObject_DelItem(x, w)) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 format_exc_check_arg(PyExc_NameError,
1982 NAME_ERROR_MSG,
1983 w);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001984 break;
1985 }
1986 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 }
1988 PyErr_Format(PyExc_SystemError,
1989 "no locals when deleting %R", w);
1990 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1993 TARGET(UNPACK_SEQUENCE)
1994 v = POP();
1995 if (PyTuple_CheckExact(v) &&
1996 PyTuple_GET_SIZE(v) == oparg) {
1997 PyObject **items = \
1998 ((PyTupleObject *)v)->ob_item;
1999 while (oparg--) {
2000 w = items[oparg];
2001 Py_INCREF(w);
2002 PUSH(w);
2003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 } else if (PyList_CheckExact(v) &&
2005 PyList_GET_SIZE(v) == oparg) {
2006 PyObject **items = \
2007 ((PyListObject *)v)->ob_item;
2008 while (oparg--) {
2009 w = items[oparg];
2010 Py_INCREF(w);
2011 PUSH(w);
2012 }
2013 } else if (unpack_iterable(v, oparg, -1,
2014 stack_pointer + oparg)) {
2015 STACKADJ(oparg);
2016 } else {
2017 /* unpack_iterable() raised an exception */
2018 why = WHY_EXCEPTION;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002019 Py_DECREF(v);
2020 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
2022 Py_DECREF(v);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002023 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 TARGET(UNPACK_EX)
2026 {
2027 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2028 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2031 stack_pointer + totalargs)) {
2032 stack_pointer += totalargs;
2033 } else {
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002034 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 why = WHY_EXCEPTION;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002036 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 }
2038 Py_DECREF(v);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002039 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 TARGET(STORE_ATTR)
2043 w = GETITEM(names, oparg);
2044 v = TOP();
2045 u = SECOND();
2046 STACKADJ(-2);
2047 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2048 Py_DECREF(v);
2049 Py_DECREF(u);
2050 if (err == 0) DISPATCH();
2051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 TARGET(DELETE_ATTR)
2054 w = GETITEM(names, oparg);
2055 v = POP();
2056 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2057 /* del v.w */
2058 Py_DECREF(v);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002059 if (err == 0) DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 TARGET(STORE_GLOBAL)
2063 w = GETITEM(names, oparg);
2064 v = POP();
2065 err = PyDict_SetItem(f->f_globals, w, v);
2066 Py_DECREF(v);
2067 if (err == 0) DISPATCH();
2068 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 TARGET(DELETE_GLOBAL)
2071 w = GETITEM(names, oparg);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002072 if ((err = PyDict_DelItem(f->f_globals, w)) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 format_exc_check_arg(
2074 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002075 break;
2076 }
2077 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 TARGET(LOAD_NAME)
2080 w = GETITEM(names, oparg);
2081 if ((v = f->f_locals) == NULL) {
2082 PyErr_Format(PyExc_SystemError,
2083 "no locals when loading %R", w);
2084 why = WHY_EXCEPTION;
2085 break;
2086 }
2087 if (PyDict_CheckExact(v)) {
2088 x = PyDict_GetItem(v, w);
2089 Py_XINCREF(x);
2090 }
2091 else {
2092 x = PyObject_GetItem(v, w);
2093 if (x == NULL && PyErr_Occurred()) {
2094 if (!PyErr_ExceptionMatches(
2095 PyExc_KeyError))
2096 break;
2097 PyErr_Clear();
2098 }
2099 }
2100 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002101 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitroubf35c152012-04-19 18:21:04 +02002102 Py_XINCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 if (x == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002104 if (PyDict_CheckExact(f->f_builtins)) {
2105 x = PyDict_GetItem(f->f_builtins, w);
2106 if (x == NULL) {
2107 format_exc_check_arg(
2108 PyExc_NameError,
2109 NAME_ERROR_MSG, w);
2110 break;
2111 }
Antoine Pitroubf35c152012-04-19 18:21:04 +02002112 Py_INCREF(x);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002113 }
2114 else {
2115 x = PyObject_GetItem(f->f_builtins, w);
2116 if (x == NULL) {
2117 if (PyErr_ExceptionMatches(PyExc_KeyError))
2118 format_exc_check_arg(
2119 PyExc_NameError,
2120 NAME_ERROR_MSG, w);
2121 break;
2122 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
2126 PUSH(x);
2127 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 TARGET(LOAD_GLOBAL)
2130 w = GETITEM(names, oparg);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002131 if (PyDict_CheckExact(f->f_globals)
2132 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002133 x = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
2134 (PyDictObject *)f->f_builtins,
2135 w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 if (x == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002137 if (!PyErr_Occurred())
2138 format_exc_check_arg(PyExc_NameError,
2139 GLOBAL_NAME_ERROR_MSG, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 break;
2141 }
Benjamin Peterson11389442012-04-26 00:26:37 -04002142 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002144 else {
2145 /* Slow-path if globals or builtins is not a dict */
2146 x = PyObject_GetItem(f->f_globals, w);
2147 if (x == NULL) {
2148 x = PyObject_GetItem(f->f_builtins, w);
2149 if (x == NULL) {
2150 if (PyErr_ExceptionMatches(PyExc_KeyError))
2151 format_exc_check_arg(
2152 PyExc_NameError,
2153 GLOBAL_NAME_ERROR_MSG, w);
2154 break;
2155 }
2156 }
2157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 PUSH(x);
2159 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 TARGET(DELETE_FAST)
2162 x = GETLOCAL(oparg);
2163 if (x != NULL) {
2164 SETLOCAL(oparg, NULL);
2165 DISPATCH();
2166 }
2167 format_exc_check_arg(
2168 PyExc_UnboundLocalError,
2169 UNBOUNDLOCAL_ERROR_MSG,
2170 PyTuple_GetItem(co->co_varnames, oparg)
2171 );
2172 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002174 TARGET(DELETE_DEREF)
2175 x = freevars[oparg];
2176 if (PyCell_GET(x) != NULL) {
2177 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002178 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002179 }
2180 err = -1;
2181 format_exc_unbound(co, oparg);
2182 break;
2183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 TARGET(LOAD_CLOSURE)
2185 x = freevars[oparg];
2186 Py_INCREF(x);
2187 PUSH(x);
2188 if (x != NULL) DISPATCH();
2189 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 TARGET(LOAD_DEREF)
2192 x = freevars[oparg];
2193 w = PyCell_Get(x);
2194 if (w != NULL) {
2195 PUSH(w);
2196 DISPATCH();
2197 }
2198 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002199 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 TARGET(STORE_DEREF)
2203 w = POP();
2204 x = freevars[oparg];
2205 PyCell_Set(x, w);
2206 Py_DECREF(w);
2207 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 TARGET(BUILD_TUPLE)
2210 x = PyTuple_New(oparg);
2211 if (x != NULL) {
2212 for (; --oparg >= 0;) {
2213 w = POP();
2214 PyTuple_SET_ITEM(x, oparg, w);
2215 }
2216 PUSH(x);
2217 DISPATCH();
2218 }
2219 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 TARGET(BUILD_LIST)
2222 x = PyList_New(oparg);
2223 if (x != NULL) {
2224 for (; --oparg >= 0;) {
2225 w = POP();
2226 PyList_SET_ITEM(x, oparg, w);
2227 }
2228 PUSH(x);
2229 DISPATCH();
2230 }
2231 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 TARGET(BUILD_SET)
2234 x = PySet_New(NULL);
2235 if (x != NULL) {
2236 for (; --oparg >= 0;) {
2237 w = POP();
2238 if (err == 0)
2239 err = PySet_Add(x, w);
2240 Py_DECREF(w);
2241 }
2242 if (err != 0) {
2243 Py_DECREF(x);
2244 break;
2245 }
2246 PUSH(x);
2247 DISPATCH();
2248 }
2249 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 TARGET(BUILD_MAP)
2252 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2253 PUSH(x);
2254 if (x != NULL) DISPATCH();
2255 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 TARGET(STORE_MAP)
2258 w = TOP(); /* key */
2259 u = SECOND(); /* value */
2260 v = THIRD(); /* dict */
2261 STACKADJ(-2);
2262 assert (PyDict_CheckExact(v));
2263 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2264 Py_DECREF(u);
2265 Py_DECREF(w);
2266 if (err == 0) DISPATCH();
2267 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 TARGET(MAP_ADD)
2270 w = TOP(); /* key */
2271 u = SECOND(); /* value */
2272 STACKADJ(-2);
2273 v = stack_pointer[-oparg]; /* dict */
2274 assert (PyDict_CheckExact(v));
2275 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2276 Py_DECREF(u);
2277 Py_DECREF(w);
2278 if (err == 0) {
2279 PREDICT(JUMP_ABSOLUTE);
2280 DISPATCH();
2281 }
2282 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 TARGET(LOAD_ATTR)
2285 w = GETITEM(names, oparg);
2286 v = TOP();
2287 x = PyObject_GetAttr(v, w);
2288 Py_DECREF(v);
2289 SET_TOP(x);
2290 if (x != NULL) DISPATCH();
2291 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 TARGET(COMPARE_OP)
2294 w = POP();
2295 v = TOP();
2296 x = cmp_outcome(oparg, v, w);
2297 Py_DECREF(v);
2298 Py_DECREF(w);
2299 SET_TOP(x);
2300 if (x == NULL) break;
2301 PREDICT(POP_JUMP_IF_FALSE);
2302 PREDICT(POP_JUMP_IF_TRUE);
2303 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 TARGET(IMPORT_NAME)
Victor Stinner3c1e4812012-03-26 22:10:51 +02002306 {
2307 _Py_IDENTIFIER(__import__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 w = GETITEM(names, oparg);
Victor Stinner3c1e4812012-03-26 22:10:51 +02002309 x = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (x == NULL) {
2311 PyErr_SetString(PyExc_ImportError,
2312 "__import__ not found");
2313 break;
2314 }
2315 Py_INCREF(x);
2316 v = POP();
2317 u = TOP();
2318 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2319 w = PyTuple_Pack(5,
2320 w,
2321 f->f_globals,
2322 f->f_locals == NULL ?
2323 Py_None : f->f_locals,
2324 v,
2325 u);
2326 else
2327 w = PyTuple_Pack(4,
2328 w,
2329 f->f_globals,
2330 f->f_locals == NULL ?
2331 Py_None : f->f_locals,
2332 v);
2333 Py_DECREF(v);
2334 Py_DECREF(u);
2335 if (w == NULL) {
2336 u = POP();
2337 Py_DECREF(x);
2338 x = NULL;
2339 break;
2340 }
2341 READ_TIMESTAMP(intr0);
2342 v = x;
2343 x = PyEval_CallObject(v, w);
2344 Py_DECREF(v);
2345 READ_TIMESTAMP(intr1);
2346 Py_DECREF(w);
2347 SET_TOP(x);
2348 if (x != NULL) DISPATCH();
2349 break;
Victor Stinner3c1e4812012-03-26 22:10:51 +02002350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 TARGET(IMPORT_STAR)
2353 v = POP();
2354 PyFrame_FastToLocals(f);
2355 if ((x = f->f_locals) == NULL) {
2356 PyErr_SetString(PyExc_SystemError,
2357 "no locals found during 'import *'");
2358 break;
2359 }
2360 READ_TIMESTAMP(intr0);
2361 err = import_all_from(x, v);
2362 READ_TIMESTAMP(intr1);
2363 PyFrame_LocalsToFast(f, 0);
2364 Py_DECREF(v);
2365 if (err == 0) DISPATCH();
2366 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 TARGET(IMPORT_FROM)
2369 w = GETITEM(names, oparg);
2370 v = TOP();
2371 READ_TIMESTAMP(intr0);
2372 x = import_from(v, w);
2373 READ_TIMESTAMP(intr1);
2374 PUSH(x);
2375 if (x != NULL) DISPATCH();
2376 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 TARGET(JUMP_FORWARD)
2379 JUMPBY(oparg);
2380 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2383 TARGET(POP_JUMP_IF_FALSE)
2384 w = POP();
2385 if (w == Py_True) {
2386 Py_DECREF(w);
2387 FAST_DISPATCH();
2388 }
2389 if (w == Py_False) {
2390 Py_DECREF(w);
2391 JUMPTO(oparg);
2392 FAST_DISPATCH();
2393 }
2394 err = PyObject_IsTrue(w);
2395 Py_DECREF(w);
2396 if (err > 0)
2397 err = 0;
2398 else if (err == 0)
2399 JUMPTO(oparg);
2400 else
2401 break;
2402 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2405 TARGET(POP_JUMP_IF_TRUE)
2406 w = POP();
2407 if (w == Py_False) {
2408 Py_DECREF(w);
2409 FAST_DISPATCH();
2410 }
2411 if (w == Py_True) {
2412 Py_DECREF(w);
2413 JUMPTO(oparg);
2414 FAST_DISPATCH();
2415 }
2416 err = PyObject_IsTrue(w);
2417 Py_DECREF(w);
2418 if (err > 0) {
2419 err = 0;
2420 JUMPTO(oparg);
2421 }
2422 else if (err == 0)
2423 ;
2424 else
2425 break;
2426 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 TARGET(JUMP_IF_FALSE_OR_POP)
2429 w = TOP();
2430 if (w == Py_True) {
2431 STACKADJ(-1);
2432 Py_DECREF(w);
2433 FAST_DISPATCH();
2434 }
2435 if (w == Py_False) {
2436 JUMPTO(oparg);
2437 FAST_DISPATCH();
2438 }
2439 err = PyObject_IsTrue(w);
2440 if (err > 0) {
2441 STACKADJ(-1);
2442 Py_DECREF(w);
2443 err = 0;
2444 }
2445 else if (err == 0)
2446 JUMPTO(oparg);
2447 else
2448 break;
2449 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 TARGET(JUMP_IF_TRUE_OR_POP)
2452 w = TOP();
2453 if (w == Py_False) {
2454 STACKADJ(-1);
2455 Py_DECREF(w);
2456 FAST_DISPATCH();
2457 }
2458 if (w == Py_True) {
2459 JUMPTO(oparg);
2460 FAST_DISPATCH();
2461 }
2462 err = PyObject_IsTrue(w);
2463 if (err > 0) {
2464 err = 0;
2465 JUMPTO(oparg);
2466 }
2467 else if (err == 0) {
2468 STACKADJ(-1);
2469 Py_DECREF(w);
2470 }
2471 else
2472 break;
2473 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2476 TARGET(JUMP_ABSOLUTE)
2477 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002478#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 /* Enabling this path speeds-up all while and for-loops by bypassing
2480 the per-loop checks for signals. By default, this should be turned-off
2481 because it prevents detection of a control-break in tight loops like
2482 "while 1: pass". Compile with this option turned-on when you need
2483 the speed-up and do not need break checking inside tight loops (ones
2484 that contain only instructions ending with FAST_DISPATCH).
2485 */
2486 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002487#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002489#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 TARGET(GET_ITER)
2492 /* before: [obj]; after [getiter(obj)] */
2493 v = TOP();
2494 x = PyObject_GetIter(v);
2495 Py_DECREF(v);
2496 if (x != NULL) {
2497 SET_TOP(x);
2498 PREDICT(FOR_ITER);
2499 DISPATCH();
2500 }
2501 STACKADJ(-1);
2502 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 PREDICTED_WITH_ARG(FOR_ITER);
2505 TARGET(FOR_ITER)
2506 /* before: [iter]; after: [iter, iter()] *or* [] */
2507 v = TOP();
2508 x = (*v->ob_type->tp_iternext)(v);
2509 if (x != NULL) {
2510 PUSH(x);
2511 PREDICT(STORE_FAST);
2512 PREDICT(UNPACK_SEQUENCE);
2513 DISPATCH();
2514 }
2515 if (PyErr_Occurred()) {
2516 if (!PyErr_ExceptionMatches(
2517 PyExc_StopIteration))
2518 break;
2519 PyErr_Clear();
2520 }
2521 /* iterator ended normally */
2522 x = v = POP();
2523 Py_DECREF(v);
2524 JUMPBY(oparg);
2525 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 TARGET(BREAK_LOOP)
2528 why = WHY_BREAK;
2529 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 TARGET(CONTINUE_LOOP)
2532 retval = PyLong_FromLong(oparg);
2533 if (!retval) {
2534 x = NULL;
2535 break;
2536 }
2537 why = WHY_CONTINUE;
2538 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2541 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2542 TARGET(SETUP_FINALLY)
2543 _setup_finally:
2544 /* NOTE: If you add any new block-setup opcodes that
2545 are not try/except/finally handlers, you may need
2546 to update the PyGen_NeedsFinalizing() function.
2547 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2550 STACK_LEVEL());
2551 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 TARGET(SETUP_WITH)
2554 {
Benjamin Petersonce798522012-01-22 11:24:29 -05002555 _Py_IDENTIFIER(__exit__);
2556 _Py_IDENTIFIER(__enter__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 w = TOP();
Benjamin Petersonce798522012-01-22 11:24:29 -05002558 x = special_lookup(w, &PyId___exit__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 if (!x)
2560 break;
2561 SET_TOP(x);
Benjamin Petersonce798522012-01-22 11:24:29 -05002562 u = special_lookup(w, &PyId___enter__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 Py_DECREF(w);
2564 if (!u) {
2565 x = NULL;
2566 break;
2567 }
2568 x = PyObject_CallFunctionObjArgs(u, NULL);
2569 Py_DECREF(u);
2570 if (!x)
2571 break;
2572 /* Setup the finally block before pushing the result
2573 of __enter__ on the stack. */
2574 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2575 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 PUSH(x);
2578 DISPATCH();
2579 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 TARGET(WITH_CLEANUP)
2582 {
2583 /* At the top of the stack are 1-3 values indicating
2584 how/why we entered the finally clause:
2585 - TOP = None
2586 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2587 - TOP = WHY_*; no retval below it
2588 - (TOP, SECOND, THIRD) = exc_info()
2589 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2590 Below them is EXIT, the context.__exit__ bound method.
2591 In the last case, we must call
2592 EXIT(TOP, SECOND, THIRD)
2593 otherwise we must call
2594 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 In the first two cases, we remove EXIT from the
2597 stack, leaving the rest in the same order. In the
2598 third case, we shift the bottom 3 values of the
2599 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 In addition, if the stack represents an exception,
2602 *and* the function call returns a 'true' value, we
2603 push WHY_SILENCED onto the stack. END_FINALLY will
2604 then not re-raise the exception. (But non-local
2605 gotos should still be resumed.)
2606 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 PyObject *exit_func;
2609 u = TOP();
2610 if (u == Py_None) {
2611 (void)POP();
2612 exit_func = TOP();
2613 SET_TOP(u);
2614 v = w = Py_None;
2615 }
2616 else if (PyLong_Check(u)) {
2617 (void)POP();
2618 switch(PyLong_AsLong(u)) {
2619 case WHY_RETURN:
2620 case WHY_CONTINUE:
2621 /* Retval in TOP. */
2622 exit_func = SECOND();
2623 SET_SECOND(TOP());
2624 SET_TOP(u);
2625 break;
2626 default:
2627 exit_func = TOP();
2628 SET_TOP(u);
2629 break;
2630 }
2631 u = v = w = Py_None;
2632 }
2633 else {
2634 PyObject *tp, *exc, *tb;
2635 PyTryBlock *block;
2636 v = SECOND();
2637 w = THIRD();
2638 tp = FOURTH();
2639 exc = PEEK(5);
2640 tb = PEEK(6);
2641 exit_func = PEEK(7);
2642 SET_VALUE(7, tb);
2643 SET_VALUE(6, exc);
2644 SET_VALUE(5, tp);
2645 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2646 SET_FOURTH(NULL);
2647 /* We just shifted the stack down, so we have
2648 to tell the except handler block that the
2649 values are lower than it expects. */
2650 block = &f->f_blockstack[f->f_iblock - 1];
2651 assert(block->b_type == EXCEPT_HANDLER);
2652 block->b_level--;
2653 }
2654 /* XXX Not the fastest way to call it... */
2655 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2656 NULL);
2657 Py_DECREF(exit_func);
2658 if (x == NULL)
2659 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 if (u != Py_None)
2662 err = PyObject_IsTrue(x);
2663 else
2664 err = 0;
2665 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 if (err < 0)
2668 break; /* Go to error exit */
2669 else if (err > 0) {
2670 err = 0;
2671 /* There was an exception and a True return */
2672 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2673 }
2674 PREDICT(END_FINALLY);
2675 break;
2676 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 TARGET(CALL_FUNCTION)
2679 {
2680 PyObject **sp;
2681 PCALL(PCALL_ALL);
2682 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002683#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002685#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002687#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 stack_pointer = sp;
2689 PUSH(x);
2690 if (x != NULL)
2691 DISPATCH();
2692 break;
2693 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2696 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2697 TARGET(CALL_FUNCTION_VAR_KW)
2698 _call_function_var_kw:
2699 {
2700 int na = oparg & 0xff;
2701 int nk = (oparg>>8) & 0xff;
2702 int flags = (opcode - CALL_FUNCTION) & 3;
2703 int n = na + 2 * nk;
2704 PyObject **pfunc, *func, **sp;
2705 PCALL(PCALL_ALL);
2706 if (flags & CALL_FLAG_VAR)
2707 n++;
2708 if (flags & CALL_FLAG_KW)
2709 n++;
2710 pfunc = stack_pointer - n - 1;
2711 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002714 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 PyObject *self = PyMethod_GET_SELF(func);
2716 Py_INCREF(self);
2717 func = PyMethod_GET_FUNCTION(func);
2718 Py_INCREF(func);
2719 Py_DECREF(*pfunc);
2720 *pfunc = self;
2721 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002722 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 } else
2724 Py_INCREF(func);
2725 sp = stack_pointer;
2726 READ_TIMESTAMP(intr0);
2727 x = ext_do_call(func, &sp, flags, na, nk);
2728 READ_TIMESTAMP(intr1);
2729 stack_pointer = sp;
2730 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 while (stack_pointer > pfunc) {
2733 w = POP();
2734 Py_DECREF(w);
2735 }
2736 PUSH(x);
2737 if (x != NULL)
2738 DISPATCH();
2739 break;
2740 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2743 TARGET(MAKE_FUNCTION)
2744 _make_function:
2745 {
2746 int posdefaults = oparg & 0xff;
2747 int kwdefaults = (oparg>>8) & 0xff;
2748 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002749
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002750 w = POP(); /* qualname */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 v = POP(); /* code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002752 x = PyFunction_NewWithQualName(v, f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 Py_DECREF(v);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002754 Py_DECREF(w);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (x != NULL && opcode == MAKE_CLOSURE) {
2757 v = POP();
2758 if (PyFunction_SetClosure(x, v) != 0) {
2759 /* Can't happen unless bytecode is corrupt. */
2760 why = WHY_EXCEPTION;
2761 }
2762 Py_DECREF(v);
2763 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (x != NULL && num_annotations > 0) {
2766 Py_ssize_t name_ix;
2767 u = POP(); /* names of args with annotations */
2768 v = PyDict_New();
2769 if (v == NULL) {
2770 Py_DECREF(x);
2771 x = NULL;
2772 break;
2773 }
2774 name_ix = PyTuple_Size(u);
2775 assert(num_annotations == name_ix+1);
2776 while (name_ix > 0) {
2777 --name_ix;
2778 t = PyTuple_GET_ITEM(u, name_ix);
2779 w = POP();
2780 /* XXX(nnorwitz): check for errors */
2781 PyDict_SetItem(v, t, w);
2782 Py_DECREF(w);
2783 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 if (PyFunction_SetAnnotations(x, v) != 0) {
2786 /* Can't happen unless
2787 PyFunction_SetAnnotations changes. */
2788 why = WHY_EXCEPTION;
2789 }
2790 Py_DECREF(v);
2791 Py_DECREF(u);
2792 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 /* XXX Maybe this should be a separate opcode? */
2795 if (x != NULL && posdefaults > 0) {
2796 v = PyTuple_New(posdefaults);
2797 if (v == NULL) {
2798 Py_DECREF(x);
2799 x = NULL;
2800 break;
2801 }
2802 while (--posdefaults >= 0) {
2803 w = POP();
2804 PyTuple_SET_ITEM(v, posdefaults, w);
2805 }
2806 if (PyFunction_SetDefaults(x, v) != 0) {
2807 /* Can't happen unless
2808 PyFunction_SetDefaults changes. */
2809 why = WHY_EXCEPTION;
2810 }
2811 Py_DECREF(v);
2812 }
2813 if (x != NULL && kwdefaults > 0) {
2814 v = PyDict_New();
2815 if (v == NULL) {
2816 Py_DECREF(x);
2817 x = NULL;
2818 break;
2819 }
2820 while (--kwdefaults >= 0) {
2821 w = POP(); /* default value */
2822 u = POP(); /* kw only arg name */
2823 /* XXX(nnorwitz): check for errors */
2824 PyDict_SetItem(v, u, w);
2825 Py_DECREF(w);
2826 Py_DECREF(u);
2827 }
2828 if (PyFunction_SetKwDefaults(x, v) != 0) {
2829 /* Can't happen unless
2830 PyFunction_SetKwDefaults changes. */
2831 why = WHY_EXCEPTION;
2832 }
2833 Py_DECREF(v);
2834 }
2835 PUSH(x);
2836 break;
2837 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 TARGET(BUILD_SLICE)
2840 if (oparg == 3)
2841 w = POP();
2842 else
2843 w = NULL;
2844 v = POP();
2845 u = TOP();
2846 x = PySlice_New(u, v, w);
2847 Py_DECREF(u);
2848 Py_DECREF(v);
2849 Py_XDECREF(w);
2850 SET_TOP(x);
2851 if (x != NULL) DISPATCH();
2852 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 TARGET(EXTENDED_ARG)
2855 opcode = NEXTOP();
2856 oparg = oparg<<16 | NEXTARG();
2857 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002858
Antoine Pitrou042b1282010-08-13 21:15:58 +00002859#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 default:
2863 fprintf(stderr,
2864 "XXX lineno: %d, opcode: %d\n",
2865 PyFrame_GetLineNumber(f),
2866 opcode);
2867 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2868 why = WHY_EXCEPTION;
2869 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002870
2871#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002873#endif
2874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 if (why == WHY_NOT) {
2884 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002885#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 /* This check is expensive! */
2887 if (PyErr_Occurred())
2888 fprintf(stderr,
2889 "XXX undetected error\n");
2890 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002891#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 READ_TIMESTAMP(loop1);
2893 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002894#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 }
2898 why = WHY_EXCEPTION;
2899 x = Py_None;
2900 err = 0;
2901 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2906 if (!PyErr_Occurred()) {
2907 PyErr_SetString(PyExc_SystemError,
2908 "error return without exception set");
2909 why = WHY_EXCEPTION;
2910 }
2911 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002912#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 else {
2914 /* This check is expensive! */
2915 if (PyErr_Occurred()) {
2916 char buf[128];
2917 sprintf(buf, "Stack unwind with exception "
2918 "set and why=%d", why);
2919 Py_FatalError(buf);
2920 }
2921 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002922#endif
2923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 if (why == WHY_EXCEPTION) {
2927 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (tstate->c_tracefunc != NULL)
2930 call_exc_trace(tstate->c_tracefunc,
2931 tstate->c_traceobj, f);
2932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (why == WHY_RERAISE)
2937 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002940
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002941fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 while (why != WHY_NOT && f->f_iblock > 0) {
2943 /* Peek at the current block. */
2944 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 assert(why != WHY_YIELD);
2947 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2948 why = WHY_NOT;
2949 JUMPTO(PyLong_AS_LONG(retval));
2950 Py_DECREF(retval);
2951 break;
2952 }
2953 /* Now we have to pop the block. */
2954 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 if (b->b_type == EXCEPT_HANDLER) {
2957 UNWIND_EXCEPT_HANDLER(b);
2958 continue;
2959 }
2960 UNWIND_BLOCK(b);
2961 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2962 why = WHY_NOT;
2963 JUMPTO(b->b_handler);
2964 break;
2965 }
2966 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2967 || b->b_type == SETUP_FINALLY)) {
2968 PyObject *exc, *val, *tb;
2969 int handler = b->b_handler;
2970 /* Beware, this invalidates all b->b_* fields */
2971 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2972 PUSH(tstate->exc_traceback);
2973 PUSH(tstate->exc_value);
2974 if (tstate->exc_type != NULL) {
2975 PUSH(tstate->exc_type);
2976 }
2977 else {
2978 Py_INCREF(Py_None);
2979 PUSH(Py_None);
2980 }
2981 PyErr_Fetch(&exc, &val, &tb);
2982 /* Make the raw exception data
2983 available to the handler,
2984 so a program can emulate the
2985 Python main loop. */
2986 PyErr_NormalizeException(
2987 &exc, &val, &tb);
2988 PyException_SetTraceback(val, tb);
2989 Py_INCREF(exc);
2990 tstate->exc_type = exc;
2991 Py_INCREF(val);
2992 tstate->exc_value = val;
2993 tstate->exc_traceback = tb;
2994 if (tb == NULL)
2995 tb = Py_None;
2996 Py_INCREF(tb);
2997 PUSH(tb);
2998 PUSH(val);
2999 PUSH(exc);
3000 why = WHY_NOT;
3001 JUMPTO(handler);
3002 break;
3003 }
3004 if (b->b_type == SETUP_FINALLY) {
3005 if (why & (WHY_RETURN | WHY_CONTINUE))
3006 PUSH(retval);
3007 PUSH(PyLong_FromLong((long)why));
3008 why = WHY_NOT;
3009 JUMPTO(b->b_handler);
3010 break;
3011 }
3012 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 if (why != WHY_NOT)
3017 break;
3018 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 assert(why != WHY_YIELD);
3023 /* Pop remaining stack entries. */
3024 while (!EMPTY()) {
3025 v = POP();
3026 Py_XDECREF(v);
3027 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 if (why != WHY_RETURN)
3030 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003031
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003032fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003033 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3034 /* The purpose of this block is to put aside the generator's exception
3035 state and restore that of the calling frame. If the current
3036 exception state is from the caller, we clear the exception values
3037 on the generator frame, so they are not swapped back in latter. The
3038 origin of the current exception state is determined by checking for
3039 except handler blocks, which we must be in iff a new exception
3040 state came into existence in this frame. (An uncaught exception
3041 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3042 int i;
3043 for (i = 0; i < f->f_iblock; i++)
3044 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3045 break;
3046 if (i == f->f_iblock)
3047 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003048 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003049 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003050 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003051 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 if (tstate->use_tracing) {
3054 if (tstate->c_tracefunc) {
3055 if (why == WHY_RETURN || why == WHY_YIELD) {
3056 if (call_trace(tstate->c_tracefunc,
3057 tstate->c_traceobj, f,
3058 PyTrace_RETURN, retval)) {
3059 Py_XDECREF(retval);
3060 retval = NULL;
3061 why = WHY_EXCEPTION;
3062 }
3063 }
3064 else if (why == WHY_EXCEPTION) {
3065 call_trace_protected(tstate->c_tracefunc,
3066 tstate->c_traceobj, f,
3067 PyTrace_RETURN, NULL);
3068 }
3069 }
3070 if (tstate->c_profilefunc) {
3071 if (why == WHY_EXCEPTION)
3072 call_trace_protected(tstate->c_profilefunc,
3073 tstate->c_profileobj, f,
3074 PyTrace_RETURN, NULL);
3075 else if (call_trace(tstate->c_profilefunc,
3076 tstate->c_profileobj, f,
3077 PyTrace_RETURN, retval)) {
3078 Py_XDECREF(retval);
3079 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003080 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 }
3082 }
3083 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003086exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 Py_LeaveRecursiveCall();
3088 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003091}
3092
Benjamin Petersonb204a422011-06-05 22:04:07 -05003093static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003094format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3095{
3096 int err;
3097 Py_ssize_t len = PyList_GET_SIZE(names);
3098 PyObject *name_str, *comma, *tail, *tmp;
3099
3100 assert(PyList_CheckExact(names));
3101 assert(len >= 1);
3102 /* Deal with the joys of natural language. */
3103 switch (len) {
3104 case 1:
3105 name_str = PyList_GET_ITEM(names, 0);
3106 Py_INCREF(name_str);
3107 break;
3108 case 2:
3109 name_str = PyUnicode_FromFormat("%U and %U",
3110 PyList_GET_ITEM(names, len - 2),
3111 PyList_GET_ITEM(names, len - 1));
3112 break;
3113 default:
3114 tail = PyUnicode_FromFormat(", %U, and %U",
3115 PyList_GET_ITEM(names, len - 2),
3116 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003117 if (tail == NULL)
3118 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003119 /* Chop off the last two objects in the list. This shouldn't actually
3120 fail, but we can't be too careful. */
3121 err = PyList_SetSlice(names, len - 2, len, NULL);
3122 if (err == -1) {
3123 Py_DECREF(tail);
3124 return;
3125 }
3126 /* Stitch everything up into a nice comma-separated list. */
3127 comma = PyUnicode_FromString(", ");
3128 if (comma == NULL) {
3129 Py_DECREF(tail);
3130 return;
3131 }
3132 tmp = PyUnicode_Join(comma, names);
3133 Py_DECREF(comma);
3134 if (tmp == NULL) {
3135 Py_DECREF(tail);
3136 return;
3137 }
3138 name_str = PyUnicode_Concat(tmp, tail);
3139 Py_DECREF(tmp);
3140 Py_DECREF(tail);
3141 break;
3142 }
3143 if (name_str == NULL)
3144 return;
3145 PyErr_Format(PyExc_TypeError,
3146 "%U() missing %i required %s argument%s: %U",
3147 co->co_name,
3148 len,
3149 kind,
3150 len == 1 ? "" : "s",
3151 name_str);
3152 Py_DECREF(name_str);
3153}
3154
3155static void
3156missing_arguments(PyCodeObject *co, int missing, int defcount,
3157 PyObject **fastlocals)
3158{
3159 int i, j = 0;
3160 int start, end;
3161 int positional = defcount != -1;
3162 const char *kind = positional ? "positional" : "keyword-only";
3163 PyObject *missing_names;
3164
3165 /* Compute the names of the arguments that are missing. */
3166 missing_names = PyList_New(missing);
3167 if (missing_names == NULL)
3168 return;
3169 if (positional) {
3170 start = 0;
3171 end = co->co_argcount - defcount;
3172 }
3173 else {
3174 start = co->co_argcount;
3175 end = start + co->co_kwonlyargcount;
3176 }
3177 for (i = start; i < end; i++) {
3178 if (GETLOCAL(i) == NULL) {
3179 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3180 PyObject *name = PyObject_Repr(raw);
3181 if (name == NULL) {
3182 Py_DECREF(missing_names);
3183 return;
3184 }
3185 PyList_SET_ITEM(missing_names, j++, name);
3186 }
3187 }
3188 assert(j == missing);
3189 format_missing(kind, co, missing_names);
3190 Py_DECREF(missing_names);
3191}
3192
3193static void
3194too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003195{
3196 int plural;
3197 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003198 int i;
3199 PyObject *sig, *kwonly_sig;
3200
Benjamin Petersone109c702011-06-24 09:37:26 -05003201 assert((co->co_flags & CO_VARARGS) == 0);
3202 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003203 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003204 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003205 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003206 if (defcount) {
3207 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003208 plural = 1;
3209 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3210 }
3211 else {
3212 plural = co->co_argcount != 1;
3213 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3214 }
3215 if (sig == NULL)
3216 return;
3217 if (kwonly_given) {
3218 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3219 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3220 kwonly_given != 1 ? "s" : "");
3221 if (kwonly_sig == NULL) {
3222 Py_DECREF(sig);
3223 return;
3224 }
3225 }
3226 else {
3227 /* This will not fail. */
3228 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003229 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003230 }
3231 PyErr_Format(PyExc_TypeError,
3232 "%U() takes %U positional argument%s but %d%U %s given",
3233 co->co_name,
3234 sig,
3235 plural ? "s" : "",
3236 given,
3237 kwonly_sig,
3238 given == 1 && !kwonly_given ? "was" : "were");
3239 Py_DECREF(sig);
3240 Py_DECREF(kwonly_sig);
3241}
3242
Guido van Rossumc2e20742006-02-27 22:32:47 +00003243/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003244 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003245 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003246
Tim Peters6d6c1a32001-08-02 04:15:00 +00003247PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003248PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 PyObject **args, int argcount, PyObject **kws, int kwcount,
3250 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003251{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003252 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 register PyFrameObject *f;
3254 register PyObject *retval = NULL;
3255 register PyObject **fastlocals, **freevars;
3256 PyThreadState *tstate = PyThreadState_GET();
3257 PyObject *x, *u;
3258 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003259 int i;
3260 int n = argcount;
3261 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 if (globals == NULL) {
3264 PyErr_SetString(PyExc_SystemError,
3265 "PyEval_EvalCodeEx: NULL globals");
3266 return NULL;
3267 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 assert(tstate != NULL);
3270 assert(globals != NULL);
3271 f = PyFrame_New(tstate, co, globals, locals);
3272 if (f == NULL)
3273 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 fastlocals = f->f_localsplus;
3276 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003277
Benjamin Petersonb204a422011-06-05 22:04:07 -05003278 /* Parse arguments. */
3279 if (co->co_flags & CO_VARKEYWORDS) {
3280 kwdict = PyDict_New();
3281 if (kwdict == NULL)
3282 goto fail;
3283 i = total_args;
3284 if (co->co_flags & CO_VARARGS)
3285 i++;
3286 SETLOCAL(i, kwdict);
3287 }
3288 if (argcount > co->co_argcount)
3289 n = co->co_argcount;
3290 for (i = 0; i < n; i++) {
3291 x = args[i];
3292 Py_INCREF(x);
3293 SETLOCAL(i, x);
3294 }
3295 if (co->co_flags & CO_VARARGS) {
3296 u = PyTuple_New(argcount - n);
3297 if (u == NULL)
3298 goto fail;
3299 SETLOCAL(total_args, u);
3300 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 x = args[i];
3302 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003303 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003305 }
3306 for (i = 0; i < kwcount; i++) {
3307 PyObject **co_varnames;
3308 PyObject *keyword = kws[2*i];
3309 PyObject *value = kws[2*i + 1];
3310 int j;
3311 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3312 PyErr_Format(PyExc_TypeError,
3313 "%U() keywords must be strings",
3314 co->co_name);
3315 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003317 /* Speed hack: do raw pointer compares. As names are
3318 normally interned this should almost always hit. */
3319 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3320 for (j = 0; j < total_args; j++) {
3321 PyObject *nm = co_varnames[j];
3322 if (nm == keyword)
3323 goto kw_found;
3324 }
3325 /* Slow fallback, just in case */
3326 for (j = 0; j < total_args; j++) {
3327 PyObject *nm = co_varnames[j];
3328 int cmp = PyObject_RichCompareBool(
3329 keyword, nm, Py_EQ);
3330 if (cmp > 0)
3331 goto kw_found;
3332 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003334 }
3335 if (j >= total_args && kwdict == NULL) {
3336 PyErr_Format(PyExc_TypeError,
3337 "%U() got an unexpected "
3338 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 co->co_name,
3340 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003341 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003343 PyDict_SetItem(kwdict, keyword, value);
3344 continue;
3345 kw_found:
3346 if (GETLOCAL(j) != NULL) {
3347 PyErr_Format(PyExc_TypeError,
3348 "%U() got multiple "
3349 "values for argument '%S'",
3350 co->co_name,
3351 keyword);
3352 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003354 Py_INCREF(value);
3355 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003357 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003358 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 goto fail;
3360 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003361 if (argcount < co->co_argcount) {
3362 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003363 int missing = 0;
3364 for (i = argcount; i < m; i++)
3365 if (GETLOCAL(i) == NULL)
3366 missing++;
3367 if (missing) {
3368 missing_arguments(co, missing, defcount, fastlocals);
3369 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003370 }
3371 if (n > m)
3372 i = n - m;
3373 else
3374 i = 0;
3375 for (; i < defcount; i++) {
3376 if (GETLOCAL(m+i) == NULL) {
3377 PyObject *def = defs[i];
3378 Py_INCREF(def);
3379 SETLOCAL(m+i, def);
3380 }
3381 }
3382 }
3383 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003384 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003385 for (i = co->co_argcount; i < total_args; i++) {
3386 PyObject *name;
3387 if (GETLOCAL(i) != NULL)
3388 continue;
3389 name = PyTuple_GET_ITEM(co->co_varnames, i);
3390 if (kwdefs != NULL) {
3391 PyObject *def = PyDict_GetItem(kwdefs, name);
3392 if (def) {
3393 Py_INCREF(def);
3394 SETLOCAL(i, def);
3395 continue;
3396 }
3397 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003398 missing++;
3399 }
3400 if (missing) {
3401 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003402 goto fail;
3403 }
3404 }
3405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003407 vars into frame. */
3408 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003410 int arg;
3411 /* Possibly account for the cell variable being an argument. */
3412 if (co->co_cell2arg != NULL &&
3413 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3414 c = PyCell_New(GETLOCAL(arg));
3415 else
3416 c = PyCell_New(NULL);
3417 if (c == NULL)
3418 goto fail;
3419 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003421 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3422 PyObject *o = PyTuple_GET_ITEM(closure, i);
3423 Py_INCREF(o);
3424 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 if (co->co_flags & CO_GENERATOR) {
3428 /* Don't need to keep the reference to f_back, it will be set
3429 * when the generator is resumed. */
3430 Py_XDECREF(f->f_back);
3431 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 /* Create a new generator that owns the ready to run frame
3436 * and return that as the value. */
3437 return PyGen_New(f);
3438 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003441
Thomas Woutersce272b62007-09-19 21:19:28 +00003442fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 /* decref'ing the frame can cause __del__ methods to get invoked,
3445 which can call back into Python. While we're done with the
3446 current Python frame (f), the associated C stack is still in use,
3447 so recursion_depth must be boosted for the duration.
3448 */
3449 assert(tstate != NULL);
3450 ++tstate->recursion_depth;
3451 Py_DECREF(f);
3452 --tstate->recursion_depth;
3453 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003454}
3455
3456
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003457static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003458special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003461 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003463 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 return NULL;
3465 }
3466 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003467}
3468
3469
Benjamin Peterson87880242011-07-03 16:48:31 -05003470/* These 3 functions deal with the exception state of generators. */
3471
3472static void
3473save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3474{
3475 PyObject *type, *value, *traceback;
3476 Py_XINCREF(tstate->exc_type);
3477 Py_XINCREF(tstate->exc_value);
3478 Py_XINCREF(tstate->exc_traceback);
3479 type = f->f_exc_type;
3480 value = f->f_exc_value;
3481 traceback = f->f_exc_traceback;
3482 f->f_exc_type = tstate->exc_type;
3483 f->f_exc_value = tstate->exc_value;
3484 f->f_exc_traceback = tstate->exc_traceback;
3485 Py_XDECREF(type);
3486 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003487 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003488}
3489
3490static void
3491swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3492{
3493 PyObject *tmp;
3494 tmp = tstate->exc_type;
3495 tstate->exc_type = f->f_exc_type;
3496 f->f_exc_type = tmp;
3497 tmp = tstate->exc_value;
3498 tstate->exc_value = f->f_exc_value;
3499 f->f_exc_value = tmp;
3500 tmp = tstate->exc_traceback;
3501 tstate->exc_traceback = f->f_exc_traceback;
3502 f->f_exc_traceback = tmp;
3503}
3504
3505static void
3506restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3507{
3508 PyObject *type, *value, *tb;
3509 type = tstate->exc_type;
3510 value = tstate->exc_value;
3511 tb = tstate->exc_traceback;
3512 tstate->exc_type = f->f_exc_type;
3513 tstate->exc_value = f->f_exc_value;
3514 tstate->exc_traceback = f->f_exc_traceback;
3515 f->f_exc_type = NULL;
3516 f->f_exc_value = NULL;
3517 f->f_exc_traceback = NULL;
3518 Py_XDECREF(type);
3519 Py_XDECREF(value);
3520 Py_XDECREF(tb);
3521}
3522
3523
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003524/* Logic for the raise statement (too complicated for inlining).
3525 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003526static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003527do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 if (exc == NULL) {
3532 /* Reraise */
3533 PyThreadState *tstate = PyThreadState_GET();
3534 PyObject *tb;
3535 type = tstate->exc_type;
3536 value = tstate->exc_value;
3537 tb = tstate->exc_traceback;
3538 if (type == Py_None) {
3539 PyErr_SetString(PyExc_RuntimeError,
3540 "No active exception to reraise");
3541 return WHY_EXCEPTION;
3542 }
3543 Py_XINCREF(type);
3544 Py_XINCREF(value);
3545 Py_XINCREF(tb);
3546 PyErr_Restore(type, value, tb);
3547 return WHY_RERAISE;
3548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 /* We support the following forms of raise:
3551 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003552 raise <instance>
3553 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 if (PyExceptionClass_Check(exc)) {
3556 type = exc;
3557 value = PyObject_CallObject(exc, NULL);
3558 if (value == NULL)
3559 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003560 if (!PyExceptionInstance_Check(value)) {
3561 PyErr_Format(PyExc_TypeError,
3562 "calling %R should have returned an instance of "
3563 "BaseException, not %R",
3564 type, Py_TYPE(value));
3565 goto raise_error;
3566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 }
3568 else if (PyExceptionInstance_Check(exc)) {
3569 value = exc;
3570 type = PyExceptionInstance_Class(exc);
3571 Py_INCREF(type);
3572 }
3573 else {
3574 /* Not something you can raise. You get an exception
3575 anyway, just not what you specified :-) */
3576 Py_DECREF(exc);
3577 PyErr_SetString(PyExc_TypeError,
3578 "exceptions must derive from BaseException");
3579 goto raise_error;
3580 }
Collin Winter828f04a2007-08-31 00:04:24 +00003581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 if (cause) {
3583 PyObject *fixed_cause;
3584 if (PyExceptionClass_Check(cause)) {
3585 fixed_cause = PyObject_CallObject(cause, NULL);
3586 if (fixed_cause == NULL)
3587 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003588 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003590 else if (PyExceptionInstance_Check(cause)) {
3591 fixed_cause = cause;
3592 }
3593 else if (cause == Py_None) {
3594 Py_DECREF(cause);
3595 fixed_cause = NULL;
3596 }
3597 else {
3598 PyErr_SetString(PyExc_TypeError,
3599 "exception causes must derive from "
3600 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 goto raise_error;
3602 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003603 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 }
Collin Winter828f04a2007-08-31 00:04:24 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 PyErr_SetObject(type, value);
3607 /* PyErr_SetObject incref's its arguments */
3608 Py_XDECREF(value);
3609 Py_XDECREF(type);
3610 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003611
3612raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 Py_XDECREF(value);
3614 Py_XDECREF(type);
3615 Py_XDECREF(cause);
3616 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003617}
3618
Tim Petersd6d010b2001-06-21 02:49:55 +00003619/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003620 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003621
Guido van Rossum0368b722007-05-11 16:50:42 +00003622 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3623 with a variable target.
3624*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003625
Barry Warsawe42b18f1997-08-25 22:13:04 +00003626static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003627unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 int i = 0, j = 0;
3630 Py_ssize_t ll = 0;
3631 PyObject *it; /* iter(v) */
3632 PyObject *w;
3633 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 it = PyObject_GetIter(v);
3638 if (it == NULL)
3639 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 for (; i < argcnt; i++) {
3642 w = PyIter_Next(it);
3643 if (w == NULL) {
3644 /* Iterator done, via error or exhaustion. */
3645 if (!PyErr_Occurred()) {
3646 PyErr_Format(PyExc_ValueError,
3647 "need more than %d value%s to unpack",
3648 i, i == 1 ? "" : "s");
3649 }
3650 goto Error;
3651 }
3652 *--sp = w;
3653 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 if (argcntafter == -1) {
3656 /* We better have exhausted the iterator now. */
3657 w = PyIter_Next(it);
3658 if (w == NULL) {
3659 if (PyErr_Occurred())
3660 goto Error;
3661 Py_DECREF(it);
3662 return 1;
3663 }
3664 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003665 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3666 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 goto Error;
3668 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 l = PySequence_List(it);
3671 if (l == NULL)
3672 goto Error;
3673 *--sp = l;
3674 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 ll = PyList_GET_SIZE(l);
3677 if (ll < argcntafter) {
3678 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3679 argcnt + ll);
3680 goto Error;
3681 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 /* Pop the "after-variable" args off the list. */
3684 for (j = argcntafter; j > 0; j--, i++) {
3685 *--sp = PyList_GET_ITEM(l, ll - j);
3686 }
3687 /* Resize the list. */
3688 Py_SIZE(l) = ll - argcntafter;
3689 Py_DECREF(it);
3690 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003691
Tim Petersd6d010b2001-06-21 02:49:55 +00003692Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 for (; i > 0; i--, sp++)
3694 Py_DECREF(*sp);
3695 Py_XDECREF(it);
3696 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003697}
3698
3699
Guido van Rossum96a42c81992-01-12 02:29:51 +00003700#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003701static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003702prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 printf("%s ", str);
3705 if (PyObject_Print(v, stdout, 0) != 0)
3706 PyErr_Clear(); /* Don't know what else to do */
3707 printf("\n");
3708 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003709}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003710#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003711
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003712static void
Fred Drake5755ce62001-06-27 19:19:46 +00003713call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 PyObject *type, *value, *traceback, *arg;
3716 int err;
3717 PyErr_Fetch(&type, &value, &traceback);
3718 if (value == NULL) {
3719 value = Py_None;
3720 Py_INCREF(value);
3721 }
3722 arg = PyTuple_Pack(3, type, value, traceback);
3723 if (arg == NULL) {
3724 PyErr_Restore(type, value, traceback);
3725 return;
3726 }
3727 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3728 Py_DECREF(arg);
3729 if (err == 0)
3730 PyErr_Restore(type, value, traceback);
3731 else {
3732 Py_XDECREF(type);
3733 Py_XDECREF(value);
3734 Py_XDECREF(traceback);
3735 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003736}
3737
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003738static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003739call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 PyObject *type, *value, *traceback;
3743 int err;
3744 PyErr_Fetch(&type, &value, &traceback);
3745 err = call_trace(func, obj, frame, what, arg);
3746 if (err == 0)
3747 {
3748 PyErr_Restore(type, value, traceback);
3749 return 0;
3750 }
3751 else {
3752 Py_XDECREF(type);
3753 Py_XDECREF(value);
3754 Py_XDECREF(traceback);
3755 return -1;
3756 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003757}
3758
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003759static int
Fred Drake5755ce62001-06-27 19:19:46 +00003760call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 register PyThreadState *tstate = frame->f_tstate;
3764 int result;
3765 if (tstate->tracing)
3766 return 0;
3767 tstate->tracing++;
3768 tstate->use_tracing = 0;
3769 result = func(obj, frame, what, arg);
3770 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3771 || (tstate->c_profilefunc != NULL));
3772 tstate->tracing--;
3773 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003774}
3775
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003776PyObject *
3777_PyEval_CallTracing(PyObject *func, PyObject *args)
3778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 PyFrameObject *frame = PyEval_GetFrame();
3780 PyThreadState *tstate = frame->f_tstate;
3781 int save_tracing = tstate->tracing;
3782 int save_use_tracing = tstate->use_tracing;
3783 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 tstate->tracing = 0;
3786 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3787 || (tstate->c_profilefunc != NULL));
3788 result = PyObject_Call(func, args, NULL);
3789 tstate->tracing = save_tracing;
3790 tstate->use_tracing = save_use_tracing;
3791 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003792}
3793
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003794/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003795static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003796maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3798 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 int result = 0;
3801 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 /* If the last instruction executed isn't in the current
3804 instruction window, reset the window.
3805 */
3806 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3807 PyAddrPair bounds;
3808 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3809 &bounds);
3810 *instr_lb = bounds.ap_lower;
3811 *instr_ub = bounds.ap_upper;
3812 }
3813 /* If the last instruction falls at the start of a line or if
3814 it represents a jump backwards, update the frame's line
3815 number and call the trace function. */
3816 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3817 frame->f_lineno = line;
3818 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3819 }
3820 *instr_prev = frame->f_lasti;
3821 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003822}
3823
Fred Drake5755ce62001-06-27 19:19:46 +00003824void
3825PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 PyThreadState *tstate = PyThreadState_GET();
3828 PyObject *temp = tstate->c_profileobj;
3829 Py_XINCREF(arg);
3830 tstate->c_profilefunc = NULL;
3831 tstate->c_profileobj = NULL;
3832 /* Must make sure that tracing is not ignored if 'temp' is freed */
3833 tstate->use_tracing = tstate->c_tracefunc != NULL;
3834 Py_XDECREF(temp);
3835 tstate->c_profilefunc = func;
3836 tstate->c_profileobj = arg;
3837 /* Flag that tracing or profiling is turned on */
3838 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003839}
3840
3841void
3842PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PyThreadState *tstate = PyThreadState_GET();
3845 PyObject *temp = tstate->c_traceobj;
3846 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3847 Py_XINCREF(arg);
3848 tstate->c_tracefunc = NULL;
3849 tstate->c_traceobj = NULL;
3850 /* Must make sure that profiling is not ignored if 'temp' is freed */
3851 tstate->use_tracing = tstate->c_profilefunc != NULL;
3852 Py_XDECREF(temp);
3853 tstate->c_tracefunc = func;
3854 tstate->c_traceobj = arg;
3855 /* Flag that tracing or profiling is turned on */
3856 tstate->use_tracing = ((func != NULL)
3857 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003858}
3859
Guido van Rossumb209a111997-04-29 18:18:01 +00003860PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003861PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 PyFrameObject *current_frame = PyEval_GetFrame();
3864 if (current_frame == NULL)
3865 return PyThreadState_GET()->interp->builtins;
3866 else
3867 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003868}
3869
Guido van Rossumb209a111997-04-29 18:18:01 +00003870PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003871PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 PyFrameObject *current_frame = PyEval_GetFrame();
3874 if (current_frame == NULL)
3875 return NULL;
3876 PyFrame_FastToLocals(current_frame);
3877 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003878}
3879
Guido van Rossumb209a111997-04-29 18:18:01 +00003880PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003881PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003883 PyFrameObject *current_frame = PyEval_GetFrame();
3884 if (current_frame == NULL)
3885 return NULL;
3886 else
3887 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003888}
3889
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003890PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003891PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 PyThreadState *tstate = PyThreadState_GET();
3894 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003895}
3896
Guido van Rossum6135a871995-01-09 17:53:26 +00003897int
Tim Peters5ba58662001-07-16 02:29:45 +00003898PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 PyFrameObject *current_frame = PyEval_GetFrame();
3901 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 if (current_frame != NULL) {
3904 const int codeflags = current_frame->f_code->co_flags;
3905 const int compilerflags = codeflags & PyCF_MASK;
3906 if (compilerflags) {
3907 result = 1;
3908 cf->cf_flags |= compilerflags;
3909 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003910#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 if (codeflags & CO_GENERATOR_ALLOWED) {
3912 result = 1;
3913 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3914 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003915#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 }
3917 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003918}
3919
Guido van Rossum3f5da241990-12-20 15:06:42 +00003920
Guido van Rossum681d79a1995-07-18 14:51:37 +00003921/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003922 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003923
Guido van Rossumb209a111997-04-29 18:18:01 +00003924PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003925PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 if (arg == NULL) {
3930 arg = PyTuple_New(0);
3931 if (arg == NULL)
3932 return NULL;
3933 }
3934 else if (!PyTuple_Check(arg)) {
3935 PyErr_SetString(PyExc_TypeError,
3936 "argument list must be a tuple");
3937 return NULL;
3938 }
3939 else
3940 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 if (kw != NULL && !PyDict_Check(kw)) {
3943 PyErr_SetString(PyExc_TypeError,
3944 "keyword list must be a dictionary");
3945 Py_DECREF(arg);
3946 return NULL;
3947 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 result = PyObject_Call(func, arg, kw);
3950 Py_DECREF(arg);
3951 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003952}
3953
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003954const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003955PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 if (PyMethod_Check(func))
3958 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3959 else if (PyFunction_Check(func))
3960 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3961 else if (PyCFunction_Check(func))
3962 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3963 else
3964 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003965}
3966
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003967const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003968PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 if (PyMethod_Check(func))
3971 return "()";
3972 else if (PyFunction_Check(func))
3973 return "()";
3974 else if (PyCFunction_Check(func))
3975 return "()";
3976 else
3977 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003978}
3979
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003980static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003981err_args(PyObject *func, int flags, int nargs)
3982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 if (flags & METH_NOARGS)
3984 PyErr_Format(PyExc_TypeError,
3985 "%.200s() takes no arguments (%d given)",
3986 ((PyCFunctionObject *)func)->m_ml->ml_name,
3987 nargs);
3988 else
3989 PyErr_Format(PyExc_TypeError,
3990 "%.200s() takes exactly one argument (%d given)",
3991 ((PyCFunctionObject *)func)->m_ml->ml_name,
3992 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003993}
3994
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003995#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003996if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 if (call_trace(tstate->c_profilefunc, \
3998 tstate->c_profileobj, \
3999 tstate->frame, PyTrace_C_CALL, \
4000 func)) { \
4001 x = NULL; \
4002 } \
4003 else { \
4004 x = call; \
4005 if (tstate->c_profilefunc != NULL) { \
4006 if (x == NULL) { \
4007 call_trace_protected(tstate->c_profilefunc, \
4008 tstate->c_profileobj, \
4009 tstate->frame, PyTrace_C_EXCEPTION, \
4010 func); \
4011 /* XXX should pass (type, value, tb) */ \
4012 } else { \
4013 if (call_trace(tstate->c_profilefunc, \
4014 tstate->c_profileobj, \
4015 tstate->frame, PyTrace_C_RETURN, \
4016 func)) { \
4017 Py_DECREF(x); \
4018 x = NULL; \
4019 } \
4020 } \
4021 } \
4022 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004023} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 x = call; \
4025 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004026
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004027static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004028call_function(PyObject ***pp_stack, int oparg
4029#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004031#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 int na = oparg & 0xff;
4035 int nk = (oparg>>8) & 0xff;
4036 int n = na + 2 * nk;
4037 PyObject **pfunc = (*pp_stack) - n - 1;
4038 PyObject *func = *pfunc;
4039 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 /* Always dispatch PyCFunction first, because these are
4042 presumed to be the most frequent callable object.
4043 */
4044 if (PyCFunction_Check(func) && nk == 0) {
4045 int flags = PyCFunction_GET_FLAGS(func);
4046 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 PCALL(PCALL_CFUNCTION);
4049 if (flags & (METH_NOARGS | METH_O)) {
4050 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4051 PyObject *self = PyCFunction_GET_SELF(func);
4052 if (flags & METH_NOARGS && na == 0) {
4053 C_TRACE(x, (*meth)(self,NULL));
4054 }
4055 else if (flags & METH_O && na == 1) {
4056 PyObject *arg = EXT_POP(*pp_stack);
4057 C_TRACE(x, (*meth)(self,arg));
4058 Py_DECREF(arg);
4059 }
4060 else {
4061 err_args(func, flags, na);
4062 x = NULL;
4063 }
4064 }
4065 else {
4066 PyObject *callargs;
4067 callargs = load_args(pp_stack, na);
4068 READ_TIMESTAMP(*pintr0);
4069 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4070 READ_TIMESTAMP(*pintr1);
4071 Py_XDECREF(callargs);
4072 }
4073 } else {
4074 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4075 /* optimize access to bound methods */
4076 PyObject *self = PyMethod_GET_SELF(func);
4077 PCALL(PCALL_METHOD);
4078 PCALL(PCALL_BOUND_METHOD);
4079 Py_INCREF(self);
4080 func = PyMethod_GET_FUNCTION(func);
4081 Py_INCREF(func);
4082 Py_DECREF(*pfunc);
4083 *pfunc = self;
4084 na++;
4085 n++;
4086 } else
4087 Py_INCREF(func);
4088 READ_TIMESTAMP(*pintr0);
4089 if (PyFunction_Check(func))
4090 x = fast_function(func, pp_stack, n, na, nk);
4091 else
4092 x = do_call(func, pp_stack, na, nk);
4093 READ_TIMESTAMP(*pintr1);
4094 Py_DECREF(func);
4095 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 /* Clear the stack of the function object. Also removes
4098 the arguments in case they weren't consumed already
4099 (fast_function() and err_args() leave them on the stack).
4100 */
4101 while ((*pp_stack) > pfunc) {
4102 w = EXT_POP(*pp_stack);
4103 Py_DECREF(w);
4104 PCALL(PCALL_POP);
4105 }
4106 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004107}
4108
Jeremy Hylton192690e2002-08-16 18:36:11 +00004109/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004110 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004111 For the simplest case -- a function that takes only positional
4112 arguments and is called with only positional arguments -- it
4113 inlines the most primitive frame setup code from
4114 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4115 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004116*/
4117
4118static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004119fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4122 PyObject *globals = PyFunction_GET_GLOBALS(func);
4123 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4124 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4125 PyObject **d = NULL;
4126 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 PCALL(PCALL_FUNCTION);
4129 PCALL(PCALL_FAST_FUNCTION);
4130 if (argdefs == NULL && co->co_argcount == n &&
4131 co->co_kwonlyargcount == 0 && nk==0 &&
4132 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4133 PyFrameObject *f;
4134 PyObject *retval = NULL;
4135 PyThreadState *tstate = PyThreadState_GET();
4136 PyObject **fastlocals, **stack;
4137 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 PCALL(PCALL_FASTER_FUNCTION);
4140 assert(globals != NULL);
4141 /* XXX Perhaps we should create a specialized
4142 PyFrame_New() that doesn't take locals, but does
4143 take builtins without sanity checking them.
4144 */
4145 assert(tstate != NULL);
4146 f = PyFrame_New(tstate, co, globals, NULL);
4147 if (f == NULL)
4148 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 fastlocals = f->f_localsplus;
4151 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 for (i = 0; i < n; i++) {
4154 Py_INCREF(*stack);
4155 fastlocals[i] = *stack++;
4156 }
4157 retval = PyEval_EvalFrameEx(f,0);
4158 ++tstate->recursion_depth;
4159 Py_DECREF(f);
4160 --tstate->recursion_depth;
4161 return retval;
4162 }
4163 if (argdefs != NULL) {
4164 d = &PyTuple_GET_ITEM(argdefs, 0);
4165 nd = Py_SIZE(argdefs);
4166 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004167 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 (PyObject *)NULL, (*pp_stack)-n, na,
4169 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4170 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004171}
4172
4173static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004174update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4175 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 PyObject *kwdict = NULL;
4178 if (orig_kwdict == NULL)
4179 kwdict = PyDict_New();
4180 else {
4181 kwdict = PyDict_Copy(orig_kwdict);
4182 Py_DECREF(orig_kwdict);
4183 }
4184 if (kwdict == NULL)
4185 return NULL;
4186 while (--nk >= 0) {
4187 int err;
4188 PyObject *value = EXT_POP(*pp_stack);
4189 PyObject *key = EXT_POP(*pp_stack);
4190 if (PyDict_GetItem(kwdict, key) != NULL) {
4191 PyErr_Format(PyExc_TypeError,
4192 "%.200s%s got multiple values "
4193 "for keyword argument '%U'",
4194 PyEval_GetFuncName(func),
4195 PyEval_GetFuncDesc(func),
4196 key);
4197 Py_DECREF(key);
4198 Py_DECREF(value);
4199 Py_DECREF(kwdict);
4200 return NULL;
4201 }
4202 err = PyDict_SetItem(kwdict, key, value);
4203 Py_DECREF(key);
4204 Py_DECREF(value);
4205 if (err) {
4206 Py_DECREF(kwdict);
4207 return NULL;
4208 }
4209 }
4210 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004211}
4212
4213static PyObject *
4214update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 callargs = PyTuple_New(nstack + nstar);
4220 if (callargs == NULL) {
4221 return NULL;
4222 }
4223 if (nstar) {
4224 int i;
4225 for (i = 0; i < nstar; i++) {
4226 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4227 Py_INCREF(a);
4228 PyTuple_SET_ITEM(callargs, nstack + i, a);
4229 }
4230 }
4231 while (--nstack >= 0) {
4232 w = EXT_POP(*pp_stack);
4233 PyTuple_SET_ITEM(callargs, nstack, w);
4234 }
4235 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004236}
4237
4238static PyObject *
4239load_args(PyObject ***pp_stack, int na)
4240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 PyObject *args = PyTuple_New(na);
4242 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 if (args == NULL)
4245 return NULL;
4246 while (--na >= 0) {
4247 w = EXT_POP(*pp_stack);
4248 PyTuple_SET_ITEM(args, na, w);
4249 }
4250 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004251}
4252
4253static PyObject *
4254do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 PyObject *callargs = NULL;
4257 PyObject *kwdict = NULL;
4258 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 if (nk > 0) {
4261 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4262 if (kwdict == NULL)
4263 goto call_fail;
4264 }
4265 callargs = load_args(pp_stack, na);
4266 if (callargs == NULL)
4267 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004268#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 /* At this point, we have to look at the type of func to
4270 update the call stats properly. Do it here so as to avoid
4271 exposing the call stats machinery outside ceval.c
4272 */
4273 if (PyFunction_Check(func))
4274 PCALL(PCALL_FUNCTION);
4275 else if (PyMethod_Check(func))
4276 PCALL(PCALL_METHOD);
4277 else if (PyType_Check(func))
4278 PCALL(PCALL_TYPE);
4279 else if (PyCFunction_Check(func))
4280 PCALL(PCALL_CFUNCTION);
4281 else
4282 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004283#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 if (PyCFunction_Check(func)) {
4285 PyThreadState *tstate = PyThreadState_GET();
4286 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4287 }
4288 else
4289 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004290call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 Py_XDECREF(callargs);
4292 Py_XDECREF(kwdict);
4293 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004294}
4295
4296static PyObject *
4297ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 int nstar = 0;
4300 PyObject *callargs = NULL;
4301 PyObject *stararg = NULL;
4302 PyObject *kwdict = NULL;
4303 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 if (flags & CALL_FLAG_KW) {
4306 kwdict = EXT_POP(*pp_stack);
4307 if (!PyDict_Check(kwdict)) {
4308 PyObject *d;
4309 d = PyDict_New();
4310 if (d == NULL)
4311 goto ext_call_fail;
4312 if (PyDict_Update(d, kwdict) != 0) {
4313 Py_DECREF(d);
4314 /* PyDict_Update raises attribute
4315 * error (percolated from an attempt
4316 * to get 'keys' attribute) instead of
4317 * a type error if its second argument
4318 * is not a mapping.
4319 */
4320 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4321 PyErr_Format(PyExc_TypeError,
4322 "%.200s%.200s argument after ** "
4323 "must be a mapping, not %.200s",
4324 PyEval_GetFuncName(func),
4325 PyEval_GetFuncDesc(func),
4326 kwdict->ob_type->tp_name);
4327 }
4328 goto ext_call_fail;
4329 }
4330 Py_DECREF(kwdict);
4331 kwdict = d;
4332 }
4333 }
4334 if (flags & CALL_FLAG_VAR) {
4335 stararg = EXT_POP(*pp_stack);
4336 if (!PyTuple_Check(stararg)) {
4337 PyObject *t = NULL;
4338 t = PySequence_Tuple(stararg);
4339 if (t == NULL) {
4340 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4341 PyErr_Format(PyExc_TypeError,
4342 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004343 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 PyEval_GetFuncName(func),
4345 PyEval_GetFuncDesc(func),
4346 stararg->ob_type->tp_name);
4347 }
4348 goto ext_call_fail;
4349 }
4350 Py_DECREF(stararg);
4351 stararg = t;
4352 }
4353 nstar = PyTuple_GET_SIZE(stararg);
4354 }
4355 if (nk > 0) {
4356 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4357 if (kwdict == NULL)
4358 goto ext_call_fail;
4359 }
4360 callargs = update_star_args(na, nstar, stararg, pp_stack);
4361 if (callargs == NULL)
4362 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004363#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 /* At this point, we have to look at the type of func to
4365 update the call stats properly. Do it here so as to avoid
4366 exposing the call stats machinery outside ceval.c
4367 */
4368 if (PyFunction_Check(func))
4369 PCALL(PCALL_FUNCTION);
4370 else if (PyMethod_Check(func))
4371 PCALL(PCALL_METHOD);
4372 else if (PyType_Check(func))
4373 PCALL(PCALL_TYPE);
4374 else if (PyCFunction_Check(func))
4375 PCALL(PCALL_CFUNCTION);
4376 else
4377 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004378#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 if (PyCFunction_Check(func)) {
4380 PyThreadState *tstate = PyThreadState_GET();
4381 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4382 }
4383 else
4384 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004385ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 Py_XDECREF(callargs);
4387 Py_XDECREF(kwdict);
4388 Py_XDECREF(stararg);
4389 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004390}
4391
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004392/* Extract a slice index from a PyInt or PyLong or an object with the
4393 nb_index slot defined, and store in *pi.
4394 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4395 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 +00004396 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004397*/
Tim Petersb5196382001-12-16 19:44:20 +00004398/* Note: If v is NULL, return success without storing into *pi. This
4399 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4400 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004401*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004402int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004403_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (v != NULL) {
4406 Py_ssize_t x;
4407 if (PyIndex_Check(v)) {
4408 x = PyNumber_AsSsize_t(v, NULL);
4409 if (x == -1 && PyErr_Occurred())
4410 return 0;
4411 }
4412 else {
4413 PyErr_SetString(PyExc_TypeError,
4414 "slice indices must be integers or "
4415 "None or have an __index__ method");
4416 return 0;
4417 }
4418 *pi = x;
4419 }
4420 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004421}
4422
Guido van Rossum486364b2007-06-30 05:01:58 +00004423#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004425
Guido van Rossumb209a111997-04-29 18:18:01 +00004426static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004427cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 int res = 0;
4430 switch (op) {
4431 case PyCmp_IS:
4432 res = (v == w);
4433 break;
4434 case PyCmp_IS_NOT:
4435 res = (v != w);
4436 break;
4437 case PyCmp_IN:
4438 res = PySequence_Contains(w, v);
4439 if (res < 0)
4440 return NULL;
4441 break;
4442 case PyCmp_NOT_IN:
4443 res = PySequence_Contains(w, v);
4444 if (res < 0)
4445 return NULL;
4446 res = !res;
4447 break;
4448 case PyCmp_EXC_MATCH:
4449 if (PyTuple_Check(w)) {
4450 Py_ssize_t i, length;
4451 length = PyTuple_Size(w);
4452 for (i = 0; i < length; i += 1) {
4453 PyObject *exc = PyTuple_GET_ITEM(w, i);
4454 if (!PyExceptionClass_Check(exc)) {
4455 PyErr_SetString(PyExc_TypeError,
4456 CANNOT_CATCH_MSG);
4457 return NULL;
4458 }
4459 }
4460 }
4461 else {
4462 if (!PyExceptionClass_Check(w)) {
4463 PyErr_SetString(PyExc_TypeError,
4464 CANNOT_CATCH_MSG);
4465 return NULL;
4466 }
4467 }
4468 res = PyErr_GivenExceptionMatches(v, w);
4469 break;
4470 default:
4471 return PyObject_RichCompare(v, w, op);
4472 }
4473 v = res ? Py_True : Py_False;
4474 Py_INCREF(v);
4475 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004476}
4477
Thomas Wouters52152252000-08-17 22:55:00 +00004478static PyObject *
4479import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 x = PyObject_GetAttr(v, name);
4484 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4485 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4486 }
4487 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004488}
Guido van Rossumac7be682001-01-17 15:42:30 +00004489
Thomas Wouters52152252000-08-17 22:55:00 +00004490static int
4491import_all_from(PyObject *locals, PyObject *v)
4492{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004493 _Py_IDENTIFIER(__all__);
4494 _Py_IDENTIFIER(__dict__);
4495 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 PyObject *dict, *name, *value;
4497 int skip_leading_underscores = 0;
4498 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 if (all == NULL) {
4501 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4502 return -1; /* Unexpected error */
4503 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004504 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (dict == NULL) {
4506 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4507 return -1;
4508 PyErr_SetString(PyExc_ImportError,
4509 "from-import-* object has no __dict__ and no __all__");
4510 return -1;
4511 }
4512 all = PyMapping_Keys(dict);
4513 Py_DECREF(dict);
4514 if (all == NULL)
4515 return -1;
4516 skip_leading_underscores = 1;
4517 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 for (pos = 0, err = 0; ; pos++) {
4520 name = PySequence_GetItem(all, pos);
4521 if (name == NULL) {
4522 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4523 err = -1;
4524 else
4525 PyErr_Clear();
4526 break;
4527 }
4528 if (skip_leading_underscores &&
4529 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004530 PyUnicode_READY(name) != -1 &&
4531 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 {
4533 Py_DECREF(name);
4534 continue;
4535 }
4536 value = PyObject_GetAttr(v, name);
4537 if (value == NULL)
4538 err = -1;
4539 else if (PyDict_CheckExact(locals))
4540 err = PyDict_SetItem(locals, name, value);
4541 else
4542 err = PyObject_SetItem(locals, name, value);
4543 Py_DECREF(name);
4544 Py_XDECREF(value);
4545 if (err != 0)
4546 break;
4547 }
4548 Py_DECREF(all);
4549 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004550}
4551
Guido van Rossumac7be682001-01-17 15:42:30 +00004552static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004553format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 if (!obj)
4558 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 obj_str = _PyUnicode_AsString(obj);
4561 if (!obj_str)
4562 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004565}
Guido van Rossum950361c1997-01-24 13:49:28 +00004566
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004567static void
4568format_exc_unbound(PyCodeObject *co, int oparg)
4569{
4570 PyObject *name;
4571 /* Don't stomp existing exception */
4572 if (PyErr_Occurred())
4573 return;
4574 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4575 name = PyTuple_GET_ITEM(co->co_cellvars,
4576 oparg);
4577 format_exc_check_arg(
4578 PyExc_UnboundLocalError,
4579 UNBOUNDLOCAL_ERROR_MSG,
4580 name);
4581 } else {
4582 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4583 PyTuple_GET_SIZE(co->co_cellvars));
4584 format_exc_check_arg(PyExc_NameError,
4585 UNBOUNDFREE_ERROR_MSG, name);
4586 }
4587}
4588
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004589static PyObject *
4590unicode_concatenate(PyObject *v, PyObject *w,
4591 PyFrameObject *f, unsigned char *next_instr)
4592{
4593 PyObject *res;
4594 if (Py_REFCNT(v) == 2) {
4595 /* In the common case, there are 2 references to the value
4596 * stored in 'variable' when the += is performed: one on the
4597 * value stack (in 'v') and one still stored in the
4598 * 'variable'. We try to delete the variable now to reduce
4599 * the refcnt to 1.
4600 */
4601 switch (*next_instr) {
4602 case STORE_FAST:
4603 {
4604 int oparg = PEEKARG();
4605 PyObject **fastlocals = f->f_localsplus;
4606 if (GETLOCAL(oparg) == v)
4607 SETLOCAL(oparg, NULL);
4608 break;
4609 }
4610 case STORE_DEREF:
4611 {
4612 PyObject **freevars = (f->f_localsplus +
4613 f->f_code->co_nlocals);
4614 PyObject *c = freevars[PEEKARG()];
4615 if (PyCell_GET(c) == v)
4616 PyCell_Set(c, NULL);
4617 break;
4618 }
4619 case STORE_NAME:
4620 {
4621 PyObject *names = f->f_code->co_names;
4622 PyObject *name = GETITEM(names, PEEKARG());
4623 PyObject *locals = f->f_locals;
4624 if (PyDict_CheckExact(locals) &&
4625 PyDict_GetItem(locals, name) == v) {
4626 if (PyDict_DelItem(locals, name) != 0) {
4627 PyErr_Clear();
4628 }
4629 }
4630 break;
4631 }
4632 }
4633 }
4634 res = v;
4635 PyUnicode_Append(&res, w);
4636 return res;
4637}
4638
Guido van Rossum950361c1997-01-24 13:49:28 +00004639#ifdef DYNAMIC_EXECUTION_PROFILE
4640
Skip Montanarof118cb12001-10-15 20:51:38 +00004641static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004642getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 int i;
4645 PyObject *l = PyList_New(256);
4646 if (l == NULL) return NULL;
4647 for (i = 0; i < 256; i++) {
4648 PyObject *x = PyLong_FromLong(a[i]);
4649 if (x == NULL) {
4650 Py_DECREF(l);
4651 return NULL;
4652 }
4653 PyList_SetItem(l, i, x);
4654 }
4655 for (i = 0; i < 256; i++)
4656 a[i] = 0;
4657 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004658}
4659
4660PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004661_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004662{
4663#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004665#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 int i;
4667 PyObject *l = PyList_New(257);
4668 if (l == NULL) return NULL;
4669 for (i = 0; i < 257; i++) {
4670 PyObject *x = getarray(dxpairs[i]);
4671 if (x == NULL) {
4672 Py_DECREF(l);
4673 return NULL;
4674 }
4675 PyList_SetItem(l, i, x);
4676 }
4677 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004678#endif
4679}
4680
4681#endif