blob: 131f5a78684479b7f27d6f27f114f436ab1b4201 [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
Tim Petersdbd9ba62000-07-09 03:09:57 +0000100static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000101#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000102static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
103 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000104static void call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000105 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000106static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000107static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000108 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000109
Tim Petersdbd9ba62000-07-09 03:09:57 +0000110static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
111static int assign_slice(PyObject *, PyObject *,
112 PyObject *, PyObject *);
113static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000114static PyObject *import_from(PyObject *, PyObject *);
115static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000116static PyObject *build_class(PyObject *, PyObject *, PyObject *);
117static int exec_statement(PyFrameObject *,
118 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000119static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
120static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000121static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000122static PyObject *string_concatenate(PyObject *, PyObject *,
123 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000124
Paul Prescode68140d2000-08-30 20:25:01 +0000125#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000126 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000127#define GLOBAL_NAME_ERROR_MSG \
128 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000129#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000130 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000131#define UNBOUNDFREE_ERROR_MSG \
132 "free variable '%.200s' referenced before assignment" \
133 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000134
Guido van Rossum950361c1997-01-24 13:49:28 +0000135/* Dynamic execution profile */
136#ifdef DYNAMIC_EXECUTION_PROFILE
137#ifdef DXPAIRS
138static long dxpairs[257][256];
139#define dxp dxpairs[256]
140#else
141static long dxp[256];
142#endif
143#endif
144
Jeremy Hylton985eba52003-02-05 23:13:00 +0000145/* Function call profile */
146#ifdef CALL_PROFILE
147#define PCALL_NUM 11
148static int pcall[PCALL_NUM];
149
150#define PCALL_ALL 0
151#define PCALL_FUNCTION 1
152#define PCALL_FAST_FUNCTION 2
153#define PCALL_FASTER_FUNCTION 3
154#define PCALL_METHOD 4
155#define PCALL_BOUND_METHOD 5
156#define PCALL_CFUNCTION 6
157#define PCALL_TYPE 7
158#define PCALL_GENERATOR 8
159#define PCALL_OTHER 9
160#define PCALL_POP 10
161
162/* Notes about the statistics
163
164 PCALL_FAST stats
165
166 FAST_FUNCTION means no argument tuple needs to be created.
167 FASTER_FUNCTION means that the fast-path frame setup code is used.
168
169 If there is a method call where the call can be optimized by changing
170 the argument tuple and calling the function directly, it gets recorded
171 twice.
172
173 As a result, the relationship among the statistics appears to be
174 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
175 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
176 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
177 PCALL_METHOD > PCALL_BOUND_METHOD
178*/
179
180#define PCALL(POS) pcall[POS]++
181
182PyObject *
183PyEval_GetCallStats(PyObject *self)
184{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000185 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000186 pcall[0], pcall[1], pcall[2], pcall[3],
187 pcall[4], pcall[5], pcall[6], pcall[7],
188 pcall[8], pcall[9]);
189}
190#else
191#define PCALL(O)
192
193PyObject *
194PyEval_GetCallStats(PyObject *self)
195{
196 Py_INCREF(Py_None);
197 return Py_None;
198}
199#endif
200
Tim Peters5ca576e2001-06-18 22:08:13 +0000201
Guido van Rossume59214e1994-08-30 08:01:59 +0000202#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000203
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000205#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000206#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000207#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000208
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000209static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000210static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211
Tim Peters7f468f22004-10-11 02:40:51 +0000212int
213PyEval_ThreadsInitialized(void)
214{
215 return interpreter_lock != 0;
216}
217
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 interpreter_lock = PyThread_allocate_lock();
224 PyThread_acquire_lock(interpreter_lock, 1);
225 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000227
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242{
243 if (tstate == NULL)
244 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000245 /* Check someone has called PyEval_InitThreads() to create the lock */
246 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248 if (PyThreadState_Swap(tstate) != NULL)
249 Py_FatalError(
250 "PyEval_AcquireThread: non-NULL old thread state");
251}
252
253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255{
256 if (tstate == NULL)
257 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
258 if (PyThreadState_Swap(NULL) != tstate)
259 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000261}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000262
263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes don't hold locks referring to threads which
265 are not running in the child process. (This could also be done using
266 pthread_atfork mechanism, at least for the pthreads implementation.) */
267
268void
269PyEval_ReInitThreads(void)
270{
271 if (!interpreter_lock)
272 return;
273 /*XXX Can't use PyThread_free_lock here because it does too
274 much error-checking. Doing this cleanly would require
275 adding a new function to each thread_*.h. Instead, just
276 create a new lock and waste a little bit of memory */
277 interpreter_lock = PyThread_allocate_lock();
278 PyThread_acquire_lock(interpreter_lock, 1);
279 main_thread = PyThread_get_thread_ident();
280}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281#endif
282
Guido van Rossumff4949e1992-08-05 19:58:53 +0000283/* Functions save_thread and restore_thread are always defined so
284 dynamically loaded modules needn't be compiled separately for use
285 with and without threads: */
286
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000287PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000289{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000290 PyThreadState *tstate = PyThreadState_Swap(NULL);
291 if (tstate == NULL)
292 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000293#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000294 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000295 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000297 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298}
299
300void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000303 if (tstate == NULL)
304 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000305#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000307 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000308 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 }
311#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000312 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
314
315
Guido van Rossuma9672091994-09-14 13:31:22 +0000316/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
317 signal handlers or Mac I/O completion routines) can schedule calls
318 to a function to be called synchronously.
319 The synchronous function is called with one void* argument.
320 It should return 0 for success or -1 for failure -- failure should
321 be accompanied by an exception.
322
323 If registry succeeds, the registry function returns 0; if it fails
324 (e.g. due to too many pending calls) it returns -1 (without setting
325 an exception condition).
326
327 Note that because registry may occur from within signal handlers,
328 or other asynchronous events, calling malloc() is unsafe!
329
330#ifdef WITH_THREAD
331 Any thread can schedule pending calls, but only the main thread
332 will execute them.
333#endif
334
335 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
336 There are two possible race conditions:
337 (1) nested asynchronous registry calls;
338 (2) registry calls made while pending calls are being processed.
339 While (1) is very unlikely, (2) is a real possibility.
340 The current code is safe against (2), but not against (1).
341 The safety against (2) is derived from the fact that only one
342 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000343
Guido van Rossuma027efa1997-05-05 20:56:21 +0000344 XXX Darn! With the advent of thread state, we should have an array
345 of pending calls per thread in the thread state! Later...
346*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000347
Guido van Rossuma9672091994-09-14 13:31:22 +0000348#define NPENDINGCALLS 32
349static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000350 int (*func)(void *);
351 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000352} pendingcalls[NPENDINGCALLS];
353static volatile int pendingfirst = 0;
354static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000355static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000356
357int
Thomas Wouters334fb892000-07-25 12:56:38 +0000358Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000359{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000360 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000361 int i, j;
362 /* XXX Begin critical section */
363 /* XXX If you want this to be safe against nested
364 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000365 if (busy)
366 return -1;
367 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000368 i = pendinglast;
369 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000370 if (j == pendingfirst) {
371 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000372 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000373 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000374 pendingcalls[i].func = func;
375 pendingcalls[i].arg = arg;
376 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000377
378 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000379 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000380 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000381 /* XXX End critical section */
382 return 0;
383}
384
Guido van Rossum180d7b41994-09-29 09:45:57 +0000385int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000387{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000388 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000389#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000390 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000391 return 0;
392#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000393 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000394 return 0;
395 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000396 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000397 for (;;) {
398 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000399 int (*func)(void *);
400 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000401 i = pendingfirst;
402 if (i == pendinglast)
403 break; /* Queue empty */
404 func = pendingcalls[i].func;
405 arg = pendingcalls[i].arg;
406 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000407 if (func(arg) < 0) {
408 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000409 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000410 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000413 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 return 0;
415}
416
417
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000418/* The interpreter's recursion limit */
419
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000420#ifndef Py_DEFAULT_RECURSION_LIMIT
421#define Py_DEFAULT_RECURSION_LIMIT 1000
422#endif
423static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
424int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000425
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000426int
427Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428{
429 return recursion_limit;
430}
431
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000432void
433Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000434{
435 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000436 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000437}
438
Armin Rigo2b3eb402003-10-28 12:05:48 +0000439/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
440 if the recursion_depth reaches _Py_CheckRecursionLimit.
441 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
442 to guarantee that _Py_CheckRecursiveCall() is regularly called.
443 Without USE_STACKCHECK, there is no need for this. */
444int
445_Py_CheckRecursiveCall(char *where)
446{
447 PyThreadState *tstate = PyThreadState_GET();
448
449#ifdef USE_STACKCHECK
450 if (PyOS_CheckStack()) {
451 --tstate->recursion_depth;
452 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
453 return -1;
454 }
455#endif
456 if (tstate->recursion_depth > recursion_limit) {
457 --tstate->recursion_depth;
458 PyErr_Format(PyExc_RuntimeError,
459 "maximum recursion depth exceeded%s",
460 where);
461 return -1;
462 }
463 _Py_CheckRecursionLimit = recursion_limit;
464 return 0;
465}
466
Guido van Rossum374a9221991-04-04 10:40:29 +0000467/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000468enum why_code {
469 WHY_NOT = 0x0001, /* No error */
470 WHY_EXCEPTION = 0x0002, /* Exception occurred */
471 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
472 WHY_RETURN = 0x0008, /* 'return' statement */
473 WHY_BREAK = 0x0010, /* 'break' statement */
474 WHY_CONTINUE = 0x0020, /* 'continue' statement */
475 WHY_YIELD = 0x0040 /* 'yield' operator */
476};
Guido van Rossum374a9221991-04-04 10:40:29 +0000477
Raymond Hettinger7c958652004-04-06 10:11:10 +0000478static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000479static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000480
Skip Montanarod581d772002-09-03 20:10:45 +0000481/* for manipulating the thread switch and periodic "stuff" - used to be
482 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000483int _Py_CheckInterval = 100;
484volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000485
Guido van Rossumb209a111997-04-29 18:18:01 +0000486PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000487PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000488{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000489 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000490 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000491 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000492 (PyObject **)NULL, 0,
493 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000494 (PyObject **)NULL, 0,
495 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000496}
497
498
499/* Interpreter main loop */
500
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000501PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000502PyEval_EvalFrame(PyFrameObject *f) {
503 /* This is for backward compatibility with extension modules that
504 used this API; core interpreter code should call PyEval_EvalFrameEx() */
505 return PyEval_EvalFrameEx(f, 0);
506}
507
508PyObject *
509PyEval_EvalFrameEx(PyFrameObject *f, int throw)
Guido van Rossum374a9221991-04-04 10:40:29 +0000510{
Guido van Rossum950361c1997-01-24 13:49:28 +0000511#ifdef DXPAIRS
512 int lastopcode = 0;
513#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000514 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000515 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000516 register int opcode; /* Current opcode */
517 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000518 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000519 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000520 register PyObject *x; /* Result object -- NULL if error */
521 register PyObject *v; /* Temporary objects popped off stack */
522 register PyObject *w;
523 register PyObject *u;
524 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000525 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000526 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000527 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000528 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000529 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000530
Tim Peters8a5c3c72004-04-05 19:36:21 +0000531 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000532
533 not (instr_lb <= current_bytecode_offset < instr_ub)
534
Tim Peters8a5c3c72004-04-05 19:36:21 +0000535 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000536 initial values are such as to make this false the first
537 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000538 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000539
Guido van Rossumd076c731998-10-07 19:42:25 +0000540 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000541 PyObject *names;
542 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000543#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000544 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000545#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000546#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000547 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000548 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000549#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000550
Neal Norwitza81d2202002-07-14 00:27:26 +0000551/* Tuple access macros */
552
553#ifndef Py_DEBUG
554#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
555#else
556#define GETITEM(v, i) PyTuple_GetItem((v), (i))
557#endif
558
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000559#ifdef WITH_TSC
560/* Use Pentium timestamp counter to mark certain events:
561 inst0 -- beginning of switch statement for opcode dispatch
562 inst1 -- end of switch statement (may be skipped)
563 loop0 -- the top of the mainloop
564 loop1 -- place where control returns again to top of mainloop
565 (may be skipped)
566 intr1 -- beginning of long interruption
567 intr2 -- end of long interruption
568
569 Many opcodes call out to helper C functions. In some cases, the
570 time in those functions should be counted towards the time for the
571 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
572 calls another Python function; there's no point in charge all the
573 bytecode executed by the called function to the caller.
574
575 It's hard to make a useful judgement statically. In the presence
576 of operator overloading, it's impossible to tell if a call will
577 execute new Python code or not.
578
579 It's a case-by-case judgement. I'll use intr1 for the following
580 cases:
581
582 EXEC_STMT
583 IMPORT_STAR
584 IMPORT_FROM
585 CALL_FUNCTION (and friends)
586
587 */
588 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
589 int ticked = 0;
590
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000591 READ_TIMESTAMP(inst0);
592 READ_TIMESTAMP(inst1);
593 READ_TIMESTAMP(loop0);
594 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000595
596 /* shut up the compiler */
597 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000598#endif
599
Guido van Rossum374a9221991-04-04 10:40:29 +0000600/* Code access macros */
601
Martin v. Löwis18e16552006-02-15 17:27:45 +0000602#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000603#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000604#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000605#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000606#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000607#define JUMPBY(x) (next_instr += (x))
608
Raymond Hettingerf606f872003-03-16 03:11:04 +0000609/* OpCode prediction macros
610 Some opcodes tend to come in pairs thus making it possible to predict
611 the second code when the first is run. For example, COMPARE_OP is often
612 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
613 followed by a POP_TOP.
614
615 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000616 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000617 processor has a high likelihood of making its own successful branch
618 prediction which results in a nearly zero overhead transition to the
619 next opcode.
620
621 A successful prediction saves a trip through the eval-loop including
622 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000623
Tim Peters8a5c3c72004-04-05 19:36:21 +0000624 If collecting opcode statistics, turn off prediction so that
625 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000626 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000627*/
628
Raymond Hettingera7216982004-02-08 19:59:27 +0000629#ifdef DYNAMIC_EXECUTION_PROFILE
630#define PREDICT(op) if (0) goto PRED_##op
631#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000632#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000633#endif
634
Raymond Hettingerf606f872003-03-16 03:11:04 +0000635#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000636#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638/* Stack manipulation macros */
639
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640/* The stack can grow at most MAXINT deep, as co_nlocals and
641 co_stacksize are ints. */
642#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000643#define EMPTY() (STACK_LEVEL() == 0)
644#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000645#define SECOND() (stack_pointer[-2])
646#define THIRD() (stack_pointer[-3])
647#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000648#define SET_TOP(v) (stack_pointer[-1] = (v))
649#define SET_SECOND(v) (stack_pointer[-2] = (v))
650#define SET_THIRD(v) (stack_pointer[-3] = (v))
651#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000652#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000653#define BASIC_PUSH(v) (*stack_pointer++ = (v))
654#define BASIC_POP() (*--stack_pointer)
655
Guido van Rossum96a42c81992-01-12 02:29:51 +0000656#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000657#define PUSH(v) { (void)(BASIC_PUSH(v), \
658 lltrace && prtrace(TOP(), "push")); \
659 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000660#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000661#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
662 lltrace && prtrace(TOP(), "stackadj")); \
663 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000664#else
665#define PUSH(v) BASIC_PUSH(v)
666#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000667#define STACKADJ(n) BASIC_STACKADJ(n)
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
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001077 if (!_Py_QnewFlag) {
1078 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001080 x = PyNumber_Divide(v, w);
1081 Py_DECREF(v);
1082 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001084 if (x != NULL) continue;
1085 break;
1086 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001088 BINARY_TRUE_DIVIDE */
1089 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001090 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001092 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001093 Py_DECREF(v);
1094 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001096 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001098
Guido van Rossum4668b002001-08-08 05:00:18 +00001099 case BINARY_FLOOR_DIVIDE:
1100 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001102 x = PyNumber_FloorDivide(v, w);
1103 Py_DECREF(v);
1104 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001105 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001106 if (x != NULL) continue;
1107 break;
1108
Guido van Rossum374a9221991-04-04 10:40:29 +00001109 case BINARY_MODULO:
1110 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001111 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001112 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001113 Py_DECREF(v);
1114 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001115 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001116 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Guido van Rossum374a9221991-04-04 10:40:29 +00001119 case BINARY_ADD:
1120 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001121 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001122 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001123 /* INLINE: int + int */
1124 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001125 a = PyInt_AS_LONG(v);
1126 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001128 if ((i^a) < 0 && (i^b) < 0)
1129 goto slow_add;
1130 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001132 else if (PyString_CheckExact(v) &&
1133 PyString_CheckExact(w)) {
1134 x = string_concatenate(v, w, f, next_instr);
1135 /* string_concatenate consumed the ref to v */
1136 goto skip_decref_vx;
1137 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001138 else {
1139 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001140 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001141 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001142 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001143 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001144 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001146 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 case BINARY_SUBTRACT:
1150 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001152 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001153 /* INLINE: int - int */
1154 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001155 a = PyInt_AS_LONG(v);
1156 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001157 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001158 if ((i^a) < 0 && (i^~b) < 0)
1159 goto slow_sub;
1160 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001161 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001162 else {
1163 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001164 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001165 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001166 Py_DECREF(v);
1167 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001168 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001169 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001170 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Guido van Rossum374a9221991-04-04 10:40:29 +00001172 case BINARY_SUBSCR:
1173 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001174 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001175 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001176 /* INLINE: list[int] */
1177 long i = PyInt_AsLong(w);
1178 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001179 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001180 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001181 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001182 Py_INCREF(x);
1183 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001184 else
1185 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001186 }
1187 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001188 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001189 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001190 Py_DECREF(v);
1191 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001193 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum7928cd71991-10-24 14:59:31 +00001196 case BINARY_LSHIFT:
1197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001199 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001200 Py_DECREF(v);
1201 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001203 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Guido van Rossum7928cd71991-10-24 14:59:31 +00001206 case BINARY_RSHIFT:
1207 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001209 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001210 Py_DECREF(v);
1211 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001213 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum7928cd71991-10-24 14:59:31 +00001216 case BINARY_AND:
1217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001219 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001220 Py_DECREF(v);
1221 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001223 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001225
Guido van Rossum7928cd71991-10-24 14:59:31 +00001226 case BINARY_XOR:
1227 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001229 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001230 Py_DECREF(v);
1231 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001233 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001234 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001235
Guido van Rossum7928cd71991-10-24 14:59:31 +00001236 case BINARY_OR:
1237 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001239 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001240 Py_DECREF(v);
1241 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001243 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001244 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001245
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001246 case LIST_APPEND:
1247 w = POP();
1248 v = POP();
1249 err = PyList_Append(v, w);
1250 Py_DECREF(v);
1251 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001252 if (err == 0) {
1253 PREDICT(JUMP_ABSOLUTE);
1254 continue;
1255 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001256 break;
1257
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 case INPLACE_POWER:
1259 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 x = PyNumber_InPlacePower(v, w, Py_None);
1262 Py_DECREF(v);
1263 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 if (x != NULL) continue;
1266 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 case INPLACE_MULTIPLY:
1269 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001271 x = PyNumber_InPlaceMultiply(v, w);
1272 Py_DECREF(v);
1273 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 if (x != NULL) continue;
1276 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001277
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001279 if (!_Py_QnewFlag) {
1280 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001282 x = PyNumber_InPlaceDivide(v, w);
1283 Py_DECREF(v);
1284 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001286 if (x != NULL) continue;
1287 break;
1288 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001290 INPLACE_TRUE_DIVIDE */
1291 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001294 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 Py_DECREF(v);
1296 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001297 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001298 if (x != NULL) continue;
1299 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001300
Guido van Rossum4668b002001-08-08 05:00:18 +00001301 case INPLACE_FLOOR_DIVIDE:
1302 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001303 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001304 x = PyNumber_InPlaceFloorDivide(v, w);
1305 Py_DECREF(v);
1306 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001307 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001308 if (x != NULL) continue;
1309 break;
1310
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 case INPLACE_MODULO:
1312 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 x = PyNumber_InPlaceRemainder(v, w);
1315 Py_DECREF(v);
1316 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001317 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001318 if (x != NULL) continue;
1319 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 case INPLACE_ADD:
1322 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001323 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001324 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 /* INLINE: int + int */
1326 register long a, b, i;
1327 a = PyInt_AS_LONG(v);
1328 b = PyInt_AS_LONG(w);
1329 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001330 if ((i^a) < 0 && (i^b) < 0)
1331 goto slow_iadd;
1332 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001334 else if (PyString_CheckExact(v) &&
1335 PyString_CheckExact(w)) {
1336 x = string_concatenate(v, w, f, next_instr);
1337 /* string_concatenate consumed the ref to v */
1338 goto skip_decref_v;
1339 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001340 else {
1341 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001343 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001345 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 if (x != NULL) continue;
1349 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 case INPLACE_SUBTRACT:
1352 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001354 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 /* INLINE: int - int */
1356 register long a, b, i;
1357 a = PyInt_AS_LONG(v);
1358 b = PyInt_AS_LONG(w);
1359 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001360 if ((i^a) < 0 && (i^~b) < 0)
1361 goto slow_isub;
1362 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001364 else {
1365 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001367 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 Py_DECREF(v);
1369 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 if (x != NULL) continue;
1372 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 case INPLACE_LSHIFT:
1375 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001377 x = PyNumber_InPlaceLshift(v, w);
1378 Py_DECREF(v);
1379 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 if (x != NULL) continue;
1382 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Thomas Wouters434d0822000-08-24 20:11:32 +00001384 case INPLACE_RSHIFT:
1385 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 x = PyNumber_InPlaceRshift(v, w);
1388 Py_DECREF(v);
1389 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 if (x != NULL) continue;
1392 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001393
Thomas Wouters434d0822000-08-24 20:11:32 +00001394 case INPLACE_AND:
1395 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001396 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001397 x = PyNumber_InPlaceAnd(v, w);
1398 Py_DECREF(v);
1399 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001401 if (x != NULL) continue;
1402 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Thomas Wouters434d0822000-08-24 20:11:32 +00001404 case INPLACE_XOR:
1405 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001406 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001407 x = PyNumber_InPlaceXor(v, w);
1408 Py_DECREF(v);
1409 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001411 if (x != NULL) continue;
1412 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Thomas Wouters434d0822000-08-24 20:11:32 +00001414 case INPLACE_OR:
1415 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001416 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001417 x = PyNumber_InPlaceOr(v, w);
1418 Py_DECREF(v);
1419 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001421 if (x != NULL) continue;
1422 break;
1423
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 case SLICE+0:
1425 case SLICE+1:
1426 case SLICE+2:
1427 case SLICE+3:
1428 if ((opcode-SLICE) & 2)
1429 w = POP();
1430 else
1431 w = NULL;
1432 if ((opcode-SLICE) & 1)
1433 v = POP();
1434 else
1435 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001436 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001437 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001438 Py_DECREF(u);
1439 Py_XDECREF(v);
1440 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001441 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001442 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 case STORE_SLICE+0:
1446 case STORE_SLICE+1:
1447 case STORE_SLICE+2:
1448 case STORE_SLICE+3:
1449 if ((opcode-STORE_SLICE) & 2)
1450 w = POP();
1451 else
1452 w = NULL;
1453 if ((opcode-STORE_SLICE) & 1)
1454 v = POP();
1455 else
1456 v = NULL;
1457 u = POP();
1458 t = POP();
1459 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001460 Py_DECREF(t);
1461 Py_DECREF(u);
1462 Py_XDECREF(v);
1463 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001464 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001466
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 case DELETE_SLICE+0:
1468 case DELETE_SLICE+1:
1469 case DELETE_SLICE+2:
1470 case DELETE_SLICE+3:
1471 if ((opcode-DELETE_SLICE) & 2)
1472 w = POP();
1473 else
1474 w = NULL;
1475 if ((opcode-DELETE_SLICE) & 1)
1476 v = POP();
1477 else
1478 v = NULL;
1479 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001482 Py_DECREF(u);
1483 Py_XDECREF(v);
1484 Py_XDECREF(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 STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 w = TOP();
1490 v = SECOND();
1491 u = THIRD();
1492 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001493 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001494 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001495 Py_DECREF(u);
1496 Py_DECREF(v);
1497 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001498 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Guido van Rossum374a9221991-04-04 10:40:29 +00001501 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001502 w = TOP();
1503 v = SECOND();
1504 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001506 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001507 Py_DECREF(v);
1508 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001509 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001511
Guido van Rossum374a9221991-04-04 10:40:29 +00001512 case PRINT_EXPR:
1513 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001514 w = PySys_GetObject("displayhook");
1515 if (w == NULL) {
1516 PyErr_SetString(PyExc_RuntimeError,
1517 "lost sys.displayhook");
1518 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001519 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001520 }
1521 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001522 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001523 if (x == NULL)
1524 err = -1;
1525 }
1526 if (err == 0) {
1527 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001528 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001529 if (w == NULL)
1530 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001531 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001533 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001534 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001535
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001536 case PRINT_ITEM_TO:
1537 w = stream = POP();
1538 /* fall through to PRINT_ITEM */
1539
Guido van Rossum374a9221991-04-04 10:40:29 +00001540 case PRINT_ITEM:
1541 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001542 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001543 w = PySys_GetObject("stdout");
1544 if (w == NULL) {
1545 PyErr_SetString(PyExc_RuntimeError,
1546 "lost sys.stdout");
1547 err = -1;
1548 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001549 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001550 /* PyFile_SoftSpace() can exececute arbitrary code
1551 if sys.stdout is an instance with a __getattr__.
1552 If __getattr__ raises an exception, w will
1553 be freed, so we need to prevent that temporarily. */
1554 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001555 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001556 err = PyFile_WriteString(" ", w);
1557 if (err == 0)
1558 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001559 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001560 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001561 if (PyString_Check(v)) {
1562 char *s = PyString_AS_STRING(v);
1563 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001564 if (len == 0 ||
1565 !isspace(Py_CHARMASK(s[len-1])) ||
1566 s[len-1] == ' ')
1567 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001568 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001569#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001570 else if (PyUnicode_Check(v)) {
1571 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1572 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001573 if (len == 0 ||
1574 !Py_UNICODE_ISSPACE(s[len-1]) ||
1575 s[len-1] == ' ')
1576 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001577 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001578#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001579 else
1580 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001582 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001583 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001584 Py_XDECREF(stream);
1585 stream = NULL;
1586 if (err == 0)
1587 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001588 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001590 case PRINT_NEWLINE_TO:
1591 w = stream = POP();
1592 /* fall through to PRINT_NEWLINE */
1593
Guido van Rossum374a9221991-04-04 10:40:29 +00001594 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001595 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001596 w = PySys_GetObject("stdout");
1597 if (w == NULL)
1598 PyErr_SetString(PyExc_RuntimeError,
1599 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001600 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001601 if (w != NULL) {
1602 err = PyFile_WriteString("\n", w);
1603 if (err == 0)
1604 PyFile_SoftSpace(w, 0);
1605 }
1606 Py_XDECREF(stream);
1607 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001608 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Thomas Wouters434d0822000-08-24 20:11:32 +00001610
1611#ifdef CASE_TOO_BIG
1612 default: switch (opcode) {
1613#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001614 case RAISE_VARARGS:
1615 u = v = w = NULL;
1616 switch (oparg) {
1617 case 3:
1618 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001619 /* Fallthrough */
1620 case 2:
1621 v = POP(); /* value */
1622 /* Fallthrough */
1623 case 1:
1624 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001625 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001626 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001627 break;
1628 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001629 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001630 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001631 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001632 break;
1633 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Guido van Rossum374a9221991-04-04 10:40:29 +00001636 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001637 if ((x = f->f_locals) != NULL) {
1638 Py_INCREF(x);
1639 PUSH(x);
1640 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001641 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001642 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Guido van Rossum374a9221991-04-04 10:40:29 +00001645 case RETURN_VALUE:
1646 retval = POP();
1647 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001648 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001649
Tim Peters5ca576e2001-06-18 22:08:13 +00001650 case YIELD_VALUE:
1651 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001652 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001653 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001654 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001655
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001656 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001657 w = TOP();
1658 v = SECOND();
1659 u = THIRD();
1660 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001661 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001662 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001663 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001664 Py_DECREF(u);
1665 Py_DECREF(v);
1666 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001667 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 case POP_BLOCK:
1670 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 while (STACK_LEVEL() > b->b_level) {
1673 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 }
1676 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001677 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 case END_FINALLY:
1680 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001681 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001682 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001683 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001684 if (why == WHY_RETURN ||
1685 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 retval = POP();
1687 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001688 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001689 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001690 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001691 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001693 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001695 else if (v != Py_None) {
1696 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 "'finally' pops bad exception");
1698 why = WHY_EXCEPTION;
1699 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001700 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001704 u = TOP();
1705 v = SECOND();
1706 w = THIRD();
1707 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001708 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001709 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001710 Py_DECREF(u);
1711 Py_DECREF(v);
1712 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001716 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001718 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001719 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001720 err = PyDict_SetItem(x, w, v);
1721 else
1722 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001723 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001724 if (err == 0) continue;
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 found when storing %s",
1729 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001733 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001734 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001735 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001736 format_exc_check_arg(PyExc_NameError,
1737 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001738 break;
1739 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001740 PyErr_Format(PyExc_SystemError,
1741 "no locals when deleting %s",
1742 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001744
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001745 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001746 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001748 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1749 PyObject **items = ((PyTupleObject *)v)->ob_item;
1750 while (oparg--) {
1751 w = items[oparg];
1752 Py_INCREF(w);
1753 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001754 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001755 Py_DECREF(v);
1756 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001757 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1758 PyObject **items = ((PyListObject *)v)->ob_item;
1759 while (oparg--) {
1760 w = items[oparg];
1761 Py_INCREF(w);
1762 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001763 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001764 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001765 stack_pointer + oparg))
1766 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001767 else {
1768 if (PyErr_ExceptionMatches(PyExc_TypeError))
1769 PyErr_SetString(PyExc_TypeError,
1770 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001771 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001772 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001773 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001777 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001778 v = TOP();
1779 u = SECOND();
1780 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1782 Py_DECREF(v);
1783 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001784 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001788 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001790 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1791 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001792 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001794
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001795 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001796 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001797 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001798 err = PyDict_SetItem(f->f_globals, w, v);
1799 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001800 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001801 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001803 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001804 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001805 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001806 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001807 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001808 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001811 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001812 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001813 PyErr_Format(PyExc_SystemError,
1814 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001815 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001816 break;
1817 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001818 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001819 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001820 Py_XINCREF(x);
1821 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001822 else {
1823 x = PyObject_GetItem(v, w);
1824 if (x == NULL && PyErr_Occurred()) {
1825 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1826 break;
1827 PyErr_Clear();
1828 }
1829 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001831 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001833 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001834 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001835 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001836 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001837 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 break;
1839 }
1840 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001841 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001843 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001844 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001847 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001848 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001849 /* Inline the PyDict_GetItem() calls.
1850 WARNING: this is an extreme speed hack.
1851 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001852 long hash = ((PyStringObject *)w)->ob_shash;
1853 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001854 PyDictObject *d;
1855 d = (PyDictObject *)(f->f_globals);
1856 x = d->ma_lookup(d, w, hash)->me_value;
1857 if (x != NULL) {
1858 Py_INCREF(x);
1859 PUSH(x);
1860 continue;
1861 }
1862 d = (PyDictObject *)(f->f_builtins);
1863 x = d->ma_lookup(d, w, hash)->me_value;
1864 if (x != NULL) {
1865 Py_INCREF(x);
1866 PUSH(x);
1867 continue;
1868 }
1869 goto load_global_error;
1870 }
1871 }
1872 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001873 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001875 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001877 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001878 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001879 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001880 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 break;
1882 }
1883 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001886 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001887
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001888 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001889 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001890 if (x != NULL) {
1891 SETLOCAL(oparg, NULL);
1892 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001893 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001894 format_exc_check_arg(
1895 PyExc_UnboundLocalError,
1896 UNBOUNDLOCAL_ERROR_MSG,
1897 PyTuple_GetItem(co->co_varnames, oparg)
1898 );
1899 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001900
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001901 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001902 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001903 Py_INCREF(x);
1904 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001905 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001906 break;
1907
1908 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001909 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001910 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001911 if (w != NULL) {
1912 PUSH(w);
1913 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001914 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001915 err = -1;
1916 /* Don't stomp existing exception */
1917 if (PyErr_Occurred())
1918 break;
1919 if (oparg < f->f_ncells) {
1920 v = PyTuple_GetItem(co->co_cellvars,
1921 oparg);
1922 format_exc_check_arg(
1923 PyExc_UnboundLocalError,
1924 UNBOUNDLOCAL_ERROR_MSG,
1925 v);
1926 } else {
1927 v = PyTuple_GetItem(
1928 co->co_freevars,
1929 oparg - f->f_ncells);
1930 format_exc_check_arg(
1931 PyExc_NameError,
1932 UNBOUNDFREE_ERROR_MSG,
1933 v);
1934 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001935 break;
1936
1937 case STORE_DEREF:
1938 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001939 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001940 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001941 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001942 continue;
1943
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001947 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001949 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 }
1951 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001952 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001953 }
1954 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001955
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001959 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001960 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001961 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001962 }
1963 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001964 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001965 }
1966 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001967
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001969 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001971 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Guido van Rossum374a9221991-04-04 10:40:29 +00001974 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001975 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001976 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 x = PyObject_GetAttr(v, w);
1978 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001979 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001980 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Guido van Rossum374a9221991-04-04 10:40:29 +00001983 case COMPARE_OP:
1984 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001985 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001986 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001987 /* INLINE: cmp(int, int) */
1988 register long a, b;
1989 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001990 a = PyInt_AS_LONG(v);
1991 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001992 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001993 case PyCmp_LT: res = a < b; break;
1994 case PyCmp_LE: res = a <= b; break;
1995 case PyCmp_EQ: res = a == b; break;
1996 case PyCmp_NE: res = a != b; break;
1997 case PyCmp_GT: res = a > b; break;
1998 case PyCmp_GE: res = a >= b; break;
1999 case PyCmp_IS: res = v == w; break;
2000 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002001 default: goto slow_compare;
2002 }
2003 x = res ? Py_True : Py_False;
2004 Py_INCREF(x);
2005 }
2006 else {
2007 slow_compare:
2008 x = cmp_outcome(oparg, v, w);
2009 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002010 Py_DECREF(v);
2011 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002012 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002013 if (x == NULL) break;
2014 PREDICT(JUMP_IF_FALSE);
2015 PREDICT(JUMP_IF_TRUE);
2016 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002017
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002019 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002021 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002022 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002023 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002024 break;
2025 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002026 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002027 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002028 w,
2029 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002030 f->f_locals == NULL ?
2031 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002032 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002033 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002034 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002035 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002036 x = NULL;
2037 break;
2038 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002039 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002041 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002043 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002044 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Thomas Wouters52152252000-08-17 22:55:00 +00002047 case IMPORT_STAR:
2048 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002050 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002051 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002052 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002053 break;
2054 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002055 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002056 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002057 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002058 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002059 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002060 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002061 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002062
Thomas Wouters52152252000-08-17 22:55:00 +00002063 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002064 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002065 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002066 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002067 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002068 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002069 PUSH(x);
2070 if (x != NULL) continue;
2071 break;
2072
Guido van Rossum374a9221991-04-04 10:40:29 +00002073 case JUMP_FORWARD:
2074 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002075 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Raymond Hettingerf606f872003-03-16 03:11:04 +00002077 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002078 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002079 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002080 if (w == Py_True) {
2081 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002082 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002083 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002084 if (w == Py_False) {
2085 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002086 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002087 }
2088 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002089 if (err > 0)
2090 err = 0;
2091 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002092 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002093 else
2094 break;
2095 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002096
Raymond Hettingerf606f872003-03-16 03:11:04 +00002097 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002098 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002099 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002100 if (w == Py_False) {
2101 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002102 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002103 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002104 if (w == Py_True) {
2105 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002106 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002107 }
2108 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002109 if (err > 0) {
2110 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002111 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002112 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002113 else if (err == 0)
2114 ;
2115 else
2116 break;
2117 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002118
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002119 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002120 case JUMP_ABSOLUTE:
2121 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002122 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002123
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124 case GET_ITER:
2125 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002126 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002127 x = PyObject_GetIter(v);
2128 Py_DECREF(v);
2129 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002130 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002131 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002132 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002134 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002135 break;
2136
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002137 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002138 case FOR_ITER:
2139 /* before: [iter]; after: [iter, iter()] *or* [] */
2140 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002141 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002142 if (x != NULL) {
2143 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002144 PREDICT(STORE_FAST);
2145 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002146 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002147 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002148 if (PyErr_Occurred()) {
2149 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2150 break;
2151 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002152 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002153 /* iterator ended normally */
2154 x = v = POP();
2155 Py_DECREF(v);
2156 JUMPBY(oparg);
2157 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002158
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002159 case BREAK_LOOP:
2160 why = WHY_BREAK;
2161 goto fast_block_end;
2162
2163 case CONTINUE_LOOP:
2164 retval = PyInt_FromLong(oparg);
2165 why = WHY_CONTINUE;
2166 goto fast_block_end;
2167
Guido van Rossum374a9221991-04-04 10:40:29 +00002168 case SETUP_LOOP:
2169 case SETUP_EXCEPT:
2170 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002171 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002172 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002173 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Guido van Rossumf10570b1995-07-07 22:53:21 +00002175 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002176 {
2177 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002178 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002179 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002180#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002181 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002182#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002183 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002184#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002185 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002186 PUSH(x);
2187 if (x != NULL)
2188 continue;
2189 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002191
Jeremy Hylton76901512000-03-28 23:49:17 +00002192 case CALL_FUNCTION_VAR:
2193 case CALL_FUNCTION_KW:
2194 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002195 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002196 int na = oparg & 0xff;
2197 int nk = (oparg>>8) & 0xff;
2198 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002199 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002200 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002201 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002202 if (flags & CALL_FLAG_VAR)
2203 n++;
2204 if (flags & CALL_FLAG_KW)
2205 n++;
2206 pfunc = stack_pointer - n - 1;
2207 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002208
Guido van Rossumac7be682001-01-17 15:42:30 +00002209 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002210 && PyMethod_GET_SELF(func) != NULL) {
2211 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002212 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002213 func = PyMethod_GET_FUNCTION(func);
2214 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002215 Py_DECREF(*pfunc);
2216 *pfunc = self;
2217 na++;
2218 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002219 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002220 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002221 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002222 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002223 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002224 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002225 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002226 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002227
Jeremy Hylton76901512000-03-28 23:49:17 +00002228 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002229 w = POP();
2230 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002231 }
2232 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002233 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002234 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002235 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002236 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002237
Guido van Rossum681d79a1995-07-18 14:51:37 +00002238 case MAKE_FUNCTION:
2239 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002240 x = PyFunction_New(v, f->f_globals);
2241 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002242 /* XXX Maybe this should be a separate opcode? */
2243 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002245 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002246 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002247 x = NULL;
2248 break;
2249 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002250 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002251 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002252 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002253 }
2254 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002255 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002256 }
2257 PUSH(x);
2258 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002259
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002260 case MAKE_CLOSURE:
2261 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002262 v = POP(); /* code object */
2263 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002264 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 if (x != NULL) {
2266 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002267 err = PyFunction_SetClosure(x, v);
2268 Py_DECREF(v);
2269 }
2270 if (x != NULL && oparg > 0) {
2271 v = PyTuple_New(oparg);
2272 if (v == NULL) {
2273 Py_DECREF(x);
2274 x = NULL;
2275 break;
2276 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002277 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002278 w = POP();
2279 PyTuple_SET_ITEM(v, oparg, w);
2280 }
2281 err = PyFunction_SetDefaults(x, v);
2282 Py_DECREF(v);
2283 }
2284 PUSH(x);
2285 break;
2286 }
2287
Guido van Rossum8861b741996-07-30 16:49:37 +00002288 case BUILD_SLICE:
2289 if (oparg == 3)
2290 w = POP();
2291 else
2292 w = NULL;
2293 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002294 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002295 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 Py_DECREF(u);
2297 Py_DECREF(v);
2298 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002299 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002300 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002301 break;
2302
Fred Drakeef8ace32000-08-24 00:32:09 +00002303 case EXTENDED_ARG:
2304 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002305 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002306 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002307
Guido van Rossum374a9221991-04-04 10:40:29 +00002308 default:
2309 fprintf(stderr,
2310 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002311 PyCode_Addr2Line(f->f_code, f->f_lasti),
2312 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002313 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 why = WHY_EXCEPTION;
2315 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002316
2317#ifdef CASE_TOO_BIG
2318 }
2319#endif
2320
Guido van Rossum374a9221991-04-04 10:40:29 +00002321 } /* switch */
2322
2323 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002325 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002330 if (err == 0 && x != NULL) {
2331#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002332 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002333 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002334 fprintf(stderr,
2335 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002336 else {
2337#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002338 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002339 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002340#ifdef CHECKEXC
2341 }
2342#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002343 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002345 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002346 err = 0;
2347 }
2348
Guido van Rossum374a9221991-04-04 10:40:29 +00002349 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002350
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002351 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002352 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002353 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002354 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002355 why = WHY_EXCEPTION;
2356 }
2357 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002358#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002360 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002362 char buf[1024];
2363 sprintf(buf, "Stack unwind with exception "
2364 "set and why=%d", why);
2365 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002366 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 }
2368#endif
2369
2370 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002371
Guido van Rossum374a9221991-04-04 10:40:29 +00002372 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002373 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002374
Fred Drake8f51f542001-10-04 14:48:42 +00002375 if (tstate->c_tracefunc != NULL)
2376 call_exc_trace(tstate->c_tracefunc,
2377 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002378 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 if (why == WHY_RERAISE)
2383 why = WHY_EXCEPTION;
2384
2385 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002386
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002387fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002388 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002389 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002390
Tim Peters8a5c3c72004-04-05 19:36:21 +00002391 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002392 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2393 /* For a continue inside a try block,
2394 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002395 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2396 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002397 why = WHY_NOT;
2398 JUMPTO(PyInt_AS_LONG(retval));
2399 Py_DECREF(retval);
2400 break;
2401 }
2402
Guido van Rossum374a9221991-04-04 10:40:29 +00002403 while (STACK_LEVEL() > b->b_level) {
2404 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002405 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002406 }
2407 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2408 why = WHY_NOT;
2409 JUMPTO(b->b_handler);
2410 break;
2411 }
2412 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002413 (b->b_type == SETUP_EXCEPT &&
2414 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002415 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002416 PyObject *exc, *val, *tb;
2417 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002418 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002419 val = Py_None;
2420 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002421 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002422 /* Make the raw exception data
2423 available to the handler,
2424 so a program can emulate the
2425 Python main loop. Don't do
2426 this for 'finally'. */
2427 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002428 PyErr_NormalizeException(
2429 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002430 set_exc_info(tstate,
2431 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002432 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002433 if (tb == NULL) {
2434 Py_INCREF(Py_None);
2435 PUSH(Py_None);
2436 } else
2437 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002438 PUSH(val);
2439 PUSH(exc);
2440 }
2441 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002442 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002443 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002444 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002445 PUSH(v);
2446 }
2447 why = WHY_NOT;
2448 JUMPTO(b->b_handler);
2449 break;
2450 }
2451 } /* unwind stack */
2452
2453 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 if (why != WHY_NOT)
2456 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002457 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002458
Guido van Rossum374a9221991-04-04 10:40:29 +00002459 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002460
Tim Peters8a5c3c72004-04-05 19:36:21 +00002461 assert(why != WHY_YIELD);
2462 /* Pop remaining stack entries. */
2463 while (!EMPTY()) {
2464 v = POP();
2465 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002466 }
2467
Tim Peters8a5c3c72004-04-05 19:36:21 +00002468 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002469 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002470
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002471fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002472 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002473 if (tstate->c_tracefunc) {
2474 if (why == WHY_RETURN || why == WHY_YIELD) {
2475 if (call_trace(tstate->c_tracefunc,
2476 tstate->c_traceobj, f,
2477 PyTrace_RETURN, retval)) {
2478 Py_XDECREF(retval);
2479 retval = NULL;
2480 why = WHY_EXCEPTION;
2481 }
2482 }
2483 else if (why == WHY_EXCEPTION) {
2484 call_trace_protected(tstate->c_tracefunc,
2485 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002486 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002487 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002488 }
Fred Drake8f51f542001-10-04 14:48:42 +00002489 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002490 if (why == WHY_EXCEPTION)
2491 call_trace_protected(tstate->c_profilefunc,
2492 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002493 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002494 else if (call_trace(tstate->c_profilefunc,
2495 tstate->c_profileobj, f,
2496 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002497 Py_XDECREF(retval);
2498 retval = NULL;
2499 why = WHY_EXCEPTION;
2500 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002501 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002502 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002503
Guido van Rossuma027efa1997-05-05 20:56:21 +00002504 reset_exc_info(tstate);
2505
Tim Peters5ca576e2001-06-18 22:08:13 +00002506 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002507 exit_eval_frame:
2508 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002509 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002510
Guido van Rossum96a42c81992-01-12 02:29:51 +00002511 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002512}
2513
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002514/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002515 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002516 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002517
Tim Peters6d6c1a32001-08-02 04:15:00 +00002518PyObject *
2519PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002520 PyObject **args, int argcount, PyObject **kws, int kwcount,
2521 PyObject **defs, int defcount, PyObject *closure)
2522{
2523 register PyFrameObject *f;
2524 register PyObject *retval = NULL;
2525 register PyObject **fastlocals, **freevars;
2526 PyThreadState *tstate = PyThreadState_GET();
2527 PyObject *x, *u;
2528
2529 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002530 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002531 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002532 return NULL;
2533 }
2534
Jeremy Hylton985eba52003-02-05 23:13:00 +00002535 assert(globals != NULL);
2536 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002537 if (f == NULL)
2538 return NULL;
2539
2540 fastlocals = f->f_localsplus;
2541 freevars = f->f_localsplus + f->f_nlocals;
2542
2543 if (co->co_argcount > 0 ||
2544 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2545 int i;
2546 int n = argcount;
2547 PyObject *kwdict = NULL;
2548 if (co->co_flags & CO_VARKEYWORDS) {
2549 kwdict = PyDict_New();
2550 if (kwdict == NULL)
2551 goto fail;
2552 i = co->co_argcount;
2553 if (co->co_flags & CO_VARARGS)
2554 i++;
2555 SETLOCAL(i, kwdict);
2556 }
2557 if (argcount > co->co_argcount) {
2558 if (!(co->co_flags & CO_VARARGS)) {
2559 PyErr_Format(PyExc_TypeError,
2560 "%.200s() takes %s %d "
2561 "%sargument%s (%d given)",
2562 PyString_AsString(co->co_name),
2563 defcount ? "at most" : "exactly",
2564 co->co_argcount,
2565 kwcount ? "non-keyword " : "",
2566 co->co_argcount == 1 ? "" : "s",
2567 argcount);
2568 goto fail;
2569 }
2570 n = co->co_argcount;
2571 }
2572 for (i = 0; i < n; i++) {
2573 x = args[i];
2574 Py_INCREF(x);
2575 SETLOCAL(i, x);
2576 }
2577 if (co->co_flags & CO_VARARGS) {
2578 u = PyTuple_New(argcount - n);
2579 if (u == NULL)
2580 goto fail;
2581 SETLOCAL(co->co_argcount, u);
2582 for (i = n; i < argcount; i++) {
2583 x = args[i];
2584 Py_INCREF(x);
2585 PyTuple_SET_ITEM(u, i-n, x);
2586 }
2587 }
2588 for (i = 0; i < kwcount; i++) {
2589 PyObject *keyword = kws[2*i];
2590 PyObject *value = kws[2*i + 1];
2591 int j;
2592 if (keyword == NULL || !PyString_Check(keyword)) {
2593 PyErr_Format(PyExc_TypeError,
2594 "%.200s() keywords must be strings",
2595 PyString_AsString(co->co_name));
2596 goto fail;
2597 }
2598 /* XXX slow -- speed up using dictionary? */
2599 for (j = 0; j < co->co_argcount; j++) {
2600 PyObject *nm = PyTuple_GET_ITEM(
2601 co->co_varnames, j);
2602 int cmp = PyObject_RichCompareBool(
2603 keyword, nm, Py_EQ);
2604 if (cmp > 0)
2605 break;
2606 else if (cmp < 0)
2607 goto fail;
2608 }
2609 /* Check errors from Compare */
2610 if (PyErr_Occurred())
2611 goto fail;
2612 if (j >= co->co_argcount) {
2613 if (kwdict == NULL) {
2614 PyErr_Format(PyExc_TypeError,
2615 "%.200s() got an unexpected "
2616 "keyword argument '%.400s'",
2617 PyString_AsString(co->co_name),
2618 PyString_AsString(keyword));
2619 goto fail;
2620 }
2621 PyDict_SetItem(kwdict, keyword, value);
2622 }
2623 else {
2624 if (GETLOCAL(j) != NULL) {
2625 PyErr_Format(PyExc_TypeError,
2626 "%.200s() got multiple "
2627 "values for keyword "
2628 "argument '%.400s'",
2629 PyString_AsString(co->co_name),
2630 PyString_AsString(keyword));
2631 goto fail;
2632 }
2633 Py_INCREF(value);
2634 SETLOCAL(j, value);
2635 }
2636 }
2637 if (argcount < co->co_argcount) {
2638 int m = co->co_argcount - defcount;
2639 for (i = argcount; i < m; i++) {
2640 if (GETLOCAL(i) == NULL) {
2641 PyErr_Format(PyExc_TypeError,
2642 "%.200s() takes %s %d "
2643 "%sargument%s (%d given)",
2644 PyString_AsString(co->co_name),
2645 ((co->co_flags & CO_VARARGS) ||
2646 defcount) ? "at least"
2647 : "exactly",
2648 m, kwcount ? "non-keyword " : "",
2649 m == 1 ? "" : "s", i);
2650 goto fail;
2651 }
2652 }
2653 if (n > m)
2654 i = n - m;
2655 else
2656 i = 0;
2657 for (; i < defcount; i++) {
2658 if (GETLOCAL(m+i) == NULL) {
2659 PyObject *def = defs[i];
2660 Py_INCREF(def);
2661 SETLOCAL(m+i, def);
2662 }
2663 }
2664 }
2665 }
2666 else {
2667 if (argcount > 0 || kwcount > 0) {
2668 PyErr_Format(PyExc_TypeError,
2669 "%.200s() takes no arguments (%d given)",
2670 PyString_AsString(co->co_name),
2671 argcount + kwcount);
2672 goto fail;
2673 }
2674 }
2675 /* Allocate and initialize storage for cell vars, and copy free
2676 vars into frame. This isn't too efficient right now. */
2677 if (f->f_ncells) {
2678 int i = 0, j = 0, nargs, found;
2679 char *cellname, *argname;
2680 PyObject *c;
2681
2682 nargs = co->co_argcount;
2683 if (co->co_flags & CO_VARARGS)
2684 nargs++;
2685 if (co->co_flags & CO_VARKEYWORDS)
2686 nargs++;
2687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 /* Initialize each cell var, taking into account
2689 cell vars that are initialized from arguments.
2690
2691 Should arrange for the compiler to put cellvars
2692 that are arguments at the beginning of the cellvars
2693 list so that we can march over it more efficiently?
2694 */
2695 for (i = 0; i < f->f_ncells; ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002696 cellname = PyString_AS_STRING(
2697 PyTuple_GET_ITEM(co->co_cellvars, i));
2698 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002700 argname = PyString_AS_STRING(
2701 PyTuple_GET_ITEM(co->co_varnames, j));
2702 if (strcmp(cellname, argname) == 0) {
2703 c = PyCell_New(GETLOCAL(j));
2704 if (c == NULL)
2705 goto fail;
2706 GETLOCAL(f->f_nlocals + i) = c;
2707 found = 1;
2708 break;
2709 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002710 }
2711 if (found == 0) {
2712 c = PyCell_New(NULL);
2713 if (c == NULL)
2714 goto fail;
2715 SETLOCAL(f->f_nlocals + i, c);
2716 }
2717 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002718 }
2719 if (f->f_nfreevars) {
2720 int i;
2721 for (i = 0; i < f->f_nfreevars; ++i) {
2722 PyObject *o = PyTuple_GET_ITEM(closure, i);
2723 Py_INCREF(o);
2724 freevars[f->f_ncells + i] = o;
2725 }
2726 }
2727
Tim Peters5ca576e2001-06-18 22:08:13 +00002728 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002729 /* Don't need to keep the reference to f_back, it will be set
2730 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002731 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002732 f->f_back = NULL;
2733
Jeremy Hylton985eba52003-02-05 23:13:00 +00002734 PCALL(PCALL_GENERATOR);
2735
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002736 /* Create a new generator that owns the ready to run frame
2737 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002738 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002739 }
2740
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002741 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002742
2743 fail: /* Jump here from prelude on failure */
2744
Tim Petersb13680b2001-11-27 23:29:29 +00002745 /* decref'ing the frame can cause __del__ methods to get invoked,
2746 which can call back into Python. While we're done with the
2747 current Python frame (f), the associated C stack is still in use,
2748 so recursion_depth must be boosted for the duration.
2749 */
2750 assert(tstate != NULL);
2751 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002752 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002753 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002754 return retval;
2755}
2756
2757
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002758/* Implementation notes for set_exc_info() and reset_exc_info():
2759
2760- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2761 'exc_traceback'. These always travel together.
2762
2763- tstate->curexc_ZZZ is the "hot" exception that is set by
2764 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2765
2766- Once an exception is caught by an except clause, it is transferred
2767 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2768 can pick it up. This is the primary task of set_exc_info().
2769
2770- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2771
2772 Long ago, when none of this existed, there were just a few globals:
2773 one set corresponding to the "hot" exception, and one set
2774 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2775 globals; they were simply stored as sys.exc_ZZZ. For backwards
2776 compatibility, they still are!) The problem was that in code like
2777 this:
2778
2779 try:
2780 "something that may fail"
2781 except "some exception":
2782 "do something else first"
2783 "print the exception from sys.exc_ZZZ."
2784
2785 if "do something else first" invoked something that raised and caught
2786 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2787 cause of subtle bugs. I fixed this by changing the semantics as
2788 follows:
2789
2790 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2791 *in that frame*.
2792
2793 - But initially, and as long as no exception is caught in a given
2794 frame, sys.exc_ZZZ will hold the last exception caught in the
2795 previous frame (or the frame before that, etc.).
2796
2797 The first bullet fixed the bug in the above example. The second
2798 bullet was for backwards compatibility: it was (and is) common to
2799 have a function that is called when an exception is caught, and to
2800 have that function access the caught exception via sys.exc_ZZZ.
2801 (Example: traceback.print_exc()).
2802
2803 At the same time I fixed the problem that sys.exc_ZZZ weren't
2804 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2805 but that's really a separate improvement.
2806
2807 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2808 variables to what they were before the current frame was called. The
2809 set_exc_info() function saves them on the frame so that
2810 reset_exc_info() can restore them. The invariant is that
2811 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2812 exception (where "catching" an exception applies only to successful
2813 except clauses); and if the current frame ever caught an exception,
2814 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2815 at the start of the current frame.
2816
2817*/
2818
Guido van Rossuma027efa1997-05-05 20:56:21 +00002819static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002820set_exc_info(PyThreadState *tstate,
2821 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002822{
2823 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002824 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002825
Guido van Rossuma027efa1997-05-05 20:56:21 +00002826 frame = tstate->frame;
2827 if (frame->f_exc_type == NULL) {
2828 /* This frame didn't catch an exception before */
2829 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002830 if (tstate->exc_type == NULL) {
2831 Py_INCREF(Py_None);
2832 tstate->exc_type = Py_None;
2833 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002834 tmp_type = frame->f_exc_type;
2835 tmp_value = frame->f_exc_value;
2836 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002837 Py_XINCREF(tstate->exc_type);
2838 Py_XINCREF(tstate->exc_value);
2839 Py_XINCREF(tstate->exc_traceback);
2840 frame->f_exc_type = tstate->exc_type;
2841 frame->f_exc_value = tstate->exc_value;
2842 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002843 Py_XDECREF(tmp_type);
2844 Py_XDECREF(tmp_value);
2845 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002846 }
2847 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002848 tmp_type = tstate->exc_type;
2849 tmp_value = tstate->exc_value;
2850 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002851 Py_XINCREF(type);
2852 Py_XINCREF(value);
2853 Py_XINCREF(tb);
2854 tstate->exc_type = type;
2855 tstate->exc_value = value;
2856 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002857 Py_XDECREF(tmp_type);
2858 Py_XDECREF(tmp_value);
2859 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002860 /* For b/w compatibility */
2861 PySys_SetObject("exc_type", type);
2862 PySys_SetObject("exc_value", value);
2863 PySys_SetObject("exc_traceback", tb);
2864}
2865
2866static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002867reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002868{
2869 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002870 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002871 frame = tstate->frame;
2872 if (frame->f_exc_type != NULL) {
2873 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002874 tmp_type = tstate->exc_type;
2875 tmp_value = tstate->exc_value;
2876 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002877 Py_XINCREF(frame->f_exc_type);
2878 Py_XINCREF(frame->f_exc_value);
2879 Py_XINCREF(frame->f_exc_traceback);
2880 tstate->exc_type = frame->f_exc_type;
2881 tstate->exc_value = frame->f_exc_value;
2882 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002883 Py_XDECREF(tmp_type);
2884 Py_XDECREF(tmp_value);
2885 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002886 /* For b/w compatibility */
2887 PySys_SetObject("exc_type", frame->f_exc_type);
2888 PySys_SetObject("exc_value", frame->f_exc_value);
2889 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2890 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002891 tmp_type = frame->f_exc_type;
2892 tmp_value = frame->f_exc_value;
2893 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002894 frame->f_exc_type = NULL;
2895 frame->f_exc_value = NULL;
2896 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002897 Py_XDECREF(tmp_type);
2898 Py_XDECREF(tmp_value);
2899 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002900}
2901
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002902/* Logic for the raise statement (too complicated for inlining).
2903 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002904static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002905do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002906{
Guido van Rossumd295f121998-04-09 21:39:57 +00002907 if (type == NULL) {
2908 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002909 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002910 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2911 value = tstate->exc_value;
2912 tb = tstate->exc_traceback;
2913 Py_XINCREF(type);
2914 Py_XINCREF(value);
2915 Py_XINCREF(tb);
2916 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002918 /* We support the following forms of raise:
2919 raise <class>, <classinstance>
2920 raise <class>, <argument tuple>
2921 raise <class>, None
2922 raise <class>, <argument>
2923 raise <classinstance>, None
2924 raise <string>, <object>
2925 raise <string>, None
2926
2927 An omitted second argument is the same as None.
2928
2929 In addition, raise <tuple>, <anything> is the same as
2930 raising the tuple's first item (and it better have one!);
2931 this rule is applied recursively.
2932
2933 Finally, an optional third argument can be supplied, which
2934 gives the traceback to be substituted (useful when
2935 re-raising an exception after examining it). */
2936
2937 /* First, check the traceback argument, replacing None with
2938 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002939 if (tb == Py_None) {
2940 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 tb = NULL;
2942 }
2943 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002944 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002945 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002946 goto raise_error;
2947 }
2948
2949 /* Next, replace a missing value with None */
2950 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002951 value = Py_None;
2952 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002953 }
2954
2955 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002956 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2957 PyObject *tmp = type;
2958 type = PyTuple_GET_ITEM(type, 0);
2959 Py_INCREF(type);
2960 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002961 }
2962
Tim Petersafb2c802002-04-18 18:06:20 +00002963 if (PyString_CheckExact(type))
2964 /* Raising builtin string is deprecated but still allowed --
2965 * do nothing. Raising an instance of a new-style str
2966 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002967 PyErr_Warn(PyExc_PendingDeprecationWarning,
2968 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002969
2970 else if (PyClass_Check(type))
2971 PyErr_NormalizeException(&type, &value, &tb);
2972
Guido van Rossumb209a111997-04-29 18:18:01 +00002973 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002974 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002975 if (value != Py_None) {
2976 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002977 "instance exception may not have a separate value");
2978 goto raise_error;
2979 }
2980 else {
2981 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002982 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002983 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002984 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2985 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002986 }
2987 }
2988 else {
2989 /* Not something you can raise. You get an exception
2990 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002991 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002992 "exceptions must be classes, instances, or "
2993 "strings (deprecated), not %s",
2994 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002995 goto raise_error;
2996 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002997 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002998 if (tb == NULL)
2999 return WHY_EXCEPTION;
3000 else
3001 return WHY_RERAISE;
3002 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003003 Py_XDECREF(value);
3004 Py_XDECREF(type);
3005 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003006 return WHY_EXCEPTION;
3007}
3008
Tim Petersd6d010b2001-06-21 02:49:55 +00003009/* Iterate v argcnt times and store the results on the stack (via decreasing
3010 sp). Return 1 for success, 0 if error. */
3011
Barry Warsawe42b18f1997-08-25 22:13:04 +00003012static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003013unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003014{
Tim Petersd6d010b2001-06-21 02:49:55 +00003015 int i = 0;
3016 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003017 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003018
Tim Petersd6d010b2001-06-21 02:49:55 +00003019 assert(v != NULL);
3020
3021 it = PyObject_GetIter(v);
3022 if (it == NULL)
3023 goto Error;
3024
3025 for (; i < argcnt; i++) {
3026 w = PyIter_Next(it);
3027 if (w == NULL) {
3028 /* Iterator done, via error or exhaustion. */
3029 if (!PyErr_Occurred()) {
3030 PyErr_Format(PyExc_ValueError,
3031 "need more than %d value%s to unpack",
3032 i, i == 1 ? "" : "s");
3033 }
3034 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003035 }
3036 *--sp = w;
3037 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003038
3039 /* We better have exhausted the iterator now. */
3040 w = PyIter_Next(it);
3041 if (w == NULL) {
3042 if (PyErr_Occurred())
3043 goto Error;
3044 Py_DECREF(it);
3045 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003046 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003047 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003048 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003049 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003050Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003051 for (; i > 0; i--, sp++)
3052 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003053 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003054 return 0;
3055}
3056
3057
Guido van Rossum96a42c81992-01-12 02:29:51 +00003058#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003059static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003060prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003061{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003062 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003063 if (PyObject_Print(v, stdout, 0) != 0)
3064 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003065 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003066 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003067}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003068#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003069
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003070static void
Fred Drake5755ce62001-06-27 19:19:46 +00003071call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003072{
Guido van Rossumb209a111997-04-29 18:18:01 +00003073 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003074 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003075 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003076 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003077 value = Py_None;
3078 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003079 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003080 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003081 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003082 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003083 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003084 }
Fred Drake5755ce62001-06-27 19:19:46 +00003085 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003086 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003087 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003088 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003089 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003090 Py_XDECREF(type);
3091 Py_XDECREF(value);
3092 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003093 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003094}
3095
Fred Drake4ec5d562001-10-04 19:26:43 +00003096static void
3097call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003098 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003099{
3100 PyObject *type, *value, *traceback;
3101 int err;
3102 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003103 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003104 if (err == 0)
3105 PyErr_Restore(type, value, traceback);
3106 else {
3107 Py_XDECREF(type);
3108 Py_XDECREF(value);
3109 Py_XDECREF(traceback);
3110 }
3111}
3112
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003113static int
Fred Drake5755ce62001-06-27 19:19:46 +00003114call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3115 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003116{
Fred Drake5755ce62001-06-27 19:19:46 +00003117 register PyThreadState *tstate = frame->f_tstate;
3118 int result;
3119 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003120 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003121 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003122 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003123 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003124 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3125 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003126 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003127 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003128}
3129
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003130PyObject *
3131_PyEval_CallTracing(PyObject *func, PyObject *args)
3132{
3133 PyFrameObject *frame = PyEval_GetFrame();
3134 PyThreadState *tstate = frame->f_tstate;
3135 int save_tracing = tstate->tracing;
3136 int save_use_tracing = tstate->use_tracing;
3137 PyObject *result;
3138
3139 tstate->tracing = 0;
3140 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3141 || (tstate->c_profilefunc != NULL));
3142 result = PyObject_Call(func, args, NULL);
3143 tstate->tracing = save_tracing;
3144 tstate->use_tracing = save_use_tracing;
3145 return result;
3146}
3147
Michael W. Hudson006c7522002-11-08 13:08:46 +00003148static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003149maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003150 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3151 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003152{
3153 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003154
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003155 In a nutshell, we use the co_lnotab field of the code object
3156 to tell when execution has moved onto a different line.
3157
3158 As mentioned above, the basic idea is so set things up so
3159 that
3160
3161 *instr_lb <= frame->f_lasti < *instr_ub
3162
3163 is true so long as execution does not change lines.
3164
3165 This is all fairly simple. Digging the information out of
3166 co_lnotab takes some work, but is conceptually clear.
3167
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003168 Somewhat harder to explain is why we don't *always* call the
3169 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170
3171 Consider this code:
3172
3173 1: def f(a):
3174 2: if a:
3175 3: print 1
3176 4: else:
3177 5: print 2
3178
3179 which compiles to this:
3180
3181 2 0 LOAD_FAST 0 (a)
3182 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003183 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003184
3185 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003186 10 PRINT_ITEM
3187 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003189 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003190
3191 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003192 19 PRINT_ITEM
3193 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003194 >> 21 LOAD_CONST 0 (None)
3195 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003196
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003197 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003198 15 and the co_lnotab will claim that execution has moved to
3199 line 3. This is at best misleading. In this case we could
3200 associate the POP_TOP with line 4, but that doesn't make
3201 sense in all cases (I think).
3202
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003203 What we do is only call the line trace function if the co_lnotab
3204 indicates we have jumped to the *start* of a line, i.e. if the
3205 current instruction offset matches the offset given for the
3206 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003207
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003208 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003209 Execution will jump from instruction offset 12 to offset 21.
3210 Then the co_lnotab would imply that execution has moved to line
3211 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003212
3213 Why do we set f_lineno when tracing? Well, consider the code
3214 above when 'a' is true. If stepping through this with 'n' in
3215 pdb, you would stop at line 1 with a "call" type event, then
3216 line events on lines 2 and 3, then a "return" type event -- but
3217 you would be shown line 5 during this event. This is a change
3218 from the behaviour in 2.2 and before, and I've found it
3219 confusing in practice. By setting and using f_lineno when
3220 tracing, one can report a line number different from that
3221 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003222 */
3223
Michael W. Hudson006c7522002-11-08 13:08:46 +00003224 int result = 0;
3225
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003226 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003227 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003228 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003229 unsigned char* p;
3230
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003231 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3232 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003233
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003234 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003235 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003236
3237 /* possible optimization: if f->f_lasti == instr_ub
3238 (likely to be a common case) then we already know
3239 instr_lb -- if we stored the matching value of p
3240 somwhere we could skip the first while loop. */
3241
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003242 /* see comments in compile.c for the description of
3243 co_lnotab. A point to remember: increments to p
3244 should come in pairs -- although we don't care about
3245 the line increments here, treating them as byte
3246 increments gets confusing, to say the least. */
3247
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003248 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003249 if (addr + *p > frame->f_lasti)
3250 break;
3251 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003252 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003253 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003254 --size;
3255 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003256
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003257 if (addr == frame->f_lasti) {
3258 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003259 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003260 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003261 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003262
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003263 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003264 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003265 addr += *p++;
3266 if (*p++)
3267 break;
3268 }
3269 *instr_ub = addr;
3270 }
3271 else {
3272 *instr_ub = INT_MAX;
3273 }
3274 }
Armin Rigobf57a142004-03-22 19:24:58 +00003275 else if (frame->f_lasti <= *instr_prev) {
3276 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003277 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003278 PyTrace_LINE, Py_None);
3279 }
3280 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003281 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003282}
3283
Fred Drake5755ce62001-06-27 19:19:46 +00003284void
3285PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003286{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003287 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003288 PyObject *temp = tstate->c_profileobj;
3289 Py_XINCREF(arg);
3290 tstate->c_profilefunc = NULL;
3291 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003292 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003293 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003294 Py_XDECREF(temp);
3295 tstate->c_profilefunc = func;
3296 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003297 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003298 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003299}
3300
3301void
3302PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3303{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003304 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003305 PyObject *temp = tstate->c_traceobj;
3306 Py_XINCREF(arg);
3307 tstate->c_tracefunc = NULL;
3308 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003309 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003310 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003311 Py_XDECREF(temp);
3312 tstate->c_tracefunc = func;
3313 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003314 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003315 tstate->use_tracing = ((func != NULL)
3316 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003317}
3318
Guido van Rossumb209a111997-04-29 18:18:01 +00003319PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003320PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003321{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003322 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003323 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003324 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003325 else
3326 return current_frame->f_builtins;
3327}
3328
Guido van Rossumb209a111997-04-29 18:18:01 +00003329PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003330PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003331{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003332 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003333 if (current_frame == NULL)
3334 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003335 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003336 return current_frame->f_locals;
3337}
3338
Guido van Rossumb209a111997-04-29 18:18:01 +00003339PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003340PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003341{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003342 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003343 if (current_frame == NULL)
3344 return NULL;
3345 else
3346 return current_frame->f_globals;
3347}
3348
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003349PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003350PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003351{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003352 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003353 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003354}
3355
Guido van Rossum6135a871995-01-09 17:53:26 +00003356int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003357PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003358{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003359 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003360 return current_frame == NULL ? 0 : current_frame->f_restricted;
3361}
3362
Guido van Rossumbe270261997-05-22 22:26:18 +00003363int
Tim Peters5ba58662001-07-16 02:29:45 +00003364PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003365{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003366 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003367 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003368
3369 if (current_frame != NULL) {
3370 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003371 const int compilerflags = codeflags & PyCF_MASK;
3372 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003373 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003374 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003375 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003376#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003377 if (codeflags & CO_GENERATOR_ALLOWED) {
3378 result = 1;
3379 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3380 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003381#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003382 }
3383 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003384}
3385
3386int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003387Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003388{
Guido van Rossumb209a111997-04-29 18:18:01 +00003389 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003390 if (f == NULL)
3391 return 0;
3392 if (!PyFile_SoftSpace(f, 0))
3393 return 0;
3394 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003395}
3396
Guido van Rossum3f5da241990-12-20 15:06:42 +00003397
Guido van Rossum681d79a1995-07-18 14:51:37 +00003398/* External interface to call any callable object.
3399 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003400
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003401#undef PyEval_CallObject
3402/* for backward compatibility: export this interface */
3403
Guido van Rossumb209a111997-04-29 18:18:01 +00003404PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003405PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003406{
Guido van Rossumb209a111997-04-29 18:18:01 +00003407 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003408}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003409#define PyEval_CallObject(func,arg) \
3410 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003411
Guido van Rossumb209a111997-04-29 18:18:01 +00003412PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003413PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003414{
Jeremy Hylton52820442001-01-03 23:52:36 +00003415 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003416
3417 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003418 arg = PyTuple_New(0);
3419 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003420 PyErr_SetString(PyExc_TypeError,
3421 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003422 return NULL;
3423 }
3424 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003425 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003426
Guido van Rossumb209a111997-04-29 18:18:01 +00003427 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003428 PyErr_SetString(PyExc_TypeError,
3429 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003430 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003431 return NULL;
3432 }
3433
Tim Peters6d6c1a32001-08-02 04:15:00 +00003434 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003435 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003436 return result;
3437}
3438
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003439const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003440PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003441{
3442 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003443 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003444 else if (PyFunction_Check(func))
3445 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3446 else if (PyCFunction_Check(func))
3447 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3448 else if (PyClass_Check(func))
3449 return PyString_AsString(((PyClassObject*)func)->cl_name);
3450 else if (PyInstance_Check(func)) {
3451 return PyString_AsString(
3452 ((PyInstanceObject*)func)->in_class->cl_name);
3453 } else {
3454 return func->ob_type->tp_name;
3455 }
3456}
3457
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003458const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003459PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003460{
3461 if (PyMethod_Check(func))
3462 return "()";
3463 else if (PyFunction_Check(func))
3464 return "()";
3465 else if (PyCFunction_Check(func))
3466 return "()";
3467 else if (PyClass_Check(func))
3468 return " constructor";
3469 else if (PyInstance_Check(func)) {
3470 return " instance";
3471 } else {
3472 return " object";
3473 }
3474}
3475
Jeremy Hylton52820442001-01-03 23:52:36 +00003476#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3477
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003478static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003479err_args(PyObject *func, int flags, int nargs)
3480{
3481 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003482 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003483 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003484 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003485 nargs);
3486 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003487 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003488 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003489 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003490 nargs);
3491}
3492
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003493#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003494if (tstate->use_tracing && tstate->c_profilefunc) { \
3495 if (call_trace(tstate->c_profilefunc, \
3496 tstate->c_profileobj, \
3497 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003498 func)) { \
3499 x = NULL; \
3500 } \
3501 else { \
3502 x = call; \
3503 if (tstate->c_profilefunc != NULL) { \
3504 if (x == NULL) { \
3505 call_trace_protected(tstate->c_profilefunc, \
3506 tstate->c_profileobj, \
3507 tstate->frame, PyTrace_C_EXCEPTION, \
3508 func); \
3509 /* XXX should pass (type, value, tb) */ \
3510 } else { \
3511 if (call_trace(tstate->c_profilefunc, \
3512 tstate->c_profileobj, \
3513 tstate->frame, PyTrace_C_RETURN, \
3514 func)) { \
3515 Py_DECREF(x); \
3516 x = NULL; \
3517 } \
3518 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003519 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003520 } \
3521} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003522 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003523 }
3524
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003525static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003526call_function(PyObject ***pp_stack, int oparg
3527#ifdef WITH_TSC
3528 , uint64* pintr0, uint64* pintr1
3529#endif
3530 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003531{
3532 int na = oparg & 0xff;
3533 int nk = (oparg>>8) & 0xff;
3534 int n = na + 2 * nk;
3535 PyObject **pfunc = (*pp_stack) - n - 1;
3536 PyObject *func = *pfunc;
3537 PyObject *x, *w;
3538
Jeremy Hylton985eba52003-02-05 23:13:00 +00003539 /* Always dispatch PyCFunction first, because these are
3540 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003541 */
3542 if (PyCFunction_Check(func) && nk == 0) {
3543 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003544 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003545
3546 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003547 if (flags & (METH_NOARGS | METH_O)) {
3548 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3549 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003550 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003551 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003552 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003553 else if (flags & METH_O && na == 1) {
3554 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003555 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003556 Py_DECREF(arg);
3557 }
3558 else {
3559 err_args(func, flags, na);
3560 x = NULL;
3561 }
3562 }
3563 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003564 PyObject *callargs;
3565 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003566 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003567 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003568 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003569 Py_XDECREF(callargs);
3570 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003571 } else {
3572 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3573 /* optimize access to bound methods */
3574 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003575 PCALL(PCALL_METHOD);
3576 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003577 Py_INCREF(self);
3578 func = PyMethod_GET_FUNCTION(func);
3579 Py_INCREF(func);
3580 Py_DECREF(*pfunc);
3581 *pfunc = self;
3582 na++;
3583 n++;
3584 } else
3585 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003586 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003587 if (PyFunction_Check(func))
3588 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003589 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003590 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003591 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003592 Py_DECREF(func);
3593 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003594
Thomas Wouters03ca23d2006-02-10 22:51:45 +00003595 /* Clear the stack of the function object and the arguments,
3596 in case they weren't consumed already */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003597 while ((*pp_stack) > pfunc) {
3598 w = EXT_POP(*pp_stack);
3599 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003600 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003601 }
3602 return x;
3603}
3604
Jeremy Hylton192690e2002-08-16 18:36:11 +00003605/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003606 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003607 For the simplest case -- a function that takes only positional
3608 arguments and is called with only positional arguments -- it
3609 inlines the most primitive frame setup code from
3610 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3611 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003612*/
3613
3614static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003615fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003616{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003617 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003618 PyObject *globals = PyFunction_GET_GLOBALS(func);
3619 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3620 PyObject **d = NULL;
3621 int nd = 0;
3622
Jeremy Hylton985eba52003-02-05 23:13:00 +00003623 PCALL(PCALL_FUNCTION);
3624 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003625 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003626 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3627 PyFrameObject *f;
3628 PyObject *retval = NULL;
3629 PyThreadState *tstate = PyThreadState_GET();
3630 PyObject **fastlocals, **stack;
3631 int i;
3632
3633 PCALL(PCALL_FASTER_FUNCTION);
3634 assert(globals != NULL);
3635 /* XXX Perhaps we should create a specialized
3636 PyFrame_New() that doesn't take locals, but does
3637 take builtins without sanity checking them.
3638 */
3639 f = PyFrame_New(tstate, co, globals, NULL);
3640 if (f == NULL)
3641 return NULL;
3642
3643 fastlocals = f->f_localsplus;
3644 stack = (*pp_stack) - n;
3645
3646 for (i = 0; i < n; i++) {
3647 Py_INCREF(*stack);
3648 fastlocals[i] = *stack++;
3649 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003650 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003651 assert(tstate != NULL);
3652 ++tstate->recursion_depth;
3653 Py_DECREF(f);
3654 --tstate->recursion_depth;
3655 return retval;
3656 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003657 if (argdefs != NULL) {
3658 d = &PyTuple_GET_ITEM(argdefs, 0);
3659 nd = ((PyTupleObject *)argdefs)->ob_size;
3660 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003661 return PyEval_EvalCodeEx(co, globals,
3662 (PyObject *)NULL, (*pp_stack)-n, na,
3663 (*pp_stack)-2*nk, nk, d, nd,
3664 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003665}
3666
3667static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003668update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3669 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003670{
3671 PyObject *kwdict = NULL;
3672 if (orig_kwdict == NULL)
3673 kwdict = PyDict_New();
3674 else {
3675 kwdict = PyDict_Copy(orig_kwdict);
3676 Py_DECREF(orig_kwdict);
3677 }
3678 if (kwdict == NULL)
3679 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003680 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003681 int err;
3682 PyObject *value = EXT_POP(*pp_stack);
3683 PyObject *key = EXT_POP(*pp_stack);
3684 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003685 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003686 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003687 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003688 PyEval_GetFuncName(func),
3689 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003690 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003691 Py_DECREF(key);
3692 Py_DECREF(value);
3693 Py_DECREF(kwdict);
3694 return NULL;
3695 }
3696 err = PyDict_SetItem(kwdict, key, value);
3697 Py_DECREF(key);
3698 Py_DECREF(value);
3699 if (err) {
3700 Py_DECREF(kwdict);
3701 return NULL;
3702 }
3703 }
3704 return kwdict;
3705}
3706
3707static PyObject *
3708update_star_args(int nstack, int nstar, PyObject *stararg,
3709 PyObject ***pp_stack)
3710{
3711 PyObject *callargs, *w;
3712
3713 callargs = PyTuple_New(nstack + nstar);
3714 if (callargs == NULL) {
3715 return NULL;
3716 }
3717 if (nstar) {
3718 int i;
3719 for (i = 0; i < nstar; i++) {
3720 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3721 Py_INCREF(a);
3722 PyTuple_SET_ITEM(callargs, nstack + i, a);
3723 }
3724 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003725 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003726 w = EXT_POP(*pp_stack);
3727 PyTuple_SET_ITEM(callargs, nstack, w);
3728 }
3729 return callargs;
3730}
3731
3732static PyObject *
3733load_args(PyObject ***pp_stack, int na)
3734{
3735 PyObject *args = PyTuple_New(na);
3736 PyObject *w;
3737
3738 if (args == NULL)
3739 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003740 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003741 w = EXT_POP(*pp_stack);
3742 PyTuple_SET_ITEM(args, na, w);
3743 }
3744 return args;
3745}
3746
3747static PyObject *
3748do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3749{
3750 PyObject *callargs = NULL;
3751 PyObject *kwdict = NULL;
3752 PyObject *result = NULL;
3753
3754 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003755 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003756 if (kwdict == NULL)
3757 goto call_fail;
3758 }
3759 callargs = load_args(pp_stack, na);
3760 if (callargs == NULL)
3761 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003762#ifdef CALL_PROFILE
3763 /* At this point, we have to look at the type of func to
3764 update the call stats properly. Do it here so as to avoid
3765 exposing the call stats machinery outside ceval.c
3766 */
3767 if (PyFunction_Check(func))
3768 PCALL(PCALL_FUNCTION);
3769 else if (PyMethod_Check(func))
3770 PCALL(PCALL_METHOD);
3771 else if (PyType_Check(func))
3772 PCALL(PCALL_TYPE);
3773 else
3774 PCALL(PCALL_OTHER);
3775#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003776 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003777 call_fail:
3778 Py_XDECREF(callargs);
3779 Py_XDECREF(kwdict);
3780 return result;
3781}
3782
3783static PyObject *
3784ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3785{
3786 int nstar = 0;
3787 PyObject *callargs = NULL;
3788 PyObject *stararg = NULL;
3789 PyObject *kwdict = NULL;
3790 PyObject *result = NULL;
3791
3792 if (flags & CALL_FLAG_KW) {
3793 kwdict = EXT_POP(*pp_stack);
3794 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003795 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003796 "%s%s argument after ** "
3797 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003798 PyEval_GetFuncName(func),
3799 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003800 goto ext_call_fail;
3801 }
3802 }
3803 if (flags & CALL_FLAG_VAR) {
3804 stararg = EXT_POP(*pp_stack);
3805 if (!PyTuple_Check(stararg)) {
3806 PyObject *t = NULL;
3807 t = PySequence_Tuple(stararg);
3808 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003809 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3810 PyErr_Format(PyExc_TypeError,
3811 "%s%s argument after * "
3812 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003813 PyEval_GetFuncName(func),
3814 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003815 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003816 goto ext_call_fail;
3817 }
3818 Py_DECREF(stararg);
3819 stararg = t;
3820 }
3821 nstar = PyTuple_GET_SIZE(stararg);
3822 }
3823 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003824 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003825 if (kwdict == NULL)
3826 goto ext_call_fail;
3827 }
3828 callargs = update_star_args(na, nstar, stararg, pp_stack);
3829 if (callargs == NULL)
3830 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003831#ifdef CALL_PROFILE
3832 /* At this point, we have to look at the type of func to
3833 update the call stats properly. Do it here so as to avoid
3834 exposing the call stats machinery outside ceval.c
3835 */
3836 if (PyFunction_Check(func))
3837 PCALL(PCALL_FUNCTION);
3838 else if (PyMethod_Check(func))
3839 PCALL(PCALL_METHOD);
3840 else if (PyType_Check(func))
3841 PCALL(PCALL_TYPE);
3842 else
3843 PCALL(PCALL_OTHER);
3844#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003845 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003846 ext_call_fail:
3847 Py_XDECREF(callargs);
3848 Py_XDECREF(kwdict);
3849 Py_XDECREF(stararg);
3850 return result;
3851}
3852
Tim Peterscb479e72001-12-16 19:11:44 +00003853/* Extract a slice index from a PyInt or PyLong, and store in *pi.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003854 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3855 and silently boost values less than -PY_SSIZE_T_MAX to 0.
3856 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003857*/
Tim Petersb5196382001-12-16 19:44:20 +00003858/* Note: If v is NULL, return success without storing into *pi. This
3859 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3860 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003861*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003862int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003863_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864{
Tim Petersb5196382001-12-16 19:44:20 +00003865 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003866 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003867 if (PyInt_Check(v)) {
3868 x = PyInt_AsLong(v);
3869 } else if (PyLong_Check(v)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003870 x = PyInt_AsSsize_t(v);
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003871 if (x==-1 && PyErr_Occurred()) {
3872 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003873 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003874
Guido van Rossumac7be682001-01-17 15:42:30 +00003875 if (!PyErr_ExceptionMatches(
3876 PyExc_OverflowError)) {
3877 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003878 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003879 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003880 }
3881
Guido van Rossumac7be682001-01-17 15:42:30 +00003882 /* Clear the OverflowError */
3883 PyErr_Clear();
3884
3885 /* It's an overflow error, so we need to
3886 check the sign of the long integer,
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003887 set the value to PY_SSIZE_T_MAX or
3888 -PY_SSIZE_T_MAX, and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003889
3890 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003891 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003892 if (long_zero == NULL)
3893 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003894
3895 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003896 cmp = PyObject_RichCompareBool(v, long_zero,
3897 Py_GT);
3898 Py_DECREF(long_zero);
3899 if (cmp < 0)
3900 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003901 else if (cmp)
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003902 x = PY_SSIZE_T_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003903 else
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003904 x = -PY_SSIZE_T_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003905 }
3906 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003907 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003908 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003909 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003911 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003912 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003913 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003914}
3915
Guido van Rossum50d756e2001-08-18 17:43:36 +00003916#undef ISINT
3917#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3918
Guido van Rossumb209a111997-04-29 18:18:01 +00003919static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003920apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003921{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003922 PyTypeObject *tp = u->ob_type;
3923 PySequenceMethods *sq = tp->tp_as_sequence;
3924
3925 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003926 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003927 if (!_PyEval_SliceIndex(v, &ilow))
3928 return NULL;
3929 if (!_PyEval_SliceIndex(w, &ihigh))
3930 return NULL;
3931 return PySequence_GetSlice(u, ilow, ihigh);
3932 }
3933 else {
3934 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003935 if (slice != NULL) {
3936 PyObject *res = PyObject_GetItem(u, slice);
3937 Py_DECREF(slice);
3938 return res;
3939 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003940 else
3941 return NULL;
3942 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003943}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003944
3945static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003946assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3947 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003948{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003949 PyTypeObject *tp = u->ob_type;
3950 PySequenceMethods *sq = tp->tp_as_sequence;
3951
3952 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003953 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003954 if (!_PyEval_SliceIndex(v, &ilow))
3955 return -1;
3956 if (!_PyEval_SliceIndex(w, &ihigh))
3957 return -1;
3958 if (x == NULL)
3959 return PySequence_DelSlice(u, ilow, ihigh);
3960 else
3961 return PySequence_SetSlice(u, ilow, ihigh, x);
3962 }
3963 else {
3964 PyObject *slice = PySlice_New(v, w, NULL);
3965 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003966 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003967 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003968 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003969 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003970 res = PyObject_DelItem(u, slice);
3971 Py_DECREF(slice);
3972 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003973 }
3974 else
3975 return -1;
3976 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003977}
3978
Guido van Rossumb209a111997-04-29 18:18:01 +00003979static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003980cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003981{
Guido van Rossumac7be682001-01-17 15:42:30 +00003982 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003984 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003985 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003986 break;
3987 case PyCmp_IS_NOT:
3988 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003989 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003990 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003991 res = PySequence_Contains(w, v);
3992 if (res < 0)
3993 return NULL;
3994 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003995 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003996 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003997 if (res < 0)
3998 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003999 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004000 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004001 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004002 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004003 break;
4004 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004005 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004006 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004007 v = res ? Py_True : Py_False;
4008 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004009 return v;
4010}
4011
Thomas Wouters52152252000-08-17 22:55:00 +00004012static PyObject *
4013import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004014{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004015 PyObject *x;
4016
4017 x = PyObject_GetAttr(v, name);
4018 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004019 PyErr_Format(PyExc_ImportError,
4020 "cannot import name %.230s",
4021 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004022 }
Thomas Wouters52152252000-08-17 22:55:00 +00004023 return x;
4024}
Guido van Rossumac7be682001-01-17 15:42:30 +00004025
Thomas Wouters52152252000-08-17 22:55:00 +00004026static int
4027import_all_from(PyObject *locals, PyObject *v)
4028{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004029 PyObject *all = PyObject_GetAttrString(v, "__all__");
4030 PyObject *dict, *name, *value;
4031 int skip_leading_underscores = 0;
4032 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004033
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004034 if (all == NULL) {
4035 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4036 return -1; /* Unexpected error */
4037 PyErr_Clear();
4038 dict = PyObject_GetAttrString(v, "__dict__");
4039 if (dict == NULL) {
4040 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4041 return -1;
4042 PyErr_SetString(PyExc_ImportError,
4043 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004044 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004045 }
4046 all = PyMapping_Keys(dict);
4047 Py_DECREF(dict);
4048 if (all == NULL)
4049 return -1;
4050 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004051 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004052
4053 for (pos = 0, err = 0; ; pos++) {
4054 name = PySequence_GetItem(all, pos);
4055 if (name == NULL) {
4056 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4057 err = -1;
4058 else
4059 PyErr_Clear();
4060 break;
4061 }
4062 if (skip_leading_underscores &&
4063 PyString_Check(name) &&
4064 PyString_AS_STRING(name)[0] == '_')
4065 {
4066 Py_DECREF(name);
4067 continue;
4068 }
4069 value = PyObject_GetAttr(v, name);
4070 if (value == NULL)
4071 err = -1;
4072 else
4073 err = PyDict_SetItem(locals, name, value);
4074 Py_DECREF(name);
4075 Py_XDECREF(value);
4076 if (err != 0)
4077 break;
4078 }
4079 Py_DECREF(all);
4080 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004081}
4082
Guido van Rossumb209a111997-04-29 18:18:01 +00004083static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004084build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004085{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004086 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004087
4088 if (PyDict_Check(methods))
4089 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004090 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004091 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004092 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4093 base = PyTuple_GET_ITEM(bases, 0);
4094 metaclass = PyObject_GetAttrString(base, "__class__");
4095 if (metaclass == NULL) {
4096 PyErr_Clear();
4097 metaclass = (PyObject *)base->ob_type;
4098 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004099 }
4100 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004101 else {
4102 PyObject *g = PyEval_GetGlobals();
4103 if (g != NULL && PyDict_Check(g))
4104 metaclass = PyDict_GetItemString(g, "__metaclass__");
4105 if (metaclass == NULL)
4106 metaclass = (PyObject *) &PyClass_Type;
4107 Py_INCREF(metaclass);
4108 }
4109 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4110 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004111 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4112 /* A type error here likely means that the user passed
4113 in a base that was not a class (such the random module
4114 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004115 by augmenting the error message with more information.*/
4116
4117 PyObject *ptype, *pvalue, *ptraceback;
4118
4119 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4120 if (PyString_Check(pvalue)) {
4121 PyObject *newmsg;
4122 newmsg = PyString_FromFormat(
4123 "Error when calling the metaclass bases\n %s",
4124 PyString_AS_STRING(pvalue));
4125 if (newmsg != NULL) {
4126 Py_DECREF(pvalue);
4127 pvalue = newmsg;
4128 }
4129 }
4130 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004131 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004132 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004133}
4134
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004135static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004136exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4137 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004138{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004139 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004140 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004141 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004142
Guido van Rossumb209a111997-04-29 18:18:01 +00004143 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4144 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004145 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004146 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004147 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 locals = PyTuple_GetItem(prog, 2);
4149 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004150 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004151 if (globals == Py_None) {
4152 globals = PyEval_GetGlobals();
4153 if (locals == Py_None) {
4154 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004155 plain = 1;
4156 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004157 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004158 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004159 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004160 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004161 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004162 !PyCode_Check(prog) &&
4163 !PyFile_Check(prog)) {
4164 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004165 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004166 return -1;
4167 }
Fred Drake661ea262000-10-24 19:57:45 +00004168 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004169 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004170 "exec: arg 2 must be a dictionary or None");
4171 return -1;
4172 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004173 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004174 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004175 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004176 return -1;
4177 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004178 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004179 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004180 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004181 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4182 PyErr_SetString(PyExc_TypeError,
4183 "code object passed to exec may not contain free variables");
4184 return -1;
4185 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004186 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004187 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004188 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004189 FILE *fp = PyFile_AsFile(prog);
4190 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004191 PyCompilerFlags cf;
4192 cf.cf_flags = 0;
4193 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004194 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004195 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004196 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004197 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004198 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004199 }
4200 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004201 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004202 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004203 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004204 cf.cf_flags = 0;
4205#ifdef Py_USING_UNICODE
4206 if (PyUnicode_Check(prog)) {
4207 tmp = PyUnicode_AsUTF8String(prog);
4208 if (tmp == NULL)
4209 return -1;
4210 prog = tmp;
4211 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4212 }
4213#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004214 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004215 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004216 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004217 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004218 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004219 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004220 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004221 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004222 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004223 if (plain)
4224 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004225 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004226 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004227 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004228 return 0;
4229}
Guido van Rossum24c13741995-02-14 09:42:43 +00004230
Guido van Rossumac7be682001-01-17 15:42:30 +00004231static void
Paul Prescode68140d2000-08-30 20:25:01 +00004232format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4233{
4234 char *obj_str;
4235
4236 if (!obj)
4237 return;
4238
4239 obj_str = PyString_AsString(obj);
4240 if (!obj_str)
4241 return;
4242
4243 PyErr_Format(exc, format_str, obj_str);
4244}
Guido van Rossum950361c1997-01-24 13:49:28 +00004245
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004246static PyObject *
4247string_concatenate(PyObject *v, PyObject *w,
4248 PyFrameObject *f, unsigned char *next_instr)
4249{
4250 /* This function implements 'variable += expr' when both arguments
4251 are strings. */
4252
4253 if (v->ob_refcnt == 2) {
4254 /* In the common case, there are 2 references to the value
4255 * stored in 'variable' when the += is performed: one on the
4256 * value stack (in 'v') and one still stored in the 'variable'.
4257 * We try to delete the variable now to reduce the refcnt to 1.
4258 */
4259 switch (*next_instr) {
4260 case STORE_FAST:
4261 {
4262 int oparg = PEEKARG();
4263 PyObject **fastlocals = f->f_localsplus;
4264 if (GETLOCAL(oparg) == v)
4265 SETLOCAL(oparg, NULL);
4266 break;
4267 }
4268 case STORE_DEREF:
4269 {
4270 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4271 PyObject *c = freevars[PEEKARG()];
4272 if (PyCell_GET(c) == v)
4273 PyCell_Set(c, NULL);
4274 break;
4275 }
4276 case STORE_NAME:
4277 {
4278 PyObject *names = f->f_code->co_names;
4279 PyObject *name = GETITEM(names, PEEKARG());
4280 PyObject *locals = f->f_locals;
4281 if (PyDict_CheckExact(locals) &&
4282 PyDict_GetItem(locals, name) == v) {
4283 if (PyDict_DelItem(locals, name) != 0) {
4284 PyErr_Clear();
4285 }
4286 }
4287 break;
4288 }
4289 }
4290 }
4291
Armin Rigo618fbf52004-08-07 20:58:32 +00004292 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004293 /* Now we own the last reference to 'v', so we can resize it
4294 * in-place.
4295 */
4296 int v_len = PyString_GET_SIZE(v);
4297 int w_len = PyString_GET_SIZE(w);
4298 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4299 /* XXX if _PyString_Resize() fails, 'v' has been
4300 * deallocated so it cannot be put back into 'variable'.
4301 * The MemoryError is raised when there is no value in
4302 * 'variable', which might (very remotely) be a cause
4303 * of incompatibilities.
4304 */
4305 return NULL;
4306 }
4307 /* copy 'w' into the newly allocated area of 'v' */
4308 memcpy(PyString_AS_STRING(v) + v_len,
4309 PyString_AS_STRING(w), w_len);
4310 return v;
4311 }
4312 else {
4313 /* When in-place resizing is not an option. */
4314 PyString_Concat(&v, w);
4315 return v;
4316 }
4317}
4318
Guido van Rossum950361c1997-01-24 13:49:28 +00004319#ifdef DYNAMIC_EXECUTION_PROFILE
4320
Skip Montanarof118cb12001-10-15 20:51:38 +00004321static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004322getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004323{
4324 int i;
4325 PyObject *l = PyList_New(256);
4326 if (l == NULL) return NULL;
4327 for (i = 0; i < 256; i++) {
4328 PyObject *x = PyInt_FromLong(a[i]);
4329 if (x == NULL) {
4330 Py_DECREF(l);
4331 return NULL;
4332 }
4333 PyList_SetItem(l, i, x);
4334 }
4335 for (i = 0; i < 256; i++)
4336 a[i] = 0;
4337 return l;
4338}
4339
4340PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004341_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004342{
4343#ifndef DXPAIRS
4344 return getarray(dxp);
4345#else
4346 int i;
4347 PyObject *l = PyList_New(257);
4348 if (l == NULL) return NULL;
4349 for (i = 0; i < 257; i++) {
4350 PyObject *x = getarray(dxpairs[i]);
4351 if (x == NULL) {
4352 Py_DECREF(l);
4353 return NULL;
4354 }
4355 PyList_SetItem(l, i, x);
4356 }
4357 return l;
4358#endif
4359}
4360
4361#endif