blob: 6c457e125c1405ff866570c27666ba0b28afa18b [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"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000019#ifdef WITH_TSC
20#include <asm/msr.h>
21
22typedef unsigned long long uint64;
23
24void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
25 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
26{
27 uint64 intr, inst, loop;
28 PyThreadState *tstate = PyThreadState_Get();
29 if (!tstate->interp->tscdump)
30 return;
31 intr = intr1 - intr0;
32 inst = inst1 - inst0 - intr;
33 loop = loop1 - loop0 - intr;
34 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
35 opcode, ticked, inst, loop);
36}
37#endif
38
Guido van Rossum04691fc1992-08-12 15:35:34 +000039/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000041
Guido van Rossum408027e1996-12-30 16:17:54 +000042#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000043/* For debugging the interpreter: */
44#define LLTRACE 1 /* Low-level trace feature */
45#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000046#endif
47
Jeremy Hylton52820442001-01-03 23:52:36 +000048typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000049
Guido van Rossum374a9221991-04-04 10:40:29 +000050/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000051#ifdef WITH_TSC
52static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
53#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000054static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000055#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000056static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000057static PyObject *do_call(PyObject *, PyObject ***, int, int);
58static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000059static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000060static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000061static PyObject *load_args(PyObject ***, int);
62#define CALL_FLAG_VAR 1
63#define CALL_FLAG_KW 2
64
Guido van Rossum0a066c01992-03-27 17:29:15 +000065#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000067#endif
Fred Drake5755ce62001-06-27 19:19:46 +000068static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
69 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000070static void call_trace_protected(Py_tracefunc, PyObject *,
71 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000072static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000073static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +000074 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000075
Tim Petersdbd9ba62000-07-09 03:09:57 +000076static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
77static int assign_slice(PyObject *, PyObject *,
78 PyObject *, PyObject *);
79static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000080static PyObject *import_from(PyObject *, PyObject *);
81static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000082static PyObject *build_class(PyObject *, PyObject *, PyObject *);
83static int exec_statement(PyFrameObject *,
84 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000085static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
86static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000087static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000088
Paul Prescode68140d2000-08-30 20:25:01 +000089#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000090 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000091#define GLOBAL_NAME_ERROR_MSG \
92 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000093#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000094 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000095#define UNBOUNDFREE_ERROR_MSG \
96 "free variable '%.200s' referenced before assignment" \
97 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000098
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
Jeremy Hylton985eba52003-02-05 23:13:00 +0000109/* Function call profile */
110#ifdef CALL_PROFILE
111#define PCALL_NUM 11
112static int pcall[PCALL_NUM];
113
114#define PCALL_ALL 0
115#define PCALL_FUNCTION 1
116#define PCALL_FAST_FUNCTION 2
117#define PCALL_FASTER_FUNCTION 3
118#define PCALL_METHOD 4
119#define PCALL_BOUND_METHOD 5
120#define PCALL_CFUNCTION 6
121#define PCALL_TYPE 7
122#define PCALL_GENERATOR 8
123#define PCALL_OTHER 9
124#define PCALL_POP 10
125
126/* Notes about the statistics
127
128 PCALL_FAST stats
129
130 FAST_FUNCTION means no argument tuple needs to be created.
131 FASTER_FUNCTION means that the fast-path frame setup code is used.
132
133 If there is a method call where the call can be optimized by changing
134 the argument tuple and calling the function directly, it gets recorded
135 twice.
136
137 As a result, the relationship among the statistics appears to be
138 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
139 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
140 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
141 PCALL_METHOD > PCALL_BOUND_METHOD
142*/
143
144#define PCALL(POS) pcall[POS]++
145
146PyObject *
147PyEval_GetCallStats(PyObject *self)
148{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000149 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000150 pcall[0], pcall[1], pcall[2], pcall[3],
151 pcall[4], pcall[5], pcall[6], pcall[7],
152 pcall[8], pcall[9]);
153}
154#else
155#define PCALL(O)
156
157PyObject *
158PyEval_GetCallStats(PyObject *self)
159{
160 Py_INCREF(Py_None);
161 return Py_None;
162}
163#endif
164
Tim Peters5ca576e2001-06-18 22:08:13 +0000165
Guido van Rossume59214e1994-08-30 08:01:59 +0000166#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000167
Guido van Rossum2571cc81999-04-07 16:07:23 +0000168#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000170#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000171#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000172
Guido van Rossuma027efa1997-05-05 20:56:21 +0000173extern int _PyThread_Started; /* Flag for Py_Exit */
174
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000175static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000176static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177
178void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000179PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000182 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000184 interpreter_lock = PyThread_allocate_lock();
185 PyThread_acquire_lock(interpreter_lock, 1);
186 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000188
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000189void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000190PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000191{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000192 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199}
200
201void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000203{
204 if (tstate == NULL)
205 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000206 /* Check someone has called PyEval_InitThreads() to create the lock */
207 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000208 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209 if (PyThreadState_Swap(tstate) != NULL)
210 Py_FatalError(
211 "PyEval_AcquireThread: non-NULL old thread state");
212}
213
214void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000215PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000216{
217 if (tstate == NULL)
218 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
219 if (PyThreadState_Swap(NULL) != tstate)
220 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000222}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000223
224/* This function is called from PyOS_AfterFork to ensure that newly
225 created child processes don't hold locks referring to threads which
226 are not running in the child process. (This could also be done using
227 pthread_atfork mechanism, at least for the pthreads implementation.) */
228
229void
230PyEval_ReInitThreads(void)
231{
232 if (!interpreter_lock)
233 return;
234 /*XXX Can't use PyThread_free_lock here because it does too
235 much error-checking. Doing this cleanly would require
236 adding a new function to each thread_*.h. Instead, just
237 create a new lock and waste a little bit of memory */
238 interpreter_lock = PyThread_allocate_lock();
239 PyThread_acquire_lock(interpreter_lock, 1);
240 main_thread = PyThread_get_thread_ident();
241}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242#endif
243
Guido van Rossumff4949e1992-08-05 19:58:53 +0000244/* Functions save_thread and restore_thread are always defined so
245 dynamically loaded modules needn't be compiled separately for use
246 with and without threads: */
247
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000248PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000251 PyThreadState *tstate = PyThreadState_Swap(NULL);
252 if (tstate == NULL)
253 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000254#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000255 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000257#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000258 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000259}
260
261void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000263{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000264 if (tstate == NULL)
265 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000266#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000268 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000270 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271 }
272#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000273 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274}
275
276
Guido van Rossuma9672091994-09-14 13:31:22 +0000277/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
278 signal handlers or Mac I/O completion routines) can schedule calls
279 to a function to be called synchronously.
280 The synchronous function is called with one void* argument.
281 It should return 0 for success or -1 for failure -- failure should
282 be accompanied by an exception.
283
284 If registry succeeds, the registry function returns 0; if it fails
285 (e.g. due to too many pending calls) it returns -1 (without setting
286 an exception condition).
287
288 Note that because registry may occur from within signal handlers,
289 or other asynchronous events, calling malloc() is unsafe!
290
291#ifdef WITH_THREAD
292 Any thread can schedule pending calls, but only the main thread
293 will execute them.
294#endif
295
296 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
297 There are two possible race conditions:
298 (1) nested asynchronous registry calls;
299 (2) registry calls made while pending calls are being processed.
300 While (1) is very unlikely, (2) is a real possibility.
301 The current code is safe against (2), but not against (1).
302 The safety against (2) is derived from the fact that only one
303 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000304
Guido van Rossuma027efa1997-05-05 20:56:21 +0000305 XXX Darn! With the advent of thread state, we should have an array
306 of pending calls per thread in the thread state! Later...
307*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309#define NPENDINGCALLS 32
310static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000311 int (*func)(void *);
312 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000313} pendingcalls[NPENDINGCALLS];
314static volatile int pendingfirst = 0;
315static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000316static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000317
318int
Thomas Wouters334fb892000-07-25 12:56:38 +0000319Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000320{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000321 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000322 int i, j;
323 /* XXX Begin critical section */
324 /* XXX If you want this to be safe against nested
325 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000326 if (busy)
327 return -1;
328 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000329 i = pendinglast;
330 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000331 if (j == pendingfirst) {
332 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000333 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000334 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000335 pendingcalls[i].func = func;
336 pendingcalls[i].arg = arg;
337 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000338
339 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000340 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000341 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000342 /* XXX End critical section */
343 return 0;
344}
345
Guido van Rossum180d7b41994-09-29 09:45:57 +0000346int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000348{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000349 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000350#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000351 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000352 return 0;
353#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000354 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000355 return 0;
356 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000357 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000358 for (;;) {
359 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000360 int (*func)(void *);
361 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000362 i = pendingfirst;
363 if (i == pendinglast)
364 break; /* Queue empty */
365 func = pendingcalls[i].func;
366 arg = pendingcalls[i].arg;
367 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000368 if (func(arg) < 0) {
369 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000370 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000371 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000372 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000373 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000374 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 return 0;
376}
377
378
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000379/* The interpreter's recursion limit */
380
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000381static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000382int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000383
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000384int
385Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000386{
387 return recursion_limit;
388}
389
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000390void
391Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000392{
393 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000394 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000395}
396
Armin Rigo2b3eb402003-10-28 12:05:48 +0000397/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
398 if the recursion_depth reaches _Py_CheckRecursionLimit.
399 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
400 to guarantee that _Py_CheckRecursiveCall() is regularly called.
401 Without USE_STACKCHECK, there is no need for this. */
402int
403_Py_CheckRecursiveCall(char *where)
404{
405 PyThreadState *tstate = PyThreadState_GET();
406
407#ifdef USE_STACKCHECK
408 if (PyOS_CheckStack()) {
409 --tstate->recursion_depth;
410 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
411 return -1;
412 }
413#endif
414 if (tstate->recursion_depth > recursion_limit) {
415 --tstate->recursion_depth;
416 PyErr_Format(PyExc_RuntimeError,
417 "maximum recursion depth exceeded%s",
418 where);
419 return -1;
420 }
421 _Py_CheckRecursionLimit = recursion_limit;
422 return 0;
423}
424
Guido van Rossum374a9221991-04-04 10:40:29 +0000425/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000426enum why_code {
427 WHY_NOT = 0x0001, /* No error */
428 WHY_EXCEPTION = 0x0002, /* Exception occurred */
429 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
430 WHY_RETURN = 0x0008, /* 'return' statement */
431 WHY_BREAK = 0x0010, /* 'break' statement */
432 WHY_CONTINUE = 0x0020, /* 'continue' statement */
433 WHY_YIELD = 0x0040 /* 'yield' operator */
434};
Guido van Rossum374a9221991-04-04 10:40:29 +0000435
Raymond Hettinger7c958652004-04-06 10:11:10 +0000436static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000437static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000438
Skip Montanarod581d772002-09-03 20:10:45 +0000439/* for manipulating the thread switch and periodic "stuff" - used to be
440 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000441int _Py_CheckInterval = 100;
442volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000443
Guido van Rossumb209a111997-04-29 18:18:01 +0000444PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000445PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000446{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000447 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000448 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000449 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000450 (PyObject **)NULL, 0,
451 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000452 (PyObject **)NULL, 0,
453 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000454}
455
456
457/* Interpreter main loop */
458
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000459PyObject *
460PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000461{
Guido van Rossum950361c1997-01-24 13:49:28 +0000462#ifdef DXPAIRS
463 int lastopcode = 0;
464#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000465 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000466 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000467 register int opcode; /* Current opcode */
468 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000469 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000470 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000471 register PyObject *x; /* Result object -- NULL if error */
472 register PyObject *v; /* Temporary objects popped off stack */
473 register PyObject *w;
474 register PyObject *u;
475 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000476 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000477 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000478 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000479 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000480 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000481
Tim Peters8a5c3c72004-04-05 19:36:21 +0000482 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000483
484 not (instr_lb <= current_bytecode_offset < instr_ub)
485
Tim Peters8a5c3c72004-04-05 19:36:21 +0000486 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000487 initial values are such as to make this false the first
488 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000489 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000490
Guido van Rossumd076c731998-10-07 19:42:25 +0000491 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000492 PyObject *names;
493 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000494#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000495 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000496#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000497#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000499 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000500#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000501
Neal Norwitza81d2202002-07-14 00:27:26 +0000502/* Tuple access macros */
503
504#ifndef Py_DEBUG
505#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
506#else
507#define GETITEM(v, i) PyTuple_GetItem((v), (i))
508#endif
509
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000510#ifdef WITH_TSC
511/* Use Pentium timestamp counter to mark certain events:
512 inst0 -- beginning of switch statement for opcode dispatch
513 inst1 -- end of switch statement (may be skipped)
514 loop0 -- the top of the mainloop
515 loop1 -- place where control returns again to top of mainloop
516 (may be skipped)
517 intr1 -- beginning of long interruption
518 intr2 -- end of long interruption
519
520 Many opcodes call out to helper C functions. In some cases, the
521 time in those functions should be counted towards the time for the
522 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
523 calls another Python function; there's no point in charge all the
524 bytecode executed by the called function to the caller.
525
526 It's hard to make a useful judgement statically. In the presence
527 of operator overloading, it's impossible to tell if a call will
528 execute new Python code or not.
529
530 It's a case-by-case judgement. I'll use intr1 for the following
531 cases:
532
533 EXEC_STMT
534 IMPORT_STAR
535 IMPORT_FROM
536 CALL_FUNCTION (and friends)
537
538 */
539 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
540 int ticked = 0;
541
542 rdtscll(inst0);
543 rdtscll(inst1);
544 rdtscll(loop0);
545 rdtscll(loop1);
546#endif
547
Guido van Rossum374a9221991-04-04 10:40:29 +0000548/* Code access macros */
549
Guido van Rossumd076c731998-10-07 19:42:25 +0000550#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000551#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000552#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000553#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000554#define JUMPBY(x) (next_instr += (x))
555
Raymond Hettingerf606f872003-03-16 03:11:04 +0000556/* OpCode prediction macros
557 Some opcodes tend to come in pairs thus making it possible to predict
558 the second code when the first is run. For example, COMPARE_OP is often
559 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
560 followed by a POP_TOP.
561
562 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000563 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000564 processor has a high likelihood of making its own successful branch
565 prediction which results in a nearly zero overhead transition to the
566 next opcode.
567
568 A successful prediction saves a trip through the eval-loop including
569 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000570
Tim Peters8a5c3c72004-04-05 19:36:21 +0000571 If collecting opcode statistics, turn off prediction so that
572 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000573 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000574*/
575
Raymond Hettingera7216982004-02-08 19:59:27 +0000576#ifdef DYNAMIC_EXECUTION_PROFILE
577#define PREDICT(op) if (0) goto PRED_##op
578#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000579#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000580#endif
581
Raymond Hettingerf606f872003-03-16 03:11:04 +0000582#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000583#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
584 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000585
Guido van Rossum374a9221991-04-04 10:40:29 +0000586/* Stack manipulation macros */
587
588#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
589#define EMPTY() (STACK_LEVEL() == 0)
590#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000591#define SECOND() (stack_pointer[-2])
592#define THIRD() (stack_pointer[-3])
593#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000594#define SET_TOP(v) (stack_pointer[-1] = (v))
595#define SET_SECOND(v) (stack_pointer[-2] = (v))
596#define SET_THIRD(v) (stack_pointer[-3] = (v))
597#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000598#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000599#define BASIC_PUSH(v) (*stack_pointer++ = (v))
600#define BASIC_POP() (*--stack_pointer)
601
Guido van Rossum96a42c81992-01-12 02:29:51 +0000602#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000603#define PUSH(v) { (void)(BASIC_PUSH(v), \
604 lltrace && prtrace(TOP(), "push")); \
605 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000606#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000607#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
608 lltrace && prtrace(TOP(), "stackadj")); \
609 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000610#else
611#define PUSH(v) BASIC_PUSH(v)
612#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000613#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000614#endif
615
Guido van Rossum681d79a1995-07-18 14:51:37 +0000616/* Local variable macros */
617
618#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000619
620/* The SETLOCAL() macro must not DECREF the local variable in-place and
621 then store the new value; it must copy the old value to a temporary
622 value, then store the new value, and then DECREF the temporary value.
623 This is because it is possible that during the DECREF the frame is
624 accessed by other code (e.g. a __del__ method or gc.collect()) and the
625 variable would be pointing to already-freed memory. */
626#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
627 GETLOCAL(i) = value; \
628 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000629
Guido van Rossuma027efa1997-05-05 20:56:21 +0000630/* Start of code */
631
Tim Peters5ca576e2001-06-18 22:08:13 +0000632 if (f == NULL)
633 return NULL;
634
Armin Rigo1d313ab2003-10-25 14:33:09 +0000635 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000636 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000637 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000638
Tim Peters5ca576e2001-06-18 22:08:13 +0000639 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000640
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000641 if (tstate->use_tracing) {
642 if (tstate->c_tracefunc != NULL) {
643 /* tstate->c_tracefunc, if defined, is a
644 function that will be called on *every* entry
645 to a code block. Its return value, if not
646 None, is a function that will be called at
647 the start of each executed line of code.
648 (Actually, the function must return itself
649 in order to continue tracing.) The trace
650 functions are called with three arguments:
651 a pointer to the current frame, a string
652 indicating why the function is called, and
653 an argument which depends on the situation.
654 The global trace function is also called
655 whenever an exception is detected. */
656 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
657 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000658 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000659 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000660 }
661 }
662 if (tstate->c_profilefunc != NULL) {
663 /* Similar for c_profilefunc, except it needn't
664 return itself and isn't called for "line" events */
665 if (call_trace(tstate->c_profilefunc,
666 tstate->c_profileobj,
667 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000668 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000670 }
671 }
672 }
673
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000674 co = f->f_code;
675 names = co->co_names;
676 consts = co->co_consts;
677 fastlocals = f->f_localsplus;
678 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000679 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000680 /* An explanation is in order for the next line.
681
682 f->f_lasti now refers to the index of the last instruction
683 executed. You might think this was obvious from the name, but
684 this wasn't always true before 2.3! PyFrame_New now sets
685 f->f_lasti to -1 (i.e. the index *before* the first instruction)
686 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
687 does work. Promise. */
688 next_instr = first_instr + f->f_lasti + 1;
689 stack_pointer = f->f_stacktop;
690 assert(stack_pointer != NULL);
691 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693#ifdef LLTRACE
694 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
695#endif
696#if defined(Py_DEBUG) || defined(LLTRACE)
697 filename = PyString_AsString(co->co_filename);
698#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000699
Guido van Rossum374a9221991-04-04 10:40:29 +0000700 why = WHY_NOT;
701 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000702 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000703 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000704
Guido van Rossum374a9221991-04-04 10:40:29 +0000705 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000706#ifdef WITH_TSC
707 if (inst1 == 0) {
708 /* Almost surely, the opcode executed a break
709 or a continue, preventing inst1 from being set
710 on the way out of the loop.
711 */
712 rdtscll(inst1);
713 loop1 = inst1;
714 }
715 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
716 intr0, intr1);
717 ticked = 0;
718 inst1 = 0;
719 intr0 = 0;
720 intr1 = 0;
721 rdtscll(loop0);
722#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000723 assert(stack_pointer >= f->f_valuestack); /* else underflow */
724 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
725
Guido van Rossuma027efa1997-05-05 20:56:21 +0000726 /* Do periodic things. Doing this every time through
727 the loop would add too much overhead, so we do it
728 only every Nth instruction. We also do it if
729 ``things_to_do'' is set, i.e. when an asynchronous
730 event needs attention (e.g. a signal handler or
731 async I/O handler); see Py_AddPendingCall() and
732 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000733
Skip Montanarod581d772002-09-03 20:10:45 +0000734 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000735 if (*next_instr == SETUP_FINALLY) {
736 /* Make the last opcode before
737 a try: finally: block uninterruptable. */
738 goto fast_next_opcode;
739 }
Skip Montanarod581d772002-09-03 20:10:45 +0000740 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000741 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000742#ifdef WITH_TSC
743 ticked = 1;
744#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000745 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000746 if (Py_MakePendingCalls() < 0) {
747 why = WHY_EXCEPTION;
748 goto on_error;
749 }
750 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000751#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000752 if (interpreter_lock) {
753 /* Give another thread a chance */
754
Guido van Rossum25ce5661997-08-02 03:10:38 +0000755 if (PyThreadState_Swap(NULL) != tstate)
756 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000757 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758
759 /* Other threads may run now */
760
Guido van Rossum65d5b571998-12-21 19:32:43 +0000761 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000762 if (PyThreadState_Swap(tstate) != NULL)
763 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000764
765 /* Check for thread interrupts */
766
767 if (tstate->async_exc != NULL) {
768 x = tstate->async_exc;
769 tstate->async_exc = NULL;
770 PyErr_SetNone(x);
771 Py_DECREF(x);
772 why = WHY_EXCEPTION;
773 goto on_error;
774 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000775 }
776#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000777 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000778
Neil Schemenauer63543862002-02-17 19:10:14 +0000779 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000780 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000781
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000782 /* line-by-line tracing support */
783
784 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
785 /* see maybe_call_line_trace
786 for expository comments */
787 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000788
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000789 err = maybe_call_line_trace(tstate->c_tracefunc,
790 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000791 f, &instr_lb, &instr_ub,
792 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000793 /* Reload possibly changed frame fields */
794 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000795 if (f->f_stacktop != NULL) {
796 stack_pointer = f->f_stacktop;
797 f->f_stacktop = NULL;
798 }
799 if (err) {
800 /* trace function raised an exception */
801 goto on_error;
802 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000803 }
804
805 /* Extract opcode and argument */
806
Guido van Rossum374a9221991-04-04 10:40:29 +0000807 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000808 oparg = 0; /* allows oparg to be stored in a register because
809 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000810 if (HAS_ARG(opcode))
811 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000812 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000813#ifdef DYNAMIC_EXECUTION_PROFILE
814#ifdef DXPAIRS
815 dxpairs[lastopcode][opcode]++;
816 lastopcode = opcode;
817#endif
818 dxp[opcode]++;
819#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000820
Guido van Rossum96a42c81992-01-12 02:29:51 +0000821#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000822 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000823
Guido van Rossum96a42c81992-01-12 02:29:51 +0000824 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000825 if (HAS_ARG(opcode)) {
826 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000827 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 }
829 else {
830 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000831 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000832 }
833 }
834#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000835
Guido van Rossum374a9221991-04-04 10:40:29 +0000836 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000837#ifdef WITH_TSC
838 rdtscll(inst0);
839#endif
Jeremy Hylton52820442001-01-03 23:52:36 +0000840
Guido van Rossum374a9221991-04-04 10:40:29 +0000841 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 /* BEWARE!
844 It is essential that any operation that fails sets either
845 x to NULL, err to nonzero, or why to anything but WHY_NOT,
846 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000847
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000850 case NOP:
851 goto fast_next_opcode;
852
Neil Schemenauer63543862002-02-17 19:10:14 +0000853 case LOAD_FAST:
854 x = GETLOCAL(oparg);
855 if (x != NULL) {
856 Py_INCREF(x);
857 PUSH(x);
858 goto fast_next_opcode;
859 }
860 format_exc_check_arg(PyExc_UnboundLocalError,
861 UNBOUNDLOCAL_ERROR_MSG,
862 PyTuple_GetItem(co->co_varnames, oparg));
863 break;
864
865 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000866 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000867 Py_INCREF(x);
868 PUSH(x);
869 goto fast_next_opcode;
870
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000871 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000872 case STORE_FAST:
873 v = POP();
874 SETLOCAL(oparg, v);
875 goto fast_next_opcode;
876
Raymond Hettingerf606f872003-03-16 03:11:04 +0000877 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 case POP_TOP:
879 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000880 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000881 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000884 v = TOP();
885 w = SECOND();
886 SET_TOP(w);
887 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000888 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000889
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000891 v = TOP();
892 w = SECOND();
893 x = THIRD();
894 SET_TOP(w);
895 SET_SECOND(x);
896 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000897 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000898
Thomas Wouters434d0822000-08-24 20:11:32 +0000899 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000900 u = TOP();
901 v = SECOND();
902 w = THIRD();
903 x = FOURTH();
904 SET_TOP(v);
905 SET_SECOND(w);
906 SET_THIRD(x);
907 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000908 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000909
Guido van Rossum374a9221991-04-04 10:40:29 +0000910 case DUP_TOP:
911 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000912 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000913 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000914 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000915
Thomas Wouters434d0822000-08-24 20:11:32 +0000916 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000917 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000919 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000920 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000921 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000922 STACKADJ(2);
923 SET_TOP(x);
924 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000925 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000926 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000927 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000928 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000929 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000930 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000932 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000933 STACKADJ(3);
934 SET_TOP(x);
935 SET_SECOND(w);
936 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000937 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000938 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000939 Py_FatalError("invalid argument to DUP_TOPX"
940 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000941 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000942
Guido van Rossum374a9221991-04-04 10:40:29 +0000943 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000945 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000946 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000947 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000948 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000950
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000953 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000954 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000956 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000961 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000962 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000963 if (err == 0) {
964 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000966 continue;
967 }
968 else if (err > 0) {
969 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000971 err = 0;
972 continue;
973 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000974 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000979 x = PyObject_Repr(v);
980 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000982 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Guido van Rossum7928cd71991-10-24 14:59:31 +0000985 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000987 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000988 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000990 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000992
Guido van Rossum50564e81996-01-12 01:13:16 +0000993 case BINARY_POWER:
994 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000996 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000997 Py_DECREF(v);
998 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001000 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001001 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 case BINARY_MULTIPLY:
1004 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001007 Py_DECREF(v);
1008 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001009 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001010 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001012
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001014 if (!_Py_QnewFlag) {
1015 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001017 x = PyNumber_Divide(v, w);
1018 Py_DECREF(v);
1019 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001021 if (x != NULL) continue;
1022 break;
1023 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001024 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001025 BINARY_TRUE_DIVIDE */
1026 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001027 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001029 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 Py_DECREF(v);
1031 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum4668b002001-08-08 05:00:18 +00001036 case BINARY_FLOOR_DIVIDE:
1037 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001038 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001039 x = PyNumber_FloorDivide(v, w);
1040 Py_DECREF(v);
1041 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001043 if (x != NULL) continue;
1044 break;
1045
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 case BINARY_MODULO:
1047 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
1051 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001053 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Guido van Rossum374a9221991-04-04 10:40:29 +00001056 case BINARY_ADD:
1057 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001059 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001060 /* INLINE: int + int */
1061 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001062 a = PyInt_AS_LONG(v);
1063 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001064 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001065 if ((i^a) < 0 && (i^b) < 0)
1066 goto slow_add;
1067 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001068 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001069 else {
1070 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001071 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001072 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001073 Py_DECREF(v);
1074 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001076 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 case BINARY_SUBTRACT:
1080 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001082 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001083 /* INLINE: int - int */
1084 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001085 a = PyInt_AS_LONG(v);
1086 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001087 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001088 if ((i^a) < 0 && (i^~b) < 0)
1089 goto slow_sub;
1090 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001091 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001092 else {
1093 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001094 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001095 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001096 Py_DECREF(v);
1097 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001099 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001101
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 case BINARY_SUBSCR:
1103 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001104 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001105 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001106 /* INLINE: list[int] */
1107 long i = PyInt_AsLong(w);
1108 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001109 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001110 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001111 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001112 Py_INCREF(x);
1113 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001114 else
1115 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 }
1117 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001118 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001120 Py_DECREF(v);
1121 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001122 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001123 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Guido van Rossum7928cd71991-10-24 14:59:31 +00001126 case BINARY_LSHIFT:
1127 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001128 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001129 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001130 Py_DECREF(v);
1131 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001133 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001135
Guido van Rossum7928cd71991-10-24 14:59:31 +00001136 case BINARY_RSHIFT:
1137 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001138 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001139 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001140 Py_DECREF(v);
1141 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001142 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001143 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001144 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Guido van Rossum7928cd71991-10-24 14:59:31 +00001146 case BINARY_AND:
1147 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001148 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001149 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001150 Py_DECREF(v);
1151 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001152 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001153 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001154 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Guido van Rossum7928cd71991-10-24 14:59:31 +00001156 case BINARY_XOR:
1157 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001159 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001160 Py_DECREF(v);
1161 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001163 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001164 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Guido van Rossum7928cd71991-10-24 14:59:31 +00001166 case BINARY_OR:
1167 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001168 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001169 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001170 Py_DECREF(v);
1171 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001173 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001174 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001175
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001176 case LIST_APPEND:
1177 w = POP();
1178 v = POP();
1179 err = PyList_Append(v, w);
1180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001182 if (err == 0) {
1183 PREDICT(JUMP_ABSOLUTE);
1184 continue;
1185 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001186 break;
1187
Thomas Wouters434d0822000-08-24 20:11:32 +00001188 case INPLACE_POWER:
1189 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001191 x = PyNumber_InPlacePower(v, w, Py_None);
1192 Py_DECREF(v);
1193 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001194 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001195 if (x != NULL) continue;
1196 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001197
Thomas Wouters434d0822000-08-24 20:11:32 +00001198 case INPLACE_MULTIPLY:
1199 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001201 x = PyNumber_InPlaceMultiply(v, w);
1202 Py_DECREF(v);
1203 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001205 if (x != NULL) continue;
1206 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Thomas Wouters434d0822000-08-24 20:11:32 +00001208 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001209 if (!_Py_QnewFlag) {
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001212 x = PyNumber_InPlaceDivide(v, w);
1213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001216 if (x != NULL) continue;
1217 break;
1218 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001219 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001220 INPLACE_TRUE_DIVIDE */
1221 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001224 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001225 Py_DECREF(v);
1226 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001227 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001228 if (x != NULL) continue;
1229 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001230
Guido van Rossum4668b002001-08-08 05:00:18 +00001231 case INPLACE_FLOOR_DIVIDE:
1232 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001233 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001234 x = PyNumber_InPlaceFloorDivide(v, w);
1235 Py_DECREF(v);
1236 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001237 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001238 if (x != NULL) continue;
1239 break;
1240
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 case INPLACE_MODULO:
1242 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 x = PyNumber_InPlaceRemainder(v, w);
1245 Py_DECREF(v);
1246 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 if (x != NULL) continue;
1249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 case INPLACE_ADD:
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001254 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 /* INLINE: int + int */
1256 register long a, b, i;
1257 a = PyInt_AS_LONG(v);
1258 b = PyInt_AS_LONG(w);
1259 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001260 if ((i^a) < 0 && (i^b) < 0)
1261 goto slow_iadd;
1262 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001264 else {
1265 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001267 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 Py_DECREF(v);
1269 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001271 if (x != NULL) continue;
1272 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Thomas Wouters434d0822000-08-24 20:11:32 +00001274 case INPLACE_SUBTRACT:
1275 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001276 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001277 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 /* INLINE: int - int */
1279 register long a, b, i;
1280 a = PyInt_AS_LONG(v);
1281 b = PyInt_AS_LONG(w);
1282 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001283 if ((i^a) < 0 && (i^~b) < 0)
1284 goto slow_isub;
1285 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001287 else {
1288 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001290 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 if (x != NULL) continue;
1295 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 case INPLACE_LSHIFT:
1298 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 x = PyNumber_InPlaceLshift(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001303 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 if (x != NULL) continue;
1305 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 case INPLACE_RSHIFT:
1308 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 x = PyNumber_InPlaceRshift(v, w);
1311 Py_DECREF(v);
1312 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 if (x != NULL) continue;
1315 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001316
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 case INPLACE_AND:
1318 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001319 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 x = PyNumber_InPlaceAnd(v, w);
1321 Py_DECREF(v);
1322 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001323 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 if (x != NULL) continue;
1325 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001326
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 case INPLACE_XOR:
1328 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001329 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 x = PyNumber_InPlaceXor(v, w);
1331 Py_DECREF(v);
1332 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001333 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 if (x != NULL) continue;
1335 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 case INPLACE_OR:
1338 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceOr(v, w);
1341 Py_DECREF(v);
1342 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 if (x != NULL) continue;
1345 break;
1346
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 case SLICE+0:
1348 case SLICE+1:
1349 case SLICE+2:
1350 case SLICE+3:
1351 if ((opcode-SLICE) & 2)
1352 w = POP();
1353 else
1354 w = NULL;
1355 if ((opcode-SLICE) & 1)
1356 v = POP();
1357 else
1358 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001359 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001360 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001361 Py_DECREF(u);
1362 Py_XDECREF(v);
1363 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001365 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Guido van Rossum374a9221991-04-04 10:40:29 +00001368 case STORE_SLICE+0:
1369 case STORE_SLICE+1:
1370 case STORE_SLICE+2:
1371 case STORE_SLICE+3:
1372 if ((opcode-STORE_SLICE) & 2)
1373 w = POP();
1374 else
1375 w = NULL;
1376 if ((opcode-STORE_SLICE) & 1)
1377 v = POP();
1378 else
1379 v = NULL;
1380 u = POP();
1381 t = POP();
1382 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001383 Py_DECREF(t);
1384 Py_DECREF(u);
1385 Py_XDECREF(v);
1386 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001387 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001388 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Guido van Rossum374a9221991-04-04 10:40:29 +00001390 case DELETE_SLICE+0:
1391 case DELETE_SLICE+1:
1392 case DELETE_SLICE+2:
1393 case DELETE_SLICE+3:
1394 if ((opcode-DELETE_SLICE) & 2)
1395 w = POP();
1396 else
1397 w = NULL;
1398 if ((opcode-DELETE_SLICE) & 1)
1399 v = POP();
1400 else
1401 v = NULL;
1402 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001403 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001404 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001405 Py_DECREF(u);
1406 Py_XDECREF(v);
1407 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001408 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 w = TOP();
1413 v = SECOND();
1414 u = THIRD();
1415 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001416 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001417 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 Py_DECREF(u);
1419 Py_DECREF(v);
1420 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001421 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001425 w = TOP();
1426 v = SECOND();
1427 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001429 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001430 Py_DECREF(v);
1431 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001432 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001433 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001434
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 case PRINT_EXPR:
1436 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001437 w = PySys_GetObject("displayhook");
1438 if (w == NULL) {
1439 PyErr_SetString(PyExc_RuntimeError,
1440 "lost sys.displayhook");
1441 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001442 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001443 }
1444 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001445 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001446 if (x == NULL)
1447 err = -1;
1448 }
1449 if (err == 0) {
1450 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001451 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001452 if (w == NULL)
1453 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001455 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001456 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001458
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001459 case PRINT_ITEM_TO:
1460 w = stream = POP();
1461 /* fall through to PRINT_ITEM */
1462
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 case PRINT_ITEM:
1464 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001465 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001466 w = PySys_GetObject("stdout");
1467 if (w == NULL) {
1468 PyErr_SetString(PyExc_RuntimeError,
1469 "lost sys.stdout");
1470 err = -1;
1471 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001472 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001473 /* PyFile_SoftSpace() can exececute arbitrary code
1474 if sys.stdout is an instance with a __getattr__.
1475 If __getattr__ raises an exception, w will
1476 be freed, so we need to prevent that temporarily. */
1477 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001478 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001479 err = PyFile_WriteString(" ", w);
1480 if (err == 0)
1481 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001482 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001483 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001484 if (PyString_Check(v)) {
1485 char *s = PyString_AS_STRING(v);
1486 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001487 if (len == 0 ||
1488 !isspace(Py_CHARMASK(s[len-1])) ||
1489 s[len-1] == ' ')
1490 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001491 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001492#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001493 else if (PyUnicode_Check(v)) {
1494 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1495 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001496 if (len == 0 ||
1497 !Py_UNICODE_ISSPACE(s[len-1]) ||
1498 s[len-1] == ' ')
1499 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001500 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001501#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001502 else
1503 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001504 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001505 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001507 Py_XDECREF(stream);
1508 stream = NULL;
1509 if (err == 0)
1510 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001513 case PRINT_NEWLINE_TO:
1514 w = stream = POP();
1515 /* fall through to PRINT_NEWLINE */
1516
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001518 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001519 w = PySys_GetObject("stdout");
1520 if (w == NULL)
1521 PyErr_SetString(PyExc_RuntimeError,
1522 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001523 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001524 if (w != NULL) {
1525 err = PyFile_WriteString("\n", w);
1526 if (err == 0)
1527 PyFile_SoftSpace(w, 0);
1528 }
1529 Py_XDECREF(stream);
1530 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001531 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Thomas Wouters434d0822000-08-24 20:11:32 +00001533
1534#ifdef CASE_TOO_BIG
1535 default: switch (opcode) {
1536#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001537 case RAISE_VARARGS:
1538 u = v = w = NULL;
1539 switch (oparg) {
1540 case 3:
1541 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001542 /* Fallthrough */
1543 case 2:
1544 v = POP(); /* value */
1545 /* Fallthrough */
1546 case 1:
1547 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001548 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001549 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001550 break;
1551 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001552 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001554 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001555 break;
1556 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001560 if ((x = f->f_locals) != NULL) {
1561 Py_INCREF(x);
1562 PUSH(x);
1563 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001564 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001565 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001566 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 case RETURN_VALUE:
1569 retval = POP();
1570 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001571 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001572
Tim Peters5ca576e2001-06-18 22:08:13 +00001573 case YIELD_VALUE:
1574 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001575 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001576 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001577 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001578
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001579 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001580 w = TOP();
1581 v = SECOND();
1582 u = THIRD();
1583 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001584#ifdef WITH_TSC
1585 rdtscll(intr0);
1586#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001587 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001588#ifdef WITH_TSC
1589 rdtscll(intr1);
1590#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001591 Py_DECREF(u);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001594 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Guido van Rossum374a9221991-04-04 10:40:29 +00001596 case POP_BLOCK:
1597 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001598 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 while (STACK_LEVEL() > b->b_level) {
1600 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001601 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001602 }
1603 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001604 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 case END_FINALLY:
1607 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001608 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001609 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001610 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001611 if (why == WHY_RETURN ||
1612 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 retval = POP();
1614 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001615 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001618 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001620 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001622 else if (v != Py_None) {
1623 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001624 "'finally' pops bad exception");
1625 why = WHY_EXCEPTION;
1626 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001631 u = TOP();
1632 v = SECOND();
1633 w = THIRD();
1634 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001635 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001636 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001637 Py_DECREF(u);
1638 Py_DECREF(v);
1639 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001643 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001645 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001646 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001647 err = PyDict_SetItem(x, w, v);
1648 else
1649 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001650 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001651 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001652 break;
1653 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001654 PyErr_Format(PyExc_SystemError,
1655 "no locals found when storing %s",
1656 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001660 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001661 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001662 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001663 format_exc_check_arg(PyExc_NameError,
1664 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001665 break;
1666 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001667 PyErr_Format(PyExc_SystemError,
1668 "no locals when deleting %s",
1669 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001671
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001672 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001673 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001675 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1676 PyObject **items = ((PyTupleObject *)v)->ob_item;
1677 while (oparg--) {
1678 w = items[oparg];
1679 Py_INCREF(w);
1680 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001681 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001682 Py_DECREF(v);
1683 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001684 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1685 PyObject **items = ((PyListObject *)v)->ob_item;
1686 while (oparg--) {
1687 w = items[oparg];
1688 Py_INCREF(w);
1689 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001690 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001691 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001692 stack_pointer + oparg))
1693 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001694 else {
1695 if (PyErr_ExceptionMatches(PyExc_TypeError))
1696 PyErr_SetString(PyExc_TypeError,
1697 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001698 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001699 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001700 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001704 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001705 v = TOP();
1706 u = SECOND();
1707 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1709 Py_DECREF(v);
1710 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001711 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001715 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001717 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1718 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001722 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001723 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001724 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001725 err = PyDict_SetItem(f->f_globals, w, v);
1726 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001727 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001730 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001732 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001733 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001734 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001735 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Guido van Rossum374a9221991-04-04 10:40:29 +00001737 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001738 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001739 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001740 PyErr_Format(PyExc_SystemError,
1741 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001742 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001743 break;
1744 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001745 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001746 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001747 Py_XINCREF(x);
1748 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001749 else {
1750 x = PyObject_GetItem(v, w);
1751 if (x == NULL && PyErr_Occurred()) {
1752 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1753 break;
1754 PyErr_Clear();
1755 }
1756 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001758 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001762 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001763 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001764 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 break;
1766 }
1767 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001768 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001771 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001774 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001775 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001776 /* Inline the PyDict_GetItem() calls.
1777 WARNING: this is an extreme speed hack.
1778 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001779 long hash = ((PyStringObject *)w)->ob_shash;
1780 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001781 PyDictObject *d;
1782 d = (PyDictObject *)(f->f_globals);
1783 x = d->ma_lookup(d, w, hash)->me_value;
1784 if (x != NULL) {
1785 Py_INCREF(x);
1786 PUSH(x);
1787 continue;
1788 }
1789 d = (PyDictObject *)(f->f_builtins);
1790 x = d->ma_lookup(d, w, hash)->me_value;
1791 if (x != NULL) {
1792 Py_INCREF(x);
1793 PUSH(x);
1794 continue;
1795 }
1796 goto load_global_error;
1797 }
1798 }
1799 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001800 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001802 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001804 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001805 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001806 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001807 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 break;
1809 }
1810 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001811 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001813 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001814
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001815 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001816 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001817 if (x != NULL) {
1818 SETLOCAL(oparg, NULL);
1819 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001820 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001821 format_exc_check_arg(
1822 PyExc_UnboundLocalError,
1823 UNBOUNDLOCAL_ERROR_MSG,
1824 PyTuple_GetItem(co->co_varnames, oparg)
1825 );
1826 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001828 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001829 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001830 Py_INCREF(x);
1831 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001832 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001833 break;
1834
1835 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001836 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001837 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001838 if (w != NULL) {
1839 PUSH(w);
1840 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001841 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001842 err = -1;
1843 /* Don't stomp existing exception */
1844 if (PyErr_Occurred())
1845 break;
1846 if (oparg < f->f_ncells) {
1847 v = PyTuple_GetItem(co->co_cellvars,
1848 oparg);
1849 format_exc_check_arg(
1850 PyExc_UnboundLocalError,
1851 UNBOUNDLOCAL_ERROR_MSG,
1852 v);
1853 } else {
1854 v = PyTuple_GetItem(
1855 co->co_freevars,
1856 oparg - f->f_ncells);
1857 format_exc_check_arg(
1858 PyExc_NameError,
1859 UNBOUNDFREE_ERROR_MSG,
1860 v);
1861 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001862 break;
1863
1864 case STORE_DEREF:
1865 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001866 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001867 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001868 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001869 continue;
1870
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001872 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001874 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001876 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001877 }
1878 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001879 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 }
1881 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001882
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001886 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001888 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 }
1890 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001891 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 }
1893 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001896 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001898 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001900
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001902 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001903 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001904 x = PyObject_GetAttr(v, w);
1905 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001906 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001907 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 case COMPARE_OP:
1911 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001912 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001913 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001914 /* INLINE: cmp(int, int) */
1915 register long a, b;
1916 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001917 a = PyInt_AS_LONG(v);
1918 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001919 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001920 case PyCmp_LT: res = a < b; break;
1921 case PyCmp_LE: res = a <= b; break;
1922 case PyCmp_EQ: res = a == b; break;
1923 case PyCmp_NE: res = a != b; break;
1924 case PyCmp_GT: res = a > b; break;
1925 case PyCmp_GE: res = a >= b; break;
1926 case PyCmp_IS: res = v == w; break;
1927 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001928 default: goto slow_compare;
1929 }
1930 x = res ? Py_True : Py_False;
1931 Py_INCREF(x);
1932 }
1933 else {
1934 slow_compare:
1935 x = cmp_outcome(oparg, v, w);
1936 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001937 Py_DECREF(v);
1938 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001939 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001940 if (x == NULL) break;
1941 PREDICT(JUMP_IF_FALSE);
1942 PREDICT(JUMP_IF_TRUE);
1943 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001946 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001947 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001948 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001949 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001950 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001951 break;
1952 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001953 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001954 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001955 w,
1956 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001957 f->f_locals == NULL ?
1958 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001959 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001961 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001962 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001963 x = NULL;
1964 break;
1965 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001966#ifdef WITH_TSC
1967 rdtscll(intr0);
1968#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001969 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001970#ifdef WITH_TSC
1971 rdtscll(intr1);
1972#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001973 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001974 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001975 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001976 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001977
Thomas Wouters52152252000-08-17 22:55:00 +00001978 case IMPORT_STAR:
1979 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001980 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001981 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001982 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001983 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001984 break;
1985 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001986#ifdef WITH_TSC
1987 rdtscll(intr0);
1988#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001989 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001990#ifdef WITH_TSC
1991 rdtscll(intr1);
1992#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001993 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001994 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001995 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001996 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001997
Thomas Wouters52152252000-08-17 22:55:00 +00001998 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001999 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002000 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002001#ifdef WITH_TSC
2002 rdtscll(intr0);
2003#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002004 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002005#ifdef WITH_TSC
2006 rdtscll(intr1);
2007#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002008 PUSH(x);
2009 if (x != NULL) continue;
2010 break;
2011
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 case JUMP_FORWARD:
2013 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002014 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Raymond Hettingerf606f872003-03-16 03:11:04 +00002016 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002017 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002018 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002019 if (w == Py_True) {
2020 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002021 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002022 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002023 if (w == Py_False) {
2024 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002025 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002026 }
2027 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002028 if (err > 0)
2029 err = 0;
2030 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002031 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002032 else
2033 break;
2034 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Raymond Hettingerf606f872003-03-16 03:11:04 +00002036 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002038 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002039 if (w == Py_False) {
2040 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002041 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002042 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002043 if (w == Py_True) {
2044 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002045 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002046 }
2047 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002048 if (err > 0) {
2049 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002051 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002052 else if (err == 0)
2053 ;
2054 else
2055 break;
2056 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002057
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002058 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 case JUMP_ABSOLUTE:
2060 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002061 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002062
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 case GET_ITER:
2064 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002065 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002066 x = PyObject_GetIter(v);
2067 Py_DECREF(v);
2068 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002069 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002070 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002071 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002072 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002073 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002074 break;
2075
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002076 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002077 case FOR_ITER:
2078 /* before: [iter]; after: [iter, iter()] *or* [] */
2079 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002080 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002081 if (x != NULL) {
2082 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002083 PREDICT(STORE_FAST);
2084 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002085 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002086 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002087 if (PyErr_Occurred()) {
2088 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2089 break;
2090 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002091 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002092 /* iterator ended normally */
2093 x = v = POP();
2094 Py_DECREF(v);
2095 JUMPBY(oparg);
2096 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002097
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002098 case BREAK_LOOP:
2099 why = WHY_BREAK;
2100 goto fast_block_end;
2101
2102 case CONTINUE_LOOP:
2103 retval = PyInt_FromLong(oparg);
2104 why = WHY_CONTINUE;
2105 goto fast_block_end;
2106
Guido van Rossum374a9221991-04-04 10:40:29 +00002107 case SETUP_LOOP:
2108 case SETUP_EXCEPT:
2109 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002110 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002111 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002112 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Guido van Rossumf10570b1995-07-07 22:53:21 +00002114 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002115 {
2116 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002117 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002118 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002119#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002120 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002121#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002122 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002123#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002124 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002125 PUSH(x);
2126 if (x != NULL)
2127 continue;
2128 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002130
Jeremy Hylton76901512000-03-28 23:49:17 +00002131 case CALL_FUNCTION_VAR:
2132 case CALL_FUNCTION_KW:
2133 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002134 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002135 int na = oparg & 0xff;
2136 int nk = (oparg>>8) & 0xff;
2137 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002138 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002139 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002140 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002141 if (flags & CALL_FLAG_VAR)
2142 n++;
2143 if (flags & CALL_FLAG_KW)
2144 n++;
2145 pfunc = stack_pointer - n - 1;
2146 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002147
Guido van Rossumac7be682001-01-17 15:42:30 +00002148 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002149 && PyMethod_GET_SELF(func) != NULL) {
2150 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002151 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002152 func = PyMethod_GET_FUNCTION(func);
2153 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002154 Py_DECREF(*pfunc);
2155 *pfunc = self;
2156 na++;
2157 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002158 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002159 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002160 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002161#ifdef WITH_TSC
2162 rdtscll(intr0);
2163#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002164 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002165#ifdef WITH_TSC
2166 rdtscll(intr1);
2167#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002168 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002169 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002170
Jeremy Hylton76901512000-03-28 23:49:17 +00002171 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002172 w = POP();
2173 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002174 }
2175 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002176 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002177 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002178 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002179 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002180
Guido van Rossum681d79a1995-07-18 14:51:37 +00002181 case MAKE_FUNCTION:
2182 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002183 x = PyFunction_New(v, f->f_globals);
2184 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002185 /* XXX Maybe this should be a separate opcode? */
2186 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002187 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002188 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002189 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002190 x = NULL;
2191 break;
2192 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002193 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002194 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002195 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002196 }
2197 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002198 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002199 }
2200 PUSH(x);
2201 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002202
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002203 case MAKE_CLOSURE:
2204 {
2205 int nfree;
2206 v = POP(); /* code object */
2207 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002208 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002209 Py_DECREF(v);
2210 /* XXX Maybe this should be a separate opcode? */
2211 if (x != NULL && nfree > 0) {
2212 v = PyTuple_New(nfree);
2213 if (v == NULL) {
2214 Py_DECREF(x);
2215 x = NULL;
2216 break;
2217 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002218 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002219 w = POP();
2220 PyTuple_SET_ITEM(v, nfree, w);
2221 }
2222 err = PyFunction_SetClosure(x, v);
2223 Py_DECREF(v);
2224 }
2225 if (x != NULL && oparg > 0) {
2226 v = PyTuple_New(oparg);
2227 if (v == NULL) {
2228 Py_DECREF(x);
2229 x = NULL;
2230 break;
2231 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002232 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002233 w = POP();
2234 PyTuple_SET_ITEM(v, oparg, w);
2235 }
2236 err = PyFunction_SetDefaults(x, v);
2237 Py_DECREF(v);
2238 }
2239 PUSH(x);
2240 break;
2241 }
2242
Guido van Rossum8861b741996-07-30 16:49:37 +00002243 case BUILD_SLICE:
2244 if (oparg == 3)
2245 w = POP();
2246 else
2247 w = NULL;
2248 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002249 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002250 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002251 Py_DECREF(u);
2252 Py_DECREF(v);
2253 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002254 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002255 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002256 break;
2257
Fred Drakeef8ace32000-08-24 00:32:09 +00002258 case EXTENDED_ARG:
2259 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002260 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002261 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002262
Guido van Rossum374a9221991-04-04 10:40:29 +00002263 default:
2264 fprintf(stderr,
2265 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002266 PyCode_Addr2Line(f->f_code, f->f_lasti),
2267 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002268 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 why = WHY_EXCEPTION;
2270 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002271
2272#ifdef CASE_TOO_BIG
2273 }
2274#endif
2275
Guido van Rossum374a9221991-04-04 10:40:29 +00002276 } /* switch */
2277
2278 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002279
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002280#ifdef WITH_TSC
2281 rdtscll(inst1);
2282#endif
2283
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Guido van Rossum374a9221991-04-04 10:40:29 +00002286 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002287 if (err == 0 && x != NULL) {
2288#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002289 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002290 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002291 fprintf(stderr,
2292 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002293 else {
2294#endif
2295#ifdef WITH_TSC
2296 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002297#endif
2298 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002299#ifdef CHECKEXC
2300 }
2301#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002302 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002303 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002304 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002305 err = 0;
2306 }
2307
Guido van Rossum374a9221991-04-04 10:40:29 +00002308 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002310 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002311 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002312 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002313 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 why = WHY_EXCEPTION;
2315 }
2316 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002317#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002318 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002319 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002321 char buf[1024];
2322 sprintf(buf, "Stack unwind with exception "
2323 "set and why=%d", why);
2324 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002325 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002326 }
2327#endif
2328
2329 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Guido van Rossum374a9221991-04-04 10:40:29 +00002331 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002332 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002333
Fred Drake8f51f542001-10-04 14:48:42 +00002334 if (tstate->c_tracefunc != NULL)
2335 call_exc_trace(tstate->c_tracefunc,
2336 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Guido van Rossum374a9221991-04-04 10:40:29 +00002341 if (why == WHY_RERAISE)
2342 why = WHY_EXCEPTION;
2343
2344 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002345
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002346fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002347 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002348 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002349
Tim Peters8a5c3c72004-04-05 19:36:21 +00002350 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002351 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2352 /* For a continue inside a try block,
2353 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002354 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2355 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002356 why = WHY_NOT;
2357 JUMPTO(PyInt_AS_LONG(retval));
2358 Py_DECREF(retval);
2359 break;
2360 }
2361
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 while (STACK_LEVEL() > b->b_level) {
2363 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 }
2366 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2367 why = WHY_NOT;
2368 JUMPTO(b->b_handler);
2369 break;
2370 }
2371 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002372 (b->b_type == SETUP_EXCEPT &&
2373 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002374 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002375 PyObject *exc, *val, *tb;
2376 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002378 val = Py_None;
2379 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002381 /* Make the raw exception data
2382 available to the handler,
2383 so a program can emulate the
2384 Python main loop. Don't do
2385 this for 'finally'. */
2386 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002387 PyErr_NormalizeException(
2388 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002389 set_exc_info(tstate,
2390 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002391 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002392 if (tb == NULL) {
2393 Py_INCREF(Py_None);
2394 PUSH(Py_None);
2395 } else
2396 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 PUSH(val);
2398 PUSH(exc);
2399 }
2400 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002401 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002402 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002403 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 PUSH(v);
2405 }
2406 why = WHY_NOT;
2407 JUMPTO(b->b_handler);
2408 break;
2409 }
2410 } /* unwind stack */
2411
2412 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002413
Guido van Rossum374a9221991-04-04 10:40:29 +00002414 if (why != WHY_NOT)
2415 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002416#ifdef WITH_TSC
2417 rdtscll(loop1);
2418#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Guido van Rossum374a9221991-04-04 10:40:29 +00002420 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002421
Tim Peters8a5c3c72004-04-05 19:36:21 +00002422 assert(why != WHY_YIELD);
2423 /* Pop remaining stack entries. */
2424 while (!EMPTY()) {
2425 v = POP();
2426 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002427 }
2428
Tim Peters8a5c3c72004-04-05 19:36:21 +00002429 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002430 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002431
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002432fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002433 if (tstate->use_tracing) {
2434 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002435 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002436 if (call_trace(tstate->c_tracefunc,
2437 tstate->c_traceobj, f,
2438 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002439 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002440 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002441 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002442 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002443 }
Fred Drake8f51f542001-10-04 14:48:42 +00002444 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002445 if (why == WHY_EXCEPTION)
2446 call_trace_protected(tstate->c_profilefunc,
2447 tstate->c_profileobj, f,
2448 PyTrace_RETURN);
2449 else if (call_trace(tstate->c_profilefunc,
2450 tstate->c_profileobj, f,
2451 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002452 Py_XDECREF(retval);
2453 retval = NULL;
2454 why = WHY_EXCEPTION;
2455 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002456 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002457 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002458
Guido van Rossuma027efa1997-05-05 20:56:21 +00002459 reset_exc_info(tstate);
2460
Tim Peters5ca576e2001-06-18 22:08:13 +00002461 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002462 exit_eval_frame:
2463 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002464 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Guido van Rossum96a42c81992-01-12 02:29:51 +00002466 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002467}
2468
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002469/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002470 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2471 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002472
Tim Peters6d6c1a32001-08-02 04:15:00 +00002473PyObject *
2474PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002475 PyObject **args, int argcount, PyObject **kws, int kwcount,
2476 PyObject **defs, int defcount, PyObject *closure)
2477{
2478 register PyFrameObject *f;
2479 register PyObject *retval = NULL;
2480 register PyObject **fastlocals, **freevars;
2481 PyThreadState *tstate = PyThreadState_GET();
2482 PyObject *x, *u;
2483
2484 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002485 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002486 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002487 return NULL;
2488 }
2489
Jeremy Hylton985eba52003-02-05 23:13:00 +00002490 assert(globals != NULL);
2491 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002492 if (f == NULL)
2493 return NULL;
2494
2495 fastlocals = f->f_localsplus;
2496 freevars = f->f_localsplus + f->f_nlocals;
2497
2498 if (co->co_argcount > 0 ||
2499 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2500 int i;
2501 int n = argcount;
2502 PyObject *kwdict = NULL;
2503 if (co->co_flags & CO_VARKEYWORDS) {
2504 kwdict = PyDict_New();
2505 if (kwdict == NULL)
2506 goto fail;
2507 i = co->co_argcount;
2508 if (co->co_flags & CO_VARARGS)
2509 i++;
2510 SETLOCAL(i, kwdict);
2511 }
2512 if (argcount > co->co_argcount) {
2513 if (!(co->co_flags & CO_VARARGS)) {
2514 PyErr_Format(PyExc_TypeError,
2515 "%.200s() takes %s %d "
2516 "%sargument%s (%d given)",
2517 PyString_AsString(co->co_name),
2518 defcount ? "at most" : "exactly",
2519 co->co_argcount,
2520 kwcount ? "non-keyword " : "",
2521 co->co_argcount == 1 ? "" : "s",
2522 argcount);
2523 goto fail;
2524 }
2525 n = co->co_argcount;
2526 }
2527 for (i = 0; i < n; i++) {
2528 x = args[i];
2529 Py_INCREF(x);
2530 SETLOCAL(i, x);
2531 }
2532 if (co->co_flags & CO_VARARGS) {
2533 u = PyTuple_New(argcount - n);
2534 if (u == NULL)
2535 goto fail;
2536 SETLOCAL(co->co_argcount, u);
2537 for (i = n; i < argcount; i++) {
2538 x = args[i];
2539 Py_INCREF(x);
2540 PyTuple_SET_ITEM(u, i-n, x);
2541 }
2542 }
2543 for (i = 0; i < kwcount; i++) {
2544 PyObject *keyword = kws[2*i];
2545 PyObject *value = kws[2*i + 1];
2546 int j;
2547 if (keyword == NULL || !PyString_Check(keyword)) {
2548 PyErr_Format(PyExc_TypeError,
2549 "%.200s() keywords must be strings",
2550 PyString_AsString(co->co_name));
2551 goto fail;
2552 }
2553 /* XXX slow -- speed up using dictionary? */
2554 for (j = 0; j < co->co_argcount; j++) {
2555 PyObject *nm = PyTuple_GET_ITEM(
2556 co->co_varnames, j);
2557 int cmp = PyObject_RichCompareBool(
2558 keyword, nm, Py_EQ);
2559 if (cmp > 0)
2560 break;
2561 else if (cmp < 0)
2562 goto fail;
2563 }
2564 /* Check errors from Compare */
2565 if (PyErr_Occurred())
2566 goto fail;
2567 if (j >= co->co_argcount) {
2568 if (kwdict == NULL) {
2569 PyErr_Format(PyExc_TypeError,
2570 "%.200s() got an unexpected "
2571 "keyword argument '%.400s'",
2572 PyString_AsString(co->co_name),
2573 PyString_AsString(keyword));
2574 goto fail;
2575 }
2576 PyDict_SetItem(kwdict, keyword, value);
2577 }
2578 else {
2579 if (GETLOCAL(j) != NULL) {
2580 PyErr_Format(PyExc_TypeError,
2581 "%.200s() got multiple "
2582 "values for keyword "
2583 "argument '%.400s'",
2584 PyString_AsString(co->co_name),
2585 PyString_AsString(keyword));
2586 goto fail;
2587 }
2588 Py_INCREF(value);
2589 SETLOCAL(j, value);
2590 }
2591 }
2592 if (argcount < co->co_argcount) {
2593 int m = co->co_argcount - defcount;
2594 for (i = argcount; i < m; i++) {
2595 if (GETLOCAL(i) == NULL) {
2596 PyErr_Format(PyExc_TypeError,
2597 "%.200s() takes %s %d "
2598 "%sargument%s (%d given)",
2599 PyString_AsString(co->co_name),
2600 ((co->co_flags & CO_VARARGS) ||
2601 defcount) ? "at least"
2602 : "exactly",
2603 m, kwcount ? "non-keyword " : "",
2604 m == 1 ? "" : "s", i);
2605 goto fail;
2606 }
2607 }
2608 if (n > m)
2609 i = n - m;
2610 else
2611 i = 0;
2612 for (; i < defcount; i++) {
2613 if (GETLOCAL(m+i) == NULL) {
2614 PyObject *def = defs[i];
2615 Py_INCREF(def);
2616 SETLOCAL(m+i, def);
2617 }
2618 }
2619 }
2620 }
2621 else {
2622 if (argcount > 0 || kwcount > 0) {
2623 PyErr_Format(PyExc_TypeError,
2624 "%.200s() takes no arguments (%d given)",
2625 PyString_AsString(co->co_name),
2626 argcount + kwcount);
2627 goto fail;
2628 }
2629 }
2630 /* Allocate and initialize storage for cell vars, and copy free
2631 vars into frame. This isn't too efficient right now. */
2632 if (f->f_ncells) {
2633 int i = 0, j = 0, nargs, found;
2634 char *cellname, *argname;
2635 PyObject *c;
2636
2637 nargs = co->co_argcount;
2638 if (co->co_flags & CO_VARARGS)
2639 nargs++;
2640 if (co->co_flags & CO_VARKEYWORDS)
2641 nargs++;
2642
2643 /* Check for cells that shadow args */
2644 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2645 cellname = PyString_AS_STRING(
2646 PyTuple_GET_ITEM(co->co_cellvars, i));
2647 found = 0;
2648 while (j < nargs) {
2649 argname = PyString_AS_STRING(
2650 PyTuple_GET_ITEM(co->co_varnames, j));
2651 if (strcmp(cellname, argname) == 0) {
2652 c = PyCell_New(GETLOCAL(j));
2653 if (c == NULL)
2654 goto fail;
2655 GETLOCAL(f->f_nlocals + i) = c;
2656 found = 1;
2657 break;
2658 }
2659 j++;
2660 }
2661 if (found == 0) {
2662 c = PyCell_New(NULL);
2663 if (c == NULL)
2664 goto fail;
2665 SETLOCAL(f->f_nlocals + i, c);
2666 }
2667 }
2668 /* Initialize any that are left */
2669 while (i < f->f_ncells) {
2670 c = PyCell_New(NULL);
2671 if (c == NULL)
2672 goto fail;
2673 SETLOCAL(f->f_nlocals + i, c);
2674 i++;
2675 }
2676 }
2677 if (f->f_nfreevars) {
2678 int i;
2679 for (i = 0; i < f->f_nfreevars; ++i) {
2680 PyObject *o = PyTuple_GET_ITEM(closure, i);
2681 Py_INCREF(o);
2682 freevars[f->f_ncells + i] = o;
2683 }
2684 }
2685
Tim Peters5ca576e2001-06-18 22:08:13 +00002686 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002687 /* Don't need to keep the reference to f_back, it will be set
2688 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002689 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002690 f->f_back = NULL;
2691
Jeremy Hylton985eba52003-02-05 23:13:00 +00002692 PCALL(PCALL_GENERATOR);
2693
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002694 /* Create a new generator that owns the ready to run frame
2695 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002696 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002697 }
2698
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002699 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002700
2701 fail: /* Jump here from prelude on failure */
2702
Tim Petersb13680b2001-11-27 23:29:29 +00002703 /* decref'ing the frame can cause __del__ methods to get invoked,
2704 which can call back into Python. While we're done with the
2705 current Python frame (f), the associated C stack is still in use,
2706 so recursion_depth must be boosted for the duration.
2707 */
2708 assert(tstate != NULL);
2709 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002710 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002711 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002712 return retval;
2713}
2714
2715
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002716/* Implementation notes for set_exc_info() and reset_exc_info():
2717
2718- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2719 'exc_traceback'. These always travel together.
2720
2721- tstate->curexc_ZZZ is the "hot" exception that is set by
2722 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2723
2724- Once an exception is caught by an except clause, it is transferred
2725 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2726 can pick it up. This is the primary task of set_exc_info().
2727
2728- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2729
2730 Long ago, when none of this existed, there were just a few globals:
2731 one set corresponding to the "hot" exception, and one set
2732 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2733 globals; they were simply stored as sys.exc_ZZZ. For backwards
2734 compatibility, they still are!) The problem was that in code like
2735 this:
2736
2737 try:
2738 "something that may fail"
2739 except "some exception":
2740 "do something else first"
2741 "print the exception from sys.exc_ZZZ."
2742
2743 if "do something else first" invoked something that raised and caught
2744 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2745 cause of subtle bugs. I fixed this by changing the semantics as
2746 follows:
2747
2748 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2749 *in that frame*.
2750
2751 - But initially, and as long as no exception is caught in a given
2752 frame, sys.exc_ZZZ will hold the last exception caught in the
2753 previous frame (or the frame before that, etc.).
2754
2755 The first bullet fixed the bug in the above example. The second
2756 bullet was for backwards compatibility: it was (and is) common to
2757 have a function that is called when an exception is caught, and to
2758 have that function access the caught exception via sys.exc_ZZZ.
2759 (Example: traceback.print_exc()).
2760
2761 At the same time I fixed the problem that sys.exc_ZZZ weren't
2762 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2763 but that's really a separate improvement.
2764
2765 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2766 variables to what they were before the current frame was called. The
2767 set_exc_info() function saves them on the frame so that
2768 reset_exc_info() can restore them. The invariant is that
2769 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2770 exception (where "catching" an exception applies only to successful
2771 except clauses); and if the current frame ever caught an exception,
2772 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2773 at the start of the current frame.
2774
2775*/
2776
Guido van Rossuma027efa1997-05-05 20:56:21 +00002777static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002778set_exc_info(PyThreadState *tstate,
2779 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002780{
2781 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002782 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002783
Guido van Rossuma027efa1997-05-05 20:56:21 +00002784 frame = tstate->frame;
2785 if (frame->f_exc_type == NULL) {
2786 /* This frame didn't catch an exception before */
2787 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002788 if (tstate->exc_type == NULL) {
2789 Py_INCREF(Py_None);
2790 tstate->exc_type = Py_None;
2791 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002792 tmp_type = frame->f_exc_type;
2793 tmp_value = frame->f_exc_value;
2794 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002795 Py_XINCREF(tstate->exc_type);
2796 Py_XINCREF(tstate->exc_value);
2797 Py_XINCREF(tstate->exc_traceback);
2798 frame->f_exc_type = tstate->exc_type;
2799 frame->f_exc_value = tstate->exc_value;
2800 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002801 Py_XDECREF(tmp_type);
2802 Py_XDECREF(tmp_value);
2803 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002804 }
2805 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002806 tmp_type = tstate->exc_type;
2807 tmp_value = tstate->exc_value;
2808 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002809 Py_XINCREF(type);
2810 Py_XINCREF(value);
2811 Py_XINCREF(tb);
2812 tstate->exc_type = type;
2813 tstate->exc_value = value;
2814 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002815 Py_XDECREF(tmp_type);
2816 Py_XDECREF(tmp_value);
2817 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002818 /* For b/w compatibility */
2819 PySys_SetObject("exc_type", type);
2820 PySys_SetObject("exc_value", value);
2821 PySys_SetObject("exc_traceback", tb);
2822}
2823
2824static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002825reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002826{
2827 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002828 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 frame = tstate->frame;
2830 if (frame->f_exc_type != NULL) {
2831 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002832 tmp_type = tstate->exc_type;
2833 tmp_value = tstate->exc_value;
2834 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002835 Py_XINCREF(frame->f_exc_type);
2836 Py_XINCREF(frame->f_exc_value);
2837 Py_XINCREF(frame->f_exc_traceback);
2838 tstate->exc_type = frame->f_exc_type;
2839 tstate->exc_value = frame->f_exc_value;
2840 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002841 Py_XDECREF(tmp_type);
2842 Py_XDECREF(tmp_value);
2843 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002844 /* For b/w compatibility */
2845 PySys_SetObject("exc_type", frame->f_exc_type);
2846 PySys_SetObject("exc_value", frame->f_exc_value);
2847 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2848 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002849 tmp_type = frame->f_exc_type;
2850 tmp_value = frame->f_exc_value;
2851 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002852 frame->f_exc_type = NULL;
2853 frame->f_exc_value = NULL;
2854 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002855 Py_XDECREF(tmp_type);
2856 Py_XDECREF(tmp_value);
2857 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002858}
2859
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002860/* Logic for the raise statement (too complicated for inlining).
2861 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002862static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002863do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002864{
Guido van Rossumd295f121998-04-09 21:39:57 +00002865 if (type == NULL) {
2866 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002867 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002868 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2869 value = tstate->exc_value;
2870 tb = tstate->exc_traceback;
2871 Py_XINCREF(type);
2872 Py_XINCREF(value);
2873 Py_XINCREF(tb);
2874 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002876 /* We support the following forms of raise:
2877 raise <class>, <classinstance>
2878 raise <class>, <argument tuple>
2879 raise <class>, None
2880 raise <class>, <argument>
2881 raise <classinstance>, None
2882 raise <string>, <object>
2883 raise <string>, None
2884
2885 An omitted second argument is the same as None.
2886
2887 In addition, raise <tuple>, <anything> is the same as
2888 raising the tuple's first item (and it better have one!);
2889 this rule is applied recursively.
2890
2891 Finally, an optional third argument can be supplied, which
2892 gives the traceback to be substituted (useful when
2893 re-raising an exception after examining it). */
2894
2895 /* First, check the traceback argument, replacing None with
2896 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002897 if (tb == Py_None) {
2898 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002899 tb = NULL;
2900 }
2901 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002902 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002903 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002904 goto raise_error;
2905 }
2906
2907 /* Next, replace a missing value with None */
2908 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002909 value = Py_None;
2910 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002911 }
2912
2913 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002914 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2915 PyObject *tmp = type;
2916 type = PyTuple_GET_ITEM(type, 0);
2917 Py_INCREF(type);
2918 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002919 }
2920
Tim Petersafb2c802002-04-18 18:06:20 +00002921 if (PyString_CheckExact(type))
2922 /* Raising builtin string is deprecated but still allowed --
2923 * do nothing. Raising an instance of a new-style str
2924 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002925 PyErr_Warn(PyExc_PendingDeprecationWarning,
2926 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002927
2928 else if (PyClass_Check(type))
2929 PyErr_NormalizeException(&type, &value, &tb);
2930
Guido van Rossumb209a111997-04-29 18:18:01 +00002931 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002932 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002933 if (value != Py_None) {
2934 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002935 "instance exception may not have a separate value");
2936 goto raise_error;
2937 }
2938 else {
2939 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002940 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002942 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2943 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002944 }
2945 }
2946 else {
2947 /* Not something you can raise. You get an exception
2948 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002949 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002950 "exceptions must be classes, instances, or "
2951 "strings (deprecated), not %s",
2952 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002953 goto raise_error;
2954 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002955 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002956 if (tb == NULL)
2957 return WHY_EXCEPTION;
2958 else
2959 return WHY_RERAISE;
2960 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002961 Py_XDECREF(value);
2962 Py_XDECREF(type);
2963 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002964 return WHY_EXCEPTION;
2965}
2966
Tim Petersd6d010b2001-06-21 02:49:55 +00002967/* Iterate v argcnt times and store the results on the stack (via decreasing
2968 sp). Return 1 for success, 0 if error. */
2969
Barry Warsawe42b18f1997-08-25 22:13:04 +00002970static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002971unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002972{
Tim Petersd6d010b2001-06-21 02:49:55 +00002973 int i = 0;
2974 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002975 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002976
Tim Petersd6d010b2001-06-21 02:49:55 +00002977 assert(v != NULL);
2978
2979 it = PyObject_GetIter(v);
2980 if (it == NULL)
2981 goto Error;
2982
2983 for (; i < argcnt; i++) {
2984 w = PyIter_Next(it);
2985 if (w == NULL) {
2986 /* Iterator done, via error or exhaustion. */
2987 if (!PyErr_Occurred()) {
2988 PyErr_Format(PyExc_ValueError,
2989 "need more than %d value%s to unpack",
2990 i, i == 1 ? "" : "s");
2991 }
2992 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002993 }
2994 *--sp = w;
2995 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002996
2997 /* We better have exhausted the iterator now. */
2998 w = PyIter_Next(it);
2999 if (w == NULL) {
3000 if (PyErr_Occurred())
3001 goto Error;
3002 Py_DECREF(it);
3003 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003004 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003005 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003006 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003007 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003008Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003009 for (; i > 0; i--, sp++)
3010 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003011 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003012 return 0;
3013}
3014
3015
Guido van Rossum96a42c81992-01-12 02:29:51 +00003016#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003017static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003018prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003019{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003020 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 if (PyObject_Print(v, stdout, 0) != 0)
3022 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003023 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003024 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003025}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003026#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003027
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003028static void
Fred Drake5755ce62001-06-27 19:19:46 +00003029call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003030{
Guido van Rossumb209a111997-04-29 18:18:01 +00003031 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003032 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003033 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003034 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003035 value = Py_None;
3036 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003037 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003038 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003039 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003040 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003041 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003042 }
Fred Drake5755ce62001-06-27 19:19:46 +00003043 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003044 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003045 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003046 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003047 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003048 Py_XDECREF(type);
3049 Py_XDECREF(value);
3050 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003051 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003052}
3053
Fred Drake4ec5d562001-10-04 19:26:43 +00003054static void
3055call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3056 int what)
3057{
3058 PyObject *type, *value, *traceback;
3059 int err;
3060 PyErr_Fetch(&type, &value, &traceback);
3061 err = call_trace(func, obj, frame, what, NULL);
3062 if (err == 0)
3063 PyErr_Restore(type, value, traceback);
3064 else {
3065 Py_XDECREF(type);
3066 Py_XDECREF(value);
3067 Py_XDECREF(traceback);
3068 }
3069}
3070
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003071static int
Fred Drake5755ce62001-06-27 19:19:46 +00003072call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3073 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003074{
Fred Drake5755ce62001-06-27 19:19:46 +00003075 register PyThreadState *tstate = frame->f_tstate;
3076 int result;
3077 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003078 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003079 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003080 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003081 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003082 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3083 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003084 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003085 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003086}
3087
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003088PyObject *
3089_PyEval_CallTracing(PyObject *func, PyObject *args)
3090{
3091 PyFrameObject *frame = PyEval_GetFrame();
3092 PyThreadState *tstate = frame->f_tstate;
3093 int save_tracing = tstate->tracing;
3094 int save_use_tracing = tstate->use_tracing;
3095 PyObject *result;
3096
3097 tstate->tracing = 0;
3098 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3099 || (tstate->c_profilefunc != NULL));
3100 result = PyObject_Call(func, args, NULL);
3101 tstate->tracing = save_tracing;
3102 tstate->use_tracing = save_use_tracing;
3103 return result;
3104}
3105
Michael W. Hudson006c7522002-11-08 13:08:46 +00003106static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003107maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003108 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3109 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003110{
3111 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003112
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003113 In a nutshell, we use the co_lnotab field of the code object
3114 to tell when execution has moved onto a different line.
3115
3116 As mentioned above, the basic idea is so set things up so
3117 that
3118
3119 *instr_lb <= frame->f_lasti < *instr_ub
3120
3121 is true so long as execution does not change lines.
3122
3123 This is all fairly simple. Digging the information out of
3124 co_lnotab takes some work, but is conceptually clear.
3125
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003126 Somewhat harder to explain is why we don't *always* call the
3127 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003128
3129 Consider this code:
3130
3131 1: def f(a):
3132 2: if a:
3133 3: print 1
3134 4: else:
3135 5: print 2
3136
3137 which compiles to this:
3138
3139 2 0 LOAD_FAST 0 (a)
3140 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003141 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003142
3143 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003144 10 PRINT_ITEM
3145 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003146 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003147 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003148
3149 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003150 19 PRINT_ITEM
3151 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003152 >> 21 LOAD_CONST 0 (None)
3153 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003154
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003155 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003156 15 and the co_lnotab will claim that execution has moved to
3157 line 3. This is at best misleading. In this case we could
3158 associate the POP_TOP with line 4, but that doesn't make
3159 sense in all cases (I think).
3160
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003161 What we do is only call the line trace function if the co_lnotab
3162 indicates we have jumped to the *start* of a line, i.e. if the
3163 current instruction offset matches the offset given for the
3164 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003166 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003167 Execution will jump from instruction offset 12 to offset 21.
3168 Then the co_lnotab would imply that execution has moved to line
3169 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003170
3171 Why do we set f_lineno when tracing? Well, consider the code
3172 above when 'a' is true. If stepping through this with 'n' in
3173 pdb, you would stop at line 1 with a "call" type event, then
3174 line events on lines 2 and 3, then a "return" type event -- but
3175 you would be shown line 5 during this event. This is a change
3176 from the behaviour in 2.2 and before, and I've found it
3177 confusing in practice. By setting and using f_lineno when
3178 tracing, one can report a line number different from that
3179 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003180 */
3181
Michael W. Hudson006c7522002-11-08 13:08:46 +00003182 int result = 0;
3183
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003184 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003185 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003186 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003187 unsigned char* p;
3188
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003189 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3190 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003191
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003192 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003193 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003194
3195 /* possible optimization: if f->f_lasti == instr_ub
3196 (likely to be a common case) then we already know
3197 instr_lb -- if we stored the matching value of p
3198 somwhere we could skip the first while loop. */
3199
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003200 /* see comments in compile.c for the description of
3201 co_lnotab. A point to remember: increments to p
3202 should come in pairs -- although we don't care about
3203 the line increments here, treating them as byte
3204 increments gets confusing, to say the least. */
3205
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003206 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003207 if (addr + *p > frame->f_lasti)
3208 break;
3209 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003210 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003211 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003212 --size;
3213 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003214
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003215 if (addr == frame->f_lasti) {
3216 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003217 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003218 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003219 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003220
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003221 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003222 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003223 addr += *p++;
3224 if (*p++)
3225 break;
3226 }
3227 *instr_ub = addr;
3228 }
3229 else {
3230 *instr_ub = INT_MAX;
3231 }
3232 }
Armin Rigobf57a142004-03-22 19:24:58 +00003233 else if (frame->f_lasti <= *instr_prev) {
3234 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003235 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003236 PyTrace_LINE, Py_None);
3237 }
3238 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003239 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003240}
3241
Fred Drake5755ce62001-06-27 19:19:46 +00003242void
3243PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003244{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003245 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003246 PyObject *temp = tstate->c_profileobj;
3247 Py_XINCREF(arg);
3248 tstate->c_profilefunc = NULL;
3249 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003250 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003251 Py_XDECREF(temp);
3252 tstate->c_profilefunc = func;
3253 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003254 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003255}
3256
3257void
3258PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3259{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003260 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003261 PyObject *temp = tstate->c_traceobj;
3262 Py_XINCREF(arg);
3263 tstate->c_tracefunc = NULL;
3264 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003265 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003266 Py_XDECREF(temp);
3267 tstate->c_tracefunc = func;
3268 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003269 tstate->use_tracing = ((func != NULL)
3270 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003271}
3272
Guido van Rossumb209a111997-04-29 18:18:01 +00003273PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003274PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003275{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003276 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003277 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003278 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003279 else
3280 return current_frame->f_builtins;
3281}
3282
Guido van Rossumb209a111997-04-29 18:18:01 +00003283PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003284PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003285{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003286 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003287 if (current_frame == NULL)
3288 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003289 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003290 return current_frame->f_locals;
3291}
3292
Guido van Rossumb209a111997-04-29 18:18:01 +00003293PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003294PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003295{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003296 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003297 if (current_frame == NULL)
3298 return NULL;
3299 else
3300 return current_frame->f_globals;
3301}
3302
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003303PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003304PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003305{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003306 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003307 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003308}
3309
Guido van Rossum6135a871995-01-09 17:53:26 +00003310int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003311PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003312{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003313 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003314 return current_frame == NULL ? 0 : current_frame->f_restricted;
3315}
3316
Guido van Rossumbe270261997-05-22 22:26:18 +00003317int
Tim Peters5ba58662001-07-16 02:29:45 +00003318PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003319{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003320 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003321 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003322
3323 if (current_frame != NULL) {
3324 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003325 const int compilerflags = codeflags & PyCF_MASK;
3326 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003327 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003328 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003329 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003330#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003331 if (codeflags & CO_GENERATOR_ALLOWED) {
3332 result = 1;
3333 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3334 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003335#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003336 }
3337 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003338}
3339
3340int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003341Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003342{
Guido van Rossumb209a111997-04-29 18:18:01 +00003343 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003344 if (f == NULL)
3345 return 0;
3346 if (!PyFile_SoftSpace(f, 0))
3347 return 0;
3348 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003349}
3350
Guido van Rossum3f5da241990-12-20 15:06:42 +00003351
Guido van Rossum681d79a1995-07-18 14:51:37 +00003352/* External interface to call any callable object.
3353 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003354
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003355#undef PyEval_CallObject
3356/* for backward compatibility: export this interface */
3357
Guido van Rossumb209a111997-04-29 18:18:01 +00003358PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003359PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003360{
Guido van Rossumb209a111997-04-29 18:18:01 +00003361 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003362}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003363#define PyEval_CallObject(func,arg) \
3364 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003365
Guido van Rossumb209a111997-04-29 18:18:01 +00003366PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003367PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003368{
Jeremy Hylton52820442001-01-03 23:52:36 +00003369 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003370
3371 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003372 arg = PyTuple_New(0);
3373 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003374 PyErr_SetString(PyExc_TypeError,
3375 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003376 return NULL;
3377 }
3378 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003379 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003380
Guido van Rossumb209a111997-04-29 18:18:01 +00003381 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003382 PyErr_SetString(PyExc_TypeError,
3383 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003384 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003385 return NULL;
3386 }
3387
Tim Peters6d6c1a32001-08-02 04:15:00 +00003388 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003389 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003390 return result;
3391}
3392
Tim Peters6d6c1a32001-08-02 04:15:00 +00003393char *
3394PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003395{
3396 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003397 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003398 else if (PyFunction_Check(func))
3399 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3400 else if (PyCFunction_Check(func))
3401 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3402 else if (PyClass_Check(func))
3403 return PyString_AsString(((PyClassObject*)func)->cl_name);
3404 else if (PyInstance_Check(func)) {
3405 return PyString_AsString(
3406 ((PyInstanceObject*)func)->in_class->cl_name);
3407 } else {
3408 return func->ob_type->tp_name;
3409 }
3410}
3411
Tim Peters6d6c1a32001-08-02 04:15:00 +00003412char *
3413PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003414{
3415 if (PyMethod_Check(func))
3416 return "()";
3417 else if (PyFunction_Check(func))
3418 return "()";
3419 else if (PyCFunction_Check(func))
3420 return "()";
3421 else if (PyClass_Check(func))
3422 return " constructor";
3423 else if (PyInstance_Check(func)) {
3424 return " instance";
3425 } else {
3426 return " object";
3427 }
3428}
3429
Jeremy Hylton52820442001-01-03 23:52:36 +00003430#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3431
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003432static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003433err_args(PyObject *func, int flags, int nargs)
3434{
3435 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003436 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003437 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003438 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003439 nargs);
3440 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003441 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003442 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003443 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003444 nargs);
3445}
3446
Nicholas Bastind858a772004-06-25 23:31:06 +00003447#define C_TRACE(call) \
3448if (tstate->use_tracing && tstate->c_profilefunc) { \
3449 if (call_trace(tstate->c_profilefunc, \
3450 tstate->c_profileobj, \
3451 tstate->frame, PyTrace_C_CALL, \
3452 func)) \
3453 { return NULL; } \
3454 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003455 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003456 if (x == NULL) { \
3457 if (call_trace (tstate->c_profilefunc, \
3458 tstate->c_profileobj, \
3459 tstate->frame, PyTrace_C_EXCEPTION, \
3460 func)) \
3461 { return NULL; } \
3462 } else { \
3463 if (call_trace(tstate->c_profilefunc, \
3464 tstate->c_profileobj, \
3465 tstate->frame, PyTrace_C_RETURN, \
3466 func)) \
3467 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003468 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003469 } \
3470} else { \
3471 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003472 }
3473
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003474static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003475call_function(PyObject ***pp_stack, int oparg
3476#ifdef WITH_TSC
3477 , uint64* pintr0, uint64* pintr1
3478#endif
3479 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003480{
3481 int na = oparg & 0xff;
3482 int nk = (oparg>>8) & 0xff;
3483 int n = na + 2 * nk;
3484 PyObject **pfunc = (*pp_stack) - n - 1;
3485 PyObject *func = *pfunc;
3486 PyObject *x, *w;
3487
Jeremy Hylton985eba52003-02-05 23:13:00 +00003488 /* Always dispatch PyCFunction first, because these are
3489 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003490 */
3491 if (PyCFunction_Check(func) && nk == 0) {
3492 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003493 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003494
3495 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003496 if (flags & (METH_NOARGS | METH_O)) {
3497 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3498 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003499 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003500 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003501 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003502 else if (flags & METH_O && na == 1) {
3503 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003504 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003505 Py_DECREF(arg);
3506 }
3507 else {
3508 err_args(func, flags, na);
3509 x = NULL;
3510 }
3511 }
3512 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003513 PyObject *callargs;
3514 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003515#ifdef WITH_TSC
3516 rdtscll(*pintr0);
3517#endif
Nicholas Bastind858a772004-06-25 23:31:06 +00003518 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003519#ifdef WITH_TSC
3520 rdtscll(*pintr1);
3521#endif
Tim Peters8a5c3c72004-04-05 19:36:21 +00003522 Py_XDECREF(callargs);
3523 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003524 } else {
3525 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3526 /* optimize access to bound methods */
3527 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003528 PCALL(PCALL_METHOD);
3529 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003530 Py_INCREF(self);
3531 func = PyMethod_GET_FUNCTION(func);
3532 Py_INCREF(func);
3533 Py_DECREF(*pfunc);
3534 *pfunc = self;
3535 na++;
3536 n++;
3537 } else
3538 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003539#ifdef WITH_TSC
3540 rdtscll(*pintr0);
3541#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003542 if (PyFunction_Check(func))
3543 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003544 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003545 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003546#ifdef WITH_TSC
3547 rdtscll(*pintr1);
3548#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003549 Py_DECREF(func);
3550 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003551
Jeremy Hylton985eba52003-02-05 23:13:00 +00003552 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003553 while ((*pp_stack) > pfunc) {
3554 w = EXT_POP(*pp_stack);
3555 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003556 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003557 }
3558 return x;
3559}
3560
Jeremy Hylton192690e2002-08-16 18:36:11 +00003561/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003562 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003563 For the simplest case -- a function that takes only positional
3564 arguments and is called with only positional arguments -- it
3565 inlines the most primitive frame setup code from
3566 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3567 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003568*/
3569
3570static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003571fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003572{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003573 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003574 PyObject *globals = PyFunction_GET_GLOBALS(func);
3575 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3576 PyObject **d = NULL;
3577 int nd = 0;
3578
Jeremy Hylton985eba52003-02-05 23:13:00 +00003579 PCALL(PCALL_FUNCTION);
3580 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003581 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003582 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3583 PyFrameObject *f;
3584 PyObject *retval = NULL;
3585 PyThreadState *tstate = PyThreadState_GET();
3586 PyObject **fastlocals, **stack;
3587 int i;
3588
3589 PCALL(PCALL_FASTER_FUNCTION);
3590 assert(globals != NULL);
3591 /* XXX Perhaps we should create a specialized
3592 PyFrame_New() that doesn't take locals, but does
3593 take builtins without sanity checking them.
3594 */
3595 f = PyFrame_New(tstate, co, globals, NULL);
3596 if (f == NULL)
3597 return NULL;
3598
3599 fastlocals = f->f_localsplus;
3600 stack = (*pp_stack) - n;
3601
3602 for (i = 0; i < n; i++) {
3603 Py_INCREF(*stack);
3604 fastlocals[i] = *stack++;
3605 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003606 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003607 assert(tstate != NULL);
3608 ++tstate->recursion_depth;
3609 Py_DECREF(f);
3610 --tstate->recursion_depth;
3611 return retval;
3612 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003613 if (argdefs != NULL) {
3614 d = &PyTuple_GET_ITEM(argdefs, 0);
3615 nd = ((PyTupleObject *)argdefs)->ob_size;
3616 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003617 return PyEval_EvalCodeEx(co, globals,
3618 (PyObject *)NULL, (*pp_stack)-n, na,
3619 (*pp_stack)-2*nk, nk, d, nd,
3620 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003621}
3622
3623static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003624update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3625 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003626{
3627 PyObject *kwdict = NULL;
3628 if (orig_kwdict == NULL)
3629 kwdict = PyDict_New();
3630 else {
3631 kwdict = PyDict_Copy(orig_kwdict);
3632 Py_DECREF(orig_kwdict);
3633 }
3634 if (kwdict == NULL)
3635 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003636 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003637 int err;
3638 PyObject *value = EXT_POP(*pp_stack);
3639 PyObject *key = EXT_POP(*pp_stack);
3640 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003641 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003642 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003643 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003644 PyEval_GetFuncName(func),
3645 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003646 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003647 Py_DECREF(key);
3648 Py_DECREF(value);
3649 Py_DECREF(kwdict);
3650 return NULL;
3651 }
3652 err = PyDict_SetItem(kwdict, key, value);
3653 Py_DECREF(key);
3654 Py_DECREF(value);
3655 if (err) {
3656 Py_DECREF(kwdict);
3657 return NULL;
3658 }
3659 }
3660 return kwdict;
3661}
3662
3663static PyObject *
3664update_star_args(int nstack, int nstar, PyObject *stararg,
3665 PyObject ***pp_stack)
3666{
3667 PyObject *callargs, *w;
3668
3669 callargs = PyTuple_New(nstack + nstar);
3670 if (callargs == NULL) {
3671 return NULL;
3672 }
3673 if (nstar) {
3674 int i;
3675 for (i = 0; i < nstar; i++) {
3676 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3677 Py_INCREF(a);
3678 PyTuple_SET_ITEM(callargs, nstack + i, a);
3679 }
3680 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003681 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003682 w = EXT_POP(*pp_stack);
3683 PyTuple_SET_ITEM(callargs, nstack, w);
3684 }
3685 return callargs;
3686}
3687
3688static PyObject *
3689load_args(PyObject ***pp_stack, int na)
3690{
3691 PyObject *args = PyTuple_New(na);
3692 PyObject *w;
3693
3694 if (args == NULL)
3695 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003696 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003697 w = EXT_POP(*pp_stack);
3698 PyTuple_SET_ITEM(args, na, w);
3699 }
3700 return args;
3701}
3702
3703static PyObject *
3704do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3705{
3706 PyObject *callargs = NULL;
3707 PyObject *kwdict = NULL;
3708 PyObject *result = NULL;
3709
3710 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003711 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003712 if (kwdict == NULL)
3713 goto call_fail;
3714 }
3715 callargs = load_args(pp_stack, na);
3716 if (callargs == NULL)
3717 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003718#ifdef CALL_PROFILE
3719 /* At this point, we have to look at the type of func to
3720 update the call stats properly. Do it here so as to avoid
3721 exposing the call stats machinery outside ceval.c
3722 */
3723 if (PyFunction_Check(func))
3724 PCALL(PCALL_FUNCTION);
3725 else if (PyMethod_Check(func))
3726 PCALL(PCALL_METHOD);
3727 else if (PyType_Check(func))
3728 PCALL(PCALL_TYPE);
3729 else
3730 PCALL(PCALL_OTHER);
3731#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003732 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003733 call_fail:
3734 Py_XDECREF(callargs);
3735 Py_XDECREF(kwdict);
3736 return result;
3737}
3738
3739static PyObject *
3740ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3741{
3742 int nstar = 0;
3743 PyObject *callargs = NULL;
3744 PyObject *stararg = NULL;
3745 PyObject *kwdict = NULL;
3746 PyObject *result = NULL;
3747
3748 if (flags & CALL_FLAG_KW) {
3749 kwdict = EXT_POP(*pp_stack);
3750 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003751 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003752 "%s%s argument after ** "
3753 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003754 PyEval_GetFuncName(func),
3755 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003756 goto ext_call_fail;
3757 }
3758 }
3759 if (flags & CALL_FLAG_VAR) {
3760 stararg = EXT_POP(*pp_stack);
3761 if (!PyTuple_Check(stararg)) {
3762 PyObject *t = NULL;
3763 t = PySequence_Tuple(stararg);
3764 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003765 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3766 PyErr_Format(PyExc_TypeError,
3767 "%s%s argument after * "
3768 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003769 PyEval_GetFuncName(func),
3770 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003771 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003772 goto ext_call_fail;
3773 }
3774 Py_DECREF(stararg);
3775 stararg = t;
3776 }
3777 nstar = PyTuple_GET_SIZE(stararg);
3778 }
3779 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003780 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003781 if (kwdict == NULL)
3782 goto ext_call_fail;
3783 }
3784 callargs = update_star_args(na, nstar, stararg, pp_stack);
3785 if (callargs == NULL)
3786 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003787#ifdef CALL_PROFILE
3788 /* At this point, we have to look at the type of func to
3789 update the call stats properly. Do it here so as to avoid
3790 exposing the call stats machinery outside ceval.c
3791 */
3792 if (PyFunction_Check(func))
3793 PCALL(PCALL_FUNCTION);
3794 else if (PyMethod_Check(func))
3795 PCALL(PCALL_METHOD);
3796 else if (PyType_Check(func))
3797 PCALL(PCALL_TYPE);
3798 else
3799 PCALL(PCALL_OTHER);
3800#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003801 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003802 ext_call_fail:
3803 Py_XDECREF(callargs);
3804 Py_XDECREF(kwdict);
3805 Py_XDECREF(stararg);
3806 return result;
3807}
3808
Tim Peterscb479e72001-12-16 19:11:44 +00003809/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3810 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3811 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3812*/
Tim Petersb5196382001-12-16 19:44:20 +00003813/* Note: If v is NULL, return success without storing into *pi. This
3814 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3815 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003816*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003817int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003818_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003819{
Tim Petersb5196382001-12-16 19:44:20 +00003820 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003821 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003822 if (PyInt_Check(v)) {
3823 x = PyInt_AsLong(v);
3824 } else if (PyLong_Check(v)) {
3825 x = PyLong_AsLong(v);
3826 if (x==-1 && PyErr_Occurred()) {
3827 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003828 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003829
Guido van Rossumac7be682001-01-17 15:42:30 +00003830 if (!PyErr_ExceptionMatches(
3831 PyExc_OverflowError)) {
3832 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003833 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003834 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003835 }
3836
Guido van Rossumac7be682001-01-17 15:42:30 +00003837 /* Clear the OverflowError */
3838 PyErr_Clear();
3839
3840 /* It's an overflow error, so we need to
3841 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003842 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003843 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003844
3845 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003846 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003847 if (long_zero == NULL)
3848 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003849
3850 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003851 cmp = PyObject_RichCompareBool(v, long_zero,
3852 Py_GT);
3853 Py_DECREF(long_zero);
3854 if (cmp < 0)
3855 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003856 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003857 x = INT_MAX;
3858 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003859 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003860 }
3861 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003862 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003863 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003864 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003865 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003866 /* Truncate -- very long indices are truncated anyway */
3867 if (x > INT_MAX)
3868 x = INT_MAX;
3869 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003870 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003871 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003873 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003874}
3875
Guido van Rossum50d756e2001-08-18 17:43:36 +00003876#undef ISINT
3877#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3878
Guido van Rossumb209a111997-04-29 18:18:01 +00003879static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003880apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003882 PyTypeObject *tp = u->ob_type;
3883 PySequenceMethods *sq = tp->tp_as_sequence;
3884
3885 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3886 int ilow = 0, ihigh = INT_MAX;
3887 if (!_PyEval_SliceIndex(v, &ilow))
3888 return NULL;
3889 if (!_PyEval_SliceIndex(w, &ihigh))
3890 return NULL;
3891 return PySequence_GetSlice(u, ilow, ihigh);
3892 }
3893 else {
3894 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003895 if (slice != NULL) {
3896 PyObject *res = PyObject_GetItem(u, slice);
3897 Py_DECREF(slice);
3898 return res;
3899 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003900 else
3901 return NULL;
3902 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003903}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003904
3905static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003906assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3907 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003909 PyTypeObject *tp = u->ob_type;
3910 PySequenceMethods *sq = tp->tp_as_sequence;
3911
3912 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3913 int ilow = 0, ihigh = INT_MAX;
3914 if (!_PyEval_SliceIndex(v, &ilow))
3915 return -1;
3916 if (!_PyEval_SliceIndex(w, &ihigh))
3917 return -1;
3918 if (x == NULL)
3919 return PySequence_DelSlice(u, ilow, ihigh);
3920 else
3921 return PySequence_SetSlice(u, ilow, ihigh, x);
3922 }
3923 else {
3924 PyObject *slice = PySlice_New(v, w, NULL);
3925 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003926 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003927 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003928 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003929 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003930 res = PyObject_DelItem(u, slice);
3931 Py_DECREF(slice);
3932 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003933 }
3934 else
3935 return -1;
3936 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003937}
3938
Guido van Rossumb209a111997-04-29 18:18:01 +00003939static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003940cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003941{
Guido van Rossumac7be682001-01-17 15:42:30 +00003942 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003943 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003944 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003945 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003946 break;
3947 case PyCmp_IS_NOT:
3948 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003949 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003950 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003951 res = PySequence_Contains(w, v);
3952 if (res < 0)
3953 return NULL;
3954 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003955 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003956 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003957 if (res < 0)
3958 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003959 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003960 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003961 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003962 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003963 break;
3964 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003965 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003966 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003967 v = res ? Py_True : Py_False;
3968 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003969 return v;
3970}
3971
Thomas Wouters52152252000-08-17 22:55:00 +00003972static PyObject *
3973import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003974{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003975 PyObject *x;
3976
3977 x = PyObject_GetAttr(v, name);
3978 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003979 PyErr_Format(PyExc_ImportError,
3980 "cannot import name %.230s",
3981 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003982 }
Thomas Wouters52152252000-08-17 22:55:00 +00003983 return x;
3984}
Guido van Rossumac7be682001-01-17 15:42:30 +00003985
Thomas Wouters52152252000-08-17 22:55:00 +00003986static int
3987import_all_from(PyObject *locals, PyObject *v)
3988{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003989 PyObject *all = PyObject_GetAttrString(v, "__all__");
3990 PyObject *dict, *name, *value;
3991 int skip_leading_underscores = 0;
3992 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003993
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003994 if (all == NULL) {
3995 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3996 return -1; /* Unexpected error */
3997 PyErr_Clear();
3998 dict = PyObject_GetAttrString(v, "__dict__");
3999 if (dict == NULL) {
4000 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4001 return -1;
4002 PyErr_SetString(PyExc_ImportError,
4003 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004004 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004005 }
4006 all = PyMapping_Keys(dict);
4007 Py_DECREF(dict);
4008 if (all == NULL)
4009 return -1;
4010 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004011 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004012
4013 for (pos = 0, err = 0; ; pos++) {
4014 name = PySequence_GetItem(all, pos);
4015 if (name == NULL) {
4016 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4017 err = -1;
4018 else
4019 PyErr_Clear();
4020 break;
4021 }
4022 if (skip_leading_underscores &&
4023 PyString_Check(name) &&
4024 PyString_AS_STRING(name)[0] == '_')
4025 {
4026 Py_DECREF(name);
4027 continue;
4028 }
4029 value = PyObject_GetAttr(v, name);
4030 if (value == NULL)
4031 err = -1;
4032 else
4033 err = PyDict_SetItem(locals, name, value);
4034 Py_DECREF(name);
4035 Py_XDECREF(value);
4036 if (err != 0)
4037 break;
4038 }
4039 Py_DECREF(all);
4040 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004041}
4042
Guido van Rossumb209a111997-04-29 18:18:01 +00004043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004044build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004045{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004046 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004047
4048 if (PyDict_Check(methods))
4049 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004050 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004051 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004052 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4053 base = PyTuple_GET_ITEM(bases, 0);
4054 metaclass = PyObject_GetAttrString(base, "__class__");
4055 if (metaclass == NULL) {
4056 PyErr_Clear();
4057 metaclass = (PyObject *)base->ob_type;
4058 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004059 }
4060 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004061 else {
4062 PyObject *g = PyEval_GetGlobals();
4063 if (g != NULL && PyDict_Check(g))
4064 metaclass = PyDict_GetItemString(g, "__metaclass__");
4065 if (metaclass == NULL)
4066 metaclass = (PyObject *) &PyClass_Type;
4067 Py_INCREF(metaclass);
4068 }
4069 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4070 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004071 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4072 /* A type error here likely means that the user passed
4073 in a base that was not a class (such the random module
4074 instead of the random.random type). Help them out with
4075 a more informative error message */
4076 PyErr_SetString(PyExc_TypeError,
4077 "Error when calling the metaclass.\n" \
4078 "Make sure the base arguments are valid.");
4079 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004080 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004081}
4082
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004083static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004084exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4085 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004086{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004087 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004088 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004089 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004090
Guido van Rossumb209a111997-04-29 18:18:01 +00004091 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4092 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004093 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004094 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004095 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004096 locals = PyTuple_GetItem(prog, 2);
4097 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004098 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004099 if (globals == Py_None) {
4100 globals = PyEval_GetGlobals();
4101 if (locals == Py_None) {
4102 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004103 plain = 1;
4104 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004105 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004106 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004107 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004108 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004109 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004110 !PyCode_Check(prog) &&
4111 !PyFile_Check(prog)) {
4112 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004113 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004114 return -1;
4115 }
Fred Drake661ea262000-10-24 19:57:45 +00004116 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004117 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004118 "exec: arg 2 must be a dictionary or None");
4119 return -1;
4120 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004121 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004122 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004123 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004124 return -1;
4125 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004126 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004127 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004128 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004129 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4130 PyErr_SetString(PyExc_TypeError,
4131 "code object passed to exec may not contain free variables");
4132 return -1;
4133 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004134 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004135 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004136 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004137 FILE *fp = PyFile_AsFile(prog);
4138 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004139 PyCompilerFlags cf;
4140 cf.cf_flags = 0;
4141 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004142 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004143 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004144 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004145 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004146 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004147 }
4148 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004149 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004150 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004151 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004152 cf.cf_flags = 0;
4153#ifdef Py_USING_UNICODE
4154 if (PyUnicode_Check(prog)) {
4155 tmp = PyUnicode_AsUTF8String(prog);
4156 if (tmp == NULL)
4157 return -1;
4158 prog = tmp;
4159 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4160 }
4161#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004162 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004163 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004164 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004165 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004166 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004167 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004168 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004169 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004171 if (plain)
4172 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004173 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004174 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004175 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004176 return 0;
4177}
Guido van Rossum24c13741995-02-14 09:42:43 +00004178
Guido van Rossumac7be682001-01-17 15:42:30 +00004179static void
Paul Prescode68140d2000-08-30 20:25:01 +00004180format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4181{
4182 char *obj_str;
4183
4184 if (!obj)
4185 return;
4186
4187 obj_str = PyString_AsString(obj);
4188 if (!obj_str)
4189 return;
4190
4191 PyErr_Format(exc, format_str, obj_str);
4192}
Guido van Rossum950361c1997-01-24 13:49:28 +00004193
4194#ifdef DYNAMIC_EXECUTION_PROFILE
4195
Skip Montanarof118cb12001-10-15 20:51:38 +00004196static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004197getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004198{
4199 int i;
4200 PyObject *l = PyList_New(256);
4201 if (l == NULL) return NULL;
4202 for (i = 0; i < 256; i++) {
4203 PyObject *x = PyInt_FromLong(a[i]);
4204 if (x == NULL) {
4205 Py_DECREF(l);
4206 return NULL;
4207 }
4208 PyList_SetItem(l, i, x);
4209 }
4210 for (i = 0; i < 256; i++)
4211 a[i] = 0;
4212 return l;
4213}
4214
4215PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004216_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004217{
4218#ifndef DXPAIRS
4219 return getarray(dxp);
4220#else
4221 int i;
4222 PyObject *l = PyList_New(257);
4223 if (l == NULL) return NULL;
4224 for (i = 0; i < 257; i++) {
4225 PyObject *x = getarray(dxpairs[i]);
4226 if (x == NULL) {
4227 Py_DECREF(l);
4228 return NULL;
4229 }
4230 PyList_SetItem(l, i, x);
4231 }
4232 return l;
4233#endif
4234}
4235
4236#endif