blob: d74149c4a936710fab3f8289b36c36ec669e2923 [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 */
Jeffrey Yasskin478a1aa2008-12-10 07:28:12 +00002266 if (u != Py_None)
2267 err = PyObject_IsTrue(x);
2268 else
2269 err = 0;
2270 Py_DECREF(x);
2271 if (err < 0)
2272 break; /* Go to error exit */
2273 else if (err > 0) {
2274 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002275 /* There was an exception and a true return */
Guido van Rossumf6694362006-03-10 02:28:35 +00002276 x = TOP(); /* Again */
2277 STACKADJ(-3);
2278 Py_INCREF(Py_None);
2279 SET_TOP(Py_None);
2280 Py_DECREF(x);
2281 Py_DECREF(u);
2282 Py_DECREF(v);
2283 Py_DECREF(w);
2284 } else {
2285 /* Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002286 x = POP();
2287 Py_DECREF(x);
2288 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002289 break;
2290 }
2291
Guido van Rossumf10570b1995-07-07 22:53:21 +00002292 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002293 {
2294 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002295 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002296 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002297#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002298 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002299#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002300 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002301#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002302 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002303 PUSH(x);
2304 if (x != NULL)
2305 continue;
2306 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Jeremy Hylton76901512000-03-28 23:49:17 +00002309 case CALL_FUNCTION_VAR:
2310 case CALL_FUNCTION_KW:
2311 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002312 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002313 int na = oparg & 0xff;
2314 int nk = (oparg>>8) & 0xff;
2315 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002316 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002317 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002318 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002319 if (flags & CALL_FLAG_VAR)
2320 n++;
2321 if (flags & CALL_FLAG_KW)
2322 n++;
2323 pfunc = stack_pointer - n - 1;
2324 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002325
Guido van Rossumac7be682001-01-17 15:42:30 +00002326 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002327 && PyMethod_GET_SELF(func) != NULL) {
2328 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002329 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002330 func = PyMethod_GET_FUNCTION(func);
2331 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002332 Py_DECREF(*pfunc);
2333 *pfunc = self;
2334 na++;
2335 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002336 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002337 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002338 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002339 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002340 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002341 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002342 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002343 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002344
Jeremy Hylton76901512000-03-28 23:49:17 +00002345 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002346 w = POP();
2347 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002348 }
2349 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002350 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002351 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002352 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002353 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002354
Guido van Rossum681d79a1995-07-18 14:51:37 +00002355 case MAKE_FUNCTION:
2356 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002357 x = PyFunction_New(v, f->f_globals);
2358 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002359 /* XXX Maybe this should be a separate opcode? */
2360 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002362 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002363 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364 x = NULL;
2365 break;
2366 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002367 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002368 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002369 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002370 }
2371 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002372 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002373 }
2374 PUSH(x);
2375 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002376
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002377 case MAKE_CLOSURE:
2378 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002379 v = POP(); /* code object */
2380 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002381 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 if (x != NULL) {
2383 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002384 err = PyFunction_SetClosure(x, v);
2385 Py_DECREF(v);
2386 }
2387 if (x != NULL && oparg > 0) {
2388 v = PyTuple_New(oparg);
2389 if (v == NULL) {
2390 Py_DECREF(x);
2391 x = NULL;
2392 break;
2393 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002394 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002395 w = POP();
2396 PyTuple_SET_ITEM(v, oparg, w);
2397 }
2398 err = PyFunction_SetDefaults(x, v);
2399 Py_DECREF(v);
2400 }
2401 PUSH(x);
2402 break;
2403 }
2404
Guido van Rossum8861b741996-07-30 16:49:37 +00002405 case BUILD_SLICE:
2406 if (oparg == 3)
2407 w = POP();
2408 else
2409 w = NULL;
2410 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002411 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002412 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002413 Py_DECREF(u);
2414 Py_DECREF(v);
2415 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002416 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002417 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002418 break;
2419
Fred Drakeef8ace32000-08-24 00:32:09 +00002420 case EXTENDED_ARG:
2421 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002422 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002423 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002424
Guido van Rossum374a9221991-04-04 10:40:29 +00002425 default:
2426 fprintf(stderr,
2427 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002428 PyCode_Addr2Line(f->f_code, f->f_lasti),
2429 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002430 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 why = WHY_EXCEPTION;
2432 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002433
2434#ifdef CASE_TOO_BIG
2435 }
2436#endif
2437
Guido van Rossum374a9221991-04-04 10:40:29 +00002438 } /* switch */
2439
2440 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002441
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002442 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002443
Guido van Rossum374a9221991-04-04 10:40:29 +00002444 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002445
Guido van Rossum374a9221991-04-04 10:40:29 +00002446 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002447 if (err == 0 && x != NULL) {
2448#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002449 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002450 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002451 fprintf(stderr,
2452 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002453 else {
2454#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002455 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002456 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002457#ifdef CHECKEXC
2458 }
2459#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002460 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002461 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002462 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002463 err = 0;
2464 }
2465
Guido van Rossum374a9221991-04-04 10:40:29 +00002466 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002467
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002468 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002470 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002471 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002472 why = WHY_EXCEPTION;
2473 }
2474 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002475#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002476 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002477 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002478 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002479 char buf[1024];
2480 sprintf(buf, "Stack unwind with exception "
2481 "set and why=%d", why);
2482 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002483 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002484 }
2485#endif
2486
2487 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002488
Guido van Rossum374a9221991-04-04 10:40:29 +00002489 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002490 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002491
Fred Drake8f51f542001-10-04 14:48:42 +00002492 if (tstate->c_tracefunc != NULL)
2493 call_exc_trace(tstate->c_tracefunc,
2494 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002496
Guido van Rossum374a9221991-04-04 10:40:29 +00002497 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002498
Guido van Rossum374a9221991-04-04 10:40:29 +00002499 if (why == WHY_RERAISE)
2500 why = WHY_EXCEPTION;
2501
2502 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002503
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002504fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002505 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002506 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002507
Tim Peters8a5c3c72004-04-05 19:36:21 +00002508 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002509 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2510 /* For a continue inside a try block,
2511 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002512 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2513 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002514 why = WHY_NOT;
2515 JUMPTO(PyInt_AS_LONG(retval));
2516 Py_DECREF(retval);
2517 break;
2518 }
2519
Guido van Rossum374a9221991-04-04 10:40:29 +00002520 while (STACK_LEVEL() > b->b_level) {
2521 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002522 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002523 }
2524 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2525 why = WHY_NOT;
2526 JUMPTO(b->b_handler);
2527 break;
2528 }
2529 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002530 (b->b_type == SETUP_EXCEPT &&
2531 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002532 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002533 PyObject *exc, *val, *tb;
2534 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002535 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002536 val = Py_None;
2537 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002538 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002539 /* Make the raw exception data
2540 available to the handler,
2541 so a program can emulate the
2542 Python main loop. Don't do
2543 this for 'finally'. */
2544 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002545 PyErr_NormalizeException(
2546 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002547 set_exc_info(tstate,
2548 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002549 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002550 if (tb == NULL) {
2551 Py_INCREF(Py_None);
2552 PUSH(Py_None);
2553 } else
2554 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002555 PUSH(val);
2556 PUSH(exc);
2557 }
2558 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002559 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002560 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002561 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002562 PUSH(v);
2563 }
2564 why = WHY_NOT;
2565 JUMPTO(b->b_handler);
2566 break;
2567 }
2568 } /* unwind stack */
2569
2570 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002571
Guido van Rossum374a9221991-04-04 10:40:29 +00002572 if (why != WHY_NOT)
2573 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002574 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002575
Guido van Rossum374a9221991-04-04 10:40:29 +00002576 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002577
Tim Peters8a5c3c72004-04-05 19:36:21 +00002578 assert(why != WHY_YIELD);
2579 /* Pop remaining stack entries. */
2580 while (!EMPTY()) {
2581 v = POP();
2582 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002583 }
2584
Tim Peters8a5c3c72004-04-05 19:36:21 +00002585 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002586 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002587
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002588fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002589 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002590 if (tstate->c_tracefunc) {
2591 if (why == WHY_RETURN || why == WHY_YIELD) {
2592 if (call_trace(tstate->c_tracefunc,
2593 tstate->c_traceobj, f,
2594 PyTrace_RETURN, retval)) {
2595 Py_XDECREF(retval);
2596 retval = NULL;
2597 why = WHY_EXCEPTION;
2598 }
2599 }
2600 else if (why == WHY_EXCEPTION) {
2601 call_trace_protected(tstate->c_tracefunc,
2602 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002603 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002604 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002605 }
Fred Drake8f51f542001-10-04 14:48:42 +00002606 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002607 if (why == WHY_EXCEPTION)
2608 call_trace_protected(tstate->c_profilefunc,
2609 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002610 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002611 else if (call_trace(tstate->c_profilefunc,
2612 tstate->c_profileobj, f,
2613 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002614 Py_XDECREF(retval);
2615 retval = NULL;
2616 why = WHY_EXCEPTION;
2617 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002618 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002619 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002620
Tim Peters7df5e7f2006-05-26 23:14:37 +00002621 if (tstate->frame->f_exc_type != NULL)
2622 reset_exc_info(tstate);
2623 else {
2624 assert(tstate->frame->f_exc_value == NULL);
2625 assert(tstate->frame->f_exc_traceback == NULL);
2626 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002627
Tim Peters5ca576e2001-06-18 22:08:13 +00002628 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002629 exit_eval_frame:
2630 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002631 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002632
Guido van Rossum96a42c81992-01-12 02:29:51 +00002633 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002634}
2635
Guido van Rossumc2e20742006-02-27 22:32:47 +00002636/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002637 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002638 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002639
Tim Peters6d6c1a32001-08-02 04:15:00 +00002640PyObject *
2641PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002642 PyObject **args, int argcount, PyObject **kws, int kwcount,
2643 PyObject **defs, int defcount, PyObject *closure)
2644{
2645 register PyFrameObject *f;
2646 register PyObject *retval = NULL;
2647 register PyObject **fastlocals, **freevars;
2648 PyThreadState *tstate = PyThreadState_GET();
2649 PyObject *x, *u;
2650
2651 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002652 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002653 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002654 return NULL;
2655 }
2656
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002657 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002658 assert(globals != NULL);
2659 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002660 if (f == NULL)
2661 return NULL;
2662
2663 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002664 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002665
2666 if (co->co_argcount > 0 ||
2667 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2668 int i;
2669 int n = argcount;
2670 PyObject *kwdict = NULL;
2671 if (co->co_flags & CO_VARKEYWORDS) {
2672 kwdict = PyDict_New();
2673 if (kwdict == NULL)
2674 goto fail;
2675 i = co->co_argcount;
2676 if (co->co_flags & CO_VARARGS)
2677 i++;
2678 SETLOCAL(i, kwdict);
2679 }
2680 if (argcount > co->co_argcount) {
2681 if (!(co->co_flags & CO_VARARGS)) {
2682 PyErr_Format(PyExc_TypeError,
2683 "%.200s() takes %s %d "
2684 "%sargument%s (%d given)",
2685 PyString_AsString(co->co_name),
2686 defcount ? "at most" : "exactly",
2687 co->co_argcount,
2688 kwcount ? "non-keyword " : "",
2689 co->co_argcount == 1 ? "" : "s",
2690 argcount);
2691 goto fail;
2692 }
2693 n = co->co_argcount;
2694 }
2695 for (i = 0; i < n; i++) {
2696 x = args[i];
2697 Py_INCREF(x);
2698 SETLOCAL(i, x);
2699 }
2700 if (co->co_flags & CO_VARARGS) {
2701 u = PyTuple_New(argcount - n);
2702 if (u == NULL)
2703 goto fail;
2704 SETLOCAL(co->co_argcount, u);
2705 for (i = n; i < argcount; i++) {
2706 x = args[i];
2707 Py_INCREF(x);
2708 PyTuple_SET_ITEM(u, i-n, x);
2709 }
2710 }
2711 for (i = 0; i < kwcount; i++) {
2712 PyObject *keyword = kws[2*i];
2713 PyObject *value = kws[2*i + 1];
2714 int j;
2715 if (keyword == NULL || !PyString_Check(keyword)) {
2716 PyErr_Format(PyExc_TypeError,
2717 "%.200s() keywords must be strings",
2718 PyString_AsString(co->co_name));
2719 goto fail;
2720 }
2721 /* XXX slow -- speed up using dictionary? */
2722 for (j = 0; j < co->co_argcount; j++) {
2723 PyObject *nm = PyTuple_GET_ITEM(
2724 co->co_varnames, j);
2725 int cmp = PyObject_RichCompareBool(
2726 keyword, nm, Py_EQ);
2727 if (cmp > 0)
2728 break;
2729 else if (cmp < 0)
2730 goto fail;
2731 }
2732 /* Check errors from Compare */
2733 if (PyErr_Occurred())
2734 goto fail;
2735 if (j >= co->co_argcount) {
2736 if (kwdict == NULL) {
2737 PyErr_Format(PyExc_TypeError,
2738 "%.200s() got an unexpected "
2739 "keyword argument '%.400s'",
2740 PyString_AsString(co->co_name),
2741 PyString_AsString(keyword));
2742 goto fail;
2743 }
2744 PyDict_SetItem(kwdict, keyword, value);
2745 }
2746 else {
2747 if (GETLOCAL(j) != NULL) {
2748 PyErr_Format(PyExc_TypeError,
2749 "%.200s() got multiple "
2750 "values for keyword "
2751 "argument '%.400s'",
2752 PyString_AsString(co->co_name),
2753 PyString_AsString(keyword));
2754 goto fail;
2755 }
2756 Py_INCREF(value);
2757 SETLOCAL(j, value);
2758 }
2759 }
2760 if (argcount < co->co_argcount) {
2761 int m = co->co_argcount - defcount;
2762 for (i = argcount; i < m; i++) {
2763 if (GETLOCAL(i) == NULL) {
2764 PyErr_Format(PyExc_TypeError,
2765 "%.200s() takes %s %d "
2766 "%sargument%s (%d given)",
2767 PyString_AsString(co->co_name),
2768 ((co->co_flags & CO_VARARGS) ||
2769 defcount) ? "at least"
2770 : "exactly",
2771 m, kwcount ? "non-keyword " : "",
2772 m == 1 ? "" : "s", i);
2773 goto fail;
2774 }
2775 }
2776 if (n > m)
2777 i = n - m;
2778 else
2779 i = 0;
2780 for (; i < defcount; i++) {
2781 if (GETLOCAL(m+i) == NULL) {
2782 PyObject *def = defs[i];
2783 Py_INCREF(def);
2784 SETLOCAL(m+i, def);
2785 }
2786 }
2787 }
2788 }
2789 else {
2790 if (argcount > 0 || kwcount > 0) {
2791 PyErr_Format(PyExc_TypeError,
2792 "%.200s() takes no arguments (%d given)",
2793 PyString_AsString(co->co_name),
2794 argcount + kwcount);
2795 goto fail;
2796 }
2797 }
2798 /* Allocate and initialize storage for cell vars, and copy free
2799 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002800 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002801 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002802 char *cellname, *argname;
2803 PyObject *c;
2804
2805 nargs = co->co_argcount;
2806 if (co->co_flags & CO_VARARGS)
2807 nargs++;
2808 if (co->co_flags & CO_VARKEYWORDS)
2809 nargs++;
2810
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 /* Initialize each cell var, taking into account
2812 cell vars that are initialized from arguments.
2813
2814 Should arrange for the compiler to put cellvars
2815 that are arguments at the beginning of the cellvars
2816 list so that we can march over it more efficiently?
2817 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002818 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002819 cellname = PyString_AS_STRING(
2820 PyTuple_GET_ITEM(co->co_cellvars, i));
2821 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002823 argname = PyString_AS_STRING(
2824 PyTuple_GET_ITEM(co->co_varnames, j));
2825 if (strcmp(cellname, argname) == 0) {
2826 c = PyCell_New(GETLOCAL(j));
2827 if (c == NULL)
2828 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002829 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002830 found = 1;
2831 break;
2832 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002833 }
2834 if (found == 0) {
2835 c = PyCell_New(NULL);
2836 if (c == NULL)
2837 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002838 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002839 }
2840 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002841 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002842 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002843 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002844 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002845 PyObject *o = PyTuple_GET_ITEM(closure, i);
2846 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002847 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002848 }
2849 }
2850
Tim Peters5ca576e2001-06-18 22:08:13 +00002851 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002852 /* Don't need to keep the reference to f_back, it will be set
2853 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002854 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002855 f->f_back = NULL;
2856
Jeremy Hylton985eba52003-02-05 23:13:00 +00002857 PCALL(PCALL_GENERATOR);
2858
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002859 /* Create a new generator that owns the ready to run frame
2860 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002861 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002862 }
2863
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002864 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002865
2866 fail: /* Jump here from prelude on failure */
2867
Tim Petersb13680b2001-11-27 23:29:29 +00002868 /* decref'ing the frame can cause __del__ methods to get invoked,
2869 which can call back into Python. While we're done with the
2870 current Python frame (f), the associated C stack is still in use,
2871 so recursion_depth must be boosted for the duration.
2872 */
2873 assert(tstate != NULL);
2874 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002875 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002876 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002877 return retval;
2878}
2879
2880
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002881/* Implementation notes for set_exc_info() and reset_exc_info():
2882
2883- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2884 'exc_traceback'. These always travel together.
2885
2886- tstate->curexc_ZZZ is the "hot" exception that is set by
2887 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2888
2889- Once an exception is caught by an except clause, it is transferred
2890 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2891 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00002892 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002893
2894- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2895
2896 Long ago, when none of this existed, there were just a few globals:
2897 one set corresponding to the "hot" exception, and one set
2898 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2899 globals; they were simply stored as sys.exc_ZZZ. For backwards
2900 compatibility, they still are!) The problem was that in code like
2901 this:
2902
2903 try:
2904 "something that may fail"
2905 except "some exception":
2906 "do something else first"
2907 "print the exception from sys.exc_ZZZ."
2908
2909 if "do something else first" invoked something that raised and caught
2910 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2911 cause of subtle bugs. I fixed this by changing the semantics as
2912 follows:
2913
2914 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2915 *in that frame*.
2916
2917 - But initially, and as long as no exception is caught in a given
2918 frame, sys.exc_ZZZ will hold the last exception caught in the
2919 previous frame (or the frame before that, etc.).
2920
2921 The first bullet fixed the bug in the above example. The second
2922 bullet was for backwards compatibility: it was (and is) common to
2923 have a function that is called when an exception is caught, and to
2924 have that function access the caught exception via sys.exc_ZZZ.
2925 (Example: traceback.print_exc()).
2926
2927 At the same time I fixed the problem that sys.exc_ZZZ weren't
2928 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2929 but that's really a separate improvement.
2930
2931 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2932 variables to what they were before the current frame was called. The
2933 set_exc_info() function saves them on the frame so that
2934 reset_exc_info() can restore them. The invariant is that
2935 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2936 exception (where "catching" an exception applies only to successful
2937 except clauses); and if the current frame ever caught an exception,
2938 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2939 at the start of the current frame.
2940
2941*/
2942
Fredrik Lundh7a830892006-05-27 10:39:48 +00002943static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002944set_exc_info(PyThreadState *tstate,
2945 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002946{
Tim Peters7df5e7f2006-05-26 23:14:37 +00002947 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002948 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002949
Tim Peters7df5e7f2006-05-26 23:14:37 +00002950 assert(type != NULL);
2951 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002952 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002953 assert(frame->f_exc_value == NULL);
2954 assert(frame->f_exc_traceback == NULL);
2955 /* This frame didn't catch an exception before. */
2956 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002957 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002958 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002959 Py_INCREF(Py_None);
2960 tstate->exc_type = Py_None;
2961 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002962 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002963 Py_XINCREF(tstate->exc_value);
2964 Py_XINCREF(tstate->exc_traceback);
2965 frame->f_exc_type = tstate->exc_type;
2966 frame->f_exc_value = tstate->exc_value;
2967 frame->f_exc_traceback = tstate->exc_traceback;
2968 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002969 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002970 tmp_type = tstate->exc_type;
2971 tmp_value = tstate->exc_value;
2972 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002973 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002974 Py_XINCREF(value);
2975 Py_XINCREF(tb);
2976 tstate->exc_type = type;
2977 tstate->exc_value = value;
2978 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002979 Py_XDECREF(tmp_type);
2980 Py_XDECREF(tmp_value);
2981 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002982 /* For b/w compatibility */
2983 PySys_SetObject("exc_type", type);
2984 PySys_SetObject("exc_value", value);
2985 PySys_SetObject("exc_traceback", tb);
2986}
2987
Fredrik Lundh7a830892006-05-27 10:39:48 +00002988static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002989reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002990{
2991 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002992 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002993
2994 /* It's a precondition that the thread state's frame caught an
2995 * exception -- verify in a debug build.
2996 */
2997 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002998 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002999 assert(frame != NULL);
3000 assert(frame->f_exc_type != NULL);
3001
3002 /* Copy the frame's exception info back to the thread state. */
3003 tmp_type = tstate->exc_type;
3004 tmp_value = tstate->exc_value;
3005 tmp_tb = tstate->exc_traceback;
3006 Py_INCREF(frame->f_exc_type);
3007 Py_XINCREF(frame->f_exc_value);
3008 Py_XINCREF(frame->f_exc_traceback);
3009 tstate->exc_type = frame->f_exc_type;
3010 tstate->exc_value = frame->f_exc_value;
3011 tstate->exc_traceback = frame->f_exc_traceback;
3012 Py_XDECREF(tmp_type);
3013 Py_XDECREF(tmp_value);
3014 Py_XDECREF(tmp_tb);
3015
3016 /* For b/w compatibility */
3017 PySys_SetObject("exc_type", frame->f_exc_type);
3018 PySys_SetObject("exc_value", frame->f_exc_value);
3019 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3020
3021 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003022 tmp_type = frame->f_exc_type;
3023 tmp_value = frame->f_exc_value;
3024 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003025 frame->f_exc_type = NULL;
3026 frame->f_exc_value = NULL;
3027 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003028 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003029 Py_XDECREF(tmp_value);
3030 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003031}
3032
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003033/* Logic for the raise statement (too complicated for inlining).
3034 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003035static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003036do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003037{
Guido van Rossumd295f121998-04-09 21:39:57 +00003038 if (type == NULL) {
3039 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003040 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003041 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3042 value = tstate->exc_value;
3043 tb = tstate->exc_traceback;
3044 Py_XINCREF(type);
3045 Py_XINCREF(value);
3046 Py_XINCREF(tb);
3047 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003048
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003049 /* We support the following forms of raise:
3050 raise <class>, <classinstance>
3051 raise <class>, <argument tuple>
3052 raise <class>, None
3053 raise <class>, <argument>
3054 raise <classinstance>, None
3055 raise <string>, <object>
3056 raise <string>, None
3057
3058 An omitted second argument is the same as None.
3059
3060 In addition, raise <tuple>, <anything> is the same as
3061 raising the tuple's first item (and it better have one!);
3062 this rule is applied recursively.
3063
3064 Finally, an optional third argument can be supplied, which
3065 gives the traceback to be substituted (useful when
3066 re-raising an exception after examining it). */
3067
3068 /* First, check the traceback argument, replacing None with
3069 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003070 if (tb == Py_None) {
3071 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003072 tb = NULL;
3073 }
3074 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003075 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003076 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003077 goto raise_error;
3078 }
3079
3080 /* Next, replace a missing value with None */
3081 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003082 value = Py_None;
3083 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003084 }
3085
3086 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003087 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3088 PyObject *tmp = type;
3089 type = PyTuple_GET_ITEM(type, 0);
3090 Py_INCREF(type);
3091 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003092 }
3093
Brett Cannona7446e32006-02-27 23:39:10 +00003094 if (PyString_CheckExact(type)) {
Tim Petersafb2c802002-04-18 18:06:20 +00003095 /* Raising builtin string is deprecated but still allowed --
3096 * do nothing. Raising an instance of a new-style str
3097 * subclass is right out. */
Brett Cannonbf364092006-03-01 04:25:17 +00003098 if (PyErr_Warn(PyExc_DeprecationWarning,
Brett Cannona7446e32006-02-27 23:39:10 +00003099 "raising a string exception is deprecated"))
3100 goto raise_error;
3101 }
Brett Cannonbf364092006-03-01 04:25:17 +00003102 else if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003103 PyErr_NormalizeException(&type, &value, &tb);
3104
Brett Cannonbf364092006-03-01 04:25:17 +00003105 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003106 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003107 if (value != Py_None) {
3108 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003109 "instance exception may not have a separate value");
3110 goto raise_error;
3111 }
3112 else {
3113 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003114 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003115 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003116 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003117 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003118 }
3119 }
3120 else {
3121 /* Not something you can raise. You get an exception
3122 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003123 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00003124 "exceptions must be classes, instances, or "
3125 "strings (deprecated), not %s",
3126 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003127 goto raise_error;
3128 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003129 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003130 if (tb == NULL)
3131 return WHY_EXCEPTION;
3132 else
3133 return WHY_RERAISE;
3134 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003135 Py_XDECREF(value);
3136 Py_XDECREF(type);
3137 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003138 return WHY_EXCEPTION;
3139}
3140
Tim Petersd6d010b2001-06-21 02:49:55 +00003141/* Iterate v argcnt times and store the results on the stack (via decreasing
3142 sp). Return 1 for success, 0 if error. */
3143
Fredrik Lundh7a830892006-05-27 10:39:48 +00003144static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003145unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003146{
Tim Petersd6d010b2001-06-21 02:49:55 +00003147 int i = 0;
3148 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003149 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003150
Tim Petersd6d010b2001-06-21 02:49:55 +00003151 assert(v != NULL);
3152
3153 it = PyObject_GetIter(v);
3154 if (it == NULL)
3155 goto Error;
3156
3157 for (; i < argcnt; i++) {
3158 w = PyIter_Next(it);
3159 if (w == NULL) {
3160 /* Iterator done, via error or exhaustion. */
3161 if (!PyErr_Occurred()) {
3162 PyErr_Format(PyExc_ValueError,
3163 "need more than %d value%s to unpack",
3164 i, i == 1 ? "" : "s");
3165 }
3166 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003167 }
3168 *--sp = w;
3169 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003170
3171 /* We better have exhausted the iterator now. */
3172 w = PyIter_Next(it);
3173 if (w == NULL) {
3174 if (PyErr_Occurred())
3175 goto Error;
3176 Py_DECREF(it);
3177 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003178 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003179 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003180 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003181 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003182Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003183 for (; i > 0; i--, sp++)
3184 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003185 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003186 return 0;
3187}
3188
3189
Guido van Rossum96a42c81992-01-12 02:29:51 +00003190#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003191static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003192prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003193{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003194 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003195 if (PyObject_Print(v, stdout, 0) != 0)
3196 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003197 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003198 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003199}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003200#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003201
Fredrik Lundh7a830892006-05-27 10:39:48 +00003202static void
Fred Drake5755ce62001-06-27 19:19:46 +00003203call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003204{
Guido van Rossumb209a111997-04-29 18:18:01 +00003205 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003206 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003207 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003208 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003209 value = Py_None;
3210 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003211 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003212 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003213 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003214 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003215 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003216 }
Fred Drake5755ce62001-06-27 19:19:46 +00003217 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003218 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003219 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003220 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003221 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003222 Py_XDECREF(type);
3223 Py_XDECREF(value);
3224 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003225 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003226}
3227
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003228static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003229call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003230 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003231{
3232 PyObject *type, *value, *traceback;
3233 int err;
3234 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003235 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003236 if (err == 0)
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003237 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003238 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003239 return 0;
3240 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003241 else {
3242 Py_XDECREF(type);
3243 Py_XDECREF(value);
3244 Py_XDECREF(traceback);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003245 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003246 }
3247}
3248
Fredrik Lundh7a830892006-05-27 10:39:48 +00003249static int
Fred Drake5755ce62001-06-27 19:19:46 +00003250call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3251 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003252{
Fred Drake5755ce62001-06-27 19:19:46 +00003253 register PyThreadState *tstate = frame->f_tstate;
3254 int result;
3255 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003256 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003257 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003258 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003259 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003260 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3261 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003262 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003263 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003264}
3265
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003266PyObject *
3267_PyEval_CallTracing(PyObject *func, PyObject *args)
3268{
3269 PyFrameObject *frame = PyEval_GetFrame();
3270 PyThreadState *tstate = frame->f_tstate;
3271 int save_tracing = tstate->tracing;
3272 int save_use_tracing = tstate->use_tracing;
3273 PyObject *result;
3274
3275 tstate->tracing = 0;
3276 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3277 || (tstate->c_profilefunc != NULL));
3278 result = PyObject_Call(func, args, NULL);
3279 tstate->tracing = save_tracing;
3280 tstate->use_tracing = save_use_tracing;
3281 return result;
3282}
3283
Fredrik Lundh7a830892006-05-27 10:39:48 +00003284static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003285maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003286 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3287 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003288{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003289 int result = 0;
3290
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003291 /* If the last instruction executed isn't in the current
3292 instruction window, reset the window. If the last
3293 instruction happens to fall at the start of a line or if it
3294 represents a jump backwards, call the trace function.
3295 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003296 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003297 int line;
3298 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003299
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003300 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3301 &bounds);
3302 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003303 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003304 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003305 PyTrace_LINE, Py_None);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003306 }
3307 *instr_lb = bounds.ap_lower;
3308 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003309 }
Armin Rigobf57a142004-03-22 19:24:58 +00003310 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003311 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003312 }
3313 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003314 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003315}
3316
Fred Drake5755ce62001-06-27 19:19:46 +00003317void
3318PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003319{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003320 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003321 PyObject *temp = tstate->c_profileobj;
3322 Py_XINCREF(arg);
3323 tstate->c_profilefunc = NULL;
3324 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003325 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003326 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003327 Py_XDECREF(temp);
3328 tstate->c_profilefunc = func;
3329 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003330 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003331 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003332}
3333
3334void
3335PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3336{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003337 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003338 PyObject *temp = tstate->c_traceobj;
3339 Py_XINCREF(arg);
3340 tstate->c_tracefunc = NULL;
3341 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003342 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003343 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003344 Py_XDECREF(temp);
3345 tstate->c_tracefunc = func;
3346 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003347 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003348 tstate->use_tracing = ((func != NULL)
3349 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003350}
3351
Guido van Rossumb209a111997-04-29 18:18:01 +00003352PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003353PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003354{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003355 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003356 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003357 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003358 else
3359 return current_frame->f_builtins;
3360}
3361
Guido van Rossumb209a111997-04-29 18:18:01 +00003362PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003364{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003365 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003366 if (current_frame == NULL)
3367 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003368 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003369 return current_frame->f_locals;
3370}
3371
Guido van Rossumb209a111997-04-29 18:18:01 +00003372PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003373PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003374{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003375 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003376 if (current_frame == NULL)
3377 return NULL;
3378 else
3379 return current_frame->f_globals;
3380}
3381
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003382PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003383PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003384{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003385 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003386 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003387}
3388
Guido van Rossum6135a871995-01-09 17:53:26 +00003389int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003390PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003391{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003392 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003393 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003394}
3395
Guido van Rossumbe270261997-05-22 22:26:18 +00003396int
Tim Peters5ba58662001-07-16 02:29:45 +00003397PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003398{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003399 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003400 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003401
3402 if (current_frame != NULL) {
3403 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003404 const int compilerflags = codeflags & PyCF_MASK;
3405 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003406 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003407 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003408 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003409#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003410 if (codeflags & CO_GENERATOR_ALLOWED) {
3411 result = 1;
3412 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3413 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003414#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003415 }
3416 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003417}
3418
3419int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003420Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003421{
Guido van Rossumb209a111997-04-29 18:18:01 +00003422 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003423 if (f == NULL)
3424 return 0;
3425 if (!PyFile_SoftSpace(f, 0))
3426 return 0;
3427 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003428}
3429
Guido van Rossum3f5da241990-12-20 15:06:42 +00003430
Guido van Rossum681d79a1995-07-18 14:51:37 +00003431/* External interface to call any callable object.
3432 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003433
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003434#undef PyEval_CallObject
3435/* for backward compatibility: export this interface */
3436
Guido van Rossumb209a111997-04-29 18:18:01 +00003437PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003438PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003439{
Guido van Rossumb209a111997-04-29 18:18:01 +00003440 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003441}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003442#define PyEval_CallObject(func,arg) \
3443 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003444
Guido van Rossumb209a111997-04-29 18:18:01 +00003445PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003446PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003447{
Jeremy Hylton52820442001-01-03 23:52:36 +00003448 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003449
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003450 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003451 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003452 if (arg == NULL)
3453 return NULL;
3454 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003455 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003456 PyErr_SetString(PyExc_TypeError,
3457 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003458 return NULL;
3459 }
3460 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003461 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003462
Guido van Rossumb209a111997-04-29 18:18:01 +00003463 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003464 PyErr_SetString(PyExc_TypeError,
3465 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003466 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003467 return NULL;
3468 }
3469
Tim Peters6d6c1a32001-08-02 04:15:00 +00003470 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003471 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003472 return result;
3473}
3474
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003475const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003476PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003477{
3478 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003479 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003480 else if (PyFunction_Check(func))
3481 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3482 else if (PyCFunction_Check(func))
3483 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3484 else if (PyClass_Check(func))
3485 return PyString_AsString(((PyClassObject*)func)->cl_name);
3486 else if (PyInstance_Check(func)) {
3487 return PyString_AsString(
3488 ((PyInstanceObject*)func)->in_class->cl_name);
3489 } else {
3490 return func->ob_type->tp_name;
3491 }
3492}
3493
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003494const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003495PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003496{
3497 if (PyMethod_Check(func))
3498 return "()";
3499 else if (PyFunction_Check(func))
3500 return "()";
3501 else if (PyCFunction_Check(func))
3502 return "()";
3503 else if (PyClass_Check(func))
3504 return " constructor";
3505 else if (PyInstance_Check(func)) {
3506 return " instance";
3507 } else {
3508 return " object";
3509 }
3510}
3511
Fredrik Lundh7a830892006-05-27 10:39:48 +00003512static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003513err_args(PyObject *func, int flags, int nargs)
3514{
3515 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003516 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003517 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003518 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003519 nargs);
3520 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003521 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003522 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003523 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003524 nargs);
3525}
3526
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003527#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003528if (tstate->use_tracing && tstate->c_profilefunc) { \
3529 if (call_trace(tstate->c_profilefunc, \
3530 tstate->c_profileobj, \
3531 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003532 func)) { \
3533 x = NULL; \
3534 } \
3535 else { \
3536 x = call; \
3537 if (tstate->c_profilefunc != NULL) { \
3538 if (x == NULL) { \
3539 call_trace_protected(tstate->c_profilefunc, \
3540 tstate->c_profileobj, \
3541 tstate->frame, PyTrace_C_EXCEPTION, \
3542 func); \
3543 /* XXX should pass (type, value, tb) */ \
3544 } else { \
3545 if (call_trace(tstate->c_profilefunc, \
3546 tstate->c_profileobj, \
3547 tstate->frame, PyTrace_C_RETURN, \
3548 func)) { \
3549 Py_DECREF(x); \
3550 x = NULL; \
3551 } \
3552 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003553 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003554 } \
3555} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003556 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003557 }
3558
Fredrik Lundh7a830892006-05-27 10:39:48 +00003559static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003560call_function(PyObject ***pp_stack, int oparg
3561#ifdef WITH_TSC
3562 , uint64* pintr0, uint64* pintr1
3563#endif
3564 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003565{
3566 int na = oparg & 0xff;
3567 int nk = (oparg>>8) & 0xff;
3568 int n = na + 2 * nk;
3569 PyObject **pfunc = (*pp_stack) - n - 1;
3570 PyObject *func = *pfunc;
3571 PyObject *x, *w;
3572
Jeremy Hylton985eba52003-02-05 23:13:00 +00003573 /* Always dispatch PyCFunction first, because these are
3574 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003575 */
3576 if (PyCFunction_Check(func) && nk == 0) {
3577 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003578 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003579
3580 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003581 if (flags & (METH_NOARGS | METH_O)) {
3582 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3583 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003584 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003585 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003586 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003587 else if (flags & METH_O && na == 1) {
3588 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003589 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003590 Py_DECREF(arg);
3591 }
3592 else {
3593 err_args(func, flags, na);
3594 x = NULL;
3595 }
3596 }
3597 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003598 PyObject *callargs;
3599 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003600 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003601 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003602 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003603 Py_XDECREF(callargs);
3604 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003605 } else {
3606 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3607 /* optimize access to bound methods */
3608 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003609 PCALL(PCALL_METHOD);
3610 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003611 Py_INCREF(self);
3612 func = PyMethod_GET_FUNCTION(func);
3613 Py_INCREF(func);
3614 Py_DECREF(*pfunc);
3615 *pfunc = self;
3616 na++;
3617 n++;
3618 } else
3619 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003620 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003621 if (PyFunction_Check(func))
3622 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003623 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003624 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003625 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003626 Py_DECREF(func);
3627 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003628
Armin Rigod34fa522006-03-28 19:10:40 +00003629 /* Clear the stack of the function object. Also removes
3630 the arguments in case they weren't consumed already
3631 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003632 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003633 while ((*pp_stack) > pfunc) {
3634 w = EXT_POP(*pp_stack);
3635 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003636 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003637 }
3638 return x;
3639}
3640
Jeremy Hylton192690e2002-08-16 18:36:11 +00003641/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003642 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003643 For the simplest case -- a function that takes only positional
3644 arguments and is called with only positional arguments -- it
3645 inlines the most primitive frame setup code from
3646 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3647 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003648*/
3649
Fredrik Lundh7a830892006-05-27 10:39:48 +00003650static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003651fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003652{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003653 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003654 PyObject *globals = PyFunction_GET_GLOBALS(func);
3655 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3656 PyObject **d = NULL;
3657 int nd = 0;
3658
Jeremy Hylton985eba52003-02-05 23:13:00 +00003659 PCALL(PCALL_FUNCTION);
3660 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003661 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003662 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3663 PyFrameObject *f;
3664 PyObject *retval = NULL;
3665 PyThreadState *tstate = PyThreadState_GET();
3666 PyObject **fastlocals, **stack;
3667 int i;
3668
3669 PCALL(PCALL_FASTER_FUNCTION);
3670 assert(globals != NULL);
3671 /* XXX Perhaps we should create a specialized
3672 PyFrame_New() that doesn't take locals, but does
3673 take builtins without sanity checking them.
3674 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003675 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003676 f = PyFrame_New(tstate, co, globals, NULL);
3677 if (f == NULL)
3678 return NULL;
3679
3680 fastlocals = f->f_localsplus;
3681 stack = (*pp_stack) - n;
3682
3683 for (i = 0; i < n; i++) {
3684 Py_INCREF(*stack);
3685 fastlocals[i] = *stack++;
3686 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003687 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003688 ++tstate->recursion_depth;
3689 Py_DECREF(f);
3690 --tstate->recursion_depth;
3691 return retval;
3692 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003693 if (argdefs != NULL) {
3694 d = &PyTuple_GET_ITEM(argdefs, 0);
3695 nd = ((PyTupleObject *)argdefs)->ob_size;
3696 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003697 return PyEval_EvalCodeEx(co, globals,
3698 (PyObject *)NULL, (*pp_stack)-n, na,
3699 (*pp_stack)-2*nk, nk, d, nd,
3700 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003701}
3702
Fredrik Lundh7a830892006-05-27 10:39:48 +00003703static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003704update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3705 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003706{
3707 PyObject *kwdict = NULL;
3708 if (orig_kwdict == NULL)
3709 kwdict = PyDict_New();
3710 else {
3711 kwdict = PyDict_Copy(orig_kwdict);
3712 Py_DECREF(orig_kwdict);
3713 }
3714 if (kwdict == NULL)
3715 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003716 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003717 int err;
3718 PyObject *value = EXT_POP(*pp_stack);
3719 PyObject *key = EXT_POP(*pp_stack);
3720 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003721 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003722 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003723 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003724 PyEval_GetFuncName(func),
3725 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003726 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 Py_DECREF(key);
3728 Py_DECREF(value);
3729 Py_DECREF(kwdict);
3730 return NULL;
3731 }
3732 err = PyDict_SetItem(kwdict, key, value);
3733 Py_DECREF(key);
3734 Py_DECREF(value);
3735 if (err) {
3736 Py_DECREF(kwdict);
3737 return NULL;
3738 }
3739 }
3740 return kwdict;
3741}
3742
Fredrik Lundh7a830892006-05-27 10:39:48 +00003743static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003744update_star_args(int nstack, int nstar, PyObject *stararg,
3745 PyObject ***pp_stack)
3746{
3747 PyObject *callargs, *w;
3748
3749 callargs = PyTuple_New(nstack + nstar);
3750 if (callargs == NULL) {
3751 return NULL;
3752 }
3753 if (nstar) {
3754 int i;
3755 for (i = 0; i < nstar; i++) {
3756 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3757 Py_INCREF(a);
3758 PyTuple_SET_ITEM(callargs, nstack + i, a);
3759 }
3760 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003761 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003762 w = EXT_POP(*pp_stack);
3763 PyTuple_SET_ITEM(callargs, nstack, w);
3764 }
3765 return callargs;
3766}
3767
Fredrik Lundh7a830892006-05-27 10:39:48 +00003768static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003769load_args(PyObject ***pp_stack, int na)
3770{
3771 PyObject *args = PyTuple_New(na);
3772 PyObject *w;
3773
3774 if (args == NULL)
3775 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003776 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003777 w = EXT_POP(*pp_stack);
3778 PyTuple_SET_ITEM(args, na, w);
3779 }
3780 return args;
3781}
3782
Fredrik Lundh7a830892006-05-27 10:39:48 +00003783static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003784do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3785{
3786 PyObject *callargs = NULL;
3787 PyObject *kwdict = NULL;
3788 PyObject *result = NULL;
3789
3790 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003791 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003792 if (kwdict == NULL)
3793 goto call_fail;
3794 }
3795 callargs = load_args(pp_stack, na);
3796 if (callargs == NULL)
3797 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003798#ifdef CALL_PROFILE
3799 /* At this point, we have to look at the type of func to
3800 update the call stats properly. Do it here so as to avoid
3801 exposing the call stats machinery outside ceval.c
3802 */
3803 if (PyFunction_Check(func))
3804 PCALL(PCALL_FUNCTION);
3805 else if (PyMethod_Check(func))
3806 PCALL(PCALL_METHOD);
3807 else if (PyType_Check(func))
3808 PCALL(PCALL_TYPE);
3809 else
3810 PCALL(PCALL_OTHER);
3811#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003812 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003813 call_fail:
3814 Py_XDECREF(callargs);
3815 Py_XDECREF(kwdict);
3816 return result;
3817}
3818
Fredrik Lundh7a830892006-05-27 10:39:48 +00003819static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003820ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3821{
3822 int nstar = 0;
3823 PyObject *callargs = NULL;
3824 PyObject *stararg = NULL;
3825 PyObject *kwdict = NULL;
3826 PyObject *result = NULL;
3827
3828 if (flags & CALL_FLAG_KW) {
3829 kwdict = EXT_POP(*pp_stack);
3830 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003831 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003832 "%s%s argument after ** "
3833 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003834 PyEval_GetFuncName(func),
3835 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003836 goto ext_call_fail;
3837 }
3838 }
3839 if (flags & CALL_FLAG_VAR) {
3840 stararg = EXT_POP(*pp_stack);
3841 if (!PyTuple_Check(stararg)) {
3842 PyObject *t = NULL;
3843 t = PySequence_Tuple(stararg);
3844 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003845 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3846 PyErr_Format(PyExc_TypeError,
3847 "%s%s argument after * "
3848 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003849 PyEval_GetFuncName(func),
3850 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003851 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003852 goto ext_call_fail;
3853 }
3854 Py_DECREF(stararg);
3855 stararg = t;
3856 }
3857 nstar = PyTuple_GET_SIZE(stararg);
3858 }
3859 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003860 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003861 if (kwdict == NULL)
3862 goto ext_call_fail;
3863 }
3864 callargs = update_star_args(na, nstar, stararg, pp_stack);
3865 if (callargs == NULL)
3866 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003867#ifdef CALL_PROFILE
3868 /* At this point, we have to look at the type of func to
3869 update the call stats properly. Do it here so as to avoid
3870 exposing the call stats machinery outside ceval.c
3871 */
3872 if (PyFunction_Check(func))
3873 PCALL(PCALL_FUNCTION);
3874 else if (PyMethod_Check(func))
3875 PCALL(PCALL_METHOD);
3876 else if (PyType_Check(func))
3877 PCALL(PCALL_TYPE);
3878 else
3879 PCALL(PCALL_OTHER);
3880#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003881 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003882 ext_call_fail:
3883 Py_XDECREF(callargs);
3884 Py_XDECREF(kwdict);
3885 Py_XDECREF(stararg);
3886 return result;
3887}
3888
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003889/* Extract a slice index from a PyInt or PyLong or an object with the
3890 nb_index slot defined, and store in *pi.
3891 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3892 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 +00003893 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003894*/
Tim Petersb5196382001-12-16 19:44:20 +00003895/* Note: If v is NULL, return success without storing into *pi. This
3896 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3897 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003898*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003899int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003900_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003901{
Tim Petersb5196382001-12-16 19:44:20 +00003902 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003903 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003904 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003905 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3906 however, it looks like it should be AsSsize_t.
3907 There should be a comment here explaining why.
3908 */
3909 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003910 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003911 else if (PyIndex_Check(v)) {
3912 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003913 if (x == -1 && PyErr_Occurred())
3914 return 0;
3915 }
3916 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003917 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003918 "slice indices must be integers or "
3919 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003920 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003921 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003922 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003923 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003924 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003925}
3926
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003927#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003928#define ISINDEX(x) ((x) == NULL || \
3929 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003930
Fredrik Lundh7a830892006-05-27 10:39:48 +00003931static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003932apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003933{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003934 PyTypeObject *tp = u->ob_type;
3935 PySequenceMethods *sq = tp->tp_as_sequence;
3936
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003937 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003938 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003939 if (!_PyEval_SliceIndex(v, &ilow))
3940 return NULL;
3941 if (!_PyEval_SliceIndex(w, &ihigh))
3942 return NULL;
3943 return PySequence_GetSlice(u, ilow, ihigh);
3944 }
3945 else {
3946 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003947 if (slice != NULL) {
3948 PyObject *res = PyObject_GetItem(u, slice);
3949 Py_DECREF(slice);
3950 return res;
3951 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003952 else
3953 return NULL;
3954 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003956
Fredrik Lundh7a830892006-05-27 10:39:48 +00003957static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003958assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3959 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003960{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003961 PyTypeObject *tp = u->ob_type;
3962 PySequenceMethods *sq = tp->tp_as_sequence;
3963
Georg Brandl0ea89162007-03-05 22:28:13 +00003964 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003965 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003966 if (!_PyEval_SliceIndex(v, &ilow))
3967 return -1;
3968 if (!_PyEval_SliceIndex(w, &ihigh))
3969 return -1;
3970 if (x == NULL)
3971 return PySequence_DelSlice(u, ilow, ihigh);
3972 else
3973 return PySequence_SetSlice(u, ilow, ihigh, x);
3974 }
3975 else {
3976 PyObject *slice = PySlice_New(v, w, NULL);
3977 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003978 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003979 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003980 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003981 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003982 res = PyObject_DelItem(u, slice);
3983 Py_DECREF(slice);
3984 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003985 }
3986 else
3987 return -1;
3988 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989}
3990
Fredrik Lundh7a830892006-05-27 10:39:48 +00003991static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003992cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003993{
Guido van Rossumac7be682001-01-17 15:42:30 +00003994 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003995 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003996 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003997 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003998 break;
3999 case PyCmp_IS_NOT:
4000 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004001 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004002 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004003 res = PySequence_Contains(w, v);
4004 if (res < 0)
4005 return NULL;
4006 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004007 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004008 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004009 if (res < 0)
4010 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004011 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004012 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004013 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004014 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004015 break;
4016 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004017 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004018 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004019 v = res ? Py_True : Py_False;
4020 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004021 return v;
4022}
4023
Fredrik Lundh7a830892006-05-27 10:39:48 +00004024static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004025import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004026{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004027 PyObject *x;
4028
4029 x = PyObject_GetAttr(v, name);
4030 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004031 PyErr_Format(PyExc_ImportError,
4032 "cannot import name %.230s",
4033 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004034 }
Thomas Wouters52152252000-08-17 22:55:00 +00004035 return x;
4036}
Guido van Rossumac7be682001-01-17 15:42:30 +00004037
Fredrik Lundh7a830892006-05-27 10:39:48 +00004038static int
Thomas Wouters52152252000-08-17 22:55:00 +00004039import_all_from(PyObject *locals, PyObject *v)
4040{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004041 PyObject *all = PyObject_GetAttrString(v, "__all__");
4042 PyObject *dict, *name, *value;
4043 int skip_leading_underscores = 0;
4044 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004045
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004046 if (all == NULL) {
4047 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4048 return -1; /* Unexpected error */
4049 PyErr_Clear();
4050 dict = PyObject_GetAttrString(v, "__dict__");
4051 if (dict == NULL) {
4052 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4053 return -1;
4054 PyErr_SetString(PyExc_ImportError,
4055 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004056 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004057 }
4058 all = PyMapping_Keys(dict);
4059 Py_DECREF(dict);
4060 if (all == NULL)
4061 return -1;
4062 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004063 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004064
4065 for (pos = 0, err = 0; ; pos++) {
4066 name = PySequence_GetItem(all, pos);
4067 if (name == NULL) {
4068 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4069 err = -1;
4070 else
4071 PyErr_Clear();
4072 break;
4073 }
4074 if (skip_leading_underscores &&
4075 PyString_Check(name) &&
4076 PyString_AS_STRING(name)[0] == '_')
4077 {
4078 Py_DECREF(name);
4079 continue;
4080 }
4081 value = PyObject_GetAttr(v, name);
4082 if (value == NULL)
4083 err = -1;
Armin Rigo1bc1ab22006-11-29 22:07:38 +00004084 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004085 err = PyDict_SetItem(locals, name, value);
Armin Rigo1bc1ab22006-11-29 22:07:38 +00004086 else
4087 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004088 Py_DECREF(name);
4089 Py_XDECREF(value);
4090 if (err != 0)
4091 break;
4092 }
4093 Py_DECREF(all);
4094 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004095}
4096
Fredrik Lundh7a830892006-05-27 10:39:48 +00004097static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004098build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004099{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004100 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004101
4102 if (PyDict_Check(methods))
4103 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004104 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004105 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004106 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4107 base = PyTuple_GET_ITEM(bases, 0);
4108 metaclass = PyObject_GetAttrString(base, "__class__");
4109 if (metaclass == NULL) {
4110 PyErr_Clear();
4111 metaclass = (PyObject *)base->ob_type;
4112 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004113 }
4114 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004115 else {
4116 PyObject *g = PyEval_GetGlobals();
4117 if (g != NULL && PyDict_Check(g))
4118 metaclass = PyDict_GetItemString(g, "__metaclass__");
4119 if (metaclass == NULL)
4120 metaclass = (PyObject *) &PyClass_Type;
4121 Py_INCREF(metaclass);
4122 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00004123 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004124 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004125 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004126 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004127 in a base that was not a class (such the random module
4128 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004129 by augmenting the error message with more information.*/
4130
4131 PyObject *ptype, *pvalue, *ptraceback;
4132
4133 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4134 if (PyString_Check(pvalue)) {
4135 PyObject *newmsg;
4136 newmsg = PyString_FromFormat(
4137 "Error when calling the metaclass bases\n %s",
4138 PyString_AS_STRING(pvalue));
4139 if (newmsg != NULL) {
4140 Py_DECREF(pvalue);
4141 pvalue = newmsg;
4142 }
4143 }
4144 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004145 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004146 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004147}
4148
Fredrik Lundh7a830892006-05-27 10:39:48 +00004149static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004150exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4151 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004152{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004153 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004154 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004155 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004156
Guido van Rossumb209a111997-04-29 18:18:01 +00004157 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4158 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004159 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004160 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004161 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004162 locals = PyTuple_GetItem(prog, 2);
4163 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004164 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004165 if (globals == Py_None) {
4166 globals = PyEval_GetGlobals();
4167 if (locals == Py_None) {
4168 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004169 plain = 1;
4170 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004171 if (!globals || !locals) {
4172 PyErr_SetString(PyExc_SystemError,
4173 "globals and locals cannot be NULL");
4174 return -1;
4175 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004176 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004177 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004178 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004179 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004180 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004181 !PyCode_Check(prog) &&
4182 !PyFile_Check(prog)) {
4183 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004184 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004185 return -1;
4186 }
Fred Drake661ea262000-10-24 19:57:45 +00004187 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004188 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004189 "exec: arg 2 must be a dictionary or None");
4190 return -1;
4191 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004192 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004193 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004194 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004195 return -1;
4196 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004197 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004198 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004199 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004200 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4201 PyErr_SetString(PyExc_TypeError,
4202 "code object passed to exec may not contain free variables");
4203 return -1;
4204 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004205 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004206 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004207 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004208 FILE *fp = PyFile_AsFile(prog);
4209 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004210 PyCompilerFlags cf;
Neal Norwitza5f5f142007-02-25 16:19:21 +00004211 if (name == NULL)
4212 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004213 cf.cf_flags = 0;
4214 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004215 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004216 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004217 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004218 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004219 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004220 }
4221 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004222 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004223 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004224 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004225 cf.cf_flags = 0;
4226#ifdef Py_USING_UNICODE
4227 if (PyUnicode_Check(prog)) {
4228 tmp = PyUnicode_AsUTF8String(prog);
4229 if (tmp == NULL)
4230 return -1;
4231 prog = tmp;
4232 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4233 }
4234#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004235 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004236 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004237 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004238 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004239 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004240 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004241 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004242 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004243 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004244 if (plain)
4245 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004246 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004247 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004248 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004249 return 0;
4250}
Guido van Rossum24c13741995-02-14 09:42:43 +00004251
Fredrik Lundh7a830892006-05-27 10:39:48 +00004252static void
Paul Prescode68140d2000-08-30 20:25:01 +00004253format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4254{
4255 char *obj_str;
4256
4257 if (!obj)
4258 return;
4259
4260 obj_str = PyString_AsString(obj);
4261 if (!obj_str)
4262 return;
4263
4264 PyErr_Format(exc, format_str, obj_str);
4265}
Guido van Rossum950361c1997-01-24 13:49:28 +00004266
Fredrik Lundh7a830892006-05-27 10:39:48 +00004267static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004268string_concatenate(PyObject *v, PyObject *w,
4269 PyFrameObject *f, unsigned char *next_instr)
4270{
4271 /* This function implements 'variable += expr' when both arguments
4272 are strings. */
Armin Rigo97ff0472006-08-09 15:37:26 +00004273 Py_ssize_t v_len = PyString_GET_SIZE(v);
4274 Py_ssize_t w_len = PyString_GET_SIZE(w);
4275 Py_ssize_t new_len = v_len + w_len;
4276 if (new_len < 0) {
4277 PyErr_SetString(PyExc_OverflowError,
4278 "strings are too large to concat");
4279 return NULL;
4280 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004281
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004282 if (v->ob_refcnt == 2) {
4283 /* In the common case, there are 2 references to the value
4284 * stored in 'variable' when the += is performed: one on the
4285 * value stack (in 'v') and one still stored in the 'variable'.
4286 * We try to delete the variable now to reduce the refcnt to 1.
4287 */
4288 switch (*next_instr) {
4289 case STORE_FAST:
4290 {
4291 int oparg = PEEKARG();
4292 PyObject **fastlocals = f->f_localsplus;
4293 if (GETLOCAL(oparg) == v)
4294 SETLOCAL(oparg, NULL);
4295 break;
4296 }
4297 case STORE_DEREF:
4298 {
Richard Jonescebbefc2006-05-23 18:28:17 +00004299 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004300 PyObject *c = freevars[PEEKARG()];
4301 if (PyCell_GET(c) == v)
4302 PyCell_Set(c, NULL);
4303 break;
4304 }
4305 case STORE_NAME:
4306 {
4307 PyObject *names = f->f_code->co_names;
4308 PyObject *name = GETITEM(names, PEEKARG());
4309 PyObject *locals = f->f_locals;
4310 if (PyDict_CheckExact(locals) &&
4311 PyDict_GetItem(locals, name) == v) {
4312 if (PyDict_DelItem(locals, name) != 0) {
4313 PyErr_Clear();
4314 }
4315 }
4316 break;
4317 }
4318 }
4319 }
4320
Armin Rigo618fbf52004-08-07 20:58:32 +00004321 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004322 /* Now we own the last reference to 'v', so we can resize it
4323 * in-place.
4324 */
Armin Rigo97ff0472006-08-09 15:37:26 +00004325 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004326 /* XXX if _PyString_Resize() fails, 'v' has been
4327 * deallocated so it cannot be put back into 'variable'.
4328 * The MemoryError is raised when there is no value in
4329 * 'variable', which might (very remotely) be a cause
4330 * of incompatibilities.
4331 */
4332 return NULL;
4333 }
4334 /* copy 'w' into the newly allocated area of 'v' */
4335 memcpy(PyString_AS_STRING(v) + v_len,
4336 PyString_AS_STRING(w), w_len);
4337 return v;
4338 }
4339 else {
4340 /* When in-place resizing is not an option. */
4341 PyString_Concat(&v, w);
4342 return v;
4343 }
4344}
4345
Guido van Rossum950361c1997-01-24 13:49:28 +00004346#ifdef DYNAMIC_EXECUTION_PROFILE
4347
Fredrik Lundh7a830892006-05-27 10:39:48 +00004348static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004349getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004350{
4351 int i;
4352 PyObject *l = PyList_New(256);
4353 if (l == NULL) return NULL;
4354 for (i = 0; i < 256; i++) {
4355 PyObject *x = PyInt_FromLong(a[i]);
4356 if (x == NULL) {
4357 Py_DECREF(l);
4358 return NULL;
4359 }
4360 PyList_SetItem(l, i, x);
4361 }
4362 for (i = 0; i < 256; i++)
4363 a[i] = 0;
4364 return l;
4365}
4366
4367PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004368_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004369{
4370#ifndef DXPAIRS
4371 return getarray(dxp);
4372#else
4373 int i;
4374 PyObject *l = PyList_New(257);
4375 if (l == NULL) return NULL;
4376 for (i = 0; i < 257; i++) {
4377 PyObject *x = getarray(dxpairs[i]);
4378 if (x == NULL) {
4379 Py_DECREF(l);
4380 return NULL;
4381 }
4382 PyList_SetItem(l, i, x);
4383 }
4384 return l;
4385#endif
4386}
4387
4388#endif