blob: d311537c8ea2165a4a09514184ce51157b6bed18 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Lö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 *,
105 PyFrameObject *, int);
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
Guido van Rossum408027e1996-12-30 16:17:54 +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
746 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
747#endif
748#if defined(Py_DEBUG) || defined(LLTRACE)
749 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 {
2260 int nfree;
2261 v = POP(); /* code object */
2262 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002263 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002264 Py_DECREF(v);
2265 /* XXX Maybe this should be a separate opcode? */
2266 if (x != NULL && nfree > 0) {
2267 v = PyTuple_New(nfree);
2268 if (v == NULL) {
2269 Py_DECREF(x);
2270 x = NULL;
2271 break;
2272 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002273 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002274 w = POP();
2275 PyTuple_SET_ITEM(v, nfree, w);
2276 }
2277 err = PyFunction_SetClosure(x, v);
2278 Py_DECREF(v);
2279 }
2280 if (x != NULL && oparg > 0) {
2281 v = PyTuple_New(oparg);
2282 if (v == NULL) {
2283 Py_DECREF(x);
2284 x = NULL;
2285 break;
2286 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002287 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002288 w = POP();
2289 PyTuple_SET_ITEM(v, oparg, w);
2290 }
2291 err = PyFunction_SetDefaults(x, v);
2292 Py_DECREF(v);
2293 }
2294 PUSH(x);
2295 break;
2296 }
2297
Guido van Rossum8861b741996-07-30 16:49:37 +00002298 case BUILD_SLICE:
2299 if (oparg == 3)
2300 w = POP();
2301 else
2302 w = NULL;
2303 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002304 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002305 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 Py_DECREF(u);
2307 Py_DECREF(v);
2308 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002309 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002310 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002311 break;
2312
Fred Drakeef8ace32000-08-24 00:32:09 +00002313 case EXTENDED_ARG:
2314 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002315 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002316 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002317
Guido van Rossum374a9221991-04-04 10:40:29 +00002318 default:
2319 fprintf(stderr,
2320 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002321 PyCode_Addr2Line(f->f_code, f->f_lasti),
2322 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002323 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 why = WHY_EXCEPTION;
2325 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002326
2327#ifdef CASE_TOO_BIG
2328 }
2329#endif
2330
Guido van Rossum374a9221991-04-04 10:40:29 +00002331 } /* switch */
2332
2333 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002334
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002335 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002336
Guido van Rossum374a9221991-04-04 10:40:29 +00002337 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002340 if (err == 0 && x != NULL) {
2341#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002342 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002343 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002344 fprintf(stderr,
2345 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002346 else {
2347#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002348 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002349 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002350#ifdef CHECKEXC
2351 }
2352#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002353 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002354 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002355 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 err = 0;
2357 }
2358
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002361 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002362 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002363 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002364 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 why = WHY_EXCEPTION;
2366 }
2367 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002368#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002369 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002370 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002371 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002372 char buf[1024];
2373 sprintf(buf, "Stack unwind with exception "
2374 "set and why=%d", why);
2375 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002376 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 }
2378#endif
2379
2380 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002383 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002384
Fred Drake8f51f542001-10-04 14:48:42 +00002385 if (tstate->c_tracefunc != NULL)
2386 call_exc_trace(tstate->c_tracefunc,
2387 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002388 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002389
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002391
Guido van Rossum374a9221991-04-04 10:40:29 +00002392 if (why == WHY_RERAISE)
2393 why = WHY_EXCEPTION;
2394
2395 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002397fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002398 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002399 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002400
Tim Peters8a5c3c72004-04-05 19:36:21 +00002401 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002402 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2403 /* For a continue inside a try block,
2404 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002405 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2406 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002407 why = WHY_NOT;
2408 JUMPTO(PyInt_AS_LONG(retval));
2409 Py_DECREF(retval);
2410 break;
2411 }
2412
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 while (STACK_LEVEL() > b->b_level) {
2414 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 }
2417 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2418 why = WHY_NOT;
2419 JUMPTO(b->b_handler);
2420 break;
2421 }
2422 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002423 (b->b_type == SETUP_EXCEPT &&
2424 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002425 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002426 PyObject *exc, *val, *tb;
2427 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002428 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002429 val = Py_None;
2430 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002432 /* Make the raw exception data
2433 available to the handler,
2434 so a program can emulate the
2435 Python main loop. Don't do
2436 this for 'finally'. */
2437 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002438 PyErr_NormalizeException(
2439 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002440 set_exc_info(tstate,
2441 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002442 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002443 if (tb == NULL) {
2444 Py_INCREF(Py_None);
2445 PUSH(Py_None);
2446 } else
2447 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002448 PUSH(val);
2449 PUSH(exc);
2450 }
2451 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002452 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002453 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002454 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 PUSH(v);
2456 }
2457 why = WHY_NOT;
2458 JUMPTO(b->b_handler);
2459 break;
2460 }
2461 } /* unwind stack */
2462
2463 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Guido van Rossum374a9221991-04-04 10:40:29 +00002465 if (why != WHY_NOT)
2466 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002467 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Guido van Rossum374a9221991-04-04 10:40:29 +00002469 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002470
Tim Peters8a5c3c72004-04-05 19:36:21 +00002471 assert(why != WHY_YIELD);
2472 /* Pop remaining stack entries. */
2473 while (!EMPTY()) {
2474 v = POP();
2475 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002476 }
2477
Tim Peters8a5c3c72004-04-05 19:36:21 +00002478 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002479 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002480
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002481fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002482 if (tstate->use_tracing) {
2483 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002484 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002485 if (call_trace(tstate->c_tracefunc,
2486 tstate->c_traceobj, f,
2487 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002488 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002489 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002490 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002491 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002492 }
Fred Drake8f51f542001-10-04 14:48:42 +00002493 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002494 if (why == WHY_EXCEPTION)
2495 call_trace_protected(tstate->c_profilefunc,
2496 tstate->c_profileobj, f,
2497 PyTrace_RETURN);
2498 else if (call_trace(tstate->c_profilefunc,
2499 tstate->c_profileobj, f,
2500 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002501 Py_XDECREF(retval);
2502 retval = NULL;
2503 why = WHY_EXCEPTION;
2504 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002505 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002506 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002507
Guido van Rossuma027efa1997-05-05 20:56:21 +00002508 reset_exc_info(tstate);
2509
Tim Peters5ca576e2001-06-18 22:08:13 +00002510 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002511 exit_eval_frame:
2512 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002513 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002514
Guido van Rossum96a42c81992-01-12 02:29:51 +00002515 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002516}
2517
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002518/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002519 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002520 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002521
Tim Peters6d6c1a32001-08-02 04:15:00 +00002522PyObject *
2523PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002524 PyObject **args, int argcount, PyObject **kws, int kwcount,
2525 PyObject **defs, int defcount, PyObject *closure)
2526{
2527 register PyFrameObject *f;
2528 register PyObject *retval = NULL;
2529 register PyObject **fastlocals, **freevars;
2530 PyThreadState *tstate = PyThreadState_GET();
2531 PyObject *x, *u;
2532
2533 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002534 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002535 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002536 return NULL;
2537 }
2538
Jeremy Hylton985eba52003-02-05 23:13:00 +00002539 assert(globals != NULL);
2540 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002541 if (f == NULL)
2542 return NULL;
2543
2544 fastlocals = f->f_localsplus;
2545 freevars = f->f_localsplus + f->f_nlocals;
2546
2547 if (co->co_argcount > 0 ||
2548 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2549 int i;
2550 int n = argcount;
2551 PyObject *kwdict = NULL;
2552 if (co->co_flags & CO_VARKEYWORDS) {
2553 kwdict = PyDict_New();
2554 if (kwdict == NULL)
2555 goto fail;
2556 i = co->co_argcount;
2557 if (co->co_flags & CO_VARARGS)
2558 i++;
2559 SETLOCAL(i, kwdict);
2560 }
2561 if (argcount > co->co_argcount) {
2562 if (!(co->co_flags & CO_VARARGS)) {
2563 PyErr_Format(PyExc_TypeError,
2564 "%.200s() takes %s %d "
2565 "%sargument%s (%d given)",
2566 PyString_AsString(co->co_name),
2567 defcount ? "at most" : "exactly",
2568 co->co_argcount,
2569 kwcount ? "non-keyword " : "",
2570 co->co_argcount == 1 ? "" : "s",
2571 argcount);
2572 goto fail;
2573 }
2574 n = co->co_argcount;
2575 }
2576 for (i = 0; i < n; i++) {
2577 x = args[i];
2578 Py_INCREF(x);
2579 SETLOCAL(i, x);
2580 }
2581 if (co->co_flags & CO_VARARGS) {
2582 u = PyTuple_New(argcount - n);
2583 if (u == NULL)
2584 goto fail;
2585 SETLOCAL(co->co_argcount, u);
2586 for (i = n; i < argcount; i++) {
2587 x = args[i];
2588 Py_INCREF(x);
2589 PyTuple_SET_ITEM(u, i-n, x);
2590 }
2591 }
2592 for (i = 0; i < kwcount; i++) {
2593 PyObject *keyword = kws[2*i];
2594 PyObject *value = kws[2*i + 1];
2595 int j;
2596 if (keyword == NULL || !PyString_Check(keyword)) {
2597 PyErr_Format(PyExc_TypeError,
2598 "%.200s() keywords must be strings",
2599 PyString_AsString(co->co_name));
2600 goto fail;
2601 }
2602 /* XXX slow -- speed up using dictionary? */
2603 for (j = 0; j < co->co_argcount; j++) {
2604 PyObject *nm = PyTuple_GET_ITEM(
2605 co->co_varnames, j);
2606 int cmp = PyObject_RichCompareBool(
2607 keyword, nm, Py_EQ);
2608 if (cmp > 0)
2609 break;
2610 else if (cmp < 0)
2611 goto fail;
2612 }
2613 /* Check errors from Compare */
2614 if (PyErr_Occurred())
2615 goto fail;
2616 if (j >= co->co_argcount) {
2617 if (kwdict == NULL) {
2618 PyErr_Format(PyExc_TypeError,
2619 "%.200s() got an unexpected "
2620 "keyword argument '%.400s'",
2621 PyString_AsString(co->co_name),
2622 PyString_AsString(keyword));
2623 goto fail;
2624 }
2625 PyDict_SetItem(kwdict, keyword, value);
2626 }
2627 else {
2628 if (GETLOCAL(j) != NULL) {
2629 PyErr_Format(PyExc_TypeError,
2630 "%.200s() got multiple "
2631 "values for keyword "
2632 "argument '%.400s'",
2633 PyString_AsString(co->co_name),
2634 PyString_AsString(keyword));
2635 goto fail;
2636 }
2637 Py_INCREF(value);
2638 SETLOCAL(j, value);
2639 }
2640 }
2641 if (argcount < co->co_argcount) {
2642 int m = co->co_argcount - defcount;
2643 for (i = argcount; i < m; i++) {
2644 if (GETLOCAL(i) == NULL) {
2645 PyErr_Format(PyExc_TypeError,
2646 "%.200s() takes %s %d "
2647 "%sargument%s (%d given)",
2648 PyString_AsString(co->co_name),
2649 ((co->co_flags & CO_VARARGS) ||
2650 defcount) ? "at least"
2651 : "exactly",
2652 m, kwcount ? "non-keyword " : "",
2653 m == 1 ? "" : "s", i);
2654 goto fail;
2655 }
2656 }
2657 if (n > m)
2658 i = n - m;
2659 else
2660 i = 0;
2661 for (; i < defcount; i++) {
2662 if (GETLOCAL(m+i) == NULL) {
2663 PyObject *def = defs[i];
2664 Py_INCREF(def);
2665 SETLOCAL(m+i, def);
2666 }
2667 }
2668 }
2669 }
2670 else {
2671 if (argcount > 0 || kwcount > 0) {
2672 PyErr_Format(PyExc_TypeError,
2673 "%.200s() takes no arguments (%d given)",
2674 PyString_AsString(co->co_name),
2675 argcount + kwcount);
2676 goto fail;
2677 }
2678 }
2679 /* Allocate and initialize storage for cell vars, and copy free
2680 vars into frame. This isn't too efficient right now. */
2681 if (f->f_ncells) {
2682 int i = 0, j = 0, nargs, found;
2683 char *cellname, *argname;
2684 PyObject *c;
2685
2686 nargs = co->co_argcount;
2687 if (co->co_flags & CO_VARARGS)
2688 nargs++;
2689 if (co->co_flags & CO_VARKEYWORDS)
2690 nargs++;
2691
2692 /* Check for cells that shadow args */
2693 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2694 cellname = PyString_AS_STRING(
2695 PyTuple_GET_ITEM(co->co_cellvars, i));
2696 found = 0;
2697 while (j < nargs) {
2698 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 }
2708 j++;
2709 }
2710 if (found == 0) {
2711 c = PyCell_New(NULL);
2712 if (c == NULL)
2713 goto fail;
2714 SETLOCAL(f->f_nlocals + i, c);
2715 }
2716 }
2717 /* Initialize any that are left */
2718 while (i < f->f_ncells) {
2719 c = PyCell_New(NULL);
2720 if (c == NULL)
2721 goto fail;
2722 SETLOCAL(f->f_nlocals + i, c);
2723 i++;
2724 }
2725 }
2726 if (f->f_nfreevars) {
2727 int i;
2728 for (i = 0; i < f->f_nfreevars; ++i) {
2729 PyObject *o = PyTuple_GET_ITEM(closure, i);
2730 Py_INCREF(o);
2731 freevars[f->f_ncells + i] = o;
2732 }
2733 }
2734
Tim Peters5ca576e2001-06-18 22:08:13 +00002735 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002736 /* Don't need to keep the reference to f_back, it will be set
2737 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002738 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002739 f->f_back = NULL;
2740
Jeremy Hylton985eba52003-02-05 23:13:00 +00002741 PCALL(PCALL_GENERATOR);
2742
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002743 /* Create a new generator that owns the ready to run frame
2744 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002745 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002746 }
2747
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002748 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002749
2750 fail: /* Jump here from prelude on failure */
2751
Tim Petersb13680b2001-11-27 23:29:29 +00002752 /* decref'ing the frame can cause __del__ methods to get invoked,
2753 which can call back into Python. While we're done with the
2754 current Python frame (f), the associated C stack is still in use,
2755 so recursion_depth must be boosted for the duration.
2756 */
2757 assert(tstate != NULL);
2758 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002759 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002760 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002761 return retval;
2762}
2763
2764
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002765/* Implementation notes for set_exc_info() and reset_exc_info():
2766
2767- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2768 'exc_traceback'. These always travel together.
2769
2770- tstate->curexc_ZZZ is the "hot" exception that is set by
2771 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2772
2773- Once an exception is caught by an except clause, it is transferred
2774 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2775 can pick it up. This is the primary task of set_exc_info().
2776
2777- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2778
2779 Long ago, when none of this existed, there were just a few globals:
2780 one set corresponding to the "hot" exception, and one set
2781 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2782 globals; they were simply stored as sys.exc_ZZZ. For backwards
2783 compatibility, they still are!) The problem was that in code like
2784 this:
2785
2786 try:
2787 "something that may fail"
2788 except "some exception":
2789 "do something else first"
2790 "print the exception from sys.exc_ZZZ."
2791
2792 if "do something else first" invoked something that raised and caught
2793 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2794 cause of subtle bugs. I fixed this by changing the semantics as
2795 follows:
2796
2797 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2798 *in that frame*.
2799
2800 - But initially, and as long as no exception is caught in a given
2801 frame, sys.exc_ZZZ will hold the last exception caught in the
2802 previous frame (or the frame before that, etc.).
2803
2804 The first bullet fixed the bug in the above example. The second
2805 bullet was for backwards compatibility: it was (and is) common to
2806 have a function that is called when an exception is caught, and to
2807 have that function access the caught exception via sys.exc_ZZZ.
2808 (Example: traceback.print_exc()).
2809
2810 At the same time I fixed the problem that sys.exc_ZZZ weren't
2811 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2812 but that's really a separate improvement.
2813
2814 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2815 variables to what they were before the current frame was called. The
2816 set_exc_info() function saves them on the frame so that
2817 reset_exc_info() can restore them. The invariant is that
2818 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2819 exception (where "catching" an exception applies only to successful
2820 except clauses); and if the current frame ever caught an exception,
2821 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2822 at the start of the current frame.
2823
2824*/
2825
Guido van Rossuma027efa1997-05-05 20:56:21 +00002826static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002827set_exc_info(PyThreadState *tstate,
2828 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829{
2830 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002831 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002832
Guido van Rossuma027efa1997-05-05 20:56:21 +00002833 frame = tstate->frame;
2834 if (frame->f_exc_type == NULL) {
2835 /* This frame didn't catch an exception before */
2836 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002837 if (tstate->exc_type == NULL) {
2838 Py_INCREF(Py_None);
2839 tstate->exc_type = Py_None;
2840 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002841 tmp_type = frame->f_exc_type;
2842 tmp_value = frame->f_exc_value;
2843 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002844 Py_XINCREF(tstate->exc_type);
2845 Py_XINCREF(tstate->exc_value);
2846 Py_XINCREF(tstate->exc_traceback);
2847 frame->f_exc_type = tstate->exc_type;
2848 frame->f_exc_value = tstate->exc_value;
2849 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002850 Py_XDECREF(tmp_type);
2851 Py_XDECREF(tmp_value);
2852 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002853 }
2854 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002855 tmp_type = tstate->exc_type;
2856 tmp_value = tstate->exc_value;
2857 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002858 Py_XINCREF(type);
2859 Py_XINCREF(value);
2860 Py_XINCREF(tb);
2861 tstate->exc_type = type;
2862 tstate->exc_value = value;
2863 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002864 Py_XDECREF(tmp_type);
2865 Py_XDECREF(tmp_value);
2866 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002867 /* For b/w compatibility */
2868 PySys_SetObject("exc_type", type);
2869 PySys_SetObject("exc_value", value);
2870 PySys_SetObject("exc_traceback", tb);
2871}
2872
2873static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002874reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002875{
2876 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002877 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002878 frame = tstate->frame;
2879 if (frame->f_exc_type != NULL) {
2880 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002881 tmp_type = tstate->exc_type;
2882 tmp_value = tstate->exc_value;
2883 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002884 Py_XINCREF(frame->f_exc_type);
2885 Py_XINCREF(frame->f_exc_value);
2886 Py_XINCREF(frame->f_exc_traceback);
2887 tstate->exc_type = frame->f_exc_type;
2888 tstate->exc_value = frame->f_exc_value;
2889 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002890 Py_XDECREF(tmp_type);
2891 Py_XDECREF(tmp_value);
2892 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002893 /* For b/w compatibility */
2894 PySys_SetObject("exc_type", frame->f_exc_type);
2895 PySys_SetObject("exc_value", frame->f_exc_value);
2896 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2897 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002898 tmp_type = frame->f_exc_type;
2899 tmp_value = frame->f_exc_value;
2900 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002901 frame->f_exc_type = NULL;
2902 frame->f_exc_value = NULL;
2903 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002904 Py_XDECREF(tmp_type);
2905 Py_XDECREF(tmp_value);
2906 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002907}
2908
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002909/* Logic for the raise statement (too complicated for inlining).
2910 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002911static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002912do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002913{
Guido van Rossumd295f121998-04-09 21:39:57 +00002914 if (type == NULL) {
2915 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002916 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002917 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2918 value = tstate->exc_value;
2919 tb = tstate->exc_traceback;
2920 Py_XINCREF(type);
2921 Py_XINCREF(value);
2922 Py_XINCREF(tb);
2923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002924
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002925 /* We support the following forms of raise:
2926 raise <class>, <classinstance>
2927 raise <class>, <argument tuple>
2928 raise <class>, None
2929 raise <class>, <argument>
2930 raise <classinstance>, None
2931 raise <string>, <object>
2932 raise <string>, None
2933
2934 An omitted second argument is the same as None.
2935
2936 In addition, raise <tuple>, <anything> is the same as
2937 raising the tuple's first item (and it better have one!);
2938 this rule is applied recursively.
2939
2940 Finally, an optional third argument can be supplied, which
2941 gives the traceback to be substituted (useful when
2942 re-raising an exception after examining it). */
2943
2944 /* First, check the traceback argument, replacing None with
2945 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002946 if (tb == Py_None) {
2947 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002948 tb = NULL;
2949 }
2950 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002951 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002952 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002953 goto raise_error;
2954 }
2955
2956 /* Next, replace a missing value with None */
2957 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002958 value = Py_None;
2959 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002960 }
2961
2962 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002963 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2964 PyObject *tmp = type;
2965 type = PyTuple_GET_ITEM(type, 0);
2966 Py_INCREF(type);
2967 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002968 }
2969
Tim Petersafb2c802002-04-18 18:06:20 +00002970 if (PyString_CheckExact(type))
2971 /* Raising builtin string is deprecated but still allowed --
2972 * do nothing. Raising an instance of a new-style str
2973 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002974 PyErr_Warn(PyExc_PendingDeprecationWarning,
2975 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002976
2977 else if (PyClass_Check(type))
2978 PyErr_NormalizeException(&type, &value, &tb);
2979
Guido van Rossumb209a111997-04-29 18:18:01 +00002980 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002981 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002982 if (value != Py_None) {
2983 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002984 "instance exception may not have a separate value");
2985 goto raise_error;
2986 }
2987 else {
2988 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002989 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002990 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002991 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2992 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002993 }
2994 }
2995 else {
2996 /* Not something you can raise. You get an exception
2997 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002998 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002999 "exceptions must be classes, instances, or "
3000 "strings (deprecated), not %s",
3001 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003002 goto raise_error;
3003 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003004 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003005 if (tb == NULL)
3006 return WHY_EXCEPTION;
3007 else
3008 return WHY_RERAISE;
3009 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003010 Py_XDECREF(value);
3011 Py_XDECREF(type);
3012 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003013 return WHY_EXCEPTION;
3014}
3015
Tim Petersd6d010b2001-06-21 02:49:55 +00003016/* Iterate v argcnt times and store the results on the stack (via decreasing
3017 sp). Return 1 for success, 0 if error. */
3018
Barry Warsawe42b18f1997-08-25 22:13:04 +00003019static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003020unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003021{
Tim Petersd6d010b2001-06-21 02:49:55 +00003022 int i = 0;
3023 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003024 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003025
Tim Petersd6d010b2001-06-21 02:49:55 +00003026 assert(v != NULL);
3027
3028 it = PyObject_GetIter(v);
3029 if (it == NULL)
3030 goto Error;
3031
3032 for (; i < argcnt; i++) {
3033 w = PyIter_Next(it);
3034 if (w == NULL) {
3035 /* Iterator done, via error or exhaustion. */
3036 if (!PyErr_Occurred()) {
3037 PyErr_Format(PyExc_ValueError,
3038 "need more than %d value%s to unpack",
3039 i, i == 1 ? "" : "s");
3040 }
3041 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003042 }
3043 *--sp = w;
3044 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003045
3046 /* We better have exhausted the iterator now. */
3047 w = PyIter_Next(it);
3048 if (w == NULL) {
3049 if (PyErr_Occurred())
3050 goto Error;
3051 Py_DECREF(it);
3052 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003053 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003054 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003055 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003056 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003057Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003058 for (; i > 0; i--, sp++)
3059 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003060 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003061 return 0;
3062}
3063
3064
Guido van Rossum96a42c81992-01-12 02:29:51 +00003065#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003066static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003067prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003068{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003069 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003070 if (PyObject_Print(v, stdout, 0) != 0)
3071 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003072 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003073 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003074}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003075#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003076
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003077static void
Fred Drake5755ce62001-06-27 19:19:46 +00003078call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003079{
Guido van Rossumb209a111997-04-29 18:18:01 +00003080 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003081 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003082 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003083 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003084 value = Py_None;
3085 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003086 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003087 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003088 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003089 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003090 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003091 }
Fred Drake5755ce62001-06-27 19:19:46 +00003092 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003093 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003094 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003095 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003096 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003097 Py_XDECREF(type);
3098 Py_XDECREF(value);
3099 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003100 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003101}
3102
Fred Drake4ec5d562001-10-04 19:26:43 +00003103static void
3104call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3105 int what)
3106{
3107 PyObject *type, *value, *traceback;
3108 int err;
3109 PyErr_Fetch(&type, &value, &traceback);
3110 err = call_trace(func, obj, frame, what, NULL);
3111 if (err == 0)
3112 PyErr_Restore(type, value, traceback);
3113 else {
3114 Py_XDECREF(type);
3115 Py_XDECREF(value);
3116 Py_XDECREF(traceback);
3117 }
3118}
3119
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003120static int
Fred Drake5755ce62001-06-27 19:19:46 +00003121call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3122 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003123{
Fred Drake5755ce62001-06-27 19:19:46 +00003124 register PyThreadState *tstate = frame->f_tstate;
3125 int result;
3126 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003127 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003128 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003129 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003130 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003131 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3132 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003133 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003134 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003135}
3136
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003137PyObject *
3138_PyEval_CallTracing(PyObject *func, PyObject *args)
3139{
3140 PyFrameObject *frame = PyEval_GetFrame();
3141 PyThreadState *tstate = frame->f_tstate;
3142 int save_tracing = tstate->tracing;
3143 int save_use_tracing = tstate->use_tracing;
3144 PyObject *result;
3145
3146 tstate->tracing = 0;
3147 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3148 || (tstate->c_profilefunc != NULL));
3149 result = PyObject_Call(func, args, NULL);
3150 tstate->tracing = save_tracing;
3151 tstate->use_tracing = save_use_tracing;
3152 return result;
3153}
3154
Michael W. Hudson006c7522002-11-08 13:08:46 +00003155static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003156maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003157 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3158 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003159{
3160 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003161
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003162 In a nutshell, we use the co_lnotab field of the code object
3163 to tell when execution has moved onto a different line.
3164
3165 As mentioned above, the basic idea is so set things up so
3166 that
3167
3168 *instr_lb <= frame->f_lasti < *instr_ub
3169
3170 is true so long as execution does not change lines.
3171
3172 This is all fairly simple. Digging the information out of
3173 co_lnotab takes some work, but is conceptually clear.
3174
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003175 Somewhat harder to explain is why we don't *always* call the
3176 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003177
3178 Consider this code:
3179
3180 1: def f(a):
3181 2: if a:
3182 3: print 1
3183 4: else:
3184 5: print 2
3185
3186 which compiles to this:
3187
3188 2 0 LOAD_FAST 0 (a)
3189 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003190 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003191
3192 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003193 10 PRINT_ITEM
3194 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003195 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003196 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003197
3198 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003199 19 PRINT_ITEM
3200 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003201 >> 21 LOAD_CONST 0 (None)
3202 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003203
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003204 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003205 15 and the co_lnotab will claim that execution has moved to
3206 line 3. This is at best misleading. In this case we could
3207 associate the POP_TOP with line 4, but that doesn't make
3208 sense in all cases (I think).
3209
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003210 What we do is only call the line trace function if the co_lnotab
3211 indicates we have jumped to the *start* of a line, i.e. if the
3212 current instruction offset matches the offset given for the
3213 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003214
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003215 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003216 Execution will jump from instruction offset 12 to offset 21.
3217 Then the co_lnotab would imply that execution has moved to line
3218 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003219
3220 Why do we set f_lineno when tracing? Well, consider the code
3221 above when 'a' is true. If stepping through this with 'n' in
3222 pdb, you would stop at line 1 with a "call" type event, then
3223 line events on lines 2 and 3, then a "return" type event -- but
3224 you would be shown line 5 during this event. This is a change
3225 from the behaviour in 2.2 and before, and I've found it
3226 confusing in practice. By setting and using f_lineno when
3227 tracing, one can report a line number different from that
3228 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003229 */
3230
Michael W. Hudson006c7522002-11-08 13:08:46 +00003231 int result = 0;
3232
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003233 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003234 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003235 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003236 unsigned char* p;
3237
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003238 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3239 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003240
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003241 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003242 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003243
3244 /* possible optimization: if f->f_lasti == instr_ub
3245 (likely to be a common case) then we already know
3246 instr_lb -- if we stored the matching value of p
3247 somwhere we could skip the first while loop. */
3248
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003249 /* see comments in compile.c for the description of
3250 co_lnotab. A point to remember: increments to p
3251 should come in pairs -- although we don't care about
3252 the line increments here, treating them as byte
3253 increments gets confusing, to say the least. */
3254
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003255 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003256 if (addr + *p > frame->f_lasti)
3257 break;
3258 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003259 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003260 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003261 --size;
3262 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003263
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003264 if (addr == frame->f_lasti) {
3265 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003266 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003267 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003268 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003269
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003270 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003271 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003272 addr += *p++;
3273 if (*p++)
3274 break;
3275 }
3276 *instr_ub = addr;
3277 }
3278 else {
3279 *instr_ub = INT_MAX;
3280 }
3281 }
Armin Rigobf57a142004-03-22 19:24:58 +00003282 else if (frame->f_lasti <= *instr_prev) {
3283 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003284 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003285 PyTrace_LINE, Py_None);
3286 }
3287 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003288 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003289}
3290
Fred Drake5755ce62001-06-27 19:19:46 +00003291void
3292PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003293{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003294 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003295 PyObject *temp = tstate->c_profileobj;
3296 Py_XINCREF(arg);
3297 tstate->c_profilefunc = NULL;
3298 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003299 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003300 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003301 Py_XDECREF(temp);
3302 tstate->c_profilefunc = func;
3303 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003304 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003305 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003306}
3307
3308void
3309PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3310{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003311 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003312 PyObject *temp = tstate->c_traceobj;
3313 Py_XINCREF(arg);
3314 tstate->c_tracefunc = NULL;
3315 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003316 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003317 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003318 Py_XDECREF(temp);
3319 tstate->c_tracefunc = func;
3320 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003321 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003322 tstate->use_tracing = ((func != NULL)
3323 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003324}
3325
Guido van Rossumb209a111997-04-29 18:18:01 +00003326PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003327PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003328{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003329 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003330 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003331 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003332 else
3333 return current_frame->f_builtins;
3334}
3335
Guido van Rossumb209a111997-04-29 18:18:01 +00003336PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003337PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003338{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003339 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003340 if (current_frame == NULL)
3341 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003342 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003343 return current_frame->f_locals;
3344}
3345
Guido van Rossumb209a111997-04-29 18:18:01 +00003346PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003347PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003348{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003349 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003350 if (current_frame == NULL)
3351 return NULL;
3352 else
3353 return current_frame->f_globals;
3354}
3355
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003356PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003357PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003358{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003359 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003360 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003361}
3362
Guido van Rossum6135a871995-01-09 17:53:26 +00003363int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003364PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003365{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003366 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003367 return current_frame == NULL ? 0 : current_frame->f_restricted;
3368}
3369
Guido van Rossumbe270261997-05-22 22:26:18 +00003370int
Tim Peters5ba58662001-07-16 02:29:45 +00003371PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003372{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003373 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003374 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003375
3376 if (current_frame != NULL) {
3377 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003378 const int compilerflags = codeflags & PyCF_MASK;
3379 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003380 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003381 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003382 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003383#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003384 if (codeflags & CO_GENERATOR_ALLOWED) {
3385 result = 1;
3386 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3387 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003388#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003389 }
3390 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003391}
3392
3393int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003394Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003395{
Guido van Rossumb209a111997-04-29 18:18:01 +00003396 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003397 if (f == NULL)
3398 return 0;
3399 if (!PyFile_SoftSpace(f, 0))
3400 return 0;
3401 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003402}
3403
Guido van Rossum3f5da241990-12-20 15:06:42 +00003404
Guido van Rossum681d79a1995-07-18 14:51:37 +00003405/* External interface to call any callable object.
3406 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003407
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003408#undef PyEval_CallObject
3409/* for backward compatibility: export this interface */
3410
Guido van Rossumb209a111997-04-29 18:18:01 +00003411PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003412PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003413{
Guido van Rossumb209a111997-04-29 18:18:01 +00003414 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003415}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003416#define PyEval_CallObject(func,arg) \
3417 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003418
Guido van Rossumb209a111997-04-29 18:18:01 +00003419PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003420PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003421{
Jeremy Hylton52820442001-01-03 23:52:36 +00003422 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003423
3424 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003425 arg = PyTuple_New(0);
3426 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003427 PyErr_SetString(PyExc_TypeError,
3428 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003429 return NULL;
3430 }
3431 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003432 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003433
Guido van Rossumb209a111997-04-29 18:18:01 +00003434 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003435 PyErr_SetString(PyExc_TypeError,
3436 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003437 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003438 return NULL;
3439 }
3440
Tim Peters6d6c1a32001-08-02 04:15:00 +00003441 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003442 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003443 return result;
3444}
3445
Tim Peters6d6c1a32001-08-02 04:15:00 +00003446char *
3447PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003448{
3449 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003450 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003451 else if (PyFunction_Check(func))
3452 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3453 else if (PyCFunction_Check(func))
3454 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3455 else if (PyClass_Check(func))
3456 return PyString_AsString(((PyClassObject*)func)->cl_name);
3457 else if (PyInstance_Check(func)) {
3458 return PyString_AsString(
3459 ((PyInstanceObject*)func)->in_class->cl_name);
3460 } else {
3461 return func->ob_type->tp_name;
3462 }
3463}
3464
Tim Peters6d6c1a32001-08-02 04:15:00 +00003465char *
3466PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003467{
3468 if (PyMethod_Check(func))
3469 return "()";
3470 else if (PyFunction_Check(func))
3471 return "()";
3472 else if (PyCFunction_Check(func))
3473 return "()";
3474 else if (PyClass_Check(func))
3475 return " constructor";
3476 else if (PyInstance_Check(func)) {
3477 return " instance";
3478 } else {
3479 return " object";
3480 }
3481}
3482
Jeremy Hylton52820442001-01-03 23:52:36 +00003483#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3484
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003485static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003486err_args(PyObject *func, int flags, int nargs)
3487{
3488 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003489 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003490 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003491 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003492 nargs);
3493 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003494 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003495 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003496 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003497 nargs);
3498}
3499
Nicholas Bastind858a772004-06-25 23:31:06 +00003500#define C_TRACE(call) \
3501if (tstate->use_tracing && tstate->c_profilefunc) { \
3502 if (call_trace(tstate->c_profilefunc, \
3503 tstate->c_profileobj, \
3504 tstate->frame, PyTrace_C_CALL, \
3505 func)) \
3506 { return NULL; } \
3507 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003508 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003509 if (x == NULL) { \
3510 if (call_trace (tstate->c_profilefunc, \
3511 tstate->c_profileobj, \
3512 tstate->frame, PyTrace_C_EXCEPTION, \
3513 func)) \
3514 { return NULL; } \
3515 } else { \
3516 if (call_trace(tstate->c_profilefunc, \
3517 tstate->c_profileobj, \
3518 tstate->frame, PyTrace_C_RETURN, \
3519 func)) \
3520 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003521 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003522 } \
3523} else { \
3524 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003525 }
3526
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003527static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003528call_function(PyObject ***pp_stack, int oparg
3529#ifdef WITH_TSC
3530 , uint64* pintr0, uint64* pintr1
3531#endif
3532 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003533{
3534 int na = oparg & 0xff;
3535 int nk = (oparg>>8) & 0xff;
3536 int n = na + 2 * nk;
3537 PyObject **pfunc = (*pp_stack) - n - 1;
3538 PyObject *func = *pfunc;
3539 PyObject *x, *w;
3540
Jeremy Hylton985eba52003-02-05 23:13:00 +00003541 /* Always dispatch PyCFunction first, because these are
3542 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003543 */
3544 if (PyCFunction_Check(func) && nk == 0) {
3545 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003546 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003547
3548 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003549 if (flags & (METH_NOARGS | METH_O)) {
3550 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3551 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003552 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003553 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003554 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003555 else if (flags & METH_O && na == 1) {
3556 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003557 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003558 Py_DECREF(arg);
3559 }
3560 else {
3561 err_args(func, flags, na);
3562 x = NULL;
3563 }
3564 }
3565 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003566 PyObject *callargs;
3567 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003568 READ_TIMESTAMP(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003569 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003570 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003571 Py_XDECREF(callargs);
3572 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003573 } else {
3574 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3575 /* optimize access to bound methods */
3576 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003577 PCALL(PCALL_METHOD);
3578 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003579 Py_INCREF(self);
3580 func = PyMethod_GET_FUNCTION(func);
3581 Py_INCREF(func);
3582 Py_DECREF(*pfunc);
3583 *pfunc = self;
3584 na++;
3585 n++;
3586 } else
3587 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003588 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003589 if (PyFunction_Check(func))
3590 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003591 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003592 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003593 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003594 Py_DECREF(func);
3595 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003596
Jeremy Hylton985eba52003-02-05 23:13:00 +00003597 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003598 while ((*pp_stack) > pfunc) {
3599 w = EXT_POP(*pp_stack);
3600 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003601 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003602 }
3603 return x;
3604}
3605
Jeremy Hylton192690e2002-08-16 18:36:11 +00003606/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003607 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003608 For the simplest case -- a function that takes only positional
3609 arguments and is called with only positional arguments -- it
3610 inlines the most primitive frame setup code from
3611 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3612 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003613*/
3614
3615static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003616fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003617{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003618 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003619 PyObject *globals = PyFunction_GET_GLOBALS(func);
3620 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3621 PyObject **d = NULL;
3622 int nd = 0;
3623
Jeremy Hylton985eba52003-02-05 23:13:00 +00003624 PCALL(PCALL_FUNCTION);
3625 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003626 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003627 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3628 PyFrameObject *f;
3629 PyObject *retval = NULL;
3630 PyThreadState *tstate = PyThreadState_GET();
3631 PyObject **fastlocals, **stack;
3632 int i;
3633
3634 PCALL(PCALL_FASTER_FUNCTION);
3635 assert(globals != NULL);
3636 /* XXX Perhaps we should create a specialized
3637 PyFrame_New() that doesn't take locals, but does
3638 take builtins without sanity checking them.
3639 */
3640 f = PyFrame_New(tstate, co, globals, NULL);
3641 if (f == NULL)
3642 return NULL;
3643
3644 fastlocals = f->f_localsplus;
3645 stack = (*pp_stack) - n;
3646
3647 for (i = 0; i < n; i++) {
3648 Py_INCREF(*stack);
3649 fastlocals[i] = *stack++;
3650 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003651 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003652 assert(tstate != NULL);
3653 ++tstate->recursion_depth;
3654 Py_DECREF(f);
3655 --tstate->recursion_depth;
3656 return retval;
3657 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003658 if (argdefs != NULL) {
3659 d = &PyTuple_GET_ITEM(argdefs, 0);
3660 nd = ((PyTupleObject *)argdefs)->ob_size;
3661 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003662 return PyEval_EvalCodeEx(co, globals,
3663 (PyObject *)NULL, (*pp_stack)-n, na,
3664 (*pp_stack)-2*nk, nk, d, nd,
3665 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003666}
3667
3668static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003669update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3670 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003671{
3672 PyObject *kwdict = NULL;
3673 if (orig_kwdict == NULL)
3674 kwdict = PyDict_New();
3675 else {
3676 kwdict = PyDict_Copy(orig_kwdict);
3677 Py_DECREF(orig_kwdict);
3678 }
3679 if (kwdict == NULL)
3680 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003681 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003682 int err;
3683 PyObject *value = EXT_POP(*pp_stack);
3684 PyObject *key = EXT_POP(*pp_stack);
3685 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003686 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003687 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003688 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003689 PyEval_GetFuncName(func),
3690 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003691 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003692 Py_DECREF(key);
3693 Py_DECREF(value);
3694 Py_DECREF(kwdict);
3695 return NULL;
3696 }
3697 err = PyDict_SetItem(kwdict, key, value);
3698 Py_DECREF(key);
3699 Py_DECREF(value);
3700 if (err) {
3701 Py_DECREF(kwdict);
3702 return NULL;
3703 }
3704 }
3705 return kwdict;
3706}
3707
3708static PyObject *
3709update_star_args(int nstack, int nstar, PyObject *stararg,
3710 PyObject ***pp_stack)
3711{
3712 PyObject *callargs, *w;
3713
3714 callargs = PyTuple_New(nstack + nstar);
3715 if (callargs == NULL) {
3716 return NULL;
3717 }
3718 if (nstar) {
3719 int i;
3720 for (i = 0; i < nstar; i++) {
3721 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3722 Py_INCREF(a);
3723 PyTuple_SET_ITEM(callargs, nstack + i, a);
3724 }
3725 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003726 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 w = EXT_POP(*pp_stack);
3728 PyTuple_SET_ITEM(callargs, nstack, w);
3729 }
3730 return callargs;
3731}
3732
3733static PyObject *
3734load_args(PyObject ***pp_stack, int na)
3735{
3736 PyObject *args = PyTuple_New(na);
3737 PyObject *w;
3738
3739 if (args == NULL)
3740 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003741 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003742 w = EXT_POP(*pp_stack);
3743 PyTuple_SET_ITEM(args, na, w);
3744 }
3745 return args;
3746}
3747
3748static PyObject *
3749do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3750{
3751 PyObject *callargs = NULL;
3752 PyObject *kwdict = NULL;
3753 PyObject *result = NULL;
3754
3755 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003756 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003757 if (kwdict == NULL)
3758 goto call_fail;
3759 }
3760 callargs = load_args(pp_stack, na);
3761 if (callargs == NULL)
3762 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003763#ifdef CALL_PROFILE
3764 /* At this point, we have to look at the type of func to
3765 update the call stats properly. Do it here so as to avoid
3766 exposing the call stats machinery outside ceval.c
3767 */
3768 if (PyFunction_Check(func))
3769 PCALL(PCALL_FUNCTION);
3770 else if (PyMethod_Check(func))
3771 PCALL(PCALL_METHOD);
3772 else if (PyType_Check(func))
3773 PCALL(PCALL_TYPE);
3774 else
3775 PCALL(PCALL_OTHER);
3776#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003778 call_fail:
3779 Py_XDECREF(callargs);
3780 Py_XDECREF(kwdict);
3781 return result;
3782}
3783
3784static PyObject *
3785ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3786{
3787 int nstar = 0;
3788 PyObject *callargs = NULL;
3789 PyObject *stararg = NULL;
3790 PyObject *kwdict = NULL;
3791 PyObject *result = NULL;
3792
3793 if (flags & CALL_FLAG_KW) {
3794 kwdict = EXT_POP(*pp_stack);
3795 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003796 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003797 "%s%s argument after ** "
3798 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003799 PyEval_GetFuncName(func),
3800 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003801 goto ext_call_fail;
3802 }
3803 }
3804 if (flags & CALL_FLAG_VAR) {
3805 stararg = EXT_POP(*pp_stack);
3806 if (!PyTuple_Check(stararg)) {
3807 PyObject *t = NULL;
3808 t = PySequence_Tuple(stararg);
3809 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003810 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3811 PyErr_Format(PyExc_TypeError,
3812 "%s%s argument after * "
3813 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003814 PyEval_GetFuncName(func),
3815 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003816 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003817 goto ext_call_fail;
3818 }
3819 Py_DECREF(stararg);
3820 stararg = t;
3821 }
3822 nstar = PyTuple_GET_SIZE(stararg);
3823 }
3824 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003825 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003826 if (kwdict == NULL)
3827 goto ext_call_fail;
3828 }
3829 callargs = update_star_args(na, nstar, stararg, pp_stack);
3830 if (callargs == NULL)
3831 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003832#ifdef CALL_PROFILE
3833 /* At this point, we have to look at the type of func to
3834 update the call stats properly. Do it here so as to avoid
3835 exposing the call stats machinery outside ceval.c
3836 */
3837 if (PyFunction_Check(func))
3838 PCALL(PCALL_FUNCTION);
3839 else if (PyMethod_Check(func))
3840 PCALL(PCALL_METHOD);
3841 else if (PyType_Check(func))
3842 PCALL(PCALL_TYPE);
3843 else
3844 PCALL(PCALL_OTHER);
3845#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003846 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003847 ext_call_fail:
3848 Py_XDECREF(callargs);
3849 Py_XDECREF(kwdict);
3850 Py_XDECREF(stararg);
3851 return result;
3852}
3853
Tim Peterscb479e72001-12-16 19:11:44 +00003854/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3855 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3856 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3857*/
Tim Petersb5196382001-12-16 19:44:20 +00003858/* Note: If v is NULL, return success without storing into *pi. This
3859 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3860 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003861*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003862int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003863_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864{
Tim Petersb5196382001-12-16 19:44:20 +00003865 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003866 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003867 if (PyInt_Check(v)) {
3868 x = PyInt_AsLong(v);
3869 } else if (PyLong_Check(v)) {
3870 x = PyLong_AsLong(v);
3871 if (x==-1 && PyErr_Occurred()) {
3872 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003873 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003874
Guido van Rossumac7be682001-01-17 15:42:30 +00003875 if (!PyErr_ExceptionMatches(
3876 PyExc_OverflowError)) {
3877 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003878 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003879 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003880 }
3881
Guido van Rossumac7be682001-01-17 15:42:30 +00003882 /* Clear the OverflowError */
3883 PyErr_Clear();
3884
3885 /* It's an overflow error, so we need to
3886 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003887 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003888 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003889
3890 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003891 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003892 if (long_zero == NULL)
3893 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003894
3895 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003896 cmp = PyObject_RichCompareBool(v, long_zero,
3897 Py_GT);
3898 Py_DECREF(long_zero);
3899 if (cmp < 0)
3900 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003901 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003902 x = INT_MAX;
3903 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003904 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003905 }
3906 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003907 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003908 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003909 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003911 /* Truncate -- very long indices are truncated anyway */
3912 if (x > INT_MAX)
3913 x = INT_MAX;
3914 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003915 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003916 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003917 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003918 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003919}
3920
Guido van Rossum50d756e2001-08-18 17:43:36 +00003921#undef ISINT
3922#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3923
Guido van Rossumb209a111997-04-29 18:18:01 +00003924static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003925apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003926{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003927 PyTypeObject *tp = u->ob_type;
3928 PySequenceMethods *sq = tp->tp_as_sequence;
3929
3930 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3931 int ilow = 0, ihigh = INT_MAX;
3932 if (!_PyEval_SliceIndex(v, &ilow))
3933 return NULL;
3934 if (!_PyEval_SliceIndex(w, &ihigh))
3935 return NULL;
3936 return PySequence_GetSlice(u, ilow, ihigh);
3937 }
3938 else {
3939 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003940 if (slice != NULL) {
3941 PyObject *res = PyObject_GetItem(u, slice);
3942 Py_DECREF(slice);
3943 return res;
3944 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003945 else
3946 return NULL;
3947 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003948}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003949
3950static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003951assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3952 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003954 PyTypeObject *tp = u->ob_type;
3955 PySequenceMethods *sq = tp->tp_as_sequence;
3956
3957 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3958 int ilow = 0, ihigh = INT_MAX;
3959 if (!_PyEval_SliceIndex(v, &ilow))
3960 return -1;
3961 if (!_PyEval_SliceIndex(w, &ihigh))
3962 return -1;
3963 if (x == NULL)
3964 return PySequence_DelSlice(u, ilow, ihigh);
3965 else
3966 return PySequence_SetSlice(u, ilow, ihigh, x);
3967 }
3968 else {
3969 PyObject *slice = PySlice_New(v, w, NULL);
3970 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003971 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003972 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003973 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003974 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003975 res = PyObject_DelItem(u, slice);
3976 Py_DECREF(slice);
3977 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003978 }
3979 else
3980 return -1;
3981 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003982}
3983
Guido van Rossumb209a111997-04-29 18:18:01 +00003984static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003985cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003986{
Guido van Rossumac7be682001-01-17 15:42:30 +00003987 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003988 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003989 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003990 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003991 break;
3992 case PyCmp_IS_NOT:
3993 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003994 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003995 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003996 res = PySequence_Contains(w, v);
3997 if (res < 0)
3998 return NULL;
3999 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004000 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004001 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004002 if (res < 0)
4003 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004004 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004005 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004006 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004007 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004008 break;
4009 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004010 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004011 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004012 v = res ? Py_True : Py_False;
4013 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004014 return v;
4015}
4016
Thomas Wouters52152252000-08-17 22:55:00 +00004017static PyObject *
4018import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004019{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004020 PyObject *x;
4021
4022 x = PyObject_GetAttr(v, name);
4023 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004024 PyErr_Format(PyExc_ImportError,
4025 "cannot import name %.230s",
4026 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004027 }
Thomas Wouters52152252000-08-17 22:55:00 +00004028 return x;
4029}
Guido van Rossumac7be682001-01-17 15:42:30 +00004030
Thomas Wouters52152252000-08-17 22:55:00 +00004031static int
4032import_all_from(PyObject *locals, PyObject *v)
4033{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004034 PyObject *all = PyObject_GetAttrString(v, "__all__");
4035 PyObject *dict, *name, *value;
4036 int skip_leading_underscores = 0;
4037 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004038
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004039 if (all == NULL) {
4040 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4041 return -1; /* Unexpected error */
4042 PyErr_Clear();
4043 dict = PyObject_GetAttrString(v, "__dict__");
4044 if (dict == NULL) {
4045 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4046 return -1;
4047 PyErr_SetString(PyExc_ImportError,
4048 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004049 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004050 }
4051 all = PyMapping_Keys(dict);
4052 Py_DECREF(dict);
4053 if (all == NULL)
4054 return -1;
4055 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004056 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004057
4058 for (pos = 0, err = 0; ; pos++) {
4059 name = PySequence_GetItem(all, pos);
4060 if (name == NULL) {
4061 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4062 err = -1;
4063 else
4064 PyErr_Clear();
4065 break;
4066 }
4067 if (skip_leading_underscores &&
4068 PyString_Check(name) &&
4069 PyString_AS_STRING(name)[0] == '_')
4070 {
4071 Py_DECREF(name);
4072 continue;
4073 }
4074 value = PyObject_GetAttr(v, name);
4075 if (value == NULL)
4076 err = -1;
4077 else
4078 err = PyDict_SetItem(locals, name, value);
4079 Py_DECREF(name);
4080 Py_XDECREF(value);
4081 if (err != 0)
4082 break;
4083 }
4084 Py_DECREF(all);
4085 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004086}
4087
Guido van Rossumb209a111997-04-29 18:18:01 +00004088static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004089build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004090{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004091 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004092
4093 if (PyDict_Check(methods))
4094 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004095 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004096 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004097 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4098 base = PyTuple_GET_ITEM(bases, 0);
4099 metaclass = PyObject_GetAttrString(base, "__class__");
4100 if (metaclass == NULL) {
4101 PyErr_Clear();
4102 metaclass = (PyObject *)base->ob_type;
4103 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004104 }
4105 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004106 else {
4107 PyObject *g = PyEval_GetGlobals();
4108 if (g != NULL && PyDict_Check(g))
4109 metaclass = PyDict_GetItemString(g, "__metaclass__");
4110 if (metaclass == NULL)
4111 metaclass = (PyObject *) &PyClass_Type;
4112 Py_INCREF(metaclass);
4113 }
4114 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4115 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004116 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4117 /* A type error here likely means that the user passed
4118 in a base that was not a class (such the random module
4119 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004120 by augmenting the error message with more information.*/
4121
4122 PyObject *ptype, *pvalue, *ptraceback;
4123
4124 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4125 if (PyString_Check(pvalue)) {
4126 PyObject *newmsg;
4127 newmsg = PyString_FromFormat(
4128 "Error when calling the metaclass bases\n %s",
4129 PyString_AS_STRING(pvalue));
4130 if (newmsg != NULL) {
4131 Py_DECREF(pvalue);
4132 pvalue = newmsg;
4133 }
4134 }
4135 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004136 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004137 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004138}
4139
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004140static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004141exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4142 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004143{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004144 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004145 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004146 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004147
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4149 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004150 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004151 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004152 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004153 locals = PyTuple_GetItem(prog, 2);
4154 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004155 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004156 if (globals == Py_None) {
4157 globals = PyEval_GetGlobals();
4158 if (locals == Py_None) {
4159 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004160 plain = 1;
4161 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004162 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004163 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004164 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004165 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004166 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004167 !PyCode_Check(prog) &&
4168 !PyFile_Check(prog)) {
4169 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004170 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004171 return -1;
4172 }
Fred Drake661ea262000-10-24 19:57:45 +00004173 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004174 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004175 "exec: arg 2 must be a dictionary or None");
4176 return -1;
4177 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004178 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004179 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004180 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004181 return -1;
4182 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004183 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004184 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004185 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004186 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4187 PyErr_SetString(PyExc_TypeError,
4188 "code object passed to exec may not contain free variables");
4189 return -1;
4190 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004191 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004192 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004193 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004194 FILE *fp = PyFile_AsFile(prog);
4195 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004196 PyCompilerFlags cf;
4197 cf.cf_flags = 0;
4198 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004199 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004200 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004201 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004202 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004203 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004204 }
4205 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004206 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004207 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004208 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004209 cf.cf_flags = 0;
4210#ifdef Py_USING_UNICODE
4211 if (PyUnicode_Check(prog)) {
4212 tmp = PyUnicode_AsUTF8String(prog);
4213 if (tmp == NULL)
4214 return -1;
4215 prog = tmp;
4216 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4217 }
4218#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004219 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004220 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004221 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004222 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004223 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004224 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004225 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004226 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004227 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004228 if (plain)
4229 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004230 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004231 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004232 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004233 return 0;
4234}
Guido van Rossum24c13741995-02-14 09:42:43 +00004235
Guido van Rossumac7be682001-01-17 15:42:30 +00004236static void
Paul Prescode68140d2000-08-30 20:25:01 +00004237format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4238{
4239 char *obj_str;
4240
4241 if (!obj)
4242 return;
4243
4244 obj_str = PyString_AsString(obj);
4245 if (!obj_str)
4246 return;
4247
4248 PyErr_Format(exc, format_str, obj_str);
4249}
Guido van Rossum950361c1997-01-24 13:49:28 +00004250
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004251static PyObject *
4252string_concatenate(PyObject *v, PyObject *w,
4253 PyFrameObject *f, unsigned char *next_instr)
4254{
4255 /* This function implements 'variable += expr' when both arguments
4256 are strings. */
4257
4258 if (v->ob_refcnt == 2) {
4259 /* In the common case, there are 2 references to the value
4260 * stored in 'variable' when the += is performed: one on the
4261 * value stack (in 'v') and one still stored in the 'variable'.
4262 * We try to delete the variable now to reduce the refcnt to 1.
4263 */
4264 switch (*next_instr) {
4265 case STORE_FAST:
4266 {
4267 int oparg = PEEKARG();
4268 PyObject **fastlocals = f->f_localsplus;
4269 if (GETLOCAL(oparg) == v)
4270 SETLOCAL(oparg, NULL);
4271 break;
4272 }
4273 case STORE_DEREF:
4274 {
4275 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4276 PyObject *c = freevars[PEEKARG()];
4277 if (PyCell_GET(c) == v)
4278 PyCell_Set(c, NULL);
4279 break;
4280 }
4281 case STORE_NAME:
4282 {
4283 PyObject *names = f->f_code->co_names;
4284 PyObject *name = GETITEM(names, PEEKARG());
4285 PyObject *locals = f->f_locals;
4286 if (PyDict_CheckExact(locals) &&
4287 PyDict_GetItem(locals, name) == v) {
4288 if (PyDict_DelItem(locals, name) != 0) {
4289 PyErr_Clear();
4290 }
4291 }
4292 break;
4293 }
4294 }
4295 }
4296
Armin Rigo618fbf52004-08-07 20:58:32 +00004297 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004298 /* Now we own the last reference to 'v', so we can resize it
4299 * in-place.
4300 */
4301 int v_len = PyString_GET_SIZE(v);
4302 int w_len = PyString_GET_SIZE(w);
4303 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4304 /* XXX if _PyString_Resize() fails, 'v' has been
4305 * deallocated so it cannot be put back into 'variable'.
4306 * The MemoryError is raised when there is no value in
4307 * 'variable', which might (very remotely) be a cause
4308 * of incompatibilities.
4309 */
4310 return NULL;
4311 }
4312 /* copy 'w' into the newly allocated area of 'v' */
4313 memcpy(PyString_AS_STRING(v) + v_len,
4314 PyString_AS_STRING(w), w_len);
4315 return v;
4316 }
4317 else {
4318 /* When in-place resizing is not an option. */
4319 PyString_Concat(&v, w);
4320 return v;
4321 }
4322}
4323
Guido van Rossum950361c1997-01-24 13:49:28 +00004324#ifdef DYNAMIC_EXECUTION_PROFILE
4325
Skip Montanarof118cb12001-10-15 20:51:38 +00004326static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004327getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004328{
4329 int i;
4330 PyObject *l = PyList_New(256);
4331 if (l == NULL) return NULL;
4332 for (i = 0; i < 256; i++) {
4333 PyObject *x = PyInt_FromLong(a[i]);
4334 if (x == NULL) {
4335 Py_DECREF(l);
4336 return NULL;
4337 }
4338 PyList_SetItem(l, i, x);
4339 }
4340 for (i = 0; i < 256; i++)
4341 a[i] = 0;
4342 return l;
4343}
4344
4345PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004346_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004347{
4348#ifndef DXPAIRS
4349 return getarray(dxp);
4350#else
4351 int i;
4352 PyObject *l = PyList_New(257);
4353 if (l == NULL) return NULL;
4354 for (i = 0; i < 257; i++) {
4355 PyObject *x = getarray(dxpairs[i]);
4356 if (x == NULL) {
4357 Py_DECREF(l);
4358 return NULL;
4359 }
4360 PyList_SetItem(l, i, x);
4361 }
4362 return l;
4363#endif
4364}
4365
4366#endif