blob: 1a3561016c3732e15c40826d93f7ec6b2ff2d302 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "code.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öwis87fa7852004-08-29 15:51:52 +000019#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000020
21#define READ_TIMESTAMP(var)
22
23#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000024
25typedef unsigned long long uint64;
26
Michael W. Hudson800ba232004-08-12 18:19:17 +000027#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
28 section should work for GCC on any PowerPC platform,
29 irrespective of OS. POWER? Who knows :-) */
30
Michael W. Hudson75eabd22005-01-18 15:56:11 +000031#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000032
33static void
34ppc_getcounter(uint64 *v)
35{
36 register unsigned long tbu, tb, tbu2;
37
38 loop:
39 asm volatile ("mftbu %0" : "=r" (tbu) );
40 asm volatile ("mftb %0" : "=r" (tb) );
41 asm volatile ("mftbu %0" : "=r" (tbu2));
42 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
43
44 /* The slightly peculiar way of writing the next lines is
45 compiled better by GCC than any other way I tried. */
46 ((long*)(v))[0] = tbu;
47 ((long*)(v))[1] = tb;
48}
49
Michael W. Hudson75eabd22005-01-18 15:56:11 +000050#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000051
Michael W. Hudson75eabd22005-01-18 15:56:11 +000052#define READ_TIMESTAMP(val) \
53 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
55#endif
56
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000057void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
58 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
59{
60 uint64 intr, inst, loop;
61 PyThreadState *tstate = PyThreadState_Get();
62 if (!tstate->interp->tscdump)
63 return;
64 intr = intr1 - intr0;
65 inst = inst1 - inst0 - intr;
66 loop = loop1 - loop0 - intr;
67 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
68 opcode, ticked, inst, loop);
69}
Michael W. Hudson800ba232004-08-12 18:19:17 +000070
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000071#endif
72
Guido van Rossum04691fc1992-08-12 15:35:34 +000073/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000074/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000075
Guido van Rossum408027e1996-12-30 16:17:54 +000076#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000077/* For debugging the interpreter: */
78#define LLTRACE 1 /* Low-level trace feature */
79#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000080#endif
81
Jeremy Hylton52820442001-01-03 23:52:36 +000082typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000083
Guido van Rossum374a9221991-04-04 10:40:29 +000084/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085#ifdef WITH_TSC
86static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
87#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000088static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000090static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000091static PyObject *do_call(PyObject *, PyObject ***, int, int);
92static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000093static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000094static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000095static PyObject *load_args(PyObject ***, int);
96#define CALL_FLAG_VAR 1
97#define CALL_FLAG_KW 2
98
Guido van Rossum0a066c01992-03-27 17:29:15 +000099#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000100static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000101static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000102#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000103static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
104 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000105static void call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000106 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000107static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000108static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000109 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000110
Tim Petersdbd9ba62000-07-09 03:09:57 +0000111static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
112static int assign_slice(PyObject *, PyObject *,
113 PyObject *, PyObject *);
114static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000115static PyObject *import_from(PyObject *, PyObject *);
116static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000117static PyObject *build_class(PyObject *, PyObject *, PyObject *);
118static int exec_statement(PyFrameObject *,
119 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000120static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
121static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000122static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000123static PyObject *string_concatenate(PyObject *, PyObject *,
124 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000125
Paul Prescode68140d2000-08-30 20:25:01 +0000126#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000127 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000128#define GLOBAL_NAME_ERROR_MSG \
129 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000130#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000131 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000132#define UNBOUNDFREE_ERROR_MSG \
133 "free variable '%.200s' referenced before assignment" \
134 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000135
Guido van Rossum950361c1997-01-24 13:49:28 +0000136/* Dynamic execution profile */
137#ifdef DYNAMIC_EXECUTION_PROFILE
138#ifdef DXPAIRS
139static long dxpairs[257][256];
140#define dxp dxpairs[256]
141#else
142static long dxp[256];
143#endif
144#endif
145
Jeremy Hylton985eba52003-02-05 23:13:00 +0000146/* Function call profile */
147#ifdef CALL_PROFILE
148#define PCALL_NUM 11
149static int pcall[PCALL_NUM];
150
151#define PCALL_ALL 0
152#define PCALL_FUNCTION 1
153#define PCALL_FAST_FUNCTION 2
154#define PCALL_FASTER_FUNCTION 3
155#define PCALL_METHOD 4
156#define PCALL_BOUND_METHOD 5
157#define PCALL_CFUNCTION 6
158#define PCALL_TYPE 7
159#define PCALL_GENERATOR 8
160#define PCALL_OTHER 9
161#define PCALL_POP 10
162
163/* Notes about the statistics
164
165 PCALL_FAST stats
166
167 FAST_FUNCTION means no argument tuple needs to be created.
168 FASTER_FUNCTION means that the fast-path frame setup code is used.
169
170 If there is a method call where the call can be optimized by changing
171 the argument tuple and calling the function directly, it gets recorded
172 twice.
173
174 As a result, the relationship among the statistics appears to be
175 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
176 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
177 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
178 PCALL_METHOD > PCALL_BOUND_METHOD
179*/
180
181#define PCALL(POS) pcall[POS]++
182
183PyObject *
184PyEval_GetCallStats(PyObject *self)
185{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000186 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187 pcall[0], pcall[1], pcall[2], pcall[3],
188 pcall[4], pcall[5], pcall[6], pcall[7],
189 pcall[8], pcall[9]);
190}
191#else
192#define PCALL(O)
193
194PyObject *
195PyEval_GetCallStats(PyObject *self)
196{
197 Py_INCREF(Py_None);
198 return Py_None;
199}
200#endif
201
Tim Peters5ca576e2001-06-18 22:08:13 +0000202
Guido van Rossume59214e1994-08-30 08:01:59 +0000203#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000204
Guido van Rossum2571cc81999-04-07 16:07:23 +0000205#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000206#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000207#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000208#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000209
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000210static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000211static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000212
Tim Peters7f468f22004-10-11 02:40:51 +0000213int
214PyEval_ThreadsInitialized(void)
215{
216 return interpreter_lock != 0;
217}
218
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000223 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 interpreter_lock = PyThread_allocate_lock();
225 PyThread_acquire_lock(interpreter_lock, 1);
226 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000228
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000229void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233}
234
235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239}
240
241void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000242PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000243{
244 if (tstate == NULL)
245 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000246 /* Check someone has called PyEval_InitThreads() to create the lock */
247 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000248 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000249 if (PyThreadState_Swap(tstate) != NULL)
250 Py_FatalError(
251 "PyEval_AcquireThread: non-NULL old thread state");
252}
253
254void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256{
257 if (tstate == NULL)
258 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
259 if (PyThreadState_Swap(NULL) != tstate)
260 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000262}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000263
264/* This function is called from PyOS_AfterFork to ensure that newly
265 created child processes don't hold locks referring to threads which
266 are not running in the child process. (This could also be done using
267 pthread_atfork mechanism, at least for the pthreads implementation.) */
268
269void
270PyEval_ReInitThreads(void)
271{
272 if (!interpreter_lock)
273 return;
274 /*XXX Can't use PyThread_free_lock here because it does too
275 much error-checking. Doing this cleanly would require
276 adding a new function to each thread_*.h. Instead, just
277 create a new lock and waste a little bit of memory */
278 interpreter_lock = PyThread_allocate_lock();
279 PyThread_acquire_lock(interpreter_lock, 1);
280 main_thread = PyThread_get_thread_ident();
281}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000282#endif
283
Guido van Rossumff4949e1992-08-05 19:58:53 +0000284/* Functions save_thread and restore_thread are always defined so
285 dynamically loaded modules needn't be compiled separately for use
286 with and without threads: */
287
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000288PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000291 PyThreadState *tstate = PyThreadState_Swap(NULL);
292 if (tstate == NULL)
293 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000294#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000295 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000296 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000298 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299}
300
301void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000304 if (tstate == NULL)
305 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000306#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000308 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000309 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311 }
312#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000313 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000314}
315
316
Guido van Rossuma9672091994-09-14 13:31:22 +0000317/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
318 signal handlers or Mac I/O completion routines) can schedule calls
319 to a function to be called synchronously.
320 The synchronous function is called with one void* argument.
321 It should return 0 for success or -1 for failure -- failure should
322 be accompanied by an exception.
323
324 If registry succeeds, the registry function returns 0; if it fails
325 (e.g. due to too many pending calls) it returns -1 (without setting
326 an exception condition).
327
328 Note that because registry may occur from within signal handlers,
329 or other asynchronous events, calling malloc() is unsafe!
330
331#ifdef WITH_THREAD
332 Any thread can schedule pending calls, but only the main thread
333 will execute them.
334#endif
335
336 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
337 There are two possible race conditions:
338 (1) nested asynchronous registry calls;
339 (2) registry calls made while pending calls are being processed.
340 While (1) is very unlikely, (2) is a real possibility.
341 The current code is safe against (2), but not against (1).
342 The safety against (2) is derived from the fact that only one
343 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000344
Guido van Rossuma027efa1997-05-05 20:56:21 +0000345 XXX Darn! With the advent of thread state, we should have an array
346 of pending calls per thread in the thread state! Later...
347*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000348
Guido van Rossuma9672091994-09-14 13:31:22 +0000349#define NPENDINGCALLS 32
350static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000351 int (*func)(void *);
352 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000353} pendingcalls[NPENDINGCALLS];
354static volatile int pendingfirst = 0;
355static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000356static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000357
358int
Thomas Wouters334fb892000-07-25 12:56:38 +0000359Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000360{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000361 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000362 int i, j;
363 /* XXX Begin critical section */
364 /* XXX If you want this to be safe against nested
365 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000366 if (busy)
367 return -1;
368 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000369 i = pendinglast;
370 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000371 if (j == pendingfirst) {
372 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000373 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000374 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 pendingcalls[i].func = func;
376 pendingcalls[i].arg = arg;
377 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000378
379 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000380 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000381 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000382 /* XXX End critical section */
383 return 0;
384}
385
Guido van Rossum180d7b41994-09-29 09:45:57 +0000386int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000388{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000389 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000390#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000391 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000392 return 0;
393#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000394 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000395 return 0;
396 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000397 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000398 for (;;) {
399 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000400 int (*func)(void *);
401 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000402 i = pendingfirst;
403 if (i == pendinglast)
404 break; /* Queue empty */
405 func = pendingcalls[i].func;
406 arg = pendingcalls[i].arg;
407 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000408 if (func(arg) < 0) {
409 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000410 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000411 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000412 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000413 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000414 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000415 return 0;
416}
417
418
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000419/* The interpreter's recursion limit */
420
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000421#ifndef Py_DEFAULT_RECURSION_LIMIT
422#define Py_DEFAULT_RECURSION_LIMIT 1000
423#endif
424static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
425int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000426
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000427int
428Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000429{
430 return recursion_limit;
431}
432
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000433void
434Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000435{
436 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000437 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000438}
439
Armin Rigo2b3eb402003-10-28 12:05:48 +0000440/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
441 if the recursion_depth reaches _Py_CheckRecursionLimit.
442 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
443 to guarantee that _Py_CheckRecursiveCall() is regularly called.
444 Without USE_STACKCHECK, there is no need for this. */
445int
446_Py_CheckRecursiveCall(char *where)
447{
448 PyThreadState *tstate = PyThreadState_GET();
449
450#ifdef USE_STACKCHECK
451 if (PyOS_CheckStack()) {
452 --tstate->recursion_depth;
453 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
454 return -1;
455 }
456#endif
457 if (tstate->recursion_depth > recursion_limit) {
458 --tstate->recursion_depth;
459 PyErr_Format(PyExc_RuntimeError,
460 "maximum recursion depth exceeded%s",
461 where);
462 return -1;
463 }
464 _Py_CheckRecursionLimit = recursion_limit;
465 return 0;
466}
467
Guido van Rossum374a9221991-04-04 10:40:29 +0000468/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000469enum why_code {
470 WHY_NOT = 0x0001, /* No error */
471 WHY_EXCEPTION = 0x0002, /* Exception occurred */
472 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
473 WHY_RETURN = 0x0008, /* 'return' statement */
474 WHY_BREAK = 0x0010, /* 'break' statement */
475 WHY_CONTINUE = 0x0020, /* 'continue' statement */
476 WHY_YIELD = 0x0040 /* 'yield' operator */
477};
Guido van Rossum374a9221991-04-04 10:40:29 +0000478
Raymond Hettinger7c958652004-04-06 10:11:10 +0000479static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000480static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000481
Skip Montanarod581d772002-09-03 20:10:45 +0000482/* for manipulating the thread switch and periodic "stuff" - used to be
483 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000484int _Py_CheckInterval = 100;
485volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000486
Guido van Rossumb209a111997-04-29 18:18:01 +0000487PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000488PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000489{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000490 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000491 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000492 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000493 (PyObject **)NULL, 0,
494 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000495 (PyObject **)NULL, 0,
496 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000497}
498
499
500/* Interpreter main loop */
501
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000502PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000503PyEval_EvalFrame(PyFrameObject *f) {
504 /* This is for backward compatibility with extension modules that
505 used this API; core interpreter code should call PyEval_EvalFrameEx() */
506 return PyEval_EvalFrameEx(f, 0);
507}
508
509PyObject *
510PyEval_EvalFrameEx(PyFrameObject *f, int throw)
Guido van Rossum374a9221991-04-04 10:40:29 +0000511{
Guido van Rossum950361c1997-01-24 13:49:28 +0000512#ifdef DXPAIRS
513 int lastopcode = 0;
514#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000515 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000516 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000517 register int opcode; /* Current opcode */
518 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000519 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000520 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000521 register PyObject *x; /* Result object -- NULL if error */
522 register PyObject *v; /* Temporary objects popped off stack */
523 register PyObject *w;
524 register PyObject *u;
525 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000526 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000527 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000528 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000529 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000530 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000531
Tim Peters8a5c3c72004-04-05 19:36:21 +0000532 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000533
534 not (instr_lb <= current_bytecode_offset < instr_ub)
535
Tim Peters8a5c3c72004-04-05 19:36:21 +0000536 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000537 initial values are such as to make this false the first
538 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000539 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000540
Guido van Rossumd076c731998-10-07 19:42:25 +0000541 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000542 PyObject *names;
543 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000544#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000545 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000546 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000547#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000548
Neal Norwitza81d2202002-07-14 00:27:26 +0000549/* Tuple access macros */
550
551#ifndef Py_DEBUG
552#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
553#else
554#define GETITEM(v, i) PyTuple_GetItem((v), (i))
555#endif
556
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000557#ifdef WITH_TSC
558/* Use Pentium timestamp counter to mark certain events:
559 inst0 -- beginning of switch statement for opcode dispatch
560 inst1 -- end of switch statement (may be skipped)
561 loop0 -- the top of the mainloop
562 loop1 -- place where control returns again to top of mainloop
563 (may be skipped)
564 intr1 -- beginning of long interruption
565 intr2 -- end of long interruption
566
567 Many opcodes call out to helper C functions. In some cases, the
568 time in those functions should be counted towards the time for the
569 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
570 calls another Python function; there's no point in charge all the
571 bytecode executed by the called function to the caller.
572
573 It's hard to make a useful judgement statically. In the presence
574 of operator overloading, it's impossible to tell if a call will
575 execute new Python code or not.
576
577 It's a case-by-case judgement. I'll use intr1 for the following
578 cases:
579
580 EXEC_STMT
581 IMPORT_STAR
582 IMPORT_FROM
583 CALL_FUNCTION (and friends)
584
585 */
586 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
587 int ticked = 0;
588
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000589 READ_TIMESTAMP(inst0);
590 READ_TIMESTAMP(inst1);
591 READ_TIMESTAMP(loop0);
592 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000593
594 /* shut up the compiler */
595 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000596#endif
597
Guido van Rossum374a9221991-04-04 10:40:29 +0000598/* Code access macros */
599
Martin v. Löwis18e16552006-02-15 17:27:45 +0000600#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000601#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000602#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000603#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000604#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000605#define JUMPBY(x) (next_instr += (x))
606
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607/* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
612
613 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
618
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000621
Tim Peters8a5c3c72004-04-05 19:36:21 +0000622 If collecting opcode statistics, turn off prediction so that
623 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000624 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000625*/
626
Raymond Hettingera7216982004-02-08 19:59:27 +0000627#ifdef DYNAMIC_EXECUTION_PROFILE
628#define PREDICT(op) if (0) goto PRED_##op
629#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000630#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000631#endif
632
Raymond Hettingerf606f872003-03-16 03:11:04 +0000633#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000634#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000635
Guido van Rossum374a9221991-04-04 10:40:29 +0000636/* Stack manipulation macros */
637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638/* The stack can grow at most MAXINT deep, as co_nlocals and
639 co_stacksize are ints. */
640#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define EMPTY() (STACK_LEVEL() == 0)
642#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define SECOND() (stack_pointer[-2])
644#define THIRD() (stack_pointer[-3])
645#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define SET_TOP(v) (stack_pointer[-1] = (v))
647#define SET_SECOND(v) (stack_pointer[-2] = (v))
648#define SET_THIRD(v) (stack_pointer[-3] = (v))
649#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000650#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000651#define BASIC_PUSH(v) (*stack_pointer++ = (v))
652#define BASIC_POP() (*--stack_pointer)
653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000655#define PUSH(v) { (void)(BASIC_PUSH(v), \
656 lltrace && prtrace(TOP(), "push")); \
657 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000658#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
660 lltrace && prtrace(TOP(), "stackadj")); \
661 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossumc2e20742006-02-27 22:32:47 +0000662#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000663#else
664#define PUSH(v) BASIC_PUSH(v)
665#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000666#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000667#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000668#endif
669
Guido van Rossum681d79a1995-07-18 14:51:37 +0000670/* Local variable macros */
671
672#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000673
674/* The SETLOCAL() macro must not DECREF the local variable in-place and
675 then store the new value; it must copy the old value to a temporary
676 value, then store the new value, and then DECREF the temporary value.
677 This is because it is possible that during the DECREF the frame is
678 accessed by other code (e.g. a __del__ method or gc.collect()) and the
679 variable would be pointing to already-freed memory. */
680#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
681 GETLOCAL(i) = value; \
682 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000683
Guido van Rossuma027efa1997-05-05 20:56:21 +0000684/* Start of code */
685
Tim Peters5ca576e2001-06-18 22:08:13 +0000686 if (f == NULL)
687 return NULL;
688
Armin Rigo1d313ab2003-10-25 14:33:09 +0000689 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000690 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000691 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000694
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000713 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000714 }
715 }
716 if (tstate->c_profilefunc != NULL) {
717 /* Similar for c_profilefunc, except it needn't
718 return itself and isn't called for "line" events */
719 if (call_trace(tstate->c_profilefunc,
720 tstate->c_profileobj,
721 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000723 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000724 }
725 }
726 }
727
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000728 co = f->f_code;
729 names = co->co_names;
730 consts = co->co_consts;
731 fastlocals = f->f_localsplus;
732 freevars = f->f_localsplus + f->f_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000733 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000734 /* An explanation is in order for the next line.
735
736 f->f_lasti now refers to the index of the last instruction
737 executed. You might think this was obvious from the name, but
738 this wasn't always true before 2.3! PyFrame_New now sets
739 f->f_lasti to -1 (i.e. the index *before* the first instruction)
740 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
741 does work. Promise. */
742 next_instr = first_instr + f->f_lasti + 1;
743 stack_pointer = f->f_stacktop;
744 assert(stack_pointer != NULL);
745 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
746
Tim Peters5ca576e2001-06-18 22:08:13 +0000747#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000749#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000750#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000751 filename = PyString_AsString(co->co_filename);
752#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000753
Guido van Rossum374a9221991-04-04 10:40:29 +0000754 why = WHY_NOT;
755 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000756 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000757 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000758
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000759 if (throw) { /* support for generator.throw() */
760 why = WHY_EXCEPTION;
761 goto on_error;
762 }
763
Guido van Rossum374a9221991-04-04 10:40:29 +0000764 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000765#ifdef WITH_TSC
766 if (inst1 == 0) {
767 /* Almost surely, the opcode executed a break
768 or a continue, preventing inst1 from being set
769 on the way out of the loop.
770 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000771 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000772 loop1 = inst1;
773 }
774 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
775 intr0, intr1);
776 ticked = 0;
777 inst1 = 0;
778 intr0 = 0;
779 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000780 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000781#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000782 assert(stack_pointer >= f->f_valuestack); /* else underflow */
783 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
784
Guido van Rossuma027efa1997-05-05 20:56:21 +0000785 /* Do periodic things. Doing this every time through
786 the loop would add too much overhead, so we do it
787 only every Nth instruction. We also do it if
788 ``things_to_do'' is set, i.e. when an asynchronous
789 event needs attention (e.g. a signal handler or
790 async I/O handler); see Py_AddPendingCall() and
791 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000792
Skip Montanarod581d772002-09-03 20:10:45 +0000793 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000794 if (*next_instr == SETUP_FINALLY) {
795 /* Make the last opcode before
796 a try: finally: block uninterruptable. */
797 goto fast_next_opcode;
798 }
Skip Montanarod581d772002-09-03 20:10:45 +0000799 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000800 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000801#ifdef WITH_TSC
802 ticked = 1;
803#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000804 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000805 if (Py_MakePendingCalls() < 0) {
806 why = WHY_EXCEPTION;
807 goto on_error;
808 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000809 if (things_to_do)
810 /* MakePendingCalls() didn't succeed.
811 Force early re-execution of this
812 "periodic" code, possibly after
813 a thread switch */
814 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000815 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000816#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000817 if (interpreter_lock) {
818 /* Give another thread a chance */
819
Guido van Rossum25ce5661997-08-02 03:10:38 +0000820 if (PyThreadState_Swap(NULL) != tstate)
821 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000822 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000823
824 /* Other threads may run now */
825
Guido van Rossum65d5b571998-12-21 19:32:43 +0000826 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000827 if (PyThreadState_Swap(tstate) != NULL)
828 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000829
830 /* Check for thread interrupts */
831
832 if (tstate->async_exc != NULL) {
833 x = tstate->async_exc;
834 tstate->async_exc = NULL;
835 PyErr_SetNone(x);
836 Py_DECREF(x);
837 why = WHY_EXCEPTION;
838 goto on_error;
839 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 }
841#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000842 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000843
Neil Schemenauer63543862002-02-17 19:10:14 +0000844 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000845 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000846
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000847 /* line-by-line tracing support */
848
849 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
850 /* see maybe_call_line_trace
851 for expository comments */
852 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000853
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000854 err = maybe_call_line_trace(tstate->c_tracefunc,
855 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000856 f, &instr_lb, &instr_ub,
857 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000858 /* Reload possibly changed frame fields */
859 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000860 if (f->f_stacktop != NULL) {
861 stack_pointer = f->f_stacktop;
862 f->f_stacktop = NULL;
863 }
864 if (err) {
865 /* trace function raised an exception */
866 goto on_error;
867 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000868 }
869
870 /* Extract opcode and argument */
871
Guido van Rossum374a9221991-04-04 10:40:29 +0000872 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000873 oparg = 0; /* allows oparg to be stored in a register because
874 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000875 if (HAS_ARG(opcode))
876 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000877 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000878#ifdef DYNAMIC_EXECUTION_PROFILE
879#ifdef DXPAIRS
880 dxpairs[lastopcode][opcode]++;
881 lastopcode = opcode;
882#endif
883 dxp[opcode]++;
884#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000885
Guido van Rossum96a42c81992-01-12 02:29:51 +0000886#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000887 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000888
Guido van Rossum96a42c81992-01-12 02:29:51 +0000889 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 if (HAS_ARG(opcode)) {
891 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000892 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 }
894 else {
895 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000896 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 }
898 }
899#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000900
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000902 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000905
Guido van Rossum374a9221991-04-04 10:40:29 +0000906 /* BEWARE!
907 It is essential that any operation that fails sets either
908 x to NULL, err to nonzero, or why to anything but WHY_NOT,
909 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000913 case NOP:
914 goto fast_next_opcode;
915
Neil Schemenauer63543862002-02-17 19:10:14 +0000916 case LOAD_FAST:
917 x = GETLOCAL(oparg);
918 if (x != NULL) {
919 Py_INCREF(x);
920 PUSH(x);
921 goto fast_next_opcode;
922 }
923 format_exc_check_arg(PyExc_UnboundLocalError,
924 UNBOUNDLOCAL_ERROR_MSG,
925 PyTuple_GetItem(co->co_varnames, oparg));
926 break;
927
928 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000929 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000930 Py_INCREF(x);
931 PUSH(x);
932 goto fast_next_opcode;
933
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000934 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000935 case STORE_FAST:
936 v = POP();
937 SETLOCAL(oparg, v);
938 goto fast_next_opcode;
939
Raymond Hettingerf606f872003-03-16 03:11:04 +0000940 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 case POP_TOP:
942 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000943 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000944 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000945
Guido van Rossum374a9221991-04-04 10:40:29 +0000946 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000947 v = TOP();
948 w = SECOND();
949 SET_TOP(w);
950 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000951 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 v = TOP();
955 w = SECOND();
956 x = THIRD();
957 SET_TOP(w);
958 SET_SECOND(x);
959 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000960 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000961
Thomas Wouters434d0822000-08-24 20:11:32 +0000962 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 u = TOP();
964 v = SECOND();
965 w = THIRD();
966 x = FOURTH();
967 SET_TOP(v);
968 SET_SECOND(w);
969 SET_THIRD(x);
970 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000971 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000972
Guido van Rossum374a9221991-04-04 10:40:29 +0000973 case DUP_TOP:
974 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000975 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000976 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000977 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000978
Thomas Wouters434d0822000-08-24 20:11:32 +0000979 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000980 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000982 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000984 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000985 STACKADJ(2);
986 SET_TOP(x);
987 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000988 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000989 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000991 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000993 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000995 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000996 STACKADJ(3);
997 SET_TOP(x);
998 SET_SECOND(w);
999 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001001 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001002 Py_FatalError("invalid argument to DUP_TOPX"
1003 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001004 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001005
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001011 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001016 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001017 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001019 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001021
Guido van Rossum374a9221991-04-04 10:40:29 +00001022 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001025 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001026 if (err == 0) {
1027 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001029 continue;
1030 }
1031 else if (err > 0) {
1032 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001034 err = 0;
1035 continue;
1036 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001037 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Guido van Rossum374a9221991-04-04 10:40:29 +00001040 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001042 x = PyObject_Repr(v);
1043 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001045 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001047
Guido van Rossum7928cd71991-10-24 14:59:31 +00001048 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001050 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001051 Py_DECREF(v);
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 Rossum7928cd71991-10-24 14:59:31 +00001054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Guido van Rossum50564e81996-01-12 01:13:16 +00001056 case BINARY_POWER:
1057 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001059 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001060 Py_DECREF(v);
1061 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001063 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001064 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001065
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 case BINARY_MULTIPLY:
1067 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001069 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001070 Py_DECREF(v);
1071 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001073 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Tim Peters3caca232001-12-06 06:23:26 +00001076 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001079 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001080 Py_DECREF(v);
1081 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001083 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Guido van Rossum4668b002001-08-08 05:00:18 +00001086 case BINARY_FLOOR_DIVIDE:
1087 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001089 x = PyNumber_FloorDivide(v, w);
1090 Py_DECREF(v);
1091 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001093 if (x != NULL) continue;
1094 break;
1095
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 case BINARY_MODULO:
1097 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001099 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001100 Py_DECREF(v);
1101 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001103 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001105
Guido van Rossum374a9221991-04-04 10:40:29 +00001106 case BINARY_ADD:
1107 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001108 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001109 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 /* INLINE: int + int */
1111 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001112 a = PyInt_AS_LONG(v);
1113 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001115 if ((i^a) < 0 && (i^b) < 0)
1116 goto slow_add;
1117 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001119 else if (PyString_CheckExact(v) &&
1120 PyString_CheckExact(w)) {
1121 x = string_concatenate(v, w, f, next_instr);
1122 /* string_concatenate consumed the ref to v */
1123 goto skip_decref_vx;
1124 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001125 else {
1126 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001128 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001129 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001130 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 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 Rossum374a9221991-04-04 10:40:29 +00001134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001135
Guido van Rossum374a9221991-04-04 10:40:29 +00001136 case BINARY_SUBTRACT:
1137 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001138 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001139 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001140 /* INLINE: int - int */
1141 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001142 a = PyInt_AS_LONG(v);
1143 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001144 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001145 if ((i^a) < 0 && (i^~b) < 0)
1146 goto slow_sub;
1147 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001148 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001149 else {
1150 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001152 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001153 Py_DECREF(v);
1154 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001156 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Guido van Rossum374a9221991-04-04 10:40:29 +00001159 case BINARY_SUBSCR:
1160 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001161 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001162 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001163 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001164 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001166 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001167 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001168 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001169 Py_INCREF(x);
1170 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001171 else
1172 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001173 }
1174 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001175 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001176 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001177 Py_DECREF(v);
1178 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001180 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001181 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001182
Guido van Rossum7928cd71991-10-24 14:59:31 +00001183 case BINARY_LSHIFT:
1184 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001186 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001187 Py_DECREF(v);
1188 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001190 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001191 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001192
Guido van Rossum7928cd71991-10-24 14:59:31 +00001193 case BINARY_RSHIFT:
1194 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001196 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001197 Py_DECREF(v);
1198 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001200 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001201 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Guido van Rossum7928cd71991-10-24 14:59:31 +00001203 case BINARY_AND:
1204 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001206 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001207 Py_DECREF(v);
1208 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001210 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001211 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Guido van Rossum7928cd71991-10-24 14:59:31 +00001213 case BINARY_XOR:
1214 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001216 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001217 Py_DECREF(v);
1218 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001219 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001220 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001221 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001222
Guido van Rossum7928cd71991-10-24 14:59:31 +00001223 case BINARY_OR:
1224 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001226 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001227 Py_DECREF(v);
1228 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001229 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001230 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001231 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001232
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001233 case LIST_APPEND:
1234 w = POP();
1235 v = POP();
1236 err = PyList_Append(v, w);
1237 Py_DECREF(v);
1238 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001239 if (err == 0) {
1240 PREDICT(JUMP_ABSOLUTE);
1241 continue;
1242 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001243 break;
1244
Thomas Wouters434d0822000-08-24 20:11:32 +00001245 case INPLACE_POWER:
1246 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 x = PyNumber_InPlacePower(v, w, Py_None);
1249 Py_DECREF(v);
1250 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001251 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 if (x != NULL) continue;
1253 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 case INPLACE_MULTIPLY:
1256 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 x = PyNumber_InPlaceMultiply(v, w);
1259 Py_DECREF(v);
1260 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001262 if (x != NULL) continue;
1263 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001266 if (!_Py_QnewFlag) {
1267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001269 x = PyNumber_InPlaceDivide(v, w);
1270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001273 if (x != NULL) continue;
1274 break;
1275 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001276 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001277 INPLACE_TRUE_DIVIDE */
1278 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001280 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001281 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 Py_DECREF(v);
1283 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 if (x != NULL) continue;
1286 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Guido van Rossum4668b002001-08-08 05:00:18 +00001288 case INPLACE_FLOOR_DIVIDE:
1289 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001291 x = PyNumber_InPlaceFloorDivide(v, w);
1292 Py_DECREF(v);
1293 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001295 if (x != NULL) continue;
1296 break;
1297
Thomas Wouters434d0822000-08-24 20:11:32 +00001298 case INPLACE_MODULO:
1299 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001300 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 x = PyNumber_InPlaceRemainder(v, w);
1302 Py_DECREF(v);
1303 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 if (x != NULL) continue;
1306 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 case INPLACE_ADD:
1309 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001310 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001311 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 /* INLINE: int + int */
1313 register long a, b, i;
1314 a = PyInt_AS_LONG(v);
1315 b = PyInt_AS_LONG(w);
1316 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001317 if ((i^a) < 0 && (i^b) < 0)
1318 goto slow_iadd;
1319 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001321 else if (PyString_CheckExact(v) &&
1322 PyString_CheckExact(w)) {
1323 x = string_concatenate(v, w, f, next_instr);
1324 /* string_concatenate consumed the ref to v */
1325 goto skip_decref_v;
1326 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001327 else {
1328 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001330 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001332 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 if (x != NULL) continue;
1336 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001337
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 case INPLACE_SUBTRACT:
1339 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001341 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 /* INLINE: int - int */
1343 register long a, b, i;
1344 a = PyInt_AS_LONG(v);
1345 b = PyInt_AS_LONG(w);
1346 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001347 if ((i^a) < 0 && (i^~b) < 0)
1348 goto slow_isub;
1349 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001351 else {
1352 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001354 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 Py_DECREF(v);
1356 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 if (x != NULL) continue;
1359 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 case INPLACE_LSHIFT:
1362 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001363 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 x = PyNumber_InPlaceLshift(v, w);
1365 Py_DECREF(v);
1366 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 if (x != NULL) continue;
1369 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 case INPLACE_RSHIFT:
1372 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 x = PyNumber_InPlaceRshift(v, w);
1375 Py_DECREF(v);
1376 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 if (x != NULL) continue;
1379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 case INPLACE_AND:
1382 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001383 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001384 x = PyNumber_InPlaceAnd(v, w);
1385 Py_DECREF(v);
1386 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 if (x != NULL) continue;
1389 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 case INPLACE_XOR:
1392 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001393 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001394 x = PyNumber_InPlaceXor(v, w);
1395 Py_DECREF(v);
1396 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001398 if (x != NULL) continue;
1399 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Thomas Wouters434d0822000-08-24 20:11:32 +00001401 case INPLACE_OR:
1402 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001404 x = PyNumber_InPlaceOr(v, w);
1405 Py_DECREF(v);
1406 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001408 if (x != NULL) continue;
1409 break;
1410
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 case SLICE+0:
1412 case SLICE+1:
1413 case SLICE+2:
1414 case SLICE+3:
1415 if ((opcode-SLICE) & 2)
1416 w = POP();
1417 else
1418 w = NULL;
1419 if ((opcode-SLICE) & 1)
1420 v = POP();
1421 else
1422 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001425 Py_DECREF(u);
1426 Py_XDECREF(v);
1427 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001428 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001429 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 case STORE_SLICE+0:
1433 case STORE_SLICE+1:
1434 case STORE_SLICE+2:
1435 case STORE_SLICE+3:
1436 if ((opcode-STORE_SLICE) & 2)
1437 w = POP();
1438 else
1439 w = NULL;
1440 if ((opcode-STORE_SLICE) & 1)
1441 v = POP();
1442 else
1443 v = NULL;
1444 u = POP();
1445 t = POP();
1446 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001447 Py_DECREF(t);
1448 Py_DECREF(u);
1449 Py_XDECREF(v);
1450 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001451 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 case DELETE_SLICE+0:
1455 case DELETE_SLICE+1:
1456 case DELETE_SLICE+2:
1457 case DELETE_SLICE+3:
1458 if ((opcode-DELETE_SLICE) & 2)
1459 w = POP();
1460 else
1461 w = NULL;
1462 if ((opcode-DELETE_SLICE) & 1)
1463 v = POP();
1464 else
1465 v = NULL;
1466 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001467 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(u);
1470 Py_XDECREF(v);
1471 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001472 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001476 w = TOP();
1477 v = SECOND();
1478 u = THIRD();
1479 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001480 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001481 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001482 Py_DECREF(u);
1483 Py_DECREF(v);
1484 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001485 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 w = TOP();
1490 v = SECOND();
1491 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001492 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001493 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001494 Py_DECREF(v);
1495 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001496 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001498
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 case PRINT_EXPR:
1500 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001501 w = PySys_GetObject("displayhook");
1502 if (w == NULL) {
1503 PyErr_SetString(PyExc_RuntimeError,
1504 "lost sys.displayhook");
1505 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001506 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001507 }
1508 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001509 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001510 if (x == NULL)
1511 err = -1;
1512 }
1513 if (err == 0) {
1514 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001515 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001516 if (w == NULL)
1517 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001519 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001520 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001521 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001522
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001523 case PRINT_ITEM_TO:
1524 w = stream = POP();
1525 /* fall through to PRINT_ITEM */
1526
Guido van Rossum374a9221991-04-04 10:40:29 +00001527 case PRINT_ITEM:
1528 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001529 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001530 w = PySys_GetObject("stdout");
1531 if (w == NULL) {
1532 PyErr_SetString(PyExc_RuntimeError,
1533 "lost sys.stdout");
1534 err = -1;
1535 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001536 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001537 /* PyFile_SoftSpace() can exececute arbitrary code
1538 if sys.stdout is an instance with a __getattr__.
1539 If __getattr__ raises an exception, w will
1540 be freed, so we need to prevent that temporarily. */
1541 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001542 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001543 err = PyFile_WriteString(" ", w);
1544 if (err == 0)
1545 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001546 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001547 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001548 if (PyString_Check(v)) {
1549 char *s = PyString_AS_STRING(v);
1550 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001551 if (len == 0 ||
1552 !isspace(Py_CHARMASK(s[len-1])) ||
1553 s[len-1] == ' ')
1554 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001555 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001556#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001557 else if (PyUnicode_Check(v)) {
1558 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1559 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001560 if (len == 0 ||
1561 !Py_UNICODE_ISSPACE(s[len-1]) ||
1562 s[len-1] == ' ')
1563 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001564 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001565#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001566 else
1567 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001569 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001570 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001571 Py_XDECREF(stream);
1572 stream = NULL;
1573 if (err == 0)
1574 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001577 case PRINT_NEWLINE_TO:
1578 w = stream = POP();
1579 /* fall through to PRINT_NEWLINE */
1580
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001582 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001583 w = PySys_GetObject("stdout");
1584 if (w == NULL)
1585 PyErr_SetString(PyExc_RuntimeError,
1586 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001587 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001588 if (w != NULL) {
1589 err = PyFile_WriteString("\n", w);
1590 if (err == 0)
1591 PyFile_SoftSpace(w, 0);
1592 }
1593 Py_XDECREF(stream);
1594 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001595 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001596
Thomas Wouters434d0822000-08-24 20:11:32 +00001597
1598#ifdef CASE_TOO_BIG
1599 default: switch (opcode) {
1600#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 case RAISE_VARARGS:
1602 u = v = w = NULL;
1603 switch (oparg) {
1604 case 3:
1605 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001606 /* Fallthrough */
1607 case 2:
1608 v = POP(); /* value */
1609 /* Fallthrough */
1610 case 1:
1611 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001612 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001613 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001614 break;
1615 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001616 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001618 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001619 break;
1620 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001622
Guido van Rossum374a9221991-04-04 10:40:29 +00001623 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001624 if ((x = f->f_locals) != NULL) {
1625 Py_INCREF(x);
1626 PUSH(x);
1627 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001628 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001629 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 case RETURN_VALUE:
1633 retval = POP();
1634 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001635 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001636
Tim Peters5ca576e2001-06-18 22:08:13 +00001637 case YIELD_VALUE:
1638 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001639 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001640 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001641 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001642
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001643 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001644 w = TOP();
1645 v = SECOND();
1646 u = THIRD();
1647 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001648 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001649 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001650 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001651 Py_DECREF(u);
1652 Py_DECREF(v);
1653 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 case POP_BLOCK:
1657 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 while (STACK_LEVEL() > b->b_level) {
1660 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001661 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 }
1663 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001664 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 case END_FINALLY:
1667 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001668 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001669 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001670 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001671 if (why == WHY_RETURN ||
1672 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 retval = POP();
1674 }
Brett Cannonbf364092006-03-01 04:25:17 +00001675 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001677 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001678 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001680 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001682 else if (v != Py_None) {
1683 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 "'finally' pops bad exception");
1685 why = WHY_EXCEPTION;
1686 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001687 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001691 u = TOP();
1692 v = SECOND();
1693 w = THIRD();
1694 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001695 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001696 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001697 Py_DECREF(u);
1698 Py_DECREF(v);
1699 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Guido van Rossum374a9221991-04-04 10:40:29 +00001702 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001703 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001705 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001706 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001707 err = PyDict_SetItem(x, w, v);
1708 else
1709 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001710 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001711 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001712 break;
1713 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001714 PyErr_Format(PyExc_SystemError,
1715 "no locals found when storing %s",
1716 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001720 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001721 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001722 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001723 format_exc_check_arg(PyExc_NameError,
1724 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001725 break;
1726 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001727 PyErr_Format(PyExc_SystemError,
1728 "no locals when deleting %s",
1729 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001731
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001732 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001733 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001735 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1736 PyObject **items = ((PyTupleObject *)v)->ob_item;
1737 while (oparg--) {
1738 w = items[oparg];
1739 Py_INCREF(w);
1740 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001741 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001742 Py_DECREF(v);
1743 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001744 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1745 PyObject **items = ((PyListObject *)v)->ob_item;
1746 while (oparg--) {
1747 w = items[oparg];
1748 Py_INCREF(w);
1749 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001750 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001751 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001752 stack_pointer + oparg))
1753 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001754 else {
1755 if (PyErr_ExceptionMatches(PyExc_TypeError))
1756 PyErr_SetString(PyExc_TypeError,
1757 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001758 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001759 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001764 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001765 v = TOP();
1766 u = SECOND();
1767 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1769 Py_DECREF(v);
1770 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001771 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001775 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001777 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1778 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001780 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001782 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001783 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001784 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 err = PyDict_SetItem(f->f_globals, w, v);
1786 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001787 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001788 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001790 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001791 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001792 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001793 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001794 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001795 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001796
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001798 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001799 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001800 PyErr_Format(PyExc_SystemError,
1801 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001802 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001803 break;
1804 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001805 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001806 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001807 Py_XINCREF(x);
1808 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001809 else {
1810 x = PyObject_GetItem(v, w);
1811 if (x == NULL && PyErr_Occurred()) {
1812 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1813 break;
1814 PyErr_Clear();
1815 }
1816 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001818 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001820 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001821 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001822 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001823 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001824 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 break;
1826 }
1827 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001828 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001829 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001831 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001832
Guido van Rossum374a9221991-04-04 10:40:29 +00001833 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001834 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001835 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001836 /* Inline the PyDict_GetItem() calls.
1837 WARNING: this is an extreme speed hack.
1838 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001839 long hash = ((PyStringObject *)w)->ob_shash;
1840 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001841 PyDictObject *d;
1842 d = (PyDictObject *)(f->f_globals);
1843 x = d->ma_lookup(d, w, hash)->me_value;
1844 if (x != NULL) {
1845 Py_INCREF(x);
1846 PUSH(x);
1847 continue;
1848 }
1849 d = (PyDictObject *)(f->f_builtins);
1850 x = d->ma_lookup(d, w, hash)->me_value;
1851 if (x != NULL) {
1852 Py_INCREF(x);
1853 PUSH(x);
1854 continue;
1855 }
1856 goto load_global_error;
1857 }
1858 }
1859 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001860 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001862 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001864 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001865 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001866 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001867 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 break;
1869 }
1870 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001871 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001873 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001874
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001875 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001876 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001877 if (x != NULL) {
1878 SETLOCAL(oparg, NULL);
1879 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001880 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001881 format_exc_check_arg(
1882 PyExc_UnboundLocalError,
1883 UNBOUNDLOCAL_ERROR_MSG,
1884 PyTuple_GetItem(co->co_varnames, oparg)
1885 );
1886 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001887
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001888 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001889 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001890 Py_INCREF(x);
1891 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001892 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001893 break;
1894
1895 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001896 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001897 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 if (w != NULL) {
1899 PUSH(w);
1900 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001901 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001902 err = -1;
1903 /* Don't stomp existing exception */
1904 if (PyErr_Occurred())
1905 break;
1906 if (oparg < f->f_ncells) {
1907 v = PyTuple_GetItem(co->co_cellvars,
1908 oparg);
1909 format_exc_check_arg(
1910 PyExc_UnboundLocalError,
1911 UNBOUNDLOCAL_ERROR_MSG,
1912 v);
1913 } else {
1914 v = PyTuple_GetItem(
1915 co->co_freevars,
1916 oparg - f->f_ncells);
1917 format_exc_check_arg(
1918 PyExc_NameError,
1919 UNBOUNDFREE_ERROR_MSG,
1920 v);
1921 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001922 break;
1923
1924 case STORE_DEREF:
1925 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001926 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001927 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001928 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001929 continue;
1930
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001934 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001935 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001936 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001937 }
1938 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001939 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 }
1941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Guido van Rossum374a9221991-04-04 10:40:29 +00001943 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001944 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001946 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001947 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001948 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 }
1950 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001951 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 }
1953 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001954
Guido van Rossum374a9221991-04-04 10:40:29 +00001955 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001958 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001959 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001962 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001963 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001964 x = PyObject_GetAttr(v, w);
1965 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001966 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001967 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 case COMPARE_OP:
1971 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001972 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001973 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001974 /* INLINE: cmp(int, int) */
1975 register long a, b;
1976 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001977 a = PyInt_AS_LONG(v);
1978 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001979 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001980 case PyCmp_LT: res = a < b; break;
1981 case PyCmp_LE: res = a <= b; break;
1982 case PyCmp_EQ: res = a == b; break;
1983 case PyCmp_NE: res = a != b; break;
1984 case PyCmp_GT: res = a > b; break;
1985 case PyCmp_GE: res = a >= b; break;
1986 case PyCmp_IS: res = v == w; break;
1987 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001988 default: goto slow_compare;
1989 }
1990 x = res ? Py_True : Py_False;
1991 Py_INCREF(x);
1992 }
1993 else {
1994 slow_compare:
1995 x = cmp_outcome(oparg, v, w);
1996 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001997 Py_DECREF(v);
1998 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001999 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002000 if (x == NULL) break;
2001 PREDICT(JUMP_IF_FALSE);
2002 PREDICT(JUMP_IF_TRUE);
2003 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002004
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002006 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002007 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002008 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002009 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002010 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002011 break;
2012 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002013 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002014 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002015 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2016 w = PyTuple_Pack(5,
2017 w,
2018 f->f_globals,
2019 f->f_locals == NULL ?
2020 Py_None : f->f_locals,
2021 v,
2022 u);
2023 else
2024 w = PyTuple_Pack(4,
2025 w,
2026 f->f_globals,
2027 f->f_locals == NULL ?
2028 Py_None : f->f_locals,
2029 v);
2030 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002031 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002032 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002033 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002034 x = NULL;
2035 break;
2036 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002037 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002039 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002041 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002042 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Thomas Wouters52152252000-08-17 22:55:00 +00002045 case IMPORT_STAR:
2046 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002047 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002048 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002049 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002050 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002051 break;
2052 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002053 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002054 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002055 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002056 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002057 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002058 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002060
Thomas Wouters52152252000-08-17 22:55:00 +00002061 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002062 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002063 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002064 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002065 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002066 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002067 PUSH(x);
2068 if (x != NULL) continue;
2069 break;
2070
Guido van Rossum374a9221991-04-04 10:40:29 +00002071 case JUMP_FORWARD:
2072 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002073 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Raymond Hettingerf606f872003-03-16 03:11:04 +00002075 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002076 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002077 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002078 if (w == Py_True) {
2079 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002080 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002081 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002082 if (w == Py_False) {
2083 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002084 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002085 }
2086 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002087 if (err > 0)
2088 err = 0;
2089 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002090 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002091 else
2092 break;
2093 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Raymond Hettingerf606f872003-03-16 03:11:04 +00002095 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002096 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002097 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002098 if (w == Py_False) {
2099 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002100 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002101 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002102 if (w == Py_True) {
2103 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002104 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002105 }
2106 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002107 if (err > 0) {
2108 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002109 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002110 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002111 else if (err == 0)
2112 ;
2113 else
2114 break;
2115 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002117 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 case JUMP_ABSOLUTE:
2119 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002120 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002122 case GET_ITER:
2123 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002124 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125 x = PyObject_GetIter(v);
2126 Py_DECREF(v);
2127 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002128 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002129 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002130 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002131 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002132 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133 break;
2134
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002135 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002136 case FOR_ITER:
2137 /* before: [iter]; after: [iter, iter()] *or* [] */
2138 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002139 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002140 if (x != NULL) {
2141 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002142 PREDICT(STORE_FAST);
2143 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002144 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002145 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002146 if (PyErr_Occurred()) {
2147 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2148 break;
2149 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002150 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002151 /* iterator ended normally */
2152 x = v = POP();
2153 Py_DECREF(v);
2154 JUMPBY(oparg);
2155 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002157 case BREAK_LOOP:
2158 why = WHY_BREAK;
2159 goto fast_block_end;
2160
2161 case CONTINUE_LOOP:
2162 retval = PyInt_FromLong(oparg);
2163 why = WHY_CONTINUE;
2164 goto fast_block_end;
2165
Guido van Rossum374a9221991-04-04 10:40:29 +00002166 case SETUP_LOOP:
2167 case SETUP_EXCEPT:
2168 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002169 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002170 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002171 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Guido van Rossumc2e20742006-02-27 22:32:47 +00002173 case WITH_CLEANUP:
2174 {
2175 /* TOP is the context.__exit__ bound method.
2176 Below that are 1-3 values indicating how/why
2177 we entered the finally clause:
2178 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002179 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002180 - SECOND = WHY_*; no retval below it
2181 - (SECOND, THIRD, FOURTH) = exc_info()
2182 In the last case, we must call
2183 TOP(SECOND, THIRD, FOURTH)
2184 otherwise we must call
2185 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002186
2187 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002188 *and* the function call returns a 'true' value, we
2189 "zap" this information, to prevent END_FINALLY from
2190 re-raising the exception. (But non-local gotos
2191 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002192 */
2193
2194 x = TOP();
2195 u = SECOND();
2196 if (PyInt_Check(u) || u == Py_None) {
2197 u = v = w = Py_None;
2198 }
2199 else {
2200 v = THIRD();
2201 w = FOURTH();
2202 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002203 /* XXX Not the fastest way to call it... */
2204 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2205 if (x == NULL)
2206 break; /* Go to error exit */
2207 if (u != Py_None && PyObject_IsTrue(x)) {
2208 /* There was an exception and a true return */
2209 Py_DECREF(x);
2210 x = TOP(); /* Again */
2211 STACKADJ(-3);
2212 Py_INCREF(Py_None);
2213 SET_TOP(Py_None);
2214 Py_DECREF(x);
2215 Py_DECREF(u);
2216 Py_DECREF(v);
2217 Py_DECREF(w);
2218 } else {
2219 /* Let END_FINALLY do its thing */
2220 Py_DECREF(x);
2221 x = POP();
2222 Py_DECREF(x);
2223 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002224 break;
2225 }
2226
Guido van Rossumf10570b1995-07-07 22:53:21 +00002227 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002228 {
2229 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002230 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002231 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002232#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002233 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002234#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002235 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002236#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002237 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002238 PUSH(x);
2239 if (x != NULL)
2240 continue;
2241 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002242 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002243
Jeremy Hylton76901512000-03-28 23:49:17 +00002244 case CALL_FUNCTION_VAR:
2245 case CALL_FUNCTION_KW:
2246 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002247 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002248 int na = oparg & 0xff;
2249 int nk = (oparg>>8) & 0xff;
2250 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002251 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002252 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002253 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002254 if (flags & CALL_FLAG_VAR)
2255 n++;
2256 if (flags & CALL_FLAG_KW)
2257 n++;
2258 pfunc = stack_pointer - n - 1;
2259 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002260
Guido van Rossumac7be682001-01-17 15:42:30 +00002261 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002262 && PyMethod_GET_SELF(func) != NULL) {
2263 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002264 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002265 func = PyMethod_GET_FUNCTION(func);
2266 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002267 Py_DECREF(*pfunc);
2268 *pfunc = self;
2269 na++;
2270 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002271 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002272 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002273 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002274 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002275 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002276 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002277 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002278 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002279
Jeremy Hylton76901512000-03-28 23:49:17 +00002280 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002281 w = POP();
2282 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002283 }
2284 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002285 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002286 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002287 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002289
Guido van Rossum681d79a1995-07-18 14:51:37 +00002290 case MAKE_FUNCTION:
2291 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002292 x = PyFunction_New(v, f->f_globals);
2293 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002294 /* XXX Maybe this should be a separate opcode? */
2295 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002297 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002298 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002299 x = NULL;
2300 break;
2301 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002302 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002303 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002304 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002305 }
2306 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002307 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002308 }
2309 PUSH(x);
2310 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002311
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002312 case MAKE_CLOSURE:
2313 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002314 v = POP(); /* code object */
2315 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002316 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 if (x != NULL) {
2318 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002319 err = PyFunction_SetClosure(x, v);
2320 Py_DECREF(v);
2321 }
2322 if (x != NULL && oparg > 0) {
2323 v = PyTuple_New(oparg);
2324 if (v == NULL) {
2325 Py_DECREF(x);
2326 x = NULL;
2327 break;
2328 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002329 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002330 w = POP();
2331 PyTuple_SET_ITEM(v, oparg, w);
2332 }
2333 err = PyFunction_SetDefaults(x, v);
2334 Py_DECREF(v);
2335 }
2336 PUSH(x);
2337 break;
2338 }
2339
Guido van Rossum8861b741996-07-30 16:49:37 +00002340 case BUILD_SLICE:
2341 if (oparg == 3)
2342 w = POP();
2343 else
2344 w = NULL;
2345 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002346 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002347 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002348 Py_DECREF(u);
2349 Py_DECREF(v);
2350 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002351 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002352 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002353 break;
2354
Fred Drakeef8ace32000-08-24 00:32:09 +00002355 case EXTENDED_ARG:
2356 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002357 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002358 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002359
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 default:
2361 fprintf(stderr,
2362 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002363 PyCode_Addr2Line(f->f_code, f->f_lasti),
2364 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002365 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 why = WHY_EXCEPTION;
2367 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002368
2369#ifdef CASE_TOO_BIG
2370 }
2371#endif
2372
Guido van Rossum374a9221991-04-04 10:40:29 +00002373 } /* switch */
2374
2375 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002377 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002378
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002380
Guido van Rossum374a9221991-04-04 10:40:29 +00002381 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002382 if (err == 0 && x != NULL) {
2383#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002384 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002385 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002386 fprintf(stderr,
2387 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002388 else {
2389#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002390 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002391 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002392#ifdef CHECKEXC
2393 }
2394#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002395 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002396 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002397 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002398 err = 0;
2399 }
2400
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002402
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002403 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002404 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002405 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002406 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002407 why = WHY_EXCEPTION;
2408 }
2409 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002410#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002411 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002412 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002413 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002414 char buf[1024];
2415 sprintf(buf, "Stack unwind with exception "
2416 "set and why=%d", why);
2417 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002418 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002419 }
2420#endif
2421
2422 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002423
Guido van Rossum374a9221991-04-04 10:40:29 +00002424 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002425 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002426
Fred Drake8f51f542001-10-04 14:48:42 +00002427 if (tstate->c_tracefunc != NULL)
2428 call_exc_trace(tstate->c_tracefunc,
2429 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002430 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002431
Guido van Rossum374a9221991-04-04 10:40:29 +00002432 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002433
Guido van Rossum374a9221991-04-04 10:40:29 +00002434 if (why == WHY_RERAISE)
2435 why = WHY_EXCEPTION;
2436
2437 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002438
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002439fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002440 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002441 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002442
Tim Peters8a5c3c72004-04-05 19:36:21 +00002443 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002444 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2445 /* For a continue inside a try block,
2446 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002447 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2448 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002449 why = WHY_NOT;
2450 JUMPTO(PyInt_AS_LONG(retval));
2451 Py_DECREF(retval);
2452 break;
2453 }
2454
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 while (STACK_LEVEL() > b->b_level) {
2456 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002457 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002458 }
2459 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2460 why = WHY_NOT;
2461 JUMPTO(b->b_handler);
2462 break;
2463 }
2464 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002465 (b->b_type == SETUP_EXCEPT &&
2466 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002467 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002468 PyObject *exc, *val, *tb;
2469 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002470 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002471 val = Py_None;
2472 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002473 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002474 /* Make the raw exception data
2475 available to the handler,
2476 so a program can emulate the
2477 Python main loop. Don't do
2478 this for 'finally'. */
2479 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002480 PyErr_NormalizeException(
2481 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002482 set_exc_info(tstate,
2483 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002484 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002485 if (tb == NULL) {
2486 Py_INCREF(Py_None);
2487 PUSH(Py_None);
2488 } else
2489 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002490 PUSH(val);
2491 PUSH(exc);
2492 }
2493 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002494 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002495 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002496 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002497 PUSH(v);
2498 }
2499 why = WHY_NOT;
2500 JUMPTO(b->b_handler);
2501 break;
2502 }
2503 } /* unwind stack */
2504
2505 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002506
Guido van Rossum374a9221991-04-04 10:40:29 +00002507 if (why != WHY_NOT)
2508 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002509 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002510
Guido van Rossum374a9221991-04-04 10:40:29 +00002511 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002512
Tim Peters8a5c3c72004-04-05 19:36:21 +00002513 assert(why != WHY_YIELD);
2514 /* Pop remaining stack entries. */
2515 while (!EMPTY()) {
2516 v = POP();
2517 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002518 }
2519
Tim Peters8a5c3c72004-04-05 19:36:21 +00002520 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002521 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002522
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002523fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002524 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002525 if (tstate->c_tracefunc) {
2526 if (why == WHY_RETURN || why == WHY_YIELD) {
2527 if (call_trace(tstate->c_tracefunc,
2528 tstate->c_traceobj, f,
2529 PyTrace_RETURN, retval)) {
2530 Py_XDECREF(retval);
2531 retval = NULL;
2532 why = WHY_EXCEPTION;
2533 }
2534 }
2535 else if (why == WHY_EXCEPTION) {
2536 call_trace_protected(tstate->c_tracefunc,
2537 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002538 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002539 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002540 }
Fred Drake8f51f542001-10-04 14:48:42 +00002541 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002542 if (why == WHY_EXCEPTION)
2543 call_trace_protected(tstate->c_profilefunc,
2544 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002545 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002546 else if (call_trace(tstate->c_profilefunc,
2547 tstate->c_profileobj, f,
2548 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002549 Py_XDECREF(retval);
2550 retval = NULL;
2551 why = WHY_EXCEPTION;
2552 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002553 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002554 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002555
Guido van Rossuma027efa1997-05-05 20:56:21 +00002556 reset_exc_info(tstate);
2557
Tim Peters5ca576e2001-06-18 22:08:13 +00002558 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002559 exit_eval_frame:
2560 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002561 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002562
Guido van Rossum96a42c81992-01-12 02:29:51 +00002563 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002564}
2565
Guido van Rossumc2e20742006-02-27 22:32:47 +00002566/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002567 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002568 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002569
Tim Peters6d6c1a32001-08-02 04:15:00 +00002570PyObject *
2571PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002572 PyObject **args, int argcount, PyObject **kws, int kwcount,
2573 PyObject **defs, int defcount, PyObject *closure)
2574{
2575 register PyFrameObject *f;
2576 register PyObject *retval = NULL;
2577 register PyObject **fastlocals, **freevars;
2578 PyThreadState *tstate = PyThreadState_GET();
2579 PyObject *x, *u;
2580
2581 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002582 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002583 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002584 return NULL;
2585 }
2586
Jeremy Hylton985eba52003-02-05 23:13:00 +00002587 assert(globals != NULL);
2588 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002589 if (f == NULL)
2590 return NULL;
2591
2592 fastlocals = f->f_localsplus;
2593 freevars = f->f_localsplus + f->f_nlocals;
2594
2595 if (co->co_argcount > 0 ||
2596 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2597 int i;
2598 int n = argcount;
2599 PyObject *kwdict = NULL;
2600 if (co->co_flags & CO_VARKEYWORDS) {
2601 kwdict = PyDict_New();
2602 if (kwdict == NULL)
2603 goto fail;
2604 i = co->co_argcount;
2605 if (co->co_flags & CO_VARARGS)
2606 i++;
2607 SETLOCAL(i, kwdict);
2608 }
2609 if (argcount > co->co_argcount) {
2610 if (!(co->co_flags & CO_VARARGS)) {
2611 PyErr_Format(PyExc_TypeError,
2612 "%.200s() takes %s %d "
2613 "%sargument%s (%d given)",
2614 PyString_AsString(co->co_name),
2615 defcount ? "at most" : "exactly",
2616 co->co_argcount,
2617 kwcount ? "non-keyword " : "",
2618 co->co_argcount == 1 ? "" : "s",
2619 argcount);
2620 goto fail;
2621 }
2622 n = co->co_argcount;
2623 }
2624 for (i = 0; i < n; i++) {
2625 x = args[i];
2626 Py_INCREF(x);
2627 SETLOCAL(i, x);
2628 }
2629 if (co->co_flags & CO_VARARGS) {
2630 u = PyTuple_New(argcount - n);
2631 if (u == NULL)
2632 goto fail;
2633 SETLOCAL(co->co_argcount, u);
2634 for (i = n; i < argcount; i++) {
2635 x = args[i];
2636 Py_INCREF(x);
2637 PyTuple_SET_ITEM(u, i-n, x);
2638 }
2639 }
2640 for (i = 0; i < kwcount; i++) {
2641 PyObject *keyword = kws[2*i];
2642 PyObject *value = kws[2*i + 1];
2643 int j;
2644 if (keyword == NULL || !PyString_Check(keyword)) {
2645 PyErr_Format(PyExc_TypeError,
2646 "%.200s() keywords must be strings",
2647 PyString_AsString(co->co_name));
2648 goto fail;
2649 }
2650 /* XXX slow -- speed up using dictionary? */
2651 for (j = 0; j < co->co_argcount; j++) {
2652 PyObject *nm = PyTuple_GET_ITEM(
2653 co->co_varnames, j);
2654 int cmp = PyObject_RichCompareBool(
2655 keyword, nm, Py_EQ);
2656 if (cmp > 0)
2657 break;
2658 else if (cmp < 0)
2659 goto fail;
2660 }
2661 /* Check errors from Compare */
2662 if (PyErr_Occurred())
2663 goto fail;
2664 if (j >= co->co_argcount) {
2665 if (kwdict == NULL) {
2666 PyErr_Format(PyExc_TypeError,
2667 "%.200s() got an unexpected "
2668 "keyword argument '%.400s'",
2669 PyString_AsString(co->co_name),
2670 PyString_AsString(keyword));
2671 goto fail;
2672 }
2673 PyDict_SetItem(kwdict, keyword, value);
2674 }
2675 else {
2676 if (GETLOCAL(j) != NULL) {
2677 PyErr_Format(PyExc_TypeError,
2678 "%.200s() got multiple "
2679 "values for keyword "
2680 "argument '%.400s'",
2681 PyString_AsString(co->co_name),
2682 PyString_AsString(keyword));
2683 goto fail;
2684 }
2685 Py_INCREF(value);
2686 SETLOCAL(j, value);
2687 }
2688 }
2689 if (argcount < co->co_argcount) {
2690 int m = co->co_argcount - defcount;
2691 for (i = argcount; i < m; i++) {
2692 if (GETLOCAL(i) == NULL) {
2693 PyErr_Format(PyExc_TypeError,
2694 "%.200s() takes %s %d "
2695 "%sargument%s (%d given)",
2696 PyString_AsString(co->co_name),
2697 ((co->co_flags & CO_VARARGS) ||
2698 defcount) ? "at least"
2699 : "exactly",
2700 m, kwcount ? "non-keyword " : "",
2701 m == 1 ? "" : "s", i);
2702 goto fail;
2703 }
2704 }
2705 if (n > m)
2706 i = n - m;
2707 else
2708 i = 0;
2709 for (; i < defcount; i++) {
2710 if (GETLOCAL(m+i) == NULL) {
2711 PyObject *def = defs[i];
2712 Py_INCREF(def);
2713 SETLOCAL(m+i, def);
2714 }
2715 }
2716 }
2717 }
2718 else {
2719 if (argcount > 0 || kwcount > 0) {
2720 PyErr_Format(PyExc_TypeError,
2721 "%.200s() takes no arguments (%d given)",
2722 PyString_AsString(co->co_name),
2723 argcount + kwcount);
2724 goto fail;
2725 }
2726 }
2727 /* Allocate and initialize storage for cell vars, and copy free
2728 vars into frame. This isn't too efficient right now. */
2729 if (f->f_ncells) {
2730 int i = 0, j = 0, nargs, found;
2731 char *cellname, *argname;
2732 PyObject *c;
2733
2734 nargs = co->co_argcount;
2735 if (co->co_flags & CO_VARARGS)
2736 nargs++;
2737 if (co->co_flags & CO_VARKEYWORDS)
2738 nargs++;
2739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 /* Initialize each cell var, taking into account
2741 cell vars that are initialized from arguments.
2742
2743 Should arrange for the compiler to put cellvars
2744 that are arguments at the beginning of the cellvars
2745 list so that we can march over it more efficiently?
2746 */
2747 for (i = 0; i < f->f_ncells; ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002748 cellname = PyString_AS_STRING(
2749 PyTuple_GET_ITEM(co->co_cellvars, i));
2750 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002752 argname = PyString_AS_STRING(
2753 PyTuple_GET_ITEM(co->co_varnames, j));
2754 if (strcmp(cellname, argname) == 0) {
2755 c = PyCell_New(GETLOCAL(j));
2756 if (c == NULL)
2757 goto fail;
2758 GETLOCAL(f->f_nlocals + i) = c;
2759 found = 1;
2760 break;
2761 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002762 }
2763 if (found == 0) {
2764 c = PyCell_New(NULL);
2765 if (c == NULL)
2766 goto fail;
2767 SETLOCAL(f->f_nlocals + i, c);
2768 }
2769 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002770 }
2771 if (f->f_nfreevars) {
2772 int i;
2773 for (i = 0; i < f->f_nfreevars; ++i) {
2774 PyObject *o = PyTuple_GET_ITEM(closure, i);
2775 Py_INCREF(o);
2776 freevars[f->f_ncells + i] = o;
2777 }
2778 }
2779
Tim Peters5ca576e2001-06-18 22:08:13 +00002780 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002781 /* Don't need to keep the reference to f_back, it will be set
2782 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002783 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002784 f->f_back = NULL;
2785
Jeremy Hylton985eba52003-02-05 23:13:00 +00002786 PCALL(PCALL_GENERATOR);
2787
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002788 /* Create a new generator that owns the ready to run frame
2789 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002790 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002791 }
2792
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002793 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002794
2795 fail: /* Jump here from prelude on failure */
2796
Tim Petersb13680b2001-11-27 23:29:29 +00002797 /* decref'ing the frame can cause __del__ methods to get invoked,
2798 which can call back into Python. While we're done with the
2799 current Python frame (f), the associated C stack is still in use,
2800 so recursion_depth must be boosted for the duration.
2801 */
2802 assert(tstate != NULL);
2803 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002804 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002805 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002806 return retval;
2807}
2808
2809
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002810/* Implementation notes for set_exc_info() and reset_exc_info():
2811
2812- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2813 'exc_traceback'. These always travel together.
2814
2815- tstate->curexc_ZZZ is the "hot" exception that is set by
2816 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2817
2818- Once an exception is caught by an except clause, it is transferred
2819 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2820 can pick it up. This is the primary task of set_exc_info().
2821
2822- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2823
2824 Long ago, when none of this existed, there were just a few globals:
2825 one set corresponding to the "hot" exception, and one set
2826 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2827 globals; they were simply stored as sys.exc_ZZZ. For backwards
2828 compatibility, they still are!) The problem was that in code like
2829 this:
2830
2831 try:
2832 "something that may fail"
2833 except "some exception":
2834 "do something else first"
2835 "print the exception from sys.exc_ZZZ."
2836
2837 if "do something else first" invoked something that raised and caught
2838 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2839 cause of subtle bugs. I fixed this by changing the semantics as
2840 follows:
2841
2842 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2843 *in that frame*.
2844
2845 - But initially, and as long as no exception is caught in a given
2846 frame, sys.exc_ZZZ will hold the last exception caught in the
2847 previous frame (or the frame before that, etc.).
2848
2849 The first bullet fixed the bug in the above example. The second
2850 bullet was for backwards compatibility: it was (and is) common to
2851 have a function that is called when an exception is caught, and to
2852 have that function access the caught exception via sys.exc_ZZZ.
2853 (Example: traceback.print_exc()).
2854
2855 At the same time I fixed the problem that sys.exc_ZZZ weren't
2856 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2857 but that's really a separate improvement.
2858
2859 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2860 variables to what they were before the current frame was called. The
2861 set_exc_info() function saves them on the frame so that
2862 reset_exc_info() can restore them. The invariant is that
2863 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2864 exception (where "catching" an exception applies only to successful
2865 except clauses); and if the current frame ever caught an exception,
2866 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2867 at the start of the current frame.
2868
2869*/
2870
Guido van Rossuma027efa1997-05-05 20:56:21 +00002871static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002872set_exc_info(PyThreadState *tstate,
2873 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002874{
2875 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002876 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002877
Guido van Rossuma027efa1997-05-05 20:56:21 +00002878 frame = tstate->frame;
2879 if (frame->f_exc_type == NULL) {
2880 /* This frame didn't catch an exception before */
2881 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002882 if (tstate->exc_type == NULL) {
2883 Py_INCREF(Py_None);
2884 tstate->exc_type = Py_None;
2885 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002886 tmp_type = frame->f_exc_type;
2887 tmp_value = frame->f_exc_value;
2888 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002889 Py_XINCREF(tstate->exc_type);
2890 Py_XINCREF(tstate->exc_value);
2891 Py_XINCREF(tstate->exc_traceback);
2892 frame->f_exc_type = tstate->exc_type;
2893 frame->f_exc_value = tstate->exc_value;
2894 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002895 Py_XDECREF(tmp_type);
2896 Py_XDECREF(tmp_value);
2897 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002898 }
2899 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002900 tmp_type = tstate->exc_type;
2901 tmp_value = tstate->exc_value;
2902 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002903 Py_XINCREF(type);
2904 Py_XINCREF(value);
2905 Py_XINCREF(tb);
2906 tstate->exc_type = type;
2907 tstate->exc_value = value;
2908 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002909 Py_XDECREF(tmp_type);
2910 Py_XDECREF(tmp_value);
2911 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002912 /* For b/w compatibility */
2913 PySys_SetObject("exc_type", type);
2914 PySys_SetObject("exc_value", value);
2915 PySys_SetObject("exc_traceback", tb);
2916}
2917
2918static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002919reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002920{
2921 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002922 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002923 frame = tstate->frame;
2924 if (frame->f_exc_type != NULL) {
2925 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002926 tmp_type = tstate->exc_type;
2927 tmp_value = tstate->exc_value;
2928 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002929 Py_XINCREF(frame->f_exc_type);
2930 Py_XINCREF(frame->f_exc_value);
2931 Py_XINCREF(frame->f_exc_traceback);
2932 tstate->exc_type = frame->f_exc_type;
2933 tstate->exc_value = frame->f_exc_value;
2934 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002935 Py_XDECREF(tmp_type);
2936 Py_XDECREF(tmp_value);
2937 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002938 /* For b/w compatibility */
2939 PySys_SetObject("exc_type", frame->f_exc_type);
2940 PySys_SetObject("exc_value", frame->f_exc_value);
2941 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2942 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002943 tmp_type = frame->f_exc_type;
2944 tmp_value = frame->f_exc_value;
2945 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002946 frame->f_exc_type = NULL;
2947 frame->f_exc_value = NULL;
2948 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002949 Py_XDECREF(tmp_type);
2950 Py_XDECREF(tmp_value);
2951 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002952}
2953
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002954/* Logic for the raise statement (too complicated for inlining).
2955 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002956static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002957do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002958{
Guido van Rossumd295f121998-04-09 21:39:57 +00002959 if (type == NULL) {
2960 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002961 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002962 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2963 value = tstate->exc_value;
2964 tb = tstate->exc_traceback;
2965 Py_XINCREF(type);
2966 Py_XINCREF(value);
2967 Py_XINCREF(tb);
2968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002969
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002970 /* We support the following forms of raise:
2971 raise <class>, <classinstance>
2972 raise <class>, <argument tuple>
2973 raise <class>, None
2974 raise <class>, <argument>
2975 raise <classinstance>, None
2976 raise <string>, <object>
2977 raise <string>, None
2978
2979 An omitted second argument is the same as None.
2980
2981 In addition, raise <tuple>, <anything> is the same as
2982 raising the tuple's first item (and it better have one!);
2983 this rule is applied recursively.
2984
2985 Finally, an optional third argument can be supplied, which
2986 gives the traceback to be substituted (useful when
2987 re-raising an exception after examining it). */
2988
2989 /* First, check the traceback argument, replacing None with
2990 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002991 if (tb == Py_None) {
2992 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002993 tb = NULL;
2994 }
2995 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002996 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002997 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002998 goto raise_error;
2999 }
3000
3001 /* Next, replace a missing value with None */
3002 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003003 value = Py_None;
3004 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003005 }
3006
3007 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003008 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3009 PyObject *tmp = type;
3010 type = PyTuple_GET_ITEM(type, 0);
3011 Py_INCREF(type);
3012 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003013 }
3014
Guido van Rossum45aecf42006-03-15 04:58:47 +00003015 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003016 PyErr_NormalizeException(&type, &value, &tb);
3017
Brett Cannonbf364092006-03-01 04:25:17 +00003018 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003019 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003020 if (value != Py_None) {
3021 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003022 "instance exception may not have a separate value");
3023 goto raise_error;
3024 }
3025 else {
3026 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003027 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003028 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003029 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003031 }
3032 }
3033 else {
3034 /* Not something you can raise. You get an exception
3035 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003036 PyErr_SetString(PyExc_TypeError,
3037 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003038 goto raise_error;
3039 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003040 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003041 if (tb == NULL)
3042 return WHY_EXCEPTION;
3043 else
3044 return WHY_RERAISE;
3045 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003046 Py_XDECREF(value);
3047 Py_XDECREF(type);
3048 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003049 return WHY_EXCEPTION;
3050}
3051
Tim Petersd6d010b2001-06-21 02:49:55 +00003052/* Iterate v argcnt times and store the results on the stack (via decreasing
3053 sp). Return 1 for success, 0 if error. */
3054
Barry Warsawe42b18f1997-08-25 22:13:04 +00003055static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003056unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003057{
Tim Petersd6d010b2001-06-21 02:49:55 +00003058 int i = 0;
3059 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003060 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003061
Tim Petersd6d010b2001-06-21 02:49:55 +00003062 assert(v != NULL);
3063
3064 it = PyObject_GetIter(v);
3065 if (it == NULL)
3066 goto Error;
3067
3068 for (; i < argcnt; i++) {
3069 w = PyIter_Next(it);
3070 if (w == NULL) {
3071 /* Iterator done, via error or exhaustion. */
3072 if (!PyErr_Occurred()) {
3073 PyErr_Format(PyExc_ValueError,
3074 "need more than %d value%s to unpack",
3075 i, i == 1 ? "" : "s");
3076 }
3077 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003078 }
3079 *--sp = w;
3080 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003081
3082 /* We better have exhausted the iterator now. */
3083 w = PyIter_Next(it);
3084 if (w == NULL) {
3085 if (PyErr_Occurred())
3086 goto Error;
3087 Py_DECREF(it);
3088 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003089 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003090 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003091 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003092 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003093Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003094 for (; i > 0; i--, sp++)
3095 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003096 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003097 return 0;
3098}
3099
3100
Guido van Rossum96a42c81992-01-12 02:29:51 +00003101#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003102static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003103prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003104{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003105 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003106 if (PyObject_Print(v, stdout, 0) != 0)
3107 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003108 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003109 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003110}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003111#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003112
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003113static void
Fred Drake5755ce62001-06-27 19:19:46 +00003114call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003115{
Guido van Rossumb209a111997-04-29 18:18:01 +00003116 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003117 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003118 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003119 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003120 value = Py_None;
3121 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003122 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003123 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003124 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003125 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003126 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003127 }
Fred Drake5755ce62001-06-27 19:19:46 +00003128 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003129 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003130 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003131 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003132 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003133 Py_XDECREF(type);
3134 Py_XDECREF(value);
3135 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003136 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003137}
3138
Fred Drake4ec5d562001-10-04 19:26:43 +00003139static void
3140call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003141 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003142{
3143 PyObject *type, *value, *traceback;
3144 int err;
3145 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003146 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003147 if (err == 0)
3148 PyErr_Restore(type, value, traceback);
3149 else {
3150 Py_XDECREF(type);
3151 Py_XDECREF(value);
3152 Py_XDECREF(traceback);
3153 }
3154}
3155
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003156static int
Fred Drake5755ce62001-06-27 19:19:46 +00003157call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3158 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003159{
Fred Drake5755ce62001-06-27 19:19:46 +00003160 register PyThreadState *tstate = frame->f_tstate;
3161 int result;
3162 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003163 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003164 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003165 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003166 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003167 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3168 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003169 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003170 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003171}
3172
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003173PyObject *
3174_PyEval_CallTracing(PyObject *func, PyObject *args)
3175{
3176 PyFrameObject *frame = PyEval_GetFrame();
3177 PyThreadState *tstate = frame->f_tstate;
3178 int save_tracing = tstate->tracing;
3179 int save_use_tracing = tstate->use_tracing;
3180 PyObject *result;
3181
3182 tstate->tracing = 0;
3183 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3184 || (tstate->c_profilefunc != NULL));
3185 result = PyObject_Call(func, args, NULL);
3186 tstate->tracing = save_tracing;
3187 tstate->use_tracing = save_use_tracing;
3188 return result;
3189}
3190
Michael W. Hudson006c7522002-11-08 13:08:46 +00003191static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003192maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003193 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3194 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003195{
3196 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003197
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003198 In a nutshell, we use the co_lnotab field of the code object
3199 to tell when execution has moved onto a different line.
3200
3201 As mentioned above, the basic idea is so set things up so
3202 that
3203
3204 *instr_lb <= frame->f_lasti < *instr_ub
3205
3206 is true so long as execution does not change lines.
3207
3208 This is all fairly simple. Digging the information out of
3209 co_lnotab takes some work, but is conceptually clear.
3210
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003211 Somewhat harder to explain is why we don't *always* call the
3212 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003213
3214 Consider this code:
3215
3216 1: def f(a):
3217 2: if a:
3218 3: print 1
3219 4: else:
3220 5: print 2
3221
3222 which compiles to this:
3223
3224 2 0 LOAD_FAST 0 (a)
3225 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003226 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003227
3228 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003229 10 PRINT_ITEM
3230 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003231 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003232 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003233
3234 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003235 19 PRINT_ITEM
3236 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003237 >> 21 LOAD_CONST 0 (None)
3238 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003239
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003240 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003241 15 and the co_lnotab will claim that execution has moved to
3242 line 3. This is at best misleading. In this case we could
3243 associate the POP_TOP with line 4, but that doesn't make
3244 sense in all cases (I think).
3245
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003246 What we do is only call the line trace function if the co_lnotab
3247 indicates we have jumped to the *start* of a line, i.e. if the
3248 current instruction offset matches the offset given for the
3249 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003250
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003251 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003252 Execution will jump from instruction offset 12 to offset 21.
3253 Then the co_lnotab would imply that execution has moved to line
3254 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003255
3256 Why do we set f_lineno when tracing? Well, consider the code
3257 above when 'a' is true. If stepping through this with 'n' in
3258 pdb, you would stop at line 1 with a "call" type event, then
3259 line events on lines 2 and 3, then a "return" type event -- but
3260 you would be shown line 5 during this event. This is a change
3261 from the behaviour in 2.2 and before, and I've found it
3262 confusing in practice. By setting and using f_lineno when
3263 tracing, one can report a line number different from that
3264 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003265 */
3266
Michael W. Hudson006c7522002-11-08 13:08:46 +00003267 int result = 0;
3268
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003269 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003270 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003271 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003272 unsigned char* p;
3273
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003274 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3275 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003276
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003277 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003278 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003279
3280 /* possible optimization: if f->f_lasti == instr_ub
3281 (likely to be a common case) then we already know
3282 instr_lb -- if we stored the matching value of p
3283 somwhere we could skip the first while loop. */
3284
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003285 /* see comments in compile.c for the description of
3286 co_lnotab. A point to remember: increments to p
3287 should come in pairs -- although we don't care about
3288 the line increments here, treating them as byte
3289 increments gets confusing, to say the least. */
3290
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003291 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003292 if (addr + *p > frame->f_lasti)
3293 break;
3294 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003295 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003296 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003297 --size;
3298 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003299
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003300 if (addr == frame->f_lasti) {
3301 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003302 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003303 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003304 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003305
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003306 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003307 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003308 addr += *p++;
3309 if (*p++)
3310 break;
3311 }
3312 *instr_ub = addr;
3313 }
3314 else {
3315 *instr_ub = INT_MAX;
3316 }
3317 }
Armin Rigobf57a142004-03-22 19:24:58 +00003318 else if (frame->f_lasti <= *instr_prev) {
3319 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003320 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003321 PyTrace_LINE, Py_None);
3322 }
3323 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003324 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003325}
3326
Fred Drake5755ce62001-06-27 19:19:46 +00003327void
3328PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003329{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003330 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003331 PyObject *temp = tstate->c_profileobj;
3332 Py_XINCREF(arg);
3333 tstate->c_profilefunc = NULL;
3334 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003335 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003336 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003337 Py_XDECREF(temp);
3338 tstate->c_profilefunc = func;
3339 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003340 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003341 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003342}
3343
3344void
3345PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3346{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003347 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003348 PyObject *temp = tstate->c_traceobj;
3349 Py_XINCREF(arg);
3350 tstate->c_tracefunc = NULL;
3351 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003352 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003353 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003354 Py_XDECREF(temp);
3355 tstate->c_tracefunc = func;
3356 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003357 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003358 tstate->use_tracing = ((func != NULL)
3359 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003360}
3361
Guido van Rossumb209a111997-04-29 18:18:01 +00003362PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003364{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003365 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003366 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003367 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003368 else
3369 return current_frame->f_builtins;
3370}
3371
Guido van Rossumb209a111997-04-29 18:18:01 +00003372PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003373PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003374{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003375 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003376 if (current_frame == NULL)
3377 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003378 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003379 return current_frame->f_locals;
3380}
3381
Guido van Rossumb209a111997-04-29 18:18:01 +00003382PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003383PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003384{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003385 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003386 if (current_frame == NULL)
3387 return NULL;
3388 else
3389 return current_frame->f_globals;
3390}
3391
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003392PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003393PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003394{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003395 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003396 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003397}
3398
Guido van Rossum6135a871995-01-09 17:53:26 +00003399int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003400PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003401{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003402 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003403 return current_frame == NULL ? 0 : current_frame->f_restricted;
3404}
3405
Guido van Rossumbe270261997-05-22 22:26:18 +00003406int
Tim Peters5ba58662001-07-16 02:29:45 +00003407PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003408{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003409 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003410 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003411
3412 if (current_frame != NULL) {
3413 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003414 const int compilerflags = codeflags & PyCF_MASK;
3415 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003416 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003417 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003418 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003419#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003420 if (codeflags & CO_GENERATOR_ALLOWED) {
3421 result = 1;
3422 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3423 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003424#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003425 }
3426 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003427}
3428
3429int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003430Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003431{
Guido van Rossumb209a111997-04-29 18:18:01 +00003432 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003433 if (f == NULL)
3434 return 0;
3435 if (!PyFile_SoftSpace(f, 0))
3436 return 0;
3437 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003438}
3439
Guido van Rossum3f5da241990-12-20 15:06:42 +00003440
Guido van Rossum681d79a1995-07-18 14:51:37 +00003441/* External interface to call any callable object.
3442 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003443
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003444#undef PyEval_CallObject
3445/* for backward compatibility: export this interface */
3446
Guido van Rossumb209a111997-04-29 18:18:01 +00003447PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003448PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003449{
Guido van Rossumb209a111997-04-29 18:18:01 +00003450 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003451}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003452#define PyEval_CallObject(func,arg) \
3453 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003454
Guido van Rossumb209a111997-04-29 18:18:01 +00003455PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003456PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003457{
Jeremy Hylton52820442001-01-03 23:52:36 +00003458 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003459
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003460 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003461 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003462 if (arg == NULL)
3463 return NULL;
3464 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003465 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003466 PyErr_SetString(PyExc_TypeError,
3467 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003468 return NULL;
3469 }
3470 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003471 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003472
Guido van Rossumb209a111997-04-29 18:18:01 +00003473 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003474 PyErr_SetString(PyExc_TypeError,
3475 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003476 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003477 return NULL;
3478 }
3479
Tim Peters6d6c1a32001-08-02 04:15:00 +00003480 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003481 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003482 return result;
3483}
3484
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003485const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003486PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003487{
3488 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003489 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003490 else if (PyFunction_Check(func))
3491 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3492 else if (PyCFunction_Check(func))
3493 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3494 else if (PyClass_Check(func))
3495 return PyString_AsString(((PyClassObject*)func)->cl_name);
3496 else if (PyInstance_Check(func)) {
3497 return PyString_AsString(
3498 ((PyInstanceObject*)func)->in_class->cl_name);
3499 } else {
3500 return func->ob_type->tp_name;
3501 }
3502}
3503
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003504const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003505PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003506{
3507 if (PyMethod_Check(func))
3508 return "()";
3509 else if (PyFunction_Check(func))
3510 return "()";
3511 else if (PyCFunction_Check(func))
3512 return "()";
3513 else if (PyClass_Check(func))
3514 return " constructor";
3515 else if (PyInstance_Check(func)) {
3516 return " instance";
3517 } else {
3518 return " object";
3519 }
3520}
3521
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003522static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003523err_args(PyObject *func, int flags, int nargs)
3524{
3525 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003526 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003527 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003528 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003529 nargs);
3530 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003531 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003532 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003533 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003534 nargs);
3535}
3536
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003537#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003538if (tstate->use_tracing && tstate->c_profilefunc) { \
3539 if (call_trace(tstate->c_profilefunc, \
3540 tstate->c_profileobj, \
3541 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003542 func)) { \
3543 x = NULL; \
3544 } \
3545 else { \
3546 x = call; \
3547 if (tstate->c_profilefunc != NULL) { \
3548 if (x == NULL) { \
3549 call_trace_protected(tstate->c_profilefunc, \
3550 tstate->c_profileobj, \
3551 tstate->frame, PyTrace_C_EXCEPTION, \
3552 func); \
3553 /* XXX should pass (type, value, tb) */ \
3554 } else { \
3555 if (call_trace(tstate->c_profilefunc, \
3556 tstate->c_profileobj, \
3557 tstate->frame, PyTrace_C_RETURN, \
3558 func)) { \
3559 Py_DECREF(x); \
3560 x = NULL; \
3561 } \
3562 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003563 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003564 } \
3565} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003566 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003567 }
3568
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003570call_function(PyObject ***pp_stack, int oparg
3571#ifdef WITH_TSC
3572 , uint64* pintr0, uint64* pintr1
3573#endif
3574 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003575{
3576 int na = oparg & 0xff;
3577 int nk = (oparg>>8) & 0xff;
3578 int n = na + 2 * nk;
3579 PyObject **pfunc = (*pp_stack) - n - 1;
3580 PyObject *func = *pfunc;
3581 PyObject *x, *w;
3582
Jeremy Hylton985eba52003-02-05 23:13:00 +00003583 /* Always dispatch PyCFunction first, because these are
3584 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003585 */
3586 if (PyCFunction_Check(func) && nk == 0) {
3587 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003588 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003589
3590 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003591 if (flags & (METH_NOARGS | METH_O)) {
3592 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3593 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003594 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003595 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003596 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003597 else if (flags & METH_O && na == 1) {
3598 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003599 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003600 Py_DECREF(arg);
3601 }
3602 else {
3603 err_args(func, flags, na);
3604 x = NULL;
3605 }
3606 }
3607 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003608 PyObject *callargs;
3609 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003610 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003611 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003612 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003613 Py_XDECREF(callargs);
3614 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003615 } else {
3616 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3617 /* optimize access to bound methods */
3618 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003619 PCALL(PCALL_METHOD);
3620 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003621 Py_INCREF(self);
3622 func = PyMethod_GET_FUNCTION(func);
3623 Py_INCREF(func);
3624 Py_DECREF(*pfunc);
3625 *pfunc = self;
3626 na++;
3627 n++;
3628 } else
3629 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003630 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003631 if (PyFunction_Check(func))
3632 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003633 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003634 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003635 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003636 Py_DECREF(func);
3637 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003638
Thomas Wouters03ca23d2006-02-10 22:51:45 +00003639 /* Clear the stack of the function object and the arguments,
Thomas Wouters7f597322006-03-01 05:32:33 +00003640 in case they weren't consumed already.
3641 XXX(twouters) when are they not consumed already?
3642 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003643 while ((*pp_stack) > pfunc) {
3644 w = EXT_POP(*pp_stack);
3645 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003646 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003647 }
3648 return x;
3649}
3650
Jeremy Hylton192690e2002-08-16 18:36:11 +00003651/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003652 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003653 For the simplest case -- a function that takes only positional
3654 arguments and is called with only positional arguments -- it
3655 inlines the most primitive frame setup code from
3656 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3657 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003658*/
3659
3660static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003661fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003662{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003663 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003664 PyObject *globals = PyFunction_GET_GLOBALS(func);
3665 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3666 PyObject **d = NULL;
3667 int nd = 0;
3668
Jeremy Hylton985eba52003-02-05 23:13:00 +00003669 PCALL(PCALL_FUNCTION);
3670 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003671 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003672 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3673 PyFrameObject *f;
3674 PyObject *retval = NULL;
3675 PyThreadState *tstate = PyThreadState_GET();
3676 PyObject **fastlocals, **stack;
3677 int i;
3678
3679 PCALL(PCALL_FASTER_FUNCTION);
3680 assert(globals != NULL);
3681 /* XXX Perhaps we should create a specialized
3682 PyFrame_New() that doesn't take locals, but does
3683 take builtins without sanity checking them.
3684 */
3685 f = PyFrame_New(tstate, co, globals, NULL);
3686 if (f == NULL)
3687 return NULL;
3688
3689 fastlocals = f->f_localsplus;
3690 stack = (*pp_stack) - n;
3691
3692 for (i = 0; i < n; i++) {
3693 Py_INCREF(*stack);
3694 fastlocals[i] = *stack++;
3695 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003696 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003697 assert(tstate != NULL);
3698 ++tstate->recursion_depth;
3699 Py_DECREF(f);
3700 --tstate->recursion_depth;
3701 return retval;
3702 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003703 if (argdefs != NULL) {
3704 d = &PyTuple_GET_ITEM(argdefs, 0);
3705 nd = ((PyTupleObject *)argdefs)->ob_size;
3706 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003707 return PyEval_EvalCodeEx(co, globals,
3708 (PyObject *)NULL, (*pp_stack)-n, na,
3709 (*pp_stack)-2*nk, nk, d, nd,
3710 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003711}
3712
3713static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003714update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3715 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003716{
3717 PyObject *kwdict = NULL;
3718 if (orig_kwdict == NULL)
3719 kwdict = PyDict_New();
3720 else {
3721 kwdict = PyDict_Copy(orig_kwdict);
3722 Py_DECREF(orig_kwdict);
3723 }
3724 if (kwdict == NULL)
3725 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003726 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 int err;
3728 PyObject *value = EXT_POP(*pp_stack);
3729 PyObject *key = EXT_POP(*pp_stack);
3730 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003731 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003732 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003733 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003734 PyEval_GetFuncName(func),
3735 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003736 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003737 Py_DECREF(key);
3738 Py_DECREF(value);
3739 Py_DECREF(kwdict);
3740 return NULL;
3741 }
3742 err = PyDict_SetItem(kwdict, key, value);
3743 Py_DECREF(key);
3744 Py_DECREF(value);
3745 if (err) {
3746 Py_DECREF(kwdict);
3747 return NULL;
3748 }
3749 }
3750 return kwdict;
3751}
3752
3753static PyObject *
3754update_star_args(int nstack, int nstar, PyObject *stararg,
3755 PyObject ***pp_stack)
3756{
3757 PyObject *callargs, *w;
3758
3759 callargs = PyTuple_New(nstack + nstar);
3760 if (callargs == NULL) {
3761 return NULL;
3762 }
3763 if (nstar) {
3764 int i;
3765 for (i = 0; i < nstar; i++) {
3766 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3767 Py_INCREF(a);
3768 PyTuple_SET_ITEM(callargs, nstack + i, a);
3769 }
3770 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003771 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003772 w = EXT_POP(*pp_stack);
3773 PyTuple_SET_ITEM(callargs, nstack, w);
3774 }
3775 return callargs;
3776}
3777
3778static PyObject *
3779load_args(PyObject ***pp_stack, int na)
3780{
3781 PyObject *args = PyTuple_New(na);
3782 PyObject *w;
3783
3784 if (args == NULL)
3785 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003786 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003787 w = EXT_POP(*pp_stack);
3788 PyTuple_SET_ITEM(args, na, w);
3789 }
3790 return args;
3791}
3792
3793static PyObject *
3794do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3795{
3796 PyObject *callargs = NULL;
3797 PyObject *kwdict = NULL;
3798 PyObject *result = NULL;
3799
3800 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003801 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003802 if (kwdict == NULL)
3803 goto call_fail;
3804 }
3805 callargs = load_args(pp_stack, na);
3806 if (callargs == NULL)
3807 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003808#ifdef CALL_PROFILE
3809 /* At this point, we have to look at the type of func to
3810 update the call stats properly. Do it here so as to avoid
3811 exposing the call stats machinery outside ceval.c
3812 */
3813 if (PyFunction_Check(func))
3814 PCALL(PCALL_FUNCTION);
3815 else if (PyMethod_Check(func))
3816 PCALL(PCALL_METHOD);
3817 else if (PyType_Check(func))
3818 PCALL(PCALL_TYPE);
3819 else
3820 PCALL(PCALL_OTHER);
3821#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003822 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003823 call_fail:
3824 Py_XDECREF(callargs);
3825 Py_XDECREF(kwdict);
3826 return result;
3827}
3828
3829static PyObject *
3830ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3831{
3832 int nstar = 0;
3833 PyObject *callargs = NULL;
3834 PyObject *stararg = NULL;
3835 PyObject *kwdict = NULL;
3836 PyObject *result = NULL;
3837
3838 if (flags & CALL_FLAG_KW) {
3839 kwdict = EXT_POP(*pp_stack);
3840 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003841 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003842 "%s%s argument after ** "
3843 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003844 PyEval_GetFuncName(func),
3845 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003846 goto ext_call_fail;
3847 }
3848 }
3849 if (flags & CALL_FLAG_VAR) {
3850 stararg = EXT_POP(*pp_stack);
3851 if (!PyTuple_Check(stararg)) {
3852 PyObject *t = NULL;
3853 t = PySequence_Tuple(stararg);
3854 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003855 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3856 PyErr_Format(PyExc_TypeError,
3857 "%s%s argument after * "
3858 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003859 PyEval_GetFuncName(func),
3860 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003861 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003862 goto ext_call_fail;
3863 }
3864 Py_DECREF(stararg);
3865 stararg = t;
3866 }
3867 nstar = PyTuple_GET_SIZE(stararg);
3868 }
3869 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003870 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003871 if (kwdict == NULL)
3872 goto ext_call_fail;
3873 }
3874 callargs = update_star_args(na, nstar, stararg, pp_stack);
3875 if (callargs == NULL)
3876 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003877#ifdef CALL_PROFILE
3878 /* At this point, we have to look at the type of func to
3879 update the call stats properly. Do it here so as to avoid
3880 exposing the call stats machinery outside ceval.c
3881 */
3882 if (PyFunction_Check(func))
3883 PCALL(PCALL_FUNCTION);
3884 else if (PyMethod_Check(func))
3885 PCALL(PCALL_METHOD);
3886 else if (PyType_Check(func))
3887 PCALL(PCALL_TYPE);
3888 else
3889 PCALL(PCALL_OTHER);
3890#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003891 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003892 ext_call_fail:
3893 Py_XDECREF(callargs);
3894 Py_XDECREF(kwdict);
3895 Py_XDECREF(stararg);
3896 return result;
3897}
3898
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003899/* Extract a slice index from a PyInt or PyLong or an object with the
3900 nb_index slot defined, and store in *pi.
3901 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3902 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003903 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003904*/
Tim Petersb5196382001-12-16 19:44:20 +00003905/* Note: If v is NULL, return success without storing into *pi. This
3906 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3907 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003908*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003909int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003910_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003911{
Tim Petersb5196382001-12-16 19:44:20 +00003912 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003913 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003914 if (PyInt_Check(v)) {
3915 x = PyInt_AsLong(v);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003916 }
3917 else if (v->ob_type->tp_as_number &&
3918 PyType_HasFeature(v->ob_type, Py_TPFLAGS_HAVE_INDEX)
3919 && v->ob_type->tp_as_number->nb_index) {
3920 x = v->ob_type->tp_as_number->nb_index(v);
3921 if (x == -1 && PyErr_Occurred())
3922 return 0;
3923 }
3924 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003925 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003926 "slice indices must be integers or "
3927 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003928 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003930 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003931 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003932 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003933}
3934
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003935#undef ISINDEX
3936#define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \
3937 ((x)->ob_type->tp_as_number && \
3938 PyType_HasFeature((x)->ob_type, Py_TPFLAGS_HAVE_INDEX) \
3939 && (x)->ob_type->tp_as_number->nb_index))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003940
Guido van Rossumb209a111997-04-29 18:18:01 +00003941static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003942apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003943{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003944 PyTypeObject *tp = u->ob_type;
3945 PySequenceMethods *sq = tp->tp_as_sequence;
3946
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003947 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003948 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003949 if (!_PyEval_SliceIndex(v, &ilow))
3950 return NULL;
3951 if (!_PyEval_SliceIndex(w, &ihigh))
3952 return NULL;
3953 return PySequence_GetSlice(u, ilow, ihigh);
3954 }
3955 else {
3956 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003957 if (slice != NULL) {
3958 PyObject *res = PyObject_GetItem(u, slice);
3959 Py_DECREF(slice);
3960 return res;
3961 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003962 else
3963 return NULL;
3964 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003965}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003966
3967static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003968assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3969 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003970{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003971 PyTypeObject *tp = u->ob_type;
3972 PySequenceMethods *sq = tp->tp_as_sequence;
3973
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003974 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003975 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003976 if (!_PyEval_SliceIndex(v, &ilow))
3977 return -1;
3978 if (!_PyEval_SliceIndex(w, &ihigh))
3979 return -1;
3980 if (x == NULL)
3981 return PySequence_DelSlice(u, ilow, ihigh);
3982 else
3983 return PySequence_SetSlice(u, ilow, ihigh, x);
3984 }
3985 else {
3986 PyObject *slice = PySlice_New(v, w, NULL);
3987 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003988 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003989 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003990 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003991 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003992 res = PyObject_DelItem(u, slice);
3993 Py_DECREF(slice);
3994 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003995 }
3996 else
3997 return -1;
3998 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003999}
4000
Guido van Rossumb209a111997-04-29 18:18:01 +00004001static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004002cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004003{
Guido van Rossumac7be682001-01-17 15:42:30 +00004004 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004005 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004006 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004007 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004008 break;
4009 case PyCmp_IS_NOT:
4010 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004011 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004012 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004013 res = PySequence_Contains(w, v);
4014 if (res < 0)
4015 return NULL;
4016 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004017 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004018 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004019 if (res < 0)
4020 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004021 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004022 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004023 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004024 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004025 break;
4026 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004027 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004028 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004029 v = res ? Py_True : Py_False;
4030 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004031 return v;
4032}
4033
Thomas Wouters52152252000-08-17 22:55:00 +00004034static PyObject *
4035import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004036{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004037 PyObject *x;
4038
4039 x = PyObject_GetAttr(v, name);
4040 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004041 PyErr_Format(PyExc_ImportError,
4042 "cannot import name %.230s",
4043 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004044 }
Thomas Wouters52152252000-08-17 22:55:00 +00004045 return x;
4046}
Guido van Rossumac7be682001-01-17 15:42:30 +00004047
Thomas Wouters52152252000-08-17 22:55:00 +00004048static int
4049import_all_from(PyObject *locals, PyObject *v)
4050{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004051 PyObject *all = PyObject_GetAttrString(v, "__all__");
4052 PyObject *dict, *name, *value;
4053 int skip_leading_underscores = 0;
4054 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004055
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004056 if (all == NULL) {
4057 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4058 return -1; /* Unexpected error */
4059 PyErr_Clear();
4060 dict = PyObject_GetAttrString(v, "__dict__");
4061 if (dict == NULL) {
4062 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4063 return -1;
4064 PyErr_SetString(PyExc_ImportError,
4065 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004066 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004067 }
4068 all = PyMapping_Keys(dict);
4069 Py_DECREF(dict);
4070 if (all == NULL)
4071 return -1;
4072 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004073 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004074
4075 for (pos = 0, err = 0; ; pos++) {
4076 name = PySequence_GetItem(all, pos);
4077 if (name == NULL) {
4078 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4079 err = -1;
4080 else
4081 PyErr_Clear();
4082 break;
4083 }
4084 if (skip_leading_underscores &&
4085 PyString_Check(name) &&
4086 PyString_AS_STRING(name)[0] == '_')
4087 {
4088 Py_DECREF(name);
4089 continue;
4090 }
4091 value = PyObject_GetAttr(v, name);
4092 if (value == NULL)
4093 err = -1;
4094 else
4095 err = PyDict_SetItem(locals, name, value);
4096 Py_DECREF(name);
4097 Py_XDECREF(value);
4098 if (err != 0)
4099 break;
4100 }
4101 Py_DECREF(all);
4102 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004103}
4104
Guido van Rossumb209a111997-04-29 18:18:01 +00004105static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004106build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004107{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004108 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004109
4110 if (PyDict_Check(methods))
4111 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004112 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004113 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004114 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4115 base = PyTuple_GET_ITEM(bases, 0);
4116 metaclass = PyObject_GetAttrString(base, "__class__");
4117 if (metaclass == NULL) {
4118 PyErr_Clear();
4119 metaclass = (PyObject *)base->ob_type;
4120 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004121 }
4122 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004123 else {
4124 PyObject *g = PyEval_GetGlobals();
4125 if (g != NULL && PyDict_Check(g))
4126 metaclass = PyDict_GetItemString(g, "__metaclass__");
4127 if (metaclass == NULL)
Guido van Rossum45aecf42006-03-15 04:58:47 +00004128 metaclass = (PyObject *) &PyType_Type;
Guido van Rossum7851eea2001-09-12 19:19:18 +00004129 Py_INCREF(metaclass);
4130 }
4131 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4132 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004133 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4134 /* A type error here likely means that the user passed
4135 in a base that was not a class (such the random module
4136 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004137 by augmenting the error message with more information.*/
4138
4139 PyObject *ptype, *pvalue, *ptraceback;
4140
4141 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4142 if (PyString_Check(pvalue)) {
4143 PyObject *newmsg;
4144 newmsg = PyString_FromFormat(
4145 "Error when calling the metaclass bases\n %s",
4146 PyString_AS_STRING(pvalue));
4147 if (newmsg != NULL) {
4148 Py_DECREF(pvalue);
4149 pvalue = newmsg;
4150 }
4151 }
4152 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004153 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004154 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004155}
4156
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004157static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004158exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4159 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004160{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004161 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004162 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004163 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004164
Guido van Rossumb209a111997-04-29 18:18:01 +00004165 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4166 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004167 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004168 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004169 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004170 locals = PyTuple_GetItem(prog, 2);
4171 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004173 if (globals == Py_None) {
4174 globals = PyEval_GetGlobals();
4175 if (locals == Py_None) {
4176 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004177 plain = 1;
4178 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004179 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004180 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004181 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004182 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004183 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004184 !PyCode_Check(prog) &&
4185 !PyFile_Check(prog)) {
4186 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004187 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004188 return -1;
4189 }
Fred Drake661ea262000-10-24 19:57:45 +00004190 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004191 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004192 "exec: arg 2 must be a dictionary or None");
4193 return -1;
4194 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004195 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004196 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004197 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004198 return -1;
4199 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004200 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004201 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004202 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004203 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4204 PyErr_SetString(PyExc_TypeError,
4205 "code object passed to exec may not contain free variables");
4206 return -1;
4207 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004208 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004209 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004210 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004211 FILE *fp = PyFile_AsFile(prog);
4212 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004213 PyCompilerFlags cf;
4214 cf.cf_flags = 0;
4215 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004216 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004217 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004218 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004219 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004220 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004221 }
4222 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004223 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004224 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004225 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004226 cf.cf_flags = 0;
4227#ifdef Py_USING_UNICODE
4228 if (PyUnicode_Check(prog)) {
4229 tmp = PyUnicode_AsUTF8String(prog);
4230 if (tmp == NULL)
4231 return -1;
4232 prog = tmp;
4233 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4234 }
4235#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004236 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004237 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004238 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004239 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004240 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004241 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004242 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004243 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004244 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004245 if (plain)
4246 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004247 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004248 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004249 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004250 return 0;
4251}
Guido van Rossum24c13741995-02-14 09:42:43 +00004252
Guido van Rossumac7be682001-01-17 15:42:30 +00004253static void
Paul Prescode68140d2000-08-30 20:25:01 +00004254format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4255{
4256 char *obj_str;
4257
4258 if (!obj)
4259 return;
4260
4261 obj_str = PyString_AsString(obj);
4262 if (!obj_str)
4263 return;
4264
4265 PyErr_Format(exc, format_str, obj_str);
4266}
Guido van Rossum950361c1997-01-24 13:49:28 +00004267
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004268static PyObject *
4269string_concatenate(PyObject *v, PyObject *w,
4270 PyFrameObject *f, unsigned char *next_instr)
4271{
4272 /* This function implements 'variable += expr' when both arguments
4273 are strings. */
4274
4275 if (v->ob_refcnt == 2) {
4276 /* In the common case, there are 2 references to the value
4277 * stored in 'variable' when the += is performed: one on the
4278 * value stack (in 'v') and one still stored in the 'variable'.
4279 * We try to delete the variable now to reduce the refcnt to 1.
4280 */
4281 switch (*next_instr) {
4282 case STORE_FAST:
4283 {
4284 int oparg = PEEKARG();
4285 PyObject **fastlocals = f->f_localsplus;
4286 if (GETLOCAL(oparg) == v)
4287 SETLOCAL(oparg, NULL);
4288 break;
4289 }
4290 case STORE_DEREF:
4291 {
4292 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4293 PyObject *c = freevars[PEEKARG()];
4294 if (PyCell_GET(c) == v)
4295 PyCell_Set(c, NULL);
4296 break;
4297 }
4298 case STORE_NAME:
4299 {
4300 PyObject *names = f->f_code->co_names;
4301 PyObject *name = GETITEM(names, PEEKARG());
4302 PyObject *locals = f->f_locals;
4303 if (PyDict_CheckExact(locals) &&
4304 PyDict_GetItem(locals, name) == v) {
4305 if (PyDict_DelItem(locals, name) != 0) {
4306 PyErr_Clear();
4307 }
4308 }
4309 break;
4310 }
4311 }
4312 }
4313
Armin Rigo618fbf52004-08-07 20:58:32 +00004314 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004315 /* Now we own the last reference to 'v', so we can resize it
4316 * in-place.
4317 */
4318 int v_len = PyString_GET_SIZE(v);
4319 int w_len = PyString_GET_SIZE(w);
4320 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4321 /* XXX if _PyString_Resize() fails, 'v' has been
4322 * deallocated so it cannot be put back into 'variable'.
4323 * The MemoryError is raised when there is no value in
4324 * 'variable', which might (very remotely) be a cause
4325 * of incompatibilities.
4326 */
4327 return NULL;
4328 }
4329 /* copy 'w' into the newly allocated area of 'v' */
4330 memcpy(PyString_AS_STRING(v) + v_len,
4331 PyString_AS_STRING(w), w_len);
4332 return v;
4333 }
4334 else {
4335 /* When in-place resizing is not an option. */
4336 PyString_Concat(&v, w);
4337 return v;
4338 }
4339}
4340
Guido van Rossum950361c1997-01-24 13:49:28 +00004341#ifdef DYNAMIC_EXECUTION_PROFILE
4342
Skip Montanarof118cb12001-10-15 20:51:38 +00004343static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004344getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004345{
4346 int i;
4347 PyObject *l = PyList_New(256);
4348 if (l == NULL) return NULL;
4349 for (i = 0; i < 256; i++) {
4350 PyObject *x = PyInt_FromLong(a[i]);
4351 if (x == NULL) {
4352 Py_DECREF(l);
4353 return NULL;
4354 }
4355 PyList_SetItem(l, i, x);
4356 }
4357 for (i = 0; i < 256; i++)
4358 a[i] = 0;
4359 return l;
4360}
4361
4362PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004363_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004364{
4365#ifndef DXPAIRS
4366 return getarray(dxp);
4367#else
4368 int i;
4369 PyObject *l = PyList_New(257);
4370 if (l == NULL) return NULL;
4371 for (i = 0; i < 257; i++) {
4372 PyObject *x = getarray(dxpairs[i]);
4373 if (x == NULL) {
4374 Py_DECREF(l);
4375 return NULL;
4376 }
4377 PyList_SetItem(l, i, x);
4378 }
4379 return l;
4380#endif
4381}
4382
4383#endif