blob: 2cebf44a4c971214f7c9e73177aef71dbf35e9b2 [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
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Martin v. Löwise440e472004-06-01 15:22:42 +000013#include "genobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000014#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000015#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000016#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017
Guido van Rossumc6004111993-11-05 10:22:19 +000018#include <ctype.h>
19
Guido van Rossum04691fc1992-08-12 15:35:34 +000020/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000021/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000022
Guido van Rossum408027e1996-12-30 16:17:54 +000023#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000024/* For debugging the interpreter: */
25#define LLTRACE 1 /* Low-level trace feature */
26#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#endif
28
Jeremy Hylton52820442001-01-03 23:52:36 +000029typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000030
Guido van Rossum374a9221991-04-04 10:40:29 +000031/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000032static PyObject *eval_frame(PyFrameObject *);
Jeremy Hyltone8c04322002-08-16 17:47:26 +000033static PyObject *call_function(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000034static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000035static PyObject *do_call(PyObject *, PyObject ***, int, int);
36static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000037static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000038static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000039static PyObject *load_args(PyObject ***, int);
40#define CALL_FLAG_VAR 1
41#define CALL_FLAG_KW 2
42
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000044static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000045#endif
Fred Drake5755ce62001-06-27 19:19:46 +000046static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
47 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000048static void call_trace_protected(Py_tracefunc, PyObject *,
49 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000050static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000051static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +000052 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000053
Tim Petersdbd9ba62000-07-09 03:09:57 +000054static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
55static int assign_slice(PyObject *, PyObject *,
56 PyObject *, PyObject *);
57static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000058static PyObject *import_from(PyObject *, PyObject *);
59static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000060static PyObject *build_class(PyObject *, PyObject *, PyObject *);
61static int exec_statement(PyFrameObject *,
62 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
64static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000065static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000066
Paul Prescode68140d2000-08-30 20:25:01 +000067#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000068 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000069#define GLOBAL_NAME_ERROR_MSG \
70 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000071#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000072 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000073#define UNBOUNDFREE_ERROR_MSG \
74 "free variable '%.200s' referenced before assignment" \
75 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000076
Guido van Rossum950361c1997-01-24 13:49:28 +000077/* Dynamic execution profile */
78#ifdef DYNAMIC_EXECUTION_PROFILE
79#ifdef DXPAIRS
80static long dxpairs[257][256];
81#define dxp dxpairs[256]
82#else
83static long dxp[256];
84#endif
85#endif
86
Jeremy Hylton985eba52003-02-05 23:13:00 +000087/* Function call profile */
88#ifdef CALL_PROFILE
89#define PCALL_NUM 11
90static int pcall[PCALL_NUM];
91
92#define PCALL_ALL 0
93#define PCALL_FUNCTION 1
94#define PCALL_FAST_FUNCTION 2
95#define PCALL_FASTER_FUNCTION 3
96#define PCALL_METHOD 4
97#define PCALL_BOUND_METHOD 5
98#define PCALL_CFUNCTION 6
99#define PCALL_TYPE 7
100#define PCALL_GENERATOR 8
101#define PCALL_OTHER 9
102#define PCALL_POP 10
103
104/* Notes about the statistics
105
106 PCALL_FAST stats
107
108 FAST_FUNCTION means no argument tuple needs to be created.
109 FASTER_FUNCTION means that the fast-path frame setup code is used.
110
111 If there is a method call where the call can be optimized by changing
112 the argument tuple and calling the function directly, it gets recorded
113 twice.
114
115 As a result, the relationship among the statistics appears to be
116 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
117 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
118 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
119 PCALL_METHOD > PCALL_BOUND_METHOD
120*/
121
122#define PCALL(POS) pcall[POS]++
123
124PyObject *
125PyEval_GetCallStats(PyObject *self)
126{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000127 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000128 pcall[0], pcall[1], pcall[2], pcall[3],
129 pcall[4], pcall[5], pcall[6], pcall[7],
130 pcall[8], pcall[9]);
131}
132#else
133#define PCALL(O)
134
135PyObject *
136PyEval_GetCallStats(PyObject *self)
137{
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141#endif
142
Tim Peters5ca576e2001-06-18 22:08:13 +0000143
Guido van Rossume59214e1994-08-30 08:01:59 +0000144#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000145
Guido van Rossum2571cc81999-04-07 16:07:23 +0000146#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000148#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000149#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000150
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151extern int _PyThread_Started; /* Flag for Py_Exit */
152
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000153static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000154static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000155
156void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000157PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000159 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000160 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000162 interpreter_lock = PyThread_allocate_lock();
163 PyThread_acquire_lock(interpreter_lock, 1);
164 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000166
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000167void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000168PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000170 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000171}
172
173void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000174PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000175{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000176 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177}
178
179void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000180PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000181{
182 if (tstate == NULL)
183 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000184 /* Check someone has called PyEval_InitThreads() to create the lock */
185 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000186 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000187 if (PyThreadState_Swap(tstate) != NULL)
188 Py_FatalError(
189 "PyEval_AcquireThread: non-NULL old thread state");
190}
191
192void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000194{
195 if (tstate == NULL)
196 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
197 if (PyThreadState_Swap(NULL) != tstate)
198 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000199 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000200}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000201
202/* This function is called from PyOS_AfterFork to ensure that newly
203 created child processes don't hold locks referring to threads which
204 are not running in the child process. (This could also be done using
205 pthread_atfork mechanism, at least for the pthreads implementation.) */
206
207void
208PyEval_ReInitThreads(void)
209{
210 if (!interpreter_lock)
211 return;
212 /*XXX Can't use PyThread_free_lock here because it does too
213 much error-checking. Doing this cleanly would require
214 adding a new function to each thread_*.h. Instead, just
215 create a new lock and waste a little bit of memory */
216 interpreter_lock = PyThread_allocate_lock();
217 PyThread_acquire_lock(interpreter_lock, 1);
218 main_thread = PyThread_get_thread_ident();
219}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220#endif
221
Guido van Rossumff4949e1992-08-05 19:58:53 +0000222/* Functions save_thread and restore_thread are always defined so
223 dynamically loaded modules needn't be compiled separately for use
224 with and without threads: */
225
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000226PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000229 PyThreadState *tstate = PyThreadState_Swap(NULL);
230 if (tstate == NULL)
231 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000232#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000233 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000234 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000236 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000237}
238
239void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000241{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000242 if (tstate == NULL)
243 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000244#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000245 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000246 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249 }
250#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000251 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000252}
253
254
Guido van Rossuma9672091994-09-14 13:31:22 +0000255/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
256 signal handlers or Mac I/O completion routines) can schedule calls
257 to a function to be called synchronously.
258 The synchronous function is called with one void* argument.
259 It should return 0 for success or -1 for failure -- failure should
260 be accompanied by an exception.
261
262 If registry succeeds, the registry function returns 0; if it fails
263 (e.g. due to too many pending calls) it returns -1 (without setting
264 an exception condition).
265
266 Note that because registry may occur from within signal handlers,
267 or other asynchronous events, calling malloc() is unsafe!
268
269#ifdef WITH_THREAD
270 Any thread can schedule pending calls, but only the main thread
271 will execute them.
272#endif
273
274 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
275 There are two possible race conditions:
276 (1) nested asynchronous registry calls;
277 (2) registry calls made while pending calls are being processed.
278 While (1) is very unlikely, (2) is a real possibility.
279 The current code is safe against (2), but not against (1).
280 The safety against (2) is derived from the fact that only one
281 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000282
Guido van Rossuma027efa1997-05-05 20:56:21 +0000283 XXX Darn! With the advent of thread state, we should have an array
284 of pending calls per thread in the thread state! Later...
285*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000286
Guido van Rossuma9672091994-09-14 13:31:22 +0000287#define NPENDINGCALLS 32
288static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000289 int (*func)(void *);
290 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000291} pendingcalls[NPENDINGCALLS];
292static volatile int pendingfirst = 0;
293static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000294static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000295
296int
Thomas Wouters334fb892000-07-25 12:56:38 +0000297Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000298{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000299 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000300 int i, j;
301 /* XXX Begin critical section */
302 /* XXX If you want this to be safe against nested
303 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000304 if (busy)
305 return -1;
306 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000307 i = pendinglast;
308 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000309 if (j == pendingfirst) {
310 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000311 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000312 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000313 pendingcalls[i].func = func;
314 pendingcalls[i].arg = arg;
315 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000316
317 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000318 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000319 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000320 /* XXX End critical section */
321 return 0;
322}
323
Guido van Rossum180d7b41994-09-29 09:45:57 +0000324int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000326{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000327 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000328#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000329 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000330 return 0;
331#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000332 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000333 return 0;
334 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000335 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000336 for (;;) {
337 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000338 int (*func)(void *);
339 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000340 i = pendingfirst;
341 if (i == pendinglast)
342 break; /* Queue empty */
343 func = pendingcalls[i].func;
344 arg = pendingcalls[i].arg;
345 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000346 if (func(arg) < 0) {
347 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000348 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000349 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000350 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000351 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000352 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000353 return 0;
354}
355
356
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000357/* The interpreter's recursion limit */
358
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000359static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000360int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000361
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000362int
363Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000364{
365 return recursion_limit;
366}
367
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000368void
369Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000370{
371 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000372 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000373}
374
Armin Rigo2b3eb402003-10-28 12:05:48 +0000375/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
376 if the recursion_depth reaches _Py_CheckRecursionLimit.
377 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
378 to guarantee that _Py_CheckRecursiveCall() is regularly called.
379 Without USE_STACKCHECK, there is no need for this. */
380int
381_Py_CheckRecursiveCall(char *where)
382{
383 PyThreadState *tstate = PyThreadState_GET();
384
385#ifdef USE_STACKCHECK
386 if (PyOS_CheckStack()) {
387 --tstate->recursion_depth;
388 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
389 return -1;
390 }
391#endif
392 if (tstate->recursion_depth > recursion_limit) {
393 --tstate->recursion_depth;
394 PyErr_Format(PyExc_RuntimeError,
395 "maximum recursion depth exceeded%s",
396 where);
397 return -1;
398 }
399 _Py_CheckRecursionLimit = recursion_limit;
400 return 0;
401}
402
Guido van Rossum374a9221991-04-04 10:40:29 +0000403/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000404enum why_code {
405 WHY_NOT = 0x0001, /* No error */
406 WHY_EXCEPTION = 0x0002, /* Exception occurred */
407 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
408 WHY_RETURN = 0x0008, /* 'return' statement */
409 WHY_BREAK = 0x0010, /* 'break' statement */
410 WHY_CONTINUE = 0x0020, /* 'continue' statement */
411 WHY_YIELD = 0x0040 /* 'yield' operator */
412};
Guido van Rossum374a9221991-04-04 10:40:29 +0000413
Raymond Hettinger7c958652004-04-06 10:11:10 +0000414static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000415static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000416
Skip Montanarod581d772002-09-03 20:10:45 +0000417/* for manipulating the thread switch and periodic "stuff" - used to be
418 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000419int _Py_CheckInterval = 100;
420volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000421
Guido van Rossumb209a111997-04-29 18:18:01 +0000422PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000424{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000425 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000426 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000427 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000428 (PyObject **)NULL, 0,
429 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000430 (PyObject **)NULL, 0,
431 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000432}
433
434
435/* Interpreter main loop */
436
Tim Peters6d6c1a32001-08-02 04:15:00 +0000437static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000438eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000439{
Guido van Rossum950361c1997-01-24 13:49:28 +0000440#ifdef DXPAIRS
441 int lastopcode = 0;
442#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000443 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000444 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000445 register int opcode=0; /* Current opcode */
446 register int oparg=0; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000447 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000448 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000449 register PyObject *x; /* Result object -- NULL if error */
450 register PyObject *v; /* Temporary objects popped off stack */
451 register PyObject *w;
452 register PyObject *u;
453 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000454 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000455 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000456 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000457 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000458 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000459
Tim Peters8a5c3c72004-04-05 19:36:21 +0000460 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000461
462 not (instr_lb <= current_bytecode_offset < instr_ub)
463
Tim Peters8a5c3c72004-04-05 19:36:21 +0000464 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000465 initial values are such as to make this false the first
466 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000467 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000468
Guido van Rossumd076c731998-10-07 19:42:25 +0000469 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000470 PyObject *names;
471 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000472#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000473 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000474#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000475#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000476 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000477 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000478#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000479
Neal Norwitza81d2202002-07-14 00:27:26 +0000480/* Tuple access macros */
481
482#ifndef Py_DEBUG
483#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
484#else
485#define GETITEM(v, i) PyTuple_GetItem((v), (i))
486#endif
487
Guido van Rossum374a9221991-04-04 10:40:29 +0000488/* Code access macros */
489
Guido van Rossumd076c731998-10-07 19:42:25 +0000490#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000491#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000492#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000493#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000494#define JUMPBY(x) (next_instr += (x))
495
Raymond Hettingerf606f872003-03-16 03:11:04 +0000496/* OpCode prediction macros
497 Some opcodes tend to come in pairs thus making it possible to predict
498 the second code when the first is run. For example, COMPARE_OP is often
499 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
500 followed by a POP_TOP.
501
502 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000503 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000504 processor has a high likelihood of making its own successful branch
505 prediction which results in a nearly zero overhead transition to the
506 next opcode.
507
508 A successful prediction saves a trip through the eval-loop including
509 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000510
Tim Peters8a5c3c72004-04-05 19:36:21 +0000511 If collecting opcode statistics, turn off prediction so that
512 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000513 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000514*/
515
Raymond Hettingera7216982004-02-08 19:59:27 +0000516#ifdef DYNAMIC_EXECUTION_PROFILE
517#define PREDICT(op) if (0) goto PRED_##op
518#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000519#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000520#endif
521
Raymond Hettingerf606f872003-03-16 03:11:04 +0000522#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000523#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
524 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000525
Guido van Rossum374a9221991-04-04 10:40:29 +0000526/* Stack manipulation macros */
527
528#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
529#define EMPTY() (STACK_LEVEL() == 0)
530#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000531#define SECOND() (stack_pointer[-2])
532#define THIRD() (stack_pointer[-3])
533#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000534#define SET_TOP(v) (stack_pointer[-1] = (v))
535#define SET_SECOND(v) (stack_pointer[-2] = (v))
536#define SET_THIRD(v) (stack_pointer[-3] = (v))
537#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000538#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000539#define BASIC_PUSH(v) (*stack_pointer++ = (v))
540#define BASIC_POP() (*--stack_pointer)
541
Guido van Rossum96a42c81992-01-12 02:29:51 +0000542#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000543#define PUSH(v) { (void)(BASIC_PUSH(v), \
544 lltrace && prtrace(TOP(), "push")); \
545 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000546#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000547#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
548 lltrace && prtrace(TOP(), "stackadj")); \
549 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000550#else
551#define PUSH(v) BASIC_PUSH(v)
552#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000553#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000554#endif
555
Guido van Rossum681d79a1995-07-18 14:51:37 +0000556/* Local variable macros */
557
558#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000559
560/* The SETLOCAL() macro must not DECREF the local variable in-place and
561 then store the new value; it must copy the old value to a temporary
562 value, then store the new value, and then DECREF the temporary value.
563 This is because it is possible that during the DECREF the frame is
564 accessed by other code (e.g. a __del__ method or gc.collect()) and the
565 variable would be pointing to already-freed memory. */
566#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
567 GETLOCAL(i) = value; \
568 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000569
Guido van Rossuma027efa1997-05-05 20:56:21 +0000570/* Start of code */
571
Tim Peters5ca576e2001-06-18 22:08:13 +0000572 if (f == NULL)
573 return NULL;
574
Armin Rigo1d313ab2003-10-25 14:33:09 +0000575 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000576 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000577 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000578
Tim Peters5ca576e2001-06-18 22:08:13 +0000579 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000580
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000581 if (tstate->use_tracing) {
582 if (tstate->c_tracefunc != NULL) {
583 /* tstate->c_tracefunc, if defined, is a
584 function that will be called on *every* entry
585 to a code block. Its return value, if not
586 None, is a function that will be called at
587 the start of each executed line of code.
588 (Actually, the function must return itself
589 in order to continue tracing.) The trace
590 functions are called with three arguments:
591 a pointer to the current frame, a string
592 indicating why the function is called, and
593 an argument which depends on the situation.
594 The global trace function is also called
595 whenever an exception is detected. */
596 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
597 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000598 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000599 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000600 }
601 }
602 if (tstate->c_profilefunc != NULL) {
603 /* Similar for c_profilefunc, except it needn't
604 return itself and isn't called for "line" events */
605 if (call_trace(tstate->c_profilefunc,
606 tstate->c_profileobj,
607 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000608 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000610 }
611 }
612 }
613
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000614 co = f->f_code;
615 names = co->co_names;
616 consts = co->co_consts;
617 fastlocals = f->f_localsplus;
618 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000619 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000620 /* An explanation is in order for the next line.
621
622 f->f_lasti now refers to the index of the last instruction
623 executed. You might think this was obvious from the name, but
624 this wasn't always true before 2.3! PyFrame_New now sets
625 f->f_lasti to -1 (i.e. the index *before* the first instruction)
626 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
627 does work. Promise. */
628 next_instr = first_instr + f->f_lasti + 1;
629 stack_pointer = f->f_stacktop;
630 assert(stack_pointer != NULL);
631 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
632
Tim Peters5ca576e2001-06-18 22:08:13 +0000633#ifdef LLTRACE
634 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
635#endif
636#if defined(Py_DEBUG) || defined(LLTRACE)
637 filename = PyString_AsString(co->co_filename);
638#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000639
Guido van Rossum374a9221991-04-04 10:40:29 +0000640 why = WHY_NOT;
641 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000642 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000643 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000644
Guido van Rossum374a9221991-04-04 10:40:29 +0000645 for (;;) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000646 assert(stack_pointer >= f->f_valuestack); /* else underflow */
647 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
648
Guido van Rossuma027efa1997-05-05 20:56:21 +0000649 /* Do periodic things. Doing this every time through
650 the loop would add too much overhead, so we do it
651 only every Nth instruction. We also do it if
652 ``things_to_do'' is set, i.e. when an asynchronous
653 event needs attention (e.g. a signal handler or
654 async I/O handler); see Py_AddPendingCall() and
655 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000656
Skip Montanarod581d772002-09-03 20:10:45 +0000657 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000658 if (*next_instr == SETUP_FINALLY) {
659 /* Make the last opcode before
660 a try: finally: block uninterruptable. */
661 goto fast_next_opcode;
662 }
Skip Montanarod581d772002-09-03 20:10:45 +0000663 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000664 tstate->tick_counter++;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000665 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000666 if (Py_MakePendingCalls() < 0) {
667 why = WHY_EXCEPTION;
668 goto on_error;
669 }
670 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000671#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000672 if (interpreter_lock) {
673 /* Give another thread a chance */
674
Guido van Rossum25ce5661997-08-02 03:10:38 +0000675 if (PyThreadState_Swap(NULL) != tstate)
676 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000677 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000678
679 /* Other threads may run now */
680
Guido van Rossum65d5b571998-12-21 19:32:43 +0000681 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000682 if (PyThreadState_Swap(tstate) != NULL)
683 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000684
685 /* Check for thread interrupts */
686
687 if (tstate->async_exc != NULL) {
688 x = tstate->async_exc;
689 tstate->async_exc = NULL;
690 PyErr_SetNone(x);
691 Py_DECREF(x);
692 why = WHY_EXCEPTION;
693 goto on_error;
694 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000695 }
696#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000697 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000698
Neil Schemenauer63543862002-02-17 19:10:14 +0000699 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000700 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000701
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000702 /* line-by-line tracing support */
703
704 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
705 /* see maybe_call_line_trace
706 for expository comments */
707 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000708
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000709 err = maybe_call_line_trace(tstate->c_tracefunc,
710 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000711 f, &instr_lb, &instr_ub,
712 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000713 /* Reload possibly changed frame fields */
714 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000715 if (f->f_stacktop != NULL) {
716 stack_pointer = f->f_stacktop;
717 f->f_stacktop = NULL;
718 }
719 if (err) {
720 /* trace function raised an exception */
721 goto on_error;
722 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000723 }
724
725 /* Extract opcode and argument */
726
Guido van Rossum374a9221991-04-04 10:40:29 +0000727 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000728 if (HAS_ARG(opcode))
729 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000730 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000731#ifdef DYNAMIC_EXECUTION_PROFILE
732#ifdef DXPAIRS
733 dxpairs[lastopcode][opcode]++;
734 lastopcode = opcode;
735#endif
736 dxp[opcode]++;
737#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000738
Guido van Rossum96a42c81992-01-12 02:29:51 +0000739#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000740 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000741
Guido van Rossum96a42c81992-01-12 02:29:51 +0000742 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000743 if (HAS_ARG(opcode)) {
744 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000745 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000746 }
747 else {
748 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000749 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000750 }
751 }
752#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000753
Guido van Rossum374a9221991-04-04 10:40:29 +0000754 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000755
Guido van Rossum374a9221991-04-04 10:40:29 +0000756 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000757
Guido van Rossum374a9221991-04-04 10:40:29 +0000758 /* BEWARE!
759 It is essential that any operation that fails sets either
760 x to NULL, err to nonzero, or why to anything but WHY_NOT,
761 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000764
Neil Schemenauer63543862002-02-17 19:10:14 +0000765 case LOAD_FAST:
766 x = GETLOCAL(oparg);
767 if (x != NULL) {
768 Py_INCREF(x);
769 PUSH(x);
770 goto fast_next_opcode;
771 }
772 format_exc_check_arg(PyExc_UnboundLocalError,
773 UNBOUNDLOCAL_ERROR_MSG,
774 PyTuple_GetItem(co->co_varnames, oparg));
775 break;
776
777 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000778 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000779 Py_INCREF(x);
780 PUSH(x);
781 goto fast_next_opcode;
782
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000783 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000784 case STORE_FAST:
785 v = POP();
786 SETLOCAL(oparg, v);
787 goto fast_next_opcode;
788
Raymond Hettingerf606f872003-03-16 03:11:04 +0000789 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000790 case POP_TOP:
791 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000792 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000793 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000794
Guido van Rossum374a9221991-04-04 10:40:29 +0000795 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000796 v = TOP();
797 w = SECOND();
798 SET_TOP(w);
799 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000800 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000801
Guido van Rossum374a9221991-04-04 10:40:29 +0000802 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000803 v = TOP();
804 w = SECOND();
805 x = THIRD();
806 SET_TOP(w);
807 SET_SECOND(x);
808 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000809 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000810
Thomas Wouters434d0822000-08-24 20:11:32 +0000811 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000812 u = TOP();
813 v = SECOND();
814 w = THIRD();
815 x = FOURTH();
816 SET_TOP(v);
817 SET_SECOND(w);
818 SET_THIRD(x);
819 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000820 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000821
Guido van Rossum374a9221991-04-04 10:40:29 +0000822 case DUP_TOP:
823 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000824 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000825 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000826 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000827
Thomas Wouters434d0822000-08-24 20:11:32 +0000828 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000829 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000830 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000831 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000832 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000833 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000834 STACKADJ(2);
835 SET_TOP(x);
836 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000837 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000838 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000839 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000840 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000841 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000842 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000843 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000844 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000845 STACKADJ(3);
846 SET_TOP(x);
847 SET_SECOND(w);
848 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000849 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000850 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000851 Py_FatalError("invalid argument to DUP_TOPX"
852 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000853 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000854
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000856 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000857 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000858 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000859 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000860 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000861 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000862
Guido van Rossum374a9221991-04-04 10:40:29 +0000863 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000864 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000865 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000866 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000867 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000868 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000869 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000870
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000872 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000873 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000874 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000875 if (err == 0) {
876 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000877 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000878 continue;
879 }
880 else if (err > 0) {
881 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000882 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000883 err = 0;
884 continue;
885 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000886 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000887 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000890 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000891 x = PyObject_Repr(v);
892 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000893 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000894 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Guido van Rossum7928cd71991-10-24 14:59:31 +0000897 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000898 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000899 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000900 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000901 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000902 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000903 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000904
Guido van Rossum50564e81996-01-12 01:13:16 +0000905 case BINARY_POWER:
906 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000907 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000908 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000909 Py_DECREF(v);
910 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000911 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000912 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000913 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000914
Guido van Rossum374a9221991-04-04 10:40:29 +0000915 case BINARY_MULTIPLY:
916 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000917 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000918 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000919 Py_DECREF(v);
920 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000921 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000922 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000923 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000924
Guido van Rossum374a9221991-04-04 10:40:29 +0000925 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +0000926 if (!_Py_QnewFlag) {
927 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000928 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +0000929 x = PyNumber_Divide(v, w);
930 Py_DECREF(v);
931 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000932 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +0000933 if (x != NULL) continue;
934 break;
935 }
Raymond Hettinger663004b2003-01-09 15:24:30 +0000936 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +0000937 BINARY_TRUE_DIVIDE */
938 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000940 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +0000941 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000942 Py_DECREF(v);
943 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000945 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000947
Guido van Rossum4668b002001-08-08 05:00:18 +0000948 case BINARY_FLOOR_DIVIDE:
949 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000950 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +0000951 x = PyNumber_FloorDivide(v, w);
952 Py_DECREF(v);
953 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +0000955 if (x != NULL) continue;
956 break;
957
Guido van Rossum374a9221991-04-04 10:40:29 +0000958 case BINARY_MODULO:
959 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000961 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000962 Py_DECREF(v);
963 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000964 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000965 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000966 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000967
Guido van Rossum374a9221991-04-04 10:40:29 +0000968 case BINARY_ADD:
969 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +0000971 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000972 /* INLINE: int + int */
973 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000974 a = PyInt_AS_LONG(v);
975 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000976 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +0000977 if ((i^a) < 0 && (i^b) < 0)
978 goto slow_add;
979 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000980 }
Guido van Rossum87780df2001-08-23 02:58:07 +0000981 else {
982 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +0000983 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +0000984 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000985 Py_DECREF(v);
986 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000988 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 case BINARY_SUBTRACT:
992 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +0000994 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000995 /* INLINE: int - int */
996 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000997 a = PyInt_AS_LONG(v);
998 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000999 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001000 if ((i^a) < 0 && (i^~b) < 0)
1001 goto slow_sub;
1002 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001003 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001004 else {
1005 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001006 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001007 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001008 Py_DECREF(v);
1009 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001011 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case BINARY_SUBSCR:
1015 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001017 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001018 /* INLINE: list[int] */
1019 long i = PyInt_AsLong(w);
1020 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001021 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001022 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001023 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001024 Py_INCREF(x);
1025 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001026 else
1027 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001028 }
1029 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001030 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001031 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001032 Py_DECREF(v);
1033 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001034 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001035 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001037
Guido van Rossum7928cd71991-10-24 14:59:31 +00001038 case BINARY_LSHIFT:
1039 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001041 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001042 Py_DECREF(v);
1043 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001045 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001047
Guido van Rossum7928cd71991-10-24 14:59:31 +00001048 case BINARY_RSHIFT:
1049 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001051 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001052 Py_DECREF(v);
1053 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001055 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001056 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Guido van Rossum7928cd71991-10-24 14:59:31 +00001058 case BINARY_AND:
1059 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001061 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001062 Py_DECREF(v);
1063 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001065 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001066 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001067
Guido van Rossum7928cd71991-10-24 14:59:31 +00001068 case BINARY_XOR:
1069 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001071 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001072 Py_DECREF(v);
1073 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001075 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001076 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001077
Guido van Rossum7928cd71991-10-24 14:59:31 +00001078 case BINARY_OR:
1079 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001081 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001082 Py_DECREF(v);
1083 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001084 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001085 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001086 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001087
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001088 case LIST_APPEND:
1089 w = POP();
1090 v = POP();
1091 err = PyList_Append(v, w);
1092 Py_DECREF(v);
1093 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001094 if (err == 0) {
1095 PREDICT(JUMP_ABSOLUTE);
1096 continue;
1097 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001098 break;
1099
Thomas Wouters434d0822000-08-24 20:11:32 +00001100 case INPLACE_POWER:
1101 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001103 x = PyNumber_InPlacePower(v, w, Py_None);
1104 Py_DECREF(v);
1105 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001106 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001107 if (x != NULL) continue;
1108 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001109
Thomas Wouters434d0822000-08-24 20:11:32 +00001110 case INPLACE_MULTIPLY:
1111 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001112 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001113 x = PyNumber_InPlaceMultiply(v, w);
1114 Py_DECREF(v);
1115 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001116 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001117 if (x != NULL) continue;
1118 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001119
Thomas Wouters434d0822000-08-24 20:11:32 +00001120 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001121 if (!_Py_QnewFlag) {
1122 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001123 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001124 x = PyNumber_InPlaceDivide(v, w);
1125 Py_DECREF(v);
1126 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001127 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001128 if (x != NULL) continue;
1129 break;
1130 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001132 INPLACE_TRUE_DIVIDE */
1133 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001134 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001136 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001137 Py_DECREF(v);
1138 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001140 if (x != NULL) continue;
1141 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001142
Guido van Rossum4668b002001-08-08 05:00:18 +00001143 case INPLACE_FLOOR_DIVIDE:
1144 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001146 x = PyNumber_InPlaceFloorDivide(v, w);
1147 Py_DECREF(v);
1148 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001150 if (x != NULL) continue;
1151 break;
1152
Thomas Wouters434d0822000-08-24 20:11:32 +00001153 case INPLACE_MODULO:
1154 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001156 x = PyNumber_InPlaceRemainder(v, w);
1157 Py_DECREF(v);
1158 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001160 if (x != NULL) continue;
1161 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001162
Thomas Wouters434d0822000-08-24 20:11:32 +00001163 case INPLACE_ADD:
1164 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001166 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001167 /* INLINE: int + int */
1168 register long a, b, i;
1169 a = PyInt_AS_LONG(v);
1170 b = PyInt_AS_LONG(w);
1171 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001172 if ((i^a) < 0 && (i^b) < 0)
1173 goto slow_iadd;
1174 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001175 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001176 else {
1177 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001178 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001179 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001183 if (x != NULL) continue;
1184 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Thomas Wouters434d0822000-08-24 20:11:32 +00001186 case INPLACE_SUBTRACT:
1187 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001189 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001190 /* INLINE: int - int */
1191 register long a, b, i;
1192 a = PyInt_AS_LONG(v);
1193 b = PyInt_AS_LONG(w);
1194 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001195 if ((i^a) < 0 && (i^~b) < 0)
1196 goto slow_isub;
1197 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001198 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001199 else {
1200 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001201 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001202 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001203 Py_DECREF(v);
1204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001206 if (x != NULL) continue;
1207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Thomas Wouters434d0822000-08-24 20:11:32 +00001209 case INPLACE_LSHIFT:
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001212 x = PyNumber_InPlaceLshift(v, w);
1213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001216 if (x != NULL) continue;
1217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Thomas Wouters434d0822000-08-24 20:11:32 +00001219 case INPLACE_RSHIFT:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 x = PyNumber_InPlaceRshift(v, w);
1223 Py_DECREF(v);
1224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001226 if (x != NULL) continue;
1227 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001228
Thomas Wouters434d0822000-08-24 20:11:32 +00001229 case INPLACE_AND:
1230 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001232 x = PyNumber_InPlaceAnd(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 if (x != NULL) continue;
1237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 case INPLACE_XOR:
1240 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001241 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001242 x = PyNumber_InPlaceXor(v, w);
1243 Py_DECREF(v);
1244 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 if (x != NULL) continue;
1247 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Thomas Wouters434d0822000-08-24 20:11:32 +00001249 case INPLACE_OR:
1250 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001251 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 x = PyNumber_InPlaceOr(v, w);
1253 Py_DECREF(v);
1254 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001255 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 if (x != NULL) continue;
1257 break;
1258
Guido van Rossum374a9221991-04-04 10:40:29 +00001259 case SLICE+0:
1260 case SLICE+1:
1261 case SLICE+2:
1262 case SLICE+3:
1263 if ((opcode-SLICE) & 2)
1264 w = POP();
1265 else
1266 w = NULL;
1267 if ((opcode-SLICE) & 1)
1268 v = POP();
1269 else
1270 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001272 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001273 Py_DECREF(u);
1274 Py_XDECREF(v);
1275 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001276 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001277 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001278 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001279
Guido van Rossum374a9221991-04-04 10:40:29 +00001280 case STORE_SLICE+0:
1281 case STORE_SLICE+1:
1282 case STORE_SLICE+2:
1283 case STORE_SLICE+3:
1284 if ((opcode-STORE_SLICE) & 2)
1285 w = POP();
1286 else
1287 w = NULL;
1288 if ((opcode-STORE_SLICE) & 1)
1289 v = POP();
1290 else
1291 v = NULL;
1292 u = POP();
1293 t = POP();
1294 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001295 Py_DECREF(t);
1296 Py_DECREF(u);
1297 Py_XDECREF(v);
1298 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001299 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001300 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001301
Guido van Rossum374a9221991-04-04 10:40:29 +00001302 case DELETE_SLICE+0:
1303 case DELETE_SLICE+1:
1304 case DELETE_SLICE+2:
1305 case DELETE_SLICE+3:
1306 if ((opcode-DELETE_SLICE) & 2)
1307 w = POP();
1308 else
1309 w = NULL;
1310 if ((opcode-DELETE_SLICE) & 1)
1311 v = POP();
1312 else
1313 v = NULL;
1314 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001315 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001316 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001317 Py_DECREF(u);
1318 Py_XDECREF(v);
1319 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001320 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001321 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001322
Guido van Rossum374a9221991-04-04 10:40:29 +00001323 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 w = TOP();
1325 v = SECOND();
1326 u = THIRD();
1327 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001328 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001329 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001330 Py_DECREF(u);
1331 Py_DECREF(v);
1332 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001333 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001334 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Guido van Rossum374a9221991-04-04 10:40:29 +00001336 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001337 w = TOP();
1338 v = SECOND();
1339 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001340 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001341 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001342 Py_DECREF(v);
1343 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001344 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001345 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001346
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 case PRINT_EXPR:
1348 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001349 w = PySys_GetObject("displayhook");
1350 if (w == NULL) {
1351 PyErr_SetString(PyExc_RuntimeError,
1352 "lost sys.displayhook");
1353 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001354 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001355 }
1356 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001357 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001358 if (x == NULL)
1359 err = -1;
1360 }
1361 if (err == 0) {
1362 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001363 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001364 if (w == NULL)
1365 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001366 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001367 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001368 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001369 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001370
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001371 case PRINT_ITEM_TO:
1372 w = stream = POP();
1373 /* fall through to PRINT_ITEM */
1374
Guido van Rossum374a9221991-04-04 10:40:29 +00001375 case PRINT_ITEM:
1376 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001377 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001378 w = PySys_GetObject("stdout");
1379 if (w == NULL) {
1380 PyErr_SetString(PyExc_RuntimeError,
1381 "lost sys.stdout");
1382 err = -1;
1383 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001384 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001385 /* PyFile_SoftSpace() can exececute arbitrary code
1386 if sys.stdout is an instance with a __getattr__.
1387 If __getattr__ raises an exception, w will
1388 be freed, so we need to prevent that temporarily. */
1389 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001390 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001391 err = PyFile_WriteString(" ", w);
1392 if (err == 0)
1393 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001394 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001395 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001396 if (PyString_Check(v)) {
1397 char *s = PyString_AS_STRING(v);
1398 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001399 if (len == 0 ||
1400 !isspace(Py_CHARMASK(s[len-1])) ||
1401 s[len-1] == ' ')
1402 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001403 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001404#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001405 else if (PyUnicode_Check(v)) {
1406 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1407 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001408 if (len == 0 ||
1409 !Py_UNICODE_ISSPACE(s[len-1]) ||
1410 s[len-1] == ' ')
1411 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001412 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001413#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001414 else
1415 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001416 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001417 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001419 Py_XDECREF(stream);
1420 stream = NULL;
1421 if (err == 0)
1422 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001425 case PRINT_NEWLINE_TO:
1426 w = stream = POP();
1427 /* fall through to PRINT_NEWLINE */
1428
Guido van Rossum374a9221991-04-04 10:40:29 +00001429 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001430 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001431 w = PySys_GetObject("stdout");
1432 if (w == NULL)
1433 PyErr_SetString(PyExc_RuntimeError,
1434 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001435 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001436 if (w != NULL) {
1437 err = PyFile_WriteString("\n", w);
1438 if (err == 0)
1439 PyFile_SoftSpace(w, 0);
1440 }
1441 Py_XDECREF(stream);
1442 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Thomas Wouters434d0822000-08-24 20:11:32 +00001445
1446#ifdef CASE_TOO_BIG
1447 default: switch (opcode) {
1448#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001449 case RAISE_VARARGS:
1450 u = v = w = NULL;
1451 switch (oparg) {
1452 case 3:
1453 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001454 /* Fallthrough */
1455 case 2:
1456 v = POP(); /* value */
1457 /* Fallthrough */
1458 case 1:
1459 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001460 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001461 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001462 break;
1463 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001464 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001465 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001466 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001467 break;
1468 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001472 if ((x = f->f_locals) != NULL) {
1473 Py_INCREF(x);
1474 PUSH(x);
1475 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001476 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001477 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001479
Guido van Rossum374a9221991-04-04 10:40:29 +00001480 case RETURN_VALUE:
1481 retval = POP();
1482 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001483 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001484
Tim Peters5ca576e2001-06-18 22:08:13 +00001485 case YIELD_VALUE:
1486 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001487 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001488 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001489 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001490
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001491 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001492 w = TOP();
1493 v = SECOND();
1494 u = THIRD();
1495 STACKADJ(-3);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001496 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001497 Py_DECREF(u);
1498 Py_DECREF(v);
1499 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001500 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Guido van Rossum374a9221991-04-04 10:40:29 +00001502 case POP_BLOCK:
1503 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001504 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 while (STACK_LEVEL() > b->b_level) {
1506 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001507 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 }
1509 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001510 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Guido van Rossum374a9221991-04-04 10:40:29 +00001512 case END_FINALLY:
1513 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001514 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001515 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001516 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001517 if (why == WHY_RETURN ||
1518 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001519 retval = POP();
1520 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001521 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001522 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001523 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001524 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001525 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001526 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001527 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001528 else if (v != Py_None) {
1529 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001530 "'finally' pops bad exception");
1531 why = WHY_EXCEPTION;
1532 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001533 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001534 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Guido van Rossum374a9221991-04-04 10:40:29 +00001536 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001537 u = TOP();
1538 v = SECOND();
1539 w = THIRD();
1540 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001541 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001542 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001543 Py_DECREF(u);
1544 Py_DECREF(v);
1545 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001546 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Guido van Rossum374a9221991-04-04 10:40:29 +00001548 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001549 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001550 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001551 if ((x = f->f_locals) != NULL) {
1552 err = PyDict_SetItem(x, w, v);
1553 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001554 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001555 break;
1556 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001557 PyErr_Format(PyExc_SystemError,
1558 "no locals found when storing %s",
1559 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001560 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001563 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001564 if ((x = f->f_locals) != NULL) {
1565 if ((err = PyDict_DelItem(x, w)) != 0)
1566 format_exc_check_arg(PyExc_NameError,
1567 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001568 break;
1569 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001570 PyErr_Format(PyExc_SystemError,
1571 "no locals when deleting %s",
1572 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001573 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001574
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001575 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001576 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001578 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1579 PyObject **items = ((PyTupleObject *)v)->ob_item;
1580 while (oparg--) {
1581 w = items[oparg];
1582 Py_INCREF(w);
1583 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001584 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001585 Py_DECREF(v);
1586 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001587 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1588 PyObject **items = ((PyListObject *)v)->ob_item;
1589 while (oparg--) {
1590 w = items[oparg];
1591 Py_INCREF(w);
1592 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001593 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001594 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001595 stack_pointer + oparg))
1596 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001597 else {
1598 if (PyErr_ExceptionMatches(PyExc_TypeError))
1599 PyErr_SetString(PyExc_TypeError,
1600 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001601 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001602 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001607 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001608 v = TOP();
1609 u = SECOND();
1610 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001611 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1612 Py_DECREF(v);
1613 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001614 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001618 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001620 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1621 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001622 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001623 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001624
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001625 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001626 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001627 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001628 err = PyDict_SetItem(f->f_globals, w, v);
1629 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001630 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001631 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001632
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001633 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001634 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001635 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001636 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001637 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001638 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Guido van Rossum374a9221991-04-04 10:40:29 +00001640 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001641 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001642 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001643 PyErr_Format(PyExc_SystemError,
1644 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001645 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001646 break;
1647 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001650 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001652 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001654 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001655 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001656 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 break;
1658 }
1659 }
1660 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001661 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001663 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001666 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001667 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001668 /* Inline the PyDict_GetItem() calls.
1669 WARNING: this is an extreme speed hack.
1670 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001671 long hash = ((PyStringObject *)w)->ob_shash;
1672 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001673 PyDictObject *d;
1674 d = (PyDictObject *)(f->f_globals);
1675 x = d->ma_lookup(d, w, hash)->me_value;
1676 if (x != NULL) {
1677 Py_INCREF(x);
1678 PUSH(x);
1679 continue;
1680 }
1681 d = (PyDictObject *)(f->f_builtins);
1682 x = d->ma_lookup(d, w, hash)->me_value;
1683 if (x != NULL) {
1684 Py_INCREF(x);
1685 PUSH(x);
1686 continue;
1687 }
1688 goto load_global_error;
1689 }
1690 }
1691 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001692 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001693 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001694 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001696 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001697 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001698 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001699 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 break;
1701 }
1702 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001703 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001705 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001706
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001707 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001708 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001709 if (x != NULL) {
1710 SETLOCAL(oparg, NULL);
1711 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001712 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001713 format_exc_check_arg(
1714 PyExc_UnboundLocalError,
1715 UNBOUNDLOCAL_ERROR_MSG,
1716 PyTuple_GetItem(co->co_varnames, oparg)
1717 );
1718 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001719
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001720 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001721 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001722 Py_INCREF(x);
1723 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001724 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001725 break;
1726
1727 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001728 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001729 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001730 if (w != NULL) {
1731 PUSH(w);
1732 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001733 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001734 err = -1;
1735 /* Don't stomp existing exception */
1736 if (PyErr_Occurred())
1737 break;
1738 if (oparg < f->f_ncells) {
1739 v = PyTuple_GetItem(co->co_cellvars,
1740 oparg);
1741 format_exc_check_arg(
1742 PyExc_UnboundLocalError,
1743 UNBOUNDLOCAL_ERROR_MSG,
1744 v);
1745 } else {
1746 v = PyTuple_GetItem(
1747 co->co_freevars,
1748 oparg - f->f_ncells);
1749 format_exc_check_arg(
1750 PyExc_NameError,
1751 UNBOUNDFREE_ERROR_MSG,
1752 v);
1753 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001754 break;
1755
1756 case STORE_DEREF:
1757 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001758 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001759 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001760 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001761 continue;
1762
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001766 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 }
1770 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001771 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 }
1773 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Guido van Rossum374a9221991-04-04 10:40:29 +00001775 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001776 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001778 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001780 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 }
1782 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001783 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 }
1785 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001790 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001794 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001795 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001796 x = PyObject_GetAttr(v, w);
1797 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001798 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001799 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 case COMPARE_OP:
1803 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001804 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001805 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001806 /* INLINE: cmp(int, int) */
1807 register long a, b;
1808 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001809 a = PyInt_AS_LONG(v);
1810 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001811 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001812 case PyCmp_LT: res = a < b; break;
1813 case PyCmp_LE: res = a <= b; break;
1814 case PyCmp_EQ: res = a == b; break;
1815 case PyCmp_NE: res = a != b; break;
1816 case PyCmp_GT: res = a > b; break;
1817 case PyCmp_GE: res = a >= b; break;
1818 case PyCmp_IS: res = v == w; break;
1819 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001820 default: goto slow_compare;
1821 }
1822 x = res ? Py_True : Py_False;
1823 Py_INCREF(x);
1824 }
1825 else {
1826 slow_compare:
1827 x = cmp_outcome(oparg, v, w);
1828 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 Py_DECREF(v);
1830 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001831 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001832 if (x == NULL) break;
1833 PREDICT(JUMP_IF_FALSE);
1834 PREDICT(JUMP_IF_TRUE);
1835 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001836
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001838 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001839 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001840 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001841 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001842 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843 break;
1844 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001845 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001846 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001847 w,
1848 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001849 f->f_locals == NULL ?
1850 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001851 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001853 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001854 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001855 x = NULL;
1856 break;
1857 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001858 x = PyEval_CallObject(x, w);
1859 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001860 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001861 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001863
Thomas Wouters52152252000-08-17 22:55:00 +00001864 case IMPORT_STAR:
1865 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001866 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001867 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001868 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001869 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001870 break;
1871 }
Thomas Wouters52152252000-08-17 22:55:00 +00001872 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001873 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001874 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001875 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001877
Thomas Wouters52152252000-08-17 22:55:00 +00001878 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001879 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001880 v = TOP();
1881 x = import_from(v, w);
1882 PUSH(x);
1883 if (x != NULL) continue;
1884 break;
1885
Guido van Rossum374a9221991-04-04 10:40:29 +00001886 case JUMP_FORWARD:
1887 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001888 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Raymond Hettingerf606f872003-03-16 03:11:04 +00001890 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001892 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00001893 if (w == Py_True) {
1894 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001895 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00001896 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001897 if (w == Py_False) {
1898 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001899 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00001900 }
1901 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001902 if (err > 0)
1903 err = 0;
1904 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001906 else
1907 break;
1908 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Raymond Hettingerf606f872003-03-16 03:11:04 +00001910 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00001912 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00001913 if (w == Py_False) {
1914 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001915 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00001916 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00001917 if (w == Py_True) {
1918 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001919 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00001920 }
1921 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001922 if (err > 0) {
1923 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001925 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001926 else if (err == 0)
1927 ;
1928 else
1929 break;
1930 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001932 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 case JUMP_ABSOLUTE:
1934 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00001935 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001936
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001937 case GET_ITER:
1938 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00001939 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001940 x = PyObject_GetIter(v);
1941 Py_DECREF(v);
1942 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001943 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001944 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001945 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001946 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001947 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001948 break;
1949
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001950 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001951 case FOR_ITER:
1952 /* before: [iter]; after: [iter, iter()] *or* [] */
1953 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00001954 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001955 if (x != NULL) {
1956 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001957 PREDICT(STORE_FAST);
1958 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001959 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001960 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00001961 if (PyErr_Occurred()) {
1962 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
1963 break;
1964 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00001965 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00001966 /* iterator ended normally */
1967 x = v = POP();
1968 Py_DECREF(v);
1969 JUMPBY(oparg);
1970 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001971
Raymond Hettinger2d783e92004-03-12 09:12:22 +00001972 case BREAK_LOOP:
1973 why = WHY_BREAK;
1974 goto fast_block_end;
1975
1976 case CONTINUE_LOOP:
1977 retval = PyInt_FromLong(oparg);
1978 why = WHY_CONTINUE;
1979 goto fast_block_end;
1980
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 case SETUP_LOOP:
1982 case SETUP_EXCEPT:
1983 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001985 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001986 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001987
Guido van Rossumf10570b1995-07-07 22:53:21 +00001988 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00001989 PCALL(PCALL_ALL);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00001990 x = call_function(&stack_pointer, oparg);
1991 PUSH(x);
1992 if (x != NULL)
1993 continue;
1994 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001995
Jeremy Hylton76901512000-03-28 23:49:17 +00001996 case CALL_FUNCTION_VAR:
1997 case CALL_FUNCTION_KW:
1998 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00001999 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002000 int na = oparg & 0xff;
2001 int nk = (oparg>>8) & 0xff;
2002 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002003 int n = na + 2 * nk;
2004 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002005 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002006 if (flags & CALL_FLAG_VAR)
2007 n++;
2008 if (flags & CALL_FLAG_KW)
2009 n++;
2010 pfunc = stack_pointer - n - 1;
2011 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002012
Guido van Rossumac7be682001-01-17 15:42:30 +00002013 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002014 && PyMethod_GET_SELF(func) != NULL) {
2015 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002016 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002017 func = PyMethod_GET_FUNCTION(func);
2018 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002019 Py_DECREF(*pfunc);
2020 *pfunc = self;
2021 na++;
2022 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002023 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002024 Py_INCREF(func);
2025 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00002026 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002027
Jeremy Hylton76901512000-03-28 23:49:17 +00002028 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002029 w = POP();
2030 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002031 }
2032 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002033 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002034 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002035 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002036 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Guido van Rossum681d79a1995-07-18 14:51:37 +00002038 case MAKE_FUNCTION:
2039 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 x = PyFunction_New(v, f->f_globals);
2041 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002042 /* XXX Maybe this should be a separate opcode? */
2043 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002044 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002045 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002046 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002047 x = NULL;
2048 break;
2049 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002050 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002051 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002052 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002053 }
2054 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002055 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002056 }
2057 PUSH(x);
2058 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002059
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002060 case MAKE_CLOSURE:
2061 {
2062 int nfree;
2063 v = POP(); /* code object */
2064 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002065 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002066 Py_DECREF(v);
2067 /* XXX Maybe this should be a separate opcode? */
2068 if (x != NULL && nfree > 0) {
2069 v = PyTuple_New(nfree);
2070 if (v == NULL) {
2071 Py_DECREF(x);
2072 x = NULL;
2073 break;
2074 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002075 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002076 w = POP();
2077 PyTuple_SET_ITEM(v, nfree, w);
2078 }
2079 err = PyFunction_SetClosure(x, v);
2080 Py_DECREF(v);
2081 }
2082 if (x != NULL && oparg > 0) {
2083 v = PyTuple_New(oparg);
2084 if (v == NULL) {
2085 Py_DECREF(x);
2086 x = NULL;
2087 break;
2088 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002089 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002090 w = POP();
2091 PyTuple_SET_ITEM(v, oparg, w);
2092 }
2093 err = PyFunction_SetDefaults(x, v);
2094 Py_DECREF(v);
2095 }
2096 PUSH(x);
2097 break;
2098 }
2099
Guido van Rossum8861b741996-07-30 16:49:37 +00002100 case BUILD_SLICE:
2101 if (oparg == 3)
2102 w = POP();
2103 else
2104 w = NULL;
2105 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002106 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002107 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002108 Py_DECREF(u);
2109 Py_DECREF(v);
2110 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002111 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002112 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002113 break;
2114
Fred Drakeef8ace32000-08-24 00:32:09 +00002115 case EXTENDED_ARG:
2116 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002117 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002118 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002119
Guido van Rossum374a9221991-04-04 10:40:29 +00002120 default:
2121 fprintf(stderr,
2122 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002123 PyCode_Addr2Line(f->f_code, f->f_lasti),
2124 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002126 why = WHY_EXCEPTION;
2127 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002128
2129#ifdef CASE_TOO_BIG
2130 }
2131#endif
2132
Guido van Rossum374a9221991-04-04 10:40:29 +00002133 } /* switch */
2134
2135 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002136
Guido van Rossum374a9221991-04-04 10:40:29 +00002137 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Guido van Rossum374a9221991-04-04 10:40:29 +00002139 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002140 if (err == 0 && x != NULL) {
2141#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002142 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002143 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002144 fprintf(stderr,
2145 "XXX undetected error\n");
2146 else
2147#endif
2148 continue; /* Normal, fast path */
2149 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002150 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002151 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002152 err = 0;
2153 }
2154
Guido van Rossum374a9221991-04-04 10:40:29 +00002155 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002156
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002157 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002158 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002159 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002160 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002161 why = WHY_EXCEPTION;
2162 }
2163 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002164#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002165 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002166 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002167 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002168 char buf[1024];
2169 sprintf(buf, "Stack unwind with exception "
2170 "set and why=%d", why);
2171 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002172 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002173 }
2174#endif
2175
2176 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002177
Guido van Rossum374a9221991-04-04 10:40:29 +00002178 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002179 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002180
Fred Drake8f51f542001-10-04 14:48:42 +00002181 if (tstate->c_tracefunc != NULL)
2182 call_exc_trace(tstate->c_tracefunc,
2183 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002184 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002185
Guido van Rossum374a9221991-04-04 10:40:29 +00002186 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002187
Guido van Rossum374a9221991-04-04 10:40:29 +00002188 if (why == WHY_RERAISE)
2189 why = WHY_EXCEPTION;
2190
2191 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002192
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002193fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002194 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002195 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002196
Tim Peters8a5c3c72004-04-05 19:36:21 +00002197 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002198 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2199 /* For a continue inside a try block,
2200 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002201 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2202 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002203 why = WHY_NOT;
2204 JUMPTO(PyInt_AS_LONG(retval));
2205 Py_DECREF(retval);
2206 break;
2207 }
2208
Guido van Rossum374a9221991-04-04 10:40:29 +00002209 while (STACK_LEVEL() > b->b_level) {
2210 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002211 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002212 }
2213 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2214 why = WHY_NOT;
2215 JUMPTO(b->b_handler);
2216 break;
2217 }
2218 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002219 (b->b_type == SETUP_EXCEPT &&
2220 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002221 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002222 PyObject *exc, *val, *tb;
2223 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002224 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002225 val = Py_None;
2226 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002227 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002228 /* Make the raw exception data
2229 available to the handler,
2230 so a program can emulate the
2231 Python main loop. Don't do
2232 this for 'finally'. */
2233 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002234 PyErr_NormalizeException(
2235 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002236 set_exc_info(tstate,
2237 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002238 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002239 if (tb == NULL) {
2240 Py_INCREF(Py_None);
2241 PUSH(Py_None);
2242 } else
2243 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002244 PUSH(val);
2245 PUSH(exc);
2246 }
2247 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002248 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002249 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002251 PUSH(v);
2252 }
2253 why = WHY_NOT;
2254 JUMPTO(b->b_handler);
2255 break;
2256 }
2257 } /* unwind stack */
2258
2259 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 if (why != WHY_NOT)
2262 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002263
Guido van Rossum374a9221991-04-04 10:40:29 +00002264 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Tim Peters8a5c3c72004-04-05 19:36:21 +00002266 assert(why != WHY_YIELD);
2267 /* Pop remaining stack entries. */
2268 while (!EMPTY()) {
2269 v = POP();
2270 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002271 }
2272
Tim Peters8a5c3c72004-04-05 19:36:21 +00002273 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002274 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002276fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002277 if (tstate->use_tracing) {
2278 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002279 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002280 if (call_trace(tstate->c_tracefunc,
2281 tstate->c_traceobj, f,
2282 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002283 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002284 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002285 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002286 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002287 }
Fred Drake8f51f542001-10-04 14:48:42 +00002288 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002289 if (why == WHY_EXCEPTION)
2290 call_trace_protected(tstate->c_profilefunc,
2291 tstate->c_profileobj, f,
2292 PyTrace_RETURN);
2293 else if (call_trace(tstate->c_profilefunc,
2294 tstate->c_profileobj, f,
2295 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002296 Py_XDECREF(retval);
2297 retval = NULL;
2298 why = WHY_EXCEPTION;
2299 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002300 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002301 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002302
Guido van Rossuma027efa1997-05-05 20:56:21 +00002303 reset_exc_info(tstate);
2304
Tim Peters5ca576e2001-06-18 22:08:13 +00002305 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002306 exit_eval_frame:
2307 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002308 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Guido van Rossum96a42c81992-01-12 02:29:51 +00002310 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002311}
2312
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002313/* this is gonna seem *real weird*, but if you put some other code between
2314 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2315 the if statement in Misc/gdbinit:ppystack */
2316
Tim Peters6d6c1a32001-08-02 04:15:00 +00002317PyObject *
2318PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002319 PyObject **args, int argcount, PyObject **kws, int kwcount,
2320 PyObject **defs, int defcount, PyObject *closure)
2321{
2322 register PyFrameObject *f;
2323 register PyObject *retval = NULL;
2324 register PyObject **fastlocals, **freevars;
2325 PyThreadState *tstate = PyThreadState_GET();
2326 PyObject *x, *u;
2327
2328 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002329 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002330 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002331 return NULL;
2332 }
2333
Jeremy Hylton985eba52003-02-05 23:13:00 +00002334 assert(globals != NULL);
2335 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002336 if (f == NULL)
2337 return NULL;
2338
2339 fastlocals = f->f_localsplus;
2340 freevars = f->f_localsplus + f->f_nlocals;
2341
2342 if (co->co_argcount > 0 ||
2343 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2344 int i;
2345 int n = argcount;
2346 PyObject *kwdict = NULL;
2347 if (co->co_flags & CO_VARKEYWORDS) {
2348 kwdict = PyDict_New();
2349 if (kwdict == NULL)
2350 goto fail;
2351 i = co->co_argcount;
2352 if (co->co_flags & CO_VARARGS)
2353 i++;
2354 SETLOCAL(i, kwdict);
2355 }
2356 if (argcount > co->co_argcount) {
2357 if (!(co->co_flags & CO_VARARGS)) {
2358 PyErr_Format(PyExc_TypeError,
2359 "%.200s() takes %s %d "
2360 "%sargument%s (%d given)",
2361 PyString_AsString(co->co_name),
2362 defcount ? "at most" : "exactly",
2363 co->co_argcount,
2364 kwcount ? "non-keyword " : "",
2365 co->co_argcount == 1 ? "" : "s",
2366 argcount);
2367 goto fail;
2368 }
2369 n = co->co_argcount;
2370 }
2371 for (i = 0; i < n; i++) {
2372 x = args[i];
2373 Py_INCREF(x);
2374 SETLOCAL(i, x);
2375 }
2376 if (co->co_flags & CO_VARARGS) {
2377 u = PyTuple_New(argcount - n);
2378 if (u == NULL)
2379 goto fail;
2380 SETLOCAL(co->co_argcount, u);
2381 for (i = n; i < argcount; i++) {
2382 x = args[i];
2383 Py_INCREF(x);
2384 PyTuple_SET_ITEM(u, i-n, x);
2385 }
2386 }
2387 for (i = 0; i < kwcount; i++) {
2388 PyObject *keyword = kws[2*i];
2389 PyObject *value = kws[2*i + 1];
2390 int j;
2391 if (keyword == NULL || !PyString_Check(keyword)) {
2392 PyErr_Format(PyExc_TypeError,
2393 "%.200s() keywords must be strings",
2394 PyString_AsString(co->co_name));
2395 goto fail;
2396 }
2397 /* XXX slow -- speed up using dictionary? */
2398 for (j = 0; j < co->co_argcount; j++) {
2399 PyObject *nm = PyTuple_GET_ITEM(
2400 co->co_varnames, j);
2401 int cmp = PyObject_RichCompareBool(
2402 keyword, nm, Py_EQ);
2403 if (cmp > 0)
2404 break;
2405 else if (cmp < 0)
2406 goto fail;
2407 }
2408 /* Check errors from Compare */
2409 if (PyErr_Occurred())
2410 goto fail;
2411 if (j >= co->co_argcount) {
2412 if (kwdict == NULL) {
2413 PyErr_Format(PyExc_TypeError,
2414 "%.200s() got an unexpected "
2415 "keyword argument '%.400s'",
2416 PyString_AsString(co->co_name),
2417 PyString_AsString(keyword));
2418 goto fail;
2419 }
2420 PyDict_SetItem(kwdict, keyword, value);
2421 }
2422 else {
2423 if (GETLOCAL(j) != NULL) {
2424 PyErr_Format(PyExc_TypeError,
2425 "%.200s() got multiple "
2426 "values for keyword "
2427 "argument '%.400s'",
2428 PyString_AsString(co->co_name),
2429 PyString_AsString(keyword));
2430 goto fail;
2431 }
2432 Py_INCREF(value);
2433 SETLOCAL(j, value);
2434 }
2435 }
2436 if (argcount < co->co_argcount) {
2437 int m = co->co_argcount - defcount;
2438 for (i = argcount; i < m; i++) {
2439 if (GETLOCAL(i) == NULL) {
2440 PyErr_Format(PyExc_TypeError,
2441 "%.200s() takes %s %d "
2442 "%sargument%s (%d given)",
2443 PyString_AsString(co->co_name),
2444 ((co->co_flags & CO_VARARGS) ||
2445 defcount) ? "at least"
2446 : "exactly",
2447 m, kwcount ? "non-keyword " : "",
2448 m == 1 ? "" : "s", i);
2449 goto fail;
2450 }
2451 }
2452 if (n > m)
2453 i = n - m;
2454 else
2455 i = 0;
2456 for (; i < defcount; i++) {
2457 if (GETLOCAL(m+i) == NULL) {
2458 PyObject *def = defs[i];
2459 Py_INCREF(def);
2460 SETLOCAL(m+i, def);
2461 }
2462 }
2463 }
2464 }
2465 else {
2466 if (argcount > 0 || kwcount > 0) {
2467 PyErr_Format(PyExc_TypeError,
2468 "%.200s() takes no arguments (%d given)",
2469 PyString_AsString(co->co_name),
2470 argcount + kwcount);
2471 goto fail;
2472 }
2473 }
2474 /* Allocate and initialize storage for cell vars, and copy free
2475 vars into frame. This isn't too efficient right now. */
2476 if (f->f_ncells) {
2477 int i = 0, j = 0, nargs, found;
2478 char *cellname, *argname;
2479 PyObject *c;
2480
2481 nargs = co->co_argcount;
2482 if (co->co_flags & CO_VARARGS)
2483 nargs++;
2484 if (co->co_flags & CO_VARKEYWORDS)
2485 nargs++;
2486
2487 /* Check for cells that shadow args */
2488 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2489 cellname = PyString_AS_STRING(
2490 PyTuple_GET_ITEM(co->co_cellvars, i));
2491 found = 0;
2492 while (j < nargs) {
2493 argname = PyString_AS_STRING(
2494 PyTuple_GET_ITEM(co->co_varnames, j));
2495 if (strcmp(cellname, argname) == 0) {
2496 c = PyCell_New(GETLOCAL(j));
2497 if (c == NULL)
2498 goto fail;
2499 GETLOCAL(f->f_nlocals + i) = c;
2500 found = 1;
2501 break;
2502 }
2503 j++;
2504 }
2505 if (found == 0) {
2506 c = PyCell_New(NULL);
2507 if (c == NULL)
2508 goto fail;
2509 SETLOCAL(f->f_nlocals + i, c);
2510 }
2511 }
2512 /* Initialize any that are left */
2513 while (i < f->f_ncells) {
2514 c = PyCell_New(NULL);
2515 if (c == NULL)
2516 goto fail;
2517 SETLOCAL(f->f_nlocals + i, c);
2518 i++;
2519 }
2520 }
2521 if (f->f_nfreevars) {
2522 int i;
2523 for (i = 0; i < f->f_nfreevars; ++i) {
2524 PyObject *o = PyTuple_GET_ITEM(closure, i);
2525 Py_INCREF(o);
2526 freevars[f->f_ncells + i] = o;
2527 }
2528 }
2529
Tim Peters5ca576e2001-06-18 22:08:13 +00002530 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002531 /* Don't need to keep the reference to f_back, it will be set
2532 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002533 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002534 f->f_back = NULL;
2535
Jeremy Hylton985eba52003-02-05 23:13:00 +00002536 PCALL(PCALL_GENERATOR);
2537
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002538 /* Create a new generator that owns the ready to run frame
2539 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002540 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002541 }
2542
2543 retval = eval_frame(f);
2544
2545 fail: /* Jump here from prelude on failure */
2546
Tim Petersb13680b2001-11-27 23:29:29 +00002547 /* decref'ing the frame can cause __del__ methods to get invoked,
2548 which can call back into Python. While we're done with the
2549 current Python frame (f), the associated C stack is still in use,
2550 so recursion_depth must be boosted for the duration.
2551 */
2552 assert(tstate != NULL);
2553 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002554 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002555 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002556 return retval;
2557}
2558
2559
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002560/* Implementation notes for set_exc_info() and reset_exc_info():
2561
2562- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2563 'exc_traceback'. These always travel together.
2564
2565- tstate->curexc_ZZZ is the "hot" exception that is set by
2566 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2567
2568- Once an exception is caught by an except clause, it is transferred
2569 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2570 can pick it up. This is the primary task of set_exc_info().
2571
2572- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2573
2574 Long ago, when none of this existed, there were just a few globals:
2575 one set corresponding to the "hot" exception, and one set
2576 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2577 globals; they were simply stored as sys.exc_ZZZ. For backwards
2578 compatibility, they still are!) The problem was that in code like
2579 this:
2580
2581 try:
2582 "something that may fail"
2583 except "some exception":
2584 "do something else first"
2585 "print the exception from sys.exc_ZZZ."
2586
2587 if "do something else first" invoked something that raised and caught
2588 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2589 cause of subtle bugs. I fixed this by changing the semantics as
2590 follows:
2591
2592 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2593 *in that frame*.
2594
2595 - But initially, and as long as no exception is caught in a given
2596 frame, sys.exc_ZZZ will hold the last exception caught in the
2597 previous frame (or the frame before that, etc.).
2598
2599 The first bullet fixed the bug in the above example. The second
2600 bullet was for backwards compatibility: it was (and is) common to
2601 have a function that is called when an exception is caught, and to
2602 have that function access the caught exception via sys.exc_ZZZ.
2603 (Example: traceback.print_exc()).
2604
2605 At the same time I fixed the problem that sys.exc_ZZZ weren't
2606 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2607 but that's really a separate improvement.
2608
2609 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2610 variables to what they were before the current frame was called. The
2611 set_exc_info() function saves them on the frame so that
2612 reset_exc_info() can restore them. The invariant is that
2613 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2614 exception (where "catching" an exception applies only to successful
2615 except clauses); and if the current frame ever caught an exception,
2616 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2617 at the start of the current frame.
2618
2619*/
2620
Guido van Rossuma027efa1997-05-05 20:56:21 +00002621static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002622set_exc_info(PyThreadState *tstate,
2623 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002624{
2625 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002626 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002627
Guido van Rossuma027efa1997-05-05 20:56:21 +00002628 frame = tstate->frame;
2629 if (frame->f_exc_type == NULL) {
2630 /* This frame didn't catch an exception before */
2631 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002632 if (tstate->exc_type == NULL) {
2633 Py_INCREF(Py_None);
2634 tstate->exc_type = Py_None;
2635 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002636 tmp_type = frame->f_exc_type;
2637 tmp_value = frame->f_exc_value;
2638 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002639 Py_XINCREF(tstate->exc_type);
2640 Py_XINCREF(tstate->exc_value);
2641 Py_XINCREF(tstate->exc_traceback);
2642 frame->f_exc_type = tstate->exc_type;
2643 frame->f_exc_value = tstate->exc_value;
2644 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002645 Py_XDECREF(tmp_type);
2646 Py_XDECREF(tmp_value);
2647 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002648 }
2649 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002650 tmp_type = tstate->exc_type;
2651 tmp_value = tstate->exc_value;
2652 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002653 Py_XINCREF(type);
2654 Py_XINCREF(value);
2655 Py_XINCREF(tb);
2656 tstate->exc_type = type;
2657 tstate->exc_value = value;
2658 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002659 Py_XDECREF(tmp_type);
2660 Py_XDECREF(tmp_value);
2661 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002662 /* For b/w compatibility */
2663 PySys_SetObject("exc_type", type);
2664 PySys_SetObject("exc_value", value);
2665 PySys_SetObject("exc_traceback", tb);
2666}
2667
2668static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002669reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002670{
2671 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002672 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002673 frame = tstate->frame;
2674 if (frame->f_exc_type != NULL) {
2675 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002676 tmp_type = tstate->exc_type;
2677 tmp_value = tstate->exc_value;
2678 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002679 Py_XINCREF(frame->f_exc_type);
2680 Py_XINCREF(frame->f_exc_value);
2681 Py_XINCREF(frame->f_exc_traceback);
2682 tstate->exc_type = frame->f_exc_type;
2683 tstate->exc_value = frame->f_exc_value;
2684 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002685 Py_XDECREF(tmp_type);
2686 Py_XDECREF(tmp_value);
2687 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002688 /* For b/w compatibility */
2689 PySys_SetObject("exc_type", frame->f_exc_type);
2690 PySys_SetObject("exc_value", frame->f_exc_value);
2691 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2692 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002693 tmp_type = frame->f_exc_type;
2694 tmp_value = frame->f_exc_value;
2695 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002696 frame->f_exc_type = NULL;
2697 frame->f_exc_value = NULL;
2698 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002699 Py_XDECREF(tmp_type);
2700 Py_XDECREF(tmp_value);
2701 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002702}
2703
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002704/* Logic for the raise statement (too complicated for inlining).
2705 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002706static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002707do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002708{
Guido van Rossumd295f121998-04-09 21:39:57 +00002709 if (type == NULL) {
2710 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002711 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002712 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2713 value = tstate->exc_value;
2714 tb = tstate->exc_traceback;
2715 Py_XINCREF(type);
2716 Py_XINCREF(value);
2717 Py_XINCREF(tb);
2718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002719
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002720 /* We support the following forms of raise:
2721 raise <class>, <classinstance>
2722 raise <class>, <argument tuple>
2723 raise <class>, None
2724 raise <class>, <argument>
2725 raise <classinstance>, None
2726 raise <string>, <object>
2727 raise <string>, None
2728
2729 An omitted second argument is the same as None.
2730
2731 In addition, raise <tuple>, <anything> is the same as
2732 raising the tuple's first item (and it better have one!);
2733 this rule is applied recursively.
2734
2735 Finally, an optional third argument can be supplied, which
2736 gives the traceback to be substituted (useful when
2737 re-raising an exception after examining it). */
2738
2739 /* First, check the traceback argument, replacing None with
2740 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002741 if (tb == Py_None) {
2742 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002743 tb = NULL;
2744 }
2745 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002746 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002747 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002748 goto raise_error;
2749 }
2750
2751 /* Next, replace a missing value with None */
2752 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002753 value = Py_None;
2754 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002755 }
2756
2757 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002758 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2759 PyObject *tmp = type;
2760 type = PyTuple_GET_ITEM(type, 0);
2761 Py_INCREF(type);
2762 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002763 }
2764
Tim Petersafb2c802002-04-18 18:06:20 +00002765 if (PyString_CheckExact(type))
2766 /* Raising builtin string is deprecated but still allowed --
2767 * do nothing. Raising an instance of a new-style str
2768 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002769 PyErr_Warn(PyExc_PendingDeprecationWarning,
2770 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002771
2772 else if (PyClass_Check(type))
2773 PyErr_NormalizeException(&type, &value, &tb);
2774
Guido van Rossumb209a111997-04-29 18:18:01 +00002775 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002776 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002777 if (value != Py_None) {
2778 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002779 "instance exception may not have a separate value");
2780 goto raise_error;
2781 }
2782 else {
2783 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002784 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002785 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002786 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2787 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002788 }
2789 }
2790 else {
2791 /* Not something you can raise. You get an exception
2792 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002793 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002794 "exceptions must be classes, instances, or "
2795 "strings (deprecated), not %s",
2796 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002797 goto raise_error;
2798 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002799 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002800 if (tb == NULL)
2801 return WHY_EXCEPTION;
2802 else
2803 return WHY_RERAISE;
2804 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002805 Py_XDECREF(value);
2806 Py_XDECREF(type);
2807 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002808 return WHY_EXCEPTION;
2809}
2810
Tim Petersd6d010b2001-06-21 02:49:55 +00002811/* Iterate v argcnt times and store the results on the stack (via decreasing
2812 sp). Return 1 for success, 0 if error. */
2813
Barry Warsawe42b18f1997-08-25 22:13:04 +00002814static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002815unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002816{
Tim Petersd6d010b2001-06-21 02:49:55 +00002817 int i = 0;
2818 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002819 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002820
Tim Petersd6d010b2001-06-21 02:49:55 +00002821 assert(v != NULL);
2822
2823 it = PyObject_GetIter(v);
2824 if (it == NULL)
2825 goto Error;
2826
2827 for (; i < argcnt; i++) {
2828 w = PyIter_Next(it);
2829 if (w == NULL) {
2830 /* Iterator done, via error or exhaustion. */
2831 if (!PyErr_Occurred()) {
2832 PyErr_Format(PyExc_ValueError,
2833 "need more than %d value%s to unpack",
2834 i, i == 1 ? "" : "s");
2835 }
2836 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002837 }
2838 *--sp = w;
2839 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002840
2841 /* We better have exhausted the iterator now. */
2842 w = PyIter_Next(it);
2843 if (w == NULL) {
2844 if (PyErr_Occurred())
2845 goto Error;
2846 Py_DECREF(it);
2847 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002848 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002849 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002850 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002851 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002852Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002853 for (; i > 0; i--, sp++)
2854 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002855 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002856 return 0;
2857}
2858
2859
Guido van Rossum96a42c81992-01-12 02:29:51 +00002860#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002861static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002862prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002863{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002864 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002865 if (PyObject_Print(v, stdout, 0) != 0)
2866 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002867 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002868 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002869}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002870#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002871
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002872static void
Fred Drake5755ce62001-06-27 19:19:46 +00002873call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002874{
Guido van Rossumb209a111997-04-29 18:18:01 +00002875 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002876 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002877 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002878 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002879 value = Py_None;
2880 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002881 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002882 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002883 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002884 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002885 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002886 }
Fred Drake5755ce62001-06-27 19:19:46 +00002887 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002888 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002889 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002890 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002891 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002892 Py_XDECREF(type);
2893 Py_XDECREF(value);
2894 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002895 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002896}
2897
Fred Drake4ec5d562001-10-04 19:26:43 +00002898static void
2899call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2900 int what)
2901{
2902 PyObject *type, *value, *traceback;
2903 int err;
2904 PyErr_Fetch(&type, &value, &traceback);
2905 err = call_trace(func, obj, frame, what, NULL);
2906 if (err == 0)
2907 PyErr_Restore(type, value, traceback);
2908 else {
2909 Py_XDECREF(type);
2910 Py_XDECREF(value);
2911 Py_XDECREF(traceback);
2912 }
2913}
2914
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002915static int
Fred Drake5755ce62001-06-27 19:19:46 +00002916call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
2917 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002918{
Fred Drake5755ce62001-06-27 19:19:46 +00002919 register PyThreadState *tstate = frame->f_tstate;
2920 int result;
2921 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002922 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002923 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00002924 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00002925 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00002926 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
2927 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00002928 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00002929 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002930}
2931
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00002932PyObject *
2933_PyEval_CallTracing(PyObject *func, PyObject *args)
2934{
2935 PyFrameObject *frame = PyEval_GetFrame();
2936 PyThreadState *tstate = frame->f_tstate;
2937 int save_tracing = tstate->tracing;
2938 int save_use_tracing = tstate->use_tracing;
2939 PyObject *result;
2940
2941 tstate->tracing = 0;
2942 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
2943 || (tstate->c_profilefunc != NULL));
2944 result = PyObject_Call(func, args, NULL);
2945 tstate->tracing = save_tracing;
2946 tstate->use_tracing = save_use_tracing;
2947 return result;
2948}
2949
Michael W. Hudson006c7522002-11-08 13:08:46 +00002950static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00002951maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00002952 PyFrameObject *frame, int *instr_lb, int *instr_ub,
2953 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002954{
2955 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00002956
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002957 In a nutshell, we use the co_lnotab field of the code object
2958 to tell when execution has moved onto a different line.
2959
2960 As mentioned above, the basic idea is so set things up so
2961 that
2962
2963 *instr_lb <= frame->f_lasti < *instr_ub
2964
2965 is true so long as execution does not change lines.
2966
2967 This is all fairly simple. Digging the information out of
2968 co_lnotab takes some work, but is conceptually clear.
2969
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002970 Somewhat harder to explain is why we don't *always* call the
2971 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002972
2973 Consider this code:
2974
2975 1: def f(a):
2976 2: if a:
2977 3: print 1
2978 4: else:
2979 5: print 2
2980
2981 which compiles to this:
2982
2983 2 0 LOAD_FAST 0 (a)
2984 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00002985 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002986
2987 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00002988 10 PRINT_ITEM
2989 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002990 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00002991 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002992
2993 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00002994 19 PRINT_ITEM
2995 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00002996 >> 21 LOAD_CONST 0 (None)
2997 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002998
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00002999 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003000 15 and the co_lnotab will claim that execution has moved to
3001 line 3. This is at best misleading. In this case we could
3002 associate the POP_TOP with line 4, but that doesn't make
3003 sense in all cases (I think).
3004
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003005 What we do is only call the line trace function if the co_lnotab
3006 indicates we have jumped to the *start* of a line, i.e. if the
3007 current instruction offset matches the offset given for the
3008 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003009
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003010 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003011 Execution will jump from instruction offset 12 to offset 21.
3012 Then the co_lnotab would imply that execution has moved to line
3013 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003014
3015 Why do we set f_lineno when tracing? Well, consider the code
3016 above when 'a' is true. If stepping through this with 'n' in
3017 pdb, you would stop at line 1 with a "call" type event, then
3018 line events on lines 2 and 3, then a "return" type event -- but
3019 you would be shown line 5 during this event. This is a change
3020 from the behaviour in 2.2 and before, and I've found it
3021 confusing in practice. By setting and using f_lineno when
3022 tracing, one can report a line number different from that
3023 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003024 */
3025
Michael W. Hudson006c7522002-11-08 13:08:46 +00003026 int result = 0;
3027
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003028 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003029 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003030 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003031 unsigned char* p;
3032
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003033 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3034 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003035
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003036 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003037 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003038
3039 /* possible optimization: if f->f_lasti == instr_ub
3040 (likely to be a common case) then we already know
3041 instr_lb -- if we stored the matching value of p
3042 somwhere we could skip the first while loop. */
3043
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003044 /* see comments in compile.c for the description of
3045 co_lnotab. A point to remember: increments to p
3046 should come in pairs -- although we don't care about
3047 the line increments here, treating them as byte
3048 increments gets confusing, to say the least. */
3049
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003050 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003051 if (addr + *p > frame->f_lasti)
3052 break;
3053 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003054 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003055 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003056 --size;
3057 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003058
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003059 if (addr == frame->f_lasti) {
3060 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003061 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003062 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003063 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003064
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003065 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003066 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003067 addr += *p++;
3068 if (*p++)
3069 break;
3070 }
3071 *instr_ub = addr;
3072 }
3073 else {
3074 *instr_ub = INT_MAX;
3075 }
3076 }
Armin Rigobf57a142004-03-22 19:24:58 +00003077 else if (frame->f_lasti <= *instr_prev) {
3078 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003079 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003080 PyTrace_LINE, Py_None);
3081 }
3082 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003083 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003084}
3085
Fred Drake5755ce62001-06-27 19:19:46 +00003086void
3087PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003088{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003089 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003090 PyObject *temp = tstate->c_profileobj;
3091 Py_XINCREF(arg);
3092 tstate->c_profilefunc = NULL;
3093 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003094 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003095 Py_XDECREF(temp);
3096 tstate->c_profilefunc = func;
3097 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003098 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003099}
3100
3101void
3102PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3103{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003104 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003105 PyObject *temp = tstate->c_traceobj;
3106 Py_XINCREF(arg);
3107 tstate->c_tracefunc = NULL;
3108 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003109 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003110 Py_XDECREF(temp);
3111 tstate->c_tracefunc = func;
3112 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003113 tstate->use_tracing = ((func != NULL)
3114 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003115}
3116
Guido van Rossumb209a111997-04-29 18:18:01 +00003117PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003118PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003119{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003120 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003121 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003122 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003123 else
3124 return current_frame->f_builtins;
3125}
3126
Guido van Rossumb209a111997-04-29 18:18:01 +00003127PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003128PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003129{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003130 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003131 if (current_frame == NULL)
3132 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003133 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003134 return current_frame->f_locals;
3135}
3136
Guido van Rossumb209a111997-04-29 18:18:01 +00003137PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003138PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003139{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003140 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003141 if (current_frame == NULL)
3142 return NULL;
3143 else
3144 return current_frame->f_globals;
3145}
3146
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003147PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003148PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003149{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003150 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003151 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003152}
3153
Guido van Rossum6135a871995-01-09 17:53:26 +00003154int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003155PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003156{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003157 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003158 return current_frame == NULL ? 0 : current_frame->f_restricted;
3159}
3160
Guido van Rossumbe270261997-05-22 22:26:18 +00003161int
Tim Peters5ba58662001-07-16 02:29:45 +00003162PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003163{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003164 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003165 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003166
3167 if (current_frame != NULL) {
3168 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003169 const int compilerflags = codeflags & PyCF_MASK;
3170 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003171 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003172 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003173 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003174#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003175 if (codeflags & CO_GENERATOR_ALLOWED) {
3176 result = 1;
3177 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3178 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003179#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003180 }
3181 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003182}
3183
3184int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003185Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003186{
Guido van Rossumb209a111997-04-29 18:18:01 +00003187 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003188 if (f == NULL)
3189 return 0;
3190 if (!PyFile_SoftSpace(f, 0))
3191 return 0;
3192 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003193}
3194
Guido van Rossum3f5da241990-12-20 15:06:42 +00003195
Guido van Rossum681d79a1995-07-18 14:51:37 +00003196/* External interface to call any callable object.
3197 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003198
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003199#undef PyEval_CallObject
3200/* for backward compatibility: export this interface */
3201
Guido van Rossumb209a111997-04-29 18:18:01 +00003202PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003203PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003204{
Guido van Rossumb209a111997-04-29 18:18:01 +00003205 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003206}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003207#define PyEval_CallObject(func,arg) \
3208 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003209
Guido van Rossumb209a111997-04-29 18:18:01 +00003210PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003211PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003212{
Jeremy Hylton52820442001-01-03 23:52:36 +00003213 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003214
3215 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003216 arg = PyTuple_New(0);
3217 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003218 PyErr_SetString(PyExc_TypeError,
3219 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003220 return NULL;
3221 }
3222 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003223 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003224
Guido van Rossumb209a111997-04-29 18:18:01 +00003225 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003226 PyErr_SetString(PyExc_TypeError,
3227 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003228 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003229 return NULL;
3230 }
3231
Tim Peters6d6c1a32001-08-02 04:15:00 +00003232 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003233 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003234 return result;
3235}
3236
Tim Peters6d6c1a32001-08-02 04:15:00 +00003237char *
3238PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003239{
3240 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003241 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003242 else if (PyFunction_Check(func))
3243 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3244 else if (PyCFunction_Check(func))
3245 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3246 else if (PyClass_Check(func))
3247 return PyString_AsString(((PyClassObject*)func)->cl_name);
3248 else if (PyInstance_Check(func)) {
3249 return PyString_AsString(
3250 ((PyInstanceObject*)func)->in_class->cl_name);
3251 } else {
3252 return func->ob_type->tp_name;
3253 }
3254}
3255
Tim Peters6d6c1a32001-08-02 04:15:00 +00003256char *
3257PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003258{
3259 if (PyMethod_Check(func))
3260 return "()";
3261 else if (PyFunction_Check(func))
3262 return "()";
3263 else if (PyCFunction_Check(func))
3264 return "()";
3265 else if (PyClass_Check(func))
3266 return " constructor";
3267 else if (PyInstance_Check(func)) {
3268 return " instance";
3269 } else {
3270 return " object";
3271 }
3272}
3273
Martin v. Löwise440e472004-06-01 15:22:42 +00003274PyObject *
3275PyEval_EvaluateFrame(PyObject *fo)
3276{
3277 return eval_frame((PyFrameObject *)fo);
3278}
3279
Jeremy Hylton52820442001-01-03 23:52:36 +00003280#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3281
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003282static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003283err_args(PyObject *func, int flags, int nargs)
3284{
3285 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003286 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003287 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003288 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003289 nargs);
3290 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003291 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003292 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003293 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003294 nargs);
3295}
3296
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003297#define BEGIN_C_TRACE \
3298if (tstate->use_tracing) { \
3299 if (tstate->c_profilefunc != NULL) { \
3300 PyObject *func_name = \
3301 PyString_FromString (((PyCFunctionObject *) \
3302 func)->m_ml->ml_name); \
3303 are_tracing = 1; \
3304 if (call_trace(tstate->c_profilefunc, \
3305 tstate->c_profileobj, \
3306 tstate->frame, PyTrace_C_CALL, \
3307 func_name)) \
3308 { return NULL; } \
3309 Py_DECREF (func_name); \
3310 } \
3311 }
3312
3313#define END_C_TRACE \
3314 if (tstate->use_tracing && are_tracing) { \
3315 if (tstate->c_profilefunc != NULL) { \
3316 if (x == NULL) { \
3317 if (call_trace (tstate->c_profilefunc, \
3318 tstate->c_profileobj, \
3319 tstate->frame, PyTrace_C_EXCEPTION, \
3320 NULL)) \
3321 { return NULL; } \
3322 } else { \
3323 if (call_trace(tstate->c_profilefunc, \
3324 tstate->c_profileobj, \
3325 tstate->frame, PyTrace_C_RETURN, \
3326 NULL)) \
3327 { return NULL; } \
3328 } \
3329 } \
3330 }
3331
3332
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003333static PyObject *
3334call_function(PyObject ***pp_stack, int oparg)
3335{
3336 int na = oparg & 0xff;
3337 int nk = (oparg>>8) & 0xff;
3338 int n = na + 2 * nk;
3339 PyObject **pfunc = (*pp_stack) - n - 1;
3340 PyObject *func = *pfunc;
3341 PyObject *x, *w;
3342
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003343 int are_tracing = 0;
3344
3345 PyThreadState *tstate = PyThreadState_GET();
3346
Jeremy Hylton985eba52003-02-05 23:13:00 +00003347 /* Always dispatch PyCFunction first, because these are
3348 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003349 */
3350 if (PyCFunction_Check(func) && nk == 0) {
3351 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003352 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003353 if (flags & (METH_NOARGS | METH_O)) {
3354 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3355 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003356 if (flags & METH_NOARGS && na == 0) {
3357 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003358 x = (*meth)(self, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003359 END_C_TRACE
3360 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003361 else if (flags & METH_O && na == 1) {
3362 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003363 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003364 x = (*meth)(self, arg);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003365 END_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003366 Py_DECREF(arg);
3367 }
3368 else {
3369 err_args(func, flags, na);
3370 x = NULL;
3371 }
3372 }
3373 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003374 PyObject *callargs;
3375 callargs = load_args(pp_stack, na);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003376 BEGIN_C_TRACE
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003377 x = PyCFunction_Call(func, callargs, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003378 END_C_TRACE
Tim Peters8a5c3c72004-04-05 19:36:21 +00003379 Py_XDECREF(callargs);
3380 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003381 } else {
3382 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3383 /* optimize access to bound methods */
3384 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003385 PCALL(PCALL_METHOD);
3386 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003387 Py_INCREF(self);
3388 func = PyMethod_GET_FUNCTION(func);
3389 Py_INCREF(func);
3390 Py_DECREF(*pfunc);
3391 *pfunc = self;
3392 na++;
3393 n++;
3394 } else
3395 Py_INCREF(func);
3396 if (PyFunction_Check(func))
3397 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003398 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003399 x = do_call(func, pp_stack, na, nk);
3400 Py_DECREF(func);
3401 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003402
Jeremy Hylton985eba52003-02-05 23:13:00 +00003403 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003404 while ((*pp_stack) > pfunc) {
3405 w = EXT_POP(*pp_stack);
3406 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003407 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003408 }
3409 return x;
3410}
3411
Jeremy Hylton192690e2002-08-16 18:36:11 +00003412/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003413 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003414 For the simplest case -- a function that takes only positional
3415 arguments and is called with only positional arguments -- it
3416 inlines the most primitive frame setup code from
3417 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3418 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003419*/
3420
3421static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003422fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003423{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003424 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003425 PyObject *globals = PyFunction_GET_GLOBALS(func);
3426 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3427 PyObject **d = NULL;
3428 int nd = 0;
3429
Jeremy Hylton985eba52003-02-05 23:13:00 +00003430 PCALL(PCALL_FUNCTION);
3431 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003432 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003433 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3434 PyFrameObject *f;
3435 PyObject *retval = NULL;
3436 PyThreadState *tstate = PyThreadState_GET();
3437 PyObject **fastlocals, **stack;
3438 int i;
3439
3440 PCALL(PCALL_FASTER_FUNCTION);
3441 assert(globals != NULL);
3442 /* XXX Perhaps we should create a specialized
3443 PyFrame_New() that doesn't take locals, but does
3444 take builtins without sanity checking them.
3445 */
3446 f = PyFrame_New(tstate, co, globals, NULL);
3447 if (f == NULL)
3448 return NULL;
3449
3450 fastlocals = f->f_localsplus;
3451 stack = (*pp_stack) - n;
3452
3453 for (i = 0; i < n; i++) {
3454 Py_INCREF(*stack);
3455 fastlocals[i] = *stack++;
3456 }
3457 retval = eval_frame(f);
3458 assert(tstate != NULL);
3459 ++tstate->recursion_depth;
3460 Py_DECREF(f);
3461 --tstate->recursion_depth;
3462 return retval;
3463 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003464 if (argdefs != NULL) {
3465 d = &PyTuple_GET_ITEM(argdefs, 0);
3466 nd = ((PyTupleObject *)argdefs)->ob_size;
3467 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003468 return PyEval_EvalCodeEx(co, globals,
3469 (PyObject *)NULL, (*pp_stack)-n, na,
3470 (*pp_stack)-2*nk, nk, d, nd,
3471 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003472}
3473
3474static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003475update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3476 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003477{
3478 PyObject *kwdict = NULL;
3479 if (orig_kwdict == NULL)
3480 kwdict = PyDict_New();
3481 else {
3482 kwdict = PyDict_Copy(orig_kwdict);
3483 Py_DECREF(orig_kwdict);
3484 }
3485 if (kwdict == NULL)
3486 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003487 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003488 int err;
3489 PyObject *value = EXT_POP(*pp_stack);
3490 PyObject *key = EXT_POP(*pp_stack);
3491 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003492 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003493 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003494 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003495 PyEval_GetFuncName(func),
3496 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003497 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003498 Py_DECREF(key);
3499 Py_DECREF(value);
3500 Py_DECREF(kwdict);
3501 return NULL;
3502 }
3503 err = PyDict_SetItem(kwdict, key, value);
3504 Py_DECREF(key);
3505 Py_DECREF(value);
3506 if (err) {
3507 Py_DECREF(kwdict);
3508 return NULL;
3509 }
3510 }
3511 return kwdict;
3512}
3513
3514static PyObject *
3515update_star_args(int nstack, int nstar, PyObject *stararg,
3516 PyObject ***pp_stack)
3517{
3518 PyObject *callargs, *w;
3519
3520 callargs = PyTuple_New(nstack + nstar);
3521 if (callargs == NULL) {
3522 return NULL;
3523 }
3524 if (nstar) {
3525 int i;
3526 for (i = 0; i < nstar; i++) {
3527 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3528 Py_INCREF(a);
3529 PyTuple_SET_ITEM(callargs, nstack + i, a);
3530 }
3531 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003532 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003533 w = EXT_POP(*pp_stack);
3534 PyTuple_SET_ITEM(callargs, nstack, w);
3535 }
3536 return callargs;
3537}
3538
3539static PyObject *
3540load_args(PyObject ***pp_stack, int na)
3541{
3542 PyObject *args = PyTuple_New(na);
3543 PyObject *w;
3544
3545 if (args == NULL)
3546 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003547 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003548 w = EXT_POP(*pp_stack);
3549 PyTuple_SET_ITEM(args, na, w);
3550 }
3551 return args;
3552}
3553
3554static PyObject *
3555do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3556{
3557 PyObject *callargs = NULL;
3558 PyObject *kwdict = NULL;
3559 PyObject *result = NULL;
3560
3561 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003562 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003563 if (kwdict == NULL)
3564 goto call_fail;
3565 }
3566 callargs = load_args(pp_stack, na);
3567 if (callargs == NULL)
3568 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003569#ifdef CALL_PROFILE
3570 /* At this point, we have to look at the type of func to
3571 update the call stats properly. Do it here so as to avoid
3572 exposing the call stats machinery outside ceval.c
3573 */
3574 if (PyFunction_Check(func))
3575 PCALL(PCALL_FUNCTION);
3576 else if (PyMethod_Check(func))
3577 PCALL(PCALL_METHOD);
3578 else if (PyType_Check(func))
3579 PCALL(PCALL_TYPE);
3580 else
3581 PCALL(PCALL_OTHER);
3582#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003583 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003584 call_fail:
3585 Py_XDECREF(callargs);
3586 Py_XDECREF(kwdict);
3587 return result;
3588}
3589
3590static PyObject *
3591ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3592{
3593 int nstar = 0;
3594 PyObject *callargs = NULL;
3595 PyObject *stararg = NULL;
3596 PyObject *kwdict = NULL;
3597 PyObject *result = NULL;
3598
3599 if (flags & CALL_FLAG_KW) {
3600 kwdict = EXT_POP(*pp_stack);
3601 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003602 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003603 "%s%s argument after ** "
3604 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003605 PyEval_GetFuncName(func),
3606 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003607 goto ext_call_fail;
3608 }
3609 }
3610 if (flags & CALL_FLAG_VAR) {
3611 stararg = EXT_POP(*pp_stack);
3612 if (!PyTuple_Check(stararg)) {
3613 PyObject *t = NULL;
3614 t = PySequence_Tuple(stararg);
3615 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003616 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3617 PyErr_Format(PyExc_TypeError,
3618 "%s%s argument after * "
3619 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003620 PyEval_GetFuncName(func),
3621 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003622 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003623 goto ext_call_fail;
3624 }
3625 Py_DECREF(stararg);
3626 stararg = t;
3627 }
3628 nstar = PyTuple_GET_SIZE(stararg);
3629 }
3630 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003631 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003632 if (kwdict == NULL)
3633 goto ext_call_fail;
3634 }
3635 callargs = update_star_args(na, nstar, stararg, pp_stack);
3636 if (callargs == NULL)
3637 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003638#ifdef CALL_PROFILE
3639 /* At this point, we have to look at the type of func to
3640 update the call stats properly. Do it here so as to avoid
3641 exposing the call stats machinery outside ceval.c
3642 */
3643 if (PyFunction_Check(func))
3644 PCALL(PCALL_FUNCTION);
3645 else if (PyMethod_Check(func))
3646 PCALL(PCALL_METHOD);
3647 else if (PyType_Check(func))
3648 PCALL(PCALL_TYPE);
3649 else
3650 PCALL(PCALL_OTHER);
3651#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003652 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003653 ext_call_fail:
3654 Py_XDECREF(callargs);
3655 Py_XDECREF(kwdict);
3656 Py_XDECREF(stararg);
3657 return result;
3658}
3659
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003660#define SLICE_ERROR_MSG \
3661 "standard sequence type does not support step size other than one"
3662
Tim Peterscb479e72001-12-16 19:11:44 +00003663/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3664 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3665 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3666*/
Tim Petersb5196382001-12-16 19:44:20 +00003667/* Note: If v is NULL, return success without storing into *pi. This
3668 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3669 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003670*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003671int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003672_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003673{
Tim Petersb5196382001-12-16 19:44:20 +00003674 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003675 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003676 if (PyInt_Check(v)) {
3677 x = PyInt_AsLong(v);
3678 } else if (PyLong_Check(v)) {
3679 x = PyLong_AsLong(v);
3680 if (x==-1 && PyErr_Occurred()) {
3681 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003682 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003683
Guido van Rossumac7be682001-01-17 15:42:30 +00003684 if (!PyErr_ExceptionMatches(
3685 PyExc_OverflowError)) {
3686 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003687 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003688 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003689 }
3690
Guido van Rossumac7be682001-01-17 15:42:30 +00003691 /* Clear the OverflowError */
3692 PyErr_Clear();
3693
3694 /* It's an overflow error, so we need to
3695 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003696 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003697 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003698
3699 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003700 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003701 if (long_zero == NULL)
3702 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003703
3704 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003705 cmp = PyObject_RichCompareBool(v, long_zero,
3706 Py_GT);
3707 Py_DECREF(long_zero);
3708 if (cmp < 0)
3709 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003710 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003711 x = INT_MAX;
3712 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003713 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003714 }
3715 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003716 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003717 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003718 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003719 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003720 /* Truncate -- very long indices are truncated anyway */
3721 if (x > INT_MAX)
3722 x = INT_MAX;
3723 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003724 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003725 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003726 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003727 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003728}
3729
Guido van Rossum50d756e2001-08-18 17:43:36 +00003730#undef ISINT
3731#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3732
Guido van Rossumb209a111997-04-29 18:18:01 +00003733static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003734apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003735{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003736 PyTypeObject *tp = u->ob_type;
3737 PySequenceMethods *sq = tp->tp_as_sequence;
3738
3739 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3740 int ilow = 0, ihigh = INT_MAX;
3741 if (!_PyEval_SliceIndex(v, &ilow))
3742 return NULL;
3743 if (!_PyEval_SliceIndex(w, &ihigh))
3744 return NULL;
3745 return PySequence_GetSlice(u, ilow, ihigh);
3746 }
3747 else {
3748 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003749 if (slice != NULL) {
3750 PyObject *res = PyObject_GetItem(u, slice);
3751 Py_DECREF(slice);
3752 return res;
3753 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003754 else
3755 return NULL;
3756 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003757}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003758
3759static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003760assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3761 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003762{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003763 PyTypeObject *tp = u->ob_type;
3764 PySequenceMethods *sq = tp->tp_as_sequence;
3765
3766 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3767 int ilow = 0, ihigh = INT_MAX;
3768 if (!_PyEval_SliceIndex(v, &ilow))
3769 return -1;
3770 if (!_PyEval_SliceIndex(w, &ihigh))
3771 return -1;
3772 if (x == NULL)
3773 return PySequence_DelSlice(u, ilow, ihigh);
3774 else
3775 return PySequence_SetSlice(u, ilow, ihigh, x);
3776 }
3777 else {
3778 PyObject *slice = PySlice_New(v, w, NULL);
3779 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003780 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003781 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003782 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003783 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003784 res = PyObject_DelItem(u, slice);
3785 Py_DECREF(slice);
3786 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003787 }
3788 else
3789 return -1;
3790 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003791}
3792
Guido van Rossumb209a111997-04-29 18:18:01 +00003793static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003794cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003795{
Guido van Rossumac7be682001-01-17 15:42:30 +00003796 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003797 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003798 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003799 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003800 break;
3801 case PyCmp_IS_NOT:
3802 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003803 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003804 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003805 res = PySequence_Contains(w, v);
3806 if (res < 0)
3807 return NULL;
3808 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003809 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003810 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003811 if (res < 0)
3812 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003813 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003814 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003815 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003816 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003817 break;
3818 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003819 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003820 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003821 v = res ? Py_True : Py_False;
3822 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003823 return v;
3824}
3825
Thomas Wouters52152252000-08-17 22:55:00 +00003826static PyObject *
3827import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003828{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003829 PyObject *x;
3830
3831 x = PyObject_GetAttr(v, name);
3832 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003833 PyErr_Format(PyExc_ImportError,
3834 "cannot import name %.230s",
3835 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003836 }
Thomas Wouters52152252000-08-17 22:55:00 +00003837 return x;
3838}
Guido van Rossumac7be682001-01-17 15:42:30 +00003839
Thomas Wouters52152252000-08-17 22:55:00 +00003840static int
3841import_all_from(PyObject *locals, PyObject *v)
3842{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003843 PyObject *all = PyObject_GetAttrString(v, "__all__");
3844 PyObject *dict, *name, *value;
3845 int skip_leading_underscores = 0;
3846 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003847
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003848 if (all == NULL) {
3849 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3850 return -1; /* Unexpected error */
3851 PyErr_Clear();
3852 dict = PyObject_GetAttrString(v, "__dict__");
3853 if (dict == NULL) {
3854 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3855 return -1;
3856 PyErr_SetString(PyExc_ImportError,
3857 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003858 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003859 }
3860 all = PyMapping_Keys(dict);
3861 Py_DECREF(dict);
3862 if (all == NULL)
3863 return -1;
3864 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003865 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003866
3867 for (pos = 0, err = 0; ; pos++) {
3868 name = PySequence_GetItem(all, pos);
3869 if (name == NULL) {
3870 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3871 err = -1;
3872 else
3873 PyErr_Clear();
3874 break;
3875 }
3876 if (skip_leading_underscores &&
3877 PyString_Check(name) &&
3878 PyString_AS_STRING(name)[0] == '_')
3879 {
3880 Py_DECREF(name);
3881 continue;
3882 }
3883 value = PyObject_GetAttr(v, name);
3884 if (value == NULL)
3885 err = -1;
3886 else
3887 err = PyDict_SetItem(locals, name, value);
3888 Py_DECREF(name);
3889 Py_XDECREF(value);
3890 if (err != 0)
3891 break;
3892 }
3893 Py_DECREF(all);
3894 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003895}
3896
Guido van Rossumb209a111997-04-29 18:18:01 +00003897static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003898build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003899{
Guido van Rossum7851eea2001-09-12 19:19:18 +00003900 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003901
3902 if (PyDict_Check(methods))
3903 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00003904 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00003905 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00003906 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
3907 base = PyTuple_GET_ITEM(bases, 0);
3908 metaclass = PyObject_GetAttrString(base, "__class__");
3909 if (metaclass == NULL) {
3910 PyErr_Clear();
3911 metaclass = (PyObject *)base->ob_type;
3912 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00003913 }
3914 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00003915 else {
3916 PyObject *g = PyEval_GetGlobals();
3917 if (g != NULL && PyDict_Check(g))
3918 metaclass = PyDict_GetItemString(g, "__metaclass__");
3919 if (metaclass == NULL)
3920 metaclass = (PyObject *) &PyClass_Type;
3921 Py_INCREF(metaclass);
3922 }
3923 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
3924 Py_DECREF(metaclass);
3925 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00003926}
3927
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003928static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003929exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3930 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003931{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003932 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003933 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003934 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003935
Guido van Rossumb209a111997-04-29 18:18:01 +00003936 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3937 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003938 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003939 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003940 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003941 locals = PyTuple_GetItem(prog, 2);
3942 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003943 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003944 if (globals == Py_None) {
3945 globals = PyEval_GetGlobals();
3946 if (locals == Py_None) {
3947 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003948 plain = 1;
3949 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003950 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003951 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003952 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003953 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003954 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003955 !PyCode_Check(prog) &&
3956 !PyFile_Check(prog)) {
3957 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003958 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003959 return -1;
3960 }
Fred Drake661ea262000-10-24 19:57:45 +00003961 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003962 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003963 "exec: arg 2 must be a dictionary or None");
3964 return -1;
3965 }
3966 if (!PyDict_Check(locals)) {
3967 PyErr_SetString(PyExc_TypeError,
3968 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003969 return -1;
3970 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003971 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003972 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003973 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00003974 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
3975 PyErr_SetString(PyExc_TypeError,
3976 "code object passed to exec may not contain free variables");
3977 return -1;
3978 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003979 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003980 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003981 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003982 FILE *fp = PyFile_AsFile(prog);
3983 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00003984 PyCompilerFlags cf;
3985 cf.cf_flags = 0;
3986 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003987 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003988 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00003989 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00003990 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003991 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003992 }
3993 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003994 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003995 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00003996 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003997 cf.cf_flags = 0;
3998#ifdef Py_USING_UNICODE
3999 if (PyUnicode_Check(prog)) {
4000 tmp = PyUnicode_AsUTF8String(prog);
4001 if (tmp == NULL)
4002 return -1;
4003 prog = tmp;
4004 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4005 }
4006#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004007 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004008 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004009 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004010 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004011 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004012 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004013 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004014 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004015 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004016 if (plain)
4017 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004018 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004019 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004020 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004021 return 0;
4022}
Guido van Rossum24c13741995-02-14 09:42:43 +00004023
Guido van Rossumac7be682001-01-17 15:42:30 +00004024static void
Paul Prescode68140d2000-08-30 20:25:01 +00004025format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4026{
4027 char *obj_str;
4028
4029 if (!obj)
4030 return;
4031
4032 obj_str = PyString_AsString(obj);
4033 if (!obj_str)
4034 return;
4035
4036 PyErr_Format(exc, format_str, obj_str);
4037}
Guido van Rossum950361c1997-01-24 13:49:28 +00004038
4039#ifdef DYNAMIC_EXECUTION_PROFILE
4040
Skip Montanarof118cb12001-10-15 20:51:38 +00004041static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004042getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004043{
4044 int i;
4045 PyObject *l = PyList_New(256);
4046 if (l == NULL) return NULL;
4047 for (i = 0; i < 256; i++) {
4048 PyObject *x = PyInt_FromLong(a[i]);
4049 if (x == NULL) {
4050 Py_DECREF(l);
4051 return NULL;
4052 }
4053 PyList_SetItem(l, i, x);
4054 }
4055 for (i = 0; i < 256; i++)
4056 a[i] = 0;
4057 return l;
4058}
4059
4060PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004061_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004062{
4063#ifndef DXPAIRS
4064 return getarray(dxp);
4065#else
4066 int i;
4067 PyObject *l = PyList_New(257);
4068 if (l == NULL) return NULL;
4069 for (i = 0; i < 257; i++) {
4070 PyObject *x = getarray(dxpairs[i]);
4071 if (x == NULL) {
4072 Py_DECREF(l);
4073 return NULL;
4074 }
4075 PyList_SetItem(l, i, x);
4076 }
4077 return l;
4078#endif
4079}
4080
4081#endif