blob: bcb15e796409456cf7d2ab77853b1b3daea4db9e [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 Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouters8ce81f72007-09-20 18:22:40 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
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{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 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;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Thomas Wouters477c8d52006-05-27 19:21:47 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000105static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * cmp_outcome(int, PyObject *, PyObject *);
117static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000118static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000119static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120static PyObject * unicode_concatenate(PyObject *, PyObject *,
121 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000122
Paul Prescode68140d2000-08-30 20:25:01 +0000123#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000124 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125#define GLOBAL_NAME_ERROR_MSG \
126 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000129#define UNBOUNDFREE_ERROR_MSG \
130 "free variable '%.200s' referenced before assignment" \
131 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000132
Guido van Rossum950361c1997-01-24 13:49:28 +0000133/* Dynamic execution profile */
134#ifdef DYNAMIC_EXECUTION_PROFILE
135#ifdef DXPAIRS
136static long dxpairs[257][256];
137#define dxp dxpairs[256]
138#else
139static long dxp[256];
140#endif
141#endif
142
Jeremy Hylton985eba52003-02-05 23:13:00 +0000143/* Function call profile */
144#ifdef CALL_PROFILE
145#define PCALL_NUM 11
146static int pcall[PCALL_NUM];
147
148#define PCALL_ALL 0
149#define PCALL_FUNCTION 1
150#define PCALL_FAST_FUNCTION 2
151#define PCALL_FASTER_FUNCTION 3
152#define PCALL_METHOD 4
153#define PCALL_BOUND_METHOD 5
154#define PCALL_CFUNCTION 6
155#define PCALL_TYPE 7
156#define PCALL_GENERATOR 8
157#define PCALL_OTHER 9
158#define PCALL_POP 10
159
160/* Notes about the statistics
161
162 PCALL_FAST stats
163
164 FAST_FUNCTION means no argument tuple needs to be created.
165 FASTER_FUNCTION means that the fast-path frame setup code is used.
166
167 If there is a method call where the call can be optimized by changing
168 the argument tuple and calling the function directly, it gets recorded
169 twice.
170
171 As a result, the relationship among the statistics appears to be
172 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
173 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
174 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
175 PCALL_METHOD > PCALL_BOUND_METHOD
176*/
177
178#define PCALL(POS) pcall[POS]++
179
180PyObject *
181PyEval_GetCallStats(PyObject *self)
182{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000184 pcall[0], pcall[1], pcall[2], pcall[3],
185 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187}
188#else
189#define PCALL(O)
190
191PyObject *
192PyEval_GetCallStats(PyObject *self)
193{
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197#endif
198
Tim Peters5ca576e2001-06-18 22:08:13 +0000199
Guido van Rossume59214e1994-08-30 08:01:59 +0000200#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000205#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000207static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000208static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209
Tim Peters7f468f22004-10-11 02:40:51 +0000210int
211PyEval_ThreadsInitialized(void)
212{
213 return interpreter_lock != 0;
214}
215
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000216void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000217PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000220 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221 interpreter_lock = PyThread_allocate_lock();
222 PyThread_acquire_lock(interpreter_lock, 1);
223 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000224}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000225
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230}
231
232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236}
237
238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000240{
241 if (tstate == NULL)
242 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000243 /* Check someone has called PyEval_InitThreads() to create the lock */
244 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000245 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246 if (PyThreadState_Swap(tstate) != NULL)
247 Py_FatalError(
248 "PyEval_AcquireThread: non-NULL old thread state");
249}
250
251void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000253{
254 if (tstate == NULL)
255 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
256 if (PyThreadState_Swap(NULL) != tstate)
257 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000260
261/* This function is called from PyOS_AfterFork to ensure that newly
262 created child processes don't hold locks referring to threads which
263 are not running in the child process. (This could also be done using
264 pthread_atfork mechanism, at least for the pthreads implementation.) */
265
266void
267PyEval_ReInitThreads(void)
268{
269 if (!interpreter_lock)
270 return;
271 /*XXX Can't use PyThread_free_lock here because it does too
272 much error-checking. Doing this cleanly would require
273 adding a new function to each thread_*.h. Instead, just
274 create a new lock and waste a little bit of memory */
275 interpreter_lock = PyThread_allocate_lock();
276 PyThread_acquire_lock(interpreter_lock, 1);
277 main_thread = PyThread_get_thread_ident();
278}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279#endif
280
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281/* Functions save_thread and restore_thread are always defined so
282 dynamically loaded modules needn't be compiled separately for use
283 with and without threads: */
284
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000285PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000286PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000287{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000288 PyThreadState *tstate = PyThreadState_Swap(NULL);
289 if (tstate == NULL)
290 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000291#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000292 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000293 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000295 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296}
297
298void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000299PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000301 if (tstate == NULL)
302 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000303#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000305 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000306 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308 }
309#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000310 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311}
312
313
Guido van Rossuma9672091994-09-14 13:31:22 +0000314/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
315 signal handlers or Mac I/O completion routines) can schedule calls
316 to a function to be called synchronously.
317 The synchronous function is called with one void* argument.
318 It should return 0 for success or -1 for failure -- failure should
319 be accompanied by an exception.
320
321 If registry succeeds, the registry function returns 0; if it fails
322 (e.g. due to too many pending calls) it returns -1 (without setting
323 an exception condition).
324
325 Note that because registry may occur from within signal handlers,
326 or other asynchronous events, calling malloc() is unsafe!
327
328#ifdef WITH_THREAD
329 Any thread can schedule pending calls, but only the main thread
330 will execute them.
331#endif
332
333 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
334 There are two possible race conditions:
335 (1) nested asynchronous registry calls;
336 (2) registry calls made while pending calls are being processed.
337 While (1) is very unlikely, (2) is a real possibility.
338 The current code is safe against (2), but not against (1).
339 The safety against (2) is derived from the fact that only one
340 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000341
Guido van Rossuma027efa1997-05-05 20:56:21 +0000342 XXX Darn! With the advent of thread state, we should have an array
343 of pending calls per thread in the thread state! Later...
344*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000345
Guido van Rossuma9672091994-09-14 13:31:22 +0000346#define NPENDINGCALLS 32
347static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000348 int (*func)(void *);
349 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000350} pendingcalls[NPENDINGCALLS];
351static volatile int pendingfirst = 0;
352static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000353static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000354
355int
Thomas Wouters334fb892000-07-25 12:56:38 +0000356Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000357{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000358 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000359 int i, j;
360 /* XXX Begin critical section */
361 /* XXX If you want this to be safe against nested
362 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000363 if (busy)
364 return -1;
365 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000366 i = pendinglast;
367 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000368 if (j == pendingfirst) {
369 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000370 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000371 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000372 pendingcalls[i].func = func;
373 pendingcalls[i].arg = arg;
374 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000375
376 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000377 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000378 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000379 /* XXX End critical section */
380 return 0;
381}
382
Guido van Rossum180d7b41994-09-29 09:45:57 +0000383int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000385{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000386 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000387#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000388 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000389 return 0;
390#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000391 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000392 return 0;
393 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000394 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000395 for (;;) {
396 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000397 int (*func)(void *);
398 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000399 i = pendingfirst;
400 if (i == pendinglast)
401 break; /* Queue empty */
402 func = pendingcalls[i].func;
403 arg = pendingcalls[i].arg;
404 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000405 if (func(arg) < 0) {
406 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000407 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000408 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000409 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000410 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 return 0;
413}
414
415
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000416/* The interpreter's recursion limit */
417
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000418#ifndef Py_DEFAULT_RECURSION_LIMIT
419#define Py_DEFAULT_RECURSION_LIMIT 1000
420#endif
421static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
422int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000423
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000424int
425Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000426{
427 return recursion_limit;
428}
429
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000430void
431Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000432{
433 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000434 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000435}
436
Armin Rigo2b3eb402003-10-28 12:05:48 +0000437/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
438 if the recursion_depth reaches _Py_CheckRecursionLimit.
439 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
440 to guarantee that _Py_CheckRecursiveCall() is regularly called.
441 Without USE_STACKCHECK, there is no need for this. */
442int
443_Py_CheckRecursiveCall(char *where)
444{
445 PyThreadState *tstate = PyThreadState_GET();
446
447#ifdef USE_STACKCHECK
448 if (PyOS_CheckStack()) {
449 --tstate->recursion_depth;
450 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
451 return -1;
452 }
453#endif
Martin v. Löwis5b222132007-06-10 09:51:05 +0000454 if (tstate->recursion_critical)
455 /* Somebody asked that we don't check for recursion. */
456 return 0;
457 if (tstate->overflowed) {
458 if (tstate->recursion_depth > recursion_limit + 50) {
459 /* Overflowing while handling an overflow. Give up. */
460 Py_FatalError("Cannot recover from stack overflow.");
461 }
462 return 0;
463 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000464 if (tstate->recursion_depth > recursion_limit) {
465 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000466 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000467 PyErr_Format(PyExc_RuntimeError,
468 "maximum recursion depth exceeded%s",
469 where);
470 return -1;
471 }
Thomas Woutersce272b62007-09-19 21:19:28 +0000472 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000473 return 0;
474}
475
Guido van Rossum374a9221991-04-04 10:40:29 +0000476/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000477enum why_code {
478 WHY_NOT = 0x0001, /* No error */
479 WHY_EXCEPTION = 0x0002, /* Exception occurred */
480 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
481 WHY_RETURN = 0x0008, /* 'return' statement */
482 WHY_BREAK = 0x0010, /* 'break' statement */
483 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000484 WHY_YIELD = 0x0040, /* 'yield' operator */
485 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000486};
Guido van Rossum374a9221991-04-04 10:40:29 +0000487
Collin Winter828f04a2007-08-31 00:04:24 +0000488static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000489static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000490
Skip Montanarod581d772002-09-03 20:10:45 +0000491/* for manipulating the thread switch and periodic "stuff" - used to be
492 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000493int _Py_CheckInterval = 100;
494volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000495
Guido van Rossumb209a111997-04-29 18:18:01 +0000496PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000497PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000499 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000500 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000501 (PyObject **)NULL, 0,
502 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000503 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000504 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000505}
506
507
508/* Interpreter main loop */
509
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000510PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000511PyEval_EvalFrame(PyFrameObject *f) {
512 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000513 used this API; core interpreter code should call
514 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000515 return PyEval_EvalFrameEx(f, 0);
516}
517
518PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000520{
Guido van Rossum950361c1997-01-24 13:49:28 +0000521#ifdef DXPAIRS
522 int lastopcode = 0;
523#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000524 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000525 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000526 register int opcode; /* Current opcode */
527 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000528 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000529 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000530 register PyObject *x; /* Result object -- NULL if error */
531 register PyObject *v; /* Temporary objects popped off stack */
532 register PyObject *w;
533 register PyObject *u;
534 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000535 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000536 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000537 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000538 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000539
Tim Peters8a5c3c72004-04-05 19:36:21 +0000540 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000541
542 not (instr_lb <= current_bytecode_offset < instr_ub)
543
Tim Peters8a5c3c72004-04-05 19:36:21 +0000544 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000545 initial values are such as to make this false the first
546 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000547 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000548
Guido van Rossumd076c731998-10-07 19:42:25 +0000549 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000550 PyObject *names;
551 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000552#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000553 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000554 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000555#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000556
Neal Norwitza81d2202002-07-14 00:27:26 +0000557/* Tuple access macros */
558
559#ifndef Py_DEBUG
560#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
561#else
562#define GETITEM(v, i) PyTuple_GetItem((v), (i))
563#endif
564
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000565#ifdef WITH_TSC
566/* Use Pentium timestamp counter to mark certain events:
567 inst0 -- beginning of switch statement for opcode dispatch
568 inst1 -- end of switch statement (may be skipped)
569 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000570 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000571 (may be skipped)
572 intr1 -- beginning of long interruption
573 intr2 -- end of long interruption
574
575 Many opcodes call out to helper C functions. In some cases, the
576 time in those functions should be counted towards the time for the
577 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
578 calls another Python function; there's no point in charge all the
579 bytecode executed by the called function to the caller.
580
581 It's hard to make a useful judgement statically. In the presence
582 of operator overloading, it's impossible to tell if a call will
583 execute new Python code or not.
584
585 It's a case-by-case judgement. I'll use intr1 for the following
586 cases:
587
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000588 IMPORT_STAR
589 IMPORT_FROM
590 CALL_FUNCTION (and friends)
591
592 */
593 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
594 int ticked = 0;
595
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000596 READ_TIMESTAMP(inst0);
597 READ_TIMESTAMP(inst1);
598 READ_TIMESTAMP(loop0);
599 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000600
601 /* shut up the compiler */
602 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000603#endif
604
Guido van Rossum374a9221991-04-04 10:40:29 +0000605/* Code access macros */
606
Martin v. Löwis18e16552006-02-15 17:27:45 +0000607#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000608#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000609#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000610#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000611#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000612#define JUMPBY(x) (next_instr += (x))
613
Raymond Hettingerf606f872003-03-16 03:11:04 +0000614/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000615 Some opcodes tend to come in pairs thus making it possible to
616 predict the second code when the first is run. For example,
617 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
618 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000619
620 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000621 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000622 processor has a high likelihood of making its own successful branch
623 prediction which results in a nearly zero overhead transition to the
624 next opcode.
625
626 A successful prediction saves a trip through the eval-loop including
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000627 its two unpredictable branches, the HAS_ARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000628
Tim Peters8a5c3c72004-04-05 19:36:21 +0000629 If collecting opcode statistics, turn off prediction so that
630 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000631 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000632*/
633
Raymond Hettingera7216982004-02-08 19:59:27 +0000634#ifdef DYNAMIC_EXECUTION_PROFILE
635#define PREDICT(op) if (0) goto PRED_##op
636#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000637#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000638#endif
639
Raymond Hettingerf606f872003-03-16 03:11:04 +0000640#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000641#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000642
Guido van Rossum374a9221991-04-04 10:40:29 +0000643/* Stack manipulation macros */
644
Martin v. Löwis18e16552006-02-15 17:27:45 +0000645/* The stack can grow at most MAXINT deep, as co_nlocals and
646 co_stacksize are ints. */
647#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000648#define EMPTY() (STACK_LEVEL() == 0)
649#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000650#define SECOND() (stack_pointer[-2])
651#define THIRD() (stack_pointer[-3])
652#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000653#define SET_TOP(v) (stack_pointer[-1] = (v))
654#define SET_SECOND(v) (stack_pointer[-2] = (v))
655#define SET_THIRD(v) (stack_pointer[-3] = (v))
656#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000657#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000658#define BASIC_PUSH(v) (*stack_pointer++ = (v))
659#define BASIC_POP() (*--stack_pointer)
660
Guido van Rossum96a42c81992-01-12 02:29:51 +0000661#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000662#define PUSH(v) { (void)(BASIC_PUSH(v), \
663 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000665#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
666 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000667#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
668 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000670#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
671 prtrace((STACK_POINTER)[-1], "ext_pop")), \
672 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000673#else
674#define PUSH(v) BASIC_PUSH(v)
675#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000676#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000677#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000678#endif
679
Guido van Rossum681d79a1995-07-18 14:51:37 +0000680/* Local variable macros */
681
682#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000683
684/* The SETLOCAL() macro must not DECREF the local variable in-place and
685 then store the new value; it must copy the old value to a temporary
686 value, then store the new value, and then DECREF the temporary value.
687 This is because it is possible that during the DECREF the frame is
688 accessed by other code (e.g. a __del__ method or gc.collect()) and the
689 variable would be pointing to already-freed memory. */
690#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
691 GETLOCAL(i) = value; \
692 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000693
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000694
695#define UNWIND_BLOCK(b) \
696 while (STACK_LEVEL() > (b)->b_level) { \
697 PyObject *v = POP(); \
698 Py_XDECREF(v); \
699 }
700
701#define UNWIND_EXCEPT_HANDLER(b) \
702 assert(STACK_LEVEL() >= (b)->b_level + 3); \
703 while (STACK_LEVEL() > (b)->b_level + 3) { \
704 PyObject *v = POP(); \
705 Py_XDECREF(v); \
706 } \
707 Py_XDECREF(tstate->exc_type); \
708 tstate->exc_type = POP(); \
709 Py_XDECREF(tstate->exc_value); \
710 tstate->exc_value = POP(); \
711 Py_XDECREF(tstate->exc_traceback); \
712 tstate->exc_traceback = POP();
713
714#define SAVE_EXC_STATE() \
715 { \
716 Py_XINCREF(tstate->exc_type); \
717 Py_XINCREF(tstate->exc_value); \
718 Py_XINCREF(tstate->exc_traceback); \
719 Py_XDECREF(f->f_exc_type); \
720 Py_XDECREF(f->f_exc_value); \
721 Py_XDECREF(f->f_exc_traceback); \
722 f->f_exc_type = tstate->exc_type; \
723 f->f_exc_value = tstate->exc_value; \
724 f->f_exc_traceback = tstate->exc_traceback; \
725 }
726
727#define SWAP_EXC_STATE() \
728 { \
729 PyObject *tmp; \
730 tmp = tstate->exc_type; \
731 tstate->exc_type = f->f_exc_type; \
732 f->f_exc_type = tmp; \
733 tmp = tstate->exc_value; \
734 tstate->exc_value = f->f_exc_value; \
735 f->f_exc_value = tmp; \
736 tmp = tstate->exc_traceback; \
737 tstate->exc_traceback = f->f_exc_traceback; \
738 f->f_exc_traceback = tmp; \
739 }
740
Guido van Rossuma027efa1997-05-05 20:56:21 +0000741/* Start of code */
742
Tim Peters5ca576e2001-06-18 22:08:13 +0000743 if (f == NULL)
744 return NULL;
745
Armin Rigo1d313ab2003-10-25 14:33:09 +0000746 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000748 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000749
Tim Peters5ca576e2001-06-18 22:08:13 +0000750 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000751
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000752 if (tstate->use_tracing) {
753 if (tstate->c_tracefunc != NULL) {
754 /* tstate->c_tracefunc, if defined, is a
755 function that will be called on *every* entry
756 to a code block. Its return value, if not
757 None, is a function that will be called at
758 the start of each executed line of code.
759 (Actually, the function must return itself
760 in order to continue tracing.) The trace
761 functions are called with three arguments:
762 a pointer to the current frame, a string
763 indicating why the function is called, and
764 an argument which depends on the situation.
765 The global trace function is also called
766 whenever an exception is detected. */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000767 if (call_trace_protected(tstate->c_tracefunc,
768 tstate->c_traceobj,
769 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000770 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000771 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000772 }
773 }
774 if (tstate->c_profilefunc != NULL) {
775 /* Similar for c_profilefunc, except it needn't
776 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000777 if (call_trace_protected(tstate->c_profilefunc,
778 tstate->c_profileobj,
779 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000780 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000781 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000782 }
783 }
784 }
785
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000786 co = f->f_code;
787 names = co->co_names;
788 consts = co->co_consts;
789 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000790 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +0000791 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000792 /* An explanation is in order for the next line.
793
794 f->f_lasti now refers to the index of the last instruction
795 executed. You might think this was obvious from the name, but
796 this wasn't always true before 2.3! PyFrame_New now sets
797 f->f_lasti to -1 (i.e. the index *before* the first instruction)
798 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000799 does work. Promise.
800
801 When the PREDICT() macros are enabled, some opcode pairs follow in
802 direct succession without updating f->f_lasti. A successful
803 prediction effectively links the two codes together as if they
804 were a single new opcode; accordingly,f->f_lasti will point to
805 the first code in the pair (for instance, GET_ITER followed by
806 FOR_ITER is effectively a single opcode and f->f_lasti will point
807 at to the beginning of the combined pair.)
808 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000809 next_instr = first_instr + f->f_lasti + 1;
810 stack_pointer = f->f_stacktop;
811 assert(stack_pointer != NULL);
812 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
813
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000814 if (f->f_code->co_flags & CO_GENERATOR) {
815 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
816 /* We were in an except handler when we left,
817 restore the exception state which was put aside
818 (see YIELD_VALUE). */
819 SWAP_EXC_STATE();
820 }
821 else {
822 SAVE_EXC_STATE();
823 }
824 }
825
Tim Peters5ca576e2001-06-18 22:08:13 +0000826#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000828#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000829#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000830 filename = PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000831#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000832
Guido van Rossum374a9221991-04-04 10:40:29 +0000833 why = WHY_NOT;
834 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000835 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000836 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000838 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000839 why = WHY_EXCEPTION;
840 goto on_error;
841 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000844#ifdef WITH_TSC
845 if (inst1 == 0) {
846 /* Almost surely, the opcode executed a break
847 or a continue, preventing inst1 from being set
848 on the way out of the loop.
849 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000850 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000851 loop1 = inst1;
852 }
853 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
854 intr0, intr1);
855 ticked = 0;
856 inst1 = 0;
857 intr0 = 0;
858 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000859 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000860#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000861 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000863
Guido van Rossuma027efa1997-05-05 20:56:21 +0000864 /* Do periodic things. Doing this every time through
865 the loop would add too much overhead, so we do it
866 only every Nth instruction. We also do it if
867 ``things_to_do'' is set, i.e. when an asynchronous
868 event needs attention (e.g. a signal handler or
869 async I/O handler); see Py_AddPendingCall() and
870 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000871
Skip Montanarod581d772002-09-03 20:10:45 +0000872 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +0000873 if (*next_instr == SETUP_FINALLY) {
874 /* Make the last opcode before
875 a try: finally: block uninterruptable. */
876 goto fast_next_opcode;
877 }
Skip Montanarod581d772002-09-03 20:10:45 +0000878 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000879 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000880#ifdef WITH_TSC
881 ticked = 1;
882#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000883 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000884 if (Py_MakePendingCalls() < 0) {
885 why = WHY_EXCEPTION;
886 goto on_error;
887 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000888 if (things_to_do)
889 /* MakePendingCalls() didn't succeed.
890 Force early re-execution of this
891 "periodic" code, possibly after
892 a thread switch */
893 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000894 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000895#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000896 if (interpreter_lock) {
897 /* Give another thread a chance */
898
Guido van Rossum25ce5661997-08-02 03:10:38 +0000899 if (PyThreadState_Swap(NULL) != tstate)
900 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000901 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000902
903 /* Other threads may run now */
904
Guido van Rossum65d5b571998-12-21 19:32:43 +0000905 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000906 if (PyThreadState_Swap(tstate) != NULL)
907 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000908
909 /* Check for thread interrupts */
910
911 if (tstate->async_exc != NULL) {
912 x = tstate->async_exc;
913 tstate->async_exc = NULL;
914 PyErr_SetNone(x);
915 Py_DECREF(x);
916 why = WHY_EXCEPTION;
917 goto on_error;
918 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000919 }
920#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000921 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000922
Neil Schemenauer63543862002-02-17 19:10:14 +0000923 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000924 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000925
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000926 /* line-by-line tracing support */
927
928 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
929 /* see maybe_call_line_trace
930 for expository comments */
931 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000932
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000933 err = maybe_call_line_trace(tstate->c_tracefunc,
934 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000935 f, &instr_lb, &instr_ub,
936 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000937 /* Reload possibly changed frame fields */
938 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000939 if (f->f_stacktop != NULL) {
940 stack_pointer = f->f_stacktop;
941 f->f_stacktop = NULL;
942 }
943 if (err) {
944 /* trace function raised an exception */
945 goto on_error;
946 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000947 }
948
949 /* Extract opcode and argument */
950
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000952 oparg = 0; /* allows oparg to be stored in a register because
953 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000954 if (HAS_ARG(opcode))
955 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000956 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000957#ifdef DYNAMIC_EXECUTION_PROFILE
958#ifdef DXPAIRS
959 dxpairs[lastopcode][opcode]++;
960 lastopcode = opcode;
961#endif
962 dxp[opcode]++;
963#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000964
Guido van Rossum96a42c81992-01-12 02:29:51 +0000965#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000966 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000967
Guido van Rossum96a42c81992-01-12 02:29:51 +0000968 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000969 if (HAS_ARG(opcode)) {
970 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000971 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000972 }
973 else {
974 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000975 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000976 }
977 }
978#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000979
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000981 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000982
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 /* BEWARE!
986 It is essential that any operation that fails sets either
987 x to NULL, err to nonzero, or why to anything but WHY_NOT,
988 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000989
Guido van Rossum374a9221991-04-04 10:40:29 +0000990 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000991
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000992 case NOP:
993 goto fast_next_opcode;
994
Neil Schemenauer63543862002-02-17 19:10:14 +0000995 case LOAD_FAST:
996 x = GETLOCAL(oparg);
997 if (x != NULL) {
998 Py_INCREF(x);
999 PUSH(x);
1000 goto fast_next_opcode;
1001 }
1002 format_exc_check_arg(PyExc_UnboundLocalError,
1003 UNBOUNDLOCAL_ERROR_MSG,
1004 PyTuple_GetItem(co->co_varnames, oparg));
1005 break;
1006
1007 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001008 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001009 Py_INCREF(x);
1010 PUSH(x);
1011 goto fast_next_opcode;
1012
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001013 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001014 case STORE_FAST:
1015 v = POP();
1016 SETLOCAL(oparg, v);
1017 goto fast_next_opcode;
1018
Raymond Hettingerf606f872003-03-16 03:11:04 +00001019 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 case POP_TOP:
1021 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001022 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001023 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001024
Guido van Rossum374a9221991-04-04 10:40:29 +00001025 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 v = TOP();
1027 w = SECOND();
1028 SET_TOP(w);
1029 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001030 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001031
Guido van Rossum374a9221991-04-04 10:40:29 +00001032 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 v = TOP();
1034 w = SECOND();
1035 x = THIRD();
1036 SET_TOP(w);
1037 SET_SECOND(x);
1038 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001039 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Thomas Wouters434d0822000-08-24 20:11:32 +00001041 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 u = TOP();
1043 v = SECOND();
1044 w = THIRD();
1045 x = FOURTH();
1046 SET_TOP(v);
1047 SET_SECOND(w);
1048 SET_THIRD(x);
1049 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001050 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001051
Guido van Rossum374a9221991-04-04 10:40:29 +00001052 case DUP_TOP:
1053 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001054 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001055 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001056 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Thomas Wouters434d0822000-08-24 20:11:32 +00001058 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001059 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001061 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001063 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 STACKADJ(2);
1065 SET_TOP(x);
1066 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001067 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001068 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001070 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001072 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001073 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001074 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 STACKADJ(3);
1076 SET_TOP(x);
1077 SET_SECOND(w);
1078 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001079 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001080 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001081 Py_FatalError("invalid argument to DUP_TOPX"
1082 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001083 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001084
Guido van Rossum374a9221991-04-04 10:40:29 +00001085 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001086 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001087 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001088 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001090 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001091 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001092
Guido van Rossum374a9221991-04-04 10:40:29 +00001093 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001094 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001095 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001096 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001097 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001098 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001099 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001100
Guido van Rossum374a9221991-04-04 10:40:29 +00001101 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001103 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001104 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001105 if (err == 0) {
1106 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001108 continue;
1109 }
1110 else if (err > 0) {
1111 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001112 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001113 err = 0;
1114 continue;
1115 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001116 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Guido van Rossum7928cd71991-10-24 14:59:31 +00001119 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001120 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001121 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001122 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001123 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001124 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001125 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001126
Guido van Rossum50564e81996-01-12 01:13:16 +00001127 case BINARY_POWER:
1128 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001130 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 Py_DECREF(v);
1132 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001133 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001134 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001135 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Guido van Rossum374a9221991-04-04 10:40:29 +00001137 case BINARY_MULTIPLY:
1138 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001140 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001141 Py_DECREF(v);
1142 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001143 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001144 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Tim Peters3caca232001-12-06 06:23:26 +00001147 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001148 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001150 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001151 Py_DECREF(v);
1152 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001154 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Guido van Rossum4668b002001-08-08 05:00:18 +00001157 case BINARY_FLOOR_DIVIDE:
1158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001160 x = PyNumber_FloorDivide(v, w);
1161 Py_DECREF(v);
1162 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001164 if (x != NULL) continue;
1165 break;
1166
Guido van Rossum374a9221991-04-04 10:40:29 +00001167 case BINARY_MODULO:
1168 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001170 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001171 Py_DECREF(v);
1172 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001173 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001174 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001175 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001176
Guido van Rossum374a9221991-04-04 10:40:29 +00001177 case BINARY_ADD:
1178 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001180 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001181 PyUnicode_CheckExact(w)) {
1182 x = unicode_concatenate(v, w, f, next_instr);
1183 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001184 goto skip_decref_vx;
1185 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001186 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001187 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001188 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001189 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001190 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum374a9221991-04-04 10:40:29 +00001196 case BINARY_SUBTRACT:
1197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001199 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001200 Py_DECREF(v);
1201 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001203 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001204 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Guido van Rossum374a9221991-04-04 10:40:29 +00001206 case BINARY_SUBSCR:
1207 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001209 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001210 Py_DECREF(v);
1211 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001213 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum7928cd71991-10-24 14:59:31 +00001216 case BINARY_LSHIFT:
1217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001219 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001220 Py_DECREF(v);
1221 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001223 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001225
Guido van Rossum7928cd71991-10-24 14:59:31 +00001226 case BINARY_RSHIFT:
1227 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001229 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001230 Py_DECREF(v);
1231 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001233 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001234 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001235
Guido van Rossum7928cd71991-10-24 14:59:31 +00001236 case BINARY_AND:
1237 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001239 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001240 Py_DECREF(v);
1241 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001243 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001244 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001245
Guido van Rossum7928cd71991-10-24 14:59:31 +00001246 case BINARY_XOR:
1247 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001248 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001249 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001250 Py_DECREF(v);
1251 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001253 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001254 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Guido van Rossum7928cd71991-10-24 14:59:31 +00001256 case BINARY_OR:
1257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001259 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001260 Py_DECREF(v);
1261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001263 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001264 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001265
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001266 case LIST_APPEND:
1267 w = POP();
1268 v = POP();
1269 err = PyList_Append(v, w);
1270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001272 if (err == 0) {
1273 PREDICT(JUMP_ABSOLUTE);
1274 continue;
1275 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001276 break;
1277
Nick Coghlan650f0d02007-04-15 12:05:43 +00001278 case SET_ADD:
1279 w = POP();
1280 v = POP();
1281 err = PySet_Add(v, w);
1282 Py_DECREF(v);
1283 Py_DECREF(w);
1284 if (err == 0) {
1285 PREDICT(JUMP_ABSOLUTE);
1286 continue;
1287 }
1288 break;
1289
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 case INPLACE_POWER:
1291 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 x = PyNumber_InPlacePower(v, w, Py_None);
1294 Py_DECREF(v);
1295 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001296 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 if (x != NULL) continue;
1298 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001299
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 case INPLACE_MULTIPLY:
1301 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001302 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 x = PyNumber_InPlaceMultiply(v, w);
1304 Py_DECREF(v);
1305 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001306 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 if (x != NULL) continue;
1308 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Tim Peters54b11912001-12-25 18:49:11 +00001310 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001313 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 Py_DECREF(v);
1315 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001316 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 if (x != NULL) continue;
1318 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Guido van Rossum4668b002001-08-08 05:00:18 +00001320 case INPLACE_FLOOR_DIVIDE:
1321 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001323 x = PyNumber_InPlaceFloorDivide(v, w);
1324 Py_DECREF(v);
1325 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001326 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001327 if (x != NULL) continue;
1328 break;
1329
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 case INPLACE_MODULO:
1331 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 x = PyNumber_InPlaceRemainder(v, w);
1334 Py_DECREF(v);
1335 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001336 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 if (x != NULL) continue;
1338 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 case INPLACE_ADD:
1341 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001343 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001344 PyUnicode_CheckExact(w)) {
1345 x = unicode_concatenate(v, w, f, next_instr);
1346 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001347 goto skip_decref_v;
1348 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001349 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001351 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001353 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 if (x != NULL) continue;
1357 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001358
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 case INPLACE_SUBTRACT:
1360 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001362 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 Py_DECREF(v);
1364 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 if (x != NULL) continue;
1367 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 case INPLACE_LSHIFT:
1370 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 x = PyNumber_InPlaceLshift(v, w);
1373 Py_DECREF(v);
1374 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 if (x != NULL) continue;
1377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 case INPLACE_RSHIFT:
1380 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 x = PyNumber_InPlaceRshift(v, w);
1383 Py_DECREF(v);
1384 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001386 if (x != NULL) continue;
1387 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 case INPLACE_AND:
1390 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 x = PyNumber_InPlaceAnd(v, w);
1393 Py_DECREF(v);
1394 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001396 if (x != NULL) continue;
1397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Thomas Wouters434d0822000-08-24 20:11:32 +00001399 case INPLACE_XOR:
1400 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001402 x = PyNumber_InPlaceXor(v, w);
1403 Py_DECREF(v);
1404 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001406 if (x != NULL) continue;
1407 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 case INPLACE_OR:
1410 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001412 x = PyNumber_InPlaceOr(v, w);
1413 Py_DECREF(v);
1414 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001416 if (x != NULL) continue;
1417 break;
1418
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 w = TOP();
1421 v = SECOND();
1422 u = THIRD();
1423 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001425 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001426 Py_DECREF(u);
1427 Py_DECREF(v);
1428 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001429 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001433 w = TOP();
1434 v = SECOND();
1435 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001437 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001438 Py_DECREF(v);
1439 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001440 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001442
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 case PRINT_EXPR:
1444 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001445 w = PySys_GetObject("displayhook");
1446 if (w == NULL) {
1447 PyErr_SetString(PyExc_RuntimeError,
1448 "lost sys.displayhook");
1449 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001450 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001451 }
1452 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001453 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001454 if (x == NULL)
1455 err = -1;
1456 }
1457 if (err == 0) {
1458 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001459 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001460 if (w == NULL)
1461 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001463 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001464 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001466
Thomas Wouters434d0822000-08-24 20:11:32 +00001467#ifdef CASE_TOO_BIG
1468 default: switch (opcode) {
1469#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001470 case RAISE_VARARGS:
Collin Winter828f04a2007-08-31 00:04:24 +00001471 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001472 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001473 case 2:
1474 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001475 case 1:
1476 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001477 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001478 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001479 break;
1480 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001481 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001482 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001483 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001484 break;
1485 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001488 case STORE_LOCALS:
1489 x = POP();
1490 v = f->f_locals;
1491 Py_XDECREF(v);
1492 f->f_locals = x;
1493 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 case RETURN_VALUE:
1496 retval = POP();
1497 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001498 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001499
Tim Peters5ca576e2001-06-18 22:08:13 +00001500 case YIELD_VALUE:
1501 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001502 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001503 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001504 /* Put aside the current exception state and restore
1505 that of the calling frame. This only serves when
1506 "yield" is used inside an except handler. */
1507 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001508 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001509
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001510 case POP_EXCEPT:
1511 {
1512 PyTryBlock *b = PyFrame_BlockPop(f);
1513 if (b->b_type != EXCEPT_HANDLER) {
1514 PyErr_SetString(PyExc_SystemError,
1515 "popped block is not an except handler");
1516 why = WHY_EXCEPTION;
1517 break;
1518 }
1519 UNWIND_EXCEPT_HANDLER(b);
1520 }
1521 continue;
1522
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 case POP_BLOCK:
1524 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001525 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001526 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001527 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001528 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001529
Christian Heimes180510d2008-03-03 19:15:45 +00001530 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001531 case END_FINALLY:
1532 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001533 if (PyLong_Check(v)) {
1534 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001535 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001536 if (why == WHY_RETURN ||
1537 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001538 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001539 if (why == WHY_SILENCED) {
1540 /* An exception was silenced by 'with', we must
1541 manually unwind the EXCEPT_HANDLER block which was
1542 created when the exception was caught, otherwise
1543 the stack will be in an inconsistent state. */
1544 PyTryBlock *b = PyFrame_BlockPop(f);
1545 if (b->b_type != EXCEPT_HANDLER) {
1546 PyErr_SetString(PyExc_SystemError,
1547 "popped block is not an except handler");
1548 why = WHY_EXCEPTION;
1549 }
1550 else {
1551 UNWIND_EXCEPT_HANDLER(b);
1552 why = WHY_NOT;
1553 }
1554 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001556 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001558 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001559 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001560 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001561 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001563 else if (v != Py_None) {
1564 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 "'finally' pops bad exception");
1566 why = WHY_EXCEPTION;
1567 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001568 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001569 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001571 case LOAD_BUILD_CLASS:
1572 x = PyDict_GetItemString(f->f_builtins,
1573 "__build_class__");
1574 if (x == NULL) {
1575 PyErr_SetString(PyExc_ImportError,
1576 "__build_class__ not found");
1577 break;
1578 }
1579 Py_INCREF(x);
1580 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Guido van Rossum374a9221991-04-04 10:40:29 +00001583 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001584 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001585 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001586 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001587 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001588 err = PyDict_SetItem(x, w, v);
1589 else
1590 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001591 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001592 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001593 break;
1594 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001595 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001596 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001600 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001601 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001602 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001603 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001604 NAME_ERROR_MSG,
1605 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001606 break;
1607 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001608 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001609 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001611
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001612 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001613 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001614 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001615 if (PyTuple_CheckExact(v) &&
1616 PyTuple_GET_SIZE(v) == oparg) {
1617 PyObject **items = \
1618 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001619 while (oparg--) {
1620 w = items[oparg];
1621 Py_INCREF(w);
1622 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001623 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001624 Py_DECREF(v);
1625 continue;
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001626 } else if (PyList_CheckExact(v) &&
1627 PyList_GET_SIZE(v) == oparg) {
1628 PyObject **items = \
1629 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001630 while (oparg--) {
1631 w = items[oparg];
1632 Py_INCREF(w);
1633 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001634 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001635 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001636 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001637 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001638 } else {
1639 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001640 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001641 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001642 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Guido van Rossum0368b722007-05-11 16:50:42 +00001645 case UNPACK_EX:
1646 {
1647 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1648 v = POP();
1649
1650 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1651 stack_pointer + totalargs)) {
1652 stack_pointer += totalargs;
1653 } else {
1654 why = WHY_EXCEPTION;
1655 }
1656 Py_DECREF(v);
1657 break;
1658 }
1659
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001661 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 v = TOP();
1663 u = SECOND();
1664 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1666 Py_DECREF(v);
1667 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001668 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001670
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001672 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001674 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1675 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001676 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001679 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001680 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001681 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001682 err = PyDict_SetItem(f->f_globals, w, v);
1683 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001684 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001685 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001687 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001688 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001689 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001690 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001691 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001692 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001693
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001695 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001696 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001697 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001698 "no locals when loading %R", w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001699 break;
1700 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001701 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001702 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001703 Py_XINCREF(x);
1704 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001705 else {
1706 x = PyObject_GetItem(v, w);
1707 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001708 if (!PyErr_ExceptionMatches(
1709 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001710 break;
1711 PyErr_Clear();
1712 }
1713 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001715 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001717 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001719 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001720 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001721 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 break;
1723 }
1724 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001725 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001726 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001728 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00001732 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001733 /* Inline the PyDict_GetItem() calls.
1734 WARNING: this is an extreme speed hack.
1735 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00001736 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001737 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001738 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001739 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001740 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001741 e = d->ma_lookup(d, w, hash);
1742 if (e == NULL) {
1743 x = NULL;
1744 break;
1745 }
1746 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001747 if (x != NULL) {
1748 Py_INCREF(x);
1749 PUSH(x);
1750 continue;
1751 }
1752 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001753 e = d->ma_lookup(d, w, hash);
1754 if (e == NULL) {
1755 x = NULL;
1756 break;
1757 }
1758 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001759 if (x != NULL) {
1760 Py_INCREF(x);
1761 PUSH(x);
1762 continue;
1763 }
1764 goto load_global_error;
1765 }
1766 }
1767 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001770 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001772 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001773 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001774 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001775 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 break;
1777 }
1778 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001780 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001781 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001782
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001783 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001784 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001785 if (x != NULL) {
1786 SETLOCAL(oparg, NULL);
1787 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001788 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001789 format_exc_check_arg(
1790 PyExc_UnboundLocalError,
1791 UNBOUNDLOCAL_ERROR_MSG,
1792 PyTuple_GetItem(co->co_varnames, oparg)
1793 );
1794 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001795
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001796 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001797 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001798 Py_INCREF(x);
1799 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001800 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001801 break;
1802
1803 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001804 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001805 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001806 if (w != NULL) {
1807 PUSH(w);
1808 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001809 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001810 err = -1;
1811 /* Don't stomp existing exception */
1812 if (PyErr_Occurred())
1813 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001814 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1815 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001816 oparg);
1817 format_exc_check_arg(
1818 PyExc_UnboundLocalError,
1819 UNBOUNDLOCAL_ERROR_MSG,
1820 v);
1821 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001822 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
1823 PyTuple_GET_SIZE(co->co_cellvars));
1824 format_exc_check_arg(PyExc_NameError,
1825 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001826 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001827 break;
1828
1829 case STORE_DEREF:
1830 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001831 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001832 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001833 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001834 continue;
1835
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001837 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001839 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001841 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 }
1843 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001844 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001845 }
1846 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Guido van Rossum374a9221991-04-04 10:40:29 +00001848 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001851 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001853 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 }
1855 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001856 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 }
1858 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Guido van Rossum86e58e22006-08-28 15:27:34 +00001860 case BUILD_SET:
1861 x = PySet_New(NULL);
1862 if (x != NULL) {
1863 for (; --oparg >= 0;) {
1864 w = POP();
1865 if (err == 0)
1866 err = PySet_Add(x, w);
1867 Py_DECREF(w);
1868 }
1869 if (err != 0) {
1870 Py_DECREF(x);
1871 break;
1872 }
1873 PUSH(x);
1874 continue;
1875 }
1876 break;
1877
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 case BUILD_MAP:
Christian Heimes99170a52007-12-19 02:07:34 +00001879 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001881 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Christian Heimes99170a52007-12-19 02:07:34 +00001884 case STORE_MAP:
1885 w = TOP(); /* key */
1886 u = SECOND(); /* value */
1887 v = THIRD(); /* dict */
1888 STACKADJ(-2);
1889 assert (PyDict_CheckExact(v));
1890 err = PyDict_SetItem(v, w, u); /* v[w] = u */
1891 Py_DECREF(u);
1892 Py_DECREF(w);
1893 if (err == 0) continue;
1894 break;
1895
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001897 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001898 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001899 x = PyObject_GetAttr(v, w);
1900 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001901 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001902 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001903 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001904
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 case COMPARE_OP:
1906 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001907 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001908 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001909 Py_DECREF(v);
1910 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001911 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001912 if (x == NULL) break;
1913 PREDICT(JUMP_IF_FALSE);
1914 PREDICT(JUMP_IF_TRUE);
1915 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001918 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001920 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001921 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001922 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001923 break;
1924 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00001925 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001926 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001927 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001928 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001929 w = PyTuple_Pack(5,
1930 w,
1931 f->f_globals,
1932 f->f_locals == NULL ?
1933 Py_None : f->f_locals,
1934 v,
1935 u);
1936 else
1937 w = PyTuple_Pack(4,
1938 w,
1939 f->f_globals,
1940 f->f_locals == NULL ?
1941 Py_None : f->f_locals,
1942 v);
1943 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001944 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001945 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001946 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00001947 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001948 x = NULL;
1949 break;
1950 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001951 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00001952 v = x;
1953 x = PyEval_CallObject(v, w);
1954 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001955 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001957 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001958 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001959 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Thomas Wouters52152252000-08-17 22:55:00 +00001961 case IMPORT_STAR:
1962 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001963 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001964 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001965 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001966 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001967 break;
1968 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001969 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00001970 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001971 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001972 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001973 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001974 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001975 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001976
Thomas Wouters52152252000-08-17 22:55:00 +00001977 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001978 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001979 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001980 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00001981 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001982 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00001983 PUSH(x);
1984 if (x != NULL) continue;
1985 break;
1986
Guido van Rossum374a9221991-04-04 10:40:29 +00001987 case JUMP_FORWARD:
1988 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001989 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001990
Raymond Hettingerf606f872003-03-16 03:11:04 +00001991 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001993 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00001994 if (w == Py_True) {
1995 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001996 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00001997 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001998 if (w == Py_False) {
1999 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002000 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002001 }
2002 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002003 if (err > 0)
2004 err = 0;
2005 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002006 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002007 else
2008 break;
2009 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002010
Raymond Hettingerf606f872003-03-16 03:11:04 +00002011 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002013 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002014 if (w == Py_False) {
2015 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002016 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002017 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002018 if (w == Py_True) {
2019 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002020 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002021 }
2022 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002023 if (err > 0) {
2024 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002025 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002026 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002027 else if (err == 0)
2028 ;
2029 else
2030 break;
2031 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002032
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002033 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002034 case JUMP_ABSOLUTE:
2035 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002036#if FAST_LOOPS
2037 /* Enabling this path speeds-up all while and for-loops by bypassing
2038 the per-loop checks for signals. By default, this should be turned-off
2039 because it prevents detection of a control-break in tight loops like
2040 "while 1: pass". Compile with this option turned-on when you need
2041 the speed-up and do not need break checking inside tight loops (ones
2042 that contain only instructions ending with goto fast_next_opcode).
2043 */
2044 goto fast_next_opcode;
2045#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002046 continue;
Guido van Rossum58da9312007-11-10 23:39:45 +00002047#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002049 case GET_ITER:
2050 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002051 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002052 x = PyObject_GetIter(v);
2053 Py_DECREF(v);
2054 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002055 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002056 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002057 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002058 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002059 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060 break;
2061
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002062 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 case FOR_ITER:
2064 /* before: [iter]; after: [iter, iter()] *or* [] */
2065 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002066 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002067 if (x != NULL) {
2068 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002069 PREDICT(STORE_FAST);
2070 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002071 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002072 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002073 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002074 if (!PyErr_ExceptionMatches(
2075 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002076 break;
2077 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002078 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002079 /* iterator ended normally */
2080 x = v = POP();
2081 Py_DECREF(v);
2082 JUMPBY(oparg);
2083 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002085 case BREAK_LOOP:
2086 why = WHY_BREAK;
2087 goto fast_block_end;
2088
2089 case CONTINUE_LOOP:
Christian Heimes217cfd12007-12-02 14:31:20 +00002090 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 if (!retval) {
2092 x = NULL;
2093 break;
2094 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002095 why = WHY_CONTINUE;
2096 goto fast_block_end;
2097
Guido van Rossum374a9221991-04-04 10:40:29 +00002098 case SETUP_LOOP:
2099 case SETUP_EXCEPT:
2100 case SETUP_FINALLY:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002101 /* NOTE: If you add any new block-setup opcodes that
2102 are not try/except/finally handlers, you may need
2103 to update the PyGen_NeedsFinalizing() function.
2104 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002105
Guido van Rossumb209a111997-04-29 18:18:01 +00002106 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002107 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002108 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002109
Guido van Rossumc2e20742006-02-27 22:32:47 +00002110 case WITH_CLEANUP:
2111 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002112 /* At the top of the stack are 1-3 values indicating
2113 how/why we entered the finally clause:
2114 - TOP = None
2115 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2116 - TOP = WHY_*; no retval below it
2117 - (TOP, SECOND, THIRD) = exc_info()
2118 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002119 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002120 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002121 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002122 EXIT(None, None, None)
2123
2124 In all cases, we remove EXIT from the stack, leaving
2125 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002126
2127 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002128 *and* the function call returns a 'true' value, we
2129 "zap" this information, to prevent END_FINALLY from
2130 re-raising the exception. (But non-local gotos
2131 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002132 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002133
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002134 PyObject *exit_func = POP();
2135 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002136 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002137 v = w = Py_None;
2138 }
2139 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002140 u = v = w = Py_None;
2141 }
2142 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002143 v = SECOND();
2144 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002145 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002146 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002147 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2148 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002149 Py_DECREF(exit_func);
2150 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002151 break; /* Go to error exit */
2152 if (u != Py_None && PyObject_IsTrue(x)) {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002153 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002154 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002155 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002156 Py_DECREF(u);
2157 Py_DECREF(v);
2158 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002159 }
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002160 Py_DECREF(x);
Christian Heimes180510d2008-03-03 19:15:45 +00002161 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002162 break;
2163 }
2164
Guido van Rossumf10570b1995-07-07 22:53:21 +00002165 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002166 {
2167 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002168 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002169 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002170#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002171 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002172#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002173 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002174#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002175 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002176 PUSH(x);
2177 if (x != NULL)
2178 continue;
2179 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002181
Jeremy Hylton76901512000-03-28 23:49:17 +00002182 case CALL_FUNCTION_VAR:
2183 case CALL_FUNCTION_KW:
2184 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002185 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002186 int na = oparg & 0xff;
2187 int nk = (oparg>>8) & 0xff;
2188 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002189 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002190 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002191 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002192 if (flags & CALL_FLAG_VAR)
2193 n++;
2194 if (flags & CALL_FLAG_KW)
2195 n++;
2196 pfunc = stack_pointer - n - 1;
2197 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002198
Guido van Rossumac7be682001-01-17 15:42:30 +00002199 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002200 && PyMethod_GET_SELF(func) != NULL) {
2201 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002202 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002203 func = PyMethod_GET_FUNCTION(func);
2204 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002205 Py_DECREF(*pfunc);
2206 *pfunc = self;
2207 na++;
2208 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002209 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002210 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002211 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002212 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002213 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002214 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002215 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002216 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002217
Jeremy Hylton76901512000-03-28 23:49:17 +00002218 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002219 w = POP();
2220 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002221 }
2222 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002223 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002224 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002225 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Guido van Rossum0240b922007-02-26 21:23:50 +00002228 case MAKE_CLOSURE:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002229 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002230 {
2231 int posdefaults = oparg & 0xff;
2232 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002233 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002234
Guido van Rossum681d79a1995-07-18 14:51:37 +00002235 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002236 x = PyFunction_New(v, f->f_globals);
2237 Py_DECREF(v);
Guido van Rossum0240b922007-02-26 21:23:50 +00002238
2239 if (x != NULL && opcode == MAKE_CLOSURE) {
2240 v = POP();
2241 err = PyFunction_SetClosure(x, v);
2242 Py_DECREF(v);
2243 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002244
2245 if (x != NULL && num_annotations > 0) {
2246 Py_ssize_t name_ix;
2247 u = POP(); /* names of args with annotations */
2248 v = PyDict_New();
2249 if (v == NULL) {
2250 Py_DECREF(x);
2251 x = NULL;
2252 break;
2253 }
2254 name_ix = PyTuple_Size(u);
2255 assert(num_annotations == name_ix+1);
2256 while (name_ix > 0) {
2257 --name_ix;
2258 t = PyTuple_GET_ITEM(u, name_ix);
2259 w = POP();
2260 /* XXX(nnorwitz): check for errors */
2261 PyDict_SetItem(v, t, w);
2262 Py_DECREF(w);
2263 }
2264
2265 err = PyFunction_SetAnnotations(x, v);
2266 Py_DECREF(v);
2267 Py_DECREF(u);
2268 }
2269
Guido van Rossum681d79a1995-07-18 14:51:37 +00002270 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002271 if (x != NULL && posdefaults > 0) {
2272 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002274 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002275 x = NULL;
2276 break;
2277 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002278 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002279 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002280 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002281 }
2282 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002283 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002284 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002285 if (x != NULL && kwdefaults > 0) {
2286 v = PyDict_New();
2287 if (v == NULL) {
2288 Py_DECREF(x);
2289 x = NULL;
2290 break;
2291 }
2292 while (--kwdefaults >= 0) {
2293 w = POP(); /* default value */
2294 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002295 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002296 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002297 Py_DECREF(w);
2298 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002299 }
2300 err = PyFunction_SetKwDefaults(x, v);
2301 Py_DECREF(v);
2302 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002303 PUSH(x);
2304 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002305 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002306
2307 case BUILD_SLICE:
2308 if (oparg == 3)
2309 w = POP();
2310 else
2311 w = NULL;
2312 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002313 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002314 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 Py_DECREF(u);
2316 Py_DECREF(v);
2317 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002318 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002319 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002320 break;
2321
Fred Drakeef8ace32000-08-24 00:32:09 +00002322 case EXTENDED_ARG:
2323 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002324 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002325 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 default:
2328 fprintf(stderr,
2329 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002330 PyCode_Addr2Line(f->f_code, f->f_lasti),
2331 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002332 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002333 why = WHY_EXCEPTION;
2334 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002335
2336#ifdef CASE_TOO_BIG
2337 }
2338#endif
2339
Guido van Rossum374a9221991-04-04 10:40:29 +00002340 } /* switch */
2341
2342 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002343
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002344 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002345
Guido van Rossum374a9221991-04-04 10:40:29 +00002346 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002347
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002349 if (err == 0 && x != NULL) {
2350#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002351 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002352 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002353 fprintf(stderr,
2354 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002355 else {
2356#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002357 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002358 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002359#ifdef CHECKEXC
2360 }
2361#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002362 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 err = 0;
2366 }
2367
Guido van Rossum374a9221991-04-04 10:40:29 +00002368 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002370 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002371 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002372 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002373 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002374 why = WHY_EXCEPTION;
2375 }
2376 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002377#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002379 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002380 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002381 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002382 sprintf(buf, "Stack unwind with exception "
2383 "set and why=%d", why);
2384 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002385 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002386 }
2387#endif
2388
2389 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Guido van Rossum374a9221991-04-04 10:40:29 +00002391 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002392 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002393
Fred Drake8f51f542001-10-04 14:48:42 +00002394 if (tstate->c_tracefunc != NULL)
2395 call_exc_trace(tstate->c_tracefunc,
2396 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Guido van Rossum374a9221991-04-04 10:40:29 +00002399 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002400
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 if (why == WHY_RERAISE)
2402 why = WHY_EXCEPTION;
2403
2404 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002406fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002407 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002408 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002409
Tim Peters8a5c3c72004-04-05 19:36:21 +00002410 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002411 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2412 /* For a continue inside a try block,
2413 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002414 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2415 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002416 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002417 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002418 Py_DECREF(retval);
2419 break;
2420 }
2421
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002422 if (b->b_type == EXCEPT_HANDLER) {
2423 UNWIND_EXCEPT_HANDLER(b);
2424 if (why == WHY_EXCEPTION) {
2425 Py_CLEAR(tstate->exc_type);
2426 Py_CLEAR(tstate->exc_value);
2427 Py_CLEAR(tstate->exc_traceback);
2428 }
2429 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002430 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002431 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002432 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2433 why = WHY_NOT;
2434 JUMPTO(b->b_handler);
2435 break;
2436 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002437 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2438 || b->b_type == SETUP_FINALLY)) {
2439 PyObject *exc, *val, *tb;
2440 int handler = b->b_handler;
2441 /* Beware, this invalidates all b->b_* fields */
2442 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2443 PUSH(tstate->exc_traceback);
2444 PUSH(tstate->exc_value);
2445 if (tstate->exc_type != NULL) {
2446 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002447 }
2448 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002449 Py_INCREF(Py_None);
2450 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002451 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002452 PyErr_Fetch(&exc, &val, &tb);
2453 /* Make the raw exception data
2454 available to the handler,
2455 so a program can emulate the
2456 Python main loop. */
2457 PyErr_NormalizeException(
2458 &exc, &val, &tb);
2459 PyException_SetTraceback(val, tb);
2460 Py_INCREF(exc);
2461 tstate->exc_type = exc;
2462 Py_INCREF(val);
2463 tstate->exc_value = val;
2464 tstate->exc_traceback = tb;
2465 if (tb == NULL)
2466 tb = Py_None;
2467 Py_INCREF(tb);
2468 PUSH(tb);
2469 PUSH(val);
2470 PUSH(exc);
2471 why = WHY_NOT;
2472 JUMPTO(handler);
2473 break;
2474 }
2475 if (b->b_type == SETUP_FINALLY) {
2476 if (why & (WHY_RETURN | WHY_CONTINUE))
2477 PUSH(retval);
2478 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002479 why = WHY_NOT;
2480 JUMPTO(b->b_handler);
2481 break;
2482 }
2483 } /* unwind stack */
2484
2485 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002486
Guido van Rossum374a9221991-04-04 10:40:29 +00002487 if (why != WHY_NOT)
2488 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002489 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002490
Guido van Rossum374a9221991-04-04 10:40:29 +00002491 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002492
Tim Peters8a5c3c72004-04-05 19:36:21 +00002493 assert(why != WHY_YIELD);
2494 /* Pop remaining stack entries. */
2495 while (!EMPTY()) {
2496 v = POP();
2497 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002498 }
2499
Tim Peters8a5c3c72004-04-05 19:36:21 +00002500 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002501 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002502
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002503fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002504 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002505 if (tstate->c_tracefunc) {
2506 if (why == WHY_RETURN || why == WHY_YIELD) {
2507 if (call_trace(tstate->c_tracefunc,
2508 tstate->c_traceobj, f,
2509 PyTrace_RETURN, retval)) {
2510 Py_XDECREF(retval);
2511 retval = NULL;
2512 why = WHY_EXCEPTION;
2513 }
2514 }
2515 else if (why == WHY_EXCEPTION) {
2516 call_trace_protected(tstate->c_tracefunc,
2517 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002518 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002519 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002520 }
Fred Drake8f51f542001-10-04 14:48:42 +00002521 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002522 if (why == WHY_EXCEPTION)
2523 call_trace_protected(tstate->c_profilefunc,
2524 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002525 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002526 else if (call_trace(tstate->c_profilefunc,
2527 tstate->c_profileobj, f,
2528 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002529 Py_XDECREF(retval);
2530 retval = NULL;
2531 why = WHY_EXCEPTION;
2532 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002533 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002534 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002535
Tim Peters5ca576e2001-06-18 22:08:13 +00002536 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002537exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002538 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002539 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002540
Guido van Rossum96a42c81992-01-12 02:29:51 +00002541 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002542}
2543
Guido van Rossumc2e20742006-02-27 22:32:47 +00002544/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002545 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002546 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002547
Tim Peters6d6c1a32001-08-02 04:15:00 +00002548PyObject *
2549PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002550 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002551 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002552{
2553 register PyFrameObject *f;
2554 register PyObject *retval = NULL;
2555 register PyObject **fastlocals, **freevars;
2556 PyThreadState *tstate = PyThreadState_GET();
2557 PyObject *x, *u;
2558
2559 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002560 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002561 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002562 return NULL;
2563 }
2564
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002565 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002566 assert(globals != NULL);
2567 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002568 if (f == NULL)
2569 return NULL;
2570
2571 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002572 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002573
2574 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002575 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002576 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2577 int i;
2578 int n = argcount;
2579 PyObject *kwdict = NULL;
2580 if (co->co_flags & CO_VARKEYWORDS) {
2581 kwdict = PyDict_New();
2582 if (kwdict == NULL)
2583 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002584 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002585 if (co->co_flags & CO_VARARGS)
2586 i++;
2587 SETLOCAL(i, kwdict);
2588 }
2589 if (argcount > co->co_argcount) {
2590 if (!(co->co_flags & CO_VARARGS)) {
2591 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002592 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002593 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002594 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002595 defcount ? "at most" : "exactly",
2596 co->co_argcount,
2597 kwcount ? "non-keyword " : "",
2598 co->co_argcount == 1 ? "" : "s",
2599 argcount);
2600 goto fail;
2601 }
2602 n = co->co_argcount;
2603 }
2604 for (i = 0; i < n; i++) {
2605 x = args[i];
2606 Py_INCREF(x);
2607 SETLOCAL(i, x);
2608 }
2609 if (co->co_flags & CO_VARARGS) {
2610 u = PyTuple_New(argcount - n);
2611 if (u == NULL)
2612 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002613 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002614 for (i = n; i < argcount; i++) {
2615 x = args[i];
2616 Py_INCREF(x);
2617 PyTuple_SET_ITEM(u, i-n, x);
2618 }
2619 }
2620 for (i = 0; i < kwcount; i++) {
2621 PyObject *keyword = kws[2*i];
2622 PyObject *value = kws[2*i + 1];
2623 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00002624 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002625 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002626 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002627 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00002628 goto fail;
2629 }
2630 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002631 for (j = 0;
2632 j < co->co_argcount + co->co_kwonlyargcount;
2633 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002634 PyObject *nm = PyTuple_GET_ITEM(
2635 co->co_varnames, j);
2636 int cmp = PyObject_RichCompareBool(
2637 keyword, nm, Py_EQ);
2638 if (cmp > 0)
2639 break;
2640 else if (cmp < 0)
2641 goto fail;
2642 }
2643 /* Check errors from Compare */
2644 if (PyErr_Occurred())
2645 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002646 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002647 if (kwdict == NULL) {
2648 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002649 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002650 "keyword argument '%S'",
2651 co->co_name,
2652 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002653 goto fail;
2654 }
2655 PyDict_SetItem(kwdict, keyword, value);
2656 }
2657 else {
2658 if (GETLOCAL(j) != NULL) {
2659 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002660 "%U() got multiple "
Tim Peters5ca576e2001-06-18 22:08:13 +00002661 "values for keyword "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002662 "argument '%S'",
2663 co->co_name,
2664 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002665 goto fail;
2666 }
2667 Py_INCREF(value);
2668 SETLOCAL(j, value);
2669 }
2670 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002671 if (co->co_kwonlyargcount > 0) {
2672 for (i = co->co_argcount;
2673 i < co->co_argcount + co->co_kwonlyargcount;
2674 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002675 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002676 if (GETLOCAL(i) != NULL)
2677 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002678 name = PyTuple_GET_ITEM(co->co_varnames, i);
2679 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002680 if (kwdefs != NULL)
2681 def = PyDict_GetItem(kwdefs, name);
2682 if (def != NULL) {
2683 Py_INCREF(def);
2684 SETLOCAL(i, def);
2685 continue;
2686 }
2687 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002688 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002689 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002690 goto fail;
2691 }
2692 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002693 if (argcount < co->co_argcount) {
2694 int m = co->co_argcount - defcount;
2695 for (i = argcount; i < m; i++) {
2696 if (GETLOCAL(i) == NULL) {
2697 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002698 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002699 "%spositional argument%s "
2700 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002701 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002702 ((co->co_flags & CO_VARARGS) ||
2703 defcount) ? "at least"
2704 : "exactly",
2705 m, kwcount ? "non-keyword " : "",
2706 m == 1 ? "" : "s", i);
2707 goto fail;
2708 }
2709 }
2710 if (n > m)
2711 i = n - m;
2712 else
2713 i = 0;
2714 for (; i < defcount; i++) {
2715 if (GETLOCAL(m+i) == NULL) {
2716 PyObject *def = defs[i];
2717 Py_INCREF(def);
2718 SETLOCAL(m+i, def);
2719 }
2720 }
2721 }
2722 }
2723 else {
2724 if (argcount > 0 || kwcount > 0) {
2725 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002726 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002727 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002728 argcount + kwcount);
2729 goto fail;
2730 }
2731 }
2732 /* Allocate and initialize storage for cell vars, and copy free
2733 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002734 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002735 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002736 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00002737 PyObject *c;
2738
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00002739 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002740 if (co->co_flags & CO_VARARGS)
2741 nargs++;
2742 if (co->co_flags & CO_VARKEYWORDS)
2743 nargs++;
2744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 /* Initialize each cell var, taking into account
2746 cell vars that are initialized from arguments.
2747
2748 Should arrange for the compiler to put cellvars
2749 that are arguments at the beginning of the cellvars
2750 list so that we can march over it more efficiently?
2751 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002752 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002753 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00002754 PyTuple_GET_ITEM(co->co_cellvars, i));
2755 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002757 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00002758 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00002759 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002760 c = PyCell_New(GETLOCAL(j));
2761 if (c == NULL)
2762 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002763 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002764 found = 1;
2765 break;
2766 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002767 }
2768 if (found == 0) {
2769 c = PyCell_New(NULL);
2770 if (c == NULL)
2771 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002772 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002773 }
2774 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002775 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002776 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002777 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002778 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002779 PyObject *o = PyTuple_GET_ITEM(closure, i);
2780 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002781 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002782 }
2783 }
2784
Tim Peters5ca576e2001-06-18 22:08:13 +00002785 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002786 /* Don't need to keep the reference to f_back, it will be set
2787 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002788 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002789 f->f_back = NULL;
2790
Jeremy Hylton985eba52003-02-05 23:13:00 +00002791 PCALL(PCALL_GENERATOR);
2792
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002793 /* Create a new generator that owns the ready to run frame
2794 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002795 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002796 }
2797
Thomas Woutersce272b62007-09-19 21:19:28 +00002798 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002799
Thomas Woutersce272b62007-09-19 21:19:28 +00002800fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00002801
Tim Petersb13680b2001-11-27 23:29:29 +00002802 /* decref'ing the frame can cause __del__ methods to get invoked,
2803 which can call back into Python. While we're done with the
2804 current Python frame (f), the associated C stack is still in use,
2805 so recursion_depth must be boosted for the duration.
2806 */
2807 assert(tstate != NULL);
2808 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00002809 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002810 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002811 return retval;
2812}
2813
2814
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002815/* Logic for the raise statement (too complicated for inlining).
2816 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002817static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00002818do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002819{
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002820 PyObject *type = NULL, *value = NULL, *tb = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00002821
2822 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00002823 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002824 PyThreadState *tstate = PyThreadState_GET();
Collin Winter828f04a2007-08-31 00:04:24 +00002825 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00002826 value = tstate->exc_value;
2827 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002828 if (type == Py_None) {
2829 PyErr_SetString(PyExc_RuntimeError,
2830 "No active exception to reraise");
2831 return WHY_EXCEPTION;
2832 }
2833 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00002834 Py_XINCREF(value);
2835 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002836 PyErr_Restore(type, value, tb);
2837 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00002838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002840 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00002841 raise
2842 raise <instance>
2843 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002844
Collin Winter828f04a2007-08-31 00:04:24 +00002845 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002846 type = exc;
2847 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00002848 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002849 goto raise_error;
2850 }
Collin Winter828f04a2007-08-31 00:04:24 +00002851 else if (PyExceptionInstance_Check(exc)) {
2852 value = exc;
2853 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00002854 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002855 }
2856 else {
2857 /* Not something you can raise. You get an exception
2858 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002859 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00002860 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002861 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002862 goto raise_error;
2863 }
Collin Winter828f04a2007-08-31 00:04:24 +00002864
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00002865 tb = PyException_GetTraceback(value);
2866 if (cause) {
2867 PyObject *fixed_cause;
2868 if (PyExceptionClass_Check(cause)) {
2869 fixed_cause = PyObject_CallObject(cause, NULL);
2870 if (fixed_cause == NULL)
2871 goto raise_error;
2872 Py_DECREF(cause);
2873 }
2874 else if (PyExceptionInstance_Check(cause)) {
2875 fixed_cause = cause;
2876 }
2877 else {
2878 PyErr_SetString(PyExc_TypeError,
2879 "exception causes must derive from "
2880 "BaseException");
2881 goto raise_error;
2882 }
2883 PyException_SetCause(value, fixed_cause);
2884 }
Collin Winter828f04a2007-08-31 00:04:24 +00002885
Guido van Rossumb209a111997-04-29 18:18:01 +00002886 PyErr_Restore(type, value, tb);
Collin Winter828f04a2007-08-31 00:04:24 +00002887 return WHY_EXCEPTION;
2888
2889raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002890 Py_XDECREF(value);
2891 Py_XDECREF(type);
2892 Py_XDECREF(tb);
Collin Winter1966f1c2007-09-01 20:26:44 +00002893 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002894 return WHY_EXCEPTION;
2895}
2896
Tim Petersd6d010b2001-06-21 02:49:55 +00002897/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00002898 sp). Return 1 for success, 0 if error.
2899
2900 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
2901 with a variable target.
2902*/
Tim Petersd6d010b2001-06-21 02:49:55 +00002903
Barry Warsawe42b18f1997-08-25 22:13:04 +00002904static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002905unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002906{
Guido van Rossum0368b722007-05-11 16:50:42 +00002907 int i = 0, j = 0;
2908 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00002909 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002910 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00002911 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Tim Petersd6d010b2001-06-21 02:49:55 +00002913 assert(v != NULL);
2914
2915 it = PyObject_GetIter(v);
2916 if (it == NULL)
2917 goto Error;
2918
2919 for (; i < argcnt; i++) {
2920 w = PyIter_Next(it);
2921 if (w == NULL) {
2922 /* Iterator done, via error or exhaustion. */
2923 if (!PyErr_Occurred()) {
2924 PyErr_Format(PyExc_ValueError,
2925 "need more than %d value%s to unpack",
2926 i, i == 1 ? "" : "s");
2927 }
2928 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002929 }
2930 *--sp = w;
2931 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002932
Guido van Rossum0368b722007-05-11 16:50:42 +00002933 if (argcntafter == -1) {
2934 /* We better have exhausted the iterator now. */
2935 w = PyIter_Next(it);
2936 if (w == NULL) {
2937 if (PyErr_Occurred())
2938 goto Error;
2939 Py_DECREF(it);
2940 return 1;
2941 }
2942 Py_DECREF(w);
2943 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
2944 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002945 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002946
2947 l = PySequence_List(it);
2948 if (l == NULL)
2949 goto Error;
2950 *--sp = l;
2951 i++;
2952
2953 ll = PyList_GET_SIZE(l);
2954 if (ll < argcntafter) {
2955 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
2956 argcnt + ll);
2957 goto Error;
2958 }
2959
2960 /* Pop the "after-variable" args off the list. */
2961 for (j = argcntafter; j > 0; j--, i++) {
2962 *--sp = PyList_GET_ITEM(l, ll - j);
2963 }
2964 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00002965 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00002966 Py_DECREF(it);
2967 return 1;
2968
Tim Petersd6d010b2001-06-21 02:49:55 +00002969Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002970 for (; i > 0; i--, sp++)
2971 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002972 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002973 return 0;
2974}
2975
2976
Guido van Rossum96a42c81992-01-12 02:29:51 +00002977#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002978static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002979prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002980{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002981 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002982 if (PyObject_Print(v, stdout, 0) != 0)
2983 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002984 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002985 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002986}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002987#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002988
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002989static void
Fred Drake5755ce62001-06-27 19:19:46 +00002990call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002991{
Guido van Rossumb209a111997-04-29 18:18:01 +00002992 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002993 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002994 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002995 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002996 value = Py_None;
2997 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002998 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002999 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003000 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003001 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003002 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003003 }
Fred Drake5755ce62001-06-27 19:19:46 +00003004 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003005 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003006 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003007 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003008 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003009 Py_XDECREF(type);
3010 Py_XDECREF(value);
3011 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003012 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003013}
3014
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003015static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003016call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003017 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003018{
3019 PyObject *type, *value, *traceback;
3020 int err;
3021 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003022 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003023 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003024 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003025 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003026 return 0;
3027 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003028 else {
3029 Py_XDECREF(type);
3030 Py_XDECREF(value);
3031 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003032 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003033 }
3034}
3035
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003036static int
Fred Drake5755ce62001-06-27 19:19:46 +00003037call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3038 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003039{
Fred Drake5755ce62001-06-27 19:19:46 +00003040 register PyThreadState *tstate = frame->f_tstate;
3041 int result;
3042 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003043 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003044 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003045 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003046 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003047 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3048 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003049 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003050 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003051}
3052
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003053PyObject *
3054_PyEval_CallTracing(PyObject *func, PyObject *args)
3055{
3056 PyFrameObject *frame = PyEval_GetFrame();
3057 PyThreadState *tstate = frame->f_tstate;
3058 int save_tracing = tstate->tracing;
3059 int save_use_tracing = tstate->use_tracing;
3060 PyObject *result;
3061
3062 tstate->tracing = 0;
3063 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3064 || (tstate->c_profilefunc != NULL));
3065 result = PyObject_Call(func, args, NULL);
3066 tstate->tracing = save_tracing;
3067 tstate->use_tracing = save_use_tracing;
3068 return result;
3069}
3070
Michael W. Hudson006c7522002-11-08 13:08:46 +00003071static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003072maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003073 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3074 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003075{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003076 int result = 0;
3077
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003078 /* If the last instruction executed isn't in the current
3079 instruction window, reset the window. If the last
3080 instruction happens to fall at the start of a line or if it
3081 represents a jump backwards, call the trace function.
3082 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003083 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003084 int line;
3085 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003086
Thomas Woutersce272b62007-09-19 21:19:28 +00003087 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3088 &bounds);
3089 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003090 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003091 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003092 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003093 }
3094 *instr_lb = bounds.ap_lower;
3095 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003096 }
Armin Rigobf57a142004-03-22 19:24:58 +00003097 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003098 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003099 }
3100 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003101 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003102}
3103
Fred Drake5755ce62001-06-27 19:19:46 +00003104void
3105PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003106{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003107 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003108 PyObject *temp = tstate->c_profileobj;
3109 Py_XINCREF(arg);
3110 tstate->c_profilefunc = NULL;
3111 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003112 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003113 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003114 Py_XDECREF(temp);
3115 tstate->c_profilefunc = func;
3116 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003117 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003118 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003119}
3120
3121void
3122PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3123{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003124 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003125 PyObject *temp = tstate->c_traceobj;
3126 Py_XINCREF(arg);
3127 tstate->c_tracefunc = NULL;
3128 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003129 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003130 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003131 Py_XDECREF(temp);
3132 tstate->c_tracefunc = func;
3133 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003134 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003135 tstate->use_tracing = ((func != NULL)
3136 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003137}
3138
Guido van Rossumb209a111997-04-29 18:18:01 +00003139PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003140PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003141{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003142 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003143 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003144 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003145 else
3146 return current_frame->f_builtins;
3147}
3148
Guido van Rossumb209a111997-04-29 18:18:01 +00003149PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003150PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003151{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003152 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003153 if (current_frame == NULL)
3154 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003155 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003156 return current_frame->f_locals;
3157}
3158
Guido van Rossumb209a111997-04-29 18:18:01 +00003159PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003160PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003161{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003162 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003163 if (current_frame == NULL)
3164 return NULL;
3165 else
3166 return current_frame->f_globals;
3167}
3168
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003169PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003170PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003171{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003172 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003173 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003174}
3175
Guido van Rossum6135a871995-01-09 17:53:26 +00003176int
Tim Peters5ba58662001-07-16 02:29:45 +00003177PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003178{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003179 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003180 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003181
3182 if (current_frame != NULL) {
3183 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003184 const int compilerflags = codeflags & PyCF_MASK;
3185 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003186 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003187 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003188 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003189#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003190 if (codeflags & CO_GENERATOR_ALLOWED) {
3191 result = 1;
3192 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3193 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003194#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003195 }
3196 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003197}
3198
Guido van Rossum3f5da241990-12-20 15:06:42 +00003199
Guido van Rossum681d79a1995-07-18 14:51:37 +00003200/* External interface to call any callable object.
3201 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003202
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003203#undef PyEval_CallObject
3204/* for backward compatibility: export this interface */
3205
Guido van Rossumb209a111997-04-29 18:18:01 +00003206PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003207PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003208{
Guido van Rossumb209a111997-04-29 18:18:01 +00003209 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003210}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003211#define PyEval_CallObject(func,arg) \
3212 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003213
Guido van Rossumb209a111997-04-29 18:18:01 +00003214PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003215PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003216{
Jeremy Hylton52820442001-01-03 23:52:36 +00003217 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003218
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003219 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003220 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003221 if (arg == NULL)
3222 return NULL;
3223 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003224 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003225 PyErr_SetString(PyExc_TypeError,
3226 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003227 return NULL;
3228 }
3229 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003230 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003231
Guido van Rossumb209a111997-04-29 18:18:01 +00003232 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003233 PyErr_SetString(PyExc_TypeError,
3234 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003235 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003236 return NULL;
3237 }
3238
Tim Peters6d6c1a32001-08-02 04:15:00 +00003239 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003240 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003241 return result;
3242}
3243
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003244const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003245PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003246{
3247 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003248 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003249 else if (PyFunction_Check(func))
Martin v. Löwis5b222132007-06-10 09:51:05 +00003250 return PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003251 else if (PyCFunction_Check(func))
3252 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003253 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003254 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003255}
3256
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003257const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003258PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003259{
3260 if (PyMethod_Check(func))
3261 return "()";
3262 else if (PyFunction_Check(func))
3263 return "()";
3264 else if (PyCFunction_Check(func))
3265 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003266 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003267 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003268}
3269
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003270static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003271err_args(PyObject *func, int flags, int nargs)
3272{
3273 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003274 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003275 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003276 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003277 nargs);
3278 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003279 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003280 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003281 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003282 nargs);
3283}
3284
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003285#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003286if (tstate->use_tracing && tstate->c_profilefunc) { \
3287 if (call_trace(tstate->c_profilefunc, \
3288 tstate->c_profileobj, \
3289 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003290 func)) { \
3291 x = NULL; \
3292 } \
3293 else { \
3294 x = call; \
3295 if (tstate->c_profilefunc != NULL) { \
3296 if (x == NULL) { \
3297 call_trace_protected(tstate->c_profilefunc, \
3298 tstate->c_profileobj, \
3299 tstate->frame, PyTrace_C_EXCEPTION, \
3300 func); \
3301 /* XXX should pass (type, value, tb) */ \
3302 } else { \
3303 if (call_trace(tstate->c_profilefunc, \
3304 tstate->c_profileobj, \
3305 tstate->frame, PyTrace_C_RETURN, \
3306 func)) { \
3307 Py_DECREF(x); \
3308 x = NULL; \
3309 } \
3310 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003311 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003312 } \
3313} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003314 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003315 }
3316
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003317static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003318call_function(PyObject ***pp_stack, int oparg
3319#ifdef WITH_TSC
3320 , uint64* pintr0, uint64* pintr1
3321#endif
3322 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003323{
3324 int na = oparg & 0xff;
3325 int nk = (oparg>>8) & 0xff;
3326 int n = na + 2 * nk;
3327 PyObject **pfunc = (*pp_stack) - n - 1;
3328 PyObject *func = *pfunc;
3329 PyObject *x, *w;
3330
Jeremy Hylton985eba52003-02-05 23:13:00 +00003331 /* Always dispatch PyCFunction first, because these are
3332 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003333 */
3334 if (PyCFunction_Check(func) && nk == 0) {
3335 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003336 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003337
3338 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003339 if (flags & (METH_NOARGS | METH_O)) {
3340 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3341 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003342 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003343 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003344 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003345 else if (flags & METH_O && na == 1) {
3346 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003347 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003348 Py_DECREF(arg);
3349 }
3350 else {
3351 err_args(func, flags, na);
3352 x = NULL;
3353 }
3354 }
3355 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003356 PyObject *callargs;
3357 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003358 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003359 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003360 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003361 Py_XDECREF(callargs);
3362 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003363 } else {
3364 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3365 /* optimize access to bound methods */
3366 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003367 PCALL(PCALL_METHOD);
3368 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003369 Py_INCREF(self);
3370 func = PyMethod_GET_FUNCTION(func);
3371 Py_INCREF(func);
3372 Py_DECREF(*pfunc);
3373 *pfunc = self;
3374 na++;
3375 n++;
3376 } else
3377 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003378 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003379 if (PyFunction_Check(func))
3380 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003381 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003382 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003383 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003384 Py_DECREF(func);
3385 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003386
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003387 /* Clear the stack of the function object. Also removes
3388 the arguments in case they weren't consumed already
3389 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003390 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003391 while ((*pp_stack) > pfunc) {
3392 w = EXT_POP(*pp_stack);
3393 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003394 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003395 }
3396 return x;
3397}
3398
Jeremy Hylton192690e2002-08-16 18:36:11 +00003399/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003400 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003401 For the simplest case -- a function that takes only positional
3402 arguments and is called with only positional arguments -- it
3403 inlines the most primitive frame setup code from
3404 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3405 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003406*/
3407
3408static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003409fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003410{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003411 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003412 PyObject *globals = PyFunction_GET_GLOBALS(func);
3413 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003414 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003415 PyObject **d = NULL;
3416 int nd = 0;
3417
Jeremy Hylton985eba52003-02-05 23:13:00 +00003418 PCALL(PCALL_FUNCTION);
3419 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003420 if (argdefs == NULL && co->co_argcount == n &&
3421 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003422 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3423 PyFrameObject *f;
3424 PyObject *retval = NULL;
3425 PyThreadState *tstate = PyThreadState_GET();
3426 PyObject **fastlocals, **stack;
3427 int i;
3428
3429 PCALL(PCALL_FASTER_FUNCTION);
3430 assert(globals != NULL);
3431 /* XXX Perhaps we should create a specialized
3432 PyFrame_New() that doesn't take locals, but does
3433 take builtins without sanity checking them.
3434 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003435 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003436 f = PyFrame_New(tstate, co, globals, NULL);
3437 if (f == NULL)
3438 return NULL;
3439
3440 fastlocals = f->f_localsplus;
3441 stack = (*pp_stack) - n;
3442
3443 for (i = 0; i < n; i++) {
3444 Py_INCREF(*stack);
3445 fastlocals[i] = *stack++;
3446 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003447 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003448 ++tstate->recursion_depth;
3449 Py_DECREF(f);
3450 --tstate->recursion_depth;
3451 return retval;
3452 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003453 if (argdefs != NULL) {
3454 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003455 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003456 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003457 return PyEval_EvalCodeEx(co, globals,
3458 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003459 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003460 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003461}
3462
3463static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003464update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3465 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003466{
3467 PyObject *kwdict = NULL;
3468 if (orig_kwdict == NULL)
3469 kwdict = PyDict_New();
3470 else {
3471 kwdict = PyDict_Copy(orig_kwdict);
3472 Py_DECREF(orig_kwdict);
3473 }
3474 if (kwdict == NULL)
3475 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003476 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003477 int err;
3478 PyObject *value = EXT_POP(*pp_stack);
3479 PyObject *key = EXT_POP(*pp_stack);
3480 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003481 PyErr_Format(PyExc_TypeError,
3482 "%.200s%s got multiple values "
3483 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003484 PyEval_GetFuncName(func),
3485 PyEval_GetFuncDesc(func),
Neal Norwitzda059e32007-08-26 05:33:45 +00003486 PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003487 Py_DECREF(key);
3488 Py_DECREF(value);
3489 Py_DECREF(kwdict);
3490 return NULL;
3491 }
3492 err = PyDict_SetItem(kwdict, key, value);
3493 Py_DECREF(key);
3494 Py_DECREF(value);
3495 if (err) {
3496 Py_DECREF(kwdict);
3497 return NULL;
3498 }
3499 }
3500 return kwdict;
3501}
3502
3503static PyObject *
3504update_star_args(int nstack, int nstar, PyObject *stararg,
3505 PyObject ***pp_stack)
3506{
3507 PyObject *callargs, *w;
3508
3509 callargs = PyTuple_New(nstack + nstar);
3510 if (callargs == NULL) {
3511 return NULL;
3512 }
3513 if (nstar) {
3514 int i;
3515 for (i = 0; i < nstar; i++) {
3516 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3517 Py_INCREF(a);
3518 PyTuple_SET_ITEM(callargs, nstack + i, a);
3519 }
3520 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003521 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003522 w = EXT_POP(*pp_stack);
3523 PyTuple_SET_ITEM(callargs, nstack, w);
3524 }
3525 return callargs;
3526}
3527
3528static PyObject *
3529load_args(PyObject ***pp_stack, int na)
3530{
3531 PyObject *args = PyTuple_New(na);
3532 PyObject *w;
3533
3534 if (args == NULL)
3535 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003536 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003537 w = EXT_POP(*pp_stack);
3538 PyTuple_SET_ITEM(args, na, w);
3539 }
3540 return args;
3541}
3542
3543static PyObject *
3544do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3545{
3546 PyObject *callargs = NULL;
3547 PyObject *kwdict = NULL;
3548 PyObject *result = NULL;
3549
3550 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003551 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003552 if (kwdict == NULL)
3553 goto call_fail;
3554 }
3555 callargs = load_args(pp_stack, na);
3556 if (callargs == NULL)
3557 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003558#ifdef CALL_PROFILE
3559 /* At this point, we have to look at the type of func to
3560 update the call stats properly. Do it here so as to avoid
3561 exposing the call stats machinery outside ceval.c
3562 */
3563 if (PyFunction_Check(func))
3564 PCALL(PCALL_FUNCTION);
3565 else if (PyMethod_Check(func))
3566 PCALL(PCALL_METHOD);
3567 else if (PyType_Check(func))
3568 PCALL(PCALL_TYPE);
3569 else
3570 PCALL(PCALL_OTHER);
3571#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003573call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003574 Py_XDECREF(callargs);
3575 Py_XDECREF(kwdict);
3576 return result;
3577}
3578
3579static PyObject *
3580ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3581{
3582 int nstar = 0;
3583 PyObject *callargs = NULL;
3584 PyObject *stararg = NULL;
3585 PyObject *kwdict = NULL;
3586 PyObject *result = NULL;
3587
3588 if (flags & CALL_FLAG_KW) {
3589 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003590 if (!PyDict_Check(kwdict)) {
3591 PyObject *d;
3592 d = PyDict_New();
3593 if (d == NULL)
3594 goto ext_call_fail;
3595 if (PyDict_Update(d, kwdict) != 0) {
3596 Py_DECREF(d);
3597 /* PyDict_Update raises attribute
3598 * error (percolated from an attempt
3599 * to get 'keys' attribute) instead of
3600 * a type error if its second argument
3601 * is not a mapping.
3602 */
3603 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3604 PyErr_Format(PyExc_TypeError,
3605 "%.200s%.200s argument after ** "
3606 "must be a mapping, not %.200s",
3607 PyEval_GetFuncName(func),
3608 PyEval_GetFuncDesc(func),
3609 kwdict->ob_type->tp_name);
3610 }
3611 goto ext_call_fail;
3612 }
3613 Py_DECREF(kwdict);
3614 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003615 }
3616 }
3617 if (flags & CALL_FLAG_VAR) {
3618 stararg = EXT_POP(*pp_stack);
3619 if (!PyTuple_Check(stararg)) {
3620 PyObject *t = NULL;
3621 t = PySequence_Tuple(stararg);
3622 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003623 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3624 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003625 "%.200s%.200s argument after * "
3626 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003627 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003628 PyEval_GetFuncDesc(func),
3629 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003630 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003631 goto ext_call_fail;
3632 }
3633 Py_DECREF(stararg);
3634 stararg = t;
3635 }
3636 nstar = PyTuple_GET_SIZE(stararg);
3637 }
3638 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003639 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003640 if (kwdict == NULL)
3641 goto ext_call_fail;
3642 }
3643 callargs = update_star_args(na, nstar, stararg, pp_stack);
3644 if (callargs == NULL)
3645 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003646#ifdef CALL_PROFILE
3647 /* At this point, we have to look at the type of func to
3648 update the call stats properly. Do it here so as to avoid
3649 exposing the call stats machinery outside ceval.c
3650 */
3651 if (PyFunction_Check(func))
3652 PCALL(PCALL_FUNCTION);
3653 else if (PyMethod_Check(func))
3654 PCALL(PCALL_METHOD);
3655 else if (PyType_Check(func))
3656 PCALL(PCALL_TYPE);
3657 else
3658 PCALL(PCALL_OTHER);
3659#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003660 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00003661ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003662 Py_XDECREF(callargs);
3663 Py_XDECREF(kwdict);
3664 Py_XDECREF(stararg);
3665 return result;
3666}
3667
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003668/* Extract a slice index from a PyInt or PyLong or an object with the
3669 nb_index slot defined, and store in *pi.
3670 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3671 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 +00003672 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003673*/
Tim Petersb5196382001-12-16 19:44:20 +00003674/* Note: If v is NULL, return success without storing into *pi. This
3675 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3676 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003677*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003678int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003679_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003680{
Tim Petersb5196382001-12-16 19:44:20 +00003681 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003682 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00003683 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003684 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003685 if (x == -1 && PyErr_Occurred())
3686 return 0;
3687 }
3688 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003689 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003690 "slice indices must be integers or "
3691 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003692 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003693 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003694 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003695 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003696 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003697}
3698
Guido van Rossum486364b2007-06-30 05:01:58 +00003699#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00003700 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00003701
Guido van Rossumb209a111997-04-29 18:18:01 +00003702static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003703cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003704{
Guido van Rossumac7be682001-01-17 15:42:30 +00003705 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003706 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003707 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003708 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003709 break;
3710 case PyCmp_IS_NOT:
3711 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003712 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003713 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003714 res = PySequence_Contains(w, v);
3715 if (res < 0)
3716 return NULL;
3717 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003718 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003719 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003720 if (res < 0)
3721 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003722 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003723 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003724 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003725 if (PyTuple_Check(w)) {
3726 Py_ssize_t i, length;
3727 length = PyTuple_Size(w);
3728 for (i = 0; i < length; i += 1) {
3729 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00003730 if (!PyExceptionClass_Check(exc)) {
3731 PyErr_SetString(PyExc_TypeError,
3732 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003733 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003734 }
3735 }
3736 }
3737 else {
Brett Cannon39590462007-02-26 22:01:14 +00003738 if (!PyExceptionClass_Check(w)) {
3739 PyErr_SetString(PyExc_TypeError,
3740 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003741 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003742 }
3743 }
Barry Warsaw4249f541997-08-22 21:26:19 +00003744 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003745 break;
3746 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003747 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003748 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003749 v = res ? Py_True : Py_False;
3750 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003751 return v;
3752}
3753
Thomas Wouters52152252000-08-17 22:55:00 +00003754static PyObject *
3755import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003756{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003757 PyObject *x;
3758
3759 x = PyObject_GetAttr(v, name);
3760 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00003761 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003762 }
Thomas Wouters52152252000-08-17 22:55:00 +00003763 return x;
3764}
Guido van Rossumac7be682001-01-17 15:42:30 +00003765
Thomas Wouters52152252000-08-17 22:55:00 +00003766static int
3767import_all_from(PyObject *locals, PyObject *v)
3768{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003769 PyObject *all = PyObject_GetAttrString(v, "__all__");
3770 PyObject *dict, *name, *value;
3771 int skip_leading_underscores = 0;
3772 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003773
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003774 if (all == NULL) {
3775 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3776 return -1; /* Unexpected error */
3777 PyErr_Clear();
3778 dict = PyObject_GetAttrString(v, "__dict__");
3779 if (dict == NULL) {
3780 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3781 return -1;
3782 PyErr_SetString(PyExc_ImportError,
3783 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003784 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003785 }
3786 all = PyMapping_Keys(dict);
3787 Py_DECREF(dict);
3788 if (all == NULL)
3789 return -1;
3790 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003791 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003792
3793 for (pos = 0, err = 0; ; pos++) {
3794 name = PySequence_GetItem(all, pos);
3795 if (name == NULL) {
3796 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3797 err = -1;
3798 else
3799 PyErr_Clear();
3800 break;
3801 }
3802 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00003803 PyUnicode_Check(name) &&
3804 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003805 {
3806 Py_DECREF(name);
3807 continue;
3808 }
3809 value = PyObject_GetAttr(v, name);
3810 if (value == NULL)
3811 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003812 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003813 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003814 else
3815 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003816 Py_DECREF(name);
3817 Py_XDECREF(value);
3818 if (err != 0)
3819 break;
3820 }
3821 Py_DECREF(all);
3822 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003823}
3824
Guido van Rossumac7be682001-01-17 15:42:30 +00003825static void
Neal Norwitzda059e32007-08-26 05:33:45 +00003826format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00003827{
Neal Norwitzda059e32007-08-26 05:33:45 +00003828 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00003829
3830 if (!obj)
3831 return;
3832
Neal Norwitzda059e32007-08-26 05:33:45 +00003833 obj_str = PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00003834 if (!obj_str)
3835 return;
3836
3837 PyErr_Format(exc, format_str, obj_str);
3838}
Guido van Rossum950361c1997-01-24 13:49:28 +00003839
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003840static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00003841unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003842 PyFrameObject *f, unsigned char *next_instr)
3843{
3844 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00003845 are (Unicode) strings. */
3846 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
3847 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003848 Py_ssize_t new_len = v_len + w_len;
3849 if (new_len < 0) {
3850 PyErr_SetString(PyExc_OverflowError,
3851 "strings are too large to concat");
3852 return NULL;
3853 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003854
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003855 if (v->ob_refcnt == 2) {
3856 /* In the common case, there are 2 references to the value
3857 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00003858 * value stack (in 'v') and one still stored in the
3859 * 'variable'. We try to delete the variable now to reduce
3860 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003861 */
3862 switch (*next_instr) {
3863 case STORE_FAST:
3864 {
3865 int oparg = PEEKARG();
3866 PyObject **fastlocals = f->f_localsplus;
3867 if (GETLOCAL(oparg) == v)
3868 SETLOCAL(oparg, NULL);
3869 break;
3870 }
3871 case STORE_DEREF:
3872 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00003873 PyObject **freevars = (f->f_localsplus +
3874 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003875 PyObject *c = freevars[PEEKARG()];
3876 if (PyCell_GET(c) == v)
3877 PyCell_Set(c, NULL);
3878 break;
3879 }
3880 case STORE_NAME:
3881 {
3882 PyObject *names = f->f_code->co_names;
3883 PyObject *name = GETITEM(names, PEEKARG());
3884 PyObject *locals = f->f_locals;
3885 if (PyDict_CheckExact(locals) &&
3886 PyDict_GetItem(locals, name) == v) {
3887 if (PyDict_DelItem(locals, name) != 0) {
3888 PyErr_Clear();
3889 }
3890 }
3891 break;
3892 }
3893 }
3894 }
3895
Guido van Rossum98297ee2007-11-06 21:34:58 +00003896 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003897 /* Now we own the last reference to 'v', so we can resize it
3898 * in-place.
3899 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003900 if (PyUnicode_Resize(&v, new_len) != 0) {
3901 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00003902 * deallocated so it cannot be put back into
3903 * 'variable'. The MemoryError is raised when there
3904 * is no value in 'variable', which might (very
3905 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003906 */
3907 return NULL;
3908 }
3909 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003910 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
3911 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003912 return v;
3913 }
3914 else {
3915 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00003916 w = PyUnicode_Concat(v, w);
3917 Py_DECREF(v);
3918 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00003919 }
3920}
3921
Guido van Rossum950361c1997-01-24 13:49:28 +00003922#ifdef DYNAMIC_EXECUTION_PROFILE
3923
Skip Montanarof118cb12001-10-15 20:51:38 +00003924static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003925getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003926{
3927 int i;
3928 PyObject *l = PyList_New(256);
3929 if (l == NULL) return NULL;
3930 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003931 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00003932 if (x == NULL) {
3933 Py_DECREF(l);
3934 return NULL;
3935 }
3936 PyList_SetItem(l, i, x);
3937 }
3938 for (i = 0; i < 256; i++)
3939 a[i] = 0;
3940 return l;
3941}
3942
3943PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003944_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003945{
3946#ifndef DXPAIRS
3947 return getarray(dxp);
3948#else
3949 int i;
3950 PyObject *l = PyList_New(257);
3951 if (l == NULL) return NULL;
3952 for (i = 0; i < 257; i++) {
3953 PyObject *x = getarray(dxpairs[i]);
3954 if (x == NULL) {
3955 Py_DECREF(l);
3956 return NULL;
3957 }
3958 PyList_SetItem(l, i, x);
3959 }
3960 return l;
3961#endif
3962}
3963
3964#endif