blob: 5f8b860ab5d33b805ab6fa8ef545f3694d9ee05f [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?)");
Jeffrey Yasskin6f5d3f32008-12-10 17:23:20 +00001028 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001029 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001030
Guido van Rossum374a9221991-04-04 10:40:29 +00001031 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001033 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001034 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001036 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Guido van Rossum374a9221991-04-04 10:40:29 +00001039 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001041 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001042 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001043 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001044 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001046
Guido van Rossum374a9221991-04-04 10:40:29 +00001047 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001051 if (err == 0) {
1052 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001053 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001054 continue;
1055 }
1056 else if (err > 0) {
1057 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001059 err = 0;
1060 continue;
1061 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001062 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001064
Guido van Rossum374a9221991-04-04 10:40:29 +00001065 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001067 x = PyObject_Repr(v);
1068 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001070 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001071 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001072
Guido van Rossum7928cd71991-10-24 14:59:31 +00001073 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001075 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001076 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001078 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001079 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001080
Guido van Rossum50564e81996-01-12 01:13:16 +00001081 case BINARY_POWER:
1082 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001084 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001085 Py_DECREF(v);
1086 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001088 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001089 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001090
Guido van Rossum374a9221991-04-04 10:40:29 +00001091 case BINARY_MULTIPLY:
1092 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001094 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001095 Py_DECREF(v);
1096 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001097 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001098 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001099 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001100
Guido van Rossum374a9221991-04-04 10:40:29 +00001101 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001102 if (!_Py_QnewFlag) {
1103 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001104 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001105 x = PyNumber_Divide(v, w);
1106 Py_DECREF(v);
1107 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001108 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001109 if (x != NULL) continue;
1110 break;
1111 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001112 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001113 BINARY_TRUE_DIVIDE */
1114 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001115 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001116 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001117 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001118 Py_DECREF(v);
1119 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001120 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001121 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001122 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001123
Guido van Rossum4668b002001-08-08 05:00:18 +00001124 case BINARY_FLOOR_DIVIDE:
1125 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001126 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001127 x = PyNumber_FloorDivide(v, w);
1128 Py_DECREF(v);
1129 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001131 if (x != NULL) continue;
1132 break;
1133
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 case BINARY_MODULO:
1135 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001137 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001138 Py_DECREF(v);
1139 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001140 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001141 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001142 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001143
Guido van Rossum374a9221991-04-04 10:40:29 +00001144 case BINARY_ADD:
1145 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001146 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001147 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001148 /* INLINE: int + int */
1149 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001150 a = PyInt_AS_LONG(v);
1151 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001152 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001153 if ((i^a) < 0 && (i^b) < 0)
1154 goto slow_add;
1155 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001156 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001157 else if (PyString_CheckExact(v) &&
1158 PyString_CheckExact(w)) {
1159 x = string_concatenate(v, w, f, next_instr);
1160 /* string_concatenate consumed the ref to v */
1161 goto skip_decref_vx;
1162 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001163 else {
1164 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001166 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001167 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001168 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001169 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001170 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001171 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001172 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001173
Guido van Rossum374a9221991-04-04 10:40:29 +00001174 case BINARY_SUBTRACT:
1175 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001177 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001178 /* INLINE: int - int */
1179 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001180 a = PyInt_AS_LONG(v);
1181 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001182 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001183 if ((i^a) < 0 && (i^~b) < 0)
1184 goto slow_sub;
1185 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001186 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001187 else {
1188 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001189 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001190 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 Py_DECREF(v);
1192 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001194 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001195 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Guido van Rossum374a9221991-04-04 10:40:29 +00001197 case BINARY_SUBSCR:
1198 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001200 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001201 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001202 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001203 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001204 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001205 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001206 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001207 Py_INCREF(x);
1208 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001209 else
1210 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001211 }
1212 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001213 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001214 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001215 Py_DECREF(v);
1216 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001218 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001219 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001220
Guido van Rossum7928cd71991-10-24 14:59:31 +00001221 case BINARY_LSHIFT:
1222 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001224 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001225 Py_DECREF(v);
1226 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001227 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001228 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001229 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001230
Guido van Rossum7928cd71991-10-24 14:59:31 +00001231 case BINARY_RSHIFT:
1232 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001233 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001234 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001235 Py_DECREF(v);
1236 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001237 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001238 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001239 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001240
Guido van Rossum7928cd71991-10-24 14:59:31 +00001241 case BINARY_AND:
1242 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001244 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001245 Py_DECREF(v);
1246 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001248 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Guido van Rossum7928cd71991-10-24 14:59:31 +00001251 case BINARY_XOR:
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001254 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001255 Py_DECREF(v);
1256 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001258 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001259 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001260
Guido van Rossum7928cd71991-10-24 14:59:31 +00001261 case BINARY_OR:
1262 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001263 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001264 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001265 Py_DECREF(v);
1266 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001268 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001269 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001270
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001271 case LIST_APPEND:
1272 w = POP();
1273 v = POP();
1274 err = PyList_Append(v, w);
1275 Py_DECREF(v);
1276 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001277 if (err == 0) {
1278 PREDICT(JUMP_ABSOLUTE);
1279 continue;
1280 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001281 break;
1282
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 case INPLACE_POWER:
1284 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 x = PyNumber_InPlacePower(v, w, Py_None);
1287 Py_DECREF(v);
1288 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 if (x != NULL) continue;
1291 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001292
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 case INPLACE_MULTIPLY:
1294 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 x = PyNumber_InPlaceMultiply(v, w);
1297 Py_DECREF(v);
1298 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 if (x != NULL) continue;
1301 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001304 if (!_Py_QnewFlag) {
1305 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001306 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001307 x = PyNumber_InPlaceDivide(v, w);
1308 Py_DECREF(v);
1309 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001310 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001311 if (x != NULL) continue;
1312 break;
1313 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001315 INPLACE_TRUE_DIVIDE */
1316 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001319 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 Py_DECREF(v);
1321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 if (x != NULL) continue;
1324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Guido van Rossum4668b002001-08-08 05:00:18 +00001326 case INPLACE_FLOOR_DIVIDE:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001329 x = PyNumber_InPlaceFloorDivide(v, w);
1330 Py_DECREF(v);
1331 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001333 if (x != NULL) continue;
1334 break;
1335
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 case INPLACE_MODULO:
1337 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 x = PyNumber_InPlaceRemainder(v, w);
1340 Py_DECREF(v);
1341 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 if (x != NULL) continue;
1344 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001345
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 case INPLACE_ADD:
1347 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001349 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 /* INLINE: int + int */
1351 register long a, b, i;
1352 a = PyInt_AS_LONG(v);
1353 b = PyInt_AS_LONG(w);
1354 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001355 if ((i^a) < 0 && (i^b) < 0)
1356 goto slow_iadd;
1357 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001359 else if (PyString_CheckExact(v) &&
1360 PyString_CheckExact(w)) {
1361 x = string_concatenate(v, w, f, next_instr);
1362 /* string_concatenate consumed the ref to v */
1363 goto skip_decref_v;
1364 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001365 else {
1366 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001368 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001370 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001372 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001373 if (x != NULL) continue;
1374 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 case INPLACE_SUBTRACT:
1377 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001378 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001379 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001380 /* INLINE: int - int */
1381 register long a, b, i;
1382 a = PyInt_AS_LONG(v);
1383 b = PyInt_AS_LONG(w);
1384 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001385 if ((i^a) < 0 && (i^~b) < 0)
1386 goto slow_isub;
1387 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001389 else {
1390 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001392 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001393 Py_DECREF(v);
1394 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001396 if (x != NULL) continue;
1397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Thomas Wouters434d0822000-08-24 20:11:32 +00001399 case INPLACE_LSHIFT:
1400 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001402 x = PyNumber_InPlaceLshift(v, w);
1403 Py_DECREF(v);
1404 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001406 if (x != NULL) continue;
1407 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 case INPLACE_RSHIFT:
1410 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001412 x = PyNumber_InPlaceRshift(v, w);
1413 Py_DECREF(v);
1414 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001416 if (x != NULL) continue;
1417 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Thomas Wouters434d0822000-08-24 20:11:32 +00001419 case INPLACE_AND:
1420 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001421 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001422 x = PyNumber_InPlaceAnd(v, w);
1423 Py_DECREF(v);
1424 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001425 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001426 if (x != NULL) continue;
1427 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Thomas Wouters434d0822000-08-24 20:11:32 +00001429 case INPLACE_XOR:
1430 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001431 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001432 x = PyNumber_InPlaceXor(v, w);
1433 Py_DECREF(v);
1434 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001435 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001436 if (x != NULL) continue;
1437 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Thomas Wouters434d0822000-08-24 20:11:32 +00001439 case INPLACE_OR:
1440 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001441 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001442 x = PyNumber_InPlaceOr(v, w);
1443 Py_DECREF(v);
1444 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001445 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001446 if (x != NULL) continue;
1447 break;
1448
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 case SLICE+0:
1450 case SLICE+1:
1451 case SLICE+2:
1452 case SLICE+3:
1453 if ((opcode-SLICE) & 2)
1454 w = POP();
1455 else
1456 w = NULL;
1457 if ((opcode-SLICE) & 1)
1458 v = POP();
1459 else
1460 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001463 Py_DECREF(u);
1464 Py_XDECREF(v);
1465 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001466 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001467 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 case STORE_SLICE+0:
1471 case STORE_SLICE+1:
1472 case STORE_SLICE+2:
1473 case STORE_SLICE+3:
1474 if ((opcode-STORE_SLICE) & 2)
1475 w = POP();
1476 else
1477 w = NULL;
1478 if ((opcode-STORE_SLICE) & 1)
1479 v = POP();
1480 else
1481 v = NULL;
1482 u = POP();
1483 t = POP();
1484 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001485 Py_DECREF(t);
1486 Py_DECREF(u);
1487 Py_XDECREF(v);
1488 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001489 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Guido van Rossum374a9221991-04-04 10:40:29 +00001492 case DELETE_SLICE+0:
1493 case DELETE_SLICE+1:
1494 case DELETE_SLICE+2:
1495 case DELETE_SLICE+3:
1496 if ((opcode-DELETE_SLICE) & 2)
1497 w = POP();
1498 else
1499 w = NULL;
1500 if ((opcode-DELETE_SLICE) & 1)
1501 v = POP();
1502 else
1503 v = NULL;
1504 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001506 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001507 Py_DECREF(u);
1508 Py_XDECREF(v);
1509 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001510 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001514 w = TOP();
1515 v = SECOND();
1516 u = THIRD();
1517 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001519 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001520 Py_DECREF(u);
1521 Py_DECREF(v);
1522 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001523 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Guido van Rossum374a9221991-04-04 10:40:29 +00001526 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001527 w = TOP();
1528 v = SECOND();
1529 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001530 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001531 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 Py_DECREF(v);
1533 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001534 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001535 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001536
Guido van Rossum374a9221991-04-04 10:40:29 +00001537 case PRINT_EXPR:
1538 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001539 w = PySys_GetObject("displayhook");
1540 if (w == NULL) {
1541 PyErr_SetString(PyExc_RuntimeError,
1542 "lost sys.displayhook");
1543 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001544 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001545 }
1546 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001547 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001548 if (x == NULL)
1549 err = -1;
1550 }
1551 if (err == 0) {
1552 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001553 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001554 if (w == NULL)
1555 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001556 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001558 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001560
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001561 case PRINT_ITEM_TO:
1562 w = stream = POP();
1563 /* fall through to PRINT_ITEM */
1564
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 case PRINT_ITEM:
1566 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001567 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001568 w = PySys_GetObject("stdout");
1569 if (w == NULL) {
1570 PyErr_SetString(PyExc_RuntimeError,
1571 "lost sys.stdout");
1572 err = -1;
1573 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001574 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001575 /* PyFile_SoftSpace() can exececute arbitrary code
1576 if sys.stdout is an instance with a __getattr__.
1577 If __getattr__ raises an exception, w will
1578 be freed, so we need to prevent that temporarily. */
1579 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001580 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001581 err = PyFile_WriteString(" ", w);
1582 if (err == 0)
1583 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001584 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001585 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001586 if (PyString_Check(v)) {
1587 char *s = PyString_AS_STRING(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001588 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001589 if (len == 0 ||
1590 !isspace(Py_CHARMASK(s[len-1])) ||
1591 s[len-1] == ' ')
1592 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001593 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001594#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001595 else if (PyUnicode_Check(v)) {
1596 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001597 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001598 if (len == 0 ||
1599 !Py_UNICODE_ISSPACE(s[len-1]) ||
1600 s[len-1] == ' ')
1601 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001602 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001603#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001604 else
1605 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001607 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001608 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001609 Py_XDECREF(stream);
1610 stream = NULL;
1611 if (err == 0)
1612 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001615 case PRINT_NEWLINE_TO:
1616 w = stream = POP();
1617 /* fall through to PRINT_NEWLINE */
1618
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001620 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001621 w = PySys_GetObject("stdout");
Jeffrey Yasskin6f5d3f32008-12-10 17:23:20 +00001622 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001623 PyErr_SetString(PyExc_RuntimeError,
1624 "lost sys.stdout");
Jeffrey Yasskin6f5d3f32008-12-10 17:23:20 +00001625 why = WHY_EXCEPTION;
1626 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001627 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001628 if (w != NULL) {
Amaury Forgeot d'Arcceda6a62008-07-01 20:52:56 +00001629 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001630 err = PyFile_WriteString("\n", w);
1631 if (err == 0)
1632 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcceda6a62008-07-01 20:52:56 +00001633 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001634 }
1635 Py_XDECREF(stream);
1636 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Thomas Wouters434d0822000-08-24 20:11:32 +00001639
1640#ifdef CASE_TOO_BIG
1641 default: switch (opcode) {
1642#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001643 case RAISE_VARARGS:
1644 u = v = w = NULL;
1645 switch (oparg) {
1646 case 3:
1647 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001648 /* Fallthrough */
1649 case 2:
1650 v = POP(); /* value */
1651 /* Fallthrough */
1652 case 1:
1653 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001654 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001655 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001656 break;
1657 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001659 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001660 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001661 break;
1662 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001666 if ((x = f->f_locals) != NULL) {
1667 Py_INCREF(x);
1668 PUSH(x);
1669 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001670 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001671 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 case RETURN_VALUE:
1675 retval = POP();
1676 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001677 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001678
Tim Peters5ca576e2001-06-18 22:08:13 +00001679 case YIELD_VALUE:
1680 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001681 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001682 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001683 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001684
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001685 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001686 w = TOP();
1687 v = SECOND();
1688 u = THIRD();
1689 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001690 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001691 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001692 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001693 Py_DECREF(u);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 case POP_BLOCK:
1699 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001700 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 while (STACK_LEVEL() > b->b_level) {
1702 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001703 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 }
1705 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001706 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 case END_FINALLY:
1709 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001710 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001711 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001712 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001713 if (why == WHY_RETURN ||
1714 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 retval = POP();
1716 }
Brett Cannonbf364092006-03-01 04:25:17 +00001717 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001719 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001720 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001722 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 else if (v != Py_None) {
1725 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001726 "'finally' pops bad exception");
1727 why = WHY_EXCEPTION;
1728 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001733 u = TOP();
1734 v = SECOND();
1735 w = THIRD();
1736 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001737 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001738 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 Py_DECREF(u);
1740 Py_DECREF(v);
1741 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001745 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001747 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001748 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001749 err = PyDict_SetItem(x, w, v);
1750 else
1751 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001752 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001753 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001754 break;
1755 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001756 PyErr_Format(PyExc_SystemError,
1757 "no locals found when storing %s",
1758 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001762 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001763 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001764 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001765 format_exc_check_arg(PyExc_NameError,
1766 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001767 break;
1768 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001769 PyErr_Format(PyExc_SystemError,
1770 "no locals when deleting %s",
1771 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001773
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001774 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001775 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001777 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1778 PyObject **items = ((PyTupleObject *)v)->ob_item;
1779 while (oparg--) {
1780 w = items[oparg];
1781 Py_INCREF(w);
1782 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001783 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001784 Py_DECREF(v);
1785 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001786 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1787 PyObject **items = ((PyListObject *)v)->ob_item;
1788 while (oparg--) {
1789 w = items[oparg];
1790 Py_INCREF(w);
1791 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001792 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001793 } else if (unpack_iterable(v, oparg,
Georg Brandl8a10ea42007-03-21 09:00:55 +00001794 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001795 stack_pointer += oparg;
Georg Brandl8a10ea42007-03-21 09:00:55 +00001796 } else {
1797 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001798 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001799 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001800 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001804 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001805 v = TOP();
1806 u = SECOND();
1807 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1809 Py_DECREF(v);
1810 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001811 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001815 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001817 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1818 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001819 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001821
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001822 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001823 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001824 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001825 err = PyDict_SetItem(f->f_globals, w, v);
1826 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001827 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001828 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001829
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001830 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001831 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001832 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001833 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001834 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001835 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001836
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001838 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001839 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001840 PyErr_Format(PyExc_SystemError,
1841 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001842 PyObject_REPR(w));
Jeffrey Yasskin6f5d3f32008-12-10 17:23:20 +00001843 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001844 break;
1845 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001846 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001847 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001848 Py_XINCREF(x);
1849 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001850 else {
1851 x = PyObject_GetItem(v, w);
1852 if (x == NULL && PyErr_Occurred()) {
1853 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1854 break;
1855 PyErr_Clear();
1856 }
1857 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001859 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001863 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001864 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001865 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 break;
1867 }
1868 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001869 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001872 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001875 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001876 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001877 /* Inline the PyDict_GetItem() calls.
1878 WARNING: this is an extreme speed hack.
1879 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001880 long hash = ((PyStringObject *)w)->ob_shash;
1881 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001882 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00001883 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001884 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00001885 e = d->ma_lookup(d, w, hash);
1886 if (e == NULL) {
1887 x = NULL;
1888 break;
1889 }
1890 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001891 if (x != NULL) {
1892 Py_INCREF(x);
1893 PUSH(x);
1894 continue;
1895 }
1896 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00001897 e = d->ma_lookup(d, w, hash);
1898 if (e == NULL) {
1899 x = NULL;
1900 break;
1901 }
1902 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001903 if (x != NULL) {
1904 Py_INCREF(x);
1905 PUSH(x);
1906 continue;
1907 }
1908 goto load_global_error;
1909 }
1910 }
1911 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001912 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001914 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001916 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001917 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001918 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001919 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 break;
1921 }
1922 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001923 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001925 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001926
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001927 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001928 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001929 if (x != NULL) {
1930 SETLOCAL(oparg, NULL);
1931 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001932 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001933 format_exc_check_arg(
1934 PyExc_UnboundLocalError,
1935 UNBOUNDLOCAL_ERROR_MSG,
1936 PyTuple_GetItem(co->co_varnames, oparg)
1937 );
1938 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001940 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001941 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001942 Py_INCREF(x);
1943 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001944 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001945 break;
1946
1947 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001948 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001949 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001950 if (w != NULL) {
1951 PUSH(w);
1952 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001953 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001954 err = -1;
1955 /* Don't stomp existing exception */
1956 if (PyErr_Occurred())
1957 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00001958 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1959 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001960 oparg);
1961 format_exc_check_arg(
1962 PyExc_UnboundLocalError,
1963 UNBOUNDLOCAL_ERROR_MSG,
1964 v);
1965 } else {
Richard Jonescebbefc2006-05-23 18:28:17 +00001966 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001967 co->co_freevars,
Richard Jonescebbefc2006-05-23 18:28:17 +00001968 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001969 format_exc_check_arg(
1970 PyExc_NameError,
1971 UNBOUNDFREE_ERROR_MSG,
1972 v);
1973 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001974 break;
1975
1976 case STORE_DEREF:
1977 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001978 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001979 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001980 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001981 continue;
1982
Guido van Rossum374a9221991-04-04 10:40:29 +00001983 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001985 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001986 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001987 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001988 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001989 }
1990 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001991 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 }
1993 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001994
Guido van Rossum374a9221991-04-04 10:40:29 +00001995 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001998 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001999 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002000 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002001 }
2002 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002003 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 }
2005 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002006
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00002008 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002010 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002011 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Guido van Rossum374a9221991-04-04 10:40:29 +00002013 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002014 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002015 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002016 x = PyObject_GetAttr(v, w);
2017 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002018 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002019 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002020 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Guido van Rossum374a9221991-04-04 10:40:29 +00002022 case COMPARE_OP:
2023 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002024 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002025 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002026 /* INLINE: cmp(int, int) */
2027 register long a, b;
2028 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002029 a = PyInt_AS_LONG(v);
2030 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002031 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002032 case PyCmp_LT: res = a < b; break;
2033 case PyCmp_LE: res = a <= b; break;
2034 case PyCmp_EQ: res = a == b; break;
2035 case PyCmp_NE: res = a != b; break;
2036 case PyCmp_GT: res = a > b; break;
2037 case PyCmp_GE: res = a >= b; break;
2038 case PyCmp_IS: res = v == w; break;
2039 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002040 default: goto slow_compare;
2041 }
2042 x = res ? Py_True : Py_False;
2043 Py_INCREF(x);
2044 }
2045 else {
2046 slow_compare:
2047 x = cmp_outcome(oparg, v, w);
2048 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 Py_DECREF(v);
2050 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002051 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002052 if (x == NULL) break;
2053 PREDICT(JUMP_IF_FALSE);
2054 PREDICT(JUMP_IF_TRUE);
2055 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Guido van Rossum374a9221991-04-04 10:40:29 +00002057 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002058 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002059 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002060 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002061 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002062 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002063 break;
2064 }
Guido van Rossume105f982008-01-23 20:09:39 +00002065 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002066 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002067 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002068 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2069 w = PyTuple_Pack(5,
2070 w,
2071 f->f_globals,
2072 f->f_locals == NULL ?
2073 Py_None : f->f_locals,
2074 v,
2075 u);
2076 else
2077 w = PyTuple_Pack(4,
2078 w,
2079 f->f_globals,
2080 f->f_locals == NULL ?
2081 Py_None : f->f_locals,
2082 v);
2083 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002084 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002085 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002086 u = POP();
Guido van Rossume105f982008-01-23 20:09:39 +00002087 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002088 x = NULL;
2089 break;
2090 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002091 READ_TIMESTAMP(intr0);
Guido van Rossume105f982008-01-23 20:09:39 +00002092 v = x;
2093 x = PyEval_CallObject(v, w);
2094 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002095 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002096 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002097 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002098 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002099 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002100
Thomas Wouters52152252000-08-17 22:55:00 +00002101 case IMPORT_STAR:
2102 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002103 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002104 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002105 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002106 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002107 break;
2108 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002109 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002110 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002111 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002112 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002113 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002114 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002115 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002116
Thomas Wouters52152252000-08-17 22:55:00 +00002117 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002118 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002119 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002120 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002121 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002122 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002123 PUSH(x);
2124 if (x != NULL) continue;
2125 break;
2126
Guido van Rossum374a9221991-04-04 10:40:29 +00002127 case JUMP_FORWARD:
2128 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002129 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002130
Raymond Hettingerf606f872003-03-16 03:11:04 +00002131 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002133 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002134 if (w == Py_True) {
2135 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002136 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002137 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002138 if (w == Py_False) {
2139 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002140 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002141 }
2142 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002143 if (err > 0)
2144 err = 0;
2145 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002146 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002147 else
2148 break;
2149 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002150
Raymond Hettingerf606f872003-03-16 03:11:04 +00002151 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002152 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002153 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002154 if (w == Py_False) {
2155 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002156 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002157 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002158 if (w == Py_True) {
2159 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002160 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002161 }
2162 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002163 if (err > 0) {
2164 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002165 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002166 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002167 else if (err == 0)
2168 ;
2169 else
2170 break;
2171 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002173 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002174 case JUMP_ABSOLUTE:
2175 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002176 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002177
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002178 case GET_ITER:
2179 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002180 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002181 x = PyObject_GetIter(v);
2182 Py_DECREF(v);
2183 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002184 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002185 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002186 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002187 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002188 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002189 break;
2190
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002191 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002192 case FOR_ITER:
2193 /* before: [iter]; after: [iter, iter()] *or* [] */
2194 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002195 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002196 if (x != NULL) {
2197 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002198 PREDICT(STORE_FAST);
2199 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002200 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002201 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002202 if (PyErr_Occurred()) {
2203 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2204 break;
2205 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002206 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002207 /* iterator ended normally */
2208 x = v = POP();
2209 Py_DECREF(v);
2210 JUMPBY(oparg);
2211 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002212
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002213 case BREAK_LOOP:
2214 why = WHY_BREAK;
2215 goto fast_block_end;
2216
2217 case CONTINUE_LOOP:
2218 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002219 if (!retval) {
2220 x = NULL;
2221 break;
2222 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002223 why = WHY_CONTINUE;
2224 goto fast_block_end;
2225
Guido van Rossum374a9221991-04-04 10:40:29 +00002226 case SETUP_LOOP:
2227 case SETUP_EXCEPT:
2228 case SETUP_FINALLY:
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002229 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2230 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2231
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002233 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002234 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002235
Guido van Rossumc2e20742006-02-27 22:32:47 +00002236 case WITH_CLEANUP:
2237 {
2238 /* TOP is the context.__exit__ bound method.
2239 Below that are 1-3 values indicating how/why
2240 we entered the finally clause:
2241 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002242 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002243 - SECOND = WHY_*; no retval below it
2244 - (SECOND, THIRD, FOURTH) = exc_info()
2245 In the last case, we must call
2246 TOP(SECOND, THIRD, FOURTH)
2247 otherwise we must call
2248 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002249
2250 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002251 *and* the function call returns a 'true' value, we
2252 "zap" this information, to prevent END_FINALLY from
2253 re-raising the exception. (But non-local gotos
2254 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002255 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002256
Guido van Rossumc2e20742006-02-27 22:32:47 +00002257 x = TOP();
2258 u = SECOND();
2259 if (PyInt_Check(u) || u == Py_None) {
2260 u = v = w = Py_None;
2261 }
2262 else {
2263 v = THIRD();
2264 w = FOURTH();
2265 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002266 /* XXX Not the fastest way to call it... */
2267 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2268 if (x == NULL)
2269 break; /* Go to error exit */
Jeffrey Yasskin478a1aa2008-12-10 07:28:12 +00002270 if (u != Py_None)
2271 err = PyObject_IsTrue(x);
2272 else
2273 err = 0;
2274 Py_DECREF(x);
2275 if (err < 0)
2276 break; /* Go to error exit */
2277 else if (err > 0) {
2278 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002279 /* There was an exception and a true return */
Guido van Rossumf6694362006-03-10 02:28:35 +00002280 x = TOP(); /* Again */
2281 STACKADJ(-3);
2282 Py_INCREF(Py_None);
2283 SET_TOP(Py_None);
2284 Py_DECREF(x);
2285 Py_DECREF(u);
2286 Py_DECREF(v);
2287 Py_DECREF(w);
2288 } else {
2289 /* Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002290 x = POP();
2291 Py_DECREF(x);
2292 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002293 break;
2294 }
2295
Guido van Rossumf10570b1995-07-07 22:53:21 +00002296 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002297 {
2298 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002299 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002300 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002301#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002302 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002303#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002304 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002305#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002306 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002307 PUSH(x);
2308 if (x != NULL)
2309 continue;
2310 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002311 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002312
Jeremy Hylton76901512000-03-28 23:49:17 +00002313 case CALL_FUNCTION_VAR:
2314 case CALL_FUNCTION_KW:
2315 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002316 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002317 int na = oparg & 0xff;
2318 int nk = (oparg>>8) & 0xff;
2319 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002320 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002321 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002322 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002323 if (flags & CALL_FLAG_VAR)
2324 n++;
2325 if (flags & CALL_FLAG_KW)
2326 n++;
2327 pfunc = stack_pointer - n - 1;
2328 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002329
Guido van Rossumac7be682001-01-17 15:42:30 +00002330 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002331 && PyMethod_GET_SELF(func) != NULL) {
2332 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002333 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002334 func = PyMethod_GET_FUNCTION(func);
2335 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002336 Py_DECREF(*pfunc);
2337 *pfunc = self;
2338 na++;
2339 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002340 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002341 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002342 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002343 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002344 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002345 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002346 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002347 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002348
Jeremy Hylton76901512000-03-28 23:49:17 +00002349 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002350 w = POP();
2351 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002352 }
2353 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002354 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002355 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002356 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002357 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002358
Guido van Rossum681d79a1995-07-18 14:51:37 +00002359 case MAKE_FUNCTION:
2360 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 x = PyFunction_New(v, f->f_globals);
2362 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002363 /* XXX Maybe this should be a separate opcode? */
2364 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002365 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002366 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002367 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002368 x = NULL;
2369 break;
2370 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002371 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002372 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002373 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002374 }
2375 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002376 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002377 }
2378 PUSH(x);
2379 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002380
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002381 case MAKE_CLOSURE:
2382 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002383 v = POP(); /* code object */
2384 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002385 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 if (x != NULL) {
2387 v = POP();
Jeffrey Yasskin6f5d3f32008-12-10 17:23:20 +00002388 if (PyFunction_SetClosure(x, v) != 0) {
2389 /* Can't happen unless bytecode is corrupt. */
2390 why = WHY_EXCEPTION;
2391 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002392 Py_DECREF(v);
2393 }
2394 if (x != NULL && oparg > 0) {
2395 v = PyTuple_New(oparg);
2396 if (v == NULL) {
2397 Py_DECREF(x);
2398 x = NULL;
2399 break;
2400 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002401 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002402 w = POP();
2403 PyTuple_SET_ITEM(v, oparg, w);
2404 }
Jeffrey Yasskin6f5d3f32008-12-10 17:23:20 +00002405 if (PyFunction_SetDefaults(x, v) != 0) {
2406 /* Can't happen unless
2407 PyFunction_SetDefaults changes. */
2408 why = WHY_EXCEPTION;
2409 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002410 Py_DECREF(v);
2411 }
2412 PUSH(x);
2413 break;
2414 }
2415
Guido van Rossum8861b741996-07-30 16:49:37 +00002416 case BUILD_SLICE:
2417 if (oparg == 3)
2418 w = POP();
2419 else
2420 w = NULL;
2421 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002422 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002423 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002424 Py_DECREF(u);
2425 Py_DECREF(v);
2426 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002427 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002428 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002429 break;
2430
Fred Drakeef8ace32000-08-24 00:32:09 +00002431 case EXTENDED_ARG:
2432 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002433 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002434 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002435
Guido van Rossum374a9221991-04-04 10:40:29 +00002436 default:
2437 fprintf(stderr,
2438 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002439 PyCode_Addr2Line(f->f_code, f->f_lasti),
2440 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002441 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002442 why = WHY_EXCEPTION;
2443 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002444
2445#ifdef CASE_TOO_BIG
2446 }
2447#endif
2448
Guido van Rossum374a9221991-04-04 10:40:29 +00002449 } /* switch */
2450
2451 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002453 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002454
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002456
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002458 if (err == 0 && x != NULL) {
2459#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002460 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002461 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002462 fprintf(stderr,
2463 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002464 else {
2465#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002466 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002467 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002468#ifdef CHECKEXC
2469 }
2470#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002471 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002472 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002473 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002474 err = 0;
2475 }
2476
Guido van Rossum374a9221991-04-04 10:40:29 +00002477 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002478
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002479 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002480 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002481 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002482 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002483 why = WHY_EXCEPTION;
2484 }
2485 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002486#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002487 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002488 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002489 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002490 char buf[1024];
2491 sprintf(buf, "Stack unwind with exception "
2492 "set and why=%d", why);
2493 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002494 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002495 }
2496#endif
2497
2498 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002499
Guido van Rossum374a9221991-04-04 10:40:29 +00002500 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002501 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002502
Fred Drake8f51f542001-10-04 14:48:42 +00002503 if (tstate->c_tracefunc != NULL)
2504 call_exc_trace(tstate->c_tracefunc,
2505 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002507
Guido van Rossum374a9221991-04-04 10:40:29 +00002508 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002509
Guido van Rossum374a9221991-04-04 10:40:29 +00002510 if (why == WHY_RERAISE)
2511 why = WHY_EXCEPTION;
2512
2513 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002514
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002515fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002516 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002517 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002518
Tim Peters8a5c3c72004-04-05 19:36:21 +00002519 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002520 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2521 /* For a continue inside a try block,
2522 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002523 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2524 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002525 why = WHY_NOT;
2526 JUMPTO(PyInt_AS_LONG(retval));
2527 Py_DECREF(retval);
2528 break;
2529 }
2530
Guido van Rossum374a9221991-04-04 10:40:29 +00002531 while (STACK_LEVEL() > b->b_level) {
2532 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002533 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002534 }
2535 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2536 why = WHY_NOT;
2537 JUMPTO(b->b_handler);
2538 break;
2539 }
2540 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002541 (b->b_type == SETUP_EXCEPT &&
2542 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002543 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002544 PyObject *exc, *val, *tb;
2545 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002546 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002547 val = Py_None;
2548 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002549 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002550 /* Make the raw exception data
2551 available to the handler,
2552 so a program can emulate the
2553 Python main loop. Don't do
2554 this for 'finally'. */
2555 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002556 PyErr_NormalizeException(
2557 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002558 set_exc_info(tstate,
2559 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002560 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002561 if (tb == NULL) {
2562 Py_INCREF(Py_None);
2563 PUSH(Py_None);
2564 } else
2565 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002566 PUSH(val);
2567 PUSH(exc);
2568 }
2569 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002570 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002571 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002572 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002573 PUSH(v);
2574 }
2575 why = WHY_NOT;
2576 JUMPTO(b->b_handler);
2577 break;
2578 }
2579 } /* unwind stack */
2580
2581 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002582
Guido van Rossum374a9221991-04-04 10:40:29 +00002583 if (why != WHY_NOT)
2584 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002585 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002586
Guido van Rossum374a9221991-04-04 10:40:29 +00002587 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002588
Tim Peters8a5c3c72004-04-05 19:36:21 +00002589 assert(why != WHY_YIELD);
2590 /* Pop remaining stack entries. */
2591 while (!EMPTY()) {
2592 v = POP();
2593 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002594 }
2595
Tim Peters8a5c3c72004-04-05 19:36:21 +00002596 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002597 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002598
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002599fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002600 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002601 if (tstate->c_tracefunc) {
2602 if (why == WHY_RETURN || why == WHY_YIELD) {
2603 if (call_trace(tstate->c_tracefunc,
2604 tstate->c_traceobj, f,
2605 PyTrace_RETURN, retval)) {
2606 Py_XDECREF(retval);
2607 retval = NULL;
2608 why = WHY_EXCEPTION;
2609 }
2610 }
2611 else if (why == WHY_EXCEPTION) {
2612 call_trace_protected(tstate->c_tracefunc,
2613 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002614 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002615 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002616 }
Fred Drake8f51f542001-10-04 14:48:42 +00002617 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002618 if (why == WHY_EXCEPTION)
2619 call_trace_protected(tstate->c_profilefunc,
2620 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002621 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002622 else if (call_trace(tstate->c_profilefunc,
2623 tstate->c_profileobj, f,
2624 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002625 Py_XDECREF(retval);
2626 retval = NULL;
2627 why = WHY_EXCEPTION;
2628 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002629 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002630 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002631
Tim Peters7df5e7f2006-05-26 23:14:37 +00002632 if (tstate->frame->f_exc_type != NULL)
2633 reset_exc_info(tstate);
2634 else {
2635 assert(tstate->frame->f_exc_value == NULL);
2636 assert(tstate->frame->f_exc_traceback == NULL);
2637 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002638
Tim Peters5ca576e2001-06-18 22:08:13 +00002639 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002640 exit_eval_frame:
2641 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002642 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002643
Guido van Rossum96a42c81992-01-12 02:29:51 +00002644 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002645}
2646
Guido van Rossumc2e20742006-02-27 22:32:47 +00002647/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002648 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002649 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002650
Tim Peters6d6c1a32001-08-02 04:15:00 +00002651PyObject *
2652PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002653 PyObject **args, int argcount, PyObject **kws, int kwcount,
2654 PyObject **defs, int defcount, PyObject *closure)
2655{
2656 register PyFrameObject *f;
2657 register PyObject *retval = NULL;
2658 register PyObject **fastlocals, **freevars;
2659 PyThreadState *tstate = PyThreadState_GET();
2660 PyObject *x, *u;
2661
2662 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002663 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002664 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002665 return NULL;
2666 }
2667
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002668 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002669 assert(globals != NULL);
2670 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002671 if (f == NULL)
2672 return NULL;
2673
2674 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002675 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002676
2677 if (co->co_argcount > 0 ||
2678 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2679 int i;
2680 int n = argcount;
2681 PyObject *kwdict = NULL;
2682 if (co->co_flags & CO_VARKEYWORDS) {
2683 kwdict = PyDict_New();
2684 if (kwdict == NULL)
2685 goto fail;
2686 i = co->co_argcount;
2687 if (co->co_flags & CO_VARARGS)
2688 i++;
2689 SETLOCAL(i, kwdict);
2690 }
2691 if (argcount > co->co_argcount) {
2692 if (!(co->co_flags & CO_VARARGS)) {
2693 PyErr_Format(PyExc_TypeError,
2694 "%.200s() takes %s %d "
2695 "%sargument%s (%d given)",
2696 PyString_AsString(co->co_name),
2697 defcount ? "at most" : "exactly",
2698 co->co_argcount,
2699 kwcount ? "non-keyword " : "",
2700 co->co_argcount == 1 ? "" : "s",
2701 argcount);
2702 goto fail;
2703 }
2704 n = co->co_argcount;
2705 }
2706 for (i = 0; i < n; i++) {
2707 x = args[i];
2708 Py_INCREF(x);
2709 SETLOCAL(i, x);
2710 }
2711 if (co->co_flags & CO_VARARGS) {
2712 u = PyTuple_New(argcount - n);
2713 if (u == NULL)
2714 goto fail;
2715 SETLOCAL(co->co_argcount, u);
2716 for (i = n; i < argcount; i++) {
2717 x = args[i];
2718 Py_INCREF(x);
2719 PyTuple_SET_ITEM(u, i-n, x);
2720 }
2721 }
2722 for (i = 0; i < kwcount; i++) {
2723 PyObject *keyword = kws[2*i];
2724 PyObject *value = kws[2*i + 1];
2725 int j;
2726 if (keyword == NULL || !PyString_Check(keyword)) {
2727 PyErr_Format(PyExc_TypeError,
2728 "%.200s() keywords must be strings",
2729 PyString_AsString(co->co_name));
2730 goto fail;
2731 }
2732 /* XXX slow -- speed up using dictionary? */
2733 for (j = 0; j < co->co_argcount; j++) {
2734 PyObject *nm = PyTuple_GET_ITEM(
2735 co->co_varnames, j);
2736 int cmp = PyObject_RichCompareBool(
2737 keyword, nm, Py_EQ);
2738 if (cmp > 0)
2739 break;
2740 else if (cmp < 0)
2741 goto fail;
2742 }
2743 /* Check errors from Compare */
2744 if (PyErr_Occurred())
2745 goto fail;
2746 if (j >= co->co_argcount) {
2747 if (kwdict == NULL) {
2748 PyErr_Format(PyExc_TypeError,
2749 "%.200s() got an unexpected "
2750 "keyword argument '%.400s'",
2751 PyString_AsString(co->co_name),
2752 PyString_AsString(keyword));
2753 goto fail;
2754 }
2755 PyDict_SetItem(kwdict, keyword, value);
2756 }
2757 else {
2758 if (GETLOCAL(j) != NULL) {
2759 PyErr_Format(PyExc_TypeError,
2760 "%.200s() got multiple "
2761 "values for keyword "
2762 "argument '%.400s'",
2763 PyString_AsString(co->co_name),
2764 PyString_AsString(keyword));
2765 goto fail;
2766 }
2767 Py_INCREF(value);
2768 SETLOCAL(j, value);
2769 }
2770 }
2771 if (argcount < co->co_argcount) {
2772 int m = co->co_argcount - defcount;
2773 for (i = argcount; i < m; i++) {
2774 if (GETLOCAL(i) == NULL) {
2775 PyErr_Format(PyExc_TypeError,
2776 "%.200s() takes %s %d "
2777 "%sargument%s (%d given)",
2778 PyString_AsString(co->co_name),
2779 ((co->co_flags & CO_VARARGS) ||
2780 defcount) ? "at least"
2781 : "exactly",
2782 m, kwcount ? "non-keyword " : "",
2783 m == 1 ? "" : "s", i);
2784 goto fail;
2785 }
2786 }
2787 if (n > m)
2788 i = n - m;
2789 else
2790 i = 0;
2791 for (; i < defcount; i++) {
2792 if (GETLOCAL(m+i) == NULL) {
2793 PyObject *def = defs[i];
2794 Py_INCREF(def);
2795 SETLOCAL(m+i, def);
2796 }
2797 }
2798 }
2799 }
2800 else {
2801 if (argcount > 0 || kwcount > 0) {
2802 PyErr_Format(PyExc_TypeError,
2803 "%.200s() takes no arguments (%d given)",
2804 PyString_AsString(co->co_name),
2805 argcount + kwcount);
2806 goto fail;
2807 }
2808 }
2809 /* Allocate and initialize storage for cell vars, and copy free
2810 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002811 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002812 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002813 char *cellname, *argname;
2814 PyObject *c;
2815
2816 nargs = co->co_argcount;
2817 if (co->co_flags & CO_VARARGS)
2818 nargs++;
2819 if (co->co_flags & CO_VARKEYWORDS)
2820 nargs++;
2821
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 /* Initialize each cell var, taking into account
2823 cell vars that are initialized from arguments.
2824
2825 Should arrange for the compiler to put cellvars
2826 that are arguments at the beginning of the cellvars
2827 list so that we can march over it more efficiently?
2828 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002829 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002830 cellname = PyString_AS_STRING(
2831 PyTuple_GET_ITEM(co->co_cellvars, i));
2832 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002834 argname = PyString_AS_STRING(
2835 PyTuple_GET_ITEM(co->co_varnames, j));
2836 if (strcmp(cellname, argname) == 0) {
2837 c = PyCell_New(GETLOCAL(j));
2838 if (c == NULL)
2839 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002840 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002841 found = 1;
2842 break;
2843 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002844 }
2845 if (found == 0) {
2846 c = PyCell_New(NULL);
2847 if (c == NULL)
2848 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002849 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002850 }
2851 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002852 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002853 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002854 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002855 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002856 PyObject *o = PyTuple_GET_ITEM(closure, i);
2857 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002858 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002859 }
2860 }
2861
Tim Peters5ca576e2001-06-18 22:08:13 +00002862 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002863 /* Don't need to keep the reference to f_back, it will be set
2864 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002865 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002866 f->f_back = NULL;
2867
Jeremy Hylton985eba52003-02-05 23:13:00 +00002868 PCALL(PCALL_GENERATOR);
2869
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002870 /* Create a new generator that owns the ready to run frame
2871 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002872 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002873 }
2874
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002875 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002876
2877 fail: /* Jump here from prelude on failure */
2878
Tim Petersb13680b2001-11-27 23:29:29 +00002879 /* decref'ing the frame can cause __del__ methods to get invoked,
2880 which can call back into Python. While we're done with the
2881 current Python frame (f), the associated C stack is still in use,
2882 so recursion_depth must be boosted for the duration.
2883 */
2884 assert(tstate != NULL);
2885 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002886 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002887 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002888 return retval;
2889}
2890
2891
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002892/* Implementation notes for set_exc_info() and reset_exc_info():
2893
2894- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2895 'exc_traceback'. These always travel together.
2896
2897- tstate->curexc_ZZZ is the "hot" exception that is set by
2898 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2899
2900- Once an exception is caught by an except clause, it is transferred
2901 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2902 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00002903 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002904
2905- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2906
2907 Long ago, when none of this existed, there were just a few globals:
2908 one set corresponding to the "hot" exception, and one set
2909 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2910 globals; they were simply stored as sys.exc_ZZZ. For backwards
2911 compatibility, they still are!) The problem was that in code like
2912 this:
2913
2914 try:
2915 "something that may fail"
2916 except "some exception":
2917 "do something else first"
2918 "print the exception from sys.exc_ZZZ."
2919
2920 if "do something else first" invoked something that raised and caught
2921 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2922 cause of subtle bugs. I fixed this by changing the semantics as
2923 follows:
2924
2925 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2926 *in that frame*.
2927
2928 - But initially, and as long as no exception is caught in a given
2929 frame, sys.exc_ZZZ will hold the last exception caught in the
2930 previous frame (or the frame before that, etc.).
2931
2932 The first bullet fixed the bug in the above example. The second
2933 bullet was for backwards compatibility: it was (and is) common to
2934 have a function that is called when an exception is caught, and to
2935 have that function access the caught exception via sys.exc_ZZZ.
2936 (Example: traceback.print_exc()).
2937
2938 At the same time I fixed the problem that sys.exc_ZZZ weren't
2939 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2940 but that's really a separate improvement.
2941
2942 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2943 variables to what they were before the current frame was called. The
2944 set_exc_info() function saves them on the frame so that
2945 reset_exc_info() can restore them. The invariant is that
2946 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2947 exception (where "catching" an exception applies only to successful
2948 except clauses); and if the current frame ever caught an exception,
2949 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2950 at the start of the current frame.
2951
2952*/
2953
Fredrik Lundh7a830892006-05-27 10:39:48 +00002954static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002955set_exc_info(PyThreadState *tstate,
2956 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002957{
Tim Peters7df5e7f2006-05-26 23:14:37 +00002958 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002959 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002960
Tim Peters7df5e7f2006-05-26 23:14:37 +00002961 assert(type != NULL);
2962 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002963 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002964 assert(frame->f_exc_value == NULL);
2965 assert(frame->f_exc_traceback == NULL);
2966 /* This frame didn't catch an exception before. */
2967 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002968 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00002969 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002970 Py_INCREF(Py_None);
2971 tstate->exc_type = Py_None;
2972 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002973 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002974 Py_XINCREF(tstate->exc_value);
2975 Py_XINCREF(tstate->exc_traceback);
2976 frame->f_exc_type = tstate->exc_type;
2977 frame->f_exc_value = tstate->exc_value;
2978 frame->f_exc_traceback = tstate->exc_traceback;
2979 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00002980 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002981 tmp_type = tstate->exc_type;
2982 tmp_value = tstate->exc_value;
2983 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00002984 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002985 Py_XINCREF(value);
2986 Py_XINCREF(tb);
2987 tstate->exc_type = type;
2988 tstate->exc_value = value;
2989 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002990 Py_XDECREF(tmp_type);
2991 Py_XDECREF(tmp_value);
2992 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002993 /* For b/w compatibility */
2994 PySys_SetObject("exc_type", type);
2995 PySys_SetObject("exc_value", value);
2996 PySys_SetObject("exc_traceback", tb);
2997}
2998
Fredrik Lundh7a830892006-05-27 10:39:48 +00002999static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003000reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003001{
3002 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003003 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003004
3005 /* It's a precondition that the thread state's frame caught an
3006 * exception -- verify in a debug build.
3007 */
3008 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003009 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003010 assert(frame != NULL);
3011 assert(frame->f_exc_type != NULL);
3012
3013 /* Copy the frame's exception info back to the thread state. */
3014 tmp_type = tstate->exc_type;
3015 tmp_value = tstate->exc_value;
3016 tmp_tb = tstate->exc_traceback;
3017 Py_INCREF(frame->f_exc_type);
3018 Py_XINCREF(frame->f_exc_value);
3019 Py_XINCREF(frame->f_exc_traceback);
3020 tstate->exc_type = frame->f_exc_type;
3021 tstate->exc_value = frame->f_exc_value;
3022 tstate->exc_traceback = frame->f_exc_traceback;
3023 Py_XDECREF(tmp_type);
3024 Py_XDECREF(tmp_value);
3025 Py_XDECREF(tmp_tb);
3026
3027 /* For b/w compatibility */
3028 PySys_SetObject("exc_type", frame->f_exc_type);
3029 PySys_SetObject("exc_value", frame->f_exc_value);
3030 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3031
3032 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003033 tmp_type = frame->f_exc_type;
3034 tmp_value = frame->f_exc_value;
3035 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003036 frame->f_exc_type = NULL;
3037 frame->f_exc_value = NULL;
3038 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003039 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003040 Py_XDECREF(tmp_value);
3041 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003042}
3043
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003044/* Logic for the raise statement (too complicated for inlining).
3045 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003046static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003047do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003048{
Guido van Rossumd295f121998-04-09 21:39:57 +00003049 if (type == NULL) {
3050 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003051 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003052 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3053 value = tstate->exc_value;
3054 tb = tstate->exc_traceback;
3055 Py_XINCREF(type);
3056 Py_XINCREF(value);
3057 Py_XINCREF(tb);
3058 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003059
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003060 /* We support the following forms of raise:
3061 raise <class>, <classinstance>
3062 raise <class>, <argument tuple>
3063 raise <class>, None
3064 raise <class>, <argument>
3065 raise <classinstance>, None
3066 raise <string>, <object>
3067 raise <string>, None
3068
3069 An omitted second argument is the same as None.
3070
3071 In addition, raise <tuple>, <anything> is the same as
3072 raising the tuple's first item (and it better have one!);
3073 this rule is applied recursively.
3074
3075 Finally, an optional third argument can be supplied, which
3076 gives the traceback to be substituted (useful when
3077 re-raising an exception after examining it). */
3078
3079 /* First, check the traceback argument, replacing None with
3080 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003081 if (tb == Py_None) {
3082 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003083 tb = NULL;
3084 }
3085 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003086 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003087 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003088 goto raise_error;
3089 }
3090
3091 /* Next, replace a missing value with None */
3092 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003093 value = Py_None;
3094 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003095 }
3096
3097 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003098 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3099 PyObject *tmp = type;
3100 type = PyTuple_GET_ITEM(type, 0);
3101 Py_INCREF(type);
3102 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003103 }
3104
Brett Cannona7446e32006-02-27 23:39:10 +00003105 if (PyString_CheckExact(type)) {
Tim Petersafb2c802002-04-18 18:06:20 +00003106 /* Raising builtin string is deprecated but still allowed --
3107 * do nothing. Raising an instance of a new-style str
3108 * subclass is right out. */
Brett Cannonbf364092006-03-01 04:25:17 +00003109 if (PyErr_Warn(PyExc_DeprecationWarning,
Brett Cannona7446e32006-02-27 23:39:10 +00003110 "raising a string exception is deprecated"))
3111 goto raise_error;
3112 }
Brett Cannonbf364092006-03-01 04:25:17 +00003113 else if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003114 PyErr_NormalizeException(&type, &value, &tb);
3115
Brett Cannonbf364092006-03-01 04:25:17 +00003116 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003117 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003118 if (value != Py_None) {
3119 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003120 "instance exception may not have a separate value");
3121 goto raise_error;
3122 }
3123 else {
3124 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003125 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003126 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003127 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003128 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003129 }
3130 }
3131 else {
3132 /* Not something you can raise. You get an exception
3133 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003134 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00003135 "exceptions must be classes, instances, or "
3136 "strings (deprecated), not %s",
3137 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003138 goto raise_error;
3139 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003140 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003141 if (tb == NULL)
3142 return WHY_EXCEPTION;
3143 else
3144 return WHY_RERAISE;
3145 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003146 Py_XDECREF(value);
3147 Py_XDECREF(type);
3148 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003149 return WHY_EXCEPTION;
3150}
3151
Tim Petersd6d010b2001-06-21 02:49:55 +00003152/* Iterate v argcnt times and store the results on the stack (via decreasing
3153 sp). Return 1 for success, 0 if error. */
3154
Fredrik Lundh7a830892006-05-27 10:39:48 +00003155static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003156unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003157{
Tim Petersd6d010b2001-06-21 02:49:55 +00003158 int i = 0;
3159 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003160 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003161
Tim Petersd6d010b2001-06-21 02:49:55 +00003162 assert(v != NULL);
3163
3164 it = PyObject_GetIter(v);
3165 if (it == NULL)
3166 goto Error;
3167
3168 for (; i < argcnt; i++) {
3169 w = PyIter_Next(it);
3170 if (w == NULL) {
3171 /* Iterator done, via error or exhaustion. */
3172 if (!PyErr_Occurred()) {
3173 PyErr_Format(PyExc_ValueError,
3174 "need more than %d value%s to unpack",
3175 i, i == 1 ? "" : "s");
3176 }
3177 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003178 }
3179 *--sp = w;
3180 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003181
3182 /* We better have exhausted the iterator now. */
3183 w = PyIter_Next(it);
3184 if (w == NULL) {
3185 if (PyErr_Occurred())
3186 goto Error;
3187 Py_DECREF(it);
3188 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003189 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003190 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003191 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003192 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003193Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003194 for (; i > 0; i--, sp++)
3195 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003196 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003197 return 0;
3198}
3199
3200
Guido van Rossum96a42c81992-01-12 02:29:51 +00003201#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003202static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003203prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003204{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003205 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003206 if (PyObject_Print(v, stdout, 0) != 0)
3207 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003208 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003209 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003210}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003211#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003212
Fredrik Lundh7a830892006-05-27 10:39:48 +00003213static void
Fred Drake5755ce62001-06-27 19:19:46 +00003214call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003215{
Guido van Rossumb209a111997-04-29 18:18:01 +00003216 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003217 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003218 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003219 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003220 value = Py_None;
3221 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003222 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003223 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003224 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003225 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003226 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003227 }
Fred Drake5755ce62001-06-27 19:19:46 +00003228 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003229 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003230 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003231 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003232 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003233 Py_XDECREF(type);
3234 Py_XDECREF(value);
3235 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003236 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003237}
3238
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003239static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003240call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003241 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003242{
3243 PyObject *type, *value, *traceback;
3244 int err;
3245 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003246 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003247 if (err == 0)
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003248 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003249 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003250 return 0;
3251 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003252 else {
3253 Py_XDECREF(type);
3254 Py_XDECREF(value);
3255 Py_XDECREF(traceback);
Amaury Forgeot d'Arcc572dc32007-11-13 22:43:05 +00003256 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003257 }
3258}
3259
Fredrik Lundh7a830892006-05-27 10:39:48 +00003260static int
Fred Drake5755ce62001-06-27 19:19:46 +00003261call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3262 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003263{
Fred Drake5755ce62001-06-27 19:19:46 +00003264 register PyThreadState *tstate = frame->f_tstate;
3265 int result;
3266 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003267 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003268 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003269 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003270 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003271 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3272 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003273 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003274 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003275}
3276
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003277PyObject *
3278_PyEval_CallTracing(PyObject *func, PyObject *args)
3279{
3280 PyFrameObject *frame = PyEval_GetFrame();
3281 PyThreadState *tstate = frame->f_tstate;
3282 int save_tracing = tstate->tracing;
3283 int save_use_tracing = tstate->use_tracing;
3284 PyObject *result;
3285
3286 tstate->tracing = 0;
3287 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3288 || (tstate->c_profilefunc != NULL));
3289 result = PyObject_Call(func, args, NULL);
3290 tstate->tracing = save_tracing;
3291 tstate->use_tracing = save_use_tracing;
3292 return result;
3293}
3294
Fredrik Lundh7a830892006-05-27 10:39:48 +00003295static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003296maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003297 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3298 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003299{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003300 int result = 0;
3301
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003302 /* If the last instruction executed isn't in the current
3303 instruction window, reset the window. If the last
3304 instruction happens to fall at the start of a line or if it
3305 represents a jump backwards, call the trace function.
3306 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003307 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003308 int line;
3309 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003310
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003311 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3312 &bounds);
3313 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003314 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003315 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003316 PyTrace_LINE, Py_None);
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003317 }
3318 *instr_lb = bounds.ap_lower;
3319 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003320 }
Armin Rigobf57a142004-03-22 19:24:58 +00003321 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003322 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003323 }
3324 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003325 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003326}
3327
Fred Drake5755ce62001-06-27 19:19:46 +00003328void
3329PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003330{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003331 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003332 PyObject *temp = tstate->c_profileobj;
3333 Py_XINCREF(arg);
3334 tstate->c_profilefunc = NULL;
3335 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003336 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003337 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003338 Py_XDECREF(temp);
3339 tstate->c_profilefunc = func;
3340 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003341 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003342 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003343}
3344
3345void
3346PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3347{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003348 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003349 PyObject *temp = tstate->c_traceobj;
3350 Py_XINCREF(arg);
3351 tstate->c_tracefunc = NULL;
3352 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003353 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003354 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003355 Py_XDECREF(temp);
3356 tstate->c_tracefunc = func;
3357 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003358 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003359 tstate->use_tracing = ((func != NULL)
3360 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003361}
3362
Guido van Rossumb209a111997-04-29 18:18:01 +00003363PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003364PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003365{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003366 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003367 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003368 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003369 else
3370 return current_frame->f_builtins;
3371}
3372
Guido van Rossumb209a111997-04-29 18:18:01 +00003373PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003374PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003375{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003376 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003377 if (current_frame == NULL)
3378 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003379 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003380 return current_frame->f_locals;
3381}
3382
Guido van Rossumb209a111997-04-29 18:18:01 +00003383PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003384PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003385{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003386 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003387 if (current_frame == NULL)
3388 return NULL;
3389 else
3390 return current_frame->f_globals;
3391}
3392
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003393PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003394PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003395{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003396 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003397 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003398}
3399
Guido van Rossum6135a871995-01-09 17:53:26 +00003400int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003401PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003402{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003403 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003404 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003405}
3406
Guido van Rossumbe270261997-05-22 22:26:18 +00003407int
Tim Peters5ba58662001-07-16 02:29:45 +00003408PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003409{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003410 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003411 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003412
3413 if (current_frame != NULL) {
3414 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003415 const int compilerflags = codeflags & PyCF_MASK;
3416 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003417 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003418 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003419 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003420#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003421 if (codeflags & CO_GENERATOR_ALLOWED) {
3422 result = 1;
3423 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3424 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003425#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003426 }
3427 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003428}
3429
3430int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003431Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003432{
Guido van Rossumb209a111997-04-29 18:18:01 +00003433 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003434 if (f == NULL)
3435 return 0;
3436 if (!PyFile_SoftSpace(f, 0))
3437 return 0;
3438 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003439}
3440
Guido van Rossum3f5da241990-12-20 15:06:42 +00003441
Guido van Rossum681d79a1995-07-18 14:51:37 +00003442/* External interface to call any callable object.
3443 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003444
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003445#undef PyEval_CallObject
3446/* for backward compatibility: export this interface */
3447
Guido van Rossumb209a111997-04-29 18:18:01 +00003448PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003449PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003450{
Guido van Rossumb209a111997-04-29 18:18:01 +00003451 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003452}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003453#define PyEval_CallObject(func,arg) \
3454 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003455
Guido van Rossumb209a111997-04-29 18:18:01 +00003456PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003457PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003458{
Jeremy Hylton52820442001-01-03 23:52:36 +00003459 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003460
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003461 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003462 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003463 if (arg == NULL)
3464 return NULL;
3465 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003466 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003467 PyErr_SetString(PyExc_TypeError,
3468 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003469 return NULL;
3470 }
3471 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003472 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003473
Guido van Rossumb209a111997-04-29 18:18:01 +00003474 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003475 PyErr_SetString(PyExc_TypeError,
3476 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003477 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003478 return NULL;
3479 }
3480
Tim Peters6d6c1a32001-08-02 04:15:00 +00003481 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003482 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003483 return result;
3484}
3485
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003486const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003487PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003488{
3489 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003490 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003491 else if (PyFunction_Check(func))
3492 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3493 else if (PyCFunction_Check(func))
3494 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3495 else if (PyClass_Check(func))
3496 return PyString_AsString(((PyClassObject*)func)->cl_name);
3497 else if (PyInstance_Check(func)) {
3498 return PyString_AsString(
3499 ((PyInstanceObject*)func)->in_class->cl_name);
3500 } else {
3501 return func->ob_type->tp_name;
3502 }
3503}
3504
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003505const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003506PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003507{
3508 if (PyMethod_Check(func))
3509 return "()";
3510 else if (PyFunction_Check(func))
3511 return "()";
3512 else if (PyCFunction_Check(func))
3513 return "()";
3514 else if (PyClass_Check(func))
3515 return " constructor";
3516 else if (PyInstance_Check(func)) {
3517 return " instance";
3518 } else {
3519 return " object";
3520 }
3521}
3522
Fredrik Lundh7a830892006-05-27 10:39:48 +00003523static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003524err_args(PyObject *func, int flags, int nargs)
3525{
3526 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003527 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003528 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003529 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003530 nargs);
3531 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003532 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003533 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003534 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003535 nargs);
3536}
3537
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003538#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003539if (tstate->use_tracing && tstate->c_profilefunc) { \
3540 if (call_trace(tstate->c_profilefunc, \
3541 tstate->c_profileobj, \
3542 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003543 func)) { \
3544 x = NULL; \
3545 } \
3546 else { \
3547 x = call; \
3548 if (tstate->c_profilefunc != NULL) { \
3549 if (x == NULL) { \
3550 call_trace_protected(tstate->c_profilefunc, \
3551 tstate->c_profileobj, \
3552 tstate->frame, PyTrace_C_EXCEPTION, \
3553 func); \
3554 /* XXX should pass (type, value, tb) */ \
3555 } else { \
3556 if (call_trace(tstate->c_profilefunc, \
3557 tstate->c_profileobj, \
3558 tstate->frame, PyTrace_C_RETURN, \
3559 func)) { \
3560 Py_DECREF(x); \
3561 x = NULL; \
3562 } \
3563 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003564 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003565 } \
3566} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003567 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003568 }
3569
Fredrik Lundh7a830892006-05-27 10:39:48 +00003570static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003571call_function(PyObject ***pp_stack, int oparg
3572#ifdef WITH_TSC
3573 , uint64* pintr0, uint64* pintr1
3574#endif
3575 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003576{
3577 int na = oparg & 0xff;
3578 int nk = (oparg>>8) & 0xff;
3579 int n = na + 2 * nk;
3580 PyObject **pfunc = (*pp_stack) - n - 1;
3581 PyObject *func = *pfunc;
3582 PyObject *x, *w;
3583
Jeremy Hylton985eba52003-02-05 23:13:00 +00003584 /* Always dispatch PyCFunction first, because these are
3585 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003586 */
3587 if (PyCFunction_Check(func) && nk == 0) {
3588 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003589 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003590
3591 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003592 if (flags & (METH_NOARGS | METH_O)) {
3593 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3594 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003595 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003596 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003597 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003598 else if (flags & METH_O && na == 1) {
3599 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003600 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003601 Py_DECREF(arg);
3602 }
3603 else {
3604 err_args(func, flags, na);
3605 x = NULL;
3606 }
3607 }
3608 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003609 PyObject *callargs;
3610 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003611 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003612 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003613 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003614 Py_XDECREF(callargs);
3615 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003616 } else {
3617 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3618 /* optimize access to bound methods */
3619 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003620 PCALL(PCALL_METHOD);
3621 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003622 Py_INCREF(self);
3623 func = PyMethod_GET_FUNCTION(func);
3624 Py_INCREF(func);
3625 Py_DECREF(*pfunc);
3626 *pfunc = self;
3627 na++;
3628 n++;
3629 } else
3630 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003631 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003632 if (PyFunction_Check(func))
3633 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003634 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003635 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003636 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003637 Py_DECREF(func);
3638 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003639
Armin Rigod34fa522006-03-28 19:10:40 +00003640 /* Clear the stack of the function object. Also removes
3641 the arguments in case they weren't consumed already
3642 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003643 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003644 while ((*pp_stack) > pfunc) {
3645 w = EXT_POP(*pp_stack);
3646 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003647 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003648 }
3649 return x;
3650}
3651
Jeremy Hylton192690e2002-08-16 18:36:11 +00003652/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003653 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003654 For the simplest case -- a function that takes only positional
3655 arguments and is called with only positional arguments -- it
3656 inlines the most primitive frame setup code from
3657 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3658 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003659*/
3660
Fredrik Lundh7a830892006-05-27 10:39:48 +00003661static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003662fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003663{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003664 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003665 PyObject *globals = PyFunction_GET_GLOBALS(func);
3666 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3667 PyObject **d = NULL;
3668 int nd = 0;
3669
Jeremy Hylton985eba52003-02-05 23:13:00 +00003670 PCALL(PCALL_FUNCTION);
3671 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003672 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003673 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3674 PyFrameObject *f;
3675 PyObject *retval = NULL;
3676 PyThreadState *tstate = PyThreadState_GET();
3677 PyObject **fastlocals, **stack;
3678 int i;
3679
3680 PCALL(PCALL_FASTER_FUNCTION);
3681 assert(globals != NULL);
3682 /* XXX Perhaps we should create a specialized
3683 PyFrame_New() that doesn't take locals, but does
3684 take builtins without sanity checking them.
3685 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003686 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003687 f = PyFrame_New(tstate, co, globals, NULL);
3688 if (f == NULL)
3689 return NULL;
3690
3691 fastlocals = f->f_localsplus;
3692 stack = (*pp_stack) - n;
3693
3694 for (i = 0; i < n; i++) {
3695 Py_INCREF(*stack);
3696 fastlocals[i] = *stack++;
3697 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003698 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003699 ++tstate->recursion_depth;
3700 Py_DECREF(f);
3701 --tstate->recursion_depth;
3702 return retval;
3703 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003704 if (argdefs != NULL) {
3705 d = &PyTuple_GET_ITEM(argdefs, 0);
3706 nd = ((PyTupleObject *)argdefs)->ob_size;
3707 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003708 return PyEval_EvalCodeEx(co, globals,
3709 (PyObject *)NULL, (*pp_stack)-n, na,
3710 (*pp_stack)-2*nk, nk, d, nd,
3711 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003712}
3713
Fredrik Lundh7a830892006-05-27 10:39:48 +00003714static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003715update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3716 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003717{
3718 PyObject *kwdict = NULL;
3719 if (orig_kwdict == NULL)
3720 kwdict = PyDict_New();
3721 else {
3722 kwdict = PyDict_Copy(orig_kwdict);
3723 Py_DECREF(orig_kwdict);
3724 }
3725 if (kwdict == NULL)
3726 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003727 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003728 int err;
3729 PyObject *value = EXT_POP(*pp_stack);
3730 PyObject *key = EXT_POP(*pp_stack);
3731 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003732 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003733 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003734 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003735 PyEval_GetFuncName(func),
3736 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003737 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003738 Py_DECREF(key);
3739 Py_DECREF(value);
3740 Py_DECREF(kwdict);
3741 return NULL;
3742 }
3743 err = PyDict_SetItem(kwdict, key, value);
3744 Py_DECREF(key);
3745 Py_DECREF(value);
3746 if (err) {
3747 Py_DECREF(kwdict);
3748 return NULL;
3749 }
3750 }
3751 return kwdict;
3752}
3753
Fredrik Lundh7a830892006-05-27 10:39:48 +00003754static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003755update_star_args(int nstack, int nstar, PyObject *stararg,
3756 PyObject ***pp_stack)
3757{
3758 PyObject *callargs, *w;
3759
3760 callargs = PyTuple_New(nstack + nstar);
3761 if (callargs == NULL) {
3762 return NULL;
3763 }
3764 if (nstar) {
3765 int i;
3766 for (i = 0; i < nstar; i++) {
3767 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3768 Py_INCREF(a);
3769 PyTuple_SET_ITEM(callargs, nstack + i, a);
3770 }
3771 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003772 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003773 w = EXT_POP(*pp_stack);
3774 PyTuple_SET_ITEM(callargs, nstack, w);
3775 }
3776 return callargs;
3777}
3778
Fredrik Lundh7a830892006-05-27 10:39:48 +00003779static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003780load_args(PyObject ***pp_stack, int na)
3781{
3782 PyObject *args = PyTuple_New(na);
3783 PyObject *w;
3784
3785 if (args == NULL)
3786 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003787 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003788 w = EXT_POP(*pp_stack);
3789 PyTuple_SET_ITEM(args, na, w);
3790 }
3791 return args;
3792}
3793
Fredrik Lundh7a830892006-05-27 10:39:48 +00003794static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003795do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3796{
3797 PyObject *callargs = NULL;
3798 PyObject *kwdict = NULL;
3799 PyObject *result = NULL;
3800
3801 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003802 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003803 if (kwdict == NULL)
3804 goto call_fail;
3805 }
3806 callargs = load_args(pp_stack, na);
3807 if (callargs == NULL)
3808 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003809#ifdef CALL_PROFILE
3810 /* At this point, we have to look at the type of func to
3811 update the call stats properly. Do it here so as to avoid
3812 exposing the call stats machinery outside ceval.c
3813 */
3814 if (PyFunction_Check(func))
3815 PCALL(PCALL_FUNCTION);
3816 else if (PyMethod_Check(func))
3817 PCALL(PCALL_METHOD);
3818 else if (PyType_Check(func))
3819 PCALL(PCALL_TYPE);
3820 else
3821 PCALL(PCALL_OTHER);
3822#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003823 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003824 call_fail:
3825 Py_XDECREF(callargs);
3826 Py_XDECREF(kwdict);
3827 return result;
3828}
3829
Fredrik Lundh7a830892006-05-27 10:39:48 +00003830static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003831ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3832{
3833 int nstar = 0;
3834 PyObject *callargs = NULL;
3835 PyObject *stararg = NULL;
3836 PyObject *kwdict = NULL;
3837 PyObject *result = NULL;
3838
3839 if (flags & CALL_FLAG_KW) {
3840 kwdict = EXT_POP(*pp_stack);
3841 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003842 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003843 "%s%s argument after ** "
3844 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003845 PyEval_GetFuncName(func),
3846 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003847 goto ext_call_fail;
3848 }
3849 }
3850 if (flags & CALL_FLAG_VAR) {
3851 stararg = EXT_POP(*pp_stack);
3852 if (!PyTuple_Check(stararg)) {
3853 PyObject *t = NULL;
3854 t = PySequence_Tuple(stararg);
3855 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003856 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3857 PyErr_Format(PyExc_TypeError,
3858 "%s%s argument after * "
3859 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003860 PyEval_GetFuncName(func),
3861 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003862 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003863 goto ext_call_fail;
3864 }
3865 Py_DECREF(stararg);
3866 stararg = t;
3867 }
3868 nstar = PyTuple_GET_SIZE(stararg);
3869 }
3870 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003871 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003872 if (kwdict == NULL)
3873 goto ext_call_fail;
3874 }
3875 callargs = update_star_args(na, nstar, stararg, pp_stack);
3876 if (callargs == NULL)
3877 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003878#ifdef CALL_PROFILE
3879 /* At this point, we have to look at the type of func to
3880 update the call stats properly. Do it here so as to avoid
3881 exposing the call stats machinery outside ceval.c
3882 */
3883 if (PyFunction_Check(func))
3884 PCALL(PCALL_FUNCTION);
3885 else if (PyMethod_Check(func))
3886 PCALL(PCALL_METHOD);
3887 else if (PyType_Check(func))
3888 PCALL(PCALL_TYPE);
3889 else
3890 PCALL(PCALL_OTHER);
3891#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003892 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003893 ext_call_fail:
3894 Py_XDECREF(callargs);
3895 Py_XDECREF(kwdict);
3896 Py_XDECREF(stararg);
3897 return result;
3898}
3899
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003900/* Extract a slice index from a PyInt or PyLong or an object with the
3901 nb_index slot defined, and store in *pi.
3902 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3903 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 +00003904 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003905*/
Tim Petersb5196382001-12-16 19:44:20 +00003906/* Note: If v is NULL, return success without storing into *pi. This
3907 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3908 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003909*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003910int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003911_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003912{
Tim Petersb5196382001-12-16 19:44:20 +00003913 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003914 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003915 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003916 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3917 however, it looks like it should be AsSsize_t.
3918 There should be a comment here explaining why.
3919 */
3920 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003921 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003922 else if (PyIndex_Check(v)) {
3923 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003924 if (x == -1 && PyErr_Occurred())
3925 return 0;
3926 }
3927 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003928 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003929 "slice indices must be integers or "
3930 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003931 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003932 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003933 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003934 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003935 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003936}
3937
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003938#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003939#define ISINDEX(x) ((x) == NULL || \
3940 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003941
Fredrik Lundh7a830892006-05-27 10:39:48 +00003942static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003943apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003944{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003945 PyTypeObject *tp = u->ob_type;
3946 PySequenceMethods *sq = tp->tp_as_sequence;
3947
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003948 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003949 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003950 if (!_PyEval_SliceIndex(v, &ilow))
3951 return NULL;
3952 if (!_PyEval_SliceIndex(w, &ihigh))
3953 return NULL;
3954 return PySequence_GetSlice(u, ilow, ihigh);
3955 }
3956 else {
3957 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003958 if (slice != NULL) {
3959 PyObject *res = PyObject_GetItem(u, slice);
3960 Py_DECREF(slice);
3961 return res;
3962 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003963 else
3964 return NULL;
3965 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003966}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003967
Fredrik Lundh7a830892006-05-27 10:39:48 +00003968static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003969assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3970 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003971{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003972 PyTypeObject *tp = u->ob_type;
3973 PySequenceMethods *sq = tp->tp_as_sequence;
3974
Georg Brandl0ea89162007-03-05 22:28:13 +00003975 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003976 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003977 if (!_PyEval_SliceIndex(v, &ilow))
3978 return -1;
3979 if (!_PyEval_SliceIndex(w, &ihigh))
3980 return -1;
3981 if (x == NULL)
3982 return PySequence_DelSlice(u, ilow, ihigh);
3983 else
3984 return PySequence_SetSlice(u, ilow, ihigh, x);
3985 }
3986 else {
3987 PyObject *slice = PySlice_New(v, w, NULL);
3988 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003989 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003990 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003991 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003992 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003993 res = PyObject_DelItem(u, slice);
3994 Py_DECREF(slice);
3995 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003996 }
3997 else
3998 return -1;
3999 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004000}
4001
Fredrik Lundh7a830892006-05-27 10:39:48 +00004002static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004003cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004004{
Guido van Rossumac7be682001-01-17 15:42:30 +00004005 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004006 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004007 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004008 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004009 break;
4010 case PyCmp_IS_NOT:
4011 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004012 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004013 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004014 res = PySequence_Contains(w, v);
4015 if (res < 0)
4016 return NULL;
4017 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004018 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004019 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004020 if (res < 0)
4021 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004022 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004023 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004024 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004025 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004026 break;
4027 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004028 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004029 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004030 v = res ? Py_True : Py_False;
4031 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004032 return v;
4033}
4034
Fredrik Lundh7a830892006-05-27 10:39:48 +00004035static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004036import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004037{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004038 PyObject *x;
4039
4040 x = PyObject_GetAttr(v, name);
4041 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004042 PyErr_Format(PyExc_ImportError,
4043 "cannot import name %.230s",
4044 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004045 }
Thomas Wouters52152252000-08-17 22:55:00 +00004046 return x;
4047}
Guido van Rossumac7be682001-01-17 15:42:30 +00004048
Fredrik Lundh7a830892006-05-27 10:39:48 +00004049static int
Thomas Wouters52152252000-08-17 22:55:00 +00004050import_all_from(PyObject *locals, PyObject *v)
4051{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004052 PyObject *all = PyObject_GetAttrString(v, "__all__");
4053 PyObject *dict, *name, *value;
4054 int skip_leading_underscores = 0;
4055 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004056
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004057 if (all == NULL) {
4058 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4059 return -1; /* Unexpected error */
4060 PyErr_Clear();
4061 dict = PyObject_GetAttrString(v, "__dict__");
4062 if (dict == NULL) {
4063 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4064 return -1;
4065 PyErr_SetString(PyExc_ImportError,
4066 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004067 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004068 }
4069 all = PyMapping_Keys(dict);
4070 Py_DECREF(dict);
4071 if (all == NULL)
4072 return -1;
4073 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004074 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004075
4076 for (pos = 0, err = 0; ; pos++) {
4077 name = PySequence_GetItem(all, pos);
4078 if (name == NULL) {
4079 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4080 err = -1;
4081 else
4082 PyErr_Clear();
4083 break;
4084 }
4085 if (skip_leading_underscores &&
4086 PyString_Check(name) &&
4087 PyString_AS_STRING(name)[0] == '_')
4088 {
4089 Py_DECREF(name);
4090 continue;
4091 }
4092 value = PyObject_GetAttr(v, name);
4093 if (value == NULL)
4094 err = -1;
Armin Rigo1bc1ab22006-11-29 22:07:38 +00004095 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004096 err = PyDict_SetItem(locals, name, value);
Armin Rigo1bc1ab22006-11-29 22:07:38 +00004097 else
4098 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004099 Py_DECREF(name);
4100 Py_XDECREF(value);
4101 if (err != 0)
4102 break;
4103 }
4104 Py_DECREF(all);
4105 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004106}
4107
Fredrik Lundh7a830892006-05-27 10:39:48 +00004108static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004109build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004110{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004111 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004112
4113 if (PyDict_Check(methods))
4114 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004115 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004116 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004117 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4118 base = PyTuple_GET_ITEM(bases, 0);
4119 metaclass = PyObject_GetAttrString(base, "__class__");
4120 if (metaclass == NULL) {
4121 PyErr_Clear();
4122 metaclass = (PyObject *)base->ob_type;
4123 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004124 }
4125 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004126 else {
4127 PyObject *g = PyEval_GetGlobals();
4128 if (g != NULL && PyDict_Check(g))
4129 metaclass = PyDict_GetItemString(g, "__metaclass__");
4130 if (metaclass == NULL)
4131 metaclass = (PyObject *) &PyClass_Type;
4132 Py_INCREF(metaclass);
4133 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00004134 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004135 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004136 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004137 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004138 in a base that was not a class (such the random module
4139 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004140 by augmenting the error message with more information.*/
4141
4142 PyObject *ptype, *pvalue, *ptraceback;
4143
4144 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4145 if (PyString_Check(pvalue)) {
4146 PyObject *newmsg;
4147 newmsg = PyString_FromFormat(
4148 "Error when calling the metaclass bases\n %s",
4149 PyString_AS_STRING(pvalue));
4150 if (newmsg != NULL) {
4151 Py_DECREF(pvalue);
4152 pvalue = newmsg;
4153 }
4154 }
4155 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004156 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004157 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004158}
4159
Fredrik Lundh7a830892006-05-27 10:39:48 +00004160static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004161exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4162 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004163{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004164 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004165 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004166 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004167
Guido van Rossumb209a111997-04-29 18:18:01 +00004168 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4169 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004171 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004173 locals = PyTuple_GetItem(prog, 2);
4174 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004175 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004176 if (globals == Py_None) {
4177 globals = PyEval_GetGlobals();
4178 if (locals == Py_None) {
4179 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004180 plain = 1;
4181 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004182 if (!globals || !locals) {
4183 PyErr_SetString(PyExc_SystemError,
4184 "globals and locals cannot be NULL");
4185 return -1;
4186 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004187 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004188 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004189 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004190 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004191 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004192 !PyCode_Check(prog) &&
4193 !PyFile_Check(prog)) {
4194 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004195 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004196 return -1;
4197 }
Fred Drake661ea262000-10-24 19:57:45 +00004198 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004199 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004200 "exec: arg 2 must be a dictionary or None");
4201 return -1;
4202 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004203 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004204 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004205 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004206 return -1;
4207 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004208 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004209 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004210 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004211 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4212 PyErr_SetString(PyExc_TypeError,
4213 "code object passed to exec may not contain free variables");
4214 return -1;
4215 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004216 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004217 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004218 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004219 FILE *fp = PyFile_AsFile(prog);
4220 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004221 PyCompilerFlags cf;
Neal Norwitza5f5f142007-02-25 16:19:21 +00004222 if (name == NULL)
4223 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004224 cf.cf_flags = 0;
4225 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004226 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004227 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004228 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004229 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004230 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004231 }
4232 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004233 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004234 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004235 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004236 cf.cf_flags = 0;
4237#ifdef Py_USING_UNICODE
4238 if (PyUnicode_Check(prog)) {
4239 tmp = PyUnicode_AsUTF8String(prog);
4240 if (tmp == NULL)
4241 return -1;
4242 prog = tmp;
4243 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4244 }
4245#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004246 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004247 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004248 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004249 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004250 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004251 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004252 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004253 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004254 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004255 if (plain)
4256 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004257 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004258 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004259 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004260 return 0;
4261}
Guido van Rossum24c13741995-02-14 09:42:43 +00004262
Fredrik Lundh7a830892006-05-27 10:39:48 +00004263static void
Paul Prescode68140d2000-08-30 20:25:01 +00004264format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4265{
4266 char *obj_str;
4267
4268 if (!obj)
4269 return;
4270
4271 obj_str = PyString_AsString(obj);
4272 if (!obj_str)
4273 return;
4274
4275 PyErr_Format(exc, format_str, obj_str);
4276}
Guido van Rossum950361c1997-01-24 13:49:28 +00004277
Fredrik Lundh7a830892006-05-27 10:39:48 +00004278static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004279string_concatenate(PyObject *v, PyObject *w,
4280 PyFrameObject *f, unsigned char *next_instr)
4281{
4282 /* This function implements 'variable += expr' when both arguments
4283 are strings. */
Armin Rigo97ff0472006-08-09 15:37:26 +00004284 Py_ssize_t v_len = PyString_GET_SIZE(v);
4285 Py_ssize_t w_len = PyString_GET_SIZE(w);
4286 Py_ssize_t new_len = v_len + w_len;
4287 if (new_len < 0) {
4288 PyErr_SetString(PyExc_OverflowError,
4289 "strings are too large to concat");
4290 return NULL;
4291 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004292
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004293 if (v->ob_refcnt == 2) {
4294 /* In the common case, there are 2 references to the value
4295 * stored in 'variable' when the += is performed: one on the
4296 * value stack (in 'v') and one still stored in the 'variable'.
4297 * We try to delete the variable now to reduce the refcnt to 1.
4298 */
4299 switch (*next_instr) {
4300 case STORE_FAST:
4301 {
4302 int oparg = PEEKARG();
4303 PyObject **fastlocals = f->f_localsplus;
4304 if (GETLOCAL(oparg) == v)
4305 SETLOCAL(oparg, NULL);
4306 break;
4307 }
4308 case STORE_DEREF:
4309 {
Richard Jonescebbefc2006-05-23 18:28:17 +00004310 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004311 PyObject *c = freevars[PEEKARG()];
4312 if (PyCell_GET(c) == v)
4313 PyCell_Set(c, NULL);
4314 break;
4315 }
4316 case STORE_NAME:
4317 {
4318 PyObject *names = f->f_code->co_names;
4319 PyObject *name = GETITEM(names, PEEKARG());
4320 PyObject *locals = f->f_locals;
4321 if (PyDict_CheckExact(locals) &&
4322 PyDict_GetItem(locals, name) == v) {
4323 if (PyDict_DelItem(locals, name) != 0) {
4324 PyErr_Clear();
4325 }
4326 }
4327 break;
4328 }
4329 }
4330 }
4331
Armin Rigo618fbf52004-08-07 20:58:32 +00004332 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004333 /* Now we own the last reference to 'v', so we can resize it
4334 * in-place.
4335 */
Armin Rigo97ff0472006-08-09 15:37:26 +00004336 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004337 /* XXX if _PyString_Resize() fails, 'v' has been
4338 * deallocated so it cannot be put back into 'variable'.
4339 * The MemoryError is raised when there is no value in
4340 * 'variable', which might (very remotely) be a cause
4341 * of incompatibilities.
4342 */
4343 return NULL;
4344 }
4345 /* copy 'w' into the newly allocated area of 'v' */
4346 memcpy(PyString_AS_STRING(v) + v_len,
4347 PyString_AS_STRING(w), w_len);
4348 return v;
4349 }
4350 else {
4351 /* When in-place resizing is not an option. */
4352 PyString_Concat(&v, w);
4353 return v;
4354 }
4355}
4356
Guido van Rossum950361c1997-01-24 13:49:28 +00004357#ifdef DYNAMIC_EXECUTION_PROFILE
4358
Fredrik Lundh7a830892006-05-27 10:39:48 +00004359static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004360getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004361{
4362 int i;
4363 PyObject *l = PyList_New(256);
4364 if (l == NULL) return NULL;
4365 for (i = 0; i < 256; i++) {
4366 PyObject *x = PyInt_FromLong(a[i]);
4367 if (x == NULL) {
4368 Py_DECREF(l);
4369 return NULL;
4370 }
4371 PyList_SetItem(l, i, x);
4372 }
4373 for (i = 0; i < 256; i++)
4374 a[i] = 0;
4375 return l;
4376}
4377
4378PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004379_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004380{
4381#ifndef DXPAIRS
4382 return getarray(dxp);
4383#else
4384 int i;
4385 PyObject *l = PyList_New(257);
4386 if (l == NULL) return NULL;
4387 for (i = 0; i < 257; i++) {
4388 PyObject *x = getarray(dxpairs[i]);
4389 if (x == NULL) {
4390 Py_DECREF(l);
4391 return NULL;
4392 }
4393 PyList_SetItem(l, i, x);
4394 }
4395 return l;
4396#endif
4397}
4398
4399#endif