blob: 777e981f1d69a6d9cd51b71fcd5ad4cb1ef13bcd [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
Guido van Rossumd076c731998-10-07 19:42:25 +0000602#define INSTR_OFFSET() (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
640#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
641#define EMPTY() (STACK_LEVEL() == 0)
642#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define SECOND() (stack_pointer[-2])
644#define THIRD() (stack_pointer[-3])
645#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define SET_TOP(v) (stack_pointer[-1] = (v))
647#define SET_SECOND(v) (stack_pointer[-2] = (v))
648#define SET_THIRD(v) (stack_pointer[-3] = (v))
649#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000650#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000651#define BASIC_PUSH(v) (*stack_pointer++ = (v))
652#define BASIC_POP() (*--stack_pointer)
653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000655#define PUSH(v) { (void)(BASIC_PUSH(v), \
656 lltrace && prtrace(TOP(), "push")); \
657 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000658#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
660 lltrace && prtrace(TOP(), "stackadj")); \
661 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000662#else
663#define PUSH(v) BASIC_PUSH(v)
664#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000665#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000666#endif
667
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668/* Local variable macros */
669
670#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000671
672/* The SETLOCAL() macro must not DECREF the local variable in-place and
673 then store the new value; it must copy the old value to a temporary
674 value, then store the new value, and then DECREF the temporary value.
675 This is because it is possible that during the DECREF the frame is
676 accessed by other code (e.g. a __del__ method or gc.collect()) and the
677 variable would be pointing to already-freed memory. */
678#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
679 GETLOCAL(i) = value; \
680 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000681
Guido van Rossuma027efa1997-05-05 20:56:21 +0000682/* Start of code */
683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 if (f == NULL)
685 return NULL;
686
Armin Rigo1d313ab2003-10-25 14:33:09 +0000687 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000688 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000689 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000690
Tim Peters5ca576e2001-06-18 22:08:13 +0000691 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000692
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000693 if (tstate->use_tracing) {
694 if (tstate->c_tracefunc != NULL) {
695 /* tstate->c_tracefunc, if defined, is a
696 function that will be called on *every* entry
697 to a code block. Its return value, if not
698 None, is a function that will be called at
699 the start of each executed line of code.
700 (Actually, the function must return itself
701 in order to continue tracing.) The trace
702 functions are called with three arguments:
703 a pointer to the current frame, a string
704 indicating why the function is called, and
705 an argument which depends on the situation.
706 The global trace function is also called
707 whenever an exception is detected. */
708 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
709 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000710 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 }
713 }
714 if (tstate->c_profilefunc != NULL) {
715 /* Similar for c_profilefunc, except it needn't
716 return itself and isn't called for "line" events */
717 if (call_trace(tstate->c_profilefunc,
718 tstate->c_profileobj,
719 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000720 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000721 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 }
723 }
724 }
725
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000726 co = f->f_code;
727 names = co->co_names;
728 consts = co->co_consts;
729 fastlocals = f->f_localsplus;
730 freevars = f->f_localsplus + f->f_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000731 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000732 /* An explanation is in order for the next line.
733
734 f->f_lasti now refers to the index of the last instruction
735 executed. You might think this was obvious from the name, but
736 this wasn't always true before 2.3! PyFrame_New now sets
737 f->f_lasti to -1 (i.e. the index *before* the first instruction)
738 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
739 does work. Promise. */
740 next_instr = first_instr + f->f_lasti + 1;
741 stack_pointer = f->f_stacktop;
742 assert(stack_pointer != NULL);
743 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
744
Tim Peters5ca576e2001-06-18 22:08:13 +0000745#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000747#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000748#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000749 filename = PyString_AsString(co->co_filename);
750#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000751
Guido van Rossum374a9221991-04-04 10:40:29 +0000752 why = WHY_NOT;
753 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000754 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000755 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000756
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000757 if (throw) { /* support for generator.throw() */
758 why = WHY_EXCEPTION;
759 goto on_error;
760 }
761
Guido van Rossum374a9221991-04-04 10:40:29 +0000762 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000763#ifdef WITH_TSC
764 if (inst1 == 0) {
765 /* Almost surely, the opcode executed a break
766 or a continue, preventing inst1 from being set
767 on the way out of the loop.
768 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000769 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000770 loop1 = inst1;
771 }
772 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
773 intr0, intr1);
774 ticked = 0;
775 inst1 = 0;
776 intr0 = 0;
777 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000778 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000779#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000780 assert(stack_pointer >= f->f_valuestack); /* else underflow */
781 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
782
Guido van Rossuma027efa1997-05-05 20:56:21 +0000783 /* Do periodic things. Doing this every time through
784 the loop would add too much overhead, so we do it
785 only every Nth instruction. We also do it if
786 ``things_to_do'' is set, i.e. when an asynchronous
787 event needs attention (e.g. a signal handler or
788 async I/O handler); see Py_AddPendingCall() and
789 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000790
Skip Montanarod581d772002-09-03 20:10:45 +0000791 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000792 if (*next_instr == SETUP_FINALLY) {
793 /* Make the last opcode before
794 a try: finally: block uninterruptable. */
795 goto fast_next_opcode;
796 }
Skip Montanarod581d772002-09-03 20:10:45 +0000797 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000798 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000799#ifdef WITH_TSC
800 ticked = 1;
801#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000802 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000803 if (Py_MakePendingCalls() < 0) {
804 why = WHY_EXCEPTION;
805 goto on_error;
806 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000807 if (things_to_do)
808 /* MakePendingCalls() didn't succeed.
809 Force early re-execution of this
810 "periodic" code, possibly after
811 a thread switch */
812 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000813 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000814#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000815 if (interpreter_lock) {
816 /* Give another thread a chance */
817
Guido van Rossum25ce5661997-08-02 03:10:38 +0000818 if (PyThreadState_Swap(NULL) != tstate)
819 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000820 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000821
822 /* Other threads may run now */
823
Guido van Rossum65d5b571998-12-21 19:32:43 +0000824 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000825 if (PyThreadState_Swap(tstate) != NULL)
826 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000827
828 /* Check for thread interrupts */
829
830 if (tstate->async_exc != NULL) {
831 x = tstate->async_exc;
832 tstate->async_exc = NULL;
833 PyErr_SetNone(x);
834 Py_DECREF(x);
835 why = WHY_EXCEPTION;
836 goto on_error;
837 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000838 }
839#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000840 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841
Neil Schemenauer63543862002-02-17 19:10:14 +0000842 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000843 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000844
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000845 /* line-by-line tracing support */
846
847 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
848 /* see maybe_call_line_trace
849 for expository comments */
850 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000851
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000852 err = maybe_call_line_trace(tstate->c_tracefunc,
853 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000854 f, &instr_lb, &instr_ub,
855 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000856 /* Reload possibly changed frame fields */
857 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000858 if (f->f_stacktop != NULL) {
859 stack_pointer = f->f_stacktop;
860 f->f_stacktop = NULL;
861 }
862 if (err) {
863 /* trace function raised an exception */
864 goto on_error;
865 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000866 }
867
868 /* Extract opcode and argument */
869
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000871 oparg = 0; /* allows oparg to be stored in a register because
872 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000873 if (HAS_ARG(opcode))
874 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000875 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000876#ifdef DYNAMIC_EXECUTION_PROFILE
877#ifdef DXPAIRS
878 dxpairs[lastopcode][opcode]++;
879 lastopcode = opcode;
880#endif
881 dxp[opcode]++;
882#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000883
Guido van Rossum96a42c81992-01-12 02:29:51 +0000884#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000886
Guido van Rossum96a42c81992-01-12 02:29:51 +0000887 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 if (HAS_ARG(opcode)) {
889 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000890 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000891 }
892 else {
893 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000894 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 }
896 }
897#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000898
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000900 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000901
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 /* BEWARE!
905 It is essential that any operation that fails sets either
906 x to NULL, err to nonzero, or why to anything but WHY_NOT,
907 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000911 case NOP:
912 goto fast_next_opcode;
913
Neil Schemenauer63543862002-02-17 19:10:14 +0000914 case LOAD_FAST:
915 x = GETLOCAL(oparg);
916 if (x != NULL) {
917 Py_INCREF(x);
918 PUSH(x);
919 goto fast_next_opcode;
920 }
921 format_exc_check_arg(PyExc_UnboundLocalError,
922 UNBOUNDLOCAL_ERROR_MSG,
923 PyTuple_GetItem(co->co_varnames, oparg));
924 break;
925
926 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000927 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000928 Py_INCREF(x);
929 PUSH(x);
930 goto fast_next_opcode;
931
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000932 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000933 case STORE_FAST:
934 v = POP();
935 SETLOCAL(oparg, v);
936 goto fast_next_opcode;
937
Raymond Hettingerf606f872003-03-16 03:11:04 +0000938 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000939 case POP_TOP:
940 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000941 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000942 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000943
Guido van Rossum374a9221991-04-04 10:40:29 +0000944 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945 v = TOP();
946 w = SECOND();
947 SET_TOP(w);
948 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000949 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000950
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 v = TOP();
953 w = SECOND();
954 x = THIRD();
955 SET_TOP(w);
956 SET_SECOND(x);
957 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000958 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000959
Thomas Wouters434d0822000-08-24 20:11:32 +0000960 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000961 u = TOP();
962 v = SECOND();
963 w = THIRD();
964 x = FOURTH();
965 SET_TOP(v);
966 SET_SECOND(w);
967 SET_THIRD(x);
968 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000969 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000970
Guido van Rossum374a9221991-04-04 10:40:29 +0000971 case DUP_TOP:
972 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000973 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000974 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000975 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Thomas Wouters434d0822000-08-24 20:11:32 +0000977 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000978 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000980 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000982 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 STACKADJ(2);
984 SET_TOP(x);
985 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000986 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000987 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000989 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000991 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000993 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 STACKADJ(3);
995 SET_TOP(x);
996 SET_SECOND(w);
997 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000998 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000999 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001000 Py_FatalError("invalid argument to DUP_TOPX"
1001 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001002 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001003
Guido van Rossum374a9221991-04-04 10:40:29 +00001004 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001007 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001008 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001009 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001011
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001015 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001017 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001018 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001023 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 if (err == 0) {
1025 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001027 continue;
1028 }
1029 else if (err > 0) {
1030 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 err = 0;
1033 continue;
1034 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001035 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001037
Guido van Rossum374a9221991-04-04 10:40:29 +00001038 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001040 x = PyObject_Repr(v);
1041 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001043 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001048 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum50564e81996-01-12 01:13:16 +00001054 case BINARY_POWER:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001057 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001061 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case BINARY_MULTIPLY:
1065 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001067 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001068 Py_DECREF(v);
1069 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001071 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001072 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001073
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001075 if (!_Py_QnewFlag) {
1076 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001078 x = PyNumber_Divide(v, w);
1079 Py_DECREF(v);
1080 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001082 if (x != NULL) continue;
1083 break;
1084 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001086 BINARY_TRUE_DIVIDE */
1087 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001090 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001091 Py_DECREF(v);
1092 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001094 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Guido van Rossum4668b002001-08-08 05:00:18 +00001097 case BINARY_FLOOR_DIVIDE:
1098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001100 x = PyNumber_FloorDivide(v, w);
1101 Py_DECREF(v);
1102 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001104 if (x != NULL) continue;
1105 break;
1106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case BINARY_MODULO:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001110 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001111 Py_DECREF(v);
1112 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001113 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001114 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001115 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 case BINARY_ADD:
1118 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001120 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001121 /* INLINE: int + int */
1122 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001123 a = PyInt_AS_LONG(v);
1124 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001125 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001126 if ((i^a) < 0 && (i^b) < 0)
1127 goto slow_add;
1128 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001129 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001130 else if (PyString_CheckExact(v) &&
1131 PyString_CheckExact(w)) {
1132 x = string_concatenate(v, w, f, next_instr);
1133 /* string_concatenate consumed the ref to v */
1134 goto skip_decref_vx;
1135 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001136 else {
1137 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001140 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001141 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001142 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001143 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001144 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Guido van Rossum374a9221991-04-04 10:40:29 +00001147 case BINARY_SUBTRACT:
1148 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001150 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 /* INLINE: int - int */
1152 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001153 a = PyInt_AS_LONG(v);
1154 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001155 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001156 if ((i^a) < 0 && (i^~b) < 0)
1157 goto slow_sub;
1158 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001159 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001160 else {
1161 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001162 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001163 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001164 Py_DECREF(v);
1165 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001166 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001167 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001168 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Guido van Rossum374a9221991-04-04 10:40:29 +00001170 case BINARY_SUBSCR:
1171 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001173 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001174 /* INLINE: list[int] */
1175 long i = PyInt_AsLong(w);
1176 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001177 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001178 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001179 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001180 Py_INCREF(x);
1181 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001182 else
1183 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001184 }
1185 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001186 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001187 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001188 Py_DECREF(v);
1189 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001191 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001192 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 case BINARY_LSHIFT:
1195 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001201 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 case BINARY_RSHIFT:
1205 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 Py_DECREF(v);
1209 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001211 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case BINARY_AND:
1215 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001217 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_DECREF(v);
1219 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001221 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 case BINARY_XOR:
1225 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001227 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001231 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001232 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001233
Guido van Rossum7928cd71991-10-24 14:59:31 +00001234 case BINARY_OR:
1235 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001237 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001238 Py_DECREF(v);
1239 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001241 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001242 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001243
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001244 case LIST_APPEND:
1245 w = POP();
1246 v = POP();
1247 err = PyList_Append(v, w);
1248 Py_DECREF(v);
1249 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001250 if (err == 0) {
1251 PREDICT(JUMP_ABSOLUTE);
1252 continue;
1253 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001254 break;
1255
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 case INPLACE_POWER:
1257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 x = PyNumber_InPlacePower(v, w, Py_None);
1260 Py_DECREF(v);
1261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 if (x != NULL) continue;
1264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 case INPLACE_MULTIPLY:
1267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 x = PyNumber_InPlaceMultiply(v, w);
1270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 if (x != NULL) continue;
1274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001277 if (!_Py_QnewFlag) {
1278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001280 x = PyNumber_InPlaceDivide(v, w);
1281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001284 if (x != NULL) continue;
1285 break;
1286 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001288 INPLACE_TRUE_DIVIDE */
1289 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001292 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 Py_DECREF(v);
1294 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 if (x != NULL) continue;
1297 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Guido van Rossum4668b002001-08-08 05:00:18 +00001299 case INPLACE_FLOOR_DIVIDE:
1300 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001302 x = PyNumber_InPlaceFloorDivide(v, w);
1303 Py_DECREF(v);
1304 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001306 if (x != NULL) continue;
1307 break;
1308
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 case INPLACE_MODULO:
1310 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 x = PyNumber_InPlaceRemainder(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 if (x != NULL) continue;
1317 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 case INPLACE_ADD:
1320 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001322 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 /* INLINE: int + int */
1324 register long a, b, i;
1325 a = PyInt_AS_LONG(v);
1326 b = PyInt_AS_LONG(w);
1327 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001328 if ((i^a) < 0 && (i^b) < 0)
1329 goto slow_iadd;
1330 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001332 else if (PyString_CheckExact(v) &&
1333 PyString_CheckExact(w)) {
1334 x = string_concatenate(v, w, f, next_instr);
1335 /* string_concatenate consumed the ref to v */
1336 goto skip_decref_v;
1337 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 else {
1339 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001341 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001343 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 case INPLACE_SUBTRACT:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001352 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 /* INLINE: int - int */
1354 register long a, b, i;
1355 a = PyInt_AS_LONG(v);
1356 b = PyInt_AS_LONG(w);
1357 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001358 if ((i^a) < 0 && (i^~b) < 0)
1359 goto slow_isub;
1360 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001362 else {
1363 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001365 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 Py_DECREF(v);
1367 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 if (x != NULL) continue;
1370 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 case INPLACE_LSHIFT:
1373 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 x = PyNumber_InPlaceLshift(v, w);
1376 Py_DECREF(v);
1377 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001378 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 if (x != NULL) continue;
1380 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 case INPLACE_RSHIFT:
1383 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 x = PyNumber_InPlaceRshift(v, w);
1386 Py_DECREF(v);
1387 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001388 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 if (x != NULL) continue;
1390 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 case INPLACE_AND:
1393 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 x = PyNumber_InPlaceAnd(v, w);
1396 Py_DECREF(v);
1397 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001398 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001399 if (x != NULL) continue;
1400 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Thomas Wouters434d0822000-08-24 20:11:32 +00001402 case INPLACE_XOR:
1403 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001404 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001405 x = PyNumber_InPlaceXor(v, w);
1406 Py_DECREF(v);
1407 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001408 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 if (x != NULL) continue;
1410 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001411
Thomas Wouters434d0822000-08-24 20:11:32 +00001412 case INPLACE_OR:
1413 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 x = PyNumber_InPlaceOr(v, w);
1416 Py_DECREF(v);
1417 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001419 if (x != NULL) continue;
1420 break;
1421
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 case SLICE+0:
1423 case SLICE+1:
1424 case SLICE+2:
1425 case SLICE+3:
1426 if ((opcode-SLICE) & 2)
1427 w = POP();
1428 else
1429 w = NULL;
1430 if ((opcode-SLICE) & 1)
1431 v = POP();
1432 else
1433 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001434 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001436 Py_DECREF(u);
1437 Py_XDECREF(v);
1438 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001440 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001442
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 case STORE_SLICE+0:
1444 case STORE_SLICE+1:
1445 case STORE_SLICE+2:
1446 case STORE_SLICE+3:
1447 if ((opcode-STORE_SLICE) & 2)
1448 w = POP();
1449 else
1450 w = NULL;
1451 if ((opcode-STORE_SLICE) & 1)
1452 v = POP();
1453 else
1454 v = NULL;
1455 u = POP();
1456 t = POP();
1457 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001458 Py_DECREF(t);
1459 Py_DECREF(u);
1460 Py_XDECREF(v);
1461 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001462 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 case DELETE_SLICE+0:
1466 case DELETE_SLICE+1:
1467 case DELETE_SLICE+2:
1468 case DELETE_SLICE+3:
1469 if ((opcode-DELETE_SLICE) & 2)
1470 w = POP();
1471 else
1472 w = NULL;
1473 if ((opcode-DELETE_SLICE) & 1)
1474 v = POP();
1475 else
1476 v = NULL;
1477 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001478 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 Py_DECREF(u);
1481 Py_XDECREF(v);
1482 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001483 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 w = TOP();
1488 v = SECOND();
1489 u = THIRD();
1490 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001491 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001492 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 Py_DECREF(u);
1494 Py_DECREF(v);
1495 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001496 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001500 w = TOP();
1501 v = SECOND();
1502 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001504 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 Py_DECREF(v);
1506 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001507 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001509
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 case PRINT_EXPR:
1511 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001512 w = PySys_GetObject("displayhook");
1513 if (w == NULL) {
1514 PyErr_SetString(PyExc_RuntimeError,
1515 "lost sys.displayhook");
1516 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001517 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001518 }
1519 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001520 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001521 if (x == NULL)
1522 err = -1;
1523 }
1524 if (err == 0) {
1525 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001526 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001527 if (w == NULL)
1528 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001529 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001530 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001531 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001532 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001533
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001534 case PRINT_ITEM_TO:
1535 w = stream = POP();
1536 /* fall through to PRINT_ITEM */
1537
Guido van Rossum374a9221991-04-04 10:40:29 +00001538 case PRINT_ITEM:
1539 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001540 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001541 w = PySys_GetObject("stdout");
1542 if (w == NULL) {
1543 PyErr_SetString(PyExc_RuntimeError,
1544 "lost sys.stdout");
1545 err = -1;
1546 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001547 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001548 /* PyFile_SoftSpace() can exececute arbitrary code
1549 if sys.stdout is an instance with a __getattr__.
1550 If __getattr__ raises an exception, w will
1551 be freed, so we need to prevent that temporarily. */
1552 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001553 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001554 err = PyFile_WriteString(" ", w);
1555 if (err == 0)
1556 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001557 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001558 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001559 if (PyString_Check(v)) {
1560 char *s = PyString_AS_STRING(v);
1561 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001562 if (len == 0 ||
1563 !isspace(Py_CHARMASK(s[len-1])) ||
1564 s[len-1] == ' ')
1565 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001566 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001567#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001568 else if (PyUnicode_Check(v)) {
1569 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1570 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001571 if (len == 0 ||
1572 !Py_UNICODE_ISSPACE(s[len-1]) ||
1573 s[len-1] == ' ')
1574 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001575 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001576#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001577 else
1578 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001580 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001581 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001582 Py_XDECREF(stream);
1583 stream = NULL;
1584 if (err == 0)
1585 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001586 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001588 case PRINT_NEWLINE_TO:
1589 w = stream = POP();
1590 /* fall through to PRINT_NEWLINE */
1591
Guido van Rossum374a9221991-04-04 10:40:29 +00001592 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001593 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001594 w = PySys_GetObject("stdout");
1595 if (w == NULL)
1596 PyErr_SetString(PyExc_RuntimeError,
1597 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001598 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001599 if (w != NULL) {
1600 err = PyFile_WriteString("\n", w);
1601 if (err == 0)
1602 PyFile_SoftSpace(w, 0);
1603 }
1604 Py_XDECREF(stream);
1605 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Thomas Wouters434d0822000-08-24 20:11:32 +00001608
1609#ifdef CASE_TOO_BIG
1610 default: switch (opcode) {
1611#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001612 case RAISE_VARARGS:
1613 u = v = w = NULL;
1614 switch (oparg) {
1615 case 3:
1616 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617 /* Fallthrough */
1618 case 2:
1619 v = POP(); /* value */
1620 /* Fallthrough */
1621 case 1:
1622 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001623 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001624 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001625 break;
1626 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001628 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001629 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001630 break;
1631 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001633
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001635 if ((x = f->f_locals) != NULL) {
1636 Py_INCREF(x);
1637 PUSH(x);
1638 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001639 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001640 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case RETURN_VALUE:
1644 retval = POP();
1645 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001646 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001647
Tim Peters5ca576e2001-06-18 22:08:13 +00001648 case YIELD_VALUE:
1649 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001650 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001651 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001652 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001653
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001654 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001655 w = TOP();
1656 v = SECOND();
1657 u = THIRD();
1658 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001659 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001660 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001661 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 Py_DECREF(u);
1663 Py_DECREF(v);
1664 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001665 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 case POP_BLOCK:
1668 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001669 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 while (STACK_LEVEL() > b->b_level) {
1671 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001672 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 }
1674 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001675 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case END_FINALLY:
1678 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001679 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001680 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001681 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001682 if (why == WHY_RETURN ||
1683 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 retval = POP();
1685 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001686 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001688 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001689 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001691 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001693 else if (v != Py_None) {
1694 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 "'finally' pops bad exception");
1696 why = WHY_EXCEPTION;
1697 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001698 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001699 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001702 u = TOP();
1703 v = SECOND();
1704 w = THIRD();
1705 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001706 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001707 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 Py_DECREF(u);
1709 Py_DECREF(v);
1710 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001714 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001716 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001717 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001718 err = PyDict_SetItem(x, w, v);
1719 else
1720 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001721 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001722 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001723 break;
1724 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001725 PyErr_Format(PyExc_SystemError,
1726 "no locals found when storing %s",
1727 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001732 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001733 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001734 format_exc_check_arg(PyExc_NameError,
1735 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001736 break;
1737 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001738 PyErr_Format(PyExc_SystemError,
1739 "no locals when deleting %s",
1740 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001742
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001743 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001744 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001746 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1747 PyObject **items = ((PyTupleObject *)v)->ob_item;
1748 while (oparg--) {
1749 w = items[oparg];
1750 Py_INCREF(w);
1751 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001752 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001753 Py_DECREF(v);
1754 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001755 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1756 PyObject **items = ((PyListObject *)v)->ob_item;
1757 while (oparg--) {
1758 w = items[oparg];
1759 Py_INCREF(w);
1760 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001761 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001762 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001763 stack_pointer + oparg))
1764 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001765 else {
1766 if (PyErr_ExceptionMatches(PyExc_TypeError))
1767 PyErr_SetString(PyExc_TypeError,
1768 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001769 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001770 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001771 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001775 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001776 v = TOP();
1777 u = SECOND();
1778 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1780 Py_DECREF(v);
1781 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001782 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001784
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001786 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001788 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1789 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001793 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001794 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001795 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001796 err = PyDict_SetItem(f->f_globals, w, v);
1797 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001798 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001799 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001801 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001802 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001803 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001804 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001805 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001806 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001807
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001809 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001810 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001811 PyErr_Format(PyExc_SystemError,
1812 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001813 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001814 break;
1815 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001816 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001817 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001818 Py_XINCREF(x);
1819 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001820 else {
1821 x = PyObject_GetItem(v, w);
1822 if (x == NULL && PyErr_Occurred()) {
1823 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1824 break;
1825 PyErr_Clear();
1826 }
1827 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 x = PyDict_GetItem(f->f_globals, w);
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_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001833 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001834 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001835 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 break;
1837 }
1838 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001839 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001842 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Guido van Rossum374a9221991-04-04 10:40:29 +00001844 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001845 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001846 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001847 /* Inline the PyDict_GetItem() calls.
1848 WARNING: this is an extreme speed hack.
1849 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001850 long hash = ((PyStringObject *)w)->ob_shash;
1851 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001852 PyDictObject *d;
1853 d = (PyDictObject *)(f->f_globals);
1854 x = d->ma_lookup(d, w, hash)->me_value;
1855 if (x != NULL) {
1856 Py_INCREF(x);
1857 PUSH(x);
1858 continue;
1859 }
1860 d = (PyDictObject *)(f->f_builtins);
1861 x = d->ma_lookup(d, w, hash)->me_value;
1862 if (x != NULL) {
1863 Py_INCREF(x);
1864 PUSH(x);
1865 continue;
1866 }
1867 goto load_global_error;
1868 }
1869 }
1870 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001871 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001873 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001875 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001876 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001877 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001878 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001879 break;
1880 }
1881 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001882 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001884 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001885
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001886 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001887 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001888 if (x != NULL) {
1889 SETLOCAL(oparg, NULL);
1890 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001891 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001892 format_exc_check_arg(
1893 PyExc_UnboundLocalError,
1894 UNBOUNDLOCAL_ERROR_MSG,
1895 PyTuple_GetItem(co->co_varnames, oparg)
1896 );
1897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001899 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001900 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001901 Py_INCREF(x);
1902 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001903 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001904 break;
1905
1906 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001907 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001908 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001909 if (w != NULL) {
1910 PUSH(w);
1911 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001912 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001913 err = -1;
1914 /* Don't stomp existing exception */
1915 if (PyErr_Occurred())
1916 break;
1917 if (oparg < f->f_ncells) {
1918 v = PyTuple_GetItem(co->co_cellvars,
1919 oparg);
1920 format_exc_check_arg(
1921 PyExc_UnboundLocalError,
1922 UNBOUNDLOCAL_ERROR_MSG,
1923 v);
1924 } else {
1925 v = PyTuple_GetItem(
1926 co->co_freevars,
1927 oparg - f->f_ncells);
1928 format_exc_check_arg(
1929 PyExc_NameError,
1930 UNBOUNDFREE_ERROR_MSG,
1931 v);
1932 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001933 break;
1934
1935 case STORE_DEREF:
1936 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001937 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001938 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001939 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001940 continue;
1941
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001945 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001947 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 }
1949 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001950 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 }
1952 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001955 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001957 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001959 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001960 }
1961 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001962 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 }
1964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Guido van Rossum374a9221991-04-04 10:40:29 +00001966 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001969 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001971
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001973 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001974 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 x = PyObject_GetAttr(v, w);
1976 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001977 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001978 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001979 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001980
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 case COMPARE_OP:
1982 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001983 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001984 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001985 /* INLINE: cmp(int, int) */
1986 register long a, b;
1987 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001988 a = PyInt_AS_LONG(v);
1989 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001990 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001991 case PyCmp_LT: res = a < b; break;
1992 case PyCmp_LE: res = a <= b; break;
1993 case PyCmp_EQ: res = a == b; break;
1994 case PyCmp_NE: res = a != b; break;
1995 case PyCmp_GT: res = a > b; break;
1996 case PyCmp_GE: res = a >= b; break;
1997 case PyCmp_IS: res = v == w; break;
1998 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001999 default: goto slow_compare;
2000 }
2001 x = res ? Py_True : Py_False;
2002 Py_INCREF(x);
2003 }
2004 else {
2005 slow_compare:
2006 x = cmp_outcome(oparg, v, w);
2007 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002008 Py_DECREF(v);
2009 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002010 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002011 if (x == NULL) break;
2012 PREDICT(JUMP_IF_FALSE);
2013 PREDICT(JUMP_IF_TRUE);
2014 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Guido van Rossum374a9221991-04-04 10:40:29 +00002016 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002017 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002019 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002021 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022 break;
2023 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002024 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002025 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002026 w,
2027 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002028 f->f_locals == NULL ?
2029 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002030 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002031 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002032 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002033 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002034 x = NULL;
2035 break;
2036 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002037 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002039 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002041 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002042 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002043 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Thomas Wouters52152252000-08-17 22:55:00 +00002045 case IMPORT_STAR:
2046 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002047 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002048 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002049 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002050 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002051 break;
2052 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002053 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002054 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002055 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002056 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002057 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002058 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002060
Thomas Wouters52152252000-08-17 22:55:00 +00002061 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002062 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002063 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002064 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002065 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002066 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002067 PUSH(x);
2068 if (x != NULL) continue;
2069 break;
2070
Guido van Rossum374a9221991-04-04 10:40:29 +00002071 case JUMP_FORWARD:
2072 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002073 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Raymond Hettingerf606f872003-03-16 03:11:04 +00002075 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002076 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002077 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002078 if (w == Py_True) {
2079 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002080 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002081 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002082 if (w == Py_False) {
2083 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002084 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002085 }
2086 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002087 if (err > 0)
2088 err = 0;
2089 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002090 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002091 else
2092 break;
2093 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Raymond Hettingerf606f872003-03-16 03:11:04 +00002095 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002096 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002097 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002098 if (w == Py_False) {
2099 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002100 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002101 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002102 if (w == Py_True) {
2103 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002104 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002105 }
2106 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002107 if (err > 0) {
2108 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002109 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002110 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002111 else if (err == 0)
2112 ;
2113 else
2114 break;
2115 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002117 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 case JUMP_ABSOLUTE:
2119 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002120 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002122 case GET_ITER:
2123 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002124 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125 x = PyObject_GetIter(v);
2126 Py_DECREF(v);
2127 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002128 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002129 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002130 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002131 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002132 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133 break;
2134
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002135 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002136 case FOR_ITER:
2137 /* before: [iter]; after: [iter, iter()] *or* [] */
2138 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002139 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002140 if (x != NULL) {
2141 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002142 PREDICT(STORE_FAST);
2143 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002144 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002145 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002146 if (PyErr_Occurred()) {
2147 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2148 break;
2149 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002150 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002151 /* iterator ended normally */
2152 x = v = POP();
2153 Py_DECREF(v);
2154 JUMPBY(oparg);
2155 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002157 case BREAK_LOOP:
2158 why = WHY_BREAK;
2159 goto fast_block_end;
2160
2161 case CONTINUE_LOOP:
2162 retval = PyInt_FromLong(oparg);
2163 why = WHY_CONTINUE;
2164 goto fast_block_end;
2165
Guido van Rossum374a9221991-04-04 10:40:29 +00002166 case SETUP_LOOP:
2167 case SETUP_EXCEPT:
2168 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002169 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002170 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002171 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Guido van Rossumf10570b1995-07-07 22:53:21 +00002173 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002174 {
2175 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002176 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002177 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002178#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002179 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002180#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002181 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002182#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002183 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002184 PUSH(x);
2185 if (x != NULL)
2186 continue;
2187 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002189
Jeremy Hylton76901512000-03-28 23:49:17 +00002190 case CALL_FUNCTION_VAR:
2191 case CALL_FUNCTION_KW:
2192 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002193 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002194 int na = oparg & 0xff;
2195 int nk = (oparg>>8) & 0xff;
2196 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002197 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002198 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002199 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002200 if (flags & CALL_FLAG_VAR)
2201 n++;
2202 if (flags & CALL_FLAG_KW)
2203 n++;
2204 pfunc = stack_pointer - n - 1;
2205 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002206
Guido van Rossumac7be682001-01-17 15:42:30 +00002207 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002208 && PyMethod_GET_SELF(func) != NULL) {
2209 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002210 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002211 func = PyMethod_GET_FUNCTION(func);
2212 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002213 Py_DECREF(*pfunc);
2214 *pfunc = self;
2215 na++;
2216 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002217 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002218 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002219 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002220 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002221 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002222 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002223 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002224 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002225
Jeremy Hylton76901512000-03-28 23:49:17 +00002226 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002227 w = POP();
2228 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002229 }
2230 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002231 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002232 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002233 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002234 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002235
Guido van Rossum681d79a1995-07-18 14:51:37 +00002236 case MAKE_FUNCTION:
2237 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002238 x = PyFunction_New(v, f->f_globals);
2239 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002240 /* XXX Maybe this should be a separate opcode? */
2241 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002242 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002243 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002245 x = NULL;
2246 break;
2247 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002248 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002249 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002251 }
2252 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002253 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002254 }
2255 PUSH(x);
2256 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002257
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002258 case MAKE_CLOSURE:
2259 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002260 v = POP(); /* code object */
2261 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002262 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 if (x != NULL) {
2264 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002265 err = PyFunction_SetClosure(x, v);
2266 Py_DECREF(v);
2267 }
2268 if (x != NULL && oparg > 0) {
2269 v = PyTuple_New(oparg);
2270 if (v == NULL) {
2271 Py_DECREF(x);
2272 x = NULL;
2273 break;
2274 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002275 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002276 w = POP();
2277 PyTuple_SET_ITEM(v, oparg, w);
2278 }
2279 err = PyFunction_SetDefaults(x, v);
2280 Py_DECREF(v);
2281 }
2282 PUSH(x);
2283 break;
2284 }
2285
Guido van Rossum8861b741996-07-30 16:49:37 +00002286 case BUILD_SLICE:
2287 if (oparg == 3)
2288 w = POP();
2289 else
2290 w = NULL;
2291 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002292 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002293 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002294 Py_DECREF(u);
2295 Py_DECREF(v);
2296 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002297 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002298 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002299 break;
2300
Fred Drakeef8ace32000-08-24 00:32:09 +00002301 case EXTENDED_ARG:
2302 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002303 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002304 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002305
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 default:
2307 fprintf(stderr,
2308 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002309 PyCode_Addr2Line(f->f_code, f->f_lasti),
2310 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002311 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 why = WHY_EXCEPTION;
2313 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002314
2315#ifdef CASE_TOO_BIG
2316 }
2317#endif
2318
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 } /* switch */
2320
2321 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002323 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002324
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002328 if (err == 0 && x != NULL) {
2329#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002330 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002332 fprintf(stderr,
2333 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002334 else {
2335#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002336 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002337 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002338#ifdef CHECKEXC
2339 }
2340#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002341 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002342 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002343 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 err = 0;
2345 }
2346
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002348
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002349 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002351 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002352 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 why = WHY_EXCEPTION;
2354 }
2355 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002356#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002358 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002359 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002360 char buf[1024];
2361 sprintf(buf, "Stack unwind with exception "
2362 "set and why=%d", why);
2363 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 }
2366#endif
2367
2368 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Guido van Rossum374a9221991-04-04 10:40:29 +00002370 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002371 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002372
Fred Drake8f51f542001-10-04 14:48:42 +00002373 if (tstate->c_tracefunc != NULL)
2374 call_exc_trace(tstate->c_tracefunc,
2375 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002377
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 if (why == WHY_RERAISE)
2381 why = WHY_EXCEPTION;
2382
2383 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002384
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002385fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002386 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002387 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002388
Tim Peters8a5c3c72004-04-05 19:36:21 +00002389 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002390 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2391 /* For a continue inside a try block,
2392 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002393 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2394 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002395 why = WHY_NOT;
2396 JUMPTO(PyInt_AS_LONG(retval));
2397 Py_DECREF(retval);
2398 break;
2399 }
2400
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 while (STACK_LEVEL() > b->b_level) {
2402 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002403 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 }
2405 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2406 why = WHY_NOT;
2407 JUMPTO(b->b_handler);
2408 break;
2409 }
2410 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002411 (b->b_type == SETUP_EXCEPT &&
2412 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002414 PyObject *exc, *val, *tb;
2415 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002417 val = Py_None;
2418 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002419 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002420 /* Make the raw exception data
2421 available to the handler,
2422 so a program can emulate the
2423 Python main loop. Don't do
2424 this for 'finally'. */
2425 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002426 PyErr_NormalizeException(
2427 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002428 set_exc_info(tstate,
2429 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002430 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002431 if (tb == NULL) {
2432 Py_INCREF(Py_None);
2433 PUSH(Py_None);
2434 } else
2435 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002436 PUSH(val);
2437 PUSH(exc);
2438 }
2439 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002440 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002441 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002442 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002443 PUSH(v);
2444 }
2445 why = WHY_NOT;
2446 JUMPTO(b->b_handler);
2447 break;
2448 }
2449 } /* unwind stack */
2450
2451 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Guido van Rossum374a9221991-04-04 10:40:29 +00002453 if (why != WHY_NOT)
2454 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002455 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002456
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002458
Tim Peters8a5c3c72004-04-05 19:36:21 +00002459 assert(why != WHY_YIELD);
2460 /* Pop remaining stack entries. */
2461 while (!EMPTY()) {
2462 v = POP();
2463 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002464 }
2465
Tim Peters8a5c3c72004-04-05 19:36:21 +00002466 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002467 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002469fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002470 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002471 if (tstate->c_tracefunc) {
2472 if (why == WHY_RETURN || why == WHY_YIELD) {
2473 if (call_trace(tstate->c_tracefunc,
2474 tstate->c_traceobj, f,
2475 PyTrace_RETURN, retval)) {
2476 Py_XDECREF(retval);
2477 retval = NULL;
2478 why = WHY_EXCEPTION;
2479 }
2480 }
2481 else if (why == WHY_EXCEPTION) {
2482 call_trace_protected(tstate->c_tracefunc,
2483 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002484 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002485 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002486 }
Fred Drake8f51f542001-10-04 14:48:42 +00002487 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002488 if (why == WHY_EXCEPTION)
2489 call_trace_protected(tstate->c_profilefunc,
2490 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002491 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002492 else if (call_trace(tstate->c_profilefunc,
2493 tstate->c_profileobj, f,
2494 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002495 Py_XDECREF(retval);
2496 retval = NULL;
2497 why = WHY_EXCEPTION;
2498 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002499 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002500 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002501
Guido van Rossuma027efa1997-05-05 20:56:21 +00002502 reset_exc_info(tstate);
2503
Tim Peters5ca576e2001-06-18 22:08:13 +00002504 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002505 exit_eval_frame:
2506 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002507 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002508
Guido van Rossum96a42c81992-01-12 02:29:51 +00002509 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002510}
2511
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002512/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002513 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002514 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002515
Tim Peters6d6c1a32001-08-02 04:15:00 +00002516PyObject *
2517PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002518 PyObject **args, int argcount, PyObject **kws, int kwcount,
2519 PyObject **defs, int defcount, PyObject *closure)
2520{
2521 register PyFrameObject *f;
2522 register PyObject *retval = NULL;
2523 register PyObject **fastlocals, **freevars;
2524 PyThreadState *tstate = PyThreadState_GET();
2525 PyObject *x, *u;
2526
2527 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002528 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002529 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002530 return NULL;
2531 }
2532
Jeremy Hylton985eba52003-02-05 23:13:00 +00002533 assert(globals != NULL);
2534 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002535 if (f == NULL)
2536 return NULL;
2537
2538 fastlocals = f->f_localsplus;
2539 freevars = f->f_localsplus + f->f_nlocals;
2540
2541 if (co->co_argcount > 0 ||
2542 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2543 int i;
2544 int n = argcount;
2545 PyObject *kwdict = NULL;
2546 if (co->co_flags & CO_VARKEYWORDS) {
2547 kwdict = PyDict_New();
2548 if (kwdict == NULL)
2549 goto fail;
2550 i = co->co_argcount;
2551 if (co->co_flags & CO_VARARGS)
2552 i++;
2553 SETLOCAL(i, kwdict);
2554 }
2555 if (argcount > co->co_argcount) {
2556 if (!(co->co_flags & CO_VARARGS)) {
2557 PyErr_Format(PyExc_TypeError,
2558 "%.200s() takes %s %d "
2559 "%sargument%s (%d given)",
2560 PyString_AsString(co->co_name),
2561 defcount ? "at most" : "exactly",
2562 co->co_argcount,
2563 kwcount ? "non-keyword " : "",
2564 co->co_argcount == 1 ? "" : "s",
2565 argcount);
2566 goto fail;
2567 }
2568 n = co->co_argcount;
2569 }
2570 for (i = 0; i < n; i++) {
2571 x = args[i];
2572 Py_INCREF(x);
2573 SETLOCAL(i, x);
2574 }
2575 if (co->co_flags & CO_VARARGS) {
2576 u = PyTuple_New(argcount - n);
2577 if (u == NULL)
2578 goto fail;
2579 SETLOCAL(co->co_argcount, u);
2580 for (i = n; i < argcount; i++) {
2581 x = args[i];
2582 Py_INCREF(x);
2583 PyTuple_SET_ITEM(u, i-n, x);
2584 }
2585 }
2586 for (i = 0; i < kwcount; i++) {
2587 PyObject *keyword = kws[2*i];
2588 PyObject *value = kws[2*i + 1];
2589 int j;
2590 if (keyword == NULL || !PyString_Check(keyword)) {
2591 PyErr_Format(PyExc_TypeError,
2592 "%.200s() keywords must be strings",
2593 PyString_AsString(co->co_name));
2594 goto fail;
2595 }
2596 /* XXX slow -- speed up using dictionary? */
2597 for (j = 0; j < co->co_argcount; j++) {
2598 PyObject *nm = PyTuple_GET_ITEM(
2599 co->co_varnames, j);
2600 int cmp = PyObject_RichCompareBool(
2601 keyword, nm, Py_EQ);
2602 if (cmp > 0)
2603 break;
2604 else if (cmp < 0)
2605 goto fail;
2606 }
2607 /* Check errors from Compare */
2608 if (PyErr_Occurred())
2609 goto fail;
2610 if (j >= co->co_argcount) {
2611 if (kwdict == NULL) {
2612 PyErr_Format(PyExc_TypeError,
2613 "%.200s() got an unexpected "
2614 "keyword argument '%.400s'",
2615 PyString_AsString(co->co_name),
2616 PyString_AsString(keyword));
2617 goto fail;
2618 }
2619 PyDict_SetItem(kwdict, keyword, value);
2620 }
2621 else {
2622 if (GETLOCAL(j) != NULL) {
2623 PyErr_Format(PyExc_TypeError,
2624 "%.200s() got multiple "
2625 "values for keyword "
2626 "argument '%.400s'",
2627 PyString_AsString(co->co_name),
2628 PyString_AsString(keyword));
2629 goto fail;
2630 }
2631 Py_INCREF(value);
2632 SETLOCAL(j, value);
2633 }
2634 }
2635 if (argcount < co->co_argcount) {
2636 int m = co->co_argcount - defcount;
2637 for (i = argcount; i < m; i++) {
2638 if (GETLOCAL(i) == NULL) {
2639 PyErr_Format(PyExc_TypeError,
2640 "%.200s() takes %s %d "
2641 "%sargument%s (%d given)",
2642 PyString_AsString(co->co_name),
2643 ((co->co_flags & CO_VARARGS) ||
2644 defcount) ? "at least"
2645 : "exactly",
2646 m, kwcount ? "non-keyword " : "",
2647 m == 1 ? "" : "s", i);
2648 goto fail;
2649 }
2650 }
2651 if (n > m)
2652 i = n - m;
2653 else
2654 i = 0;
2655 for (; i < defcount; i++) {
2656 if (GETLOCAL(m+i) == NULL) {
2657 PyObject *def = defs[i];
2658 Py_INCREF(def);
2659 SETLOCAL(m+i, def);
2660 }
2661 }
2662 }
2663 }
2664 else {
2665 if (argcount > 0 || kwcount > 0) {
2666 PyErr_Format(PyExc_TypeError,
2667 "%.200s() takes no arguments (%d given)",
2668 PyString_AsString(co->co_name),
2669 argcount + kwcount);
2670 goto fail;
2671 }
2672 }
2673 /* Allocate and initialize storage for cell vars, and copy free
2674 vars into frame. This isn't too efficient right now. */
2675 if (f->f_ncells) {
2676 int i = 0, j = 0, nargs, found;
2677 char *cellname, *argname;
2678 PyObject *c;
2679
2680 nargs = co->co_argcount;
2681 if (co->co_flags & CO_VARARGS)
2682 nargs++;
2683 if (co->co_flags & CO_VARKEYWORDS)
2684 nargs++;
2685
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 /* Initialize each cell var, taking into account
2687 cell vars that are initialized from arguments.
2688
2689 Should arrange for the compiler to put cellvars
2690 that are arguments at the beginning of the cellvars
2691 list so that we can march over it more efficiently?
2692 */
2693 for (i = 0; i < f->f_ncells; ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002694 cellname = PyString_AS_STRING(
2695 PyTuple_GET_ITEM(co->co_cellvars, i));
2696 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002698 argname = PyString_AS_STRING(
2699 PyTuple_GET_ITEM(co->co_varnames, j));
2700 if (strcmp(cellname, argname) == 0) {
2701 c = PyCell_New(GETLOCAL(j));
2702 if (c == NULL)
2703 goto fail;
2704 GETLOCAL(f->f_nlocals + i) = c;
2705 found = 1;
2706 break;
2707 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002708 }
2709 if (found == 0) {
2710 c = PyCell_New(NULL);
2711 if (c == NULL)
2712 goto fail;
2713 SETLOCAL(f->f_nlocals + i, c);
2714 }
2715 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002716 }
2717 if (f->f_nfreevars) {
2718 int i;
2719 for (i = 0; i < f->f_nfreevars; ++i) {
2720 PyObject *o = PyTuple_GET_ITEM(closure, i);
2721 Py_INCREF(o);
2722 freevars[f->f_ncells + i] = o;
2723 }
2724 }
2725
Tim Peters5ca576e2001-06-18 22:08:13 +00002726 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002727 /* Don't need to keep the reference to f_back, it will be set
2728 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002729 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002730 f->f_back = NULL;
2731
Jeremy Hylton985eba52003-02-05 23:13:00 +00002732 PCALL(PCALL_GENERATOR);
2733
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002734 /* Create a new generator that owns the ready to run frame
2735 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002736 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002737 }
2738
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002739 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002740
2741 fail: /* Jump here from prelude on failure */
2742
Tim Petersb13680b2001-11-27 23:29:29 +00002743 /* decref'ing the frame can cause __del__ methods to get invoked,
2744 which can call back into Python. While we're done with the
2745 current Python frame (f), the associated C stack is still in use,
2746 so recursion_depth must be boosted for the duration.
2747 */
2748 assert(tstate != NULL);
2749 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002750 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002751 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002752 return retval;
2753}
2754
2755
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002756/* Implementation notes for set_exc_info() and reset_exc_info():
2757
2758- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2759 'exc_traceback'. These always travel together.
2760
2761- tstate->curexc_ZZZ is the "hot" exception that is set by
2762 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2763
2764- Once an exception is caught by an except clause, it is transferred
2765 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2766 can pick it up. This is the primary task of set_exc_info().
2767
2768- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2769
2770 Long ago, when none of this existed, there were just a few globals:
2771 one set corresponding to the "hot" exception, and one set
2772 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2773 globals; they were simply stored as sys.exc_ZZZ. For backwards
2774 compatibility, they still are!) The problem was that in code like
2775 this:
2776
2777 try:
2778 "something that may fail"
2779 except "some exception":
2780 "do something else first"
2781 "print the exception from sys.exc_ZZZ."
2782
2783 if "do something else first" invoked something that raised and caught
2784 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2785 cause of subtle bugs. I fixed this by changing the semantics as
2786 follows:
2787
2788 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2789 *in that frame*.
2790
2791 - But initially, and as long as no exception is caught in a given
2792 frame, sys.exc_ZZZ will hold the last exception caught in the
2793 previous frame (or the frame before that, etc.).
2794
2795 The first bullet fixed the bug in the above example. The second
2796 bullet was for backwards compatibility: it was (and is) common to
2797 have a function that is called when an exception is caught, and to
2798 have that function access the caught exception via sys.exc_ZZZ.
2799 (Example: traceback.print_exc()).
2800
2801 At the same time I fixed the problem that sys.exc_ZZZ weren't
2802 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2803 but that's really a separate improvement.
2804
2805 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2806 variables to what they were before the current frame was called. The
2807 set_exc_info() function saves them on the frame so that
2808 reset_exc_info() can restore them. The invariant is that
2809 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2810 exception (where "catching" an exception applies only to successful
2811 except clauses); and if the current frame ever caught an exception,
2812 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2813 at the start of the current frame.
2814
2815*/
2816
Guido van Rossuma027efa1997-05-05 20:56:21 +00002817static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002818set_exc_info(PyThreadState *tstate,
2819 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002820{
2821 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002822 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002823
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824 frame = tstate->frame;
2825 if (frame->f_exc_type == NULL) {
2826 /* This frame didn't catch an exception before */
2827 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002828 if (tstate->exc_type == NULL) {
2829 Py_INCREF(Py_None);
2830 tstate->exc_type = Py_None;
2831 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002832 tmp_type = frame->f_exc_type;
2833 tmp_value = frame->f_exc_value;
2834 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002835 Py_XINCREF(tstate->exc_type);
2836 Py_XINCREF(tstate->exc_value);
2837 Py_XINCREF(tstate->exc_traceback);
2838 frame->f_exc_type = tstate->exc_type;
2839 frame->f_exc_value = tstate->exc_value;
2840 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002841 Py_XDECREF(tmp_type);
2842 Py_XDECREF(tmp_value);
2843 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002844 }
2845 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002846 tmp_type = tstate->exc_type;
2847 tmp_value = tstate->exc_value;
2848 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002849 Py_XINCREF(type);
2850 Py_XINCREF(value);
2851 Py_XINCREF(tb);
2852 tstate->exc_type = type;
2853 tstate->exc_value = value;
2854 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002855 Py_XDECREF(tmp_type);
2856 Py_XDECREF(tmp_value);
2857 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002858 /* For b/w compatibility */
2859 PySys_SetObject("exc_type", type);
2860 PySys_SetObject("exc_value", value);
2861 PySys_SetObject("exc_traceback", tb);
2862}
2863
2864static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002865reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002866{
2867 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002868 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002869 frame = tstate->frame;
2870 if (frame->f_exc_type != NULL) {
2871 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002872 tmp_type = tstate->exc_type;
2873 tmp_value = tstate->exc_value;
2874 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002875 Py_XINCREF(frame->f_exc_type);
2876 Py_XINCREF(frame->f_exc_value);
2877 Py_XINCREF(frame->f_exc_traceback);
2878 tstate->exc_type = frame->f_exc_type;
2879 tstate->exc_value = frame->f_exc_value;
2880 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002881 Py_XDECREF(tmp_type);
2882 Py_XDECREF(tmp_value);
2883 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002884 /* For b/w compatibility */
2885 PySys_SetObject("exc_type", frame->f_exc_type);
2886 PySys_SetObject("exc_value", frame->f_exc_value);
2887 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2888 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002889 tmp_type = frame->f_exc_type;
2890 tmp_value = frame->f_exc_value;
2891 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002892 frame->f_exc_type = NULL;
2893 frame->f_exc_value = NULL;
2894 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002895 Py_XDECREF(tmp_type);
2896 Py_XDECREF(tmp_value);
2897 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002898}
2899
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002900/* Logic for the raise statement (too complicated for inlining).
2901 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002902static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002903do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002904{
Guido van Rossumd295f121998-04-09 21:39:57 +00002905 if (type == NULL) {
2906 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002907 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002908 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2909 value = tstate->exc_value;
2910 tb = tstate->exc_traceback;
2911 Py_XINCREF(type);
2912 Py_XINCREF(value);
2913 Py_XINCREF(tb);
2914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002915
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002916 /* We support the following forms of raise:
2917 raise <class>, <classinstance>
2918 raise <class>, <argument tuple>
2919 raise <class>, None
2920 raise <class>, <argument>
2921 raise <classinstance>, None
2922 raise <string>, <object>
2923 raise <string>, None
2924
2925 An omitted second argument is the same as None.
2926
2927 In addition, raise <tuple>, <anything> is the same as
2928 raising the tuple's first item (and it better have one!);
2929 this rule is applied recursively.
2930
2931 Finally, an optional third argument can be supplied, which
2932 gives the traceback to be substituted (useful when
2933 re-raising an exception after examining it). */
2934
2935 /* First, check the traceback argument, replacing None with
2936 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002937 if (tb == Py_None) {
2938 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002939 tb = NULL;
2940 }
2941 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002942 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002943 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002944 goto raise_error;
2945 }
2946
2947 /* Next, replace a missing value with None */
2948 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002949 value = Py_None;
2950 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002951 }
2952
2953 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002954 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2955 PyObject *tmp = type;
2956 type = PyTuple_GET_ITEM(type, 0);
2957 Py_INCREF(type);
2958 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002959 }
2960
Tim Petersafb2c802002-04-18 18:06:20 +00002961 if (PyString_CheckExact(type))
2962 /* Raising builtin string is deprecated but still allowed --
2963 * do nothing. Raising an instance of a new-style str
2964 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002965 PyErr_Warn(PyExc_PendingDeprecationWarning,
2966 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002967
2968 else if (PyClass_Check(type))
2969 PyErr_NormalizeException(&type, &value, &tb);
2970
Guido van Rossumb209a111997-04-29 18:18:01 +00002971 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002972 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002973 if (value != Py_None) {
2974 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002975 "instance exception may not have a separate value");
2976 goto raise_error;
2977 }
2978 else {
2979 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002980 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002981 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002982 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2983 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002984 }
2985 }
2986 else {
2987 /* Not something you can raise. You get an exception
2988 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002989 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002990 "exceptions must be classes, instances, or "
2991 "strings (deprecated), not %s",
2992 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002993 goto raise_error;
2994 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002995 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002996 if (tb == NULL)
2997 return WHY_EXCEPTION;
2998 else
2999 return WHY_RERAISE;
3000 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003001 Py_XDECREF(value);
3002 Py_XDECREF(type);
3003 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003004 return WHY_EXCEPTION;
3005}
3006
Tim Petersd6d010b2001-06-21 02:49:55 +00003007/* Iterate v argcnt times and store the results on the stack (via decreasing
3008 sp). Return 1 for success, 0 if error. */
3009
Barry Warsawe42b18f1997-08-25 22:13:04 +00003010static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003011unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003012{
Tim Petersd6d010b2001-06-21 02:49:55 +00003013 int i = 0;
3014 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003015 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003016
Tim Petersd6d010b2001-06-21 02:49:55 +00003017 assert(v != NULL);
3018
3019 it = PyObject_GetIter(v);
3020 if (it == NULL)
3021 goto Error;
3022
3023 for (; i < argcnt; i++) {
3024 w = PyIter_Next(it);
3025 if (w == NULL) {
3026 /* Iterator done, via error or exhaustion. */
3027 if (!PyErr_Occurred()) {
3028 PyErr_Format(PyExc_ValueError,
3029 "need more than %d value%s to unpack",
3030 i, i == 1 ? "" : "s");
3031 }
3032 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003033 }
3034 *--sp = w;
3035 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003036
3037 /* We better have exhausted the iterator now. */
3038 w = PyIter_Next(it);
3039 if (w == NULL) {
3040 if (PyErr_Occurred())
3041 goto Error;
3042 Py_DECREF(it);
3043 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003044 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003045 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003046 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003047 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003048Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003049 for (; i > 0; i--, sp++)
3050 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003051 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003052 return 0;
3053}
3054
3055
Guido van Rossum96a42c81992-01-12 02:29:51 +00003056#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003057static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003058prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003059{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003060 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003061 if (PyObject_Print(v, stdout, 0) != 0)
3062 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003063 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003064 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003065}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003066#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003067
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003068static void
Fred Drake5755ce62001-06-27 19:19:46 +00003069call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003070{
Guido van Rossumb209a111997-04-29 18:18:01 +00003071 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003072 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003073 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003074 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003075 value = Py_None;
3076 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003077 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003078 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003079 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003080 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003081 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003082 }
Fred Drake5755ce62001-06-27 19:19:46 +00003083 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003084 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003085 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003086 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003087 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003088 Py_XDECREF(type);
3089 Py_XDECREF(value);
3090 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003091 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003092}
3093
Fred Drake4ec5d562001-10-04 19:26:43 +00003094static void
3095call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003096 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003097{
3098 PyObject *type, *value, *traceback;
3099 int err;
3100 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003101 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003102 if (err == 0)
3103 PyErr_Restore(type, value, traceback);
3104 else {
3105 Py_XDECREF(type);
3106 Py_XDECREF(value);
3107 Py_XDECREF(traceback);
3108 }
3109}
3110
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003111static int
Fred Drake5755ce62001-06-27 19:19:46 +00003112call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3113 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003114{
Fred Drake5755ce62001-06-27 19:19:46 +00003115 register PyThreadState *tstate = frame->f_tstate;
3116 int result;
3117 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003118 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003119 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003120 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003121 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003122 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3123 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003124 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003125 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003126}
3127
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003128PyObject *
3129_PyEval_CallTracing(PyObject *func, PyObject *args)
3130{
3131 PyFrameObject *frame = PyEval_GetFrame();
3132 PyThreadState *tstate = frame->f_tstate;
3133 int save_tracing = tstate->tracing;
3134 int save_use_tracing = tstate->use_tracing;
3135 PyObject *result;
3136
3137 tstate->tracing = 0;
3138 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3139 || (tstate->c_profilefunc != NULL));
3140 result = PyObject_Call(func, args, NULL);
3141 tstate->tracing = save_tracing;
3142 tstate->use_tracing = save_use_tracing;
3143 return result;
3144}
3145
Michael W. Hudson006c7522002-11-08 13:08:46 +00003146static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003147maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003148 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3149 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150{
3151 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003152
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003153 In a nutshell, we use the co_lnotab field of the code object
3154 to tell when execution has moved onto a different line.
3155
3156 As mentioned above, the basic idea is so set things up so
3157 that
3158
3159 *instr_lb <= frame->f_lasti < *instr_ub
3160
3161 is true so long as execution does not change lines.
3162
3163 This is all fairly simple. Digging the information out of
3164 co_lnotab takes some work, but is conceptually clear.
3165
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003166 Somewhat harder to explain is why we don't *always* call the
3167 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003168
3169 Consider this code:
3170
3171 1: def f(a):
3172 2: if a:
3173 3: print 1
3174 4: else:
3175 5: print 2
3176
3177 which compiles to this:
3178
3179 2 0 LOAD_FAST 0 (a)
3180 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003181 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003182
3183 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003184 10 PRINT_ITEM
3185 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003186 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003187 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188
3189 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003190 19 PRINT_ITEM
3191 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003192 >> 21 LOAD_CONST 0 (None)
3193 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003194
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003195 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003196 15 and the co_lnotab will claim that execution has moved to
3197 line 3. This is at best misleading. In this case we could
3198 associate the POP_TOP with line 4, but that doesn't make
3199 sense in all cases (I think).
3200
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003201 What we do is only call the line trace function if the co_lnotab
3202 indicates we have jumped to the *start* of a line, i.e. if the
3203 current instruction offset matches the offset given for the
3204 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003205
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003206 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003207 Execution will jump from instruction offset 12 to offset 21.
3208 Then the co_lnotab would imply that execution has moved to line
3209 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003210
3211 Why do we set f_lineno when tracing? Well, consider the code
3212 above when 'a' is true. If stepping through this with 'n' in
3213 pdb, you would stop at line 1 with a "call" type event, then
3214 line events on lines 2 and 3, then a "return" type event -- but
3215 you would be shown line 5 during this event. This is a change
3216 from the behaviour in 2.2 and before, and I've found it
3217 confusing in practice. By setting and using f_lineno when
3218 tracing, one can report a line number different from that
3219 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003220 */
3221
Michael W. Hudson006c7522002-11-08 13:08:46 +00003222 int result = 0;
3223
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003224 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003225 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003226 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003227 unsigned char* p;
3228
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003229 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3230 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003231
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003232 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003233 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003234
3235 /* possible optimization: if f->f_lasti == instr_ub
3236 (likely to be a common case) then we already know
3237 instr_lb -- if we stored the matching value of p
3238 somwhere we could skip the first while loop. */
3239
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003240 /* see comments in compile.c for the description of
3241 co_lnotab. A point to remember: increments to p
3242 should come in pairs -- although we don't care about
3243 the line increments here, treating them as byte
3244 increments gets confusing, to say the least. */
3245
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003246 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003247 if (addr + *p > frame->f_lasti)
3248 break;
3249 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003250 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003251 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003252 --size;
3253 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003254
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003255 if (addr == frame->f_lasti) {
3256 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003257 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003258 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003259 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003260
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003261 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003262 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003263 addr += *p++;
3264 if (*p++)
3265 break;
3266 }
3267 *instr_ub = addr;
3268 }
3269 else {
3270 *instr_ub = INT_MAX;
3271 }
3272 }
Armin Rigobf57a142004-03-22 19:24:58 +00003273 else if (frame->f_lasti <= *instr_prev) {
3274 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003275 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003276 PyTrace_LINE, Py_None);
3277 }
3278 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003279 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003280}
3281
Fred Drake5755ce62001-06-27 19:19:46 +00003282void
3283PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003284{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003285 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003286 PyObject *temp = tstate->c_profileobj;
3287 Py_XINCREF(arg);
3288 tstate->c_profilefunc = NULL;
3289 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003290 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003291 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003292 Py_XDECREF(temp);
3293 tstate->c_profilefunc = func;
3294 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003295 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003296 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003297}
3298
3299void
3300PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3301{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003302 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003303 PyObject *temp = tstate->c_traceobj;
3304 Py_XINCREF(arg);
3305 tstate->c_tracefunc = NULL;
3306 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003307 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003308 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003309 Py_XDECREF(temp);
3310 tstate->c_tracefunc = func;
3311 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003312 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003313 tstate->use_tracing = ((func != NULL)
3314 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003315}
3316
Guido van Rossumb209a111997-04-29 18:18:01 +00003317PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003318PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003319{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003320 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003321 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003322 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003323 else
3324 return current_frame->f_builtins;
3325}
3326
Guido van Rossumb209a111997-04-29 18:18:01 +00003327PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003328PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003329{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003330 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003331 if (current_frame == NULL)
3332 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003333 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003334 return current_frame->f_locals;
3335}
3336
Guido van Rossumb209a111997-04-29 18:18:01 +00003337PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003338PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003339{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003340 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003341 if (current_frame == NULL)
3342 return NULL;
3343 else
3344 return current_frame->f_globals;
3345}
3346
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003347PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003348PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003349{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003350 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003351 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003352}
3353
Guido van Rossum6135a871995-01-09 17:53:26 +00003354int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003355PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003356{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003357 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003358 return current_frame == NULL ? 0 : current_frame->f_restricted;
3359}
3360
Guido van Rossumbe270261997-05-22 22:26:18 +00003361int
Tim Peters5ba58662001-07-16 02:29:45 +00003362PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003363{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003364 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003365 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003366
3367 if (current_frame != NULL) {
3368 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003369 const int compilerflags = codeflags & PyCF_MASK;
3370 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003371 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003372 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003373 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003374#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003375 if (codeflags & CO_GENERATOR_ALLOWED) {
3376 result = 1;
3377 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3378 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003379#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003380 }
3381 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003382}
3383
3384int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003385Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003386{
Guido van Rossumb209a111997-04-29 18:18:01 +00003387 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003388 if (f == NULL)
3389 return 0;
3390 if (!PyFile_SoftSpace(f, 0))
3391 return 0;
3392 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003393}
3394
Guido van Rossum3f5da241990-12-20 15:06:42 +00003395
Guido van Rossum681d79a1995-07-18 14:51:37 +00003396/* External interface to call any callable object.
3397 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003398
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003399#undef PyEval_CallObject
3400/* for backward compatibility: export this interface */
3401
Guido van Rossumb209a111997-04-29 18:18:01 +00003402PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003403PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003404{
Guido van Rossumb209a111997-04-29 18:18:01 +00003405 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003406}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003407#define PyEval_CallObject(func,arg) \
3408 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003409
Guido van Rossumb209a111997-04-29 18:18:01 +00003410PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003411PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003412{
Jeremy Hylton52820442001-01-03 23:52:36 +00003413 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003414
3415 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003416 arg = PyTuple_New(0);
3417 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003418 PyErr_SetString(PyExc_TypeError,
3419 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003420 return NULL;
3421 }
3422 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003423 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003424
Guido van Rossumb209a111997-04-29 18:18:01 +00003425 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003426 PyErr_SetString(PyExc_TypeError,
3427 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003428 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003429 return NULL;
3430 }
3431
Tim Peters6d6c1a32001-08-02 04:15:00 +00003432 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003433 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003434 return result;
3435}
3436
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003437const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003438PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003439{
3440 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003441 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003442 else if (PyFunction_Check(func))
3443 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3444 else if (PyCFunction_Check(func))
3445 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3446 else if (PyClass_Check(func))
3447 return PyString_AsString(((PyClassObject*)func)->cl_name);
3448 else if (PyInstance_Check(func)) {
3449 return PyString_AsString(
3450 ((PyInstanceObject*)func)->in_class->cl_name);
3451 } else {
3452 return func->ob_type->tp_name;
3453 }
3454}
3455
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003456const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003457PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003458{
3459 if (PyMethod_Check(func))
3460 return "()";
3461 else if (PyFunction_Check(func))
3462 return "()";
3463 else if (PyCFunction_Check(func))
3464 return "()";
3465 else if (PyClass_Check(func))
3466 return " constructor";
3467 else if (PyInstance_Check(func)) {
3468 return " instance";
3469 } else {
3470 return " object";
3471 }
3472}
3473
Jeremy Hylton52820442001-01-03 23:52:36 +00003474#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3475
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003476static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003477err_args(PyObject *func, int flags, int nargs)
3478{
3479 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003480 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003481 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003482 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003483 nargs);
3484 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003485 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003486 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003487 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003488 nargs);
3489}
3490
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003491#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003492if (tstate->use_tracing && tstate->c_profilefunc) { \
3493 if (call_trace(tstate->c_profilefunc, \
3494 tstate->c_profileobj, \
3495 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003496 func)) { \
3497 x = NULL; \
3498 } \
3499 else { \
3500 x = call; \
3501 if (tstate->c_profilefunc != NULL) { \
3502 if (x == NULL) { \
3503 call_trace_protected(tstate->c_profilefunc, \
3504 tstate->c_profileobj, \
3505 tstate->frame, PyTrace_C_EXCEPTION, \
3506 func); \
3507 /* XXX should pass (type, value, tb) */ \
3508 } else { \
3509 if (call_trace(tstate->c_profilefunc, \
3510 tstate->c_profileobj, \
3511 tstate->frame, PyTrace_C_RETURN, \
3512 func)) { \
3513 Py_DECREF(x); \
3514 x = NULL; \
3515 } \
3516 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003517 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003518 } \
3519} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003520 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003521 }
3522
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003523static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003524call_function(PyObject ***pp_stack, int oparg
3525#ifdef WITH_TSC
3526 , uint64* pintr0, uint64* pintr1
3527#endif
3528 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003529{
3530 int na = oparg & 0xff;
3531 int nk = (oparg>>8) & 0xff;
3532 int n = na + 2 * nk;
3533 PyObject **pfunc = (*pp_stack) - n - 1;
3534 PyObject *func = *pfunc;
3535 PyObject *x, *w;
3536
Jeremy Hylton985eba52003-02-05 23:13:00 +00003537 /* Always dispatch PyCFunction first, because these are
3538 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003539 */
3540 if (PyCFunction_Check(func) && nk == 0) {
3541 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003542 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003543
3544 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003545 if (flags & (METH_NOARGS | METH_O)) {
3546 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3547 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003548 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003549 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003550 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003551 else if (flags & METH_O && na == 1) {
3552 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003553 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003554 Py_DECREF(arg);
3555 }
3556 else {
3557 err_args(func, flags, na);
3558 x = NULL;
3559 }
3560 }
3561 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003562 PyObject *callargs;
3563 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003564 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003565 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003566 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003567 Py_XDECREF(callargs);
3568 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569 } else {
3570 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3571 /* optimize access to bound methods */
3572 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003573 PCALL(PCALL_METHOD);
3574 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003575 Py_INCREF(self);
3576 func = PyMethod_GET_FUNCTION(func);
3577 Py_INCREF(func);
3578 Py_DECREF(*pfunc);
3579 *pfunc = self;
3580 na++;
3581 n++;
3582 } else
3583 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003584 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003585 if (PyFunction_Check(func))
3586 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003587 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003588 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003589 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003590 Py_DECREF(func);
3591 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003592
Jeremy Hylton985eba52003-02-05 23:13:00 +00003593 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003594 while ((*pp_stack) > pfunc) {
3595 w = EXT_POP(*pp_stack);
3596 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003597 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003598 }
3599 return x;
3600}
3601
Jeremy Hylton192690e2002-08-16 18:36:11 +00003602/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003603 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003604 For the simplest case -- a function that takes only positional
3605 arguments and is called with only positional arguments -- it
3606 inlines the most primitive frame setup code from
3607 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3608 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003609*/
3610
3611static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003612fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003613{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003614 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003615 PyObject *globals = PyFunction_GET_GLOBALS(func);
3616 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3617 PyObject **d = NULL;
3618 int nd = 0;
3619
Jeremy Hylton985eba52003-02-05 23:13:00 +00003620 PCALL(PCALL_FUNCTION);
3621 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003622 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003623 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3624 PyFrameObject *f;
3625 PyObject *retval = NULL;
3626 PyThreadState *tstate = PyThreadState_GET();
3627 PyObject **fastlocals, **stack;
3628 int i;
3629
3630 PCALL(PCALL_FASTER_FUNCTION);
3631 assert(globals != NULL);
3632 /* XXX Perhaps we should create a specialized
3633 PyFrame_New() that doesn't take locals, but does
3634 take builtins without sanity checking them.
3635 */
3636 f = PyFrame_New(tstate, co, globals, NULL);
3637 if (f == NULL)
3638 return NULL;
3639
3640 fastlocals = f->f_localsplus;
3641 stack = (*pp_stack) - n;
3642
3643 for (i = 0; i < n; i++) {
3644 Py_INCREF(*stack);
3645 fastlocals[i] = *stack++;
3646 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003647 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003648 assert(tstate != NULL);
3649 ++tstate->recursion_depth;
3650 Py_DECREF(f);
3651 --tstate->recursion_depth;
3652 return retval;
3653 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003654 if (argdefs != NULL) {
3655 d = &PyTuple_GET_ITEM(argdefs, 0);
3656 nd = ((PyTupleObject *)argdefs)->ob_size;
3657 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003658 return PyEval_EvalCodeEx(co, globals,
3659 (PyObject *)NULL, (*pp_stack)-n, na,
3660 (*pp_stack)-2*nk, nk, d, nd,
3661 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003662}
3663
3664static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003665update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3666 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003667{
3668 PyObject *kwdict = NULL;
3669 if (orig_kwdict == NULL)
3670 kwdict = PyDict_New();
3671 else {
3672 kwdict = PyDict_Copy(orig_kwdict);
3673 Py_DECREF(orig_kwdict);
3674 }
3675 if (kwdict == NULL)
3676 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003677 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003678 int err;
3679 PyObject *value = EXT_POP(*pp_stack);
3680 PyObject *key = EXT_POP(*pp_stack);
3681 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003682 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003683 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003684 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003685 PyEval_GetFuncName(func),
3686 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003687 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003688 Py_DECREF(key);
3689 Py_DECREF(value);
3690 Py_DECREF(kwdict);
3691 return NULL;
3692 }
3693 err = PyDict_SetItem(kwdict, key, value);
3694 Py_DECREF(key);
3695 Py_DECREF(value);
3696 if (err) {
3697 Py_DECREF(kwdict);
3698 return NULL;
3699 }
3700 }
3701 return kwdict;
3702}
3703
3704static PyObject *
3705update_star_args(int nstack, int nstar, PyObject *stararg,
3706 PyObject ***pp_stack)
3707{
3708 PyObject *callargs, *w;
3709
3710 callargs = PyTuple_New(nstack + nstar);
3711 if (callargs == NULL) {
3712 return NULL;
3713 }
3714 if (nstar) {
3715 int i;
3716 for (i = 0; i < nstar; i++) {
3717 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3718 Py_INCREF(a);
3719 PyTuple_SET_ITEM(callargs, nstack + i, a);
3720 }
3721 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003722 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003723 w = EXT_POP(*pp_stack);
3724 PyTuple_SET_ITEM(callargs, nstack, w);
3725 }
3726 return callargs;
3727}
3728
3729static PyObject *
3730load_args(PyObject ***pp_stack, int na)
3731{
3732 PyObject *args = PyTuple_New(na);
3733 PyObject *w;
3734
3735 if (args == NULL)
3736 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003737 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003738 w = EXT_POP(*pp_stack);
3739 PyTuple_SET_ITEM(args, na, w);
3740 }
3741 return args;
3742}
3743
3744static PyObject *
3745do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3746{
3747 PyObject *callargs = NULL;
3748 PyObject *kwdict = NULL;
3749 PyObject *result = NULL;
3750
3751 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003752 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003753 if (kwdict == NULL)
3754 goto call_fail;
3755 }
3756 callargs = load_args(pp_stack, na);
3757 if (callargs == NULL)
3758 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003759#ifdef CALL_PROFILE
3760 /* At this point, we have to look at the type of func to
3761 update the call stats properly. Do it here so as to avoid
3762 exposing the call stats machinery outside ceval.c
3763 */
3764 if (PyFunction_Check(func))
3765 PCALL(PCALL_FUNCTION);
3766 else if (PyMethod_Check(func))
3767 PCALL(PCALL_METHOD);
3768 else if (PyType_Check(func))
3769 PCALL(PCALL_TYPE);
3770 else
3771 PCALL(PCALL_OTHER);
3772#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003773 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003774 call_fail:
3775 Py_XDECREF(callargs);
3776 Py_XDECREF(kwdict);
3777 return result;
3778}
3779
3780static PyObject *
3781ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3782{
3783 int nstar = 0;
3784 PyObject *callargs = NULL;
3785 PyObject *stararg = NULL;
3786 PyObject *kwdict = NULL;
3787 PyObject *result = NULL;
3788
3789 if (flags & CALL_FLAG_KW) {
3790 kwdict = EXT_POP(*pp_stack);
3791 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003792 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003793 "%s%s argument after ** "
3794 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003795 PyEval_GetFuncName(func),
3796 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003797 goto ext_call_fail;
3798 }
3799 }
3800 if (flags & CALL_FLAG_VAR) {
3801 stararg = EXT_POP(*pp_stack);
3802 if (!PyTuple_Check(stararg)) {
3803 PyObject *t = NULL;
3804 t = PySequence_Tuple(stararg);
3805 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003806 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3807 PyErr_Format(PyExc_TypeError,
3808 "%s%s argument after * "
3809 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003810 PyEval_GetFuncName(func),
3811 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003812 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003813 goto ext_call_fail;
3814 }
3815 Py_DECREF(stararg);
3816 stararg = t;
3817 }
3818 nstar = PyTuple_GET_SIZE(stararg);
3819 }
3820 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003821 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003822 if (kwdict == NULL)
3823 goto ext_call_fail;
3824 }
3825 callargs = update_star_args(na, nstar, stararg, pp_stack);
3826 if (callargs == NULL)
3827 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003828#ifdef CALL_PROFILE
3829 /* At this point, we have to look at the type of func to
3830 update the call stats properly. Do it here so as to avoid
3831 exposing the call stats machinery outside ceval.c
3832 */
3833 if (PyFunction_Check(func))
3834 PCALL(PCALL_FUNCTION);
3835 else if (PyMethod_Check(func))
3836 PCALL(PCALL_METHOD);
3837 else if (PyType_Check(func))
3838 PCALL(PCALL_TYPE);
3839 else
3840 PCALL(PCALL_OTHER);
3841#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003842 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003843 ext_call_fail:
3844 Py_XDECREF(callargs);
3845 Py_XDECREF(kwdict);
3846 Py_XDECREF(stararg);
3847 return result;
3848}
3849
Tim Peterscb479e72001-12-16 19:11:44 +00003850/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3851 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3852 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3853*/
Tim Petersb5196382001-12-16 19:44:20 +00003854/* Note: If v is NULL, return success without storing into *pi. This
3855 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3856 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003857*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003858int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003859_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003860{
Tim Petersb5196382001-12-16 19:44:20 +00003861 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003862 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003863 if (PyInt_Check(v)) {
3864 x = PyInt_AsLong(v);
3865 } else if (PyLong_Check(v)) {
3866 x = PyLong_AsLong(v);
3867 if (x==-1 && PyErr_Occurred()) {
3868 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003869 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003870
Guido van Rossumac7be682001-01-17 15:42:30 +00003871 if (!PyErr_ExceptionMatches(
3872 PyExc_OverflowError)) {
3873 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003874 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003875 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003876 }
3877
Guido van Rossumac7be682001-01-17 15:42:30 +00003878 /* Clear the OverflowError */
3879 PyErr_Clear();
3880
3881 /* It's an overflow error, so we need to
3882 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003883 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003884 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003885
3886 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003887 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003888 if (long_zero == NULL)
3889 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003890
3891 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003892 cmp = PyObject_RichCompareBool(v, long_zero,
3893 Py_GT);
3894 Py_DECREF(long_zero);
3895 if (cmp < 0)
3896 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003897 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003898 x = INT_MAX;
3899 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003900 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003901 }
3902 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003903 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003904 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003905 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003906 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003907 /* Truncate -- very long indices are truncated anyway */
3908 if (x > INT_MAX)
3909 x = INT_MAX;
3910 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003911 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003912 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003913 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003914 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003915}
3916
Guido van Rossum50d756e2001-08-18 17:43:36 +00003917#undef ISINT
3918#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3919
Guido van Rossumb209a111997-04-29 18:18:01 +00003920static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003921apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003922{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003923 PyTypeObject *tp = u->ob_type;
3924 PySequenceMethods *sq = tp->tp_as_sequence;
3925
3926 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3927 int ilow = 0, ihigh = INT_MAX;
3928 if (!_PyEval_SliceIndex(v, &ilow))
3929 return NULL;
3930 if (!_PyEval_SliceIndex(w, &ihigh))
3931 return NULL;
3932 return PySequence_GetSlice(u, ilow, ihigh);
3933 }
3934 else {
3935 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003936 if (slice != NULL) {
3937 PyObject *res = PyObject_GetItem(u, slice);
3938 Py_DECREF(slice);
3939 return res;
3940 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003941 else
3942 return NULL;
3943 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003944}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003945
3946static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003947assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3948 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003949{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003950 PyTypeObject *tp = u->ob_type;
3951 PySequenceMethods *sq = tp->tp_as_sequence;
3952
3953 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3954 int ilow = 0, ihigh = INT_MAX;
3955 if (!_PyEval_SliceIndex(v, &ilow))
3956 return -1;
3957 if (!_PyEval_SliceIndex(w, &ihigh))
3958 return -1;
3959 if (x == NULL)
3960 return PySequence_DelSlice(u, ilow, ihigh);
3961 else
3962 return PySequence_SetSlice(u, ilow, ihigh, x);
3963 }
3964 else {
3965 PyObject *slice = PySlice_New(v, w, NULL);
3966 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003967 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003968 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003969 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003970 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003971 res = PyObject_DelItem(u, slice);
3972 Py_DECREF(slice);
3973 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003974 }
3975 else
3976 return -1;
3977 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003978}
3979
Guido van Rossumb209a111997-04-29 18:18:01 +00003980static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003981cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003982{
Guido van Rossumac7be682001-01-17 15:42:30 +00003983 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003984 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003985 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003986 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003987 break;
3988 case PyCmp_IS_NOT:
3989 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003990 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003991 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003992 res = PySequence_Contains(w, v);
3993 if (res < 0)
3994 return NULL;
3995 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003996 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003997 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003998 if (res < 0)
3999 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004000 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004001 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004002 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004003 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004004 break;
4005 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004006 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004007 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004008 v = res ? Py_True : Py_False;
4009 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004010 return v;
4011}
4012
Thomas Wouters52152252000-08-17 22:55:00 +00004013static PyObject *
4014import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004015{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004016 PyObject *x;
4017
4018 x = PyObject_GetAttr(v, name);
4019 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004020 PyErr_Format(PyExc_ImportError,
4021 "cannot import name %.230s",
4022 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004023 }
Thomas Wouters52152252000-08-17 22:55:00 +00004024 return x;
4025}
Guido van Rossumac7be682001-01-17 15:42:30 +00004026
Thomas Wouters52152252000-08-17 22:55:00 +00004027static int
4028import_all_from(PyObject *locals, PyObject *v)
4029{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004030 PyObject *all = PyObject_GetAttrString(v, "__all__");
4031 PyObject *dict, *name, *value;
4032 int skip_leading_underscores = 0;
4033 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004034
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004035 if (all == NULL) {
4036 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4037 return -1; /* Unexpected error */
4038 PyErr_Clear();
4039 dict = PyObject_GetAttrString(v, "__dict__");
4040 if (dict == NULL) {
4041 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4042 return -1;
4043 PyErr_SetString(PyExc_ImportError,
4044 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004045 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004046 }
4047 all = PyMapping_Keys(dict);
4048 Py_DECREF(dict);
4049 if (all == NULL)
4050 return -1;
4051 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004052 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004053
4054 for (pos = 0, err = 0; ; pos++) {
4055 name = PySequence_GetItem(all, pos);
4056 if (name == NULL) {
4057 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4058 err = -1;
4059 else
4060 PyErr_Clear();
4061 break;
4062 }
4063 if (skip_leading_underscores &&
4064 PyString_Check(name) &&
4065 PyString_AS_STRING(name)[0] == '_')
4066 {
4067 Py_DECREF(name);
4068 continue;
4069 }
4070 value = PyObject_GetAttr(v, name);
4071 if (value == NULL)
4072 err = -1;
4073 else
4074 err = PyDict_SetItem(locals, name, value);
4075 Py_DECREF(name);
4076 Py_XDECREF(value);
4077 if (err != 0)
4078 break;
4079 }
4080 Py_DECREF(all);
4081 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004082}
4083
Guido van Rossumb209a111997-04-29 18:18:01 +00004084static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004085build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004086{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004087 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004088
4089 if (PyDict_Check(methods))
4090 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004091 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004092 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004093 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4094 base = PyTuple_GET_ITEM(bases, 0);
4095 metaclass = PyObject_GetAttrString(base, "__class__");
4096 if (metaclass == NULL) {
4097 PyErr_Clear();
4098 metaclass = (PyObject *)base->ob_type;
4099 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004100 }
4101 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004102 else {
4103 PyObject *g = PyEval_GetGlobals();
4104 if (g != NULL && PyDict_Check(g))
4105 metaclass = PyDict_GetItemString(g, "__metaclass__");
4106 if (metaclass == NULL)
4107 metaclass = (PyObject *) &PyClass_Type;
4108 Py_INCREF(metaclass);
4109 }
4110 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4111 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004112 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4113 /* A type error here likely means that the user passed
4114 in a base that was not a class (such the random module
4115 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004116 by augmenting the error message with more information.*/
4117
4118 PyObject *ptype, *pvalue, *ptraceback;
4119
4120 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4121 if (PyString_Check(pvalue)) {
4122 PyObject *newmsg;
4123 newmsg = PyString_FromFormat(
4124 "Error when calling the metaclass bases\n %s",
4125 PyString_AS_STRING(pvalue));
4126 if (newmsg != NULL) {
4127 Py_DECREF(pvalue);
4128 pvalue = newmsg;
4129 }
4130 }
4131 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004132 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004133 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004134}
4135
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004136static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004137exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4138 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004139{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004140 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004141 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004142 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004143
Guido van Rossumb209a111997-04-29 18:18:01 +00004144 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4145 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004147 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004148 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004149 locals = PyTuple_GetItem(prog, 2);
4150 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004151 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004152 if (globals == Py_None) {
4153 globals = PyEval_GetGlobals();
4154 if (locals == Py_None) {
4155 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004156 plain = 1;
4157 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004158 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004159 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004160 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004161 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004162 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004163 !PyCode_Check(prog) &&
4164 !PyFile_Check(prog)) {
4165 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004166 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004167 return -1;
4168 }
Fred Drake661ea262000-10-24 19:57:45 +00004169 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004170 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004171 "exec: arg 2 must be a dictionary or None");
4172 return -1;
4173 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004174 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004175 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004176 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004177 return -1;
4178 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004179 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004180 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004181 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004182 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4183 PyErr_SetString(PyExc_TypeError,
4184 "code object passed to exec may not contain free variables");
4185 return -1;
4186 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004187 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004188 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004189 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004190 FILE *fp = PyFile_AsFile(prog);
4191 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004192 PyCompilerFlags cf;
4193 cf.cf_flags = 0;
4194 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004195 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004196 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004197 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004198 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004199 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004200 }
4201 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004202 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004203 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004204 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004205 cf.cf_flags = 0;
4206#ifdef Py_USING_UNICODE
4207 if (PyUnicode_Check(prog)) {
4208 tmp = PyUnicode_AsUTF8String(prog);
4209 if (tmp == NULL)
4210 return -1;
4211 prog = tmp;
4212 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4213 }
4214#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004215 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004216 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004217 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004218 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004219 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004220 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004221 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004222 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004223 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004224 if (plain)
4225 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004226 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004227 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004228 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004229 return 0;
4230}
Guido van Rossum24c13741995-02-14 09:42:43 +00004231
Guido van Rossumac7be682001-01-17 15:42:30 +00004232static void
Paul Prescode68140d2000-08-30 20:25:01 +00004233format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4234{
4235 char *obj_str;
4236
4237 if (!obj)
4238 return;
4239
4240 obj_str = PyString_AsString(obj);
4241 if (!obj_str)
4242 return;
4243
4244 PyErr_Format(exc, format_str, obj_str);
4245}
Guido van Rossum950361c1997-01-24 13:49:28 +00004246
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004247static PyObject *
4248string_concatenate(PyObject *v, PyObject *w,
4249 PyFrameObject *f, unsigned char *next_instr)
4250{
4251 /* This function implements 'variable += expr' when both arguments
4252 are strings. */
4253
4254 if (v->ob_refcnt == 2) {
4255 /* In the common case, there are 2 references to the value
4256 * stored in 'variable' when the += is performed: one on the
4257 * value stack (in 'v') and one still stored in the 'variable'.
4258 * We try to delete the variable now to reduce the refcnt to 1.
4259 */
4260 switch (*next_instr) {
4261 case STORE_FAST:
4262 {
4263 int oparg = PEEKARG();
4264 PyObject **fastlocals = f->f_localsplus;
4265 if (GETLOCAL(oparg) == v)
4266 SETLOCAL(oparg, NULL);
4267 break;
4268 }
4269 case STORE_DEREF:
4270 {
4271 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4272 PyObject *c = freevars[PEEKARG()];
4273 if (PyCell_GET(c) == v)
4274 PyCell_Set(c, NULL);
4275 break;
4276 }
4277 case STORE_NAME:
4278 {
4279 PyObject *names = f->f_code->co_names;
4280 PyObject *name = GETITEM(names, PEEKARG());
4281 PyObject *locals = f->f_locals;
4282 if (PyDict_CheckExact(locals) &&
4283 PyDict_GetItem(locals, name) == v) {
4284 if (PyDict_DelItem(locals, name) != 0) {
4285 PyErr_Clear();
4286 }
4287 }
4288 break;
4289 }
4290 }
4291 }
4292
Armin Rigo618fbf52004-08-07 20:58:32 +00004293 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004294 /* Now we own the last reference to 'v', so we can resize it
4295 * in-place.
4296 */
4297 int v_len = PyString_GET_SIZE(v);
4298 int w_len = PyString_GET_SIZE(w);
4299 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4300 /* XXX if _PyString_Resize() fails, 'v' has been
4301 * deallocated so it cannot be put back into 'variable'.
4302 * The MemoryError is raised when there is no value in
4303 * 'variable', which might (very remotely) be a cause
4304 * of incompatibilities.
4305 */
4306 return NULL;
4307 }
4308 /* copy 'w' into the newly allocated area of 'v' */
4309 memcpy(PyString_AS_STRING(v) + v_len,
4310 PyString_AS_STRING(w), w_len);
4311 return v;
4312 }
4313 else {
4314 /* When in-place resizing is not an option. */
4315 PyString_Concat(&v, w);
4316 return v;
4317 }
4318}
4319
Guido van Rossum950361c1997-01-24 13:49:28 +00004320#ifdef DYNAMIC_EXECUTION_PROFILE
4321
Skip Montanarof118cb12001-10-15 20:51:38 +00004322static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004323getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004324{
4325 int i;
4326 PyObject *l = PyList_New(256);
4327 if (l == NULL) return NULL;
4328 for (i = 0; i < 256; i++) {
4329 PyObject *x = PyInt_FromLong(a[i]);
4330 if (x == NULL) {
4331 Py_DECREF(l);
4332 return NULL;
4333 }
4334 PyList_SetItem(l, i, x);
4335 }
4336 for (i = 0; i < 256; i++)
4337 a[i] = 0;
4338 return l;
4339}
4340
4341PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004342_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004343{
4344#ifndef DXPAIRS
4345 return getarray(dxp);
4346#else
4347 int i;
4348 PyObject *l = PyList_New(257);
4349 if (l == NULL) return NULL;
4350 for (i = 0; i < 257; i++) {
4351 PyObject *x = getarray(dxpairs[i]);
4352 if (x == NULL) {
4353 Py_DECREF(l);
4354 return NULL;
4355 }
4356 PyList_SetItem(l, i, x);
4357 }
4358 return l;
4359#endif
4360}
4361
4362#endif