blob: ddfe3c48eadd9b31bc8f30b74a137c81fe42c81e [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
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#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
Tim Peters7df5e7f2006-05-26 23:14:37 +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
31 section should work for GCC on any PowerPC platform,
32 irrespective of OS. POWER? Who knows :-) */
33
Michael W. Hudson75eabd22005-01-18 15:56:11 +000034#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
Fredrik Lundh7a830892006-05-27 10:39:48 +000036static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000037ppc_getcounter(uint64 *v)
38{
39 register unsigned long tbu, tb, tbu2;
40
41 loop:
42 asm volatile ("mftbu %0" : "=r" (tbu) );
43 asm volatile ("mftb %0" : "=r" (tb) );
44 asm volatile ("mftbu %0" : "=r" (tbu2));
45 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
46
Tim Peters7df5e7f2006-05-26 23:14:37 +000047 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000048 compiled better by GCC than any other way I tried. */
49 ((long*)(v))[0] = tbu;
50 ((long*)(v))[1] = tb;
51}
52
Michael W. Hudson75eabd22005-01-18 15:56:11 +000053#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
Michael W. Hudson75eabd22005-01-18 15:56:11 +000055#define READ_TIMESTAMP(val) \
56 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
58#endif
59
Tim Peters7df5e7f2006-05-26 23:14:37 +000060void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000061 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
62{
63 uint64 intr, inst, loop;
64 PyThreadState *tstate = PyThreadState_Get();
65 if (!tstate->interp->tscdump)
66 return;
67 intr = intr1 - intr0;
68 inst = inst1 - inst0 - intr;
69 loop = loop1 - loop0 - intr;
70 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
71 opcode, ticked, inst, loop);
72}
Michael W. Hudson800ba232004-08-12 18:19:17 +000073
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000074#endif
75
Guido van Rossum04691fc1992-08-12 15:35:34 +000076/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000078
Guido van Rossum408027e1996-12-30 16:17:54 +000079#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000080/* For debugging the interpreter: */
81#define LLTRACE 1 /* Low-level trace feature */
82#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000083#endif
84
Jeremy Hylton52820442001-01-03 23:52:36 +000085typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000086
Guido van Rossum374a9221991-04-04 10:40:29 +000087/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000088#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +000089static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090#else
Fredrik Lundh7a830892006-05-27 10:39:48 +000091static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +000093static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
94static PyObject * do_call(PyObject *, PyObject ***, int, int);
95static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
96static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
97static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
98static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000099#define CALL_FLAG_VAR 1
100#define CALL_FLAG_KW 2
101
Guido van Rossum0a066c01992-03-27 17:29:15 +0000102#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000103static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000104static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000105#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000106static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000107 int, PyObject *);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +0000108static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000109 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000110static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
111static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000112 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000113
Fredrik Lundh7a830892006-05-27 10:39:48 +0000114static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
115static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000116 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000117static PyObject * cmp_outcome(int, PyObject *, PyObject *);
118static PyObject * import_from(PyObject *, PyObject *);
119static int import_all_from(PyObject *, PyObject *);
120static PyObject * build_class(PyObject *, PyObject *, PyObject *);
121static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000122 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000123static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
124static void reset_exc_info(PyThreadState *);
125static void format_exc_check_arg(PyObject *, char *, PyObject *);
126static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000127 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000128
Paul Prescode68140d2000-08-30 20:25:01 +0000129#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000130 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000131#define GLOBAL_NAME_ERROR_MSG \
132 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000133#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000134 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000135#define UNBOUNDFREE_ERROR_MSG \
136 "free variable '%.200s' referenced before assignment" \
137 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000138
Guido van Rossum950361c1997-01-24 13:49:28 +0000139/* Dynamic execution profile */
140#ifdef DYNAMIC_EXECUTION_PROFILE
141#ifdef DXPAIRS
142static long dxpairs[257][256];
143#define dxp dxpairs[256]
144#else
145static long dxp[256];
146#endif
147#endif
148
Jeremy Hylton985eba52003-02-05 23:13:00 +0000149/* Function call profile */
150#ifdef CALL_PROFILE
151#define PCALL_NUM 11
152static int pcall[PCALL_NUM];
153
154#define PCALL_ALL 0
155#define PCALL_FUNCTION 1
156#define PCALL_FAST_FUNCTION 2
157#define PCALL_FASTER_FUNCTION 3
158#define PCALL_METHOD 4
159#define PCALL_BOUND_METHOD 5
160#define PCALL_CFUNCTION 6
161#define PCALL_TYPE 7
162#define PCALL_GENERATOR 8
163#define PCALL_OTHER 9
164#define PCALL_POP 10
165
166/* Notes about the statistics
167
168 PCALL_FAST stats
169
170 FAST_FUNCTION means no argument tuple needs to be created.
171 FASTER_FUNCTION means that the fast-path frame setup code is used.
172
173 If there is a method call where the call can be optimized by changing
174 the argument tuple and calling the function directly, it gets recorded
175 twice.
176
177 As a result, the relationship among the statistics appears to be
178 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
179 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
180 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
181 PCALL_METHOD > PCALL_BOUND_METHOD
182*/
183
184#define PCALL(POS) pcall[POS]++
185
186PyObject *
187PyEval_GetCallStats(PyObject *self)
188{
Andrew M. Kuchling5f958702006-10-27 13:29:41 +0000189 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000190 pcall[0], pcall[1], pcall[2], pcall[3],
191 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling5f958702006-10-27 13:29:41 +0000192 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000193}
194#else
195#define PCALL(O)
196
197PyObject *
198PyEval_GetCallStats(PyObject *self)
199{
200 Py_INCREF(Py_None);
201 return Py_None;
202}
203#endif
204
Tim Peters5ca576e2001-06-18 22:08:13 +0000205
Guido van Rossume59214e1994-08-30 08:01:59 +0000206#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000207
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000208#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000209#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000210#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000211#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000212
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000213static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000214static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215
Tim Peters7f468f22004-10-11 02:40:51 +0000216int
217PyEval_ThreadsInitialized(void)
218{
219 return interpreter_lock != 0;
220}
221
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000224{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000225 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000226 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000227 interpreter_lock = PyThread_allocate_lock();
228 PyThread_acquire_lock(interpreter_lock, 1);
229 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000230}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000231
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000235 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236}
237
238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242}
243
244void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246{
247 if (tstate == NULL)
248 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000249 /* Check someone has called PyEval_InitThreads() to create the lock */
250 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000251 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000252 if (PyThreadState_Swap(tstate) != NULL)
253 Py_FatalError(
254 "PyEval_AcquireThread: non-NULL old thread state");
255}
256
257void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259{
260 if (tstate == NULL)
261 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
262 if (PyThreadState_Swap(NULL) != tstate)
263 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000265}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000266
267/* This function is called from PyOS_AfterFork to ensure that newly
268 created child processes don't hold locks referring to threads which
269 are not running in the child process. (This could also be done using
270 pthread_atfork mechanism, at least for the pthreads implementation.) */
271
272void
273PyEval_ReInitThreads(void)
274{
Gregory P. Smith5e8dc972008-08-17 23:01:11 +0000275 PyObject *threading, *result;
276 PyThreadState *tstate;
277
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000278 if (!interpreter_lock)
279 return;
280 /*XXX Can't use PyThread_free_lock here because it does too
281 much error-checking. Doing this cleanly would require
282 adding a new function to each thread_*.h. Instead, just
283 create a new lock and waste a little bit of memory */
284 interpreter_lock = PyThread_allocate_lock();
285 PyThread_acquire_lock(interpreter_lock, 1);
286 main_thread = PyThread_get_thread_ident();
Gregory P. Smith5e8dc972008-08-17 23:01:11 +0000287
288 /* Update the threading module with the new state.
289 */
290 tstate = PyThreadState_GET();
291 threading = PyMapping_GetItemString(tstate->interp->modules,
292 "threading");
293 if (threading == NULL) {
294 /* threading not imported */
295 PyErr_Clear();
296 return;
297 }
298 result = PyObject_CallMethod(threading, "_after_fork", NULL);
299 if (result == NULL)
300 PyErr_WriteUnraisable(threading);
301 else
302 Py_DECREF(result);
303 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000304}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305#endif
306
Guido van Rossumff4949e1992-08-05 19:58:53 +0000307/* Functions save_thread and restore_thread are always defined so
308 dynamically loaded modules needn't be compiled separately for use
309 with and without threads: */
310
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000311PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000312PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000314 PyThreadState *tstate = PyThreadState_Swap(NULL);
315 if (tstate == NULL)
316 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000317#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000318 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000319 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000321 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000327 if (tstate == NULL)
328 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000329#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000331 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000332 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334 }
335#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000336 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337}
338
339
Guido van Rossuma9672091994-09-14 13:31:22 +0000340/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
341 signal handlers or Mac I/O completion routines) can schedule calls
342 to a function to be called synchronously.
343 The synchronous function is called with one void* argument.
344 It should return 0 for success or -1 for failure -- failure should
345 be accompanied by an exception.
346
347 If registry succeeds, the registry function returns 0; if it fails
348 (e.g. due to too many pending calls) it returns -1 (without setting
349 an exception condition).
350
351 Note that because registry may occur from within signal handlers,
352 or other asynchronous events, calling malloc() is unsafe!
353
354#ifdef WITH_THREAD
355 Any thread can schedule pending calls, but only the main thread
356 will execute them.
357#endif
358
359 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
360 There are two possible race conditions:
361 (1) nested asynchronous registry calls;
362 (2) registry calls made while pending calls are being processed.
363 While (1) is very unlikely, (2) is a real possibility.
364 The current code is safe against (2), but not against (1).
365 The safety against (2) is derived from the fact that only one
366 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000367
Guido van Rossuma027efa1997-05-05 20:56:21 +0000368 XXX Darn! With the advent of thread state, we should have an array
369 of pending calls per thread in the thread state! Later...
370*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000371
Guido van Rossuma9672091994-09-14 13:31:22 +0000372#define NPENDINGCALLS 32
373static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000374 int (*func)(void *);
375 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000376} pendingcalls[NPENDINGCALLS];
377static volatile int pendingfirst = 0;
378static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000379static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000380
381int
Thomas Wouters334fb892000-07-25 12:56:38 +0000382Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000383{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000384 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000385 int i, j;
386 /* XXX Begin critical section */
387 /* XXX If you want this to be safe against nested
388 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000389 if (busy)
390 return -1;
391 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000392 i = pendinglast;
393 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000394 if (j == pendingfirst) {
395 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000396 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000397 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000398 pendingcalls[i].func = func;
399 pendingcalls[i].arg = arg;
400 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000401
402 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000403 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000404 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000405 /* XXX End critical section */
406 return 0;
407}
408
Guido van Rossum180d7b41994-09-29 09:45:57 +0000409int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000410Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000411{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000412 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000413#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000414 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000415 return 0;
416#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000417 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000418 return 0;
419 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000420 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000421 for (;;) {
422 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000423 int (*func)(void *);
424 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000425 i = pendingfirst;
426 if (i == pendinglast)
427 break; /* Queue empty */
428 func = pendingcalls[i].func;
429 arg = pendingcalls[i].arg;
430 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000431 if (func(arg) < 0) {
432 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000433 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000434 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000435 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000436 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000437 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000438 return 0;
439}
440
441
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000442/* The interpreter's recursion limit */
443
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000444#ifndef Py_DEFAULT_RECURSION_LIMIT
445#define Py_DEFAULT_RECURSION_LIMIT 1000
446#endif
447static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
448int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000449
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000450int
451Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452{
453 return recursion_limit;
454}
455
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000456void
457Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000458{
459 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000460 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000461}
462
Armin Rigo2b3eb402003-10-28 12:05:48 +0000463/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
464 if the recursion_depth reaches _Py_CheckRecursionLimit.
465 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
466 to guarantee that _Py_CheckRecursiveCall() is regularly called.
467 Without USE_STACKCHECK, there is no need for this. */
468int
469_Py_CheckRecursiveCall(char *where)
470{
471 PyThreadState *tstate = PyThreadState_GET();
472
473#ifdef USE_STACKCHECK
474 if (PyOS_CheckStack()) {
475 --tstate->recursion_depth;
476 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
477 return -1;
478 }
479#endif
480 if (tstate->recursion_depth > recursion_limit) {
481 --tstate->recursion_depth;
482 PyErr_Format(PyExc_RuntimeError,
483 "maximum recursion depth exceeded%s",
484 where);
485 return -1;
486 }
487 _Py_CheckRecursionLimit = recursion_limit;
488 return 0;
489}
490
Guido van Rossum374a9221991-04-04 10:40:29 +0000491/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000492enum why_code {
493 WHY_NOT = 0x0001, /* No error */
494 WHY_EXCEPTION = 0x0002, /* Exception occurred */
495 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
496 WHY_RETURN = 0x0008, /* 'return' statement */
497 WHY_BREAK = 0x0010, /* 'break' statement */
498 WHY_CONTINUE = 0x0020, /* 'continue' statement */
499 WHY_YIELD = 0x0040 /* 'yield' operator */
500};
Guido van Rossum374a9221991-04-04 10:40:29 +0000501
Fredrik Lundh7a830892006-05-27 10:39:48 +0000502static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
503static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000504
Skip Montanarod581d772002-09-03 20:10:45 +0000505/* for manipulating the thread switch and periodic "stuff" - used to be
506 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000507int _Py_CheckInterval = 100;
508volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000509
Guido van Rossumb209a111997-04-29 18:18:01 +0000510PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000511PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000512{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000513 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000514 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000515 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000516 (PyObject **)NULL, 0,
517 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000518 (PyObject **)NULL, 0,
519 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000520}
521
522
523/* Interpreter main loop */
524
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000525PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000526PyEval_EvalFrame(PyFrameObject *f) {
527 /* This is for backward compatibility with extension modules that
528 used this API; core interpreter code should call PyEval_EvalFrameEx() */
529 return PyEval_EvalFrameEx(f, 0);
530}
531
532PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000533PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000534{
Guido van Rossum950361c1997-01-24 13:49:28 +0000535#ifdef DXPAIRS
536 int lastopcode = 0;
537#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000538 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000539 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000540 register int opcode; /* Current opcode */
541 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000542 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000543 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000544 register PyObject *x; /* Result object -- NULL if error */
545 register PyObject *v; /* Temporary objects popped off stack */
546 register PyObject *w;
547 register PyObject *u;
548 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000549 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000550 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000551 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000552 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000553 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000554
Tim Peters8a5c3c72004-04-05 19:36:21 +0000555 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000556
557 not (instr_lb <= current_bytecode_offset < instr_ub)
558
Tim Peters8a5c3c72004-04-05 19:36:21 +0000559 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000560 initial values are such as to make this false the first
561 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000562 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000563
Guido van Rossumd076c731998-10-07 19:42:25 +0000564 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000565 PyObject *names;
566 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000567#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000568 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000569 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000570#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000571
Neal Norwitza81d2202002-07-14 00:27:26 +0000572/* Tuple access macros */
573
574#ifndef Py_DEBUG
575#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
576#else
577#define GETITEM(v, i) PyTuple_GetItem((v), (i))
578#endif
579
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000580#ifdef WITH_TSC
581/* Use Pentium timestamp counter to mark certain events:
582 inst0 -- beginning of switch statement for opcode dispatch
583 inst1 -- end of switch statement (may be skipped)
584 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000585 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000586 (may be skipped)
587 intr1 -- beginning of long interruption
588 intr2 -- end of long interruption
589
590 Many opcodes call out to helper C functions. In some cases, the
591 time in those functions should be counted towards the time for the
592 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
593 calls another Python function; there's no point in charge all the
594 bytecode executed by the called function to the caller.
595
596 It's hard to make a useful judgement statically. In the presence
597 of operator overloading, it's impossible to tell if a call will
598 execute new Python code or not.
599
600 It's a case-by-case judgement. I'll use intr1 for the following
601 cases:
602
603 EXEC_STMT
604 IMPORT_STAR
605 IMPORT_FROM
606 CALL_FUNCTION (and friends)
607
608 */
609 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
610 int ticked = 0;
611
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000612 READ_TIMESTAMP(inst0);
613 READ_TIMESTAMP(inst1);
614 READ_TIMESTAMP(loop0);
615 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000616
617 /* shut up the compiler */
618 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000619#endif
620
Guido van Rossum374a9221991-04-04 10:40:29 +0000621/* Code access macros */
622
Martin v. Löwis18e16552006-02-15 17:27:45 +0000623#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000624#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000625#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000626#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000627#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000628#define JUMPBY(x) (next_instr += (x))
629
Raymond Hettingerf606f872003-03-16 03:11:04 +0000630/* OpCode prediction macros
631 Some opcodes tend to come in pairs thus making it possible to predict
632 the second code when the first is run. For example, COMPARE_OP is often
633 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
634 followed by a POP_TOP.
635
636 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000637 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000638 processor has a high likelihood of making its own successful branch
639 prediction which results in a nearly zero overhead transition to the
640 next opcode.
641
642 A successful prediction saves a trip through the eval-loop including
643 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000644
Tim Peters8a5c3c72004-04-05 19:36:21 +0000645 If collecting opcode statistics, turn off prediction so that
646 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000647 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000648*/
649
Raymond Hettingera7216982004-02-08 19:59:27 +0000650#ifdef DYNAMIC_EXECUTION_PROFILE
651#define PREDICT(op) if (0) goto PRED_##op
652#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000653#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000654#endif
655
Raymond Hettingerf606f872003-03-16 03:11:04 +0000656#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000657#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000658
Guido van Rossum374a9221991-04-04 10:40:29 +0000659/* Stack manipulation macros */
660
Martin v. Löwis18e16552006-02-15 17:27:45 +0000661/* The stack can grow at most MAXINT deep, as co_nlocals and
662 co_stacksize are ints. */
663#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000664#define EMPTY() (STACK_LEVEL() == 0)
665#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000666#define SECOND() (stack_pointer[-2])
667#define THIRD() (stack_pointer[-3])
668#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000669#define SET_TOP(v) (stack_pointer[-1] = (v))
670#define SET_SECOND(v) (stack_pointer[-2] = (v))
671#define SET_THIRD(v) (stack_pointer[-3] = (v))
672#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000673#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000674#define BASIC_PUSH(v) (*stack_pointer++ = (v))
675#define BASIC_POP() (*--stack_pointer)
676
Guido van Rossum96a42c81992-01-12 02:29:51 +0000677#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000678#define PUSH(v) { (void)(BASIC_PUSH(v), \
679 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000680 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000681#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000682#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
683 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000684 assert(STACK_LEVEL() <= co->co_stacksize); }
Neal Norwitz03c566a2007-04-16 06:19:52 +0000685#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000686#else
687#define PUSH(v) BASIC_PUSH(v)
688#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000689#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000690#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000691#endif
692
Guido van Rossum681d79a1995-07-18 14:51:37 +0000693/* Local variable macros */
694
695#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000696
697/* The SETLOCAL() macro must not DECREF the local variable in-place and
698 then store the new value; it must copy the old value to a temporary
699 value, then store the new value, and then DECREF the temporary value.
700 This is because it is possible that during the DECREF the frame is
701 accessed by other code (e.g. a __del__ method or gc.collect()) and the
702 variable would be pointing to already-freed memory. */
703#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
704 GETLOCAL(i) = value; \
705 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000706
Guido van Rossuma027efa1997-05-05 20:56:21 +0000707/* Start of code */
708
Tim Peters5ca576e2001-06-18 22:08:13 +0000709 if (f == NULL)
710 return NULL;
711
Armin Rigo1d313ab2003-10-25 14:33:09 +0000712 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000713 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000714 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000715
Tim Peters5ca576e2001-06-18 22:08:13 +0000716 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000717
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000718 if (tstate->use_tracing) {
719 if (tstate->c_tracefunc != NULL) {
720 /* tstate->c_tracefunc, if defined, is a
721 function that will be called on *every* entry
722 to a code block. Its return value, if not
723 None, is a function that will be called at
724 the start of each executed line of code.
725 (Actually, the function must return itself
726 in order to continue tracing.) The trace
727 functions are called with three arguments:
728 a pointer to the current frame, a string
729 indicating why the function is called, and
730 an argument which depends on the situation.
731 The global trace function is also called
732 whenever an exception is detected. */
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +0000733 if (call_trace_protected(tstate->c_tracefunc,
734 tstate->c_traceobj,
735 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000736 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000737 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000738 }
739 }
740 if (tstate->c_profilefunc != NULL) {
741 /* Similar for c_profilefunc, except it needn't
742 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +0000743 if (call_trace_protected(tstate->c_profilefunc,
744 tstate->c_profileobj,
745 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000746 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000748 }
749 }
750 }
751
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000752 co = f->f_code;
753 names = co->co_names;
754 consts = co->co_consts;
755 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000756 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000757 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000758 /* An explanation is in order for the next line.
759
760 f->f_lasti now refers to the index of the last instruction
761 executed. You might think this was obvious from the name, but
762 this wasn't always true before 2.3! PyFrame_New now sets
763 f->f_lasti to -1 (i.e. the index *before* the first instruction)
764 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
765 does work. Promise. */
766 next_instr = first_instr + f->f_lasti + 1;
767 stack_pointer = f->f_stacktop;
768 assert(stack_pointer != NULL);
769 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
770
Tim Peters5ca576e2001-06-18 22:08:13 +0000771#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000773#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000774#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000775 filename = PyString_AsString(co->co_filename);
776#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000777
Guido van Rossum374a9221991-04-04 10:40:29 +0000778 why = WHY_NOT;
779 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000780 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000781 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000782
Anthony Baxtera863d332006-04-11 07:43:46 +0000783 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000784 why = WHY_EXCEPTION;
785 goto on_error;
786 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000787
Guido van Rossum374a9221991-04-04 10:40:29 +0000788 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000789#ifdef WITH_TSC
790 if (inst1 == 0) {
791 /* Almost surely, the opcode executed a break
792 or a continue, preventing inst1 from being set
793 on the way out of the loop.
794 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000795 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000796 loop1 = inst1;
797 }
798 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
799 intr0, intr1);
800 ticked = 0;
801 inst1 = 0;
802 intr0 = 0;
803 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000804 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000805#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000806 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000807 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000808
Guido van Rossuma027efa1997-05-05 20:56:21 +0000809 /* Do periodic things. Doing this every time through
810 the loop would add too much overhead, so we do it
811 only every Nth instruction. We also do it if
812 ``things_to_do'' is set, i.e. when an asynchronous
813 event needs attention (e.g. a signal handler or
814 async I/O handler); see Py_AddPendingCall() and
815 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000816
Skip Montanarod581d772002-09-03 20:10:45 +0000817 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000818 if (*next_instr == SETUP_FINALLY) {
819 /* Make the last opcode before
820 a try: finally: block uninterruptable. */
821 goto fast_next_opcode;
822 }
Skip Montanarod581d772002-09-03 20:10:45 +0000823 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000824 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000825#ifdef WITH_TSC
826 ticked = 1;
827#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000828 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000829 if (Py_MakePendingCalls() < 0) {
830 why = WHY_EXCEPTION;
831 goto on_error;
832 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000833 if (things_to_do)
834 /* MakePendingCalls() didn't succeed.
835 Force early re-execution of this
836 "periodic" code, possibly after
837 a thread switch */
838 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000839 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000840#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841 if (interpreter_lock) {
842 /* Give another thread a chance */
843
Guido van Rossum25ce5661997-08-02 03:10:38 +0000844 if (PyThreadState_Swap(NULL) != tstate)
845 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000846 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847
848 /* Other threads may run now */
849
Guido van Rossum65d5b571998-12-21 19:32:43 +0000850 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000851 if (PyThreadState_Swap(tstate) != NULL)
852 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000853
854 /* Check for thread interrupts */
855
856 if (tstate->async_exc != NULL) {
857 x = tstate->async_exc;
858 tstate->async_exc = NULL;
859 PyErr_SetNone(x);
860 Py_DECREF(x);
861 why = WHY_EXCEPTION;
862 goto on_error;
863 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000864 }
865#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000866 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000867
Neil Schemenauer63543862002-02-17 19:10:14 +0000868 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000869 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000870
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000871 /* line-by-line tracing support */
872
873 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
874 /* see maybe_call_line_trace
875 for expository comments */
876 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000877
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000878 err = maybe_call_line_trace(tstate->c_tracefunc,
879 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000880 f, &instr_lb, &instr_ub,
881 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000882 /* Reload possibly changed frame fields */
883 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000884 if (f->f_stacktop != NULL) {
885 stack_pointer = f->f_stacktop;
886 f->f_stacktop = NULL;
887 }
888 if (err) {
889 /* trace function raised an exception */
890 goto on_error;
891 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000892 }
893
894 /* Extract opcode and argument */
895
Guido van Rossum374a9221991-04-04 10:40:29 +0000896 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000897 oparg = 0; /* allows oparg to be stored in a register because
898 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000899 if (HAS_ARG(opcode))
900 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000901 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000902#ifdef DYNAMIC_EXECUTION_PROFILE
903#ifdef DXPAIRS
904 dxpairs[lastopcode][opcode]++;
905 lastopcode = opcode;
906#endif
907 dxp[opcode]++;
908#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000909
Guido van Rossum96a42c81992-01-12 02:29:51 +0000910#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Guido van Rossum96a42c81992-01-12 02:29:51 +0000913 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000914 if (HAS_ARG(opcode)) {
915 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000916 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000917 }
918 else {
919 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000920 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000921 }
922 }
923#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000924
Guido van Rossum374a9221991-04-04 10:40:29 +0000925 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000926 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000927
Guido van Rossum374a9221991-04-04 10:40:29 +0000928 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Guido van Rossum374a9221991-04-04 10:40:29 +0000930 /* BEWARE!
931 It is essential that any operation that fails sets either
932 x to NULL, err to nonzero, or why to anything but WHY_NOT,
933 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000934
Guido van Rossum374a9221991-04-04 10:40:29 +0000935 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000936
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000937 case NOP:
938 goto fast_next_opcode;
939
Neil Schemenauer63543862002-02-17 19:10:14 +0000940 case LOAD_FAST:
941 x = GETLOCAL(oparg);
942 if (x != NULL) {
943 Py_INCREF(x);
944 PUSH(x);
945 goto fast_next_opcode;
946 }
947 format_exc_check_arg(PyExc_UnboundLocalError,
948 UNBOUNDLOCAL_ERROR_MSG,
949 PyTuple_GetItem(co->co_varnames, oparg));
950 break;
951
952 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000953 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000954 Py_INCREF(x);
955 PUSH(x);
956 goto fast_next_opcode;
957
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000958 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000959 case STORE_FAST:
960 v = POP();
961 SETLOCAL(oparg, v);
962 goto fast_next_opcode;
963
Raymond Hettingerf606f872003-03-16 03:11:04 +0000964 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000965 case POP_TOP:
966 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000967 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000968 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000969
Guido van Rossum374a9221991-04-04 10:40:29 +0000970 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 v = TOP();
972 w = SECOND();
973 SET_TOP(w);
974 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000975 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 v = TOP();
979 w = SECOND();
980 x = THIRD();
981 SET_TOP(w);
982 SET_SECOND(x);
983 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000984 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000985
Thomas Wouters434d0822000-08-24 20:11:32 +0000986 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 u = TOP();
988 v = SECOND();
989 w = THIRD();
990 x = FOURTH();
991 SET_TOP(v);
992 SET_SECOND(w);
993 SET_THIRD(x);
994 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000995 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000996
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 case DUP_TOP:
998 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000999 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001001 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Thomas Wouters434d0822000-08-24 20:11:32 +00001003 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001004 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001006 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001008 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001009 STACKADJ(2);
1010 SET_TOP(x);
1011 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001012 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001013 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001015 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001017 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001019 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 STACKADJ(3);
1021 SET_TOP(x);
1022 SET_SECOND(w);
1023 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001024 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001025 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001026 Py_FatalError("invalid argument to DUP_TOPX"
1027 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001028 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001029
Guido van Rossum374a9221991-04-04 10:40:29 +00001030 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001033 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001034 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001035 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001037
Guido van Rossum374a9221991-04-04 10:40:29 +00001038 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001040 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001041 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001043 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001048 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001050 if (err == 0) {
1051 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001053 continue;
1054 }
1055 else if (err > 0) {
1056 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001058 err = 0;
1059 continue;
1060 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001061 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001065 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001066 x = PyObject_Repr(v);
1067 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001069 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001070 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001071
Guido van Rossum7928cd71991-10-24 14:59:31 +00001072 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001073 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001074 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001075 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001077 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001078 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001079
Guido van Rossum50564e81996-01-12 01:13:16 +00001080 case BINARY_POWER:
1081 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001083 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001084 Py_DECREF(v);
1085 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001086 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001087 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001088 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001089
Guido van Rossum374a9221991-04-04 10:40:29 +00001090 case BINARY_MULTIPLY:
1091 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001093 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001094 Py_DECREF(v);
1095 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001096 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001097 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001101 if (!_Py_QnewFlag) {
1102 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001104 x = PyNumber_Divide(v, w);
1105 Py_DECREF(v);
1106 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001108 if (x != NULL) continue;
1109 break;
1110 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001111 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001112 BINARY_TRUE_DIVIDE */
1113 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001115 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001116 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001117 Py_DECREF(v);
1118 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001120 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Guido van Rossum4668b002001-08-08 05:00:18 +00001123 case BINARY_FLOOR_DIVIDE:
1124 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001126 x = PyNumber_FloorDivide(v, w);
1127 Py_DECREF(v);
1128 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001130 if (x != NULL) continue;
1131 break;
1132
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 case BINARY_MODULO:
1134 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001136 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001137 Py_DECREF(v);
1138 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001140 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001141 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001142
Guido van Rossum374a9221991-04-04 10:40:29 +00001143 case BINARY_ADD:
1144 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001146 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 /* INLINE: int + int */
1148 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001149 a = PyInt_AS_LONG(v);
1150 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001152 if ((i^a) < 0 && (i^b) < 0)
1153 goto slow_add;
1154 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001155 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001156 else if (PyString_CheckExact(v) &&
1157 PyString_CheckExact(w)) {
1158 x = string_concatenate(v, w, f, next_instr);
1159 /* string_concatenate consumed the ref to v */
1160 goto skip_decref_vx;
1161 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001162 else {
1163 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001164 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001165 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001166 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001167 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001168 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001170 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001171 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Guido van Rossum374a9221991-04-04 10:40:29 +00001173 case BINARY_SUBTRACT:
1174 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001176 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001177 /* INLINE: int - int */
1178 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001179 a = PyInt_AS_LONG(v);
1180 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001181 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001182 if ((i^a) < 0 && (i^~b) < 0)
1183 goto slow_sub;
1184 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001185 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001186 else {
1187 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001188 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001189 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001190 Py_DECREF(v);
1191 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_SUBSCR:
1197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001199 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001200 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001201 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001202 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001203 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001204 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001205 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001206 Py_INCREF(x);
1207 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001208 else
1209 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001210 }
1211 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001212 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001213 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001214 Py_DECREF(v);
1215 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001217 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Guido van Rossum7928cd71991-10-24 14:59:31 +00001220 case BINARY_LSHIFT:
1221 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001223 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001224 Py_DECREF(v);
1225 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001227 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Guido van Rossum7928cd71991-10-24 14:59:31 +00001230 case BINARY_RSHIFT:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001233 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001237 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Guido van Rossum7928cd71991-10-24 14:59:31 +00001240 case BINARY_AND:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001243 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001247 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Guido van Rossum7928cd71991-10-24 14:59:31 +00001250 case BINARY_XOR:
1251 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001253 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001254 Py_DECREF(v);
1255 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001256 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001257 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001258 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Guido van Rossum7928cd71991-10-24 14:59:31 +00001260 case BINARY_OR:
1261 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001263 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001264 Py_DECREF(v);
1265 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001266 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001267 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001268 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001269
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001270 case LIST_APPEND:
1271 w = POP();
1272 v = POP();
1273 err = PyList_Append(v, w);
1274 Py_DECREF(v);
1275 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001276 if (err == 0) {
1277 PREDICT(JUMP_ABSOLUTE);
1278 continue;
1279 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001280 break;
1281
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 case INPLACE_POWER:
1283 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 x = PyNumber_InPlacePower(v, w, Py_None);
1286 Py_DECREF(v);
1287 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 if (x != NULL) continue;
1290 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 case INPLACE_MULTIPLY:
1293 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 x = PyNumber_InPlaceMultiply(v, w);
1296 Py_DECREF(v);
1297 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 if (x != NULL) continue;
1300 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001301
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001303 if (!_Py_QnewFlag) {
1304 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001306 x = PyNumber_InPlaceDivide(v, w);
1307 Py_DECREF(v);
1308 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001310 if (x != NULL) continue;
1311 break;
1312 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001314 INPLACE_TRUE_DIVIDE */
1315 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001317 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001318 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 Py_DECREF(v);
1320 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 if (x != NULL) continue;
1323 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Guido van Rossum4668b002001-08-08 05:00:18 +00001325 case INPLACE_FLOOR_DIVIDE:
1326 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001328 x = PyNumber_InPlaceFloorDivide(v, w);
1329 Py_DECREF(v);
1330 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001332 if (x != NULL) continue;
1333 break;
1334
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 case INPLACE_MODULO:
1336 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001337 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 x = PyNumber_InPlaceRemainder(v, w);
1339 Py_DECREF(v);
1340 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 if (x != NULL) continue;
1343 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 case INPLACE_ADD:
1346 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001348 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 /* INLINE: int + int */
1350 register long a, b, i;
1351 a = PyInt_AS_LONG(v);
1352 b = PyInt_AS_LONG(w);
1353 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001354 if ((i^a) < 0 && (i^b) < 0)
1355 goto slow_iadd;
1356 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001358 else if (PyString_CheckExact(v) &&
1359 PyString_CheckExact(w)) {
1360 x = string_concatenate(v, w, f, next_instr);
1361 /* string_concatenate consumed the ref to v */
1362 goto skip_decref_v;
1363 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001364 else {
1365 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001367 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001369 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 if (x != NULL) continue;
1373 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 case INPLACE_SUBTRACT:
1376 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001378 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 /* INLINE: int - int */
1380 register long a, b, i;
1381 a = PyInt_AS_LONG(v);
1382 b = PyInt_AS_LONG(w);
1383 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001384 if ((i^a) < 0 && (i^~b) < 0)
1385 goto slow_isub;
1386 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001388 else {
1389 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001390 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001391 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 Py_DECREF(v);
1393 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 if (x != NULL) continue;
1396 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Thomas Wouters434d0822000-08-24 20:11:32 +00001398 case INPLACE_LSHIFT:
1399 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001401 x = PyNumber_InPlaceLshift(v, w);
1402 Py_DECREF(v);
1403 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001404 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001405 if (x != NULL) continue;
1406 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Thomas Wouters434d0822000-08-24 20:11:32 +00001408 case INPLACE_RSHIFT:
1409 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001411 x = PyNumber_InPlaceRshift(v, w);
1412 Py_DECREF(v);
1413 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 if (x != NULL) continue;
1416 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Thomas Wouters434d0822000-08-24 20:11:32 +00001418 case INPLACE_AND:
1419 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001421 x = PyNumber_InPlaceAnd(v, w);
1422 Py_DECREF(v);
1423 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001425 if (x != NULL) continue;
1426 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Thomas Wouters434d0822000-08-24 20:11:32 +00001428 case INPLACE_XOR:
1429 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001430 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001431 x = PyNumber_InPlaceXor(v, w);
1432 Py_DECREF(v);
1433 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001434 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001435 if (x != NULL) continue;
1436 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Thomas Wouters434d0822000-08-24 20:11:32 +00001438 case INPLACE_OR:
1439 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001440 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001441 x = PyNumber_InPlaceOr(v, w);
1442 Py_DECREF(v);
1443 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001444 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001445 if (x != NULL) continue;
1446 break;
1447
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 case SLICE+0:
1449 case SLICE+1:
1450 case SLICE+2:
1451 case SLICE+3:
1452 if ((opcode-SLICE) & 2)
1453 w = POP();
1454 else
1455 w = NULL;
1456 if ((opcode-SLICE) & 1)
1457 v = POP();
1458 else
1459 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001460 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001461 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001462 Py_DECREF(u);
1463 Py_XDECREF(v);
1464 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001465 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001466 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 case STORE_SLICE+0:
1470 case STORE_SLICE+1:
1471 case STORE_SLICE+2:
1472 case STORE_SLICE+3:
1473 if ((opcode-STORE_SLICE) & 2)
1474 w = POP();
1475 else
1476 w = NULL;
1477 if ((opcode-STORE_SLICE) & 1)
1478 v = POP();
1479 else
1480 v = NULL;
1481 u = POP();
1482 t = POP();
1483 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001484 Py_DECREF(t);
1485 Py_DECREF(u);
1486 Py_XDECREF(v);
1487 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001488 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001489 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Guido van Rossum374a9221991-04-04 10:40:29 +00001491 case DELETE_SLICE+0:
1492 case DELETE_SLICE+1:
1493 case DELETE_SLICE+2:
1494 case DELETE_SLICE+3:
1495 if ((opcode-DELETE_SLICE) & 2)
1496 w = POP();
1497 else
1498 w = NULL;
1499 if ((opcode-DELETE_SLICE) & 1)
1500 v = POP();
1501 else
1502 v = NULL;
1503 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001504 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(u);
1507 Py_XDECREF(v);
1508 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001509 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Guido van Rossum374a9221991-04-04 10:40:29 +00001512 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001513 w = TOP();
1514 v = SECOND();
1515 u = THIRD();
1516 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001518 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001519 Py_DECREF(u);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001522 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Guido van Rossum374a9221991-04-04 10:40:29 +00001525 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001526 w = TOP();
1527 v = SECOND();
1528 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001529 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001530 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001531 Py_DECREF(v);
1532 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001533 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001534 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001535
Guido van Rossum374a9221991-04-04 10:40:29 +00001536 case PRINT_EXPR:
1537 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001538 w = PySys_GetObject("displayhook");
1539 if (w == NULL) {
1540 PyErr_SetString(PyExc_RuntimeError,
1541 "lost sys.displayhook");
1542 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001543 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001544 }
1545 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001546 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001547 if (x == NULL)
1548 err = -1;
1549 }
1550 if (err == 0) {
1551 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001552 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001553 if (w == NULL)
1554 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001556 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001557 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001558 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001559
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001560 case PRINT_ITEM_TO:
1561 w = stream = POP();
1562 /* fall through to PRINT_ITEM */
1563
Guido van Rossum374a9221991-04-04 10:40:29 +00001564 case PRINT_ITEM:
1565 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001566 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001567 w = PySys_GetObject("stdout");
1568 if (w == NULL) {
1569 PyErr_SetString(PyExc_RuntimeError,
1570 "lost sys.stdout");
1571 err = -1;
1572 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001573 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001574 /* PyFile_SoftSpace() can exececute arbitrary code
1575 if sys.stdout is an instance with a __getattr__.
1576 If __getattr__ raises an exception, w will
1577 be freed, so we need to prevent that temporarily. */
1578 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001579 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001580 err = PyFile_WriteString(" ", w);
1581 if (err == 0)
1582 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001583 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001584 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001585 if (PyString_Check(v)) {
1586 char *s = PyString_AS_STRING(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001587 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001588 if (len == 0 ||
1589 !isspace(Py_CHARMASK(s[len-1])) ||
1590 s[len-1] == ' ')
1591 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001592 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001593#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001594 else if (PyUnicode_Check(v)) {
1595 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001596 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001597 if (len == 0 ||
1598 !Py_UNICODE_ISSPACE(s[len-1]) ||
1599 s[len-1] == ' ')
1600 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001601 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001602#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001603 else
1604 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001605 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001606 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001607 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001608 Py_XDECREF(stream);
1609 stream = NULL;
1610 if (err == 0)
1611 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001614 case PRINT_NEWLINE_TO:
1615 w = stream = POP();
1616 /* fall through to PRINT_NEWLINE */
1617
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001619 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001620 w = PySys_GetObject("stdout");
1621 if (w == NULL)
1622 PyErr_SetString(PyExc_RuntimeError,
1623 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001624 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001625 if (w != NULL) {
Amaury Forgeot d'Arcceda6a62008-07-01 20:52:56 +00001626 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001627 err = PyFile_WriteString("\n", w);
1628 if (err == 0)
1629 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcceda6a62008-07-01 20:52:56 +00001630 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001631 }
1632 Py_XDECREF(stream);
1633 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Thomas Wouters434d0822000-08-24 20:11:32 +00001636
1637#ifdef CASE_TOO_BIG
1638 default: switch (opcode) {
1639#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001640 case RAISE_VARARGS:
1641 u = v = w = NULL;
1642 switch (oparg) {
1643 case 3:
1644 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001645 /* Fallthrough */
1646 case 2:
1647 v = POP(); /* value */
1648 /* Fallthrough */
1649 case 1:
1650 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001651 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001652 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001653 break;
1654 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001655 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001656 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001657 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001658 break;
1659 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001663 if ((x = f->f_locals) != NULL) {
1664 Py_INCREF(x);
1665 PUSH(x);
1666 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001667 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001668 PyErr_SetString(PyExc_SystemError, "no locals");
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 RETURN_VALUE:
1672 retval = POP();
1673 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001674 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001675
Tim Peters5ca576e2001-06-18 22:08:13 +00001676 case YIELD_VALUE:
1677 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001678 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001679 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001680 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001681
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001682 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001683 w = TOP();
1684 v = SECOND();
1685 u = THIRD();
1686 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001687 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001688 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001689 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001690 Py_DECREF(u);
1691 Py_DECREF(v);
1692 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001693 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001694
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 case POP_BLOCK:
1696 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001697 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 while (STACK_LEVEL() > b->b_level) {
1699 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001700 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 }
1702 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001703 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001704
Guido van Rossum374a9221991-04-04 10:40:29 +00001705 case END_FINALLY:
1706 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001707 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001708 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001709 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001710 if (why == WHY_RETURN ||
1711 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 retval = POP();
1713 }
Brett Cannonbf364092006-03-01 04:25:17 +00001714 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001716 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001717 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001719 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001721 else if (v != Py_None) {
1722 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 "'finally' pops bad exception");
1724 why = WHY_EXCEPTION;
1725 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001726 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Guido van Rossum374a9221991-04-04 10:40:29 +00001729 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001730 u = TOP();
1731 v = SECOND();
1732 w = THIRD();
1733 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001734 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001735 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001736 Py_DECREF(u);
1737 Py_DECREF(v);
1738 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001740
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001742 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001744 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001745 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001746 err = PyDict_SetItem(x, w, v);
1747 else
1748 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001749 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001750 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001751 break;
1752 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001753 PyErr_Format(PyExc_SystemError,
1754 "no locals found when storing %s",
1755 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001757
Guido van Rossum374a9221991-04-04 10:40:29 +00001758 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001759 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001760 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001761 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001762 format_exc_check_arg(PyExc_NameError,
1763 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001764 break;
1765 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001766 PyErr_Format(PyExc_SystemError,
1767 "no locals when deleting %s",
1768 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001770
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001771 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001772 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001774 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1775 PyObject **items = ((PyTupleObject *)v)->ob_item;
1776 while (oparg--) {
1777 w = items[oparg];
1778 Py_INCREF(w);
1779 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001780 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001781 Py_DECREF(v);
1782 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001783 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1784 PyObject **items = ((PyListObject *)v)->ob_item;
1785 while (oparg--) {
1786 w = items[oparg];
1787 Py_INCREF(w);
1788 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001789 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001790 } else if (unpack_iterable(v, oparg,
Georg Brandl8a10ea42007-03-21 09:00:55 +00001791 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001792 stack_pointer += oparg;
Georg Brandl8a10ea42007-03-21 09:00:55 +00001793 } else {
1794 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001795 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001796 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001797 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001801 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001802 v = TOP();
1803 u = SECOND();
1804 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001805 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1806 Py_DECREF(v);
1807 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001808 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001812 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001814 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1815 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001816 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001819 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001820 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001821 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001822 err = PyDict_SetItem(f->f_globals, w, v);
1823 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001824 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001825 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001827 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001828 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001830 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001831 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001832 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Guido van Rossum374a9221991-04-04 10:40:29 +00001834 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001835 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001836 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001837 PyErr_Format(PyExc_SystemError,
1838 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001839 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001840 break;
1841 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001842 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001843 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001844 Py_XINCREF(x);
1845 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001846 else {
1847 x = PyObject_GetItem(v, w);
1848 if (x == NULL && PyErr_Occurred()) {
1849 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1850 break;
1851 PyErr_Clear();
1852 }
1853 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001855 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001859 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001860 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001861 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 break;
1863 }
1864 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001865 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001867 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001868 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001869
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001871 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001872 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001873 /* Inline the PyDict_GetItem() calls.
1874 WARNING: this is an extreme speed hack.
1875 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001876 long hash = ((PyStringObject *)w)->ob_shash;
1877 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001878 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00001879 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001880 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00001881 e = d->ma_lookup(d, w, hash);
1882 if (e == NULL) {
1883 x = NULL;
1884 break;
1885 }
1886 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001887 if (x != NULL) {
1888 Py_INCREF(x);
1889 PUSH(x);
1890 continue;
1891 }
1892 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00001893 e = d->ma_lookup(d, w, hash);
1894 if (e == NULL) {
1895 x = NULL;
1896 break;
1897 }
1898 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001899 if (x != NULL) {
1900 Py_INCREF(x);
1901 PUSH(x);
1902 continue;
1903 }
1904 goto load_global_error;
1905 }
1906 }
1907 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001908 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001910 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001912 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001913 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001914 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001915 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 break;
1917 }
1918 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001921 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001922
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001923 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001924 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001925 if (x != NULL) {
1926 SETLOCAL(oparg, NULL);
1927 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001928 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001929 format_exc_check_arg(
1930 PyExc_UnboundLocalError,
1931 UNBOUNDLOCAL_ERROR_MSG,
1932 PyTuple_GetItem(co->co_varnames, oparg)
1933 );
1934 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001935
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001936 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001937 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001938 Py_INCREF(x);
1939 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001940 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001941 break;
1942
1943 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001944 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001945 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001946 if (w != NULL) {
1947 PUSH(w);
1948 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001949 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001950 err = -1;
1951 /* Don't stomp existing exception */
1952 if (PyErr_Occurred())
1953 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00001954 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1955 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001956 oparg);
1957 format_exc_check_arg(
1958 PyExc_UnboundLocalError,
1959 UNBOUNDLOCAL_ERROR_MSG,
1960 v);
1961 } else {
Richard Jonescebbefc2006-05-23 18:28:17 +00001962 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001963 co->co_freevars,
Richard Jonescebbefc2006-05-23 18:28:17 +00001964 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001965 format_exc_check_arg(
1966 PyExc_NameError,
1967 UNBOUNDFREE_ERROR_MSG,
1968 v);
1969 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001970 break;
1971
1972 case STORE_DEREF:
1973 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001974 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001975 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001976 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001977 continue;
1978
Guido van Rossum374a9221991-04-04 10:40:29 +00001979 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001980 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001982 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001983 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001985 }
1986 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001987 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001988 }
1989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001990
Guido van Rossum374a9221991-04-04 10:40:29 +00001991 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001992 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001993 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001994 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001995 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001996 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 }
1998 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001999 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 }
2001 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Guido van Rossum374a9221991-04-04 10:40:29 +00002003 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00002004 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002006 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002010 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002011 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002012 x = PyObject_GetAttr(v, w);
2013 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002014 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002015 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002016 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002017
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 case COMPARE_OP:
2019 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002020 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002021 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002022 /* INLINE: cmp(int, int) */
2023 register long a, b;
2024 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002025 a = PyInt_AS_LONG(v);
2026 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002027 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002028 case PyCmp_LT: res = a < b; break;
2029 case PyCmp_LE: res = a <= b; break;
2030 case PyCmp_EQ: res = a == b; break;
2031 case PyCmp_NE: res = a != b; break;
2032 case PyCmp_GT: res = a > b; break;
2033 case PyCmp_GE: res = a >= b; break;
2034 case PyCmp_IS: res = v == w; break;
2035 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002036 default: goto slow_compare;
2037 }
2038 x = res ? Py_True : Py_False;
2039 Py_INCREF(x);
2040 }
2041 else {
2042 slow_compare:
2043 x = cmp_outcome(oparg, v, w);
2044 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002045 Py_DECREF(v);
2046 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002047 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002048 if (x == NULL) break;
2049 PREDICT(JUMP_IF_FALSE);
2050 PREDICT(JUMP_IF_TRUE);
2051 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002054 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002055 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002056 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002057 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002058 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002059 break;
2060 }
Guido van Rossume105f982008-01-23 20:09:39 +00002061 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002062 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002063 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002064 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2065 w = PyTuple_Pack(5,
2066 w,
2067 f->f_globals,
2068 f->f_locals == NULL ?
2069 Py_None : f->f_locals,
2070 v,
2071 u);
2072 else
2073 w = PyTuple_Pack(4,
2074 w,
2075 f->f_globals,
2076 f->f_locals == NULL ?
2077 Py_None : f->f_locals,
2078 v);
2079 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002080 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002081 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002082 u = POP();
Guido van Rossume105f982008-01-23 20:09:39 +00002083 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002084 x = NULL;
2085 break;
2086 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002087 READ_TIMESTAMP(intr0);
Guido van Rossume105f982008-01-23 20:09:39 +00002088 v = x;
2089 x = PyEval_CallObject(v, w);
2090 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002091 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002092 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002093 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002094 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002096
Thomas Wouters52152252000-08-17 22:55:00 +00002097 case IMPORT_STAR:
2098 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002099 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002100 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002101 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002102 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002103 break;
2104 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002105 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002106 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002107 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002108 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002109 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002110 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002111 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002112
Thomas Wouters52152252000-08-17 22:55:00 +00002113 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002114 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002115 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002116 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002117 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002118 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002119 PUSH(x);
2120 if (x != NULL) continue;
2121 break;
2122
Guido van Rossum374a9221991-04-04 10:40:29 +00002123 case JUMP_FORWARD:
2124 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002125 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002126
Raymond Hettingerf606f872003-03-16 03:11:04 +00002127 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002128 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002129 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002130 if (w == Py_True) {
2131 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002132 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002133 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002134 if (w == Py_False) {
2135 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002136 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002137 }
2138 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002139 if (err > 0)
2140 err = 0;
2141 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002142 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002143 else
2144 break;
2145 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Raymond Hettingerf606f872003-03-16 03:11:04 +00002147 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002148 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002149 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002150 if (w == Py_False) {
2151 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002152 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002153 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002154 if (w == Py_True) {
2155 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002156 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002157 }
2158 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002159 if (err > 0) {
2160 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002161 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002162 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002163 else if (err == 0)
2164 ;
2165 else
2166 break;
2167 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002168
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002169 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002170 case JUMP_ABSOLUTE:
2171 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002172 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002174 case GET_ITER:
2175 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002176 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002177 x = PyObject_GetIter(v);
2178 Py_DECREF(v);
2179 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002180 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002181 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002182 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002183 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002184 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002185 break;
2186
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002187 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002188 case FOR_ITER:
2189 /* before: [iter]; after: [iter, iter()] *or* [] */
2190 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002191 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002192 if (x != NULL) {
2193 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002194 PREDICT(STORE_FAST);
2195 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002196 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002197 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002198 if (PyErr_Occurred()) {
2199 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2200 break;
2201 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002202 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002203 /* iterator ended normally */
2204 x = v = POP();
2205 Py_DECREF(v);
2206 JUMPBY(oparg);
2207 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002208
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002209 case BREAK_LOOP:
2210 why = WHY_BREAK;
2211 goto fast_block_end;
2212
2213 case CONTINUE_LOOP:
2214 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002215 if (!retval) {
2216 x = NULL;
2217 break;
2218 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002219 why = WHY_CONTINUE;
2220 goto fast_block_end;
2221
Guido van Rossum374a9221991-04-04 10:40:29 +00002222 case SETUP_LOOP:
2223 case SETUP_EXCEPT:
2224 case SETUP_FINALLY:
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002225 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2226 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2227
Guido van Rossumb209a111997-04-29 18:18:01 +00002228 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002229 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002230 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002231
Guido van Rossumc2e20742006-02-27 22:32:47 +00002232 case WITH_CLEANUP:
2233 {
2234 /* TOP is the context.__exit__ bound method.
2235 Below that are 1-3 values indicating how/why
2236 we entered the finally clause:
2237 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002238 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002239 - SECOND = WHY_*; no retval below it
2240 - (SECOND, THIRD, FOURTH) = exc_info()
2241 In the last case, we must call
2242 TOP(SECOND, THIRD, FOURTH)
2243 otherwise we must call
2244 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002245
2246 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002247 *and* the function call returns a 'true' value, we
2248 "zap" this information, to prevent END_FINALLY from
2249 re-raising the exception. (But non-local gotos
2250 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002251 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002252
Guido van Rossumc2e20742006-02-27 22:32:47 +00002253 x = TOP();
2254 u = SECOND();
2255 if (PyInt_Check(u) || u == Py_None) {
2256 u = v = w = Py_None;
2257 }
2258 else {
2259 v = THIRD();
2260 w = FOURTH();
2261 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002262 /* XXX Not the fastest way to call it... */
2263 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2264 if (x == NULL)
2265 break; /* Go to error exit */
2266 if (u != Py_None && PyObject_IsTrue(x)) {
2267 /* There was an exception and a true return */
2268 Py_DECREF(x);
2269 x = TOP(); /* Again */
2270 STACKADJ(-3);
2271 Py_INCREF(Py_None);
2272 SET_TOP(Py_None);
2273 Py_DECREF(x);
2274 Py_DECREF(u);
2275 Py_DECREF(v);
2276 Py_DECREF(w);
2277 } else {
2278 /* Let END_FINALLY do its thing */
2279 Py_DECREF(x);
2280 x = POP();
2281 Py_DECREF(x);
2282 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002283 break;
2284 }
2285
Guido van Rossumf10570b1995-07-07 22:53:21 +00002286 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002287 {
2288 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002289 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002290 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002291#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002292 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002293#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002294 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002295#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002296 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002297 PUSH(x);
2298 if (x != NULL)
2299 continue;
2300 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002302
Jeremy Hylton76901512000-03-28 23:49:17 +00002303 case CALL_FUNCTION_VAR:
2304 case CALL_FUNCTION_KW:
2305 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002306 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002307 int na = oparg & 0xff;
2308 int nk = (oparg>>8) & 0xff;
2309 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002310 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002311 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002312 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002313 if (flags & CALL_FLAG_VAR)
2314 n++;
2315 if (flags & CALL_FLAG_KW)
2316 n++;
2317 pfunc = stack_pointer - n - 1;
2318 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002319
Guido van Rossumac7be682001-01-17 15:42:30 +00002320 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002321 && PyMethod_GET_SELF(func) != NULL) {
2322 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002323 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002324 func = PyMethod_GET_FUNCTION(func);
2325 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002326 Py_DECREF(*pfunc);
2327 *pfunc = self;
2328 na++;
2329 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002330 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002331 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002332 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002333 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002334 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002335 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002336 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002337 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002338
Jeremy Hylton76901512000-03-28 23:49:17 +00002339 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002340 w = POP();
2341 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002342 }
2343 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002344 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002345 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002346 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002347 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002348
Guido van Rossum681d79a1995-07-18 14:51:37 +00002349 case MAKE_FUNCTION:
2350 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002351 x = PyFunction_New(v, f->f_globals);
2352 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002353 /* XXX Maybe this should be a separate opcode? */
2354 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002355 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002356 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002357 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002358 x = NULL;
2359 break;
2360 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002361 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002362 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002363 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364 }
2365 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002366 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002367 }
2368 PUSH(x);
2369 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002370
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002371 case MAKE_CLOSURE:
2372 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002373 v = POP(); /* code object */
2374 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002375 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 if (x != NULL) {
2377 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002378 err = PyFunction_SetClosure(x, v);
2379 Py_DECREF(v);
2380 }
2381 if (x != NULL && oparg > 0) {
2382 v = PyTuple_New(oparg);
2383 if (v == NULL) {
2384 Py_DECREF(x);
2385 x = NULL;
2386 break;
2387 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002388 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002389 w = POP();
2390 PyTuple_SET_ITEM(v, oparg, w);
2391 }
2392 err = PyFunction_SetDefaults(x, v);
2393 Py_DECREF(v);
2394 }
2395 PUSH(x);
2396 break;
2397 }
2398
Guido van Rossum8861b741996-07-30 16:49:37 +00002399 case BUILD_SLICE:
2400 if (oparg == 3)
2401 w = POP();
2402 else
2403 w = NULL;
2404 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002405 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002406 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002407 Py_DECREF(u);
2408 Py_DECREF(v);
2409 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002410 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002411 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002412 break;
2413
Fred Drakeef8ace32000-08-24 00:32:09 +00002414 case EXTENDED_ARG:
2415 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002416 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002417 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002418
Guido van Rossum374a9221991-04-04 10:40:29 +00002419 default:
2420 fprintf(stderr,
2421 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002422 PyCode_Addr2Line(f->f_code, f->f_lasti),
2423 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002424 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002425 why = WHY_EXCEPTION;
2426 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002427
2428#ifdef CASE_TOO_BIG
2429 }
2430#endif
2431
Guido van Rossum374a9221991-04-04 10:40:29 +00002432 } /* switch */
2433
2434 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002436 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002437
Guido van Rossum374a9221991-04-04 10:40:29 +00002438 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002439
Guido van Rossum374a9221991-04-04 10:40:29 +00002440 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002441 if (err == 0 && x != NULL) {
2442#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002443 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002444 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002445 fprintf(stderr,
2446 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002447 else {
2448#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002449 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002450 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002451#ifdef CHECKEXC
2452 }
2453#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002454 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002456 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 err = 0;
2458 }
2459
Guido van Rossum374a9221991-04-04 10:40:29 +00002460 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002461
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002462 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002463 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002464 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002465 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002466 why = WHY_EXCEPTION;
2467 }
2468 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002469#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002470 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002471 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002472 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002473 char buf[1024];
2474 sprintf(buf, "Stack unwind with exception "
2475 "set and why=%d", why);
2476 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002477 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002478 }
2479#endif
2480
2481 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002482
Guido van Rossum374a9221991-04-04 10:40:29 +00002483 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002484 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002485
Fred Drake8f51f542001-10-04 14:48:42 +00002486 if (tstate->c_tracefunc != NULL)
2487 call_exc_trace(tstate->c_tracefunc,
2488 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002489 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002490
Guido van Rossum374a9221991-04-04 10:40:29 +00002491 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002492
Guido van Rossum374a9221991-04-04 10:40:29 +00002493 if (why == WHY_RERAISE)
2494 why = WHY_EXCEPTION;
2495
2496 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002497
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002498fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002499 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002500 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002501
Tim Peters8a5c3c72004-04-05 19:36:21 +00002502 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002503 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2504 /* For a continue inside a try block,
2505 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002506 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2507 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002508 why = WHY_NOT;
2509 JUMPTO(PyInt_AS_LONG(retval));
2510 Py_DECREF(retval);
2511 break;
2512 }
2513
Guido van Rossum374a9221991-04-04 10:40:29 +00002514 while (STACK_LEVEL() > b->b_level) {
2515 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002516 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002517 }
2518 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2519 why = WHY_NOT;
2520 JUMPTO(b->b_handler);
2521 break;
2522 }
2523 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002524 (b->b_type == SETUP_EXCEPT &&
2525 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002526 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002527 PyObject *exc, *val, *tb;
2528 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002529 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002530 val = Py_None;
2531 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002532 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002533 /* Make the raw exception data
2534 available to the handler,
2535 so a program can emulate the
2536 Python main loop. Don't do
2537 this for 'finally'. */
2538 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002539 PyErr_NormalizeException(
2540 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002541 set_exc_info(tstate,
2542 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002543 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002544 if (tb == NULL) {
2545 Py_INCREF(Py_None);
2546 PUSH(Py_None);
2547 } else
2548 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002549 PUSH(val);
2550 PUSH(exc);
2551 }
2552 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002553 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002554 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002555 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002556 PUSH(v);
2557 }
2558 why = WHY_NOT;
2559 JUMPTO(b->b_handler);
2560 break;
2561 }
2562 } /* unwind stack */
2563
2564 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002565
Guido van Rossum374a9221991-04-04 10:40:29 +00002566 if (why != WHY_NOT)
2567 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002568 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002569
Guido van Rossum374a9221991-04-04 10:40:29 +00002570 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002571
Tim Peters8a5c3c72004-04-05 19:36:21 +00002572 assert(why != WHY_YIELD);
2573 /* Pop remaining stack entries. */
2574 while (!EMPTY()) {
2575 v = POP();
2576 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002577 }
2578
Tim Peters8a5c3c72004-04-05 19:36:21 +00002579 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002580 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002581
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002582fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002583 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002584 if (tstate->c_tracefunc) {
2585 if (why == WHY_RETURN || why == WHY_YIELD) {
2586 if (call_trace(tstate->c_tracefunc,
2587 tstate->c_traceobj, f,
2588 PyTrace_RETURN, retval)) {
2589 Py_XDECREF(retval);
2590 retval = NULL;
2591 why = WHY_EXCEPTION;
2592 }
2593 }
2594 else if (why == WHY_EXCEPTION) {
2595 call_trace_protected(tstate->c_tracefunc,
2596 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002597 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002598 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002599 }
Fred Drake8f51f542001-10-04 14:48:42 +00002600 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002601 if (why == WHY_EXCEPTION)
2602 call_trace_protected(tstate->c_profilefunc,
2603 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002604 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002605 else if (call_trace(tstate->c_profilefunc,
2606 tstate->c_profileobj, f,
2607 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002608 Py_XDECREF(retval);
2609 retval = NULL;
2610 why = WHY_EXCEPTION;
2611 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002612 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002613 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002614
Tim Peters7df5e7f2006-05-26 23:14:37 +00002615 if (tstate->frame->f_exc_type != NULL)
2616 reset_exc_info(tstate);
2617 else {
2618 assert(tstate->frame->f_exc_value == NULL);
2619 assert(tstate->frame->f_exc_traceback == NULL);
2620 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002621
Tim Peters5ca576e2001-06-18 22:08:13 +00002622 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002623 exit_eval_frame:
2624 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002625 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002626
Guido van Rossum96a42c81992-01-12 02:29:51 +00002627 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002628}
2629
Guido van Rossumc2e20742006-02-27 22:32:47 +00002630/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002631 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002632 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002633
Tim Peters6d6c1a32001-08-02 04:15:00 +00002634PyObject *
2635PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002636 PyObject **args, int argcount, PyObject **kws, int kwcount,
2637 PyObject **defs, int defcount, PyObject *closure)
2638{
2639 register PyFrameObject *f;
2640 register PyObject *retval = NULL;
2641 register PyObject **fastlocals, **freevars;
2642 PyThreadState *tstate = PyThreadState_GET();
2643 PyObject *x, *u;
2644
2645 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002646 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002647 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002648 return NULL;
2649 }
2650
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002651 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002652 assert(globals != NULL);
2653 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002654 if (f == NULL)
2655 return NULL;
2656
2657 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002658 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002659
2660 if (co->co_argcount > 0 ||
2661 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2662 int i;
2663 int n = argcount;
2664 PyObject *kwdict = NULL;
2665 if (co->co_flags & CO_VARKEYWORDS) {
2666 kwdict = PyDict_New();
2667 if (kwdict == NULL)
2668 goto fail;
2669 i = co->co_argcount;
2670 if (co->co_flags & CO_VARARGS)
2671 i++;
2672 SETLOCAL(i, kwdict);
2673 }
2674 if (argcount > co->co_argcount) {
2675 if (!(co->co_flags & CO_VARARGS)) {
2676 PyErr_Format(PyExc_TypeError,
2677 "%.200s() takes %s %d "
2678 "%sargument%s (%d given)",
2679 PyString_AsString(co->co_name),
2680 defcount ? "at most" : "exactly",
2681 co->co_argcount,
2682 kwcount ? "non-keyword " : "",
2683 co->co_argcount == 1 ? "" : "s",
2684 argcount);
2685 goto fail;
2686 }
2687 n = co->co_argcount;
2688 }
2689 for (i = 0; i < n; i++) {
2690 x = args[i];
2691 Py_INCREF(x);
2692 SETLOCAL(i, x);
2693 }
2694 if (co->co_flags & CO_VARARGS) {
2695 u = PyTuple_New(argcount - n);
2696 if (u == NULL)
2697 goto fail;
2698 SETLOCAL(co->co_argcount, u);
2699 for (i = n; i < argcount; i++) {
2700 x = args[i];
2701 Py_INCREF(x);
2702 PyTuple_SET_ITEM(u, i-n, x);
2703 }
2704 }
2705 for (i = 0; i < kwcount; i++) {
2706 PyObject *keyword = kws[2*i];
2707 PyObject *value = kws[2*i + 1];
2708 int j;
2709 if (keyword == NULL || !PyString_Check(keyword)) {
2710 PyErr_Format(PyExc_TypeError,
2711 "%.200s() keywords must be strings",
2712 PyString_AsString(co->co_name));
2713 goto fail;
2714 }
2715 /* XXX slow -- speed up using dictionary? */
2716 for (j = 0; j < co->co_argcount; j++) {
2717 PyObject *nm = PyTuple_GET_ITEM(
2718 co->co_varnames, j);
2719 int cmp = PyObject_RichCompareBool(
2720 keyword, nm, Py_EQ);
2721 if (cmp > 0)
2722 break;
2723 else if (cmp < 0)
2724 goto fail;
2725 }
2726 /* Check errors from Compare */
2727 if (PyErr_Occurred())
2728 goto fail;
2729 if (j >= co->co_argcount) {
2730 if (kwdict == NULL) {
2731 PyErr_Format(PyExc_TypeError,
2732 "%.200s() got an unexpected "
2733 "keyword argument '%.400s'",
2734 PyString_AsString(co->co_name),
2735 PyString_AsString(keyword));
2736 goto fail;
2737 }
2738 PyDict_SetItem(kwdict, keyword, value);
2739 }
2740 else {
2741 if (GETLOCAL(j) != NULL) {
2742 PyErr_Format(PyExc_TypeError,
2743 "%.200s() got multiple "
2744 "values for keyword "
2745 "argument '%.400s'",
2746 PyString_AsString(co->co_name),
2747 PyString_AsString(keyword));
2748 goto fail;
2749 }
2750 Py_INCREF(value);
2751 SETLOCAL(j, value);
2752 }
2753 }
2754 if (argcount < co->co_argcount) {
2755 int m = co->co_argcount - defcount;
2756 for (i = argcount; i < m; i++) {
2757 if (GETLOCAL(i) == NULL) {
2758 PyErr_Format(PyExc_TypeError,
2759 "%.200s() takes %s %d "
2760 "%sargument%s (%d given)",
2761 PyString_AsString(co->co_name),
2762 ((co->co_flags & CO_VARARGS) ||
2763 defcount) ? "at least"
2764 : "exactly",
2765 m, kwcount ? "non-keyword " : "",
2766 m == 1 ? "" : "s", i);
2767 goto fail;
2768 }
2769 }
2770 if (n > m)
2771 i = n - m;
2772 else
2773 i = 0;
2774 for (; i < defcount; i++) {
2775 if (GETLOCAL(m+i) == NULL) {
2776 PyObject *def = defs[i];
2777 Py_INCREF(def);
2778 SETLOCAL(m+i, def);
2779 }
2780 }
2781 }
2782 }
2783 else {
2784 if (argcount > 0 || kwcount > 0) {
2785 PyErr_Format(PyExc_TypeError,
2786 "%.200s() takes no arguments (%d given)",
2787 PyString_AsString(co->co_name),
2788 argcount + kwcount);
2789 goto fail;
2790 }
2791 }
2792 /* Allocate and initialize storage for cell vars, and copy free
2793 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002794 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002795 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002796 char *cellname, *argname;
2797 PyObject *c;
2798
2799 nargs = co->co_argcount;
2800 if (co->co_flags & CO_VARARGS)
2801 nargs++;
2802 if (co->co_flags & CO_VARKEYWORDS)
2803 nargs++;
2804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 /* Initialize each cell var, taking into account
2806 cell vars that are initialized from arguments.
2807
2808 Should arrange for the compiler to put cellvars
2809 that are arguments at the beginning of the cellvars
2810 list so that we can march over it more efficiently?
2811 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002812 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002813 cellname = PyString_AS_STRING(
2814 PyTuple_GET_ITEM(co->co_cellvars, i));
2815 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002817 argname = PyString_AS_STRING(
2818 PyTuple_GET_ITEM(co->co_varnames, j));
2819 if (strcmp(cellname, argname) == 0) {
2820 c = PyCell_New(GETLOCAL(j));
2821 if (c == NULL)
2822 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002823 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002824 found = 1;
2825 break;
2826 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002827 }
2828 if (found == 0) {
2829 c = PyCell_New(NULL);
2830 if (c == NULL)
2831 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002832 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002833 }
2834 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002835 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002836 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002837 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002838 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002839 PyObject *o = PyTuple_GET_ITEM(closure, i);
2840 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002841 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002842 }
2843 }
2844
Tim Peters5ca576e2001-06-18 22:08:13 +00002845 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002846 /* Don't need to keep the reference to f_back, it will be set
2847 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002848 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002849 f->f_back = NULL;
2850
Jeremy Hylton985eba52003-02-05 23:13:00 +00002851 PCALL(PCALL_GENERATOR);
2852
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002853 /* Create a new generator that owns the ready to run frame
2854 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002855 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002856 }
2857
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002858 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002859
2860 fail: /* Jump here from prelude on failure */
2861
Tim Petersb13680b2001-11-27 23:29:29 +00002862 /* decref'ing the frame can cause __del__ methods to get invoked,
2863 which can call back into Python. While we're done with the
2864 current Python frame (f), the associated C stack is still in use,
2865 so recursion_depth must be boosted for the duration.
2866 */
2867 assert(tstate != NULL);
2868 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002869 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002870 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002871 return retval;
2872}
2873
2874
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002875/* Implementation notes for set_exc_info() and reset_exc_info():
2876
2877- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2878 'exc_traceback'. These always travel together.
2879
2880- tstate->curexc_ZZZ is the "hot" exception that is set by
2881 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2882
2883- Once an exception is caught by an except clause, it is transferred
2884 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2885 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00002886 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002887
2888- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2889
2890 Long ago, when none of this existed, there were just a few globals:
2891 one set corresponding to the "hot" exception, and one set
2892 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2893 globals; they were simply stored as sys.exc_ZZZ. For backwards
2894 compatibility, they still are!) The problem was that in code like
2895 this:
2896
2897 try:
2898 "something that may fail"
2899 except "some exception":
2900 "do something else first"
2901 "print the exception from sys.exc_ZZZ."
2902
2903 if "do something else first" invoked something that raised and caught
2904 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2905 cause of subtle bugs. I fixed this by changing the semantics as
2906 follows:
2907
2908 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2909 *in that frame*.
2910
2911 - But initially, and as long as no exception is caught in a given
2912 frame, sys.exc_ZZZ will hold the last exception caught in the
2913 previous frame (or the frame before that, etc.).
2914
2915 The first bullet fixed the bug in the above example. The second
2916 bullet was for backwards compatibility: it was (and is) common to
2917 have a function that is called when an exception is caught, and to
2918 have that function access the caught exception via sys.exc_ZZZ.
2919 (Example: traceback.print_exc()).
2920
2921 At the same time I fixed the problem that sys.exc_ZZZ weren't
2922 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2923 but that's really a separate improvement.
2924
2925 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2926 variables to what they were before the current frame was called. The
2927 set_exc_info() function saves them on the frame so that
2928 reset_exc_info() can restore them. The invariant is that
2929 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2930 exception (where "catching" an exception applies only to successful
2931 except clauses); and if the current frame ever caught an exception,
2932 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2933 at the start of the current frame.
2934
2935*/
2936
Fredrik Lundh7a830892006-05-27 10:39:48 +00002937static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002938set_exc_info(PyThreadState *tstate,
2939 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002940{
Tim Peters7df5e7f2006-05-26 23:14:37 +00002941 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002942 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002943
Tim Peters7df5e7f2006-05-26 23:14:37 +00002944 assert(type != NULL);
2945 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002946 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002947 assert(frame->f_exc_value == NULL);
2948 assert(frame->f_exc_traceback == NULL);
2949 /* This frame didn't catch an exception before. */
2950 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002951 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002952 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002953 Py_INCREF(Py_None);
2954 tstate->exc_type = Py_None;
2955 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002956 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002957 Py_XINCREF(tstate->exc_value);
2958 Py_XINCREF(tstate->exc_traceback);
2959 frame->f_exc_type = tstate->exc_type;
2960 frame->f_exc_value = tstate->exc_value;
2961 frame->f_exc_traceback = tstate->exc_traceback;
2962 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002963 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002964 tmp_type = tstate->exc_type;
2965 tmp_value = tstate->exc_value;
2966 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002967 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002968 Py_XINCREF(value);
2969 Py_XINCREF(tb);
2970 tstate->exc_type = type;
2971 tstate->exc_value = value;
2972 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002973 Py_XDECREF(tmp_type);
2974 Py_XDECREF(tmp_value);
2975 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002976 /* For b/w compatibility */
2977 PySys_SetObject("exc_type", type);
2978 PySys_SetObject("exc_value", value);
2979 PySys_SetObject("exc_traceback", tb);
2980}
2981
Fredrik Lundh7a830892006-05-27 10:39:48 +00002982static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002983reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002984{
2985 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002986 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002987
2988 /* It's a precondition that the thread state's frame caught an
2989 * exception -- verify in a debug build.
2990 */
2991 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002992 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002993 assert(frame != NULL);
2994 assert(frame->f_exc_type != NULL);
2995
2996 /* Copy the frame's exception info back to the thread state. */
2997 tmp_type = tstate->exc_type;
2998 tmp_value = tstate->exc_value;
2999 tmp_tb = tstate->exc_traceback;
3000 Py_INCREF(frame->f_exc_type);
3001 Py_XINCREF(frame->f_exc_value);
3002 Py_XINCREF(frame->f_exc_traceback);
3003 tstate->exc_type = frame->f_exc_type;
3004 tstate->exc_value = frame->f_exc_value;
3005 tstate->exc_traceback = frame->f_exc_traceback;
3006 Py_XDECREF(tmp_type);
3007 Py_XDECREF(tmp_value);
3008 Py_XDECREF(tmp_tb);
3009
3010 /* For b/w compatibility */
3011 PySys_SetObject("exc_type", frame->f_exc_type);
3012 PySys_SetObject("exc_value", frame->f_exc_value);
3013 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3014
3015 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003016 tmp_type = frame->f_exc_type;
3017 tmp_value = frame->f_exc_value;
3018 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003019 frame->f_exc_type = NULL;
3020 frame->f_exc_value = NULL;
3021 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003022 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003023 Py_XDECREF(tmp_value);
3024 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003025}
3026
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003027/* Logic for the raise statement (too complicated for inlining).
3028 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003029static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003030do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003031{
Guido van Rossumd295f121998-04-09 21:39:57 +00003032 if (type == NULL) {
3033 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003034 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003035 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3036 value = tstate->exc_value;
3037 tb = tstate->exc_traceback;
3038 Py_XINCREF(type);
3039 Py_XINCREF(value);
3040 Py_XINCREF(tb);
3041 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003042
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003043 /* We support the following forms of raise:
3044 raise <class>, <classinstance>
3045 raise <class>, <argument tuple>
3046 raise <class>, None
3047 raise <class>, <argument>
3048 raise <classinstance>, None
3049 raise <string>, <object>
3050 raise <string>, None
3051
3052 An omitted second argument is the same as None.
3053
3054 In addition, raise <tuple>, <anything> is the same as
3055 raising the tuple's first item (and it better have one!);
3056 this rule is applied recursively.
3057
3058 Finally, an optional third argument can be supplied, which
3059 gives the traceback to be substituted (useful when
3060 re-raising an exception after examining it). */
3061
3062 /* First, check the traceback argument, replacing None with
3063 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003064 if (tb == Py_None) {
3065 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003066 tb = NULL;
3067 }
3068 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003070 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003071 goto raise_error;
3072 }
3073
3074 /* Next, replace a missing value with None */
3075 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003076 value = Py_None;
3077 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003078 }
3079
3080 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003081 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3082 PyObject *tmp = type;
3083 type = PyTuple_GET_ITEM(type, 0);
3084 Py_INCREF(type);
3085 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003086 }
3087
Brett Cannona7446e32006-02-27 23:39:10 +00003088 if (PyString_CheckExact(type)) {
Tim Petersafb2c802002-04-18 18:06:20 +00003089 /* Raising builtin string is deprecated but still allowed --
3090 * do nothing. Raising an instance of a new-style str
3091 * subclass is right out. */
Brett Cannonbf364092006-03-01 04:25:17 +00003092 if (PyErr_Warn(PyExc_DeprecationWarning,
Brett Cannona7446e32006-02-27 23:39:10 +00003093 "raising a string exception is deprecated"))
3094 goto raise_error;
3095 }
Brett Cannonbf364092006-03-01 04:25:17 +00003096 else if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003097 PyErr_NormalizeException(&type, &value, &tb);
3098
Brett Cannonbf364092006-03-01 04:25:17 +00003099 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003100 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003101 if (value != Py_None) {
3102 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003103 "instance exception may not have a separate value");
3104 goto raise_error;
3105 }
3106 else {
3107 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003108 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003109 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003110 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003111 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003112 }
3113 }
3114 else {
3115 /* Not something you can raise. You get an exception
3116 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003117 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00003118 "exceptions must be classes, instances, or "
3119 "strings (deprecated), not %s",
3120 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003121 goto raise_error;
3122 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003123 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003124 if (tb == NULL)
3125 return WHY_EXCEPTION;
3126 else
3127 return WHY_RERAISE;
3128 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003129 Py_XDECREF(value);
3130 Py_XDECREF(type);
3131 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003132 return WHY_EXCEPTION;
3133}
3134
Tim Petersd6d010b2001-06-21 02:49:55 +00003135/* Iterate v argcnt times and store the results on the stack (via decreasing
3136 sp). Return 1 for success, 0 if error. */
3137
Fredrik Lundh7a830892006-05-27 10:39:48 +00003138static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003139unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003140{
Tim Petersd6d010b2001-06-21 02:49:55 +00003141 int i = 0;
3142 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003143 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003144
Tim Petersd6d010b2001-06-21 02:49:55 +00003145 assert(v != NULL);
3146
3147 it = PyObject_GetIter(v);
3148 if (it == NULL)
3149 goto Error;
3150
3151 for (; i < argcnt; i++) {
3152 w = PyIter_Next(it);
3153 if (w == NULL) {
3154 /* Iterator done, via error or exhaustion. */
3155 if (!PyErr_Occurred()) {
3156 PyErr_Format(PyExc_ValueError,
3157 "need more than %d value%s to unpack",
3158 i, i == 1 ? "" : "s");
3159 }
3160 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003161 }
3162 *--sp = w;
3163 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003164
3165 /* We better have exhausted the iterator now. */
3166 w = PyIter_Next(it);
3167 if (w == NULL) {
3168 if (PyErr_Occurred())
3169 goto Error;
3170 Py_DECREF(it);
3171 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003172 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003173 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003174 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003175 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003176Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003177 for (; i > 0; i--, sp++)
3178 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003179 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003180 return 0;
3181}
3182
3183
Guido van Rossum96a42c81992-01-12 02:29:51 +00003184#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003185static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003186prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003187{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003188 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003189 if (PyObject_Print(v, stdout, 0) != 0)
3190 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003191 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003192 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003193}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003194#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003195
Fredrik Lundh7a830892006-05-27 10:39:48 +00003196static void
Fred Drake5755ce62001-06-27 19:19:46 +00003197call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003198{
Guido van Rossumb209a111997-04-29 18:18:01 +00003199 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003200 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003201 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003202 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003203 value = Py_None;
3204 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003205 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003206 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003207 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003208 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003209 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003210 }
Fred Drake5755ce62001-06-27 19:19:46 +00003211 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003212 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003213 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003214 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003215 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003216 Py_XDECREF(type);
3217 Py_XDECREF(value);
3218 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003219 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003220}
3221
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003222static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003223call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003224 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003225{
3226 PyObject *type, *value, *traceback;
3227 int err;
3228 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003229 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003230 if (err == 0)
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003231 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003232 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003233 return 0;
3234 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003235 else {
3236 Py_XDECREF(type);
3237 Py_XDECREF(value);
3238 Py_XDECREF(traceback);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003239 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003240 }
3241}
3242
Fredrik Lundh7a830892006-05-27 10:39:48 +00003243static int
Fred Drake5755ce62001-06-27 19:19:46 +00003244call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3245 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003246{
Fred Drake5755ce62001-06-27 19:19:46 +00003247 register PyThreadState *tstate = frame->f_tstate;
3248 int result;
3249 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003250 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003251 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003252 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003253 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003254 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3255 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003256 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003257 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003258}
3259
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003260PyObject *
3261_PyEval_CallTracing(PyObject *func, PyObject *args)
3262{
3263 PyFrameObject *frame = PyEval_GetFrame();
3264 PyThreadState *tstate = frame->f_tstate;
3265 int save_tracing = tstate->tracing;
3266 int save_use_tracing = tstate->use_tracing;
3267 PyObject *result;
3268
3269 tstate->tracing = 0;
3270 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3271 || (tstate->c_profilefunc != NULL));
3272 result = PyObject_Call(func, args, NULL);
3273 tstate->tracing = save_tracing;
3274 tstate->use_tracing = save_use_tracing;
3275 return result;
3276}
3277
Fredrik Lundh7a830892006-05-27 10:39:48 +00003278static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003279maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003280 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3281 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003282{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003283 int result = 0;
3284
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003285 /* If the last instruction executed isn't in the current
3286 instruction window, reset the window. If the last
3287 instruction happens to fall at the start of a line or if it
3288 represents a jump backwards, call the trace function.
3289 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003290 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003291 int line;
3292 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003293
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003294 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3295 &bounds);
3296 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003297 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003298 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003299 PyTrace_LINE, Py_None);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003300 }
3301 *instr_lb = bounds.ap_lower;
3302 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003303 }
Armin Rigobf57a142004-03-22 19:24:58 +00003304 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003305 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003306 }
3307 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003308 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003309}
3310
Fred Drake5755ce62001-06-27 19:19:46 +00003311void
3312PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003313{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003314 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003315 PyObject *temp = tstate->c_profileobj;
3316 Py_XINCREF(arg);
3317 tstate->c_profilefunc = NULL;
3318 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003319 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003320 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003321 Py_XDECREF(temp);
3322 tstate->c_profilefunc = func;
3323 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003324 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003325 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003326}
3327
3328void
3329PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3330{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003331 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003332 PyObject *temp = tstate->c_traceobj;
3333 Py_XINCREF(arg);
3334 tstate->c_tracefunc = NULL;
3335 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003336 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003337 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003338 Py_XDECREF(temp);
3339 tstate->c_tracefunc = func;
3340 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003341 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003342 tstate->use_tracing = ((func != NULL)
3343 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003344}
3345
Guido van Rossumb209a111997-04-29 18:18:01 +00003346PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003347PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003348{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003349 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003350 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003351 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003352 else
3353 return current_frame->f_builtins;
3354}
3355
Guido van Rossumb209a111997-04-29 18:18:01 +00003356PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003357PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003358{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003359 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003360 if (current_frame == NULL)
3361 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003362 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003363 return current_frame->f_locals;
3364}
3365
Guido van Rossumb209a111997-04-29 18:18:01 +00003366PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003367PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003368{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003369 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003370 if (current_frame == NULL)
3371 return NULL;
3372 else
3373 return current_frame->f_globals;
3374}
3375
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003376PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003377PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003378{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003379 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003380 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003381}
3382
Guido van Rossum6135a871995-01-09 17:53:26 +00003383int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003384PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003385{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003386 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003387 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003388}
3389
Guido van Rossumbe270261997-05-22 22:26:18 +00003390int
Tim Peters5ba58662001-07-16 02:29:45 +00003391PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003392{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003393 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003394 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003395
3396 if (current_frame != NULL) {
3397 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003398 const int compilerflags = codeflags & PyCF_MASK;
3399 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003400 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003401 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003402 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003403#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003404 if (codeflags & CO_GENERATOR_ALLOWED) {
3405 result = 1;
3406 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3407 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003408#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003409 }
3410 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003411}
3412
3413int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003414Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003415{
Guido van Rossumb209a111997-04-29 18:18:01 +00003416 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003417 if (f == NULL)
3418 return 0;
3419 if (!PyFile_SoftSpace(f, 0))
3420 return 0;
3421 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003422}
3423
Guido van Rossum3f5da241990-12-20 15:06:42 +00003424
Guido van Rossum681d79a1995-07-18 14:51:37 +00003425/* External interface to call any callable object.
3426 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003427
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003428#undef PyEval_CallObject
3429/* for backward compatibility: export this interface */
3430
Guido van Rossumb209a111997-04-29 18:18:01 +00003431PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003432PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003433{
Guido van Rossumb209a111997-04-29 18:18:01 +00003434 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003435}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003436#define PyEval_CallObject(func,arg) \
3437 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003438
Guido van Rossumb209a111997-04-29 18:18:01 +00003439PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003440PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003441{
Jeremy Hylton52820442001-01-03 23:52:36 +00003442 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003443
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003444 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003445 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003446 if (arg == NULL)
3447 return NULL;
3448 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003449 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003450 PyErr_SetString(PyExc_TypeError,
3451 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003452 return NULL;
3453 }
3454 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003455 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003456
Guido van Rossumb209a111997-04-29 18:18:01 +00003457 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003458 PyErr_SetString(PyExc_TypeError,
3459 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003460 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003461 return NULL;
3462 }
3463
Tim Peters6d6c1a32001-08-02 04:15:00 +00003464 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003465 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003466 return result;
3467}
3468
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003469const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003470PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003471{
3472 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003473 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003474 else if (PyFunction_Check(func))
3475 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3476 else if (PyCFunction_Check(func))
3477 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3478 else if (PyClass_Check(func))
3479 return PyString_AsString(((PyClassObject*)func)->cl_name);
3480 else if (PyInstance_Check(func)) {
3481 return PyString_AsString(
3482 ((PyInstanceObject*)func)->in_class->cl_name);
3483 } else {
3484 return func->ob_type->tp_name;
3485 }
3486}
3487
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003488const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003489PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003490{
3491 if (PyMethod_Check(func))
3492 return "()";
3493 else if (PyFunction_Check(func))
3494 return "()";
3495 else if (PyCFunction_Check(func))
3496 return "()";
3497 else if (PyClass_Check(func))
3498 return " constructor";
3499 else if (PyInstance_Check(func)) {
3500 return " instance";
3501 } else {
3502 return " object";
3503 }
3504}
3505
Fredrik Lundh7a830892006-05-27 10:39:48 +00003506static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003507err_args(PyObject *func, int flags, int nargs)
3508{
3509 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003510 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003511 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003512 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003513 nargs);
3514 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003515 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003516 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003517 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003518 nargs);
3519}
3520
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003521#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003522if (tstate->use_tracing && tstate->c_profilefunc) { \
3523 if (call_trace(tstate->c_profilefunc, \
3524 tstate->c_profileobj, \
3525 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003526 func)) { \
3527 x = NULL; \
3528 } \
3529 else { \
3530 x = call; \
3531 if (tstate->c_profilefunc != NULL) { \
3532 if (x == NULL) { \
3533 call_trace_protected(tstate->c_profilefunc, \
3534 tstate->c_profileobj, \
3535 tstate->frame, PyTrace_C_EXCEPTION, \
3536 func); \
3537 /* XXX should pass (type, value, tb) */ \
3538 } else { \
3539 if (call_trace(tstate->c_profilefunc, \
3540 tstate->c_profileobj, \
3541 tstate->frame, PyTrace_C_RETURN, \
3542 func)) { \
3543 Py_DECREF(x); \
3544 x = NULL; \
3545 } \
3546 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003547 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003548 } \
3549} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003550 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003551 }
3552
Fredrik Lundh7a830892006-05-27 10:39:48 +00003553static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003554call_function(PyObject ***pp_stack, int oparg
3555#ifdef WITH_TSC
3556 , uint64* pintr0, uint64* pintr1
3557#endif
3558 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003559{
3560 int na = oparg & 0xff;
3561 int nk = (oparg>>8) & 0xff;
3562 int n = na + 2 * nk;
3563 PyObject **pfunc = (*pp_stack) - n - 1;
3564 PyObject *func = *pfunc;
3565 PyObject *x, *w;
3566
Jeremy Hylton985eba52003-02-05 23:13:00 +00003567 /* Always dispatch PyCFunction first, because these are
3568 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569 */
3570 if (PyCFunction_Check(func) && nk == 0) {
3571 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003572 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003573
3574 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003575 if (flags & (METH_NOARGS | METH_O)) {
3576 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3577 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003578 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003579 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003580 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003581 else if (flags & METH_O && na == 1) {
3582 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003583 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003584 Py_DECREF(arg);
3585 }
3586 else {
3587 err_args(func, flags, na);
3588 x = NULL;
3589 }
3590 }
3591 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003592 PyObject *callargs;
3593 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003594 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003595 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003596 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003597 Py_XDECREF(callargs);
3598 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003599 } else {
3600 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3601 /* optimize access to bound methods */
3602 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003603 PCALL(PCALL_METHOD);
3604 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003605 Py_INCREF(self);
3606 func = PyMethod_GET_FUNCTION(func);
3607 Py_INCREF(func);
3608 Py_DECREF(*pfunc);
3609 *pfunc = self;
3610 na++;
3611 n++;
3612 } else
3613 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003614 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003615 if (PyFunction_Check(func))
3616 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003617 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003618 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003619 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003620 Py_DECREF(func);
3621 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003622
Armin Rigod34fa522006-03-28 19:10:40 +00003623 /* Clear the stack of the function object. Also removes
3624 the arguments in case they weren't consumed already
3625 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003626 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003627 while ((*pp_stack) > pfunc) {
3628 w = EXT_POP(*pp_stack);
3629 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003630 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003631 }
3632 return x;
3633}
3634
Jeremy Hylton192690e2002-08-16 18:36:11 +00003635/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003636 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003637 For the simplest case -- a function that takes only positional
3638 arguments and is called with only positional arguments -- it
3639 inlines the most primitive frame setup code from
3640 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3641 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003642*/
3643
Fredrik Lundh7a830892006-05-27 10:39:48 +00003644static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003645fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003646{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003647 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003648 PyObject *globals = PyFunction_GET_GLOBALS(func);
3649 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3650 PyObject **d = NULL;
3651 int nd = 0;
3652
Jeremy Hylton985eba52003-02-05 23:13:00 +00003653 PCALL(PCALL_FUNCTION);
3654 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003655 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003656 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3657 PyFrameObject *f;
3658 PyObject *retval = NULL;
3659 PyThreadState *tstate = PyThreadState_GET();
3660 PyObject **fastlocals, **stack;
3661 int i;
3662
3663 PCALL(PCALL_FASTER_FUNCTION);
3664 assert(globals != NULL);
3665 /* XXX Perhaps we should create a specialized
3666 PyFrame_New() that doesn't take locals, but does
3667 take builtins without sanity checking them.
3668 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003669 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003670 f = PyFrame_New(tstate, co, globals, NULL);
3671 if (f == NULL)
3672 return NULL;
3673
3674 fastlocals = f->f_localsplus;
3675 stack = (*pp_stack) - n;
3676
3677 for (i = 0; i < n; i++) {
3678 Py_INCREF(*stack);
3679 fastlocals[i] = *stack++;
3680 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003681 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003682 ++tstate->recursion_depth;
3683 Py_DECREF(f);
3684 --tstate->recursion_depth;
3685 return retval;
3686 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003687 if (argdefs != NULL) {
3688 d = &PyTuple_GET_ITEM(argdefs, 0);
3689 nd = ((PyTupleObject *)argdefs)->ob_size;
3690 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003691 return PyEval_EvalCodeEx(co, globals,
3692 (PyObject *)NULL, (*pp_stack)-n, na,
3693 (*pp_stack)-2*nk, nk, d, nd,
3694 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003695}
3696
Fredrik Lundh7a830892006-05-27 10:39:48 +00003697static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003698update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3699 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003700{
3701 PyObject *kwdict = NULL;
3702 if (orig_kwdict == NULL)
3703 kwdict = PyDict_New();
3704 else {
3705 kwdict = PyDict_Copy(orig_kwdict);
3706 Py_DECREF(orig_kwdict);
3707 }
3708 if (kwdict == NULL)
3709 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003710 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003711 int err;
3712 PyObject *value = EXT_POP(*pp_stack);
3713 PyObject *key = EXT_POP(*pp_stack);
3714 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003715 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003716 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003717 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003718 PyEval_GetFuncName(func),
3719 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003720 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003721 Py_DECREF(key);
3722 Py_DECREF(value);
3723 Py_DECREF(kwdict);
3724 return NULL;
3725 }
3726 err = PyDict_SetItem(kwdict, key, value);
3727 Py_DECREF(key);
3728 Py_DECREF(value);
3729 if (err) {
3730 Py_DECREF(kwdict);
3731 return NULL;
3732 }
3733 }
3734 return kwdict;
3735}
3736
Fredrik Lundh7a830892006-05-27 10:39:48 +00003737static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003738update_star_args(int nstack, int nstar, PyObject *stararg,
3739 PyObject ***pp_stack)
3740{
3741 PyObject *callargs, *w;
3742
3743 callargs = PyTuple_New(nstack + nstar);
3744 if (callargs == NULL) {
3745 return NULL;
3746 }
3747 if (nstar) {
3748 int i;
3749 for (i = 0; i < nstar; i++) {
3750 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3751 Py_INCREF(a);
3752 PyTuple_SET_ITEM(callargs, nstack + i, a);
3753 }
3754 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003755 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003756 w = EXT_POP(*pp_stack);
3757 PyTuple_SET_ITEM(callargs, nstack, w);
3758 }
3759 return callargs;
3760}
3761
Fredrik Lundh7a830892006-05-27 10:39:48 +00003762static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003763load_args(PyObject ***pp_stack, int na)
3764{
3765 PyObject *args = PyTuple_New(na);
3766 PyObject *w;
3767
3768 if (args == NULL)
3769 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003770 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003771 w = EXT_POP(*pp_stack);
3772 PyTuple_SET_ITEM(args, na, w);
3773 }
3774 return args;
3775}
3776
Fredrik Lundh7a830892006-05-27 10:39:48 +00003777static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003778do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3779{
3780 PyObject *callargs = NULL;
3781 PyObject *kwdict = NULL;
3782 PyObject *result = NULL;
3783
3784 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003785 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003786 if (kwdict == NULL)
3787 goto call_fail;
3788 }
3789 callargs = load_args(pp_stack, na);
3790 if (callargs == NULL)
3791 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003792#ifdef CALL_PROFILE
3793 /* At this point, we have to look at the type of func to
3794 update the call stats properly. Do it here so as to avoid
3795 exposing the call stats machinery outside ceval.c
3796 */
3797 if (PyFunction_Check(func))
3798 PCALL(PCALL_FUNCTION);
3799 else if (PyMethod_Check(func))
3800 PCALL(PCALL_METHOD);
3801 else if (PyType_Check(func))
3802 PCALL(PCALL_TYPE);
3803 else
3804 PCALL(PCALL_OTHER);
3805#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003806 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003807 call_fail:
3808 Py_XDECREF(callargs);
3809 Py_XDECREF(kwdict);
3810 return result;
3811}
3812
Fredrik Lundh7a830892006-05-27 10:39:48 +00003813static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003814ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3815{
3816 int nstar = 0;
3817 PyObject *callargs = NULL;
3818 PyObject *stararg = NULL;
3819 PyObject *kwdict = NULL;
3820 PyObject *result = NULL;
3821
3822 if (flags & CALL_FLAG_KW) {
3823 kwdict = EXT_POP(*pp_stack);
3824 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003825 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003826 "%s%s argument after ** "
3827 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003828 PyEval_GetFuncName(func),
3829 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003830 goto ext_call_fail;
3831 }
3832 }
3833 if (flags & CALL_FLAG_VAR) {
3834 stararg = EXT_POP(*pp_stack);
3835 if (!PyTuple_Check(stararg)) {
3836 PyObject *t = NULL;
3837 t = PySequence_Tuple(stararg);
3838 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003839 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3840 PyErr_Format(PyExc_TypeError,
3841 "%s%s argument after * "
3842 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003843 PyEval_GetFuncName(func),
3844 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003845 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003846 goto ext_call_fail;
3847 }
3848 Py_DECREF(stararg);
3849 stararg = t;
3850 }
3851 nstar = PyTuple_GET_SIZE(stararg);
3852 }
3853 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003854 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003855 if (kwdict == NULL)
3856 goto ext_call_fail;
3857 }
3858 callargs = update_star_args(na, nstar, stararg, pp_stack);
3859 if (callargs == NULL)
3860 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003861#ifdef CALL_PROFILE
3862 /* At this point, we have to look at the type of func to
3863 update the call stats properly. Do it here so as to avoid
3864 exposing the call stats machinery outside ceval.c
3865 */
3866 if (PyFunction_Check(func))
3867 PCALL(PCALL_FUNCTION);
3868 else if (PyMethod_Check(func))
3869 PCALL(PCALL_METHOD);
3870 else if (PyType_Check(func))
3871 PCALL(PCALL_TYPE);
3872 else
3873 PCALL(PCALL_OTHER);
3874#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003875 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003876 ext_call_fail:
3877 Py_XDECREF(callargs);
3878 Py_XDECREF(kwdict);
3879 Py_XDECREF(stararg);
3880 return result;
3881}
3882
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003883/* Extract a slice index from a PyInt or PyLong or an object with the
3884 nb_index slot defined, and store in *pi.
3885 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3886 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 +00003887 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003888*/
Tim Petersb5196382001-12-16 19:44:20 +00003889/* Note: If v is NULL, return success without storing into *pi. This
3890 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3891 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003892*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003893int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003894_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003895{
Tim Petersb5196382001-12-16 19:44:20 +00003896 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003897 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003898 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003899 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3900 however, it looks like it should be AsSsize_t.
3901 There should be a comment here explaining why.
3902 */
3903 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003904 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003905 else if (PyIndex_Check(v)) {
3906 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003907 if (x == -1 && PyErr_Occurred())
3908 return 0;
3909 }
3910 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003911 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003912 "slice indices must be integers or "
3913 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003914 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003915 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003916 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003917 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003918 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003919}
3920
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003921#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003922#define ISINDEX(x) ((x) == NULL || \
3923 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003924
Fredrik Lundh7a830892006-05-27 10:39:48 +00003925static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003926apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003927{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003928 PyTypeObject *tp = u->ob_type;
3929 PySequenceMethods *sq = tp->tp_as_sequence;
3930
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003931 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003932 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003933 if (!_PyEval_SliceIndex(v, &ilow))
3934 return NULL;
3935 if (!_PyEval_SliceIndex(w, &ihigh))
3936 return NULL;
3937 return PySequence_GetSlice(u, ilow, ihigh);
3938 }
3939 else {
3940 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003941 if (slice != NULL) {
3942 PyObject *res = PyObject_GetItem(u, slice);
3943 Py_DECREF(slice);
3944 return res;
3945 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003946 else
3947 return NULL;
3948 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003949}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003950
Fredrik Lundh7a830892006-05-27 10:39:48 +00003951static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003952assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3953 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003954{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003955 PyTypeObject *tp = u->ob_type;
3956 PySequenceMethods *sq = tp->tp_as_sequence;
3957
Georg Brandl0ea89162007-03-05 22:28:13 +00003958 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003959 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003960 if (!_PyEval_SliceIndex(v, &ilow))
3961 return -1;
3962 if (!_PyEval_SliceIndex(w, &ihigh))
3963 return -1;
3964 if (x == NULL)
3965 return PySequence_DelSlice(u, ilow, ihigh);
3966 else
3967 return PySequence_SetSlice(u, ilow, ihigh, x);
3968 }
3969 else {
3970 PyObject *slice = PySlice_New(v, w, NULL);
3971 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003972 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003973 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003974 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003975 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003976 res = PyObject_DelItem(u, slice);
3977 Py_DECREF(slice);
3978 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003979 }
3980 else
3981 return -1;
3982 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983}
3984
Fredrik Lundh7a830892006-05-27 10:39:48 +00003985static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003986cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003987{
Guido van Rossumac7be682001-01-17 15:42:30 +00003988 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003990 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003991 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003992 break;
3993 case PyCmp_IS_NOT:
3994 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003995 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003996 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003997 res = PySequence_Contains(w, v);
3998 if (res < 0)
3999 return NULL;
4000 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004001 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004002 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004003 if (res < 0)
4004 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004005 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004006 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004007 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004008 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004009 break;
4010 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004011 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004012 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004013 v = res ? Py_True : Py_False;
4014 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004015 return v;
4016}
4017
Fredrik Lundh7a830892006-05-27 10:39:48 +00004018static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004019import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004020{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004021 PyObject *x;
4022
4023 x = PyObject_GetAttr(v, name);
4024 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004025 PyErr_Format(PyExc_ImportError,
4026 "cannot import name %.230s",
4027 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004028 }
Thomas Wouters52152252000-08-17 22:55:00 +00004029 return x;
4030}
Guido van Rossumac7be682001-01-17 15:42:30 +00004031
Fredrik Lundh7a830892006-05-27 10:39:48 +00004032static int
Thomas Wouters52152252000-08-17 22:55:00 +00004033import_all_from(PyObject *locals, PyObject *v)
4034{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004035 PyObject *all = PyObject_GetAttrString(v, "__all__");
4036 PyObject *dict, *name, *value;
4037 int skip_leading_underscores = 0;
4038 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004039
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004040 if (all == NULL) {
4041 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4042 return -1; /* Unexpected error */
4043 PyErr_Clear();
4044 dict = PyObject_GetAttrString(v, "__dict__");
4045 if (dict == NULL) {
4046 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4047 return -1;
4048 PyErr_SetString(PyExc_ImportError,
4049 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004050 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004051 }
4052 all = PyMapping_Keys(dict);
4053 Py_DECREF(dict);
4054 if (all == NULL)
4055 return -1;
4056 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004057 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004058
4059 for (pos = 0, err = 0; ; pos++) {
4060 name = PySequence_GetItem(all, pos);
4061 if (name == NULL) {
4062 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4063 err = -1;
4064 else
4065 PyErr_Clear();
4066 break;
4067 }
4068 if (skip_leading_underscores &&
4069 PyString_Check(name) &&
4070 PyString_AS_STRING(name)[0] == '_')
4071 {
4072 Py_DECREF(name);
4073 continue;
4074 }
4075 value = PyObject_GetAttr(v, name);
4076 if (value == NULL)
4077 err = -1;
Armin Rigo1bc1ab22006-11-29 22:07:38 +00004078 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004079 err = PyDict_SetItem(locals, name, value);
Armin Rigo1bc1ab22006-11-29 22:07:38 +00004080 else
4081 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004082 Py_DECREF(name);
4083 Py_XDECREF(value);
4084 if (err != 0)
4085 break;
4086 }
4087 Py_DECREF(all);
4088 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004089}
4090
Fredrik Lundh7a830892006-05-27 10:39:48 +00004091static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004092build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004093{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004094 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004095
4096 if (PyDict_Check(methods))
4097 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004098 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004099 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004100 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4101 base = PyTuple_GET_ITEM(bases, 0);
4102 metaclass = PyObject_GetAttrString(base, "__class__");
4103 if (metaclass == NULL) {
4104 PyErr_Clear();
4105 metaclass = (PyObject *)base->ob_type;
4106 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004107 }
4108 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004109 else {
4110 PyObject *g = PyEval_GetGlobals();
4111 if (g != NULL && PyDict_Check(g))
4112 metaclass = PyDict_GetItemString(g, "__metaclass__");
4113 if (metaclass == NULL)
4114 metaclass = (PyObject *) &PyClass_Type;
4115 Py_INCREF(metaclass);
4116 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00004117 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004118 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004119 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004120 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004121 in a base that was not a class (such the random module
4122 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004123 by augmenting the error message with more information.*/
4124
4125 PyObject *ptype, *pvalue, *ptraceback;
4126
4127 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4128 if (PyString_Check(pvalue)) {
4129 PyObject *newmsg;
4130 newmsg = PyString_FromFormat(
4131 "Error when calling the metaclass bases\n %s",
4132 PyString_AS_STRING(pvalue));
4133 if (newmsg != NULL) {
4134 Py_DECREF(pvalue);
4135 pvalue = newmsg;
4136 }
4137 }
4138 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004139 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004140 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004141}
4142
Fredrik Lundh7a830892006-05-27 10:39:48 +00004143static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004144exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4145 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004147 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004149 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004150
Guido van Rossumb209a111997-04-29 18:18:01 +00004151 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4152 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004153 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004154 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004155 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004156 locals = PyTuple_GetItem(prog, 2);
4157 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004158 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004159 if (globals == Py_None) {
4160 globals = PyEval_GetGlobals();
4161 if (locals == Py_None) {
4162 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004163 plain = 1;
4164 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004165 if (!globals || !locals) {
4166 PyErr_SetString(PyExc_SystemError,
4167 "globals and locals cannot be NULL");
4168 return -1;
4169 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004171 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004173 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004174 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004175 !PyCode_Check(prog) &&
4176 !PyFile_Check(prog)) {
4177 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004178 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004179 return -1;
4180 }
Fred Drake661ea262000-10-24 19:57:45 +00004181 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004182 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004183 "exec: arg 2 must be a dictionary or None");
4184 return -1;
4185 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004186 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004187 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004188 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004189 return -1;
4190 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004191 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004192 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004193 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004194 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4195 PyErr_SetString(PyExc_TypeError,
4196 "code object passed to exec may not contain free variables");
4197 return -1;
4198 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004199 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004200 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004201 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004202 FILE *fp = PyFile_AsFile(prog);
4203 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004204 PyCompilerFlags cf;
Neal Norwitza5f5f142007-02-25 16:19:21 +00004205 if (name == NULL)
4206 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004207 cf.cf_flags = 0;
4208 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004209 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004210 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004211 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004212 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004213 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004214 }
4215 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004216 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004217 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004218 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004219 cf.cf_flags = 0;
4220#ifdef Py_USING_UNICODE
4221 if (PyUnicode_Check(prog)) {
4222 tmp = PyUnicode_AsUTF8String(prog);
4223 if (tmp == NULL)
4224 return -1;
4225 prog = tmp;
4226 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4227 }
4228#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004229 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004230 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004231 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004232 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004233 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004234 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004235 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004236 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004237 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004238 if (plain)
4239 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004240 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004241 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004242 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004243 return 0;
4244}
Guido van Rossum24c13741995-02-14 09:42:43 +00004245
Fredrik Lundh7a830892006-05-27 10:39:48 +00004246static void
Paul Prescode68140d2000-08-30 20:25:01 +00004247format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4248{
4249 char *obj_str;
4250
4251 if (!obj)
4252 return;
4253
4254 obj_str = PyString_AsString(obj);
4255 if (!obj_str)
4256 return;
4257
4258 PyErr_Format(exc, format_str, obj_str);
4259}
Guido van Rossum950361c1997-01-24 13:49:28 +00004260
Fredrik Lundh7a830892006-05-27 10:39:48 +00004261static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004262string_concatenate(PyObject *v, PyObject *w,
4263 PyFrameObject *f, unsigned char *next_instr)
4264{
4265 /* This function implements 'variable += expr' when both arguments
4266 are strings. */
Armin Rigo97ff0472006-08-09 15:37:26 +00004267 Py_ssize_t v_len = PyString_GET_SIZE(v);
4268 Py_ssize_t w_len = PyString_GET_SIZE(w);
4269 Py_ssize_t new_len = v_len + w_len;
4270 if (new_len < 0) {
4271 PyErr_SetString(PyExc_OverflowError,
4272 "strings are too large to concat");
4273 return NULL;
4274 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004275
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004276 if (v->ob_refcnt == 2) {
4277 /* In the common case, there are 2 references to the value
4278 * stored in 'variable' when the += is performed: one on the
4279 * value stack (in 'v') and one still stored in the 'variable'.
4280 * We try to delete the variable now to reduce the refcnt to 1.
4281 */
4282 switch (*next_instr) {
4283 case STORE_FAST:
4284 {
4285 int oparg = PEEKARG();
4286 PyObject **fastlocals = f->f_localsplus;
4287 if (GETLOCAL(oparg) == v)
4288 SETLOCAL(oparg, NULL);
4289 break;
4290 }
4291 case STORE_DEREF:
4292 {
Richard Jonescebbefc2006-05-23 18:28:17 +00004293 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004294 PyObject *c = freevars[PEEKARG()];
4295 if (PyCell_GET(c) == v)
4296 PyCell_Set(c, NULL);
4297 break;
4298 }
4299 case STORE_NAME:
4300 {
4301 PyObject *names = f->f_code->co_names;
4302 PyObject *name = GETITEM(names, PEEKARG());
4303 PyObject *locals = f->f_locals;
4304 if (PyDict_CheckExact(locals) &&
4305 PyDict_GetItem(locals, name) == v) {
4306 if (PyDict_DelItem(locals, name) != 0) {
4307 PyErr_Clear();
4308 }
4309 }
4310 break;
4311 }
4312 }
4313 }
4314
Armin Rigo618fbf52004-08-07 20:58:32 +00004315 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004316 /* Now we own the last reference to 'v', so we can resize it
4317 * in-place.
4318 */
Armin Rigo97ff0472006-08-09 15:37:26 +00004319 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004320 /* XXX if _PyString_Resize() fails, 'v' has been
4321 * deallocated so it cannot be put back into 'variable'.
4322 * The MemoryError is raised when there is no value in
4323 * 'variable', which might (very remotely) be a cause
4324 * of incompatibilities.
4325 */
4326 return NULL;
4327 }
4328 /* copy 'w' into the newly allocated area of 'v' */
4329 memcpy(PyString_AS_STRING(v) + v_len,
4330 PyString_AS_STRING(w), w_len);
4331 return v;
4332 }
4333 else {
4334 /* When in-place resizing is not an option. */
4335 PyString_Concat(&v, w);
4336 return v;
4337 }
4338}
4339
Guido van Rossum950361c1997-01-24 13:49:28 +00004340#ifdef DYNAMIC_EXECUTION_PROFILE
4341
Fredrik Lundh7a830892006-05-27 10:39:48 +00004342static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004343getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004344{
4345 int i;
4346 PyObject *l = PyList_New(256);
4347 if (l == NULL) return NULL;
4348 for (i = 0; i < 256; i++) {
4349 PyObject *x = PyInt_FromLong(a[i]);
4350 if (x == NULL) {
4351 Py_DECREF(l);
4352 return NULL;
4353 }
4354 PyList_SetItem(l, i, x);
4355 }
4356 for (i = 0; i < 256; i++)
4357 a[i] = 0;
4358 return l;
4359}
4360
4361PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004362_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004363{
4364#ifndef DXPAIRS
4365 return getarray(dxp);
4366#else
4367 int i;
4368 PyObject *l = PyList_New(257);
4369 if (l == NULL) return NULL;
4370 for (i = 0; i < 257; i++) {
4371 PyObject *x = getarray(dxpairs[i]);
4372 if (x == NULL) {
4373 Py_DECREF(l);
4374 return NULL;
4375 }
4376 PyList_SetItem(l, i, x);
4377 }
4378 return l;
4379#endif
4380}
4381
4382#endif